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