1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2015 Freescale Semiconductor, Inc.
5 * Freescale DCU drm device driver
9 #include <linux/clk-provider.h>
10 #include <linux/console.h>
12 #include <linux/mfd/syscon.h>
14 #include <linux/module.h>
15 #include <linux/of_platform.h>
16 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/regmap.h>
21 #include <drm/drm_atomic_helper.h>
22 #include <drm/drm_client_setup.h>
23 #include <drm/drm_drv.h>
24 #include <drm/drm_fbdev_dma.h>
25 #include <drm/drm_gem_dma_helper.h>
26 #include <drm/drm_modeset_helper.h>
27 #include <drm/drm_module.h>
28 #include <drm/drm_probe_helper.h>
29 #include <drm/drm_vblank.h>
31 #include "fsl_dcu_drm_crtc.h"
32 #include "fsl_dcu_drm_drv.h"
35 static int legacyfb_depth
= 24;
36 module_param(legacyfb_depth
, int, 0444);
38 static bool fsl_dcu_drm_is_volatile_reg(struct device
*dev
, unsigned int reg
)
40 if (reg
== DCU_INT_STATUS
|| reg
== DCU_UPDATE_MODE
)
46 static const struct regmap_config fsl_dcu_regmap_config
= {
51 .volatile_reg
= fsl_dcu_drm_is_volatile_reg
,
54 static void fsl_dcu_irq_reset(struct drm_device
*dev
)
56 struct fsl_dcu_drm_device
*fsl_dev
= dev
->dev_private
;
58 regmap_write(fsl_dev
->regmap
, DCU_INT_STATUS
, ~0);
59 regmap_write(fsl_dev
->regmap
, DCU_INT_MASK
, ~0);
62 static irqreturn_t
fsl_dcu_drm_irq(int irq
, void *arg
)
64 struct drm_device
*dev
= arg
;
65 struct fsl_dcu_drm_device
*fsl_dev
= dev
->dev_private
;
66 unsigned int int_status
;
69 ret
= regmap_read(fsl_dev
->regmap
, DCU_INT_STATUS
, &int_status
);
71 dev_err(dev
->dev
, "read DCU_INT_STATUS failed\n");
75 if (int_status
& DCU_INT_STATUS_VBLANK
)
76 drm_handle_vblank(dev
, 0);
78 regmap_write(fsl_dev
->regmap
, DCU_INT_STATUS
, int_status
);
83 static int fsl_dcu_irq_install(struct drm_device
*dev
, unsigned int irq
)
85 if (irq
== IRQ_NOTCONNECTED
)
88 fsl_dcu_irq_reset(dev
);
90 return request_irq(irq
, fsl_dcu_drm_irq
, 0, dev
->driver
->name
, dev
);
93 static void fsl_dcu_irq_uninstall(struct drm_device
*dev
)
95 struct fsl_dcu_drm_device
*fsl_dev
= dev
->dev_private
;
97 fsl_dcu_irq_reset(dev
);
98 free_irq(fsl_dev
->irq
, dev
);
101 static int fsl_dcu_load(struct drm_device
*dev
, unsigned long flags
)
103 struct fsl_dcu_drm_device
*fsl_dev
= dev
->dev_private
;
107 ret
= fsl_dcu_drm_modeset_init(fsl_dev
);
109 return dev_err_probe(dev
->dev
, ret
, "failed to initialize mode setting\n");
111 scfg
= syscon_regmap_lookup_by_compatible("fsl,ls1021a-scfg");
112 if (PTR_ERR(scfg
) != -ENODEV
) {
114 * For simplicity, enable the PIXCLK unconditionally,
115 * resulting in increased power consumption. Disabling
116 * the clock in PM or on unload could be implemented as
117 * a future improvement.
119 ret
= regmap_update_bits(scfg
, SCFG_PIXCLKCR
, SCFG_PIXCLKCR_PXCEN
,
120 SCFG_PIXCLKCR_PXCEN
);
122 return dev_err_probe(dev
->dev
, ret
, "failed to enable pixclk\n");
125 ret
= drm_vblank_init(dev
, dev
->mode_config
.num_crtc
);
127 dev_err(dev
->dev
, "failed to initialize vblank\n");
131 ret
= fsl_dcu_irq_install(dev
, fsl_dev
->irq
);
133 dev_err(dev
->dev
, "failed to install IRQ handler\n");
137 if (legacyfb_depth
!= 16 && legacyfb_depth
!= 24 &&
138 legacyfb_depth
!= 32) {
140 "Invalid legacyfb_depth. Defaulting to 24bpp\n");
146 drm_kms_helper_poll_fini(dev
);
148 drm_mode_config_cleanup(dev
);
150 dev
->dev_private
= NULL
;
155 static void fsl_dcu_unload(struct drm_device
*dev
)
157 drm_atomic_helper_shutdown(dev
);
158 drm_kms_helper_poll_fini(dev
);
160 drm_mode_config_cleanup(dev
);
161 fsl_dcu_irq_uninstall(dev
);
163 dev
->dev_private
= NULL
;
166 DEFINE_DRM_GEM_DMA_FOPS(fsl_dcu_drm_fops
);
168 static const struct drm_driver fsl_dcu_drm_driver
= {
169 .driver_features
= DRIVER_GEM
| DRIVER_MODESET
| DRIVER_ATOMIC
,
170 .load
= fsl_dcu_load
,
171 .unload
= fsl_dcu_unload
,
172 DRM_GEM_DMA_DRIVER_OPS
,
173 DRM_FBDEV_DMA_DRIVER_OPS
,
174 .fops
= &fsl_dcu_drm_fops
,
175 .name
= "fsl-dcu-drm",
176 .desc
= "Freescale DCU DRM",
182 #ifdef CONFIG_PM_SLEEP
183 static int fsl_dcu_drm_pm_suspend(struct device
*dev
)
185 struct fsl_dcu_drm_device
*fsl_dev
= dev_get_drvdata(dev
);
191 disable_irq(fsl_dev
->irq
);
193 ret
= drm_mode_config_helper_suspend(fsl_dev
->drm
);
195 enable_irq(fsl_dev
->irq
);
199 clk_disable_unprepare(fsl_dev
->clk
);
204 static int fsl_dcu_drm_pm_resume(struct device
*dev
)
206 struct fsl_dcu_drm_device
*fsl_dev
= dev_get_drvdata(dev
);
212 ret
= clk_prepare_enable(fsl_dev
->clk
);
214 dev_err(dev
, "failed to enable dcu clk\n");
219 fsl_tcon_bypass_enable(fsl_dev
->tcon
);
220 fsl_dcu_drm_init_planes(fsl_dev
->drm
);
221 enable_irq(fsl_dev
->irq
);
223 drm_mode_config_helper_resume(fsl_dev
->drm
);
229 static const struct dev_pm_ops fsl_dcu_drm_pm_ops
= {
230 SET_SYSTEM_SLEEP_PM_OPS(fsl_dcu_drm_pm_suspend
, fsl_dcu_drm_pm_resume
)
233 static const struct fsl_dcu_soc_data fsl_dcu_ls1021a_data
= {
237 .layer_regs
= LS1021A_LAYER_REG_NUM
,
240 static const struct fsl_dcu_soc_data fsl_dcu_vf610_data
= {
244 .layer_regs
= VF610_LAYER_REG_NUM
,
247 static const struct of_device_id fsl_dcu_of_match
[] = {
249 .compatible
= "fsl,ls1021a-dcu",
250 .data
= &fsl_dcu_ls1021a_data
,
252 .compatible
= "fsl,vf610-dcu",
253 .data
= &fsl_dcu_vf610_data
,
257 MODULE_DEVICE_TABLE(of
, fsl_dcu_of_match
);
259 static int fsl_dcu_drm_probe(struct platform_device
*pdev
)
261 struct fsl_dcu_drm_device
*fsl_dev
;
262 struct drm_device
*drm
;
263 struct device
*dev
= &pdev
->dev
;
264 struct resource
*res
;
266 struct clk
*pix_clk_in
;
267 char pix_clk_name
[32];
268 const char *pix_clk_in_name
;
269 const struct of_device_id
*id
;
271 u8 div_ratio_shift
= 0;
273 fsl_dev
= devm_kzalloc(dev
, sizeof(*fsl_dev
), GFP_KERNEL
);
277 id
= of_match_node(fsl_dcu_of_match
, pdev
->dev
.of_node
);
280 fsl_dev
->soc
= id
->data
;
282 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
283 base
= devm_ioremap_resource(dev
, res
);
289 fsl_dev
->irq
= platform_get_irq(pdev
, 0);
290 if (fsl_dev
->irq
< 0)
293 fsl_dev
->regmap
= devm_regmap_init_mmio(dev
, base
,
294 &fsl_dcu_regmap_config
);
295 if (IS_ERR(fsl_dev
->regmap
)) {
296 dev_err(dev
, "regmap init failed\n");
297 return PTR_ERR(fsl_dev
->regmap
);
300 fsl_dev
->clk
= devm_clk_get(dev
, "dcu");
301 if (IS_ERR(fsl_dev
->clk
)) {
302 dev_err(dev
, "failed to get dcu clock\n");
303 return PTR_ERR(fsl_dev
->clk
);
305 ret
= clk_prepare_enable(fsl_dev
->clk
);
307 dev_err(dev
, "failed to enable dcu clk\n");
311 pix_clk_in
= devm_clk_get(dev
, "pix");
312 if (IS_ERR(pix_clk_in
)) {
313 /* legancy binding, use dcu clock as pixel clock input */
314 pix_clk_in
= fsl_dev
->clk
;
317 if (of_property_read_bool(dev
->of_node
, "big-endian"))
318 div_ratio_shift
= 24;
320 pix_clk_in_name
= __clk_get_name(pix_clk_in
);
321 snprintf(pix_clk_name
, sizeof(pix_clk_name
), "%s_pix", pix_clk_in_name
);
322 fsl_dev
->pix_clk
= clk_register_divider(dev
, pix_clk_name
,
323 pix_clk_in_name
, 0, base
+ DCU_DIV_RATIO
,
324 div_ratio_shift
, 8, CLK_DIVIDER_ROUND_CLOSEST
, NULL
);
325 if (IS_ERR(fsl_dev
->pix_clk
)) {
326 dev_err(dev
, "failed to register pix clk\n");
327 ret
= PTR_ERR(fsl_dev
->pix_clk
);
331 fsl_dev
->tcon
= fsl_tcon_init(dev
);
333 drm
= drm_dev_alloc(&fsl_dcu_drm_driver
, dev
);
336 goto unregister_pix_clk
;
341 fsl_dev
->np
= dev
->of_node
;
342 drm
->dev_private
= fsl_dev
;
343 dev_set_drvdata(dev
, fsl_dev
);
345 ret
= drm_dev_register(drm
, 0);
349 drm_client_setup_with_color_mode(drm
, legacyfb_depth
);
356 clk_unregister(fsl_dev
->pix_clk
);
358 clk_disable_unprepare(fsl_dev
->clk
);
362 static void fsl_dcu_drm_remove(struct platform_device
*pdev
)
364 struct fsl_dcu_drm_device
*fsl_dev
= platform_get_drvdata(pdev
);
366 drm_dev_unregister(fsl_dev
->drm
);
367 drm_dev_put(fsl_dev
->drm
);
368 clk_disable_unprepare(fsl_dev
->clk
);
369 clk_unregister(fsl_dev
->pix_clk
);
372 static void fsl_dcu_drm_shutdown(struct platform_device
*pdev
)
374 struct fsl_dcu_drm_device
*fsl_dev
= platform_get_drvdata(pdev
);
376 drm_atomic_helper_shutdown(fsl_dev
->drm
);
379 static struct platform_driver fsl_dcu_drm_platform_driver
= {
380 .probe
= fsl_dcu_drm_probe
,
381 .remove
= fsl_dcu_drm_remove
,
382 .shutdown
= fsl_dcu_drm_shutdown
,
385 .pm
= &fsl_dcu_drm_pm_ops
,
386 .of_match_table
= fsl_dcu_of_match
,
390 drm_module_platform_driver(fsl_dcu_drm_platform_driver
);
392 MODULE_DESCRIPTION("Freescale DCU DRM Driver");
393 MODULE_LICENSE("GPL");