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