vsp1_rwpf.c 7.4 KB

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