1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright 2015 Freescale Semiconductor, Inc.
4 *
5 * Freescale DCU drm device driver
6 */
7
8 #include <linux/clk.h>
9 #include <linux/clk-provider.h>
10 #include <linux/console.h>
11 #include <linux/io.h>
12 #include <linux/mfd/syscon.h>
13 #include <linux/mm.h>
14 #include <linux/module.h>
15 #include <linux/of_platform.h>
16 #include <linux/platform_device.h>
17 #include <linux/pm.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/regmap.h>
20
21 #include <drm/clients/drm_client_setup.h>
22 #include <drm/drm_atomic_helper.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>
30
31 #include "fsl_dcu_drm_crtc.h"
32 #include "fsl_dcu_drm_drv.h"
33 #include "fsl_tcon.h"
34
35 static int legacyfb_depth = 24;
36 module_param(legacyfb_depth, int, 0444);
37
fsl_dcu_drm_is_volatile_reg(struct device * dev,unsigned int reg)38 static bool fsl_dcu_drm_is_volatile_reg(struct device *dev, unsigned int reg)
39 {
40 if (reg == DCU_INT_STATUS || reg == DCU_UPDATE_MODE)
41 return true;
42
43 return false;
44 }
45
46 static const struct regmap_config fsl_dcu_regmap_config = {
47 .reg_bits = 32,
48 .reg_stride = 4,
49 .val_bits = 32,
50
51 .volatile_reg = fsl_dcu_drm_is_volatile_reg,
52 };
53
fsl_dcu_irq_reset(struct drm_device * dev)54 static void fsl_dcu_irq_reset(struct drm_device *dev)
55 {
56 struct fsl_dcu_drm_device *fsl_dev = dev->dev_private;
57
58 regmap_write(fsl_dev->regmap, DCU_INT_STATUS, ~0);
59 regmap_write(fsl_dev->regmap, DCU_INT_MASK, ~0);
60 }
61
fsl_dcu_drm_irq(int irq,void * arg)62 static irqreturn_t fsl_dcu_drm_irq(int irq, void *arg)
63 {
64 struct drm_device *dev = arg;
65 struct fsl_dcu_drm_device *fsl_dev = dev->dev_private;
66 unsigned int int_status;
67 int ret;
68
69 ret = regmap_read(fsl_dev->regmap, DCU_INT_STATUS, &int_status);
70 if (ret) {
71 dev_err(dev->dev, "read DCU_INT_STATUS failed\n");
72 return IRQ_NONE;
73 }
74
75 if (int_status & DCU_INT_STATUS_VBLANK)
76 drm_handle_vblank(dev, 0);
77
78 regmap_write(fsl_dev->regmap, DCU_INT_STATUS, int_status);
79
80 return IRQ_HANDLED;
81 }
82
fsl_dcu_irq_install(struct drm_device * dev,unsigned int irq)83 static int fsl_dcu_irq_install(struct drm_device *dev, unsigned int irq)
84 {
85 if (irq == IRQ_NOTCONNECTED)
86 return -ENOTCONN;
87
88 fsl_dcu_irq_reset(dev);
89
90 return request_irq(irq, fsl_dcu_drm_irq, 0, dev->driver->name, dev);
91 }
92
fsl_dcu_irq_uninstall(struct drm_device * dev)93 static void fsl_dcu_irq_uninstall(struct drm_device *dev)
94 {
95 struct fsl_dcu_drm_device *fsl_dev = dev->dev_private;
96
97 fsl_dcu_irq_reset(dev);
98 free_irq(fsl_dev->irq, dev);
99 }
100
fsl_dcu_load(struct drm_device * dev,unsigned long flags)101 static int fsl_dcu_load(struct drm_device *dev, unsigned long flags)
102 {
103 struct fsl_dcu_drm_device *fsl_dev = dev->dev_private;
104 struct regmap *scfg;
105 int ret;
106
107 ret = fsl_dcu_drm_modeset_init(fsl_dev);
108 if (ret < 0)
109 return dev_err_probe(dev->dev, ret, "failed to initialize mode setting\n");
110
111 scfg = syscon_regmap_lookup_by_compatible("fsl,ls1021a-scfg");
112 if (PTR_ERR(scfg) != -ENODEV) {
113 /*
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.
118 */
119 ret = regmap_update_bits(scfg, SCFG_PIXCLKCR, SCFG_PIXCLKCR_PXCEN,
120 SCFG_PIXCLKCR_PXCEN);
121 if (ret < 0)
122 return dev_err_probe(dev->dev, ret, "failed to enable pixclk\n");
123 }
124
125 ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
126 if (ret < 0) {
127 dev_err(dev->dev, "failed to initialize vblank\n");
128 goto done_vblank;
129 }
130
131 ret = fsl_dcu_irq_install(dev, fsl_dev->irq);
132 if (ret < 0) {
133 dev_err(dev->dev, "failed to install IRQ handler\n");
134 goto done_irq;
135 }
136
137 if (legacyfb_depth != 16 && legacyfb_depth != 24 &&
138 legacyfb_depth != 32) {
139 dev_warn(dev->dev,
140 "Invalid legacyfb_depth. Defaulting to 24bpp\n");
141 legacyfb_depth = 24;
142 }
143
144 return 0;
145 done_irq:
146 drm_kms_helper_poll_fini(dev);
147
148 drm_mode_config_cleanup(dev);
149 done_vblank:
150 dev->dev_private = NULL;
151
152 return ret;
153 }
154
fsl_dcu_unload(struct drm_device * dev)155 static void fsl_dcu_unload(struct drm_device *dev)
156 {
157 drm_atomic_helper_shutdown(dev);
158 drm_kms_helper_poll_fini(dev);
159
160 drm_mode_config_cleanup(dev);
161 fsl_dcu_irq_uninstall(dev);
162
163 dev->dev_private = NULL;
164 }
165
166 DEFINE_DRM_GEM_DMA_FOPS(fsl_dcu_drm_fops);
167
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",
177 .major = 1,
178 .minor = 1,
179 };
180
181 #ifdef CONFIG_PM_SLEEP
fsl_dcu_drm_pm_suspend(struct device * dev)182 static int fsl_dcu_drm_pm_suspend(struct device *dev)
183 {
184 struct fsl_dcu_drm_device *fsl_dev = dev_get_drvdata(dev);
185 int ret;
186
187 if (!fsl_dev)
188 return 0;
189
190 disable_irq(fsl_dev->irq);
191
192 ret = drm_mode_config_helper_suspend(fsl_dev->drm);
193 if (ret) {
194 enable_irq(fsl_dev->irq);
195 return ret;
196 }
197
198 clk_disable_unprepare(fsl_dev->clk);
199
200 return 0;
201 }
202
fsl_dcu_drm_pm_resume(struct device * dev)203 static int fsl_dcu_drm_pm_resume(struct device *dev)
204 {
205 struct fsl_dcu_drm_device *fsl_dev = dev_get_drvdata(dev);
206 int ret;
207
208 if (!fsl_dev)
209 return 0;
210
211 ret = clk_prepare_enable(fsl_dev->clk);
212 if (ret < 0) {
213 dev_err(dev, "failed to enable dcu clk\n");
214 return ret;
215 }
216
217 if (fsl_dev->tcon)
218 fsl_tcon_bypass_enable(fsl_dev->tcon);
219 fsl_dcu_drm_init_planes(fsl_dev->drm);
220 enable_irq(fsl_dev->irq);
221
222 drm_mode_config_helper_resume(fsl_dev->drm);
223
224 return 0;
225 }
226 #endif
227
228 static const struct dev_pm_ops fsl_dcu_drm_pm_ops = {
229 SET_SYSTEM_SLEEP_PM_OPS(fsl_dcu_drm_pm_suspend, fsl_dcu_drm_pm_resume)
230 };
231
232 static const struct fsl_dcu_soc_data fsl_dcu_ls1021a_data = {
233 .name = "ls1021a",
234 .total_layer = 16,
235 .max_layer = 4,
236 .layer_regs = LS1021A_LAYER_REG_NUM,
237 };
238
239 static const struct fsl_dcu_soc_data fsl_dcu_vf610_data = {
240 .name = "vf610",
241 .total_layer = 64,
242 .max_layer = 6,
243 .layer_regs = VF610_LAYER_REG_NUM,
244 };
245
246 static const struct of_device_id fsl_dcu_of_match[] = {
247 {
248 .compatible = "fsl,ls1021a-dcu",
249 .data = &fsl_dcu_ls1021a_data,
250 }, {
251 .compatible = "fsl,vf610-dcu",
252 .data = &fsl_dcu_vf610_data,
253 }, {
254 },
255 };
256 MODULE_DEVICE_TABLE(of, fsl_dcu_of_match);
257
fsl_dcu_drm_probe(struct platform_device * pdev)258 static int fsl_dcu_drm_probe(struct platform_device *pdev)
259 {
260 struct fsl_dcu_drm_device *fsl_dev;
261 struct drm_device *drm;
262 struct device *dev = &pdev->dev;
263 struct resource *res;
264 void __iomem *base;
265 struct clk *pix_clk_in;
266 char pix_clk_name[32];
267 const char *pix_clk_in_name;
268 const struct of_device_id *id;
269 int ret;
270 u8 div_ratio_shift = 0;
271
272 fsl_dev = devm_kzalloc(dev, sizeof(*fsl_dev), GFP_KERNEL);
273 if (!fsl_dev)
274 return -ENOMEM;
275
276 id = of_match_node(fsl_dcu_of_match, pdev->dev.of_node);
277 if (!id)
278 return -ENODEV;
279 fsl_dev->soc = id->data;
280
281 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
282 base = devm_ioremap_resource(dev, res);
283 if (IS_ERR(base)) {
284 ret = PTR_ERR(base);
285 return ret;
286 }
287
288 fsl_dev->irq = platform_get_irq(pdev, 0);
289 if (fsl_dev->irq < 0)
290 return fsl_dev->irq;
291
292 fsl_dev->regmap = devm_regmap_init_mmio(dev, base,
293 &fsl_dcu_regmap_config);
294 if (IS_ERR(fsl_dev->regmap)) {
295 dev_err(dev, "regmap init failed\n");
296 return PTR_ERR(fsl_dev->regmap);
297 }
298
299 fsl_dev->clk = devm_clk_get(dev, "dcu");
300 if (IS_ERR(fsl_dev->clk)) {
301 dev_err(dev, "failed to get dcu clock\n");
302 return PTR_ERR(fsl_dev->clk);
303 }
304 ret = clk_prepare_enable(fsl_dev->clk);
305 if (ret < 0) {
306 dev_err(dev, "failed to enable dcu clk\n");
307 return ret;
308 }
309
310 pix_clk_in = devm_clk_get(dev, "pix");
311 if (IS_ERR(pix_clk_in)) {
312 /* legancy binding, use dcu clock as pixel clock input */
313 pix_clk_in = fsl_dev->clk;
314 }
315
316 if (of_property_read_bool(dev->of_node, "big-endian"))
317 div_ratio_shift = 24;
318
319 pix_clk_in_name = __clk_get_name(pix_clk_in);
320 snprintf(pix_clk_name, sizeof(pix_clk_name), "%s_pix", pix_clk_in_name);
321 fsl_dev->pix_clk = clk_register_divider(dev, pix_clk_name,
322 pix_clk_in_name, 0, base + DCU_DIV_RATIO,
323 div_ratio_shift, 8, CLK_DIVIDER_ROUND_CLOSEST, NULL);
324 if (IS_ERR(fsl_dev->pix_clk)) {
325 dev_err(dev, "failed to register pix clk\n");
326 ret = PTR_ERR(fsl_dev->pix_clk);
327 goto disable_clk;
328 }
329
330 fsl_dev->tcon = fsl_tcon_init(dev);
331
332 drm = drm_dev_alloc(&fsl_dcu_drm_driver, dev);
333 if (IS_ERR(drm)) {
334 ret = PTR_ERR(drm);
335 goto unregister_pix_clk;
336 }
337
338 fsl_dev->dev = dev;
339 fsl_dev->drm = drm;
340 fsl_dev->np = dev->of_node;
341 drm->dev_private = fsl_dev;
342 dev_set_drvdata(dev, fsl_dev);
343
344 ret = drm_dev_register(drm, 0);
345 if (ret < 0)
346 goto put;
347
348 drm_client_setup_with_color_mode(drm, legacyfb_depth);
349
350 return 0;
351
352 put:
353 drm_dev_put(drm);
354 unregister_pix_clk:
355 clk_unregister(fsl_dev->pix_clk);
356 disable_clk:
357 clk_disable_unprepare(fsl_dev->clk);
358 return ret;
359 }
360
fsl_dcu_drm_remove(struct platform_device * pdev)361 static void fsl_dcu_drm_remove(struct platform_device *pdev)
362 {
363 struct fsl_dcu_drm_device *fsl_dev = platform_get_drvdata(pdev);
364
365 drm_dev_unregister(fsl_dev->drm);
366 drm_dev_put(fsl_dev->drm);
367 clk_disable_unprepare(fsl_dev->clk);
368 clk_unregister(fsl_dev->pix_clk);
369 }
370
fsl_dcu_drm_shutdown(struct platform_device * pdev)371 static void fsl_dcu_drm_shutdown(struct platform_device *pdev)
372 {
373 struct fsl_dcu_drm_device *fsl_dev = platform_get_drvdata(pdev);
374
375 drm_atomic_helper_shutdown(fsl_dev->drm);
376 }
377
378 static struct platform_driver fsl_dcu_drm_platform_driver = {
379 .probe = fsl_dcu_drm_probe,
380 .remove = fsl_dcu_drm_remove,
381 .shutdown = fsl_dcu_drm_shutdown,
382 .driver = {
383 .name = "fsl-dcu",
384 .pm = &fsl_dcu_drm_pm_ops,
385 .of_match_table = fsl_dcu_of_match,
386 },
387 };
388
389 drm_module_platform_driver(fsl_dcu_drm_platform_driver);
390
391 MODULE_DESCRIPTION("Freescale DCU DRM Driver");
392 MODULE_LICENSE("GPL");
393