xilinx-vip.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. #include <linux/clk.h>
  12. #include <linux/export.h>
  13. #include <linux/kernel.h>
  14. #include <linux/of.h>
  15. #include <linux/platform_device.h>
  16. #include <dt-bindings/media/xilinx-vip.h>
  17. #include "xilinx-vip.h"
  18. /* -----------------------------------------------------------------------------
  19. * Helper functions
  20. */
  21. static const struct xvip_video_format xvip_video_formats[] = {
  22. { XVIP_VF_YUV_422, 8, NULL, MEDIA_BUS_FMT_UYVY8_1X16,
  23. 2, V4L2_PIX_FMT_YUYV },
  24. { XVIP_VF_YUV_444, 8, NULL, MEDIA_BUS_FMT_VUY8_1X24,
  25. 3, V4L2_PIX_FMT_YUV444 },
  26. { XVIP_VF_RBG, 8, NULL, MEDIA_BUS_FMT_RBG888_1X24,
  27. 3, 0 },
  28. { XVIP_VF_MONO_SENSOR, 8, "mono", MEDIA_BUS_FMT_Y8_1X8,
  29. 1, V4L2_PIX_FMT_GREY },
  30. { XVIP_VF_MONO_SENSOR, 8, "rggb", MEDIA_BUS_FMT_SRGGB8_1X8,
  31. 1, V4L2_PIX_FMT_SRGGB8 },
  32. { XVIP_VF_MONO_SENSOR, 8, "grbg", MEDIA_BUS_FMT_SGRBG8_1X8,
  33. 1, V4L2_PIX_FMT_SGRBG8 },
  34. { XVIP_VF_MONO_SENSOR, 8, "gbrg", MEDIA_BUS_FMT_SGBRG8_1X8,
  35. 1, V4L2_PIX_FMT_SGBRG8 },
  36. { XVIP_VF_MONO_SENSOR, 8, "bggr", MEDIA_BUS_FMT_SBGGR8_1X8,
  37. 1, V4L2_PIX_FMT_SBGGR8 },
  38. { XVIP_VF_MONO_SENSOR, 12, "mono", MEDIA_BUS_FMT_Y12_1X12,
  39. 2, V4L2_PIX_FMT_Y12 },
  40. };
  41. /**
  42. * xvip_get_format_by_code - Retrieve format information for a media bus code
  43. * @code: the format media bus code
  44. *
  45. * Return: a pointer to the format information structure corresponding to the
  46. * given V4L2 media bus format @code, or ERR_PTR if no corresponding format can
  47. * be found.
  48. */
  49. const struct xvip_video_format *xvip_get_format_by_code(unsigned int code)
  50. {
  51. unsigned int i;
  52. for (i = 0; i < ARRAY_SIZE(xvip_video_formats); ++i) {
  53. const struct xvip_video_format *format = &xvip_video_formats[i];
  54. if (format->code == code)
  55. return format;
  56. }
  57. return ERR_PTR(-EINVAL);
  58. }
  59. EXPORT_SYMBOL_GPL(xvip_get_format_by_code);
  60. /**
  61. * xvip_get_format_by_fourcc - Retrieve format information for a 4CC
  62. * @fourcc: the format 4CC
  63. *
  64. * Return: a pointer to the format information structure corresponding to the
  65. * given V4L2 format @fourcc. If not found, return a pointer to the first
  66. * available format (V4L2_PIX_FMT_YUYV).
  67. */
  68. const struct xvip_video_format *xvip_get_format_by_fourcc(u32 fourcc)
  69. {
  70. unsigned int i;
  71. for (i = 0; i < ARRAY_SIZE(xvip_video_formats); ++i) {
  72. const struct xvip_video_format *format = &xvip_video_formats[i];
  73. if (format->fourcc == fourcc)
  74. return format;
  75. }
  76. return &xvip_video_formats[0];
  77. }
  78. EXPORT_SYMBOL_GPL(xvip_get_format_by_fourcc);
  79. /**
  80. * xvip_of_get_format - Parse a device tree node and return format information
  81. * @node: the device tree node
  82. *
  83. * Read the xlnx,video-format, xlnx,video-width and xlnx,cfa-pattern properties
  84. * from the device tree @node passed as an argument and return the corresponding
  85. * format information.
  86. *
  87. * Return: a pointer to the format information structure corresponding to the
  88. * format name and width, or ERR_PTR if no corresponding format can be found.
  89. */
  90. const struct xvip_video_format *xvip_of_get_format(struct device_node *node)
  91. {
  92. const char *pattern = "mono";
  93. unsigned int vf_code;
  94. unsigned int i;
  95. u32 width;
  96. int ret;
  97. ret = of_property_read_u32(node, "xlnx,video-format", &vf_code);
  98. if (ret < 0)
  99. return ERR_PTR(ret);
  100. ret = of_property_read_u32(node, "xlnx,video-width", &width);
  101. if (ret < 0)
  102. return ERR_PTR(ret);
  103. if (vf_code == XVIP_VF_MONO_SENSOR)
  104. of_property_read_string(node, "xlnx,cfa-pattern", &pattern);
  105. for (i = 0; i < ARRAY_SIZE(xvip_video_formats); ++i) {
  106. const struct xvip_video_format *format = &xvip_video_formats[i];
  107. if (format->vf_code != vf_code || format->width != width)
  108. continue;
  109. if (vf_code == XVIP_VF_MONO_SENSOR &&
  110. strcmp(pattern, format->pattern))
  111. continue;
  112. return format;
  113. }
  114. return ERR_PTR(-EINVAL);
  115. }
  116. EXPORT_SYMBOL_GPL(xvip_of_get_format);
  117. /**
  118. * xvip_set_format_size - Set the media bus frame format size
  119. * @format: V4L2 frame format on media bus
  120. * @fmt: media bus format
  121. *
  122. * Set the media bus frame format size. The width / height from the subdevice
  123. * format are set to the given media bus format. The new format size is stored
  124. * in @format. The width and height are clamped using default min / max values.
  125. */
  126. void xvip_set_format_size(struct v4l2_mbus_framefmt *format,
  127. const struct v4l2_subdev_format *fmt)
  128. {
  129. format->width = clamp_t(unsigned int, fmt->format.width,
  130. XVIP_MIN_WIDTH, XVIP_MAX_WIDTH);
  131. format->height = clamp_t(unsigned int, fmt->format.height,
  132. XVIP_MIN_HEIGHT, XVIP_MAX_HEIGHT);
  133. }
  134. EXPORT_SYMBOL_GPL(xvip_set_format_size);
  135. /**
  136. * xvip_clr_or_set - Clear or set the register with a bitmask
  137. * @xvip: Xilinx Video IP device
  138. * @addr: address of register
  139. * @mask: bitmask to be set or cleared
  140. * @set: boolean flag indicating whether to set or clear
  141. *
  142. * Clear or set the register at address @addr with a bitmask @mask depending on
  143. * the boolean flag @set. When the flag @set is true, the bitmask is set in
  144. * the register, otherwise the bitmask is cleared from the register
  145. * when the flag @set is false.
  146. *
  147. * Fox example, this function can be used to set a control with a boolean value
  148. * requested by users. If the caller knows whether to set or clear in the first
  149. * place, the caller should call xvip_clr() or xvip_set() directly instead of
  150. * using this function.
  151. */
  152. void xvip_clr_or_set(struct xvip_device *xvip, u32 addr, u32 mask, bool set)
  153. {
  154. u32 reg;
  155. reg = xvip_read(xvip, addr);
  156. reg = set ? reg | mask : reg & ~mask;
  157. xvip_write(xvip, addr, reg);
  158. }
  159. EXPORT_SYMBOL_GPL(xvip_clr_or_set);
  160. /**
  161. * xvip_clr_and_set - Clear and set the register with a bitmask
  162. * @xvip: Xilinx Video IP device
  163. * @addr: address of register
  164. * @clr: bitmask to be cleared
  165. * @set: bitmask to be set
  166. *
  167. * Clear a bit(s) of mask @clr in the register at address @addr, then set
  168. * a bit(s) of mask @set in the register after.
  169. */
  170. void xvip_clr_and_set(struct xvip_device *xvip, u32 addr, u32 clr, u32 set)
  171. {
  172. u32 reg;
  173. reg = xvip_read(xvip, addr);
  174. reg &= ~clr;
  175. reg |= set;
  176. xvip_write(xvip, addr, reg);
  177. }
  178. EXPORT_SYMBOL_GPL(xvip_clr_and_set);
  179. int xvip_init_resources(struct xvip_device *xvip)
  180. {
  181. struct platform_device *pdev = to_platform_device(xvip->dev);
  182. xvip->iomem = devm_platform_ioremap_resource(pdev, 0);
  183. if (IS_ERR(xvip->iomem))
  184. return PTR_ERR(xvip->iomem);
  185. xvip->clk = devm_clk_get(xvip->dev, NULL);
  186. if (IS_ERR(xvip->clk))
  187. return PTR_ERR(xvip->clk);
  188. clk_prepare_enable(xvip->clk);
  189. return 0;
  190. }
  191. EXPORT_SYMBOL_GPL(xvip_init_resources);
  192. void xvip_cleanup_resources(struct xvip_device *xvip)
  193. {
  194. clk_disable_unprepare(xvip->clk);
  195. }
  196. EXPORT_SYMBOL_GPL(xvip_cleanup_resources);
  197. /* -----------------------------------------------------------------------------
  198. * Subdev operations handlers
  199. */
  200. /**
  201. * xvip_enum_mbus_code - Enumerate the media format code
  202. * @subdev: V4L2 subdevice
  203. * @sd_state: V4L2 subdev state
  204. * @code: returning media bus code
  205. *
  206. * Enumerate the media bus code of the subdevice. Return the corresponding
  207. * pad format code. This function only works for subdevices with fixed format
  208. * on all pads. Subdevices with multiple format should have their own
  209. * function to enumerate mbus codes.
  210. *
  211. * Return: 0 if the media bus code is found, or -EINVAL if the format index
  212. * is not valid.
  213. */
  214. int xvip_enum_mbus_code(struct v4l2_subdev *subdev,
  215. struct v4l2_subdev_state *sd_state,
  216. struct v4l2_subdev_mbus_code_enum *code)
  217. {
  218. struct v4l2_mbus_framefmt *format;
  219. /* Enumerating frame sizes based on the active configuration isn't
  220. * supported yet.
  221. */
  222. if (code->which == V4L2_SUBDEV_FORMAT_ACTIVE)
  223. return -EINVAL;
  224. if (code->index)
  225. return -EINVAL;
  226. format = v4l2_subdev_get_try_format(subdev, sd_state, code->pad);
  227. code->code = format->code;
  228. return 0;
  229. }
  230. EXPORT_SYMBOL_GPL(xvip_enum_mbus_code);
  231. /**
  232. * xvip_enum_frame_size - Enumerate the media bus frame size
  233. * @subdev: V4L2 subdevice
  234. * @sd_state: V4L2 subdev state
  235. * @fse: returning media bus frame size
  236. *
  237. * This function is a drop-in implementation of the subdev enum_frame_size pad
  238. * operation. It assumes that the subdevice has one sink pad and one source
  239. * pad, and that the format on the source pad is always identical to the
  240. * format on the sink pad. Entities with different requirements need to
  241. * implement their own enum_frame_size handlers.
  242. *
  243. * Return: 0 if the media bus frame size is found, or -EINVAL
  244. * if the index or the code is not valid.
  245. */
  246. int xvip_enum_frame_size(struct v4l2_subdev *subdev,
  247. struct v4l2_subdev_state *sd_state,
  248. struct v4l2_subdev_frame_size_enum *fse)
  249. {
  250. struct v4l2_mbus_framefmt *format;
  251. /* Enumerating frame sizes based on the active configuration isn't
  252. * supported yet.
  253. */
  254. if (fse->which == V4L2_SUBDEV_FORMAT_ACTIVE)
  255. return -EINVAL;
  256. format = v4l2_subdev_get_try_format(subdev, sd_state, fse->pad);
  257. if (fse->index || fse->code != format->code)
  258. return -EINVAL;
  259. if (fse->pad == XVIP_PAD_SINK) {
  260. fse->min_width = XVIP_MIN_WIDTH;
  261. fse->max_width = XVIP_MAX_WIDTH;
  262. fse->min_height = XVIP_MIN_HEIGHT;
  263. fse->max_height = XVIP_MAX_HEIGHT;
  264. } else {
  265. /* The size on the source pad is fixed and always identical to
  266. * the size on the sink pad.
  267. */
  268. fse->min_width = format->width;
  269. fse->max_width = format->width;
  270. fse->min_height = format->height;
  271. fse->max_height = format->height;
  272. }
  273. return 0;
  274. }
  275. EXPORT_SYMBOL_GPL(xvip_enum_frame_size);