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