fsl_dcu_drm_drv.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. #include <linux/clk.h>
  12. #include <linux/clk-provider.h>
  13. #include <linux/io.h>
  14. #include <linux/mfd/syscon.h>
  15. #include <linux/mm.h>
  16. #include <linux/module.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/pm.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/regmap.h>
  22. #include <drm/drmP.h>
  23. #include <drm/drm_crtc_helper.h>
  24. #include <drm/drm_gem_cma_helper.h>
  25. #include "fsl_dcu_drm_crtc.h"
  26. #include "fsl_dcu_drm_drv.h"
  27. static const struct regmap_config fsl_dcu_regmap_config = {
  28. .reg_bits = 32,
  29. .reg_stride = 4,
  30. .val_bits = 32,
  31. .cache_type = REGCACHE_RBTREE,
  32. };
  33. static int fsl_dcu_drm_irq_init(struct drm_device *dev)
  34. {
  35. struct fsl_dcu_drm_device *fsl_dev = dev->dev_private;
  36. unsigned int value;
  37. int ret;
  38. ret = drm_irq_install(dev, fsl_dev->irq);
  39. if (ret < 0)
  40. dev_err(dev->dev, "failed to install IRQ handler\n");
  41. ret = regmap_write(fsl_dev->regmap, DCU_INT_STATUS, 0);
  42. if (ret)
  43. dev_err(dev->dev, "set DCU_INT_STATUS failed\n");
  44. ret = regmap_read(fsl_dev->regmap, DCU_INT_MASK, &value);
  45. if (ret)
  46. dev_err(dev->dev, "read DCU_INT_MASK failed\n");
  47. value &= DCU_INT_MASK_VBLANK;
  48. ret = regmap_write(fsl_dev->regmap, DCU_INT_MASK, value);
  49. if (ret)
  50. dev_err(dev->dev, "set DCU_INT_MASK failed\n");
  51. ret = regmap_write(fsl_dev->regmap, DCU_UPDATE_MODE,
  52. DCU_UPDATE_MODE_READREG);
  53. if (ret)
  54. dev_err(dev->dev, "set DCU_UPDATE_MODE failed\n");
  55. return ret;
  56. }
  57. static int fsl_dcu_load(struct drm_device *drm, unsigned long flags)
  58. {
  59. struct device *dev = drm->dev;
  60. struct fsl_dcu_drm_device *fsl_dev = drm->dev_private;
  61. int ret;
  62. ret = fsl_dcu_drm_modeset_init(fsl_dev);
  63. if (ret < 0) {
  64. dev_err(dev, "failed to initialize mode setting\n");
  65. return ret;
  66. }
  67. ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
  68. if (ret < 0) {
  69. dev_err(dev, "failed to initialize vblank\n");
  70. goto done;
  71. }
  72. drm->vblank_disable_allowed = true;
  73. ret = fsl_dcu_drm_irq_init(drm);
  74. if (ret < 0)
  75. goto done;
  76. drm->irq_enabled = true;
  77. fsl_dcu_fbdev_init(drm);
  78. return 0;
  79. done:
  80. if (ret) {
  81. drm_mode_config_cleanup(drm);
  82. drm_vblank_cleanup(drm);
  83. drm_irq_uninstall(drm);
  84. drm->dev_private = NULL;
  85. }
  86. return ret;
  87. }
  88. static int fsl_dcu_unload(struct drm_device *dev)
  89. {
  90. drm_mode_config_cleanup(dev);
  91. drm_vblank_cleanup(dev);
  92. drm_irq_uninstall(dev);
  93. dev->dev_private = NULL;
  94. return 0;
  95. }
  96. static void fsl_dcu_drm_preclose(struct drm_device *dev, struct drm_file *file)
  97. {
  98. }
  99. static irqreturn_t fsl_dcu_drm_irq(int irq, void *arg)
  100. {
  101. struct drm_device *dev = arg;
  102. struct fsl_dcu_drm_device *fsl_dev = dev->dev_private;
  103. unsigned int int_status;
  104. int ret;
  105. ret = regmap_read(fsl_dev->regmap, DCU_INT_STATUS, &int_status);
  106. if (ret)
  107. dev_err(dev->dev, "set DCU_INT_STATUS failed\n");
  108. if (int_status & DCU_INT_STATUS_VBLANK)
  109. drm_handle_vblank(dev, 0);
  110. ret = regmap_write(fsl_dev->regmap, DCU_INT_STATUS, 0xffffffff);
  111. if (ret)
  112. dev_err(dev->dev, "set DCU_INT_STATUS failed\n");
  113. ret = regmap_write(fsl_dev->regmap, DCU_UPDATE_MODE,
  114. DCU_UPDATE_MODE_READREG);
  115. if (ret)
  116. dev_err(dev->dev, "set DCU_UPDATE_MODE failed\n");
  117. return IRQ_HANDLED;
  118. }
  119. static int fsl_dcu_drm_enable_vblank(struct drm_device *dev, unsigned int pipe)
  120. {
  121. struct fsl_dcu_drm_device *fsl_dev = dev->dev_private;
  122. unsigned int value;
  123. int ret;
  124. ret = regmap_read(fsl_dev->regmap, DCU_INT_MASK, &value);
  125. if (ret)
  126. dev_err(dev->dev, "read DCU_INT_MASK failed\n");
  127. value &= ~DCU_INT_MASK_VBLANK;
  128. ret = regmap_write(fsl_dev->regmap, DCU_INT_MASK, value);
  129. if (ret)
  130. dev_err(dev->dev, "set DCU_INT_MASK failed\n");
  131. return 0;
  132. }
  133. static void fsl_dcu_drm_disable_vblank(struct drm_device *dev,
  134. unsigned int pipe)
  135. {
  136. struct fsl_dcu_drm_device *fsl_dev = dev->dev_private;
  137. unsigned int value;
  138. int ret;
  139. ret = regmap_read(fsl_dev->regmap, DCU_INT_MASK, &value);
  140. if (ret)
  141. dev_err(dev->dev, "read DCU_INT_MASK failed\n");
  142. value |= DCU_INT_MASK_VBLANK;
  143. ret = regmap_write(fsl_dev->regmap, DCU_INT_MASK, value);
  144. if (ret)
  145. dev_err(dev->dev, "set DCU_INT_MASK failed\n");
  146. }
  147. static const struct file_operations fsl_dcu_drm_fops = {
  148. .owner = THIS_MODULE,
  149. .open = drm_open,
  150. .release = drm_release,
  151. .unlocked_ioctl = drm_ioctl,
  152. #ifdef CONFIG_COMPAT
  153. .compat_ioctl = drm_compat_ioctl,
  154. #endif
  155. .poll = drm_poll,
  156. .read = drm_read,
  157. .llseek = no_llseek,
  158. .mmap = drm_gem_cma_mmap,
  159. };
  160. static struct drm_driver fsl_dcu_drm_driver = {
  161. .driver_features = DRIVER_HAVE_IRQ | DRIVER_GEM | DRIVER_MODESET
  162. | DRIVER_PRIME | DRIVER_ATOMIC,
  163. .load = fsl_dcu_load,
  164. .unload = fsl_dcu_unload,
  165. .preclose = fsl_dcu_drm_preclose,
  166. .irq_handler = fsl_dcu_drm_irq,
  167. .get_vblank_counter = drm_vblank_no_hw_counter,
  168. .enable_vblank = fsl_dcu_drm_enable_vblank,
  169. .disable_vblank = fsl_dcu_drm_disable_vblank,
  170. .gem_free_object = drm_gem_cma_free_object,
  171. .gem_vm_ops = &drm_gem_cma_vm_ops,
  172. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  173. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  174. .gem_prime_import = drm_gem_prime_import,
  175. .gem_prime_export = drm_gem_prime_export,
  176. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  177. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  178. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  179. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  180. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  181. .dumb_create = drm_gem_cma_dumb_create,
  182. .dumb_map_offset = drm_gem_cma_dumb_map_offset,
  183. .dumb_destroy = drm_gem_dumb_destroy,
  184. .fops = &fsl_dcu_drm_fops,
  185. .name = "fsl-dcu-drm",
  186. .desc = "Freescale DCU DRM",
  187. .date = "20150213",
  188. .major = 1,
  189. .minor = 0,
  190. };
  191. #ifdef CONFIG_PM_SLEEP
  192. static int fsl_dcu_drm_pm_suspend(struct device *dev)
  193. {
  194. struct fsl_dcu_drm_device *fsl_dev = dev_get_drvdata(dev);
  195. if (!fsl_dev)
  196. return 0;
  197. drm_kms_helper_poll_disable(fsl_dev->drm);
  198. regcache_cache_only(fsl_dev->regmap, true);
  199. regcache_mark_dirty(fsl_dev->regmap);
  200. clk_disable(fsl_dev->clk);
  201. clk_unprepare(fsl_dev->clk);
  202. return 0;
  203. }
  204. static int fsl_dcu_drm_pm_resume(struct device *dev)
  205. {
  206. struct fsl_dcu_drm_device *fsl_dev = dev_get_drvdata(dev);
  207. int ret;
  208. if (!fsl_dev)
  209. return 0;
  210. ret = clk_enable(fsl_dev->clk);
  211. if (ret < 0) {
  212. dev_err(dev, "failed to enable dcu clk\n");
  213. clk_unprepare(fsl_dev->clk);
  214. return ret;
  215. }
  216. ret = clk_prepare(fsl_dev->clk);
  217. if (ret < 0) {
  218. dev_err(dev, "failed to prepare dcu clk\n");
  219. return ret;
  220. }
  221. drm_kms_helper_poll_enable(fsl_dev->drm);
  222. regcache_cache_only(fsl_dev->regmap, false);
  223. regcache_sync(fsl_dev->regmap);
  224. return 0;
  225. }
  226. #endif
  227. static const struct dev_pm_ops fsl_dcu_drm_pm_ops = {
  228. SET_SYSTEM_SLEEP_PM_OPS(fsl_dcu_drm_pm_suspend, fsl_dcu_drm_pm_resume)
  229. };
  230. static const struct fsl_dcu_soc_data fsl_dcu_ls1021a_data = {
  231. .name = "ls1021a",
  232. .total_layer = 16,
  233. .max_layer = 4,
  234. };
  235. static const struct fsl_dcu_soc_data fsl_dcu_vf610_data = {
  236. .name = "vf610",
  237. .total_layer = 64,
  238. .max_layer = 6,
  239. };
  240. static const struct of_device_id fsl_dcu_of_match[] = {
  241. {
  242. .compatible = "fsl,ls1021a-dcu",
  243. .data = &fsl_dcu_ls1021a_data,
  244. }, {
  245. .compatible = "fsl,vf610-dcu",
  246. .data = &fsl_dcu_vf610_data,
  247. }, {
  248. },
  249. };
  250. MODULE_DEVICE_TABLE(of, fsl_dcu_of_match);
  251. static int fsl_dcu_drm_probe(struct platform_device *pdev)
  252. {
  253. struct fsl_dcu_drm_device *fsl_dev;
  254. struct drm_device *drm;
  255. struct device *dev = &pdev->dev;
  256. struct resource *res;
  257. void __iomem *base;
  258. struct drm_driver *driver = &fsl_dcu_drm_driver;
  259. const struct of_device_id *id;
  260. int ret;
  261. fsl_dev = devm_kzalloc(dev, sizeof(*fsl_dev), GFP_KERNEL);
  262. if (!fsl_dev)
  263. return -ENOMEM;
  264. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  265. if (!res) {
  266. dev_err(dev, "could not get memory IO resource\n");
  267. return -ENODEV;
  268. }
  269. base = devm_ioremap_resource(dev, res);
  270. if (IS_ERR(base)) {
  271. ret = PTR_ERR(base);
  272. return ret;
  273. }
  274. fsl_dev->irq = platform_get_irq(pdev, 0);
  275. if (fsl_dev->irq < 0) {
  276. dev_err(dev, "failed to get irq\n");
  277. return -ENXIO;
  278. }
  279. fsl_dev->clk = devm_clk_get(dev, "dcu");
  280. if (IS_ERR(fsl_dev->clk)) {
  281. ret = PTR_ERR(fsl_dev->clk);
  282. dev_err(dev, "failed to get dcu clock\n");
  283. return ret;
  284. }
  285. ret = clk_prepare(fsl_dev->clk);
  286. if (ret < 0) {
  287. dev_err(dev, "failed to prepare dcu clk\n");
  288. return ret;
  289. }
  290. ret = clk_enable(fsl_dev->clk);
  291. if (ret < 0) {
  292. dev_err(dev, "failed to enable dcu clk\n");
  293. clk_unprepare(fsl_dev->clk);
  294. return ret;
  295. }
  296. fsl_dev->regmap = devm_regmap_init_mmio(dev, base,
  297. &fsl_dcu_regmap_config);
  298. if (IS_ERR(fsl_dev->regmap)) {
  299. dev_err(dev, "regmap init failed\n");
  300. return PTR_ERR(fsl_dev->regmap);
  301. }
  302. id = of_match_node(fsl_dcu_of_match, pdev->dev.of_node);
  303. if (!id)
  304. return -ENODEV;
  305. fsl_dev->soc = id->data;
  306. drm = drm_dev_alloc(driver, dev);
  307. if (!drm)
  308. return -ENOMEM;
  309. fsl_dev->dev = dev;
  310. fsl_dev->drm = drm;
  311. fsl_dev->np = dev->of_node;
  312. drm->dev_private = fsl_dev;
  313. dev_set_drvdata(dev, fsl_dev);
  314. drm_dev_set_unique(drm, dev_name(dev));
  315. ret = drm_dev_register(drm, 0);
  316. if (ret < 0)
  317. goto unref;
  318. DRM_INFO("Initialized %s %d.%d.%d %s on minor %d\n", driver->name,
  319. driver->major, driver->minor, driver->patchlevel,
  320. driver->date, drm->primary->index);
  321. return 0;
  322. unref:
  323. drm_dev_unref(drm);
  324. return ret;
  325. }
  326. static int fsl_dcu_drm_remove(struct platform_device *pdev)
  327. {
  328. struct fsl_dcu_drm_device *fsl_dev = platform_get_drvdata(pdev);
  329. drm_put_dev(fsl_dev->drm);
  330. return 0;
  331. }
  332. static struct platform_driver fsl_dcu_drm_platform_driver = {
  333. .probe = fsl_dcu_drm_probe,
  334. .remove = fsl_dcu_drm_remove,
  335. .driver = {
  336. .name = "fsl-dcu",
  337. .pm = &fsl_dcu_drm_pm_ops,
  338. .of_match_table = fsl_dcu_of_match,
  339. },
  340. };
  341. module_platform_driver(fsl_dcu_drm_platform_driver);
  342. MODULE_DESCRIPTION("Freescale DCU DRM Driver");
  343. MODULE_LICENSE("GPL");