shmob_drm_drv.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * shmob_drm_drv.c -- SH Mobile DRM driver
  4. *
  5. * Copyright (C) 2012 Renesas Electronics Corporation
  6. *
  7. * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/io.h>
  11. #include <linux/mm.h>
  12. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/pm.h>
  15. #include <linux/slab.h>
  16. #include <drm/drmP.h>
  17. #include <drm/drm_crtc_helper.h>
  18. #include <drm/drm_gem_cma_helper.h>
  19. #include "shmob_drm_drv.h"
  20. #include "shmob_drm_kms.h"
  21. #include "shmob_drm_plane.h"
  22. #include "shmob_drm_regs.h"
  23. /* -----------------------------------------------------------------------------
  24. * Hardware initialization
  25. */
  26. static int shmob_drm_init_interface(struct shmob_drm_device *sdev)
  27. {
  28. static const u32 ldmt1r[] = {
  29. [SHMOB_DRM_IFACE_RGB8] = LDMT1R_MIFTYP_RGB8,
  30. [SHMOB_DRM_IFACE_RGB9] = LDMT1R_MIFTYP_RGB9,
  31. [SHMOB_DRM_IFACE_RGB12A] = LDMT1R_MIFTYP_RGB12A,
  32. [SHMOB_DRM_IFACE_RGB12B] = LDMT1R_MIFTYP_RGB12B,
  33. [SHMOB_DRM_IFACE_RGB16] = LDMT1R_MIFTYP_RGB16,
  34. [SHMOB_DRM_IFACE_RGB18] = LDMT1R_MIFTYP_RGB18,
  35. [SHMOB_DRM_IFACE_RGB24] = LDMT1R_MIFTYP_RGB24,
  36. [SHMOB_DRM_IFACE_YUV422] = LDMT1R_MIFTYP_YCBCR,
  37. [SHMOB_DRM_IFACE_SYS8A] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS8A,
  38. [SHMOB_DRM_IFACE_SYS8B] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS8B,
  39. [SHMOB_DRM_IFACE_SYS8C] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS8C,
  40. [SHMOB_DRM_IFACE_SYS8D] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS8D,
  41. [SHMOB_DRM_IFACE_SYS9] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS9,
  42. [SHMOB_DRM_IFACE_SYS12] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS12,
  43. [SHMOB_DRM_IFACE_SYS16A] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS16A,
  44. [SHMOB_DRM_IFACE_SYS16B] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS16B,
  45. [SHMOB_DRM_IFACE_SYS16C] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS16C,
  46. [SHMOB_DRM_IFACE_SYS18] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS18,
  47. [SHMOB_DRM_IFACE_SYS24] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS24,
  48. };
  49. if (sdev->pdata->iface.interface >= ARRAY_SIZE(ldmt1r)) {
  50. dev_err(sdev->dev, "invalid interface type %u\n",
  51. sdev->pdata->iface.interface);
  52. return -EINVAL;
  53. }
  54. sdev->ldmt1r = ldmt1r[sdev->pdata->iface.interface];
  55. return 0;
  56. }
  57. static int shmob_drm_setup_clocks(struct shmob_drm_device *sdev,
  58. enum shmob_drm_clk_source clksrc)
  59. {
  60. struct clk *clk;
  61. char *clkname;
  62. switch (clksrc) {
  63. case SHMOB_DRM_CLK_BUS:
  64. clkname = "bus_clk";
  65. sdev->lddckr = LDDCKR_ICKSEL_BUS;
  66. break;
  67. case SHMOB_DRM_CLK_PERIPHERAL:
  68. clkname = "peripheral_clk";
  69. sdev->lddckr = LDDCKR_ICKSEL_MIPI;
  70. break;
  71. case SHMOB_DRM_CLK_EXTERNAL:
  72. clkname = NULL;
  73. sdev->lddckr = LDDCKR_ICKSEL_HDMI;
  74. break;
  75. default:
  76. return -EINVAL;
  77. }
  78. clk = devm_clk_get(sdev->dev, clkname);
  79. if (IS_ERR(clk)) {
  80. dev_err(sdev->dev, "cannot get dot clock %s\n", clkname);
  81. return PTR_ERR(clk);
  82. }
  83. sdev->clock = clk;
  84. return 0;
  85. }
  86. /* -----------------------------------------------------------------------------
  87. * DRM operations
  88. */
  89. static irqreturn_t shmob_drm_irq(int irq, void *arg)
  90. {
  91. struct drm_device *dev = arg;
  92. struct shmob_drm_device *sdev = dev->dev_private;
  93. unsigned long flags;
  94. u32 status;
  95. /* Acknowledge interrupts. Putting interrupt enable and interrupt flag
  96. * bits in the same register is really brain-dead design and requires
  97. * taking a spinlock.
  98. */
  99. spin_lock_irqsave(&sdev->irq_lock, flags);
  100. status = lcdc_read(sdev, LDINTR);
  101. lcdc_write(sdev, LDINTR, status ^ LDINTR_STATUS_MASK);
  102. spin_unlock_irqrestore(&sdev->irq_lock, flags);
  103. if (status & LDINTR_VES) {
  104. drm_handle_vblank(dev, 0);
  105. shmob_drm_crtc_finish_page_flip(&sdev->crtc);
  106. }
  107. return IRQ_HANDLED;
  108. }
  109. DEFINE_DRM_GEM_CMA_FOPS(shmob_drm_fops);
  110. static struct drm_driver shmob_drm_driver = {
  111. .driver_features = DRIVER_HAVE_IRQ | DRIVER_GEM | DRIVER_MODESET
  112. | DRIVER_PRIME,
  113. .irq_handler = shmob_drm_irq,
  114. .gem_free_object_unlocked = drm_gem_cma_free_object,
  115. .gem_vm_ops = &drm_gem_cma_vm_ops,
  116. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  117. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  118. .gem_prime_import = drm_gem_prime_import,
  119. .gem_prime_export = drm_gem_prime_export,
  120. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  121. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  122. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  123. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  124. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  125. .dumb_create = drm_gem_cma_dumb_create,
  126. .fops = &shmob_drm_fops,
  127. .name = "shmob-drm",
  128. .desc = "Renesas SH Mobile DRM",
  129. .date = "20120424",
  130. .major = 1,
  131. .minor = 0,
  132. };
  133. /* -----------------------------------------------------------------------------
  134. * Power management
  135. */
  136. #ifdef CONFIG_PM_SLEEP
  137. static int shmob_drm_pm_suspend(struct device *dev)
  138. {
  139. struct shmob_drm_device *sdev = dev_get_drvdata(dev);
  140. drm_kms_helper_poll_disable(sdev->ddev);
  141. shmob_drm_crtc_suspend(&sdev->crtc);
  142. return 0;
  143. }
  144. static int shmob_drm_pm_resume(struct device *dev)
  145. {
  146. struct shmob_drm_device *sdev = dev_get_drvdata(dev);
  147. drm_modeset_lock_all(sdev->ddev);
  148. shmob_drm_crtc_resume(&sdev->crtc);
  149. drm_modeset_unlock_all(sdev->ddev);
  150. drm_kms_helper_poll_enable(sdev->ddev);
  151. return 0;
  152. }
  153. #endif
  154. static const struct dev_pm_ops shmob_drm_pm_ops = {
  155. SET_SYSTEM_SLEEP_PM_OPS(shmob_drm_pm_suspend, shmob_drm_pm_resume)
  156. };
  157. /* -----------------------------------------------------------------------------
  158. * Platform driver
  159. */
  160. static int shmob_drm_remove(struct platform_device *pdev)
  161. {
  162. struct shmob_drm_device *sdev = platform_get_drvdata(pdev);
  163. struct drm_device *ddev = sdev->ddev;
  164. drm_dev_unregister(ddev);
  165. drm_kms_helper_poll_fini(ddev);
  166. drm_mode_config_cleanup(ddev);
  167. drm_irq_uninstall(ddev);
  168. drm_dev_unref(ddev);
  169. return 0;
  170. }
  171. static int shmob_drm_probe(struct platform_device *pdev)
  172. {
  173. struct shmob_drm_platform_data *pdata = pdev->dev.platform_data;
  174. struct shmob_drm_device *sdev;
  175. struct drm_device *ddev;
  176. struct resource *res;
  177. unsigned int i;
  178. int ret;
  179. if (pdata == NULL) {
  180. dev_err(&pdev->dev, "no platform data\n");
  181. return -EINVAL;
  182. }
  183. /*
  184. * Allocate and initialize the driver private data, I/O resources and
  185. * clocks.
  186. */
  187. sdev = devm_kzalloc(&pdev->dev, sizeof(*sdev), GFP_KERNEL);
  188. if (sdev == NULL)
  189. return -ENOMEM;
  190. sdev->dev = &pdev->dev;
  191. sdev->pdata = pdata;
  192. spin_lock_init(&sdev->irq_lock);
  193. platform_set_drvdata(pdev, sdev);
  194. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  195. sdev->mmio = devm_ioremap_resource(&pdev->dev, res);
  196. if (sdev->mmio == NULL)
  197. return -ENOMEM;
  198. ret = shmob_drm_setup_clocks(sdev, pdata->clk_source);
  199. if (ret < 0)
  200. return ret;
  201. ret = shmob_drm_init_interface(sdev);
  202. if (ret < 0)
  203. return ret;
  204. /* Allocate and initialize the DRM device. */
  205. ddev = drm_dev_alloc(&shmob_drm_driver, &pdev->dev);
  206. if (IS_ERR(ddev))
  207. return PTR_ERR(ddev);
  208. sdev->ddev = ddev;
  209. ddev->dev_private = sdev;
  210. ret = shmob_drm_modeset_init(sdev);
  211. if (ret < 0) {
  212. dev_err(&pdev->dev, "failed to initialize mode setting\n");
  213. goto err_free_drm_dev;
  214. }
  215. for (i = 0; i < 4; ++i) {
  216. ret = shmob_drm_plane_create(sdev, i);
  217. if (ret < 0) {
  218. dev_err(&pdev->dev, "failed to create plane %u\n", i);
  219. goto err_modeset_cleanup;
  220. }
  221. }
  222. ret = drm_vblank_init(ddev, 1);
  223. if (ret < 0) {
  224. dev_err(&pdev->dev, "failed to initialize vblank\n");
  225. goto err_modeset_cleanup;
  226. }
  227. ret = drm_irq_install(ddev, platform_get_irq(pdev, 0));
  228. if (ret < 0) {
  229. dev_err(&pdev->dev, "failed to install IRQ handler\n");
  230. goto err_modeset_cleanup;
  231. }
  232. /*
  233. * Register the DRM device with the core and the connectors with
  234. * sysfs.
  235. */
  236. ret = drm_dev_register(ddev, 0);
  237. if (ret < 0)
  238. goto err_irq_uninstall;
  239. return 0;
  240. err_irq_uninstall:
  241. drm_irq_uninstall(ddev);
  242. err_modeset_cleanup:
  243. drm_kms_helper_poll_fini(ddev);
  244. drm_mode_config_cleanup(ddev);
  245. err_free_drm_dev:
  246. drm_dev_unref(ddev);
  247. return ret;
  248. }
  249. static struct platform_driver shmob_drm_platform_driver = {
  250. .probe = shmob_drm_probe,
  251. .remove = shmob_drm_remove,
  252. .driver = {
  253. .name = "shmob-drm",
  254. .pm = &shmob_drm_pm_ops,
  255. },
  256. };
  257. module_platform_driver(shmob_drm_platform_driver);
  258. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  259. MODULE_DESCRIPTION("Renesas SH Mobile DRM Driver");
  260. MODULE_LICENSE("GPL");