vsp1_uds.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * vsp1_uds.c -- R-Car VSP1 Up and Down Scaler
  3. *
  4. * Copyright (C) 2013-2014 Renesas Electronics 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. void vsp1_uds_set_alpha(struct vsp1_uds *uds, unsigned int alpha)
  39. {
  40. vsp1_uds_write(uds, VI6_UDS_ALPVAL, alpha << VI6_UDS_ALPVAL_VAL0_SHIFT);
  41. }
  42. /*
  43. * uds_output_size - Return the output size for an input size and scaling ratio
  44. * @input: input size in pixels
  45. * @ratio: scaling ratio in U4.12 fixed-point format
  46. */
  47. static unsigned int uds_output_size(unsigned int input, unsigned int ratio)
  48. {
  49. if (ratio > 4096) {
  50. /* Down-scaling */
  51. unsigned int mp;
  52. mp = ratio / 4096;
  53. mp = mp < 4 ? 1 : (mp < 8 ? 2 : 4);
  54. return (input - 1) / mp * mp * 4096 / ratio + 1;
  55. } else {
  56. /* Up-scaling */
  57. return (input - 1) * 4096 / ratio + 1;
  58. }
  59. }
  60. /*
  61. * uds_output_limits - Return the min and max output sizes for an input size
  62. * @input: input size in pixels
  63. * @minimum: minimum output size (returned)
  64. * @maximum: maximum output size (returned)
  65. */
  66. static void uds_output_limits(unsigned int input,
  67. unsigned int *minimum, unsigned int *maximum)
  68. {
  69. *minimum = max(uds_output_size(input, UDS_MAX_FACTOR), UDS_MIN_SIZE);
  70. *maximum = min(uds_output_size(input, UDS_MIN_FACTOR), UDS_MAX_SIZE);
  71. }
  72. /*
  73. * uds_passband_width - Return the passband filter width for a scaling ratio
  74. * @ratio: scaling ratio in U4.12 fixed-point format
  75. */
  76. static unsigned int uds_passband_width(unsigned int ratio)
  77. {
  78. if (ratio >= 4096) {
  79. /* Down-scaling */
  80. unsigned int mp;
  81. mp = ratio / 4096;
  82. mp = mp < 4 ? 1 : (mp < 8 ? 2 : 4);
  83. return 64 * 4096 * mp / ratio;
  84. } else {
  85. /* Up-scaling */
  86. return 64;
  87. }
  88. }
  89. static unsigned int uds_compute_ratio(unsigned int input, unsigned int output)
  90. {
  91. /* TODO: This is an approximation that will need to be refined. */
  92. return (input - 1) * 4096 / (output - 1);
  93. }
  94. /* -----------------------------------------------------------------------------
  95. * V4L2 Subdevice Core Operations
  96. */
  97. static int uds_s_stream(struct v4l2_subdev *subdev, int enable)
  98. {
  99. struct vsp1_uds *uds = to_uds(subdev);
  100. const struct v4l2_mbus_framefmt *output;
  101. const struct v4l2_mbus_framefmt *input;
  102. unsigned int hscale;
  103. unsigned int vscale;
  104. bool multitap;
  105. if (!enable)
  106. return 0;
  107. input = &uds->entity.formats[UDS_PAD_SINK];
  108. output = &uds->entity.formats[UDS_PAD_SOURCE];
  109. hscale = uds_compute_ratio(input->width, output->width);
  110. vscale = uds_compute_ratio(input->height, output->height);
  111. dev_dbg(uds->entity.vsp1->dev, "hscale %u vscale %u\n", hscale, vscale);
  112. /* Multi-tap scaling can't be enabled along with alpha scaling when
  113. * scaling down with a factor lower than or equal to 1/2 in either
  114. * direction.
  115. */
  116. if (uds->scale_alpha && (hscale >= 8192 || vscale >= 8192))
  117. multitap = false;
  118. else
  119. multitap = true;
  120. vsp1_uds_write(uds, VI6_UDS_CTRL,
  121. (uds->scale_alpha ? VI6_UDS_CTRL_AON : 0) |
  122. (multitap ? VI6_UDS_CTRL_BC : 0));
  123. vsp1_uds_write(uds, VI6_UDS_PASS_BWIDTH,
  124. (uds_passband_width(hscale)
  125. << VI6_UDS_PASS_BWIDTH_H_SHIFT) |
  126. (uds_passband_width(vscale)
  127. << VI6_UDS_PASS_BWIDTH_V_SHIFT));
  128. /* Set the scaling ratios and the output size. */
  129. vsp1_uds_write(uds, VI6_UDS_SCALE,
  130. (hscale << VI6_UDS_SCALE_HFRAC_SHIFT) |
  131. (vscale << VI6_UDS_SCALE_VFRAC_SHIFT));
  132. vsp1_uds_write(uds, VI6_UDS_CLIP_SIZE,
  133. (output->width << VI6_UDS_CLIP_SIZE_HSIZE_SHIFT) |
  134. (output->height << VI6_UDS_CLIP_SIZE_VSIZE_SHIFT));
  135. return 0;
  136. }
  137. /* -----------------------------------------------------------------------------
  138. * V4L2 Subdevice Pad Operations
  139. */
  140. static int uds_enum_mbus_code(struct v4l2_subdev *subdev,
  141. struct v4l2_subdev_pad_config *cfg,
  142. struct v4l2_subdev_mbus_code_enum *code)
  143. {
  144. static const unsigned int codes[] = {
  145. MEDIA_BUS_FMT_ARGB8888_1X32,
  146. MEDIA_BUS_FMT_AYUV8_1X32,
  147. };
  148. struct vsp1_uds *uds = to_uds(subdev);
  149. if (code->pad == UDS_PAD_SINK) {
  150. if (code->index >= ARRAY_SIZE(codes))
  151. return -EINVAL;
  152. code->code = codes[code->index];
  153. } else {
  154. struct v4l2_mbus_framefmt *format;
  155. /* The UDS can't perform format conversion, the sink format is
  156. * always identical to the source format.
  157. */
  158. if (code->index)
  159. return -EINVAL;
  160. format = vsp1_entity_get_pad_format(&uds->entity, cfg,
  161. UDS_PAD_SINK, code->which);
  162. code->code = format->code;
  163. }
  164. return 0;
  165. }
  166. static int uds_enum_frame_size(struct v4l2_subdev *subdev,
  167. struct v4l2_subdev_pad_config *cfg,
  168. struct v4l2_subdev_frame_size_enum *fse)
  169. {
  170. struct vsp1_uds *uds = to_uds(subdev);
  171. struct v4l2_mbus_framefmt *format;
  172. format = vsp1_entity_get_pad_format(&uds->entity, cfg,
  173. UDS_PAD_SINK, fse->which);
  174. if (fse->index || fse->code != format->code)
  175. return -EINVAL;
  176. if (fse->pad == UDS_PAD_SINK) {
  177. fse->min_width = UDS_MIN_SIZE;
  178. fse->max_width = UDS_MAX_SIZE;
  179. fse->min_height = UDS_MIN_SIZE;
  180. fse->max_height = UDS_MAX_SIZE;
  181. } else {
  182. uds_output_limits(format->width, &fse->min_width,
  183. &fse->max_width);
  184. uds_output_limits(format->height, &fse->min_height,
  185. &fse->max_height);
  186. }
  187. return 0;
  188. }
  189. static int uds_get_format(struct v4l2_subdev *subdev, struct v4l2_subdev_pad_config *cfg,
  190. struct v4l2_subdev_format *fmt)
  191. {
  192. struct vsp1_uds *uds = to_uds(subdev);
  193. fmt->format = *vsp1_entity_get_pad_format(&uds->entity, cfg, fmt->pad,
  194. fmt->which);
  195. return 0;
  196. }
  197. static void uds_try_format(struct vsp1_uds *uds, struct v4l2_subdev_pad_config *cfg,
  198. unsigned int pad, struct v4l2_mbus_framefmt *fmt,
  199. enum v4l2_subdev_format_whence which)
  200. {
  201. struct v4l2_mbus_framefmt *format;
  202. unsigned int minimum;
  203. unsigned int maximum;
  204. switch (pad) {
  205. case UDS_PAD_SINK:
  206. /* Default to YUV if the requested format is not supported. */
  207. if (fmt->code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
  208. fmt->code != MEDIA_BUS_FMT_AYUV8_1X32)
  209. fmt->code = MEDIA_BUS_FMT_AYUV8_1X32;
  210. fmt->width = clamp(fmt->width, UDS_MIN_SIZE, UDS_MAX_SIZE);
  211. fmt->height = clamp(fmt->height, UDS_MIN_SIZE, UDS_MAX_SIZE);
  212. break;
  213. case UDS_PAD_SOURCE:
  214. /* The UDS scales but can't perform format conversion. */
  215. format = vsp1_entity_get_pad_format(&uds->entity, cfg,
  216. UDS_PAD_SINK, which);
  217. fmt->code = format->code;
  218. uds_output_limits(format->width, &minimum, &maximum);
  219. fmt->width = clamp(fmt->width, minimum, maximum);
  220. uds_output_limits(format->height, &minimum, &maximum);
  221. fmt->height = clamp(fmt->height, minimum, maximum);
  222. break;
  223. }
  224. fmt->field = V4L2_FIELD_NONE;
  225. fmt->colorspace = V4L2_COLORSPACE_SRGB;
  226. }
  227. static int uds_set_format(struct v4l2_subdev *subdev, struct v4l2_subdev_pad_config *cfg,
  228. struct v4l2_subdev_format *fmt)
  229. {
  230. struct vsp1_uds *uds = to_uds(subdev);
  231. struct v4l2_mbus_framefmt *format;
  232. uds_try_format(uds, cfg, fmt->pad, &fmt->format, fmt->which);
  233. format = vsp1_entity_get_pad_format(&uds->entity, cfg, fmt->pad,
  234. fmt->which);
  235. *format = fmt->format;
  236. if (fmt->pad == UDS_PAD_SINK) {
  237. /* Propagate the format to the source pad. */
  238. format = vsp1_entity_get_pad_format(&uds->entity, cfg,
  239. UDS_PAD_SOURCE, fmt->which);
  240. *format = fmt->format;
  241. uds_try_format(uds, cfg, UDS_PAD_SOURCE, format, fmt->which);
  242. }
  243. return 0;
  244. }
  245. /* -----------------------------------------------------------------------------
  246. * V4L2 Subdevice Operations
  247. */
  248. static struct v4l2_subdev_video_ops uds_video_ops = {
  249. .s_stream = uds_s_stream,
  250. };
  251. static struct v4l2_subdev_pad_ops uds_pad_ops = {
  252. .enum_mbus_code = uds_enum_mbus_code,
  253. .enum_frame_size = uds_enum_frame_size,
  254. .get_fmt = uds_get_format,
  255. .set_fmt = uds_set_format,
  256. };
  257. static struct v4l2_subdev_ops uds_ops = {
  258. .video = &uds_video_ops,
  259. .pad = &uds_pad_ops,
  260. };
  261. /* -----------------------------------------------------------------------------
  262. * Initialization and Cleanup
  263. */
  264. struct vsp1_uds *vsp1_uds_create(struct vsp1_device *vsp1, unsigned int index)
  265. {
  266. struct v4l2_subdev *subdev;
  267. struct vsp1_uds *uds;
  268. int ret;
  269. uds = devm_kzalloc(vsp1->dev, sizeof(*uds), GFP_KERNEL);
  270. if (uds == NULL)
  271. return ERR_PTR(-ENOMEM);
  272. uds->entity.type = VSP1_ENTITY_UDS;
  273. uds->entity.index = index;
  274. ret = vsp1_entity_init(vsp1, &uds->entity, 2);
  275. if (ret < 0)
  276. return ERR_PTR(ret);
  277. /* Initialize the V4L2 subdev. */
  278. subdev = &uds->entity.subdev;
  279. v4l2_subdev_init(subdev, &uds_ops);
  280. subdev->entity.ops = &vsp1_media_ops;
  281. subdev->internal_ops = &vsp1_subdev_internal_ops;
  282. snprintf(subdev->name, sizeof(subdev->name), "%s uds.%u",
  283. dev_name(vsp1->dev), index);
  284. v4l2_set_subdevdata(subdev, uds);
  285. subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  286. vsp1_entity_init_formats(subdev, NULL);
  287. return uds;
  288. }