xilinx-vip.h 7.4 KB

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