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