Merge remote-tracking branch 'mmc-uh/next'
[deliverable/linux.git] / drivers / gpu / drm / imx / imx-drm-core.c
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/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/reservation.h>
22 #include <drm/drmP.h>
23 #include <drm/drm_atomic.h>
24 #include <drm/drm_atomic_helper.h>
25 #include <drm/drm_fb_helper.h>
26 #include <drm/drm_crtc_helper.h>
27 #include <drm/drm_gem_cma_helper.h>
28 #include <drm/drm_fb_cma_helper.h>
29 #include <drm/drm_plane_helper.h>
30 #include <drm/drm_of.h>
31 #include <video/imx-ipu-v3.h>
32
33 #include "imx-drm.h"
34
35 #define MAX_CRTC 4
36
37 struct imx_drm_component {
38 struct device_node *of_node;
39 struct list_head list;
40 };
41
42 struct imx_drm_device {
43 struct drm_device *drm;
44 struct imx_drm_crtc *crtc[MAX_CRTC];
45 unsigned int pipes;
46 struct drm_fbdev_cma *fbhelper;
47 struct drm_atomic_state *state;
48 };
49
50 struct imx_drm_crtc {
51 struct drm_crtc *crtc;
52 struct imx_drm_crtc_helper_funcs imx_drm_helper_funcs;
53 };
54
55 #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
56 static int legacyfb_depth = 16;
57 module_param(legacyfb_depth, int, 0444);
58 #endif
59
60 static void imx_drm_driver_lastclose(struct drm_device *drm)
61 {
62 struct imx_drm_device *imxdrm = drm->dev_private;
63
64 drm_fbdev_cma_restore_mode(imxdrm->fbhelper);
65 }
66
67 static int imx_drm_driver_unload(struct drm_device *drm)
68 {
69 struct imx_drm_device *imxdrm = drm->dev_private;
70
71 drm_kms_helper_poll_fini(drm);
72
73 if (imxdrm->fbhelper)
74 drm_fbdev_cma_fini(imxdrm->fbhelper);
75
76 component_unbind_all(drm->dev, drm);
77
78 drm_vblank_cleanup(drm);
79 drm_mode_config_cleanup(drm);
80
81 platform_set_drvdata(drm->platformdev, NULL);
82
83 return 0;
84 }
85
86 static int imx_drm_enable_vblank(struct drm_device *drm, unsigned int pipe)
87 {
88 struct imx_drm_device *imxdrm = drm->dev_private;
89 struct imx_drm_crtc *imx_drm_crtc = imxdrm->crtc[pipe];
90 int ret;
91
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
104 static void imx_drm_disable_vblank(struct drm_device *drm, unsigned int pipe)
105 {
106 struct imx_drm_device *imxdrm = drm->dev_private;
107 struct imx_drm_crtc *imx_drm_crtc = imxdrm->crtc[pipe];
108
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
118 static 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,
125 .read = drm_read,
126 .llseek = noop_llseek,
127 };
128
129 void imx_drm_connector_destroy(struct drm_connector *connector)
130 {
131 drm_connector_unregister(connector);
132 drm_connector_cleanup(connector);
133 }
134 EXPORT_SYMBOL_GPL(imx_drm_connector_destroy);
135
136 void imx_drm_encoder_destroy(struct drm_encoder *encoder)
137 {
138 drm_encoder_cleanup(encoder);
139 }
140 EXPORT_SYMBOL_GPL(imx_drm_encoder_destroy);
141
142 static void imx_drm_output_poll_changed(struct drm_device *drm)
143 {
144 struct imx_drm_device *imxdrm = drm->dev_private;
145
146 drm_fbdev_cma_hotplug_event(imxdrm->fbhelper);
147 }
148
149 static 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
173 static const struct drm_mode_config_funcs imx_drm_mode_config_funcs = {
174 .fb_create = drm_fb_cma_create,
175 .output_poll_changed = imx_drm_output_poll_changed,
176 .atomic_check = imx_drm_atomic_check,
177 .atomic_commit = drm_atomic_helper_commit,
178 };
179
180 static 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
220 drm_atomic_helper_commit_planes(dev, state,
221 DRM_PLANE_COMMIT_ACTIVE_ONLY);
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
232 static 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 */
240 static 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
331 err_unbind:
332 component_unbind_all(drm->dev, drm);
333 err_vblank:
334 drm_vblank_cleanup(drm);
335 err_kms:
336 drm_mode_config_cleanup(drm);
337
338 return ret;
339 }
340
341 /*
342 * imx_drm_add_crtc - add a new crtc
343 */
344 int 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 }
383 EXPORT_SYMBOL_GPL(imx_drm_add_crtc);
384
385 /*
386 * imx_drm_remove_crtc - remove a crtc
387 */
388 int 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 }
401 EXPORT_SYMBOL_GPL(imx_drm_remove_crtc);
402
403 int 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 }
424 EXPORT_SYMBOL_GPL(imx_drm_encoder_parse_of);
425
426 static const struct drm_ioctl_desc imx_drm_ioctls[] = {
427 /* none so far */
428 };
429
430 static 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
465 static 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
485 static int imx_drm_bind(struct device *dev)
486 {
487 return drm_platform_init(&imx_drm_driver, to_platform_device(dev));
488 }
489
490 static void imx_drm_unbind(struct device *dev)
491 {
492 drm_put_dev(dev_get_drvdata(dev));
493 }
494
495 static const struct component_master_ops imx_drm_ops = {
496 .bind = imx_drm_bind,
497 .unbind = imx_drm_unbind,
498 };
499
500 static 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
510 static 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
517 static 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
538 static 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
554 static SIMPLE_DEV_PM_OPS(imx_drm_pm_ops, imx_drm_suspend, imx_drm_resume);
555
556 static const struct of_device_id imx_drm_dt_ids[] = {
557 { .compatible = "fsl,imx-display-subsystem", },
558 { /* sentinel */ },
559 };
560 MODULE_DEVICE_TABLE(of, imx_drm_dt_ids);
561
562 static 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 };
571 module_platform_driver(imx_drm_pdrv);
572
573 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
574 MODULE_DESCRIPTION("i.MX drm driver core");
575 MODULE_LICENSE("GPL");
This page took 0.042481 seconds and 5 git commands to generate.