vsp1_rwpf.c 7.5 KB

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