xref: /openbmc/linux/drivers/gpu/drm/mxsfb/mxsfb_drv.c (revision 297e77e5)
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/component.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/list.h>
15 #include <linux/module.h>
16 #include <linux/of_device.h>
17 #include <linux/of_graph.h>
18 #include <linux/of_reserved_mem.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/dma-resv.h>
21 #include <linux/spinlock.h>
22 
23 #include <drm/drm_atomic.h>
24 #include <drm/drm_atomic_helper.h>
25 #include <drm/drm_crtc.h>
26 #include <drm/drm_drv.h>
27 #include <drm/drm_fb_cma_helper.h>
28 #include <drm/drm_fb_helper.h>
29 #include <drm/drm_gem_cma_helper.h>
30 #include <drm/drm_gem_framebuffer_helper.h>
31 #include <drm/drm_irq.h>
32 #include <drm/drm_of.h>
33 #include <drm/drm_panel.h>
34 #include <drm/drm_probe_helper.h>
35 #include <drm/drm_simple_kms_helper.h>
36 #include <drm/drm_vblank.h>
37 
38 #include "mxsfb_drv.h"
39 #include "mxsfb_regs.h"
40 
41 enum mxsfb_devtype {
42 	MXSFB_V3,
43 	MXSFB_V4,
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 		.debug0		= LCDC_V3_DEBUG0,
52 		.hs_wdth_mask	= 0xff,
53 		.hs_wdth_shift	= 24,
54 		.ipversion	= 3,
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 		.debug0		= LCDC_V4_DEBUG0,
61 		.hs_wdth_mask	= 0x3fff,
62 		.hs_wdth_shift	= 18,
63 		.ipversion	= 4,
64 	},
65 };
66 
67 static const uint32_t mxsfb_formats[] = {
68 	DRM_FORMAT_XRGB8888,
69 	DRM_FORMAT_RGB565
70 };
71 
72 static const uint64_t mxsfb_modifiers[] = {
73 	DRM_FORMAT_MOD_LINEAR,
74 	DRM_FORMAT_MOD_INVALID
75 };
76 
77 static struct mxsfb_drm_private *
78 drm_pipe_to_mxsfb_drm_private(struct drm_simple_display_pipe *pipe)
79 {
80 	return container_of(pipe, struct mxsfb_drm_private, pipe);
81 }
82 
83 void mxsfb_enable_axi_clk(struct mxsfb_drm_private *mxsfb)
84 {
85 	if (mxsfb->clk_axi)
86 		clk_prepare_enable(mxsfb->clk_axi);
87 }
88 
89 void mxsfb_disable_axi_clk(struct mxsfb_drm_private *mxsfb)
90 {
91 	if (mxsfb->clk_axi)
92 		clk_disable_unprepare(mxsfb->clk_axi);
93 }
94 
95 static const struct drm_mode_config_funcs mxsfb_mode_config_funcs = {
96 	.fb_create		= drm_gem_fb_create,
97 	.atomic_check		= drm_atomic_helper_check,
98 	.atomic_commit		= drm_atomic_helper_commit,
99 };
100 
101 static const struct drm_mode_config_helper_funcs mxsfb_mode_config_helpers = {
102 	.atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
103 };
104 
105 static void mxsfb_pipe_enable(struct drm_simple_display_pipe *pipe,
106 			      struct drm_crtc_state *crtc_state,
107 			      struct drm_plane_state *plane_state)
108 {
109 	struct drm_connector *connector;
110 	struct mxsfb_drm_private *mxsfb = drm_pipe_to_mxsfb_drm_private(pipe);
111 	struct drm_device *drm = pipe->plane.dev;
112 
113 	if (!mxsfb->connector) {
114 		list_for_each_entry(connector,
115 				    &drm->mode_config.connector_list,
116 				    head)
117 			if (connector->encoder == &mxsfb->pipe.encoder) {
118 				mxsfb->connector = connector;
119 				break;
120 			}
121 	}
122 
123 	if (!mxsfb->connector) {
124 		dev_warn(drm->dev, "No connector attached, using default\n");
125 		mxsfb->connector = &mxsfb->panel_connector;
126 	}
127 
128 	pm_runtime_get_sync(drm->dev);
129 	drm_panel_prepare(mxsfb->panel);
130 	mxsfb_crtc_enable(mxsfb);
131 	drm_panel_enable(mxsfb->panel);
132 }
133 
134 static void mxsfb_pipe_disable(struct drm_simple_display_pipe *pipe)
135 {
136 	struct mxsfb_drm_private *mxsfb = drm_pipe_to_mxsfb_drm_private(pipe);
137 	struct drm_device *drm = pipe->plane.dev;
138 	struct drm_crtc *crtc = &pipe->crtc;
139 	struct drm_pending_vblank_event *event;
140 
141 	drm_panel_disable(mxsfb->panel);
142 	mxsfb_crtc_disable(mxsfb);
143 	drm_panel_unprepare(mxsfb->panel);
144 	pm_runtime_put_sync(drm->dev);
145 
146 	spin_lock_irq(&drm->event_lock);
147 	event = crtc->state->event;
148 	if (event) {
149 		crtc->state->event = NULL;
150 		drm_crtc_send_vblank_event(crtc, event);
151 	}
152 	spin_unlock_irq(&drm->event_lock);
153 
154 	if (mxsfb->connector != &mxsfb->panel_connector)
155 		mxsfb->connector = NULL;
156 }
157 
158 static void mxsfb_pipe_update(struct drm_simple_display_pipe *pipe,
159 			      struct drm_plane_state *plane_state)
160 {
161 	struct mxsfb_drm_private *mxsfb = drm_pipe_to_mxsfb_drm_private(pipe);
162 
163 	mxsfb_plane_atomic_update(mxsfb, plane_state);
164 }
165 
166 static int mxsfb_pipe_enable_vblank(struct drm_simple_display_pipe *pipe)
167 {
168 	struct mxsfb_drm_private *mxsfb = drm_pipe_to_mxsfb_drm_private(pipe);
169 
170 	/* Clear and enable VBLANK IRQ */
171 	mxsfb_enable_axi_clk(mxsfb);
172 	writel(CTRL1_CUR_FRAME_DONE_IRQ, mxsfb->base + LCDC_CTRL1 + REG_CLR);
173 	writel(CTRL1_CUR_FRAME_DONE_IRQ_EN, mxsfb->base + LCDC_CTRL1 + REG_SET);
174 	mxsfb_disable_axi_clk(mxsfb);
175 
176 	return 0;
177 }
178 
179 static void mxsfb_pipe_disable_vblank(struct drm_simple_display_pipe *pipe)
180 {
181 	struct mxsfb_drm_private *mxsfb = drm_pipe_to_mxsfb_drm_private(pipe);
182 
183 	/* Disable and clear VBLANK IRQ */
184 	mxsfb_enable_axi_clk(mxsfb);
185 	writel(CTRL1_CUR_FRAME_DONE_IRQ_EN, mxsfb->base + LCDC_CTRL1 + REG_CLR);
186 	writel(CTRL1_CUR_FRAME_DONE_IRQ, mxsfb->base + LCDC_CTRL1 + REG_CLR);
187 	mxsfb_disable_axi_clk(mxsfb);
188 }
189 
190 static struct drm_simple_display_pipe_funcs mxsfb_funcs = {
191 	.enable		= mxsfb_pipe_enable,
192 	.disable	= mxsfb_pipe_disable,
193 	.update		= mxsfb_pipe_update,
194 	.prepare_fb	= drm_gem_fb_simple_display_pipe_prepare_fb,
195 	.enable_vblank	= mxsfb_pipe_enable_vblank,
196 	.disable_vblank	= mxsfb_pipe_disable_vblank,
197 };
198 
199 static int mxsfb_load(struct drm_device *drm)
200 {
201 	struct platform_device *pdev = to_platform_device(drm->dev);
202 	struct mxsfb_drm_private *mxsfb;
203 	struct resource *res;
204 	int ret;
205 
206 	mxsfb = devm_kzalloc(&pdev->dev, sizeof(*mxsfb), GFP_KERNEL);
207 	if (!mxsfb)
208 		return -ENOMEM;
209 
210 	drm->dev_private = mxsfb;
211 	mxsfb->devdata = &mxsfb_devdata[pdev->id_entry->driver_data];
212 
213 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
214 	mxsfb->base = devm_ioremap_resource(drm->dev, res);
215 	if (IS_ERR(mxsfb->base))
216 		return PTR_ERR(mxsfb->base);
217 
218 	mxsfb->clk = devm_clk_get(drm->dev, NULL);
219 	if (IS_ERR(mxsfb->clk))
220 		return PTR_ERR(mxsfb->clk);
221 
222 	mxsfb->clk_axi = devm_clk_get(drm->dev, "axi");
223 	if (IS_ERR(mxsfb->clk_axi))
224 		mxsfb->clk_axi = NULL;
225 
226 	mxsfb->clk_disp_axi = devm_clk_get(drm->dev, "disp_axi");
227 	if (IS_ERR(mxsfb->clk_disp_axi))
228 		mxsfb->clk_disp_axi = NULL;
229 
230 	ret = dma_set_mask_and_coherent(drm->dev, DMA_BIT_MASK(32));
231 	if (ret)
232 		return ret;
233 
234 	pm_runtime_enable(drm->dev);
235 
236 	ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
237 	if (ret < 0) {
238 		dev_err(drm->dev, "Failed to initialise vblank\n");
239 		goto err_vblank;
240 	}
241 
242 	/* Modeset init */
243 	drm_mode_config_init(drm);
244 
245 	ret = mxsfb_create_output(drm);
246 	if (ret < 0) {
247 		dev_err(drm->dev, "Failed to create outputs\n");
248 		goto err_vblank;
249 	}
250 
251 	ret = drm_simple_display_pipe_init(drm, &mxsfb->pipe, &mxsfb_funcs,
252 			mxsfb_formats, ARRAY_SIZE(mxsfb_formats),
253 			mxsfb_modifiers, mxsfb->connector);
254 	if (ret < 0) {
255 		dev_err(drm->dev, "Cannot setup simple display pipe\n");
256 		goto err_vblank;
257 	}
258 
259 	/*
260 	 * Attach panel only if there is one.
261 	 * If there is no panel attach, it must be a bridge. In this case, we
262 	 * need a reference to its connector for a proper initialization.
263 	 * We will do this check in pipe->enable(), since the connector won't
264 	 * be attached to an encoder until then.
265 	 */
266 
267 	if (mxsfb->panel) {
268 		ret = drm_panel_attach(mxsfb->panel, mxsfb->connector);
269 		if (ret) {
270 			dev_err(drm->dev, "Cannot connect panel: %d\n", ret);
271 			goto err_vblank;
272 		}
273 	} else if (mxsfb->bridge) {
274 		ret = drm_simple_display_pipe_attach_bridge(&mxsfb->pipe,
275 							    mxsfb->bridge);
276 		if (ret) {
277 			dev_err(drm->dev, "Cannot connect bridge: %d\n", ret);
278 			goto err_vblank;
279 		}
280 	}
281 
282 	drm->mode_config.min_width	= MXSFB_MIN_XRES;
283 	drm->mode_config.min_height	= MXSFB_MIN_YRES;
284 	drm->mode_config.max_width	= MXSFB_MAX_XRES;
285 	drm->mode_config.max_height	= MXSFB_MAX_YRES;
286 	drm->mode_config.funcs		= &mxsfb_mode_config_funcs;
287 	drm->mode_config.helper_private	= &mxsfb_mode_config_helpers;
288 
289 	drm_mode_config_reset(drm);
290 
291 	pm_runtime_get_sync(drm->dev);
292 	ret = drm_irq_install(drm, platform_get_irq(pdev, 0));
293 	pm_runtime_put_sync(drm->dev);
294 
295 	if (ret < 0) {
296 		dev_err(drm->dev, "Failed to install IRQ handler\n");
297 		goto err_irq;
298 	}
299 
300 	drm_kms_helper_poll_init(drm);
301 
302 	platform_set_drvdata(pdev, drm);
303 
304 	drm_helper_hpd_irq_event(drm);
305 
306 	return 0;
307 
308 err_irq:
309 	drm_panel_detach(mxsfb->panel);
310 err_vblank:
311 	pm_runtime_disable(drm->dev);
312 
313 	return ret;
314 }
315 
316 static void mxsfb_unload(struct drm_device *drm)
317 {
318 	drm_kms_helper_poll_fini(drm);
319 	drm_mode_config_cleanup(drm);
320 
321 	pm_runtime_get_sync(drm->dev);
322 	drm_irq_uninstall(drm);
323 	pm_runtime_put_sync(drm->dev);
324 
325 	drm->dev_private = NULL;
326 
327 	pm_runtime_disable(drm->dev);
328 }
329 
330 static void mxsfb_irq_preinstall(struct drm_device *drm)
331 {
332 	struct mxsfb_drm_private *mxsfb = drm->dev_private;
333 
334 	mxsfb_pipe_disable_vblank(&mxsfb->pipe);
335 }
336 
337 static irqreturn_t mxsfb_irq_handler(int irq, void *data)
338 {
339 	struct drm_device *drm = data;
340 	struct mxsfb_drm_private *mxsfb = drm->dev_private;
341 	u32 reg;
342 
343 	mxsfb_enable_axi_clk(mxsfb);
344 
345 	reg = readl(mxsfb->base + LCDC_CTRL1);
346 
347 	if (reg & CTRL1_CUR_FRAME_DONE_IRQ)
348 		drm_crtc_handle_vblank(&mxsfb->pipe.crtc);
349 
350 	writel(CTRL1_CUR_FRAME_DONE_IRQ, mxsfb->base + LCDC_CTRL1 + REG_CLR);
351 
352 	mxsfb_disable_axi_clk(mxsfb);
353 
354 	return IRQ_HANDLED;
355 }
356 
357 DEFINE_DRM_GEM_CMA_FOPS(fops);
358 
359 static struct drm_driver mxsfb_driver = {
360 	.driver_features	= DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
361 	.irq_handler		= mxsfb_irq_handler,
362 	.irq_preinstall		= mxsfb_irq_preinstall,
363 	.irq_uninstall		= mxsfb_irq_preinstall,
364 	DRM_GEM_CMA_DRIVER_OPS,
365 	.fops	= &fops,
366 	.name	= "mxsfb-drm",
367 	.desc	= "MXSFB Controller DRM",
368 	.date	= "20160824",
369 	.major	= 1,
370 	.minor	= 0,
371 };
372 
373 static const struct platform_device_id mxsfb_devtype[] = {
374 	{ .name = "imx23-fb", .driver_data = MXSFB_V3, },
375 	{ .name = "imx28-fb", .driver_data = MXSFB_V4, },
376 	{ .name = "imx6sx-fb", .driver_data = MXSFB_V4, },
377 	{ /* sentinel */ }
378 };
379 MODULE_DEVICE_TABLE(platform, mxsfb_devtype);
380 
381 static const struct of_device_id mxsfb_dt_ids[] = {
382 	{ .compatible = "fsl,imx23-lcdif", .data = &mxsfb_devtype[0], },
383 	{ .compatible = "fsl,imx28-lcdif", .data = &mxsfb_devtype[1], },
384 	{ .compatible = "fsl,imx6sx-lcdif", .data = &mxsfb_devtype[2], },
385 	{ /* sentinel */ }
386 };
387 MODULE_DEVICE_TABLE(of, mxsfb_dt_ids);
388 
389 static int mxsfb_probe(struct platform_device *pdev)
390 {
391 	struct drm_device *drm;
392 	const struct of_device_id *of_id =
393 			of_match_device(mxsfb_dt_ids, &pdev->dev);
394 	int ret;
395 
396 	if (!pdev->dev.of_node)
397 		return -ENODEV;
398 
399 	if (of_id)
400 		pdev->id_entry = of_id->data;
401 
402 	drm = drm_dev_alloc(&mxsfb_driver, &pdev->dev);
403 	if (IS_ERR(drm))
404 		return PTR_ERR(drm);
405 
406 	ret = mxsfb_load(drm);
407 	if (ret)
408 		goto err_free;
409 
410 	ret = drm_dev_register(drm, 0);
411 	if (ret)
412 		goto err_unload;
413 
414 	drm_fbdev_generic_setup(drm, 32);
415 
416 	return 0;
417 
418 err_unload:
419 	mxsfb_unload(drm);
420 err_free:
421 	drm_dev_put(drm);
422 
423 	return ret;
424 }
425 
426 static int mxsfb_remove(struct platform_device *pdev)
427 {
428 	struct drm_device *drm = platform_get_drvdata(pdev);
429 
430 	drm_dev_unregister(drm);
431 	mxsfb_unload(drm);
432 	drm_dev_put(drm);
433 
434 	return 0;
435 }
436 
437 #ifdef CONFIG_PM_SLEEP
438 static int mxsfb_suspend(struct device *dev)
439 {
440 	struct drm_device *drm = dev_get_drvdata(dev);
441 
442 	return drm_mode_config_helper_suspend(drm);
443 }
444 
445 static int mxsfb_resume(struct device *dev)
446 {
447 	struct drm_device *drm = dev_get_drvdata(dev);
448 
449 	return drm_mode_config_helper_resume(drm);
450 }
451 #endif
452 
453 static const struct dev_pm_ops mxsfb_pm_ops = {
454 	SET_SYSTEM_SLEEP_PM_OPS(mxsfb_suspend, mxsfb_resume)
455 };
456 
457 static struct platform_driver mxsfb_platform_driver = {
458 	.probe		= mxsfb_probe,
459 	.remove		= mxsfb_remove,
460 	.id_table	= mxsfb_devtype,
461 	.driver	= {
462 		.name		= "mxsfb",
463 		.of_match_table	= mxsfb_dt_ids,
464 		.pm		= &mxsfb_pm_ops,
465 	},
466 };
467 
468 module_platform_driver(mxsfb_platform_driver);
469 
470 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
471 MODULE_DESCRIPTION("Freescale MXS DRM/KMS driver");
472 MODULE_LICENSE("GPL");
473