xilinx-vip.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Xilinx Video IP Core
  4. *
  5. * Copyright (C) 2013-2015 Ideas on Board
  6. * Copyright (C) 2013-2015 Xilinx, Inc.
  7. *
  8. * Contacts: Hyun Kwon <[email protected]>
  9. * Laurent Pinchart <[email protected]>
  10. */
  11. #ifndef __XILINX_VIP_H__
  12. #define __XILINX_VIP_H__
  13. #include <linux/bitops.h>
  14. #include <linux/io.h>
  15. #include <media/v4l2-subdev.h>
  16. struct clk;
  17. /*
  18. * Minimum and maximum width and height common to most video IP cores. IP
  19. * cores with different requirements must define their own values.
  20. */
  21. #define XVIP_MIN_WIDTH 32
  22. #define XVIP_MAX_WIDTH 7680
  23. #define XVIP_MIN_HEIGHT 32
  24. #define XVIP_MAX_HEIGHT 7680
  25. /*
  26. * Pad IDs. IP cores with multiple inputs or outputs should define their own
  27. * values.
  28. */
  29. #define XVIP_PAD_SINK 0
  30. #define XVIP_PAD_SOURCE 1
  31. /* Xilinx Video IP Control Registers */
  32. #define XVIP_CTRL_CONTROL 0x0000
  33. #define XVIP_CTRL_CONTROL_SW_ENABLE BIT(0)
  34. #define XVIP_CTRL_CONTROL_REG_UPDATE BIT(1)
  35. #define XVIP_CTRL_CONTROL_BYPASS BIT(4)
  36. #define XVIP_CTRL_CONTROL_TEST_PATTERN BIT(5)
  37. #define XVIP_CTRL_CONTROL_FRAME_SYNC_RESET BIT(30)
  38. #define XVIP_CTRL_CONTROL_SW_RESET BIT(31)
  39. #define XVIP_CTRL_STATUS 0x0004
  40. #define XVIP_CTRL_STATUS_PROC_STARTED BIT(0)
  41. #define XVIP_CTRL_STATUS_EOF BIT(1)
  42. #define XVIP_CTRL_ERROR 0x0008
  43. #define XVIP_CTRL_ERROR_SLAVE_EOL_EARLY BIT(0)
  44. #define XVIP_CTRL_ERROR_SLAVE_EOL_LATE BIT(1)
  45. #define XVIP_CTRL_ERROR_SLAVE_SOF_EARLY BIT(2)
  46. #define XVIP_CTRL_ERROR_SLAVE_SOF_LATE BIT(3)
  47. #define XVIP_CTRL_IRQ_ENABLE 0x000c
  48. #define XVIP_CTRL_IRQ_ENABLE_PROC_STARTED BIT(0)
  49. #define XVIP_CTRL_IRQ_EOF BIT(1)
  50. #define XVIP_CTRL_VERSION 0x0010
  51. #define XVIP_CTRL_VERSION_MAJOR_MASK (0xff << 24)
  52. #define XVIP_CTRL_VERSION_MAJOR_SHIFT 24
  53. #define XVIP_CTRL_VERSION_MINOR_MASK (0xff << 16)
  54. #define XVIP_CTRL_VERSION_MINOR_SHIFT 16
  55. #define XVIP_CTRL_VERSION_REVISION_MASK (0xf << 12)
  56. #define XVIP_CTRL_VERSION_REVISION_SHIFT 12
  57. #define XVIP_CTRL_VERSION_PATCH_MASK (0xf << 8)
  58. #define XVIP_CTRL_VERSION_PATCH_SHIFT 8
  59. #define XVIP_CTRL_VERSION_INTERNAL_MASK (0xff << 0)
  60. #define XVIP_CTRL_VERSION_INTERNAL_SHIFT 0
  61. /* Xilinx Video IP Timing Registers */
  62. #define XVIP_ACTIVE_SIZE 0x0020
  63. #define XVIP_ACTIVE_VSIZE_MASK (0x7ff << 16)
  64. #define XVIP_ACTIVE_VSIZE_SHIFT 16
  65. #define XVIP_ACTIVE_HSIZE_MASK (0x7ff << 0)
  66. #define XVIP_ACTIVE_HSIZE_SHIFT 0
  67. #define XVIP_ENCODING 0x0028
  68. #define XVIP_ENCODING_NBITS_8 (0 << 4)
  69. #define XVIP_ENCODING_NBITS_10 (1 << 4)
  70. #define XVIP_ENCODING_NBITS_12 (2 << 4)
  71. #define XVIP_ENCODING_NBITS_16 (3 << 4)
  72. #define XVIP_ENCODING_NBITS_MASK (3 << 4)
  73. #define XVIP_ENCODING_NBITS_SHIFT 4
  74. #define XVIP_ENCODING_VIDEO_FORMAT_YUV422 (0 << 0)
  75. #define XVIP_ENCODING_VIDEO_FORMAT_YUV444 (1 << 0)
  76. #define XVIP_ENCODING_VIDEO_FORMAT_RGB (2 << 0)
  77. #define XVIP_ENCODING_VIDEO_FORMAT_YUV420 (3 << 0)
  78. #define XVIP_ENCODING_VIDEO_FORMAT_MASK (3 << 0)
  79. #define XVIP_ENCODING_VIDEO_FORMAT_SHIFT 0
  80. /**
  81. * struct xvip_device - Xilinx Video IP device structure
  82. * @subdev: V4L2 subdevice
  83. * @dev: (OF) device
  84. * @iomem: device I/O register space remapped to kernel virtual memory
  85. * @clk: video core clock
  86. * @saved_ctrl: saved control register for resume / suspend
  87. */
  88. struct xvip_device {
  89. struct v4l2_subdev subdev;
  90. struct device *dev;
  91. void __iomem *iomem;
  92. struct clk *clk;
  93. u32 saved_ctrl;
  94. };
  95. /**
  96. * struct xvip_video_format - Xilinx Video IP video format description
  97. * @vf_code: AXI4 video format code
  98. * @width: AXI4 format width in bits per component
  99. * @pattern: CFA pattern for Mono/Sensor formats
  100. * @code: media bus format code
  101. * @bpp: bytes per pixel (when stored in memory)
  102. * @fourcc: V4L2 pixel format FCC identifier
  103. */
  104. struct xvip_video_format {
  105. unsigned int vf_code;
  106. unsigned int width;
  107. const char *pattern;
  108. unsigned int code;
  109. unsigned int bpp;
  110. u32 fourcc;
  111. };
  112. const struct xvip_video_format *xvip_get_format_by_code(unsigned int code);
  113. const struct xvip_video_format *xvip_get_format_by_fourcc(u32 fourcc);
  114. const struct xvip_video_format *xvip_of_get_format(struct device_node *node);
  115. void xvip_set_format_size(struct v4l2_mbus_framefmt *format,
  116. const struct v4l2_subdev_format *fmt);
  117. int xvip_enum_mbus_code(struct v4l2_subdev *subdev,
  118. struct v4l2_subdev_state *sd_state,
  119. struct v4l2_subdev_mbus_code_enum *code);
  120. int xvip_enum_frame_size(struct v4l2_subdev *subdev,
  121. struct v4l2_subdev_state *sd_state,
  122. struct v4l2_subdev_frame_size_enum *fse);
  123. static inline u32 xvip_read(struct xvip_device *xvip, u32 addr)
  124. {
  125. return ioread32(xvip->iomem + addr);
  126. }
  127. static inline void xvip_write(struct xvip_device *xvip, u32 addr, u32 value)
  128. {
  129. iowrite32(value, xvip->iomem + addr);
  130. }
  131. static inline void xvip_clr(struct xvip_device *xvip, u32 addr, u32 clr)
  132. {
  133. xvip_write(xvip, addr, xvip_read(xvip, addr) & ~clr);
  134. }
  135. static inline void xvip_set(struct xvip_device *xvip, u32 addr, u32 set)
  136. {
  137. xvip_write(xvip, addr, xvip_read(xvip, addr) | set);
  138. }
  139. void xvip_clr_or_set(struct xvip_device *xvip, u32 addr, u32 mask, bool set);
  140. void xvip_clr_and_set(struct xvip_device *xvip, u32 addr, u32 clr, u32 set);
  141. int xvip_init_resources(struct xvip_device *xvip);
  142. void xvip_cleanup_resources(struct xvip_device *xvip);
  143. static inline void xvip_reset(struct xvip_device *xvip)
  144. {
  145. xvip_write(xvip, XVIP_CTRL_CONTROL, XVIP_CTRL_CONTROL_SW_RESET);
  146. }
  147. static inline void xvip_start(struct xvip_device *xvip)
  148. {
  149. xvip_set(xvip, XVIP_CTRL_CONTROL,
  150. XVIP_CTRL_CONTROL_SW_ENABLE | XVIP_CTRL_CONTROL_REG_UPDATE);
  151. }
  152. static inline void xvip_stop(struct xvip_device *xvip)
  153. {
  154. xvip_clr(xvip, XVIP_CTRL_CONTROL, XVIP_CTRL_CONTROL_SW_ENABLE);
  155. }
  156. static inline void xvip_resume(struct xvip_device *xvip)
  157. {
  158. xvip_write(xvip, XVIP_CTRL_CONTROL,
  159. xvip->saved_ctrl | XVIP_CTRL_CONTROL_SW_ENABLE);
  160. }
  161. static inline void xvip_suspend(struct xvip_device *xvip)
  162. {
  163. xvip->saved_ctrl = xvip_read(xvip, XVIP_CTRL_CONTROL);
  164. xvip_write(xvip, XVIP_CTRL_CONTROL,
  165. xvip->saved_ctrl & ~XVIP_CTRL_CONTROL_SW_ENABLE);
  166. }
  167. static inline void xvip_set_frame_size(struct xvip_device *xvip,
  168. const struct v4l2_mbus_framefmt *format)
  169. {
  170. xvip_write(xvip, XVIP_ACTIVE_SIZE,
  171. (format->height << XVIP_ACTIVE_VSIZE_SHIFT) |
  172. (format->width << XVIP_ACTIVE_HSIZE_SHIFT));
  173. }
  174. static inline void xvip_get_frame_size(struct xvip_device *xvip,
  175. struct v4l2_mbus_framefmt *format)
  176. {
  177. u32 reg;
  178. reg = xvip_read(xvip, XVIP_ACTIVE_SIZE);
  179. format->width = (reg & XVIP_ACTIVE_HSIZE_MASK) >>
  180. XVIP_ACTIVE_HSIZE_SHIFT;
  181. format->height = (reg & XVIP_ACTIVE_VSIZE_MASK) >>
  182. XVIP_ACTIVE_VSIZE_SHIFT;
  183. }
  184. static inline void xvip_enable_reg_update(struct xvip_device *xvip)
  185. {
  186. xvip_set(xvip, XVIP_CTRL_CONTROL, XVIP_CTRL_CONTROL_REG_UPDATE);
  187. }
  188. static inline void xvip_disable_reg_update(struct xvip_device *xvip)
  189. {
  190. xvip_clr(xvip, XVIP_CTRL_CONTROL, XVIP_CTRL_CONTROL_REG_UPDATE);
  191. }
  192. static inline void xvip_print_version(struct xvip_device *xvip)
  193. {
  194. u32 version;
  195. version = xvip_read(xvip, XVIP_CTRL_VERSION);
  196. dev_info(xvip->dev, "device found, version %u.%02x%x\n",
  197. ((version & XVIP_CTRL_VERSION_MAJOR_MASK) >>
  198. XVIP_CTRL_VERSION_MAJOR_SHIFT),
  199. ((version & XVIP_CTRL_VERSION_MINOR_MASK) >>
  200. XVIP_CTRL_VERSION_MINOR_SHIFT),
  201. ((version & XVIP_CTRL_VERSION_REVISION_MASK) >>
  202. XVIP_CTRL_VERSION_REVISION_SHIFT));
  203. }
  204. #endif /* __XILINX_VIP_H__ */