1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * shmob_drm_drv.c  --  SH Mobile DRM driver
4  *
5  * Copyright (C) 2012 Renesas Electronics Corporation
6  *
7  * Laurent Pinchart ([email protected])
8  */
9 
10 #include <linux/clk.h>
11 #include <linux/io.h>
12 #include <linux/mm.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/platform_device.h>
16 #include <linux/pm.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/slab.h>
19 
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>
30 
31 #include "shmob_drm_drv.h"
32 #include "shmob_drm_kms.h"
33 #include "shmob_drm_plane.h"
34 #include "shmob_drm_regs.h"
35 
36 /* -----------------------------------------------------------------------------
37  * Hardware initialization
38  */
39 
shmob_drm_setup_clocks(struct shmob_drm_device * sdev,enum shmob_drm_clk_source clksrc)40 static int shmob_drm_setup_clocks(struct shmob_drm_device *sdev,
41 				  enum shmob_drm_clk_source clksrc)
42 {
43 	struct clk *clk;
44 	char *clkname;
45 
46 	switch (clksrc) {
47 	case SHMOB_DRM_CLK_BUS:
48 		clkname = "fck";
49 		sdev->lddckr = LDDCKR_ICKSEL_BUS;
50 		break;
51 	case SHMOB_DRM_CLK_PERIPHERAL:
52 		clkname = "media";
53 		sdev->lddckr = LDDCKR_ICKSEL_MIPI;
54 		break;
55 	case SHMOB_DRM_CLK_EXTERNAL:
56 		clkname = "lclk";
57 		sdev->lddckr = LDDCKR_ICKSEL_HDMI;
58 		break;
59 	default:
60 		return -EINVAL;
61 	}
62 
63 	clk = devm_clk_get(sdev->dev, clkname);
64 	if (IS_ERR(clk)) {
65 		dev_err(sdev->dev, "cannot get dot clock %s\n", clkname);
66 		return PTR_ERR(clk);
67 	}
68 
69 	sdev->clock = clk;
70 	return 0;
71 }
72 
73 /* -----------------------------------------------------------------------------
74  * DRM operations
75  */
76 
shmob_drm_irq(int irq,void * arg)77 static irqreturn_t shmob_drm_irq(int irq, void *arg)
78 {
79 	struct drm_device *dev = arg;
80 	struct shmob_drm_device *sdev = to_shmob_device(dev);
81 	unsigned long flags;
82 	u32 status;
83 
84 	/* Acknowledge interrupts. Putting interrupt enable and interrupt flag
85 	 * bits in the same register is really brain-dead design and requires
86 	 * taking a spinlock.
87 	 */
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);
92 
93 	if (status & LDINTR_VES) {
94 		drm_crtc_handle_vblank(&sdev->crtc.base);
95 		shmob_drm_crtc_finish_page_flip(&sdev->crtc);
96 	}
97 
98 	return IRQ_HANDLED;
99 }
100 
101 DEFINE_DRM_GEM_DMA_FOPS(shmob_drm_fops);
102 
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,
108 	.name			= "shmob-drm",
109 	.desc			= "Renesas SH Mobile DRM",
110 	.major			= 1,
111 	.minor			= 0,
112 };
113 
114 /* -----------------------------------------------------------------------------
115  * Power management
116  */
117 
shmob_drm_pm_suspend(struct device * dev)118 static int shmob_drm_pm_suspend(struct device *dev)
119 {
120 	struct shmob_drm_device *sdev = dev_get_drvdata(dev);
121 
122 	return drm_mode_config_helper_suspend(&sdev->ddev);
123 }
124 
shmob_drm_pm_resume(struct device * dev)125 static int shmob_drm_pm_resume(struct device *dev)
126 {
127 	struct shmob_drm_device *sdev = dev_get_drvdata(dev);
128 
129 	return drm_mode_config_helper_resume(&sdev->ddev);
130 }
131 
shmob_drm_pm_runtime_suspend(struct device * dev)132 static int shmob_drm_pm_runtime_suspend(struct device *dev)
133 {
134 	struct shmob_drm_device *sdev = dev_get_drvdata(dev);
135 
136 	if (sdev->clock)
137 		clk_disable_unprepare(sdev->clock);
138 
139 	return 0;
140 }
141 
shmob_drm_pm_runtime_resume(struct device * dev)142 static int shmob_drm_pm_runtime_resume(struct device *dev)
143 {
144 	struct shmob_drm_device *sdev = dev_get_drvdata(dev);
145 	int ret;
146 
147 	if (sdev->clock) {
148 		ret = clk_prepare_enable(sdev->clock);
149 		if (ret < 0)
150 			return ret;
151 	}
152 
153 	return 0;
154 }
155 
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)
160 };
161 
162 /* -----------------------------------------------------------------------------
163  * Platform driver
164  */
165 
shmob_drm_remove(struct platform_device * pdev)166 static void shmob_drm_remove(struct platform_device *pdev)
167 {
168 	struct shmob_drm_device *sdev = platform_get_drvdata(pdev);
169 	struct drm_device *ddev = &sdev->ddev;
170 
171 	drm_dev_unregister(ddev);
172 	drm_atomic_helper_shutdown(ddev);
173 	drm_kms_helper_poll_fini(ddev);
174 }
175 
shmob_drm_shutdown(struct platform_device * pdev)176 static void shmob_drm_shutdown(struct platform_device *pdev)
177 {
178 	struct shmob_drm_device *sdev = platform_get_drvdata(pdev);
179 
180 	drm_atomic_helper_shutdown(&sdev->ddev);
181 }
182 
shmob_drm_probe(struct platform_device * pdev)183 static int shmob_drm_probe(struct platform_device *pdev)
184 {
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;
189 	int ret;
190 
191 	config = of_device_get_match_data(&pdev->dev);
192 	if (!config && !pdata) {
193 		dev_err(&pdev->dev, "no platform data\n");
194 		return -EINVAL;
195 	}
196 
197 	/*
198 	 * Allocate and initialize the DRM device, driver private data, I/O
199 	 * resources and clocks.
200 	 */
201 	sdev = devm_drm_dev_alloc(&pdev->dev, &shmob_drm_driver,
202 				  struct shmob_drm_device, ddev);
203 	if (IS_ERR(sdev))
204 		return PTR_ERR(sdev);
205 
206 	ddev = &sdev->ddev;
207 	sdev->dev = &pdev->dev;
208 	if (config) {
209 		sdev->config = *config;
210 	} else {
211 		sdev->pdata = pdata;
212 		sdev->config.clk_source = pdata->clk_source;
213 		sdev->config.clk_div = pdata->iface.clk_div;
214 	}
215 	spin_lock_init(&sdev->irq_lock);
216 
217 	platform_set_drvdata(pdev, sdev);
218 
219 	sdev->mmio = devm_platform_ioremap_resource(pdev, 0);
220 	if (IS_ERR(sdev->mmio))
221 		return PTR_ERR(sdev->mmio);
222 
223 	ret = shmob_drm_setup_clocks(sdev, sdev->config.clk_source);
224 	if (ret < 0)
225 		return ret;
226 
227 	ret = devm_pm_runtime_enable(&pdev->dev);
228 	if (ret)
229 		return ret;
230 
231 	ret = drm_vblank_init(ddev, 1);
232 	if (ret < 0) {
233 		dev_err(&pdev->dev, "failed to initialize vblank\n");
234 		return ret;
235 	}
236 
237 	ret = shmob_drm_modeset_init(sdev);
238 	if (ret < 0)
239 		return dev_err_probe(&pdev->dev, ret,
240 				     "failed to initialize mode setting\n");
241 
242 	ret = platform_get_irq(pdev, 0);
243 	if (ret < 0)
244 		goto err_modeset_cleanup;
245 	sdev->irq = ret;
246 
247 	ret = devm_request_irq(&pdev->dev, sdev->irq, shmob_drm_irq, 0,
248 			       ddev->driver->name, ddev);
249 	if (ret < 0) {
250 		dev_err(&pdev->dev, "failed to install IRQ handler\n");
251 		goto err_modeset_cleanup;
252 	}
253 
254 	/*
255 	 * Register the DRM device with the core and the connectors with
256 	 * sysfs.
257 	 */
258 	ret = drm_dev_register(ddev, 0);
259 	if (ret < 0)
260 		goto err_modeset_cleanup;
261 
262 	drm_client_setup_with_fourcc(ddev, DRM_FORMAT_RGB565);
263 
264 	return 0;
265 
266 err_modeset_cleanup:
267 	drm_kms_helper_poll_fini(ddev);
268 	return ret;
269 }
270 
271 static const struct shmob_drm_config shmob_arm_config = {
272 	.clk_source = SHMOB_DRM_CLK_BUS,
273 	.clk_div = 5,
274 };
275 
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, },
279 	{ /* sentinel */ }
280 };
281 
282 static struct platform_driver shmob_drm_platform_driver = {
283 	.probe		= shmob_drm_probe,
284 	.remove		= shmob_drm_remove,
285 	.shutdown	= shmob_drm_shutdown,
286 	.driver		= {
287 		.name	= "shmob-drm",
288 		.of_match_table = of_match_ptr(shmob_drm_of_table),
289 		.pm	= &shmob_drm_pm_ops,
290 	},
291 };
292 
293 drm_module_platform_driver(shmob_drm_platform_driver);
294 
295 MODULE_AUTHOR("Laurent Pinchart <[email protected]>");
296 MODULE_DESCRIPTION("Renesas SH Mobile DRM Driver");
297 MODULE_LICENSE("GPL");
298