1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright 2015 Freescale Semiconductor, Inc. 4 * 5 * Freescale DCU drm device driver 6 */ 7 8 #include <linux/clk.h> 9 #include <linux/clk-provider.h> 10 #include <linux/console.h> 11 #include <linux/io.h> 12 #include <linux/mfd/syscon.h> 13 #include <linux/mm.h> 14 #include <linux/module.h> 15 #include <linux/of_platform.h> 16 #include <linux/platform_device.h> 17 #include <linux/pm.h> 18 #include <linux/pm_runtime.h> 19 #include <linux/regmap.h> 20 21 #include <drm/drm_atomic_helper.h> 22 #include <drm/drm_drv.h> 23 #include <drm/drm_fb_cma_helper.h> 24 #include <drm/drm_fb_helper.h> 25 #include <drm/drm_gem_cma_helper.h> 26 #include <drm/drm_modeset_helper.h> 27 #include <drm/drm_module.h> 28 #include <drm/drm_probe_helper.h> 29 #include <drm/drm_vblank.h> 30 31 #include "fsl_dcu_drm_crtc.h" 32 #include "fsl_dcu_drm_drv.h" 33 #include "fsl_tcon.h" 34 35 static int legacyfb_depth = 24; 36 module_param(legacyfb_depth, int, 0444); 37 38 static bool fsl_dcu_drm_is_volatile_reg(struct device *dev, unsigned int reg) 39 { 40 if (reg == DCU_INT_STATUS || reg == DCU_UPDATE_MODE) 41 return true; 42 43 return false; 44 } 45 46 static const struct regmap_config fsl_dcu_regmap_config = { 47 .reg_bits = 32, 48 .reg_stride = 4, 49 .val_bits = 32, 50 51 .volatile_reg = fsl_dcu_drm_is_volatile_reg, 52 }; 53 54 static void fsl_dcu_irq_reset(struct drm_device *dev) 55 { 56 struct fsl_dcu_drm_device *fsl_dev = dev->dev_private; 57 58 regmap_write(fsl_dev->regmap, DCU_INT_STATUS, ~0); 59 regmap_write(fsl_dev->regmap, DCU_INT_MASK, ~0); 60 } 61 62 static irqreturn_t fsl_dcu_drm_irq(int irq, void *arg) 63 { 64 struct drm_device *dev = arg; 65 struct fsl_dcu_drm_device *fsl_dev = dev->dev_private; 66 unsigned int int_status; 67 int ret; 68 69 ret = regmap_read(fsl_dev->regmap, DCU_INT_STATUS, &int_status); 70 if (ret) { 71 dev_err(dev->dev, "read DCU_INT_STATUS failed\n"); 72 return IRQ_NONE; 73 } 74 75 if (int_status & DCU_INT_STATUS_VBLANK) 76 drm_handle_vblank(dev, 0); 77 78 regmap_write(fsl_dev->regmap, DCU_INT_STATUS, int_status); 79 80 return IRQ_HANDLED; 81 } 82 83 static int fsl_dcu_irq_install(struct drm_device *dev, unsigned int irq) 84 { 85 if (irq == IRQ_NOTCONNECTED) 86 return -ENOTCONN; 87 88 fsl_dcu_irq_reset(dev); 89 90 return request_irq(irq, fsl_dcu_drm_irq, 0, dev->driver->name, dev); 91 } 92 93 static void fsl_dcu_irq_uninstall(struct drm_device *dev) 94 { 95 struct fsl_dcu_drm_device *fsl_dev = dev->dev_private; 96 97 fsl_dcu_irq_reset(dev); 98 free_irq(fsl_dev->irq, dev); 99 } 100 101 static int fsl_dcu_load(struct drm_device *dev, unsigned long flags) 102 { 103 struct fsl_dcu_drm_device *fsl_dev = dev->dev_private; 104 int ret; 105 106 ret = fsl_dcu_drm_modeset_init(fsl_dev); 107 if (ret < 0) { 108 dev_err(dev->dev, "failed to initialize mode setting\n"); 109 return ret; 110 } 111 112 ret = drm_vblank_init(dev, dev->mode_config.num_crtc); 113 if (ret < 0) { 114 dev_err(dev->dev, "failed to initialize vblank\n"); 115 goto done_vblank; 116 } 117 118 ret = fsl_dcu_irq_install(dev, fsl_dev->irq); 119 if (ret < 0) { 120 dev_err(dev->dev, "failed to install IRQ handler\n"); 121 goto done_irq; 122 } 123 124 if (legacyfb_depth != 16 && legacyfb_depth != 24 && 125 legacyfb_depth != 32) { 126 dev_warn(dev->dev, 127 "Invalid legacyfb_depth. Defaulting to 24bpp\n"); 128 legacyfb_depth = 24; 129 } 130 131 return 0; 132 done_irq: 133 drm_kms_helper_poll_fini(dev); 134 135 drm_mode_config_cleanup(dev); 136 done_vblank: 137 dev->dev_private = NULL; 138 139 return ret; 140 } 141 142 static void fsl_dcu_unload(struct drm_device *dev) 143 { 144 drm_atomic_helper_shutdown(dev); 145 drm_kms_helper_poll_fini(dev); 146 147 drm_mode_config_cleanup(dev); 148 fsl_dcu_irq_uninstall(dev); 149 150 dev->dev_private = NULL; 151 } 152 153 DEFINE_DRM_GEM_CMA_FOPS(fsl_dcu_drm_fops); 154 155 static const struct drm_driver fsl_dcu_drm_driver = { 156 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC, 157 .load = fsl_dcu_load, 158 .unload = fsl_dcu_unload, 159 DRM_GEM_CMA_DRIVER_OPS, 160 .fops = &fsl_dcu_drm_fops, 161 .name = "fsl-dcu-drm", 162 .desc = "Freescale DCU DRM", 163 .date = "20160425", 164 .major = 1, 165 .minor = 1, 166 }; 167 168 #ifdef CONFIG_PM_SLEEP 169 static int fsl_dcu_drm_pm_suspend(struct device *dev) 170 { 171 struct fsl_dcu_drm_device *fsl_dev = dev_get_drvdata(dev); 172 int ret; 173 174 if (!fsl_dev) 175 return 0; 176 177 disable_irq(fsl_dev->irq); 178 179 ret = drm_mode_config_helper_suspend(fsl_dev->drm); 180 if (ret) { 181 enable_irq(fsl_dev->irq); 182 return ret; 183 } 184 185 clk_disable_unprepare(fsl_dev->clk); 186 187 return 0; 188 } 189 190 static int fsl_dcu_drm_pm_resume(struct device *dev) 191 { 192 struct fsl_dcu_drm_device *fsl_dev = dev_get_drvdata(dev); 193 int ret; 194 195 if (!fsl_dev) 196 return 0; 197 198 ret = clk_prepare_enable(fsl_dev->clk); 199 if (ret < 0) { 200 dev_err(dev, "failed to enable dcu clk\n"); 201 return ret; 202 } 203 204 if (fsl_dev->tcon) 205 fsl_tcon_bypass_enable(fsl_dev->tcon); 206 fsl_dcu_drm_init_planes(fsl_dev->drm); 207 enable_irq(fsl_dev->irq); 208 209 drm_mode_config_helper_resume(fsl_dev->drm); 210 211 return 0; 212 } 213 #endif 214 215 static const struct dev_pm_ops fsl_dcu_drm_pm_ops = { 216 SET_SYSTEM_SLEEP_PM_OPS(fsl_dcu_drm_pm_suspend, fsl_dcu_drm_pm_resume) 217 }; 218 219 static const struct fsl_dcu_soc_data fsl_dcu_ls1021a_data = { 220 .name = "ls1021a", 221 .total_layer = 16, 222 .max_layer = 4, 223 .layer_regs = LS1021A_LAYER_REG_NUM, 224 }; 225 226 static const struct fsl_dcu_soc_data fsl_dcu_vf610_data = { 227 .name = "vf610", 228 .total_layer = 64, 229 .max_layer = 6, 230 .layer_regs = VF610_LAYER_REG_NUM, 231 }; 232 233 static const struct of_device_id fsl_dcu_of_match[] = { 234 { 235 .compatible = "fsl,ls1021a-dcu", 236 .data = &fsl_dcu_ls1021a_data, 237 }, { 238 .compatible = "fsl,vf610-dcu", 239 .data = &fsl_dcu_vf610_data, 240 }, { 241 }, 242 }; 243 MODULE_DEVICE_TABLE(of, fsl_dcu_of_match); 244 245 static int fsl_dcu_drm_probe(struct platform_device *pdev) 246 { 247 struct fsl_dcu_drm_device *fsl_dev; 248 struct drm_device *drm; 249 struct device *dev = &pdev->dev; 250 struct resource *res; 251 void __iomem *base; 252 struct clk *pix_clk_in; 253 char pix_clk_name[32]; 254 const char *pix_clk_in_name; 255 const struct of_device_id *id; 256 int ret; 257 u8 div_ratio_shift = 0; 258 259 fsl_dev = devm_kzalloc(dev, sizeof(*fsl_dev), GFP_KERNEL); 260 if (!fsl_dev) 261 return -ENOMEM; 262 263 id = of_match_node(fsl_dcu_of_match, pdev->dev.of_node); 264 if (!id) 265 return -ENODEV; 266 fsl_dev->soc = id->data; 267 268 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 269 base = devm_ioremap_resource(dev, res); 270 if (IS_ERR(base)) { 271 ret = PTR_ERR(base); 272 return ret; 273 } 274 275 fsl_dev->irq = platform_get_irq(pdev, 0); 276 if (fsl_dev->irq < 0) { 277 dev_err(dev, "failed to get irq\n"); 278 return fsl_dev->irq; 279 } 280 281 fsl_dev->regmap = devm_regmap_init_mmio(dev, base, 282 &fsl_dcu_regmap_config); 283 if (IS_ERR(fsl_dev->regmap)) { 284 dev_err(dev, "regmap init failed\n"); 285 return PTR_ERR(fsl_dev->regmap); 286 } 287 288 fsl_dev->clk = devm_clk_get(dev, "dcu"); 289 if (IS_ERR(fsl_dev->clk)) { 290 dev_err(dev, "failed to get dcu clock\n"); 291 return PTR_ERR(fsl_dev->clk); 292 } 293 ret = clk_prepare_enable(fsl_dev->clk); 294 if (ret < 0) { 295 dev_err(dev, "failed to enable dcu clk\n"); 296 return ret; 297 } 298 299 pix_clk_in = devm_clk_get(dev, "pix"); 300 if (IS_ERR(pix_clk_in)) { 301 /* legancy binding, use dcu clock as pixel clock input */ 302 pix_clk_in = fsl_dev->clk; 303 } 304 305 if (of_property_read_bool(dev->of_node, "big-endian")) 306 div_ratio_shift = 24; 307 308 pix_clk_in_name = __clk_get_name(pix_clk_in); 309 snprintf(pix_clk_name, sizeof(pix_clk_name), "%s_pix", pix_clk_in_name); 310 fsl_dev->pix_clk = clk_register_divider(dev, pix_clk_name, 311 pix_clk_in_name, 0, base + DCU_DIV_RATIO, 312 div_ratio_shift, 8, CLK_DIVIDER_ROUND_CLOSEST, NULL); 313 if (IS_ERR(fsl_dev->pix_clk)) { 314 dev_err(dev, "failed to register pix clk\n"); 315 ret = PTR_ERR(fsl_dev->pix_clk); 316 goto disable_clk; 317 } 318 319 fsl_dev->tcon = fsl_tcon_init(dev); 320 321 drm = drm_dev_alloc(&fsl_dcu_drm_driver, dev); 322 if (IS_ERR(drm)) { 323 ret = PTR_ERR(drm); 324 goto unregister_pix_clk; 325 } 326 327 fsl_dev->dev = dev; 328 fsl_dev->drm = drm; 329 fsl_dev->np = dev->of_node; 330 drm->dev_private = fsl_dev; 331 dev_set_drvdata(dev, fsl_dev); 332 333 ret = drm_dev_register(drm, 0); 334 if (ret < 0) 335 goto put; 336 337 drm_fbdev_generic_setup(drm, legacyfb_depth); 338 339 return 0; 340 341 put: 342 drm_dev_put(drm); 343 unregister_pix_clk: 344 clk_unregister(fsl_dev->pix_clk); 345 disable_clk: 346 clk_disable_unprepare(fsl_dev->clk); 347 return ret; 348 } 349 350 static int fsl_dcu_drm_remove(struct platform_device *pdev) 351 { 352 struct fsl_dcu_drm_device *fsl_dev = platform_get_drvdata(pdev); 353 354 drm_dev_unregister(fsl_dev->drm); 355 drm_dev_put(fsl_dev->drm); 356 clk_disable_unprepare(fsl_dev->clk); 357 clk_unregister(fsl_dev->pix_clk); 358 359 return 0; 360 } 361 362 static struct platform_driver fsl_dcu_drm_platform_driver = { 363 .probe = fsl_dcu_drm_probe, 364 .remove = fsl_dcu_drm_remove, 365 .driver = { 366 .name = "fsl-dcu", 367 .pm = &fsl_dcu_drm_pm_ops, 368 .of_match_table = fsl_dcu_of_match, 369 }, 370 }; 371 372 drm_module_platform_driver(fsl_dcu_drm_platform_driver); 373 374 MODULE_DESCRIPTION("Freescale DCU DRM Driver"); 375 MODULE_LICENSE("GPL"); 376