rcar_du_drv.c 12 KB

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