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