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