rcar_du_drv.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. .llseek = no_llseek,
  122. .mmap = drm_gem_cma_mmap,
  123. };
  124. static struct drm_driver rcar_du_driver = {
  125. .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME,
  126. .load = rcar_du_load,
  127. .unload = rcar_du_unload,
  128. .preclose = rcar_du_preclose,
  129. .lastclose = rcar_du_lastclose,
  130. .get_vblank_counter = drm_vblank_count,
  131. .enable_vblank = rcar_du_enable_vblank,
  132. .disable_vblank = rcar_du_disable_vblank,
  133. .gem_free_object = drm_gem_cma_free_object,
  134. .gem_vm_ops = &drm_gem_cma_vm_ops,
  135. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  136. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  137. .gem_prime_import = drm_gem_prime_import,
  138. .gem_prime_export = drm_gem_prime_export,
  139. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  140. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  141. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  142. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  143. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  144. .dumb_create = rcar_du_dumb_create,
  145. .dumb_map_offset = drm_gem_cma_dumb_map_offset,
  146. .dumb_destroy = drm_gem_dumb_destroy,
  147. .fops = &rcar_du_fops,
  148. .name = "rcar-du",
  149. .desc = "Renesas R-Car Display Unit",
  150. .date = "20130110",
  151. .major = 1,
  152. .minor = 0,
  153. };
  154. /* -----------------------------------------------------------------------------
  155. * Power management
  156. */
  157. #if CONFIG_PM_SLEEP
  158. static int rcar_du_pm_suspend(struct device *dev)
  159. {
  160. struct rcar_du_device *rcdu = dev_get_drvdata(dev);
  161. drm_kms_helper_poll_disable(rcdu->ddev);
  162. /* TODO Suspend the CRTC */
  163. return 0;
  164. }
  165. static int rcar_du_pm_resume(struct device *dev)
  166. {
  167. struct rcar_du_device *rcdu = dev_get_drvdata(dev);
  168. /* TODO Resume the CRTC */
  169. drm_kms_helper_poll_enable(rcdu->ddev);
  170. return 0;
  171. }
  172. #endif
  173. static const struct dev_pm_ops rcar_du_pm_ops = {
  174. SET_SYSTEM_SLEEP_PM_OPS(rcar_du_pm_suspend, rcar_du_pm_resume)
  175. };
  176. /* -----------------------------------------------------------------------------
  177. * Platform driver
  178. */
  179. static int rcar_du_probe(struct platform_device *pdev)
  180. {
  181. return drm_platform_init(&rcar_du_driver, pdev);
  182. }
  183. static int rcar_du_remove(struct platform_device *pdev)
  184. {
  185. drm_platform_exit(&rcar_du_driver, pdev);
  186. return 0;
  187. }
  188. static const struct rcar_du_device_info rcar_du_r8a7779_info = {
  189. .features = 0,
  190. .num_crtcs = 2,
  191. .routes = {
  192. /* R8A7779 has two RGB outputs and one (currently unsupported)
  193. * TCON output.
  194. */
  195. [RCAR_DU_OUTPUT_DPAD0] = {
  196. .possible_crtcs = BIT(0),
  197. .encoder_type = DRM_MODE_ENCODER_NONE,
  198. },
  199. [RCAR_DU_OUTPUT_DPAD1] = {
  200. .possible_crtcs = BIT(1) | BIT(0),
  201. .encoder_type = DRM_MODE_ENCODER_NONE,
  202. },
  203. },
  204. .num_lvds = 0,
  205. };
  206. static const struct rcar_du_device_info rcar_du_r8a7790_info = {
  207. .features = RCAR_DU_FEATURE_CRTC_IRQ_CLOCK | RCAR_DU_FEATURE_ALIGN_128B
  208. | RCAR_DU_FEATURE_DEFR8,
  209. .num_crtcs = 3,
  210. .routes = {
  211. /* R8A7790 has one RGB output, two LVDS outputs and one
  212. * (currently unsupported) TCON output.
  213. */
  214. [RCAR_DU_OUTPUT_DPAD0] = {
  215. .possible_crtcs = BIT(2) | BIT(1) | BIT(0),
  216. .encoder_type = DRM_MODE_ENCODER_NONE,
  217. },
  218. [RCAR_DU_OUTPUT_LVDS0] = {
  219. .possible_crtcs = BIT(0),
  220. .encoder_type = DRM_MODE_ENCODER_LVDS,
  221. },
  222. [RCAR_DU_OUTPUT_LVDS1] = {
  223. .possible_crtcs = BIT(2) | BIT(1),
  224. .encoder_type = DRM_MODE_ENCODER_LVDS,
  225. },
  226. },
  227. .num_lvds = 2,
  228. };
  229. static const struct platform_device_id rcar_du_id_table[] = {
  230. { "rcar-du-r8a7779", (kernel_ulong_t)&rcar_du_r8a7779_info },
  231. { "rcar-du-r8a7790", (kernel_ulong_t)&rcar_du_r8a7790_info },
  232. { }
  233. };
  234. MODULE_DEVICE_TABLE(platform, rcar_du_id_table);
  235. static struct platform_driver rcar_du_platform_driver = {
  236. .probe = rcar_du_probe,
  237. .remove = rcar_du_remove,
  238. .driver = {
  239. .owner = THIS_MODULE,
  240. .name = "rcar-du",
  241. .pm = &rcar_du_pm_ops,
  242. },
  243. .id_table = rcar_du_id_table,
  244. };
  245. module_platform_driver(rcar_du_platform_driver);
  246. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  247. MODULE_DESCRIPTION("Renesas R-Car Display Unit DRM Driver");
  248. MODULE_LICENSE("GPL");