sti_vtg.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * Copyright (C) STMicroelectronics SA 2014
  3. * Authors: Benjamin Gaignard <benjamin.gaignard@st.com>
  4. * Fabien Dessenne <fabien.dessenne@st.com>
  5. * Vincent Abriou <vincent.abriou@st.com>
  6. * for STMicroelectronics.
  7. * License terms: GNU General Public License (GPL), version 2
  8. */
  9. #include <linux/module.h>
  10. #include <linux/notifier.h>
  11. #include <linux/platform_device.h>
  12. #include <drm/drmP.h>
  13. #include "sti_vtg.h"
  14. #define VTG_TYPE_MASTER 0
  15. #define VTG_TYPE_SLAVE_BY_EXT0 1
  16. /* registers offset */
  17. #define VTG_MODE 0x0000
  18. #define VTG_CLKLN 0x0008
  19. #define VTG_HLFLN 0x000C
  20. #define VTG_DRST_AUTOC 0x0010
  21. #define VTG_VID_TFO 0x0040
  22. #define VTG_VID_TFS 0x0044
  23. #define VTG_VID_BFO 0x0048
  24. #define VTG_VID_BFS 0x004C
  25. #define VTG_HOST_ITS 0x0078
  26. #define VTG_HOST_ITS_BCLR 0x007C
  27. #define VTG_HOST_ITM_BCLR 0x0088
  28. #define VTG_HOST_ITM_BSET 0x008C
  29. #define VTG_H_HD_1 0x00C0
  30. #define VTG_TOP_V_VD_1 0x00C4
  31. #define VTG_BOT_V_VD_1 0x00C8
  32. #define VTG_TOP_V_HD_1 0x00CC
  33. #define VTG_BOT_V_HD_1 0x00D0
  34. #define VTG_H_HD_2 0x00E0
  35. #define VTG_TOP_V_VD_2 0x00E4
  36. #define VTG_BOT_V_VD_2 0x00E8
  37. #define VTG_TOP_V_HD_2 0x00EC
  38. #define VTG_BOT_V_HD_2 0x00F0
  39. #define VTG_H_HD_3 0x0100
  40. #define VTG_TOP_V_VD_3 0x0104
  41. #define VTG_BOT_V_VD_3 0x0108
  42. #define VTG_TOP_V_HD_3 0x010C
  43. #define VTG_BOT_V_HD_3 0x0110
  44. #define VTG_IRQ_BOTTOM BIT(0)
  45. #define VTG_IRQ_TOP BIT(1)
  46. #define VTG_IRQ_MASK (VTG_IRQ_TOP | VTG_IRQ_BOTTOM)
  47. /* delay introduced by the Arbitrary Waveform Generator in nb of pixels */
  48. #define AWG_DELAY_HD (-9)
  49. #define AWG_DELAY_ED (-8)
  50. #define AWG_DELAY_SD (-7)
  51. LIST_HEAD(vtg_lookup);
  52. /**
  53. * STI VTG structure
  54. *
  55. * @dev: pointer to device driver
  56. * @data: data associated to the device
  57. * @irq: VTG irq
  58. * @type: VTG type (main or aux)
  59. * @notifier_list: notifier callback
  60. * @crtc_id: the crtc id for vblank event
  61. * @slave: slave vtg
  62. * @link: List node to link the structure in lookup list
  63. */
  64. struct sti_vtg {
  65. struct device *dev;
  66. struct device_node *np;
  67. void __iomem *regs;
  68. int irq;
  69. u32 irq_status;
  70. struct raw_notifier_head notifier_list;
  71. int crtc_id;
  72. struct sti_vtg *slave;
  73. struct list_head link;
  74. };
  75. static void vtg_register(struct sti_vtg *vtg)
  76. {
  77. list_add_tail(&vtg->link, &vtg_lookup);
  78. }
  79. struct sti_vtg *of_vtg_find(struct device_node *np)
  80. {
  81. struct sti_vtg *vtg;
  82. list_for_each_entry(vtg, &vtg_lookup, link) {
  83. if (vtg->np == np)
  84. return vtg;
  85. }
  86. return NULL;
  87. }
  88. EXPORT_SYMBOL(of_vtg_find);
  89. static void vtg_reset(struct sti_vtg *vtg)
  90. {
  91. /* reset slave and then master */
  92. if (vtg->slave)
  93. vtg_reset(vtg->slave);
  94. writel(1, vtg->regs + VTG_DRST_AUTOC);
  95. }
  96. static void vtg_set_mode(struct sti_vtg *vtg,
  97. int type, const struct drm_display_mode *mode)
  98. {
  99. u32 tmp;
  100. if (vtg->slave)
  101. vtg_set_mode(vtg->slave, VTG_TYPE_SLAVE_BY_EXT0, mode);
  102. writel(mode->htotal, vtg->regs + VTG_CLKLN);
  103. writel(mode->vtotal * 2, vtg->regs + VTG_HLFLN);
  104. tmp = (mode->vtotal - mode->vsync_start + 1) << 16;
  105. tmp |= mode->htotal - mode->hsync_start;
  106. writel(tmp, vtg->regs + VTG_VID_TFO);
  107. writel(tmp, vtg->regs + VTG_VID_BFO);
  108. tmp = (mode->vdisplay + mode->vtotal - mode->vsync_start + 1) << 16;
  109. tmp |= mode->hdisplay + mode->htotal - mode->hsync_start;
  110. writel(tmp, vtg->regs + VTG_VID_TFS);
  111. writel(tmp, vtg->regs + VTG_VID_BFS);
  112. /* prepare VTG set 1 and 2 for HDMI and VTG set 3 for HD DAC */
  113. tmp = (mode->hsync_end - mode->hsync_start) << 16;
  114. writel(tmp, vtg->regs + VTG_H_HD_1);
  115. writel(tmp, vtg->regs + VTG_H_HD_2);
  116. tmp = (mode->vsync_end - mode->vsync_start + 1) << 16;
  117. tmp |= 1;
  118. writel(tmp, vtg->regs + VTG_TOP_V_VD_1);
  119. writel(tmp, vtg->regs + VTG_BOT_V_VD_1);
  120. writel(0, vtg->regs + VTG_TOP_V_HD_1);
  121. writel(0, vtg->regs + VTG_BOT_V_HD_1);
  122. /* prepare VTG set 2 for for HD DCS */
  123. writel(tmp, vtg->regs + VTG_TOP_V_VD_2);
  124. writel(tmp, vtg->regs + VTG_BOT_V_VD_2);
  125. writel(0, vtg->regs + VTG_TOP_V_HD_2);
  126. writel(0, vtg->regs + VTG_BOT_V_HD_2);
  127. /* prepare VTG set 3 for HD Analog in HD mode */
  128. tmp = (mode->hsync_end - mode->hsync_start + AWG_DELAY_HD) << 16;
  129. tmp |= mode->htotal + AWG_DELAY_HD;
  130. writel(tmp, vtg->regs + VTG_H_HD_3);
  131. tmp = (mode->vsync_end - mode->vsync_start) << 16;
  132. tmp |= mode->vtotal;
  133. writel(tmp, vtg->regs + VTG_TOP_V_VD_3);
  134. writel(tmp, vtg->regs + VTG_BOT_V_VD_3);
  135. tmp = (mode->htotal + AWG_DELAY_HD) << 16;
  136. tmp |= mode->htotal + AWG_DELAY_HD;
  137. writel(tmp, vtg->regs + VTG_TOP_V_HD_3);
  138. writel(tmp, vtg->regs + VTG_BOT_V_HD_3);
  139. /* mode */
  140. writel(type, vtg->regs + VTG_MODE);
  141. }
  142. static void vtg_enable_irq(struct sti_vtg *vtg)
  143. {
  144. /* clear interrupt status and mask */
  145. writel(0xFFFF, vtg->regs + VTG_HOST_ITS_BCLR);
  146. writel(0xFFFF, vtg->regs + VTG_HOST_ITM_BCLR);
  147. writel(VTG_IRQ_MASK, vtg->regs + VTG_HOST_ITM_BSET);
  148. }
  149. void sti_vtg_set_config(struct sti_vtg *vtg,
  150. const struct drm_display_mode *mode)
  151. {
  152. /* write configuration */
  153. vtg_set_mode(vtg, VTG_TYPE_MASTER, mode);
  154. vtg_reset(vtg);
  155. /* enable irq for the vtg vblank synchro */
  156. if (vtg->slave)
  157. vtg_enable_irq(vtg->slave);
  158. else
  159. vtg_enable_irq(vtg);
  160. }
  161. EXPORT_SYMBOL(sti_vtg_set_config);
  162. /**
  163. * sti_vtg_get_line_number
  164. *
  165. * @mode: display mode to be used
  166. * @y: line
  167. *
  168. * Return the line number according to the display mode taking
  169. * into account the Sync and Back Porch information.
  170. * Video frame line numbers start at 1, y starts at 0.
  171. * In interlaced modes the start line is the field line number of the odd
  172. * field, but y is still defined as a progressive frame.
  173. */
  174. u32 sti_vtg_get_line_number(struct drm_display_mode mode, int y)
  175. {
  176. u32 start_line = mode.vtotal - mode.vsync_start + 1;
  177. if (mode.flags & DRM_MODE_FLAG_INTERLACE)
  178. start_line *= 2;
  179. return start_line + y;
  180. }
  181. EXPORT_SYMBOL(sti_vtg_get_line_number);
  182. /**
  183. * sti_vtg_get_pixel_number
  184. *
  185. * @mode: display mode to be used
  186. * @x: row
  187. *
  188. * Return the pixel number according to the display mode taking
  189. * into account the Sync and Back Porch information.
  190. * Pixels are counted from 0.
  191. */
  192. u32 sti_vtg_get_pixel_number(struct drm_display_mode mode, int x)
  193. {
  194. return mode.htotal - mode.hsync_start + x;
  195. }
  196. EXPORT_SYMBOL(sti_vtg_get_pixel_number);
  197. int sti_vtg_register_client(struct sti_vtg *vtg,
  198. struct notifier_block *nb, int crtc_id)
  199. {
  200. if (vtg->slave)
  201. return sti_vtg_register_client(vtg->slave, nb, crtc_id);
  202. vtg->crtc_id = crtc_id;
  203. return raw_notifier_chain_register(&vtg->notifier_list, nb);
  204. }
  205. EXPORT_SYMBOL(sti_vtg_register_client);
  206. int sti_vtg_unregister_client(struct sti_vtg *vtg, struct notifier_block *nb)
  207. {
  208. if (vtg->slave)
  209. return sti_vtg_unregister_client(vtg->slave, nb);
  210. return raw_notifier_chain_unregister(&vtg->notifier_list, nb);
  211. }
  212. EXPORT_SYMBOL(sti_vtg_unregister_client);
  213. static irqreturn_t vtg_irq_thread(int irq, void *arg)
  214. {
  215. struct sti_vtg *vtg = arg;
  216. u32 event;
  217. event = (vtg->irq_status & VTG_IRQ_TOP) ?
  218. VTG_TOP_FIELD_EVENT : VTG_BOTTOM_FIELD_EVENT;
  219. raw_notifier_call_chain(&vtg->notifier_list, event, &vtg->crtc_id);
  220. return IRQ_HANDLED;
  221. }
  222. static irqreturn_t vtg_irq(int irq, void *arg)
  223. {
  224. struct sti_vtg *vtg = arg;
  225. vtg->irq_status = readl(vtg->regs + VTG_HOST_ITS);
  226. writel(vtg->irq_status, vtg->regs + VTG_HOST_ITS_BCLR);
  227. /* force sync bus write */
  228. readl(vtg->regs + VTG_HOST_ITS);
  229. return IRQ_WAKE_THREAD;
  230. }
  231. static int vtg_probe(struct platform_device *pdev)
  232. {
  233. struct device *dev = &pdev->dev;
  234. struct device_node *np;
  235. struct sti_vtg *vtg;
  236. struct resource *res;
  237. char irq_name[32];
  238. int ret;
  239. vtg = devm_kzalloc(dev, sizeof(*vtg), GFP_KERNEL);
  240. if (!vtg)
  241. return -ENOMEM;
  242. vtg->dev = dev;
  243. vtg->np = pdev->dev.of_node;
  244. /* Get Memory ressources */
  245. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  246. if (!res) {
  247. DRM_ERROR("Get memory resource failed\n");
  248. return -ENOMEM;
  249. }
  250. vtg->regs = devm_ioremap_nocache(dev, res->start, resource_size(res));
  251. np = of_parse_phandle(pdev->dev.of_node, "st,slave", 0);
  252. if (np) {
  253. vtg->slave = of_vtg_find(np);
  254. if (!vtg->slave)
  255. return -EPROBE_DEFER;
  256. } else {
  257. vtg->irq = platform_get_irq(pdev, 0);
  258. if (IS_ERR_VALUE(vtg->irq)) {
  259. DRM_ERROR("Failed to get VTG interrupt\n");
  260. return vtg->irq;
  261. }
  262. snprintf(irq_name, sizeof(irq_name), "vsync-%s",
  263. dev_name(vtg->dev));
  264. RAW_INIT_NOTIFIER_HEAD(&vtg->notifier_list);
  265. ret = devm_request_threaded_irq(dev, vtg->irq, vtg_irq,
  266. vtg_irq_thread, IRQF_ONESHOT, irq_name, vtg);
  267. if (IS_ERR_VALUE(ret)) {
  268. DRM_ERROR("Failed to register VTG interrupt\n");
  269. return ret;
  270. }
  271. }
  272. vtg_register(vtg);
  273. platform_set_drvdata(pdev, vtg);
  274. DRM_INFO("%s %s\n", __func__, dev_name(vtg->dev));
  275. return 0;
  276. }
  277. static int vtg_remove(struct platform_device *pdev)
  278. {
  279. return 0;
  280. }
  281. static const struct of_device_id vtg_of_match[] = {
  282. { .compatible = "st,vtg", },
  283. { /* sentinel */ }
  284. };
  285. MODULE_DEVICE_TABLE(of, vtg_of_match);
  286. struct platform_driver sti_vtg_driver = {
  287. .driver = {
  288. .name = "sti-vtg",
  289. .owner = THIS_MODULE,
  290. .of_match_table = vtg_of_match,
  291. },
  292. .probe = vtg_probe,
  293. .remove = vtg_remove,
  294. };
  295. module_platform_driver(sti_vtg_driver);
  296. MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
  297. MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
  298. MODULE_LICENSE("GPL");