sti_vtg.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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_drv.h"
  14. #include "sti_vtg.h"
  15. #define VTG_MODE_MASTER 0
  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 DVO in nb of pixel */
  55. #define DVO_DELAY (7)
  56. /* delay introduced by the Arbitrary Waveform Generator in nb of pixels */
  57. #define AWG_DELAY_HD (-9)
  58. #define AWG_DELAY_ED (-8)
  59. #define AWG_DELAY_SD (-7)
  60. static LIST_HEAD(vtg_lookup);
  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. * @dev: pointer to device driver
  108. * @np: device node
  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. * @link: List node to link the structure in lookup list
  116. */
  117. struct sti_vtg {
  118. struct device *dev;
  119. struct device_node *np;
  120. void __iomem *regs;
  121. struct sti_vtg_sync_params sync_params[VTG_MAX_SYNC_OUTPUT];
  122. int irq;
  123. u32 irq_status;
  124. struct raw_notifier_head notifier_list;
  125. struct drm_crtc *crtc;
  126. struct list_head link;
  127. };
  128. static void vtg_register(struct sti_vtg *vtg)
  129. {
  130. list_add_tail(&vtg->link, &vtg_lookup);
  131. }
  132. struct sti_vtg *of_vtg_find(struct device_node *np)
  133. {
  134. struct sti_vtg *vtg;
  135. list_for_each_entry(vtg, &vtg_lookup, link) {
  136. if (vtg->np == np)
  137. return vtg;
  138. }
  139. return NULL;
  140. }
  141. static void vtg_reset(struct sti_vtg *vtg)
  142. {
  143. writel(1, vtg->regs + VTG_DRST_AUTOC);
  144. }
  145. static void vtg_set_output_window(void __iomem *regs,
  146. const struct drm_display_mode *mode)
  147. {
  148. u32 video_top_field_start;
  149. u32 video_top_field_stop;
  150. u32 video_bottom_field_start;
  151. u32 video_bottom_field_stop;
  152. u32 xstart = sti_vtg_get_pixel_number(*mode, 0);
  153. u32 ystart = sti_vtg_get_line_number(*mode, 0);
  154. u32 xstop = sti_vtg_get_pixel_number(*mode, mode->hdisplay - 1);
  155. u32 ystop = sti_vtg_get_line_number(*mode, mode->vdisplay - 1);
  156. /* Set output window to fit the display mode selected */
  157. video_top_field_start = (ystart << 16) | xstart;
  158. video_top_field_stop = (ystop << 16) | xstop;
  159. /* Only progressive supported for now */
  160. video_bottom_field_start = video_top_field_start;
  161. video_bottom_field_stop = video_top_field_stop;
  162. writel(video_top_field_start, regs + VTG_VID_TFO);
  163. writel(video_top_field_stop, regs + VTG_VID_TFS);
  164. writel(video_bottom_field_start, regs + VTG_VID_BFO);
  165. writel(video_bottom_field_stop, regs + VTG_VID_BFS);
  166. }
  167. static void vtg_set_hsync_vsync_pos(struct sti_vtg_sync_params *sync,
  168. int delay,
  169. const struct drm_display_mode *mode)
  170. {
  171. long clocksperline, start, stop;
  172. u32 risesync_top, fallsync_top;
  173. u32 risesync_offs_top, fallsync_offs_top;
  174. clocksperline = mode->htotal;
  175. /* Get the hsync position */
  176. start = 0;
  177. stop = mode->hsync_end - mode->hsync_start;
  178. start += delay;
  179. stop += delay;
  180. if (start < 0)
  181. start += clocksperline;
  182. else if (start >= clocksperline)
  183. start -= clocksperline;
  184. if (stop < 0)
  185. stop += clocksperline;
  186. else if (stop >= clocksperline)
  187. stop -= clocksperline;
  188. sync->hsync = (stop << 16) | start;
  189. /* Get the vsync position */
  190. if (delay >= 0) {
  191. risesync_top = 1;
  192. fallsync_top = risesync_top;
  193. fallsync_top += mode->vsync_end - mode->vsync_start;
  194. fallsync_offs_top = (u32)delay;
  195. risesync_offs_top = (u32)delay;
  196. } else {
  197. risesync_top = mode->vtotal;
  198. fallsync_top = mode->vsync_end - mode->vsync_start;
  199. fallsync_offs_top = clocksperline + delay;
  200. risesync_offs_top = clocksperline + delay;
  201. }
  202. sync->vsync_line_top = (fallsync_top << 16) | risesync_top;
  203. sync->vsync_off_top = (fallsync_offs_top << 16) | risesync_offs_top;
  204. /* Only progressive supported for now */
  205. sync->vsync_line_bot = sync->vsync_line_top;
  206. sync->vsync_off_bot = sync->vsync_off_top;
  207. }
  208. static void vtg_set_mode(struct sti_vtg *vtg,
  209. int type,
  210. struct sti_vtg_sync_params *sync,
  211. const struct drm_display_mode *mode)
  212. {
  213. unsigned int i;
  214. /* Set the number of clock cycles per line */
  215. writel(mode->htotal, vtg->regs + VTG_CLKLN);
  216. /* Set Half Line Per Field (only progressive supported for now) */
  217. writel(mode->vtotal * 2, vtg->regs + VTG_HLFLN);
  218. /* Program output window */
  219. vtg_set_output_window(vtg->regs, mode);
  220. /* Set hsync and vsync position for HDMI */
  221. vtg_set_hsync_vsync_pos(&sync[VTG_SYNC_ID_HDMI - 1], HDMI_DELAY, mode);
  222. /* Set hsync and vsync position for HD DCS */
  223. vtg_set_hsync_vsync_pos(&sync[VTG_SYNC_ID_HDDCS - 1], 0, mode);
  224. /* Set hsync and vsync position for HDF */
  225. vtg_set_hsync_vsync_pos(&sync[VTG_SYNC_ID_HDF - 1], AWG_DELAY_HD, mode);
  226. /* Set hsync and vsync position for DVO */
  227. vtg_set_hsync_vsync_pos(&sync[VTG_SYNC_ID_DVO - 1], DVO_DELAY, mode);
  228. /* Progam the syncs outputs */
  229. for (i = 0; i < VTG_MAX_SYNC_OUTPUT ; i++) {
  230. writel(sync[i].hsync,
  231. vtg->regs + vtg_regs_offs[i].h_hd);
  232. writel(sync[i].vsync_line_top,
  233. vtg->regs + vtg_regs_offs[i].top_v_vd);
  234. writel(sync[i].vsync_line_bot,
  235. vtg->regs + vtg_regs_offs[i].bot_v_vd);
  236. writel(sync[i].vsync_off_top,
  237. vtg->regs + vtg_regs_offs[i].top_v_hd);
  238. writel(sync[i].vsync_off_bot,
  239. vtg->regs + vtg_regs_offs[i].bot_v_hd);
  240. }
  241. /* mode */
  242. writel(type, vtg->regs + VTG_MODE);
  243. }
  244. static void vtg_enable_irq(struct sti_vtg *vtg)
  245. {
  246. /* clear interrupt status and mask */
  247. writel(0xFFFF, vtg->regs + VTG_HOST_ITS_BCLR);
  248. writel(0xFFFF, vtg->regs + VTG_HOST_ITM_BCLR);
  249. writel(VTG_IRQ_MASK, vtg->regs + VTG_HOST_ITM_BSET);
  250. }
  251. void sti_vtg_set_config(struct sti_vtg *vtg,
  252. const struct drm_display_mode *mode)
  253. {
  254. /* write configuration */
  255. vtg_set_mode(vtg, VTG_MODE_MASTER, vtg->sync_params, mode);
  256. vtg_reset(vtg);
  257. vtg_enable_irq(vtg);
  258. }
  259. /**
  260. * sti_vtg_get_line_number
  261. *
  262. * @mode: display mode to be used
  263. * @y: line
  264. *
  265. * Return the line number according to the display mode taking
  266. * into account the Sync and Back Porch information.
  267. * Video frame line numbers start at 1, y starts at 0.
  268. * In interlaced modes the start line is the field line number of the odd
  269. * field, but y is still defined as a progressive frame.
  270. */
  271. u32 sti_vtg_get_line_number(struct drm_display_mode mode, int y)
  272. {
  273. u32 start_line = mode.vtotal - mode.vsync_start + 1;
  274. if (mode.flags & DRM_MODE_FLAG_INTERLACE)
  275. start_line *= 2;
  276. return start_line + y;
  277. }
  278. /**
  279. * sti_vtg_get_pixel_number
  280. *
  281. * @mode: display mode to be used
  282. * @x: row
  283. *
  284. * Return the pixel number according to the display mode taking
  285. * into account the Sync and Back Porch information.
  286. * Pixels are counted from 0.
  287. */
  288. u32 sti_vtg_get_pixel_number(struct drm_display_mode mode, int x)
  289. {
  290. return mode.htotal - mode.hsync_start + x;
  291. }
  292. int sti_vtg_register_client(struct sti_vtg *vtg, struct notifier_block *nb,
  293. struct drm_crtc *crtc)
  294. {
  295. vtg->crtc = crtc;
  296. return raw_notifier_chain_register(&vtg->notifier_list, nb);
  297. }
  298. int sti_vtg_unregister_client(struct sti_vtg *vtg, struct notifier_block *nb)
  299. {
  300. return raw_notifier_chain_unregister(&vtg->notifier_list, nb);
  301. }
  302. static irqreturn_t vtg_irq_thread(int irq, void *arg)
  303. {
  304. struct sti_vtg *vtg = arg;
  305. u32 event;
  306. event = (vtg->irq_status & VTG_IRQ_TOP) ?
  307. VTG_TOP_FIELD_EVENT : VTG_BOTTOM_FIELD_EVENT;
  308. raw_notifier_call_chain(&vtg->notifier_list, event, vtg->crtc);
  309. return IRQ_HANDLED;
  310. }
  311. static irqreturn_t vtg_irq(int irq, void *arg)
  312. {
  313. struct sti_vtg *vtg = arg;
  314. vtg->irq_status = readl(vtg->regs + VTG_HOST_ITS);
  315. writel(vtg->irq_status, vtg->regs + VTG_HOST_ITS_BCLR);
  316. /* force sync bus write */
  317. readl(vtg->regs + VTG_HOST_ITS);
  318. return IRQ_WAKE_THREAD;
  319. }
  320. static int vtg_probe(struct platform_device *pdev)
  321. {
  322. struct device *dev = &pdev->dev;
  323. struct sti_vtg *vtg;
  324. struct resource *res;
  325. int ret;
  326. vtg = devm_kzalloc(dev, sizeof(*vtg), GFP_KERNEL);
  327. if (!vtg)
  328. return -ENOMEM;
  329. vtg->dev = dev;
  330. vtg->np = pdev->dev.of_node;
  331. /* Get Memory ressources */
  332. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  333. if (!res) {
  334. DRM_ERROR("Get memory resource failed\n");
  335. return -ENOMEM;
  336. }
  337. vtg->regs = devm_ioremap_nocache(dev, res->start, resource_size(res));
  338. if (!vtg->regs) {
  339. DRM_ERROR("failed to remap I/O memory\n");
  340. return -ENOMEM;
  341. }
  342. vtg->irq = platform_get_irq(pdev, 0);
  343. if (vtg->irq < 0) {
  344. DRM_ERROR("Failed to get VTG interrupt\n");
  345. return vtg->irq;
  346. }
  347. RAW_INIT_NOTIFIER_HEAD(&vtg->notifier_list);
  348. ret = devm_request_threaded_irq(dev, vtg->irq, vtg_irq,
  349. vtg_irq_thread, IRQF_ONESHOT,
  350. dev_name(dev), vtg);
  351. if (ret < 0) {
  352. DRM_ERROR("Failed to register VTG interrupt\n");
  353. return ret;
  354. }
  355. vtg_register(vtg);
  356. platform_set_drvdata(pdev, vtg);
  357. DRM_INFO("%s %s\n", __func__, dev_name(vtg->dev));
  358. return 0;
  359. }
  360. static int vtg_remove(struct platform_device *pdev)
  361. {
  362. return 0;
  363. }
  364. static const struct of_device_id vtg_of_match[] = {
  365. { .compatible = "st,vtg", },
  366. { /* sentinel */ }
  367. };
  368. MODULE_DEVICE_TABLE(of, vtg_of_match);
  369. struct platform_driver sti_vtg_driver = {
  370. .driver = {
  371. .name = "sti-vtg",
  372. .owner = THIS_MODULE,
  373. .of_match_table = vtg_of_match,
  374. },
  375. .probe = vtg_probe,
  376. .remove = vtg_remove,
  377. };
  378. MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
  379. MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
  380. MODULE_LICENSE("GPL");