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