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