1 // SPDX-License-Identifier: GPL-2.0+
3 * shmob_drm_drv.c -- SH Mobile DRM driver
5 * Copyright (C) 2012 Renesas Electronics Corporation
7 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
10 #include <linux/clk.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
16 #include <linux/slab.h>
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_irq.h>
22 #include <drm/drm_probe_helper.h>
23 #include <drm/drm_vblank.h>
25 #include "shmob_drm_drv.h"
26 #include "shmob_drm_kms.h"
27 #include "shmob_drm_plane.h"
28 #include "shmob_drm_regs.h"
30 /* -----------------------------------------------------------------------------
31 * Hardware initialization
34 static int shmob_drm_init_interface(struct shmob_drm_device
*sdev
)
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
,
58 if (sdev
->pdata
->iface
.interface
>= ARRAY_SIZE(ldmt1r
)) {
59 dev_err(sdev
->dev
, "invalid interface type %u\n",
60 sdev
->pdata
->iface
.interface
);
64 sdev
->ldmt1r
= ldmt1r
[sdev
->pdata
->iface
.interface
];
68 static int shmob_drm_setup_clocks(struct shmob_drm_device
*sdev
,
69 enum shmob_drm_clk_source clksrc
)
75 case SHMOB_DRM_CLK_BUS
:
77 sdev
->lddckr
= LDDCKR_ICKSEL_BUS
;
79 case SHMOB_DRM_CLK_PERIPHERAL
:
80 clkname
= "peripheral_clk";
81 sdev
->lddckr
= LDDCKR_ICKSEL_MIPI
;
83 case SHMOB_DRM_CLK_EXTERNAL
:
85 sdev
->lddckr
= LDDCKR_ICKSEL_HDMI
;
91 clk
= devm_clk_get(sdev
->dev
, clkname
);
93 dev_err(sdev
->dev
, "cannot get dot clock %s\n", clkname
);
101 /* -----------------------------------------------------------------------------
105 static irqreturn_t
shmob_drm_irq(int irq
, void *arg
)
107 struct drm_device
*dev
= arg
;
108 struct shmob_drm_device
*sdev
= dev
->dev_private
;
112 /* Acknowledge interrupts. Putting interrupt enable and interrupt flag
113 * bits in the same register is really brain-dead design and requires
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
);
121 if (status
& LDINTR_VES
) {
122 drm_handle_vblank(dev
, 0);
123 shmob_drm_crtc_finish_page_flip(&sdev
->crtc
);
129 DEFINE_DRM_GEM_CMA_FOPS(shmob_drm_fops
);
131 static const struct drm_driver shmob_drm_driver
= {
132 .driver_features
= DRIVER_GEM
| DRIVER_MODESET
,
133 .irq_handler
= shmob_drm_irq
,
134 DRM_GEM_CMA_DRIVER_OPS
,
135 .fops
= &shmob_drm_fops
,
137 .desc
= "Renesas SH Mobile DRM",
143 /* -----------------------------------------------------------------------------
147 #ifdef CONFIG_PM_SLEEP
148 static int shmob_drm_pm_suspend(struct device
*dev
)
150 struct shmob_drm_device
*sdev
= dev_get_drvdata(dev
);
152 drm_kms_helper_poll_disable(sdev
->ddev
);
153 shmob_drm_crtc_suspend(&sdev
->crtc
);
158 static int shmob_drm_pm_resume(struct device
*dev
)
160 struct shmob_drm_device
*sdev
= dev_get_drvdata(dev
);
162 drm_modeset_lock_all(sdev
->ddev
);
163 shmob_drm_crtc_resume(&sdev
->crtc
);
164 drm_modeset_unlock_all(sdev
->ddev
);
166 drm_kms_helper_poll_enable(sdev
->ddev
);
171 static const struct dev_pm_ops shmob_drm_pm_ops
= {
172 SET_SYSTEM_SLEEP_PM_OPS(shmob_drm_pm_suspend
, shmob_drm_pm_resume
)
175 /* -----------------------------------------------------------------------------
179 static int shmob_drm_remove(struct platform_device
*pdev
)
181 struct shmob_drm_device
*sdev
= platform_get_drvdata(pdev
);
182 struct drm_device
*ddev
= sdev
->ddev
;
184 drm_dev_unregister(ddev
);
185 drm_kms_helper_poll_fini(ddev
);
186 drm_irq_uninstall(ddev
);
192 static int shmob_drm_probe(struct platform_device
*pdev
)
194 struct shmob_drm_platform_data
*pdata
= pdev
->dev
.platform_data
;
195 struct shmob_drm_device
*sdev
;
196 struct drm_device
*ddev
;
197 struct resource
*res
;
202 dev_err(&pdev
->dev
, "no platform data\n");
207 * Allocate and initialize the driver private data, I/O resources and
210 sdev
= devm_kzalloc(&pdev
->dev
, sizeof(*sdev
), GFP_KERNEL
);
214 sdev
->dev
= &pdev
->dev
;
216 spin_lock_init(&sdev
->irq_lock
);
218 platform_set_drvdata(pdev
, sdev
);
220 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
221 sdev
->mmio
= devm_ioremap_resource(&pdev
->dev
, res
);
222 if (IS_ERR(sdev
->mmio
))
223 return PTR_ERR(sdev
->mmio
);
225 ret
= shmob_drm_setup_clocks(sdev
, pdata
->clk_source
);
229 ret
= shmob_drm_init_interface(sdev
);
233 /* Allocate and initialize the DRM device. */
234 ddev
= drm_dev_alloc(&shmob_drm_driver
, &pdev
->dev
);
236 return PTR_ERR(ddev
);
239 ddev
->dev_private
= sdev
;
241 ret
= shmob_drm_modeset_init(sdev
);
243 dev_err(&pdev
->dev
, "failed to initialize mode setting\n");
244 goto err_free_drm_dev
;
247 for (i
= 0; i
< 4; ++i
) {
248 ret
= shmob_drm_plane_create(sdev
, i
);
250 dev_err(&pdev
->dev
, "failed to create plane %u\n", i
);
251 goto err_modeset_cleanup
;
255 ret
= drm_vblank_init(ddev
, 1);
257 dev_err(&pdev
->dev
, "failed to initialize vblank\n");
258 goto err_modeset_cleanup
;
261 ret
= drm_irq_install(ddev
, platform_get_irq(pdev
, 0));
263 dev_err(&pdev
->dev
, "failed to install IRQ handler\n");
264 goto err_modeset_cleanup
;
268 * Register the DRM device with the core and the connectors with
271 ret
= drm_dev_register(ddev
, 0);
273 goto err_irq_uninstall
;
278 drm_irq_uninstall(ddev
);
280 drm_kms_helper_poll_fini(ddev
);
287 static struct platform_driver shmob_drm_platform_driver
= {
288 .probe
= shmob_drm_probe
,
289 .remove
= shmob_drm_remove
,
292 .pm
= &shmob_drm_pm_ops
,
296 module_platform_driver(shmob_drm_platform_driver
);
298 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
299 MODULE_DESCRIPTION("Renesas SH Mobile DRM Driver");
300 MODULE_LICENSE("GPL");