xref: /openbmc/linux/drivers/gpu/drm/mxsfb/mxsfb_drv.c (revision 4c79e98b)
1 /*
2  * Copyright (C) 2016 Marek Vasut <marex@denx.de>
3  *
4  * This code is based on drivers/video/fbdev/mxsfb.c :
5  * Copyright (C) 2010 Juergen Beisert, Pengutronix
6  * Copyright (C) 2008-2009 Freescale Semiconductor, Inc. All Rights Reserved.
7  * Copyright (C) 2008 Embedded Alley Solutions, Inc All Rights Reserved.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  */
18 
19 #include <linux/module.h>
20 #include <linux/spinlock.h>
21 #include <linux/clk.h>
22 #include <linux/component.h>
23 #include <linux/list.h>
24 #include <linux/of_device.h>
25 #include <linux/of_graph.h>
26 #include <linux/of_reserved_mem.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/reservation.h>
29 
30 #include <drm/drmP.h>
31 #include <drm/drm_atomic.h>
32 #include <drm/drm_atomic_helper.h>
33 #include <drm/drm_crtc.h>
34 #include <drm/drm_crtc_helper.h>
35 #include <drm/drm_fb_helper.h>
36 #include <drm/drm_fb_cma_helper.h>
37 #include <drm/drm_gem_cma_helper.h>
38 #include <drm/drm_of.h>
39 #include <drm/drm_panel.h>
40 #include <drm/drm_simple_kms_helper.h>
41 
42 #include "mxsfb_drv.h"
43 #include "mxsfb_regs.h"
44 
45 enum mxsfb_devtype {
46 	MXSFB_V3,
47 	MXSFB_V4,
48 };
49 
50 static const struct mxsfb_devdata mxsfb_devdata[] = {
51 	[MXSFB_V3] = {
52 		.transfer_count	= LCDC_V3_TRANSFER_COUNT,
53 		.cur_buf	= LCDC_V3_CUR_BUF,
54 		.next_buf	= LCDC_V3_NEXT_BUF,
55 		.debug0		= LCDC_V3_DEBUG0,
56 		.hs_wdth_mask	= 0xff,
57 		.hs_wdth_shift	= 24,
58 		.ipversion	= 3,
59 	},
60 	[MXSFB_V4] = {
61 		.transfer_count	= LCDC_V4_TRANSFER_COUNT,
62 		.cur_buf	= LCDC_V4_CUR_BUF,
63 		.next_buf	= LCDC_V4_NEXT_BUF,
64 		.debug0		= LCDC_V4_DEBUG0,
65 		.hs_wdth_mask	= 0x3fff,
66 		.hs_wdth_shift	= 18,
67 		.ipversion	= 4,
68 	},
69 };
70 
71 static const uint32_t mxsfb_formats[] = {
72 	DRM_FORMAT_XRGB8888,
73 	DRM_FORMAT_RGB565
74 };
75 
76 static struct mxsfb_drm_private *
77 drm_pipe_to_mxsfb_drm_private(struct drm_simple_display_pipe *pipe)
78 {
79 	return container_of(pipe, struct mxsfb_drm_private, pipe);
80 }
81 
82 void mxsfb_enable_axi_clk(struct mxsfb_drm_private *mxsfb)
83 {
84 	if (mxsfb->clk_axi)
85 		clk_prepare_enable(mxsfb->clk_axi);
86 }
87 
88 void mxsfb_disable_axi_clk(struct mxsfb_drm_private *mxsfb)
89 {
90 	if (mxsfb->clk_axi)
91 		clk_disable_unprepare(mxsfb->clk_axi);
92 }
93 
94 static const struct drm_mode_config_funcs mxsfb_mode_config_funcs = {
95 	.fb_create		= drm_fb_cma_create,
96 	.atomic_check		= drm_atomic_helper_check,
97 	.atomic_commit		= drm_atomic_helper_commit,
98 };
99 
100 static void mxsfb_pipe_enable(struct drm_simple_display_pipe *pipe,
101 			      struct drm_crtc_state *crtc_state)
102 {
103 	struct mxsfb_drm_private *mxsfb = drm_pipe_to_mxsfb_drm_private(pipe);
104 
105 	drm_panel_prepare(mxsfb->panel);
106 	mxsfb_crtc_enable(mxsfb);
107 	drm_panel_enable(mxsfb->panel);
108 }
109 
110 static void mxsfb_pipe_disable(struct drm_simple_display_pipe *pipe)
111 {
112 	struct mxsfb_drm_private *mxsfb = drm_pipe_to_mxsfb_drm_private(pipe);
113 
114 	drm_panel_disable(mxsfb->panel);
115 	mxsfb_crtc_disable(mxsfb);
116 	drm_panel_unprepare(mxsfb->panel);
117 }
118 
119 static void mxsfb_pipe_update(struct drm_simple_display_pipe *pipe,
120 			      struct drm_plane_state *plane_state)
121 {
122 	struct mxsfb_drm_private *mxsfb = drm_pipe_to_mxsfb_drm_private(pipe);
123 
124 	mxsfb_plane_atomic_update(mxsfb, plane_state);
125 }
126 
127 static int mxsfb_pipe_prepare_fb(struct drm_simple_display_pipe *pipe,
128 				 struct drm_plane_state *plane_state)
129 {
130 	return drm_fb_cma_prepare_fb(&pipe->plane, plane_state);
131 }
132 
133 struct drm_simple_display_pipe_funcs mxsfb_funcs = {
134 	.enable		= mxsfb_pipe_enable,
135 	.disable	= mxsfb_pipe_disable,
136 	.update		= mxsfb_pipe_update,
137 	.prepare_fb	= mxsfb_pipe_prepare_fb,
138 };
139 
140 static int mxsfb_load(struct drm_device *drm, unsigned long flags)
141 {
142 	struct platform_device *pdev = to_platform_device(drm->dev);
143 	struct mxsfb_drm_private *mxsfb;
144 	struct resource *res;
145 	int ret;
146 
147 	mxsfb = devm_kzalloc(&pdev->dev, sizeof(*mxsfb), GFP_KERNEL);
148 	if (!mxsfb)
149 		return -ENOMEM;
150 
151 	drm->dev_private = mxsfb;
152 	mxsfb->devdata = &mxsfb_devdata[pdev->id_entry->driver_data];
153 
154 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
155 	mxsfb->base = devm_ioremap_resource(drm->dev, res);
156 	if (IS_ERR(mxsfb->base))
157 		return PTR_ERR(mxsfb->base);
158 
159 	mxsfb->clk = devm_clk_get(drm->dev, NULL);
160 	if (IS_ERR(mxsfb->clk))
161 		return PTR_ERR(mxsfb->clk);
162 
163 	mxsfb->clk_axi = devm_clk_get(drm->dev, "axi");
164 	if (IS_ERR(mxsfb->clk_axi))
165 		mxsfb->clk_axi = NULL;
166 
167 	mxsfb->clk_disp_axi = devm_clk_get(drm->dev, "disp_axi");
168 	if (IS_ERR(mxsfb->clk_disp_axi))
169 		mxsfb->clk_disp_axi = NULL;
170 
171 	ret = dma_set_mask_and_coherent(drm->dev, DMA_BIT_MASK(32));
172 	if (ret)
173 		return ret;
174 
175 	pm_runtime_enable(drm->dev);
176 
177 	ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
178 	if (ret < 0) {
179 		dev_err(drm->dev, "Failed to initialise vblank\n");
180 		goto err_vblank;
181 	}
182 
183 	/* Modeset init */
184 	drm_mode_config_init(drm);
185 
186 	ret = mxsfb_create_output(drm);
187 	if (ret < 0) {
188 		dev_err(drm->dev, "Failed to create outputs\n");
189 		goto err_vblank;
190 	}
191 
192 	ret = drm_simple_display_pipe_init(drm, &mxsfb->pipe, &mxsfb_funcs,
193 			mxsfb_formats, ARRAY_SIZE(mxsfb_formats),
194 			&mxsfb->connector);
195 	if (ret < 0) {
196 		dev_err(drm->dev, "Cannot setup simple display pipe\n");
197 		goto err_vblank;
198 	}
199 
200 	ret = drm_panel_attach(mxsfb->panel, &mxsfb->connector);
201 	if (ret) {
202 		dev_err(drm->dev, "Cannot connect panel\n");
203 		goto err_vblank;
204 	}
205 
206 	drm->mode_config.min_width	= MXSFB_MIN_XRES;
207 	drm->mode_config.min_height	= MXSFB_MIN_YRES;
208 	drm->mode_config.max_width	= MXSFB_MAX_XRES;
209 	drm->mode_config.max_height	= MXSFB_MAX_YRES;
210 	drm->mode_config.funcs		= &mxsfb_mode_config_funcs;
211 
212 	drm_mode_config_reset(drm);
213 
214 	pm_runtime_get_sync(drm->dev);
215 	ret = drm_irq_install(drm, platform_get_irq(pdev, 0));
216 	pm_runtime_put_sync(drm->dev);
217 
218 	if (ret < 0) {
219 		dev_err(drm->dev, "Failed to install IRQ handler\n");
220 		goto err_irq;
221 	}
222 
223 	drm_kms_helper_poll_init(drm);
224 
225 	mxsfb->fbdev = drm_fbdev_cma_init(drm, 32,
226 					  drm->mode_config.num_connector);
227 	if (IS_ERR(mxsfb->fbdev)) {
228 		mxsfb->fbdev = NULL;
229 		dev_err(drm->dev, "Failed to init FB CMA area\n");
230 		goto err_cma;
231 	}
232 
233 	platform_set_drvdata(pdev, drm);
234 
235 	drm_helper_hpd_irq_event(drm);
236 
237 	return 0;
238 
239 err_cma:
240 	drm_irq_uninstall(drm);
241 err_irq:
242 	drm_panel_detach(mxsfb->panel);
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 	struct mxsfb_drm_private *mxsfb = drm->dev_private;
252 
253 	if (mxsfb->fbdev)
254 		drm_fbdev_cma_fini(mxsfb->fbdev);
255 
256 	drm_kms_helper_poll_fini(drm);
257 	drm_mode_config_cleanup(drm);
258 	drm_vblank_cleanup(drm);
259 
260 	pm_runtime_get_sync(drm->dev);
261 	drm_irq_uninstall(drm);
262 	pm_runtime_put_sync(drm->dev);
263 
264 	drm->dev_private = NULL;
265 
266 	pm_runtime_disable(drm->dev);
267 }
268 
269 static void mxsfb_lastclose(struct drm_device *drm)
270 {
271 	struct mxsfb_drm_private *mxsfb = drm->dev_private;
272 
273 	drm_fbdev_cma_restore_mode(mxsfb->fbdev);
274 }
275 
276 static int mxsfb_enable_vblank(struct drm_device *drm, unsigned int crtc)
277 {
278 	struct mxsfb_drm_private *mxsfb = drm->dev_private;
279 
280 	/* Clear and enable VBLANK IRQ */
281 	mxsfb_enable_axi_clk(mxsfb);
282 	writel(CTRL1_CUR_FRAME_DONE_IRQ, mxsfb->base + LCDC_CTRL1 + REG_CLR);
283 	writel(CTRL1_CUR_FRAME_DONE_IRQ_EN, mxsfb->base + LCDC_CTRL1 + REG_SET);
284 	mxsfb_disable_axi_clk(mxsfb);
285 
286 	return 0;
287 }
288 
289 static void mxsfb_disable_vblank(struct drm_device *drm, unsigned int crtc)
290 {
291 	struct mxsfb_drm_private *mxsfb = drm->dev_private;
292 
293 	/* Disable and clear VBLANK IRQ */
294 	mxsfb_enable_axi_clk(mxsfb);
295 	writel(CTRL1_CUR_FRAME_DONE_IRQ_EN, mxsfb->base + LCDC_CTRL1 + REG_CLR);
296 	writel(CTRL1_CUR_FRAME_DONE_IRQ, mxsfb->base + LCDC_CTRL1 + REG_CLR);
297 	mxsfb_disable_axi_clk(mxsfb);
298 }
299 
300 static void mxsfb_irq_preinstall(struct drm_device *drm)
301 {
302 	mxsfb_disable_vblank(drm, 0);
303 }
304 
305 static irqreturn_t mxsfb_irq_handler(int irq, void *data)
306 {
307 	struct drm_device *drm = data;
308 	struct mxsfb_drm_private *mxsfb = drm->dev_private;
309 	u32 reg;
310 
311 	mxsfb_enable_axi_clk(mxsfb);
312 
313 	reg = readl(mxsfb->base + LCDC_CTRL1);
314 
315 	if (reg & CTRL1_CUR_FRAME_DONE_IRQ)
316 		drm_crtc_handle_vblank(&mxsfb->pipe.crtc);
317 
318 	writel(CTRL1_CUR_FRAME_DONE_IRQ, mxsfb->base + LCDC_CTRL1 + REG_CLR);
319 
320 	mxsfb_disable_axi_clk(mxsfb);
321 
322 	return IRQ_HANDLED;
323 }
324 
325 static const struct file_operations fops = {
326 	.owner		= THIS_MODULE,
327 	.open		= drm_open,
328 	.release	= drm_release,
329 	.unlocked_ioctl	= drm_ioctl,
330 #ifdef CONFIG_COMPAT
331 	.compat_ioctl	= drm_compat_ioctl,
332 #endif
333 	.poll		= drm_poll,
334 	.read		= drm_read,
335 	.llseek		= noop_llseek,
336 	.mmap		= drm_gem_cma_mmap,
337 };
338 
339 static struct drm_driver mxsfb_driver = {
340 	.driver_features	= DRIVER_GEM | DRIVER_MODESET |
341 				  DRIVER_PRIME | DRIVER_ATOMIC |
342 				  DRIVER_HAVE_IRQ,
343 	.lastclose		= mxsfb_lastclose,
344 	.irq_handler		= mxsfb_irq_handler,
345 	.irq_preinstall		= mxsfb_irq_preinstall,
346 	.irq_uninstall		= mxsfb_irq_preinstall,
347 	.get_vblank_counter	= drm_vblank_no_hw_counter,
348 	.enable_vblank		= mxsfb_enable_vblank,
349 	.disable_vblank		= mxsfb_disable_vblank,
350 	.gem_free_object	= drm_gem_cma_free_object,
351 	.gem_vm_ops		= &drm_gem_cma_vm_ops,
352 	.dumb_create		= drm_gem_cma_dumb_create,
353 	.dumb_map_offset	= drm_gem_cma_dumb_map_offset,
354 	.dumb_destroy		= drm_gem_dumb_destroy,
355 	.prime_handle_to_fd	= drm_gem_prime_handle_to_fd,
356 	.prime_fd_to_handle	= drm_gem_prime_fd_to_handle,
357 	.gem_prime_export	= drm_gem_prime_export,
358 	.gem_prime_import	= drm_gem_prime_import,
359 	.gem_prime_get_sg_table	= drm_gem_cma_prime_get_sg_table,
360 	.gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
361 	.gem_prime_vmap		= drm_gem_cma_prime_vmap,
362 	.gem_prime_vunmap	= drm_gem_cma_prime_vunmap,
363 	.gem_prime_mmap		= drm_gem_cma_prime_mmap,
364 	.fops	= &fops,
365 	.name	= "mxsfb-drm",
366 	.desc	= "MXSFB Controller DRM",
367 	.date	= "20160824",
368 	.major	= 1,
369 	.minor	= 0,
370 };
371 
372 static const struct platform_device_id mxsfb_devtype[] = {
373 	{ .name = "imx23-fb", .driver_data = MXSFB_V3, },
374 	{ .name = "imx28-fb", .driver_data = MXSFB_V4, },
375 	{ .name = "imx6sx-fb", .driver_data = MXSFB_V4, },
376 	{ /* sentinel */ }
377 };
378 MODULE_DEVICE_TABLE(platform, mxsfb_devtype);
379 
380 static const struct of_device_id mxsfb_dt_ids[] = {
381 	{ .compatible = "fsl,imx23-lcdif", .data = &mxsfb_devtype[0], },
382 	{ .compatible = "fsl,imx28-lcdif", .data = &mxsfb_devtype[1], },
383 	{ .compatible = "fsl,imx6sx-lcdif", .data = &mxsfb_devtype[2], },
384 	{ /* sentinel */ }
385 };
386 MODULE_DEVICE_TABLE(of, mxsfb_dt_ids);
387 
388 static int mxsfb_probe(struct platform_device *pdev)
389 {
390 	struct drm_device *drm;
391 	const struct of_device_id *of_id =
392 			of_match_device(mxsfb_dt_ids, &pdev->dev);
393 	int ret;
394 
395 	if (!pdev->dev.of_node)
396 		return -ENODEV;
397 
398 	if (of_id)
399 		pdev->id_entry = of_id->data;
400 
401 	drm = drm_dev_alloc(&mxsfb_driver, &pdev->dev);
402 	if (IS_ERR(drm))
403 		return PTR_ERR(drm);
404 
405 	ret = mxsfb_load(drm, 0);
406 	if (ret)
407 		goto err_free;
408 
409 	ret = drm_dev_register(drm, 0);
410 	if (ret)
411 		goto err_unload;
412 
413 	return 0;
414 
415 err_unload:
416 	mxsfb_unload(drm);
417 err_free:
418 	drm_dev_unref(drm);
419 
420 	return ret;
421 }
422 
423 static int mxsfb_remove(struct platform_device *pdev)
424 {
425 	struct drm_device *drm = platform_get_drvdata(pdev);
426 
427 	drm_dev_unregister(drm);
428 	mxsfb_unload(drm);
429 	drm_dev_unref(drm);
430 
431 	return 0;
432 }
433 
434 static struct platform_driver mxsfb_platform_driver = {
435 	.probe		= mxsfb_probe,
436 	.remove		= mxsfb_remove,
437 	.id_table	= mxsfb_devtype,
438 	.driver	= {
439 		.name		= "mxsfb",
440 		.of_match_table	= mxsfb_dt_ids,
441 	},
442 };
443 
444 module_platform_driver(mxsfb_platform_driver);
445 
446 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
447 MODULE_DESCRIPTION("Freescale MXS DRM/KMS driver");
448 MODULE_LICENSE("GPL");
449