2 * Copyright 2015 Freescale Semiconductor, Inc.
4 * Freescale DCU drm device driver
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
12 #include <linux/clk.h>
13 #include <linux/clk-provider.h>
15 #include <linux/mfd/syscon.h>
17 #include <linux/module.h>
18 #include <linux/of_platform.h>
19 #include <linux/platform_device.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/regmap.h>
25 #include <drm/drm_crtc_helper.h>
26 #include <drm/drm_gem_cma_helper.h>
28 #include "fsl_dcu_drm_crtc.h"
29 #include "fsl_dcu_drm_drv.h"
31 static const struct regmap_config fsl_dcu_regmap_config
= {
35 .cache_type
= REGCACHE_RBTREE
,
38 static int fsl_dcu_drm_irq_init(struct drm_device
*dev
)
40 struct fsl_dcu_drm_device
*fsl_dev
= dev
->dev_private
;
44 ret
= drm_irq_install(dev
, fsl_dev
->irq
);
46 dev_err(dev
->dev
, "failed to install IRQ handler\n");
48 ret
= regmap_write(fsl_dev
->regmap
, DCU_INT_STATUS
, 0);
50 dev_err(dev
->dev
, "set DCU_INT_STATUS failed\n");
51 ret
= regmap_read(fsl_dev
->regmap
, DCU_INT_MASK
, &value
);
53 dev_err(dev
->dev
, "read DCU_INT_MASK failed\n");
54 value
&= DCU_INT_MASK_VBLANK
;
55 ret
= regmap_write(fsl_dev
->regmap
, DCU_INT_MASK
, value
);
57 dev_err(dev
->dev
, "set DCU_INT_MASK failed\n");
58 ret
= regmap_write(fsl_dev
->regmap
, DCU_UPDATE_MODE
,
59 DCU_UPDATE_MODE_READREG
);
61 dev_err(dev
->dev
, "set DCU_UPDATE_MODE failed\n");
66 static int fsl_dcu_load(struct drm_device
*drm
, unsigned long flags
)
68 struct device
*dev
= drm
->dev
;
69 struct fsl_dcu_drm_device
*fsl_dev
= drm
->dev_private
;
72 ret
= fsl_dcu_drm_modeset_init(fsl_dev
);
74 dev_err(dev
, "failed to initialize mode setting\n");
78 ret
= drm_vblank_init(drm
, drm
->mode_config
.num_crtc
);
80 dev_err(dev
, "failed to initialize vblank\n");
83 drm
->vblank_disable_allowed
= true;
85 ret
= fsl_dcu_drm_irq_init(drm
);
88 drm
->irq_enabled
= true;
90 fsl_dcu_fbdev_init(drm
);
95 drm_mode_config_cleanup(drm
);
96 drm_vblank_cleanup(drm
);
97 drm_irq_uninstall(drm
);
98 drm
->dev_private
= NULL
;
104 static int fsl_dcu_unload(struct drm_device
*dev
)
106 drm_mode_config_cleanup(dev
);
107 drm_vblank_cleanup(dev
);
108 drm_irq_uninstall(dev
);
110 dev
->dev_private
= NULL
;
115 static void fsl_dcu_drm_preclose(struct drm_device
*dev
, struct drm_file
*file
)
119 static irqreturn_t
fsl_dcu_drm_irq(int irq
, void *arg
)
121 struct drm_device
*dev
= arg
;
122 struct fsl_dcu_drm_device
*fsl_dev
= dev
->dev_private
;
123 unsigned int int_status
;
126 ret
= regmap_read(fsl_dev
->regmap
, DCU_INT_STATUS
, &int_status
);
128 dev_err(dev
->dev
, "set DCU_INT_STATUS failed\n");
129 if (int_status
& DCU_INT_STATUS_VBLANK
)
130 drm_handle_vblank(dev
, 0);
132 ret
= regmap_write(fsl_dev
->regmap
, DCU_INT_STATUS
, 0xffffffff);
134 dev_err(dev
->dev
, "set DCU_INT_STATUS failed\n");
135 ret
= regmap_write(fsl_dev
->regmap
, DCU_UPDATE_MODE
,
136 DCU_UPDATE_MODE_READREG
);
138 dev_err(dev
->dev
, "set DCU_UPDATE_MODE failed\n");
143 static int fsl_dcu_drm_enable_vblank(struct drm_device
*dev
, unsigned int pipe
)
145 struct fsl_dcu_drm_device
*fsl_dev
= dev
->dev_private
;
149 ret
= regmap_read(fsl_dev
->regmap
, DCU_INT_MASK
, &value
);
151 dev_err(dev
->dev
, "read DCU_INT_MASK failed\n");
152 value
&= ~DCU_INT_MASK_VBLANK
;
153 ret
= regmap_write(fsl_dev
->regmap
, DCU_INT_MASK
, value
);
155 dev_err(dev
->dev
, "set DCU_INT_MASK failed\n");
159 static void fsl_dcu_drm_disable_vblank(struct drm_device
*dev
,
162 struct fsl_dcu_drm_device
*fsl_dev
= dev
->dev_private
;
166 ret
= regmap_read(fsl_dev
->regmap
, DCU_INT_MASK
, &value
);
168 dev_err(dev
->dev
, "read DCU_INT_MASK failed\n");
169 value
|= DCU_INT_MASK_VBLANK
;
170 ret
= regmap_write(fsl_dev
->regmap
, DCU_INT_MASK
, value
);
172 dev_err(dev
->dev
, "set DCU_INT_MASK failed\n");
175 static const struct file_operations fsl_dcu_drm_fops
= {
176 .owner
= THIS_MODULE
,
178 .release
= drm_release
,
179 .unlocked_ioctl
= drm_ioctl
,
181 .compat_ioctl
= drm_compat_ioctl
,
186 .mmap
= drm_gem_cma_mmap
,
189 static struct drm_driver fsl_dcu_drm_driver
= {
190 .driver_features
= DRIVER_HAVE_IRQ
| DRIVER_GEM
| DRIVER_MODESET
191 | DRIVER_PRIME
| DRIVER_ATOMIC
,
192 .load
= fsl_dcu_load
,
193 .unload
= fsl_dcu_unload
,
194 .preclose
= fsl_dcu_drm_preclose
,
195 .irq_handler
= fsl_dcu_drm_irq
,
196 .get_vblank_counter
= drm_vblank_no_hw_counter
,
197 .enable_vblank
= fsl_dcu_drm_enable_vblank
,
198 .disable_vblank
= fsl_dcu_drm_disable_vblank
,
199 .gem_free_object
= drm_gem_cma_free_object
,
200 .gem_vm_ops
= &drm_gem_cma_vm_ops
,
201 .prime_handle_to_fd
= drm_gem_prime_handle_to_fd
,
202 .prime_fd_to_handle
= drm_gem_prime_fd_to_handle
,
203 .gem_prime_import
= drm_gem_prime_import
,
204 .gem_prime_export
= drm_gem_prime_export
,
205 .gem_prime_get_sg_table
= drm_gem_cma_prime_get_sg_table
,
206 .gem_prime_import_sg_table
= drm_gem_cma_prime_import_sg_table
,
207 .gem_prime_vmap
= drm_gem_cma_prime_vmap
,
208 .gem_prime_vunmap
= drm_gem_cma_prime_vunmap
,
209 .gem_prime_mmap
= drm_gem_cma_prime_mmap
,
210 .dumb_create
= drm_gem_cma_dumb_create
,
211 .dumb_map_offset
= drm_gem_cma_dumb_map_offset
,
212 .dumb_destroy
= drm_gem_dumb_destroy
,
213 .fops
= &fsl_dcu_drm_fops
,
214 .name
= "fsl-dcu-drm",
215 .desc
= "Freescale DCU DRM",
221 #ifdef CONFIG_PM_SLEEP
222 static int fsl_dcu_drm_pm_suspend(struct device
*dev
)
224 struct fsl_dcu_drm_device
*fsl_dev
= dev_get_drvdata(dev
);
229 drm_kms_helper_poll_disable(fsl_dev
->drm
);
230 regcache_cache_only(fsl_dev
->regmap
, true);
231 regcache_mark_dirty(fsl_dev
->regmap
);
232 clk_disable(fsl_dev
->clk
);
233 clk_unprepare(fsl_dev
->clk
);
238 static int fsl_dcu_drm_pm_resume(struct device
*dev
)
240 struct fsl_dcu_drm_device
*fsl_dev
= dev_get_drvdata(dev
);
246 ret
= clk_enable(fsl_dev
->clk
);
248 dev_err(dev
, "failed to enable dcu clk\n");
249 clk_unprepare(fsl_dev
->clk
);
252 ret
= clk_prepare(fsl_dev
->clk
);
254 dev_err(dev
, "failed to prepare dcu clk\n");
258 drm_kms_helper_poll_enable(fsl_dev
->drm
);
259 regcache_cache_only(fsl_dev
->regmap
, false);
260 regcache_sync(fsl_dev
->regmap
);
266 static const struct dev_pm_ops fsl_dcu_drm_pm_ops
= {
267 SET_SYSTEM_SLEEP_PM_OPS(fsl_dcu_drm_pm_suspend
, fsl_dcu_drm_pm_resume
)
270 static const struct fsl_dcu_soc_data fsl_dcu_ls1021a_data
= {
276 static const struct fsl_dcu_soc_data fsl_dcu_vf610_data
= {
282 static const struct of_device_id fsl_dcu_of_match
[] = {
284 .compatible
= "fsl,ls1021a-dcu",
285 .data
= &fsl_dcu_ls1021a_data
,
287 .compatible
= "fsl,vf610-dcu",
288 .data
= &fsl_dcu_vf610_data
,
292 MODULE_DEVICE_TABLE(of
, fsl_dcu_of_match
);
294 static int fsl_dcu_drm_probe(struct platform_device
*pdev
)
296 struct fsl_dcu_drm_device
*fsl_dev
;
297 struct drm_device
*drm
;
298 struct device
*dev
= &pdev
->dev
;
299 struct resource
*res
;
301 struct drm_driver
*driver
= &fsl_dcu_drm_driver
;
302 const struct of_device_id
*id
;
305 fsl_dev
= devm_kzalloc(dev
, sizeof(*fsl_dev
), GFP_KERNEL
);
309 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
311 dev_err(dev
, "could not get memory IO resource\n");
315 base
= devm_ioremap_resource(dev
, res
);
321 fsl_dev
->irq
= platform_get_irq(pdev
, 0);
322 if (fsl_dev
->irq
< 0) {
323 dev_err(dev
, "failed to get irq\n");
327 fsl_dev
->clk
= devm_clk_get(dev
, "dcu");
328 if (IS_ERR(fsl_dev
->clk
)) {
329 ret
= PTR_ERR(fsl_dev
->clk
);
330 dev_err(dev
, "failed to get dcu clock\n");
333 ret
= clk_prepare(fsl_dev
->clk
);
335 dev_err(dev
, "failed to prepare dcu clk\n");
338 ret
= clk_enable(fsl_dev
->clk
);
340 dev_err(dev
, "failed to enable dcu clk\n");
341 clk_unprepare(fsl_dev
->clk
);
345 fsl_dev
->regmap
= devm_regmap_init_mmio(dev
, base
,
346 &fsl_dcu_regmap_config
);
347 if (IS_ERR(fsl_dev
->regmap
)) {
348 dev_err(dev
, "regmap init failed\n");
349 return PTR_ERR(fsl_dev
->regmap
);
352 id
= of_match_node(fsl_dcu_of_match
, pdev
->dev
.of_node
);
355 fsl_dev
->soc
= id
->data
;
357 drm
= drm_dev_alloc(driver
, dev
);
363 fsl_dev
->np
= dev
->of_node
;
364 drm
->dev_private
= fsl_dev
;
365 dev_set_drvdata(dev
, fsl_dev
);
366 drm_dev_set_unique(drm
, dev_name(dev
));
368 ret
= drm_dev_register(drm
, 0);
372 DRM_INFO("Initialized %s %d.%d.%d %s on minor %d\n", driver
->name
,
373 driver
->major
, driver
->minor
, driver
->patchlevel
,
374 driver
->date
, drm
->primary
->index
);
383 static int fsl_dcu_drm_remove(struct platform_device
*pdev
)
385 struct fsl_dcu_drm_device
*fsl_dev
= platform_get_drvdata(pdev
);
387 drm_put_dev(fsl_dev
->drm
);
392 static struct platform_driver fsl_dcu_drm_platform_driver
= {
393 .probe
= fsl_dcu_drm_probe
,
394 .remove
= fsl_dcu_drm_remove
,
397 .pm
= &fsl_dcu_drm_pm_ops
,
398 .of_match_table
= fsl_dcu_of_match
,
402 module_platform_driver(fsl_dcu_drm_platform_driver
);
404 MODULE_DESCRIPTION("Freescale DCU DRM Driver");
405 MODULE_LICENSE("GPL");