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