rcar_du_drv.c 8.6 KB

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