1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2022 Marek Vasut <marex@denx.de>
4 *
5 * This code is based on drivers/gpu/drm/mxsfb/mxsfb*
6 */
7
8 #include <linux/clk.h>
9 #include <linux/dma-mapping.h>
10 #include <linux/io.h>
11 #include <linux/module.h>
12 #include <linux/of.h>
13 #include <linux/of_graph.h>
14 #include <linux/platform_device.h>
15 #include <linux/pm_runtime.h>
16
17 #include <drm/clients/drm_client_setup.h>
18 #include <drm/drm_atomic_helper.h>
19 #include <drm/drm_bridge.h>
20 #include <drm/drm_drv.h>
21 #include <drm/drm_encoder.h>
22 #include <drm/drm_fbdev_dma.h>
23 #include <drm/drm_gem_dma_helper.h>
24 #include <drm/drm_gem_framebuffer_helper.h>
25 #include <drm/drm_mode_config.h>
26 #include <drm/drm_module.h>
27 #include <drm/drm_of.h>
28 #include <drm/drm_probe_helper.h>
29 #include <drm/drm_vblank.h>
30
31 #include "lcdif_drv.h"
32 #include "lcdif_regs.h"
33
34 static const struct drm_mode_config_funcs lcdif_mode_config_funcs = {
35 .fb_create = drm_gem_fb_create,
36 .atomic_check = drm_atomic_helper_check,
37 .atomic_commit = drm_atomic_helper_commit,
38 };
39
40 static const struct drm_mode_config_helper_funcs lcdif_mode_config_helpers = {
41 .atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
42 };
43
44 static const struct drm_encoder_funcs lcdif_encoder_funcs = {
45 .destroy = drm_encoder_cleanup,
46 };
47
lcdif_attach_bridge(struct lcdif_drm_private * lcdif)48 static int lcdif_attach_bridge(struct lcdif_drm_private *lcdif)
49 {
50 struct device *dev = lcdif->drm->dev;
51 struct device_node *ep;
52 struct drm_bridge *bridge;
53 int ret;
54
55 for_each_endpoint_of_node(dev->of_node, ep) {
56 struct device_node *remote;
57 struct of_endpoint of_ep;
58 struct drm_encoder *encoder;
59
60 remote = of_graph_get_remote_port_parent(ep);
61 if (!of_device_is_available(remote)) {
62 of_node_put(remote);
63 continue;
64 }
65 of_node_put(remote);
66
67 ret = of_graph_parse_endpoint(ep, &of_ep);
68 if (ret < 0) {
69 dev_err(dev, "Failed to parse endpoint %pOF\n", ep);
70 of_node_put(ep);
71 return ret;
72 }
73
74 bridge = devm_drm_of_get_bridge(dev, dev->of_node, 0, of_ep.id);
75 if (IS_ERR(bridge)) {
76 of_node_put(ep);
77 return dev_err_probe(dev, PTR_ERR(bridge),
78 "Failed to get bridge for endpoint%u\n",
79 of_ep.id);
80 }
81
82 encoder = devm_kzalloc(dev, sizeof(*encoder), GFP_KERNEL);
83 if (!encoder) {
84 dev_err(dev, "Failed to allocate encoder for endpoint%u\n",
85 of_ep.id);
86 of_node_put(ep);
87 return -ENOMEM;
88 }
89
90 encoder->possible_crtcs = drm_crtc_mask(&lcdif->crtc);
91 ret = drm_encoder_init(lcdif->drm, encoder, &lcdif_encoder_funcs,
92 DRM_MODE_ENCODER_NONE, NULL);
93 if (ret) {
94 dev_err(dev, "Failed to initialize encoder for endpoint%u: %d\n",
95 of_ep.id, ret);
96 of_node_put(ep);
97 return ret;
98 }
99
100 ret = drm_bridge_attach(encoder, bridge, NULL, 0);
101 if (ret) {
102 of_node_put(ep);
103 return dev_err_probe(dev, ret,
104 "Failed to attach bridge for endpoint%u\n",
105 of_ep.id);
106 }
107 }
108
109 return 0;
110 }
111
lcdif_irq_handler(int irq,void * data)112 static irqreturn_t lcdif_irq_handler(int irq, void *data)
113 {
114 struct drm_device *drm = data;
115 struct lcdif_drm_private *lcdif = drm->dev_private;
116 u32 reg, stat;
117
118 stat = readl(lcdif->base + LCDC_V8_INT_STATUS_D0);
119 if (!stat)
120 return IRQ_NONE;
121
122 if (stat & INT_STATUS_D0_VS_BLANK) {
123 reg = readl(lcdif->base + LCDC_V8_CTRLDESCL0_5);
124 if (!(reg & CTRLDESCL0_5_SHADOW_LOAD_EN))
125 drm_crtc_handle_vblank(&lcdif->crtc);
126 }
127
128 writel(stat, lcdif->base + LCDC_V8_INT_STATUS_D0);
129
130 return IRQ_HANDLED;
131 }
132
lcdif_load(struct drm_device * drm)133 static int lcdif_load(struct drm_device *drm)
134 {
135 struct platform_device *pdev = to_platform_device(drm->dev);
136 struct lcdif_drm_private *lcdif;
137 struct resource *res;
138 int ret;
139
140 lcdif = devm_kzalloc(&pdev->dev, sizeof(*lcdif), GFP_KERNEL);
141 if (!lcdif)
142 return -ENOMEM;
143
144 lcdif->drm = drm;
145 drm->dev_private = lcdif;
146
147 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
148 lcdif->base = devm_ioremap_resource(drm->dev, res);
149 if (IS_ERR(lcdif->base))
150 return PTR_ERR(lcdif->base);
151
152 lcdif->clk = devm_clk_get(drm->dev, "pix");
153 if (IS_ERR(lcdif->clk))
154 return PTR_ERR(lcdif->clk);
155
156 lcdif->clk_axi = devm_clk_get(drm->dev, "axi");
157 if (IS_ERR(lcdif->clk_axi))
158 return PTR_ERR(lcdif->clk_axi);
159
160 lcdif->clk_disp_axi = devm_clk_get(drm->dev, "disp_axi");
161 if (IS_ERR(lcdif->clk_disp_axi))
162 return PTR_ERR(lcdif->clk_disp_axi);
163
164 platform_set_drvdata(pdev, drm);
165
166 ret = dma_set_mask_and_coherent(drm->dev, DMA_BIT_MASK(36));
167 if (ret)
168 return ret;
169
170 /* Modeset init */
171 ret = drmm_mode_config_init(drm);
172 if (ret) {
173 dev_err(drm->dev, "Failed to initialize mode config\n");
174 return ret;
175 }
176
177 ret = lcdif_kms_init(lcdif);
178 if (ret < 0) {
179 dev_err(drm->dev, "Failed to initialize KMS pipeline\n");
180 return ret;
181 }
182
183 ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
184 if (ret < 0) {
185 dev_err(drm->dev, "Failed to initialise vblank\n");
186 return ret;
187 }
188
189 /* Start with vertical blanking interrupt reporting disabled. */
190 drm_crtc_vblank_off(&lcdif->crtc);
191
192 ret = lcdif_attach_bridge(lcdif);
193 if (ret)
194 return dev_err_probe(drm->dev, ret, "Cannot connect bridge\n");
195
196 drm->mode_config.min_width = LCDIF_MIN_XRES;
197 drm->mode_config.min_height = LCDIF_MIN_YRES;
198 drm->mode_config.max_width = LCDIF_MAX_XRES;
199 drm->mode_config.max_height = LCDIF_MAX_YRES;
200 drm->mode_config.funcs = &lcdif_mode_config_funcs;
201 drm->mode_config.helper_private = &lcdif_mode_config_helpers;
202
203 drm_mode_config_reset(drm);
204
205 ret = platform_get_irq(pdev, 0);
206 if (ret < 0)
207 return ret;
208 lcdif->irq = ret;
209
210 ret = devm_request_irq(drm->dev, lcdif->irq, lcdif_irq_handler, 0,
211 drm->driver->name, drm);
212 if (ret < 0) {
213 dev_err(drm->dev, "Failed to install IRQ handler\n");
214 return ret;
215 }
216
217 drm_kms_helper_poll_init(drm);
218
219 drm_helper_hpd_irq_event(drm);
220
221 pm_runtime_enable(drm->dev);
222
223 return 0;
224 }
225
lcdif_unload(struct drm_device * drm)226 static void lcdif_unload(struct drm_device *drm)
227 {
228 struct lcdif_drm_private *lcdif = drm->dev_private;
229
230 pm_runtime_get_sync(drm->dev);
231
232 drm_crtc_vblank_off(&lcdif->crtc);
233
234 drm_kms_helper_poll_fini(drm);
235
236 pm_runtime_put_sync(drm->dev);
237 pm_runtime_disable(drm->dev);
238
239 drm->dev_private = NULL;
240 }
241
242 DEFINE_DRM_GEM_DMA_FOPS(fops);
243
244 static const struct drm_driver lcdif_driver = {
245 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
246 DRM_GEM_DMA_DRIVER_OPS,
247 DRM_FBDEV_DMA_DRIVER_OPS,
248 .fops = &fops,
249 .name = "imx-lcdif",
250 .desc = "i.MX LCDIF Controller DRM",
251 .major = 1,
252 .minor = 0,
253 };
254
255 static const struct of_device_id lcdif_dt_ids[] = {
256 { .compatible = "fsl,imx8mp-lcdif" },
257 { .compatible = "fsl,imx93-lcdif" },
258 { /* sentinel */ }
259 };
260 MODULE_DEVICE_TABLE(of, lcdif_dt_ids);
261
lcdif_probe(struct platform_device * pdev)262 static int lcdif_probe(struct platform_device *pdev)
263 {
264 struct drm_device *drm;
265 int ret;
266
267 drm = drm_dev_alloc(&lcdif_driver, &pdev->dev);
268 if (IS_ERR(drm))
269 return PTR_ERR(drm);
270
271 ret = lcdif_load(drm);
272 if (ret)
273 goto err_free;
274
275 ret = drm_dev_register(drm, 0);
276 if (ret)
277 goto err_unload;
278
279 drm_client_setup(drm, NULL);
280
281 return 0;
282
283 err_unload:
284 lcdif_unload(drm);
285 err_free:
286 drm_dev_put(drm);
287
288 return ret;
289 }
290
lcdif_remove(struct platform_device * pdev)291 static void lcdif_remove(struct platform_device *pdev)
292 {
293 struct drm_device *drm = platform_get_drvdata(pdev);
294
295 drm_dev_unregister(drm);
296 drm_atomic_helper_shutdown(drm);
297 lcdif_unload(drm);
298 drm_dev_put(drm);
299 }
300
lcdif_shutdown(struct platform_device * pdev)301 static void lcdif_shutdown(struct platform_device *pdev)
302 {
303 struct drm_device *drm = platform_get_drvdata(pdev);
304
305 drm_atomic_helper_shutdown(drm);
306 }
307
lcdif_rpm_suspend(struct device * dev)308 static int __maybe_unused lcdif_rpm_suspend(struct device *dev)
309 {
310 struct drm_device *drm = dev_get_drvdata(dev);
311 struct lcdif_drm_private *lcdif = drm->dev_private;
312
313 /* These clock supply the DISPLAY CLOCK Domain */
314 clk_disable_unprepare(lcdif->clk);
315 /* These clock supply the System Bus, AXI, Write Path, LFIFO */
316 clk_disable_unprepare(lcdif->clk_disp_axi);
317 /* These clock supply the Control Bus, APB, APBH Ctrl Registers */
318 clk_disable_unprepare(lcdif->clk_axi);
319
320 return 0;
321 }
322
lcdif_rpm_resume(struct device * dev)323 static int __maybe_unused lcdif_rpm_resume(struct device *dev)
324 {
325 struct drm_device *drm = dev_get_drvdata(dev);
326 struct lcdif_drm_private *lcdif = drm->dev_private;
327
328 /* These clock supply the Control Bus, APB, APBH Ctrl Registers */
329 clk_prepare_enable(lcdif->clk_axi);
330 /* These clock supply the System Bus, AXI, Write Path, LFIFO */
331 clk_prepare_enable(lcdif->clk_disp_axi);
332 /* These clock supply the DISPLAY CLOCK Domain */
333 clk_prepare_enable(lcdif->clk);
334
335 return 0;
336 }
337
lcdif_suspend(struct device * dev)338 static int __maybe_unused lcdif_suspend(struct device *dev)
339 {
340 struct drm_device *drm = dev_get_drvdata(dev);
341 int ret;
342
343 ret = drm_mode_config_helper_suspend(drm);
344 if (ret)
345 return ret;
346
347 if (pm_runtime_suspended(dev))
348 return 0;
349
350 return lcdif_rpm_suspend(dev);
351 }
352
lcdif_resume(struct device * dev)353 static int __maybe_unused lcdif_resume(struct device *dev)
354 {
355 struct drm_device *drm = dev_get_drvdata(dev);
356
357 if (!pm_runtime_suspended(dev))
358 lcdif_rpm_resume(dev);
359
360 return drm_mode_config_helper_resume(drm);
361 }
362
363 static const struct dev_pm_ops lcdif_pm_ops = {
364 SET_SYSTEM_SLEEP_PM_OPS(lcdif_suspend, lcdif_resume)
365 SET_RUNTIME_PM_OPS(lcdif_rpm_suspend, lcdif_rpm_resume, NULL)
366 };
367
368 static struct platform_driver lcdif_platform_driver = {
369 .probe = lcdif_probe,
370 .remove = lcdif_remove,
371 .shutdown = lcdif_shutdown,
372 .driver = {
373 .name = "imx-lcdif",
374 .of_match_table = lcdif_dt_ids,
375 .pm = &lcdif_pm_ops,
376 },
377 };
378
379 drm_module_platform_driver(lcdif_platform_driver);
380
381 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
382 MODULE_DESCRIPTION("Freescale LCDIF DRM/KMS driver");
383 MODULE_LICENSE("GPL");
384