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>
18 #include <linux/dma-buf.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
22 #include <drm/drm_atomic.h>
23 #include <drm/drm_atomic_helper.h>
24 #include <drm/drm_fb_helper.h>
25 #include <drm/drm_crtc_helper.h>
26 #include <drm/drm_gem_cma_helper.h>
27 #include <drm/drm_fb_cma_helper.h>
28 #include <drm/drm_plane_helper.h>
29 #include <drm/drm_of.h>
30 #include <video/imx-ipu-v3.h>
36 struct imx_drm_component {
37 struct device_node *of_node;
38 struct list_head list;
41 struct imx_drm_device {
42 struct drm_device *drm;
43 struct imx_drm_crtc *crtc[MAX_CRTC];
45 struct drm_fbdev_cma *fbhelper;
46 struct drm_atomic_state *state;
50 struct drm_crtc *crtc;
51 struct imx_drm_crtc_helper_funcs imx_drm_helper_funcs;
54 #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
55 static int legacyfb_depth = 16;
56 module_param(legacyfb_depth, int, 0444);
59 static void imx_drm_driver_lastclose(struct drm_device *drm)
61 struct imx_drm_device *imxdrm = drm->dev_private;
63 drm_fbdev_cma_restore_mode(imxdrm->fbhelper);
66 static int imx_drm_enable_vblank(struct drm_device *drm, unsigned int pipe)
68 struct imx_drm_device *imxdrm = drm->dev_private;
69 struct imx_drm_crtc *imx_drm_crtc = imxdrm->crtc[pipe];
75 if (!imx_drm_crtc->imx_drm_helper_funcs.enable_vblank)
78 ret = imx_drm_crtc->imx_drm_helper_funcs.enable_vblank(
84 static void imx_drm_disable_vblank(struct drm_device *drm, unsigned int pipe)
86 struct imx_drm_device *imxdrm = drm->dev_private;
87 struct imx_drm_crtc *imx_drm_crtc = imxdrm->crtc[pipe];
92 if (!imx_drm_crtc->imx_drm_helper_funcs.disable_vblank)
95 imx_drm_crtc->imx_drm_helper_funcs.disable_vblank(imx_drm_crtc->crtc);
98 static const struct file_operations imx_drm_driver_fops = {
101 .release = drm_release,
102 .unlocked_ioctl = drm_ioctl,
103 .mmap = drm_gem_cma_mmap,
106 .llseek = noop_llseek,
109 void imx_drm_connector_destroy(struct drm_connector *connector)
111 drm_connector_unregister(connector);
112 drm_connector_cleanup(connector);
114 EXPORT_SYMBOL_GPL(imx_drm_connector_destroy);
116 void imx_drm_encoder_destroy(struct drm_encoder *encoder)
118 drm_encoder_cleanup(encoder);
120 EXPORT_SYMBOL_GPL(imx_drm_encoder_destroy);
122 static void imx_drm_output_poll_changed(struct drm_device *drm)
124 struct imx_drm_device *imxdrm = drm->dev_private;
126 drm_fbdev_cma_hotplug_event(imxdrm->fbhelper);
129 static int imx_drm_atomic_check(struct drm_device *dev,
130 struct drm_atomic_state *state)
134 ret = drm_atomic_helper_check_modeset(dev, state);
138 ret = drm_atomic_helper_check_planes(dev, state);
143 * Check modeset again in case crtc_state->mode_changed is
144 * updated in plane's ->atomic_check callback.
146 ret = drm_atomic_helper_check_modeset(dev, state);
153 static const struct drm_mode_config_funcs imx_drm_mode_config_funcs = {
154 .fb_create = drm_fb_cma_create,
155 .output_poll_changed = imx_drm_output_poll_changed,
156 .atomic_check = imx_drm_atomic_check,
157 .atomic_commit = drm_atomic_helper_commit,
160 static void imx_drm_atomic_commit_tail(struct drm_atomic_state *state)
162 struct drm_device *dev = state->dev;
164 drm_atomic_helper_commit_modeset_disables(dev, state);
166 drm_atomic_helper_commit_planes(dev, state,
167 DRM_PLANE_COMMIT_ACTIVE_ONLY |
168 DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET);
170 drm_atomic_helper_commit_modeset_enables(dev, state);
172 drm_atomic_helper_commit_hw_done(state);
174 drm_atomic_helper_wait_for_vblanks(dev, state);
176 drm_atomic_helper_cleanup_planes(dev, state);
179 static struct drm_mode_config_helper_funcs imx_drm_mode_config_helpers = {
180 .atomic_commit_tail = imx_drm_atomic_commit_tail,
184 * imx_drm_add_crtc - add a new crtc
186 int imx_drm_add_crtc(struct drm_device *drm, struct drm_crtc *crtc,
187 struct imx_drm_crtc **new_crtc, struct drm_plane *primary_plane,
188 const struct imx_drm_crtc_helper_funcs *imx_drm_helper_funcs,
189 struct device_node *port)
191 struct imx_drm_device *imxdrm = drm->dev_private;
192 struct imx_drm_crtc *imx_drm_crtc;
195 * The vblank arrays are dimensioned by MAX_CRTC - we can't
196 * pass IDs greater than this to those functions.
198 if (imxdrm->pipes >= MAX_CRTC)
201 if (imxdrm->drm->open_count)
204 imx_drm_crtc = kzalloc(sizeof(*imx_drm_crtc), GFP_KERNEL);
208 imx_drm_crtc->imx_drm_helper_funcs = *imx_drm_helper_funcs;
209 imx_drm_crtc->crtc = crtc;
213 imxdrm->crtc[imxdrm->pipes++] = imx_drm_crtc;
215 *new_crtc = imx_drm_crtc;
217 drm_crtc_helper_add(crtc,
218 imx_drm_crtc->imx_drm_helper_funcs.crtc_helper_funcs);
220 drm_crtc_init_with_planes(drm, crtc, primary_plane, NULL,
221 imx_drm_crtc->imx_drm_helper_funcs.crtc_funcs, NULL);
225 EXPORT_SYMBOL_GPL(imx_drm_add_crtc);
228 * imx_drm_remove_crtc - remove a crtc
230 int imx_drm_remove_crtc(struct imx_drm_crtc *imx_drm_crtc)
232 struct imx_drm_device *imxdrm = imx_drm_crtc->crtc->dev->dev_private;
233 unsigned int pipe = drm_crtc_index(imx_drm_crtc->crtc);
235 drm_crtc_cleanup(imx_drm_crtc->crtc);
237 imxdrm->crtc[pipe] = NULL;
243 EXPORT_SYMBOL_GPL(imx_drm_remove_crtc);
245 int imx_drm_encoder_parse_of(struct drm_device *drm,
246 struct drm_encoder *encoder, struct device_node *np)
248 uint32_t crtc_mask = drm_of_find_possible_crtcs(drm, np);
251 * If we failed to find the CRTC(s) which this encoder is
252 * supposed to be connected to, it's because the CRTC has
253 * not been registered yet. Defer probing, and hope that
254 * the required CRTC is added later.
257 return -EPROBE_DEFER;
259 encoder->possible_crtcs = crtc_mask;
261 /* FIXME: this is the mask of outputs which can clone this output. */
262 encoder->possible_clones = ~0;
266 EXPORT_SYMBOL_GPL(imx_drm_encoder_parse_of);
268 static const struct drm_ioctl_desc imx_drm_ioctls[] = {
272 static struct drm_driver imx_drm_driver = {
273 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME |
275 .lastclose = imx_drm_driver_lastclose,
276 .gem_free_object_unlocked = drm_gem_cma_free_object,
277 .gem_vm_ops = &drm_gem_cma_vm_ops,
278 .dumb_create = drm_gem_cma_dumb_create,
279 .dumb_map_offset = drm_gem_cma_dumb_map_offset,
280 .dumb_destroy = drm_gem_dumb_destroy,
282 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
283 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
284 .gem_prime_import = drm_gem_prime_import,
285 .gem_prime_export = drm_gem_prime_export,
286 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
287 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
288 .gem_prime_vmap = drm_gem_cma_prime_vmap,
289 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
290 .gem_prime_mmap = drm_gem_cma_prime_mmap,
291 .get_vblank_counter = drm_vblank_no_hw_counter,
292 .enable_vblank = imx_drm_enable_vblank,
293 .disable_vblank = imx_drm_disable_vblank,
294 .ioctls = imx_drm_ioctls,
295 .num_ioctls = ARRAY_SIZE(imx_drm_ioctls),
296 .fops = &imx_drm_driver_fops,
298 .desc = "i.MX DRM graphics",
305 static int compare_of(struct device *dev, void *data)
307 struct device_node *np = data;
309 /* Special case for DI, dev->of_node may not be set yet */
310 if (strcmp(dev->driver->name, "imx-ipuv3-crtc") == 0) {
311 struct ipu_client_platformdata *pdata = dev->platform_data;
313 return pdata->of_node == np;
316 /* Special case for LDB, one device for two channels */
317 if (of_node_cmp(np->name, "lvds-channel") == 0) {
318 np = of_get_parent(np);
322 return dev->of_node == np;
325 static int imx_drm_bind(struct device *dev)
327 struct drm_device *drm;
328 struct imx_drm_device *imxdrm;
331 drm = drm_dev_alloc(&imx_drm_driver, dev);
335 imxdrm = devm_kzalloc(dev, sizeof(*imxdrm), GFP_KERNEL);
342 drm->dev_private = imxdrm;
345 * enable drm irq mode.
346 * - with irq_enabled = true, we can use the vblank feature.
348 * P.S. note that we wouldn't use drm irq handler but
349 * just specific driver own one instead because
350 * drm framework supports only one irq handler and
351 * drivers can well take care of their interrupts
353 drm->irq_enabled = true;
356 * set max width and height as default value(4096x4096).
357 * this value would be used to check framebuffer size limitation
358 * at drm_mode_addfb().
360 drm->mode_config.min_width = 1;
361 drm->mode_config.min_height = 1;
362 drm->mode_config.max_width = 4096;
363 drm->mode_config.max_height = 4096;
364 drm->mode_config.funcs = &imx_drm_mode_config_funcs;
365 drm->mode_config.helper_private = &imx_drm_mode_config_helpers;
367 drm_mode_config_init(drm);
369 ret = drm_vblank_init(drm, MAX_CRTC);
373 dev_set_drvdata(dev, drm);
375 /* Now try and bind all our sub-components */
376 ret = component_bind_all(dev, drm);
380 drm_mode_config_reset(drm);
383 * All components are now initialised, so setup the fb helper.
384 * The fb helper takes copies of key hardware information, so the
385 * crtcs/connectors/encoders must not change after this point.
387 #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
388 if (legacyfb_depth != 16 && legacyfb_depth != 32) {
389 dev_warn(dev, "Invalid legacyfb_depth. Defaulting to 16bpp\n");
392 imxdrm->fbhelper = drm_fbdev_cma_init(drm, legacyfb_depth, MAX_CRTC);
393 if (IS_ERR(imxdrm->fbhelper)) {
394 ret = PTR_ERR(imxdrm->fbhelper);
395 imxdrm->fbhelper = NULL;
400 drm_kms_helper_poll_init(drm);
402 ret = drm_dev_register(drm, 0);
409 drm_kms_helper_poll_fini(drm);
410 #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
411 if (imxdrm->fbhelper)
412 drm_fbdev_cma_fini(imxdrm->fbhelper);
415 component_unbind_all(drm->dev, drm);
417 drm_vblank_cleanup(drm);
419 drm_mode_config_cleanup(drm);
426 static void imx_drm_unbind(struct device *dev)
428 struct drm_device *drm = dev_get_drvdata(dev);
429 struct imx_drm_device *imxdrm = drm->dev_private;
431 drm_dev_unregister(drm);
433 drm_kms_helper_poll_fini(drm);
435 if (imxdrm->fbhelper)
436 drm_fbdev_cma_fini(imxdrm->fbhelper);
438 drm_mode_config_cleanup(drm);
440 component_unbind_all(drm->dev, drm);
441 dev_set_drvdata(dev, NULL);
446 static const struct component_master_ops imx_drm_ops = {
447 .bind = imx_drm_bind,
448 .unbind = imx_drm_unbind,
451 static int imx_drm_platform_probe(struct platform_device *pdev)
453 int ret = drm_of_component_probe(&pdev->dev, compare_of, &imx_drm_ops);
456 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
461 static int imx_drm_platform_remove(struct platform_device *pdev)
463 component_master_del(&pdev->dev, &imx_drm_ops);
467 #ifdef CONFIG_PM_SLEEP
468 static int imx_drm_suspend(struct device *dev)
470 struct drm_device *drm_dev = dev_get_drvdata(dev);
471 struct imx_drm_device *imxdrm;
473 /* The drm_dev is NULL before .load hook is called */
477 drm_kms_helper_poll_disable(drm_dev);
479 imxdrm = drm_dev->dev_private;
480 imxdrm->state = drm_atomic_helper_suspend(drm_dev);
481 if (IS_ERR(imxdrm->state)) {
482 drm_kms_helper_poll_enable(drm_dev);
483 return PTR_ERR(imxdrm->state);
489 static int imx_drm_resume(struct device *dev)
491 struct drm_device *drm_dev = dev_get_drvdata(dev);
492 struct imx_drm_device *imx_drm;
497 imx_drm = drm_dev->dev_private;
498 drm_atomic_helper_resume(drm_dev, imx_drm->state);
499 drm_kms_helper_poll_enable(drm_dev);
505 static SIMPLE_DEV_PM_OPS(imx_drm_pm_ops, imx_drm_suspend, imx_drm_resume);
507 static const struct of_device_id imx_drm_dt_ids[] = {
508 { .compatible = "fsl,imx-display-subsystem", },
511 MODULE_DEVICE_TABLE(of, imx_drm_dt_ids);
513 static struct platform_driver imx_drm_pdrv = {
514 .probe = imx_drm_platform_probe,
515 .remove = imx_drm_platform_remove,
518 .pm = &imx_drm_pm_ops,
519 .of_match_table = imx_drm_dt_ids,
522 module_platform_driver(imx_drm_pdrv);
525 MODULE_DESCRIPTION("i.MX drm driver core");
526 MODULE_LICENSE("GPL");