1 // SPDX-License-Identifier: GPL-2.0+
3 * vsp1_rwpf.c -- R-Car VSP1 Read and Write Pixel Formatters
5 * Copyright (C) 2013-2014 Renesas Electronics Corporation
7 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
10 #include <media/v4l2-subdev.h>
13 #include "vsp1_rwpf.h"
14 #include "vsp1_video.h"
16 #define RWPF_MIN_WIDTH 1
17 #define RWPF_MIN_HEIGHT 1
19 struct v4l2_rect *vsp1_rwpf_get_crop(struct vsp1_rwpf *rwpf,
20 struct v4l2_subdev_state *sd_state)
22 return v4l2_subdev_get_try_crop(&rwpf->entity.subdev, sd_state,
26 /* -----------------------------------------------------------------------------
27 * V4L2 Subdevice Operations
30 static int vsp1_rwpf_enum_mbus_code(struct v4l2_subdev *subdev,
31 struct v4l2_subdev_state *sd_state,
32 struct v4l2_subdev_mbus_code_enum *code)
34 static const unsigned int codes[] = {
35 MEDIA_BUS_FMT_ARGB8888_1X32,
36 MEDIA_BUS_FMT_AHSV8888_1X32,
37 MEDIA_BUS_FMT_AYUV8_1X32,
40 if (code->index >= ARRAY_SIZE(codes))
43 code->code = codes[code->index];
48 static int vsp1_rwpf_enum_frame_size(struct v4l2_subdev *subdev,
49 struct v4l2_subdev_state *sd_state,
50 struct v4l2_subdev_frame_size_enum *fse)
52 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
54 return vsp1_subdev_enum_frame_size(subdev, sd_state, fse,
56 RWPF_MIN_HEIGHT, rwpf->max_width,
60 static int vsp1_rwpf_set_format(struct v4l2_subdev *subdev,
61 struct v4l2_subdev_state *sd_state,
62 struct v4l2_subdev_format *fmt)
64 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
65 struct v4l2_subdev_state *config;
66 struct v4l2_mbus_framefmt *format;
69 mutex_lock(&rwpf->entity.lock);
71 config = vsp1_entity_get_pad_config(&rwpf->entity, sd_state,
78 /* Default to YUV if the requested format is not supported. */
79 if (fmt->format.code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
80 fmt->format.code != MEDIA_BUS_FMT_AHSV8888_1X32 &&
81 fmt->format.code != MEDIA_BUS_FMT_AYUV8_1X32)
82 fmt->format.code = MEDIA_BUS_FMT_AYUV8_1X32;
84 format = vsp1_entity_get_pad_format(&rwpf->entity, config, fmt->pad);
86 if (fmt->pad == RWPF_PAD_SOURCE) {
88 * The RWPF performs format conversion but can't scale, only the
89 * format code can be changed on the source pad.
91 format->code = fmt->format.code;
92 fmt->format = *format;
96 format->code = fmt->format.code;
97 format->width = clamp_t(unsigned int, fmt->format.width,
98 RWPF_MIN_WIDTH, rwpf->max_width);
99 format->height = clamp_t(unsigned int, fmt->format.height,
100 RWPF_MIN_HEIGHT, rwpf->max_height);
101 format->field = V4L2_FIELD_NONE;
102 format->colorspace = V4L2_COLORSPACE_SRGB;
104 fmt->format = *format;
106 if (rwpf->entity.type == VSP1_ENTITY_RPF) {
107 struct v4l2_rect *crop;
109 /* Update the sink crop rectangle. */
110 crop = vsp1_rwpf_get_crop(rwpf, config);
113 crop->width = fmt->format.width;
114 crop->height = fmt->format.height;
117 /* Propagate the format to the source pad. */
118 format = vsp1_entity_get_pad_format(&rwpf->entity, config,
120 *format = fmt->format;
122 if (rwpf->flip.rotate) {
123 format->width = fmt->format.height;
124 format->height = fmt->format.width;
128 mutex_unlock(&rwpf->entity.lock);
132 static int vsp1_rwpf_get_selection(struct v4l2_subdev *subdev,
133 struct v4l2_subdev_state *sd_state,
134 struct v4l2_subdev_selection *sel)
136 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
137 struct v4l2_subdev_state *config;
138 struct v4l2_mbus_framefmt *format;
142 * Cropping is only supported on the RPF and is implemented on the sink
145 if (rwpf->entity.type == VSP1_ENTITY_WPF || sel->pad != RWPF_PAD_SINK)
148 mutex_lock(&rwpf->entity.lock);
150 config = vsp1_entity_get_pad_config(&rwpf->entity, sd_state,
157 switch (sel->target) {
158 case V4L2_SEL_TGT_CROP:
159 sel->r = *vsp1_rwpf_get_crop(rwpf, config);
162 case V4L2_SEL_TGT_CROP_BOUNDS:
163 format = vsp1_entity_get_pad_format(&rwpf->entity, config,
167 sel->r.width = format->width;
168 sel->r.height = format->height;
177 mutex_unlock(&rwpf->entity.lock);
181 static int vsp1_rwpf_set_selection(struct v4l2_subdev *subdev,
182 struct v4l2_subdev_state *sd_state,
183 struct v4l2_subdev_selection *sel)
185 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
186 struct v4l2_subdev_state *config;
187 struct v4l2_mbus_framefmt *format;
188 struct v4l2_rect *crop;
192 * Cropping is only supported on the RPF and is implemented on the sink
195 if (rwpf->entity.type == VSP1_ENTITY_WPF || sel->pad != RWPF_PAD_SINK)
198 if (sel->target != V4L2_SEL_TGT_CROP)
201 mutex_lock(&rwpf->entity.lock);
203 config = vsp1_entity_get_pad_config(&rwpf->entity, sd_state,
210 /* Make sure the crop rectangle is entirely contained in the image. */
211 format = vsp1_entity_get_pad_format(&rwpf->entity, config,
215 * Restrict the crop rectangle coordinates to multiples of 2 to avoid
216 * shifting the color plane.
218 if (format->code == MEDIA_BUS_FMT_AYUV8_1X32) {
219 sel->r.left = ALIGN(sel->r.left, 2);
220 sel->r.top = ALIGN(sel->r.top, 2);
221 sel->r.width = round_down(sel->r.width, 2);
222 sel->r.height = round_down(sel->r.height, 2);
225 sel->r.left = min_t(unsigned int, sel->r.left, format->width - 2);
226 sel->r.top = min_t(unsigned int, sel->r.top, format->height - 2);
227 sel->r.width = min_t(unsigned int, sel->r.width,
228 format->width - sel->r.left);
229 sel->r.height = min_t(unsigned int, sel->r.height,
230 format->height - sel->r.top);
232 crop = vsp1_rwpf_get_crop(rwpf, config);
235 /* Propagate the format to the source pad. */
236 format = vsp1_entity_get_pad_format(&rwpf->entity, config,
238 format->width = crop->width;
239 format->height = crop->height;
242 mutex_unlock(&rwpf->entity.lock);
246 static const struct v4l2_subdev_pad_ops vsp1_rwpf_pad_ops = {
247 .init_cfg = vsp1_entity_init_cfg,
248 .enum_mbus_code = vsp1_rwpf_enum_mbus_code,
249 .enum_frame_size = vsp1_rwpf_enum_frame_size,
250 .get_fmt = vsp1_subdev_get_pad_format,
251 .set_fmt = vsp1_rwpf_set_format,
252 .get_selection = vsp1_rwpf_get_selection,
253 .set_selection = vsp1_rwpf_set_selection,
256 const struct v4l2_subdev_ops vsp1_rwpf_subdev_ops = {
257 .pad = &vsp1_rwpf_pad_ops,
260 /* -----------------------------------------------------------------------------
264 static int vsp1_rwpf_s_ctrl(struct v4l2_ctrl *ctrl)
266 struct vsp1_rwpf *rwpf =
267 container_of(ctrl->handler, struct vsp1_rwpf, ctrls);
270 case V4L2_CID_ALPHA_COMPONENT:
271 rwpf->alpha = ctrl->val;
278 static const struct v4l2_ctrl_ops vsp1_rwpf_ctrl_ops = {
279 .s_ctrl = vsp1_rwpf_s_ctrl,
282 int vsp1_rwpf_init_ctrls(struct vsp1_rwpf *rwpf, unsigned int ncontrols)
284 v4l2_ctrl_handler_init(&rwpf->ctrls, ncontrols + 1);
285 v4l2_ctrl_new_std(&rwpf->ctrls, &vsp1_rwpf_ctrl_ops,
286 V4L2_CID_ALPHA_COMPONENT, 0, 255, 1, 255);
288 rwpf->entity.subdev.ctrl_handler = &rwpf->ctrls;
290 return rwpf->ctrls.error;