Commit | Line | Data |
---|---|---|
e692da4d SH |
1 | /* |
2 | * Freescale i.MX drm driver | |
3 | * | |
4 | * Copyright (C) 2011 Sascha Hauer, Pengutronix | |
5 | * | |
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. | |
14 | * | |
15 | */ | |
17b5001b | 16 | #include <linux/component.h> |
e692da4d | 17 | #include <linux/device.h> |
5f2f9115 | 18 | #include <linux/dma-buf.h> |
e7d6231e | 19 | #include <linux/module.h> |
e692da4d SH |
20 | #include <linux/platform_device.h> |
21 | #include <drm/drmP.h> | |
5f2f9115 LY |
22 | #include <drm/drm_atomic.h> |
23 | #include <drm/drm_atomic_helper.h> | |
e692da4d SH |
24 | #include <drm/drm_fb_helper.h> |
25 | #include <drm/drm_crtc_helper.h> | |
e692da4d | 26 | #include <drm/drm_gem_cma_helper.h> |
bd106353 | 27 | #include <drm/drm_gem_framebuffer_helper.h> |
e692da4d | 28 | #include <drm/drm_fb_cma_helper.h> |
3cb9ae4f | 29 | #include <drm/drm_plane_helper.h> |
6457b971 | 30 | #include <drm/drm_of.h> |
310944d1 | 31 | #include <video/imx-ipu-v3.h> |
e692da4d SH |
32 | |
33 | #include "imx-drm.h" | |
eb8c8880 | 34 | #include "ipuv3-plane.h" |
e692da4d SH |
35 | |
36 | #define MAX_CRTC 4 | |
37 | ||
e692da4d SH |
38 | struct imx_drm_device { |
39 | struct drm_device *drm; | |
d2ab8ad9 | 40 | unsigned int pipes; |
e692da4d | 41 | struct drm_fbdev_cma *fbhelper; |
5f2f9115 | 42 | struct drm_atomic_state *state; |
e692da4d SH |
43 | }; |
44 | ||
c1ff5a7a | 45 | #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION) |
8acba02f RK |
46 | static int legacyfb_depth = 16; |
47 | module_param(legacyfb_depth, int, 0444); | |
c1ff5a7a | 48 | #endif |
8acba02f | 49 | |
e692da4d SH |
50 | static void imx_drm_driver_lastclose(struct drm_device *drm) |
51 | { | |
52 | struct imx_drm_device *imxdrm = drm->dev_private; | |
53 | ||
3f3a7280 | 54 | drm_fbdev_cma_restore_mode(imxdrm->fbhelper); |
e692da4d SH |
55 | } |
56 | ||
d55f7e5d | 57 | DEFINE_DRM_GEM_CMA_FOPS(imx_drm_driver_fops); |
e692da4d | 58 | |
8a51a33b RK |
59 | void imx_drm_connector_destroy(struct drm_connector *connector) |
60 | { | |
34ea3d38 | 61 | drm_connector_unregister(connector); |
8a51a33b RK |
62 | drm_connector_cleanup(connector); |
63 | } | |
64 | EXPORT_SYMBOL_GPL(imx_drm_connector_destroy); | |
65 | ||
66 | void imx_drm_encoder_destroy(struct drm_encoder *encoder) | |
67 | { | |
68 | drm_encoder_cleanup(encoder); | |
69 | } | |
70 | EXPORT_SYMBOL_GPL(imx_drm_encoder_destroy); | |
71 | ||
3e68439b RK |
72 | static void imx_drm_output_poll_changed(struct drm_device *drm) |
73 | { | |
3e68439b RK |
74 | struct imx_drm_device *imxdrm = drm->dev_private; |
75 | ||
76 | drm_fbdev_cma_hotplug_event(imxdrm->fbhelper); | |
3e68439b RK |
77 | } |
78 | ||
1780999c LY |
79 | static int imx_drm_atomic_check(struct drm_device *dev, |
80 | struct drm_atomic_state *state) | |
81 | { | |
82 | int ret; | |
83 | ||
84 | ret = drm_atomic_helper_check_modeset(dev, state); | |
85 | if (ret) | |
86 | return ret; | |
87 | ||
88 | ret = drm_atomic_helper_check_planes(dev, state); | |
89 | if (ret) | |
90 | return ret; | |
91 | ||
92 | /* | |
93 | * Check modeset again in case crtc_state->mode_changed is | |
94 | * updated in plane's ->atomic_check callback. | |
95 | */ | |
96 | ret = drm_atomic_helper_check_modeset(dev, state); | |
97 | if (ret) | |
98 | return ret; | |
99 | ||
00514e85 LS |
100 | /* Assign PRG/PRE channels and check if all constrains are satisfied. */ |
101 | ret = ipu_planes_assign_pre(dev, state); | |
102 | if (ret) | |
103 | return ret; | |
104 | ||
1780999c LY |
105 | return ret; |
106 | } | |
107 | ||
7ae847dd | 108 | static const struct drm_mode_config_funcs imx_drm_mode_config_funcs = { |
bd106353 | 109 | .fb_create = drm_gem_fb_create, |
3e68439b | 110 | .output_poll_changed = imx_drm_output_poll_changed, |
1780999c | 111 | .atomic_check = imx_drm_atomic_check, |
782ea2a4 | 112 | .atomic_commit = drm_atomic_helper_commit, |
5f2f9115 LY |
113 | }; |
114 | ||
115 | static void imx_drm_atomic_commit_tail(struct drm_atomic_state *state) | |
116 | { | |
117 | struct drm_device *dev = state->dev; | |
eb8c8880 | 118 | struct drm_plane *plane; |
30ea7521 | 119 | struct drm_plane_state *old_plane_state, *new_plane_state; |
eb8c8880 PZ |
120 | bool plane_disabling = false; |
121 | int i; | |
5f2f9115 LY |
122 | |
123 | drm_atomic_helper_commit_modeset_disables(dev, state); | |
124 | ||
2b58e98d | 125 | drm_atomic_helper_commit_planes(dev, state, |
5f4df0c7 LY |
126 | DRM_PLANE_COMMIT_ACTIVE_ONLY | |
127 | DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET); | |
5f2f9115 LY |
128 | |
129 | drm_atomic_helper_commit_modeset_enables(dev, state); | |
130 | ||
30ea7521 ML |
131 | for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) { |
132 | if (drm_atomic_plane_disabling(old_plane_state, new_plane_state)) | |
eb8c8880 PZ |
133 | plane_disabling = true; |
134 | } | |
135 | ||
a1529920 LS |
136 | /* |
137 | * The flip done wait is only strictly required by imx-drm if a deferred | |
138 | * plane disable is in-flight. As the core requires blocking commits | |
139 | * to wait for the flip it is done here unconditionally. This keeps the | |
140 | * workitem around a bit longer than required for the majority of | |
141 | * non-blocking commits, but we accept that for the sake of simplicity. | |
142 | */ | |
143 | drm_atomic_helper_wait_for_flip_done(dev, state); | |
eb8c8880 | 144 | |
a1529920 | 145 | if (plane_disabling) { |
30ea7521 | 146 | for_each_old_plane_in_state(state, plane, old_plane_state, i) |
eb8c8880 PZ |
147 | ipu_plane_disable_deferred(plane); |
148 | ||
149 | } | |
150 | ||
5f2f9115 | 151 | drm_atomic_helper_commit_hw_done(state); |
5f2f9115 LY |
152 | } |
153 | ||
a4b10cce | 154 | static const struct drm_mode_config_helper_funcs imx_drm_mode_config_helpers = { |
5f2f9115 | 155 | .atomic_commit_tail = imx_drm_atomic_commit_tail, |
1df8b530 RK |
156 | }; |
157 | ||
e692da4d | 158 | |
6457b971 RK |
159 | int imx_drm_encoder_parse_of(struct drm_device *drm, |
160 | struct drm_encoder *encoder, struct device_node *np) | |
9e2d410d | 161 | { |
6457b971 | 162 | uint32_t crtc_mask = drm_of_find_possible_crtcs(drm, np); |
9e2d410d | 163 | |
6457b971 RK |
164 | /* |
165 | * If we failed to find the CRTC(s) which this encoder is | |
166 | * supposed to be connected to, it's because the CRTC has | |
167 | * not been registered yet. Defer probing, and hope that | |
168 | * the required CRTC is added later. | |
169 | */ | |
170 | if (crtc_mask == 0) | |
171 | return -EPROBE_DEFER; | |
655b43cc | 172 | |
6457b971 | 173 | encoder->possible_crtcs = crtc_mask; |
63bc5164 | 174 | |
6457b971 RK |
175 | /* FIXME: this is the mask of outputs which can clone this output. */ |
176 | encoder->possible_clones = ~0; | |
9e2d410d RK |
177 | |
178 | return 0; | |
179 | } | |
6457b971 | 180 | EXPORT_SYMBOL_GPL(imx_drm_encoder_parse_of); |
9e2d410d | 181 | |
baa70943 | 182 | static const struct drm_ioctl_desc imx_drm_ioctls[] = { |
e692da4d SH |
183 | /* none so far */ |
184 | }; | |
185 | ||
186 | static struct drm_driver imx_drm_driver = { | |
8535c022 LY |
187 | .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME | |
188 | DRIVER_ATOMIC, | |
e692da4d | 189 | .lastclose = imx_drm_driver_lastclose, |
4b193663 | 190 | .gem_free_object_unlocked = drm_gem_cma_free_object, |
e692da4d SH |
191 | .gem_vm_ops = &drm_gem_cma_vm_ops, |
192 | .dumb_create = drm_gem_cma_dumb_create, | |
e692da4d | 193 | |
bd3665c9 PZ |
194 | .prime_handle_to_fd = drm_gem_prime_handle_to_fd, |
195 | .prime_fd_to_handle = drm_gem_prime_fd_to_handle, | |
196 | .gem_prime_import = drm_gem_prime_import, | |
197 | .gem_prime_export = drm_gem_prime_export, | |
198 | .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table, | |
199 | .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table, | |
200 | .gem_prime_vmap = drm_gem_cma_prime_vmap, | |
201 | .gem_prime_vunmap = drm_gem_cma_prime_vunmap, | |
202 | .gem_prime_mmap = drm_gem_cma_prime_mmap, | |
e692da4d SH |
203 | .ioctls = imx_drm_ioctls, |
204 | .num_ioctls = ARRAY_SIZE(imx_drm_ioctls), | |
205 | .fops = &imx_drm_driver_fops, | |
206 | .name = "imx-drm", | |
207 | .desc = "i.MX DRM graphics", | |
208 | .date = "20120507", | |
209 | .major = 1, | |
210 | .minor = 0, | |
211 | .patchlevel = 0, | |
212 | }; | |
213 | ||
17b5001b RK |
214 | static int compare_of(struct device *dev, void *data) |
215 | { | |
655b43cc | 216 | struct device_node *np = data; |
17b5001b | 217 | |
310944d1 PZ |
218 | /* Special case for DI, dev->of_node may not be set yet */ |
219 | if (strcmp(dev->driver->name, "imx-ipuv3-crtc") == 0) { | |
220 | struct ipu_client_platformdata *pdata = dev->platform_data; | |
221 | ||
222 | return pdata->of_node == np; | |
223 | } | |
224 | ||
655b43cc PZ |
225 | /* Special case for LDB, one device for two channels */ |
226 | if (of_node_cmp(np->name, "lvds-channel") == 0) { | |
227 | np = of_get_parent(np); | |
228 | of_node_put(np); | |
17b5001b RK |
229 | } |
230 | ||
655b43cc PZ |
231 | return dev->of_node == np; |
232 | } | |
17b5001b | 233 | |
17b5001b RK |
234 | static int imx_drm_bind(struct device *dev) |
235 | { | |
54db5dec LS |
236 | struct drm_device *drm; |
237 | struct imx_drm_device *imxdrm; | |
238 | int ret; | |
239 | ||
240 | drm = drm_dev_alloc(&imx_drm_driver, dev); | |
8cca3548 DC |
241 | if (IS_ERR(drm)) |
242 | return PTR_ERR(drm); | |
54db5dec LS |
243 | |
244 | imxdrm = devm_kzalloc(dev, sizeof(*imxdrm), GFP_KERNEL); | |
245 | if (!imxdrm) { | |
246 | ret = -ENOMEM; | |
247 | goto err_unref; | |
248 | } | |
249 | ||
250 | imxdrm->drm = drm; | |
251 | drm->dev_private = imxdrm; | |
252 | ||
253 | /* | |
254 | * enable drm irq mode. | |
255 | * - with irq_enabled = true, we can use the vblank feature. | |
256 | * | |
257 | * P.S. note that we wouldn't use drm irq handler but | |
258 | * just specific driver own one instead because | |
259 | * drm framework supports only one irq handler and | |
260 | * drivers can well take care of their interrupts | |
261 | */ | |
262 | drm->irq_enabled = true; | |
263 | ||
264 | /* | |
265 | * set max width and height as default value(4096x4096). | |
266 | * this value would be used to check framebuffer size limitation | |
267 | * at drm_mode_addfb(). | |
268 | */ | |
f57c511a PZ |
269 | drm->mode_config.min_width = 1; |
270 | drm->mode_config.min_height = 1; | |
54db5dec LS |
271 | drm->mode_config.max_width = 4096; |
272 | drm->mode_config.max_height = 4096; | |
273 | drm->mode_config.funcs = &imx_drm_mode_config_funcs; | |
274 | drm->mode_config.helper_private = &imx_drm_mode_config_helpers; | |
275 | ||
276 | drm_mode_config_init(drm); | |
277 | ||
278 | ret = drm_vblank_init(drm, MAX_CRTC); | |
279 | if (ret) | |
280 | goto err_kms; | |
281 | ||
282 | dev_set_drvdata(dev, drm); | |
283 | ||
284 | /* Now try and bind all our sub-components */ | |
285 | ret = component_bind_all(dev, drm); | |
286 | if (ret) | |
85769cf7 | 287 | goto err_kms; |
54db5dec LS |
288 | |
289 | drm_mode_config_reset(drm); | |
290 | ||
291 | /* | |
292 | * All components are now initialised, so setup the fb helper. | |
293 | * The fb helper takes copies of key hardware information, so the | |
294 | * crtcs/connectors/encoders must not change after this point. | |
295 | */ | |
296 | #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION) | |
297 | if (legacyfb_depth != 16 && legacyfb_depth != 32) { | |
298 | dev_warn(dev, "Invalid legacyfb_depth. Defaulting to 16bpp\n"); | |
299 | legacyfb_depth = 16; | |
300 | } | |
e4563f6b | 301 | imxdrm->fbhelper = drm_fbdev_cma_init(drm, legacyfb_depth, MAX_CRTC); |
54db5dec LS |
302 | if (IS_ERR(imxdrm->fbhelper)) { |
303 | ret = PTR_ERR(imxdrm->fbhelper); | |
304 | imxdrm->fbhelper = NULL; | |
305 | goto err_unbind; | |
306 | } | |
307 | #endif | |
308 | ||
309 | drm_kms_helper_poll_init(drm); | |
310 | ||
311 | ret = drm_dev_register(drm, 0); | |
312 | if (ret) | |
313 | goto err_fbhelper; | |
314 | ||
315 | return 0; | |
316 | ||
317 | err_fbhelper: | |
318 | drm_kms_helper_poll_fini(drm); | |
3e3affe5 | 319 | #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION) |
54db5dec LS |
320 | if (imxdrm->fbhelper) |
321 | drm_fbdev_cma_fini(imxdrm->fbhelper); | |
322 | err_unbind: | |
3e3affe5 | 323 | #endif |
54db5dec | 324 | component_unbind_all(drm->dev, drm); |
54db5dec LS |
325 | err_kms: |
326 | drm_mode_config_cleanup(drm); | |
327 | err_unref: | |
328 | drm_dev_unref(drm); | |
329 | ||
330 | return ret; | |
17b5001b RK |
331 | } |
332 | ||
333 | static void imx_drm_unbind(struct device *dev) | |
334 | { | |
54db5dec LS |
335 | struct drm_device *drm = dev_get_drvdata(dev); |
336 | struct imx_drm_device *imxdrm = drm->dev_private; | |
337 | ||
338 | drm_dev_unregister(drm); | |
339 | ||
340 | drm_kms_helper_poll_fini(drm); | |
341 | ||
342 | if (imxdrm->fbhelper) | |
343 | drm_fbdev_cma_fini(imxdrm->fbhelper); | |
344 | ||
8e3b16e2 LS |
345 | drm_mode_config_cleanup(drm); |
346 | ||
54db5dec LS |
347 | component_unbind_all(drm->dev, drm); |
348 | dev_set_drvdata(dev, NULL); | |
349 | ||
54db5dec | 350 | drm_dev_unref(drm); |
17b5001b RK |
351 | } |
352 | ||
353 | static const struct component_master_ops imx_drm_ops = { | |
17b5001b RK |
354 | .bind = imx_drm_bind, |
355 | .unbind = imx_drm_unbind, | |
356 | }; | |
357 | ||
e692da4d SH |
358 | static int imx_drm_platform_probe(struct platform_device *pdev) |
359 | { | |
9cace32f | 360 | int ret = drm_of_component_probe(&pdev->dev, compare_of, &imx_drm_ops); |
655b43cc | 361 | |
9cace32f LD |
362 | if (!ret) |
363 | ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32)); | |
655b43cc | 364 | |
9cace32f | 365 | return ret; |
e692da4d SH |
366 | } |
367 | ||
368 | static int imx_drm_platform_remove(struct platform_device *pdev) | |
369 | { | |
17b5001b | 370 | component_master_del(&pdev->dev, &imx_drm_ops); |
e692da4d SH |
371 | return 0; |
372 | } | |
373 | ||
bfe945c8 SG |
374 | #ifdef CONFIG_PM_SLEEP |
375 | static int imx_drm_suspend(struct device *dev) | |
376 | { | |
377 | struct drm_device *drm_dev = dev_get_drvdata(dev); | |
5f2f9115 | 378 | struct imx_drm_device *imxdrm; |
bfe945c8 SG |
379 | |
380 | /* The drm_dev is NULL before .load hook is called */ | |
381 | if (drm_dev == NULL) | |
382 | return 0; | |
383 | ||
384 | drm_kms_helper_poll_disable(drm_dev); | |
385 | ||
5f2f9115 LY |
386 | imxdrm = drm_dev->dev_private; |
387 | imxdrm->state = drm_atomic_helper_suspend(drm_dev); | |
388 | if (IS_ERR(imxdrm->state)) { | |
389 | drm_kms_helper_poll_enable(drm_dev); | |
390 | return PTR_ERR(imxdrm->state); | |
391 | } | |
392 | ||
bfe945c8 SG |
393 | return 0; |
394 | } | |
395 | ||
396 | static int imx_drm_resume(struct device *dev) | |
397 | { | |
398 | struct drm_device *drm_dev = dev_get_drvdata(dev); | |
5f2f9115 | 399 | struct imx_drm_device *imx_drm; |
bfe945c8 SG |
400 | |
401 | if (drm_dev == NULL) | |
402 | return 0; | |
403 | ||
5f2f9115 LY |
404 | imx_drm = drm_dev->dev_private; |
405 | drm_atomic_helper_resume(drm_dev, imx_drm->state); | |
bfe945c8 SG |
406 | drm_kms_helper_poll_enable(drm_dev); |
407 | ||
408 | return 0; | |
409 | } | |
410 | #endif | |
411 | ||
412 | static SIMPLE_DEV_PM_OPS(imx_drm_pm_ops, imx_drm_suspend, imx_drm_resume); | |
413 | ||
17b5001b | 414 | static const struct of_device_id imx_drm_dt_ids[] = { |
655b43cc | 415 | { .compatible = "fsl,imx-display-subsystem", }, |
17b5001b RK |
416 | { /* sentinel */ }, |
417 | }; | |
418 | MODULE_DEVICE_TABLE(of, imx_drm_dt_ids); | |
419 | ||
e692da4d SH |
420 | static struct platform_driver imx_drm_pdrv = { |
421 | .probe = imx_drm_platform_probe, | |
99c28f10 | 422 | .remove = imx_drm_platform_remove, |
e692da4d | 423 | .driver = { |
e692da4d | 424 | .name = "imx-drm", |
bfe945c8 | 425 | .pm = &imx_drm_pm_ops, |
17b5001b | 426 | .of_match_table = imx_drm_dt_ids, |
e692da4d SH |
427 | }, |
428 | }; | |
3d1df96a LS |
429 | |
430 | static struct platform_driver * const drivers[] = { | |
431 | &imx_drm_pdrv, | |
432 | &ipu_drm_driver, | |
433 | }; | |
434 | ||
435 | static int __init imx_drm_init(void) | |
436 | { | |
437 | return platform_register_drivers(drivers, ARRAY_SIZE(drivers)); | |
438 | } | |
439 | module_init(imx_drm_init); | |
440 | ||
441 | static void __exit imx_drm_exit(void) | |
442 | { | |
443 | platform_unregister_drivers(drivers, ARRAY_SIZE(drivers)); | |
444 | } | |
445 | module_exit(imx_drm_exit); | |
e692da4d SH |
446 | |
447 | MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>"); | |
448 | MODULE_DESCRIPTION("i.MX drm driver core"); | |
449 | MODULE_LICENSE("GPL"); |