rcar_du_drv.c 10 KB

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