xref: /openbmc/linux/drivers/gpu/drm/mxsfb/lcdif_drv.c (revision 97e6f135)
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_device.h>
14 #include <linux/of_graph.h>
15 #include <linux/platform_device.h>
16 #include <linux/pm_runtime.h>
17 
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 
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 
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 
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 	drm_mode_config_init(drm);
172 
173 	ret = lcdif_kms_init(lcdif);
174 	if (ret < 0) {
175 		dev_err(drm->dev, "Failed to initialize KMS pipeline\n");
176 		return ret;
177 	}
178 
179 	ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
180 	if (ret < 0) {
181 		dev_err(drm->dev, "Failed to initialise vblank\n");
182 		return ret;
183 	}
184 
185 	/* Start with vertical blanking interrupt reporting disabled. */
186 	drm_crtc_vblank_off(&lcdif->crtc);
187 
188 	ret = lcdif_attach_bridge(lcdif);
189 	if (ret)
190 		return dev_err_probe(drm->dev, ret, "Cannot connect bridge\n");
191 
192 	drm->mode_config.min_width	= LCDIF_MIN_XRES;
193 	drm->mode_config.min_height	= LCDIF_MIN_YRES;
194 	drm->mode_config.max_width	= LCDIF_MAX_XRES;
195 	drm->mode_config.max_height	= LCDIF_MAX_YRES;
196 	drm->mode_config.funcs		= &lcdif_mode_config_funcs;
197 	drm->mode_config.helper_private	= &lcdif_mode_config_helpers;
198 
199 	drm_mode_config_reset(drm);
200 
201 	ret = platform_get_irq(pdev, 0);
202 	if (ret < 0)
203 		return ret;
204 	lcdif->irq = ret;
205 
206 	ret = devm_request_irq(drm->dev, lcdif->irq, lcdif_irq_handler, 0,
207 			       drm->driver->name, drm);
208 	if (ret < 0) {
209 		dev_err(drm->dev, "Failed to install IRQ handler\n");
210 		return ret;
211 	}
212 
213 	drm_kms_helper_poll_init(drm);
214 
215 	drm_helper_hpd_irq_event(drm);
216 
217 	pm_runtime_enable(drm->dev);
218 
219 	return 0;
220 }
221 
222 static void lcdif_unload(struct drm_device *drm)
223 {
224 	struct lcdif_drm_private *lcdif = drm->dev_private;
225 
226 	pm_runtime_get_sync(drm->dev);
227 
228 	drm_crtc_vblank_off(&lcdif->crtc);
229 
230 	drm_kms_helper_poll_fini(drm);
231 	drm_mode_config_cleanup(drm);
232 
233 	pm_runtime_put_sync(drm->dev);
234 	pm_runtime_disable(drm->dev);
235 
236 	drm->dev_private = NULL;
237 }
238 
239 DEFINE_DRM_GEM_DMA_FOPS(fops);
240 
241 static const struct drm_driver lcdif_driver = {
242 	.driver_features	= DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
243 	DRM_GEM_DMA_DRIVER_OPS,
244 	.fops	= &fops,
245 	.name	= "imx-lcdif",
246 	.desc	= "i.MX LCDIF Controller DRM",
247 	.date	= "20220417",
248 	.major	= 1,
249 	.minor	= 0,
250 };
251 
252 static const struct of_device_id lcdif_dt_ids[] = {
253 	{ .compatible = "fsl,imx8mp-lcdif" },
254 	{ .compatible = "fsl,imx93-lcdif" },
255 	{ /* sentinel */ }
256 };
257 MODULE_DEVICE_TABLE(of, lcdif_dt_ids);
258 
259 static int lcdif_probe(struct platform_device *pdev)
260 {
261 	struct drm_device *drm;
262 	int ret;
263 
264 	drm = drm_dev_alloc(&lcdif_driver, &pdev->dev);
265 	if (IS_ERR(drm))
266 		return PTR_ERR(drm);
267 
268 	ret = lcdif_load(drm);
269 	if (ret)
270 		goto err_free;
271 
272 	ret = drm_dev_register(drm, 0);
273 	if (ret)
274 		goto err_unload;
275 
276 	drm_fbdev_dma_setup(drm, 32);
277 
278 	return 0;
279 
280 err_unload:
281 	lcdif_unload(drm);
282 err_free:
283 	drm_dev_put(drm);
284 
285 	return ret;
286 }
287 
288 static int lcdif_remove(struct platform_device *pdev)
289 {
290 	struct drm_device *drm = platform_get_drvdata(pdev);
291 
292 	drm_dev_unregister(drm);
293 	drm_atomic_helper_shutdown(drm);
294 	lcdif_unload(drm);
295 	drm_dev_put(drm);
296 
297 	return 0;
298 }
299 
300 static void lcdif_shutdown(struct platform_device *pdev)
301 {
302 	struct drm_device *drm = platform_get_drvdata(pdev);
303 
304 	drm_atomic_helper_shutdown(drm);
305 }
306 
307 static int __maybe_unused lcdif_rpm_suspend(struct device *dev)
308 {
309 	struct drm_device *drm = dev_get_drvdata(dev);
310 	struct lcdif_drm_private *lcdif = drm->dev_private;
311 
312 	/* These clock supply the DISPLAY CLOCK Domain */
313 	clk_disable_unprepare(lcdif->clk);
314 	/* These clock supply the System Bus, AXI, Write Path, LFIFO */
315 	clk_disable_unprepare(lcdif->clk_disp_axi);
316 	/* These clock supply the Control Bus, APB, APBH Ctrl Registers */
317 	clk_disable_unprepare(lcdif->clk_axi);
318 
319 	return 0;
320 }
321 
322 static int __maybe_unused lcdif_rpm_resume(struct device *dev)
323 {
324 	struct drm_device *drm = dev_get_drvdata(dev);
325 	struct lcdif_drm_private *lcdif = drm->dev_private;
326 
327 	/* These clock supply the Control Bus, APB, APBH Ctrl Registers */
328 	clk_prepare_enable(lcdif->clk_axi);
329 	/* These clock supply the System Bus, AXI, Write Path, LFIFO */
330 	clk_prepare_enable(lcdif->clk_disp_axi);
331 	/* These clock supply the DISPLAY CLOCK Domain */
332 	clk_prepare_enable(lcdif->clk);
333 
334 	return 0;
335 }
336 
337 static int __maybe_unused lcdif_suspend(struct device *dev)
338 {
339 	struct drm_device *drm = dev_get_drvdata(dev);
340 	int ret;
341 
342 	ret = drm_mode_config_helper_suspend(drm);
343 	if (ret)
344 		return ret;
345 
346 	return lcdif_rpm_suspend(dev);
347 }
348 
349 static int __maybe_unused lcdif_resume(struct device *dev)
350 {
351 	struct drm_device *drm = dev_get_drvdata(dev);
352 
353 	lcdif_rpm_resume(dev);
354 
355 	return drm_mode_config_helper_resume(drm);
356 }
357 
358 static const struct dev_pm_ops lcdif_pm_ops = {
359 	SET_SYSTEM_SLEEP_PM_OPS(lcdif_suspend, lcdif_resume)
360 	SET_RUNTIME_PM_OPS(lcdif_rpm_suspend, lcdif_rpm_resume, NULL)
361 };
362 
363 static struct platform_driver lcdif_platform_driver = {
364 	.probe		= lcdif_probe,
365 	.remove		= lcdif_remove,
366 	.shutdown	= lcdif_shutdown,
367 	.driver	= {
368 		.name		= "imx-lcdif",
369 		.of_match_table	= lcdif_dt_ids,
370 		.pm		= &lcdif_pm_ops,
371 	},
372 };
373 
374 drm_module_platform_driver(lcdif_platform_driver);
375 
376 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
377 MODULE_DESCRIPTION("Freescale LCDIF DRM/KMS driver");
378 MODULE_LICENSE("GPL");
379