vsp1_uds.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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_dl.h"
  18. #include "vsp1_pipe.h"
  19. #include "vsp1_uds.h"
  20. #define UDS_MIN_SIZE 4U
  21. #define UDS_MAX_SIZE 8190U
  22. #define UDS_MIN_FACTOR 0x0100
  23. #define UDS_MAX_FACTOR 0xffff
  24. /* -----------------------------------------------------------------------------
  25. * Device Access
  26. */
  27. static inline void vsp1_uds_write(struct vsp1_uds *uds, struct vsp1_dl_list *dl,
  28. u32 reg, u32 data)
  29. {
  30. vsp1_dl_list_write(dl, reg + uds->entity.index * VI6_UDS_OFFSET, data);
  31. }
  32. /* -----------------------------------------------------------------------------
  33. * Scaling Computation
  34. */
  35. void vsp1_uds_set_alpha(struct vsp1_entity *entity, struct vsp1_dl_list *dl,
  36. unsigned int alpha)
  37. {
  38. struct vsp1_uds *uds = to_uds(&entity->subdev);
  39. vsp1_uds_write(uds, dl, VI6_UDS_ALPVAL,
  40. 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 Pad Operations
  96. */
  97. static int uds_enum_mbus_code(struct v4l2_subdev *subdev,
  98. struct v4l2_subdev_pad_config *cfg,
  99. struct v4l2_subdev_mbus_code_enum *code)
  100. {
  101. static const unsigned int codes[] = {
  102. MEDIA_BUS_FMT_ARGB8888_1X32,
  103. MEDIA_BUS_FMT_AYUV8_1X32,
  104. };
  105. return vsp1_subdev_enum_mbus_code(subdev, cfg, code, codes,
  106. ARRAY_SIZE(codes));
  107. }
  108. static int uds_enum_frame_size(struct v4l2_subdev *subdev,
  109. struct v4l2_subdev_pad_config *cfg,
  110. struct v4l2_subdev_frame_size_enum *fse)
  111. {
  112. struct vsp1_uds *uds = to_uds(subdev);
  113. struct v4l2_subdev_pad_config *config;
  114. struct v4l2_mbus_framefmt *format;
  115. int ret = 0;
  116. config = vsp1_entity_get_pad_config(&uds->entity, cfg, fse->which);
  117. if (!config)
  118. return -EINVAL;
  119. format = vsp1_entity_get_pad_format(&uds->entity, config,
  120. UDS_PAD_SINK);
  121. mutex_lock(&uds->entity.lock);
  122. if (fse->index || fse->code != format->code) {
  123. ret = -EINVAL;
  124. goto done;
  125. }
  126. if (fse->pad == UDS_PAD_SINK) {
  127. fse->min_width = UDS_MIN_SIZE;
  128. fse->max_width = UDS_MAX_SIZE;
  129. fse->min_height = UDS_MIN_SIZE;
  130. fse->max_height = UDS_MAX_SIZE;
  131. } else {
  132. uds_output_limits(format->width, &fse->min_width,
  133. &fse->max_width);
  134. uds_output_limits(format->height, &fse->min_height,
  135. &fse->max_height);
  136. }
  137. done:
  138. mutex_unlock(&uds->entity.lock);
  139. return ret;
  140. }
  141. static void uds_try_format(struct vsp1_uds *uds,
  142. struct v4l2_subdev_pad_config *config,
  143. unsigned int pad, struct v4l2_mbus_framefmt *fmt)
  144. {
  145. struct v4l2_mbus_framefmt *format;
  146. unsigned int minimum;
  147. unsigned int maximum;
  148. switch (pad) {
  149. case UDS_PAD_SINK:
  150. /* Default to YUV if the requested format is not supported. */
  151. if (fmt->code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
  152. fmt->code != MEDIA_BUS_FMT_AYUV8_1X32)
  153. fmt->code = MEDIA_BUS_FMT_AYUV8_1X32;
  154. fmt->width = clamp(fmt->width, UDS_MIN_SIZE, UDS_MAX_SIZE);
  155. fmt->height = clamp(fmt->height, UDS_MIN_SIZE, UDS_MAX_SIZE);
  156. break;
  157. case UDS_PAD_SOURCE:
  158. /* The UDS scales but can't perform format conversion. */
  159. format = vsp1_entity_get_pad_format(&uds->entity, config,
  160. UDS_PAD_SINK);
  161. fmt->code = format->code;
  162. uds_output_limits(format->width, &minimum, &maximum);
  163. fmt->width = clamp(fmt->width, minimum, maximum);
  164. uds_output_limits(format->height, &minimum, &maximum);
  165. fmt->height = clamp(fmt->height, minimum, maximum);
  166. break;
  167. }
  168. fmt->field = V4L2_FIELD_NONE;
  169. fmt->colorspace = V4L2_COLORSPACE_SRGB;
  170. }
  171. static int uds_set_format(struct v4l2_subdev *subdev,
  172. struct v4l2_subdev_pad_config *cfg,
  173. struct v4l2_subdev_format *fmt)
  174. {
  175. struct vsp1_uds *uds = to_uds(subdev);
  176. struct v4l2_subdev_pad_config *config;
  177. struct v4l2_mbus_framefmt *format;
  178. int ret = 0;
  179. mutex_lock(&uds->entity.lock);
  180. config = vsp1_entity_get_pad_config(&uds->entity, cfg, fmt->which);
  181. if (!config) {
  182. ret = -EINVAL;
  183. goto done;
  184. }
  185. uds_try_format(uds, config, fmt->pad, &fmt->format);
  186. format = vsp1_entity_get_pad_format(&uds->entity, config, fmt->pad);
  187. *format = fmt->format;
  188. if (fmt->pad == UDS_PAD_SINK) {
  189. /* Propagate the format to the source pad. */
  190. format = vsp1_entity_get_pad_format(&uds->entity, config,
  191. UDS_PAD_SOURCE);
  192. *format = fmt->format;
  193. uds_try_format(uds, config, UDS_PAD_SOURCE, format);
  194. }
  195. done:
  196. mutex_unlock(&uds->entity.lock);
  197. return ret;
  198. }
  199. /* -----------------------------------------------------------------------------
  200. * V4L2 Subdevice Operations
  201. */
  202. static const struct v4l2_subdev_pad_ops uds_pad_ops = {
  203. .init_cfg = vsp1_entity_init_cfg,
  204. .enum_mbus_code = uds_enum_mbus_code,
  205. .enum_frame_size = uds_enum_frame_size,
  206. .get_fmt = vsp1_subdev_get_pad_format,
  207. .set_fmt = uds_set_format,
  208. };
  209. static const struct v4l2_subdev_ops uds_ops = {
  210. .pad = &uds_pad_ops,
  211. };
  212. /* -----------------------------------------------------------------------------
  213. * VSP1 Entity Operations
  214. */
  215. static void uds_configure(struct vsp1_entity *entity,
  216. struct vsp1_pipeline *pipe,
  217. struct vsp1_dl_list *dl,
  218. enum vsp1_entity_params params)
  219. {
  220. struct vsp1_uds *uds = to_uds(&entity->subdev);
  221. const struct v4l2_mbus_framefmt *output;
  222. const struct v4l2_mbus_framefmt *input;
  223. unsigned int hscale;
  224. unsigned int vscale;
  225. bool multitap;
  226. if (params == VSP1_ENTITY_PARAMS_PARTITION) {
  227. const struct v4l2_rect *clip = &pipe->partition;
  228. vsp1_uds_write(uds, dl, VI6_UDS_CLIP_SIZE,
  229. (clip->width << VI6_UDS_CLIP_SIZE_HSIZE_SHIFT) |
  230. (clip->height << VI6_UDS_CLIP_SIZE_VSIZE_SHIFT));
  231. return;
  232. }
  233. if (params != VSP1_ENTITY_PARAMS_INIT)
  234. return;
  235. input = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
  236. UDS_PAD_SINK);
  237. output = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
  238. UDS_PAD_SOURCE);
  239. hscale = uds_compute_ratio(input->width, output->width);
  240. vscale = uds_compute_ratio(input->height, output->height);
  241. dev_dbg(uds->entity.vsp1->dev, "hscale %u vscale %u\n", hscale, vscale);
  242. /* Multi-tap scaling can't be enabled along with alpha scaling when
  243. * scaling down with a factor lower than or equal to 1/2 in either
  244. * direction.
  245. */
  246. if (uds->scale_alpha && (hscale >= 8192 || vscale >= 8192))
  247. multitap = false;
  248. else
  249. multitap = true;
  250. vsp1_uds_write(uds, dl, VI6_UDS_CTRL,
  251. (uds->scale_alpha ? VI6_UDS_CTRL_AON : 0) |
  252. (multitap ? VI6_UDS_CTRL_BC : 0));
  253. vsp1_uds_write(uds, dl, VI6_UDS_PASS_BWIDTH,
  254. (uds_passband_width(hscale)
  255. << VI6_UDS_PASS_BWIDTH_H_SHIFT) |
  256. (uds_passband_width(vscale)
  257. << VI6_UDS_PASS_BWIDTH_V_SHIFT));
  258. /* Set the scaling ratios. */
  259. vsp1_uds_write(uds, dl, VI6_UDS_SCALE,
  260. (hscale << VI6_UDS_SCALE_HFRAC_SHIFT) |
  261. (vscale << VI6_UDS_SCALE_VFRAC_SHIFT));
  262. }
  263. static unsigned int uds_max_width(struct vsp1_entity *entity,
  264. struct vsp1_pipeline *pipe)
  265. {
  266. struct vsp1_uds *uds = to_uds(&entity->subdev);
  267. const struct v4l2_mbus_framefmt *output;
  268. const struct v4l2_mbus_framefmt *input;
  269. unsigned int hscale;
  270. input = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
  271. UDS_PAD_SINK);
  272. output = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
  273. UDS_PAD_SOURCE);
  274. hscale = output->width / input->width;
  275. if (hscale <= 2)
  276. return 256;
  277. else if (hscale <= 4)
  278. return 512;
  279. else if (hscale <= 8)
  280. return 1024;
  281. else
  282. return 2048;
  283. }
  284. static const struct vsp1_entity_operations uds_entity_ops = {
  285. .configure = uds_configure,
  286. .max_width = uds_max_width,
  287. };
  288. /* -----------------------------------------------------------------------------
  289. * Initialization and Cleanup
  290. */
  291. struct vsp1_uds *vsp1_uds_create(struct vsp1_device *vsp1, unsigned int index)
  292. {
  293. struct vsp1_uds *uds;
  294. char name[6];
  295. int ret;
  296. uds = devm_kzalloc(vsp1->dev, sizeof(*uds), GFP_KERNEL);
  297. if (uds == NULL)
  298. return ERR_PTR(-ENOMEM);
  299. uds->entity.ops = &uds_entity_ops;
  300. uds->entity.type = VSP1_ENTITY_UDS;
  301. uds->entity.index = index;
  302. sprintf(name, "uds.%u", index);
  303. ret = vsp1_entity_init(vsp1, &uds->entity, name, 2, &uds_ops,
  304. MEDIA_ENT_F_PROC_VIDEO_SCALER);
  305. if (ret < 0)
  306. return ERR_PTR(ret);
  307. return uds;
  308. }