sti_vtg.c 13 KB

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