sti_vtg.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 (5)
  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: the CRTC 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. struct drm_crtc *crtc;
  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. static void vtg_reset(struct sti_vtg *vtg)
  96. {
  97. /* reset slave and then master */
  98. if (vtg->slave)
  99. vtg_reset(vtg->slave);
  100. writel(1, vtg->regs + VTG_DRST_AUTOC);
  101. }
  102. static void vtg_set_output_window(void __iomem *regs,
  103. const struct drm_display_mode *mode)
  104. {
  105. u32 video_top_field_start;
  106. u32 video_top_field_stop;
  107. u32 video_bottom_field_start;
  108. u32 video_bottom_field_stop;
  109. u32 xstart = sti_vtg_get_pixel_number(*mode, 0);
  110. u32 ystart = sti_vtg_get_line_number(*mode, 0);
  111. u32 xstop = sti_vtg_get_pixel_number(*mode, mode->hdisplay - 1);
  112. u32 ystop = sti_vtg_get_line_number(*mode, mode->vdisplay - 1);
  113. /* Set output window to fit the display mode selected */
  114. video_top_field_start = (ystart << 16) | xstart;
  115. video_top_field_stop = (ystop << 16) | xstop;
  116. /* Only progressive supported for now */
  117. video_bottom_field_start = video_top_field_start;
  118. video_bottom_field_stop = video_top_field_stop;
  119. writel(video_top_field_start, regs + VTG_VID_TFO);
  120. writel(video_top_field_stop, regs + VTG_VID_TFS);
  121. writel(video_bottom_field_start, regs + VTG_VID_BFO);
  122. writel(video_bottom_field_stop, regs + VTG_VID_BFS);
  123. }
  124. static void vtg_set_mode(struct sti_vtg *vtg,
  125. int type, const struct drm_display_mode *mode)
  126. {
  127. u32 tmp;
  128. if (vtg->slave)
  129. vtg_set_mode(vtg->slave, VTG_TYPE_SLAVE_BY_EXT0, mode);
  130. /* Set the number of clock cycles per line */
  131. writel(mode->htotal, vtg->regs + VTG_CLKLN);
  132. /* Set Half Line Per Field (only progressive supported for now) */
  133. writel(mode->vtotal * 2, vtg->regs + VTG_HLFLN);
  134. /* Program output window */
  135. vtg_set_output_window(vtg->regs, mode);
  136. /* prepare VTG set 1 for HDMI */
  137. tmp = (mode->hsync_end - mode->hsync_start + HDMI_DELAY) << 16;
  138. tmp |= HDMI_DELAY;
  139. writel(tmp, vtg->regs + VTG_H_HD_1);
  140. tmp = (mode->vsync_end - mode->vsync_start + 1) << 16;
  141. tmp |= 1;
  142. writel(tmp, vtg->regs + VTG_TOP_V_VD_1);
  143. writel(tmp, vtg->regs + VTG_BOT_V_VD_1);
  144. tmp = HDMI_DELAY << 16;
  145. tmp |= HDMI_DELAY;
  146. writel(tmp, vtg->regs + VTG_TOP_V_HD_1);
  147. writel(tmp, vtg->regs + VTG_BOT_V_HD_1);
  148. /* prepare VTG set 2 for for HD DCS */
  149. tmp = (mode->hsync_end - mode->hsync_start) << 16;
  150. writel(tmp, vtg->regs + VTG_H_HD_2);
  151. tmp = (mode->vsync_end - mode->vsync_start + 1) << 16;
  152. tmp |= 1;
  153. writel(tmp, vtg->regs + VTG_TOP_V_VD_2);
  154. writel(tmp, vtg->regs + VTG_BOT_V_VD_2);
  155. writel(0, vtg->regs + VTG_TOP_V_HD_2);
  156. writel(0, vtg->regs + VTG_BOT_V_HD_2);
  157. /* prepare VTG set 3 for HD Analog in HD mode */
  158. tmp = (mode->hsync_end - mode->hsync_start + AWG_DELAY_HD) << 16;
  159. tmp |= mode->htotal + AWG_DELAY_HD;
  160. writel(tmp, vtg->regs + VTG_H_HD_3);
  161. tmp = (mode->vsync_end - mode->vsync_start) << 16;
  162. tmp |= mode->vtotal;
  163. writel(tmp, vtg->regs + VTG_TOP_V_VD_3);
  164. writel(tmp, vtg->regs + VTG_BOT_V_VD_3);
  165. tmp = (mode->htotal + AWG_DELAY_HD) << 16;
  166. tmp |= mode->htotal + AWG_DELAY_HD;
  167. writel(tmp, vtg->regs + VTG_TOP_V_HD_3);
  168. writel(tmp, vtg->regs + VTG_BOT_V_HD_3);
  169. /* Prepare VTG set 4 for DVO */
  170. tmp = (mode->hsync_end - mode->hsync_start) << 16;
  171. writel(tmp, vtg->regs + VTG_H_HD_4);
  172. tmp = (mode->vsync_end - mode->vsync_start + 1) << 16;
  173. tmp |= 1;
  174. writel(tmp, vtg->regs + VTG_TOP_V_VD_4);
  175. writel(tmp, vtg->regs + VTG_BOT_V_VD_4);
  176. writel(0, vtg->regs + VTG_TOP_V_HD_4);
  177. writel(0, vtg->regs + VTG_BOT_V_HD_4);
  178. /* mode */
  179. writel(type, vtg->regs + VTG_MODE);
  180. }
  181. static void vtg_enable_irq(struct sti_vtg *vtg)
  182. {
  183. /* clear interrupt status and mask */
  184. writel(0xFFFF, vtg->regs + VTG_HOST_ITS_BCLR);
  185. writel(0xFFFF, vtg->regs + VTG_HOST_ITM_BCLR);
  186. writel(VTG_IRQ_MASK, vtg->regs + VTG_HOST_ITM_BSET);
  187. }
  188. void sti_vtg_set_config(struct sti_vtg *vtg,
  189. const struct drm_display_mode *mode)
  190. {
  191. /* write configuration */
  192. vtg_set_mode(vtg, VTG_TYPE_MASTER, mode);
  193. vtg_reset(vtg);
  194. /* enable irq for the vtg vblank synchro */
  195. if (vtg->slave)
  196. vtg_enable_irq(vtg->slave);
  197. else
  198. vtg_enable_irq(vtg);
  199. }
  200. /**
  201. * sti_vtg_get_line_number
  202. *
  203. * @mode: display mode to be used
  204. * @y: line
  205. *
  206. * Return the line number according to the display mode taking
  207. * into account the Sync and Back Porch information.
  208. * Video frame line numbers start at 1, y starts at 0.
  209. * In interlaced modes the start line is the field line number of the odd
  210. * field, but y is still defined as a progressive frame.
  211. */
  212. u32 sti_vtg_get_line_number(struct drm_display_mode mode, int y)
  213. {
  214. u32 start_line = mode.vtotal - mode.vsync_start + 1;
  215. if (mode.flags & DRM_MODE_FLAG_INTERLACE)
  216. start_line *= 2;
  217. return start_line + y;
  218. }
  219. /**
  220. * sti_vtg_get_pixel_number
  221. *
  222. * @mode: display mode to be used
  223. * @x: row
  224. *
  225. * Return the pixel number according to the display mode taking
  226. * into account the Sync and Back Porch information.
  227. * Pixels are counted from 0.
  228. */
  229. u32 sti_vtg_get_pixel_number(struct drm_display_mode mode, int x)
  230. {
  231. return mode.htotal - mode.hsync_start + x;
  232. }
  233. int sti_vtg_register_client(struct sti_vtg *vtg, struct notifier_block *nb,
  234. struct drm_crtc *crtc)
  235. {
  236. if (vtg->slave)
  237. return sti_vtg_register_client(vtg->slave, nb, crtc);
  238. vtg->crtc = crtc;
  239. return raw_notifier_chain_register(&vtg->notifier_list, nb);
  240. }
  241. int sti_vtg_unregister_client(struct sti_vtg *vtg, struct notifier_block *nb)
  242. {
  243. if (vtg->slave)
  244. return sti_vtg_unregister_client(vtg->slave, nb);
  245. return raw_notifier_chain_unregister(&vtg->notifier_list, nb);
  246. }
  247. static irqreturn_t vtg_irq_thread(int irq, void *arg)
  248. {
  249. struct sti_vtg *vtg = arg;
  250. u32 event;
  251. event = (vtg->irq_status & VTG_IRQ_TOP) ?
  252. VTG_TOP_FIELD_EVENT : VTG_BOTTOM_FIELD_EVENT;
  253. raw_notifier_call_chain(&vtg->notifier_list, event, vtg->crtc);
  254. return IRQ_HANDLED;
  255. }
  256. static irqreturn_t vtg_irq(int irq, void *arg)
  257. {
  258. struct sti_vtg *vtg = arg;
  259. vtg->irq_status = readl(vtg->regs + VTG_HOST_ITS);
  260. writel(vtg->irq_status, vtg->regs + VTG_HOST_ITS_BCLR);
  261. /* force sync bus write */
  262. readl(vtg->regs + VTG_HOST_ITS);
  263. return IRQ_WAKE_THREAD;
  264. }
  265. static int vtg_probe(struct platform_device *pdev)
  266. {
  267. struct device *dev = &pdev->dev;
  268. struct device_node *np;
  269. struct sti_vtg *vtg;
  270. struct resource *res;
  271. int ret;
  272. vtg = devm_kzalloc(dev, sizeof(*vtg), GFP_KERNEL);
  273. if (!vtg)
  274. return -ENOMEM;
  275. vtg->dev = dev;
  276. vtg->np = pdev->dev.of_node;
  277. /* Get Memory ressources */
  278. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  279. if (!res) {
  280. DRM_ERROR("Get memory resource failed\n");
  281. return -ENOMEM;
  282. }
  283. vtg->regs = devm_ioremap_nocache(dev, res->start, resource_size(res));
  284. if (!vtg->regs) {
  285. DRM_ERROR("failed to remap I/O memory\n");
  286. return -ENOMEM;
  287. }
  288. np = of_parse_phandle(pdev->dev.of_node, "st,slave", 0);
  289. if (np) {
  290. vtg->slave = of_vtg_find(np);
  291. if (!vtg->slave)
  292. return -EPROBE_DEFER;
  293. } else {
  294. vtg->irq = platform_get_irq(pdev, 0);
  295. if (IS_ERR_VALUE(vtg->irq)) {
  296. DRM_ERROR("Failed to get VTG interrupt\n");
  297. return vtg->irq;
  298. }
  299. RAW_INIT_NOTIFIER_HEAD(&vtg->notifier_list);
  300. ret = devm_request_threaded_irq(dev, vtg->irq, vtg_irq,
  301. vtg_irq_thread, IRQF_ONESHOT,
  302. dev_name(dev), vtg);
  303. if (IS_ERR_VALUE(ret)) {
  304. DRM_ERROR("Failed to register VTG interrupt\n");
  305. return ret;
  306. }
  307. }
  308. vtg_register(vtg);
  309. platform_set_drvdata(pdev, vtg);
  310. DRM_INFO("%s %s\n", __func__, dev_name(vtg->dev));
  311. return 0;
  312. }
  313. static int vtg_remove(struct platform_device *pdev)
  314. {
  315. return 0;
  316. }
  317. static const struct of_device_id vtg_of_match[] = {
  318. { .compatible = "st,vtg", },
  319. { /* sentinel */ }
  320. };
  321. MODULE_DEVICE_TABLE(of, vtg_of_match);
  322. struct platform_driver sti_vtg_driver = {
  323. .driver = {
  324. .name = "sti-vtg",
  325. .owner = THIS_MODULE,
  326. .of_match_table = vtg_of_match,
  327. },
  328. .probe = vtg_probe,
  329. .remove = vtg_remove,
  330. };
  331. MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
  332. MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
  333. MODULE_LICENSE("GPL");