1 // SPDX-License-Identifier: GPL-2.0-or-later
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.
11 #include <linux/clk.h>
12 #include <linux/dma-mapping.h>
14 #include <linux/module.h>
15 #include <linux/of_device.h>
16 #include <linux/platform_device.h>
17 #include <linux/pm_runtime.h>
19 #include <drm/drm_atomic_helper.h>
20 #include <drm/drm_bridge.h>
21 #include <drm/drm_connector.h>
22 #include <drm/drm_drv.h>
23 #include <drm/drm_fb_helper.h>
24 #include <drm/drm_fourcc.h>
25 #include <drm/drm_gem_cma_helper.h>
26 #include <drm/drm_gem_framebuffer_helper.h>
27 #include <drm/drm_irq.h>
28 #include <drm/drm_mode_config.h>
29 #include <drm/drm_of.h>
30 #include <drm/drm_probe_helper.h>
31 #include <drm/drm_vblank.h>
33 #include "mxsfb_drv.h"
34 #include "mxsfb_regs.h"
40 * Starting at i.MX6 the hardware version register is gone, use the
41 * i.MX family number as the version.
46 static const struct mxsfb_devdata mxsfb_devdata[] = {
48 .transfer_count = LCDC_V3_TRANSFER_COUNT,
49 .cur_buf = LCDC_V3_CUR_BUF,
50 .next_buf = LCDC_V3_NEXT_BUF,
56 .transfer_count = LCDC_V4_TRANSFER_COUNT,
57 .cur_buf = LCDC_V4_CUR_BUF,
58 .next_buf = LCDC_V4_NEXT_BUF,
59 .hs_wdth_mask = 0x3fff,
64 .transfer_count = LCDC_V4_TRANSFER_COUNT,
65 .cur_buf = LCDC_V4_CUR_BUF,
66 .next_buf = LCDC_V4_NEXT_BUF,
67 .hs_wdth_mask = 0x3fff,
73 void mxsfb_enable_axi_clk(struct mxsfb_drm_private *mxsfb)
76 clk_prepare_enable(mxsfb->clk_axi);
79 void mxsfb_disable_axi_clk(struct mxsfb_drm_private *mxsfb)
82 clk_disable_unprepare(mxsfb->clk_axi);
85 static struct drm_framebuffer *
86 mxsfb_fb_create(struct drm_device *dev, struct drm_file *file_priv,
87 const struct drm_mode_fb_cmd2 *mode_cmd)
89 const struct drm_format_info *info;
91 info = drm_get_format_info(dev, mode_cmd);
93 return ERR_PTR(-EINVAL);
95 if (mode_cmd->width * info->cpp[0] != mode_cmd->pitches[0]) {
96 dev_dbg(dev->dev, "Invalid pitch: fb width must match pitch\n");
97 return ERR_PTR(-EINVAL);
100 return drm_gem_fb_create(dev, file_priv, mode_cmd);
103 static const struct drm_mode_config_funcs mxsfb_mode_config_funcs = {
104 .fb_create = mxsfb_fb_create,
105 .atomic_check = drm_atomic_helper_check,
106 .atomic_commit = drm_atomic_helper_commit,
109 static const struct drm_mode_config_helper_funcs mxsfb_mode_config_helpers = {
110 .atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
113 static int mxsfb_attach_bridge(struct mxsfb_drm_private *mxsfb)
115 struct drm_device *drm = mxsfb->drm;
116 struct drm_connector_list_iter iter;
117 struct drm_panel *panel;
118 struct drm_bridge *bridge;
121 ret = drm_of_find_panel_or_bridge(drm->dev->of_node, 0, 0, &panel,
127 bridge = devm_drm_panel_bridge_add_typed(drm->dev, panel,
128 DRM_MODE_CONNECTOR_DPI);
130 return PTR_ERR(bridge);
136 ret = drm_bridge_attach(&mxsfb->encoder, bridge, NULL, 0);
138 DRM_DEV_ERROR(drm->dev,
139 "failed to attach bridge: %d\n", ret);
143 mxsfb->bridge = bridge;
146 * Get hold of the connector. This is a bit of a hack, until the bridge
147 * API gives us bus flags and formats.
149 drm_connector_list_iter_begin(drm, &iter);
150 mxsfb->connector = drm_connector_list_iter_next(&iter);
151 drm_connector_list_iter_end(&iter);
156 static int mxsfb_load(struct drm_device *drm,
157 const struct mxsfb_devdata *devdata)
159 struct platform_device *pdev = to_platform_device(drm->dev);
160 struct mxsfb_drm_private *mxsfb;
161 struct resource *res;
164 mxsfb = devm_kzalloc(&pdev->dev, sizeof(*mxsfb), GFP_KERNEL);
169 drm->dev_private = mxsfb;
170 mxsfb->devdata = devdata;
172 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
173 mxsfb->base = devm_ioremap_resource(drm->dev, res);
174 if (IS_ERR(mxsfb->base))
175 return PTR_ERR(mxsfb->base);
177 mxsfb->clk = devm_clk_get(drm->dev, NULL);
178 if (IS_ERR(mxsfb->clk))
179 return PTR_ERR(mxsfb->clk);
181 mxsfb->clk_axi = devm_clk_get(drm->dev, "axi");
182 if (IS_ERR(mxsfb->clk_axi))
183 mxsfb->clk_axi = NULL;
185 mxsfb->clk_disp_axi = devm_clk_get(drm->dev, "disp_axi");
186 if (IS_ERR(mxsfb->clk_disp_axi))
187 mxsfb->clk_disp_axi = NULL;
189 ret = dma_set_mask_and_coherent(drm->dev, DMA_BIT_MASK(32));
193 pm_runtime_enable(drm->dev);
196 drm_mode_config_init(drm);
198 ret = mxsfb_kms_init(mxsfb);
200 dev_err(drm->dev, "Failed to initialize KMS pipeline\n");
204 ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
206 dev_err(drm->dev, "Failed to initialise vblank\n");
210 /* Start with vertical blanking interrupt reporting disabled. */
211 drm_crtc_vblank_off(&mxsfb->crtc);
213 ret = mxsfb_attach_bridge(mxsfb);
215 dev_err(drm->dev, "Cannot connect bridge: %d\n", ret);
219 drm->mode_config.min_width = MXSFB_MIN_XRES;
220 drm->mode_config.min_height = MXSFB_MIN_YRES;
221 drm->mode_config.max_width = MXSFB_MAX_XRES;
222 drm->mode_config.max_height = MXSFB_MAX_YRES;
223 drm->mode_config.funcs = &mxsfb_mode_config_funcs;
224 drm->mode_config.helper_private = &mxsfb_mode_config_helpers;
226 drm_mode_config_reset(drm);
228 pm_runtime_get_sync(drm->dev);
229 ret = drm_irq_install(drm, platform_get_irq(pdev, 0));
230 pm_runtime_put_sync(drm->dev);
233 dev_err(drm->dev, "Failed to install IRQ handler\n");
237 drm_kms_helper_poll_init(drm);
239 platform_set_drvdata(pdev, drm);
241 drm_helper_hpd_irq_event(drm);
246 pm_runtime_disable(drm->dev);
251 static void mxsfb_unload(struct drm_device *drm)
253 drm_kms_helper_poll_fini(drm);
254 drm_mode_config_cleanup(drm);
256 pm_runtime_get_sync(drm->dev);
257 drm_irq_uninstall(drm);
258 pm_runtime_put_sync(drm->dev);
260 drm->dev_private = NULL;
262 pm_runtime_disable(drm->dev);
265 static void mxsfb_irq_disable(struct drm_device *drm)
267 struct mxsfb_drm_private *mxsfb = drm->dev_private;
269 mxsfb_enable_axi_clk(mxsfb);
270 mxsfb->crtc.funcs->disable_vblank(&mxsfb->crtc);
271 mxsfb_disable_axi_clk(mxsfb);
274 static irqreturn_t mxsfb_irq_handler(int irq, void *data)
276 struct drm_device *drm = data;
277 struct mxsfb_drm_private *mxsfb = drm->dev_private;
280 reg = readl(mxsfb->base + LCDC_CTRL1);
282 if (reg & CTRL1_CUR_FRAME_DONE_IRQ)
283 drm_crtc_handle_vblank(&mxsfb->crtc);
285 writel(CTRL1_CUR_FRAME_DONE_IRQ, mxsfb->base + LCDC_CTRL1 + REG_CLR);
290 DEFINE_DRM_GEM_CMA_FOPS(fops);
292 static struct drm_driver mxsfb_driver = {
293 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
294 .irq_handler = mxsfb_irq_handler,
295 .irq_preinstall = mxsfb_irq_disable,
296 .irq_uninstall = mxsfb_irq_disable,
297 DRM_GEM_CMA_DRIVER_OPS,
300 .desc = "MXSFB Controller DRM",
306 static const struct of_device_id mxsfb_dt_ids[] = {
307 { .compatible = "fsl,imx23-lcdif", .data = &mxsfb_devdata[MXSFB_V3], },
308 { .compatible = "fsl,imx28-lcdif", .data = &mxsfb_devdata[MXSFB_V4], },
309 { .compatible = "fsl,imx6sx-lcdif", .data = &mxsfb_devdata[MXSFB_V6], },
312 MODULE_DEVICE_TABLE(of, mxsfb_dt_ids);
314 static int mxsfb_probe(struct platform_device *pdev)
316 struct drm_device *drm;
317 const struct of_device_id *of_id =
318 of_match_device(mxsfb_dt_ids, &pdev->dev);
321 if (!pdev->dev.of_node)
324 drm = drm_dev_alloc(&mxsfb_driver, &pdev->dev);
328 ret = mxsfb_load(drm, of_id->data);
332 ret = drm_dev_register(drm, 0);
336 drm_fbdev_generic_setup(drm, 32);
348 static int mxsfb_remove(struct platform_device *pdev)
350 struct drm_device *drm = platform_get_drvdata(pdev);
352 drm_dev_unregister(drm);
359 #ifdef CONFIG_PM_SLEEP
360 static int mxsfb_suspend(struct device *dev)
362 struct drm_device *drm = dev_get_drvdata(dev);
364 return drm_mode_config_helper_suspend(drm);
367 static int mxsfb_resume(struct device *dev)
369 struct drm_device *drm = dev_get_drvdata(dev);
371 return drm_mode_config_helper_resume(drm);
375 static const struct dev_pm_ops mxsfb_pm_ops = {
376 SET_SYSTEM_SLEEP_PM_OPS(mxsfb_suspend, mxsfb_resume)
379 static struct platform_driver mxsfb_platform_driver = {
380 .probe = mxsfb_probe,
381 .remove = mxsfb_remove,
384 .of_match_table = mxsfb_dt_ids,
389 module_platform_driver(mxsfb_platform_driver);
391 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
392 MODULE_DESCRIPTION("Freescale MXS DRM/KMS driver");
393 MODULE_LICENSE("GPL");