rcar_du_drv.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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_fb_cma_helper.h>
  23. #include <drm/drm_gem_cma_helper.h>
  24. #include "rcar_du_crtc.h"
  25. #include "rcar_du_drv.h"
  26. #include "rcar_du_kms.h"
  27. #include "rcar_du_regs.h"
  28. /* -----------------------------------------------------------------------------
  29. * DRM operations
  30. */
  31. static int rcar_du_unload(struct drm_device *dev)
  32. {
  33. struct rcar_du_device *rcdu = dev->dev_private;
  34. if (rcdu->fbdev)
  35. drm_fbdev_cma_fini(rcdu->fbdev);
  36. drm_kms_helper_poll_fini(dev);
  37. drm_mode_config_cleanup(dev);
  38. drm_vblank_cleanup(dev);
  39. dev->irq_enabled = 0;
  40. dev->dev_private = NULL;
  41. return 0;
  42. }
  43. static int rcar_du_load(struct drm_device *dev, unsigned long flags)
  44. {
  45. struct platform_device *pdev = dev->platformdev;
  46. struct rcar_du_platform_data *pdata = pdev->dev.platform_data;
  47. struct rcar_du_device *rcdu;
  48. struct resource *mem;
  49. int ret;
  50. if (pdata == NULL) {
  51. dev_err(dev->dev, "no platform data\n");
  52. return -ENODEV;
  53. }
  54. rcdu = devm_kzalloc(&pdev->dev, sizeof(*rcdu), GFP_KERNEL);
  55. if (rcdu == NULL) {
  56. dev_err(dev->dev, "failed to allocate private data\n");
  57. return -ENOMEM;
  58. }
  59. rcdu->dev = &pdev->dev;
  60. rcdu->pdata = pdata;
  61. rcdu->info = (struct rcar_du_device_info *)pdev->id_entry->driver_data;
  62. rcdu->ddev = dev;
  63. dev->dev_private = rcdu;
  64. /* I/O resources */
  65. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  66. rcdu->mmio = devm_ioremap_resource(&pdev->dev, mem);
  67. if (IS_ERR(rcdu->mmio))
  68. return PTR_ERR(rcdu->mmio);
  69. /* DRM/KMS objects */
  70. ret = rcar_du_modeset_init(rcdu);
  71. if (ret < 0) {
  72. dev_err(&pdev->dev, "failed to initialize DRM/KMS\n");
  73. goto done;
  74. }
  75. /* vblank handling */
  76. ret = drm_vblank_init(dev, (1 << rcdu->num_crtcs) - 1);
  77. if (ret < 0) {
  78. dev_err(&pdev->dev, "failed to initialize vblank\n");
  79. goto done;
  80. }
  81. dev->irq_enabled = 1;
  82. platform_set_drvdata(pdev, rcdu);
  83. done:
  84. if (ret)
  85. rcar_du_unload(dev);
  86. return ret;
  87. }
  88. static void rcar_du_preclose(struct drm_device *dev, struct drm_file *file)
  89. {
  90. struct rcar_du_device *rcdu = dev->dev_private;
  91. unsigned int i;
  92. for (i = 0; i < rcdu->num_crtcs; ++i)
  93. rcar_du_crtc_cancel_page_flip(&rcdu->crtcs[i], file);
  94. }
  95. static void rcar_du_lastclose(struct drm_device *dev)
  96. {
  97. struct rcar_du_device *rcdu = dev->dev_private;
  98. drm_fbdev_cma_restore_mode(rcdu->fbdev);
  99. }
  100. static int rcar_du_enable_vblank(struct drm_device *dev, int crtc)
  101. {
  102. struct rcar_du_device *rcdu = dev->dev_private;
  103. rcar_du_crtc_enable_vblank(&rcdu->crtcs[crtc], true);
  104. return 0;
  105. }
  106. static void rcar_du_disable_vblank(struct drm_device *dev, int crtc)
  107. {
  108. struct rcar_du_device *rcdu = dev->dev_private;
  109. rcar_du_crtc_enable_vblank(&rcdu->crtcs[crtc], false);
  110. }
  111. static const struct file_operations rcar_du_fops = {
  112. .owner = THIS_MODULE,
  113. .open = drm_open,
  114. .release = drm_release,
  115. .unlocked_ioctl = drm_ioctl,
  116. #ifdef CONFIG_COMPAT
  117. .compat_ioctl = drm_compat_ioctl,
  118. #endif
  119. .poll = drm_poll,
  120. .read = drm_read,
  121. .fasync = drm_fasync,
  122. .llseek = no_llseek,
  123. .mmap = drm_gem_cma_mmap,
  124. };
  125. static struct drm_driver rcar_du_driver = {
  126. .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME,
  127. .load = rcar_du_load,
  128. .unload = rcar_du_unload,
  129. .preclose = rcar_du_preclose,
  130. .lastclose = rcar_du_lastclose,
  131. .get_vblank_counter = drm_vblank_count,
  132. .enable_vblank = rcar_du_enable_vblank,
  133. .disable_vblank = rcar_du_disable_vblank,
  134. .gem_free_object = drm_gem_cma_free_object,
  135. .gem_vm_ops = &drm_gem_cma_vm_ops,
  136. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  137. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  138. .gem_prime_import = drm_gem_prime_import,
  139. .gem_prime_export = drm_gem_prime_export,
  140. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  141. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  142. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  143. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  144. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  145. .dumb_create = rcar_du_dumb_create,
  146. .dumb_map_offset = drm_gem_cma_dumb_map_offset,
  147. .dumb_destroy = drm_gem_dumb_destroy,
  148. .fops = &rcar_du_fops,
  149. .name = "rcar-du",
  150. .desc = "Renesas R-Car Display Unit",
  151. .date = "20130110",
  152. .major = 1,
  153. .minor = 0,
  154. };
  155. /* -----------------------------------------------------------------------------
  156. * Power management
  157. */
  158. #if CONFIG_PM_SLEEP
  159. static int rcar_du_pm_suspend(struct device *dev)
  160. {
  161. struct rcar_du_device *rcdu = dev_get_drvdata(dev);
  162. drm_kms_helper_poll_disable(rcdu->ddev);
  163. /* TODO Suspend the CRTC */
  164. return 0;
  165. }
  166. static int rcar_du_pm_resume(struct device *dev)
  167. {
  168. struct rcar_du_device *rcdu = dev_get_drvdata(dev);
  169. /* TODO Resume the CRTC */
  170. drm_kms_helper_poll_enable(rcdu->ddev);
  171. return 0;
  172. }
  173. #endif
  174. static const struct dev_pm_ops rcar_du_pm_ops = {
  175. SET_SYSTEM_SLEEP_PM_OPS(rcar_du_pm_suspend, rcar_du_pm_resume)
  176. };
  177. /* -----------------------------------------------------------------------------
  178. * Platform driver
  179. */
  180. static int rcar_du_probe(struct platform_device *pdev)
  181. {
  182. return drm_platform_init(&rcar_du_driver, pdev);
  183. }
  184. static int rcar_du_remove(struct platform_device *pdev)
  185. {
  186. drm_platform_exit(&rcar_du_driver, pdev);
  187. return 0;
  188. }
  189. static const struct rcar_du_device_info rcar_du_r8a7779_info = {
  190. .features = 0,
  191. .num_crtcs = 2,
  192. .routes = {
  193. /* R8A7779 has two RGB outputs and one (currently unsupported)
  194. * TCON output.
  195. */
  196. [RCAR_DU_OUTPUT_DPAD0] = {
  197. .possible_crtcs = BIT(0),
  198. .encoder_type = DRM_MODE_ENCODER_NONE,
  199. },
  200. [RCAR_DU_OUTPUT_DPAD1] = {
  201. .possible_crtcs = BIT(1) | BIT(0),
  202. .encoder_type = DRM_MODE_ENCODER_NONE,
  203. },
  204. },
  205. .num_lvds = 0,
  206. };
  207. static const struct rcar_du_device_info rcar_du_r8a7790_info = {
  208. .features = RCAR_DU_FEATURE_CRTC_IRQ_CLOCK | RCAR_DU_FEATURE_ALIGN_128B
  209. | RCAR_DU_FEATURE_DEFR8,
  210. .num_crtcs = 3,
  211. .routes = {
  212. /* R8A7790 has one RGB output, two LVDS outputs and one
  213. * (currently unsupported) TCON output.
  214. */
  215. [RCAR_DU_OUTPUT_DPAD0] = {
  216. .possible_crtcs = BIT(2) | BIT(1) | BIT(0),
  217. .encoder_type = DRM_MODE_ENCODER_NONE,
  218. },
  219. [RCAR_DU_OUTPUT_LVDS0] = {
  220. .possible_crtcs = BIT(0),
  221. .encoder_type = DRM_MODE_ENCODER_LVDS,
  222. },
  223. [RCAR_DU_OUTPUT_LVDS1] = {
  224. .possible_crtcs = BIT(2) | BIT(1),
  225. .encoder_type = DRM_MODE_ENCODER_LVDS,
  226. },
  227. },
  228. .num_lvds = 2,
  229. };
  230. static const struct platform_device_id rcar_du_id_table[] = {
  231. { "rcar-du-r8a7779", (kernel_ulong_t)&rcar_du_r8a7779_info },
  232. { "rcar-du-r8a7790", (kernel_ulong_t)&rcar_du_r8a7790_info },
  233. { }
  234. };
  235. MODULE_DEVICE_TABLE(platform, rcar_du_id_table);
  236. static struct platform_driver rcar_du_platform_driver = {
  237. .probe = rcar_du_probe,
  238. .remove = rcar_du_remove,
  239. .driver = {
  240. .owner = THIS_MODULE,
  241. .name = "rcar-du",
  242. .pm = &rcar_du_pm_ops,
  243. },
  244. .id_table = rcar_du_id_table,
  245. };
  246. module_platform_driver(rcar_du_platform_driver);
  247. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  248. MODULE_DESCRIPTION("Renesas R-Car Display Unit DRM Driver");
  249. MODULE_LICENSE("GPL");