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