rcar_du_drv.c 9.1 KB

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