sti_vtg.c 9.8 KB

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