Input: xpad - add support for Xbox1 PDP Camo series gamepad
[linux/fpc-iii.git] / drivers / media / platform / vsp1 / vsp1_rwpf.c
blob66e4d7ea31d67701267ccd252e5fecc7b244ec41
1 /*
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>
16 #include "vsp1.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,
27 RWPF_PAD_SINK);
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_AYUV8_1X32,
43 if (code->index >= ARRAY_SIZE(codes))
44 return -EINVAL;
46 code->code = codes[code->index];
48 return 0;
51 static int vsp1_rwpf_enum_frame_size(struct v4l2_subdev *subdev,
52 struct v4l2_subdev_pad_config *cfg,
53 struct v4l2_subdev_frame_size_enum *fse)
55 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
57 return vsp1_subdev_enum_frame_size(subdev, cfg, fse, RWPF_MIN_WIDTH,
58 RWPF_MIN_HEIGHT, rwpf->max_width,
59 rwpf->max_height);
62 static int vsp1_rwpf_set_format(struct v4l2_subdev *subdev,
63 struct v4l2_subdev_pad_config *cfg,
64 struct v4l2_subdev_format *fmt)
66 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
67 struct v4l2_subdev_pad_config *config;
68 struct v4l2_mbus_framefmt *format;
69 int ret = 0;
71 mutex_lock(&rwpf->entity.lock);
73 config = vsp1_entity_get_pad_config(&rwpf->entity, cfg, fmt->which);
74 if (!config) {
75 ret = -EINVAL;
76 goto done;
79 /* Default to YUV if the requested format is not supported. */
80 if (fmt->format.code != MEDIA_BUS_FMT_ARGB8888_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) {
87 /* The RWPF performs format conversion but can't scale, only the
88 * format code can be changed on the source pad.
90 format->code = fmt->format.code;
91 fmt->format = *format;
92 goto done;
95 format->code = fmt->format.code;
96 format->width = clamp_t(unsigned int, fmt->format.width,
97 RWPF_MIN_WIDTH, rwpf->max_width);
98 format->height = clamp_t(unsigned int, fmt->format.height,
99 RWPF_MIN_HEIGHT, rwpf->max_height);
100 format->field = V4L2_FIELD_NONE;
101 format->colorspace = V4L2_COLORSPACE_SRGB;
103 fmt->format = *format;
105 if (rwpf->entity.type == VSP1_ENTITY_RPF) {
106 struct v4l2_rect *crop;
108 /* Update the sink crop rectangle. */
109 crop = vsp1_rwpf_get_crop(rwpf, config);
110 crop->left = 0;
111 crop->top = 0;
112 crop->width = fmt->format.width;
113 crop->height = fmt->format.height;
116 /* Propagate the format to the source pad. */
117 format = vsp1_entity_get_pad_format(&rwpf->entity, config,
118 RWPF_PAD_SOURCE);
119 *format = fmt->format;
121 done:
122 mutex_unlock(&rwpf->entity.lock);
123 return ret;
126 static int vsp1_rwpf_get_selection(struct v4l2_subdev *subdev,
127 struct v4l2_subdev_pad_config *cfg,
128 struct v4l2_subdev_selection *sel)
130 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
131 struct v4l2_subdev_pad_config *config;
132 struct v4l2_mbus_framefmt *format;
133 int ret = 0;
136 * Cropping is only supported on the RPF and is implemented on the sink
137 * pad.
139 if (rwpf->entity.type == VSP1_ENTITY_WPF || sel->pad != RWPF_PAD_SINK)
140 return -EINVAL;
142 mutex_lock(&rwpf->entity.lock);
144 config = vsp1_entity_get_pad_config(&rwpf->entity, cfg, sel->which);
145 if (!config) {
146 ret = -EINVAL;
147 goto done;
150 switch (sel->target) {
151 case V4L2_SEL_TGT_CROP:
152 sel->r = *vsp1_rwpf_get_crop(rwpf, config);
153 break;
155 case V4L2_SEL_TGT_CROP_BOUNDS:
156 format = vsp1_entity_get_pad_format(&rwpf->entity, config,
157 RWPF_PAD_SINK);
158 sel->r.left = 0;
159 sel->r.top = 0;
160 sel->r.width = format->width;
161 sel->r.height = format->height;
162 break;
164 default:
165 ret = -EINVAL;
166 break;
169 done:
170 mutex_unlock(&rwpf->entity.lock);
171 return ret;
174 static int vsp1_rwpf_set_selection(struct v4l2_subdev *subdev,
175 struct v4l2_subdev_pad_config *cfg,
176 struct v4l2_subdev_selection *sel)
178 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
179 struct v4l2_subdev_pad_config *config;
180 struct v4l2_mbus_framefmt *format;
181 struct v4l2_rect *crop;
182 int ret = 0;
185 * Cropping is only supported on the RPF and is implemented on the sink
186 * pad.
188 if (rwpf->entity.type == VSP1_ENTITY_WPF || sel->pad != RWPF_PAD_SINK)
189 return -EINVAL;
191 if (sel->target != V4L2_SEL_TGT_CROP)
192 return -EINVAL;
194 mutex_lock(&rwpf->entity.lock);
196 config = vsp1_entity_get_pad_config(&rwpf->entity, cfg, sel->which);
197 if (!config) {
198 ret = -EINVAL;
199 goto done;
202 /* Make sure the crop rectangle is entirely contained in the image. */
203 format = vsp1_entity_get_pad_format(&rwpf->entity, config,
204 RWPF_PAD_SINK);
206 /* Restrict the crop rectangle coordinates to multiples of 2 to avoid
207 * shifting the color plane.
209 if (format->code == MEDIA_BUS_FMT_AYUV8_1X32) {
210 sel->r.left = ALIGN(sel->r.left, 2);
211 sel->r.top = ALIGN(sel->r.top, 2);
212 sel->r.width = round_down(sel->r.width, 2);
213 sel->r.height = round_down(sel->r.height, 2);
216 sel->r.left = min_t(unsigned int, sel->r.left, format->width - 2);
217 sel->r.top = min_t(unsigned int, sel->r.top, format->height - 2);
218 sel->r.width = min_t(unsigned int, sel->r.width,
219 format->width - sel->r.left);
220 sel->r.height = min_t(unsigned int, sel->r.height,
221 format->height - sel->r.top);
223 crop = vsp1_rwpf_get_crop(rwpf, config);
224 *crop = sel->r;
226 /* Propagate the format to the source pad. */
227 format = vsp1_entity_get_pad_format(&rwpf->entity, config,
228 RWPF_PAD_SOURCE);
229 format->width = crop->width;
230 format->height = crop->height;
232 done:
233 mutex_unlock(&rwpf->entity.lock);
234 return ret;
237 const struct v4l2_subdev_pad_ops vsp1_rwpf_pad_ops = {
238 .init_cfg = vsp1_entity_init_cfg,
239 .enum_mbus_code = vsp1_rwpf_enum_mbus_code,
240 .enum_frame_size = vsp1_rwpf_enum_frame_size,
241 .get_fmt = vsp1_subdev_get_pad_format,
242 .set_fmt = vsp1_rwpf_set_format,
243 .get_selection = vsp1_rwpf_get_selection,
244 .set_selection = vsp1_rwpf_set_selection,
247 /* -----------------------------------------------------------------------------
248 * Controls
251 static int vsp1_rwpf_s_ctrl(struct v4l2_ctrl *ctrl)
253 struct vsp1_rwpf *rwpf =
254 container_of(ctrl->handler, struct vsp1_rwpf, ctrls);
256 switch (ctrl->id) {
257 case V4L2_CID_ALPHA_COMPONENT:
258 rwpf->alpha = ctrl->val;
259 break;
262 return 0;
265 static const struct v4l2_ctrl_ops vsp1_rwpf_ctrl_ops = {
266 .s_ctrl = vsp1_rwpf_s_ctrl,
269 int vsp1_rwpf_init_ctrls(struct vsp1_rwpf *rwpf, unsigned int ncontrols)
271 v4l2_ctrl_handler_init(&rwpf->ctrls, ncontrols + 1);
272 v4l2_ctrl_new_std(&rwpf->ctrls, &vsp1_rwpf_ctrl_ops,
273 V4L2_CID_ALPHA_COMPONENT, 0, 255, 1, 255);
275 rwpf->entity.subdev.ctrl_handler = &rwpf->ctrls;
277 return rwpf->ctrls.error;