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