xilinx-vtc.c 11 KB

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