2 * shmob_drm_drv.c -- SH Mobile DRM driver
4 * Copyright (C) 2012 Renesas Electronics Corporation
6 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #include <linux/clk.h>
17 #include <linux/module.h>
18 #include <linux/platform_device.h>
20 #include <linux/slab.h>
23 #include <drm/drm_crtc_helper.h>
24 #include <drm/drm_gem_cma_helper.h>
26 #include "shmob_drm_crtc.h"
27 #include "shmob_drm_drv.h"
28 #include "shmob_drm_kms.h"
29 #include "shmob_drm_plane.h"
30 #include "shmob_drm_regs.h"
32 /* -----------------------------------------------------------------------------
33 * Hardware initialization
36 static int shmob_drm_init_interface(struct shmob_drm_device
*sdev
)
38 static const u32 ldmt1r
[] = {
39 [SHMOB_DRM_IFACE_RGB8
] = LDMT1R_MIFTYP_RGB8
,
40 [SHMOB_DRM_IFACE_RGB9
] = LDMT1R_MIFTYP_RGB9
,
41 [SHMOB_DRM_IFACE_RGB12A
] = LDMT1R_MIFTYP_RGB12A
,
42 [SHMOB_DRM_IFACE_RGB12B
] = LDMT1R_MIFTYP_RGB12B
,
43 [SHMOB_DRM_IFACE_RGB16
] = LDMT1R_MIFTYP_RGB16
,
44 [SHMOB_DRM_IFACE_RGB18
] = LDMT1R_MIFTYP_RGB18
,
45 [SHMOB_DRM_IFACE_RGB24
] = LDMT1R_MIFTYP_RGB24
,
46 [SHMOB_DRM_IFACE_YUV422
] = LDMT1R_MIFTYP_YCBCR
,
47 [SHMOB_DRM_IFACE_SYS8A
] = LDMT1R_IFM
| LDMT1R_MIFTYP_SYS8A
,
48 [SHMOB_DRM_IFACE_SYS8B
] = LDMT1R_IFM
| LDMT1R_MIFTYP_SYS8B
,
49 [SHMOB_DRM_IFACE_SYS8C
] = LDMT1R_IFM
| LDMT1R_MIFTYP_SYS8C
,
50 [SHMOB_DRM_IFACE_SYS8D
] = LDMT1R_IFM
| LDMT1R_MIFTYP_SYS8D
,
51 [SHMOB_DRM_IFACE_SYS9
] = LDMT1R_IFM
| LDMT1R_MIFTYP_SYS9
,
52 [SHMOB_DRM_IFACE_SYS12
] = LDMT1R_IFM
| LDMT1R_MIFTYP_SYS12
,
53 [SHMOB_DRM_IFACE_SYS16A
] = LDMT1R_IFM
| LDMT1R_MIFTYP_SYS16A
,
54 [SHMOB_DRM_IFACE_SYS16B
] = LDMT1R_IFM
| LDMT1R_MIFTYP_SYS16B
,
55 [SHMOB_DRM_IFACE_SYS16C
] = LDMT1R_IFM
| LDMT1R_MIFTYP_SYS16C
,
56 [SHMOB_DRM_IFACE_SYS18
] = LDMT1R_IFM
| LDMT1R_MIFTYP_SYS18
,
57 [SHMOB_DRM_IFACE_SYS24
] = LDMT1R_IFM
| LDMT1R_MIFTYP_SYS24
,
60 if (sdev
->pdata
->iface
.interface
>= ARRAY_SIZE(ldmt1r
)) {
61 dev_err(sdev
->dev
, "invalid interface type %u\n",
62 sdev
->pdata
->iface
.interface
);
66 sdev
->ldmt1r
= ldmt1r
[sdev
->pdata
->iface
.interface
];
70 static int shmob_drm_setup_clocks(struct shmob_drm_device
*sdev
,
71 enum shmob_drm_clk_source clksrc
)
77 case SHMOB_DRM_CLK_BUS
:
79 sdev
->lddckr
= LDDCKR_ICKSEL_BUS
;
81 case SHMOB_DRM_CLK_PERIPHERAL
:
82 clkname
= "peripheral_clk";
83 sdev
->lddckr
= LDDCKR_ICKSEL_MIPI
;
85 case SHMOB_DRM_CLK_EXTERNAL
:
87 sdev
->lddckr
= LDDCKR_ICKSEL_HDMI
;
93 clk
= devm_clk_get(sdev
->dev
, clkname
);
95 dev_err(sdev
->dev
, "cannot get dot clock %s\n", clkname
);
103 /* -----------------------------------------------------------------------------
107 static void shmob_drm_unload(struct drm_device
*dev
)
109 drm_kms_helper_poll_fini(dev
);
110 drm_mode_config_cleanup(dev
);
111 drm_vblank_cleanup(dev
);
112 drm_irq_uninstall(dev
);
114 dev
->dev_private
= NULL
;
117 static int shmob_drm_load(struct drm_device
*dev
, unsigned long flags
)
119 struct shmob_drm_platform_data
*pdata
= dev
->dev
->platform_data
;
120 struct platform_device
*pdev
= dev
->platformdev
;
121 struct shmob_drm_device
*sdev
;
122 struct resource
*res
;
127 dev_err(dev
->dev
, "no platform data\n");
131 sdev
= devm_kzalloc(&pdev
->dev
, sizeof(*sdev
), GFP_KERNEL
);
133 dev_err(dev
->dev
, "failed to allocate private data\n");
137 sdev
->dev
= &pdev
->dev
;
139 spin_lock_init(&sdev
->irq_lock
);
142 dev
->dev_private
= sdev
;
144 /* I/O resources and clocks */
145 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
147 dev_err(&pdev
->dev
, "failed to get memory resource\n");
151 sdev
->mmio
= devm_ioremap_nocache(&pdev
->dev
, res
->start
,
153 if (sdev
->mmio
== NULL
) {
154 dev_err(&pdev
->dev
, "failed to remap memory resource\n");
158 ret
= shmob_drm_setup_clocks(sdev
, pdata
->clk_source
);
162 ret
= shmob_drm_init_interface(sdev
);
166 ret
= shmob_drm_modeset_init(sdev
);
168 dev_err(&pdev
->dev
, "failed to initialize mode setting\n");
172 for (i
= 0; i
< 4; ++i
) {
173 ret
= shmob_drm_plane_create(sdev
, i
);
175 dev_err(&pdev
->dev
, "failed to create plane %u\n", i
);
180 ret
= drm_vblank_init(dev
, 1);
182 dev_err(&pdev
->dev
, "failed to initialize vblank\n");
186 ret
= drm_irq_install(dev
, platform_get_irq(dev
->platformdev
, 0));
188 dev_err(&pdev
->dev
, "failed to install IRQ handler\n");
192 platform_set_drvdata(pdev
, sdev
);
196 shmob_drm_unload(dev
);
201 static irqreturn_t
shmob_drm_irq(int irq
, void *arg
)
203 struct drm_device
*dev
= arg
;
204 struct shmob_drm_device
*sdev
= dev
->dev_private
;
208 /* Acknowledge interrupts. Putting interrupt enable and interrupt flag
209 * bits in the same register is really brain-dead design and requires
212 spin_lock_irqsave(&sdev
->irq_lock
, flags
);
213 status
= lcdc_read(sdev
, LDINTR
);
214 lcdc_write(sdev
, LDINTR
, status
^ LDINTR_STATUS_MASK
);
215 spin_unlock_irqrestore(&sdev
->irq_lock
, flags
);
217 if (status
& LDINTR_VES
) {
218 drm_handle_vblank(dev
, 0);
219 shmob_drm_crtc_finish_page_flip(&sdev
->crtc
);
225 static int shmob_drm_enable_vblank(struct drm_device
*dev
, unsigned int pipe
)
227 struct shmob_drm_device
*sdev
= dev
->dev_private
;
229 shmob_drm_crtc_enable_vblank(sdev
, true);
234 static void shmob_drm_disable_vblank(struct drm_device
*dev
, unsigned int pipe
)
236 struct shmob_drm_device
*sdev
= dev
->dev_private
;
238 shmob_drm_crtc_enable_vblank(sdev
, false);
241 static const struct file_operations shmob_drm_fops
= {
242 .owner
= THIS_MODULE
,
244 .release
= drm_release
,
245 .unlocked_ioctl
= drm_ioctl
,
246 .compat_ioctl
= drm_compat_ioctl
,
250 .mmap
= drm_gem_cma_mmap
,
253 static struct drm_driver shmob_drm_driver
= {
254 .driver_features
= DRIVER_HAVE_IRQ
| DRIVER_GEM
| DRIVER_MODESET
256 .load
= shmob_drm_load
,
257 .unload
= shmob_drm_unload
,
258 .irq_handler
= shmob_drm_irq
,
259 .get_vblank_counter
= drm_vblank_no_hw_counter
,
260 .enable_vblank
= shmob_drm_enable_vblank
,
261 .disable_vblank
= shmob_drm_disable_vblank
,
262 .gem_free_object_unlocked
= drm_gem_cma_free_object
,
263 .gem_vm_ops
= &drm_gem_cma_vm_ops
,
264 .prime_handle_to_fd
= drm_gem_prime_handle_to_fd
,
265 .prime_fd_to_handle
= drm_gem_prime_fd_to_handle
,
266 .gem_prime_import
= drm_gem_prime_import
,
267 .gem_prime_export
= drm_gem_prime_export
,
268 .gem_prime_get_sg_table
= drm_gem_cma_prime_get_sg_table
,
269 .gem_prime_import_sg_table
= drm_gem_cma_prime_import_sg_table
,
270 .gem_prime_vmap
= drm_gem_cma_prime_vmap
,
271 .gem_prime_vunmap
= drm_gem_cma_prime_vunmap
,
272 .gem_prime_mmap
= drm_gem_cma_prime_mmap
,
273 .dumb_create
= drm_gem_cma_dumb_create
,
274 .dumb_map_offset
= drm_gem_cma_dumb_map_offset
,
275 .dumb_destroy
= drm_gem_dumb_destroy
,
276 .fops
= &shmob_drm_fops
,
278 .desc
= "Renesas SH Mobile DRM",
284 /* -----------------------------------------------------------------------------
288 #ifdef CONFIG_PM_SLEEP
289 static int shmob_drm_pm_suspend(struct device
*dev
)
291 struct shmob_drm_device
*sdev
= dev_get_drvdata(dev
);
293 drm_kms_helper_poll_disable(sdev
->ddev
);
294 shmob_drm_crtc_suspend(&sdev
->crtc
);
299 static int shmob_drm_pm_resume(struct device
*dev
)
301 struct shmob_drm_device
*sdev
= dev_get_drvdata(dev
);
303 drm_modeset_lock_all(sdev
->ddev
);
304 shmob_drm_crtc_resume(&sdev
->crtc
);
305 drm_modeset_unlock_all(sdev
->ddev
);
307 drm_kms_helper_poll_enable(sdev
->ddev
);
312 static const struct dev_pm_ops shmob_drm_pm_ops
= {
313 SET_SYSTEM_SLEEP_PM_OPS(shmob_drm_pm_suspend
, shmob_drm_pm_resume
)
316 /* -----------------------------------------------------------------------------
320 static int shmob_drm_probe(struct platform_device
*pdev
)
322 return drm_platform_init(&shmob_drm_driver
, pdev
);
325 static int shmob_drm_remove(struct platform_device
*pdev
)
327 struct shmob_drm_device
*sdev
= platform_get_drvdata(pdev
);
329 drm_put_dev(sdev
->ddev
);
334 static struct platform_driver shmob_drm_platform_driver
= {
335 .probe
= shmob_drm_probe
,
336 .remove
= shmob_drm_remove
,
339 .pm
= &shmob_drm_pm_ops
,
343 module_platform_driver(shmob_drm_platform_driver
);
345 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
346 MODULE_DESCRIPTION("Renesas SH Mobile DRM Driver");
347 MODULE_LICENSE("GPL");