rcar_du_drv.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * rcar_du_drv.c -- R-Car Display Unit DRM driver
  3. *
  4. * Copyright (C) 2013-2015 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_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. .gen = 2,
  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. .port = 0,
  43. },
  44. [RCAR_DU_OUTPUT_DPAD1] = {
  45. .possible_crtcs = BIT(1) | BIT(0),
  46. .port = 1,
  47. },
  48. },
  49. .num_lvds = 0,
  50. };
  51. static const struct rcar_du_device_info rcar_du_r8a7790_info = {
  52. .gen = 2,
  53. .features = RCAR_DU_FEATURE_CRTC_IRQ_CLOCK
  54. | RCAR_DU_FEATURE_EXT_CTRL_REGS,
  55. .quirks = RCAR_DU_QUIRK_ALIGN_128B | RCAR_DU_QUIRK_LVDS_LANES,
  56. .num_crtcs = 3,
  57. .routes = {
  58. /* R8A7790 has one RGB output, two LVDS outputs and one
  59. * (currently unsupported) TCON output.
  60. */
  61. [RCAR_DU_OUTPUT_DPAD0] = {
  62. .possible_crtcs = BIT(2) | BIT(1) | BIT(0),
  63. .port = 0,
  64. },
  65. [RCAR_DU_OUTPUT_LVDS0] = {
  66. .possible_crtcs = BIT(0),
  67. .port = 1,
  68. },
  69. [RCAR_DU_OUTPUT_LVDS1] = {
  70. .possible_crtcs = BIT(2) | BIT(1),
  71. .port = 2,
  72. },
  73. },
  74. .num_lvds = 2,
  75. };
  76. /* M2-W (r8a7791) and M2-N (r8a7793) are identical */
  77. static const struct rcar_du_device_info rcar_du_r8a7791_info = {
  78. .gen = 2,
  79. .features = RCAR_DU_FEATURE_CRTC_IRQ_CLOCK
  80. | RCAR_DU_FEATURE_EXT_CTRL_REGS,
  81. .num_crtcs = 2,
  82. .routes = {
  83. /* R8A779[13] has one RGB output, one LVDS output and one
  84. * (currently unsupported) TCON output.
  85. */
  86. [RCAR_DU_OUTPUT_DPAD0] = {
  87. .possible_crtcs = BIT(1) | BIT(0),
  88. .port = 0,
  89. },
  90. [RCAR_DU_OUTPUT_LVDS0] = {
  91. .possible_crtcs = BIT(0),
  92. .port = 1,
  93. },
  94. },
  95. .num_lvds = 1,
  96. };
  97. static const struct rcar_du_device_info rcar_du_r8a7792_info = {
  98. .gen = 2,
  99. .features = RCAR_DU_FEATURE_CRTC_IRQ_CLOCK
  100. | RCAR_DU_FEATURE_EXT_CTRL_REGS,
  101. .num_crtcs = 2,
  102. .routes = {
  103. /* R8A7792 has two RGB outputs. */
  104. [RCAR_DU_OUTPUT_DPAD0] = {
  105. .possible_crtcs = BIT(0),
  106. .port = 0,
  107. },
  108. [RCAR_DU_OUTPUT_DPAD1] = {
  109. .possible_crtcs = BIT(1),
  110. .port = 1,
  111. },
  112. },
  113. .num_lvds = 0,
  114. };
  115. static const struct rcar_du_device_info rcar_du_r8a7794_info = {
  116. .gen = 2,
  117. .features = RCAR_DU_FEATURE_CRTC_IRQ_CLOCK
  118. | RCAR_DU_FEATURE_EXT_CTRL_REGS,
  119. .num_crtcs = 2,
  120. .routes = {
  121. /* R8A7794 has two RGB outputs and one (currently unsupported)
  122. * TCON output.
  123. */
  124. [RCAR_DU_OUTPUT_DPAD0] = {
  125. .possible_crtcs = BIT(0),
  126. .port = 0,
  127. },
  128. [RCAR_DU_OUTPUT_DPAD1] = {
  129. .possible_crtcs = BIT(1),
  130. .port = 1,
  131. },
  132. },
  133. .num_lvds = 0,
  134. };
  135. static const struct rcar_du_device_info rcar_du_r8a7795_info = {
  136. .gen = 3,
  137. .features = RCAR_DU_FEATURE_CRTC_IRQ_CLOCK
  138. | RCAR_DU_FEATURE_EXT_CTRL_REGS
  139. | RCAR_DU_FEATURE_VSP1_SOURCE,
  140. .num_crtcs = 4,
  141. .routes = {
  142. /* R8A7795 has one RGB output, two HDMI outputs and one
  143. * LVDS output.
  144. */
  145. [RCAR_DU_OUTPUT_DPAD0] = {
  146. .possible_crtcs = BIT(3),
  147. .port = 0,
  148. },
  149. [RCAR_DU_OUTPUT_HDMI0] = {
  150. .possible_crtcs = BIT(1),
  151. .port = 1,
  152. },
  153. [RCAR_DU_OUTPUT_HDMI1] = {
  154. .possible_crtcs = BIT(2),
  155. .port = 2,
  156. },
  157. [RCAR_DU_OUTPUT_LVDS0] = {
  158. .possible_crtcs = BIT(0),
  159. .port = 3,
  160. },
  161. },
  162. .num_lvds = 1,
  163. .dpll_ch = BIT(1) | BIT(2),
  164. };
  165. static const struct rcar_du_device_info rcar_du_r8a7796_info = {
  166. .gen = 3,
  167. .features = RCAR_DU_FEATURE_CRTC_IRQ_CLOCK
  168. | RCAR_DU_FEATURE_EXT_CTRL_REGS
  169. | RCAR_DU_FEATURE_VSP1_SOURCE,
  170. .num_crtcs = 3,
  171. .routes = {
  172. /* R8A7796 has one RGB output, one LVDS output and one
  173. * (currently unsupported) HDMI output.
  174. */
  175. [RCAR_DU_OUTPUT_DPAD0] = {
  176. .possible_crtcs = BIT(2),
  177. .port = 0,
  178. },
  179. [RCAR_DU_OUTPUT_LVDS0] = {
  180. .possible_crtcs = BIT(0),
  181. .port = 2,
  182. },
  183. },
  184. .num_lvds = 1,
  185. };
  186. static const struct of_device_id rcar_du_of_table[] = {
  187. { .compatible = "renesas,du-r8a7779", .data = &rcar_du_r8a7779_info },
  188. { .compatible = "renesas,du-r8a7790", .data = &rcar_du_r8a7790_info },
  189. { .compatible = "renesas,du-r8a7791", .data = &rcar_du_r8a7791_info },
  190. { .compatible = "renesas,du-r8a7792", .data = &rcar_du_r8a7792_info },
  191. { .compatible = "renesas,du-r8a7793", .data = &rcar_du_r8a7791_info },
  192. { .compatible = "renesas,du-r8a7794", .data = &rcar_du_r8a7794_info },
  193. { .compatible = "renesas,du-r8a7795", .data = &rcar_du_r8a7795_info },
  194. { .compatible = "renesas,du-r8a7796", .data = &rcar_du_r8a7796_info },
  195. { }
  196. };
  197. MODULE_DEVICE_TABLE(of, rcar_du_of_table);
  198. /* -----------------------------------------------------------------------------
  199. * DRM operations
  200. */
  201. static void rcar_du_lastclose(struct drm_device *dev)
  202. {
  203. struct rcar_du_device *rcdu = dev->dev_private;
  204. drm_fbdev_cma_restore_mode(rcdu->fbdev);
  205. }
  206. DEFINE_DRM_GEM_CMA_FOPS(rcar_du_fops);
  207. static struct drm_driver rcar_du_driver = {
  208. .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME
  209. | DRIVER_ATOMIC,
  210. .lastclose = rcar_du_lastclose,
  211. .gem_free_object_unlocked = drm_gem_cma_free_object,
  212. .gem_vm_ops = &drm_gem_cma_vm_ops,
  213. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  214. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  215. .gem_prime_import = drm_gem_prime_import,
  216. .gem_prime_export = drm_gem_prime_export,
  217. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  218. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  219. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  220. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  221. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  222. .dumb_create = rcar_du_dumb_create,
  223. .dumb_map_offset = drm_gem_cma_dumb_map_offset,
  224. .dumb_destroy = drm_gem_dumb_destroy,
  225. .fops = &rcar_du_fops,
  226. .name = "rcar-du",
  227. .desc = "Renesas R-Car Display Unit",
  228. .date = "20130110",
  229. .major = 1,
  230. .minor = 0,
  231. };
  232. /* -----------------------------------------------------------------------------
  233. * Power management
  234. */
  235. #ifdef CONFIG_PM_SLEEP
  236. static int rcar_du_pm_suspend(struct device *dev)
  237. {
  238. struct rcar_du_device *rcdu = dev_get_drvdata(dev);
  239. drm_kms_helper_poll_disable(rcdu->ddev);
  240. /* TODO Suspend the CRTC */
  241. return 0;
  242. }
  243. static int rcar_du_pm_resume(struct device *dev)
  244. {
  245. struct rcar_du_device *rcdu = dev_get_drvdata(dev);
  246. /* TODO Resume the CRTC */
  247. drm_kms_helper_poll_enable(rcdu->ddev);
  248. return 0;
  249. }
  250. #endif
  251. static const struct dev_pm_ops rcar_du_pm_ops = {
  252. SET_SYSTEM_SLEEP_PM_OPS(rcar_du_pm_suspend, rcar_du_pm_resume)
  253. };
  254. /* -----------------------------------------------------------------------------
  255. * Platform driver
  256. */
  257. static int rcar_du_remove(struct platform_device *pdev)
  258. {
  259. struct rcar_du_device *rcdu = platform_get_drvdata(pdev);
  260. struct drm_device *ddev = rcdu->ddev;
  261. drm_dev_unregister(ddev);
  262. if (rcdu->fbdev)
  263. drm_fbdev_cma_fini(rcdu->fbdev);
  264. drm_kms_helper_poll_fini(ddev);
  265. drm_mode_config_cleanup(ddev);
  266. drm_dev_unref(ddev);
  267. return 0;
  268. }
  269. static int rcar_du_probe(struct platform_device *pdev)
  270. {
  271. struct rcar_du_device *rcdu;
  272. struct drm_device *ddev;
  273. struct resource *mem;
  274. int ret;
  275. /* Allocate and initialize the R-Car device structure. */
  276. rcdu = devm_kzalloc(&pdev->dev, sizeof(*rcdu), GFP_KERNEL);
  277. if (rcdu == NULL)
  278. return -ENOMEM;
  279. rcdu->dev = &pdev->dev;
  280. rcdu->info = of_device_get_match_data(rcdu->dev);
  281. platform_set_drvdata(pdev, rcdu);
  282. /* I/O resources */
  283. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  284. rcdu->mmio = devm_ioremap_resource(&pdev->dev, mem);
  285. if (IS_ERR(rcdu->mmio))
  286. return PTR_ERR(rcdu->mmio);
  287. /* DRM/KMS objects */
  288. ddev = drm_dev_alloc(&rcar_du_driver, &pdev->dev);
  289. if (IS_ERR(ddev))
  290. return PTR_ERR(ddev);
  291. rcdu->ddev = ddev;
  292. ddev->dev_private = rcdu;
  293. ret = rcar_du_modeset_init(rcdu);
  294. if (ret < 0) {
  295. if (ret != -EPROBE_DEFER)
  296. dev_err(&pdev->dev,
  297. "failed to initialize DRM/KMS (%d)\n", ret);
  298. goto error;
  299. }
  300. ddev->irq_enabled = 1;
  301. /* Register the DRM device with the core and the connectors with
  302. * sysfs.
  303. */
  304. ret = drm_dev_register(ddev, 0);
  305. if (ret)
  306. goto error;
  307. DRM_INFO("Device %s probed\n", dev_name(&pdev->dev));
  308. return 0;
  309. error:
  310. rcar_du_remove(pdev);
  311. return ret;
  312. }
  313. static struct platform_driver rcar_du_platform_driver = {
  314. .probe = rcar_du_probe,
  315. .remove = rcar_du_remove,
  316. .driver = {
  317. .name = "rcar-du",
  318. .pm = &rcar_du_pm_ops,
  319. .of_match_table = rcar_du_of_table,
  320. },
  321. };
  322. module_platform_driver(rcar_du_platform_driver);
  323. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  324. MODULE_DESCRIPTION("Renesas R-Car Display Unit DRM Driver");
  325. MODULE_LICENSE("GPL");