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