vsp1_rwpf.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * vsp1_rwpf.c -- R-Car VSP1 Read and Write Pixel Formatters
  4. *
  5. * Copyright (C) 2013-2014 Renesas Electronics Corporation
  6. *
  7. * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  8. */
  9. #include <media/v4l2-subdev.h>
  10. #include "vsp1.h"
  11. #include "vsp1_rwpf.h"
  12. #include "vsp1_video.h"
  13. #define RWPF_MIN_WIDTH 1
  14. #define RWPF_MIN_HEIGHT 1
  15. struct v4l2_rect *vsp1_rwpf_get_crop(struct vsp1_rwpf *rwpf,
  16. struct v4l2_subdev_pad_config *config)
  17. {
  18. return v4l2_subdev_get_try_crop(&rwpf->entity.subdev, config,
  19. RWPF_PAD_SINK);
  20. }
  21. /* -----------------------------------------------------------------------------
  22. * V4L2 Subdevice Pad Operations
  23. */
  24. static int vsp1_rwpf_enum_mbus_code(struct v4l2_subdev *subdev,
  25. struct v4l2_subdev_pad_config *cfg,
  26. struct v4l2_subdev_mbus_code_enum *code)
  27. {
  28. static const unsigned int codes[] = {
  29. MEDIA_BUS_FMT_ARGB8888_1X32,
  30. MEDIA_BUS_FMT_AHSV8888_1X32,
  31. MEDIA_BUS_FMT_AYUV8_1X32,
  32. };
  33. if (code->index >= ARRAY_SIZE(codes))
  34. return -EINVAL;
  35. code->code = codes[code->index];
  36. return 0;
  37. }
  38. static int vsp1_rwpf_enum_frame_size(struct v4l2_subdev *subdev,
  39. struct v4l2_subdev_pad_config *cfg,
  40. struct v4l2_subdev_frame_size_enum *fse)
  41. {
  42. struct vsp1_rwpf *rwpf = to_rwpf(subdev);
  43. return vsp1_subdev_enum_frame_size(subdev, cfg, fse, RWPF_MIN_WIDTH,
  44. RWPF_MIN_HEIGHT, rwpf->max_width,
  45. rwpf->max_height);
  46. }
  47. static int vsp1_rwpf_set_format(struct v4l2_subdev *subdev,
  48. struct v4l2_subdev_pad_config *cfg,
  49. struct v4l2_subdev_format *fmt)
  50. {
  51. struct vsp1_rwpf *rwpf = to_rwpf(subdev);
  52. struct v4l2_subdev_pad_config *config;
  53. struct v4l2_mbus_framefmt *format;
  54. int ret = 0;
  55. mutex_lock(&rwpf->entity.lock);
  56. config = vsp1_entity_get_pad_config(&rwpf->entity, cfg, fmt->which);
  57. if (!config) {
  58. ret = -EINVAL;
  59. goto done;
  60. }
  61. /* Default to YUV if the requested format is not supported. */
  62. if (fmt->format.code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
  63. fmt->format.code != MEDIA_BUS_FMT_AHSV8888_1X32 &&
  64. fmt->format.code != MEDIA_BUS_FMT_AYUV8_1X32)
  65. fmt->format.code = MEDIA_BUS_FMT_AYUV8_1X32;
  66. format = vsp1_entity_get_pad_format(&rwpf->entity, config, fmt->pad);
  67. if (fmt->pad == RWPF_PAD_SOURCE) {
  68. /*
  69. * The RWPF performs format conversion but can't scale, only the
  70. * format code can be changed on the source pad.
  71. */
  72. format->code = fmt->format.code;
  73. fmt->format = *format;
  74. goto done;
  75. }
  76. format->code = fmt->format.code;
  77. format->width = clamp_t(unsigned int, fmt->format.width,
  78. RWPF_MIN_WIDTH, rwpf->max_width);
  79. format->height = clamp_t(unsigned int, fmt->format.height,
  80. RWPF_MIN_HEIGHT, rwpf->max_height);
  81. format->field = V4L2_FIELD_NONE;
  82. format->colorspace = V4L2_COLORSPACE_SRGB;
  83. fmt->format = *format;
  84. if (rwpf->entity.type == VSP1_ENTITY_RPF) {
  85. struct v4l2_rect *crop;
  86. /* Update the sink crop rectangle. */
  87. crop = vsp1_rwpf_get_crop(rwpf, config);
  88. crop->left = 0;
  89. crop->top = 0;
  90. crop->width = fmt->format.width;
  91. crop->height = fmt->format.height;
  92. }
  93. /* Propagate the format to the source pad. */
  94. format = vsp1_entity_get_pad_format(&rwpf->entity, config,
  95. RWPF_PAD_SOURCE);
  96. *format = fmt->format;
  97. if (rwpf->flip.rotate) {
  98. format->width = fmt->format.height;
  99. format->height = fmt->format.width;
  100. }
  101. done:
  102. mutex_unlock(&rwpf->entity.lock);
  103. return ret;
  104. }
  105. static int vsp1_rwpf_get_selection(struct v4l2_subdev *subdev,
  106. struct v4l2_subdev_pad_config *cfg,
  107. struct v4l2_subdev_selection *sel)
  108. {
  109. struct vsp1_rwpf *rwpf = to_rwpf(subdev);
  110. struct v4l2_subdev_pad_config *config;
  111. struct v4l2_mbus_framefmt *format;
  112. int ret = 0;
  113. /*
  114. * Cropping is only supported on the RPF and is implemented on the sink
  115. * pad.
  116. */
  117. if (rwpf->entity.type == VSP1_ENTITY_WPF || sel->pad != RWPF_PAD_SINK)
  118. return -EINVAL;
  119. mutex_lock(&rwpf->entity.lock);
  120. config = vsp1_entity_get_pad_config(&rwpf->entity, cfg, sel->which);
  121. if (!config) {
  122. ret = -EINVAL;
  123. goto done;
  124. }
  125. switch (sel->target) {
  126. case V4L2_SEL_TGT_CROP:
  127. sel->r = *vsp1_rwpf_get_crop(rwpf, config);
  128. break;
  129. case V4L2_SEL_TGT_CROP_BOUNDS:
  130. format = vsp1_entity_get_pad_format(&rwpf->entity, config,
  131. RWPF_PAD_SINK);
  132. sel->r.left = 0;
  133. sel->r.top = 0;
  134. sel->r.width = format->width;
  135. sel->r.height = format->height;
  136. break;
  137. default:
  138. ret = -EINVAL;
  139. break;
  140. }
  141. done:
  142. mutex_unlock(&rwpf->entity.lock);
  143. return ret;
  144. }
  145. static int vsp1_rwpf_set_selection(struct v4l2_subdev *subdev,
  146. struct v4l2_subdev_pad_config *cfg,
  147. struct v4l2_subdev_selection *sel)
  148. {
  149. struct vsp1_rwpf *rwpf = to_rwpf(subdev);
  150. struct v4l2_subdev_pad_config *config;
  151. struct v4l2_mbus_framefmt *format;
  152. struct v4l2_rect *crop;
  153. int ret = 0;
  154. /*
  155. * Cropping is only supported on the RPF and is implemented on the sink
  156. * pad.
  157. */
  158. if (rwpf->entity.type == VSP1_ENTITY_WPF || sel->pad != RWPF_PAD_SINK)
  159. return -EINVAL;
  160. if (sel->target != V4L2_SEL_TGT_CROP)
  161. return -EINVAL;
  162. mutex_lock(&rwpf->entity.lock);
  163. config = vsp1_entity_get_pad_config(&rwpf->entity, cfg, sel->which);
  164. if (!config) {
  165. ret = -EINVAL;
  166. goto done;
  167. }
  168. /* Make sure the crop rectangle is entirely contained in the image. */
  169. format = vsp1_entity_get_pad_format(&rwpf->entity, config,
  170. RWPF_PAD_SINK);
  171. /*
  172. * Restrict the crop rectangle coordinates to multiples of 2 to avoid
  173. * shifting the color plane.
  174. */
  175. if (format->code == MEDIA_BUS_FMT_AYUV8_1X32) {
  176. sel->r.left = ALIGN(sel->r.left, 2);
  177. sel->r.top = ALIGN(sel->r.top, 2);
  178. sel->r.width = round_down(sel->r.width, 2);
  179. sel->r.height = round_down(sel->r.height, 2);
  180. }
  181. sel->r.left = min_t(unsigned int, sel->r.left, format->width - 2);
  182. sel->r.top = min_t(unsigned int, sel->r.top, format->height - 2);
  183. sel->r.width = min_t(unsigned int, sel->r.width,
  184. format->width - sel->r.left);
  185. sel->r.height = min_t(unsigned int, sel->r.height,
  186. format->height - sel->r.top);
  187. crop = vsp1_rwpf_get_crop(rwpf, config);
  188. *crop = sel->r;
  189. /* Propagate the format to the source pad. */
  190. format = vsp1_entity_get_pad_format(&rwpf->entity, config,
  191. RWPF_PAD_SOURCE);
  192. format->width = crop->width;
  193. format->height = crop->height;
  194. done:
  195. mutex_unlock(&rwpf->entity.lock);
  196. return ret;
  197. }
  198. const struct v4l2_subdev_pad_ops vsp1_rwpf_pad_ops = {
  199. .init_cfg = vsp1_entity_init_cfg,
  200. .enum_mbus_code = vsp1_rwpf_enum_mbus_code,
  201. .enum_frame_size = vsp1_rwpf_enum_frame_size,
  202. .get_fmt = vsp1_subdev_get_pad_format,
  203. .set_fmt = vsp1_rwpf_set_format,
  204. .get_selection = vsp1_rwpf_get_selection,
  205. .set_selection = vsp1_rwpf_set_selection,
  206. };
  207. /* -----------------------------------------------------------------------------
  208. * Controls
  209. */
  210. static int vsp1_rwpf_s_ctrl(struct v4l2_ctrl *ctrl)
  211. {
  212. struct vsp1_rwpf *rwpf =
  213. container_of(ctrl->handler, struct vsp1_rwpf, ctrls);
  214. switch (ctrl->id) {
  215. case V4L2_CID_ALPHA_COMPONENT:
  216. rwpf->alpha = ctrl->val;
  217. break;
  218. }
  219. return 0;
  220. }
  221. static const struct v4l2_ctrl_ops vsp1_rwpf_ctrl_ops = {
  222. .s_ctrl = vsp1_rwpf_s_ctrl,
  223. };
  224. int vsp1_rwpf_init_ctrls(struct vsp1_rwpf *rwpf, unsigned int ncontrols)
  225. {
  226. v4l2_ctrl_handler_init(&rwpf->ctrls, ncontrols + 1);
  227. v4l2_ctrl_new_std(&rwpf->ctrls, &vsp1_rwpf_ctrl_ops,
  228. V4L2_CID_ALPHA_COMPONENT, 0, 255, 1, 255);
  229. rwpf->entity.subdev.ctrl_handler = &rwpf->ctrls;
  230. return rwpf->ctrls.error;
  231. }