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