Linux 4.6
[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>
e7d6231e
RK
18#include <linux/fb.h>
19#include <linux/module.h>
e692da4d
SH
20#include <linux/platform_device.h>
21#include <drm/drmP.h>
22#include <drm/drm_fb_helper.h>
23#include <drm/drm_crtc_helper.h>
e692da4d
SH
24#include <drm/drm_gem_cma_helper.h>
25#include <drm/drm_fb_cma_helper.h>
3cb9ae4f 26#include <drm/drm_plane_helper.h>
6457b971 27#include <drm/drm_of.h>
e692da4d
SH
28
29#include "imx-drm.h"
30
31#define MAX_CRTC 4
32
655b43cc
PZ
33struct imx_drm_component {
34 struct device_node *of_node;
35 struct list_head list;
36};
37
e692da4d
SH
38struct imx_drm_device {
39 struct drm_device *drm;
887eceac 40 struct imx_drm_crtc *crtc[MAX_CRTC];
d2ab8ad9 41 unsigned int pipes;
e692da4d
SH
42 struct drm_fbdev_cma *fbhelper;
43};
44
45struct imx_drm_crtc {
46 struct drm_crtc *crtc;
e692da4d 47 struct imx_drm_crtc_helper_funcs imx_drm_helper_funcs;
e692da4d
SH
48};
49
c1ff5a7a 50#if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
8acba02f
RK
51static int legacyfb_depth = 16;
52module_param(legacyfb_depth, int, 0444);
c1ff5a7a 53#endif
8acba02f 54
d2ab8ad9 55unsigned int imx_drm_crtc_id(struct imx_drm_crtc *crtc)
b8d181e4 56{
d4b20e4d 57 return drm_crtc_index(crtc->crtc);
b8d181e4 58}
9c74360f 59EXPORT_SYMBOL_GPL(imx_drm_crtc_id);
b8d181e4 60
e692da4d
SH
61static void imx_drm_driver_lastclose(struct drm_device *drm)
62{
63 struct imx_drm_device *imxdrm = drm->dev_private;
64
3f3a7280 65 drm_fbdev_cma_restore_mode(imxdrm->fbhelper);
e692da4d
SH
66}
67
68static int imx_drm_driver_unload(struct drm_device *drm)
69{
70 struct imx_drm_device *imxdrm = drm->dev_private;
3e68439b
RK
71
72 drm_kms_helper_poll_fini(drm);
e692da4d 73
8acba02f
RK
74 if (imxdrm->fbhelper)
75 drm_fbdev_cma_fini(imxdrm->fbhelper);
8acba02f 76
17b5001b
RK
77 component_unbind_all(drm->dev, drm);
78
020a9ea7 79 drm_vblank_cleanup(drm);
020a9ea7 80 drm_mode_config_cleanup(drm);
e692da4d 81
bfe945c8
SG
82 platform_set_drvdata(drm->platformdev, NULL);
83
e692da4d
SH
84 return 0;
85}
86
fdffa6f2 87static struct imx_drm_crtc *imx_drm_find_crtc(struct drm_crtc *crtc)
e692da4d 88{
32266b45 89 struct imx_drm_device *imxdrm = crtc->dev->dev_private;
887eceac
RK
90 unsigned i;
91
92 for (i = 0; i < MAX_CRTC; i++)
93 if (imxdrm->crtc[i] && imxdrm->crtc[i]->crtc == crtc)
94 return imxdrm->crtc[i];
e692da4d 95
e692da4d
SH
96 return NULL;
97}
98
2872c807
PZ
99int imx_drm_set_bus_format_pins(struct drm_encoder *encoder, u32 bus_format,
100 int hsync_pin, int vsync_pin)
e692da4d 101{
e692da4d 102 struct imx_drm_crtc_helper_funcs *helper;
887eceac 103 struct imx_drm_crtc *imx_crtc;
e692da4d 104
f2d66aad 105 imx_crtc = imx_drm_find_crtc(encoder->crtc);
887eceac
RK
106 if (!imx_crtc)
107 return -EINVAL;
e692da4d 108
e692da4d
SH
109 helper = &imx_crtc->imx_drm_helper_funcs;
110 if (helper->set_interface_pix_fmt)
f2d66aad 111 return helper->set_interface_pix_fmt(encoder->crtc,
2872c807 112 bus_format, hsync_pin, vsync_pin);
e692da4d
SH
113 return 0;
114}
2872c807 115EXPORT_SYMBOL_GPL(imx_drm_set_bus_format_pins);
2ea42608 116
2872c807 117int imx_drm_set_bus_format(struct drm_encoder *encoder, u32 bus_format)
2ea42608 118{
2872c807 119 return imx_drm_set_bus_format_pins(encoder, bus_format, 2, 3);
2ea42608 120}
2872c807 121EXPORT_SYMBOL_GPL(imx_drm_set_bus_format);
e692da4d
SH
122
123int imx_drm_crtc_vblank_get(struct imx_drm_crtc *imx_drm_crtc)
124{
d4b20e4d 125 return drm_crtc_vblank_get(imx_drm_crtc->crtc);
e692da4d
SH
126}
127EXPORT_SYMBOL_GPL(imx_drm_crtc_vblank_get);
128
129void imx_drm_crtc_vblank_put(struct imx_drm_crtc *imx_drm_crtc)
130{
d4b20e4d 131 drm_crtc_vblank_put(imx_drm_crtc->crtc);
e692da4d
SH
132}
133EXPORT_SYMBOL_GPL(imx_drm_crtc_vblank_put);
134
135void imx_drm_handle_vblank(struct imx_drm_crtc *imx_drm_crtc)
136{
d4b20e4d 137 drm_crtc_handle_vblank(imx_drm_crtc->crtc);
e692da4d
SH
138}
139EXPORT_SYMBOL_GPL(imx_drm_handle_vblank);
140
88e72717 141static int imx_drm_enable_vblank(struct drm_device *drm, unsigned int pipe)
e692da4d
SH
142{
143 struct imx_drm_device *imxdrm = drm->dev_private;
88e72717 144 struct imx_drm_crtc *imx_drm_crtc = imxdrm->crtc[pipe];
e692da4d
SH
145 int ret;
146
e692da4d
SH
147 if (!imx_drm_crtc)
148 return -EINVAL;
149
150 if (!imx_drm_crtc->imx_drm_helper_funcs.enable_vblank)
151 return -ENOSYS;
152
153 ret = imx_drm_crtc->imx_drm_helper_funcs.enable_vblank(
154 imx_drm_crtc->crtc);
155
156 return ret;
157}
158
88e72717 159static void imx_drm_disable_vblank(struct drm_device *drm, unsigned int pipe)
e692da4d
SH
160{
161 struct imx_drm_device *imxdrm = drm->dev_private;
88e72717 162 struct imx_drm_crtc *imx_drm_crtc = imxdrm->crtc[pipe];
e692da4d 163
e692da4d
SH
164 if (!imx_drm_crtc)
165 return;
166
167 if (!imx_drm_crtc->imx_drm_helper_funcs.disable_vblank)
168 return;
169
170 imx_drm_crtc->imx_drm_helper_funcs.disable_vblank(imx_drm_crtc->crtc);
171}
172
173static const struct file_operations imx_drm_driver_fops = {
174 .owner = THIS_MODULE,
175 .open = drm_open,
176 .release = drm_release,
177 .unlocked_ioctl = drm_ioctl,
178 .mmap = drm_gem_cma_mmap,
179 .poll = drm_poll,
e692da4d
SH
180 .read = drm_read,
181 .llseek = noop_llseek,
182};
183
8a51a33b
RK
184void imx_drm_connector_destroy(struct drm_connector *connector)
185{
34ea3d38 186 drm_connector_unregister(connector);
8a51a33b
RK
187 drm_connector_cleanup(connector);
188}
189EXPORT_SYMBOL_GPL(imx_drm_connector_destroy);
190
191void imx_drm_encoder_destroy(struct drm_encoder *encoder)
192{
193 drm_encoder_cleanup(encoder);
194}
195EXPORT_SYMBOL_GPL(imx_drm_encoder_destroy);
196
3e68439b
RK
197static void imx_drm_output_poll_changed(struct drm_device *drm)
198{
3e68439b
RK
199 struct imx_drm_device *imxdrm = drm->dev_private;
200
201 drm_fbdev_cma_hotplug_event(imxdrm->fbhelper);
3e68439b
RK
202}
203
7ae847dd 204static const struct drm_mode_config_funcs imx_drm_mode_config_funcs = {
1df8b530 205 .fb_create = drm_fb_cma_create,
3e68439b 206 .output_poll_changed = imx_drm_output_poll_changed,
1df8b530
RK
207};
208
e692da4d 209/*
17b5001b
RK
210 * Main DRM initialisation. This binds, initialises and registers
211 * with DRM the subcomponents of the driver.
e692da4d
SH
212 */
213static int imx_drm_driver_load(struct drm_device *drm, unsigned long flags)
214{
b85f2b5d 215 struct imx_drm_device *imxdrm;
e355e7dd 216 struct drm_connector *connector;
e692da4d
SH
217 int ret;
218
b85f2b5d
RK
219 imxdrm = devm_kzalloc(drm->dev, sizeof(*imxdrm), GFP_KERNEL);
220 if (!imxdrm)
221 return -ENOMEM;
222
e692da4d
SH
223 imxdrm->drm = drm;
224
225 drm->dev_private = imxdrm;
226
227 /*
228 * enable drm irq mode.
4423843c 229 * - with irq_enabled = true, we can use the vblank feature.
e692da4d
SH
230 *
231 * P.S. note that we wouldn't use drm irq handler but
232 * just specific driver own one instead because
233 * drm framework supports only one irq handler and
234 * drivers can well take care of their interrupts
235 */
4423843c 236 drm->irq_enabled = true;
e692da4d 237
1df8b530
RK
238 /*
239 * set max width and height as default value(4096x4096).
240 * this value would be used to check framebuffer size limitation
241 * at drm_mode_addfb().
242 */
243 drm->mode_config.min_width = 64;
244 drm->mode_config.min_height = 64;
245 drm->mode_config.max_width = 4096;
246 drm->mode_config.max_height = 4096;
247 drm->mode_config.funcs = &imx_drm_mode_config_funcs;
248
e692da4d 249 drm_mode_config_init(drm);
e692da4d 250
020a9ea7 251 ret = drm_vblank_init(drm, MAX_CRTC);
e692da4d 252 if (ret)
8007875f 253 goto err_kms;
e692da4d
SH
254
255 /*
e7d6231e
RK
256 * with vblank_disable_allowed = true, vblank interrupt will be
257 * disabled by drm timer once a current process gives up ownership
258 * of vblank event. (after drm_vblank_put function is called)
e692da4d 259 */
020a9ea7 260 drm->vblank_disable_allowed = true;
e692da4d 261
a72f8bee 262 platform_set_drvdata(drm->platformdev, drm);
8d71de61 263
17b5001b
RK
264 /* Now try and bind all our sub-components */
265 ret = component_bind_all(drm->dev, drm);
266 if (ret)
ccec7f62 267 goto err_vblank;
e355e7dd
RK
268
269 /*
270 * All components are now added, we can publish the connector sysfs
271 * entries to userspace. This will generate hotplug events and so
272 * userspace will expect to be able to access DRM at this point.
273 */
274 list_for_each_entry(connector, &drm->mode_config.connector_list, head) {
34ea3d38 275 ret = drm_connector_register(connector);
e355e7dd
RK
276 if (ret) {
277 dev_err(drm->dev,
34ea3d38 278 "[CONNECTOR:%d:%s] drm_connector_register failed: %d\n",
e355e7dd 279 connector->base.id,
4a2a450f 280 connector->name, ret);
e355e7dd
RK
281 goto err_unbind;
282 }
283 }
284
8acba02f
RK
285 /*
286 * All components are now initialised, so setup the fb helper.
287 * The fb helper takes copies of key hardware information, so the
288 * crtcs/connectors/encoders must not change after this point.
289 */
c1ff5a7a 290#if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
8acba02f
RK
291 if (legacyfb_depth != 16 && legacyfb_depth != 32) {
292 dev_warn(drm->dev, "Invalid legacyfb_depth. Defaulting to 16bpp\n");
293 legacyfb_depth = 16;
294 }
4314e19e 295 drm_helper_disable_unused_functions(drm);
8acba02f
RK
296 imxdrm->fbhelper = drm_fbdev_cma_init(drm, legacyfb_depth,
297 drm->mode_config.num_crtc, MAX_CRTC);
298 if (IS_ERR(imxdrm->fbhelper)) {
299 ret = PTR_ERR(imxdrm->fbhelper);
300 imxdrm->fbhelper = NULL;
301 goto err_unbind;
302 }
303#endif
3e68439b
RK
304
305 drm_kms_helper_poll_init(drm);
306
8007875f 307 return 0;
a72f8bee 308
e355e7dd
RK
309err_unbind:
310 component_unbind_all(drm->dev, drm);
ccec7f62 311err_vblank:
8007875f
RK
312 drm_vblank_cleanup(drm);
313err_kms:
8007875f 314 drm_mode_config_cleanup(drm);
e692da4d
SH
315
316 return ret;
317}
318
e692da4d
SH
319/*
320 * imx_drm_add_crtc - add a new crtc
e692da4d 321 */
32266b45 322int imx_drm_add_crtc(struct drm_device *drm, struct drm_crtc *crtc,
43895599 323 struct imx_drm_crtc **new_crtc, struct drm_plane *primary_plane,
e692da4d 324 const struct imx_drm_crtc_helper_funcs *imx_drm_helper_funcs,
655b43cc 325 struct device_node *port)
e692da4d 326{
32266b45 327 struct imx_drm_device *imxdrm = drm->dev_private;
e692da4d 328 struct imx_drm_crtc *imx_drm_crtc;
e692da4d 329
fd6040ed
RK
330 /*
331 * The vblank arrays are dimensioned by MAX_CRTC - we can't
332 * pass IDs greater than this to those functions.
333 */
e7d6231e
RK
334 if (imxdrm->pipes >= MAX_CRTC)
335 return -EINVAL;
fd6040ed 336
e7d6231e
RK
337 if (imxdrm->drm->open_count)
338 return -EBUSY;
e692da4d
SH
339
340 imx_drm_crtc = kzalloc(sizeof(*imx_drm_crtc), GFP_KERNEL);
e7d6231e
RK
341 if (!imx_drm_crtc)
342 return -ENOMEM;
e692da4d
SH
343
344 imx_drm_crtc->imx_drm_helper_funcs = *imx_drm_helper_funcs;
e692da4d 345 imx_drm_crtc->crtc = crtc;
e692da4d 346
6457b971
RK
347 crtc->port = port;
348
d4b20e4d 349 imxdrm->crtc[imxdrm->pipes++] = imx_drm_crtc;
e692da4d
SH
350
351 *new_crtc = imx_drm_crtc;
352
ec9557d7
RK
353 drm_crtc_helper_add(crtc,
354 imx_drm_crtc->imx_drm_helper_funcs.crtc_helper_funcs);
355
43895599 356 drm_crtc_init_with_planes(drm, crtc, primary_plane, NULL,
f9882876 357 imx_drm_crtc->imx_drm_helper_funcs.crtc_funcs, NULL);
ec9557d7 358
e692da4d 359 return 0;
e692da4d
SH
360}
361EXPORT_SYMBOL_GPL(imx_drm_add_crtc);
362
363/*
364 * imx_drm_remove_crtc - remove a crtc
365 */
366int imx_drm_remove_crtc(struct imx_drm_crtc *imx_drm_crtc)
367{
ccec7f62 368 struct imx_drm_device *imxdrm = imx_drm_crtc->crtc->dev->dev_private;
d4b20e4d 369 unsigned int pipe = drm_crtc_index(imx_drm_crtc->crtc);
e692da4d
SH
370
371 drm_crtc_cleanup(imx_drm_crtc->crtc);
372
d4b20e4d 373 imxdrm->crtc[pipe] = NULL;
e692da4d 374
e692da4d
SH
375 kfree(imx_drm_crtc);
376
377 return 0;
378}
379EXPORT_SYMBOL_GPL(imx_drm_remove_crtc);
380
6457b971
RK
381int imx_drm_encoder_parse_of(struct drm_device *drm,
382 struct drm_encoder *encoder, struct device_node *np)
9e2d410d 383{
6457b971 384 uint32_t crtc_mask = drm_of_find_possible_crtcs(drm, np);
9e2d410d 385
6457b971
RK
386 /*
387 * If we failed to find the CRTC(s) which this encoder is
388 * supposed to be connected to, it's because the CRTC has
389 * not been registered yet. Defer probing, and hope that
390 * the required CRTC is added later.
391 */
392 if (crtc_mask == 0)
393 return -EPROBE_DEFER;
655b43cc 394
6457b971 395 encoder->possible_crtcs = crtc_mask;
63bc5164 396
6457b971
RK
397 /* FIXME: this is the mask of outputs which can clone this output. */
398 encoder->possible_clones = ~0;
9e2d410d
RK
399
400 return 0;
401}
6457b971 402EXPORT_SYMBOL_GPL(imx_drm_encoder_parse_of);
9e2d410d 403
baa70943 404static const struct drm_ioctl_desc imx_drm_ioctls[] = {
e692da4d
SH
405 /* none so far */
406};
407
408static struct drm_driver imx_drm_driver = {
bd3665c9 409 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME,
e692da4d
SH
410 .load = imx_drm_driver_load,
411 .unload = imx_drm_driver_unload,
e692da4d 412 .lastclose = imx_drm_driver_lastclose,
915b4d11 413 .set_busid = drm_platform_set_busid,
e692da4d
SH
414 .gem_free_object = drm_gem_cma_free_object,
415 .gem_vm_ops = &drm_gem_cma_vm_ops,
416 .dumb_create = drm_gem_cma_dumb_create,
417 .dumb_map_offset = drm_gem_cma_dumb_map_offset,
43387b37 418 .dumb_destroy = drm_gem_dumb_destroy,
e692da4d 419
bd3665c9
PZ
420 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
421 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
422 .gem_prime_import = drm_gem_prime_import,
423 .gem_prime_export = drm_gem_prime_export,
424 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
425 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
426 .gem_prime_vmap = drm_gem_cma_prime_vmap,
427 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
428 .gem_prime_mmap = drm_gem_cma_prime_mmap,
b44f8408 429 .get_vblank_counter = drm_vblank_no_hw_counter,
e692da4d
SH
430 .enable_vblank = imx_drm_enable_vblank,
431 .disable_vblank = imx_drm_disable_vblank,
432 .ioctls = imx_drm_ioctls,
433 .num_ioctls = ARRAY_SIZE(imx_drm_ioctls),
434 .fops = &imx_drm_driver_fops,
435 .name = "imx-drm",
436 .desc = "i.MX DRM graphics",
437 .date = "20120507",
438 .major = 1,
439 .minor = 0,
440 .patchlevel = 0,
441};
442
17b5001b
RK
443static int compare_of(struct device *dev, void *data)
444{
655b43cc 445 struct device_node *np = data;
17b5001b 446
655b43cc
PZ
447 /* Special case for LDB, one device for two channels */
448 if (of_node_cmp(np->name, "lvds-channel") == 0) {
449 np = of_get_parent(np);
450 of_node_put(np);
17b5001b
RK
451 }
452
655b43cc
PZ
453 return dev->of_node == np;
454}
17b5001b 455
17b5001b
RK
456static int imx_drm_bind(struct device *dev)
457{
458 return drm_platform_init(&imx_drm_driver, to_platform_device(dev));
459}
460
461static void imx_drm_unbind(struct device *dev)
462{
463 drm_put_dev(dev_get_drvdata(dev));
464}
465
466static const struct component_master_ops imx_drm_ops = {
17b5001b
RK
467 .bind = imx_drm_bind,
468 .unbind = imx_drm_unbind,
469};
470
e692da4d
SH
471static int imx_drm_platform_probe(struct platform_device *pdev)
472{
9cace32f 473 int ret = drm_of_component_probe(&pdev->dev, compare_of, &imx_drm_ops);
655b43cc 474
9cace32f
LD
475 if (!ret)
476 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
655b43cc 477
9cace32f 478 return ret;
e692da4d
SH
479}
480
481static int imx_drm_platform_remove(struct platform_device *pdev)
482{
17b5001b 483 component_master_del(&pdev->dev, &imx_drm_ops);
e692da4d
SH
484 return 0;
485}
486
bfe945c8
SG
487#ifdef CONFIG_PM_SLEEP
488static int imx_drm_suspend(struct device *dev)
489{
490 struct drm_device *drm_dev = dev_get_drvdata(dev);
491
492 /* The drm_dev is NULL before .load hook is called */
493 if (drm_dev == NULL)
494 return 0;
495
496 drm_kms_helper_poll_disable(drm_dev);
497
498 return 0;
499}
500
501static int imx_drm_resume(struct device *dev)
502{
503 struct drm_device *drm_dev = dev_get_drvdata(dev);
504
505 if (drm_dev == NULL)
506 return 0;
507
508 drm_helper_resume_force_mode(drm_dev);
509 drm_kms_helper_poll_enable(drm_dev);
510
511 return 0;
512}
513#endif
514
515static SIMPLE_DEV_PM_OPS(imx_drm_pm_ops, imx_drm_suspend, imx_drm_resume);
516
17b5001b 517static const struct of_device_id imx_drm_dt_ids[] = {
655b43cc 518 { .compatible = "fsl,imx-display-subsystem", },
17b5001b
RK
519 { /* sentinel */ },
520};
521MODULE_DEVICE_TABLE(of, imx_drm_dt_ids);
522
e692da4d
SH
523static struct platform_driver imx_drm_pdrv = {
524 .probe = imx_drm_platform_probe,
99c28f10 525 .remove = imx_drm_platform_remove,
e692da4d 526 .driver = {
e692da4d 527 .name = "imx-drm",
bfe945c8 528 .pm = &imx_drm_pm_ops,
17b5001b 529 .of_match_table = imx_drm_dt_ids,
e692da4d
SH
530 },
531};
b85f2b5d 532module_platform_driver(imx_drm_pdrv);
e692da4d
SH
533
534MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
535MODULE_DESCRIPTION("i.MX drm driver core");
536MODULE_LICENSE("GPL");