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