treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / gpu / drm / mxsfb / mxsfb_drv.c
blob497cf443a9afa9f030bd8e659aff303a59528a9d
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2016 Marek Vasut <marex@denx.de>
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 */
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>
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>
38 #include "mxsfb_drv.h"
39 #include "mxsfb_regs.h"
41 enum mxsfb_devtype {
42 MXSFB_V3,
43 MXSFB_V4,
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,
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,
67 static const uint32_t mxsfb_formats[] = {
68 DRM_FORMAT_XRGB8888,
69 DRM_FORMAT_RGB565
72 static struct mxsfb_drm_private *
73 drm_pipe_to_mxsfb_drm_private(struct drm_simple_display_pipe *pipe)
75 return container_of(pipe, struct mxsfb_drm_private, pipe);
78 void mxsfb_enable_axi_clk(struct mxsfb_drm_private *mxsfb)
80 if (mxsfb->clk_axi)
81 clk_prepare_enable(mxsfb->clk_axi);
84 void mxsfb_disable_axi_clk(struct mxsfb_drm_private *mxsfb)
86 if (mxsfb->clk_axi)
87 clk_disable_unprepare(mxsfb->clk_axi);
90 static const struct drm_mode_config_funcs mxsfb_mode_config_funcs = {
91 .fb_create = drm_gem_fb_create,
92 .atomic_check = drm_atomic_helper_check,
93 .atomic_commit = drm_atomic_helper_commit,
96 static const struct drm_mode_config_helper_funcs mxsfb_mode_config_helpers = {
97 .atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
100 static void mxsfb_pipe_enable(struct drm_simple_display_pipe *pipe,
101 struct drm_crtc_state *crtc_state,
102 struct drm_plane_state *plane_state)
104 struct drm_connector *connector;
105 struct mxsfb_drm_private *mxsfb = drm_pipe_to_mxsfb_drm_private(pipe);
106 struct drm_device *drm = pipe->plane.dev;
108 if (!mxsfb->connector) {
109 list_for_each_entry(connector,
110 &drm->mode_config.connector_list,
111 head)
112 if (connector->encoder == &mxsfb->pipe.encoder) {
113 mxsfb->connector = connector;
114 break;
118 if (!mxsfb->connector) {
119 dev_warn(drm->dev, "No connector attached, using default\n");
120 mxsfb->connector = &mxsfb->panel_connector;
123 pm_runtime_get_sync(drm->dev);
124 drm_panel_prepare(mxsfb->panel);
125 mxsfb_crtc_enable(mxsfb);
126 drm_panel_enable(mxsfb->panel);
129 static void mxsfb_pipe_disable(struct drm_simple_display_pipe *pipe)
131 struct mxsfb_drm_private *mxsfb = drm_pipe_to_mxsfb_drm_private(pipe);
132 struct drm_device *drm = pipe->plane.dev;
133 struct drm_crtc *crtc = &pipe->crtc;
134 struct drm_pending_vblank_event *event;
136 drm_panel_disable(mxsfb->panel);
137 mxsfb_crtc_disable(mxsfb);
138 drm_panel_unprepare(mxsfb->panel);
139 pm_runtime_put_sync(drm->dev);
141 spin_lock_irq(&drm->event_lock);
142 event = crtc->state->event;
143 if (event) {
144 crtc->state->event = NULL;
145 drm_crtc_send_vblank_event(crtc, event);
147 spin_unlock_irq(&drm->event_lock);
149 if (mxsfb->connector != &mxsfb->panel_connector)
150 mxsfb->connector = NULL;
153 static void mxsfb_pipe_update(struct drm_simple_display_pipe *pipe,
154 struct drm_plane_state *plane_state)
156 struct mxsfb_drm_private *mxsfb = drm_pipe_to_mxsfb_drm_private(pipe);
158 mxsfb_plane_atomic_update(mxsfb, plane_state);
161 static int mxsfb_pipe_enable_vblank(struct drm_simple_display_pipe *pipe)
163 struct mxsfb_drm_private *mxsfb = drm_pipe_to_mxsfb_drm_private(pipe);
165 /* Clear and enable VBLANK IRQ */
166 mxsfb_enable_axi_clk(mxsfb);
167 writel(CTRL1_CUR_FRAME_DONE_IRQ, mxsfb->base + LCDC_CTRL1 + REG_CLR);
168 writel(CTRL1_CUR_FRAME_DONE_IRQ_EN, mxsfb->base + LCDC_CTRL1 + REG_SET);
169 mxsfb_disable_axi_clk(mxsfb);
171 return 0;
174 static void mxsfb_pipe_disable_vblank(struct drm_simple_display_pipe *pipe)
176 struct mxsfb_drm_private *mxsfb = drm_pipe_to_mxsfb_drm_private(pipe);
178 /* Disable and clear VBLANK IRQ */
179 mxsfb_enable_axi_clk(mxsfb);
180 writel(CTRL1_CUR_FRAME_DONE_IRQ_EN, mxsfb->base + LCDC_CTRL1 + REG_CLR);
181 writel(CTRL1_CUR_FRAME_DONE_IRQ, mxsfb->base + LCDC_CTRL1 + REG_CLR);
182 mxsfb_disable_axi_clk(mxsfb);
185 static struct drm_simple_display_pipe_funcs mxsfb_funcs = {
186 .enable = mxsfb_pipe_enable,
187 .disable = mxsfb_pipe_disable,
188 .update = mxsfb_pipe_update,
189 .prepare_fb = drm_gem_fb_simple_display_pipe_prepare_fb,
190 .enable_vblank = mxsfb_pipe_enable_vblank,
191 .disable_vblank = mxsfb_pipe_disable_vblank,
194 static int mxsfb_load(struct drm_device *drm, unsigned long flags)
196 struct platform_device *pdev = to_platform_device(drm->dev);
197 struct mxsfb_drm_private *mxsfb;
198 struct resource *res;
199 int ret;
201 mxsfb = devm_kzalloc(&pdev->dev, sizeof(*mxsfb), GFP_KERNEL);
202 if (!mxsfb)
203 return -ENOMEM;
205 drm->dev_private = mxsfb;
206 mxsfb->devdata = &mxsfb_devdata[pdev->id_entry->driver_data];
208 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
209 mxsfb->base = devm_ioremap_resource(drm->dev, res);
210 if (IS_ERR(mxsfb->base))
211 return PTR_ERR(mxsfb->base);
213 mxsfb->clk = devm_clk_get(drm->dev, NULL);
214 if (IS_ERR(mxsfb->clk))
215 return PTR_ERR(mxsfb->clk);
217 mxsfb->clk_axi = devm_clk_get(drm->dev, "axi");
218 if (IS_ERR(mxsfb->clk_axi))
219 mxsfb->clk_axi = NULL;
221 mxsfb->clk_disp_axi = devm_clk_get(drm->dev, "disp_axi");
222 if (IS_ERR(mxsfb->clk_disp_axi))
223 mxsfb->clk_disp_axi = NULL;
225 ret = dma_set_mask_and_coherent(drm->dev, DMA_BIT_MASK(32));
226 if (ret)
227 return ret;
229 pm_runtime_enable(drm->dev);
231 ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
232 if (ret < 0) {
233 dev_err(drm->dev, "Failed to initialise vblank\n");
234 goto err_vblank;
237 /* Modeset init */
238 drm_mode_config_init(drm);
240 ret = mxsfb_create_output(drm);
241 if (ret < 0) {
242 dev_err(drm->dev, "Failed to create outputs\n");
243 goto err_vblank;
246 ret = drm_simple_display_pipe_init(drm, &mxsfb->pipe, &mxsfb_funcs,
247 mxsfb_formats, ARRAY_SIZE(mxsfb_formats), NULL,
248 mxsfb->connector);
249 if (ret < 0) {
250 dev_err(drm->dev, "Cannot setup simple display pipe\n");
251 goto err_vblank;
255 * Attach panel only if there is one.
256 * If there is no panel attach, it must be a bridge. In this case, we
257 * need a reference to its connector for a proper initialization.
258 * We will do this check in pipe->enable(), since the connector won't
259 * be attached to an encoder until then.
262 if (mxsfb->panel) {
263 ret = drm_panel_attach(mxsfb->panel, mxsfb->connector);
264 if (ret) {
265 dev_err(drm->dev, "Cannot connect panel: %d\n", ret);
266 goto err_vblank;
268 } else if (mxsfb->bridge) {
269 ret = drm_simple_display_pipe_attach_bridge(&mxsfb->pipe,
270 mxsfb->bridge);
271 if (ret) {
272 dev_err(drm->dev, "Cannot connect bridge: %d\n", ret);
273 goto err_vblank;
277 drm->mode_config.min_width = MXSFB_MIN_XRES;
278 drm->mode_config.min_height = MXSFB_MIN_YRES;
279 drm->mode_config.max_width = MXSFB_MAX_XRES;
280 drm->mode_config.max_height = MXSFB_MAX_YRES;
281 drm->mode_config.funcs = &mxsfb_mode_config_funcs;
282 drm->mode_config.helper_private = &mxsfb_mode_config_helpers;
284 drm_mode_config_reset(drm);
286 pm_runtime_get_sync(drm->dev);
287 ret = drm_irq_install(drm, platform_get_irq(pdev, 0));
288 pm_runtime_put_sync(drm->dev);
290 if (ret < 0) {
291 dev_err(drm->dev, "Failed to install IRQ handler\n");
292 goto err_irq;
295 drm_kms_helper_poll_init(drm);
297 platform_set_drvdata(pdev, drm);
299 drm_helper_hpd_irq_event(drm);
301 return 0;
303 err_irq:
304 drm_panel_detach(mxsfb->panel);
305 err_vblank:
306 pm_runtime_disable(drm->dev);
308 return ret;
311 static void mxsfb_unload(struct drm_device *drm)
313 drm_kms_helper_poll_fini(drm);
314 drm_mode_config_cleanup(drm);
316 pm_runtime_get_sync(drm->dev);
317 drm_irq_uninstall(drm);
318 pm_runtime_put_sync(drm->dev);
320 drm->dev_private = NULL;
322 pm_runtime_disable(drm->dev);
325 static void mxsfb_irq_preinstall(struct drm_device *drm)
327 struct mxsfb_drm_private *mxsfb = drm->dev_private;
329 mxsfb_pipe_disable_vblank(&mxsfb->pipe);
332 static irqreturn_t mxsfb_irq_handler(int irq, void *data)
334 struct drm_device *drm = data;
335 struct mxsfb_drm_private *mxsfb = drm->dev_private;
336 u32 reg;
338 mxsfb_enable_axi_clk(mxsfb);
340 reg = readl(mxsfb->base + LCDC_CTRL1);
342 if (reg & CTRL1_CUR_FRAME_DONE_IRQ)
343 drm_crtc_handle_vblank(&mxsfb->pipe.crtc);
345 writel(CTRL1_CUR_FRAME_DONE_IRQ, mxsfb->base + LCDC_CTRL1 + REG_CLR);
347 mxsfb_disable_axi_clk(mxsfb);
349 return IRQ_HANDLED;
352 DEFINE_DRM_GEM_CMA_FOPS(fops);
354 static struct drm_driver mxsfb_driver = {
355 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
356 .irq_handler = mxsfb_irq_handler,
357 .irq_preinstall = mxsfb_irq_preinstall,
358 .irq_uninstall = mxsfb_irq_preinstall,
359 .gem_free_object_unlocked = drm_gem_cma_free_object,
360 .gem_vm_ops = &drm_gem_cma_vm_ops,
361 .dumb_create = drm_gem_cma_dumb_create,
362 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
363 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
364 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
365 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
366 .gem_prime_vmap = drm_gem_cma_prime_vmap,
367 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
368 .gem_prime_mmap = drm_gem_cma_prime_mmap,
369 .fops = &fops,
370 .name = "mxsfb-drm",
371 .desc = "MXSFB Controller DRM",
372 .date = "20160824",
373 .major = 1,
374 .minor = 0,
377 static const struct platform_device_id mxsfb_devtype[] = {
378 { .name = "imx23-fb", .driver_data = MXSFB_V3, },
379 { .name = "imx28-fb", .driver_data = MXSFB_V4, },
380 { .name = "imx6sx-fb", .driver_data = MXSFB_V4, },
381 { /* sentinel */ }
383 MODULE_DEVICE_TABLE(platform, mxsfb_devtype);
385 static const struct of_device_id mxsfb_dt_ids[] = {
386 { .compatible = "fsl,imx23-lcdif", .data = &mxsfb_devtype[0], },
387 { .compatible = "fsl,imx28-lcdif", .data = &mxsfb_devtype[1], },
388 { .compatible = "fsl,imx6sx-lcdif", .data = &mxsfb_devtype[2], },
389 { /* sentinel */ }
391 MODULE_DEVICE_TABLE(of, mxsfb_dt_ids);
393 static int mxsfb_probe(struct platform_device *pdev)
395 struct drm_device *drm;
396 const struct of_device_id *of_id =
397 of_match_device(mxsfb_dt_ids, &pdev->dev);
398 int ret;
400 if (!pdev->dev.of_node)
401 return -ENODEV;
403 if (of_id)
404 pdev->id_entry = of_id->data;
406 drm = drm_dev_alloc(&mxsfb_driver, &pdev->dev);
407 if (IS_ERR(drm))
408 return PTR_ERR(drm);
410 ret = mxsfb_load(drm, 0);
411 if (ret)
412 goto err_free;
414 ret = drm_dev_register(drm, 0);
415 if (ret)
416 goto err_unload;
418 drm_fbdev_generic_setup(drm, 32);
420 return 0;
422 err_unload:
423 mxsfb_unload(drm);
424 err_free:
425 drm_dev_put(drm);
427 return ret;
430 static int mxsfb_remove(struct platform_device *pdev)
432 struct drm_device *drm = platform_get_drvdata(pdev);
434 drm_dev_unregister(drm);
435 mxsfb_unload(drm);
436 drm_dev_put(drm);
438 return 0;
441 #ifdef CONFIG_PM_SLEEP
442 static int mxsfb_suspend(struct device *dev)
444 struct drm_device *drm = dev_get_drvdata(dev);
446 return drm_mode_config_helper_suspend(drm);
449 static int mxsfb_resume(struct device *dev)
451 struct drm_device *drm = dev_get_drvdata(dev);
453 return drm_mode_config_helper_resume(drm);
455 #endif
457 static const struct dev_pm_ops mxsfb_pm_ops = {
458 SET_SYSTEM_SLEEP_PM_OPS(mxsfb_suspend, mxsfb_resume)
461 static struct platform_driver mxsfb_platform_driver = {
462 .probe = mxsfb_probe,
463 .remove = mxsfb_remove,
464 .id_table = mxsfb_devtype,
465 .driver = {
466 .name = "mxsfb",
467 .of_match_table = mxsfb_dt_ids,
468 .pm = &mxsfb_pm_ops,
472 module_platform_driver(mxsfb_platform_driver);
474 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
475 MODULE_DESCRIPTION("Freescale MXS DRM/KMS driver");
476 MODULE_LICENSE("GPL");