rcar_du_drv.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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_platform_data *pdata = pdev->dev.platform_data;
  132. struct rcar_du_device *rcdu;
  133. struct resource *mem;
  134. int ret;
  135. if (pdata == NULL && np == NULL) {
  136. dev_err(dev->dev, "no platform data\n");
  137. return -ENODEV;
  138. }
  139. rcdu = devm_kzalloc(&pdev->dev, sizeof(*rcdu), GFP_KERNEL);
  140. if (rcdu == NULL) {
  141. dev_err(dev->dev, "failed to allocate private data\n");
  142. return -ENOMEM;
  143. }
  144. rcdu->dev = &pdev->dev;
  145. rcdu->pdata = pdata;
  146. rcdu->info = np ? of_match_device(rcar_du_of_table, rcdu->dev)->data
  147. : (void *)platform_get_device_id(pdev)->driver_data;
  148. rcdu->ddev = dev;
  149. dev->dev_private = rcdu;
  150. /* I/O resources */
  151. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  152. rcdu->mmio = devm_ioremap_resource(&pdev->dev, mem);
  153. if (IS_ERR(rcdu->mmio))
  154. return PTR_ERR(rcdu->mmio);
  155. /* DRM/KMS objects */
  156. ret = rcar_du_modeset_init(rcdu);
  157. if (ret < 0) {
  158. dev_err(&pdev->dev, "failed to initialize DRM/KMS\n");
  159. goto done;
  160. }
  161. /* vblank handling */
  162. ret = drm_vblank_init(dev, (1 << rcdu->num_crtcs) - 1);
  163. if (ret < 0) {
  164. dev_err(&pdev->dev, "failed to initialize vblank\n");
  165. goto done;
  166. }
  167. dev->irq_enabled = 1;
  168. platform_set_drvdata(pdev, rcdu);
  169. done:
  170. if (ret)
  171. rcar_du_unload(dev);
  172. return ret;
  173. }
  174. static void rcar_du_preclose(struct drm_device *dev, struct drm_file *file)
  175. {
  176. struct rcar_du_device *rcdu = dev->dev_private;
  177. unsigned int i;
  178. for (i = 0; i < rcdu->num_crtcs; ++i)
  179. rcar_du_crtc_cancel_page_flip(&rcdu->crtcs[i], file);
  180. }
  181. static void rcar_du_lastclose(struct drm_device *dev)
  182. {
  183. struct rcar_du_device *rcdu = dev->dev_private;
  184. drm_fbdev_cma_restore_mode(rcdu->fbdev);
  185. }
  186. static int rcar_du_enable_vblank(struct drm_device *dev, int crtc)
  187. {
  188. struct rcar_du_device *rcdu = dev->dev_private;
  189. rcar_du_crtc_enable_vblank(&rcdu->crtcs[crtc], true);
  190. return 0;
  191. }
  192. static void rcar_du_disable_vblank(struct drm_device *dev, int crtc)
  193. {
  194. struct rcar_du_device *rcdu = dev->dev_private;
  195. rcar_du_crtc_enable_vblank(&rcdu->crtcs[crtc], false);
  196. }
  197. static const struct file_operations rcar_du_fops = {
  198. .owner = THIS_MODULE,
  199. .open = drm_open,
  200. .release = drm_release,
  201. .unlocked_ioctl = drm_ioctl,
  202. #ifdef CONFIG_COMPAT
  203. .compat_ioctl = drm_compat_ioctl,
  204. #endif
  205. .poll = drm_poll,
  206. .read = drm_read,
  207. .llseek = no_llseek,
  208. .mmap = drm_gem_cma_mmap,
  209. };
  210. static struct drm_driver rcar_du_driver = {
  211. .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME,
  212. .load = rcar_du_load,
  213. .unload = rcar_du_unload,
  214. .preclose = rcar_du_preclose,
  215. .lastclose = rcar_du_lastclose,
  216. .set_busid = drm_platform_set_busid,
  217. .get_vblank_counter = drm_vblank_count,
  218. .enable_vblank = rcar_du_enable_vblank,
  219. .disable_vblank = rcar_du_disable_vblank,
  220. .gem_free_object = drm_gem_cma_free_object,
  221. .gem_vm_ops = &drm_gem_cma_vm_ops,
  222. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  223. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  224. .gem_prime_import = drm_gem_prime_import,
  225. .gem_prime_export = drm_gem_prime_export,
  226. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  227. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  228. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  229. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  230. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  231. .dumb_create = rcar_du_dumb_create,
  232. .dumb_map_offset = drm_gem_cma_dumb_map_offset,
  233. .dumb_destroy = drm_gem_dumb_destroy,
  234. .fops = &rcar_du_fops,
  235. .name = "rcar-du",
  236. .desc = "Renesas R-Car Display Unit",
  237. .date = "20130110",
  238. .major = 1,
  239. .minor = 0,
  240. };
  241. /* -----------------------------------------------------------------------------
  242. * Power management
  243. */
  244. #ifdef CONFIG_PM_SLEEP
  245. static int rcar_du_pm_suspend(struct device *dev)
  246. {
  247. struct rcar_du_device *rcdu = dev_get_drvdata(dev);
  248. drm_kms_helper_poll_disable(rcdu->ddev);
  249. /* TODO Suspend the CRTC */
  250. return 0;
  251. }
  252. static int rcar_du_pm_resume(struct device *dev)
  253. {
  254. struct rcar_du_device *rcdu = dev_get_drvdata(dev);
  255. /* TODO Resume the CRTC */
  256. drm_kms_helper_poll_enable(rcdu->ddev);
  257. return 0;
  258. }
  259. #endif
  260. static const struct dev_pm_ops rcar_du_pm_ops = {
  261. SET_SYSTEM_SLEEP_PM_OPS(rcar_du_pm_suspend, rcar_du_pm_resume)
  262. };
  263. /* -----------------------------------------------------------------------------
  264. * Platform driver
  265. */
  266. static int rcar_du_probe(struct platform_device *pdev)
  267. {
  268. return drm_platform_init(&rcar_du_driver, pdev);
  269. }
  270. static int rcar_du_remove(struct platform_device *pdev)
  271. {
  272. struct rcar_du_device *rcdu = platform_get_drvdata(pdev);
  273. drm_put_dev(rcdu->ddev);
  274. return 0;
  275. }
  276. static struct platform_driver rcar_du_platform_driver = {
  277. .probe = rcar_du_probe,
  278. .remove = rcar_du_remove,
  279. .driver = {
  280. .owner = THIS_MODULE,
  281. .name = "rcar-du",
  282. .pm = &rcar_du_pm_ops,
  283. .of_match_table = rcar_du_of_table,
  284. },
  285. .id_table = rcar_du_id_table,
  286. };
  287. module_platform_driver(rcar_du_platform_driver);
  288. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  289. MODULE_DESCRIPTION("Renesas R-Car Display Unit DRM Driver");
  290. MODULE_LICENSE("GPL");