sti_vtg.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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_TYPE_MASTER 0
  15. #define VTG_TYPE_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 Arbitrary Waveform Generator in nb of pixels */
  55. #define AWG_DELAY_HD (-9)
  56. #define AWG_DELAY_ED (-8)
  57. #define AWG_DELAY_SD (-7)
  58. LIST_HEAD(vtg_lookup);
  59. /**
  60. * STI VTG structure
  61. *
  62. * @dev: pointer to device driver
  63. * @data: data associated to the device
  64. * @irq: VTG irq
  65. * @type: VTG type (main or aux)
  66. * @notifier_list: notifier callback
  67. * @crtc_id: the crtc id for vblank event
  68. * @slave: slave vtg
  69. * @link: List node to link the structure in lookup list
  70. */
  71. struct sti_vtg {
  72. struct device *dev;
  73. struct device_node *np;
  74. void __iomem *regs;
  75. int irq;
  76. u32 irq_status;
  77. struct raw_notifier_head notifier_list;
  78. int crtc_id;
  79. struct sti_vtg *slave;
  80. struct list_head link;
  81. };
  82. static void vtg_register(struct sti_vtg *vtg)
  83. {
  84. list_add_tail(&vtg->link, &vtg_lookup);
  85. }
  86. struct sti_vtg *of_vtg_find(struct device_node *np)
  87. {
  88. struct sti_vtg *vtg;
  89. list_for_each_entry(vtg, &vtg_lookup, link) {
  90. if (vtg->np == np)
  91. return vtg;
  92. }
  93. return NULL;
  94. }
  95. EXPORT_SYMBOL(of_vtg_find);
  96. static void vtg_reset(struct sti_vtg *vtg)
  97. {
  98. /* reset slave and then master */
  99. if (vtg->slave)
  100. vtg_reset(vtg->slave);
  101. writel(1, vtg->regs + VTG_DRST_AUTOC);
  102. }
  103. static void vtg_set_output_window(void __iomem *regs,
  104. const struct drm_display_mode *mode)
  105. {
  106. u32 video_top_field_start;
  107. u32 video_top_field_stop;
  108. u32 video_bottom_field_start;
  109. u32 video_bottom_field_stop;
  110. u32 xstart = sti_vtg_get_pixel_number(*mode, 0);
  111. u32 ystart = sti_vtg_get_line_number(*mode, 0);
  112. u32 xstop = sti_vtg_get_pixel_number(*mode, mode->hdisplay - 1);
  113. u32 ystop = sti_vtg_get_line_number(*mode, mode->vdisplay - 1);
  114. /* Set output window to fit the display mode selected */
  115. video_top_field_start = (ystart << 16) | xstart;
  116. video_top_field_stop = (ystop << 16) | xstop;
  117. /* Only progressive supported for now */
  118. video_bottom_field_start = video_top_field_start;
  119. video_bottom_field_stop = video_top_field_stop;
  120. writel(video_top_field_start, regs + VTG_VID_TFO);
  121. writel(video_top_field_stop, regs + VTG_VID_TFS);
  122. writel(video_bottom_field_start, regs + VTG_VID_BFO);
  123. writel(video_bottom_field_stop, regs + VTG_VID_BFS);
  124. }
  125. static void vtg_set_mode(struct sti_vtg *vtg,
  126. int type, const struct drm_display_mode *mode)
  127. {
  128. u32 tmp;
  129. if (vtg->slave)
  130. vtg_set_mode(vtg->slave, VTG_TYPE_SLAVE_BY_EXT0, mode);
  131. /* Set the number of clock cycles per line */
  132. writel(mode->htotal, vtg->regs + VTG_CLKLN);
  133. /* Set Half Line Per Field (only progressive supported for now) */
  134. writel(mode->vtotal * 2, vtg->regs + VTG_HLFLN);
  135. /* Program output window */
  136. vtg_set_output_window(vtg->regs, mode);
  137. /* prepare VTG set 1 for HDMI */
  138. tmp = (mode->hsync_end - mode->hsync_start + HDMI_DELAY) << 16;
  139. tmp |= HDMI_DELAY;
  140. writel(tmp, vtg->regs + VTG_H_HD_1);
  141. tmp = (mode->vsync_end - mode->vsync_start + 1) << 16;
  142. tmp |= 1;
  143. writel(tmp, vtg->regs + VTG_TOP_V_VD_1);
  144. writel(tmp, vtg->regs + VTG_BOT_V_VD_1);
  145. tmp = HDMI_DELAY << 16;
  146. tmp |= HDMI_DELAY;
  147. writel(tmp, vtg->regs + VTG_TOP_V_HD_1);
  148. writel(tmp, vtg->regs + VTG_BOT_V_HD_1);
  149. /* prepare VTG set 2 for for HD DCS */
  150. tmp = (mode->hsync_end - mode->hsync_start) << 16;
  151. writel(tmp, vtg->regs + VTG_H_HD_2);
  152. tmp = (mode->vsync_end - mode->vsync_start + 1) << 16;
  153. tmp |= 1;
  154. writel(tmp, vtg->regs + VTG_TOP_V_VD_2);
  155. writel(tmp, vtg->regs + VTG_BOT_V_VD_2);
  156. writel(0, vtg->regs + VTG_TOP_V_HD_2);
  157. writel(0, vtg->regs + VTG_BOT_V_HD_2);
  158. /* prepare VTG set 3 for HD Analog in HD mode */
  159. tmp = (mode->hsync_end - mode->hsync_start + AWG_DELAY_HD) << 16;
  160. tmp |= mode->htotal + AWG_DELAY_HD;
  161. writel(tmp, vtg->regs + VTG_H_HD_3);
  162. tmp = (mode->vsync_end - mode->vsync_start) << 16;
  163. tmp |= mode->vtotal;
  164. writel(tmp, vtg->regs + VTG_TOP_V_VD_3);
  165. writel(tmp, vtg->regs + VTG_BOT_V_VD_3);
  166. tmp = (mode->htotal + AWG_DELAY_HD) << 16;
  167. tmp |= mode->htotal + AWG_DELAY_HD;
  168. writel(tmp, vtg->regs + VTG_TOP_V_HD_3);
  169. writel(tmp, vtg->regs + VTG_BOT_V_HD_3);
  170. /* Prepare VTG set 4 for DVO */
  171. tmp = (mode->hsync_end - mode->hsync_start) << 16;
  172. writel(tmp, vtg->regs + VTG_H_HD_4);
  173. tmp = (mode->vsync_end - mode->vsync_start + 1) << 16;
  174. tmp |= 1;
  175. writel(tmp, vtg->regs + VTG_TOP_V_VD_4);
  176. writel(tmp, vtg->regs + VTG_BOT_V_VD_4);
  177. writel(0, vtg->regs + VTG_TOP_V_HD_4);
  178. writel(0, vtg->regs + VTG_BOT_V_HD_4);
  179. /* mode */
  180. writel(type, vtg->regs + VTG_MODE);
  181. }
  182. static void vtg_enable_irq(struct sti_vtg *vtg)
  183. {
  184. /* clear interrupt status and mask */
  185. writel(0xFFFF, vtg->regs + VTG_HOST_ITS_BCLR);
  186. writel(0xFFFF, vtg->regs + VTG_HOST_ITM_BCLR);
  187. writel(VTG_IRQ_MASK, vtg->regs + VTG_HOST_ITM_BSET);
  188. }
  189. void sti_vtg_set_config(struct sti_vtg *vtg,
  190. const struct drm_display_mode *mode)
  191. {
  192. /* write configuration */
  193. vtg_set_mode(vtg, VTG_TYPE_MASTER, mode);
  194. vtg_reset(vtg);
  195. /* enable irq for the vtg vblank synchro */
  196. if (vtg->slave)
  197. vtg_enable_irq(vtg->slave);
  198. else
  199. vtg_enable_irq(vtg);
  200. }
  201. EXPORT_SYMBOL(sti_vtg_set_config);
  202. /**
  203. * sti_vtg_get_line_number
  204. *
  205. * @mode: display mode to be used
  206. * @y: line
  207. *
  208. * Return the line number according to the display mode taking
  209. * into account the Sync and Back Porch information.
  210. * Video frame line numbers start at 1, y starts at 0.
  211. * In interlaced modes the start line is the field line number of the odd
  212. * field, but y is still defined as a progressive frame.
  213. */
  214. u32 sti_vtg_get_line_number(struct drm_display_mode mode, int y)
  215. {
  216. u32 start_line = mode.vtotal - mode.vsync_start + 1;
  217. if (mode.flags & DRM_MODE_FLAG_INTERLACE)
  218. start_line *= 2;
  219. return start_line + y;
  220. }
  221. EXPORT_SYMBOL(sti_vtg_get_line_number);
  222. /**
  223. * sti_vtg_get_pixel_number
  224. *
  225. * @mode: display mode to be used
  226. * @x: row
  227. *
  228. * Return the pixel number according to the display mode taking
  229. * into account the Sync and Back Porch information.
  230. * Pixels are counted from 0.
  231. */
  232. u32 sti_vtg_get_pixel_number(struct drm_display_mode mode, int x)
  233. {
  234. return mode.htotal - mode.hsync_start + x;
  235. }
  236. EXPORT_SYMBOL(sti_vtg_get_pixel_number);
  237. int sti_vtg_register_client(struct sti_vtg *vtg,
  238. struct notifier_block *nb, int crtc_id)
  239. {
  240. if (vtg->slave)
  241. return sti_vtg_register_client(vtg->slave, nb, crtc_id);
  242. vtg->crtc_id = crtc_id;
  243. return raw_notifier_chain_register(&vtg->notifier_list, nb);
  244. }
  245. EXPORT_SYMBOL(sti_vtg_register_client);
  246. int sti_vtg_unregister_client(struct sti_vtg *vtg, struct notifier_block *nb)
  247. {
  248. if (vtg->slave)
  249. return sti_vtg_unregister_client(vtg->slave, nb);
  250. return raw_notifier_chain_unregister(&vtg->notifier_list, nb);
  251. }
  252. EXPORT_SYMBOL(sti_vtg_unregister_client);
  253. static irqreturn_t vtg_irq_thread(int irq, void *arg)
  254. {
  255. struct sti_vtg *vtg = arg;
  256. u32 event;
  257. event = (vtg->irq_status & VTG_IRQ_TOP) ?
  258. VTG_TOP_FIELD_EVENT : VTG_BOTTOM_FIELD_EVENT;
  259. raw_notifier_call_chain(&vtg->notifier_list, event, &vtg->crtc_id);
  260. return IRQ_HANDLED;
  261. }
  262. static irqreturn_t vtg_irq(int irq, void *arg)
  263. {
  264. struct sti_vtg *vtg = arg;
  265. vtg->irq_status = readl(vtg->regs + VTG_HOST_ITS);
  266. writel(vtg->irq_status, vtg->regs + VTG_HOST_ITS_BCLR);
  267. /* force sync bus write */
  268. readl(vtg->regs + VTG_HOST_ITS);
  269. return IRQ_WAKE_THREAD;
  270. }
  271. static int vtg_probe(struct platform_device *pdev)
  272. {
  273. struct device *dev = &pdev->dev;
  274. struct device_node *np;
  275. struct sti_vtg *vtg;
  276. struct resource *res;
  277. int ret;
  278. vtg = devm_kzalloc(dev, sizeof(*vtg), GFP_KERNEL);
  279. if (!vtg)
  280. return -ENOMEM;
  281. vtg->dev = dev;
  282. vtg->np = pdev->dev.of_node;
  283. /* Get Memory ressources */
  284. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  285. if (!res) {
  286. DRM_ERROR("Get memory resource failed\n");
  287. return -ENOMEM;
  288. }
  289. vtg->regs = devm_ioremap_nocache(dev, res->start, resource_size(res));
  290. np = of_parse_phandle(pdev->dev.of_node, "st,slave", 0);
  291. if (np) {
  292. vtg->slave = of_vtg_find(np);
  293. if (!vtg->slave)
  294. return -EPROBE_DEFER;
  295. } else {
  296. vtg->irq = platform_get_irq(pdev, 0);
  297. if (IS_ERR_VALUE(vtg->irq)) {
  298. DRM_ERROR("Failed to get VTG interrupt\n");
  299. return vtg->irq;
  300. }
  301. RAW_INIT_NOTIFIER_HEAD(&vtg->notifier_list);
  302. ret = devm_request_threaded_irq(dev, vtg->irq, vtg_irq,
  303. vtg_irq_thread, IRQF_ONESHOT,
  304. dev_name(dev), vtg);
  305. if (IS_ERR_VALUE(ret)) {
  306. DRM_ERROR("Failed to register VTG interrupt\n");
  307. return ret;
  308. }
  309. }
  310. vtg_register(vtg);
  311. platform_set_drvdata(pdev, vtg);
  312. DRM_INFO("%s %s\n", __func__, dev_name(vtg->dev));
  313. return 0;
  314. }
  315. static int vtg_remove(struct platform_device *pdev)
  316. {
  317. return 0;
  318. }
  319. static const struct of_device_id vtg_of_match[] = {
  320. { .compatible = "st,vtg", },
  321. { /* sentinel */ }
  322. };
  323. MODULE_DEVICE_TABLE(of, vtg_of_match);
  324. struct platform_driver sti_vtg_driver = {
  325. .driver = {
  326. .name = "sti-vtg",
  327. .owner = THIS_MODULE,
  328. .of_match_table = vtg_of_match,
  329. },
  330. .probe = vtg_probe,
  331. .remove = vtg_remove,
  332. };
  333. module_platform_driver(sti_vtg_driver);
  334. MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
  335. MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
  336. MODULE_LICENSE("GPL");