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