dt-bindings: mtd: ingenic: Use standard ecc-engine property
[linux/fpc-iii.git] / drivers / gpu / drm / imx / imx-drm-core.c
blobc935cbe059a79c323306fb2d70f6684c972d3fe0
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Freescale i.MX drm driver
5 * Copyright (C) 2011 Sascha Hauer, Pengutronix
6 */
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>
12 #include <drm/drmP.h>
13 #include <drm/drm_atomic.h>
14 #include <drm/drm_atomic_helper.h>
15 #include <drm/drm_fb_cma_helper.h>
16 #include <drm/drm_fb_helper.h>
17 #include <drm/drm_gem_cma_helper.h>
18 #include <drm/drm_gem_framebuffer_helper.h>
19 #include <drm/drm_of.h>
20 #include <drm/drm_plane_helper.h>
21 #include <drm/drm_probe_helper.h>
22 #include <video/imx-ipu-v3.h>
24 #include "imx-drm.h"
25 #include "ipuv3-plane.h"
27 #define MAX_CRTC 4
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)
50 int ret;
52 ret = drm_atomic_helper_check(dev, state);
53 if (ret)
54 return ret;
57 * Check modeset again in case crtc_state->mode_changed is
58 * updated in plane's ->atomic_check callback.
60 ret = drm_atomic_helper_check_modeset(dev, state);
61 if (ret)
62 return ret;
64 /* Assign PRG/PRE channels and check if all constrains are satisfied. */
65 ret = ipu_planes_assign_pre(dev, state);
66 if (ret)
67 return ret;
69 return ret;
72 static const struct drm_mode_config_funcs imx_drm_mode_config_funcs = {
73 .fb_create = drm_gem_fb_create,
74 .atomic_check = imx_drm_atomic_check,
75 .atomic_commit = drm_atomic_helper_commit,
78 static void imx_drm_atomic_commit_tail(struct drm_atomic_state *state)
80 struct drm_device *dev = state->dev;
81 struct drm_plane *plane;
82 struct drm_plane_state *old_plane_state, *new_plane_state;
83 bool plane_disabling = false;
84 int i;
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);
117 static const struct drm_mode_config_helper_funcs imx_drm_mode_config_helpers = {
118 .atomic_commit_tail = imx_drm_atomic_commit_tail,
122 int imx_drm_encoder_parse_of(struct drm_device *drm,
123 struct drm_encoder *encoder, struct device_node *np)
125 uint32_t crtc_mask = drm_of_find_possible_crtcs(drm, np);
128 * If we failed to find the CRTC(s) which this encoder is
129 * supposed to be connected to, it's because the CRTC has
130 * not been registered yet. Defer probing, and hope that
131 * the required CRTC is added later.
133 if (crtc_mask == 0)
134 return -EPROBE_DEFER;
136 encoder->possible_crtcs = crtc_mask;
138 /* FIXME: this is the mask of outputs which can clone this output. */
139 encoder->possible_clones = ~0;
141 return 0;
143 EXPORT_SYMBOL_GPL(imx_drm_encoder_parse_of);
145 static const struct drm_ioctl_desc imx_drm_ioctls[] = {
146 /* none so far */
149 static struct drm_driver imx_drm_driver = {
150 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME |
151 DRIVER_ATOMIC,
152 .gem_free_object_unlocked = drm_gem_cma_free_object,
153 .gem_vm_ops = &drm_gem_cma_vm_ops,
154 .dumb_create = drm_gem_cma_dumb_create,
156 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
157 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
158 .gem_prime_import = drm_gem_prime_import,
159 .gem_prime_export = drm_gem_prime_export,
160 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
161 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
162 .gem_prime_vmap = drm_gem_cma_prime_vmap,
163 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
164 .gem_prime_mmap = drm_gem_cma_prime_mmap,
165 .ioctls = imx_drm_ioctls,
166 .num_ioctls = ARRAY_SIZE(imx_drm_ioctls),
167 .fops = &imx_drm_driver_fops,
168 .name = "imx-drm",
169 .desc = "i.MX DRM graphics",
170 .date = "20120507",
171 .major = 1,
172 .minor = 0,
173 .patchlevel = 0,
176 static int compare_of(struct device *dev, void *data)
178 struct device_node *np = data;
180 /* Special case for DI, dev->of_node may not be set yet */
181 if (strcmp(dev->driver->name, "imx-ipuv3-crtc") == 0) {
182 struct ipu_client_platformdata *pdata = dev->platform_data;
184 return pdata->of_node == np;
187 /* Special case for LDB, one device for two channels */
188 if (of_node_cmp(np->name, "lvds-channel") == 0) {
189 np = of_get_parent(np);
190 of_node_put(np);
193 return dev->of_node == np;
196 static int imx_drm_bind(struct device *dev)
198 struct drm_device *drm;
199 int ret;
201 drm = drm_dev_alloc(&imx_drm_driver, dev);
202 if (IS_ERR(drm))
203 return PTR_ERR(drm);
206 * enable drm irq mode.
207 * - with irq_enabled = true, we can use the vblank feature.
209 * P.S. note that we wouldn't use drm irq handler but
210 * just specific driver own one instead because
211 * drm framework supports only one irq handler and
212 * drivers can well take care of their interrupts
214 drm->irq_enabled = true;
217 * set max width and height as default value(4096x4096).
218 * this value would be used to check framebuffer size limitation
219 * at drm_mode_addfb().
221 drm->mode_config.min_width = 1;
222 drm->mode_config.min_height = 1;
223 drm->mode_config.max_width = 4096;
224 drm->mode_config.max_height = 4096;
225 drm->mode_config.funcs = &imx_drm_mode_config_funcs;
226 drm->mode_config.helper_private = &imx_drm_mode_config_helpers;
227 drm->mode_config.allow_fb_modifiers = true;
228 drm->mode_config.normalize_zpos = true;
230 drm_mode_config_init(drm);
232 ret = drm_vblank_init(drm, MAX_CRTC);
233 if (ret)
234 goto err_kms;
236 dev_set_drvdata(dev, drm);
238 /* Now try and bind all our sub-components */
239 ret = component_bind_all(dev, drm);
240 if (ret)
241 goto err_kms;
243 drm_mode_config_reset(drm);
246 * All components are now initialised, so setup the fb helper.
247 * The fb helper takes copies of key hardware information, so the
248 * crtcs/connectors/encoders must not change after this point.
250 if (legacyfb_depth != 16 && legacyfb_depth != 32) {
251 dev_warn(dev, "Invalid legacyfb_depth. Defaulting to 16bpp\n");
252 legacyfb_depth = 16;
255 drm_kms_helper_poll_init(drm);
257 ret = drm_dev_register(drm, 0);
258 if (ret)
259 goto err_poll_fini;
261 drm_fbdev_generic_setup(drm, legacyfb_depth);
263 return 0;
265 err_poll_fini:
266 drm_kms_helper_poll_fini(drm);
267 component_unbind_all(drm->dev, drm);
268 err_kms:
269 drm_mode_config_cleanup(drm);
270 drm_dev_put(drm);
272 return ret;
275 static void imx_drm_unbind(struct device *dev)
277 struct drm_device *drm = dev_get_drvdata(dev);
279 drm_dev_unregister(drm);
281 drm_kms_helper_poll_fini(drm);
283 drm_mode_config_cleanup(drm);
285 component_unbind_all(drm->dev, drm);
286 dev_set_drvdata(dev, NULL);
288 drm_dev_put(drm);
291 static const struct component_master_ops imx_drm_ops = {
292 .bind = imx_drm_bind,
293 .unbind = imx_drm_unbind,
296 static int imx_drm_platform_probe(struct platform_device *pdev)
298 int ret = drm_of_component_probe(&pdev->dev, compare_of, &imx_drm_ops);
300 if (!ret)
301 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
303 return ret;
306 static int imx_drm_platform_remove(struct platform_device *pdev)
308 component_master_del(&pdev->dev, &imx_drm_ops);
309 return 0;
312 #ifdef CONFIG_PM_SLEEP
313 static int imx_drm_suspend(struct device *dev)
315 struct drm_device *drm_dev = dev_get_drvdata(dev);
317 return drm_mode_config_helper_suspend(drm_dev);
320 static int imx_drm_resume(struct device *dev)
322 struct drm_device *drm_dev = dev_get_drvdata(dev);
324 return drm_mode_config_helper_resume(drm_dev);
326 #endif
328 static SIMPLE_DEV_PM_OPS(imx_drm_pm_ops, imx_drm_suspend, imx_drm_resume);
330 static const struct of_device_id imx_drm_dt_ids[] = {
331 { .compatible = "fsl,imx-display-subsystem", },
332 { /* sentinel */ },
334 MODULE_DEVICE_TABLE(of, imx_drm_dt_ids);
336 static struct platform_driver imx_drm_pdrv = {
337 .probe = imx_drm_platform_probe,
338 .remove = imx_drm_platform_remove,
339 .driver = {
340 .name = "imx-drm",
341 .pm = &imx_drm_pm_ops,
342 .of_match_table = imx_drm_dt_ids,
346 static struct platform_driver * const drivers[] = {
347 &imx_drm_pdrv,
348 &ipu_drm_driver,
351 static int __init imx_drm_init(void)
353 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
355 module_init(imx_drm_init);
357 static void __exit imx_drm_exit(void)
359 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
361 module_exit(imx_drm_exit);
363 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
364 MODULE_DESCRIPTION("i.MX drm driver core");
365 MODULE_LICENSE("GPL");