drm/msm/hdmi: Enable HPD after HDMI IRQ is set up
[linux/fpc-iii.git] / drivers / gpu / drm / mxsfb / mxsfb_drv.c
blobffe5137ccaf8806b7a6342a7c3280f9dc399bfd6
1 /*
2 * Copyright (C) 2016 Marek Vasut <marex@denx.de>
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.
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.
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>
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_gem_framebuffer_helper.h>
39 #include <drm/drm_of.h>
40 #include <drm/drm_panel.h>
41 #include <drm/drm_simple_kms_helper.h>
43 #include "mxsfb_drv.h"
44 #include "mxsfb_regs.h"
46 enum mxsfb_devtype {
47 MXSFB_V3,
48 MXSFB_V4,
51 static const struct mxsfb_devdata mxsfb_devdata[] = {
52 [MXSFB_V3] = {
53 .transfer_count = LCDC_V3_TRANSFER_COUNT,
54 .cur_buf = LCDC_V3_CUR_BUF,
55 .next_buf = LCDC_V3_NEXT_BUF,
56 .debug0 = LCDC_V3_DEBUG0,
57 .hs_wdth_mask = 0xff,
58 .hs_wdth_shift = 24,
59 .ipversion = 3,
61 [MXSFB_V4] = {
62 .transfer_count = LCDC_V4_TRANSFER_COUNT,
63 .cur_buf = LCDC_V4_CUR_BUF,
64 .next_buf = LCDC_V4_NEXT_BUF,
65 .debug0 = LCDC_V4_DEBUG0,
66 .hs_wdth_mask = 0x3fff,
67 .hs_wdth_shift = 18,
68 .ipversion = 4,
72 static const uint32_t mxsfb_formats[] = {
73 DRM_FORMAT_XRGB8888,
74 DRM_FORMAT_RGB565
77 static struct mxsfb_drm_private *
78 drm_pipe_to_mxsfb_drm_private(struct drm_simple_display_pipe *pipe)
80 return container_of(pipe, struct mxsfb_drm_private, pipe);
83 void mxsfb_enable_axi_clk(struct mxsfb_drm_private *mxsfb)
85 if (mxsfb->clk_axi)
86 clk_prepare_enable(mxsfb->clk_axi);
89 void mxsfb_disable_axi_clk(struct mxsfb_drm_private *mxsfb)
91 if (mxsfb->clk_axi)
92 clk_disable_unprepare(mxsfb->clk_axi);
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,
101 static void mxsfb_pipe_enable(struct drm_simple_display_pipe *pipe,
102 struct drm_crtc_state *crtc_state,
103 struct drm_plane_state *plane_state)
105 struct mxsfb_drm_private *mxsfb = drm_pipe_to_mxsfb_drm_private(pipe);
107 drm_panel_prepare(mxsfb->panel);
108 mxsfb_crtc_enable(mxsfb);
109 drm_panel_enable(mxsfb->panel);
112 static void mxsfb_pipe_disable(struct drm_simple_display_pipe *pipe)
114 struct mxsfb_drm_private *mxsfb = drm_pipe_to_mxsfb_drm_private(pipe);
116 drm_panel_disable(mxsfb->panel);
117 mxsfb_crtc_disable(mxsfb);
118 drm_panel_unprepare(mxsfb->panel);
121 static void mxsfb_pipe_update(struct drm_simple_display_pipe *pipe,
122 struct drm_plane_state *plane_state)
124 struct mxsfb_drm_private *mxsfb = drm_pipe_to_mxsfb_drm_private(pipe);
126 mxsfb_plane_atomic_update(mxsfb, plane_state);
129 static int mxsfb_pipe_enable_vblank(struct drm_simple_display_pipe *pipe)
131 struct mxsfb_drm_private *mxsfb = drm_pipe_to_mxsfb_drm_private(pipe);
133 /* Clear and enable VBLANK IRQ */
134 mxsfb_enable_axi_clk(mxsfb);
135 writel(CTRL1_CUR_FRAME_DONE_IRQ, mxsfb->base + LCDC_CTRL1 + REG_CLR);
136 writel(CTRL1_CUR_FRAME_DONE_IRQ_EN, mxsfb->base + LCDC_CTRL1 + REG_SET);
137 mxsfb_disable_axi_clk(mxsfb);
139 return 0;
142 static void mxsfb_pipe_disable_vblank(struct drm_simple_display_pipe *pipe)
144 struct mxsfb_drm_private *mxsfb = drm_pipe_to_mxsfb_drm_private(pipe);
146 /* Disable and clear VBLANK IRQ */
147 mxsfb_enable_axi_clk(mxsfb);
148 writel(CTRL1_CUR_FRAME_DONE_IRQ_EN, mxsfb->base + LCDC_CTRL1 + REG_CLR);
149 writel(CTRL1_CUR_FRAME_DONE_IRQ, mxsfb->base + LCDC_CTRL1 + REG_CLR);
150 mxsfb_disable_axi_clk(mxsfb);
153 static struct drm_simple_display_pipe_funcs mxsfb_funcs = {
154 .enable = mxsfb_pipe_enable,
155 .disable = mxsfb_pipe_disable,
156 .update = mxsfb_pipe_update,
157 .prepare_fb = drm_gem_fb_simple_display_pipe_prepare_fb,
158 .enable_vblank = mxsfb_pipe_enable_vblank,
159 .disable_vblank = mxsfb_pipe_disable_vblank,
162 static int mxsfb_load(struct drm_device *drm, unsigned long flags)
164 struct platform_device *pdev = to_platform_device(drm->dev);
165 struct mxsfb_drm_private *mxsfb;
166 struct resource *res;
167 int ret;
169 mxsfb = devm_kzalloc(&pdev->dev, sizeof(*mxsfb), GFP_KERNEL);
170 if (!mxsfb)
171 return -ENOMEM;
173 drm->dev_private = mxsfb;
174 mxsfb->devdata = &mxsfb_devdata[pdev->id_entry->driver_data];
176 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
177 mxsfb->base = devm_ioremap_resource(drm->dev, res);
178 if (IS_ERR(mxsfb->base))
179 return PTR_ERR(mxsfb->base);
181 mxsfb->clk = devm_clk_get(drm->dev, NULL);
182 if (IS_ERR(mxsfb->clk))
183 return PTR_ERR(mxsfb->clk);
185 mxsfb->clk_axi = devm_clk_get(drm->dev, "axi");
186 if (IS_ERR(mxsfb->clk_axi))
187 mxsfb->clk_axi = NULL;
189 mxsfb->clk_disp_axi = devm_clk_get(drm->dev, "disp_axi");
190 if (IS_ERR(mxsfb->clk_disp_axi))
191 mxsfb->clk_disp_axi = NULL;
193 ret = dma_set_mask_and_coherent(drm->dev, DMA_BIT_MASK(32));
194 if (ret)
195 return ret;
197 pm_runtime_enable(drm->dev);
199 ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
200 if (ret < 0) {
201 dev_err(drm->dev, "Failed to initialise vblank\n");
202 goto err_vblank;
205 /* Modeset init */
206 drm_mode_config_init(drm);
208 ret = mxsfb_create_output(drm);
209 if (ret < 0) {
210 dev_err(drm->dev, "Failed to create outputs\n");
211 goto err_vblank;
214 ret = drm_simple_display_pipe_init(drm, &mxsfb->pipe, &mxsfb_funcs,
215 mxsfb_formats, ARRAY_SIZE(mxsfb_formats), NULL,
216 &mxsfb->connector);
217 if (ret < 0) {
218 dev_err(drm->dev, "Cannot setup simple display pipe\n");
219 goto err_vblank;
222 ret = drm_panel_attach(mxsfb->panel, &mxsfb->connector);
223 if (ret) {
224 dev_err(drm->dev, "Cannot connect panel\n");
225 goto err_vblank;
228 drm->mode_config.min_width = MXSFB_MIN_XRES;
229 drm->mode_config.min_height = MXSFB_MIN_YRES;
230 drm->mode_config.max_width = MXSFB_MAX_XRES;
231 drm->mode_config.max_height = MXSFB_MAX_YRES;
232 drm->mode_config.funcs = &mxsfb_mode_config_funcs;
234 drm_mode_config_reset(drm);
236 pm_runtime_get_sync(drm->dev);
237 ret = drm_irq_install(drm, platform_get_irq(pdev, 0));
238 pm_runtime_put_sync(drm->dev);
240 if (ret < 0) {
241 dev_err(drm->dev, "Failed to install IRQ handler\n");
242 goto err_irq;
245 drm_kms_helper_poll_init(drm);
247 mxsfb->fbdev = drm_fbdev_cma_init(drm, 32,
248 drm->mode_config.num_connector);
249 if (IS_ERR(mxsfb->fbdev)) {
250 ret = PTR_ERR(mxsfb->fbdev);
251 mxsfb->fbdev = NULL;
252 dev_err(drm->dev, "Failed to init FB CMA area\n");
253 goto err_cma;
256 platform_set_drvdata(pdev, drm);
258 drm_helper_hpd_irq_event(drm);
260 return 0;
262 err_cma:
263 drm_irq_uninstall(drm);
264 err_irq:
265 drm_panel_detach(mxsfb->panel);
266 err_vblank:
267 pm_runtime_disable(drm->dev);
269 return ret;
272 static void mxsfb_unload(struct drm_device *drm)
274 struct mxsfb_drm_private *mxsfb = drm->dev_private;
276 if (mxsfb->fbdev)
277 drm_fbdev_cma_fini(mxsfb->fbdev);
279 drm_kms_helper_poll_fini(drm);
280 drm_mode_config_cleanup(drm);
282 pm_runtime_get_sync(drm->dev);
283 drm_irq_uninstall(drm);
284 pm_runtime_put_sync(drm->dev);
286 drm->dev_private = NULL;
288 pm_runtime_disable(drm->dev);
291 static void mxsfb_lastclose(struct drm_device *drm)
293 struct mxsfb_drm_private *mxsfb = drm->dev_private;
295 drm_fbdev_cma_restore_mode(mxsfb->fbdev);
298 static void mxsfb_irq_preinstall(struct drm_device *drm)
300 struct mxsfb_drm_private *mxsfb = drm->dev_private;
302 mxsfb_pipe_disable_vblank(&mxsfb->pipe);
305 static irqreturn_t mxsfb_irq_handler(int irq, void *data)
307 struct drm_device *drm = data;
308 struct mxsfb_drm_private *mxsfb = drm->dev_private;
309 u32 reg;
311 mxsfb_enable_axi_clk(mxsfb);
313 reg = readl(mxsfb->base + LCDC_CTRL1);
315 if (reg & CTRL1_CUR_FRAME_DONE_IRQ)
316 drm_crtc_handle_vblank(&mxsfb->pipe.crtc);
318 writel(CTRL1_CUR_FRAME_DONE_IRQ, mxsfb->base + LCDC_CTRL1 + REG_CLR);
320 mxsfb_disable_axi_clk(mxsfb);
322 return IRQ_HANDLED;
325 DEFINE_DRM_GEM_CMA_FOPS(fops);
327 static struct drm_driver mxsfb_driver = {
328 .driver_features = DRIVER_GEM | DRIVER_MODESET |
329 DRIVER_PRIME | DRIVER_ATOMIC |
330 DRIVER_HAVE_IRQ,
331 .lastclose = mxsfb_lastclose,
332 .irq_handler = mxsfb_irq_handler,
333 .irq_preinstall = mxsfb_irq_preinstall,
334 .irq_uninstall = mxsfb_irq_preinstall,
335 .gem_free_object_unlocked = drm_gem_cma_free_object,
336 .gem_vm_ops = &drm_gem_cma_vm_ops,
337 .dumb_create = drm_gem_cma_dumb_create,
338 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
339 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
340 .gem_prime_export = drm_gem_prime_export,
341 .gem_prime_import = drm_gem_prime_import,
342 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
343 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
344 .gem_prime_vmap = drm_gem_cma_prime_vmap,
345 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
346 .gem_prime_mmap = drm_gem_cma_prime_mmap,
347 .fops = &fops,
348 .name = "mxsfb-drm",
349 .desc = "MXSFB Controller DRM",
350 .date = "20160824",
351 .major = 1,
352 .minor = 0,
355 static const struct platform_device_id mxsfb_devtype[] = {
356 { .name = "imx23-fb", .driver_data = MXSFB_V3, },
357 { .name = "imx28-fb", .driver_data = MXSFB_V4, },
358 { .name = "imx6sx-fb", .driver_data = MXSFB_V4, },
359 { /* sentinel */ }
361 MODULE_DEVICE_TABLE(platform, mxsfb_devtype);
363 static const struct of_device_id mxsfb_dt_ids[] = {
364 { .compatible = "fsl,imx23-lcdif", .data = &mxsfb_devtype[0], },
365 { .compatible = "fsl,imx28-lcdif", .data = &mxsfb_devtype[1], },
366 { .compatible = "fsl,imx6sx-lcdif", .data = &mxsfb_devtype[2], },
367 { /* sentinel */ }
369 MODULE_DEVICE_TABLE(of, mxsfb_dt_ids);
371 static int mxsfb_probe(struct platform_device *pdev)
373 struct drm_device *drm;
374 const struct of_device_id *of_id =
375 of_match_device(mxsfb_dt_ids, &pdev->dev);
376 int ret;
378 if (!pdev->dev.of_node)
379 return -ENODEV;
381 if (of_id)
382 pdev->id_entry = of_id->data;
384 drm = drm_dev_alloc(&mxsfb_driver, &pdev->dev);
385 if (IS_ERR(drm))
386 return PTR_ERR(drm);
388 ret = mxsfb_load(drm, 0);
389 if (ret)
390 goto err_free;
392 ret = drm_dev_register(drm, 0);
393 if (ret)
394 goto err_unload;
396 return 0;
398 err_unload:
399 mxsfb_unload(drm);
400 err_free:
401 drm_dev_unref(drm);
403 return ret;
406 static int mxsfb_remove(struct platform_device *pdev)
408 struct drm_device *drm = platform_get_drvdata(pdev);
410 drm_dev_unregister(drm);
411 mxsfb_unload(drm);
412 drm_dev_unref(drm);
414 return 0;
417 static struct platform_driver mxsfb_platform_driver = {
418 .probe = mxsfb_probe,
419 .remove = mxsfb_remove,
420 .id_table = mxsfb_devtype,
421 .driver = {
422 .name = "mxsfb",
423 .of_match_table = mxsfb_dt_ids,
427 module_platform_driver(mxsfb_platform_driver);
429 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
430 MODULE_DESCRIPTION("Freescale MXS DRM/KMS driver");
431 MODULE_LICENSE("GPL");