1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2022 Marek Vasut <marex@denx.de> 4 * 5 * This code is based on drivers/gpu/drm/mxsfb/mxsfb* 6 */ 7 8 #include <linux/clk.h> 9 #include <linux/dma-mapping.h> 10 #include <linux/io.h> 11 #include <linux/module.h> 12 #include <linux/of_device.h> 13 #include <linux/platform_device.h> 14 #include <linux/pm_runtime.h> 15 16 #include <drm/drm_atomic_helper.h> 17 #include <drm/drm_bridge.h> 18 #include <drm/drm_drv.h> 19 #include <drm/drm_fbdev_dma.h> 20 #include <drm/drm_gem_dma_helper.h> 21 #include <drm/drm_gem_framebuffer_helper.h> 22 #include <drm/drm_mode_config.h> 23 #include <drm/drm_module.h> 24 #include <drm/drm_of.h> 25 #include <drm/drm_probe_helper.h> 26 #include <drm/drm_vblank.h> 27 28 #include "lcdif_drv.h" 29 #include "lcdif_regs.h" 30 31 static const struct drm_mode_config_funcs lcdif_mode_config_funcs = { 32 .fb_create = drm_gem_fb_create, 33 .atomic_check = drm_atomic_helper_check, 34 .atomic_commit = drm_atomic_helper_commit, 35 }; 36 37 static const struct drm_mode_config_helper_funcs lcdif_mode_config_helpers = { 38 .atomic_commit_tail = drm_atomic_helper_commit_tail_rpm, 39 }; 40 41 static int lcdif_attach_bridge(struct lcdif_drm_private *lcdif) 42 { 43 struct drm_device *drm = lcdif->drm; 44 struct drm_bridge *bridge; 45 int ret; 46 47 bridge = devm_drm_of_get_bridge(drm->dev, drm->dev->of_node, 0, 0); 48 if (IS_ERR(bridge)) 49 return PTR_ERR(bridge); 50 51 ret = drm_bridge_attach(&lcdif->encoder, bridge, NULL, 0); 52 if (ret) 53 return dev_err_probe(drm->dev, ret, "Failed to attach bridge\n"); 54 55 lcdif->bridge = bridge; 56 57 return 0; 58 } 59 60 static irqreturn_t lcdif_irq_handler(int irq, void *data) 61 { 62 struct drm_device *drm = data; 63 struct lcdif_drm_private *lcdif = drm->dev_private; 64 u32 reg, stat; 65 66 stat = readl(lcdif->base + LCDC_V8_INT_STATUS_D0); 67 if (!stat) 68 return IRQ_NONE; 69 70 if (stat & INT_STATUS_D0_VS_BLANK) { 71 reg = readl(lcdif->base + LCDC_V8_CTRLDESCL0_5); 72 if (!(reg & CTRLDESCL0_5_SHADOW_LOAD_EN)) 73 drm_crtc_handle_vblank(&lcdif->crtc); 74 } 75 76 writel(stat, lcdif->base + LCDC_V8_INT_STATUS_D0); 77 78 return IRQ_HANDLED; 79 } 80 81 static int lcdif_load(struct drm_device *drm) 82 { 83 struct platform_device *pdev = to_platform_device(drm->dev); 84 struct lcdif_drm_private *lcdif; 85 struct resource *res; 86 int ret; 87 88 lcdif = devm_kzalloc(&pdev->dev, sizeof(*lcdif), GFP_KERNEL); 89 if (!lcdif) 90 return -ENOMEM; 91 92 lcdif->drm = drm; 93 drm->dev_private = lcdif; 94 95 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 96 lcdif->base = devm_ioremap_resource(drm->dev, res); 97 if (IS_ERR(lcdif->base)) 98 return PTR_ERR(lcdif->base); 99 100 lcdif->clk = devm_clk_get(drm->dev, "pix"); 101 if (IS_ERR(lcdif->clk)) 102 return PTR_ERR(lcdif->clk); 103 104 lcdif->clk_axi = devm_clk_get(drm->dev, "axi"); 105 if (IS_ERR(lcdif->clk_axi)) 106 return PTR_ERR(lcdif->clk_axi); 107 108 lcdif->clk_disp_axi = devm_clk_get(drm->dev, "disp_axi"); 109 if (IS_ERR(lcdif->clk_disp_axi)) 110 return PTR_ERR(lcdif->clk_disp_axi); 111 112 platform_set_drvdata(pdev, drm); 113 114 ret = dma_set_mask_and_coherent(drm->dev, DMA_BIT_MASK(36)); 115 if (ret) 116 return ret; 117 118 /* Modeset init */ 119 drm_mode_config_init(drm); 120 121 ret = lcdif_kms_init(lcdif); 122 if (ret < 0) { 123 dev_err(drm->dev, "Failed to initialize KMS pipeline\n"); 124 return ret; 125 } 126 127 ret = drm_vblank_init(drm, drm->mode_config.num_crtc); 128 if (ret < 0) { 129 dev_err(drm->dev, "Failed to initialise vblank\n"); 130 return ret; 131 } 132 133 /* Start with vertical blanking interrupt reporting disabled. */ 134 drm_crtc_vblank_off(&lcdif->crtc); 135 136 ret = lcdif_attach_bridge(lcdif); 137 if (ret) 138 return dev_err_probe(drm->dev, ret, "Cannot connect bridge\n"); 139 140 drm->mode_config.min_width = LCDIF_MIN_XRES; 141 drm->mode_config.min_height = LCDIF_MIN_YRES; 142 drm->mode_config.max_width = LCDIF_MAX_XRES; 143 drm->mode_config.max_height = LCDIF_MAX_YRES; 144 drm->mode_config.funcs = &lcdif_mode_config_funcs; 145 drm->mode_config.helper_private = &lcdif_mode_config_helpers; 146 147 drm_mode_config_reset(drm); 148 149 ret = platform_get_irq(pdev, 0); 150 if (ret < 0) 151 return ret; 152 lcdif->irq = ret; 153 154 ret = devm_request_irq(drm->dev, lcdif->irq, lcdif_irq_handler, 0, 155 drm->driver->name, drm); 156 if (ret < 0) { 157 dev_err(drm->dev, "Failed to install IRQ handler\n"); 158 return ret; 159 } 160 161 drm_kms_helper_poll_init(drm); 162 163 drm_helper_hpd_irq_event(drm); 164 165 pm_runtime_enable(drm->dev); 166 167 return 0; 168 } 169 170 static void lcdif_unload(struct drm_device *drm) 171 { 172 struct lcdif_drm_private *lcdif = drm->dev_private; 173 174 pm_runtime_get_sync(drm->dev); 175 176 drm_crtc_vblank_off(&lcdif->crtc); 177 178 drm_kms_helper_poll_fini(drm); 179 drm_mode_config_cleanup(drm); 180 181 pm_runtime_put_sync(drm->dev); 182 pm_runtime_disable(drm->dev); 183 184 drm->dev_private = NULL; 185 } 186 187 DEFINE_DRM_GEM_DMA_FOPS(fops); 188 189 static const struct drm_driver lcdif_driver = { 190 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC, 191 DRM_GEM_DMA_DRIVER_OPS, 192 .fops = &fops, 193 .name = "imx-lcdif", 194 .desc = "i.MX LCDIF Controller DRM", 195 .date = "20220417", 196 .major = 1, 197 .minor = 0, 198 }; 199 200 static const struct of_device_id lcdif_dt_ids[] = { 201 { .compatible = "fsl,imx8mp-lcdif" }, 202 { /* sentinel */ } 203 }; 204 MODULE_DEVICE_TABLE(of, lcdif_dt_ids); 205 206 static int lcdif_probe(struct platform_device *pdev) 207 { 208 struct drm_device *drm; 209 int ret; 210 211 drm = drm_dev_alloc(&lcdif_driver, &pdev->dev); 212 if (IS_ERR(drm)) 213 return PTR_ERR(drm); 214 215 ret = lcdif_load(drm); 216 if (ret) 217 goto err_free; 218 219 ret = drm_dev_register(drm, 0); 220 if (ret) 221 goto err_unload; 222 223 drm_fbdev_dma_setup(drm, 32); 224 225 return 0; 226 227 err_unload: 228 lcdif_unload(drm); 229 err_free: 230 drm_dev_put(drm); 231 232 return ret; 233 } 234 235 static int lcdif_remove(struct platform_device *pdev) 236 { 237 struct drm_device *drm = platform_get_drvdata(pdev); 238 239 drm_dev_unregister(drm); 240 drm_atomic_helper_shutdown(drm); 241 lcdif_unload(drm); 242 drm_dev_put(drm); 243 244 return 0; 245 } 246 247 static void lcdif_shutdown(struct platform_device *pdev) 248 { 249 struct drm_device *drm = platform_get_drvdata(pdev); 250 251 drm_atomic_helper_shutdown(drm); 252 } 253 254 static int __maybe_unused lcdif_rpm_suspend(struct device *dev) 255 { 256 struct drm_device *drm = dev_get_drvdata(dev); 257 struct lcdif_drm_private *lcdif = drm->dev_private; 258 259 /* These clock supply the DISPLAY CLOCK Domain */ 260 clk_disable_unprepare(lcdif->clk); 261 /* These clock supply the System Bus, AXI, Write Path, LFIFO */ 262 clk_disable_unprepare(lcdif->clk_disp_axi); 263 /* These clock supply the Control Bus, APB, APBH Ctrl Registers */ 264 clk_disable_unprepare(lcdif->clk_axi); 265 266 return 0; 267 } 268 269 static int __maybe_unused lcdif_rpm_resume(struct device *dev) 270 { 271 struct drm_device *drm = dev_get_drvdata(dev); 272 struct lcdif_drm_private *lcdif = drm->dev_private; 273 274 /* These clock supply the Control Bus, APB, APBH Ctrl Registers */ 275 clk_prepare_enable(lcdif->clk_axi); 276 /* These clock supply the System Bus, AXI, Write Path, LFIFO */ 277 clk_prepare_enable(lcdif->clk_disp_axi); 278 /* These clock supply the DISPLAY CLOCK Domain */ 279 clk_prepare_enable(lcdif->clk); 280 281 return 0; 282 } 283 284 static int __maybe_unused lcdif_suspend(struct device *dev) 285 { 286 struct drm_device *drm = dev_get_drvdata(dev); 287 int ret; 288 289 ret = drm_mode_config_helper_suspend(drm); 290 if (ret) 291 return ret; 292 293 return lcdif_rpm_suspend(dev); 294 } 295 296 static int __maybe_unused lcdif_resume(struct device *dev) 297 { 298 struct drm_device *drm = dev_get_drvdata(dev); 299 300 lcdif_rpm_resume(dev); 301 302 return drm_mode_config_helper_resume(drm); 303 } 304 305 static const struct dev_pm_ops lcdif_pm_ops = { 306 SET_SYSTEM_SLEEP_PM_OPS(lcdif_suspend, lcdif_resume) 307 SET_RUNTIME_PM_OPS(lcdif_rpm_suspend, lcdif_rpm_resume, NULL) 308 }; 309 310 static struct platform_driver lcdif_platform_driver = { 311 .probe = lcdif_probe, 312 .remove = lcdif_remove, 313 .shutdown = lcdif_shutdown, 314 .driver = { 315 .name = "imx-lcdif", 316 .of_match_table = lcdif_dt_ids, 317 .pm = &lcdif_pm_ops, 318 }, 319 }; 320 321 drm_module_platform_driver(lcdif_platform_driver); 322 323 MODULE_AUTHOR("Marek Vasut <marex@denx.de>"); 324 MODULE_DESCRIPTION("Freescale LCDIF DRM/KMS driver"); 325 MODULE_LICENSE("GPL"); 326