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