2 * vsp1_rwpf.c -- R-Car VSP1 Read and Write Pixel Formatters
4 * Copyright (C) 2013-2014 Renesas Electronics Corporation
6 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
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.
14 #include <media/v4l2-subdev.h>
17 #include "vsp1_rwpf.h"
18 #include "vsp1_video.h"
20 #define RWPF_MIN_WIDTH 1
21 #define RWPF_MIN_HEIGHT 1
23 struct v4l2_rect *vsp1_rwpf_get_crop(struct vsp1_rwpf *rwpf,
24 struct v4l2_subdev_pad_config *config)
26 return v4l2_subdev_get_try_crop(&rwpf->entity.subdev, config,
30 /* -----------------------------------------------------------------------------
31 * V4L2 Subdevice Pad Operations
34 static int vsp1_rwpf_enum_mbus_code(struct v4l2_subdev *subdev,
35 struct v4l2_subdev_pad_config *cfg,
36 struct v4l2_subdev_mbus_code_enum *code)
38 static const unsigned int codes[] = {
39 MEDIA_BUS_FMT_ARGB8888_1X32,
40 MEDIA_BUS_FMT_AHSV8888_1X32,
41 MEDIA_BUS_FMT_AYUV8_1X32,
44 if (code->index >= ARRAY_SIZE(codes))
47 code->code = codes[code->index];
52 static int vsp1_rwpf_enum_frame_size(struct v4l2_subdev *subdev,
53 struct v4l2_subdev_pad_config *cfg,
54 struct v4l2_subdev_frame_size_enum *fse)
56 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
58 return vsp1_subdev_enum_frame_size(subdev, cfg, fse, RWPF_MIN_WIDTH,
59 RWPF_MIN_HEIGHT, rwpf->max_width,
63 static int vsp1_rwpf_set_format(struct v4l2_subdev *subdev,
64 struct v4l2_subdev_pad_config *cfg,
65 struct v4l2_subdev_format *fmt)
67 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
68 struct v4l2_subdev_pad_config *config;
69 struct v4l2_mbus_framefmt *format;
72 mutex_lock(&rwpf->entity.lock);
74 config = vsp1_entity_get_pad_config(&rwpf->entity, cfg, fmt->which);
80 /* Default to YUV if the requested format is not supported. */
81 if (fmt->format.code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
82 fmt->format.code != MEDIA_BUS_FMT_AHSV8888_1X32 &&
83 fmt->format.code != MEDIA_BUS_FMT_AYUV8_1X32)
84 fmt->format.code = MEDIA_BUS_FMT_AYUV8_1X32;
86 format = vsp1_entity_get_pad_format(&rwpf->entity, config, fmt->pad);
88 if (fmt->pad == RWPF_PAD_SOURCE) {
90 * The RWPF performs format conversion but can't scale, only the
91 * format code can be changed on the source pad.
93 format->code = fmt->format.code;
94 fmt->format = *format;
98 format->code = fmt->format.code;
99 format->width = clamp_t(unsigned int, fmt->format.width,
100 RWPF_MIN_WIDTH, rwpf->max_width);
101 format->height = clamp_t(unsigned int, fmt->format.height,
102 RWPF_MIN_HEIGHT, rwpf->max_height);
103 format->field = V4L2_FIELD_NONE;
104 format->colorspace = V4L2_COLORSPACE_SRGB;
106 fmt->format = *format;
108 if (rwpf->entity.type == VSP1_ENTITY_RPF) {
109 struct v4l2_rect *crop;
111 /* Update the sink crop rectangle. */
112 crop = vsp1_rwpf_get_crop(rwpf, config);
115 crop->width = fmt->format.width;
116 crop->height = fmt->format.height;
119 /* Propagate the format to the source pad. */
120 format = vsp1_entity_get_pad_format(&rwpf->entity, config,
122 *format = fmt->format;
124 if (rwpf->flip.rotate) {
125 format->width = fmt->format.height;
126 format->height = fmt->format.width;
130 mutex_unlock(&rwpf->entity.lock);
134 static int vsp1_rwpf_get_selection(struct v4l2_subdev *subdev,
135 struct v4l2_subdev_pad_config *cfg,
136 struct v4l2_subdev_selection *sel)
138 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
139 struct v4l2_subdev_pad_config *config;
140 struct v4l2_mbus_framefmt *format;
144 * Cropping is only supported on the RPF and is implemented on the sink
147 if (rwpf->entity.type == VSP1_ENTITY_WPF || sel->pad != RWPF_PAD_SINK)
150 mutex_lock(&rwpf->entity.lock);
152 config = vsp1_entity_get_pad_config(&rwpf->entity, cfg, sel->which);
158 switch (sel->target) {
159 case V4L2_SEL_TGT_CROP:
160 sel->r = *vsp1_rwpf_get_crop(rwpf, config);
163 case V4L2_SEL_TGT_CROP_BOUNDS:
164 format = vsp1_entity_get_pad_format(&rwpf->entity, config,
168 sel->r.width = format->width;
169 sel->r.height = format->height;
178 mutex_unlock(&rwpf->entity.lock);
182 static int vsp1_rwpf_set_selection(struct v4l2_subdev *subdev,
183 struct v4l2_subdev_pad_config *cfg,
184 struct v4l2_subdev_selection *sel)
186 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
187 struct v4l2_subdev_pad_config *config;
188 struct v4l2_mbus_framefmt *format;
189 struct v4l2_rect *crop;
193 * Cropping is only supported on the RPF and is implemented on the sink
196 if (rwpf->entity.type == VSP1_ENTITY_WPF || sel->pad != RWPF_PAD_SINK)
199 if (sel->target != V4L2_SEL_TGT_CROP)
202 mutex_lock(&rwpf->entity.lock);
204 config = vsp1_entity_get_pad_config(&rwpf->entity, cfg, sel->which);
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 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 /* -----------------------------------------------------------------------------
260 static int vsp1_rwpf_s_ctrl(struct v4l2_ctrl *ctrl)
262 struct vsp1_rwpf *rwpf =
263 container_of(ctrl->handler, struct vsp1_rwpf, ctrls);
266 case V4L2_CID_ALPHA_COMPONENT:
267 rwpf->alpha = ctrl->val;
274 static const struct v4l2_ctrl_ops vsp1_rwpf_ctrl_ops = {
275 .s_ctrl = vsp1_rwpf_s_ctrl,
278 int vsp1_rwpf_init_ctrls(struct vsp1_rwpf *rwpf, unsigned int ncontrols)
280 v4l2_ctrl_handler_init(&rwpf->ctrls, ncontrols + 1);
281 v4l2_ctrl_new_std(&rwpf->ctrls, &vsp1_rwpf_ctrl_ops,
282 V4L2_CID_ALPHA_COMPONENT, 0, 255, 1, 255);
284 rwpf->entity.subdev.ctrl_handler = &rwpf->ctrls;
286 return rwpf->ctrls.error;