sti_vtg.c 12 KB

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