vsp1_uds.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * vsp1_uds.c -- R-Car VSP1 Up and Down Scaler
  4. *
  5. * Copyright (C) 2013-2014 Renesas Electronics Corporation
  6. *
  7. * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  8. */
  9. #include <linux/device.h>
  10. #include <linux/gfp.h>
  11. #include <media/v4l2-subdev.h>
  12. #include "vsp1.h"
  13. #include "vsp1_dl.h"
  14. #include "vsp1_pipe.h"
  15. #include "vsp1_uds.h"
  16. #define UDS_MIN_SIZE 4U
  17. #define UDS_MAX_SIZE 8190U
  18. #define UDS_MIN_FACTOR 0x0100
  19. #define UDS_MAX_FACTOR 0xffff
  20. /* -----------------------------------------------------------------------------
  21. * Device Access
  22. */
  23. static inline void vsp1_uds_write(struct vsp1_uds *uds,
  24. struct vsp1_dl_body *dlb, u32 reg, u32 data)
  25. {
  26. vsp1_dl_body_write(dlb, reg + uds->entity.index * VI6_UDS_OFFSET, data);
  27. }
  28. /* -----------------------------------------------------------------------------
  29. * Scaling Computation
  30. */
  31. void vsp1_uds_set_alpha(struct vsp1_entity *entity, struct vsp1_dl_body *dlb,
  32. unsigned int alpha)
  33. {
  34. struct vsp1_uds *uds = to_uds(&entity->subdev);
  35. vsp1_uds_write(uds, dlb, VI6_UDS_ALPVAL,
  36. alpha << VI6_UDS_ALPVAL_VAL0_SHIFT);
  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. /* -----------------------------------------------------------------------------
  91. * V4L2 Subdevice Pad Operations
  92. */
  93. static int uds_enum_mbus_code(struct v4l2_subdev *subdev,
  94. struct v4l2_subdev_pad_config *cfg,
  95. struct v4l2_subdev_mbus_code_enum *code)
  96. {
  97. static const unsigned int codes[] = {
  98. MEDIA_BUS_FMT_ARGB8888_1X32,
  99. MEDIA_BUS_FMT_AYUV8_1X32,
  100. };
  101. return vsp1_subdev_enum_mbus_code(subdev, cfg, code, codes,
  102. ARRAY_SIZE(codes));
  103. }
  104. static int uds_enum_frame_size(struct v4l2_subdev *subdev,
  105. struct v4l2_subdev_pad_config *cfg,
  106. struct v4l2_subdev_frame_size_enum *fse)
  107. {
  108. struct vsp1_uds *uds = to_uds(subdev);
  109. struct v4l2_subdev_pad_config *config;
  110. struct v4l2_mbus_framefmt *format;
  111. int ret = 0;
  112. config = vsp1_entity_get_pad_config(&uds->entity, cfg, fse->which);
  113. if (!config)
  114. return -EINVAL;
  115. format = vsp1_entity_get_pad_format(&uds->entity, config,
  116. UDS_PAD_SINK);
  117. mutex_lock(&uds->entity.lock);
  118. if (fse->index || fse->code != format->code) {
  119. ret = -EINVAL;
  120. goto done;
  121. }
  122. if (fse->pad == UDS_PAD_SINK) {
  123. fse->min_width = UDS_MIN_SIZE;
  124. fse->max_width = UDS_MAX_SIZE;
  125. fse->min_height = UDS_MIN_SIZE;
  126. fse->max_height = UDS_MAX_SIZE;
  127. } else {
  128. uds_output_limits(format->width, &fse->min_width,
  129. &fse->max_width);
  130. uds_output_limits(format->height, &fse->min_height,
  131. &fse->max_height);
  132. }
  133. done:
  134. mutex_unlock(&uds->entity.lock);
  135. return ret;
  136. }
  137. static void uds_try_format(struct vsp1_uds *uds,
  138. struct v4l2_subdev_pad_config *config,
  139. unsigned int pad, struct v4l2_mbus_framefmt *fmt)
  140. {
  141. struct v4l2_mbus_framefmt *format;
  142. unsigned int minimum;
  143. unsigned int maximum;
  144. switch (pad) {
  145. case UDS_PAD_SINK:
  146. /* Default to YUV if the requested format is not supported. */
  147. if (fmt->code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
  148. fmt->code != MEDIA_BUS_FMT_AYUV8_1X32)
  149. fmt->code = MEDIA_BUS_FMT_AYUV8_1X32;
  150. fmt->width = clamp(fmt->width, UDS_MIN_SIZE, UDS_MAX_SIZE);
  151. fmt->height = clamp(fmt->height, UDS_MIN_SIZE, UDS_MAX_SIZE);
  152. break;
  153. case UDS_PAD_SOURCE:
  154. /* The UDS scales but can't perform format conversion. */
  155. format = vsp1_entity_get_pad_format(&uds->entity, config,
  156. UDS_PAD_SINK);
  157. fmt->code = format->code;
  158. uds_output_limits(format->width, &minimum, &maximum);
  159. fmt->width = clamp(fmt->width, minimum, maximum);
  160. uds_output_limits(format->height, &minimum, &maximum);
  161. fmt->height = clamp(fmt->height, minimum, maximum);
  162. break;
  163. }
  164. fmt->field = V4L2_FIELD_NONE;
  165. fmt->colorspace = V4L2_COLORSPACE_SRGB;
  166. }
  167. static int uds_set_format(struct v4l2_subdev *subdev,
  168. struct v4l2_subdev_pad_config *cfg,
  169. struct v4l2_subdev_format *fmt)
  170. {
  171. struct vsp1_uds *uds = to_uds(subdev);
  172. struct v4l2_subdev_pad_config *config;
  173. struct v4l2_mbus_framefmt *format;
  174. int ret = 0;
  175. mutex_lock(&uds->entity.lock);
  176. config = vsp1_entity_get_pad_config(&uds->entity, cfg, fmt->which);
  177. if (!config) {
  178. ret = -EINVAL;
  179. goto done;
  180. }
  181. uds_try_format(uds, config, fmt->pad, &fmt->format);
  182. format = vsp1_entity_get_pad_format(&uds->entity, config, fmt->pad);
  183. *format = fmt->format;
  184. if (fmt->pad == UDS_PAD_SINK) {
  185. /* Propagate the format to the source pad. */
  186. format = vsp1_entity_get_pad_format(&uds->entity, config,
  187. UDS_PAD_SOURCE);
  188. *format = fmt->format;
  189. uds_try_format(uds, config, UDS_PAD_SOURCE, format);
  190. }
  191. done:
  192. mutex_unlock(&uds->entity.lock);
  193. return ret;
  194. }
  195. /* -----------------------------------------------------------------------------
  196. * V4L2 Subdevice Operations
  197. */
  198. static const struct v4l2_subdev_pad_ops uds_pad_ops = {
  199. .init_cfg = vsp1_entity_init_cfg,
  200. .enum_mbus_code = uds_enum_mbus_code,
  201. .enum_frame_size = uds_enum_frame_size,
  202. .get_fmt = vsp1_subdev_get_pad_format,
  203. .set_fmt = uds_set_format,
  204. };
  205. static const struct v4l2_subdev_ops uds_ops = {
  206. .pad = &uds_pad_ops,
  207. };
  208. /* -----------------------------------------------------------------------------
  209. * VSP1 Entity Operations
  210. */
  211. static void uds_configure_stream(struct vsp1_entity *entity,
  212. struct vsp1_pipeline *pipe,
  213. struct vsp1_dl_body *dlb)
  214. {
  215. struct vsp1_uds *uds = to_uds(&entity->subdev);
  216. const struct v4l2_mbus_framefmt *output;
  217. const struct v4l2_mbus_framefmt *input;
  218. unsigned int hscale;
  219. unsigned int vscale;
  220. bool multitap;
  221. input = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
  222. UDS_PAD_SINK);
  223. output = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
  224. UDS_PAD_SOURCE);
  225. hscale = uds_compute_ratio(input->width, output->width);
  226. vscale = uds_compute_ratio(input->height, output->height);
  227. dev_dbg(uds->entity.vsp1->dev, "hscale %u vscale %u\n", hscale, vscale);
  228. /*
  229. * Multi-tap scaling can't be enabled along with alpha scaling when
  230. * scaling down with a factor lower than or equal to 1/2 in either
  231. * direction.
  232. */
  233. if (uds->scale_alpha && (hscale >= 8192 || vscale >= 8192))
  234. multitap = false;
  235. else
  236. multitap = true;
  237. vsp1_uds_write(uds, dlb, VI6_UDS_CTRL,
  238. (uds->scale_alpha ? VI6_UDS_CTRL_AON : 0) |
  239. (multitap ? VI6_UDS_CTRL_BC : 0));
  240. vsp1_uds_write(uds, dlb, VI6_UDS_PASS_BWIDTH,
  241. (uds_passband_width(hscale)
  242. << VI6_UDS_PASS_BWIDTH_H_SHIFT) |
  243. (uds_passband_width(vscale)
  244. << VI6_UDS_PASS_BWIDTH_V_SHIFT));
  245. /* Set the scaling ratios. */
  246. vsp1_uds_write(uds, dlb, VI6_UDS_SCALE,
  247. (hscale << VI6_UDS_SCALE_HFRAC_SHIFT) |
  248. (vscale << VI6_UDS_SCALE_VFRAC_SHIFT));
  249. }
  250. static void uds_configure_partition(struct vsp1_entity *entity,
  251. struct vsp1_pipeline *pipe,
  252. struct vsp1_dl_list *dl,
  253. struct vsp1_dl_body *dlb)
  254. {
  255. struct vsp1_uds *uds = to_uds(&entity->subdev);
  256. struct vsp1_partition *partition = pipe->partition;
  257. const struct v4l2_mbus_framefmt *output;
  258. output = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
  259. UDS_PAD_SOURCE);
  260. /* Input size clipping */
  261. vsp1_uds_write(uds, dlb, VI6_UDS_HSZCLIP, VI6_UDS_HSZCLIP_HCEN |
  262. (0 << VI6_UDS_HSZCLIP_HCL_OFST_SHIFT) |
  263. (partition->uds_sink.width
  264. << VI6_UDS_HSZCLIP_HCL_SIZE_SHIFT));
  265. /* Output size clipping */
  266. vsp1_uds_write(uds, dlb, VI6_UDS_CLIP_SIZE,
  267. (partition->uds_source.width
  268. << VI6_UDS_CLIP_SIZE_HSIZE_SHIFT) |
  269. (output->height
  270. << VI6_UDS_CLIP_SIZE_VSIZE_SHIFT));
  271. }
  272. static unsigned int uds_max_width(struct vsp1_entity *entity,
  273. struct vsp1_pipeline *pipe)
  274. {
  275. struct vsp1_uds *uds = to_uds(&entity->subdev);
  276. const struct v4l2_mbus_framefmt *output;
  277. const struct v4l2_mbus_framefmt *input;
  278. unsigned int hscale;
  279. input = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
  280. UDS_PAD_SINK);
  281. output = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
  282. UDS_PAD_SOURCE);
  283. hscale = output->width / input->width;
  284. if (hscale <= 2)
  285. return 256;
  286. else if (hscale <= 4)
  287. return 512;
  288. else if (hscale <= 8)
  289. return 1024;
  290. else
  291. return 2048;
  292. }
  293. /* -----------------------------------------------------------------------------
  294. * Partition Algorithm Support
  295. */
  296. static void uds_partition(struct vsp1_entity *entity,
  297. struct vsp1_pipeline *pipe,
  298. struct vsp1_partition *partition,
  299. unsigned int partition_idx,
  300. struct vsp1_partition_window *window)
  301. {
  302. struct vsp1_uds *uds = to_uds(&entity->subdev);
  303. const struct v4l2_mbus_framefmt *output;
  304. const struct v4l2_mbus_framefmt *input;
  305. /* Initialise the partition state */
  306. partition->uds_sink = *window;
  307. partition->uds_source = *window;
  308. input = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
  309. UDS_PAD_SINK);
  310. output = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
  311. UDS_PAD_SOURCE);
  312. partition->uds_sink.width = window->width * input->width
  313. / output->width;
  314. partition->uds_sink.left = window->left * input->width
  315. / output->width;
  316. *window = partition->uds_sink;
  317. }
  318. static const struct vsp1_entity_operations uds_entity_ops = {
  319. .configure_stream = uds_configure_stream,
  320. .configure_partition = uds_configure_partition,
  321. .max_width = uds_max_width,
  322. .partition = uds_partition,
  323. };
  324. /* -----------------------------------------------------------------------------
  325. * Initialization and Cleanup
  326. */
  327. struct vsp1_uds *vsp1_uds_create(struct vsp1_device *vsp1, unsigned int index)
  328. {
  329. struct vsp1_uds *uds;
  330. char name[6];
  331. int ret;
  332. uds = devm_kzalloc(vsp1->dev, sizeof(*uds), GFP_KERNEL);
  333. if (uds == NULL)
  334. return ERR_PTR(-ENOMEM);
  335. uds->entity.ops = &uds_entity_ops;
  336. uds->entity.type = VSP1_ENTITY_UDS;
  337. uds->entity.index = index;
  338. sprintf(name, "uds.%u", index);
  339. ret = vsp1_entity_init(vsp1, &uds->entity, name, 2, &uds_ops,
  340. MEDIA_ENT_F_PROC_VIDEO_SCALER);
  341. if (ret < 0)
  342. return ERR_PTR(ret);
  343. return uds;
  344. }