1 // SPDX-License-Identifier: GPL-2.0+
3 * Freescale i.MX drm driver
5 * Copyright (C) 2011 Sascha Hauer, Pengutronix
8 #include <linux/component.h>
9 #include <linux/device.h>
10 #include <linux/dma-buf.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
14 #include <video/imx-ipu-v3.h>
16 #include <drm/drm_atomic.h>
17 #include <drm/drm_atomic_helper.h>
18 #include <drm/drm_drv.h>
19 #include <drm/drm_fb_cma_helper.h>
20 #include <drm/drm_fb_helper.h>
21 #include <drm/drm_gem_cma_helper.h>
22 #include <drm/drm_gem_framebuffer_helper.h>
23 #include <drm/drm_managed.h>
24 #include <drm/drm_of.h>
25 #include <drm/drm_plane_helper.h>
26 #include <drm/drm_probe_helper.h>
27 #include <drm/drm_vblank.h>
30 #include "ipuv3-plane.h"
34 static int legacyfb_depth = 16;
35 module_param(legacyfb_depth, int, 0444);
37 DEFINE_DRM_GEM_CMA_FOPS(imx_drm_driver_fops);
39 void imx_drm_connector_destroy(struct drm_connector *connector)
41 drm_connector_unregister(connector);
42 drm_connector_cleanup(connector);
44 EXPORT_SYMBOL_GPL(imx_drm_connector_destroy);
46 static int imx_drm_atomic_check(struct drm_device *dev,
47 struct drm_atomic_state *state)
51 ret = drm_atomic_helper_check(dev, state);
56 * Check modeset again in case crtc_state->mode_changed is
57 * updated in plane's ->atomic_check callback.
59 ret = drm_atomic_helper_check_modeset(dev, state);
63 /* Assign PRG/PRE channels and check if all constrains are satisfied. */
64 ret = ipu_planes_assign_pre(dev, state);
71 static const struct drm_mode_config_funcs imx_drm_mode_config_funcs = {
72 .fb_create = drm_gem_fb_create,
73 .atomic_check = imx_drm_atomic_check,
74 .atomic_commit = drm_atomic_helper_commit,
77 static void imx_drm_atomic_commit_tail(struct drm_atomic_state *state)
79 struct drm_device *dev = state->dev;
80 struct drm_plane *plane;
81 struct drm_plane_state *old_plane_state, *new_plane_state;
82 bool plane_disabling = false;
84 bool fence_cookie = dma_fence_begin_signalling();
86 drm_atomic_helper_commit_modeset_disables(dev, state);
88 drm_atomic_helper_commit_planes(dev, state,
89 DRM_PLANE_COMMIT_ACTIVE_ONLY |
90 DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET);
92 drm_atomic_helper_commit_modeset_enables(dev, state);
94 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
95 if (drm_atomic_plane_disabling(old_plane_state, new_plane_state))
96 plane_disabling = true;
100 * The flip done wait is only strictly required by imx-drm if a deferred
101 * plane disable is in-flight. As the core requires blocking commits
102 * to wait for the flip it is done here unconditionally. This keeps the
103 * workitem around a bit longer than required for the majority of
104 * non-blocking commits, but we accept that for the sake of simplicity.
106 drm_atomic_helper_wait_for_flip_done(dev, state);
108 if (plane_disabling) {
109 for_each_old_plane_in_state(state, plane, old_plane_state, i)
110 ipu_plane_disable_deferred(plane);
114 drm_atomic_helper_commit_hw_done(state);
115 dma_fence_end_signalling(fence_cookie);
118 static const struct drm_mode_config_helper_funcs imx_drm_mode_config_helpers = {
119 .atomic_commit_tail = imx_drm_atomic_commit_tail,
123 int imx_drm_encoder_parse_of(struct drm_device *drm,
124 struct drm_encoder *encoder, struct device_node *np)
126 uint32_t crtc_mask = drm_of_find_possible_crtcs(drm, np);
129 * If we failed to find the CRTC(s) which this encoder is
130 * supposed to be connected to, it's because the CRTC has
131 * not been registered yet. Defer probing, and hope that
132 * the required CRTC is added later.
135 return -EPROBE_DEFER;
137 encoder->possible_crtcs = crtc_mask;
139 /* FIXME: cloning support not clear, disable it all for now */
140 encoder->possible_clones = 0;
144 EXPORT_SYMBOL_GPL(imx_drm_encoder_parse_of);
146 static const struct drm_ioctl_desc imx_drm_ioctls[] = {
150 static int imx_drm_dumb_create(struct drm_file *file_priv,
151 struct drm_device *drm,
152 struct drm_mode_create_dumb *args)
154 u32 width = args->width;
157 args->width = ALIGN(width, 8);
159 ret = drm_gem_cma_dumb_create(file_priv, drm, args);
167 static const struct drm_driver imx_drm_driver = {
168 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
169 DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE(imx_drm_dumb_create),
170 .ioctls = imx_drm_ioctls,
171 .num_ioctls = ARRAY_SIZE(imx_drm_ioctls),
172 .fops = &imx_drm_driver_fops,
174 .desc = "i.MX DRM graphics",
181 static int compare_of(struct device *dev, void *data)
183 struct device_node *np = data;
185 /* Special case for DI, dev->of_node may not be set yet */
186 if (strcmp(dev->driver->name, "imx-ipuv3-crtc") == 0) {
187 struct ipu_client_platformdata *pdata = dev->platform_data;
189 return pdata->of_node == np;
192 /* Special case for LDB, one device for two channels */
193 if (of_node_name_eq(np, "lvds-channel")) {
194 np = of_get_parent(np);
198 return dev->of_node == np;
201 static int imx_drm_bind(struct device *dev)
203 struct drm_device *drm;
206 drm = drm_dev_alloc(&imx_drm_driver, dev);
211 * enable drm irq mode.
212 * - with irq_enabled = true, we can use the vblank feature.
214 * P.S. note that we wouldn't use drm irq handler but
215 * just specific driver own one instead because
216 * drm framework supports only one irq handler and
217 * drivers can well take care of their interrupts
219 drm->irq_enabled = true;
222 * set max width and height as default value(4096x4096).
223 * this value would be used to check framebuffer size limitation
224 * at drm_mode_addfb().
226 drm->mode_config.min_width = 1;
227 drm->mode_config.min_height = 1;
228 drm->mode_config.max_width = 4096;
229 drm->mode_config.max_height = 4096;
230 drm->mode_config.funcs = &imx_drm_mode_config_funcs;
231 drm->mode_config.helper_private = &imx_drm_mode_config_helpers;
232 drm->mode_config.normalize_zpos = true;
234 ret = drmm_mode_config_init(drm);
238 ret = drm_vblank_init(drm, MAX_CRTC);
242 dev_set_drvdata(dev, drm);
244 /* Now try and bind all our sub-components */
245 ret = component_bind_all(dev, drm);
249 drm_mode_config_reset(drm);
252 * All components are now initialised, so setup the fb helper.
253 * The fb helper takes copies of key hardware information, so the
254 * crtcs/connectors/encoders must not change after this point.
256 if (legacyfb_depth != 16 && legacyfb_depth != 32) {
257 dev_warn(dev, "Invalid legacyfb_depth. Defaulting to 16bpp\n");
261 drm_kms_helper_poll_init(drm);
263 ret = drm_dev_register(drm, 0);
267 drm_fbdev_generic_setup(drm, legacyfb_depth);
272 drm_kms_helper_poll_fini(drm);
273 component_unbind_all(drm->dev, drm);
280 static void imx_drm_unbind(struct device *dev)
282 struct drm_device *drm = dev_get_drvdata(dev);
284 drm_dev_unregister(drm);
286 drm_kms_helper_poll_fini(drm);
288 component_unbind_all(drm->dev, drm);
292 dev_set_drvdata(dev, NULL);
295 static const struct component_master_ops imx_drm_ops = {
296 .bind = imx_drm_bind,
297 .unbind = imx_drm_unbind,
300 static int imx_drm_platform_probe(struct platform_device *pdev)
302 int ret = drm_of_component_probe(&pdev->dev, compare_of, &imx_drm_ops);
305 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
310 static int imx_drm_platform_remove(struct platform_device *pdev)
312 component_master_del(&pdev->dev, &imx_drm_ops);
316 #ifdef CONFIG_PM_SLEEP
317 static int imx_drm_suspend(struct device *dev)
319 struct drm_device *drm_dev = dev_get_drvdata(dev);
321 return drm_mode_config_helper_suspend(drm_dev);
324 static int imx_drm_resume(struct device *dev)
326 struct drm_device *drm_dev = dev_get_drvdata(dev);
328 return drm_mode_config_helper_resume(drm_dev);
332 static SIMPLE_DEV_PM_OPS(imx_drm_pm_ops, imx_drm_suspend, imx_drm_resume);
334 static const struct of_device_id imx_drm_dt_ids[] = {
335 { .compatible = "fsl,imx-display-subsystem", },
338 MODULE_DEVICE_TABLE(of, imx_drm_dt_ids);
340 static struct platform_driver imx_drm_pdrv = {
341 .probe = imx_drm_platform_probe,
342 .remove = imx_drm_platform_remove,
345 .pm = &imx_drm_pm_ops,
346 .of_match_table = imx_drm_dt_ids,
350 static struct platform_driver * const drivers[] = {
355 static int __init imx_drm_init(void)
357 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
359 module_init(imx_drm_init);
361 static void __exit imx_drm_exit(void)
363 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
365 module_exit(imx_drm_exit);
368 MODULE_DESCRIPTION("i.MX drm driver core");
369 MODULE_LICENSE("GPL");