Merge tag 'topic/drm-misc-2016-10-27' of git://anongit.freedesktop.org/git/drm-intel...
[linux-2.6-block.git] / drivers / gpu / drm / rockchip / rockchip_drm_drv.c
1 /*
2  * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
3  * Author:Mark Yao <mark.yao@rock-chips.com>
4  *
5  * based on exynos_drm_drv.c
6  *
7  * This software is licensed under the terms of the GNU General Public
8  * License version 2, as published by the Free Software Foundation, and
9  * may be copied, distributed, and modified under those terms.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  */
16
17 #include <asm/dma-iommu.h>
18
19 #include <drm/drmP.h>
20 #include <drm/drm_crtc_helper.h>
21 #include <drm/drm_fb_helper.h>
22 #include <drm/drm_gem_cma_helper.h>
23 #include <drm/drm_of.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/module.h>
27 #include <linux/of_graph.h>
28 #include <linux/component.h>
29 #include <linux/console.h>
30
31 #include "rockchip_drm_drv.h"
32 #include "rockchip_drm_fb.h"
33 #include "rockchip_drm_fbdev.h"
34 #include "rockchip_drm_gem.h"
35
36 #define DRIVER_NAME     "rockchip"
37 #define DRIVER_DESC     "RockChip Soc DRM"
38 #define DRIVER_DATE     "20140818"
39 #define DRIVER_MAJOR    1
40 #define DRIVER_MINOR    0
41
42 static bool is_support_iommu = true;
43 static struct drm_driver rockchip_drm_driver;
44
45 /*
46  * Attach a (component) device to the shared drm dma mapping from master drm
47  * device.  This is used by the VOPs to map GEM buffers to a common DMA
48  * mapping.
49  */
50 int rockchip_drm_dma_attach_device(struct drm_device *drm_dev,
51                                    struct device *dev)
52 {
53         struct dma_iommu_mapping *mapping = drm_dev->dev->archdata.mapping;
54         int ret;
55
56         if (!is_support_iommu)
57                 return 0;
58
59         ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
60         if (ret)
61                 return ret;
62
63         dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
64
65         return arm_iommu_attach_device(dev, mapping);
66 }
67
68 void rockchip_drm_dma_detach_device(struct drm_device *drm_dev,
69                                     struct device *dev)
70 {
71         if (!is_support_iommu)
72                 return;
73
74         arm_iommu_detach_device(dev);
75 }
76
77 int rockchip_register_crtc_funcs(struct drm_crtc *crtc,
78                                  const struct rockchip_crtc_funcs *crtc_funcs)
79 {
80         int pipe = drm_crtc_index(crtc);
81         struct rockchip_drm_private *priv = crtc->dev->dev_private;
82
83         if (pipe >= ROCKCHIP_MAX_CRTC)
84                 return -EINVAL;
85
86         priv->crtc_funcs[pipe] = crtc_funcs;
87
88         return 0;
89 }
90
91 void rockchip_unregister_crtc_funcs(struct drm_crtc *crtc)
92 {
93         int pipe = drm_crtc_index(crtc);
94         struct rockchip_drm_private *priv = crtc->dev->dev_private;
95
96         if (pipe >= ROCKCHIP_MAX_CRTC)
97                 return;
98
99         priv->crtc_funcs[pipe] = NULL;
100 }
101
102 static struct drm_crtc *rockchip_crtc_from_pipe(struct drm_device *drm,
103                                                 int pipe)
104 {
105         struct drm_crtc *crtc;
106         int i = 0;
107
108         list_for_each_entry(crtc, &drm->mode_config.crtc_list, head)
109                 if (i++ == pipe)
110                         return crtc;
111
112         return NULL;
113 }
114
115 static int rockchip_drm_crtc_enable_vblank(struct drm_device *dev,
116                                            unsigned int pipe)
117 {
118         struct rockchip_drm_private *priv = dev->dev_private;
119         struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);
120
121         if (crtc && priv->crtc_funcs[pipe] &&
122             priv->crtc_funcs[pipe]->enable_vblank)
123                 return priv->crtc_funcs[pipe]->enable_vblank(crtc);
124
125         return 0;
126 }
127
128 static void rockchip_drm_crtc_disable_vblank(struct drm_device *dev,
129                                              unsigned int pipe)
130 {
131         struct rockchip_drm_private *priv = dev->dev_private;
132         struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);
133
134         if (crtc && priv->crtc_funcs[pipe] &&
135             priv->crtc_funcs[pipe]->enable_vblank)
136                 priv->crtc_funcs[pipe]->disable_vblank(crtc);
137 }
138
139 static int rockchip_drm_bind(struct device *dev)
140 {
141         struct drm_device *drm_dev;
142         struct rockchip_drm_private *private;
143         struct dma_iommu_mapping *mapping = NULL;
144         int ret;
145
146         drm_dev = drm_dev_alloc(&rockchip_drm_driver, dev);
147         if (IS_ERR(drm_dev))
148                 return PTR_ERR(drm_dev);
149
150         dev_set_drvdata(dev, drm_dev);
151
152         private = devm_kzalloc(drm_dev->dev, sizeof(*private), GFP_KERNEL);
153         if (!private) {
154                 ret = -ENOMEM;
155                 goto err_free;
156         }
157
158         drm_dev->dev_private = private;
159
160         INIT_LIST_HEAD(&private->psr_list);
161         spin_lock_init(&private->psr_list_lock);
162
163         drm_mode_config_init(drm_dev);
164
165         rockchip_drm_mode_config_init(drm_dev);
166
167         dev->dma_parms = devm_kzalloc(dev, sizeof(*dev->dma_parms),
168                                       GFP_KERNEL);
169         if (!dev->dma_parms) {
170                 ret = -ENOMEM;
171                 goto err_config_cleanup;
172         }
173
174         if (is_support_iommu) {
175                 /* TODO(djkurtz): fetch the mapping start/size from somewhere */
176                 mapping = arm_iommu_create_mapping(&platform_bus_type,
177                                                    0x00000000,
178                                                    SZ_2G);
179                 if (IS_ERR(mapping)) {
180                         ret = PTR_ERR(mapping);
181                         goto err_config_cleanup;
182                 }
183
184                 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
185                 if (ret)
186                         goto err_release_mapping;
187
188                 dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
189
190                 ret = arm_iommu_attach_device(dev, mapping);
191                 if (ret)
192                         goto err_release_mapping;
193         }
194
195         /* Try to bind all sub drivers. */
196         ret = component_bind_all(dev, drm_dev);
197         if (ret)
198                 goto err_detach_device;
199
200         /* init kms poll for handling hpd */
201         drm_kms_helper_poll_init(drm_dev);
202
203         /*
204          * enable drm irq mode.
205          * - with irq_enabled = true, we can use the vblank feature.
206          */
207         drm_dev->irq_enabled = true;
208
209         ret = drm_vblank_init(drm_dev, ROCKCHIP_MAX_CRTC);
210         if (ret)
211                 goto err_kms_helper_poll_fini;
212
213         drm_mode_config_reset(drm_dev);
214
215         ret = rockchip_drm_fbdev_init(drm_dev);
216         if (ret)
217                 goto err_vblank_cleanup;
218
219         ret = drm_dev_register(drm_dev, 0);
220         if (ret)
221                 goto err_fbdev_fini;
222
223         if (is_support_iommu)
224                 arm_iommu_release_mapping(mapping);
225         return 0;
226 err_fbdev_fini:
227         rockchip_drm_fbdev_fini(drm_dev);
228 err_vblank_cleanup:
229         drm_vblank_cleanup(drm_dev);
230 err_kms_helper_poll_fini:
231         drm_kms_helper_poll_fini(drm_dev);
232         component_unbind_all(dev, drm_dev);
233 err_detach_device:
234         if (is_support_iommu)
235                 arm_iommu_detach_device(dev);
236 err_release_mapping:
237         if (is_support_iommu)
238                 arm_iommu_release_mapping(mapping);
239 err_config_cleanup:
240         drm_mode_config_cleanup(drm_dev);
241         drm_dev->dev_private = NULL;
242 err_free:
243         drm_dev_unref(drm_dev);
244         return ret;
245 }
246
247 static void rockchip_drm_unbind(struct device *dev)
248 {
249         struct drm_device *drm_dev = dev_get_drvdata(dev);
250
251         rockchip_drm_fbdev_fini(drm_dev);
252         drm_vblank_cleanup(drm_dev);
253         drm_kms_helper_poll_fini(drm_dev);
254         component_unbind_all(dev, drm_dev);
255         if (is_support_iommu)
256                 arm_iommu_detach_device(dev);
257         drm_mode_config_cleanup(drm_dev);
258         drm_dev->dev_private = NULL;
259         drm_dev_unregister(drm_dev);
260         drm_dev_unref(drm_dev);
261         dev_set_drvdata(dev, NULL);
262 }
263
264 static void rockchip_drm_lastclose(struct drm_device *dev)
265 {
266         struct rockchip_drm_private *priv = dev->dev_private;
267
268         drm_fb_helper_restore_fbdev_mode_unlocked(&priv->fbdev_helper);
269 }
270
271 static const struct file_operations rockchip_drm_driver_fops = {
272         .owner = THIS_MODULE,
273         .open = drm_open,
274         .mmap = rockchip_gem_mmap,
275         .poll = drm_poll,
276         .read = drm_read,
277         .unlocked_ioctl = drm_ioctl,
278 #ifdef CONFIG_COMPAT
279         .compat_ioctl = drm_compat_ioctl,
280 #endif
281         .release = drm_release,
282 };
283
284 static struct drm_driver rockchip_drm_driver = {
285         .driver_features        = DRIVER_MODESET | DRIVER_GEM |
286                                   DRIVER_PRIME | DRIVER_ATOMIC,
287         .lastclose              = rockchip_drm_lastclose,
288         .get_vblank_counter     = drm_vblank_no_hw_counter,
289         .enable_vblank          = rockchip_drm_crtc_enable_vblank,
290         .disable_vblank         = rockchip_drm_crtc_disable_vblank,
291         .gem_vm_ops             = &drm_gem_cma_vm_ops,
292         .gem_free_object_unlocked = rockchip_gem_free_object,
293         .dumb_create            = rockchip_gem_dumb_create,
294         .dumb_map_offset        = rockchip_gem_dumb_map_offset,
295         .dumb_destroy           = drm_gem_dumb_destroy,
296         .prime_handle_to_fd     = drm_gem_prime_handle_to_fd,
297         .prime_fd_to_handle     = drm_gem_prime_fd_to_handle,
298         .gem_prime_import       = drm_gem_prime_import,
299         .gem_prime_export       = drm_gem_prime_export,
300         .gem_prime_get_sg_table = rockchip_gem_prime_get_sg_table,
301         .gem_prime_vmap         = rockchip_gem_prime_vmap,
302         .gem_prime_vunmap       = rockchip_gem_prime_vunmap,
303         .gem_prime_mmap         = rockchip_gem_mmap_buf,
304         .fops                   = &rockchip_drm_driver_fops,
305         .name   = DRIVER_NAME,
306         .desc   = DRIVER_DESC,
307         .date   = DRIVER_DATE,
308         .major  = DRIVER_MAJOR,
309         .minor  = DRIVER_MINOR,
310 };
311
312 #ifdef CONFIG_PM_SLEEP
313 static void rockchip_drm_fb_suspend(struct drm_device *drm)
314 {
315         struct rockchip_drm_private *priv = drm->dev_private;
316
317         console_lock();
318         drm_fb_helper_set_suspend(&priv->fbdev_helper, 1);
319         console_unlock();
320 }
321
322 static void rockchip_drm_fb_resume(struct drm_device *drm)
323 {
324         struct rockchip_drm_private *priv = drm->dev_private;
325
326         console_lock();
327         drm_fb_helper_set_suspend(&priv->fbdev_helper, 0);
328         console_unlock();
329 }
330
331 static int rockchip_drm_sys_suspend(struct device *dev)
332 {
333         struct drm_device *drm = dev_get_drvdata(dev);
334         struct rockchip_drm_private *priv = drm->dev_private;
335
336         drm_kms_helper_poll_disable(drm);
337         rockchip_drm_fb_suspend(drm);
338
339         priv->state = drm_atomic_helper_suspend(drm);
340         if (IS_ERR(priv->state)) {
341                 rockchip_drm_fb_resume(drm);
342                 drm_kms_helper_poll_enable(drm);
343                 return PTR_ERR(priv->state);
344         }
345
346         return 0;
347 }
348
349 static int rockchip_drm_sys_resume(struct device *dev)
350 {
351         struct drm_device *drm = dev_get_drvdata(dev);
352         struct rockchip_drm_private *priv = drm->dev_private;
353
354         drm_atomic_helper_resume(drm, priv->state);
355         rockchip_drm_fb_resume(drm);
356         drm_kms_helper_poll_enable(drm);
357
358         return 0;
359 }
360 #endif
361
362 static const struct dev_pm_ops rockchip_drm_pm_ops = {
363         SET_SYSTEM_SLEEP_PM_OPS(rockchip_drm_sys_suspend,
364                                 rockchip_drm_sys_resume)
365 };
366
367 static int compare_of(struct device *dev, void *data)
368 {
369         struct device_node *np = data;
370
371         return dev->of_node == np;
372 }
373
374 static void rockchip_add_endpoints(struct device *dev,
375                                    struct component_match **match,
376                                    struct device_node *port)
377 {
378         struct device_node *ep, *remote;
379
380         for_each_child_of_node(port, ep) {
381                 remote = of_graph_get_remote_port_parent(ep);
382                 if (!remote || !of_device_is_available(remote)) {
383                         of_node_put(remote);
384                         continue;
385                 } else if (!of_device_is_available(remote->parent)) {
386                         dev_warn(dev, "parent device of %s is not available\n",
387                                  remote->full_name);
388                         of_node_put(remote);
389                         continue;
390                 }
391
392                 drm_of_component_match_add(dev, match, compare_of, remote);
393                 of_node_put(remote);
394         }
395 }
396
397 static const struct component_master_ops rockchip_drm_ops = {
398         .bind = rockchip_drm_bind,
399         .unbind = rockchip_drm_unbind,
400 };
401
402 static int rockchip_drm_platform_probe(struct platform_device *pdev)
403 {
404         struct device *dev = &pdev->dev;
405         struct component_match *match = NULL;
406         struct device_node *np = dev->of_node;
407         struct device_node *port;
408         int i;
409
410         if (!np)
411                 return -ENODEV;
412         /*
413          * Bind the crtc ports first, so that
414          * drm_of_find_possible_crtcs called from encoder .bind callbacks
415          * works as expected.
416          */
417         for (i = 0;; i++) {
418                 struct device_node *iommu;
419
420                 port = of_parse_phandle(np, "ports", i);
421                 if (!port)
422                         break;
423
424                 if (!of_device_is_available(port->parent)) {
425                         of_node_put(port);
426                         continue;
427                 }
428
429                 iommu = of_parse_phandle(port->parent, "iommus", 0);
430                 if (!iommu || !of_device_is_available(iommu->parent)) {
431                         dev_dbg(dev, "no iommu attached for %s, using non-iommu buffers\n",
432                                 port->parent->full_name);
433                         /*
434                          * if there is a crtc not support iommu, force set all
435                          * crtc use non-iommu buffer.
436                          */
437                         is_support_iommu = false;
438                 }
439
440                 of_node_put(iommu);
441                 drm_of_component_match_add(dev, &match, compare_of,
442                                            port->parent);
443                 of_node_put(port);
444         }
445
446         if (i == 0) {
447                 dev_err(dev, "missing 'ports' property\n");
448                 return -ENODEV;
449         }
450
451         if (!match) {
452                 dev_err(dev, "No available vop found for display-subsystem.\n");
453                 return -ENODEV;
454         }
455         /*
456          * For each bound crtc, bind the encoders attached to its
457          * remote endpoint.
458          */
459         for (i = 0;; i++) {
460                 port = of_parse_phandle(np, "ports", i);
461                 if (!port)
462                         break;
463
464                 if (!of_device_is_available(port->parent)) {
465                         of_node_put(port);
466                         continue;
467                 }
468
469                 rockchip_add_endpoints(dev, &match, port);
470                 of_node_put(port);
471         }
472
473         return component_master_add_with_match(dev, &rockchip_drm_ops, match);
474 }
475
476 static int rockchip_drm_platform_remove(struct platform_device *pdev)
477 {
478         component_master_del(&pdev->dev, &rockchip_drm_ops);
479
480         return 0;
481 }
482
483 static const struct of_device_id rockchip_drm_dt_ids[] = {
484         { .compatible = "rockchip,display-subsystem", },
485         { /* sentinel */ },
486 };
487 MODULE_DEVICE_TABLE(of, rockchip_drm_dt_ids);
488
489 static struct platform_driver rockchip_drm_platform_driver = {
490         .probe = rockchip_drm_platform_probe,
491         .remove = rockchip_drm_platform_remove,
492         .driver = {
493                 .name = "rockchip-drm",
494                 .of_match_table = rockchip_drm_dt_ids,
495                 .pm = &rockchip_drm_pm_ops,
496         },
497 };
498
499 module_platform_driver(rockchip_drm_platform_driver);
500
501 MODULE_AUTHOR("Mark Yao <mark.yao@rock-chips.com>");
502 MODULE_DESCRIPTION("ROCKCHIP DRM Driver");
503 MODULE_LICENSE("GPL v2");