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