vsp1_rwpf.c 7.3 KB

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