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 static int legacyfb_depth = 16;
39 module_param(legacyfb_depth, int, 0444);
41 DEFINE_DRM_GEM_CMA_FOPS(imx_drm_driver_fops);
43 void imx_drm_connector_destroy(struct drm_connector *connector)
45 drm_connector_unregister(connector);
46 drm_connector_cleanup(connector);
48 EXPORT_SYMBOL_GPL(imx_drm_connector_destroy);
50 void imx_drm_encoder_destroy(struct drm_encoder *encoder)
52 drm_encoder_cleanup(encoder);
54 EXPORT_SYMBOL_GPL(imx_drm_encoder_destroy);
56 static int imx_drm_atomic_check(struct drm_device *dev,
57 struct drm_atomic_state *state)
61 ret = drm_atomic_helper_check_modeset(dev, state);
65 ret = drm_atomic_helper_check_planes(dev, state);
70 * Check modeset again in case crtc_state->mode_changed is
71 * updated in plane's ->atomic_check callback.
73 ret = drm_atomic_helper_check_modeset(dev, state);
77 /* Assign PRG/PRE channels and check if all constrains are satisfied. */
78 ret = ipu_planes_assign_pre(dev, state);
85 static const struct drm_mode_config_funcs imx_drm_mode_config_funcs = {
86 .fb_create = drm_gem_fb_create,
87 .atomic_check = imx_drm_atomic_check,
88 .atomic_commit = drm_atomic_helper_commit,
91 static void imx_drm_atomic_commit_tail(struct drm_atomic_state *state)
93 struct drm_device *dev = state->dev;
94 struct drm_plane *plane;
95 struct drm_plane_state *old_plane_state, *new_plane_state;
96 bool plane_disabling = false;
99 drm_atomic_helper_commit_modeset_disables(dev, state);
101 drm_atomic_helper_commit_planes(dev, state,
102 DRM_PLANE_COMMIT_ACTIVE_ONLY |
103 DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET);
105 drm_atomic_helper_commit_modeset_enables(dev, state);
107 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
108 if (drm_atomic_plane_disabling(old_plane_state, new_plane_state))
109 plane_disabling = true;
113 * The flip done wait is only strictly required by imx-drm if a deferred
114 * plane disable is in-flight. As the core requires blocking commits
115 * to wait for the flip it is done here unconditionally. This keeps the
116 * workitem around a bit longer than required for the majority of
117 * non-blocking commits, but we accept that for the sake of simplicity.
119 drm_atomic_helper_wait_for_flip_done(dev, state);
121 if (plane_disabling) {
122 for_each_old_plane_in_state(state, plane, old_plane_state, i)
123 ipu_plane_disable_deferred(plane);
127 drm_atomic_helper_commit_hw_done(state);
130 static const struct drm_mode_config_helper_funcs imx_drm_mode_config_helpers = {
131 .atomic_commit_tail = imx_drm_atomic_commit_tail,
135 int imx_drm_encoder_parse_of(struct drm_device *drm,
136 struct drm_encoder *encoder, struct device_node *np)
138 uint32_t crtc_mask = drm_of_find_possible_crtcs(drm, np);
141 * If we failed to find the CRTC(s) which this encoder is
142 * supposed to be connected to, it's because the CRTC has
143 * not been registered yet. Defer probing, and hope that
144 * the required CRTC is added later.
147 return -EPROBE_DEFER;
149 encoder->possible_crtcs = crtc_mask;
151 /* FIXME: this is the mask of outputs which can clone this output. */
152 encoder->possible_clones = ~0;
156 EXPORT_SYMBOL_GPL(imx_drm_encoder_parse_of);
158 static const struct drm_ioctl_desc imx_drm_ioctls[] = {
162 static struct drm_driver imx_drm_driver = {
163 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME |
165 .gem_free_object_unlocked = drm_gem_cma_free_object,
166 .gem_vm_ops = &drm_gem_cma_vm_ops,
167 .dumb_create = drm_gem_cma_dumb_create,
169 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
170 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
171 .gem_prime_import = drm_gem_prime_import,
172 .gem_prime_export = drm_gem_prime_export,
173 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
174 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
175 .gem_prime_vmap = drm_gem_cma_prime_vmap,
176 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
177 .gem_prime_mmap = drm_gem_cma_prime_mmap,
178 .ioctls = imx_drm_ioctls,
179 .num_ioctls = ARRAY_SIZE(imx_drm_ioctls),
180 .fops = &imx_drm_driver_fops,
182 .desc = "i.MX DRM graphics",
189 static int compare_of(struct device *dev, void *data)
191 struct device_node *np = data;
193 /* Special case for DI, dev->of_node may not be set yet */
194 if (strcmp(dev->driver->name, "imx-ipuv3-crtc") == 0) {
195 struct ipu_client_platformdata *pdata = dev->platform_data;
197 return pdata->of_node == np;
200 /* Special case for LDB, one device for two channels */
201 if (of_node_cmp(np->name, "lvds-channel") == 0) {
202 np = of_get_parent(np);
206 return dev->of_node == np;
209 static int imx_drm_bind(struct device *dev)
211 struct drm_device *drm;
214 drm = drm_dev_alloc(&imx_drm_driver, dev);
219 * enable drm irq mode.
220 * - with irq_enabled = true, we can use the vblank feature.
222 * P.S. note that we wouldn't use drm irq handler but
223 * just specific driver own one instead because
224 * drm framework supports only one irq handler and
225 * drivers can well take care of their interrupts
227 drm->irq_enabled = true;
230 * set max width and height as default value(4096x4096).
231 * this value would be used to check framebuffer size limitation
232 * at drm_mode_addfb().
234 drm->mode_config.min_width = 1;
235 drm->mode_config.min_height = 1;
236 drm->mode_config.max_width = 4096;
237 drm->mode_config.max_height = 4096;
238 drm->mode_config.funcs = &imx_drm_mode_config_funcs;
239 drm->mode_config.helper_private = &imx_drm_mode_config_helpers;
240 drm->mode_config.allow_fb_modifiers = true;
242 drm_mode_config_init(drm);
244 ret = drm_vblank_init(drm, MAX_CRTC);
248 dev_set_drvdata(dev, drm);
250 /* Now try and bind all our sub-components */
251 ret = component_bind_all(dev, drm);
255 drm_mode_config_reset(drm);
258 * All components are now initialised, so setup the fb helper.
259 * The fb helper takes copies of key hardware information, so the
260 * crtcs/connectors/encoders must not change after this point.
262 if (legacyfb_depth != 16 && legacyfb_depth != 32) {
263 dev_warn(dev, "Invalid legacyfb_depth. Defaulting to 16bpp\n");
267 drm_kms_helper_poll_init(drm);
269 ret = drm_dev_register(drm, 0);
273 drm_fbdev_generic_setup(drm, legacyfb_depth);
278 drm_kms_helper_poll_fini(drm);
279 component_unbind_all(drm->dev, drm);
281 drm_mode_config_cleanup(drm);
287 static void imx_drm_unbind(struct device *dev)
289 struct drm_device *drm = dev_get_drvdata(dev);
291 drm_dev_unregister(drm);
293 drm_kms_helper_poll_fini(drm);
295 drm_mode_config_cleanup(drm);
297 component_unbind_all(drm->dev, drm);
298 dev_set_drvdata(dev, NULL);
303 static const struct component_master_ops imx_drm_ops = {
304 .bind = imx_drm_bind,
305 .unbind = imx_drm_unbind,
308 static int imx_drm_platform_probe(struct platform_device *pdev)
310 int ret = drm_of_component_probe(&pdev->dev, compare_of, &imx_drm_ops);
313 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
318 static int imx_drm_platform_remove(struct platform_device *pdev)
320 component_master_del(&pdev->dev, &imx_drm_ops);
324 #ifdef CONFIG_PM_SLEEP
325 static int imx_drm_suspend(struct device *dev)
327 struct drm_device *drm_dev = dev_get_drvdata(dev);
329 return drm_mode_config_helper_suspend(drm_dev);
332 static int imx_drm_resume(struct device *dev)
334 struct drm_device *drm_dev = dev_get_drvdata(dev);
336 return drm_mode_config_helper_resume(drm_dev);
340 static SIMPLE_DEV_PM_OPS(imx_drm_pm_ops, imx_drm_suspend, imx_drm_resume);
342 static const struct of_device_id imx_drm_dt_ids[] = {
343 { .compatible = "fsl,imx-display-subsystem", },
346 MODULE_DEVICE_TABLE(of, imx_drm_dt_ids);
348 static struct platform_driver imx_drm_pdrv = {
349 .probe = imx_drm_platform_probe,
350 .remove = imx_drm_platform_remove,
353 .pm = &imx_drm_pm_ops,
354 .of_match_table = imx_drm_dt_ids,
358 static struct platform_driver * const drivers[] = {
363 static int __init imx_drm_init(void)
365 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
367 module_init(imx_drm_init);
369 static void __exit imx_drm_exit(void)
371 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
373 module_exit(imx_drm_exit);
376 MODULE_DESCRIPTION("i.MX drm driver core");
377 MODULE_LICENSE("GPL");