dt-bindings: mtd: ingenic: Use standard ecc-engine property
[linux/fpc-iii.git] / drivers / gpu / drm / mxsfb / mxsfb_drv.c
blob967379f3f571995f89393742dae4d0f559c7f834
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_fb_helper.h>
35 #include <drm/drm_fb_cma_helper.h>
36 #include <drm/drm_gem_cma_helper.h>
37 #include <drm/drm_gem_framebuffer_helper.h>
38 #include <drm/drm_of.h>
39 #include <drm/drm_panel.h>
40 #include <drm/drm_probe_helper.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 const struct drm_mode_config_helper_funcs mxsfb_mode_config_helpers = {
102 .atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
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)
109 struct mxsfb_drm_private *mxsfb = drm_pipe_to_mxsfb_drm_private(pipe);
110 struct drm_device *drm = pipe->plane.dev;
112 pm_runtime_get_sync(drm->dev);
113 drm_panel_prepare(mxsfb->panel);
114 mxsfb_crtc_enable(mxsfb);
115 drm_panel_enable(mxsfb->panel);
118 static void mxsfb_pipe_disable(struct drm_simple_display_pipe *pipe)
120 struct mxsfb_drm_private *mxsfb = drm_pipe_to_mxsfb_drm_private(pipe);
121 struct drm_device *drm = pipe->plane.dev;
122 struct drm_crtc *crtc = &pipe->crtc;
123 struct drm_pending_vblank_event *event;
125 drm_panel_disable(mxsfb->panel);
126 mxsfb_crtc_disable(mxsfb);
127 drm_panel_unprepare(mxsfb->panel);
128 pm_runtime_put_sync(drm->dev);
130 spin_lock_irq(&drm->event_lock);
131 event = crtc->state->event;
132 if (event) {
133 crtc->state->event = NULL;
134 drm_crtc_send_vblank_event(crtc, event);
136 spin_unlock_irq(&drm->event_lock);
139 static void mxsfb_pipe_update(struct drm_simple_display_pipe *pipe,
140 struct drm_plane_state *plane_state)
142 struct mxsfb_drm_private *mxsfb = drm_pipe_to_mxsfb_drm_private(pipe);
144 mxsfb_plane_atomic_update(mxsfb, plane_state);
147 static int mxsfb_pipe_enable_vblank(struct drm_simple_display_pipe *pipe)
149 struct mxsfb_drm_private *mxsfb = drm_pipe_to_mxsfb_drm_private(pipe);
151 /* Clear and enable VBLANK IRQ */
152 mxsfb_enable_axi_clk(mxsfb);
153 writel(CTRL1_CUR_FRAME_DONE_IRQ, mxsfb->base + LCDC_CTRL1 + REG_CLR);
154 writel(CTRL1_CUR_FRAME_DONE_IRQ_EN, mxsfb->base + LCDC_CTRL1 + REG_SET);
155 mxsfb_disable_axi_clk(mxsfb);
157 return 0;
160 static void mxsfb_pipe_disable_vblank(struct drm_simple_display_pipe *pipe)
162 struct mxsfb_drm_private *mxsfb = drm_pipe_to_mxsfb_drm_private(pipe);
164 /* Disable and clear VBLANK IRQ */
165 mxsfb_enable_axi_clk(mxsfb);
166 writel(CTRL1_CUR_FRAME_DONE_IRQ_EN, mxsfb->base + LCDC_CTRL1 + REG_CLR);
167 writel(CTRL1_CUR_FRAME_DONE_IRQ, mxsfb->base + LCDC_CTRL1 + REG_CLR);
168 mxsfb_disable_axi_clk(mxsfb);
171 static struct drm_simple_display_pipe_funcs mxsfb_funcs = {
172 .enable = mxsfb_pipe_enable,
173 .disable = mxsfb_pipe_disable,
174 .update = mxsfb_pipe_update,
175 .prepare_fb = drm_gem_fb_simple_display_pipe_prepare_fb,
176 .enable_vblank = mxsfb_pipe_enable_vblank,
177 .disable_vblank = mxsfb_pipe_disable_vblank,
180 static int mxsfb_load(struct drm_device *drm, unsigned long flags)
182 struct platform_device *pdev = to_platform_device(drm->dev);
183 struct mxsfb_drm_private *mxsfb;
184 struct resource *res;
185 int ret;
187 mxsfb = devm_kzalloc(&pdev->dev, sizeof(*mxsfb), GFP_KERNEL);
188 if (!mxsfb)
189 return -ENOMEM;
191 drm->dev_private = mxsfb;
192 mxsfb->devdata = &mxsfb_devdata[pdev->id_entry->driver_data];
194 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
195 mxsfb->base = devm_ioremap_resource(drm->dev, res);
196 if (IS_ERR(mxsfb->base))
197 return PTR_ERR(mxsfb->base);
199 mxsfb->clk = devm_clk_get(drm->dev, NULL);
200 if (IS_ERR(mxsfb->clk))
201 return PTR_ERR(mxsfb->clk);
203 mxsfb->clk_axi = devm_clk_get(drm->dev, "axi");
204 if (IS_ERR(mxsfb->clk_axi))
205 mxsfb->clk_axi = NULL;
207 mxsfb->clk_disp_axi = devm_clk_get(drm->dev, "disp_axi");
208 if (IS_ERR(mxsfb->clk_disp_axi))
209 mxsfb->clk_disp_axi = NULL;
211 ret = dma_set_mask_and_coherent(drm->dev, DMA_BIT_MASK(32));
212 if (ret)
213 return ret;
215 pm_runtime_enable(drm->dev);
217 ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
218 if (ret < 0) {
219 dev_err(drm->dev, "Failed to initialise vblank\n");
220 goto err_vblank;
223 /* Modeset init */
224 drm_mode_config_init(drm);
226 ret = mxsfb_create_output(drm);
227 if (ret < 0) {
228 dev_err(drm->dev, "Failed to create outputs\n");
229 goto err_vblank;
232 ret = drm_simple_display_pipe_init(drm, &mxsfb->pipe, &mxsfb_funcs,
233 mxsfb_formats, ARRAY_SIZE(mxsfb_formats), NULL,
234 &mxsfb->connector);
235 if (ret < 0) {
236 dev_err(drm->dev, "Cannot setup simple display pipe\n");
237 goto err_vblank;
240 ret = drm_panel_attach(mxsfb->panel, &mxsfb->connector);
241 if (ret) {
242 dev_err(drm->dev, "Cannot connect panel\n");
243 goto err_vblank;
246 drm->mode_config.min_width = MXSFB_MIN_XRES;
247 drm->mode_config.min_height = MXSFB_MIN_YRES;
248 drm->mode_config.max_width = MXSFB_MAX_XRES;
249 drm->mode_config.max_height = MXSFB_MAX_YRES;
250 drm->mode_config.funcs = &mxsfb_mode_config_funcs;
251 drm->mode_config.helper_private = &mxsfb_mode_config_helpers;
253 drm_mode_config_reset(drm);
255 pm_runtime_get_sync(drm->dev);
256 ret = drm_irq_install(drm, platform_get_irq(pdev, 0));
257 pm_runtime_put_sync(drm->dev);
259 if (ret < 0) {
260 dev_err(drm->dev, "Failed to install IRQ handler\n");
261 goto err_irq;
264 drm_kms_helper_poll_init(drm);
266 platform_set_drvdata(pdev, drm);
268 drm_helper_hpd_irq_event(drm);
270 return 0;
272 err_irq:
273 drm_panel_detach(mxsfb->panel);
274 err_vblank:
275 pm_runtime_disable(drm->dev);
277 return ret;
280 static void mxsfb_unload(struct drm_device *drm)
282 drm_kms_helper_poll_fini(drm);
283 drm_mode_config_cleanup(drm);
285 pm_runtime_get_sync(drm->dev);
286 drm_irq_uninstall(drm);
287 pm_runtime_put_sync(drm->dev);
289 drm->dev_private = NULL;
291 pm_runtime_disable(drm->dev);
294 static void mxsfb_irq_preinstall(struct drm_device *drm)
296 struct mxsfb_drm_private *mxsfb = drm->dev_private;
298 mxsfb_pipe_disable_vblank(&mxsfb->pipe);
301 static irqreturn_t mxsfb_irq_handler(int irq, void *data)
303 struct drm_device *drm = data;
304 struct mxsfb_drm_private *mxsfb = drm->dev_private;
305 u32 reg;
307 mxsfb_enable_axi_clk(mxsfb);
309 reg = readl(mxsfb->base + LCDC_CTRL1);
311 if (reg & CTRL1_CUR_FRAME_DONE_IRQ)
312 drm_crtc_handle_vblank(&mxsfb->pipe.crtc);
314 writel(CTRL1_CUR_FRAME_DONE_IRQ, mxsfb->base + LCDC_CTRL1 + REG_CLR);
316 mxsfb_disable_axi_clk(mxsfb);
318 return IRQ_HANDLED;
321 DEFINE_DRM_GEM_CMA_FOPS(fops);
323 static struct drm_driver mxsfb_driver = {
324 .driver_features = DRIVER_GEM | DRIVER_MODESET |
325 DRIVER_PRIME | DRIVER_ATOMIC,
326 .irq_handler = mxsfb_irq_handler,
327 .irq_preinstall = mxsfb_irq_preinstall,
328 .irq_uninstall = mxsfb_irq_preinstall,
329 .gem_free_object_unlocked = drm_gem_cma_free_object,
330 .gem_vm_ops = &drm_gem_cma_vm_ops,
331 .dumb_create = drm_gem_cma_dumb_create,
332 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
333 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
334 .gem_prime_export = drm_gem_prime_export,
335 .gem_prime_import = drm_gem_prime_import,
336 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
337 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
338 .gem_prime_vmap = drm_gem_cma_prime_vmap,
339 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
340 .gem_prime_mmap = drm_gem_cma_prime_mmap,
341 .fops = &fops,
342 .name = "mxsfb-drm",
343 .desc = "MXSFB Controller DRM",
344 .date = "20160824",
345 .major = 1,
346 .minor = 0,
349 static const struct platform_device_id mxsfb_devtype[] = {
350 { .name = "imx23-fb", .driver_data = MXSFB_V3, },
351 { .name = "imx28-fb", .driver_data = MXSFB_V4, },
352 { .name = "imx6sx-fb", .driver_data = MXSFB_V4, },
353 { /* sentinel */ }
355 MODULE_DEVICE_TABLE(platform, mxsfb_devtype);
357 static const struct of_device_id mxsfb_dt_ids[] = {
358 { .compatible = "fsl,imx23-lcdif", .data = &mxsfb_devtype[0], },
359 { .compatible = "fsl,imx28-lcdif", .data = &mxsfb_devtype[1], },
360 { .compatible = "fsl,imx6sx-lcdif", .data = &mxsfb_devtype[2], },
361 { /* sentinel */ }
363 MODULE_DEVICE_TABLE(of, mxsfb_dt_ids);
365 static int mxsfb_probe(struct platform_device *pdev)
367 struct drm_device *drm;
368 const struct of_device_id *of_id =
369 of_match_device(mxsfb_dt_ids, &pdev->dev);
370 int ret;
372 if (!pdev->dev.of_node)
373 return -ENODEV;
375 if (of_id)
376 pdev->id_entry = of_id->data;
378 drm = drm_dev_alloc(&mxsfb_driver, &pdev->dev);
379 if (IS_ERR(drm))
380 return PTR_ERR(drm);
382 ret = mxsfb_load(drm, 0);
383 if (ret)
384 goto err_free;
386 ret = drm_dev_register(drm, 0);
387 if (ret)
388 goto err_unload;
390 drm_fbdev_generic_setup(drm, 32);
392 return 0;
394 err_unload:
395 mxsfb_unload(drm);
396 err_free:
397 drm_dev_put(drm);
399 return ret;
402 static int mxsfb_remove(struct platform_device *pdev)
404 struct drm_device *drm = platform_get_drvdata(pdev);
406 drm_dev_unregister(drm);
407 mxsfb_unload(drm);
408 drm_dev_put(drm);
410 return 0;
413 #ifdef CONFIG_PM_SLEEP
414 static int mxsfb_suspend(struct device *dev)
416 struct drm_device *drm = dev_get_drvdata(dev);
418 return drm_mode_config_helper_suspend(drm);
421 static int mxsfb_resume(struct device *dev)
423 struct drm_device *drm = dev_get_drvdata(dev);
425 return drm_mode_config_helper_resume(drm);
427 #endif
429 static const struct dev_pm_ops mxsfb_pm_ops = {
430 SET_SYSTEM_SLEEP_PM_OPS(mxsfb_suspend, mxsfb_resume)
433 static struct platform_driver mxsfb_platform_driver = {
434 .probe = mxsfb_probe,
435 .remove = mxsfb_remove,
436 .id_table = mxsfb_devtype,
437 .driver = {
438 .name = "mxsfb",
439 .of_match_table = mxsfb_dt_ids,
440 .pm = &mxsfb_pm_ops,
444 module_platform_driver(mxsfb_platform_driver);
446 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
447 MODULE_DESCRIPTION("Freescale MXS DRM/KMS driver");
448 MODULE_LICENSE("GPL");