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_gem_framebuffer_helper.h>
28 #include <drm/drm_fb_cma_helper.h>
29 #include <drm/drm_plane_helper.h>
30 #include <drm/drm_of.h>
31 #include <video/imx-ipu-v3.h>
34 #include "ipuv3-plane.h"
38 #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
39 static int legacyfb_depth = 16;
40 module_param(legacyfb_depth, int, 0444);
43 DEFINE_DRM_GEM_CMA_FOPS(imx_drm_driver_fops);
45 void imx_drm_connector_destroy(struct drm_connector *connector)
47 drm_connector_unregister(connector);
48 drm_connector_cleanup(connector);
50 EXPORT_SYMBOL_GPL(imx_drm_connector_destroy);
52 void imx_drm_encoder_destroy(struct drm_encoder *encoder)
54 drm_encoder_cleanup(encoder);
56 EXPORT_SYMBOL_GPL(imx_drm_encoder_destroy);
58 static int imx_drm_atomic_check(struct drm_device *dev,
59 struct drm_atomic_state *state)
63 ret = drm_atomic_helper_check_modeset(dev, state);
67 ret = drm_atomic_helper_check_planes(dev, state);
72 * Check modeset again in case crtc_state->mode_changed is
73 * updated in plane's ->atomic_check callback.
75 ret = drm_atomic_helper_check_modeset(dev, state);
79 /* Assign PRG/PRE channels and check if all constrains are satisfied. */
80 ret = ipu_planes_assign_pre(dev, state);
87 static const struct drm_mode_config_funcs imx_drm_mode_config_funcs = {
88 .fb_create = drm_gem_fb_create,
89 .atomic_check = imx_drm_atomic_check,
90 .atomic_commit = drm_atomic_helper_commit,
93 static void imx_drm_atomic_commit_tail(struct drm_atomic_state *state)
95 struct drm_device *dev = state->dev;
96 struct drm_plane *plane;
97 struct drm_plane_state *old_plane_state, *new_plane_state;
98 bool plane_disabling = false;
101 drm_atomic_helper_commit_modeset_disables(dev, state);
103 drm_atomic_helper_commit_planes(dev, state,
104 DRM_PLANE_COMMIT_ACTIVE_ONLY |
105 DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET);
107 drm_atomic_helper_commit_modeset_enables(dev, state);
109 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
110 if (drm_atomic_plane_disabling(old_plane_state, new_plane_state))
111 plane_disabling = true;
115 * The flip done wait is only strictly required by imx-drm if a deferred
116 * plane disable is in-flight. As the core requires blocking commits
117 * to wait for the flip it is done here unconditionally. This keeps the
118 * workitem around a bit longer than required for the majority of
119 * non-blocking commits, but we accept that for the sake of simplicity.
121 drm_atomic_helper_wait_for_flip_done(dev, state);
123 if (plane_disabling) {
124 for_each_old_plane_in_state(state, plane, old_plane_state, i)
125 ipu_plane_disable_deferred(plane);
129 drm_atomic_helper_commit_hw_done(state);
132 static const struct drm_mode_config_helper_funcs imx_drm_mode_config_helpers = {
133 .atomic_commit_tail = imx_drm_atomic_commit_tail,
137 int imx_drm_encoder_parse_of(struct drm_device *drm,
138 struct drm_encoder *encoder, struct device_node *np)
140 uint32_t crtc_mask = drm_of_find_possible_crtcs(drm, np);
143 * If we failed to find the CRTC(s) which this encoder is
144 * supposed to be connected to, it's because the CRTC has
145 * not been registered yet. Defer probing, and hope that
146 * the required CRTC is added later.
149 return -EPROBE_DEFER;
151 encoder->possible_crtcs = crtc_mask;
153 /* FIXME: this is the mask of outputs which can clone this output. */
154 encoder->possible_clones = ~0;
158 EXPORT_SYMBOL_GPL(imx_drm_encoder_parse_of);
160 static const struct drm_ioctl_desc imx_drm_ioctls[] = {
164 static struct drm_driver imx_drm_driver = {
165 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME |
167 .gem_free_object_unlocked = drm_gem_cma_free_object,
168 .gem_vm_ops = &drm_gem_cma_vm_ops,
169 .dumb_create = drm_gem_cma_dumb_create,
171 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
172 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
173 .gem_prime_import = drm_gem_prime_import,
174 .gem_prime_export = drm_gem_prime_export,
175 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
176 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
177 .gem_prime_vmap = drm_gem_cma_prime_vmap,
178 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
179 .gem_prime_mmap = drm_gem_cma_prime_mmap,
180 .ioctls = imx_drm_ioctls,
181 .num_ioctls = ARRAY_SIZE(imx_drm_ioctls),
182 .fops = &imx_drm_driver_fops,
184 .desc = "i.MX DRM graphics",
191 static int compare_of(struct device *dev, void *data)
193 struct device_node *np = data;
195 /* Special case for DI, dev->of_node may not be set yet */
196 if (strcmp(dev->driver->name, "imx-ipuv3-crtc") == 0) {
197 struct ipu_client_platformdata *pdata = dev->platform_data;
199 return pdata->of_node == np;
202 /* Special case for LDB, one device for two channels */
203 if (of_node_cmp(np->name, "lvds-channel") == 0) {
204 np = of_get_parent(np);
208 return dev->of_node == np;
211 static int imx_drm_bind(struct device *dev)
213 struct drm_device *drm;
216 drm = drm_dev_alloc(&imx_drm_driver, dev);
221 * enable drm irq mode.
222 * - with irq_enabled = true, we can use the vblank feature.
224 * P.S. note that we wouldn't use drm irq handler but
225 * just specific driver own one instead because
226 * drm framework supports only one irq handler and
227 * drivers can well take care of their interrupts
229 drm->irq_enabled = true;
232 * set max width and height as default value(4096x4096).
233 * this value would be used to check framebuffer size limitation
234 * at drm_mode_addfb().
236 drm->mode_config.min_width = 1;
237 drm->mode_config.min_height = 1;
238 drm->mode_config.max_width = 4096;
239 drm->mode_config.max_height = 4096;
240 drm->mode_config.funcs = &imx_drm_mode_config_funcs;
241 drm->mode_config.helper_private = &imx_drm_mode_config_helpers;
242 drm->mode_config.allow_fb_modifiers = true;
244 drm_mode_config_init(drm);
246 ret = drm_vblank_init(drm, MAX_CRTC);
250 dev_set_drvdata(dev, drm);
252 /* Now try and bind all our sub-components */
253 ret = component_bind_all(dev, drm);
257 drm_mode_config_reset(drm);
260 * All components are now initialised, so setup the fb helper.
261 * The fb helper takes copies of key hardware information, so the
262 * crtcs/connectors/encoders must not change after this point.
264 if (legacyfb_depth != 16 && legacyfb_depth != 32) {
265 dev_warn(dev, "Invalid legacyfb_depth. Defaulting to 16bpp\n");
269 drm_kms_helper_poll_init(drm);
271 ret = drm_dev_register(drm, 0);
275 drm_fbdev_generic_setup(drm, legacyfb_depth);
280 drm_kms_helper_poll_fini(drm);
281 component_unbind_all(drm->dev, drm);
283 drm_mode_config_cleanup(drm);
289 static void imx_drm_unbind(struct device *dev)
291 struct drm_device *drm = dev_get_drvdata(dev);
293 drm_dev_unregister(drm);
295 drm_kms_helper_poll_fini(drm);
297 drm_mode_config_cleanup(drm);
299 component_unbind_all(drm->dev, drm);
300 dev_set_drvdata(dev, NULL);
305 static const struct component_master_ops imx_drm_ops = {
306 .bind = imx_drm_bind,
307 .unbind = imx_drm_unbind,
310 static int imx_drm_platform_probe(struct platform_device *pdev)
312 int ret = drm_of_component_probe(&pdev->dev, compare_of, &imx_drm_ops);
315 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
320 static int imx_drm_platform_remove(struct platform_device *pdev)
322 component_master_del(&pdev->dev, &imx_drm_ops);
326 #ifdef CONFIG_PM_SLEEP
327 static int imx_drm_suspend(struct device *dev)
329 struct drm_device *drm_dev = dev_get_drvdata(dev);
331 return drm_mode_config_helper_suspend(drm_dev);
334 static int imx_drm_resume(struct device *dev)
336 struct drm_device *drm_dev = dev_get_drvdata(dev);
338 return drm_mode_config_helper_resume(drm_dev);
342 static SIMPLE_DEV_PM_OPS(imx_drm_pm_ops, imx_drm_suspend, imx_drm_resume);
344 static const struct of_device_id imx_drm_dt_ids[] = {
345 { .compatible = "fsl,imx-display-subsystem", },
348 MODULE_DEVICE_TABLE(of, imx_drm_dt_ids);
350 static struct platform_driver imx_drm_pdrv = {
351 .probe = imx_drm_platform_probe,
352 .remove = imx_drm_platform_remove,
355 .pm = &imx_drm_pm_ops,
356 .of_match_table = imx_drm_dt_ids,
360 static struct platform_driver * const drivers[] = {
365 static int __init imx_drm_init(void)
367 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
369 module_init(imx_drm_init);
371 static void __exit imx_drm_exit(void)
373 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
375 module_exit(imx_drm_exit);
377 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
378 MODULE_DESCRIPTION("i.MX drm driver core");
379 MODULE_LICENSE("GPL");