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