1 // SPDX-License-Identifier: GPL-2.0+
3 * Freescale i.MX drm driver
5 * Copyright (C) 2011 Sascha Hauer, Pengutronix
7 #include <linux/component.h>
8 #include <linux/device.h>
9 #include <linux/dma-buf.h>
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
13 #include <drm/drm_atomic.h>
14 #include <drm/drm_atomic_helper.h>
15 #include <drm/drm_fb_helper.h>
16 #include <drm/drm_crtc_helper.h>
17 #include <drm/drm_gem_cma_helper.h>
18 #include <drm/drm_gem_framebuffer_helper.h>
19 #include <drm/drm_fb_cma_helper.h>
20 #include <drm/drm_plane_helper.h>
21 #include <drm/drm_of.h>
22 #include <video/imx-ipu-v3.h>
25 #include "ipuv3-plane.h"
29 static int legacyfb_depth = 16;
30 module_param(legacyfb_depth, int, 0444);
32 DEFINE_DRM_GEM_CMA_FOPS(imx_drm_driver_fops);
34 void imx_drm_connector_destroy(struct drm_connector *connector)
36 drm_connector_unregister(connector);
37 drm_connector_cleanup(connector);
39 EXPORT_SYMBOL_GPL(imx_drm_connector_destroy);
41 void imx_drm_encoder_destroy(struct drm_encoder *encoder)
43 drm_encoder_cleanup(encoder);
45 EXPORT_SYMBOL_GPL(imx_drm_encoder_destroy);
47 static int imx_drm_atomic_check(struct drm_device *dev,
48 struct drm_atomic_state *state)
52 ret = drm_atomic_helper_check_modeset(dev, state);
56 ret = drm_atomic_helper_check_planes(dev, state);
61 * Check modeset again in case crtc_state->mode_changed is
62 * updated in plane's ->atomic_check callback.
64 ret = drm_atomic_helper_check_modeset(dev, state);
68 /* Assign PRG/PRE channels and check if all constrains are satisfied. */
69 ret = ipu_planes_assign_pre(dev, state);
76 static const struct drm_mode_config_funcs imx_drm_mode_config_funcs = {
77 .fb_create = drm_gem_fb_create,
78 .atomic_check = imx_drm_atomic_check,
79 .atomic_commit = drm_atomic_helper_commit,
82 static void imx_drm_atomic_commit_tail(struct drm_atomic_state *state)
84 struct drm_device *dev = state->dev;
85 struct drm_plane *plane;
86 struct drm_plane_state *old_plane_state, *new_plane_state;
87 bool plane_disabling = false;
90 drm_atomic_helper_commit_modeset_disables(dev, state);
92 drm_atomic_helper_commit_planes(dev, state,
93 DRM_PLANE_COMMIT_ACTIVE_ONLY |
94 DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET);
96 drm_atomic_helper_commit_modeset_enables(dev, state);
98 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
99 if (drm_atomic_plane_disabling(old_plane_state, new_plane_state))
100 plane_disabling = true;
104 * The flip done wait is only strictly required by imx-drm if a deferred
105 * plane disable is in-flight. As the core requires blocking commits
106 * to wait for the flip it is done here unconditionally. This keeps the
107 * workitem around a bit longer than required for the majority of
108 * non-blocking commits, but we accept that for the sake of simplicity.
110 drm_atomic_helper_wait_for_flip_done(dev, state);
112 if (plane_disabling) {
113 for_each_old_plane_in_state(state, plane, old_plane_state, i)
114 ipu_plane_disable_deferred(plane);
118 drm_atomic_helper_commit_hw_done(state);
121 static const struct drm_mode_config_helper_funcs imx_drm_mode_config_helpers = {
122 .atomic_commit_tail = imx_drm_atomic_commit_tail,
126 int imx_drm_encoder_parse_of(struct drm_device *drm,
127 struct drm_encoder *encoder, struct device_node *np)
129 uint32_t crtc_mask = drm_of_find_possible_crtcs(drm, np);
132 * If we failed to find the CRTC(s) which this encoder is
133 * supposed to be connected to, it's because the CRTC has
134 * not been registered yet. Defer probing, and hope that
135 * the required CRTC is added later.
138 return -EPROBE_DEFER;
140 encoder->possible_crtcs = crtc_mask;
142 /* FIXME: this is the mask of outputs which can clone this output. */
143 encoder->possible_clones = ~0;
147 EXPORT_SYMBOL_GPL(imx_drm_encoder_parse_of);
149 static const struct drm_ioctl_desc imx_drm_ioctls[] = {
153 static struct drm_driver imx_drm_driver = {
154 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME |
156 .gem_free_object_unlocked = drm_gem_cma_free_object,
157 .gem_vm_ops = &drm_gem_cma_vm_ops,
158 .dumb_create = drm_gem_cma_dumb_create,
160 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
161 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
162 .gem_prime_import = drm_gem_prime_import,
163 .gem_prime_export = drm_gem_prime_export,
164 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
165 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
166 .gem_prime_vmap = drm_gem_cma_prime_vmap,
167 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
168 .gem_prime_mmap = drm_gem_cma_prime_mmap,
169 .ioctls = imx_drm_ioctls,
170 .num_ioctls = ARRAY_SIZE(imx_drm_ioctls),
171 .fops = &imx_drm_driver_fops,
173 .desc = "i.MX DRM graphics",
180 static int compare_of(struct device *dev, void *data)
182 struct device_node *np = data;
184 /* Special case for DI, dev->of_node may not be set yet */
185 if (strcmp(dev->driver->name, "imx-ipuv3-crtc") == 0) {
186 struct ipu_client_platformdata *pdata = dev->platform_data;
188 return pdata->of_node == np;
191 /* Special case for LDB, one device for two channels */
192 if (of_node_cmp(np->name, "lvds-channel") == 0) {
193 np = of_get_parent(np);
197 return dev->of_node == np;
200 static int imx_drm_bind(struct device *dev)
202 struct drm_device *drm;
205 drm = drm_dev_alloc(&imx_drm_driver, dev);
210 * enable drm irq mode.
211 * - with irq_enabled = true, we can use the vblank feature.
213 * P.S. note that we wouldn't use drm irq handler but
214 * just specific driver own one instead because
215 * drm framework supports only one irq handler and
216 * drivers can well take care of their interrupts
218 drm->irq_enabled = true;
221 * set max width and height as default value(4096x4096).
222 * this value would be used to check framebuffer size limitation
223 * at drm_mode_addfb().
225 drm->mode_config.min_width = 1;
226 drm->mode_config.min_height = 1;
227 drm->mode_config.max_width = 4096;
228 drm->mode_config.max_height = 4096;
229 drm->mode_config.funcs = &imx_drm_mode_config_funcs;
230 drm->mode_config.helper_private = &imx_drm_mode_config_helpers;
231 drm->mode_config.allow_fb_modifiers = true;
233 drm_mode_config_init(drm);
235 ret = drm_vblank_init(drm, MAX_CRTC);
239 dev_set_drvdata(dev, drm);
241 /* Now try and bind all our sub-components */
242 ret = component_bind_all(dev, drm);
246 drm_mode_config_reset(drm);
249 * All components are now initialised, so setup the fb helper.
250 * The fb helper takes copies of key hardware information, so the
251 * crtcs/connectors/encoders must not change after this point.
253 if (legacyfb_depth != 16 && legacyfb_depth != 32) {
254 dev_warn(dev, "Invalid legacyfb_depth. Defaulting to 16bpp\n");
258 drm_kms_helper_poll_init(drm);
260 ret = drm_dev_register(drm, 0);
264 drm_fbdev_generic_setup(drm, legacyfb_depth);
269 drm_kms_helper_poll_fini(drm);
270 component_unbind_all(drm->dev, drm);
272 drm_mode_config_cleanup(drm);
278 static void imx_drm_unbind(struct device *dev)
280 struct drm_device *drm = dev_get_drvdata(dev);
282 drm_dev_unregister(drm);
284 drm_kms_helper_poll_fini(drm);
286 drm_mode_config_cleanup(drm);
288 component_unbind_all(drm->dev, drm);
289 dev_set_drvdata(dev, NULL);
294 static const struct component_master_ops imx_drm_ops = {
295 .bind = imx_drm_bind,
296 .unbind = imx_drm_unbind,
299 static int imx_drm_platform_probe(struct platform_device *pdev)
301 int ret = drm_of_component_probe(&pdev->dev, compare_of, &imx_drm_ops);
304 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
309 static int imx_drm_platform_remove(struct platform_device *pdev)
311 component_master_del(&pdev->dev, &imx_drm_ops);
315 #ifdef CONFIG_PM_SLEEP
316 static int imx_drm_suspend(struct device *dev)
318 struct drm_device *drm_dev = dev_get_drvdata(dev);
320 return drm_mode_config_helper_suspend(drm_dev);
323 static int imx_drm_resume(struct device *dev)
325 struct drm_device *drm_dev = dev_get_drvdata(dev);
327 return drm_mode_config_helper_resume(drm_dev);
331 static SIMPLE_DEV_PM_OPS(imx_drm_pm_ops, imx_drm_suspend, imx_drm_resume);
333 static const struct of_device_id imx_drm_dt_ids[] = {
334 { .compatible = "fsl,imx-display-subsystem", },
337 MODULE_DEVICE_TABLE(of, imx_drm_dt_ids);
339 static struct platform_driver imx_drm_pdrv = {
340 .probe = imx_drm_platform_probe,
341 .remove = imx_drm_platform_remove,
344 .pm = &imx_drm_pm_ops,
345 .of_match_table = imx_drm_dt_ids,
349 static struct platform_driver * const drivers[] = {
354 static int __init imx_drm_init(void)
356 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
358 module_init(imx_drm_init);
360 static void __exit imx_drm_exit(void)
362 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
364 module_exit(imx_drm_exit);
367 MODULE_DESCRIPTION("i.MX drm driver core");
368 MODULE_LICENSE("GPL");