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