shmob_drm_drv.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * shmob_drm_drv.c -- SH Mobile DRM driver
  3. *
  4. * Copyright (C) 2012 Renesas 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_crtc.h"
  24. #include "shmob_drm_drv.h"
  25. #include "shmob_drm_kms.h"
  26. #include "shmob_drm_plane.h"
  27. #include "shmob_drm_regs.h"
  28. /* -----------------------------------------------------------------------------
  29. * Hardware initialization
  30. */
  31. static int shmob_drm_init_interface(struct shmob_drm_device *sdev)
  32. {
  33. static const u32 ldmt1r[] = {
  34. [SHMOB_DRM_IFACE_RGB8] = LDMT1R_MIFTYP_RGB8,
  35. [SHMOB_DRM_IFACE_RGB9] = LDMT1R_MIFTYP_RGB9,
  36. [SHMOB_DRM_IFACE_RGB12A] = LDMT1R_MIFTYP_RGB12A,
  37. [SHMOB_DRM_IFACE_RGB12B] = LDMT1R_MIFTYP_RGB12B,
  38. [SHMOB_DRM_IFACE_RGB16] = LDMT1R_MIFTYP_RGB16,
  39. [SHMOB_DRM_IFACE_RGB18] = LDMT1R_MIFTYP_RGB18,
  40. [SHMOB_DRM_IFACE_RGB24] = LDMT1R_MIFTYP_RGB24,
  41. [SHMOB_DRM_IFACE_YUV422] = LDMT1R_MIFTYP_YCBCR,
  42. [SHMOB_DRM_IFACE_SYS8A] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS8A,
  43. [SHMOB_DRM_IFACE_SYS8B] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS8B,
  44. [SHMOB_DRM_IFACE_SYS8C] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS8C,
  45. [SHMOB_DRM_IFACE_SYS8D] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS8D,
  46. [SHMOB_DRM_IFACE_SYS9] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS9,
  47. [SHMOB_DRM_IFACE_SYS12] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS12,
  48. [SHMOB_DRM_IFACE_SYS16A] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS16A,
  49. [SHMOB_DRM_IFACE_SYS16B] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS16B,
  50. [SHMOB_DRM_IFACE_SYS16C] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS16C,
  51. [SHMOB_DRM_IFACE_SYS18] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS18,
  52. [SHMOB_DRM_IFACE_SYS24] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS24,
  53. };
  54. if (sdev->pdata->iface.interface >= ARRAY_SIZE(ldmt1r)) {
  55. dev_err(sdev->dev, "invalid interface type %u\n",
  56. sdev->pdata->iface.interface);
  57. return -EINVAL;
  58. }
  59. sdev->ldmt1r = ldmt1r[sdev->pdata->iface.interface];
  60. return 0;
  61. }
  62. static int shmob_drm_setup_clocks(struct shmob_drm_device *sdev,
  63. enum shmob_drm_clk_source clksrc)
  64. {
  65. struct clk *clk;
  66. char *clkname;
  67. switch (clksrc) {
  68. case SHMOB_DRM_CLK_BUS:
  69. clkname = "bus_clk";
  70. sdev->lddckr = LDDCKR_ICKSEL_BUS;
  71. break;
  72. case SHMOB_DRM_CLK_PERIPHERAL:
  73. clkname = "peripheral_clk";
  74. sdev->lddckr = LDDCKR_ICKSEL_MIPI;
  75. break;
  76. case SHMOB_DRM_CLK_EXTERNAL:
  77. clkname = NULL;
  78. sdev->lddckr = LDDCKR_ICKSEL_HDMI;
  79. break;
  80. default:
  81. return -EINVAL;
  82. }
  83. clk = clk_get(sdev->dev, clkname);
  84. if (IS_ERR(clk)) {
  85. dev_err(sdev->dev, "cannot get dot clock %s\n", clkname);
  86. return PTR_ERR(clk);
  87. }
  88. sdev->clock = clk;
  89. return 0;
  90. }
  91. /* -----------------------------------------------------------------------------
  92. * DRM operations
  93. */
  94. static int shmob_drm_unload(struct drm_device *dev)
  95. {
  96. struct shmob_drm_device *sdev = dev->dev_private;
  97. drm_kms_helper_poll_fini(dev);
  98. drm_mode_config_cleanup(dev);
  99. drm_vblank_cleanup(dev);
  100. drm_irq_uninstall(dev);
  101. if (sdev->clock)
  102. clk_put(sdev->clock);
  103. if (sdev->mmio)
  104. iounmap(sdev->mmio);
  105. dev->dev_private = NULL;
  106. kfree(sdev);
  107. return 0;
  108. }
  109. static int shmob_drm_load(struct drm_device *dev, unsigned long flags)
  110. {
  111. struct shmob_drm_platform_data *pdata = dev->dev->platform_data;
  112. struct platform_device *pdev = dev->platformdev;
  113. struct shmob_drm_device *sdev;
  114. struct resource *res;
  115. unsigned int i;
  116. int ret;
  117. if (pdata == NULL) {
  118. dev_err(dev->dev, "no platform data\n");
  119. return -EINVAL;
  120. }
  121. sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
  122. if (sdev == NULL) {
  123. dev_err(dev->dev, "failed to allocate private data\n");
  124. return -ENOMEM;
  125. }
  126. sdev->dev = &pdev->dev;
  127. sdev->pdata = pdata;
  128. spin_lock_init(&sdev->irq_lock);
  129. sdev->ddev = dev;
  130. dev->dev_private = sdev;
  131. /* I/O resources and clocks */
  132. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  133. if (res == NULL) {
  134. dev_err(&pdev->dev, "failed to get memory resource\n");
  135. ret = -EINVAL;
  136. goto done;
  137. }
  138. sdev->mmio = ioremap_nocache(res->start, resource_size(res));
  139. if (sdev->mmio == NULL) {
  140. dev_err(&pdev->dev, "failed to remap memory resource\n");
  141. ret = -ENOMEM;
  142. goto done;
  143. }
  144. ret = shmob_drm_setup_clocks(sdev, pdata->clk_source);
  145. if (ret < 0)
  146. goto done;
  147. ret = shmob_drm_init_interface(sdev);
  148. if (ret < 0)
  149. goto done;
  150. ret = shmob_drm_modeset_init(sdev);
  151. if (ret < 0) {
  152. dev_err(&pdev->dev, "failed to initialize mode setting\n");
  153. goto done;
  154. }
  155. for (i = 0; i < 4; ++i) {
  156. ret = shmob_drm_plane_create(sdev, i);
  157. if (ret < 0) {
  158. dev_err(&pdev->dev, "failed to create plane %u\n", i);
  159. goto done;
  160. }
  161. }
  162. ret = drm_vblank_init(dev, 1);
  163. if (ret < 0) {
  164. dev_err(&pdev->dev, "failed to initialize vblank\n");
  165. goto done;
  166. }
  167. ret = drm_irq_install(dev);
  168. if (ret < 0) {
  169. dev_err(&pdev->dev, "failed to install IRQ handler\n");
  170. goto done;
  171. }
  172. platform_set_drvdata(pdev, sdev);
  173. done:
  174. if (ret)
  175. shmob_drm_unload(dev);
  176. return ret;
  177. }
  178. static void shmob_drm_preclose(struct drm_device *dev, struct drm_file *file)
  179. {
  180. struct shmob_drm_device *sdev = dev->dev_private;
  181. shmob_drm_crtc_cancel_page_flip(&sdev->crtc, file);
  182. }
  183. static irqreturn_t shmob_drm_irq(int irq, void *arg)
  184. {
  185. struct drm_device *dev = arg;
  186. struct shmob_drm_device *sdev = dev->dev_private;
  187. unsigned long flags;
  188. u32 status;
  189. /* Acknowledge interrupts. Putting interrupt enable and interrupt flag
  190. * bits in the same register is really brain-dead design and requires
  191. * taking a spinlock.
  192. */
  193. spin_lock_irqsave(&sdev->irq_lock, flags);
  194. status = lcdc_read(sdev, LDINTR);
  195. lcdc_write(sdev, LDINTR, status ^ LDINTR_STATUS_MASK);
  196. spin_unlock_irqrestore(&sdev->irq_lock, flags);
  197. if (status & LDINTR_VES) {
  198. drm_handle_vblank(dev, 0);
  199. shmob_drm_crtc_finish_page_flip(&sdev->crtc);
  200. }
  201. return IRQ_HANDLED;
  202. }
  203. static int shmob_drm_enable_vblank(struct drm_device *dev, int crtc)
  204. {
  205. struct shmob_drm_device *sdev = dev->dev_private;
  206. shmob_drm_crtc_enable_vblank(sdev, true);
  207. return 0;
  208. }
  209. static void shmob_drm_disable_vblank(struct drm_device *dev, int crtc)
  210. {
  211. struct shmob_drm_device *sdev = dev->dev_private;
  212. shmob_drm_crtc_enable_vblank(sdev, false);
  213. }
  214. static const struct file_operations shmob_drm_fops = {
  215. .owner = THIS_MODULE,
  216. .open = drm_open,
  217. .release = drm_release,
  218. .unlocked_ioctl = drm_ioctl,
  219. #ifdef CONFIG_COMPAT
  220. .compat_ioctl = drm_compat_ioctl,
  221. #endif
  222. .poll = drm_poll,
  223. .read = drm_read,
  224. .fasync = drm_fasync,
  225. .llseek = no_llseek,
  226. .mmap = drm_gem_cma_mmap,
  227. };
  228. static struct drm_driver shmob_drm_driver = {
  229. .driver_features = DRIVER_HAVE_IRQ | DRIVER_GEM | DRIVER_MODESET,
  230. .load = shmob_drm_load,
  231. .unload = shmob_drm_unload,
  232. .preclose = shmob_drm_preclose,
  233. .irq_handler = shmob_drm_irq,
  234. .get_vblank_counter = drm_vblank_count,
  235. .enable_vblank = shmob_drm_enable_vblank,
  236. .disable_vblank = shmob_drm_disable_vblank,
  237. .gem_free_object = drm_gem_cma_free_object,
  238. .gem_vm_ops = &drm_gem_cma_vm_ops,
  239. .dumb_create = drm_gem_cma_dumb_create,
  240. .dumb_map_offset = drm_gem_cma_dumb_map_offset,
  241. .dumb_destroy = drm_gem_cma_dumb_destroy,
  242. .fops = &shmob_drm_fops,
  243. .name = "shmob-drm",
  244. .desc = "Renesas SH Mobile DRM",
  245. .date = "20120424",
  246. .major = 1,
  247. .minor = 0,
  248. };
  249. /* -----------------------------------------------------------------------------
  250. * Power management
  251. */
  252. #if CONFIG_PM_SLEEP
  253. static int shmob_drm_pm_suspend(struct device *dev)
  254. {
  255. struct shmob_drm_device *sdev = dev_get_drvdata(dev);
  256. drm_kms_helper_poll_disable(sdev->ddev);
  257. shmob_drm_crtc_suspend(&sdev->crtc);
  258. return 0;
  259. }
  260. static int shmob_drm_pm_resume(struct device *dev)
  261. {
  262. struct shmob_drm_device *sdev = dev_get_drvdata(dev);
  263. drm_modeset_lock_all(sdev->ddev);
  264. shmob_drm_crtc_resume(&sdev->crtc);
  265. drm_modeset_unlock_all(sdev->ddev);
  266. drm_kms_helper_poll_enable(sdev->ddev);
  267. return 0;
  268. }
  269. #endif
  270. static const struct dev_pm_ops shmob_drm_pm_ops = {
  271. SET_SYSTEM_SLEEP_PM_OPS(shmob_drm_pm_suspend, shmob_drm_pm_resume)
  272. };
  273. /* -----------------------------------------------------------------------------
  274. * Platform driver
  275. */
  276. static int shmob_drm_probe(struct platform_device *pdev)
  277. {
  278. return drm_platform_init(&shmob_drm_driver, pdev);
  279. }
  280. static int shmob_drm_remove(struct platform_device *pdev)
  281. {
  282. drm_platform_exit(&shmob_drm_driver, pdev);
  283. return 0;
  284. }
  285. static struct platform_driver shmob_drm_platform_driver = {
  286. .probe = shmob_drm_probe,
  287. .remove = shmob_drm_remove,
  288. .driver = {
  289. .owner = THIS_MODULE,
  290. .name = "shmob-drm",
  291. .pm = &shmob_drm_pm_ops,
  292. },
  293. };
  294. module_platform_driver(shmob_drm_platform_driver);
  295. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  296. MODULE_DESCRIPTION("Renesas SH Mobile DRM Driver");
  297. MODULE_LICENSE("GPL");