rcar_du_drv.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 the first reference setups core registers. A reference must be
  34. * 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. if (rcdu->use_count)
  43. goto done;
  44. /* Enable extended features */
  45. rcar_du_write(rcdu, DEFR, DEFR_CODE | DEFR_DEFE);
  46. rcar_du_write(rcdu, DEFR2, DEFR2_CODE | DEFR2_DEFE2G);
  47. rcar_du_write(rcdu, DEFR3, DEFR3_CODE | DEFR3_DEFE3);
  48. rcar_du_write(rcdu, DEFR4, DEFR4_CODE);
  49. rcar_du_write(rcdu, DEFR5, DEFR5_CODE | DEFR5_DEFE5);
  50. /* Use DS1PR and DS2PR to configure planes priorities and connects the
  51. * superposition 0 to DU0 pins. DU1 pins will be configured dynamically.
  52. */
  53. rcar_du_write(rcdu, DORCR, DORCR_PG1D_DS1 | DORCR_DPRS);
  54. done:
  55. rcdu->use_count++;
  56. return 0;
  57. }
  58. /*
  59. * rcar_du_put - Release a reference to the DU
  60. *
  61. * This function must be called with the DRM mode_config lock held.
  62. */
  63. void rcar_du_put(struct rcar_du_device *rcdu)
  64. {
  65. --rcdu->use_count;
  66. }
  67. /* -----------------------------------------------------------------------------
  68. * DRM operations
  69. */
  70. static int rcar_du_unload(struct drm_device *dev)
  71. {
  72. drm_kms_helper_poll_fini(dev);
  73. drm_mode_config_cleanup(dev);
  74. drm_vblank_cleanup(dev);
  75. dev->irq_enabled = 0;
  76. dev->dev_private = NULL;
  77. return 0;
  78. }
  79. static int rcar_du_load(struct drm_device *dev, unsigned long flags)
  80. {
  81. struct platform_device *pdev = dev->platformdev;
  82. struct rcar_du_platform_data *pdata = pdev->dev.platform_data;
  83. struct rcar_du_device *rcdu;
  84. struct resource *mem;
  85. int ret;
  86. if (pdata == NULL) {
  87. dev_err(dev->dev, "no platform data\n");
  88. return -ENODEV;
  89. }
  90. rcdu = devm_kzalloc(&pdev->dev, sizeof(*rcdu), GFP_KERNEL);
  91. if (rcdu == NULL) {
  92. dev_err(dev->dev, "failed to allocate private data\n");
  93. return -ENOMEM;
  94. }
  95. rcdu->dev = &pdev->dev;
  96. rcdu->pdata = pdata;
  97. rcdu->info = (struct rcar_du_device_info *)pdev->id_entry->driver_data;
  98. rcdu->ddev = dev;
  99. dev->dev_private = rcdu;
  100. /* I/O resources */
  101. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  102. rcdu->mmio = devm_ioremap_resource(&pdev->dev, mem);
  103. if (IS_ERR(rcdu->mmio))
  104. return PTR_ERR(rcdu->mmio);
  105. /* DRM/KMS objects */
  106. ret = rcar_du_modeset_init(rcdu);
  107. if (ret < 0) {
  108. dev_err(&pdev->dev, "failed to initialize DRM/KMS\n");
  109. goto done;
  110. }
  111. /* vblank handling */
  112. ret = drm_vblank_init(dev, (1 << rcdu->num_crtcs) - 1);
  113. if (ret < 0) {
  114. dev_err(&pdev->dev, "failed to initialize vblank\n");
  115. goto done;
  116. }
  117. dev->irq_enabled = 1;
  118. platform_set_drvdata(pdev, rcdu);
  119. done:
  120. if (ret)
  121. rcar_du_unload(dev);
  122. return ret;
  123. }
  124. static void rcar_du_preclose(struct drm_device *dev, struct drm_file *file)
  125. {
  126. struct rcar_du_device *rcdu = dev->dev_private;
  127. unsigned int i;
  128. for (i = 0; i < ARRAY_SIZE(rcdu->crtcs); ++i)
  129. rcar_du_crtc_cancel_page_flip(&rcdu->crtcs[i], file);
  130. }
  131. static int rcar_du_enable_vblank(struct drm_device *dev, int crtc)
  132. {
  133. struct rcar_du_device *rcdu = dev->dev_private;
  134. rcar_du_crtc_enable_vblank(&rcdu->crtcs[crtc], true);
  135. return 0;
  136. }
  137. static void rcar_du_disable_vblank(struct drm_device *dev, int crtc)
  138. {
  139. struct rcar_du_device *rcdu = dev->dev_private;
  140. rcar_du_crtc_enable_vblank(&rcdu->crtcs[crtc], false);
  141. }
  142. static const struct file_operations rcar_du_fops = {
  143. .owner = THIS_MODULE,
  144. .open = drm_open,
  145. .release = drm_release,
  146. .unlocked_ioctl = drm_ioctl,
  147. #ifdef CONFIG_COMPAT
  148. .compat_ioctl = drm_compat_ioctl,
  149. #endif
  150. .poll = drm_poll,
  151. .read = drm_read,
  152. .fasync = drm_fasync,
  153. .llseek = no_llseek,
  154. .mmap = drm_gem_cma_mmap,
  155. };
  156. static struct drm_driver rcar_du_driver = {
  157. .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME,
  158. .load = rcar_du_load,
  159. .unload = rcar_du_unload,
  160. .preclose = rcar_du_preclose,
  161. .get_vblank_counter = drm_vblank_count,
  162. .enable_vblank = rcar_du_enable_vblank,
  163. .disable_vblank = rcar_du_disable_vblank,
  164. .gem_free_object = drm_gem_cma_free_object,
  165. .gem_vm_ops = &drm_gem_cma_vm_ops,
  166. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  167. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  168. .gem_prime_import = drm_gem_prime_import,
  169. .gem_prime_export = drm_gem_prime_export,
  170. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  171. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  172. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  173. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  174. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  175. .dumb_create = rcar_du_dumb_create,
  176. .dumb_map_offset = drm_gem_cma_dumb_map_offset,
  177. .dumb_destroy = drm_gem_cma_dumb_destroy,
  178. .fops = &rcar_du_fops,
  179. .name = "rcar-du",
  180. .desc = "Renesas R-Car Display Unit",
  181. .date = "20130110",
  182. .major = 1,
  183. .minor = 0,
  184. };
  185. /* -----------------------------------------------------------------------------
  186. * Power management
  187. */
  188. #if CONFIG_PM_SLEEP
  189. static int rcar_du_pm_suspend(struct device *dev)
  190. {
  191. struct rcar_du_device *rcdu = dev_get_drvdata(dev);
  192. drm_kms_helper_poll_disable(rcdu->ddev);
  193. /* TODO Suspend the CRTC */
  194. return 0;
  195. }
  196. static int rcar_du_pm_resume(struct device *dev)
  197. {
  198. struct rcar_du_device *rcdu = dev_get_drvdata(dev);
  199. /* TODO Resume the CRTC */
  200. drm_kms_helper_poll_enable(rcdu->ddev);
  201. return 0;
  202. }
  203. #endif
  204. static const struct dev_pm_ops rcar_du_pm_ops = {
  205. SET_SYSTEM_SLEEP_PM_OPS(rcar_du_pm_suspend, rcar_du_pm_resume)
  206. };
  207. /* -----------------------------------------------------------------------------
  208. * Platform driver
  209. */
  210. static int rcar_du_probe(struct platform_device *pdev)
  211. {
  212. return drm_platform_init(&rcar_du_driver, pdev);
  213. }
  214. static int rcar_du_remove(struct platform_device *pdev)
  215. {
  216. drm_platform_exit(&rcar_du_driver, pdev);
  217. return 0;
  218. }
  219. static const struct rcar_du_device_info rcar_du_r8a7779_info = {
  220. .features = 0,
  221. };
  222. static const struct platform_device_id rcar_du_id_table[] = {
  223. { "rcar-du-r8a7779", (kernel_ulong_t)&rcar_du_r8a7779_info },
  224. { }
  225. };
  226. MODULE_DEVICE_TABLE(platform, rcar_du_id_table);
  227. static struct platform_driver rcar_du_platform_driver = {
  228. .probe = rcar_du_probe,
  229. .remove = rcar_du_remove,
  230. .driver = {
  231. .owner = THIS_MODULE,
  232. .name = "rcar-du",
  233. .pm = &rcar_du_pm_ops,
  234. },
  235. .id_table = rcar_du_id_table,
  236. };
  237. module_platform_driver(rcar_du_platform_driver);
  238. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  239. MODULE_DESCRIPTION("Renesas R-Car Display Unit DRM Driver");
  240. MODULE_LICENSE("GPL");