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