vsp1_uds.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * vsp1_uds.c -- R-Car VSP1 Up and Down Scaler
  3. *
  4. * Copyright (C) 2013 Renesas Corporation
  5. *
  6. * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/device.h>
  14. #include <linux/gfp.h>
  15. #include <media/v4l2-subdev.h>
  16. #include "vsp1.h"
  17. #include "vsp1_uds.h"
  18. #define UDS_MIN_SIZE 4U
  19. #define UDS_MAX_SIZE 8190U
  20. #define UDS_MIN_FACTOR 0x0100
  21. #define UDS_MAX_FACTOR 0xffff
  22. /* -----------------------------------------------------------------------------
  23. * Device Access
  24. */
  25. static inline u32 vsp1_uds_read(struct vsp1_uds *uds, u32 reg)
  26. {
  27. return vsp1_read(uds->entity.vsp1,
  28. reg + uds->entity.index * VI6_UDS_OFFSET);
  29. }
  30. static inline void vsp1_uds_write(struct vsp1_uds *uds, u32 reg, u32 data)
  31. {
  32. vsp1_write(uds->entity.vsp1,
  33. reg + uds->entity.index * VI6_UDS_OFFSET, data);
  34. }
  35. /* -----------------------------------------------------------------------------
  36. * Scaling Computation
  37. */
  38. /*
  39. * uds_output_size - Return the output size for an input size and scaling ratio
  40. * @input: input size in pixels
  41. * @ratio: scaling ratio in U4.12 fixed-point format
  42. */
  43. static unsigned int uds_output_size(unsigned int input, unsigned int ratio)
  44. {
  45. if (ratio > 4096) {
  46. /* Down-scaling */
  47. unsigned int mp;
  48. mp = ratio / 4096;
  49. mp = mp < 4 ? 1 : (mp < 8 ? 2 : 4);
  50. return (input - 1) / mp * mp * 4096 / ratio + 1;
  51. } else {
  52. /* Up-scaling */
  53. return (input - 1) * 4096 / ratio + 1;
  54. }
  55. }
  56. /*
  57. * uds_output_limits - Return the min and max output sizes for an input size
  58. * @input: input size in pixels
  59. * @minimum: minimum output size (returned)
  60. * @maximum: maximum output size (returned)
  61. */
  62. static void uds_output_limits(unsigned int input,
  63. unsigned int *minimum, unsigned int *maximum)
  64. {
  65. *minimum = max(uds_output_size(input, UDS_MAX_FACTOR), UDS_MIN_SIZE);
  66. *maximum = min(uds_output_size(input, UDS_MIN_FACTOR), UDS_MAX_SIZE);
  67. }
  68. /*
  69. * uds_passband_width - Return the passband filter width for a scaling ratio
  70. * @ratio: scaling ratio in U4.12 fixed-point format
  71. */
  72. static unsigned int uds_passband_width(unsigned int ratio)
  73. {
  74. if (ratio >= 4096) {
  75. /* Down-scaling */
  76. unsigned int mp;
  77. mp = ratio / 4096;
  78. mp = mp < 4 ? 1 : (mp < 8 ? 2 : 4);
  79. return 64 * 4096 * mp / ratio;
  80. } else {
  81. /* Up-scaling */
  82. return 64;
  83. }
  84. }
  85. static unsigned int uds_compute_ratio(unsigned int input, unsigned int output)
  86. {
  87. /* TODO: This is an approximation that will need to be refined. */
  88. return (input - 1) * 4096 / (output - 1);
  89. }
  90. static void uds_compute_ratios(struct vsp1_uds *uds)
  91. {
  92. struct v4l2_mbus_framefmt *input = &uds->entity.formats[UDS_PAD_SINK];
  93. struct v4l2_mbus_framefmt *output =
  94. &uds->entity.formats[UDS_PAD_SOURCE];
  95. uds->hscale = uds_compute_ratio(input->width, output->width);
  96. uds->vscale = uds_compute_ratio(input->height, output->height);
  97. dev_dbg(uds->entity.vsp1->dev, "hscale %u vscale %u\n",
  98. uds->hscale, uds->vscale);
  99. }
  100. /* -----------------------------------------------------------------------------
  101. * V4L2 Subdevice Core Operations
  102. */
  103. static int uds_s_stream(struct v4l2_subdev *subdev, int enable)
  104. {
  105. const struct v4l2_mbus_framefmt *format;
  106. struct vsp1_uds *uds = to_uds(subdev);
  107. if (!enable)
  108. return 0;
  109. /* Enable multi-tap scaling. */
  110. vsp1_uds_write(uds, VI6_UDS_CTRL, VI6_UDS_CTRL_BC);
  111. vsp1_uds_write(uds, VI6_UDS_PASS_BWIDTH,
  112. (uds_passband_width(uds->hscale)
  113. << VI6_UDS_PASS_BWIDTH_H_SHIFT) |
  114. (uds_passband_width(uds->vscale)
  115. << VI6_UDS_PASS_BWIDTH_V_SHIFT));
  116. /* Set the scaling ratios and the output size. */
  117. format = &uds->entity.formats[UDS_PAD_SOURCE];
  118. vsp1_uds_write(uds, VI6_UDS_SCALE,
  119. (uds->hscale << VI6_UDS_SCALE_HFRAC_SHIFT) |
  120. (uds->vscale << VI6_UDS_SCALE_VFRAC_SHIFT));
  121. vsp1_uds_write(uds, VI6_UDS_CLIP_SIZE,
  122. (format->width << VI6_UDS_CLIP_SIZE_HSIZE_SHIFT) |
  123. (format->height << VI6_UDS_CLIP_SIZE_VSIZE_SHIFT));
  124. return 0;
  125. }
  126. /* -----------------------------------------------------------------------------
  127. * V4L2 Subdevice Pad Operations
  128. */
  129. static int uds_enum_mbus_code(struct v4l2_subdev *subdev,
  130. struct v4l2_subdev_fh *fh,
  131. struct v4l2_subdev_mbus_code_enum *code)
  132. {
  133. static const unsigned int codes[] = {
  134. V4L2_MBUS_FMT_ARGB8888_1X32,
  135. V4L2_MBUS_FMT_AYUV8_1X32,
  136. };
  137. if (code->pad == UDS_PAD_SINK) {
  138. if (code->index >= ARRAY_SIZE(codes))
  139. return -EINVAL;
  140. code->code = codes[code->index];
  141. } else {
  142. struct v4l2_mbus_framefmt *format;
  143. /* The UDS can't perform format conversion, the sink format is
  144. * always identical to the source format.
  145. */
  146. if (code->index)
  147. return -EINVAL;
  148. format = v4l2_subdev_get_try_format(fh, UDS_PAD_SINK);
  149. code->code = format->code;
  150. }
  151. return 0;
  152. }
  153. static int uds_enum_frame_size(struct v4l2_subdev *subdev,
  154. struct v4l2_subdev_fh *fh,
  155. struct v4l2_subdev_frame_size_enum *fse)
  156. {
  157. struct v4l2_mbus_framefmt *format;
  158. format = v4l2_subdev_get_try_format(fh, UDS_PAD_SINK);
  159. if (fse->index || fse->code != format->code)
  160. return -EINVAL;
  161. if (fse->pad == UDS_PAD_SINK) {
  162. fse->min_width = UDS_MIN_SIZE;
  163. fse->max_width = UDS_MAX_SIZE;
  164. fse->min_height = UDS_MIN_SIZE;
  165. fse->max_height = UDS_MAX_SIZE;
  166. } else {
  167. uds_output_limits(format->width, &fse->min_width,
  168. &fse->max_width);
  169. uds_output_limits(format->height, &fse->min_height,
  170. &fse->max_height);
  171. }
  172. return 0;
  173. }
  174. static int uds_get_format(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh,
  175. struct v4l2_subdev_format *fmt)
  176. {
  177. struct vsp1_uds *uds = to_uds(subdev);
  178. fmt->format = *vsp1_entity_get_pad_format(&uds->entity, fh, fmt->pad,
  179. fmt->which);
  180. return 0;
  181. }
  182. static void uds_try_format(struct vsp1_uds *uds, struct v4l2_subdev_fh *fh,
  183. unsigned int pad, struct v4l2_mbus_framefmt *fmt,
  184. enum v4l2_subdev_format_whence which)
  185. {
  186. struct v4l2_mbus_framefmt *format;
  187. unsigned int minimum;
  188. unsigned int maximum;
  189. switch (pad) {
  190. case UDS_PAD_SINK:
  191. /* Default to YUV if the requested format is not supported. */
  192. if (fmt->code != V4L2_MBUS_FMT_ARGB8888_1X32 &&
  193. fmt->code != V4L2_MBUS_FMT_AYUV8_1X32)
  194. fmt->code = V4L2_MBUS_FMT_AYUV8_1X32;
  195. fmt->width = clamp(fmt->width, UDS_MIN_SIZE, UDS_MAX_SIZE);
  196. fmt->height = clamp(fmt->height, UDS_MIN_SIZE, UDS_MAX_SIZE);
  197. break;
  198. case UDS_PAD_SOURCE:
  199. /* The UDS scales but can't perform format conversion. */
  200. format = vsp1_entity_get_pad_format(&uds->entity, fh,
  201. UDS_PAD_SINK, which);
  202. fmt->code = format->code;
  203. uds_output_limits(format->width, &minimum, &maximum);
  204. fmt->width = clamp(fmt->width, minimum, maximum);
  205. uds_output_limits(format->height, &minimum, &maximum);
  206. fmt->height = clamp(fmt->height, minimum, maximum);
  207. break;
  208. }
  209. fmt->field = V4L2_FIELD_NONE;
  210. fmt->colorspace = V4L2_COLORSPACE_SRGB;
  211. }
  212. static int uds_set_format(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh,
  213. struct v4l2_subdev_format *fmt)
  214. {
  215. struct vsp1_uds *uds = to_uds(subdev);
  216. struct v4l2_mbus_framefmt *format;
  217. uds_try_format(uds, fh, fmt->pad, &fmt->format, fmt->which);
  218. format = vsp1_entity_get_pad_format(&uds->entity, fh, fmt->pad,
  219. fmt->which);
  220. *format = fmt->format;
  221. if (fmt->pad == UDS_PAD_SINK) {
  222. /* Propagate the format to the source pad. */
  223. format = vsp1_entity_get_pad_format(&uds->entity, fh,
  224. UDS_PAD_SOURCE, fmt->which);
  225. *format = fmt->format;
  226. uds_try_format(uds, fh, UDS_PAD_SOURCE, format, fmt->which);
  227. }
  228. if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE)
  229. uds_compute_ratios(uds);
  230. return 0;
  231. }
  232. /* -----------------------------------------------------------------------------
  233. * V4L2 Subdevice Operations
  234. */
  235. static struct v4l2_subdev_video_ops uds_video_ops = {
  236. .s_stream = uds_s_stream,
  237. };
  238. static struct v4l2_subdev_pad_ops uds_pad_ops = {
  239. .enum_mbus_code = uds_enum_mbus_code,
  240. .enum_frame_size = uds_enum_frame_size,
  241. .get_fmt = uds_get_format,
  242. .set_fmt = uds_set_format,
  243. };
  244. static struct v4l2_subdev_ops uds_ops = {
  245. .video = &uds_video_ops,
  246. .pad = &uds_pad_ops,
  247. };
  248. /* -----------------------------------------------------------------------------
  249. * Initialization and Cleanup
  250. */
  251. struct vsp1_uds *vsp1_uds_create(struct vsp1_device *vsp1, unsigned int index)
  252. {
  253. struct v4l2_subdev *subdev;
  254. struct vsp1_uds *uds;
  255. int ret;
  256. uds = devm_kzalloc(vsp1->dev, sizeof(*uds), GFP_KERNEL);
  257. if (uds == NULL)
  258. return ERR_PTR(-ENOMEM);
  259. uds->entity.type = VSP1_ENTITY_UDS;
  260. uds->entity.index = index;
  261. uds->entity.id = VI6_DPR_NODE_UDS(index);
  262. ret = vsp1_entity_init(vsp1, &uds->entity, 2);
  263. if (ret < 0)
  264. return ERR_PTR(ret);
  265. /* Initialize the V4L2 subdev. */
  266. subdev = &uds->entity.subdev;
  267. v4l2_subdev_init(subdev, &uds_ops);
  268. subdev->entity.ops = &vsp1_media_ops;
  269. subdev->internal_ops = &vsp1_subdev_internal_ops;
  270. snprintf(subdev->name, sizeof(subdev->name), "%s uds.%u",
  271. dev_name(vsp1->dev), index);
  272. v4l2_set_subdevdata(subdev, uds);
  273. subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  274. vsp1_entity_init_formats(subdev, NULL);
  275. return uds;
  276. }