1 /*
2 * Freescale i.MX drm driver
3 *
4 * Copyright (C) 2011 Sascha Hauer, Pengutronix
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 */
16
17 #include <linux/device.h>
18 #include <linux/platform_device.h>
19 #include <drm/drmP.h>
20 #include <drm/drm_fb_helper.h>
21 #include <drm/drm_crtc_helper.h>
22 #include <linux/fb.h>
23 #include <linux/module.h>
24 #include <drm/drm_gem_cma_helper.h>
25 #include <drm/drm_fb_cma_helper.h>
26
27 #include "imx-drm.h"
28
29 #define MAX_CRTC 4
30
31 struct crtc_cookie {
32 void *cookie;
33 int id;
34 struct list_head list;
35 };
36
37 struct imx_drm_device {
38 struct drm_device *drm;
39 struct device *dev;
40 struct list_head crtc_list;
41 struct list_head encoder_list;
42 struct list_head connector_list;
43 struct mutex mutex;
44 int references;
45 int pipes;
46 struct drm_fbdev_cma *fbhelper;
47 };
48
49 struct imx_drm_crtc {
50 struct drm_crtc *crtc;
51 struct list_head list;
52 struct imx_drm_device *imxdrm;
53 int pipe;
54 struct imx_drm_crtc_helper_funcs imx_drm_helper_funcs;
55 struct module *owner;
56 struct crtc_cookie cookie;
57 };
58
59 struct imx_drm_encoder {
60 struct drm_encoder *encoder;
61 struct list_head list;
62 struct module *owner;
63 struct list_head possible_crtcs;
64 };
65
66 struct imx_drm_connector {
67 struct drm_connector *connector;
68 struct list_head list;
69 struct module *owner;
70 };
71
imx_drm_driver_firstopen(struct drm_device * drm)72 static int imx_drm_driver_firstopen(struct drm_device *drm)
73 {
74 if (!imx_drm_device_get())
75 return -EINVAL;
76
77 return 0;
78 }
79
imx_drm_driver_lastclose(struct drm_device * drm)80 static void imx_drm_driver_lastclose(struct drm_device *drm)
81 {
82 struct imx_drm_device *imxdrm = drm->dev_private;
83
84 if (imxdrm->fbhelper)
85 drm_fbdev_cma_restore_mode(imxdrm->fbhelper);
86
87 imx_drm_device_put();
88 }
89
imx_drm_driver_unload(struct drm_device * drm)90 static int imx_drm_driver_unload(struct drm_device *drm)
91 {
92 struct imx_drm_device *imxdrm = drm->dev_private;
93
94 drm_mode_config_cleanup(imxdrm->drm);
95 drm_kms_helper_poll_fini(imxdrm->drm);
96
97 return 0;
98 }
99
100 /*
101 * We don't care at all for crtc numbers, but the core expects the
102 * crtcs to be numbered
103 */
imx_drm_crtc_by_num(struct imx_drm_device * imxdrm,int num)104 static struct imx_drm_crtc *imx_drm_crtc_by_num(struct imx_drm_device *imxdrm,
105 int num)
106 {
107 struct imx_drm_crtc *imx_drm_crtc;
108
109 list_for_each_entry(imx_drm_crtc, &imxdrm->crtc_list, list)
110 if (imx_drm_crtc->pipe == num)
111 return imx_drm_crtc;
112 return NULL;
113 }
114
imx_drm_crtc_panel_format_pins(struct drm_crtc * crtc,u32 encoder_type,u32 interface_pix_fmt,int hsync_pin,int vsync_pin)115 int imx_drm_crtc_panel_format_pins(struct drm_crtc *crtc, u32 encoder_type,
116 u32 interface_pix_fmt, int hsync_pin, int vsync_pin)
117 {
118 struct imx_drm_device *imxdrm = crtc->dev->dev_private;
119 struct imx_drm_crtc *imx_crtc;
120 struct imx_drm_crtc_helper_funcs *helper;
121
122 mutex_lock(&imxdrm->mutex);
123
124 list_for_each_entry(imx_crtc, &imxdrm->crtc_list, list)
125 if (imx_crtc->crtc == crtc)
126 goto found;
127
128 mutex_unlock(&imxdrm->mutex);
129
130 return -EINVAL;
131 found:
132 mutex_unlock(&imxdrm->mutex);
133
134 helper = &imx_crtc->imx_drm_helper_funcs;
135 if (helper->set_interface_pix_fmt)
136 return helper->set_interface_pix_fmt(crtc,
137 encoder_type, interface_pix_fmt,
138 hsync_pin, vsync_pin);
139 return 0;
140 }
141 EXPORT_SYMBOL_GPL(imx_drm_crtc_panel_format_pins);
142
imx_drm_crtc_panel_format(struct drm_crtc * crtc,u32 encoder_type,u32 interface_pix_fmt)143 int imx_drm_crtc_panel_format(struct drm_crtc *crtc, u32 encoder_type,
144 u32 interface_pix_fmt)
145 {
146 return imx_drm_crtc_panel_format_pins(crtc, encoder_type,
147 interface_pix_fmt, 0, 0);
148 }
149 EXPORT_SYMBOL_GPL(imx_drm_crtc_panel_format);
150
imx_drm_crtc_vblank_get(struct imx_drm_crtc * imx_drm_crtc)151 int imx_drm_crtc_vblank_get(struct imx_drm_crtc *imx_drm_crtc)
152 {
153 return drm_vblank_get(imx_drm_crtc->imxdrm->drm, imx_drm_crtc->pipe);
154 }
155 EXPORT_SYMBOL_GPL(imx_drm_crtc_vblank_get);
156
imx_drm_crtc_vblank_put(struct imx_drm_crtc * imx_drm_crtc)157 void imx_drm_crtc_vblank_put(struct imx_drm_crtc *imx_drm_crtc)
158 {
159 drm_vblank_put(imx_drm_crtc->imxdrm->drm, imx_drm_crtc->pipe);
160 }
161 EXPORT_SYMBOL_GPL(imx_drm_crtc_vblank_put);
162
imx_drm_handle_vblank(struct imx_drm_crtc * imx_drm_crtc)163 void imx_drm_handle_vblank(struct imx_drm_crtc *imx_drm_crtc)
164 {
165 drm_handle_vblank(imx_drm_crtc->imxdrm->drm, imx_drm_crtc->pipe);
166 }
167 EXPORT_SYMBOL_GPL(imx_drm_handle_vblank);
168
imx_drm_enable_vblank(struct drm_device * drm,int crtc)169 static int imx_drm_enable_vblank(struct drm_device *drm, int crtc)
170 {
171 struct imx_drm_device *imxdrm = drm->dev_private;
172 struct imx_drm_crtc *imx_drm_crtc;
173 int ret;
174
175 imx_drm_crtc = imx_drm_crtc_by_num(imxdrm, crtc);
176 if (!imx_drm_crtc)
177 return -EINVAL;
178
179 if (!imx_drm_crtc->imx_drm_helper_funcs.enable_vblank)
180 return -ENOSYS;
181
182 ret = imx_drm_crtc->imx_drm_helper_funcs.enable_vblank(
183 imx_drm_crtc->crtc);
184
185 return ret;
186 }
187
imx_drm_disable_vblank(struct drm_device * drm,int crtc)188 static void imx_drm_disable_vblank(struct drm_device *drm, int crtc)
189 {
190 struct imx_drm_device *imxdrm = drm->dev_private;
191 struct imx_drm_crtc *imx_drm_crtc;
192
193 imx_drm_crtc = imx_drm_crtc_by_num(imxdrm, crtc);
194 if (!imx_drm_crtc)
195 return;
196
197 if (!imx_drm_crtc->imx_drm_helper_funcs.disable_vblank)
198 return;
199
200 imx_drm_crtc->imx_drm_helper_funcs.disable_vblank(imx_drm_crtc->crtc);
201 }
202
203 static const struct file_operations imx_drm_driver_fops = {
204 .owner = THIS_MODULE,
205 .open = drm_open,
206 .release = drm_release,
207 .unlocked_ioctl = drm_ioctl,
208 .mmap = drm_gem_cma_mmap,
209 .poll = drm_poll,
210 .fasync = drm_fasync,
211 .read = drm_read,
212 .llseek = noop_llseek,
213 };
214
215 static struct imx_drm_device *imx_drm_device;
216
__imx_drm_device(void)217 static struct imx_drm_device *__imx_drm_device(void)
218 {
219 return imx_drm_device;
220 }
221
imx_drm_device_get(void)222 struct drm_device *imx_drm_device_get(void)
223 {
224 struct imx_drm_device *imxdrm = __imx_drm_device();
225 struct imx_drm_encoder *enc;
226 struct imx_drm_connector *con;
227 struct imx_drm_crtc *crtc;
228
229 mutex_lock(&imxdrm->mutex);
230
231 list_for_each_entry(enc, &imxdrm->encoder_list, list) {
232 if (!try_module_get(enc->owner)) {
233 dev_err(imxdrm->dev, "could not get module %s\n",
234 module_name(enc->owner));
235 goto unwind_enc;
236 }
237 }
238
239 list_for_each_entry(con, &imxdrm->connector_list, list) {
240 if (!try_module_get(con->owner)) {
241 dev_err(imxdrm->dev, "could not get module %s\n",
242 module_name(con->owner));
243 goto unwind_con;
244 }
245 }
246
247 list_for_each_entry(crtc, &imxdrm->crtc_list, list) {
248 if (!try_module_get(crtc->owner)) {
249 dev_err(imxdrm->dev, "could not get module %s\n",
250 module_name(crtc->owner));
251 goto unwind_crtc;
252 }
253 }
254
255 imxdrm->references++;
256
257 mutex_unlock(&imxdrm->mutex);
258
259 return imxdrm->drm;
260
261 unwind_crtc:
262 list_for_each_entry_continue_reverse(crtc, &imxdrm->crtc_list, list)
263 module_put(crtc->owner);
264 unwind_con:
265 list_for_each_entry_continue_reverse(con, &imxdrm->connector_list, list)
266 module_put(con->owner);
267 unwind_enc:
268 list_for_each_entry_continue_reverse(enc, &imxdrm->encoder_list, list)
269 module_put(enc->owner);
270
271 mutex_unlock(&imxdrm->mutex);
272
273 return NULL;
274
275 }
276 EXPORT_SYMBOL_GPL(imx_drm_device_get);
277
imx_drm_device_put(void)278 void imx_drm_device_put(void)
279 {
280 struct imx_drm_device *imxdrm = __imx_drm_device();
281 struct imx_drm_encoder *enc;
282 struct imx_drm_connector *con;
283 struct imx_drm_crtc *crtc;
284
285 mutex_lock(&imxdrm->mutex);
286
287 list_for_each_entry(crtc, &imxdrm->crtc_list, list)
288 module_put(crtc->owner);
289
290 list_for_each_entry(con, &imxdrm->connector_list, list)
291 module_put(con->owner);
292
293 list_for_each_entry(enc, &imxdrm->encoder_list, list)
294 module_put(enc->owner);
295
296 imxdrm->references--;
297
298 mutex_unlock(&imxdrm->mutex);
299 }
300 EXPORT_SYMBOL_GPL(imx_drm_device_put);
301
drm_mode_group_reinit(struct drm_device * dev)302 static int drm_mode_group_reinit(struct drm_device *dev)
303 {
304 struct drm_mode_group *group = &dev->primary->mode_group;
305 uint32_t *id_list = group->id_list;
306 int ret;
307
308 ret = drm_mode_group_init_legacy_group(dev, group);
309 if (ret < 0)
310 return ret;
311
312 kfree(id_list);
313 return 0;
314 }
315
316 /*
317 * register an encoder to the drm core
318 */
imx_drm_encoder_register(struct imx_drm_encoder * imx_drm_encoder)319 static int imx_drm_encoder_register(struct imx_drm_encoder *imx_drm_encoder)
320 {
321 struct imx_drm_device *imxdrm = __imx_drm_device();
322
323 INIT_LIST_HEAD(&imx_drm_encoder->possible_crtcs);
324
325 drm_encoder_init(imxdrm->drm, imx_drm_encoder->encoder,
326 imx_drm_encoder->encoder->funcs,
327 imx_drm_encoder->encoder->encoder_type);
328
329 drm_mode_group_reinit(imxdrm->drm);
330
331 return 0;
332 }
333
334 /*
335 * unregister an encoder from the drm core
336 */
imx_drm_encoder_unregister(struct imx_drm_encoder * imx_drm_encoder)337 static void imx_drm_encoder_unregister(struct imx_drm_encoder
338 *imx_drm_encoder)
339 {
340 struct imx_drm_device *imxdrm = __imx_drm_device();
341
342 drm_encoder_cleanup(imx_drm_encoder->encoder);
343
344 drm_mode_group_reinit(imxdrm->drm);
345 }
346
347 /*
348 * register a connector to the drm core
349 */
imx_drm_connector_register(struct imx_drm_connector * imx_drm_connector)350 static int imx_drm_connector_register(
351 struct imx_drm_connector *imx_drm_connector)
352 {
353 struct imx_drm_device *imxdrm = __imx_drm_device();
354
355 drm_connector_init(imxdrm->drm, imx_drm_connector->connector,
356 imx_drm_connector->connector->funcs,
357 imx_drm_connector->connector->connector_type);
358 drm_mode_group_reinit(imxdrm->drm);
359
360 return drm_sysfs_connector_add(imx_drm_connector->connector);
361 }
362
363 /*
364 * unregister a connector from the drm core
365 */
imx_drm_connector_unregister(struct imx_drm_connector * imx_drm_connector)366 static void imx_drm_connector_unregister(
367 struct imx_drm_connector *imx_drm_connector)
368 {
369 struct imx_drm_device *imxdrm = __imx_drm_device();
370
371 drm_sysfs_connector_remove(imx_drm_connector->connector);
372 drm_connector_cleanup(imx_drm_connector->connector);
373
374 drm_mode_group_reinit(imxdrm->drm);
375 }
376
377 /*
378 * register a crtc to the drm core
379 */
imx_drm_crtc_register(struct imx_drm_crtc * imx_drm_crtc)380 static int imx_drm_crtc_register(struct imx_drm_crtc *imx_drm_crtc)
381 {
382 struct imx_drm_device *imxdrm = __imx_drm_device();
383 int ret;
384
385 drm_crtc_init(imxdrm->drm, imx_drm_crtc->crtc,
386 imx_drm_crtc->imx_drm_helper_funcs.crtc_funcs);
387 ret = drm_mode_crtc_set_gamma_size(imx_drm_crtc->crtc, 256);
388 if (ret)
389 return ret;
390
391 drm_crtc_helper_add(imx_drm_crtc->crtc,
392 imx_drm_crtc->imx_drm_helper_funcs.crtc_helper_funcs);
393
394 drm_mode_group_reinit(imxdrm->drm);
395
396 return 0;
397 }
398
399 /*
400 * Called by the CRTC driver when all CRTCs are registered. This
401 * puts all the pieces together and initializes the driver.
402 * Once this is called no more CRTCs can be registered since
403 * the drm core has hardcoded the number of crtcs in several
404 * places.
405 */
imx_drm_driver_load(struct drm_device * drm,unsigned long flags)406 static int imx_drm_driver_load(struct drm_device *drm, unsigned long flags)
407 {
408 struct imx_drm_device *imxdrm = __imx_drm_device();
409 int ret;
410
411 imxdrm->drm = drm;
412
413 drm->dev_private = imxdrm;
414
415 /*
416 * enable drm irq mode.
417 * - with irq_enabled = 1, we can use the vblank feature.
418 *
419 * P.S. note that we wouldn't use drm irq handler but
420 * just specific driver own one instead because
421 * drm framework supports only one irq handler and
422 * drivers can well take care of their interrupts
423 */
424 drm->irq_enabled = 1;
425
426 drm_mode_config_init(drm);
427 imx_drm_mode_config_init(drm);
428
429 mutex_lock(&imxdrm->mutex);
430
431 drm_kms_helper_poll_init(imxdrm->drm);
432
433 /* setup the grouping for the legacy output */
434 ret = drm_mode_group_init_legacy_group(imxdrm->drm,
435 &imxdrm->drm->primary->mode_group);
436 if (ret)
437 goto err_init;
438
439 ret = drm_vblank_init(imxdrm->drm, MAX_CRTC);
440 if (ret)
441 goto err_init;
442
443 /*
444 * with vblank_disable_allowed = 1, vblank interrupt will be disabled
445 * by drm timer once a current process gives up ownership of
446 * vblank event.(after drm_vblank_put function is called)
447 */
448 imxdrm->drm->vblank_disable_allowed = 1;
449
450 ret = 0;
451
452 err_init:
453 mutex_unlock(&imxdrm->mutex);
454
455 return ret;
456 }
457
imx_drm_update_possible_crtcs(void)458 static void imx_drm_update_possible_crtcs(void)
459 {
460 struct imx_drm_device *imxdrm = __imx_drm_device();
461 struct imx_drm_crtc *imx_drm_crtc;
462 struct imx_drm_encoder *enc;
463 struct crtc_cookie *cookie;
464
465 list_for_each_entry(enc, &imxdrm->encoder_list, list) {
466 u32 possible_crtcs = 0;
467
468 list_for_each_entry(cookie, &enc->possible_crtcs, list) {
469 list_for_each_entry(imx_drm_crtc, &imxdrm->crtc_list, list) {
470 if (imx_drm_crtc->cookie.cookie == cookie->cookie &&
471 imx_drm_crtc->cookie.id == cookie->id) {
472 possible_crtcs |= 1 << imx_drm_crtc->pipe;
473 }
474 }
475 }
476 enc->encoder->possible_crtcs = possible_crtcs;
477 enc->encoder->possible_clones = possible_crtcs;
478 }
479 }
480
481 /*
482 * imx_drm_add_crtc - add a new crtc
483 *
484 * The return value if !NULL is a cookie for the caller to pass to
485 * imx_drm_remove_crtc later.
486 */
imx_drm_add_crtc(struct drm_crtc * crtc,struct imx_drm_crtc ** new_crtc,const struct imx_drm_crtc_helper_funcs * imx_drm_helper_funcs,struct module * owner,void * cookie,int id)487 int imx_drm_add_crtc(struct drm_crtc *crtc,
488 struct imx_drm_crtc **new_crtc,
489 const struct imx_drm_crtc_helper_funcs *imx_drm_helper_funcs,
490 struct module *owner, void *cookie, int id)
491 {
492 struct imx_drm_device *imxdrm = __imx_drm_device();
493 struct imx_drm_crtc *imx_drm_crtc;
494 const struct drm_crtc_funcs *crtc_funcs;
495 int ret;
496
497 mutex_lock(&imxdrm->mutex);
498
499 if (imxdrm->references) {
500 ret = -EBUSY;
501 goto err_busy;
502 }
503
504 imx_drm_crtc = kzalloc(sizeof(*imx_drm_crtc), GFP_KERNEL);
505 if (!imx_drm_crtc) {
506 ret = -ENOMEM;
507 goto err_alloc;
508 }
509
510 imx_drm_crtc->imx_drm_helper_funcs = *imx_drm_helper_funcs;
511 imx_drm_crtc->pipe = imxdrm->pipes++;
512 imx_drm_crtc->cookie.cookie = cookie;
513 imx_drm_crtc->cookie.id = id;
514
515 crtc_funcs = imx_drm_helper_funcs->crtc_funcs;
516
517 imx_drm_crtc->crtc = crtc;
518 imx_drm_crtc->imxdrm = imxdrm;
519
520 imx_drm_crtc->owner = owner;
521
522 list_add_tail(&imx_drm_crtc->list, &imxdrm->crtc_list);
523
524 *new_crtc = imx_drm_crtc;
525
526 ret = imx_drm_crtc_register(imx_drm_crtc);
527 if (ret)
528 goto err_register;
529
530 imx_drm_update_possible_crtcs();
531
532 mutex_unlock(&imxdrm->mutex);
533
534 return 0;
535
536 err_register:
537 kfree(imx_drm_crtc);
538 err_alloc:
539 err_busy:
540 mutex_unlock(&imxdrm->mutex);
541 return ret;
542 }
543 EXPORT_SYMBOL_GPL(imx_drm_add_crtc);
544
545 /*
546 * imx_drm_remove_crtc - remove a crtc
547 */
imx_drm_remove_crtc(struct imx_drm_crtc * imx_drm_crtc)548 int imx_drm_remove_crtc(struct imx_drm_crtc *imx_drm_crtc)
549 {
550 struct imx_drm_device *imxdrm = imx_drm_crtc->imxdrm;
551
552 mutex_lock(&imxdrm->mutex);
553
554 drm_crtc_cleanup(imx_drm_crtc->crtc);
555
556 list_del(&imx_drm_crtc->list);
557
558 drm_mode_group_reinit(imxdrm->drm);
559
560 mutex_unlock(&imxdrm->mutex);
561
562 kfree(imx_drm_crtc);
563
564 return 0;
565 }
566 EXPORT_SYMBOL_GPL(imx_drm_remove_crtc);
567
568 /*
569 * imx_drm_add_encoder - add a new encoder
570 */
imx_drm_add_encoder(struct drm_encoder * encoder,struct imx_drm_encoder ** newenc,struct module * owner)571 int imx_drm_add_encoder(struct drm_encoder *encoder,
572 struct imx_drm_encoder **newenc, struct module *owner)
573 {
574 struct imx_drm_device *imxdrm = __imx_drm_device();
575 struct imx_drm_encoder *imx_drm_encoder;
576 int ret;
577
578 mutex_lock(&imxdrm->mutex);
579
580 if (imxdrm->references) {
581 ret = -EBUSY;
582 goto err_busy;
583 }
584
585 imx_drm_encoder = kzalloc(sizeof(*imx_drm_encoder), GFP_KERNEL);
586 if (!imx_drm_encoder) {
587 ret = -ENOMEM;
588 goto err_alloc;
589 }
590
591 imx_drm_encoder->encoder = encoder;
592 imx_drm_encoder->owner = owner;
593
594 ret = imx_drm_encoder_register(imx_drm_encoder);
595 if (ret) {
596 ret = -ENOMEM;
597 goto err_register;
598 }
599
600 list_add_tail(&imx_drm_encoder->list, &imxdrm->encoder_list);
601
602 *newenc = imx_drm_encoder;
603
604 mutex_unlock(&imxdrm->mutex);
605
606 return 0;
607
608 err_register:
609 kfree(imx_drm_encoder);
610 err_alloc:
611 err_busy:
612 mutex_unlock(&imxdrm->mutex);
613
614 return ret;
615 }
616 EXPORT_SYMBOL_GPL(imx_drm_add_encoder);
617
imx_drm_encoder_add_possible_crtcs(struct imx_drm_encoder * imx_drm_encoder,struct device_node * np)618 int imx_drm_encoder_add_possible_crtcs(
619 struct imx_drm_encoder *imx_drm_encoder,
620 struct device_node *np)
621 {
622 struct imx_drm_device *imxdrm = __imx_drm_device();
623 struct of_phandle_args args;
624 struct crtc_cookie *c;
625 int ret = 0;
626 int i;
627
628 if (!list_empty(&imx_drm_encoder->possible_crtcs))
629 return -EBUSY;
630
631 for (i = 0; !ret; i++) {
632 ret = of_parse_phandle_with_args(np, "crtcs",
633 "#crtc-cells", i, &args);
634 if (ret < 0)
635 break;
636
637 c = kzalloc(sizeof(*c), GFP_KERNEL);
638 if (!c) {
639 of_node_put(args.np);
640 return -ENOMEM;
641 }
642
643 c->cookie = args.np;
644 c->id = args.args_count > 0 ? args.args[0] : 0;
645
646 of_node_put(args.np);
647
648 mutex_lock(&imxdrm->mutex);
649
650 list_add_tail(&c->list, &imx_drm_encoder->possible_crtcs);
651
652 mutex_unlock(&imxdrm->mutex);
653 }
654
655 imx_drm_update_possible_crtcs();
656
657 return 0;
658 }
659 EXPORT_SYMBOL_GPL(imx_drm_encoder_add_possible_crtcs);
660
imx_drm_encoder_get_mux_id(struct imx_drm_encoder * imx_drm_encoder,struct drm_crtc * crtc)661 int imx_drm_encoder_get_mux_id(struct imx_drm_encoder *imx_drm_encoder,
662 struct drm_crtc *crtc)
663 {
664 struct imx_drm_device *imxdrm = __imx_drm_device();
665 struct imx_drm_crtc *imx_crtc;
666 int i = 0;
667
668 mutex_lock(&imxdrm->mutex);
669
670 list_for_each_entry(imx_crtc, &imxdrm->crtc_list, list) {
671 if (imx_crtc->crtc == crtc)
672 goto found;
673 i++;
674 }
675
676 mutex_unlock(&imxdrm->mutex);
677
678 return -EINVAL;
679 found:
680 mutex_unlock(&imxdrm->mutex);
681
682 return i;
683 }
684
685 /*
686 * imx_drm_remove_encoder - remove an encoder
687 */
imx_drm_remove_encoder(struct imx_drm_encoder * imx_drm_encoder)688 int imx_drm_remove_encoder(struct imx_drm_encoder *imx_drm_encoder)
689 {
690 struct imx_drm_device *imxdrm = __imx_drm_device();
691 struct crtc_cookie *c, *tmp;
692
693 mutex_lock(&imxdrm->mutex);
694
695 imx_drm_encoder_unregister(imx_drm_encoder);
696
697 list_del(&imx_drm_encoder->list);
698
699 list_for_each_entry_safe(c, tmp, &imx_drm_encoder->possible_crtcs,
700 list)
701 kfree(c);
702
703 mutex_unlock(&imxdrm->mutex);
704
705 kfree(imx_drm_encoder);
706
707 return 0;
708 }
709 EXPORT_SYMBOL_GPL(imx_drm_remove_encoder);
710
711 /*
712 * imx_drm_add_connector - add a connector
713 */
imx_drm_add_connector(struct drm_connector * connector,struct imx_drm_connector ** new_con,struct module * owner)714 int imx_drm_add_connector(struct drm_connector *connector,
715 struct imx_drm_connector **new_con,
716 struct module *owner)
717 {
718 struct imx_drm_device *imxdrm = __imx_drm_device();
719 struct imx_drm_connector *imx_drm_connector;
720 int ret;
721
722 mutex_lock(&imxdrm->mutex);
723
724 if (imxdrm->references) {
725 ret = -EBUSY;
726 goto err_busy;
727 }
728
729 imx_drm_connector = kzalloc(sizeof(*imx_drm_connector), GFP_KERNEL);
730 if (!imx_drm_connector) {
731 ret = -ENOMEM;
732 goto err_alloc;
733 }
734
735 imx_drm_connector->connector = connector;
736 imx_drm_connector->owner = owner;
737
738 ret = imx_drm_connector_register(imx_drm_connector);
739 if (ret)
740 goto err_register;
741
742 list_add_tail(&imx_drm_connector->list, &imxdrm->connector_list);
743
744 *new_con = imx_drm_connector;
745
746 mutex_unlock(&imxdrm->mutex);
747
748 return 0;
749
750 err_register:
751 kfree(imx_drm_connector);
752 err_alloc:
753 err_busy:
754 mutex_unlock(&imxdrm->mutex);
755
756 return ret;
757 }
758 EXPORT_SYMBOL_GPL(imx_drm_add_connector);
759
imx_drm_fb_helper_set(struct drm_fbdev_cma * fbdev_helper)760 void imx_drm_fb_helper_set(struct drm_fbdev_cma *fbdev_helper)
761 {
762 struct imx_drm_device *imxdrm = __imx_drm_device();
763
764 imxdrm->fbhelper = fbdev_helper;
765 }
766 EXPORT_SYMBOL_GPL(imx_drm_fb_helper_set);
767
768 /*
769 * imx_drm_remove_connector - remove a connector
770 */
imx_drm_remove_connector(struct imx_drm_connector * imx_drm_connector)771 int imx_drm_remove_connector(struct imx_drm_connector *imx_drm_connector)
772 {
773 struct imx_drm_device *imxdrm = __imx_drm_device();
774
775 mutex_lock(&imxdrm->mutex);
776
777 imx_drm_connector_unregister(imx_drm_connector);
778
779 list_del(&imx_drm_connector->list);
780
781 mutex_unlock(&imxdrm->mutex);
782
783 kfree(imx_drm_connector);
784
785 return 0;
786 }
787 EXPORT_SYMBOL_GPL(imx_drm_remove_connector);
788
789 static struct drm_ioctl_desc imx_drm_ioctls[] = {
790 /* none so far */
791 };
792
793 static struct drm_driver imx_drm_driver = {
794 .driver_features = DRIVER_MODESET | DRIVER_GEM,
795 .load = imx_drm_driver_load,
796 .unload = imx_drm_driver_unload,
797 .firstopen = imx_drm_driver_firstopen,
798 .lastclose = imx_drm_driver_lastclose,
799 .gem_free_object = drm_gem_cma_free_object,
800 .gem_vm_ops = &drm_gem_cma_vm_ops,
801 .dumb_create = drm_gem_cma_dumb_create,
802 .dumb_map_offset = drm_gem_cma_dumb_map_offset,
803 .dumb_destroy = drm_gem_cma_dumb_destroy,
804
805 .get_vblank_counter = drm_vblank_count,
806 .enable_vblank = imx_drm_enable_vblank,
807 .disable_vblank = imx_drm_disable_vblank,
808 .ioctls = imx_drm_ioctls,
809 .num_ioctls = ARRAY_SIZE(imx_drm_ioctls),
810 .fops = &imx_drm_driver_fops,
811 .name = "imx-drm",
812 .desc = "i.MX DRM graphics",
813 .date = "20120507",
814 .major = 1,
815 .minor = 0,
816 .patchlevel = 0,
817 };
818
imx_drm_platform_probe(struct platform_device * pdev)819 static int imx_drm_platform_probe(struct platform_device *pdev)
820 {
821 imx_drm_device->dev = &pdev->dev;
822
823 return drm_platform_init(&imx_drm_driver, pdev);
824 }
825
imx_drm_platform_remove(struct platform_device * pdev)826 static int imx_drm_platform_remove(struct platform_device *pdev)
827 {
828 drm_platform_exit(&imx_drm_driver, pdev);
829
830 return 0;
831 }
832
833 static struct platform_driver imx_drm_pdrv = {
834 .probe = imx_drm_platform_probe,
835 .remove = imx_drm_platform_remove,
836 .driver = {
837 .owner = THIS_MODULE,
838 .name = "imx-drm",
839 },
840 };
841
842 static struct platform_device *imx_drm_pdev;
843
imx_drm_init(void)844 static int __init imx_drm_init(void)
845 {
846 int ret;
847
848 imx_drm_device = kzalloc(sizeof(*imx_drm_device), GFP_KERNEL);
849 if (!imx_drm_device)
850 return -ENOMEM;
851
852 mutex_init(&imx_drm_device->mutex);
853 INIT_LIST_HEAD(&imx_drm_device->crtc_list);
854 INIT_LIST_HEAD(&imx_drm_device->connector_list);
855 INIT_LIST_HEAD(&imx_drm_device->encoder_list);
856
857 imx_drm_pdev = platform_device_register_simple("imx-drm", -1, NULL, 0);
858 if (!imx_drm_pdev) {
859 ret = -EINVAL;
860 goto err_pdev;
861 }
862
863 imx_drm_pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32),
864
865 ret = platform_driver_register(&imx_drm_pdrv);
866 if (ret)
867 goto err_pdrv;
868
869 return 0;
870
871 err_pdrv:
872 platform_device_unregister(imx_drm_pdev);
873 err_pdev:
874 kfree(imx_drm_device);
875
876 return ret;
877 }
878
imx_drm_exit(void)879 static void __exit imx_drm_exit(void)
880 {
881 platform_device_unregister(imx_drm_pdev);
882 platform_driver_unregister(&imx_drm_pdrv);
883
884 kfree(imx_drm_device);
885 }
886
887 module_init(imx_drm_init);
888 module_exit(imx_drm_exit);
889
890 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
891 MODULE_DESCRIPTION("i.MX drm driver core");
892 MODULE_LICENSE("GPL");
893