[media] v4l: vsp1: Factorize get pad format code
[deliverable/linux.git] / drivers / media / platform / vsp1 / vsp1_rwpf.c
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
14 #include <media/v4l2-subdev.h>
15
16 #include "vsp1.h"
17 #include "vsp1_rwpf.h"
18 #include "vsp1_video.h"
19
20 #define RWPF_MIN_WIDTH 1
21 #define RWPF_MIN_HEIGHT 1
22
23 struct v4l2_rect *vsp1_rwpf_get_crop(struct vsp1_rwpf *rwpf,
24 struct v4l2_subdev_pad_config *config)
25 {
26 return v4l2_subdev_get_try_crop(&rwpf->entity.subdev, config,
27 RWPF_PAD_SINK);
28 }
29
30 /* -----------------------------------------------------------------------------
31 * V4L2 Subdevice Pad Operations
32 */
33
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)
37 {
38 static const unsigned int codes[] = {
39 MEDIA_BUS_FMT_ARGB8888_1X32,
40 MEDIA_BUS_FMT_AYUV8_1X32,
41 };
42
43 if (code->index >= ARRAY_SIZE(codes))
44 return -EINVAL;
45
46 code->code = codes[code->index];
47
48 return 0;
49 }
50
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)
54 {
55 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
56 struct v4l2_subdev_pad_config *config;
57 struct v4l2_mbus_framefmt *format;
58
59 config = vsp1_entity_get_pad_config(&rwpf->entity, cfg, fse->which);
60 if (!config)
61 return -EINVAL;
62
63 format = vsp1_entity_get_pad_format(&rwpf->entity, config, fse->pad);
64
65 if (fse->index || fse->code != format->code)
66 return -EINVAL;
67
68 if (fse->pad == RWPF_PAD_SINK) {
69 fse->min_width = RWPF_MIN_WIDTH;
70 fse->max_width = rwpf->max_width;
71 fse->min_height = RWPF_MIN_HEIGHT;
72 fse->max_height = rwpf->max_height;
73 } else {
74 /* The size on the source pad are fixed and always identical to
75 * the size on the sink pad.
76 */
77 fse->min_width = format->width;
78 fse->max_width = format->width;
79 fse->min_height = format->height;
80 fse->max_height = format->height;
81 }
82
83 return 0;
84 }
85
86 static int vsp1_rwpf_set_format(struct v4l2_subdev *subdev,
87 struct v4l2_subdev_pad_config *cfg,
88 struct v4l2_subdev_format *fmt)
89 {
90 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
91 struct v4l2_subdev_pad_config *config;
92 struct v4l2_mbus_framefmt *format;
93 struct v4l2_rect *crop;
94
95 config = vsp1_entity_get_pad_config(&rwpf->entity, cfg, fmt->which);
96 if (!config)
97 return -EINVAL;
98
99 /* Default to YUV if the requested format is not supported. */
100 if (fmt->format.code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
101 fmt->format.code != MEDIA_BUS_FMT_AYUV8_1X32)
102 fmt->format.code = MEDIA_BUS_FMT_AYUV8_1X32;
103
104 format = vsp1_entity_get_pad_format(&rwpf->entity, config, fmt->pad);
105
106 if (fmt->pad == RWPF_PAD_SOURCE) {
107 /* The RWPF performs format conversion but can't scale, only the
108 * format code can be changed on the source pad.
109 */
110 format->code = fmt->format.code;
111 fmt->format = *format;
112 return 0;
113 }
114
115 format->code = fmt->format.code;
116 format->width = clamp_t(unsigned int, fmt->format.width,
117 RWPF_MIN_WIDTH, rwpf->max_width);
118 format->height = clamp_t(unsigned int, fmt->format.height,
119 RWPF_MIN_HEIGHT, rwpf->max_height);
120 format->field = V4L2_FIELD_NONE;
121 format->colorspace = V4L2_COLORSPACE_SRGB;
122
123 fmt->format = *format;
124
125 /* Update the sink crop rectangle. */
126 crop = vsp1_rwpf_get_crop(rwpf, config);
127 crop->left = 0;
128 crop->top = 0;
129 crop->width = fmt->format.width;
130 crop->height = fmt->format.height;
131
132 /* Propagate the format to the source pad. */
133 format = vsp1_entity_get_pad_format(&rwpf->entity, config,
134 RWPF_PAD_SOURCE);
135 *format = fmt->format;
136
137 return 0;
138 }
139
140 static int vsp1_rwpf_get_selection(struct v4l2_subdev *subdev,
141 struct v4l2_subdev_pad_config *cfg,
142 struct v4l2_subdev_selection *sel)
143 {
144 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
145 struct v4l2_subdev_pad_config *config;
146 struct v4l2_mbus_framefmt *format;
147
148 /* Cropping is implemented on the sink pad. */
149 if (sel->pad != RWPF_PAD_SINK)
150 return -EINVAL;
151
152 config = vsp1_entity_get_pad_config(&rwpf->entity, cfg, sel->which);
153 if (!config)
154 return -EINVAL;
155
156 switch (sel->target) {
157 case V4L2_SEL_TGT_CROP:
158 sel->r = *vsp1_rwpf_get_crop(rwpf, config);
159 break;
160
161 case V4L2_SEL_TGT_CROP_BOUNDS:
162 format = vsp1_entity_get_pad_format(&rwpf->entity, config,
163 RWPF_PAD_SINK);
164 sel->r.left = 0;
165 sel->r.top = 0;
166 sel->r.width = format->width;
167 sel->r.height = format->height;
168 break;
169
170 default:
171 return -EINVAL;
172 }
173
174 return 0;
175 }
176
177 static int vsp1_rwpf_set_selection(struct v4l2_subdev *subdev,
178 struct v4l2_subdev_pad_config *cfg,
179 struct v4l2_subdev_selection *sel)
180 {
181 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
182 struct v4l2_subdev_pad_config *config;
183 struct v4l2_mbus_framefmt *format;
184 struct v4l2_rect *crop;
185
186 /* Cropping is implemented on the sink pad. */
187 if (sel->pad != RWPF_PAD_SINK)
188 return -EINVAL;
189
190 if (sel->target != V4L2_SEL_TGT_CROP)
191 return -EINVAL;
192
193 config = vsp1_entity_get_pad_config(&rwpf->entity, cfg, sel->which);
194 if (!config)
195 return -EINVAL;
196
197 /* Make sure the crop rectangle is entirely contained in the image. The
198 * WPF top and left offsets are limited to 255.
199 */
200 format = vsp1_entity_get_pad_format(&rwpf->entity, config,
201 RWPF_PAD_SINK);
202
203 /* Restrict the crop rectangle coordinates to multiples of 2 to avoid
204 * shifting the color plane.
205 */
206 if (format->code == MEDIA_BUS_FMT_AYUV8_1X32) {
207 sel->r.left = ALIGN(sel->r.left, 2);
208 sel->r.top = ALIGN(sel->r.top, 2);
209 sel->r.width = round_down(sel->r.width, 2);
210 sel->r.height = round_down(sel->r.height, 2);
211 }
212
213 sel->r.left = min_t(unsigned int, sel->r.left, format->width - 2);
214 sel->r.top = min_t(unsigned int, sel->r.top, format->height - 2);
215 if (rwpf->entity.type == VSP1_ENTITY_WPF) {
216 sel->r.left = min_t(unsigned int, sel->r.left, 255);
217 sel->r.top = min_t(unsigned int, sel->r.top, 255);
218 }
219 sel->r.width = min_t(unsigned int, sel->r.width,
220 format->width - sel->r.left);
221 sel->r.height = min_t(unsigned int, sel->r.height,
222 format->height - sel->r.top);
223
224 crop = vsp1_rwpf_get_crop(rwpf, config);
225 *crop = sel->r;
226
227 /* Propagate the format to the source pad. */
228 format = vsp1_entity_get_pad_format(&rwpf->entity, config,
229 RWPF_PAD_SOURCE);
230 format->width = crop->width;
231 format->height = crop->height;
232
233 return 0;
234 }
235
236 const struct v4l2_subdev_pad_ops vsp1_rwpf_pad_ops = {
237 .init_cfg = vsp1_entity_init_cfg,
238 .enum_mbus_code = vsp1_rwpf_enum_mbus_code,
239 .enum_frame_size = vsp1_rwpf_enum_frame_size,
240 .get_fmt = vsp1_subdev_get_pad_format,
241 .set_fmt = vsp1_rwpf_set_format,
242 .get_selection = vsp1_rwpf_get_selection,
243 .set_selection = vsp1_rwpf_set_selection,
244 };
245
246 /* -----------------------------------------------------------------------------
247 * Controls
248 */
249
250 static int vsp1_rwpf_s_ctrl(struct v4l2_ctrl *ctrl)
251 {
252 struct vsp1_rwpf *rwpf =
253 container_of(ctrl->handler, struct vsp1_rwpf, ctrls);
254
255 switch (ctrl->id) {
256 case V4L2_CID_ALPHA_COMPONENT:
257 rwpf->alpha = ctrl->val;
258 break;
259 }
260
261 return 0;
262 }
263
264 static const struct v4l2_ctrl_ops vsp1_rwpf_ctrl_ops = {
265 .s_ctrl = vsp1_rwpf_s_ctrl,
266 };
267
268 int vsp1_rwpf_init_ctrls(struct vsp1_rwpf *rwpf)
269 {
270 rwpf->alpha = 255;
271
272 v4l2_ctrl_handler_init(&rwpf->ctrls, 1);
273 v4l2_ctrl_new_std(&rwpf->ctrls, &vsp1_rwpf_ctrl_ops,
274 V4L2_CID_ALPHA_COMPONENT, 0, 255, 1, 255);
275
276 rwpf->entity.subdev.ctrl_handler = &rwpf->ctrls;
277
278 return rwpf->ctrls.error;
279 }
This page took 0.038513 seconds and 5 git commands to generate.