sti_vtg.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) STMicroelectronics SA 2014
  4. * Authors: Benjamin Gaignard <benjamin.gaignard@st.com>
  5. * Fabien Dessenne <fabien.dessenne@st.com>
  6. * Vincent Abriou <vincent.abriou@st.com>
  7. * for STMicroelectronics.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/io.h>
  11. #include <linux/notifier.h>
  12. #include <linux/of_platform.h>
  13. #include <linux/platform_device.h>
  14. #include <drm/drm_modes.h>
  15. #include <drm/drm_print.h>
  16. #include "sti_drv.h"
  17. #include "sti_vtg.h"
  18. #define VTG_MODE_MASTER 0
  19. /* registers offset */
  20. #define VTG_MODE 0x0000
  21. #define VTG_CLKLN 0x0008
  22. #define VTG_HLFLN 0x000C
  23. #define VTG_DRST_AUTOC 0x0010
  24. #define VTG_VID_TFO 0x0040
  25. #define VTG_VID_TFS 0x0044
  26. #define VTG_VID_BFO 0x0048
  27. #define VTG_VID_BFS 0x004C
  28. #define VTG_HOST_ITS 0x0078
  29. #define VTG_HOST_ITS_BCLR 0x007C
  30. #define VTG_HOST_ITM_BCLR 0x0088
  31. #define VTG_HOST_ITM_BSET 0x008C
  32. #define VTG_H_HD_1 0x00C0
  33. #define VTG_TOP_V_VD_1 0x00C4
  34. #define VTG_BOT_V_VD_1 0x00C8
  35. #define VTG_TOP_V_HD_1 0x00CC
  36. #define VTG_BOT_V_HD_1 0x00D0
  37. #define VTG_H_HD_2 0x00E0
  38. #define VTG_TOP_V_VD_2 0x00E4
  39. #define VTG_BOT_V_VD_2 0x00E8
  40. #define VTG_TOP_V_HD_2 0x00EC
  41. #define VTG_BOT_V_HD_2 0x00F0
  42. #define VTG_H_HD_3 0x0100
  43. #define VTG_TOP_V_VD_3 0x0104
  44. #define VTG_BOT_V_VD_3 0x0108
  45. #define VTG_TOP_V_HD_3 0x010C
  46. #define VTG_BOT_V_HD_3 0x0110
  47. #define VTG_H_HD_4 0x0120
  48. #define VTG_TOP_V_VD_4 0x0124
  49. #define VTG_BOT_V_VD_4 0x0128
  50. #define VTG_TOP_V_HD_4 0x012c
  51. #define VTG_BOT_V_HD_4 0x0130
  52. #define VTG_IRQ_BOTTOM BIT(0)
  53. #define VTG_IRQ_TOP BIT(1)
  54. #define VTG_IRQ_MASK (VTG_IRQ_TOP | VTG_IRQ_BOTTOM)
  55. /* Delay introduced by the HDMI in nb of pixel */
  56. #define HDMI_DELAY (5)
  57. /* Delay introduced by the DVO in nb of pixel */
  58. #define DVO_DELAY (7)
  59. /* delay introduced by the Arbitrary Waveform Generator in nb of pixels */
  60. #define AWG_DELAY_HD (-9)
  61. #define AWG_DELAY_ED (-8)
  62. #define AWG_DELAY_SD (-7)
  63. /*
  64. * STI VTG register offset structure
  65. *
  66. *@h_hd: stores the VTG_H_HD_x register offset
  67. *@top_v_vd: stores the VTG_TOP_V_VD_x register offset
  68. *@bot_v_vd: stores the VTG_BOT_V_VD_x register offset
  69. *@top_v_hd: stores the VTG_TOP_V_HD_x register offset
  70. *@bot_v_hd: stores the VTG_BOT_V_HD_x register offset
  71. */
  72. struct sti_vtg_regs_offs {
  73. u32 h_hd;
  74. u32 top_v_vd;
  75. u32 bot_v_vd;
  76. u32 top_v_hd;
  77. u32 bot_v_hd;
  78. };
  79. #define VTG_MAX_SYNC_OUTPUT 4
  80. static const struct sti_vtg_regs_offs vtg_regs_offs[VTG_MAX_SYNC_OUTPUT] = {
  81. { VTG_H_HD_1,
  82. VTG_TOP_V_VD_1, VTG_BOT_V_VD_1, VTG_TOP_V_HD_1, VTG_BOT_V_HD_1 },
  83. { VTG_H_HD_2,
  84. VTG_TOP_V_VD_2, VTG_BOT_V_VD_2, VTG_TOP_V_HD_2, VTG_BOT_V_HD_2 },
  85. { VTG_H_HD_3,
  86. VTG_TOP_V_VD_3, VTG_BOT_V_VD_3, VTG_TOP_V_HD_3, VTG_BOT_V_HD_3 },
  87. { VTG_H_HD_4,
  88. VTG_TOP_V_VD_4, VTG_BOT_V_VD_4, VTG_TOP_V_HD_4, VTG_BOT_V_HD_4 }
  89. };
  90. /*
  91. * STI VTG synchronisation parameters structure
  92. *
  93. *@hsync: sample number falling and rising edge
  94. *@vsync_line_top: vertical top field line number falling and rising edge
  95. *@vsync_line_bot: vertical bottom field line number falling and rising edge
  96. *@vsync_off_top: vertical top field sample number rising and falling edge
  97. *@vsync_off_bot: vertical bottom field sample number rising and falling edge
  98. */
  99. struct sti_vtg_sync_params {
  100. u32 hsync;
  101. u32 vsync_line_top;
  102. u32 vsync_line_bot;
  103. u32 vsync_off_top;
  104. u32 vsync_off_bot;
  105. };
  106. /*
  107. * STI VTG structure
  108. *
  109. * @regs: register mapping
  110. * @sync_params: synchronisation parameters used to generate timings
  111. * @irq: VTG irq
  112. * @irq_status: store the IRQ status value
  113. * @notifier_list: notifier callback
  114. * @crtc: the CRTC for vblank event
  115. */
  116. struct sti_vtg {
  117. void __iomem *regs;
  118. struct sti_vtg_sync_params sync_params[VTG_MAX_SYNC_OUTPUT];
  119. int irq;
  120. u32 irq_status;
  121. struct raw_notifier_head notifier_list;
  122. struct drm_crtc *crtc;
  123. };
  124. struct sti_vtg *of_vtg_find(struct device_node *np)
  125. {
  126. struct platform_device *pdev;
  127. struct sti_vtg *vtg;
  128. pdev = of_find_device_by_node(np);
  129. if (!pdev)
  130. return NULL;
  131. vtg = platform_get_drvdata(pdev);
  132. put_device(&pdev->dev);
  133. return vtg;
  134. }
  135. static void vtg_reset(struct sti_vtg *vtg)
  136. {
  137. writel(1, vtg->regs + VTG_DRST_AUTOC);
  138. }
  139. static void vtg_set_output_window(void __iomem *regs,
  140. const struct drm_display_mode *mode)
  141. {
  142. u32 video_top_field_start;
  143. u32 video_top_field_stop;
  144. u32 video_bottom_field_start;
  145. u32 video_bottom_field_stop;
  146. u32 xstart = sti_vtg_get_pixel_number(*mode, 0);
  147. u32 ystart = sti_vtg_get_line_number(*mode, 0);
  148. u32 xstop = sti_vtg_get_pixel_number(*mode, mode->hdisplay - 1);
  149. u32 ystop = sti_vtg_get_line_number(*mode, mode->vdisplay - 1);
  150. /* Set output window to fit the display mode selected */
  151. video_top_field_start = (ystart << 16) | xstart;
  152. video_top_field_stop = (ystop << 16) | xstop;
  153. /* Only progressive supported for now */
  154. video_bottom_field_start = video_top_field_start;
  155. video_bottom_field_stop = video_top_field_stop;
  156. writel(video_top_field_start, regs + VTG_VID_TFO);
  157. writel(video_top_field_stop, regs + VTG_VID_TFS);
  158. writel(video_bottom_field_start, regs + VTG_VID_BFO);
  159. writel(video_bottom_field_stop, regs + VTG_VID_BFS);
  160. }
  161. static void vtg_set_hsync_vsync_pos(struct sti_vtg_sync_params *sync,
  162. int delay,
  163. const struct drm_display_mode *mode)
  164. {
  165. long clocksperline, start, stop;
  166. u32 risesync_top, fallsync_top;
  167. u32 risesync_offs_top, fallsync_offs_top;
  168. clocksperline = mode->htotal;
  169. /* Get the hsync position */
  170. start = 0;
  171. stop = mode->hsync_end - mode->hsync_start;
  172. start += delay;
  173. stop += delay;
  174. if (start < 0)
  175. start += clocksperline;
  176. else if (start >= clocksperline)
  177. start -= clocksperline;
  178. if (stop < 0)
  179. stop += clocksperline;
  180. else if (stop >= clocksperline)
  181. stop -= clocksperline;
  182. sync->hsync = (stop << 16) | start;
  183. /* Get the vsync position */
  184. if (delay >= 0) {
  185. risesync_top = 1;
  186. fallsync_top = risesync_top;
  187. fallsync_top += mode->vsync_end - mode->vsync_start;
  188. fallsync_offs_top = (u32)delay;
  189. risesync_offs_top = (u32)delay;
  190. } else {
  191. risesync_top = mode->vtotal;
  192. fallsync_top = mode->vsync_end - mode->vsync_start;
  193. fallsync_offs_top = clocksperline + delay;
  194. risesync_offs_top = clocksperline + delay;
  195. }
  196. sync->vsync_line_top = (fallsync_top << 16) | risesync_top;
  197. sync->vsync_off_top = (fallsync_offs_top << 16) | risesync_offs_top;
  198. /* Only progressive supported for now */
  199. sync->vsync_line_bot = sync->vsync_line_top;
  200. sync->vsync_off_bot = sync->vsync_off_top;
  201. }
  202. static void vtg_set_mode(struct sti_vtg *vtg,
  203. int type,
  204. struct sti_vtg_sync_params *sync,
  205. const struct drm_display_mode *mode)
  206. {
  207. unsigned int i;
  208. /* Set the number of clock cycles per line */
  209. writel(mode->htotal, vtg->regs + VTG_CLKLN);
  210. /* Set Half Line Per Field (only progressive supported for now) */
  211. writel(mode->vtotal * 2, vtg->regs + VTG_HLFLN);
  212. /* Program output window */
  213. vtg_set_output_window(vtg->regs, mode);
  214. /* Set hsync and vsync position for HDMI */
  215. vtg_set_hsync_vsync_pos(&sync[VTG_SYNC_ID_HDMI - 1], HDMI_DELAY, mode);
  216. /* Set hsync and vsync position for HD DCS */
  217. vtg_set_hsync_vsync_pos(&sync[VTG_SYNC_ID_HDDCS - 1], 0, mode);
  218. /* Set hsync and vsync position for HDF */
  219. vtg_set_hsync_vsync_pos(&sync[VTG_SYNC_ID_HDF - 1], AWG_DELAY_HD, mode);
  220. /* Set hsync and vsync position for DVO */
  221. vtg_set_hsync_vsync_pos(&sync[VTG_SYNC_ID_DVO - 1], DVO_DELAY, mode);
  222. /* Progam the syncs outputs */
  223. for (i = 0; i < VTG_MAX_SYNC_OUTPUT ; i++) {
  224. writel(sync[i].hsync,
  225. vtg->regs + vtg_regs_offs[i].h_hd);
  226. writel(sync[i].vsync_line_top,
  227. vtg->regs + vtg_regs_offs[i].top_v_vd);
  228. writel(sync[i].vsync_line_bot,
  229. vtg->regs + vtg_regs_offs[i].bot_v_vd);
  230. writel(sync[i].vsync_off_top,
  231. vtg->regs + vtg_regs_offs[i].top_v_hd);
  232. writel(sync[i].vsync_off_bot,
  233. vtg->regs + vtg_regs_offs[i].bot_v_hd);
  234. }
  235. /* mode */
  236. writel(type, vtg->regs + VTG_MODE);
  237. }
  238. static void vtg_enable_irq(struct sti_vtg *vtg)
  239. {
  240. /* clear interrupt status and mask */
  241. writel(0xFFFF, vtg->regs + VTG_HOST_ITS_BCLR);
  242. writel(0xFFFF, vtg->regs + VTG_HOST_ITM_BCLR);
  243. writel(VTG_IRQ_MASK, vtg->regs + VTG_HOST_ITM_BSET);
  244. }
  245. void sti_vtg_set_config(struct sti_vtg *vtg,
  246. const struct drm_display_mode *mode)
  247. {
  248. /* write configuration */
  249. vtg_set_mode(vtg, VTG_MODE_MASTER, vtg->sync_params, mode);
  250. vtg_reset(vtg);
  251. vtg_enable_irq(vtg);
  252. }
  253. /**
  254. * sti_vtg_get_line_number
  255. *
  256. * @mode: display mode to be used
  257. * @y: line
  258. *
  259. * Return the line number according to the display mode taking
  260. * into account the Sync and Back Porch information.
  261. * Video frame line numbers start at 1, y starts at 0.
  262. * In interlaced modes the start line is the field line number of the odd
  263. * field, but y is still defined as a progressive frame.
  264. */
  265. u32 sti_vtg_get_line_number(struct drm_display_mode mode, int y)
  266. {
  267. u32 start_line = mode.vtotal - mode.vsync_start + 1;
  268. if (mode.flags & DRM_MODE_FLAG_INTERLACE)
  269. start_line *= 2;
  270. return start_line + y;
  271. }
  272. /**
  273. * sti_vtg_get_pixel_number
  274. *
  275. * @mode: display mode to be used
  276. * @x: row
  277. *
  278. * Return the pixel number according to the display mode taking
  279. * into account the Sync and Back Porch information.
  280. * Pixels are counted from 0.
  281. */
  282. u32 sti_vtg_get_pixel_number(struct drm_display_mode mode, int x)
  283. {
  284. return mode.htotal - mode.hsync_start + x;
  285. }
  286. int sti_vtg_register_client(struct sti_vtg *vtg, struct notifier_block *nb,
  287. struct drm_crtc *crtc)
  288. {
  289. vtg->crtc = crtc;
  290. return raw_notifier_chain_register(&vtg->notifier_list, nb);
  291. }
  292. int sti_vtg_unregister_client(struct sti_vtg *vtg, struct notifier_block *nb)
  293. {
  294. return raw_notifier_chain_unregister(&vtg->notifier_list, nb);
  295. }
  296. static irqreturn_t vtg_irq_thread(int irq, void *arg)
  297. {
  298. struct sti_vtg *vtg = arg;
  299. u32 event;
  300. event = (vtg->irq_status & VTG_IRQ_TOP) ?
  301. VTG_TOP_FIELD_EVENT : VTG_BOTTOM_FIELD_EVENT;
  302. raw_notifier_call_chain(&vtg->notifier_list, event, vtg->crtc);
  303. return IRQ_HANDLED;
  304. }
  305. static irqreturn_t vtg_irq(int irq, void *arg)
  306. {
  307. struct sti_vtg *vtg = arg;
  308. vtg->irq_status = readl(vtg->regs + VTG_HOST_ITS);
  309. writel(vtg->irq_status, vtg->regs + VTG_HOST_ITS_BCLR);
  310. /* force sync bus write */
  311. readl(vtg->regs + VTG_HOST_ITS);
  312. return IRQ_WAKE_THREAD;
  313. }
  314. static int vtg_probe(struct platform_device *pdev)
  315. {
  316. struct device *dev = &pdev->dev;
  317. struct sti_vtg *vtg;
  318. int ret;
  319. vtg = devm_kzalloc(dev, sizeof(*vtg), GFP_KERNEL);
  320. if (!vtg)
  321. return -ENOMEM;
  322. vtg->regs = devm_platform_ioremap_resource(pdev, 0);
  323. if (IS_ERR(vtg->regs)) {
  324. DRM_ERROR("failed to remap I/O memory\n");
  325. return PTR_ERR(vtg->regs);
  326. }
  327. vtg->irq = platform_get_irq(pdev, 0);
  328. if (vtg->irq < 0) {
  329. DRM_ERROR("Failed to get VTG interrupt\n");
  330. return vtg->irq;
  331. }
  332. RAW_INIT_NOTIFIER_HEAD(&vtg->notifier_list);
  333. ret = devm_request_threaded_irq(dev, vtg->irq, vtg_irq,
  334. vtg_irq_thread, IRQF_ONESHOT,
  335. dev_name(dev), vtg);
  336. if (ret < 0) {
  337. DRM_ERROR("Failed to register VTG interrupt\n");
  338. return ret;
  339. }
  340. platform_set_drvdata(pdev, vtg);
  341. DRM_INFO("%s %s\n", __func__, dev_name(dev));
  342. return 0;
  343. }
  344. static const struct of_device_id vtg_of_match[] = {
  345. { .compatible = "st,vtg", },
  346. { /* sentinel */ }
  347. };
  348. MODULE_DEVICE_TABLE(of, vtg_of_match);
  349. struct platform_driver sti_vtg_driver = {
  350. .driver = {
  351. .name = "sti-vtg",
  352. .of_match_table = vtg_of_match,
  353. },
  354. .probe = vtg_probe,
  355. };
  356. MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
  357. MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
  358. MODULE_LICENSE("GPL");