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