rcar_du_drv.c 9.0 KB

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