]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/rcar-du/rcar_du_drv.c
drm/rcar-du: Configure RGB output routing to DPAD0
[karo-tx-linux.git] / drivers / gpu / drm / rcar-du / rcar_du_drv.c
1 /*
2  * rcar_du_drv.c  --  R-Car Display Unit DRM driver
3  *
4  * Copyright (C) 2013 Renesas Corporation
5  *
6  * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  */
13
14 #include <linux/clk.h>
15 #include <linux/io.h>
16 #include <linux/mm.h>
17 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/pm.h>
20 #include <linux/slab.h>
21
22 #include <drm/drmP.h>
23 #include <drm/drm_crtc_helper.h>
24 #include <drm/drm_gem_cma_helper.h>
25
26 #include "rcar_du_crtc.h"
27 #include "rcar_du_drv.h"
28 #include "rcar_du_kms.h"
29 #include "rcar_du_regs.h"
30
31 /* -----------------------------------------------------------------------------
32  * DRM operations
33  */
34
35 static int rcar_du_unload(struct drm_device *dev)
36 {
37         drm_kms_helper_poll_fini(dev);
38         drm_mode_config_cleanup(dev);
39         drm_vblank_cleanup(dev);
40
41         dev->irq_enabled = 0;
42         dev->dev_private = NULL;
43
44         return 0;
45 }
46
47 static int rcar_du_load(struct drm_device *dev, unsigned long flags)
48 {
49         struct platform_device *pdev = dev->platformdev;
50         struct rcar_du_platform_data *pdata = pdev->dev.platform_data;
51         struct rcar_du_device *rcdu;
52         struct resource *mem;
53         int ret;
54
55         if (pdata == NULL) {
56                 dev_err(dev->dev, "no platform data\n");
57                 return -ENODEV;
58         }
59
60         rcdu = devm_kzalloc(&pdev->dev, sizeof(*rcdu), GFP_KERNEL);
61         if (rcdu == NULL) {
62                 dev_err(dev->dev, "failed to allocate private data\n");
63                 return -ENOMEM;
64         }
65
66         rcdu->dev = &pdev->dev;
67         rcdu->pdata = pdata;
68         rcdu->info = (struct rcar_du_device_info *)pdev->id_entry->driver_data;
69         rcdu->ddev = dev;
70         dev->dev_private = rcdu;
71
72         /* I/O resources */
73         mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
74         rcdu->mmio = devm_ioremap_resource(&pdev->dev, mem);
75         if (IS_ERR(rcdu->mmio))
76                 return PTR_ERR(rcdu->mmio);
77
78         /* DRM/KMS objects */
79         ret = rcar_du_modeset_init(rcdu);
80         if (ret < 0) {
81                 dev_err(&pdev->dev, "failed to initialize DRM/KMS\n");
82                 goto done;
83         }
84
85         /* vblank handling */
86         ret = drm_vblank_init(dev, (1 << rcdu->num_crtcs) - 1);
87         if (ret < 0) {
88                 dev_err(&pdev->dev, "failed to initialize vblank\n");
89                 goto done;
90         }
91
92         dev->irq_enabled = 1;
93
94         platform_set_drvdata(pdev, rcdu);
95
96 done:
97         if (ret)
98                 rcar_du_unload(dev);
99
100         return ret;
101 }
102
103 static void rcar_du_preclose(struct drm_device *dev, struct drm_file *file)
104 {
105         struct rcar_du_device *rcdu = dev->dev_private;
106         unsigned int i;
107
108         for (i = 0; i < rcdu->num_crtcs; ++i)
109                 rcar_du_crtc_cancel_page_flip(&rcdu->crtcs[i], file);
110 }
111
112 static int rcar_du_enable_vblank(struct drm_device *dev, int crtc)
113 {
114         struct rcar_du_device *rcdu = dev->dev_private;
115
116         rcar_du_crtc_enable_vblank(&rcdu->crtcs[crtc], true);
117
118         return 0;
119 }
120
121 static void rcar_du_disable_vblank(struct drm_device *dev, int crtc)
122 {
123         struct rcar_du_device *rcdu = dev->dev_private;
124
125         rcar_du_crtc_enable_vblank(&rcdu->crtcs[crtc], false);
126 }
127
128 static const struct file_operations rcar_du_fops = {
129         .owner          = THIS_MODULE,
130         .open           = drm_open,
131         .release        = drm_release,
132         .unlocked_ioctl = drm_ioctl,
133 #ifdef CONFIG_COMPAT
134         .compat_ioctl   = drm_compat_ioctl,
135 #endif
136         .poll           = drm_poll,
137         .read           = drm_read,
138         .fasync         = drm_fasync,
139         .llseek         = no_llseek,
140         .mmap           = drm_gem_cma_mmap,
141 };
142
143 static struct drm_driver rcar_du_driver = {
144         .driver_features        = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME,
145         .load                   = rcar_du_load,
146         .unload                 = rcar_du_unload,
147         .preclose               = rcar_du_preclose,
148         .get_vblank_counter     = drm_vblank_count,
149         .enable_vblank          = rcar_du_enable_vblank,
150         .disable_vblank         = rcar_du_disable_vblank,
151         .gem_free_object        = drm_gem_cma_free_object,
152         .gem_vm_ops             = &drm_gem_cma_vm_ops,
153         .prime_handle_to_fd     = drm_gem_prime_handle_to_fd,
154         .prime_fd_to_handle     = drm_gem_prime_fd_to_handle,
155         .gem_prime_import       = drm_gem_prime_import,
156         .gem_prime_export       = drm_gem_prime_export,
157         .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
158         .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
159         .gem_prime_vmap         = drm_gem_cma_prime_vmap,
160         .gem_prime_vunmap       = drm_gem_cma_prime_vunmap,
161         .gem_prime_mmap         = drm_gem_cma_prime_mmap,
162         .dumb_create            = rcar_du_dumb_create,
163         .dumb_map_offset        = drm_gem_cma_dumb_map_offset,
164         .dumb_destroy           = drm_gem_cma_dumb_destroy,
165         .fops                   = &rcar_du_fops,
166         .name                   = "rcar-du",
167         .desc                   = "Renesas R-Car Display Unit",
168         .date                   = "20130110",
169         .major                  = 1,
170         .minor                  = 0,
171 };
172
173 /* -----------------------------------------------------------------------------
174  * Power management
175  */
176
177 #if CONFIG_PM_SLEEP
178 static int rcar_du_pm_suspend(struct device *dev)
179 {
180         struct rcar_du_device *rcdu = dev_get_drvdata(dev);
181
182         drm_kms_helper_poll_disable(rcdu->ddev);
183         /* TODO Suspend the CRTC */
184
185         return 0;
186 }
187
188 static int rcar_du_pm_resume(struct device *dev)
189 {
190         struct rcar_du_device *rcdu = dev_get_drvdata(dev);
191
192         /* TODO Resume the CRTC */
193
194         drm_kms_helper_poll_enable(rcdu->ddev);
195         return 0;
196 }
197 #endif
198
199 static const struct dev_pm_ops rcar_du_pm_ops = {
200         SET_SYSTEM_SLEEP_PM_OPS(rcar_du_pm_suspend, rcar_du_pm_resume)
201 };
202
203 /* -----------------------------------------------------------------------------
204  * Platform driver
205  */
206
207 static int rcar_du_probe(struct platform_device *pdev)
208 {
209         return drm_platform_init(&rcar_du_driver, pdev);
210 }
211
212 static int rcar_du_remove(struct platform_device *pdev)
213 {
214         drm_platform_exit(&rcar_du_driver, pdev);
215
216         return 0;
217 }
218
219 static const struct rcar_du_device_info rcar_du_r8a7779_info = {
220         .features = 0,
221         .num_crtcs = 2,
222         .routes = {
223                 /* R8A7779 has two RGB outputs and one (currently unsupported)
224                  * TCON output.
225                  */
226                 [RCAR_DU_OUTPUT_DPAD0] = {
227                         .possible_crtcs = BIT(0),
228                         .encoder_type = DRM_MODE_ENCODER_NONE,
229                 },
230                 [RCAR_DU_OUTPUT_DPAD1] = {
231                         .possible_crtcs = BIT(1) | BIT(0),
232                         .encoder_type = DRM_MODE_ENCODER_NONE,
233                 },
234         },
235 };
236
237 static const struct rcar_du_device_info rcar_du_r8a7790_info = {
238         .features = RCAR_DU_FEATURE_CRTC_IRQ_CLOCK | RCAR_DU_FEATURE_ALIGN_128B
239                   | RCAR_DU_FEATURE_DEFR8,
240         .num_crtcs = 3,
241         .routes = {
242                 /* R8A7790 has one RGB output, two LVDS outputs and one
243                  * (currently unsupported) TCON output.
244                  */
245                 [RCAR_DU_OUTPUT_DPAD0] = {
246                         .possible_crtcs = BIT(2) | BIT(1) | BIT(0),
247                         .encoder_type = DRM_MODE_ENCODER_NONE,
248                 },
249                 [RCAR_DU_OUTPUT_LVDS0] = {
250                         .possible_crtcs = BIT(0),
251                         .encoder_type = DRM_MODE_ENCODER_LVDS,
252                 },
253                 [RCAR_DU_OUTPUT_LVDS1] = {
254                         .possible_crtcs = BIT(2) | BIT(1),
255                         .encoder_type = DRM_MODE_ENCODER_LVDS,
256                 },
257         },
258 };
259
260 static const struct platform_device_id rcar_du_id_table[] = {
261         { "rcar-du-r8a7779", (kernel_ulong_t)&rcar_du_r8a7779_info },
262         { "rcar-du-r8a7790", (kernel_ulong_t)&rcar_du_r8a7790_info },
263         { }
264 };
265
266 MODULE_DEVICE_TABLE(platform, rcar_du_id_table);
267
268 static struct platform_driver rcar_du_platform_driver = {
269         .probe          = rcar_du_probe,
270         .remove         = rcar_du_remove,
271         .driver         = {
272                 .owner  = THIS_MODULE,
273                 .name   = "rcar-du",
274                 .pm     = &rcar_du_pm_ops,
275         },
276         .id_table       = rcar_du_id_table,
277 };
278
279 module_platform_driver(rcar_du_platform_driver);
280
281 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
282 MODULE_DESCRIPTION("Renesas R-Car Display Unit DRM Driver");
283 MODULE_LICENSE("GPL");