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