rcar_du_drv.c 10 KB

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