rcar_du_drv.c 9.3 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_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_r8a7794_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. /* R8A7794 has two RGB outputs and one (currently unsupported)
  112. * TCON output.
  113. */
  114. [RCAR_DU_OUTPUT_DPAD0] = {
  115. .possible_crtcs = BIT(0),
  116. .encoder_type = DRM_MODE_ENCODER_NONE,
  117. .port = 0,
  118. },
  119. [RCAR_DU_OUTPUT_DPAD1] = {
  120. .possible_crtcs = BIT(1),
  121. .encoder_type = DRM_MODE_ENCODER_NONE,
  122. .port = 1,
  123. },
  124. },
  125. .num_lvds = 0,
  126. };
  127. static const struct rcar_du_device_info rcar_du_r8a7795_info = {
  128. .gen = 3,
  129. .features = RCAR_DU_FEATURE_CRTC_IRQ_CLOCK
  130. | RCAR_DU_FEATURE_EXT_CTRL_REGS
  131. | RCAR_DU_FEATURE_VSP1_SOURCE,
  132. .num_crtcs = 4,
  133. .routes = {
  134. /* R8A7795 has one RGB output, one LVDS output and two
  135. * (currently unsupported) HDMI outputs.
  136. */
  137. [RCAR_DU_OUTPUT_DPAD0] = {
  138. .possible_crtcs = BIT(3),
  139. .encoder_type = DRM_MODE_ENCODER_NONE,
  140. .port = 0,
  141. },
  142. [RCAR_DU_OUTPUT_LVDS0] = {
  143. .possible_crtcs = BIT(0),
  144. .encoder_type = DRM_MODE_ENCODER_LVDS,
  145. .port = 3,
  146. },
  147. },
  148. .num_lvds = 1,
  149. };
  150. static const struct of_device_id rcar_du_of_table[] = {
  151. { .compatible = "renesas,du-r8a7779", .data = &rcar_du_r8a7779_info },
  152. { .compatible = "renesas,du-r8a7790", .data = &rcar_du_r8a7790_info },
  153. { .compatible = "renesas,du-r8a7791", .data = &rcar_du_r8a7791_info },
  154. { .compatible = "renesas,du-r8a7793", .data = &rcar_du_r8a7791_info },
  155. { .compatible = "renesas,du-r8a7794", .data = &rcar_du_r8a7794_info },
  156. { .compatible = "renesas,du-r8a7795", .data = &rcar_du_r8a7795_info },
  157. { }
  158. };
  159. MODULE_DEVICE_TABLE(of, rcar_du_of_table);
  160. /* -----------------------------------------------------------------------------
  161. * DRM operations
  162. */
  163. static void rcar_du_lastclose(struct drm_device *dev)
  164. {
  165. struct rcar_du_device *rcdu = dev->dev_private;
  166. drm_fbdev_cma_restore_mode(rcdu->fbdev);
  167. }
  168. static int rcar_du_enable_vblank(struct drm_device *dev, unsigned int pipe)
  169. {
  170. struct rcar_du_device *rcdu = dev->dev_private;
  171. rcar_du_crtc_enable_vblank(&rcdu->crtcs[pipe], true);
  172. return 0;
  173. }
  174. static void rcar_du_disable_vblank(struct drm_device *dev, unsigned int pipe)
  175. {
  176. struct rcar_du_device *rcdu = dev->dev_private;
  177. rcar_du_crtc_enable_vblank(&rcdu->crtcs[pipe], false);
  178. }
  179. static const struct file_operations rcar_du_fops = {
  180. .owner = THIS_MODULE,
  181. .open = drm_open,
  182. .release = drm_release,
  183. .unlocked_ioctl = drm_ioctl,
  184. #ifdef CONFIG_COMPAT
  185. .compat_ioctl = drm_compat_ioctl,
  186. #endif
  187. .poll = drm_poll,
  188. .read = drm_read,
  189. .llseek = no_llseek,
  190. .mmap = drm_gem_cma_mmap,
  191. };
  192. static struct drm_driver rcar_du_driver = {
  193. .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME
  194. | DRIVER_ATOMIC,
  195. .lastclose = rcar_du_lastclose,
  196. .get_vblank_counter = drm_vblank_no_hw_counter,
  197. .enable_vblank = rcar_du_enable_vblank,
  198. .disable_vblank = rcar_du_disable_vblank,
  199. .gem_free_object_unlocked = drm_gem_cma_free_object,
  200. .gem_vm_ops = &drm_gem_cma_vm_ops,
  201. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  202. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  203. .gem_prime_import = drm_gem_prime_import,
  204. .gem_prime_export = drm_gem_prime_export,
  205. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  206. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  207. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  208. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  209. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  210. .dumb_create = rcar_du_dumb_create,
  211. .dumb_map_offset = drm_gem_cma_dumb_map_offset,
  212. .dumb_destroy = drm_gem_dumb_destroy,
  213. .fops = &rcar_du_fops,
  214. .name = "rcar-du",
  215. .desc = "Renesas R-Car Display Unit",
  216. .date = "20130110",
  217. .major = 1,
  218. .minor = 0,
  219. };
  220. /* -----------------------------------------------------------------------------
  221. * Power management
  222. */
  223. #ifdef CONFIG_PM_SLEEP
  224. static int rcar_du_pm_suspend(struct device *dev)
  225. {
  226. struct rcar_du_device *rcdu = dev_get_drvdata(dev);
  227. drm_kms_helper_poll_disable(rcdu->ddev);
  228. /* TODO Suspend the CRTC */
  229. return 0;
  230. }
  231. static int rcar_du_pm_resume(struct device *dev)
  232. {
  233. struct rcar_du_device *rcdu = dev_get_drvdata(dev);
  234. /* TODO Resume the CRTC */
  235. drm_kms_helper_poll_enable(rcdu->ddev);
  236. return 0;
  237. }
  238. #endif
  239. static const struct dev_pm_ops rcar_du_pm_ops = {
  240. SET_SYSTEM_SLEEP_PM_OPS(rcar_du_pm_suspend, rcar_du_pm_resume)
  241. };
  242. /* -----------------------------------------------------------------------------
  243. * Platform driver
  244. */
  245. static int rcar_du_remove(struct platform_device *pdev)
  246. {
  247. struct rcar_du_device *rcdu = platform_get_drvdata(pdev);
  248. struct drm_device *ddev = rcdu->ddev;
  249. drm_dev_unregister(ddev);
  250. if (rcdu->fbdev)
  251. drm_fbdev_cma_fini(rcdu->fbdev);
  252. drm_kms_helper_poll_fini(ddev);
  253. drm_mode_config_cleanup(ddev);
  254. drm_dev_unref(ddev);
  255. return 0;
  256. }
  257. static int rcar_du_probe(struct platform_device *pdev)
  258. {
  259. struct device_node *np = pdev->dev.of_node;
  260. struct rcar_du_device *rcdu;
  261. struct drm_device *ddev;
  262. struct resource *mem;
  263. int ret;
  264. if (np == NULL) {
  265. dev_err(&pdev->dev, "no device tree node\n");
  266. return -ENODEV;
  267. }
  268. /* Allocate and initialize the R-Car device structure. */
  269. rcdu = devm_kzalloc(&pdev->dev, sizeof(*rcdu), GFP_KERNEL);
  270. if (rcdu == NULL)
  271. return -ENOMEM;
  272. init_waitqueue_head(&rcdu->commit.wait);
  273. rcdu->dev = &pdev->dev;
  274. rcdu->info = of_match_device(rcar_du_of_table, rcdu->dev)->data;
  275. platform_set_drvdata(pdev, rcdu);
  276. /* I/O resources */
  277. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  278. rcdu->mmio = devm_ioremap_resource(&pdev->dev, mem);
  279. if (IS_ERR(rcdu->mmio))
  280. return PTR_ERR(rcdu->mmio);
  281. /* DRM/KMS objects */
  282. ddev = drm_dev_alloc(&rcar_du_driver, &pdev->dev);
  283. if (IS_ERR(ddev))
  284. return PTR_ERR(ddev);
  285. rcdu->ddev = ddev;
  286. ddev->dev_private = rcdu;
  287. ret = rcar_du_modeset_init(rcdu);
  288. if (ret < 0) {
  289. if (ret != -EPROBE_DEFER)
  290. dev_err(&pdev->dev,
  291. "failed to initialize DRM/KMS (%d)\n", ret);
  292. goto error;
  293. }
  294. ddev->irq_enabled = 1;
  295. /* Register the DRM device with the core and the connectors with
  296. * sysfs.
  297. */
  298. ret = drm_dev_register(ddev, 0);
  299. if (ret)
  300. goto error;
  301. DRM_INFO("Device %s probed\n", dev_name(&pdev->dev));
  302. return 0;
  303. error:
  304. rcar_du_remove(pdev);
  305. return ret;
  306. }
  307. static struct platform_driver rcar_du_platform_driver = {
  308. .probe = rcar_du_probe,
  309. .remove = rcar_du_remove,
  310. .driver = {
  311. .name = "rcar-du",
  312. .pm = &rcar_du_pm_ops,
  313. .of_match_table = rcar_du_of_table,
  314. },
  315. };
  316. module_platform_driver(rcar_du_platform_driver);
  317. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  318. MODULE_DESCRIPTION("Renesas R-Car Display Unit DRM Driver");
  319. MODULE_LICENSE("GPL");