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