blob: 4bc399734490e8f9fa3da406bd8ff818d3fbbda4 [file] [log] [blame]
Laurent Pinchart4bf8e192013-06-19 13:54:11 +02001/*
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/* -----------------------------------------------------------------------------
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020032 * DRM operations
33 */
34
35static 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);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020040
Laurent Pinchartf66ee302013-06-14 14:15:01 +020041 dev->irq_enabled = 0;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020042 dev->dev_private = NULL;
43
44 return 0;
45}
46
47static 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;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020052 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;
Laurent Pinchart481d3422013-06-14 13:38:33 +020068 rcdu->info = (struct rcar_du_device_info *)pdev->id_entry->driver_data;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020069 rcdu->ddev = dev;
70 dev->dev_private = rcdu;
71
Laurent Pinchartf66ee302013-06-14 14:15:01 +020072 /* I/O resources */
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020073 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Laurent Pinchartd5b6dcc2013-06-17 02:29:07 +020074 rcdu->mmio = devm_ioremap_resource(&pdev->dev, mem);
75 if (IS_ERR(rcdu->mmio))
76 return PTR_ERR(rcdu->mmio);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020077
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020078 /* 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
Laurent Pinchartf66ee302013-06-14 14:15:01 +020085 /* vblank handling */
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020086 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
Laurent Pinchartf66ee302013-06-14 14:15:01 +020092 dev->irq_enabled = 1;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020093
94 platform_set_drvdata(pdev, rcdu);
95
96done:
97 if (ret)
98 rcar_du_unload(dev);
99
100 return ret;
101}
102
103static 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
Laurent Pinchart990d07a2013-06-16 22:22:23 +0200108 for (i = 0; i < rcdu->num_crtcs; ++i)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200109 rcar_du_crtc_cancel_page_flip(&rcdu->crtcs[i], file);
110}
111
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200112static 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
121static 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
128static 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
143static struct drm_driver rcar_du_driver = {
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200144 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME,
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200145 .load = rcar_du_load,
146 .unload = rcar_du_unload,
147 .preclose = rcar_du_preclose,
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200148 .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,
Laurent Pinchartffb40402013-07-10 15:23:35 +0200155 .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,
Laurent Pinchart59e32642013-07-04 20:05:51 +0200162 .dumb_create = rcar_du_dumb_create,
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200163 .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
178static 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
188static 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
199static 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
207static int rcar_du_probe(struct platform_device *pdev)
208{
209 return drm_platform_init(&rcar_du_driver, pdev);
210}
211
212static int rcar_du_remove(struct platform_device *pdev)
213{
214 drm_platform_exit(&rcar_du_driver, pdev);
215
216 return 0;
217}
218
Laurent Pinchart481d3422013-06-14 13:38:33 +0200219static const struct rcar_du_device_info rcar_du_r8a7779_info = {
220 .features = 0,
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200221 .num_crtcs = 2,
Laurent Pinchartef67a902013-06-17 03:13:11 +0200222 .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 },
Laurent Pinchart481d3422013-06-14 13:38:33 +0200235};
236
Laurent Pinchartef2d84b2013-06-14 14:16:35 +0200237static const struct rcar_du_device_info rcar_du_r8a7790_info = {
Laurent Pinchart38b62fb2013-06-15 02:40:57 +0200238 .features = RCAR_DU_FEATURE_CRTC_IRQ_CLOCK | RCAR_DU_FEATURE_ALIGN_128B
239 | RCAR_DU_FEATURE_DEFR8,
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200240 .num_crtcs = 3,
Laurent Pinchartef67a902013-06-17 03:13:11 +0200241 .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 },
Laurent Pinchartef2d84b2013-06-14 14:16:35 +0200258};
259
Laurent Pinchart481d3422013-06-14 13:38:33 +0200260static const struct platform_device_id rcar_du_id_table[] = {
261 { "rcar-du-r8a7779", (kernel_ulong_t)&rcar_du_r8a7779_info },
Laurent Pinchartef2d84b2013-06-14 14:16:35 +0200262 { "rcar-du-r8a7790", (kernel_ulong_t)&rcar_du_r8a7790_info },
Laurent Pinchart481d3422013-06-14 13:38:33 +0200263 { }
264};
265
266MODULE_DEVICE_TABLE(platform, rcar_du_id_table);
267
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200268static 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 },
Laurent Pinchart481d3422013-06-14 13:38:33 +0200276 .id_table = rcar_du_id_table,
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200277};
278
279module_platform_driver(rcar_du_platform_driver);
280
281MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
282MODULE_DESCRIPTION("Renesas R-Car Display Unit DRM Driver");
283MODULE_LICENSE("GPL");