vsp1_rwpf.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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_mbus_framefmt *format;
  41. format = vsp1_entity_get_pad_format(&rwpf->entity, cfg, fse->pad,
  42. fse->which);
  43. if (fse->index || fse->code != format->code)
  44. return -EINVAL;
  45. if (fse->pad == RWPF_PAD_SINK) {
  46. fse->min_width = RWPF_MIN_WIDTH;
  47. fse->max_width = rwpf->max_width;
  48. fse->min_height = RWPF_MIN_HEIGHT;
  49. fse->max_height = rwpf->max_height;
  50. } else {
  51. /* The size on the source pad are fixed and always identical to
  52. * the size on the sink pad.
  53. */
  54. fse->min_width = format->width;
  55. fse->max_width = format->width;
  56. fse->min_height = format->height;
  57. fse->max_height = format->height;
  58. }
  59. return 0;
  60. }
  61. static struct v4l2_rect *
  62. vsp1_rwpf_get_crop(struct vsp1_rwpf *rwpf, struct v4l2_subdev_pad_config *cfg, u32 which)
  63. {
  64. switch (which) {
  65. case V4L2_SUBDEV_FORMAT_TRY:
  66. return v4l2_subdev_get_try_crop(&rwpf->entity.subdev, cfg, RWPF_PAD_SINK);
  67. case V4L2_SUBDEV_FORMAT_ACTIVE:
  68. return &rwpf->crop;
  69. default:
  70. return NULL;
  71. }
  72. }
  73. int vsp1_rwpf_get_format(struct v4l2_subdev *subdev, struct v4l2_subdev_pad_config *cfg,
  74. struct v4l2_subdev_format *fmt)
  75. {
  76. struct vsp1_rwpf *rwpf = to_rwpf(subdev);
  77. fmt->format = *vsp1_entity_get_pad_format(&rwpf->entity, cfg, fmt->pad,
  78. fmt->which);
  79. return 0;
  80. }
  81. int vsp1_rwpf_set_format(struct v4l2_subdev *subdev, struct v4l2_subdev_pad_config *cfg,
  82. struct v4l2_subdev_format *fmt)
  83. {
  84. struct vsp1_rwpf *rwpf = to_rwpf(subdev);
  85. struct v4l2_mbus_framefmt *format;
  86. struct v4l2_rect *crop;
  87. /* Default to YUV if the requested format is not supported. */
  88. if (fmt->format.code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
  89. fmt->format.code != MEDIA_BUS_FMT_AYUV8_1X32)
  90. fmt->format.code = MEDIA_BUS_FMT_AYUV8_1X32;
  91. format = vsp1_entity_get_pad_format(&rwpf->entity, cfg, fmt->pad,
  92. fmt->which);
  93. if (fmt->pad == RWPF_PAD_SOURCE) {
  94. /* The RWPF performs format conversion but can't scale, only the
  95. * format code can be changed on the source pad.
  96. */
  97. format->code = fmt->format.code;
  98. fmt->format = *format;
  99. return 0;
  100. }
  101. format->code = fmt->format.code;
  102. format->width = clamp_t(unsigned int, fmt->format.width,
  103. RWPF_MIN_WIDTH, rwpf->max_width);
  104. format->height = clamp_t(unsigned int, fmt->format.height,
  105. RWPF_MIN_HEIGHT, rwpf->max_height);
  106. format->field = V4L2_FIELD_NONE;
  107. format->colorspace = V4L2_COLORSPACE_SRGB;
  108. fmt->format = *format;
  109. /* Update the sink crop rectangle. */
  110. crop = vsp1_rwpf_get_crop(rwpf, cfg, fmt->which);
  111. crop->left = 0;
  112. crop->top = 0;
  113. crop->width = fmt->format.width;
  114. crop->height = fmt->format.height;
  115. /* Propagate the format to the source pad. */
  116. format = vsp1_entity_get_pad_format(&rwpf->entity, cfg, RWPF_PAD_SOURCE,
  117. fmt->which);
  118. *format = fmt->format;
  119. return 0;
  120. }
  121. int vsp1_rwpf_get_selection(struct v4l2_subdev *subdev,
  122. struct v4l2_subdev_pad_config *cfg,
  123. struct v4l2_subdev_selection *sel)
  124. {
  125. struct vsp1_rwpf *rwpf = to_rwpf(subdev);
  126. struct v4l2_mbus_framefmt *format;
  127. /* Cropping is implemented on the sink pad. */
  128. if (sel->pad != RWPF_PAD_SINK)
  129. return -EINVAL;
  130. switch (sel->target) {
  131. case V4L2_SEL_TGT_CROP:
  132. sel->r = *vsp1_rwpf_get_crop(rwpf, cfg, sel->which);
  133. break;
  134. case V4L2_SEL_TGT_CROP_BOUNDS:
  135. format = vsp1_entity_get_pad_format(&rwpf->entity, cfg,
  136. RWPF_PAD_SINK, sel->which);
  137. sel->r.left = 0;
  138. sel->r.top = 0;
  139. sel->r.width = format->width;
  140. sel->r.height = format->height;
  141. break;
  142. default:
  143. return -EINVAL;
  144. }
  145. return 0;
  146. }
  147. int vsp1_rwpf_set_selection(struct v4l2_subdev *subdev,
  148. struct v4l2_subdev_pad_config *cfg,
  149. struct v4l2_subdev_selection *sel)
  150. {
  151. struct vsp1_rwpf *rwpf = to_rwpf(subdev);
  152. struct v4l2_mbus_framefmt *format;
  153. struct v4l2_rect *crop;
  154. /* Cropping is implemented on the sink pad. */
  155. if (sel->pad != RWPF_PAD_SINK)
  156. return -EINVAL;
  157. if (sel->target != V4L2_SEL_TGT_CROP)
  158. return -EINVAL;
  159. /* Make sure the crop rectangle is entirely contained in the image. The
  160. * WPF top and left offsets are limited to 255.
  161. */
  162. format = vsp1_entity_get_pad_format(&rwpf->entity, cfg, RWPF_PAD_SINK,
  163. sel->which);
  164. /* Restrict the crop rectangle coordinates to multiples of 2 to avoid
  165. * shifting the color plane.
  166. */
  167. if (format->code == MEDIA_BUS_FMT_AYUV8_1X32) {
  168. sel->r.left = ALIGN(sel->r.left, 2);
  169. sel->r.top = ALIGN(sel->r.top, 2);
  170. sel->r.width = round_down(sel->r.width, 2);
  171. sel->r.height = round_down(sel->r.height, 2);
  172. }
  173. sel->r.left = min_t(unsigned int, sel->r.left, format->width - 2);
  174. sel->r.top = min_t(unsigned int, sel->r.top, format->height - 2);
  175. if (rwpf->entity.type == VSP1_ENTITY_WPF) {
  176. sel->r.left = min_t(unsigned int, sel->r.left, 255);
  177. sel->r.top = min_t(unsigned int, sel->r.top, 255);
  178. }
  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, cfg, sel->which);
  184. *crop = sel->r;
  185. /* Propagate the format to the source pad. */
  186. format = vsp1_entity_get_pad_format(&rwpf->entity, cfg, RWPF_PAD_SOURCE,
  187. sel->which);
  188. format->width = crop->width;
  189. format->height = crop->height;
  190. return 0;
  191. }