xilinx-vtc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Xilinx Video Timing Controller
  4. *
  5. * Copyright (C) 2013-2015 Ideas on Board
  6. * Copyright (C) 2013-2015 Xilinx, Inc.
  7. *
  8. * Contacts: Hyun Kwon <hyun.kwon@xilinx.com>
  9. * Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/slab.h>
  16. #include "xilinx-vip.h"
  17. #include "xilinx-vtc.h"
  18. #define XVTC_CONTROL_FIELD_ID_POL_SRC (1 << 26)
  19. #define XVTC_CONTROL_ACTIVE_CHROMA_POL_SRC (1 << 25)
  20. #define XVTC_CONTROL_ACTIVE_VIDEO_POL_SRC (1 << 24)
  21. #define XVTC_CONTROL_HSYNC_POL_SRC (1 << 23)
  22. #define XVTC_CONTROL_VSYNC_POL_SRC (1 << 22)
  23. #define XVTC_CONTROL_HBLANK_POL_SRC (1 << 21)
  24. #define XVTC_CONTROL_VBLANK_POL_SRC (1 << 20)
  25. #define XVTC_CONTROL_CHROMA_SRC (1 << 18)
  26. #define XVTC_CONTROL_VBLANK_HOFF_SRC (1 << 17)
  27. #define XVTC_CONTROL_VSYNC_END_SRC (1 << 16)
  28. #define XVTC_CONTROL_VSYNC_START_SRC (1 << 15)
  29. #define XVTC_CONTROL_ACTIVE_VSIZE_SRC (1 << 14)
  30. #define XVTC_CONTROL_FRAME_VSIZE_SRC (1 << 13)
  31. #define XVTC_CONTROL_HSYNC_END_SRC (1 << 11)
  32. #define XVTC_CONTROL_HSYNC_START_SRC (1 << 10)
  33. #define XVTC_CONTROL_ACTIVE_HSIZE_SRC (1 << 9)
  34. #define XVTC_CONTROL_FRAME_HSIZE_SRC (1 << 8)
  35. #define XVTC_CONTROL_SYNC_ENABLE (1 << 5)
  36. #define XVTC_CONTROL_DET_ENABLE (1 << 3)
  37. #define XVTC_CONTROL_GEN_ENABLE (1 << 2)
  38. #define XVTC_STATUS_FSYNC(n) ((n) << 16)
  39. #define XVTC_STATUS_GEN_ACTIVE_VIDEO (1 << 13)
  40. #define XVTC_STATUS_GEN_VBLANK (1 << 12)
  41. #define XVTC_STATUS_DET_ACTIVE_VIDEO (1 << 11)
  42. #define XVTC_STATUS_DET_VBLANK (1 << 10)
  43. #define XVTC_STATUS_LOCK_LOSS (1 << 9)
  44. #define XVTC_STATUS_LOCK (1 << 8)
  45. #define XVTC_ERROR_ACTIVE_CHROMA_LOCK (1 << 21)
  46. #define XVTC_ERROR_ACTIVE_VIDEO_LOCK (1 << 20)
  47. #define XVTC_ERROR_HSYNC_LOCK (1 << 19)
  48. #define XVTC_ERROR_VSYNC_LOCK (1 << 18)
  49. #define XVTC_ERROR_HBLANK_LOCK (1 << 17)
  50. #define XVTC_ERROR_VBLANK_LOCK (1 << 16)
  51. #define XVTC_IRQ_ENABLE_FSYNC(n) ((n) << 16)
  52. #define XVTC_IRQ_ENABLE_GEN_ACTIVE_VIDEO (1 << 13)
  53. #define XVTC_IRQ_ENABLE_GEN_VBLANK (1 << 12)
  54. #define XVTC_IRQ_ENABLE_DET_ACTIVE_VIDEO (1 << 11)
  55. #define XVTC_IRQ_ENABLE_DET_VBLANK (1 << 10)
  56. #define XVTC_IRQ_ENABLE_LOCK_LOSS (1 << 9)
  57. #define XVTC_IRQ_ENABLE_LOCK (1 << 8)
  58. /*
  59. * The following registers exist in two blocks, one at 0x0020 for the detector
  60. * and one at 0x0060 for the generator.
  61. */
  62. #define XVTC_DETECTOR_OFFSET 0x0020
  63. #define XVTC_GENERATOR_OFFSET 0x0060
  64. #define XVTC_ACTIVE_SIZE 0x0000
  65. #define XVTC_ACTIVE_VSIZE_SHIFT 16
  66. #define XVTC_ACTIVE_VSIZE_MASK (0x1fff << 16)
  67. #define XVTC_ACTIVE_HSIZE_SHIFT 0
  68. #define XVTC_ACTIVE_HSIZE_MASK (0x1fff << 0)
  69. #define XVTC_TIMING_STATUS 0x0004
  70. #define XVTC_TIMING_STATUS_ACTIVE_VIDEO (1 << 2)
  71. #define XVTC_TIMING_STATUS_VBLANK (1 << 1)
  72. #define XVTC_TIMING_STATUS_LOCKED (1 << 0)
  73. #define XVTC_ENCODING 0x0008
  74. #define XVTC_ENCODING_CHROMA_PARITY_SHIFT 8
  75. #define XVTC_ENCODING_CHROMA_PARITY_MASK (3 << 8)
  76. #define XVTC_ENCODING_CHROMA_PARITY_EVEN_ALL (0 << 8)
  77. #define XVTC_ENCODING_CHROMA_PARITY_ODD_ALL (1 << 8)
  78. #define XVTC_ENCODING_CHROMA_PARITY_EVEN_EVEN (2 << 8)
  79. #define XVTC_ENCODING_CHROMA_PARITY_ODD_EVEN (3 << 8)
  80. #define XVTC_ENCODING_VIDEO_FORMAT_SHIFT 0
  81. #define XVTC_ENCODING_VIDEO_FORMAT_MASK (0xf << 0)
  82. #define XVTC_ENCODING_VIDEO_FORMAT_YUV422 (0 << 0)
  83. #define XVTC_ENCODING_VIDEO_FORMAT_YUV444 (1 << 0)
  84. #define XVTC_ENCODING_VIDEO_FORMAT_RGB (2 << 0)
  85. #define XVTC_ENCODING_VIDEO_FORMAT_YUV420 (3 << 0)
  86. #define XVTC_POLARITY 0x000c
  87. #define XVTC_POLARITY_ACTIVE_CHROMA_POL (1 << 5)
  88. #define XVTC_POLARITY_ACTIVE_VIDEO_POL (1 << 4)
  89. #define XVTC_POLARITY_HSYNC_POL (1 << 3)
  90. #define XVTC_POLARITY_VSYNC_POL (1 << 2)
  91. #define XVTC_POLARITY_HBLANK_POL (1 << 1)
  92. #define XVTC_POLARITY_VBLANK_POL (1 << 0)
  93. #define XVTC_HSIZE 0x0010
  94. #define XVTC_HSIZE_MASK (0x1fff << 0)
  95. #define XVTC_VSIZE 0x0014
  96. #define XVTC_VSIZE_MASK (0x1fff << 0)
  97. #define XVTC_HSYNC 0x0018
  98. #define XVTC_HSYNC_END_SHIFT 16
  99. #define XVTC_HSYNC_END_MASK (0x1fff << 16)
  100. #define XVTC_HSYNC_START_SHIFT 0
  101. #define XVTC_HSYNC_START_MASK (0x1fff << 0)
  102. #define XVTC_F0_VBLANK_H 0x001c
  103. #define XVTC_F0_VBLANK_HEND_SHIFT 16
  104. #define XVTC_F0_VBLANK_HEND_MASK (0x1fff << 16)
  105. #define XVTC_F0_VBLANK_HSTART_SHIFT 0
  106. #define XVTC_F0_VBLANK_HSTART_MASK (0x1fff << 0)
  107. #define XVTC_F0_VSYNC_V 0x0020
  108. #define XVTC_F0_VSYNC_VEND_SHIFT 16
  109. #define XVTC_F0_VSYNC_VEND_MASK (0x1fff << 16)
  110. #define XVTC_F0_VSYNC_VSTART_SHIFT 0
  111. #define XVTC_F0_VSYNC_VSTART_MASK (0x1fff << 0)
  112. #define XVTC_F0_VSYNC_H 0x0024
  113. #define XVTC_F0_VSYNC_HEND_SHIFT 16
  114. #define XVTC_F0_VSYNC_HEND_MASK (0x1fff << 16)
  115. #define XVTC_F0_VSYNC_HSTART_SHIFT 0
  116. #define XVTC_F0_VSYNC_HSTART_MASK (0x1fff << 0)
  117. #define XVTC_FRAME_SYNC_CONFIG(n) (0x0100 + 4 * (n))
  118. #define XVTC_FRAME_SYNC_V_START_SHIFT 16
  119. #define XVTC_FRAME_SYNC_V_START_MASK (0x1fff << 16)
  120. #define XVTC_FRAME_SYNC_H_START_SHIFT 0
  121. #define XVTC_FRAME_SYNC_H_START_MASK (0x1fff << 0)
  122. #define XVTC_GENERATOR_GLOBAL_DELAY 0x0104
  123. /**
  124. * struct xvtc_device - Xilinx Video Timing Controller device structure
  125. * @xvip: Xilinx Video IP device
  126. * @list: entry in the global VTC list
  127. * @has_detector: the VTC has a timing detector
  128. * @has_generator: the VTC has a timing generator
  129. * @config: generator timings configuration
  130. */
  131. struct xvtc_device {
  132. struct xvip_device xvip;
  133. struct list_head list;
  134. bool has_detector;
  135. bool has_generator;
  136. struct xvtc_config config;
  137. };
  138. static LIST_HEAD(xvtc_list);
  139. static DEFINE_MUTEX(xvtc_lock);
  140. static inline void xvtc_gen_write(struct xvtc_device *xvtc, u32 addr, u32 value)
  141. {
  142. xvip_write(&xvtc->xvip, XVTC_GENERATOR_OFFSET + addr, value);
  143. }
  144. /* -----------------------------------------------------------------------------
  145. * Generator Operations
  146. */
  147. int xvtc_generator_start(struct xvtc_device *xvtc,
  148. const struct xvtc_config *config)
  149. {
  150. int ret;
  151. if (!xvtc->has_generator)
  152. return -ENXIO;
  153. ret = clk_prepare_enable(xvtc->xvip.clk);
  154. if (ret < 0)
  155. return ret;
  156. /* We don't care about the chroma active signal, encoding parameters are
  157. * not important for now.
  158. */
  159. xvtc_gen_write(xvtc, XVTC_POLARITY,
  160. XVTC_POLARITY_ACTIVE_CHROMA_POL |
  161. XVTC_POLARITY_ACTIVE_VIDEO_POL |
  162. XVTC_POLARITY_HSYNC_POL | XVTC_POLARITY_VSYNC_POL |
  163. XVTC_POLARITY_HBLANK_POL | XVTC_POLARITY_VBLANK_POL);
  164. /* Hardcode the polarity to active high, as required by the video in to
  165. * AXI4-stream core.
  166. */
  167. xvtc_gen_write(xvtc, XVTC_ENCODING, 0);
  168. /* Configure the timings. The VBLANK and VSYNC signals assertion and
  169. * deassertion are hardcoded to the first pixel of the line.
  170. */
  171. xvtc_gen_write(xvtc, XVTC_ACTIVE_SIZE,
  172. (config->vblank_start << XVTC_ACTIVE_VSIZE_SHIFT) |
  173. (config->hblank_start << XVTC_ACTIVE_HSIZE_SHIFT));
  174. xvtc_gen_write(xvtc, XVTC_HSIZE, config->hsize);
  175. xvtc_gen_write(xvtc, XVTC_VSIZE, config->vsize);
  176. xvtc_gen_write(xvtc, XVTC_HSYNC,
  177. (config->hsync_end << XVTC_HSYNC_END_SHIFT) |
  178. (config->hsync_start << XVTC_HSYNC_START_SHIFT));
  179. xvtc_gen_write(xvtc, XVTC_F0_VBLANK_H, 0);
  180. xvtc_gen_write(xvtc, XVTC_F0_VSYNC_V,
  181. (config->vsync_end << XVTC_F0_VSYNC_VEND_SHIFT) |
  182. (config->vsync_start << XVTC_F0_VSYNC_VSTART_SHIFT));
  183. xvtc_gen_write(xvtc, XVTC_F0_VSYNC_H, 0);
  184. /* Enable the generator. Set the source of all generator parameters to
  185. * generator registers.
  186. */
  187. xvip_write(&xvtc->xvip, XVIP_CTRL_CONTROL,
  188. XVTC_CONTROL_ACTIVE_CHROMA_POL_SRC |
  189. XVTC_CONTROL_ACTIVE_VIDEO_POL_SRC |
  190. XVTC_CONTROL_HSYNC_POL_SRC | XVTC_CONTROL_VSYNC_POL_SRC |
  191. XVTC_CONTROL_HBLANK_POL_SRC | XVTC_CONTROL_VBLANK_POL_SRC |
  192. XVTC_CONTROL_CHROMA_SRC | XVTC_CONTROL_VBLANK_HOFF_SRC |
  193. XVTC_CONTROL_VSYNC_END_SRC | XVTC_CONTROL_VSYNC_START_SRC |
  194. XVTC_CONTROL_ACTIVE_VSIZE_SRC |
  195. XVTC_CONTROL_FRAME_VSIZE_SRC | XVTC_CONTROL_HSYNC_END_SRC |
  196. XVTC_CONTROL_HSYNC_START_SRC |
  197. XVTC_CONTROL_ACTIVE_HSIZE_SRC |
  198. XVTC_CONTROL_FRAME_HSIZE_SRC | XVTC_CONTROL_GEN_ENABLE |
  199. XVIP_CTRL_CONTROL_REG_UPDATE);
  200. return 0;
  201. }
  202. EXPORT_SYMBOL_GPL(xvtc_generator_start);
  203. int xvtc_generator_stop(struct xvtc_device *xvtc)
  204. {
  205. if (!xvtc->has_generator)
  206. return -ENXIO;
  207. xvip_write(&xvtc->xvip, XVIP_CTRL_CONTROL, 0);
  208. clk_disable_unprepare(xvtc->xvip.clk);
  209. return 0;
  210. }
  211. EXPORT_SYMBOL_GPL(xvtc_generator_stop);
  212. struct xvtc_device *xvtc_of_get(struct device_node *np)
  213. {
  214. struct device_node *xvtc_node;
  215. struct xvtc_device *found = NULL;
  216. struct xvtc_device *xvtc;
  217. if (!of_find_property(np, "xlnx,vtc", NULL))
  218. return NULL;
  219. xvtc_node = of_parse_phandle(np, "xlnx,vtc", 0);
  220. if (xvtc_node == NULL)
  221. return ERR_PTR(-EINVAL);
  222. mutex_lock(&xvtc_lock);
  223. list_for_each_entry(xvtc, &xvtc_list, list) {
  224. if (xvtc->xvip.dev->of_node == xvtc_node) {
  225. found = xvtc;
  226. break;
  227. }
  228. }
  229. mutex_unlock(&xvtc_lock);
  230. of_node_put(xvtc_node);
  231. if (!found)
  232. return ERR_PTR(-EPROBE_DEFER);
  233. return found;
  234. }
  235. EXPORT_SYMBOL_GPL(xvtc_of_get);
  236. void xvtc_put(struct xvtc_device *xvtc)
  237. {
  238. }
  239. EXPORT_SYMBOL_GPL(xvtc_put);
  240. /* -----------------------------------------------------------------------------
  241. * Registration and Unregistration
  242. */
  243. static void xvtc_register_device(struct xvtc_device *xvtc)
  244. {
  245. mutex_lock(&xvtc_lock);
  246. list_add_tail(&xvtc->list, &xvtc_list);
  247. mutex_unlock(&xvtc_lock);
  248. }
  249. static void xvtc_unregister_device(struct xvtc_device *xvtc)
  250. {
  251. mutex_lock(&xvtc_lock);
  252. list_del(&xvtc->list);
  253. mutex_unlock(&xvtc_lock);
  254. }
  255. /* -----------------------------------------------------------------------------
  256. * Platform Device Driver
  257. */
  258. static int xvtc_parse_of(struct xvtc_device *xvtc)
  259. {
  260. struct device_node *node = xvtc->xvip.dev->of_node;
  261. xvtc->has_detector = of_property_read_bool(node, "xlnx,detector");
  262. xvtc->has_generator = of_property_read_bool(node, "xlnx,generator");
  263. return 0;
  264. }
  265. static int xvtc_probe(struct platform_device *pdev)
  266. {
  267. struct xvtc_device *xvtc;
  268. int ret;
  269. xvtc = devm_kzalloc(&pdev->dev, sizeof(*xvtc), GFP_KERNEL);
  270. if (!xvtc)
  271. return -ENOMEM;
  272. xvtc->xvip.dev = &pdev->dev;
  273. ret = xvtc_parse_of(xvtc);
  274. if (ret < 0)
  275. return ret;
  276. ret = xvip_init_resources(&xvtc->xvip);
  277. if (ret < 0)
  278. return ret;
  279. platform_set_drvdata(pdev, xvtc);
  280. xvip_print_version(&xvtc->xvip);
  281. xvtc_register_device(xvtc);
  282. return 0;
  283. }
  284. static int xvtc_remove(struct platform_device *pdev)
  285. {
  286. struct xvtc_device *xvtc = platform_get_drvdata(pdev);
  287. xvtc_unregister_device(xvtc);
  288. xvip_cleanup_resources(&xvtc->xvip);
  289. return 0;
  290. }
  291. static const struct of_device_id xvtc_of_id_table[] = {
  292. { .compatible = "xlnx,v-tc-6.1" },
  293. { }
  294. };
  295. MODULE_DEVICE_TABLE(of, xvtc_of_id_table);
  296. static struct platform_driver xvtc_driver = {
  297. .driver = {
  298. .name = "xilinx-vtc",
  299. .of_match_table = xvtc_of_id_table,
  300. },
  301. .probe = xvtc_probe,
  302. .remove = xvtc_remove,
  303. };
  304. module_platform_driver(xvtc_driver);
  305. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  306. MODULE_DESCRIPTION("Xilinx Video Timing Controller Driver");
  307. MODULE_LICENSE("GPL v2");