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.
16 #include <linux/component.h>
17 #include <linux/device.h>
19 #include <linux/module.h>
20 #include <linux/of_graph.h>
21 #include <linux/platform_device.h>
23 #include <drm/drm_fb_helper.h>
24 #include <drm/drm_crtc_helper.h>
25 #include <drm/drm_gem_cma_helper.h>
26 #include <drm/drm_fb_cma_helper.h>
34 struct imx_drm_component {
35 struct device_node *of_node;
36 struct list_head list;
39 struct imx_drm_device {
40 struct drm_device *drm;
41 struct imx_drm_crtc *crtc[MAX_CRTC];
43 struct drm_fbdev_cma *fbhelper;
47 struct drm_crtc *crtc;
49 struct imx_drm_crtc_helper_funcs imx_drm_helper_funcs;
50 struct device_node *port;
53 static int legacyfb_depth = 16;
54 module_param(legacyfb_depth, int, 0444);
56 int imx_drm_crtc_id(struct imx_drm_crtc *crtc)
60 EXPORT_SYMBOL_GPL(imx_drm_crtc_id);
62 static void imx_drm_driver_lastclose(struct drm_device *drm)
64 #if IS_ENABLED(CONFIG_DRM_IMX_FB_HELPER)
65 struct imx_drm_device *imxdrm = drm->dev_private;
68 drm_fbdev_cma_restore_mode(imxdrm->fbhelper);
72 static int imx_drm_driver_unload(struct drm_device *drm)
74 #if IS_ENABLED(CONFIG_DRM_IMX_FB_HELPER)
75 struct imx_drm_device *imxdrm = drm->dev_private;
78 drm_kms_helper_poll_fini(drm);
80 #if IS_ENABLED(CONFIG_DRM_IMX_FB_HELPER)
82 drm_fbdev_cma_fini(imxdrm->fbhelper);
85 component_unbind_all(drm->dev, drm);
87 drm_vblank_cleanup(drm);
88 drm_mode_config_cleanup(drm);
90 platform_set_drvdata(drm->platformdev, NULL);
95 static struct imx_drm_crtc *imx_drm_find_crtc(struct drm_crtc *crtc)
97 struct imx_drm_device *imxdrm = crtc->dev->dev_private;
100 for (i = 0; i < MAX_CRTC; i++)
101 if (imxdrm->crtc[i] && imxdrm->crtc[i]->crtc == crtc)
102 return imxdrm->crtc[i];
107 int imx_drm_panel_format_pins(struct drm_encoder *encoder,
108 u32 interface_pix_fmt, int hsync_pin, int vsync_pin)
110 struct imx_drm_crtc_helper_funcs *helper;
111 struct imx_drm_crtc *imx_crtc;
113 imx_crtc = imx_drm_find_crtc(encoder->crtc);
117 helper = &imx_crtc->imx_drm_helper_funcs;
118 if (helper->set_interface_pix_fmt)
119 return helper->set_interface_pix_fmt(encoder->crtc,
120 encoder->encoder_type, interface_pix_fmt,
121 hsync_pin, vsync_pin);
124 EXPORT_SYMBOL_GPL(imx_drm_panel_format_pins);
126 int imx_drm_panel_format(struct drm_encoder *encoder, u32 interface_pix_fmt)
128 return imx_drm_panel_format_pins(encoder, interface_pix_fmt, 2, 3);
130 EXPORT_SYMBOL_GPL(imx_drm_panel_format);
132 int imx_drm_crtc_vblank_get(struct imx_drm_crtc *imx_drm_crtc)
134 return drm_vblank_get(imx_drm_crtc->crtc->dev, imx_drm_crtc->pipe);
136 EXPORT_SYMBOL_GPL(imx_drm_crtc_vblank_get);
138 void imx_drm_crtc_vblank_put(struct imx_drm_crtc *imx_drm_crtc)
140 drm_vblank_put(imx_drm_crtc->crtc->dev, imx_drm_crtc->pipe);
142 EXPORT_SYMBOL_GPL(imx_drm_crtc_vblank_put);
144 void imx_drm_handle_vblank(struct imx_drm_crtc *imx_drm_crtc)
146 drm_handle_vblank(imx_drm_crtc->crtc->dev, imx_drm_crtc->pipe);
148 EXPORT_SYMBOL_GPL(imx_drm_handle_vblank);
150 static int imx_drm_enable_vblank(struct drm_device *drm, int crtc)
152 struct imx_drm_device *imxdrm = drm->dev_private;
153 struct imx_drm_crtc *imx_drm_crtc = imxdrm->crtc[crtc];
159 if (!imx_drm_crtc->imx_drm_helper_funcs.enable_vblank)
162 ret = imx_drm_crtc->imx_drm_helper_funcs.enable_vblank(
168 static void imx_drm_disable_vblank(struct drm_device *drm, int crtc)
170 struct imx_drm_device *imxdrm = drm->dev_private;
171 struct imx_drm_crtc *imx_drm_crtc = imxdrm->crtc[crtc];
176 if (!imx_drm_crtc->imx_drm_helper_funcs.disable_vblank)
179 imx_drm_crtc->imx_drm_helper_funcs.disable_vblank(imx_drm_crtc->crtc);
182 static void imx_drm_driver_preclose(struct drm_device *drm,
183 struct drm_file *file)
187 if (!file->is_master)
190 for (i = 0; i < MAX_CRTC; i++)
191 imx_drm_disable_vblank(drm, i);
194 static const struct file_operations imx_drm_driver_fops = {
195 .owner = THIS_MODULE,
197 .release = drm_release,
198 .unlocked_ioctl = drm_ioctl,
199 .mmap = drm_gem_cma_mmap,
202 .llseek = noop_llseek,
205 void imx_drm_connector_destroy(struct drm_connector *connector)
207 drm_connector_unregister(connector);
208 drm_connector_cleanup(connector);
210 EXPORT_SYMBOL_GPL(imx_drm_connector_destroy);
212 void imx_drm_encoder_destroy(struct drm_encoder *encoder)
214 drm_encoder_cleanup(encoder);
216 EXPORT_SYMBOL_GPL(imx_drm_encoder_destroy);
218 static void imx_drm_output_poll_changed(struct drm_device *drm)
220 #if IS_ENABLED(CONFIG_DRM_IMX_FB_HELPER)
221 struct imx_drm_device *imxdrm = drm->dev_private;
223 drm_fbdev_cma_hotplug_event(imxdrm->fbhelper);
227 static struct drm_mode_config_funcs imx_drm_mode_config_funcs = {
228 .fb_create = drm_fb_cma_create,
229 .output_poll_changed = imx_drm_output_poll_changed,
233 * Main DRM initialisation. This binds, initialises and registers
234 * with DRM the subcomponents of the driver.
236 static int imx_drm_driver_load(struct drm_device *drm, unsigned long flags)
238 struct imx_drm_device *imxdrm;
239 struct drm_connector *connector;
242 imxdrm = devm_kzalloc(drm->dev, sizeof(*imxdrm), GFP_KERNEL);
248 drm->dev_private = imxdrm;
251 * enable drm irq mode.
252 * - with irq_enabled = true, we can use the vblank feature.
254 * P.S. note that we wouldn't use drm irq handler but
255 * just specific driver own one instead because
256 * drm framework supports only one irq handler and
257 * drivers can well take care of their interrupts
259 drm->irq_enabled = true;
262 * set max width and height as default value(4096x4096).
263 * this value would be used to check framebuffer size limitation
264 * at drm_mode_addfb().
266 drm->mode_config.min_width = 64;
267 drm->mode_config.min_height = 64;
268 drm->mode_config.max_width = 4096;
269 drm->mode_config.max_height = 4096;
270 drm->mode_config.funcs = &imx_drm_mode_config_funcs;
272 drm_mode_config_init(drm);
274 ret = drm_vblank_init(drm, MAX_CRTC);
279 * with vblank_disable_allowed = true, vblank interrupt will be
280 * disabled by drm timer once a current process gives up ownership
281 * of vblank event. (after drm_vblank_put function is called)
283 drm->vblank_disable_allowed = true;
285 platform_set_drvdata(drm->platformdev, drm);
287 /* Now try and bind all our sub-components */
288 ret = component_bind_all(drm->dev, drm);
293 * All components are now added, we can publish the connector sysfs
294 * entries to userspace. This will generate hotplug events and so
295 * userspace will expect to be able to access DRM at this point.
297 list_for_each_entry(connector, &drm->mode_config.connector_list, head) {
298 ret = drm_connector_register(connector);
301 "[CONNECTOR:%d:%s] drm_connector_register failed: %d\n",
303 connector->name, ret);
309 * All components are now initialised, so setup the fb helper.
310 * The fb helper takes copies of key hardware information, so the
311 * crtcs/connectors/encoders must not change after this point.
313 #if IS_ENABLED(CONFIG_DRM_IMX_FB_HELPER)
314 if (legacyfb_depth != 16 && legacyfb_depth != 32) {
315 dev_warn(drm->dev, "Invalid legacyfb_depth. Defaulting to 16bpp\n");
318 imxdrm->fbhelper = drm_fbdev_cma_init(drm, legacyfb_depth,
319 drm->mode_config.num_crtc, MAX_CRTC);
320 if (IS_ERR(imxdrm->fbhelper)) {
321 ret = PTR_ERR(imxdrm->fbhelper);
322 imxdrm->fbhelper = NULL;
327 drm_kms_helper_poll_init(drm);
332 component_unbind_all(drm->dev, drm);
334 drm_vblank_cleanup(drm);
336 drm_mode_config_cleanup(drm);
342 * imx_drm_add_crtc - add a new crtc
344 int imx_drm_add_crtc(struct drm_device *drm, struct drm_crtc *crtc,
345 struct imx_drm_crtc **new_crtc,
346 const struct imx_drm_crtc_helper_funcs *imx_drm_helper_funcs,
347 struct device_node *port)
349 struct imx_drm_device *imxdrm = drm->dev_private;
350 struct imx_drm_crtc *imx_drm_crtc;
354 * The vblank arrays are dimensioned by MAX_CRTC - we can't
355 * pass IDs greater than this to those functions.
357 if (imxdrm->pipes >= MAX_CRTC)
360 if (imxdrm->drm->open_count)
363 imx_drm_crtc = kzalloc(sizeof(*imx_drm_crtc), GFP_KERNEL);
367 imx_drm_crtc->imx_drm_helper_funcs = *imx_drm_helper_funcs;
368 imx_drm_crtc->pipe = imxdrm->pipes++;
369 imx_drm_crtc->port = port;
370 imx_drm_crtc->crtc = crtc;
372 imxdrm->crtc[imx_drm_crtc->pipe] = imx_drm_crtc;
374 *new_crtc = imx_drm_crtc;
376 ret = drm_mode_crtc_set_gamma_size(imx_drm_crtc->crtc, 256);
380 drm_crtc_helper_add(crtc,
381 imx_drm_crtc->imx_drm_helper_funcs.crtc_helper_funcs);
383 drm_crtc_init(drm, crtc,
384 imx_drm_crtc->imx_drm_helper_funcs.crtc_funcs);
389 imxdrm->crtc[imx_drm_crtc->pipe] = NULL;
393 EXPORT_SYMBOL_GPL(imx_drm_add_crtc);
396 * imx_drm_remove_crtc - remove a crtc
398 int imx_drm_remove_crtc(struct imx_drm_crtc *imx_drm_crtc)
400 struct imx_drm_device *imxdrm = imx_drm_crtc->crtc->dev->dev_private;
402 drm_crtc_cleanup(imx_drm_crtc->crtc);
404 imxdrm->crtc[imx_drm_crtc->pipe] = NULL;
410 EXPORT_SYMBOL_GPL(imx_drm_remove_crtc);
413 * Find the DRM CRTC possible mask for the connected endpoint.
415 * The encoder possible masks are defined by their position in the
416 * mode_config crtc_list. This means that CRTCs must not be added
417 * or removed once the DRM device has been fully initialised.
419 static uint32_t imx_drm_find_crtc_mask(struct imx_drm_device *imxdrm,
420 struct device_node *endpoint)
422 struct device_node *port;
425 port = of_graph_get_remote_port(endpoint);
430 for (i = 0; i < MAX_CRTC; i++) {
431 struct imx_drm_crtc *imx_drm_crtc = imxdrm->crtc[i];
433 if (imx_drm_crtc && imx_drm_crtc->port == port)
434 return drm_crtc_mask(imx_drm_crtc->crtc);
440 static struct device_node *imx_drm_of_get_next_endpoint(
441 const struct device_node *parent, struct device_node *prev)
443 struct device_node *node = of_graph_get_next_endpoint(parent, prev);
449 int imx_drm_encoder_parse_of(struct drm_device *drm,
450 struct drm_encoder *encoder, struct device_node *np)
452 struct imx_drm_device *imxdrm = drm->dev_private;
453 struct device_node *ep = NULL;
454 uint32_t crtc_mask = 0;
460 ep = imx_drm_of_get_next_endpoint(np, ep);
464 mask = imx_drm_find_crtc_mask(imxdrm, ep);
467 * If we failed to find the CRTC(s) which this encoder is
468 * supposed to be connected to, it's because the CRTC has
469 * not been registered yet. Defer probing, and hope that
470 * the required CRTC is added later.
473 return -EPROBE_DEFER;
482 encoder->possible_crtcs = crtc_mask;
484 /* FIXME: this is the mask of outputs which can clone this output. */
485 encoder->possible_clones = ~0;
489 EXPORT_SYMBOL_GPL(imx_drm_encoder_parse_of);
492 * @node: device tree node containing encoder input ports
493 * @encoder: drm_encoder
495 int imx_drm_encoder_get_mux_id(struct device_node *node,
496 struct drm_encoder *encoder)
498 struct imx_drm_crtc *imx_crtc = imx_drm_find_crtc(encoder->crtc);
499 struct device_node *ep = NULL;
500 struct of_endpoint endpoint;
501 struct device_node *port;
504 if (!node || !imx_crtc)
508 ep = imx_drm_of_get_next_endpoint(node, ep);
512 port = of_graph_get_remote_port(ep);
514 if (port == imx_crtc->port) {
515 ret = of_graph_parse_endpoint(ep, &endpoint);
516 return ret ? ret : endpoint.port;
522 EXPORT_SYMBOL_GPL(imx_drm_encoder_get_mux_id);
524 static const struct drm_ioctl_desc imx_drm_ioctls[] = {
528 static struct drm_driver imx_drm_driver = {
529 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME,
530 .load = imx_drm_driver_load,
531 .unload = imx_drm_driver_unload,
532 .lastclose = imx_drm_driver_lastclose,
533 .preclose = imx_drm_driver_preclose,
534 .set_busid = drm_platform_set_busid,
535 .gem_free_object = drm_gem_cma_free_object,
536 .gem_vm_ops = &drm_gem_cma_vm_ops,
537 .dumb_create = drm_gem_cma_dumb_create,
538 .dumb_map_offset = drm_gem_cma_dumb_map_offset,
539 .dumb_destroy = drm_gem_dumb_destroy,
541 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
542 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
543 .gem_prime_import = drm_gem_prime_import,
544 .gem_prime_export = drm_gem_prime_export,
545 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
546 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
547 .gem_prime_vmap = drm_gem_cma_prime_vmap,
548 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
549 .gem_prime_mmap = drm_gem_cma_prime_mmap,
550 .get_vblank_counter = drm_vblank_count,
551 .enable_vblank = imx_drm_enable_vblank,
552 .disable_vblank = imx_drm_disable_vblank,
553 .ioctls = imx_drm_ioctls,
554 .num_ioctls = ARRAY_SIZE(imx_drm_ioctls),
555 .fops = &imx_drm_driver_fops,
557 .desc = "i.MX DRM graphics",
564 static int compare_of(struct device *dev, void *data)
566 struct device_node *np = data;
568 /* Special case for LDB, one device for two channels */
569 if (of_node_cmp(np->name, "lvds-channel") == 0) {
570 np = of_get_parent(np);
574 return dev->of_node == np;
577 static int imx_drm_bind(struct device *dev)
579 return drm_platform_init(&imx_drm_driver, to_platform_device(dev));
582 static void imx_drm_unbind(struct device *dev)
584 drm_put_dev(dev_get_drvdata(dev));
587 static const struct component_master_ops imx_drm_ops = {
588 .bind = imx_drm_bind,
589 .unbind = imx_drm_unbind,
592 static int imx_drm_platform_probe(struct platform_device *pdev)
594 struct device_node *ep, *port, *remote;
595 struct component_match *match = NULL;
600 * Bind the IPU display interface ports first, so that
601 * imx_drm_encoder_parse_of called from encoder .bind callbacks
605 port = of_parse_phandle(pdev->dev.of_node, "ports", i);
609 component_match_add(&pdev->dev, &match, compare_of, port);
613 dev_err(&pdev->dev, "missing 'ports' property\n");
617 /* Then bind all encoders */
619 port = of_parse_phandle(pdev->dev.of_node, "ports", i);
623 for_each_child_of_node(port, ep) {
624 remote = of_graph_get_remote_port_parent(ep);
625 if (!remote || !of_device_is_available(remote)) {
628 } else if (!of_device_is_available(remote->parent)) {
629 dev_warn(&pdev->dev, "parent device of %s is not available\n",
635 component_match_add(&pdev->dev, &match, compare_of, remote);
641 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
645 return component_master_add_with_match(&pdev->dev, &imx_drm_ops, match);
648 static int imx_drm_platform_remove(struct platform_device *pdev)
650 component_master_del(&pdev->dev, &imx_drm_ops);
654 #ifdef CONFIG_PM_SLEEP
655 static int imx_drm_suspend(struct device *dev)
657 struct drm_device *drm_dev = dev_get_drvdata(dev);
659 /* The drm_dev is NULL before .load hook is called */
663 drm_kms_helper_poll_disable(drm_dev);
668 static int imx_drm_resume(struct device *dev)
670 struct drm_device *drm_dev = dev_get_drvdata(dev);
675 drm_helper_resume_force_mode(drm_dev);
676 drm_kms_helper_poll_enable(drm_dev);
682 static SIMPLE_DEV_PM_OPS(imx_drm_pm_ops, imx_drm_suspend, imx_drm_resume);
684 static const struct of_device_id imx_drm_dt_ids[] = {
685 { .compatible = "fsl,imx-display-subsystem", },
688 MODULE_DEVICE_TABLE(of, imx_drm_dt_ids);
690 static struct platform_driver imx_drm_pdrv = {
691 .probe = imx_drm_platform_probe,
692 .remove = imx_drm_platform_remove,
694 .owner = THIS_MODULE,
696 .pm = &imx_drm_pm_ops,
697 .of_match_table = imx_drm_dt_ids,
700 module_platform_driver(imx_drm_pdrv);
702 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
703 MODULE_DESCRIPTION("i.MX drm driver core");
704 MODULE_LICENSE("GPL");