xref: /linux/drivers/gpu/drm/imx/ipuv3/imx-drm-core.c (revision e70140ba0d2b1a30467d4af6bcfe761327b9ec95)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Freescale i.MX drm driver
4  *
5  * Copyright (C) 2011 Sascha Hauer, Pengutronix
6  */
7 
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>
13 
14 #include <video/imx-ipu-v3.h>
15 
16 #include <drm/drm_atomic.h>
17 #include <drm/drm_atomic_helper.h>
18 #include <drm/drm_client_setup.h>
19 #include <drm/drm_drv.h>
20 #include <drm/drm_fbdev_dma.h>
21 #include <drm/drm_gem_dma_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_probe_helper.h>
26 #include <drm/drm_vblank.h>
27 
28 #include "imx-drm.h"
29 #include "ipuv3-plane.h"
30 
31 #define MAX_CRTC	4
32 
33 static int legacyfb_depth = 16;
34 module_param(legacyfb_depth, int, 0444);
35 
36 DEFINE_DRM_GEM_DMA_FOPS(imx_drm_driver_fops);
37 
imx_drm_atomic_check(struct drm_device * dev,struct drm_atomic_state * state)38 static int imx_drm_atomic_check(struct drm_device *dev,
39 				struct drm_atomic_state *state)
40 {
41 	int ret;
42 
43 	ret = drm_atomic_helper_check(dev, state);
44 	if (ret)
45 		return ret;
46 
47 	/*
48 	 * Check modeset again in case crtc_state->mode_changed is
49 	 * updated in plane's ->atomic_check callback.
50 	 */
51 	ret = drm_atomic_helper_check_modeset(dev, state);
52 	if (ret)
53 		return ret;
54 
55 	/* Assign PRG/PRE channels and check if all constrains are satisfied. */
56 	ret = ipu_planes_assign_pre(dev, state);
57 	if (ret)
58 		return ret;
59 
60 	return ret;
61 }
62 
63 static const struct drm_mode_config_funcs imx_drm_mode_config_funcs = {
64 	.fb_create = drm_gem_fb_create,
65 	.atomic_check = imx_drm_atomic_check,
66 	.atomic_commit = drm_atomic_helper_commit,
67 };
68 
imx_drm_atomic_commit_tail(struct drm_atomic_state * state)69 static void imx_drm_atomic_commit_tail(struct drm_atomic_state *state)
70 {
71 	struct drm_device *dev = state->dev;
72 	struct drm_plane *plane;
73 	struct drm_plane_state *old_plane_state, *new_plane_state;
74 	bool plane_disabling = false;
75 	int i;
76 
77 	drm_atomic_helper_commit_modeset_disables(dev, state);
78 
79 	drm_atomic_helper_commit_planes(dev, state,
80 				DRM_PLANE_COMMIT_ACTIVE_ONLY |
81 				DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET);
82 
83 	drm_atomic_helper_commit_modeset_enables(dev, state);
84 
85 	for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
86 		if (drm_atomic_plane_disabling(old_plane_state, new_plane_state))
87 			plane_disabling = true;
88 	}
89 
90 	/*
91 	 * The flip done wait is only strictly required by imx-drm if a deferred
92 	 * plane disable is in-flight. As the core requires blocking commits
93 	 * to wait for the flip it is done here unconditionally. This keeps the
94 	 * workitem around a bit longer than required for the majority of
95 	 * non-blocking commits, but we accept that for the sake of simplicity.
96 	 */
97 	drm_atomic_helper_wait_for_flip_done(dev, state);
98 
99 	if (plane_disabling) {
100 		for_each_old_plane_in_state(state, plane, old_plane_state, i)
101 			ipu_plane_disable_deferred(plane);
102 
103 	}
104 
105 	drm_atomic_helper_commit_hw_done(state);
106 }
107 
108 static const struct drm_mode_config_helper_funcs imx_drm_mode_config_helpers = {
109 	.atomic_commit_tail = imx_drm_atomic_commit_tail,
110 };
111 
112 
imx_drm_encoder_parse_of(struct drm_device * drm,struct drm_encoder * encoder,struct device_node * np)113 int imx_drm_encoder_parse_of(struct drm_device *drm,
114 	struct drm_encoder *encoder, struct device_node *np)
115 {
116 	uint32_t crtc_mask = drm_of_find_possible_crtcs(drm, np);
117 
118 	/*
119 	 * If we failed to find the CRTC(s) which this encoder is
120 	 * supposed to be connected to, it's because the CRTC has
121 	 * not been registered yet.  Defer probing, and hope that
122 	 * the required CRTC is added later.
123 	 */
124 	if (crtc_mask == 0)
125 		return -EPROBE_DEFER;
126 
127 	encoder->possible_crtcs = crtc_mask;
128 
129 	/* FIXME: cloning support not clear, disable it all for now */
130 	encoder->possible_clones = 0;
131 
132 	return 0;
133 }
134 EXPORT_SYMBOL_GPL(imx_drm_encoder_parse_of);
135 
136 static const struct drm_ioctl_desc imx_drm_ioctls[] = {
137 	/* none so far */
138 };
139 
imx_drm_dumb_create(struct drm_file * file_priv,struct drm_device * drm,struct drm_mode_create_dumb * args)140 static int imx_drm_dumb_create(struct drm_file *file_priv,
141 			       struct drm_device *drm,
142 			       struct drm_mode_create_dumb *args)
143 {
144 	u32 width = args->width;
145 	int ret;
146 
147 	args->width = ALIGN(width, 8);
148 
149 	ret = drm_gem_dma_dumb_create(file_priv, drm, args);
150 	if (ret)
151 		return ret;
152 
153 	args->width = width;
154 	return ret;
155 }
156 
157 static const struct drm_driver imx_drm_driver = {
158 	.driver_features	= DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
159 	DRM_GEM_DMA_DRIVER_OPS_WITH_DUMB_CREATE(imx_drm_dumb_create),
160 	DRM_FBDEV_DMA_DRIVER_OPS,
161 	.ioctls			= imx_drm_ioctls,
162 	.num_ioctls		= ARRAY_SIZE(imx_drm_ioctls),
163 	.fops			= &imx_drm_driver_fops,
164 	.name			= "imx-drm",
165 	.desc			= "i.MX DRM graphics",
166 	.date			= "20120507",
167 	.major			= 1,
168 	.minor			= 0,
169 	.patchlevel		= 0,
170 };
171 
compare_of(struct device * dev,void * data)172 static int compare_of(struct device *dev, void *data)
173 {
174 	struct device_node *np = data;
175 
176 	/* Special case for DI, dev->of_node may not be set yet */
177 	if (strcmp(dev->driver->name, "imx-ipuv3-crtc") == 0) {
178 		struct ipu_client_platformdata *pdata = dev->platform_data;
179 
180 		return pdata->of_node == np;
181 	}
182 
183 	/* Special case for LDB, one device for two channels */
184 	if (of_node_name_eq(np, "lvds-channel")) {
185 		np = of_get_parent(np);
186 		of_node_put(np);
187 	}
188 
189 	return dev->of_node == np;
190 }
191 
imx_drm_bind(struct device * dev)192 static int imx_drm_bind(struct device *dev)
193 {
194 	struct drm_device *drm;
195 	int ret;
196 
197 	drm = drm_dev_alloc(&imx_drm_driver, dev);
198 	if (IS_ERR(drm))
199 		return PTR_ERR(drm);
200 
201 	/*
202 	 * set max width and height as default value(4096x4096).
203 	 * this value would be used to check framebuffer size limitation
204 	 * at drm_mode_addfb().
205 	 */
206 	drm->mode_config.min_width = 1;
207 	drm->mode_config.min_height = 1;
208 	drm->mode_config.max_width = 4096;
209 	drm->mode_config.max_height = 4096;
210 	drm->mode_config.funcs = &imx_drm_mode_config_funcs;
211 	drm->mode_config.helper_private = &imx_drm_mode_config_helpers;
212 	drm->mode_config.normalize_zpos = true;
213 
214 	ret = drmm_mode_config_init(drm);
215 	if (ret)
216 		goto err_kms;
217 
218 	ret = drm_vblank_init(drm, MAX_CRTC);
219 	if (ret)
220 		goto err_kms;
221 
222 	dev_set_drvdata(dev, drm);
223 
224 	/* Now try and bind all our sub-components */
225 	ret = component_bind_all(dev, drm);
226 	if (ret)
227 		goto err_kms;
228 
229 	drm_mode_config_reset(drm);
230 
231 	/*
232 	 * All components are now initialised, so setup the fb helper.
233 	 * The fb helper takes copies of key hardware information, so the
234 	 * crtcs/connectors/encoders must not change after this point.
235 	 */
236 	if (legacyfb_depth != 16 && legacyfb_depth != 32) {
237 		dev_warn(dev, "Invalid legacyfb_depth.  Defaulting to 16bpp\n");
238 		legacyfb_depth = 16;
239 	}
240 
241 	drm_kms_helper_poll_init(drm);
242 
243 	ret = drm_dev_register(drm, 0);
244 	if (ret)
245 		goto err_poll_fini;
246 
247 	drm_client_setup_with_color_mode(drm, legacyfb_depth);
248 
249 	return 0;
250 
251 err_poll_fini:
252 	drm_kms_helper_poll_fini(drm);
253 	component_unbind_all(drm->dev, drm);
254 err_kms:
255 	dev_set_drvdata(dev, NULL);
256 	drm_dev_put(drm);
257 
258 	return ret;
259 }
260 
imx_drm_unbind(struct device * dev)261 static void imx_drm_unbind(struct device *dev)
262 {
263 	struct drm_device *drm = dev_get_drvdata(dev);
264 
265 	drm_dev_unregister(drm);
266 
267 	drm_kms_helper_poll_fini(drm);
268 	drm_atomic_helper_shutdown(drm);
269 
270 	component_unbind_all(drm->dev, drm);
271 
272 	drm_dev_put(drm);
273 
274 	dev_set_drvdata(dev, NULL);
275 }
276 
277 static const struct component_master_ops imx_drm_ops = {
278 	.bind = imx_drm_bind,
279 	.unbind = imx_drm_unbind,
280 };
281 
imx_drm_platform_probe(struct platform_device * pdev)282 static int imx_drm_platform_probe(struct platform_device *pdev)
283 {
284 	int ret = drm_of_component_probe(&pdev->dev, compare_of, &imx_drm_ops);
285 
286 	if (!ret)
287 		ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
288 
289 	return ret;
290 }
291 
imx_drm_platform_remove(struct platform_device * pdev)292 static void imx_drm_platform_remove(struct platform_device *pdev)
293 {
294 	component_master_del(&pdev->dev, &imx_drm_ops);
295 }
296 
imx_drm_platform_shutdown(struct platform_device * pdev)297 static void imx_drm_platform_shutdown(struct platform_device *pdev)
298 {
299 	drm_atomic_helper_shutdown(platform_get_drvdata(pdev));
300 }
301 
302 #ifdef CONFIG_PM_SLEEP
imx_drm_suspend(struct device * dev)303 static int imx_drm_suspend(struct device *dev)
304 {
305 	struct drm_device *drm_dev = dev_get_drvdata(dev);
306 
307 	return drm_mode_config_helper_suspend(drm_dev);
308 }
309 
imx_drm_resume(struct device * dev)310 static int imx_drm_resume(struct device *dev)
311 {
312 	struct drm_device *drm_dev = dev_get_drvdata(dev);
313 
314 	return drm_mode_config_helper_resume(drm_dev);
315 }
316 #endif
317 
318 static SIMPLE_DEV_PM_OPS(imx_drm_pm_ops, imx_drm_suspend, imx_drm_resume);
319 
320 static const struct of_device_id imx_drm_dt_ids[] = {
321 	{ .compatible = "fsl,imx-display-subsystem", },
322 	{ /* sentinel */ },
323 };
324 MODULE_DEVICE_TABLE(of, imx_drm_dt_ids);
325 
326 static struct platform_driver imx_drm_pdrv = {
327 	.probe		= imx_drm_platform_probe,
328 	.remove		= imx_drm_platform_remove,
329 	.shutdown	= imx_drm_platform_shutdown,
330 	.driver		= {
331 		.name	= "imx-drm",
332 		.pm	= &imx_drm_pm_ops,
333 		.of_match_table = imx_drm_dt_ids,
334 	},
335 };
336 
337 static struct platform_driver * const drivers[] = {
338 	&imx_drm_pdrv,
339 	&ipu_drm_driver,
340 };
341 
imx_drm_init(void)342 static int __init imx_drm_init(void)
343 {
344 	if (drm_firmware_drivers_only())
345 		return -ENODEV;
346 
347 	return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
348 }
349 module_init(imx_drm_init);
350 
imx_drm_exit(void)351 static void __exit imx_drm_exit(void)
352 {
353 	platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
354 }
355 module_exit(imx_drm_exit);
356 
357 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
358 MODULE_DESCRIPTION("i.MX drm driver core");
359 MODULE_LICENSE("GPL");
360