sti_vtg.c 13 KB

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