vsp1_rwpf.c 7.2 KB

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