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/clients/drm_client_setup.h>
21 #include <drm/drm_atomic_helper.h>
22 #include <drm/drm_drv.h>
23 #include <drm/drm_fbdev_dma.h>
24 #include <drm/drm_fourcc.h>
25 #include <drm/drm_gem_dma_helper.h>
26 #include <drm/drm_modeset_helper.h>
27 #include <drm/drm_module.h>
28 #include <drm/drm_probe_helper.h>
29 #include <drm/drm_vblank.h>
31 #include "shmob_drm_drv.h"
32 #include "shmob_drm_kms.h"
33 #include "shmob_drm_plane.h"
34 #include "shmob_drm_regs.h"
36 /* -----------------------------------------------------------------------------
37 * Hardware initialization
40 static int shmob_drm_setup_clocks(struct shmob_drm_device *sdev,
41 enum shmob_drm_clk_source clksrc)
47 case SHMOB_DRM_CLK_BUS:
49 sdev->lddckr = LDDCKR_ICKSEL_BUS;
51 case SHMOB_DRM_CLK_PERIPHERAL:
53 sdev->lddckr = LDDCKR_ICKSEL_MIPI;
55 case SHMOB_DRM_CLK_EXTERNAL:
57 sdev->lddckr = LDDCKR_ICKSEL_HDMI;
63 clk = devm_clk_get(sdev->dev, clkname);
65 dev_err(sdev->dev, "cannot get dot clock %s\n", clkname);
73 /* -----------------------------------------------------------------------------
77 static irqreturn_t shmob_drm_irq(int irq, void *arg)
79 struct drm_device *dev = arg;
80 struct shmob_drm_device *sdev = to_shmob_device(dev);
84 /* Acknowledge interrupts. Putting interrupt enable and interrupt flag
85 * bits in the same register is really brain-dead design and requires
88 spin_lock_irqsave(&sdev->irq_lock, flags);
89 status = lcdc_read(sdev, LDINTR);
90 lcdc_write(sdev, LDINTR, status ^ LDINTR_STATUS_MASK);
91 spin_unlock_irqrestore(&sdev->irq_lock, flags);
93 if (status & LDINTR_VES) {
94 drm_crtc_handle_vblank(&sdev->crtc.base);
95 shmob_drm_crtc_finish_page_flip(&sdev->crtc);
101 DEFINE_DRM_GEM_DMA_FOPS(shmob_drm_fops);
103 static const struct drm_driver shmob_drm_driver = {
104 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
105 DRM_GEM_DMA_DRIVER_OPS,
106 DRM_FBDEV_DMA_DRIVER_OPS,
107 .fops = &shmob_drm_fops,
109 .desc = "Renesas SH Mobile DRM",
114 /* -----------------------------------------------------------------------------
118 static int shmob_drm_pm_suspend(struct device *dev)
120 struct shmob_drm_device *sdev = dev_get_drvdata(dev);
122 return drm_mode_config_helper_suspend(&sdev->ddev);
125 static int shmob_drm_pm_resume(struct device *dev)
127 struct shmob_drm_device *sdev = dev_get_drvdata(dev);
129 return drm_mode_config_helper_resume(&sdev->ddev);
132 static int shmob_drm_pm_runtime_suspend(struct device *dev)
134 struct shmob_drm_device *sdev = dev_get_drvdata(dev);
137 clk_disable_unprepare(sdev->clock);
142 static int shmob_drm_pm_runtime_resume(struct device *dev)
144 struct shmob_drm_device *sdev = dev_get_drvdata(dev);
148 ret = clk_prepare_enable(sdev->clock);
156 static const struct dev_pm_ops shmob_drm_pm_ops = {
157 SYSTEM_SLEEP_PM_OPS(shmob_drm_pm_suspend, shmob_drm_pm_resume)
158 RUNTIME_PM_OPS(shmob_drm_pm_runtime_suspend,
159 shmob_drm_pm_runtime_resume, NULL)
162 /* -----------------------------------------------------------------------------
166 static void shmob_drm_remove(struct platform_device *pdev)
168 struct shmob_drm_device *sdev = platform_get_drvdata(pdev);
169 struct drm_device *ddev = &sdev->ddev;
171 drm_dev_unregister(ddev);
172 drm_atomic_helper_shutdown(ddev);
173 drm_kms_helper_poll_fini(ddev);
176 static void shmob_drm_shutdown(struct platform_device *pdev)
178 struct shmob_drm_device *sdev = platform_get_drvdata(pdev);
180 drm_atomic_helper_shutdown(&sdev->ddev);
183 static int shmob_drm_probe(struct platform_device *pdev)
185 struct shmob_drm_platform_data *pdata = pdev->dev.platform_data;
186 const struct shmob_drm_config *config;
187 struct shmob_drm_device *sdev;
188 struct drm_device *ddev;
191 config = of_device_get_match_data(&pdev->dev);
192 if (!config && !pdata) {
193 dev_err(&pdev->dev, "no platform data\n");
198 * Allocate and initialize the DRM device, driver private data, I/O
199 * resources and clocks.
201 sdev = devm_drm_dev_alloc(&pdev->dev, &shmob_drm_driver,
202 struct shmob_drm_device, ddev);
204 return PTR_ERR(sdev);
207 sdev->dev = &pdev->dev;
209 sdev->config = *config;
212 sdev->config.clk_source = pdata->clk_source;
213 sdev->config.clk_div = pdata->iface.clk_div;
215 spin_lock_init(&sdev->irq_lock);
217 platform_set_drvdata(pdev, sdev);
219 sdev->mmio = devm_platform_ioremap_resource(pdev, 0);
220 if (IS_ERR(sdev->mmio))
221 return PTR_ERR(sdev->mmio);
223 ret = shmob_drm_setup_clocks(sdev, sdev->config.clk_source);
227 ret = devm_pm_runtime_enable(&pdev->dev);
231 ret = drm_vblank_init(ddev, 1);
233 dev_err(&pdev->dev, "failed to initialize vblank\n");
237 ret = shmob_drm_modeset_init(sdev);
239 return dev_err_probe(&pdev->dev, ret,
240 "failed to initialize mode setting\n");
242 ret = platform_get_irq(pdev, 0);
244 goto err_modeset_cleanup;
247 ret = devm_request_irq(&pdev->dev, sdev->irq, shmob_drm_irq, 0,
248 ddev->driver->name, ddev);
250 dev_err(&pdev->dev, "failed to install IRQ handler\n");
251 goto err_modeset_cleanup;
255 * Register the DRM device with the core and the connectors with
258 ret = drm_dev_register(ddev, 0);
260 goto err_modeset_cleanup;
262 drm_client_setup_with_fourcc(ddev, DRM_FORMAT_RGB565);
267 drm_kms_helper_poll_fini(ddev);
271 static const struct shmob_drm_config shmob_arm_config = {
272 .clk_source = SHMOB_DRM_CLK_BUS,
276 static const struct of_device_id shmob_drm_of_table[] __maybe_unused = {
277 { .compatible = "renesas,r8a7740-lcdc", .data = &shmob_arm_config, },
278 { .compatible = "renesas,sh73a0-lcdc", .data = &shmob_arm_config, },
282 static struct platform_driver shmob_drm_platform_driver = {
283 .probe = shmob_drm_probe,
284 .remove = shmob_drm_remove,
285 .shutdown = shmob_drm_shutdown,
288 .of_match_table = of_match_ptr(shmob_drm_of_table),
289 .pm = &shmob_drm_pm_ops,
293 drm_module_platform_driver(shmob_drm_platform_driver);
296 MODULE_DESCRIPTION("Renesas SH Mobile DRM Driver");
297 MODULE_LICENSE("GPL");