blob: 4b7b92a7bcf7bdeb37aa5af08ecb336148c25d5d [file] [log] [blame]
Sascha Hauere692da42012-09-21 10:07:47 +02001/*
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 */
Russell King17b50012013-11-03 11:23:34 +000016#include <linux/component.h>
Sascha Hauere692da42012-09-21 10:07:47 +020017#include <linux/device.h>
Liu Ying5f2f9112016-07-08 17:40:59 +080018#include <linux/dma-buf.h>
Russell Kinge7d62312013-11-03 15:38:09 +000019#include <linux/module.h>
Sascha Hauere692da42012-09-21 10:07:47 +020020#include <linux/platform_device.h>
21#include <drm/drmP.h>
Liu Ying5f2f9112016-07-08 17:40:59 +080022#include <drm/drm_atomic.h>
23#include <drm/drm_atomic_helper.h>
Sascha Hauere692da42012-09-21 10:07:47 +020024#include <drm/drm_fb_helper.h>
25#include <drm/drm_crtc_helper.h>
Sascha Hauere692da42012-09-21 10:07:47 +020026#include <drm/drm_gem_cma_helper.h>
27#include <drm/drm_fb_cma_helper.h>
Daniel Vetter3cb9ae42014-10-29 10:03:57 +010028#include <drm/drm_plane_helper.h>
Russell King6457b972014-07-03 17:52:57 +010029#include <drm/drm_of.h>
Philipp Zabel310944d2016-05-12 15:00:44 +020030#include <video/imx-ipu-v3.h>
Sascha Hauere692da42012-09-21 10:07:47 +020031
32#include "imx-drm.h"
33
34#define MAX_CRTC 4
35
Philipp Zabel655b43c2014-03-05 10:20:52 +010036struct imx_drm_component {
37 struct device_node *of_node;
38 struct list_head list;
39};
40
Sascha Hauere692da42012-09-21 10:07:47 +020041struct imx_drm_device {
42 struct drm_device *drm;
Thierry Redingd2ab8ad2015-12-15 12:20:51 +010043 unsigned int pipes;
Sascha Hauere692da42012-09-21 10:07:47 +020044 struct drm_fbdev_cma *fbhelper;
Liu Ying5f2f9112016-07-08 17:40:59 +080045 struct drm_atomic_state *state;
Sascha Hauere692da42012-09-21 10:07:47 +020046};
47
Archit Tanejac1ff5a72015-10-27 13:40:57 +053048#if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
Russell King8acba022013-11-03 12:13:47 +000049static int legacyfb_depth = 16;
50module_param(legacyfb_depth, int, 0444);
Archit Tanejac1ff5a72015-10-27 13:40:57 +053051#endif
Russell King8acba022013-11-03 12:13:47 +000052
Sascha Hauere692da42012-09-21 10:07:47 +020053static void imx_drm_driver_lastclose(struct drm_device *drm)
54{
55 struct imx_drm_device *imxdrm = drm->dev_private;
56
Markus Elfring3f3a7282015-07-05 22:45:23 +020057 drm_fbdev_cma_restore_mode(imxdrm->fbhelper);
Sascha Hauere692da42012-09-21 10:07:47 +020058}
59
Sascha Hauere692da42012-09-21 10:07:47 +020060static const struct file_operations imx_drm_driver_fops = {
61 .owner = THIS_MODULE,
62 .open = drm_open,
63 .release = drm_release,
64 .unlocked_ioctl = drm_ioctl,
65 .mmap = drm_gem_cma_mmap,
66 .poll = drm_poll,
Sascha Hauere692da42012-09-21 10:07:47 +020067 .read = drm_read,
68 .llseek = noop_llseek,
69};
70
Russell King8a51a332013-11-03 13:28:24 +000071void imx_drm_connector_destroy(struct drm_connector *connector)
72{
Thomas Wood34ea3d32014-05-29 16:57:41 +010073 drm_connector_unregister(connector);
Russell King8a51a332013-11-03 13:28:24 +000074 drm_connector_cleanup(connector);
75}
76EXPORT_SYMBOL_GPL(imx_drm_connector_destroy);
77
78void imx_drm_encoder_destroy(struct drm_encoder *encoder)
79{
80 drm_encoder_cleanup(encoder);
81}
82EXPORT_SYMBOL_GPL(imx_drm_encoder_destroy);
83
Russell King3e68439b2013-11-03 22:18:40 +000084static void imx_drm_output_poll_changed(struct drm_device *drm)
85{
Russell King3e68439b2013-11-03 22:18:40 +000086 struct imx_drm_device *imxdrm = drm->dev_private;
87
88 drm_fbdev_cma_hotplug_event(imxdrm->fbhelper);
Russell King3e68439b2013-11-03 22:18:40 +000089}
90
Liu Ying17809992016-08-26 15:30:44 +080091static int imx_drm_atomic_check(struct drm_device *dev,
92 struct drm_atomic_state *state)
93{
94 int ret;
95
96 ret = drm_atomic_helper_check_modeset(dev, state);
97 if (ret)
98 return ret;
99
100 ret = drm_atomic_helper_check_planes(dev, state);
101 if (ret)
102 return ret;
103
104 /*
105 * Check modeset again in case crtc_state->mode_changed is
106 * updated in plane's ->atomic_check callback.
107 */
108 ret = drm_atomic_helper_check_modeset(dev, state);
109 if (ret)
110 return ret;
111
112 return ret;
113}
114
Ville Syrjälä7ae847dd2015-12-15 12:21:09 +0100115static const struct drm_mode_config_funcs imx_drm_mode_config_funcs = {
Russell King1df8b532013-11-03 16:04:48 +0000116 .fb_create = drm_fb_cma_create,
Russell King3e68439b2013-11-03 22:18:40 +0000117 .output_poll_changed = imx_drm_output_poll_changed,
Liu Ying17809992016-08-26 15:30:44 +0800118 .atomic_check = imx_drm_atomic_check,
Marek Vasut782ea2a2016-11-14 11:07:32 +0100119 .atomic_commit = drm_atomic_helper_commit,
Liu Ying5f2f9112016-07-08 17:40:59 +0800120};
121
122static void imx_drm_atomic_commit_tail(struct drm_atomic_state *state)
123{
124 struct drm_device *dev = state->dev;
Liu Ying5f2f9112016-07-08 17:40:59 +0800125
126 drm_atomic_helper_commit_modeset_disables(dev, state);
127
Liu Ying2b58e982016-08-29 17:12:03 +0800128 drm_atomic_helper_commit_planes(dev, state,
Liu Ying5f4df0c2016-08-26 15:30:43 +0800129 DRM_PLANE_COMMIT_ACTIVE_ONLY |
130 DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET);
Liu Ying5f2f9112016-07-08 17:40:59 +0800131
132 drm_atomic_helper_commit_modeset_enables(dev, state);
133
134 drm_atomic_helper_commit_hw_done(state);
135
136 drm_atomic_helper_wait_for_vblanks(dev, state);
137
138 drm_atomic_helper_cleanup_planes(dev, state);
139}
140
Laurent Pincharta4b10cc2017-01-02 11:16:13 +0200141static const struct drm_mode_config_helper_funcs imx_drm_mode_config_helpers = {
Liu Ying5f2f9112016-07-08 17:40:59 +0800142 .atomic_commit_tail = imx_drm_atomic_commit_tail,
Russell King1df8b532013-11-03 16:04:48 +0000143};
144
Sascha Hauere692da42012-09-21 10:07:47 +0200145
Russell King9e2d4102013-11-03 14:04:47 +0000146int imx_drm_encoder_parse_of(struct drm_device *drm,
147 struct drm_encoder *encoder, struct device_node *np)
148{
Russell King6457b972014-07-03 17:52:57 +0100149 uint32_t crtc_mask = drm_of_find_possible_crtcs(drm, np);
Russell King9e2d4102013-11-03 14:04:47 +0000150
Russell King6457b972014-07-03 17:52:57 +0100151 /*
152 * If we failed to find the CRTC(s) which this encoder is
153 * supposed to be connected to, it's because the CRTC has
154 * not been registered yet. Defer probing, and hope that
155 * the required CRTC is added later.
156 */
157 if (crtc_mask == 0)
158 return -EPROBE_DEFER;
Philipp Zabel655b43c2014-03-05 10:20:52 +0100159
Russell King9e2d4102013-11-03 14:04:47 +0000160 encoder->possible_crtcs = crtc_mask;
161
162 /* FIXME: this is the mask of outputs which can clone this output. */
163 encoder->possible_clones = ~0;
164
165 return 0;
166}
167EXPORT_SYMBOL_GPL(imx_drm_encoder_parse_of);
168
Rob Clarkbaa70942013-08-02 13:27:49 -0400169static const struct drm_ioctl_desc imx_drm_ioctls[] = {
Sascha Hauere692da42012-09-21 10:07:47 +0200170 /* none so far */
171};
172
173static struct drm_driver imx_drm_driver = {
Liu Ying8535c022016-07-08 17:41:02 +0800174 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME |
175 DRIVER_ATOMIC,
Sascha Hauere692da42012-09-21 10:07:47 +0200176 .lastclose = imx_drm_driver_lastclose,
Daniel Vetter4b193662016-04-26 19:29:52 +0200177 .gem_free_object_unlocked = drm_gem_cma_free_object,
Sascha Hauere692da42012-09-21 10:07:47 +0200178 .gem_vm_ops = &drm_gem_cma_vm_ops,
179 .dumb_create = drm_gem_cma_dumb_create,
180 .dumb_map_offset = drm_gem_cma_dumb_map_offset,
Daniel Vetter43387b32013-07-16 09:12:04 +0200181 .dumb_destroy = drm_gem_dumb_destroy,
Sascha Hauere692da42012-09-21 10:07:47 +0200182
Philipp Zabelbd3665c2013-10-10 16:18:46 +0200183 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
184 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
185 .gem_prime_import = drm_gem_prime_import,
186 .gem_prime_export = drm_gem_prime_export,
187 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
188 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
189 .gem_prime_vmap = drm_gem_cma_prime_vmap,
190 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
191 .gem_prime_mmap = drm_gem_cma_prime_mmap,
Sascha Hauere692da42012-09-21 10:07:47 +0200192 .ioctls = imx_drm_ioctls,
193 .num_ioctls = ARRAY_SIZE(imx_drm_ioctls),
194 .fops = &imx_drm_driver_fops,
195 .name = "imx-drm",
196 .desc = "i.MX DRM graphics",
197 .date = "20120507",
198 .major = 1,
199 .minor = 0,
200 .patchlevel = 0,
201};
202
Russell King17b50012013-11-03 11:23:34 +0000203static int compare_of(struct device *dev, void *data)
204{
Philipp Zabel655b43c2014-03-05 10:20:52 +0100205 struct device_node *np = data;
206
Philipp Zabel310944d2016-05-12 15:00:44 +0200207 /* Special case for DI, dev->of_node may not be set yet */
208 if (strcmp(dev->driver->name, "imx-ipuv3-crtc") == 0) {
209 struct ipu_client_platformdata *pdata = dev->platform_data;
210
211 return pdata->of_node == np;
212 }
213
Philipp Zabel655b43c2014-03-05 10:20:52 +0100214 /* Special case for LDB, one device for two channels */
215 if (of_node_cmp(np->name, "lvds-channel") == 0) {
216 np = of_get_parent(np);
217 of_node_put(np);
218 }
219
220 return dev->of_node == np;
Russell King17b50012013-11-03 11:23:34 +0000221}
222
Russell King17b50012013-11-03 11:23:34 +0000223static int imx_drm_bind(struct device *dev)
224{
Lucas Stach54db5dec2016-08-11 11:18:48 +0200225 struct drm_device *drm;
226 struct imx_drm_device *imxdrm;
227 int ret;
228
229 drm = drm_dev_alloc(&imx_drm_driver, dev);
Dan Carpenter8cca3542016-10-13 11:53:21 +0300230 if (IS_ERR(drm))
231 return PTR_ERR(drm);
Lucas Stach54db5dec2016-08-11 11:18:48 +0200232
233 imxdrm = devm_kzalloc(dev, sizeof(*imxdrm), GFP_KERNEL);
234 if (!imxdrm) {
235 ret = -ENOMEM;
236 goto err_unref;
237 }
238
239 imxdrm->drm = drm;
240 drm->dev_private = imxdrm;
241
242 /*
243 * enable drm irq mode.
244 * - with irq_enabled = true, we can use the vblank feature.
245 *
246 * P.S. note that we wouldn't use drm irq handler but
247 * just specific driver own one instead because
248 * drm framework supports only one irq handler and
249 * drivers can well take care of their interrupts
250 */
251 drm->irq_enabled = true;
252
253 /*
254 * set max width and height as default value(4096x4096).
255 * this value would be used to check framebuffer size limitation
256 * at drm_mode_addfb().
257 */
Philipp Zabelf57c5112017-01-27 11:57:19 +0100258 drm->mode_config.min_width = 1;
259 drm->mode_config.min_height = 1;
Lucas Stach54db5dec2016-08-11 11:18:48 +0200260 drm->mode_config.max_width = 4096;
261 drm->mode_config.max_height = 4096;
262 drm->mode_config.funcs = &imx_drm_mode_config_funcs;
263 drm->mode_config.helper_private = &imx_drm_mode_config_helpers;
264
265 drm_mode_config_init(drm);
266
267 ret = drm_vblank_init(drm, MAX_CRTC);
268 if (ret)
269 goto err_kms;
270
271 dev_set_drvdata(dev, drm);
272
273 /* Now try and bind all our sub-components */
274 ret = component_bind_all(dev, drm);
275 if (ret)
276 goto err_vblank;
277
278 drm_mode_config_reset(drm);
279
280 /*
281 * All components are now initialised, so setup the fb helper.
282 * The fb helper takes copies of key hardware information, so the
283 * crtcs/connectors/encoders must not change after this point.
284 */
285#if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
286 if (legacyfb_depth != 16 && legacyfb_depth != 32) {
287 dev_warn(dev, "Invalid legacyfb_depth. Defaulting to 16bpp\n");
288 legacyfb_depth = 16;
289 }
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200290 imxdrm->fbhelper = drm_fbdev_cma_init(drm, legacyfb_depth, MAX_CRTC);
Lucas Stach54db5dec2016-08-11 11:18:48 +0200291 if (IS_ERR(imxdrm->fbhelper)) {
292 ret = PTR_ERR(imxdrm->fbhelper);
293 imxdrm->fbhelper = NULL;
294 goto err_unbind;
295 }
296#endif
297
298 drm_kms_helper_poll_init(drm);
299
300 ret = drm_dev_register(drm, 0);
301 if (ret)
302 goto err_fbhelper;
303
304 return 0;
305
306err_fbhelper:
307 drm_kms_helper_poll_fini(drm);
Arnd Bergmann3e3affe2016-09-22 11:50:52 +0200308#if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
Lucas Stach54db5dec2016-08-11 11:18:48 +0200309 if (imxdrm->fbhelper)
310 drm_fbdev_cma_fini(imxdrm->fbhelper);
311err_unbind:
Arnd Bergmann3e3affe2016-09-22 11:50:52 +0200312#endif
Lucas Stach54db5dec2016-08-11 11:18:48 +0200313 component_unbind_all(drm->dev, drm);
314err_vblank:
315 drm_vblank_cleanup(drm);
316err_kms:
317 drm_mode_config_cleanup(drm);
318err_unref:
319 drm_dev_unref(drm);
320
321 return ret;
Russell King17b50012013-11-03 11:23:34 +0000322}
323
324static void imx_drm_unbind(struct device *dev)
325{
Lucas Stach54db5dec2016-08-11 11:18:48 +0200326 struct drm_device *drm = dev_get_drvdata(dev);
327 struct imx_drm_device *imxdrm = drm->dev_private;
328
329 drm_dev_unregister(drm);
330
331 drm_kms_helper_poll_fini(drm);
332
333 if (imxdrm->fbhelper)
334 drm_fbdev_cma_fini(imxdrm->fbhelper);
335
Lucas Stach8e3b16e2016-08-11 11:18:49 +0200336 drm_mode_config_cleanup(drm);
337
Lucas Stach54db5dec2016-08-11 11:18:48 +0200338 component_unbind_all(drm->dev, drm);
339 dev_set_drvdata(dev, NULL);
340
Lucas Stach54db5dec2016-08-11 11:18:48 +0200341 drm_dev_unref(drm);
Russell King17b50012013-11-03 11:23:34 +0000342}
343
344static const struct component_master_ops imx_drm_ops = {
Russell King17b50012013-11-03 11:23:34 +0000345 .bind = imx_drm_bind,
346 .unbind = imx_drm_unbind,
347};
348
Sascha Hauere692da42012-09-21 10:07:47 +0200349static int imx_drm_platform_probe(struct platform_device *pdev)
350{
Liviu Dudau9cace322015-10-20 10:23:13 +0100351 int ret = drm_of_component_probe(&pdev->dev, compare_of, &imx_drm_ops);
Philipp Zabel655b43c2014-03-05 10:20:52 +0100352
Liviu Dudau9cace322015-10-20 10:23:13 +0100353 if (!ret)
354 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
Philipp Zabel655b43c2014-03-05 10:20:52 +0100355
Liviu Dudau9cace322015-10-20 10:23:13 +0100356 return ret;
Sascha Hauere692da42012-09-21 10:07:47 +0200357}
358
359static int imx_drm_platform_remove(struct platform_device *pdev)
360{
Russell King17b50012013-11-03 11:23:34 +0000361 component_master_del(&pdev->dev, &imx_drm_ops);
Sascha Hauere692da42012-09-21 10:07:47 +0200362 return 0;
363}
364
Shawn Guobfe945c2014-09-10 22:43:51 +0800365#ifdef CONFIG_PM_SLEEP
366static int imx_drm_suspend(struct device *dev)
367{
368 struct drm_device *drm_dev = dev_get_drvdata(dev);
Liu Ying5f2f9112016-07-08 17:40:59 +0800369 struct imx_drm_device *imxdrm;
Shawn Guobfe945c2014-09-10 22:43:51 +0800370
371 /* The drm_dev is NULL before .load hook is called */
372 if (drm_dev == NULL)
373 return 0;
374
375 drm_kms_helper_poll_disable(drm_dev);
376
Liu Ying5f2f9112016-07-08 17:40:59 +0800377 imxdrm = drm_dev->dev_private;
378 imxdrm->state = drm_atomic_helper_suspend(drm_dev);
379 if (IS_ERR(imxdrm->state)) {
380 drm_kms_helper_poll_enable(drm_dev);
381 return PTR_ERR(imxdrm->state);
382 }
383
Shawn Guobfe945c2014-09-10 22:43:51 +0800384 return 0;
385}
386
387static int imx_drm_resume(struct device *dev)
388{
389 struct drm_device *drm_dev = dev_get_drvdata(dev);
Liu Ying5f2f9112016-07-08 17:40:59 +0800390 struct imx_drm_device *imx_drm;
Shawn Guobfe945c2014-09-10 22:43:51 +0800391
392 if (drm_dev == NULL)
393 return 0;
394
Liu Ying5f2f9112016-07-08 17:40:59 +0800395 imx_drm = drm_dev->dev_private;
396 drm_atomic_helper_resume(drm_dev, imx_drm->state);
Shawn Guobfe945c2014-09-10 22:43:51 +0800397 drm_kms_helper_poll_enable(drm_dev);
398
399 return 0;
400}
401#endif
402
403static SIMPLE_DEV_PM_OPS(imx_drm_pm_ops, imx_drm_suspend, imx_drm_resume);
404
Russell King17b50012013-11-03 11:23:34 +0000405static const struct of_device_id imx_drm_dt_ids[] = {
Philipp Zabel655b43c2014-03-05 10:20:52 +0100406 { .compatible = "fsl,imx-display-subsystem", },
Russell King17b50012013-11-03 11:23:34 +0000407 { /* sentinel */ },
408};
409MODULE_DEVICE_TABLE(of, imx_drm_dt_ids);
410
Sascha Hauere692da42012-09-21 10:07:47 +0200411static struct platform_driver imx_drm_pdrv = {
412 .probe = imx_drm_platform_probe,
Bill Pemberton99c28f12012-11-19 13:20:51 -0500413 .remove = imx_drm_platform_remove,
Sascha Hauere692da42012-09-21 10:07:47 +0200414 .driver = {
Sascha Hauere692da42012-09-21 10:07:47 +0200415 .name = "imx-drm",
Shawn Guobfe945c2014-09-10 22:43:51 +0800416 .pm = &imx_drm_pm_ops,
Russell King17b50012013-11-03 11:23:34 +0000417 .of_match_table = imx_drm_dt_ids,
Sascha Hauere692da42012-09-21 10:07:47 +0200418 },
419};
Russell Kingb85f2b52013-11-03 15:31:22 +0000420module_platform_driver(imx_drm_pdrv);
Sascha Hauere692da42012-09-21 10:07:47 +0200421
422MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
423MODULE_DESCRIPTION("i.MX drm driver core");
424MODULE_LICENSE("GPL");