1 /* 2 * Copyright 2015 Freescale Semiconductor, Inc. 3 * 4 * Freescale DCU drm device driver 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 */ 11 12 #include <linux/clk.h> 13 #include <linux/clk-provider.h> 14 #include <linux/io.h> 15 #include <linux/mfd/syscon.h> 16 #include <linux/mm.h> 17 #include <linux/module.h> 18 #include <linux/of_platform.h> 19 #include <linux/platform_device.h> 20 #include <linux/pm.h> 21 #include <linux/pm_runtime.h> 22 #include <linux/regmap.h> 23 24 #include <drm/drmP.h> 25 #include <drm/drm_crtc_helper.h> 26 #include <drm/drm_fb_cma_helper.h> 27 #include <drm/drm_gem_cma_helper.h> 28 29 #include "fsl_dcu_drm_crtc.h" 30 #include "fsl_dcu_drm_drv.h" 31 #include "fsl_tcon.h" 32 33 static bool fsl_dcu_drm_is_volatile_reg(struct device *dev, unsigned int reg) 34 { 35 if (reg == DCU_INT_STATUS || reg == DCU_UPDATE_MODE) 36 return true; 37 38 return false; 39 } 40 41 static const struct regmap_config fsl_dcu_regmap_config = { 42 .reg_bits = 32, 43 .reg_stride = 4, 44 .val_bits = 32, 45 .cache_type = REGCACHE_FLAT, 46 47 .volatile_reg = fsl_dcu_drm_is_volatile_reg, 48 .max_register = 0x11fc, 49 }; 50 51 static int fsl_dcu_drm_irq_init(struct drm_device *dev) 52 { 53 struct fsl_dcu_drm_device *fsl_dev = dev->dev_private; 54 int ret; 55 56 ret = drm_irq_install(dev, fsl_dev->irq); 57 if (ret < 0) 58 dev_err(dev->dev, "failed to install IRQ handler\n"); 59 60 regmap_write(fsl_dev->regmap, DCU_INT_STATUS, 0); 61 regmap_write(fsl_dev->regmap, DCU_INT_MASK, ~0); 62 regmap_write(fsl_dev->regmap, DCU_UPDATE_MODE, 63 DCU_UPDATE_MODE_READREG); 64 65 return ret; 66 } 67 68 static int fsl_dcu_load(struct drm_device *dev, unsigned long flags) 69 { 70 struct fsl_dcu_drm_device *fsl_dev = dev->dev_private; 71 int ret; 72 73 ret = fsl_dcu_drm_modeset_init(fsl_dev); 74 if (ret < 0) { 75 dev_err(dev->dev, "failed to initialize mode setting\n"); 76 return ret; 77 } 78 79 ret = drm_vblank_init(dev, dev->mode_config.num_crtc); 80 if (ret < 0) { 81 dev_err(dev->dev, "failed to initialize vblank\n"); 82 goto done; 83 } 84 85 ret = fsl_dcu_drm_irq_init(dev); 86 if (ret < 0) 87 goto done; 88 dev->irq_enabled = true; 89 90 fsl_dcu_fbdev_init(dev); 91 92 return 0; 93 done: 94 drm_kms_helper_poll_fini(dev); 95 96 if (fsl_dev->fbdev) 97 drm_fbdev_cma_fini(fsl_dev->fbdev); 98 99 drm_mode_config_cleanup(dev); 100 drm_vblank_cleanup(dev); 101 drm_irq_uninstall(dev); 102 dev->dev_private = NULL; 103 104 return ret; 105 } 106 107 static int fsl_dcu_unload(struct drm_device *dev) 108 { 109 struct fsl_dcu_drm_device *fsl_dev = dev->dev_private; 110 111 drm_kms_helper_poll_fini(dev); 112 113 if (fsl_dev->fbdev) 114 drm_fbdev_cma_fini(fsl_dev->fbdev); 115 116 drm_mode_config_cleanup(dev); 117 drm_vblank_cleanup(dev); 118 drm_irq_uninstall(dev); 119 120 dev->dev_private = NULL; 121 122 return 0; 123 } 124 125 static irqreturn_t fsl_dcu_drm_irq(int irq, void *arg) 126 { 127 struct drm_device *dev = arg; 128 struct fsl_dcu_drm_device *fsl_dev = dev->dev_private; 129 unsigned int int_status; 130 int ret; 131 132 ret = regmap_read(fsl_dev->regmap, DCU_INT_STATUS, &int_status); 133 if (ret) { 134 dev_err(dev->dev, "read DCU_INT_STATUS failed\n"); 135 return IRQ_NONE; 136 } 137 138 if (int_status & DCU_INT_STATUS_VBLANK) 139 drm_handle_vblank(dev, 0); 140 141 regmap_write(fsl_dev->regmap, DCU_INT_STATUS, int_status); 142 regmap_write(fsl_dev->regmap, DCU_UPDATE_MODE, 143 DCU_UPDATE_MODE_READREG); 144 145 return IRQ_HANDLED; 146 } 147 148 static int fsl_dcu_drm_enable_vblank(struct drm_device *dev, unsigned int pipe) 149 { 150 struct fsl_dcu_drm_device *fsl_dev = dev->dev_private; 151 unsigned int value; 152 153 regmap_read(fsl_dev->regmap, DCU_INT_MASK, &value); 154 value &= ~DCU_INT_MASK_VBLANK; 155 regmap_write(fsl_dev->regmap, DCU_INT_MASK, value); 156 157 return 0; 158 } 159 160 static void fsl_dcu_drm_disable_vblank(struct drm_device *dev, 161 unsigned int pipe) 162 { 163 struct fsl_dcu_drm_device *fsl_dev = dev->dev_private; 164 unsigned int value; 165 166 regmap_read(fsl_dev->regmap, DCU_INT_MASK, &value); 167 value |= DCU_INT_MASK_VBLANK; 168 regmap_write(fsl_dev->regmap, DCU_INT_MASK, value); 169 } 170 171 static void fsl_dcu_drm_lastclose(struct drm_device *dev) 172 { 173 struct fsl_dcu_drm_device *fsl_dev = dev->dev_private; 174 175 drm_fbdev_cma_restore_mode(fsl_dev->fbdev); 176 } 177 178 static const struct file_operations fsl_dcu_drm_fops = { 179 .owner = THIS_MODULE, 180 .open = drm_open, 181 .release = drm_release, 182 .unlocked_ioctl = drm_ioctl, 183 #ifdef CONFIG_COMPAT 184 .compat_ioctl = drm_compat_ioctl, 185 #endif 186 .poll = drm_poll, 187 .read = drm_read, 188 .llseek = no_llseek, 189 .mmap = drm_gem_cma_mmap, 190 }; 191 192 static struct drm_driver fsl_dcu_drm_driver = { 193 .driver_features = DRIVER_HAVE_IRQ | DRIVER_GEM | DRIVER_MODESET 194 | DRIVER_PRIME | DRIVER_ATOMIC, 195 .lastclose = fsl_dcu_drm_lastclose, 196 .load = fsl_dcu_load, 197 .unload = fsl_dcu_unload, 198 .irq_handler = fsl_dcu_drm_irq, 199 .get_vblank_counter = drm_vblank_no_hw_counter, 200 .enable_vblank = fsl_dcu_drm_enable_vblank, 201 .disable_vblank = fsl_dcu_drm_disable_vblank, 202 .gem_free_object = drm_gem_cma_free_object, 203 .gem_vm_ops = &drm_gem_cma_vm_ops, 204 .prime_handle_to_fd = drm_gem_prime_handle_to_fd, 205 .prime_fd_to_handle = drm_gem_prime_fd_to_handle, 206 .gem_prime_import = drm_gem_prime_import, 207 .gem_prime_export = drm_gem_prime_export, 208 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table, 209 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table, 210 .gem_prime_vmap = drm_gem_cma_prime_vmap, 211 .gem_prime_vunmap = drm_gem_cma_prime_vunmap, 212 .gem_prime_mmap = drm_gem_cma_prime_mmap, 213 .dumb_create = drm_gem_cma_dumb_create, 214 .dumb_map_offset = drm_gem_cma_dumb_map_offset, 215 .dumb_destroy = drm_gem_dumb_destroy, 216 .fops = &fsl_dcu_drm_fops, 217 .name = "fsl-dcu-drm", 218 .desc = "Freescale DCU DRM", 219 .date = "20160425", 220 .major = 1, 221 .minor = 1, 222 }; 223 224 #ifdef CONFIG_PM_SLEEP 225 static int fsl_dcu_drm_pm_suspend(struct device *dev) 226 { 227 struct fsl_dcu_drm_device *fsl_dev = dev_get_drvdata(dev); 228 229 if (!fsl_dev) 230 return 0; 231 232 drm_kms_helper_poll_disable(fsl_dev->drm); 233 regcache_cache_only(fsl_dev->regmap, true); 234 regcache_mark_dirty(fsl_dev->regmap); 235 clk_disable(fsl_dev->clk); 236 clk_unprepare(fsl_dev->clk); 237 238 return 0; 239 } 240 241 static int fsl_dcu_drm_pm_resume(struct device *dev) 242 { 243 struct fsl_dcu_drm_device *fsl_dev = dev_get_drvdata(dev); 244 int ret; 245 246 if (!fsl_dev) 247 return 0; 248 249 ret = clk_enable(fsl_dev->clk); 250 if (ret < 0) { 251 dev_err(dev, "failed to enable dcu clk\n"); 252 clk_unprepare(fsl_dev->clk); 253 return ret; 254 } 255 ret = clk_prepare(fsl_dev->clk); 256 if (ret < 0) { 257 dev_err(dev, "failed to prepare dcu clk\n"); 258 return ret; 259 } 260 261 drm_kms_helper_poll_enable(fsl_dev->drm); 262 regcache_cache_only(fsl_dev->regmap, false); 263 regcache_sync(fsl_dev->regmap); 264 265 return 0; 266 } 267 #endif 268 269 static const struct dev_pm_ops fsl_dcu_drm_pm_ops = { 270 SET_SYSTEM_SLEEP_PM_OPS(fsl_dcu_drm_pm_suspend, fsl_dcu_drm_pm_resume) 271 }; 272 273 static const struct fsl_dcu_soc_data fsl_dcu_ls1021a_data = { 274 .name = "ls1021a", 275 .total_layer = 16, 276 .max_layer = 4, 277 }; 278 279 static const struct fsl_dcu_soc_data fsl_dcu_vf610_data = { 280 .name = "vf610", 281 .total_layer = 64, 282 .max_layer = 6, 283 }; 284 285 static const struct of_device_id fsl_dcu_of_match[] = { 286 { 287 .compatible = "fsl,ls1021a-dcu", 288 .data = &fsl_dcu_ls1021a_data, 289 }, { 290 .compatible = "fsl,vf610-dcu", 291 .data = &fsl_dcu_vf610_data, 292 }, { 293 }, 294 }; 295 MODULE_DEVICE_TABLE(of, fsl_dcu_of_match); 296 297 static int fsl_dcu_drm_probe(struct platform_device *pdev) 298 { 299 struct fsl_dcu_drm_device *fsl_dev; 300 struct drm_device *drm; 301 struct device *dev = &pdev->dev; 302 struct resource *res; 303 void __iomem *base; 304 struct drm_driver *driver = &fsl_dcu_drm_driver; 305 struct clk *pix_clk_in; 306 char pix_clk_name[32]; 307 const char *pix_clk_in_name; 308 const struct of_device_id *id; 309 int ret; 310 311 fsl_dev = devm_kzalloc(dev, sizeof(*fsl_dev), GFP_KERNEL); 312 if (!fsl_dev) 313 return -ENOMEM; 314 315 id = of_match_node(fsl_dcu_of_match, pdev->dev.of_node); 316 if (!id) 317 return -ENODEV; 318 fsl_dev->soc = id->data; 319 320 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 321 if (!res) { 322 dev_err(dev, "could not get memory IO resource\n"); 323 return -ENODEV; 324 } 325 326 base = devm_ioremap_resource(dev, res); 327 if (IS_ERR(base)) { 328 ret = PTR_ERR(base); 329 return ret; 330 } 331 332 fsl_dev->irq = platform_get_irq(pdev, 0); 333 if (fsl_dev->irq < 0) { 334 dev_err(dev, "failed to get irq\n"); 335 return -ENXIO; 336 } 337 338 fsl_dev->regmap = devm_regmap_init_mmio(dev, base, 339 &fsl_dcu_regmap_config); 340 if (IS_ERR(fsl_dev->regmap)) { 341 dev_err(dev, "regmap init failed\n"); 342 return PTR_ERR(fsl_dev->regmap); 343 } 344 345 fsl_dev->clk = devm_clk_get(dev, "dcu"); 346 if (IS_ERR(fsl_dev->clk)) { 347 dev_err(dev, "failed to get dcu clock\n"); 348 return PTR_ERR(fsl_dev->clk); 349 } 350 ret = clk_prepare_enable(fsl_dev->clk); 351 if (ret < 0) { 352 dev_err(dev, "failed to enable dcu clk\n"); 353 return ret; 354 } 355 356 pix_clk_in = devm_clk_get(dev, "pix"); 357 if (IS_ERR(pix_clk_in)) { 358 /* legancy binding, use dcu clock as pixel clock input */ 359 pix_clk_in = fsl_dev->clk; 360 } 361 362 pix_clk_in_name = __clk_get_name(pix_clk_in); 363 snprintf(pix_clk_name, sizeof(pix_clk_name), "%s_pix", pix_clk_in_name); 364 fsl_dev->pix_clk = clk_register_divider(dev, pix_clk_name, 365 pix_clk_in_name, 0, base + DCU_DIV_RATIO, 366 0, 8, CLK_DIVIDER_ROUND_CLOSEST, NULL); 367 if (IS_ERR(fsl_dev->pix_clk)) { 368 dev_err(dev, "failed to register pix clk\n"); 369 ret = PTR_ERR(fsl_dev->pix_clk); 370 goto disable_clk; 371 } 372 373 ret = clk_prepare_enable(fsl_dev->pix_clk); 374 if (ret < 0) { 375 dev_err(dev, "failed to enable pix clk\n"); 376 goto unregister_pix_clk; 377 } 378 379 fsl_dev->tcon = fsl_tcon_init(dev); 380 381 drm = drm_dev_alloc(driver, dev); 382 if (!drm) { 383 ret = -ENOMEM; 384 goto disable_pix_clk; 385 } 386 387 fsl_dev->dev = dev; 388 fsl_dev->drm = drm; 389 fsl_dev->np = dev->of_node; 390 drm->dev_private = fsl_dev; 391 dev_set_drvdata(dev, fsl_dev); 392 393 ret = drm_dev_register(drm, 0); 394 if (ret < 0) 395 goto unref; 396 397 DRM_INFO("Initialized %s %d.%d.%d %s on minor %d\n", driver->name, 398 driver->major, driver->minor, driver->patchlevel, 399 driver->date, drm->primary->index); 400 401 return 0; 402 403 unref: 404 drm_dev_unref(drm); 405 disable_pix_clk: 406 clk_disable_unprepare(fsl_dev->pix_clk); 407 unregister_pix_clk: 408 clk_unregister(fsl_dev->pix_clk); 409 disable_clk: 410 clk_disable_unprepare(fsl_dev->clk); 411 return ret; 412 } 413 414 static int fsl_dcu_drm_remove(struct platform_device *pdev) 415 { 416 struct fsl_dcu_drm_device *fsl_dev = platform_get_drvdata(pdev); 417 418 clk_disable_unprepare(fsl_dev->clk); 419 clk_disable_unprepare(fsl_dev->pix_clk); 420 clk_unregister(fsl_dev->pix_clk); 421 drm_put_dev(fsl_dev->drm); 422 423 return 0; 424 } 425 426 static struct platform_driver fsl_dcu_drm_platform_driver = { 427 .probe = fsl_dcu_drm_probe, 428 .remove = fsl_dcu_drm_remove, 429 .driver = { 430 .name = "fsl-dcu", 431 .pm = &fsl_dcu_drm_pm_ops, 432 .of_match_table = fsl_dcu_of_match, 433 }, 434 }; 435 436 module_platform_driver(fsl_dcu_drm_platform_driver); 437 438 MODULE_DESCRIPTION("Freescale DCU DRM Driver"); 439 MODULE_LICENSE("GPL"); 440