2 * rcar_du_drv.c -- R-Car Display Unit DRM driver
4 * Copyright (C) 2013 Renesas Corporation
6 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
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.
14 #include <linux/clk.h>
17 #include <linux/module.h>
18 #include <linux/platform_device.h>
20 #include <linux/slab.h>
23 #include <drm/drm_crtc_helper.h>
24 #include <drm/drm_gem_cma_helper.h>
26 #include "rcar_du_crtc.h"
27 #include "rcar_du_drv.h"
28 #include "rcar_du_kms.h"
29 #include "rcar_du_regs.h"
31 /* -----------------------------------------------------------------------------
32 * Core device operations
36 * rcar_du_get - Acquire a reference to the DU
38 * Acquiring a reference enables the device clock and setup core registers. A
39 * reference must be held before accessing any hardware registers.
41 * This function must be called with the DRM mode_config lock held.
43 * Return 0 in case of success or a negative error code otherwise.
45 int rcar_du_get(struct rcar_du_device *rcdu)
52 /* Enable clocks before accessing the hardware. */
53 ret = clk_prepare_enable(rcdu->clock);
57 /* Enable extended features */
58 rcar_du_write(rcdu, DEFR, DEFR_CODE | DEFR_DEFE);
59 rcar_du_write(rcdu, DEFR2, DEFR2_CODE | DEFR2_DEFE2G);
60 rcar_du_write(rcdu, DEFR3, DEFR3_CODE | DEFR3_DEFE3);
61 rcar_du_write(rcdu, DEFR4, DEFR4_CODE);
62 rcar_du_write(rcdu, DEFR5, DEFR5_CODE | DEFR5_DEFE5);
64 /* Use DS1PR and DS2PR to configure planes priorities and connects the
65 * superposition 0 to DU0 pins. DU1 pins will be configured dynamically.
67 rcar_du_write(rcdu, DORCR, DORCR_PG1D_DS1 | DORCR_DPRS);
75 * rcar_du_put - Release a reference to the DU
77 * Releasing the last reference disables the device clock.
79 * This function must be called with the DRM mode_config lock held.
81 void rcar_du_put(struct rcar_du_device *rcdu)
83 if (--rcdu->use_count)
86 clk_disable_unprepare(rcdu->clock);
89 /* -----------------------------------------------------------------------------
93 static int rcar_du_unload(struct drm_device *dev)
95 drm_kms_helper_poll_fini(dev);
96 drm_mode_config_cleanup(dev);
97 drm_vblank_cleanup(dev);
98 drm_irq_uninstall(dev);
100 dev->dev_private = NULL;
105 static int rcar_du_load(struct drm_device *dev, unsigned long flags)
107 struct platform_device *pdev = dev->platformdev;
108 struct rcar_du_platform_data *pdata = pdev->dev.platform_data;
109 struct rcar_du_device *rcdu;
110 struct resource *ioarea;
111 struct resource *mem;
115 dev_err(dev->dev, "no platform data\n");
119 rcdu = devm_kzalloc(&pdev->dev, sizeof(*rcdu), GFP_KERNEL);
121 dev_err(dev->dev, "failed to allocate private data\n");
125 rcdu->dev = &pdev->dev;
128 dev->dev_private = rcdu;
130 /* I/O resources and clocks */
131 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
133 dev_err(&pdev->dev, "failed to get memory resource\n");
137 ioarea = devm_request_mem_region(&pdev->dev, mem->start,
138 resource_size(mem), pdev->name);
139 if (ioarea == NULL) {
140 dev_err(&pdev->dev, "failed to request memory region\n");
144 rcdu->mmio = devm_ioremap_nocache(&pdev->dev, ioarea->start,
145 resource_size(ioarea));
146 if (rcdu->mmio == NULL) {
147 dev_err(&pdev->dev, "failed to remap memory resource\n");
151 rcdu->clock = devm_clk_get(&pdev->dev, NULL);
152 if (IS_ERR(rcdu->clock)) {
153 dev_err(&pdev->dev, "failed to get clock\n");
157 /* DRM/KMS objects */
158 ret = rcar_du_modeset_init(rcdu);
160 dev_err(&pdev->dev, "failed to initialize DRM/KMS\n");
164 /* IRQ and vblank handling */
165 ret = drm_vblank_init(dev, (1 << rcdu->num_crtcs) - 1);
167 dev_err(&pdev->dev, "failed to initialize vblank\n");
171 ret = drm_irq_install(dev);
173 dev_err(&pdev->dev, "failed to install IRQ handler\n");
177 platform_set_drvdata(pdev, rcdu);
186 static void rcar_du_preclose(struct drm_device *dev, struct drm_file *file)
188 struct rcar_du_device *rcdu = dev->dev_private;
191 for (i = 0; i < ARRAY_SIZE(rcdu->crtcs); ++i)
192 rcar_du_crtc_cancel_page_flip(&rcdu->crtcs[i], file);
195 static irqreturn_t rcar_du_irq(int irq, void *arg)
197 struct drm_device *dev = arg;
198 struct rcar_du_device *rcdu = dev->dev_private;
201 for (i = 0; i < ARRAY_SIZE(rcdu->crtcs); ++i)
202 rcar_du_crtc_irq(&rcdu->crtcs[i]);
207 static int rcar_du_enable_vblank(struct drm_device *dev, int crtc)
209 struct rcar_du_device *rcdu = dev->dev_private;
211 rcar_du_crtc_enable_vblank(&rcdu->crtcs[crtc], true);
216 static void rcar_du_disable_vblank(struct drm_device *dev, int crtc)
218 struct rcar_du_device *rcdu = dev->dev_private;
220 rcar_du_crtc_enable_vblank(&rcdu->crtcs[crtc], false);
223 static const struct file_operations rcar_du_fops = {
224 .owner = THIS_MODULE,
226 .release = drm_release,
227 .unlocked_ioctl = drm_ioctl,
229 .compat_ioctl = drm_compat_ioctl,
233 .fasync = drm_fasync,
235 .mmap = drm_gem_cma_mmap,
238 static struct drm_driver rcar_du_driver = {
239 .driver_features = DRIVER_HAVE_IRQ | DRIVER_GEM | DRIVER_MODESET
241 .load = rcar_du_load,
242 .unload = rcar_du_unload,
243 .preclose = rcar_du_preclose,
244 .irq_handler = rcar_du_irq,
245 .get_vblank_counter = drm_vblank_count,
246 .enable_vblank = rcar_du_enable_vblank,
247 .disable_vblank = rcar_du_disable_vblank,
248 .gem_free_object = drm_gem_cma_free_object,
249 .gem_vm_ops = &drm_gem_cma_vm_ops,
250 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
251 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
252 .gem_prime_import = drm_gem_cma_dmabuf_import,
253 .gem_prime_export = drm_gem_cma_dmabuf_export,
254 .dumb_create = rcar_du_dumb_create,
255 .dumb_map_offset = drm_gem_cma_dumb_map_offset,
256 .dumb_destroy = drm_gem_cma_dumb_destroy,
257 .fops = &rcar_du_fops,
259 .desc = "Renesas R-Car Display Unit",
265 /* -----------------------------------------------------------------------------
270 static int rcar_du_pm_suspend(struct device *dev)
272 struct rcar_du_device *rcdu = dev_get_drvdata(dev);
274 drm_kms_helper_poll_disable(rcdu->ddev);
275 /* TODO Suspend the CRTC */
280 static int rcar_du_pm_resume(struct device *dev)
282 struct rcar_du_device *rcdu = dev_get_drvdata(dev);
284 /* TODO Resume the CRTC */
286 drm_kms_helper_poll_enable(rcdu->ddev);
291 static const struct dev_pm_ops rcar_du_pm_ops = {
292 SET_SYSTEM_SLEEP_PM_OPS(rcar_du_pm_suspend, rcar_du_pm_resume)
295 /* -----------------------------------------------------------------------------
299 static int rcar_du_probe(struct platform_device *pdev)
301 return drm_platform_init(&rcar_du_driver, pdev);
304 static int rcar_du_remove(struct platform_device *pdev)
306 drm_platform_exit(&rcar_du_driver, pdev);
311 static struct platform_driver rcar_du_platform_driver = {
312 .probe = rcar_du_probe,
313 .remove = rcar_du_remove,
315 .owner = THIS_MODULE,
317 .pm = &rcar_du_pm_ops,
321 module_platform_driver(rcar_du_platform_driver);
323 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
324 MODULE_DESCRIPTION("Renesas R-Car Display Unit DRM Driver");
325 MODULE_LICENSE("GPL");