shmob_drm_drv.c 8.2 KB

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