1 // SPDX-License-Identifier: GPL-2.0+
3 * shmob_drm_drv.c -- SH Mobile DRM driver
5 * Copyright (C) 2012 Renesas Electronics Corporation
10 #include <linux/clk.h>
13 #include <linux/module.h>
15 #include <linux/platform_device.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/slab.h>
20 #include <drm/drm_atomic_helper.h>
21 #include <drm/drm_drv.h>
22 #include <drm/drm_fbdev_dma.h>
23 #include <drm/drm_gem_dma_helper.h>
24 #include <drm/drm_modeset_helper.h>
25 #include <drm/drm_module.h>
26 #include <drm/drm_probe_helper.h>
27 #include <drm/drm_vblank.h>
29 #include "shmob_drm_drv.h"
30 #include "shmob_drm_kms.h"
31 #include "shmob_drm_plane.h"
32 #include "shmob_drm_regs.h"
34 /* -----------------------------------------------------------------------------
35 * Hardware initialization
38 static int shmob_drm_setup_clocks(struct shmob_drm_device *sdev,
39 enum shmob_drm_clk_source clksrc)
45 case SHMOB_DRM_CLK_BUS:
47 sdev->lddckr = LDDCKR_ICKSEL_BUS;
49 case SHMOB_DRM_CLK_PERIPHERAL:
51 sdev->lddckr = LDDCKR_ICKSEL_MIPI;
53 case SHMOB_DRM_CLK_EXTERNAL:
55 sdev->lddckr = LDDCKR_ICKSEL_HDMI;
61 clk = devm_clk_get(sdev->dev, clkname);
63 dev_err(sdev->dev, "cannot get dot clock %s\n", clkname);
71 /* -----------------------------------------------------------------------------
75 static irqreturn_t shmob_drm_irq(int irq, void *arg)
77 struct drm_device *dev = arg;
78 struct shmob_drm_device *sdev = to_shmob_device(dev);
82 /* Acknowledge interrupts. Putting interrupt enable and interrupt flag
83 * bits in the same register is really brain-dead design and requires
86 spin_lock_irqsave(&sdev->irq_lock, flags);
87 status = lcdc_read(sdev, LDINTR);
88 lcdc_write(sdev, LDINTR, status ^ LDINTR_STATUS_MASK);
89 spin_unlock_irqrestore(&sdev->irq_lock, flags);
91 if (status & LDINTR_VES) {
92 drm_crtc_handle_vblank(&sdev->crtc.base);
93 shmob_drm_crtc_finish_page_flip(&sdev->crtc);
99 DEFINE_DRM_GEM_DMA_FOPS(shmob_drm_fops);
101 static const struct drm_driver shmob_drm_driver = {
102 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
103 DRM_GEM_DMA_DRIVER_OPS,
104 .fops = &shmob_drm_fops,
106 .desc = "Renesas SH Mobile DRM",
112 /* -----------------------------------------------------------------------------
116 static int shmob_drm_pm_suspend(struct device *dev)
118 struct shmob_drm_device *sdev = dev_get_drvdata(dev);
120 return drm_mode_config_helper_suspend(&sdev->ddev);
123 static int shmob_drm_pm_resume(struct device *dev)
125 struct shmob_drm_device *sdev = dev_get_drvdata(dev);
127 return drm_mode_config_helper_resume(&sdev->ddev);
130 static int shmob_drm_pm_runtime_suspend(struct device *dev)
132 struct shmob_drm_device *sdev = dev_get_drvdata(dev);
135 clk_disable_unprepare(sdev->clock);
140 static int shmob_drm_pm_runtime_resume(struct device *dev)
142 struct shmob_drm_device *sdev = dev_get_drvdata(dev);
146 ret = clk_prepare_enable(sdev->clock);
154 static const struct dev_pm_ops shmob_drm_pm_ops = {
155 SYSTEM_SLEEP_PM_OPS(shmob_drm_pm_suspend, shmob_drm_pm_resume)
156 RUNTIME_PM_OPS(shmob_drm_pm_runtime_suspend,
157 shmob_drm_pm_runtime_resume, NULL)
160 /* -----------------------------------------------------------------------------
164 static void shmob_drm_remove(struct platform_device *pdev)
166 struct shmob_drm_device *sdev = platform_get_drvdata(pdev);
167 struct drm_device *ddev = &sdev->ddev;
169 drm_dev_unregister(ddev);
170 drm_atomic_helper_shutdown(ddev);
171 drm_kms_helper_poll_fini(ddev);
174 static void shmob_drm_shutdown(struct platform_device *pdev)
176 struct shmob_drm_device *sdev = platform_get_drvdata(pdev);
178 drm_atomic_helper_shutdown(&sdev->ddev);
181 static int shmob_drm_probe(struct platform_device *pdev)
183 struct shmob_drm_platform_data *pdata = pdev->dev.platform_data;
184 const struct shmob_drm_config *config;
185 struct shmob_drm_device *sdev;
186 struct drm_device *ddev;
189 config = of_device_get_match_data(&pdev->dev);
190 if (!config && !pdata) {
191 dev_err(&pdev->dev, "no platform data\n");
196 * Allocate and initialize the DRM device, driver private data, I/O
197 * resources and clocks.
199 sdev = devm_drm_dev_alloc(&pdev->dev, &shmob_drm_driver,
200 struct shmob_drm_device, ddev);
202 return PTR_ERR(sdev);
205 sdev->dev = &pdev->dev;
207 sdev->config = *config;
210 sdev->config.clk_source = pdata->clk_source;
211 sdev->config.clk_div = pdata->iface.clk_div;
213 spin_lock_init(&sdev->irq_lock);
215 platform_set_drvdata(pdev, sdev);
217 sdev->mmio = devm_platform_ioremap_resource(pdev, 0);
218 if (IS_ERR(sdev->mmio))
219 return PTR_ERR(sdev->mmio);
221 ret = shmob_drm_setup_clocks(sdev, sdev->config.clk_source);
225 ret = devm_pm_runtime_enable(&pdev->dev);
229 ret = drm_vblank_init(ddev, 1);
231 dev_err(&pdev->dev, "failed to initialize vblank\n");
235 ret = shmob_drm_modeset_init(sdev);
237 return dev_err_probe(&pdev->dev, ret,
238 "failed to initialize mode setting\n");
240 ret = platform_get_irq(pdev, 0);
242 goto err_modeset_cleanup;
245 ret = devm_request_irq(&pdev->dev, sdev->irq, shmob_drm_irq, 0,
246 ddev->driver->name, ddev);
248 dev_err(&pdev->dev, "failed to install IRQ handler\n");
249 goto err_modeset_cleanup;
253 * Register the DRM device with the core and the connectors with
256 ret = drm_dev_register(ddev, 0);
258 goto err_modeset_cleanup;
260 drm_fbdev_dma_setup(ddev, 16);
265 drm_kms_helper_poll_fini(ddev);
269 static const struct shmob_drm_config shmob_arm_config = {
270 .clk_source = SHMOB_DRM_CLK_BUS,
274 static const struct of_device_id shmob_drm_of_table[] __maybe_unused = {
275 { .compatible = "renesas,r8a7740-lcdc", .data = &shmob_arm_config, },
276 { .compatible = "renesas,sh73a0-lcdc", .data = &shmob_arm_config, },
280 static struct platform_driver shmob_drm_platform_driver = {
281 .probe = shmob_drm_probe,
282 .remove_new = shmob_drm_remove,
283 .shutdown = shmob_drm_shutdown,
286 .of_match_table = of_match_ptr(shmob_drm_of_table),
287 .pm = &shmob_drm_pm_ops,
291 drm_module_platform_driver(shmob_drm_platform_driver);
294 MODULE_DESCRIPTION("Renesas SH Mobile DRM Driver");
295 MODULE_LICENSE("GPL");