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 unsigned int i;
196 int ret;
197
198 if (pdata == NULL) {
199 dev_err(&pdev->dev, "no platform data\n");
200 return -EINVAL;
201 }
202
203 /*
204 * Allocate and initialize the driver private data, I/O resources and
205 * clocks.
206 */
207 sdev = devm_kzalloc(&pdev->dev, sizeof(*sdev), GFP_KERNEL);
208 if (sdev == NULL)
209 return -ENOMEM;
210
211 sdev->dev = &pdev->dev;
212 sdev->pdata = pdata;
213 spin_lock_init(&sdev->irq_lock);
214
215 platform_set_drvdata(pdev, sdev);
216
217 sdev->mmio = devm_platform_ioremap_resource(pdev, 0);
218 if (IS_ERR(sdev->mmio))
219 return PTR_ERR(sdev->mmio);
220
221 ret = shmob_drm_setup_clocks(sdev, pdata->clk_source);
222 if (ret < 0)
223 return ret;
224
225 ret = shmob_drm_init_interface(sdev);
226 if (ret < 0)
227 return ret;
228
229 /* Allocate and initialize the DRM device. */
230 ddev = drm_dev_alloc(&shmob_drm_driver, &pdev->dev);
231 if (IS_ERR(ddev))
232 return PTR_ERR(ddev);
233
234 sdev->ddev = ddev;
235 ddev->dev_private = sdev;
236
237 ret = shmob_drm_modeset_init(sdev);
238 if (ret < 0) {
239 dev_err(&pdev->dev, "failed to initialize mode setting\n");
240 goto err_free_drm_dev;
241 }
242
243 for (i = 0; i < 4; ++i) {
244 ret = shmob_drm_plane_create(sdev, i);
245 if (ret < 0) {
246 dev_err(&pdev->dev, "failed to create plane %u\n", i);
247 goto err_modeset_cleanup;
248 }
249 }
250
251 ret = drm_vblank_init(ddev, 1);
252 if (ret < 0) {
253 dev_err(&pdev->dev, "failed to initialize vblank\n");
254 goto err_modeset_cleanup;
255 }
256
257 ret = platform_get_irq(pdev, 0);
258 if (ret < 0)
259 goto err_modeset_cleanup;
260 sdev->irq = ret;
261
262 ret = request_irq(sdev->irq, shmob_drm_irq, 0, ddev->driver->name,
263 ddev);
264 if (ret < 0) {
265 dev_err(&pdev->dev, "failed to install IRQ handler\n");
266 goto err_modeset_cleanup;
267 }
268
269 /*
270 * Register the DRM device with the core and the connectors with
271 * sysfs.
272 */
273 ret = drm_dev_register(ddev, 0);
274 if (ret < 0)
275 goto err_irq_uninstall;
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 = &shmob_drm_pm_ops,
295 },
296 };
297
298 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