2 * Freescale i.MX drm driver
4 * Copyright (C) 2011 Sascha Hauer, Pengutronix
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.
17 #include <linux/device.h>
18 #include <linux/platform_device.h>
20 #include <drm/drm_fb_helper.h>
21 #include <drm/drm_crtc_helper.h>
23 #include <linux/module.h>
24 #include <drm/drm_gem_cma_helper.h>
25 #include <drm/drm_fb_cma_helper.h>
34 struct list_head list;
37 struct imx_drm_device {
38 struct drm_device *drm;
40 struct list_head crtc_list;
41 struct list_head encoder_list;
42 struct list_head connector_list;
45 struct drm_fbdev_cma *fbhelper;
49 struct drm_crtc *crtc;
50 struct list_head list;
51 struct imx_drm_device *imxdrm;
53 struct imx_drm_crtc_helper_funcs imx_drm_helper_funcs;
55 struct crtc_cookie cookie;
58 struct imx_drm_encoder {
59 struct drm_encoder *encoder;
60 struct list_head list;
62 struct list_head possible_crtcs;
65 struct imx_drm_connector {
66 struct drm_connector *connector;
67 struct list_head list;
71 int imx_drm_crtc_id(struct imx_drm_crtc *crtc)
75 EXPORT_SYMBOL_GPL(imx_drm_crtc_id);
77 static void imx_drm_driver_lastclose(struct drm_device *drm)
79 struct imx_drm_device *imxdrm = drm->dev_private;
82 drm_fbdev_cma_restore_mode(imxdrm->fbhelper);
85 static int imx_drm_driver_unload(struct drm_device *drm)
87 struct imx_drm_device *imxdrm = drm->dev_private;
91 drm_vblank_cleanup(drm);
92 drm_kms_helper_poll_fini(drm);
93 drm_mode_config_cleanup(drm);
99 * We don't care at all for crtc numbers, but the core expects the
100 * crtcs to be numbered
102 static struct imx_drm_crtc *imx_drm_crtc_by_num(struct imx_drm_device *imxdrm,
105 struct imx_drm_crtc *imx_drm_crtc;
107 list_for_each_entry(imx_drm_crtc, &imxdrm->crtc_list, list)
108 if (imx_drm_crtc->pipe == num)
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)
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;
120 list_for_each_entry(imx_crtc, &imxdrm->crtc_list, list)
121 if (imx_crtc->crtc == crtc)
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);
133 EXPORT_SYMBOL_GPL(imx_drm_crtc_panel_format_pins);
135 int imx_drm_crtc_panel_format(struct drm_crtc *crtc, u32 encoder_type,
136 u32 interface_pix_fmt)
138 return imx_drm_crtc_panel_format_pins(crtc, encoder_type,
139 interface_pix_fmt, 2, 3);
141 EXPORT_SYMBOL_GPL(imx_drm_crtc_panel_format);
143 int imx_drm_crtc_vblank_get(struct imx_drm_crtc *imx_drm_crtc)
145 return drm_vblank_get(imx_drm_crtc->crtc->dev, imx_drm_crtc->pipe);
147 EXPORT_SYMBOL_GPL(imx_drm_crtc_vblank_get);
149 void imx_drm_crtc_vblank_put(struct imx_drm_crtc *imx_drm_crtc)
151 drm_vblank_put(imx_drm_crtc->crtc->dev, imx_drm_crtc->pipe);
153 EXPORT_SYMBOL_GPL(imx_drm_crtc_vblank_put);
155 void imx_drm_handle_vblank(struct imx_drm_crtc *imx_drm_crtc)
157 drm_handle_vblank(imx_drm_crtc->crtc->dev, imx_drm_crtc->pipe);
159 EXPORT_SYMBOL_GPL(imx_drm_handle_vblank);
161 static int imx_drm_enable_vblank(struct drm_device *drm, int crtc)
163 struct imx_drm_device *imxdrm = drm->dev_private;
164 struct imx_drm_crtc *imx_drm_crtc;
167 imx_drm_crtc = imx_drm_crtc_by_num(imxdrm, crtc);
171 if (!imx_drm_crtc->imx_drm_helper_funcs.enable_vblank)
174 ret = imx_drm_crtc->imx_drm_helper_funcs.enable_vblank(
180 static void imx_drm_disable_vblank(struct drm_device *drm, int crtc)
182 struct imx_drm_device *imxdrm = drm->dev_private;
183 struct imx_drm_crtc *imx_drm_crtc;
185 imx_drm_crtc = imx_drm_crtc_by_num(imxdrm, crtc);
189 if (!imx_drm_crtc->imx_drm_helper_funcs.disable_vblank)
192 imx_drm_crtc->imx_drm_helper_funcs.disable_vblank(imx_drm_crtc->crtc);
195 static void imx_drm_driver_preclose(struct drm_device *drm,
196 struct drm_file *file)
200 if (!file->is_master)
203 for (i = 0; i < MAX_CRTC; i++)
204 imx_drm_disable_vblank(drm, i);
207 static const struct file_operations imx_drm_driver_fops = {
208 .owner = THIS_MODULE,
210 .release = drm_release,
211 .unlocked_ioctl = drm_ioctl,
212 .mmap = drm_gem_cma_mmap,
215 .llseek = noop_llseek,
218 static struct imx_drm_device *imx_drm_device;
220 static struct imx_drm_device *__imx_drm_device(void)
222 return imx_drm_device;
225 struct drm_device *imx_drm_device_get(void)
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;
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));
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));
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));
259 list_for_each_entry_continue_reverse(crtc, &imxdrm->crtc_list, list)
260 module_put(crtc->owner);
262 list_for_each_entry_continue_reverse(con, &imxdrm->connector_list, list)
263 module_put(con->owner);
265 list_for_each_entry_continue_reverse(enc, &imxdrm->encoder_list, list)
266 module_put(enc->owner);
268 mutex_unlock(&imxdrm->mutex);
273 EXPORT_SYMBOL_GPL(imx_drm_device_get);
275 void imx_drm_device_put(void)
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;
282 mutex_lock(&imxdrm->mutex);
284 list_for_each_entry(crtc, &imxdrm->crtc_list, list)
285 module_put(crtc->owner);
287 list_for_each_entry(con, &imxdrm->connector_list, list)
288 module_put(con->owner);
290 list_for_each_entry(enc, &imxdrm->encoder_list, list)
291 module_put(enc->owner);
293 mutex_unlock(&imxdrm->mutex);
295 EXPORT_SYMBOL_GPL(imx_drm_device_put);
297 static int drm_mode_group_reinit(struct drm_device *dev)
299 struct drm_mode_group *group = &dev->primary->mode_group;
300 uint32_t *id_list = group->id_list;
303 ret = drm_mode_group_init_legacy_group(dev, group);
312 * register an encoder to the drm core
314 static int imx_drm_encoder_register(struct imx_drm_encoder *imx_drm_encoder)
316 struct imx_drm_device *imxdrm = __imx_drm_device();
318 INIT_LIST_HEAD(&imx_drm_encoder->possible_crtcs);
320 drm_encoder_init(imxdrm->drm, imx_drm_encoder->encoder,
321 imx_drm_encoder->encoder->funcs,
322 imx_drm_encoder->encoder->encoder_type);
324 drm_mode_group_reinit(imxdrm->drm);
330 * unregister an encoder from the drm core
332 static void imx_drm_encoder_unregister(struct imx_drm_encoder
335 struct imx_drm_device *imxdrm = __imx_drm_device();
337 drm_encoder_cleanup(imx_drm_encoder->encoder);
339 drm_mode_group_reinit(imxdrm->drm);
343 * register a connector to the drm core
345 static int imx_drm_connector_register(
346 struct imx_drm_connector *imx_drm_connector)
348 struct imx_drm_device *imxdrm = __imx_drm_device();
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);
355 return drm_sysfs_connector_add(imx_drm_connector->connector);
359 * unregister a connector from the drm core
361 static void imx_drm_connector_unregister(
362 struct imx_drm_connector *imx_drm_connector)
364 struct imx_drm_device *imxdrm = __imx_drm_device();
366 drm_sysfs_connector_remove(imx_drm_connector->connector);
367 drm_connector_cleanup(imx_drm_connector->connector);
369 drm_mode_group_reinit(imxdrm->drm);
373 * Called by the CRTC driver when all CRTCs are registered. This
374 * puts all the pieces together and initializes the driver.
375 * Once this is called no more CRTCs can be registered since
376 * the drm core has hardcoded the number of crtcs in several
379 static int imx_drm_driver_load(struct drm_device *drm, unsigned long flags)
381 struct imx_drm_device *imxdrm = __imx_drm_device();
386 drm->dev_private = imxdrm;
389 * enable drm irq mode.
390 * - with irq_enabled = true, we can use the vblank feature.
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
397 drm->irq_enabled = true;
399 drm_mode_config_init(drm);
400 imx_drm_mode_config_init(drm);
402 mutex_lock(&imxdrm->mutex);
404 drm_kms_helper_poll_init(drm);
406 /* setup the grouping for the legacy output */
407 ret = drm_mode_group_init_legacy_group(drm,
408 &drm->primary->mode_group);
412 ret = drm_vblank_init(drm, MAX_CRTC);
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)
421 drm->vblank_disable_allowed = true;
423 if (!imx_drm_device_get()) {
428 platform_set_drvdata(drm->platformdev, drm);
429 mutex_unlock(&imxdrm->mutex);
433 drm_vblank_cleanup(drm);
435 drm_kms_helper_poll_fini(drm);
436 drm_mode_config_cleanup(drm);
437 mutex_unlock(&imxdrm->mutex);
442 static void imx_drm_update_possible_crtcs(void)
444 struct imx_drm_device *imxdrm = __imx_drm_device();
445 struct imx_drm_crtc *imx_drm_crtc;
446 struct imx_drm_encoder *enc;
447 struct crtc_cookie *cookie;
449 list_for_each_entry(enc, &imxdrm->encoder_list, list) {
450 u32 possible_crtcs = 0;
452 list_for_each_entry(cookie, &enc->possible_crtcs, list) {
453 list_for_each_entry(imx_drm_crtc, &imxdrm->crtc_list, list) {
454 if (imx_drm_crtc->cookie.cookie == cookie->cookie &&
455 imx_drm_crtc->cookie.id == cookie->id) {
456 possible_crtcs |= 1 << imx_drm_crtc->pipe;
460 enc->encoder->possible_crtcs = possible_crtcs;
461 enc->encoder->possible_clones = possible_crtcs;
466 * imx_drm_add_crtc - add a new crtc
468 * The return value if !NULL is a cookie for the caller to pass to
469 * imx_drm_remove_crtc later.
471 int imx_drm_add_crtc(struct drm_crtc *crtc,
472 struct imx_drm_crtc **new_crtc,
473 const struct imx_drm_crtc_helper_funcs *imx_drm_helper_funcs,
474 struct module *owner, void *cookie, int id)
476 struct imx_drm_device *imxdrm = __imx_drm_device();
477 struct imx_drm_crtc *imx_drm_crtc;
480 mutex_lock(&imxdrm->mutex);
483 * The vblank arrays are dimensioned by MAX_CRTC - we can't
484 * pass IDs greater than this to those functions.
486 if (imxdrm->pipes >= MAX_CRTC) {
491 if (imxdrm->drm->open_count) {
496 imx_drm_crtc = kzalloc(sizeof(*imx_drm_crtc), GFP_KERNEL);
502 imx_drm_crtc->imx_drm_helper_funcs = *imx_drm_helper_funcs;
503 imx_drm_crtc->pipe = imxdrm->pipes++;
504 imx_drm_crtc->cookie.cookie = cookie;
505 imx_drm_crtc->cookie.id = id;
507 imx_drm_crtc->crtc = crtc;
508 imx_drm_crtc->imxdrm = imxdrm;
510 imx_drm_crtc->owner = owner;
512 list_add_tail(&imx_drm_crtc->list, &imxdrm->crtc_list);
514 *new_crtc = imx_drm_crtc;
516 ret = drm_mode_crtc_set_gamma_size(imx_drm_crtc->crtc, 256);
520 drm_crtc_helper_add(crtc,
521 imx_drm_crtc->imx_drm_helper_funcs.crtc_helper_funcs);
523 drm_crtc_init(imxdrm->drm, crtc,
524 imx_drm_crtc->imx_drm_helper_funcs.crtc_funcs);
526 drm_mode_group_reinit(imxdrm->drm);
528 imx_drm_update_possible_crtcs();
530 mutex_unlock(&imxdrm->mutex);
535 list_del(&imx_drm_crtc->list);
539 mutex_unlock(&imxdrm->mutex);
542 EXPORT_SYMBOL_GPL(imx_drm_add_crtc);
545 * imx_drm_remove_crtc - remove a crtc
547 int imx_drm_remove_crtc(struct imx_drm_crtc *imx_drm_crtc)
549 struct imx_drm_device *imxdrm = imx_drm_crtc->imxdrm;
551 mutex_lock(&imxdrm->mutex);
553 drm_crtc_cleanup(imx_drm_crtc->crtc);
555 list_del(&imx_drm_crtc->list);
557 drm_mode_group_reinit(imxdrm->drm);
559 mutex_unlock(&imxdrm->mutex);
565 EXPORT_SYMBOL_GPL(imx_drm_remove_crtc);
568 * imx_drm_add_encoder - add a new encoder
570 int imx_drm_add_encoder(struct drm_encoder *encoder,
571 struct imx_drm_encoder **newenc, struct module *owner)
573 struct imx_drm_device *imxdrm = __imx_drm_device();
574 struct imx_drm_encoder *imx_drm_encoder;
577 mutex_lock(&imxdrm->mutex);
579 if (imxdrm->drm->open_count) {
584 imx_drm_encoder = kzalloc(sizeof(*imx_drm_encoder), GFP_KERNEL);
585 if (!imx_drm_encoder) {
590 imx_drm_encoder->encoder = encoder;
591 imx_drm_encoder->owner = owner;
593 ret = imx_drm_encoder_register(imx_drm_encoder);
599 list_add_tail(&imx_drm_encoder->list, &imxdrm->encoder_list);
601 *newenc = imx_drm_encoder;
603 mutex_unlock(&imxdrm->mutex);
608 kfree(imx_drm_encoder);
611 mutex_unlock(&imxdrm->mutex);
615 EXPORT_SYMBOL_GPL(imx_drm_add_encoder);
617 int imx_drm_encoder_add_possible_crtcs(
618 struct imx_drm_encoder *imx_drm_encoder,
619 struct device_node *np)
621 struct imx_drm_device *imxdrm = __imx_drm_device();
622 struct of_phandle_args args;
623 struct crtc_cookie *c;
627 if (!list_empty(&imx_drm_encoder->possible_crtcs))
630 for (i = 0; !ret; i++) {
631 ret = of_parse_phandle_with_args(np, "crtcs",
632 "#crtc-cells", i, &args);
636 c = kzalloc(sizeof(*c), GFP_KERNEL);
638 of_node_put(args.np);
643 c->id = args.args_count > 0 ? args.args[0] : 0;
645 of_node_put(args.np);
647 mutex_lock(&imxdrm->mutex);
649 list_add_tail(&c->list, &imx_drm_encoder->possible_crtcs);
651 mutex_unlock(&imxdrm->mutex);
654 imx_drm_update_possible_crtcs();
658 EXPORT_SYMBOL_GPL(imx_drm_encoder_add_possible_crtcs);
660 int imx_drm_encoder_get_mux_id(struct imx_drm_encoder *imx_drm_encoder,
661 struct drm_crtc *crtc)
663 struct imx_drm_device *imxdrm = __imx_drm_device();
664 struct imx_drm_crtc *imx_crtc;
667 list_for_each_entry(imx_crtc, &imxdrm->crtc_list, list) {
668 if (imx_crtc->crtc == crtc)
677 EXPORT_SYMBOL_GPL(imx_drm_encoder_get_mux_id);
680 * imx_drm_remove_encoder - remove an encoder
682 int imx_drm_remove_encoder(struct imx_drm_encoder *imx_drm_encoder)
684 struct imx_drm_device *imxdrm = __imx_drm_device();
685 struct crtc_cookie *c, *tmp;
687 mutex_lock(&imxdrm->mutex);
689 imx_drm_encoder_unregister(imx_drm_encoder);
691 list_del(&imx_drm_encoder->list);
693 list_for_each_entry_safe(c, tmp, &imx_drm_encoder->possible_crtcs,
697 mutex_unlock(&imxdrm->mutex);
699 kfree(imx_drm_encoder);
703 EXPORT_SYMBOL_GPL(imx_drm_remove_encoder);
706 * imx_drm_add_connector - add a connector
708 int imx_drm_add_connector(struct drm_connector *connector,
709 struct imx_drm_connector **new_con,
710 struct module *owner)
712 struct imx_drm_device *imxdrm = __imx_drm_device();
713 struct imx_drm_connector *imx_drm_connector;
716 mutex_lock(&imxdrm->mutex);
718 if (imxdrm->drm->open_count) {
723 imx_drm_connector = kzalloc(sizeof(*imx_drm_connector), GFP_KERNEL);
724 if (!imx_drm_connector) {
729 imx_drm_connector->connector = connector;
730 imx_drm_connector->owner = owner;
732 ret = imx_drm_connector_register(imx_drm_connector);
736 list_add_tail(&imx_drm_connector->list, &imxdrm->connector_list);
738 *new_con = imx_drm_connector;
740 mutex_unlock(&imxdrm->mutex);
745 kfree(imx_drm_connector);
748 mutex_unlock(&imxdrm->mutex);
752 EXPORT_SYMBOL_GPL(imx_drm_add_connector);
754 void imx_drm_fb_helper_set(struct drm_fbdev_cma *fbdev_helper)
756 struct imx_drm_device *imxdrm = __imx_drm_device();
758 imxdrm->fbhelper = fbdev_helper;
760 EXPORT_SYMBOL_GPL(imx_drm_fb_helper_set);
763 * imx_drm_remove_connector - remove a connector
765 int imx_drm_remove_connector(struct imx_drm_connector *imx_drm_connector)
767 struct imx_drm_device *imxdrm = __imx_drm_device();
769 mutex_lock(&imxdrm->mutex);
771 imx_drm_connector_unregister(imx_drm_connector);
773 list_del(&imx_drm_connector->list);
775 mutex_unlock(&imxdrm->mutex);
777 kfree(imx_drm_connector);
781 EXPORT_SYMBOL_GPL(imx_drm_remove_connector);
783 static const struct drm_ioctl_desc imx_drm_ioctls[] = {
787 static struct drm_driver imx_drm_driver = {
788 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME,
789 .load = imx_drm_driver_load,
790 .unload = imx_drm_driver_unload,
791 .lastclose = imx_drm_driver_lastclose,
792 .preclose = imx_drm_driver_preclose,
793 .gem_free_object = drm_gem_cma_free_object,
794 .gem_vm_ops = &drm_gem_cma_vm_ops,
795 .dumb_create = drm_gem_cma_dumb_create,
796 .dumb_map_offset = drm_gem_cma_dumb_map_offset,
797 .dumb_destroy = drm_gem_dumb_destroy,
799 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
800 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
801 .gem_prime_import = drm_gem_prime_import,
802 .gem_prime_export = drm_gem_prime_export,
803 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
804 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
805 .gem_prime_vmap = drm_gem_cma_prime_vmap,
806 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
807 .gem_prime_mmap = drm_gem_cma_prime_mmap,
808 .get_vblank_counter = drm_vblank_count,
809 .enable_vblank = imx_drm_enable_vblank,
810 .disable_vblank = imx_drm_disable_vblank,
811 .ioctls = imx_drm_ioctls,
812 .num_ioctls = ARRAY_SIZE(imx_drm_ioctls),
813 .fops = &imx_drm_driver_fops,
815 .desc = "i.MX DRM graphics",
822 static int imx_drm_platform_probe(struct platform_device *pdev)
826 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
830 imx_drm_device->dev = &pdev->dev;
832 return drm_platform_init(&imx_drm_driver, pdev);
835 static int imx_drm_platform_remove(struct platform_device *pdev)
837 drm_put_dev(platform_get_drvdata(pdev));
842 static struct platform_driver imx_drm_pdrv = {
843 .probe = imx_drm_platform_probe,
844 .remove = imx_drm_platform_remove,
846 .owner = THIS_MODULE,
851 static struct platform_device *imx_drm_pdev;
853 static int __init imx_drm_init(void)
857 imx_drm_device = kzalloc(sizeof(*imx_drm_device), GFP_KERNEL);
861 mutex_init(&imx_drm_device->mutex);
862 INIT_LIST_HEAD(&imx_drm_device->crtc_list);
863 INIT_LIST_HEAD(&imx_drm_device->connector_list);
864 INIT_LIST_HEAD(&imx_drm_device->encoder_list);
866 imx_drm_pdev = platform_device_register_simple("imx-drm", -1, NULL, 0);
867 if (IS_ERR(imx_drm_pdev)) {
868 ret = PTR_ERR(imx_drm_pdev);
872 ret = platform_driver_register(&imx_drm_pdrv);
879 platform_device_unregister(imx_drm_pdev);
881 kfree(imx_drm_device);
886 static void __exit imx_drm_exit(void)
888 platform_device_unregister(imx_drm_pdev);
889 platform_driver_unregister(&imx_drm_pdrv);
891 kfree(imx_drm_device);
894 module_init(imx_drm_init);
895 module_exit(imx_drm_exit);
897 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
898 MODULE_DESCRIPTION("i.MX drm driver core");
899 MODULE_LICENSE("GPL");