Backmerge tag 'v4.7' into drm-next
[deliverable/linux.git] / drivers / gpu / drm / imx / imx-drm-core.c
... / ...
CommitLineData
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 */
16#include <linux/component.h>
17#include <linux/device.h>
18#include <linux/dma-buf.h>
19#include <linux/fb.h>
20#include <linux/module.h>
21#include <linux/platform_device.h>
22#include <linux/reservation.h>
23#include <drm/drmP.h>
24#include <drm/drm_atomic.h>
25#include <drm/drm_atomic_helper.h>
26#include <drm/drm_fb_helper.h>
27#include <drm/drm_crtc_helper.h>
28#include <drm/drm_gem_cma_helper.h>
29#include <drm/drm_fb_cma_helper.h>
30#include <drm/drm_plane_helper.h>
31#include <drm/drm_of.h>
32#include <video/imx-ipu-v3.h>
33
34#include "imx-drm.h"
35
36#define MAX_CRTC 4
37
38struct imx_drm_component {
39 struct device_node *of_node;
40 struct list_head list;
41};
42
43struct imx_drm_device {
44 struct drm_device *drm;
45 struct imx_drm_crtc *crtc[MAX_CRTC];
46 unsigned int pipes;
47 struct drm_fbdev_cma *fbhelper;
48 struct drm_atomic_state *state;
49};
50
51struct imx_drm_crtc {
52 struct drm_crtc *crtc;
53 struct imx_drm_crtc_helper_funcs imx_drm_helper_funcs;
54};
55
56#if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
57static int legacyfb_depth = 16;
58module_param(legacyfb_depth, int, 0444);
59#endif
60
61unsigned int imx_drm_crtc_id(struct imx_drm_crtc *crtc)
62{
63 return drm_crtc_index(crtc->crtc);
64}
65EXPORT_SYMBOL_GPL(imx_drm_crtc_id);
66
67static void imx_drm_driver_lastclose(struct drm_device *drm)
68{
69 struct imx_drm_device *imxdrm = drm->dev_private;
70
71 drm_fbdev_cma_restore_mode(imxdrm->fbhelper);
72}
73
74static int imx_drm_driver_unload(struct drm_device *drm)
75{
76 struct imx_drm_device *imxdrm = drm->dev_private;
77
78 drm_kms_helper_poll_fini(drm);
79
80 if (imxdrm->fbhelper)
81 drm_fbdev_cma_fini(imxdrm->fbhelper);
82
83 component_unbind_all(drm->dev, drm);
84
85 drm_vblank_cleanup(drm);
86 drm_mode_config_cleanup(drm);
87
88 platform_set_drvdata(drm->platformdev, NULL);
89
90 return 0;
91}
92
93int imx_drm_crtc_vblank_get(struct imx_drm_crtc *imx_drm_crtc)
94{
95 return drm_crtc_vblank_get(imx_drm_crtc->crtc);
96}
97EXPORT_SYMBOL_GPL(imx_drm_crtc_vblank_get);
98
99void imx_drm_crtc_vblank_put(struct imx_drm_crtc *imx_drm_crtc)
100{
101 drm_crtc_vblank_put(imx_drm_crtc->crtc);
102}
103EXPORT_SYMBOL_GPL(imx_drm_crtc_vblank_put);
104
105void imx_drm_handle_vblank(struct imx_drm_crtc *imx_drm_crtc)
106{
107 drm_crtc_handle_vblank(imx_drm_crtc->crtc);
108}
109EXPORT_SYMBOL_GPL(imx_drm_handle_vblank);
110
111static int imx_drm_enable_vblank(struct drm_device *drm, unsigned int pipe)
112{
113 struct imx_drm_device *imxdrm = drm->dev_private;
114 struct imx_drm_crtc *imx_drm_crtc = imxdrm->crtc[pipe];
115 int ret;
116
117 if (!imx_drm_crtc)
118 return -EINVAL;
119
120 if (!imx_drm_crtc->imx_drm_helper_funcs.enable_vblank)
121 return -ENOSYS;
122
123 ret = imx_drm_crtc->imx_drm_helper_funcs.enable_vblank(
124 imx_drm_crtc->crtc);
125
126 return ret;
127}
128
129static void imx_drm_disable_vblank(struct drm_device *drm, unsigned int pipe)
130{
131 struct imx_drm_device *imxdrm = drm->dev_private;
132 struct imx_drm_crtc *imx_drm_crtc = imxdrm->crtc[pipe];
133
134 if (!imx_drm_crtc)
135 return;
136
137 if (!imx_drm_crtc->imx_drm_helper_funcs.disable_vblank)
138 return;
139
140 imx_drm_crtc->imx_drm_helper_funcs.disable_vblank(imx_drm_crtc->crtc);
141}
142
143static const struct file_operations imx_drm_driver_fops = {
144 .owner = THIS_MODULE,
145 .open = drm_open,
146 .release = drm_release,
147 .unlocked_ioctl = drm_ioctl,
148 .mmap = drm_gem_cma_mmap,
149 .poll = drm_poll,
150 .read = drm_read,
151 .llseek = noop_llseek,
152};
153
154void imx_drm_connector_destroy(struct drm_connector *connector)
155{
156 drm_connector_unregister(connector);
157 drm_connector_cleanup(connector);
158}
159EXPORT_SYMBOL_GPL(imx_drm_connector_destroy);
160
161void imx_drm_encoder_destroy(struct drm_encoder *encoder)
162{
163 drm_encoder_cleanup(encoder);
164}
165EXPORT_SYMBOL_GPL(imx_drm_encoder_destroy);
166
167static void imx_drm_output_poll_changed(struct drm_device *drm)
168{
169 struct imx_drm_device *imxdrm = drm->dev_private;
170
171 drm_fbdev_cma_hotplug_event(imxdrm->fbhelper);
172}
173
174static const struct drm_mode_config_funcs imx_drm_mode_config_funcs = {
175 .fb_create = drm_fb_cma_create,
176 .output_poll_changed = imx_drm_output_poll_changed,
177 .atomic_check = drm_atomic_helper_check,
178 .atomic_commit = drm_atomic_helper_commit,
179};
180
181static void imx_drm_atomic_commit_tail(struct drm_atomic_state *state)
182{
183 struct drm_device *dev = state->dev;
184 struct drm_crtc *crtc;
185 struct drm_crtc_state *crtc_state;
186 struct drm_plane_state *plane_state;
187 struct drm_gem_cma_object *cma_obj;
188 struct fence *excl;
189 unsigned shared_count;
190 struct fence **shared;
191 unsigned int i, j;
192 int ret;
193
194 /* Wait for fences. */
195 for_each_crtc_in_state(state, crtc, crtc_state, i) {
196 plane_state = crtc->primary->state;
197 if (plane_state->fb) {
198 cma_obj = drm_fb_cma_get_gem_obj(plane_state->fb, 0);
199 if (cma_obj->base.dma_buf) {
200 ret = reservation_object_get_fences_rcu(
201 cma_obj->base.dma_buf->resv, &excl,
202 &shared_count, &shared);
203 if (unlikely(ret))
204 DRM_ERROR("failed to get fences "
205 "for buffer\n");
206
207 if (excl) {
208 fence_wait(excl, false);
209 fence_put(excl);
210 }
211 for (j = 0; j < shared_count; i++) {
212 fence_wait(shared[j], false);
213 fence_put(shared[j]);
214 }
215 }
216 }
217 }
218
219 drm_atomic_helper_commit_modeset_disables(dev, state);
220
221 drm_atomic_helper_commit_planes(dev, state, true);
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,
234};
235
236/*
237 * Main DRM initialisation. This binds, initialises and registers
238 * with DRM the subcomponents of the driver.
239 */
240static int imx_drm_driver_load(struct drm_device *drm, unsigned long flags)
241{
242 struct imx_drm_device *imxdrm;
243 struct drm_connector *connector;
244 int ret;
245
246 imxdrm = devm_kzalloc(drm->dev, sizeof(*imxdrm), GFP_KERNEL);
247 if (!imxdrm)
248 return -ENOMEM;
249
250 imxdrm->drm = drm;
251
252 drm->dev_private = imxdrm;
253
254 /*
255 * enable drm irq mode.
256 * - with irq_enabled = true, we can use the vblank feature.
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 */
263 drm->irq_enabled = true;
264
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;
275 drm->mode_config.helper_private = &imx_drm_mode_config_helpers;
276
277 drm_mode_config_init(drm);
278
279 ret = drm_vblank_init(drm, MAX_CRTC);
280 if (ret)
281 goto err_kms;
282
283 platform_set_drvdata(drm->platformdev, drm);
284
285 /* Now try and bind all our sub-components */
286 ret = component_bind_all(drm->dev, drm);
287 if (ret)
288 goto err_vblank;
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) {
296 ret = drm_connector_register(connector);
297 if (ret) {
298 dev_err(drm->dev,
299 "[CONNECTOR:%d:%s] drm_connector_register failed: %d\n",
300 connector->base.id,
301 connector->name, ret);
302 goto err_unbind;
303 }
304 }
305
306 drm_mode_config_reset(drm);
307
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 */
313#if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
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
326
327 drm_kms_helper_poll_init(drm);
328
329 return 0;
330
331err_unbind:
332 component_unbind_all(drm->dev, drm);
333err_vblank:
334 drm_vblank_cleanup(drm);
335err_kms:
336 drm_mode_config_cleanup(drm);
337
338 return ret;
339}
340
341/*
342 * imx_drm_add_crtc - add a new crtc
343 */
344int imx_drm_add_crtc(struct drm_device *drm, struct drm_crtc *crtc,
345 struct imx_drm_crtc **new_crtc, struct drm_plane *primary_plane,
346 const struct imx_drm_crtc_helper_funcs *imx_drm_helper_funcs,
347 struct device_node *port)
348{
349 struct imx_drm_device *imxdrm = drm->dev_private;
350 struct imx_drm_crtc *imx_drm_crtc;
351
352 /*
353 * The vblank arrays are dimensioned by MAX_CRTC - we can't
354 * pass IDs greater than this to those functions.
355 */
356 if (imxdrm->pipes >= MAX_CRTC)
357 return -EINVAL;
358
359 if (imxdrm->drm->open_count)
360 return -EBUSY;
361
362 imx_drm_crtc = kzalloc(sizeof(*imx_drm_crtc), GFP_KERNEL);
363 if (!imx_drm_crtc)
364 return -ENOMEM;
365
366 imx_drm_crtc->imx_drm_helper_funcs = *imx_drm_helper_funcs;
367 imx_drm_crtc->crtc = crtc;
368
369 crtc->port = port;
370
371 imxdrm->crtc[imxdrm->pipes++] = imx_drm_crtc;
372
373 *new_crtc = imx_drm_crtc;
374
375 drm_crtc_helper_add(crtc,
376 imx_drm_crtc->imx_drm_helper_funcs.crtc_helper_funcs);
377
378 drm_crtc_init_with_planes(drm, crtc, primary_plane, NULL,
379 imx_drm_crtc->imx_drm_helper_funcs.crtc_funcs, NULL);
380
381 return 0;
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{
390 struct imx_drm_device *imxdrm = imx_drm_crtc->crtc->dev->dev_private;
391 unsigned int pipe = drm_crtc_index(imx_drm_crtc->crtc);
392
393 drm_crtc_cleanup(imx_drm_crtc->crtc);
394
395 imxdrm->crtc[pipe] = NULL;
396
397 kfree(imx_drm_crtc);
398
399 return 0;
400}
401EXPORT_SYMBOL_GPL(imx_drm_remove_crtc);
402
403int imx_drm_encoder_parse_of(struct drm_device *drm,
404 struct drm_encoder *encoder, struct device_node *np)
405{
406 uint32_t crtc_mask = drm_of_find_possible_crtcs(drm, np);
407
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;
416
417 encoder->possible_crtcs = crtc_mask;
418
419 /* FIXME: this is the mask of outputs which can clone this output. */
420 encoder->possible_clones = ~0;
421
422 return 0;
423}
424EXPORT_SYMBOL_GPL(imx_drm_encoder_parse_of);
425
426static const struct drm_ioctl_desc imx_drm_ioctls[] = {
427 /* none so far */
428};
429
430static struct drm_driver imx_drm_driver = {
431 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME |
432 DRIVER_ATOMIC,
433 .load = imx_drm_driver_load,
434 .unload = imx_drm_driver_unload,
435 .lastclose = imx_drm_driver_lastclose,
436 .gem_free_object_unlocked = drm_gem_cma_free_object,
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,
440 .dumb_destroy = drm_gem_dumb_destroy,
441
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,
451 .get_vblank_counter = drm_vblank_no_hw_counter,
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
465static int compare_of(struct device *dev, void *data)
466{
467 struct device_node *np = data;
468
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
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);
480 }
481
482 return dev->of_node == np;
483}
484
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 = {
496 .bind = imx_drm_bind,
497 .unbind = imx_drm_unbind,
498};
499
500static int imx_drm_platform_probe(struct platform_device *pdev)
501{
502 int ret = drm_of_component_probe(&pdev->dev, compare_of, &imx_drm_ops);
503
504 if (!ret)
505 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
506
507 return ret;
508}
509
510static int imx_drm_platform_remove(struct platform_device *pdev)
511{
512 component_master_del(&pdev->dev, &imx_drm_ops);
513 return 0;
514}
515
516#ifdef CONFIG_PM_SLEEP
517static int imx_drm_suspend(struct device *dev)
518{
519 struct drm_device *drm_dev = dev_get_drvdata(dev);
520 struct imx_drm_device *imxdrm;
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
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
535 return 0;
536}
537
538static int imx_drm_resume(struct device *dev)
539{
540 struct drm_device *drm_dev = dev_get_drvdata(dev);
541 struct imx_drm_device *imx_drm;
542
543 if (drm_dev == NULL)
544 return 0;
545
546 imx_drm = drm_dev->dev_private;
547 drm_atomic_helper_resume(drm_dev, imx_drm->state);
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
556static const struct of_device_id imx_drm_dt_ids[] = {
557 { .compatible = "fsl,imx-display-subsystem", },
558 { /* sentinel */ },
559};
560MODULE_DEVICE_TABLE(of, imx_drm_dt_ids);
561
562static struct platform_driver imx_drm_pdrv = {
563 .probe = imx_drm_platform_probe,
564 .remove = imx_drm_platform_remove,
565 .driver = {
566 .name = "imx-drm",
567 .pm = &imx_drm_pm_ops,
568 .of_match_table = imx_drm_dt_ids,
569 },
570};
571module_platform_driver(imx_drm_pdrv);
572
573MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
574MODULE_DESCRIPTION("i.MX drm driver core");
575MODULE_LICENSE("GPL");
This page took 0.079647 seconds and 5 git commands to generate.