xilinx-vip.c 9.6 KB

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