vsp1_rwpf.c 5.9 KB

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