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