rcar_du_drv.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * rcar_du_drv.c -- R-Car Display Unit DRM driver
  3. *
  4. * Copyright (C) 2013 Renesas Corporation
  5. *
  6. * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/io.h>
  15. #include <linux/mm.h>
  16. #include <linux/module.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/pm.h>
  19. #include <linux/slab.h>
  20. #include <drm/drmP.h>
  21. #include <drm/drm_crtc_helper.h>
  22. #include <drm/drm_gem_cma_helper.h>
  23. #include "rcar_du_crtc.h"
  24. #include "rcar_du_drv.h"
  25. #include "rcar_du_kms.h"
  26. #include "rcar_du_regs.h"
  27. /* -----------------------------------------------------------------------------
  28. * Core device operations
  29. */
  30. /*
  31. * rcar_du_get - Acquire a reference to the DU
  32. *
  33. * Acquiring a reference enables the device clock and setup core registers. A
  34. * reference must be held before accessing any hardware registers.
  35. *
  36. * This function must be called with the DRM mode_config lock held.
  37. *
  38. * Return 0 in case of success or a negative error code otherwise.
  39. */
  40. int rcar_du_get(struct rcar_du_device *rcdu)
  41. {
  42. int ret;
  43. if (rcdu->use_count)
  44. goto done;
  45. /* Enable clocks before accessing the hardware. */
  46. ret = clk_prepare_enable(rcdu->clock);
  47. if (ret < 0)
  48. return ret;
  49. /* Enable extended features */
  50. rcar_du_write(rcdu, DEFR, DEFR_CODE | DEFR_DEFE);
  51. rcar_du_write(rcdu, DEFR2, DEFR2_CODE | DEFR2_DEFE2G);
  52. rcar_du_write(rcdu, DEFR3, DEFR3_CODE | DEFR3_DEFE3);
  53. rcar_du_write(rcdu, DEFR4, DEFR4_CODE);
  54. rcar_du_write(rcdu, DEFR5, DEFR5_CODE | DEFR5_DEFE5);
  55. /* Use DS1PR and DS2PR to configure planes priorities and connects the
  56. * superposition 0 to DU0 pins. DU1 pins will be configured dynamically.
  57. */
  58. rcar_du_write(rcdu, DORCR, DORCR_PG1D_DS1 | DORCR_DPRS);
  59. done:
  60. rcdu->use_count++;
  61. return 0;
  62. }
  63. /*
  64. * rcar_du_put - Release a reference to the DU
  65. *
  66. * Releasing the last reference disables the device clock.
  67. *
  68. * This function must be called with the DRM mode_config lock held.
  69. */
  70. void rcar_du_put(struct rcar_du_device *rcdu)
  71. {
  72. if (--rcdu->use_count)
  73. return;
  74. clk_disable_unprepare(rcdu->clock);
  75. }
  76. /* -----------------------------------------------------------------------------
  77. * DRM operations
  78. */
  79. static int rcar_du_unload(struct drm_device *dev)
  80. {
  81. drm_kms_helper_poll_fini(dev);
  82. drm_mode_config_cleanup(dev);
  83. drm_vblank_cleanup(dev);
  84. drm_irq_uninstall(dev);
  85. dev->dev_private = NULL;
  86. return 0;
  87. }
  88. static int rcar_du_load(struct drm_device *dev, unsigned long flags)
  89. {
  90. struct platform_device *pdev = dev->platformdev;
  91. struct rcar_du_platform_data *pdata = pdev->dev.platform_data;
  92. struct rcar_du_device *rcdu;
  93. struct resource *ioarea;
  94. struct resource *mem;
  95. int ret;
  96. if (pdata == NULL) {
  97. dev_err(dev->dev, "no platform data\n");
  98. return -ENODEV;
  99. }
  100. rcdu = devm_kzalloc(&pdev->dev, sizeof(*rcdu), GFP_KERNEL);
  101. if (rcdu == NULL) {
  102. dev_err(dev->dev, "failed to allocate private data\n");
  103. return -ENOMEM;
  104. }
  105. rcdu->dev = &pdev->dev;
  106. rcdu->pdata = pdata;
  107. rcdu->ddev = dev;
  108. dev->dev_private = rcdu;
  109. /* I/O resources and clocks */
  110. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  111. if (mem == NULL) {
  112. dev_err(&pdev->dev, "failed to get memory resource\n");
  113. return -EINVAL;
  114. }
  115. ioarea = devm_request_mem_region(&pdev->dev, mem->start,
  116. resource_size(mem), pdev->name);
  117. if (ioarea == NULL) {
  118. dev_err(&pdev->dev, "failed to request memory region\n");
  119. return -EBUSY;
  120. }
  121. rcdu->mmio = devm_ioremap_nocache(&pdev->dev, ioarea->start,
  122. resource_size(ioarea));
  123. if (rcdu->mmio == NULL) {
  124. dev_err(&pdev->dev, "failed to remap memory resource\n");
  125. return -ENOMEM;
  126. }
  127. rcdu->clock = devm_clk_get(&pdev->dev, NULL);
  128. if (IS_ERR(rcdu->clock)) {
  129. dev_err(&pdev->dev, "failed to get clock\n");
  130. return -ENOENT;
  131. }
  132. /* DRM/KMS objects */
  133. ret = rcar_du_modeset_init(rcdu);
  134. if (ret < 0) {
  135. dev_err(&pdev->dev, "failed to initialize DRM/KMS\n");
  136. goto done;
  137. }
  138. /* IRQ and vblank handling */
  139. ret = drm_vblank_init(dev, (1 << rcdu->num_crtcs) - 1);
  140. if (ret < 0) {
  141. dev_err(&pdev->dev, "failed to initialize vblank\n");
  142. goto done;
  143. }
  144. ret = drm_irq_install(dev);
  145. if (ret < 0) {
  146. dev_err(&pdev->dev, "failed to install IRQ handler\n");
  147. goto done;
  148. }
  149. platform_set_drvdata(pdev, rcdu);
  150. done:
  151. if (ret)
  152. rcar_du_unload(dev);
  153. return ret;
  154. }
  155. static void rcar_du_preclose(struct drm_device *dev, struct drm_file *file)
  156. {
  157. struct rcar_du_device *rcdu = dev->dev_private;
  158. unsigned int i;
  159. for (i = 0; i < ARRAY_SIZE(rcdu->crtcs); ++i)
  160. rcar_du_crtc_cancel_page_flip(&rcdu->crtcs[i], file);
  161. }
  162. static irqreturn_t rcar_du_irq(int irq, void *arg)
  163. {
  164. struct drm_device *dev = arg;
  165. struct rcar_du_device *rcdu = dev->dev_private;
  166. unsigned int i;
  167. for (i = 0; i < ARRAY_SIZE(rcdu->crtcs); ++i)
  168. rcar_du_crtc_irq(&rcdu->crtcs[i]);
  169. return IRQ_HANDLED;
  170. }
  171. static int rcar_du_enable_vblank(struct drm_device *dev, int crtc)
  172. {
  173. struct rcar_du_device *rcdu = dev->dev_private;
  174. rcar_du_crtc_enable_vblank(&rcdu->crtcs[crtc], true);
  175. return 0;
  176. }
  177. static void rcar_du_disable_vblank(struct drm_device *dev, int crtc)
  178. {
  179. struct rcar_du_device *rcdu = dev->dev_private;
  180. rcar_du_crtc_enable_vblank(&rcdu->crtcs[crtc], false);
  181. }
  182. static const struct file_operations rcar_du_fops = {
  183. .owner = THIS_MODULE,
  184. .open = drm_open,
  185. .release = drm_release,
  186. .unlocked_ioctl = drm_ioctl,
  187. #ifdef CONFIG_COMPAT
  188. .compat_ioctl = drm_compat_ioctl,
  189. #endif
  190. .poll = drm_poll,
  191. .read = drm_read,
  192. .fasync = drm_fasync,
  193. .llseek = no_llseek,
  194. .mmap = drm_gem_cma_mmap,
  195. };
  196. static struct drm_driver rcar_du_driver = {
  197. .driver_features = DRIVER_HAVE_IRQ | DRIVER_GEM | DRIVER_MODESET
  198. | DRIVER_PRIME,
  199. .load = rcar_du_load,
  200. .unload = rcar_du_unload,
  201. .preclose = rcar_du_preclose,
  202. .irq_handler = rcar_du_irq,
  203. .get_vblank_counter = drm_vblank_count,
  204. .enable_vblank = rcar_du_enable_vblank,
  205. .disable_vblank = rcar_du_disable_vblank,
  206. .gem_free_object = drm_gem_cma_free_object,
  207. .gem_vm_ops = &drm_gem_cma_vm_ops,
  208. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  209. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  210. .gem_prime_import = drm_gem_prime_import,
  211. .gem_prime_export = drm_gem_prime_export,
  212. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  213. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  214. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  215. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  216. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  217. .dumb_create = rcar_du_dumb_create,
  218. .dumb_map_offset = drm_gem_cma_dumb_map_offset,
  219. .dumb_destroy = drm_gem_cma_dumb_destroy,
  220. .fops = &rcar_du_fops,
  221. .name = "rcar-du",
  222. .desc = "Renesas R-Car Display Unit",
  223. .date = "20130110",
  224. .major = 1,
  225. .minor = 0,
  226. };
  227. /* -----------------------------------------------------------------------------
  228. * Power management
  229. */
  230. #if CONFIG_PM_SLEEP
  231. static int rcar_du_pm_suspend(struct device *dev)
  232. {
  233. struct rcar_du_device *rcdu = dev_get_drvdata(dev);
  234. drm_kms_helper_poll_disable(rcdu->ddev);
  235. /* TODO Suspend the CRTC */
  236. return 0;
  237. }
  238. static int rcar_du_pm_resume(struct device *dev)
  239. {
  240. struct rcar_du_device *rcdu = dev_get_drvdata(dev);
  241. /* TODO Resume the CRTC */
  242. drm_kms_helper_poll_enable(rcdu->ddev);
  243. return 0;
  244. }
  245. #endif
  246. static const struct dev_pm_ops rcar_du_pm_ops = {
  247. SET_SYSTEM_SLEEP_PM_OPS(rcar_du_pm_suspend, rcar_du_pm_resume)
  248. };
  249. /* -----------------------------------------------------------------------------
  250. * Platform driver
  251. */
  252. static int rcar_du_probe(struct platform_device *pdev)
  253. {
  254. return drm_platform_init(&rcar_du_driver, pdev);
  255. }
  256. static int rcar_du_remove(struct platform_device *pdev)
  257. {
  258. drm_platform_exit(&rcar_du_driver, pdev);
  259. return 0;
  260. }
  261. static struct platform_driver rcar_du_platform_driver = {
  262. .probe = rcar_du_probe,
  263. .remove = rcar_du_remove,
  264. .driver = {
  265. .owner = THIS_MODULE,
  266. .name = "rcar-du",
  267. .pm = &rcar_du_pm_ops,
  268. },
  269. };
  270. module_platform_driver(rcar_du_platform_driver);
  271. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  272. MODULE_DESCRIPTION("Renesas R-Car Display Unit DRM Driver");
  273. MODULE_LICENSE("GPL");