xref: /linux/drivers/gpu/drm/renesas/shmobile/shmob_drm_drv.c (revision 0c7c237b1c35011ef0b8d30c1d5c20bc6ae7b69b)
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 (laurent.pinchart@ideasonboard.com)
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/platform_device.h>
15 #include <linux/pm.h>
16 #include <linux/slab.h>
17 
18 #include <drm/drm_drv.h>
19 #include <drm/drm_fbdev_generic.h>
20 #include <drm/drm_gem_dma_helper.h>
21 #include <drm/drm_module.h>
22 #include <drm/drm_probe_helper.h>
23 #include <drm/drm_vblank.h>
24 
25 #include "shmob_drm_drv.h"
26 #include "shmob_drm_kms.h"
27 #include "shmob_drm_plane.h"
28 #include "shmob_drm_regs.h"
29 
30 /* -----------------------------------------------------------------------------
31  * Hardware initialization
32  */
33 
34 static int shmob_drm_init_interface(struct shmob_drm_device *sdev)
35 {
36 	static const u32 ldmt1r[] = {
37 		[SHMOB_DRM_IFACE_RGB8] = LDMT1R_MIFTYP_RGB8,
38 		[SHMOB_DRM_IFACE_RGB9] = LDMT1R_MIFTYP_RGB9,
39 		[SHMOB_DRM_IFACE_RGB12A] = LDMT1R_MIFTYP_RGB12A,
40 		[SHMOB_DRM_IFACE_RGB12B] = LDMT1R_MIFTYP_RGB12B,
41 		[SHMOB_DRM_IFACE_RGB16] = LDMT1R_MIFTYP_RGB16,
42 		[SHMOB_DRM_IFACE_RGB18] = LDMT1R_MIFTYP_RGB18,
43 		[SHMOB_DRM_IFACE_RGB24] = LDMT1R_MIFTYP_RGB24,
44 		[SHMOB_DRM_IFACE_YUV422] = LDMT1R_MIFTYP_YCBCR,
45 		[SHMOB_DRM_IFACE_SYS8A] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS8A,
46 		[SHMOB_DRM_IFACE_SYS8B] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS8B,
47 		[SHMOB_DRM_IFACE_SYS8C] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS8C,
48 		[SHMOB_DRM_IFACE_SYS8D] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS8D,
49 		[SHMOB_DRM_IFACE_SYS9] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS9,
50 		[SHMOB_DRM_IFACE_SYS12] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS12,
51 		[SHMOB_DRM_IFACE_SYS16A] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS16A,
52 		[SHMOB_DRM_IFACE_SYS16B] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS16B,
53 		[SHMOB_DRM_IFACE_SYS16C] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS16C,
54 		[SHMOB_DRM_IFACE_SYS18] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS18,
55 		[SHMOB_DRM_IFACE_SYS24] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS24,
56 	};
57 
58 	if (sdev->pdata->iface.interface >= ARRAY_SIZE(ldmt1r)) {
59 		dev_err(sdev->dev, "invalid interface type %u\n",
60 			sdev->pdata->iface.interface);
61 		return -EINVAL;
62 	}
63 
64 	sdev->ldmt1r = ldmt1r[sdev->pdata->iface.interface];
65 	return 0;
66 }
67 
68 static int shmob_drm_setup_clocks(struct shmob_drm_device *sdev,
69 					    enum shmob_drm_clk_source clksrc)
70 {
71 	struct clk *clk;
72 	char *clkname;
73 
74 	switch (clksrc) {
75 	case SHMOB_DRM_CLK_BUS:
76 		clkname = "bus_clk";
77 		sdev->lddckr = LDDCKR_ICKSEL_BUS;
78 		break;
79 	case SHMOB_DRM_CLK_PERIPHERAL:
80 		clkname = "peripheral_clk";
81 		sdev->lddckr = LDDCKR_ICKSEL_MIPI;
82 		break;
83 	case SHMOB_DRM_CLK_EXTERNAL:
84 		clkname = NULL;
85 		sdev->lddckr = LDDCKR_ICKSEL_HDMI;
86 		break;
87 	default:
88 		return -EINVAL;
89 	}
90 
91 	clk = devm_clk_get(sdev->dev, clkname);
92 	if (IS_ERR(clk)) {
93 		dev_err(sdev->dev, "cannot get dot clock %s\n", clkname);
94 		return PTR_ERR(clk);
95 	}
96 
97 	sdev->clock = clk;
98 	return 0;
99 }
100 
101 /* -----------------------------------------------------------------------------
102  * DRM operations
103  */
104 
105 static irqreturn_t shmob_drm_irq(int irq, void *arg)
106 {
107 	struct drm_device *dev = arg;
108 	struct shmob_drm_device *sdev = dev->dev_private;
109 	unsigned long flags;
110 	u32 status;
111 
112 	/* Acknowledge interrupts. Putting interrupt enable and interrupt flag
113 	 * bits in the same register is really brain-dead design and requires
114 	 * taking a spinlock.
115 	 */
116 	spin_lock_irqsave(&sdev->irq_lock, flags);
117 	status = lcdc_read(sdev, LDINTR);
118 	lcdc_write(sdev, LDINTR, status ^ LDINTR_STATUS_MASK);
119 	spin_unlock_irqrestore(&sdev->irq_lock, flags);
120 
121 	if (status & LDINTR_VES) {
122 		drm_handle_vblank(dev, 0);
123 		shmob_drm_crtc_finish_page_flip(&sdev->crtc);
124 	}
125 
126 	return IRQ_HANDLED;
127 }
128 
129 DEFINE_DRM_GEM_DMA_FOPS(shmob_drm_fops);
130 
131 static const struct drm_driver shmob_drm_driver = {
132 	.driver_features	= DRIVER_GEM | DRIVER_MODESET,
133 	DRM_GEM_DMA_DRIVER_OPS,
134 	.fops			= &shmob_drm_fops,
135 	.name			= "shmob-drm",
136 	.desc			= "Renesas SH Mobile DRM",
137 	.date			= "20120424",
138 	.major			= 1,
139 	.minor			= 0,
140 };
141 
142 /* -----------------------------------------------------------------------------
143  * Power management
144  */
145 
146 static int shmob_drm_pm_suspend(struct device *dev)
147 {
148 	struct shmob_drm_device *sdev = dev_get_drvdata(dev);
149 
150 	drm_kms_helper_poll_disable(sdev->ddev);
151 	shmob_drm_crtc_suspend(&sdev->crtc);
152 
153 	return 0;
154 }
155 
156 static int shmob_drm_pm_resume(struct device *dev)
157 {
158 	struct shmob_drm_device *sdev = dev_get_drvdata(dev);
159 
160 	drm_modeset_lock_all(sdev->ddev);
161 	shmob_drm_crtc_resume(&sdev->crtc);
162 	drm_modeset_unlock_all(sdev->ddev);
163 
164 	drm_kms_helper_poll_enable(sdev->ddev);
165 	return 0;
166 }
167 
168 static DEFINE_SIMPLE_DEV_PM_OPS(shmob_drm_pm_ops,
169 				shmob_drm_pm_suspend, shmob_drm_pm_resume);
170 
171 /* -----------------------------------------------------------------------------
172  * Platform driver
173  */
174 
175 static int shmob_drm_remove(struct platform_device *pdev)
176 {
177 	struct shmob_drm_device *sdev = platform_get_drvdata(pdev);
178 	struct drm_device *ddev = sdev->ddev;
179 
180 	drm_dev_unregister(ddev);
181 	drm_kms_helper_poll_fini(ddev);
182 	free_irq(sdev->irq, ddev);
183 	drm_dev_put(ddev);
184 
185 	return 0;
186 }
187 
188 static int shmob_drm_probe(struct platform_device *pdev)
189 {
190 	struct shmob_drm_platform_data *pdata = pdev->dev.platform_data;
191 	struct shmob_drm_device *sdev;
192 	struct drm_device *ddev;
193 	unsigned int i;
194 	int ret;
195 
196 	if (pdata == NULL) {
197 		dev_err(&pdev->dev, "no platform data\n");
198 		return -EINVAL;
199 	}
200 
201 	/*
202 	 * Allocate and initialize the driver private data, I/O resources and
203 	 * clocks.
204 	 */
205 	sdev = devm_kzalloc(&pdev->dev, sizeof(*sdev), GFP_KERNEL);
206 	if (sdev == NULL)
207 		return -ENOMEM;
208 
209 	sdev->dev = &pdev->dev;
210 	sdev->pdata = pdata;
211 	spin_lock_init(&sdev->irq_lock);
212 
213 	platform_set_drvdata(pdev, sdev);
214 
215 	sdev->mmio = devm_platform_ioremap_resource(pdev, 0);
216 	if (IS_ERR(sdev->mmio))
217 		return PTR_ERR(sdev->mmio);
218 
219 	ret = shmob_drm_setup_clocks(sdev, pdata->clk_source);
220 	if (ret < 0)
221 		return ret;
222 
223 	ret = shmob_drm_init_interface(sdev);
224 	if (ret < 0)
225 		return ret;
226 
227 	/* Allocate and initialize the DRM device. */
228 	ddev = drm_dev_alloc(&shmob_drm_driver, &pdev->dev);
229 	if (IS_ERR(ddev))
230 		return PTR_ERR(ddev);
231 
232 	sdev->ddev = ddev;
233 	ddev->dev_private = sdev;
234 
235 	ret = shmob_drm_modeset_init(sdev);
236 	if (ret < 0) {
237 		dev_err(&pdev->dev, "failed to initialize mode setting\n");
238 		goto err_free_drm_dev;
239 	}
240 
241 	for (i = 0; i < 4; ++i) {
242 		ret = shmob_drm_plane_create(sdev, i);
243 		if (ret < 0) {
244 			dev_err(&pdev->dev, "failed to create plane %u\n", i);
245 			goto err_modeset_cleanup;
246 		}
247 	}
248 
249 	ret = drm_vblank_init(ddev, 1);
250 	if (ret < 0) {
251 		dev_err(&pdev->dev, "failed to initialize vblank\n");
252 		goto err_modeset_cleanup;
253 	}
254 
255 	ret = platform_get_irq(pdev, 0);
256 	if (ret < 0)
257 		goto err_modeset_cleanup;
258 	sdev->irq = ret;
259 
260 	ret = request_irq(sdev->irq, shmob_drm_irq, 0, ddev->driver->name,
261 			  ddev);
262 	if (ret < 0) {
263 		dev_err(&pdev->dev, "failed to install IRQ handler\n");
264 		goto err_modeset_cleanup;
265 	}
266 
267 	/*
268 	 * Register the DRM device with the core and the connectors with
269 	 * sysfs.
270 	 */
271 	ret = drm_dev_register(ddev, 0);
272 	if (ret < 0)
273 		goto err_irq_uninstall;
274 
275 	drm_fbdev_generic_setup(ddev, 16);
276 
277 	return 0;
278 
279 err_irq_uninstall:
280 	free_irq(sdev->irq, ddev);
281 err_modeset_cleanup:
282 	drm_kms_helper_poll_fini(ddev);
283 err_free_drm_dev:
284 	drm_dev_put(ddev);
285 
286 	return ret;
287 }
288 
289 static struct platform_driver shmob_drm_platform_driver = {
290 	.probe		= shmob_drm_probe,
291 	.remove		= shmob_drm_remove,
292 	.driver		= {
293 		.name	= "shmob-drm",
294 		.pm	= pm_sleep_ptr(&shmob_drm_pm_ops),
295 	},
296 };
297 
298 drm_module_platform_driver(shmob_drm_platform_driver);
299 
300 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
301 MODULE_DESCRIPTION("Renesas SH Mobile DRM Driver");
302 MODULE_LICENSE("GPL");
303