1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Virtual vop driver based on vkms
4 *
5 */
6
7 #include <linux/module.h>
8 #include <linux/component.h>
9 #include <linux/platform_device.h>
10 #include <drm/drm_gem.h>
11 #include <drm/drm_atomic_helper.h>
12 #include <drm/drm_crtc_helper.h>
13 #include <drm/drm_gem_framebuffer_helper.h>
14 #include <drm/drm_plane_helper.h>
15 #include <drm/drm_probe_helper.h>
16 #include <drm/drm_vblank.h>
17
18 #define DRIVER_NAME "virtual-vop"
19
20 #define XRES_MIN 32
21 #define YRES_MIN 32
22
23 #define XRES_DEF 1024
24 #define YRES_DEF 768
25
26 #define XRES_MAX 8192
27 #define YRES_MAX 8192
28
29
30 struct vvop {
31 struct device *dev;
32 struct drm_device *drm_dev;
33 struct platform_device *pdev;
34 struct drm_crtc crtc;
35 struct drm_plane *plane;
36 struct drm_encoder encoder;
37 struct drm_connector connector;
38 struct hrtimer vblank_hrtimer;
39 ktime_t period_ns;
40 struct drm_pending_vblank_event *event;
41
42 };
43
44 static const u32 vvop_formats[] = {
45 DRM_FORMAT_XRGB8888,
46 };
47
48 #define drm_crtc_to_vvop(crtc) \
49 container_of(crtc, struct vvop, crtc)
50
51
52 static const struct drm_plane_funcs vvop_plane_funcs = {
53 .update_plane = drm_atomic_helper_update_plane,
54 .disable_plane = drm_atomic_helper_disable_plane,
55 .destroy = drm_plane_cleanup,
56 .reset = drm_atomic_helper_plane_reset,
57 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
58 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
59 };
60
vvop_plane_atomic_update(struct drm_plane * plane,struct drm_plane_state * old_state)61 static void vvop_plane_atomic_update(struct drm_plane *plane,
62 struct drm_plane_state *old_state)
63 {
64 }
65
66 static const struct drm_plane_helper_funcs vvop_plane_helper_funcs = {
67 .atomic_update = vvop_plane_atomic_update,
68 };
69
vvop_plane_init(struct vvop * vvop)70 static struct drm_plane *vvop_plane_init(struct vvop *vvop)
71 {
72 struct drm_device *dev = vvop->drm_dev;
73 struct drm_plane *plane;
74 const u32 *formats;
75 int ret, nformats;
76
77 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
78 if (!plane)
79 return ERR_PTR(-ENOMEM);
80
81 formats = vvop_formats;
82 nformats = ARRAY_SIZE(vvop_formats);
83
84 ret = drm_universal_plane_init(dev, plane, 0,
85 &vvop_plane_funcs,
86 formats, nformats,
87 NULL, DRM_PLANE_TYPE_PRIMARY, NULL);
88 if (ret) {
89 kfree(plane);
90 return ERR_PTR(ret);
91 }
92
93 drm_plane_helper_add(plane, &vvop_plane_helper_funcs);
94
95 return plane;
96 }
97
vvop_vblank_simulate(struct hrtimer * timer)98 static enum hrtimer_restart vvop_vblank_simulate(struct hrtimer *timer)
99 {
100 struct vvop *vvop = container_of(timer, struct vvop, vblank_hrtimer);
101 struct drm_crtc *crtc = &vvop->crtc;
102 bool ret;
103
104 ret = drm_crtc_handle_vblank(crtc);
105 if (!ret)
106 DRM_ERROR("vvop failure on handling vblank");
107
108 hrtimer_forward_now(&vvop->vblank_hrtimer, vvop->period_ns);
109
110 return HRTIMER_RESTART;
111 }
112
vvop_enable_vblank(struct drm_crtc * crtc)113 static int vvop_enable_vblank(struct drm_crtc *crtc)
114 {
115 struct drm_device *dev = crtc->dev;
116 unsigned int pipe = drm_crtc_index(crtc);
117 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
118 struct vvop *vvop = drm_crtc_to_vvop(crtc);
119
120 drm_calc_timestamping_constants(crtc, &crtc->mode);
121
122 hrtimer_init(&vvop->vblank_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
123 vvop->vblank_hrtimer.function = &vvop_vblank_simulate;
124 vvop->period_ns = ktime_set(0, vblank->framedur_ns);
125 hrtimer_start(&vvop->vblank_hrtimer, vvop->period_ns, HRTIMER_MODE_REL);
126
127 return 0;
128 }
129
vvop_disable_vblank(struct drm_crtc * crtc)130 static void vvop_disable_vblank(struct drm_crtc *crtc)
131 {
132 struct vvop *vvop = drm_crtc_to_vvop(crtc);
133
134 hrtimer_cancel(&vvop->vblank_hrtimer);
135 }
136
vvop_connector_destroy(struct drm_connector * connector)137 static void vvop_connector_destroy(struct drm_connector *connector)
138 {
139 drm_connector_unregister(connector);
140 drm_connector_cleanup(connector);
141 }
142
143 static const struct drm_connector_funcs vvop_connector_funcs = {
144 .fill_modes = drm_helper_probe_single_connector_modes,
145 .destroy = vvop_connector_destroy,
146 .reset = drm_atomic_helper_connector_reset,
147 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
148 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
149 };
150
151 static const struct drm_encoder_funcs vvop_encoder_funcs = {
152 .destroy = drm_encoder_cleanup,
153 };
154
vvop_conn_get_modes(struct drm_connector * connector)155 static int vvop_conn_get_modes(struct drm_connector *connector)
156 {
157 int count;
158
159 count = drm_add_modes_noedid(connector, XRES_MAX, YRES_MAX);
160 drm_set_preferred_mode(connector, XRES_DEF, YRES_DEF);
161
162 return count;
163 }
164
165 static const struct drm_connector_helper_funcs vvop_conn_helper_funcs = {
166 .get_modes = vvop_conn_get_modes,
167 };
168
169 static const struct drm_crtc_funcs vvop_crtc_funcs = {
170 .set_config = drm_atomic_helper_set_config,
171 .destroy = drm_crtc_cleanup,
172 .page_flip = drm_atomic_helper_page_flip,
173 .reset = drm_atomic_helper_crtc_reset,
174 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
175 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
176 .enable_vblank = vvop_enable_vblank,
177 .disable_vblank = vvop_disable_vblank,
178 };
179
vvop_crtc_atomic_enable(struct drm_crtc * crtc,struct drm_crtc_state * old_state)180 static void vvop_crtc_atomic_enable(struct drm_crtc *crtc,
181 struct drm_crtc_state *old_state)
182 {
183 drm_crtc_vblank_on(crtc);
184 }
185
vvop_crtc_atomic_disable(struct drm_crtc * crtc,struct drm_crtc_state * old_state)186 static void vvop_crtc_atomic_disable(struct drm_crtc *crtc,
187 struct drm_crtc_state *old_state)
188 {
189 unsigned long flags;
190
191 drm_crtc_vblank_off(crtc);
192 if (crtc->state->event && !crtc->state->active) {
193 spin_lock_irqsave(&crtc->dev->event_lock, flags);
194 drm_crtc_send_vblank_event(crtc, crtc->state->event);
195 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
196
197 crtc->state->event = NULL;
198 }
199
200 }
201
vvop_crtc_atomic_flush(struct drm_crtc * crtc,struct drm_crtc_state * old_crtc_state)202 static void vvop_crtc_atomic_flush(struct drm_crtc *crtc,
203 struct drm_crtc_state *old_crtc_state)
204 {
205 unsigned long flags;
206
207 if (crtc->state->event) {
208 spin_lock_irqsave(&crtc->dev->event_lock, flags);
209
210 if (drm_crtc_vblank_get(crtc) != 0)
211 drm_crtc_send_vblank_event(crtc, crtc->state->event);
212 else
213 drm_crtc_arm_vblank_event(crtc, crtc->state->event);
214
215 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
216
217 crtc->state->event = NULL;
218 }
219 }
220
221 static const struct drm_crtc_helper_funcs vvop_crtc_helper_funcs = {
222 .atomic_flush = vvop_crtc_atomic_flush,
223 .atomic_enable = vvop_crtc_atomic_enable,
224 .atomic_disable = vvop_crtc_atomic_disable,
225 };
226
vvop_crtc_init(struct drm_device * dev,struct drm_crtc * crtc,struct drm_plane * primary,struct drm_plane * cursor)227 static int vvop_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
228 struct drm_plane *primary, struct drm_plane *cursor)
229 {
230 int ret;
231
232 ret = drm_crtc_init_with_planes(dev, crtc, primary, cursor,
233 &vvop_crtc_funcs, NULL);
234 if (ret) {
235 DRM_ERROR("Failed to init CRTC\n");
236 return ret;
237 }
238
239 drm_crtc_helper_add(crtc, &vvop_crtc_helper_funcs);
240
241 return ret;
242 }
243
vvop_bind(struct device * dev,struct device * master,void * data)244 static int vvop_bind(struct device *dev, struct device *master, void *data)
245 {
246 struct drm_device *drm_dev = data;
247 struct drm_connector *connector;
248 struct drm_encoder *encoder;
249 struct drm_plane *primary;
250 struct drm_crtc *crtc;
251 struct vvop *vvop;
252 int ret;
253
254 vvop = devm_kzalloc(dev, sizeof(*vvop), GFP_KERNEL);
255 if (!vvop)
256 return -ENOMEM;
257
258 vvop->dev = dev;
259 vvop->drm_dev = drm_dev;
260 connector = &vvop->connector;
261 encoder = &vvop->encoder;
262 crtc = &vvop->crtc;
263
264 dev_set_drvdata(dev, vvop);
265
266 primary = vvop_plane_init(vvop);
267 if (IS_ERR(primary))
268 return PTR_ERR(primary);
269 vvop->plane = primary;
270
271 ret = vvop_crtc_init(drm_dev, crtc, primary, NULL);
272 if (ret)
273 goto err_crtc;
274
275 ret = drm_connector_init(drm_dev, connector, &vvop_connector_funcs,
276 DRM_MODE_CONNECTOR_VIRTUAL);
277 if (ret) {
278 DRM_ERROR("Failed to init connector\n");
279 goto err_connector;
280 }
281
282 drm_connector_helper_add(connector, &vvop_conn_helper_funcs);
283
284 ret = drm_connector_register(connector);
285 if (ret) {
286 DRM_ERROR("Failed to register connector\n");
287 goto err_connector_register;
288 }
289
290 ret = drm_encoder_init(drm_dev, encoder, &vvop_encoder_funcs,
291 DRM_MODE_ENCODER_VIRTUAL, NULL);
292 if (ret) {
293 DRM_ERROR("Failed to init encoder\n");
294 goto err_encoder;
295 }
296 encoder->possible_crtcs = 1;
297
298 ret = drm_connector_attach_encoder(connector, encoder);
299 if (ret) {
300 DRM_ERROR("Failed to attach connector to encoder\n");
301 goto err_attach;
302 }
303
304 return 0;
305
306 err_attach:
307 drm_encoder_cleanup(encoder);
308
309 err_encoder:
310 drm_connector_unregister(connector);
311
312 err_connector_register:
313 drm_connector_cleanup(connector);
314
315 err_connector:
316 drm_crtc_cleanup(crtc);
317
318 err_crtc:
319 drm_plane_cleanup(primary);
320
321 return ret;
322 }
323
vvop_unbind(struct device * dev,struct device * master,void * data)324 static void vvop_unbind(struct device *dev, struct device *master, void *data)
325 {
326 struct vvop *vvop = dev_get_drvdata(dev);
327
328 drm_plane_cleanup(vvop->plane);
329 drm_connector_cleanup(&vvop->connector);
330 drm_crtc_cleanup(&vvop->crtc);
331 }
332
333 const struct component_ops vvop_component_ops = {
334 .bind = vvop_bind,
335 .unbind = vvop_unbind,
336 };
337
vvop_probe(struct platform_device * pdev)338 static int vvop_probe(struct platform_device *pdev)
339 {
340 struct device *dev = &pdev->dev;
341
342 DRM_DEV_INFO(dev, "virtual vop probe\n");
343
344 return component_add(dev, &vvop_component_ops);
345 }
346
vvop_remove(struct platform_device * pdev)347 static int vvop_remove(struct platform_device *pdev)
348 {
349 component_del(&pdev->dev, &vvop_component_ops);
350
351 return 0;
352 }
353
354
355 struct platform_driver vvop_platform_driver = {
356 .probe = vvop_probe,
357 .remove = vvop_remove,
358 .driver = {
359 .name = DRIVER_NAME,
360 },
361 };
362
vvop_init(void)363 static int __init vvop_init(void)
364 {
365 struct platform_device *pdev;
366
367 pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
368 if (IS_ERR(pdev)) {
369 DRM_ERROR("failed to register platform device %s\n", DRIVER_NAME);
370 return PTR_ERR(pdev);
371 }
372
373 return 0;
374 }
375
vvop_exit(void)376 static void __exit vvop_exit(void)
377 {
378 }
379
380 rootfs_initcall(vvop_init);
381 module_exit(vvop_exit);
382
383 MODULE_AUTHOR("Andy Yan <rock-chips@.com>");
384 MODULE_LICENSE("GPL");
385