vsp1_uds.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. input = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
  227. UDS_PAD_SINK);
  228. output = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
  229. UDS_PAD_SOURCE);
  230. if (params == VSP1_ENTITY_PARAMS_PARTITION) {
  231. struct vsp1_partition *partition = pipe->partition;
  232. /* Input size clipping */
  233. vsp1_uds_write(uds, dl, VI6_UDS_HSZCLIP, VI6_UDS_HSZCLIP_HCEN |
  234. (0 << VI6_UDS_HSZCLIP_HCL_OFST_SHIFT) |
  235. (partition->uds_sink.width
  236. << VI6_UDS_HSZCLIP_HCL_SIZE_SHIFT));
  237. /* Output size clipping */
  238. vsp1_uds_write(uds, dl, VI6_UDS_CLIP_SIZE,
  239. (partition->uds_source.width
  240. << VI6_UDS_CLIP_SIZE_HSIZE_SHIFT) |
  241. (output->height
  242. << VI6_UDS_CLIP_SIZE_VSIZE_SHIFT));
  243. return;
  244. }
  245. if (params != VSP1_ENTITY_PARAMS_INIT)
  246. return;
  247. hscale = uds_compute_ratio(input->width, output->width);
  248. vscale = uds_compute_ratio(input->height, output->height);
  249. dev_dbg(uds->entity.vsp1->dev, "hscale %u vscale %u\n", hscale, vscale);
  250. /*
  251. * Multi-tap scaling can't be enabled along with alpha scaling when
  252. * scaling down with a factor lower than or equal to 1/2 in either
  253. * direction.
  254. */
  255. if (uds->scale_alpha && (hscale >= 8192 || vscale >= 8192))
  256. multitap = false;
  257. else
  258. multitap = true;
  259. vsp1_uds_write(uds, dl, VI6_UDS_CTRL,
  260. (uds->scale_alpha ? VI6_UDS_CTRL_AON : 0) |
  261. (multitap ? VI6_UDS_CTRL_BC : 0));
  262. vsp1_uds_write(uds, dl, VI6_UDS_PASS_BWIDTH,
  263. (uds_passband_width(hscale)
  264. << VI6_UDS_PASS_BWIDTH_H_SHIFT) |
  265. (uds_passband_width(vscale)
  266. << VI6_UDS_PASS_BWIDTH_V_SHIFT));
  267. /* Set the scaling ratios. */
  268. vsp1_uds_write(uds, dl, VI6_UDS_SCALE,
  269. (hscale << VI6_UDS_SCALE_HFRAC_SHIFT) |
  270. (vscale << VI6_UDS_SCALE_VFRAC_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 = uds_configure,
  320. .max_width = uds_max_width,
  321. .partition = uds_partition,
  322. };
  323. /* -----------------------------------------------------------------------------
  324. * Initialization and Cleanup
  325. */
  326. struct vsp1_uds *vsp1_uds_create(struct vsp1_device *vsp1, unsigned int index)
  327. {
  328. struct vsp1_uds *uds;
  329. char name[6];
  330. int ret;
  331. uds = devm_kzalloc(vsp1->dev, sizeof(*uds), GFP_KERNEL);
  332. if (uds == NULL)
  333. return ERR_PTR(-ENOMEM);
  334. uds->entity.ops = &uds_entity_ops;
  335. uds->entity.type = VSP1_ENTITY_UDS;
  336. uds->entity.index = index;
  337. sprintf(name, "uds.%u", index);
  338. ret = vsp1_entity_init(vsp1, &uds->entity, name, 2, &uds_ops,
  339. MEDIA_ENT_F_PROC_VIDEO_SCALER);
  340. if (ret < 0)
  341. return ERR_PTR(ret);
  342. return uds;
  343. }