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/platform_device.h>
22 #include <drm/drm_fb_helper.h>
23 #include <drm/drm_crtc_helper.h>
24 #include <drm/drm_gem_cma_helper.h>
25 #include <drm/drm_fb_cma_helper.h>
26 #include <drm/drm_plane_helper.h>
27 #include <drm/drm_of.h>
33 struct imx_drm_component {
34 struct device_node *of_node;
35 struct list_head list;
38 struct imx_drm_device {
39 struct drm_device *drm;
40 struct imx_drm_crtc *crtc[MAX_CRTC];
42 struct drm_fbdev_cma *fbhelper;
46 struct drm_crtc *crtc;
47 struct imx_drm_crtc_helper_funcs imx_drm_helper_funcs;
50 #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
51 static int legacyfb_depth = 16;
52 module_param(legacyfb_depth, int, 0444);
55 unsigned int imx_drm_crtc_id(struct imx_drm_crtc *crtc)
57 return drm_crtc_index(crtc->crtc);
59 EXPORT_SYMBOL_GPL(imx_drm_crtc_id);
61 static void imx_drm_driver_lastclose(struct drm_device *drm)
63 struct imx_drm_device *imxdrm = drm->dev_private;
65 drm_fbdev_cma_restore_mode(imxdrm->fbhelper);
68 static int imx_drm_driver_unload(struct drm_device *drm)
70 struct imx_drm_device *imxdrm = drm->dev_private;
72 drm_kms_helper_poll_fini(drm);
75 drm_fbdev_cma_fini(imxdrm->fbhelper);
77 component_unbind_all(drm->dev, drm);
79 drm_vblank_cleanup(drm);
80 drm_mode_config_cleanup(drm);
82 platform_set_drvdata(drm->platformdev, NULL);
87 static struct imx_drm_crtc *imx_drm_find_crtc(struct drm_crtc *crtc)
89 struct imx_drm_device *imxdrm = crtc->dev->dev_private;
92 for (i = 0; i < MAX_CRTC; i++)
93 if (imxdrm->crtc[i] && imxdrm->crtc[i]->crtc == crtc)
94 return imxdrm->crtc[i];
99 int imx_drm_set_bus_format_pins(struct drm_encoder *encoder, u32 bus_format,
100 int hsync_pin, int vsync_pin)
102 struct imx_drm_crtc_helper_funcs *helper;
103 struct imx_drm_crtc *imx_crtc;
105 imx_crtc = imx_drm_find_crtc(encoder->crtc);
109 helper = &imx_crtc->imx_drm_helper_funcs;
110 if (helper->set_interface_pix_fmt)
111 return helper->set_interface_pix_fmt(encoder->crtc,
112 bus_format, hsync_pin, vsync_pin);
115 EXPORT_SYMBOL_GPL(imx_drm_set_bus_format_pins);
117 int imx_drm_set_bus_format(struct drm_encoder *encoder, u32 bus_format)
119 return imx_drm_set_bus_format_pins(encoder, bus_format, 2, 3);
121 EXPORT_SYMBOL_GPL(imx_drm_set_bus_format);
123 int imx_drm_crtc_vblank_get(struct imx_drm_crtc *imx_drm_crtc)
125 return drm_crtc_vblank_get(imx_drm_crtc->crtc);
127 EXPORT_SYMBOL_GPL(imx_drm_crtc_vblank_get);
129 void imx_drm_crtc_vblank_put(struct imx_drm_crtc *imx_drm_crtc)
131 drm_crtc_vblank_put(imx_drm_crtc->crtc);
133 EXPORT_SYMBOL_GPL(imx_drm_crtc_vblank_put);
135 void imx_drm_handle_vblank(struct imx_drm_crtc *imx_drm_crtc)
137 drm_crtc_handle_vblank(imx_drm_crtc->crtc);
139 EXPORT_SYMBOL_GPL(imx_drm_handle_vblank);
141 static int imx_drm_enable_vblank(struct drm_device *drm, unsigned int pipe)
143 struct imx_drm_device *imxdrm = drm->dev_private;
144 struct imx_drm_crtc *imx_drm_crtc = imxdrm->crtc[pipe];
150 if (!imx_drm_crtc->imx_drm_helper_funcs.enable_vblank)
153 ret = imx_drm_crtc->imx_drm_helper_funcs.enable_vblank(
159 static void imx_drm_disable_vblank(struct drm_device *drm, unsigned int pipe)
161 struct imx_drm_device *imxdrm = drm->dev_private;
162 struct imx_drm_crtc *imx_drm_crtc = imxdrm->crtc[pipe];
167 if (!imx_drm_crtc->imx_drm_helper_funcs.disable_vblank)
170 imx_drm_crtc->imx_drm_helper_funcs.disable_vblank(imx_drm_crtc->crtc);
173 static const struct file_operations imx_drm_driver_fops = {
174 .owner = THIS_MODULE,
176 .release = drm_release,
177 .unlocked_ioctl = drm_ioctl,
178 .mmap = drm_gem_cma_mmap,
181 .llseek = noop_llseek,
184 void imx_drm_connector_destroy(struct drm_connector *connector)
186 drm_connector_unregister(connector);
187 drm_connector_cleanup(connector);
189 EXPORT_SYMBOL_GPL(imx_drm_connector_destroy);
191 void imx_drm_encoder_destroy(struct drm_encoder *encoder)
193 drm_encoder_cleanup(encoder);
195 EXPORT_SYMBOL_GPL(imx_drm_encoder_destroy);
197 static void imx_drm_output_poll_changed(struct drm_device *drm)
199 struct imx_drm_device *imxdrm = drm->dev_private;
201 drm_fbdev_cma_hotplug_event(imxdrm->fbhelper);
204 static const struct drm_mode_config_funcs imx_drm_mode_config_funcs = {
205 .fb_create = drm_fb_cma_create,
206 .output_poll_changed = imx_drm_output_poll_changed,
210 * Main DRM initialisation. This binds, initialises and registers
211 * with DRM the subcomponents of the driver.
213 static int imx_drm_driver_load(struct drm_device *drm, unsigned long flags)
215 struct imx_drm_device *imxdrm;
216 struct drm_connector *connector;
219 imxdrm = devm_kzalloc(drm->dev, sizeof(*imxdrm), GFP_KERNEL);
225 drm->dev_private = imxdrm;
228 * enable drm irq mode.
229 * - with irq_enabled = true, we can use the vblank feature.
231 * P.S. note that we wouldn't use drm irq handler but
232 * just specific driver own one instead because
233 * drm framework supports only one irq handler and
234 * drivers can well take care of their interrupts
236 drm->irq_enabled = true;
239 * set max width and height as default value(4096x4096).
240 * this value would be used to check framebuffer size limitation
241 * at drm_mode_addfb().
243 drm->mode_config.min_width = 64;
244 drm->mode_config.min_height = 64;
245 drm->mode_config.max_width = 4096;
246 drm->mode_config.max_height = 4096;
247 drm->mode_config.funcs = &imx_drm_mode_config_funcs;
249 drm_mode_config_init(drm);
251 ret = drm_vblank_init(drm, MAX_CRTC);
255 platform_set_drvdata(drm->platformdev, drm);
257 /* Now try and bind all our sub-components */
258 ret = component_bind_all(drm->dev, drm);
263 * All components are now added, we can publish the connector sysfs
264 * entries to userspace. This will generate hotplug events and so
265 * userspace will expect to be able to access DRM at this point.
267 list_for_each_entry(connector, &drm->mode_config.connector_list, head) {
268 ret = drm_connector_register(connector);
271 "[CONNECTOR:%d:%s] drm_connector_register failed: %d\n",
273 connector->name, ret);
279 * All components are now initialised, so setup the fb helper.
280 * The fb helper takes copies of key hardware information, so the
281 * crtcs/connectors/encoders must not change after this point.
283 #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
284 if (legacyfb_depth != 16 && legacyfb_depth != 32) {
285 dev_warn(drm->dev, "Invalid legacyfb_depth. Defaulting to 16bpp\n");
288 drm_helper_disable_unused_functions(drm);
289 imxdrm->fbhelper = drm_fbdev_cma_init(drm, legacyfb_depth,
290 drm->mode_config.num_crtc, MAX_CRTC);
291 if (IS_ERR(imxdrm->fbhelper)) {
292 ret = PTR_ERR(imxdrm->fbhelper);
293 imxdrm->fbhelper = NULL;
298 drm_kms_helper_poll_init(drm);
303 component_unbind_all(drm->dev, drm);
305 drm_vblank_cleanup(drm);
307 drm_mode_config_cleanup(drm);
313 * imx_drm_add_crtc - add a new crtc
315 int imx_drm_add_crtc(struct drm_device *drm, struct drm_crtc *crtc,
316 struct imx_drm_crtc **new_crtc, struct drm_plane *primary_plane,
317 const struct imx_drm_crtc_helper_funcs *imx_drm_helper_funcs,
318 struct device_node *port)
320 struct imx_drm_device *imxdrm = drm->dev_private;
321 struct imx_drm_crtc *imx_drm_crtc;
324 * The vblank arrays are dimensioned by MAX_CRTC - we can't
325 * pass IDs greater than this to those functions.
327 if (imxdrm->pipes >= MAX_CRTC)
330 if (imxdrm->drm->open_count)
333 imx_drm_crtc = kzalloc(sizeof(*imx_drm_crtc), GFP_KERNEL);
337 imx_drm_crtc->imx_drm_helper_funcs = *imx_drm_helper_funcs;
338 imx_drm_crtc->crtc = crtc;
342 imxdrm->crtc[imxdrm->pipes++] = imx_drm_crtc;
344 *new_crtc = imx_drm_crtc;
346 drm_crtc_helper_add(crtc,
347 imx_drm_crtc->imx_drm_helper_funcs.crtc_helper_funcs);
349 drm_crtc_init_with_planes(drm, crtc, primary_plane, NULL,
350 imx_drm_crtc->imx_drm_helper_funcs.crtc_funcs, NULL);
354 EXPORT_SYMBOL_GPL(imx_drm_add_crtc);
357 * imx_drm_remove_crtc - remove a crtc
359 int imx_drm_remove_crtc(struct imx_drm_crtc *imx_drm_crtc)
361 struct imx_drm_device *imxdrm = imx_drm_crtc->crtc->dev->dev_private;
362 unsigned int pipe = drm_crtc_index(imx_drm_crtc->crtc);
364 drm_crtc_cleanup(imx_drm_crtc->crtc);
366 imxdrm->crtc[pipe] = NULL;
372 EXPORT_SYMBOL_GPL(imx_drm_remove_crtc);
374 int imx_drm_encoder_parse_of(struct drm_device *drm,
375 struct drm_encoder *encoder, struct device_node *np)
377 uint32_t crtc_mask = drm_of_find_possible_crtcs(drm, np);
380 * If we failed to find the CRTC(s) which this encoder is
381 * supposed to be connected to, it's because the CRTC has
382 * not been registered yet. Defer probing, and hope that
383 * the required CRTC is added later.
386 return -EPROBE_DEFER;
388 encoder->possible_crtcs = crtc_mask;
390 /* FIXME: this is the mask of outputs which can clone this output. */
391 encoder->possible_clones = ~0;
395 EXPORT_SYMBOL_GPL(imx_drm_encoder_parse_of);
397 static const struct drm_ioctl_desc imx_drm_ioctls[] = {
401 static struct drm_driver imx_drm_driver = {
402 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME,
403 .load = imx_drm_driver_load,
404 .unload = imx_drm_driver_unload,
405 .lastclose = imx_drm_driver_lastclose,
406 .set_busid = drm_platform_set_busid,
407 .gem_free_object_unlocked = drm_gem_cma_free_object,
408 .gem_vm_ops = &drm_gem_cma_vm_ops,
409 .dumb_create = drm_gem_cma_dumb_create,
410 .dumb_map_offset = drm_gem_cma_dumb_map_offset,
411 .dumb_destroy = drm_gem_dumb_destroy,
413 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
414 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
415 .gem_prime_import = drm_gem_prime_import,
416 .gem_prime_export = drm_gem_prime_export,
417 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
418 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
419 .gem_prime_vmap = drm_gem_cma_prime_vmap,
420 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
421 .gem_prime_mmap = drm_gem_cma_prime_mmap,
422 .get_vblank_counter = drm_vblank_no_hw_counter,
423 .enable_vblank = imx_drm_enable_vblank,
424 .disable_vblank = imx_drm_disable_vblank,
425 .ioctls = imx_drm_ioctls,
426 .num_ioctls = ARRAY_SIZE(imx_drm_ioctls),
427 .fops = &imx_drm_driver_fops,
429 .desc = "i.MX DRM graphics",
436 static int compare_of(struct device *dev, void *data)
438 struct device_node *np = data;
440 /* Special case for LDB, one device for two channels */
441 if (of_node_cmp(np->name, "lvds-channel") == 0) {
442 np = of_get_parent(np);
446 return dev->of_node == np;
449 static int imx_drm_bind(struct device *dev)
451 return drm_platform_init(&imx_drm_driver, to_platform_device(dev));
454 static void imx_drm_unbind(struct device *dev)
456 drm_put_dev(dev_get_drvdata(dev));
459 static const struct component_master_ops imx_drm_ops = {
460 .bind = imx_drm_bind,
461 .unbind = imx_drm_unbind,
464 static int imx_drm_platform_probe(struct platform_device *pdev)
466 int ret = drm_of_component_probe(&pdev->dev, compare_of, &imx_drm_ops);
469 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
474 static int imx_drm_platform_remove(struct platform_device *pdev)
476 component_master_del(&pdev->dev, &imx_drm_ops);
480 #ifdef CONFIG_PM_SLEEP
481 static int imx_drm_suspend(struct device *dev)
483 struct drm_device *drm_dev = dev_get_drvdata(dev);
485 /* The drm_dev is NULL before .load hook is called */
489 drm_kms_helper_poll_disable(drm_dev);
494 static int imx_drm_resume(struct device *dev)
496 struct drm_device *drm_dev = dev_get_drvdata(dev);
501 drm_helper_resume_force_mode(drm_dev);
502 drm_kms_helper_poll_enable(drm_dev);
508 static SIMPLE_DEV_PM_OPS(imx_drm_pm_ops, imx_drm_suspend, imx_drm_resume);
510 static const struct of_device_id imx_drm_dt_ids[] = {
511 { .compatible = "fsl,imx-display-subsystem", },
514 MODULE_DEVICE_TABLE(of, imx_drm_dt_ids);
516 static struct platform_driver imx_drm_pdrv = {
517 .probe = imx_drm_platform_probe,
518 .remove = imx_drm_platform_remove,
521 .pm = &imx_drm_pm_ops,
522 .of_match_table = imx_drm_dt_ids,
525 module_platform_driver(imx_drm_pdrv);
528 MODULE_DESCRIPTION("i.MX drm driver core");
529 MODULE_LICENSE("GPL");