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