Merge remote-tracking branch 'lsk/v3.10/topic/arm64-fvp' into linux-linaro-lsk
[firefly-linux-kernel-4.4.55.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 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
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
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  */
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
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
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
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
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
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
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
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
217 static struct imx_drm_device *__imx_drm_device(void)
218 {
219         return imx_drm_device;
220 }
221
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
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
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  */
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  */
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  */
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  */
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  */
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  */
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
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  */
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  */
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  */
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
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
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 EXPORT_SYMBOL_GPL(imx_drm_encoder_get_mux_id);
685
686 /*
687  * imx_drm_remove_encoder - remove an encoder
688  */
689 int imx_drm_remove_encoder(struct imx_drm_encoder *imx_drm_encoder)
690 {
691         struct imx_drm_device *imxdrm = __imx_drm_device();
692         struct crtc_cookie *c, *tmp;
693
694         mutex_lock(&imxdrm->mutex);
695
696         imx_drm_encoder_unregister(imx_drm_encoder);
697
698         list_del(&imx_drm_encoder->list);
699
700         list_for_each_entry_safe(c, tmp, &imx_drm_encoder->possible_crtcs,
701                         list)
702                 kfree(c);
703
704         mutex_unlock(&imxdrm->mutex);
705
706         kfree(imx_drm_encoder);
707
708         return 0;
709 }
710 EXPORT_SYMBOL_GPL(imx_drm_remove_encoder);
711
712 /*
713  * imx_drm_add_connector - add a connector
714  */
715 int imx_drm_add_connector(struct drm_connector *connector,
716                 struct imx_drm_connector **new_con,
717                 struct module *owner)
718 {
719         struct imx_drm_device *imxdrm = __imx_drm_device();
720         struct imx_drm_connector *imx_drm_connector;
721         int ret;
722
723         mutex_lock(&imxdrm->mutex);
724
725         if (imxdrm->references) {
726                 ret = -EBUSY;
727                 goto err_busy;
728         }
729
730         imx_drm_connector = kzalloc(sizeof(*imx_drm_connector), GFP_KERNEL);
731         if (!imx_drm_connector) {
732                 ret = -ENOMEM;
733                 goto err_alloc;
734         }
735
736         imx_drm_connector->connector = connector;
737         imx_drm_connector->owner = owner;
738
739         ret = imx_drm_connector_register(imx_drm_connector);
740         if (ret)
741                 goto err_register;
742
743         list_add_tail(&imx_drm_connector->list, &imxdrm->connector_list);
744
745         *new_con = imx_drm_connector;
746
747         mutex_unlock(&imxdrm->mutex);
748
749         return 0;
750
751 err_register:
752         kfree(imx_drm_connector);
753 err_alloc:
754 err_busy:
755         mutex_unlock(&imxdrm->mutex);
756
757         return ret;
758 }
759 EXPORT_SYMBOL_GPL(imx_drm_add_connector);
760
761 void imx_drm_fb_helper_set(struct drm_fbdev_cma *fbdev_helper)
762 {
763         struct imx_drm_device *imxdrm = __imx_drm_device();
764
765         imxdrm->fbhelper = fbdev_helper;
766 }
767 EXPORT_SYMBOL_GPL(imx_drm_fb_helper_set);
768
769 /*
770  * imx_drm_remove_connector - remove a connector
771  */
772 int imx_drm_remove_connector(struct imx_drm_connector *imx_drm_connector)
773 {
774         struct imx_drm_device *imxdrm = __imx_drm_device();
775
776         mutex_lock(&imxdrm->mutex);
777
778         imx_drm_connector_unregister(imx_drm_connector);
779
780         list_del(&imx_drm_connector->list);
781
782         mutex_unlock(&imxdrm->mutex);
783
784         kfree(imx_drm_connector);
785
786         return 0;
787 }
788 EXPORT_SYMBOL_GPL(imx_drm_remove_connector);
789
790 static struct drm_ioctl_desc imx_drm_ioctls[] = {
791         /* none so far */
792 };
793
794 static struct drm_driver imx_drm_driver = {
795         .driver_features        = DRIVER_MODESET | DRIVER_GEM,
796         .load                   = imx_drm_driver_load,
797         .unload                 = imx_drm_driver_unload,
798         .firstopen              = imx_drm_driver_firstopen,
799         .lastclose              = imx_drm_driver_lastclose,
800         .gem_free_object        = drm_gem_cma_free_object,
801         .gem_vm_ops             = &drm_gem_cma_vm_ops,
802         .dumb_create            = drm_gem_cma_dumb_create,
803         .dumb_map_offset        = drm_gem_cma_dumb_map_offset,
804         .dumb_destroy           = drm_gem_cma_dumb_destroy,
805
806         .get_vblank_counter     = drm_vblank_count,
807         .enable_vblank          = imx_drm_enable_vblank,
808         .disable_vblank         = imx_drm_disable_vblank,
809         .ioctls                 = imx_drm_ioctls,
810         .num_ioctls             = ARRAY_SIZE(imx_drm_ioctls),
811         .fops                   = &imx_drm_driver_fops,
812         .name                   = "imx-drm",
813         .desc                   = "i.MX DRM graphics",
814         .date                   = "20120507",
815         .major                  = 1,
816         .minor                  = 0,
817         .patchlevel             = 0,
818 };
819
820 static int imx_drm_platform_probe(struct platform_device *pdev)
821 {
822         imx_drm_device->dev = &pdev->dev;
823
824         return drm_platform_init(&imx_drm_driver, pdev);
825 }
826
827 static int imx_drm_platform_remove(struct platform_device *pdev)
828 {
829         drm_platform_exit(&imx_drm_driver, pdev);
830
831         return 0;
832 }
833
834 static struct platform_driver imx_drm_pdrv = {
835         .probe          = imx_drm_platform_probe,
836         .remove         = imx_drm_platform_remove,
837         .driver         = {
838                 .owner  = THIS_MODULE,
839                 .name   = "imx-drm",
840         },
841 };
842
843 static struct platform_device *imx_drm_pdev;
844
845 static int __init imx_drm_init(void)
846 {
847         int ret;
848
849         imx_drm_device = kzalloc(sizeof(*imx_drm_device), GFP_KERNEL);
850         if (!imx_drm_device)
851                 return -ENOMEM;
852
853         mutex_init(&imx_drm_device->mutex);
854         INIT_LIST_HEAD(&imx_drm_device->crtc_list);
855         INIT_LIST_HEAD(&imx_drm_device->connector_list);
856         INIT_LIST_HEAD(&imx_drm_device->encoder_list);
857
858         imx_drm_pdev = platform_device_register_simple("imx-drm", -1, NULL, 0);
859         if (!imx_drm_pdev) {
860                 ret = -EINVAL;
861                 goto err_pdev;
862         }
863
864         imx_drm_pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32),
865
866         ret = platform_driver_register(&imx_drm_pdrv);
867         if (ret)
868                 goto err_pdrv;
869
870         return 0;
871
872 err_pdrv:
873         platform_device_unregister(imx_drm_pdev);
874 err_pdev:
875         kfree(imx_drm_device);
876
877         return ret;
878 }
879
880 static void __exit imx_drm_exit(void)
881 {
882         platform_device_unregister(imx_drm_pdev);
883         platform_driver_unregister(&imx_drm_pdrv);
884
885         kfree(imx_drm_device);
886 }
887
888 module_init(imx_drm_init);
889 module_exit(imx_drm_exit);
890
891 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
892 MODULE_DESCRIPTION("i.MX drm driver core");
893 MODULE_LICENSE("GPL");