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>
27 #include <drm/drm_plane_helper.h>
28 #include <drm/drm_of.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;
48 struct imx_drm_crtc_helper_funcs imx_drm_helper_funcs;
51 #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
52 static int legacyfb_depth = 16;
53 module_param(legacyfb_depth, int, 0444);
56 unsigned int imx_drm_crtc_id(struct imx_drm_crtc *crtc)
58 return drm_crtc_index(crtc->crtc);
60 EXPORT_SYMBOL_GPL(imx_drm_crtc_id);
62 static void imx_drm_driver_lastclose(struct drm_device *drm)
64 struct imx_drm_device *imxdrm = drm->dev_private;
66 drm_fbdev_cma_restore_mode(imxdrm->fbhelper);
69 static int imx_drm_driver_unload(struct drm_device *drm)
71 struct imx_drm_device *imxdrm = drm->dev_private;
73 drm_kms_helper_poll_fini(drm);
76 drm_fbdev_cma_fini(imxdrm->fbhelper);
78 component_unbind_all(drm->dev, drm);
80 drm_vblank_cleanup(drm);
81 drm_mode_config_cleanup(drm);
83 platform_set_drvdata(drm->platformdev, NULL);
88 static struct imx_drm_crtc *imx_drm_find_crtc(struct drm_crtc *crtc)
90 struct imx_drm_device *imxdrm = crtc->dev->dev_private;
93 for (i = 0; i < MAX_CRTC; i++)
94 if (imxdrm->crtc[i] && imxdrm->crtc[i]->crtc == crtc)
95 return imxdrm->crtc[i];
100 int imx_drm_set_bus_format_pins(struct drm_encoder *encoder, u32 bus_format,
101 int hsync_pin, int vsync_pin)
103 struct imx_drm_crtc_helper_funcs *helper;
104 struct imx_drm_crtc *imx_crtc;
106 imx_crtc = imx_drm_find_crtc(encoder->crtc);
110 helper = &imx_crtc->imx_drm_helper_funcs;
111 if (helper->set_interface_pix_fmt)
112 return helper->set_interface_pix_fmt(encoder->crtc,
113 bus_format, hsync_pin, vsync_pin);
116 EXPORT_SYMBOL_GPL(imx_drm_set_bus_format_pins);
118 int imx_drm_set_bus_format(struct drm_encoder *encoder, u32 bus_format)
120 return imx_drm_set_bus_format_pins(encoder, bus_format, 2, 3);
122 EXPORT_SYMBOL_GPL(imx_drm_set_bus_format);
124 int imx_drm_crtc_vblank_get(struct imx_drm_crtc *imx_drm_crtc)
126 return drm_crtc_vblank_get(imx_drm_crtc->crtc);
128 EXPORT_SYMBOL_GPL(imx_drm_crtc_vblank_get);
130 void imx_drm_crtc_vblank_put(struct imx_drm_crtc *imx_drm_crtc)
132 drm_crtc_vblank_put(imx_drm_crtc->crtc);
134 EXPORT_SYMBOL_GPL(imx_drm_crtc_vblank_put);
136 void imx_drm_handle_vblank(struct imx_drm_crtc *imx_drm_crtc)
138 drm_crtc_handle_vblank(imx_drm_crtc->crtc);
140 EXPORT_SYMBOL_GPL(imx_drm_handle_vblank);
142 static int imx_drm_enable_vblank(struct drm_device *drm, unsigned int pipe)
144 struct imx_drm_device *imxdrm = drm->dev_private;
145 struct imx_drm_crtc *imx_drm_crtc = imxdrm->crtc[pipe];
151 if (!imx_drm_crtc->imx_drm_helper_funcs.enable_vblank)
154 ret = imx_drm_crtc->imx_drm_helper_funcs.enable_vblank(
160 static void imx_drm_disable_vblank(struct drm_device *drm, unsigned int pipe)
162 struct imx_drm_device *imxdrm = drm->dev_private;
163 struct imx_drm_crtc *imx_drm_crtc = imxdrm->crtc[pipe];
168 if (!imx_drm_crtc->imx_drm_helper_funcs.disable_vblank)
171 imx_drm_crtc->imx_drm_helper_funcs.disable_vblank(imx_drm_crtc->crtc);
174 static const struct file_operations imx_drm_driver_fops = {
175 .owner = THIS_MODULE,
177 .release = drm_release,
178 .unlocked_ioctl = drm_ioctl,
179 .mmap = drm_gem_cma_mmap,
182 .llseek = noop_llseek,
185 void imx_drm_connector_destroy(struct drm_connector *connector)
187 drm_connector_unregister(connector);
188 drm_connector_cleanup(connector);
190 EXPORT_SYMBOL_GPL(imx_drm_connector_destroy);
192 void imx_drm_encoder_destroy(struct drm_encoder *encoder)
194 drm_encoder_cleanup(encoder);
196 EXPORT_SYMBOL_GPL(imx_drm_encoder_destroy);
198 static void imx_drm_output_poll_changed(struct drm_device *drm)
200 struct imx_drm_device *imxdrm = drm->dev_private;
202 drm_fbdev_cma_hotplug_event(imxdrm->fbhelper);
205 static const struct drm_mode_config_funcs imx_drm_mode_config_funcs = {
206 .fb_create = drm_fb_cma_create,
207 .output_poll_changed = imx_drm_output_poll_changed,
211 * Main DRM initialisation. This binds, initialises and registers
212 * with DRM the subcomponents of the driver.
214 static int imx_drm_driver_load(struct drm_device *drm, unsigned long flags)
216 struct imx_drm_device *imxdrm;
217 struct drm_connector *connector;
220 imxdrm = devm_kzalloc(drm->dev, sizeof(*imxdrm), GFP_KERNEL);
226 drm->dev_private = imxdrm;
229 * enable drm irq mode.
230 * - with irq_enabled = true, we can use the vblank feature.
232 * P.S. note that we wouldn't use drm irq handler but
233 * just specific driver own one instead because
234 * drm framework supports only one irq handler and
235 * drivers can well take care of their interrupts
237 drm->irq_enabled = true;
240 * set max width and height as default value(4096x4096).
241 * this value would be used to check framebuffer size limitation
242 * at drm_mode_addfb().
244 drm->mode_config.min_width = 64;
245 drm->mode_config.min_height = 64;
246 drm->mode_config.max_width = 4096;
247 drm->mode_config.max_height = 4096;
248 drm->mode_config.funcs = &imx_drm_mode_config_funcs;
250 drm_mode_config_init(drm);
252 ret = drm_vblank_init(drm, MAX_CRTC);
257 * with vblank_disable_allowed = true, vblank interrupt will be
258 * disabled by drm timer once a current process gives up ownership
259 * of vblank event. (after drm_vblank_put function is called)
261 drm->vblank_disable_allowed = true;
263 platform_set_drvdata(drm->platformdev, drm);
265 /* Now try and bind all our sub-components */
266 ret = component_bind_all(drm->dev, drm);
271 * All components are now added, we can publish the connector sysfs
272 * entries to userspace. This will generate hotplug events and so
273 * userspace will expect to be able to access DRM at this point.
275 list_for_each_entry(connector, &drm->mode_config.connector_list, head) {
276 ret = drm_connector_register(connector);
279 "[CONNECTOR:%d:%s] drm_connector_register failed: %d\n",
281 connector->name, ret);
287 * All components are now initialised, so setup the fb helper.
288 * The fb helper takes copies of key hardware information, so the
289 * crtcs/connectors/encoders must not change after this point.
291 #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
292 if (legacyfb_depth != 16 && legacyfb_depth != 32) {
293 dev_warn(drm->dev, "Invalid legacyfb_depth. Defaulting to 16bpp\n");
296 drm_helper_disable_unused_functions(drm);
297 imxdrm->fbhelper = drm_fbdev_cma_init(drm, legacyfb_depth,
298 drm->mode_config.num_crtc, MAX_CRTC);
299 if (IS_ERR(imxdrm->fbhelper)) {
300 ret = PTR_ERR(imxdrm->fbhelper);
301 imxdrm->fbhelper = NULL;
306 drm_kms_helper_poll_init(drm);
311 component_unbind_all(drm->dev, drm);
313 drm_vblank_cleanup(drm);
315 drm_mode_config_cleanup(drm);
321 * imx_drm_add_crtc - add a new crtc
323 int imx_drm_add_crtc(struct drm_device *drm, struct drm_crtc *crtc,
324 struct imx_drm_crtc **new_crtc, struct drm_plane *primary_plane,
325 const struct imx_drm_crtc_helper_funcs *imx_drm_helper_funcs,
326 struct device_node *port)
328 struct imx_drm_device *imxdrm = drm->dev_private;
329 struct imx_drm_crtc *imx_drm_crtc;
333 * The vblank arrays are dimensioned by MAX_CRTC - we can't
334 * pass IDs greater than this to those functions.
336 if (imxdrm->pipes >= MAX_CRTC)
339 if (imxdrm->drm->open_count)
342 imx_drm_crtc = kzalloc(sizeof(*imx_drm_crtc), GFP_KERNEL);
346 imx_drm_crtc->imx_drm_helper_funcs = *imx_drm_helper_funcs;
347 imx_drm_crtc->crtc = crtc;
351 imxdrm->crtc[imxdrm->pipes++] = imx_drm_crtc;
353 *new_crtc = imx_drm_crtc;
355 ret = drm_mode_crtc_set_gamma_size(imx_drm_crtc->crtc, 256);
359 drm_crtc_helper_add(crtc,
360 imx_drm_crtc->imx_drm_helper_funcs.crtc_helper_funcs);
362 drm_crtc_init_with_planes(drm, crtc, primary_plane, NULL,
363 imx_drm_crtc->imx_drm_helper_funcs.crtc_funcs, NULL);
368 imxdrm->crtc[--imxdrm->pipes] = NULL;
372 EXPORT_SYMBOL_GPL(imx_drm_add_crtc);
375 * imx_drm_remove_crtc - remove a crtc
377 int imx_drm_remove_crtc(struct imx_drm_crtc *imx_drm_crtc)
379 struct imx_drm_device *imxdrm = imx_drm_crtc->crtc->dev->dev_private;
380 unsigned int pipe = drm_crtc_index(imx_drm_crtc->crtc);
382 drm_crtc_cleanup(imx_drm_crtc->crtc);
384 imxdrm->crtc[pipe] = NULL;
390 EXPORT_SYMBOL_GPL(imx_drm_remove_crtc);
392 int imx_drm_encoder_parse_of(struct drm_device *drm,
393 struct drm_encoder *encoder, struct device_node *np)
395 uint32_t crtc_mask = drm_of_find_possible_crtcs(drm, np);
398 * If we failed to find the CRTC(s) which this encoder is
399 * supposed to be connected to, it's because the CRTC has
400 * not been registered yet. Defer probing, and hope that
401 * the required CRTC is added later.
404 return -EPROBE_DEFER;
406 encoder->possible_crtcs = crtc_mask;
408 /* FIXME: this is the mask of outputs which can clone this output. */
409 encoder->possible_clones = ~0;
413 EXPORT_SYMBOL_GPL(imx_drm_encoder_parse_of);
416 * @node: device tree node containing encoder input ports
417 * @encoder: drm_encoder
419 int imx_drm_encoder_get_mux_id(struct device_node *node,
420 struct drm_encoder *encoder)
422 struct imx_drm_crtc *imx_crtc = imx_drm_find_crtc(encoder->crtc);
423 struct device_node *ep;
424 struct of_endpoint endpoint;
425 struct device_node *port;
428 if (!node || !imx_crtc)
431 for_each_endpoint_of_node(node, ep) {
432 port = of_graph_get_remote_port(ep);
434 if (port == imx_crtc->crtc->port) {
435 ret = of_graph_parse_endpoint(ep, &endpoint);
437 return ret ? ret : endpoint.port;
443 EXPORT_SYMBOL_GPL(imx_drm_encoder_get_mux_id);
445 static const struct drm_ioctl_desc imx_drm_ioctls[] = {
449 static struct drm_driver imx_drm_driver = {
450 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME,
451 .load = imx_drm_driver_load,
452 .unload = imx_drm_driver_unload,
453 .lastclose = imx_drm_driver_lastclose,
454 .set_busid = drm_platform_set_busid,
455 .gem_free_object = drm_gem_cma_free_object,
456 .gem_vm_ops = &drm_gem_cma_vm_ops,
457 .dumb_create = drm_gem_cma_dumb_create,
458 .dumb_map_offset = drm_gem_cma_dumb_map_offset,
459 .dumb_destroy = drm_gem_dumb_destroy,
461 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
462 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
463 .gem_prime_import = drm_gem_prime_import,
464 .gem_prime_export = drm_gem_prime_export,
465 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
466 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
467 .gem_prime_vmap = drm_gem_cma_prime_vmap,
468 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
469 .gem_prime_mmap = drm_gem_cma_prime_mmap,
470 .get_vblank_counter = drm_vblank_no_hw_counter,
471 .enable_vblank = imx_drm_enable_vblank,
472 .disable_vblank = imx_drm_disable_vblank,
473 .ioctls = imx_drm_ioctls,
474 .num_ioctls = ARRAY_SIZE(imx_drm_ioctls),
475 .fops = &imx_drm_driver_fops,
477 .desc = "i.MX DRM graphics",
484 static int compare_of(struct device *dev, void *data)
486 struct device_node *np = data;
488 /* Special case for LDB, one device for two channels */
489 if (of_node_cmp(np->name, "lvds-channel") == 0) {
490 np = of_get_parent(np);
494 return dev->of_node == np;
497 static int imx_drm_bind(struct device *dev)
499 return drm_platform_init(&imx_drm_driver, to_platform_device(dev));
502 static void imx_drm_unbind(struct device *dev)
504 drm_put_dev(dev_get_drvdata(dev));
507 static const struct component_master_ops imx_drm_ops = {
508 .bind = imx_drm_bind,
509 .unbind = imx_drm_unbind,
512 static int imx_drm_platform_probe(struct platform_device *pdev)
514 int ret = drm_of_component_probe(&pdev->dev, compare_of, &imx_drm_ops);
517 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
522 static int imx_drm_platform_remove(struct platform_device *pdev)
524 component_master_del(&pdev->dev, &imx_drm_ops);
528 #ifdef CONFIG_PM_SLEEP
529 static int imx_drm_suspend(struct device *dev)
531 struct drm_device *drm_dev = dev_get_drvdata(dev);
533 /* The drm_dev is NULL before .load hook is called */
537 drm_kms_helper_poll_disable(drm_dev);
542 static int imx_drm_resume(struct device *dev)
544 struct drm_device *drm_dev = dev_get_drvdata(dev);
549 drm_helper_resume_force_mode(drm_dev);
550 drm_kms_helper_poll_enable(drm_dev);
556 static SIMPLE_DEV_PM_OPS(imx_drm_pm_ops, imx_drm_suspend, imx_drm_resume);
558 static const struct of_device_id imx_drm_dt_ids[] = {
559 { .compatible = "fsl,imx-display-subsystem", },
562 MODULE_DEVICE_TABLE(of, imx_drm_dt_ids);
564 static struct platform_driver imx_drm_pdrv = {
565 .probe = imx_drm_platform_probe,
566 .remove = imx_drm_platform_remove,
569 .pm = &imx_drm_pm_ops,
570 .of_match_table = imx_drm_dt_ids,
573 module_platform_driver(imx_drm_pdrv);
575 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
576 MODULE_DESCRIPTION("i.MX drm driver core");
577 MODULE_LICENSE("GPL");