xref: /linux/drivers/gpu/drm/mxsfb/mxsfb_drv.c (revision cf79f291f985662150363b4a93d16f88f12643bc)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2016 Marek Vasut <marex@denx.de>
4  *
5  * This code is based on drivers/video/fbdev/mxsfb.c :
6  * Copyright (C) 2010 Juergen Beisert, Pengutronix
7  * Copyright (C) 2008-2009 Freescale Semiconductor, Inc. All Rights Reserved.
8  * Copyright (C) 2008 Embedded Alley Solutions, Inc All Rights Reserved.
9  */
10 
11 #include <linux/clk.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/io.h>
14 #include <linux/mod_devicetable.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/property.h>
18 #include <linux/pm_runtime.h>
19 
20 #include <drm/drm_atomic_helper.h>
21 #include <drm/drm_bridge.h>
22 #include <drm/drm_connector.h>
23 #include <drm/drm_drv.h>
24 #include <drm/drm_fbdev_dma.h>
25 #include <drm/drm_fourcc.h>
26 #include <drm/drm_gem_dma_helper.h>
27 #include <drm/drm_gem_framebuffer_helper.h>
28 #include <drm/drm_mode_config.h>
29 #include <drm/drm_module.h>
30 #include <drm/drm_of.h>
31 #include <drm/drm_probe_helper.h>
32 #include <drm/drm_vblank.h>
33 
34 #include "mxsfb_drv.h"
35 #include "mxsfb_regs.h"
36 
37 enum mxsfb_devtype {
38 	MXSFB_V3,
39 	MXSFB_V4,
40 	/*
41 	 * Starting at i.MX6 the hardware version register is gone, use the
42 	 * i.MX family number as the version.
43 	 */
44 	MXSFB_V6,
45 };
46 
47 static const struct mxsfb_devdata mxsfb_devdata[] = {
48 	[MXSFB_V3] = {
49 		.transfer_count	= LCDC_V3_TRANSFER_COUNT,
50 		.cur_buf	= LCDC_V3_CUR_BUF,
51 		.next_buf	= LCDC_V3_NEXT_BUF,
52 		.hs_wdth_mask	= 0xff,
53 		.hs_wdth_shift	= 24,
54 		.has_overlay	= false,
55 		.has_ctrl2	= false,
56 		.has_crc32	= false,
57 	},
58 	[MXSFB_V4] = {
59 		.transfer_count	= LCDC_V4_TRANSFER_COUNT,
60 		.cur_buf	= LCDC_V4_CUR_BUF,
61 		.next_buf	= LCDC_V4_NEXT_BUF,
62 		.hs_wdth_mask	= 0x3fff,
63 		.hs_wdth_shift	= 18,
64 		.has_overlay	= false,
65 		.has_ctrl2	= true,
66 		.has_crc32	= true,
67 	},
68 	[MXSFB_V6] = {
69 		.transfer_count	= LCDC_V4_TRANSFER_COUNT,
70 		.cur_buf	= LCDC_V4_CUR_BUF,
71 		.next_buf	= LCDC_V4_NEXT_BUF,
72 		.hs_wdth_mask	= 0x3fff,
73 		.hs_wdth_shift	= 18,
74 		.has_overlay	= true,
75 		.has_ctrl2	= true,
76 		.has_crc32	= true,
77 	},
78 };
79 
80 void mxsfb_enable_axi_clk(struct mxsfb_drm_private *mxsfb)
81 {
82 	clk_prepare_enable(mxsfb->clk_axi);
83 }
84 
85 void mxsfb_disable_axi_clk(struct mxsfb_drm_private *mxsfb)
86 {
87 	clk_disable_unprepare(mxsfb->clk_axi);
88 }
89 
90 static struct drm_framebuffer *
91 mxsfb_fb_create(struct drm_device *dev, struct drm_file *file_priv,
92 		const struct drm_mode_fb_cmd2 *mode_cmd)
93 {
94 	const struct drm_format_info *info;
95 
96 	info = drm_get_format_info(dev, mode_cmd);
97 	if (!info)
98 		return ERR_PTR(-EINVAL);
99 
100 	if (mode_cmd->width * info->cpp[0] != mode_cmd->pitches[0]) {
101 		dev_dbg(dev->dev, "Invalid pitch: fb width must match pitch\n");
102 		return ERR_PTR(-EINVAL);
103 	}
104 
105 	return drm_gem_fb_create(dev, file_priv, mode_cmd);
106 }
107 
108 static const struct drm_mode_config_funcs mxsfb_mode_config_funcs = {
109 	.fb_create		= mxsfb_fb_create,
110 	.atomic_check		= drm_atomic_helper_check,
111 	.atomic_commit		= drm_atomic_helper_commit,
112 };
113 
114 static const struct drm_mode_config_helper_funcs mxsfb_mode_config_helpers = {
115 	.atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
116 };
117 
118 static int mxsfb_attach_bridge(struct mxsfb_drm_private *mxsfb)
119 {
120 	struct drm_device *drm = mxsfb->drm;
121 	struct drm_connector_list_iter iter;
122 	struct drm_panel *panel;
123 	struct drm_bridge *bridge;
124 	int ret;
125 
126 	ret = drm_of_find_panel_or_bridge(drm->dev->of_node, 0, 0, &panel,
127 					  &bridge);
128 	if (ret)
129 		return ret;
130 
131 	if (panel) {
132 		bridge = devm_drm_panel_bridge_add_typed(drm->dev, panel,
133 							 DRM_MODE_CONNECTOR_DPI);
134 		if (IS_ERR(bridge))
135 			return PTR_ERR(bridge);
136 	}
137 
138 	if (!bridge)
139 		return -ENODEV;
140 
141 	ret = drm_bridge_attach(&mxsfb->encoder, bridge, NULL, 0);
142 	if (ret)
143 		return dev_err_probe(drm->dev, ret, "Failed to attach bridge\n");
144 
145 	mxsfb->bridge = bridge;
146 
147 	/*
148 	 * Get hold of the connector. This is a bit of a hack, until the bridge
149 	 * API gives us bus flags and formats.
150 	 */
151 	drm_connector_list_iter_begin(drm, &iter);
152 	mxsfb->connector = drm_connector_list_iter_next(&iter);
153 	drm_connector_list_iter_end(&iter);
154 
155 	return 0;
156 }
157 
158 static irqreturn_t mxsfb_irq_handler(int irq, void *data)
159 {
160 	struct drm_device *drm = data;
161 	struct mxsfb_drm_private *mxsfb = drm->dev_private;
162 	u32 reg, crc;
163 	u64 vbc;
164 
165 	reg = readl(mxsfb->base + LCDC_CTRL1);
166 
167 	if (reg & CTRL1_CUR_FRAME_DONE_IRQ) {
168 		drm_crtc_handle_vblank(&mxsfb->crtc);
169 		if (mxsfb->crc_active) {
170 			crc = readl(mxsfb->base + LCDC_V4_CRC_STAT);
171 			vbc = drm_crtc_accurate_vblank_count(&mxsfb->crtc);
172 			drm_crtc_add_crc_entry(&mxsfb->crtc, true, vbc, &crc);
173 		}
174 	}
175 
176 	writel(CTRL1_CUR_FRAME_DONE_IRQ, mxsfb->base + LCDC_CTRL1 + REG_CLR);
177 
178 	return IRQ_HANDLED;
179 }
180 
181 static void mxsfb_irq_disable(struct drm_device *drm)
182 {
183 	struct mxsfb_drm_private *mxsfb = drm->dev_private;
184 
185 	mxsfb_enable_axi_clk(mxsfb);
186 
187 	/* Disable and clear VBLANK IRQ */
188 	writel(CTRL1_CUR_FRAME_DONE_IRQ_EN, mxsfb->base + LCDC_CTRL1 + REG_CLR);
189 	writel(CTRL1_CUR_FRAME_DONE_IRQ, mxsfb->base + LCDC_CTRL1 + REG_CLR);
190 
191 	mxsfb_disable_axi_clk(mxsfb);
192 }
193 
194 static int mxsfb_irq_install(struct drm_device *dev, int irq)
195 {
196 	if (irq == IRQ_NOTCONNECTED)
197 		return -ENOTCONN;
198 
199 	mxsfb_irq_disable(dev);
200 
201 	return request_irq(irq, mxsfb_irq_handler, 0,  dev->driver->name, dev);
202 }
203 
204 static void mxsfb_irq_uninstall(struct drm_device *dev)
205 {
206 	struct mxsfb_drm_private *mxsfb = dev->dev_private;
207 
208 	mxsfb_irq_disable(dev);
209 	free_irq(mxsfb->irq, dev);
210 }
211 
212 static int mxsfb_load(struct drm_device *drm,
213 		      const struct mxsfb_devdata *devdata)
214 {
215 	struct platform_device *pdev = to_platform_device(drm->dev);
216 	struct mxsfb_drm_private *mxsfb;
217 	struct resource *res;
218 	int ret;
219 
220 	mxsfb = devm_kzalloc(&pdev->dev, sizeof(*mxsfb), GFP_KERNEL);
221 	if (!mxsfb)
222 		return -ENOMEM;
223 
224 	mxsfb->drm = drm;
225 	drm->dev_private = mxsfb;
226 	mxsfb->devdata = devdata;
227 
228 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
229 	mxsfb->base = devm_ioremap_resource(drm->dev, res);
230 	if (IS_ERR(mxsfb->base))
231 		return PTR_ERR(mxsfb->base);
232 
233 	mxsfb->clk = devm_clk_get(drm->dev, NULL);
234 	if (IS_ERR(mxsfb->clk))
235 		return PTR_ERR(mxsfb->clk);
236 
237 	mxsfb->clk_axi = devm_clk_get_optional(drm->dev, "axi");
238 	if (IS_ERR(mxsfb->clk_axi))
239 		return PTR_ERR(mxsfb->clk_axi);
240 
241 	mxsfb->clk_disp_axi = devm_clk_get(drm->dev, "disp_axi");
242 	if (IS_ERR(mxsfb->clk_disp_axi))
243 		mxsfb->clk_disp_axi = NULL;
244 
245 	ret = dma_set_mask_and_coherent(drm->dev, DMA_BIT_MASK(32));
246 	if (ret)
247 		return ret;
248 
249 	pm_runtime_enable(drm->dev);
250 
251 	/* Modeset init */
252 	drm_mode_config_init(drm);
253 
254 	ret = mxsfb_kms_init(mxsfb);
255 	if (ret < 0) {
256 		dev_err(drm->dev, "Failed to initialize KMS pipeline\n");
257 		goto err_vblank;
258 	}
259 
260 	ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
261 	if (ret < 0) {
262 		dev_err(drm->dev, "Failed to initialise vblank\n");
263 		goto err_vblank;
264 	}
265 
266 	/* Start with vertical blanking interrupt reporting disabled. */
267 	drm_crtc_vblank_off(&mxsfb->crtc);
268 
269 	ret = mxsfb_attach_bridge(mxsfb);
270 	if (ret) {
271 		dev_err_probe(drm->dev, ret, "Cannot connect bridge\n");
272 		goto err_vblank;
273 	}
274 
275 	drm->mode_config.min_width	= MXSFB_MIN_XRES;
276 	drm->mode_config.min_height	= MXSFB_MIN_YRES;
277 	drm->mode_config.max_width	= MXSFB_MAX_XRES;
278 	drm->mode_config.max_height	= MXSFB_MAX_YRES;
279 	drm->mode_config.funcs		= &mxsfb_mode_config_funcs;
280 	drm->mode_config.helper_private	= &mxsfb_mode_config_helpers;
281 
282 	drm_mode_config_reset(drm);
283 
284 	ret = platform_get_irq(pdev, 0);
285 	if (ret < 0)
286 		goto err_vblank;
287 	mxsfb->irq = ret;
288 
289 	pm_runtime_get_sync(drm->dev);
290 	ret = mxsfb_irq_install(drm, mxsfb->irq);
291 	pm_runtime_put_sync(drm->dev);
292 
293 	if (ret < 0) {
294 		dev_err(drm->dev, "Failed to install IRQ handler\n");
295 		goto err_vblank;
296 	}
297 
298 	drm_kms_helper_poll_init(drm);
299 
300 	platform_set_drvdata(pdev, drm);
301 
302 	drm_helper_hpd_irq_event(drm);
303 
304 	return 0;
305 
306 err_vblank:
307 	pm_runtime_disable(drm->dev);
308 
309 	return ret;
310 }
311 
312 static void mxsfb_unload(struct drm_device *drm)
313 {
314 	drm_kms_helper_poll_fini(drm);
315 	drm_mode_config_cleanup(drm);
316 
317 	pm_runtime_get_sync(drm->dev);
318 	mxsfb_irq_uninstall(drm);
319 	pm_runtime_put_sync(drm->dev);
320 
321 	drm->dev_private = NULL;
322 
323 	pm_runtime_disable(drm->dev);
324 }
325 
326 DEFINE_DRM_GEM_DMA_FOPS(fops);
327 
328 static const struct drm_driver mxsfb_driver = {
329 	.driver_features	= DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
330 	DRM_GEM_DMA_DRIVER_OPS,
331 	.fops	= &fops,
332 	.name	= "mxsfb-drm",
333 	.desc	= "MXSFB Controller DRM",
334 	.date	= "20160824",
335 	.major	= 1,
336 	.minor	= 0,
337 };
338 
339 static const struct of_device_id mxsfb_dt_ids[] = {
340 	{ .compatible = "fsl,imx23-lcdif", .data = &mxsfb_devdata[MXSFB_V3], },
341 	{ .compatible = "fsl,imx28-lcdif", .data = &mxsfb_devdata[MXSFB_V4], },
342 	{ .compatible = "fsl,imx6sx-lcdif", .data = &mxsfb_devdata[MXSFB_V6], },
343 	{ /* sentinel */ }
344 };
345 MODULE_DEVICE_TABLE(of, mxsfb_dt_ids);
346 
347 static int mxsfb_probe(struct platform_device *pdev)
348 {
349 	struct drm_device *drm;
350 	int ret;
351 
352 	drm = drm_dev_alloc(&mxsfb_driver, &pdev->dev);
353 	if (IS_ERR(drm))
354 		return PTR_ERR(drm);
355 
356 	ret = mxsfb_load(drm, device_get_match_data(&pdev->dev));
357 	if (ret)
358 		goto err_free;
359 
360 	ret = drm_dev_register(drm, 0);
361 	if (ret)
362 		goto err_unload;
363 
364 	drm_fbdev_dma_setup(drm, 32);
365 
366 	return 0;
367 
368 err_unload:
369 	mxsfb_unload(drm);
370 err_free:
371 	drm_dev_put(drm);
372 
373 	return ret;
374 }
375 
376 static void mxsfb_remove(struct platform_device *pdev)
377 {
378 	struct drm_device *drm = platform_get_drvdata(pdev);
379 
380 	drm_dev_unregister(drm);
381 	drm_atomic_helper_shutdown(drm);
382 	mxsfb_unload(drm);
383 	drm_dev_put(drm);
384 }
385 
386 static void mxsfb_shutdown(struct platform_device *pdev)
387 {
388 	struct drm_device *drm = platform_get_drvdata(pdev);
389 
390 	drm_atomic_helper_shutdown(drm);
391 }
392 
393 #ifdef CONFIG_PM_SLEEP
394 static int mxsfb_suspend(struct device *dev)
395 {
396 	struct drm_device *drm = dev_get_drvdata(dev);
397 
398 	return drm_mode_config_helper_suspend(drm);
399 }
400 
401 static int mxsfb_resume(struct device *dev)
402 {
403 	struct drm_device *drm = dev_get_drvdata(dev);
404 
405 	return drm_mode_config_helper_resume(drm);
406 }
407 #endif
408 
409 static const struct dev_pm_ops mxsfb_pm_ops = {
410 	SET_SYSTEM_SLEEP_PM_OPS(mxsfb_suspend, mxsfb_resume)
411 };
412 
413 static struct platform_driver mxsfb_platform_driver = {
414 	.probe		= mxsfb_probe,
415 	.remove_new	= mxsfb_remove,
416 	.shutdown	= mxsfb_shutdown,
417 	.driver	= {
418 		.name		= "mxsfb",
419 		.of_match_table	= mxsfb_dt_ids,
420 		.pm		= &mxsfb_pm_ops,
421 	},
422 };
423 
424 drm_module_platform_driver(mxsfb_platform_driver);
425 
426 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
427 MODULE_DESCRIPTION("Freescale MXS DRM/KMS driver");
428 MODULE_LICENSE("GPL");
429