blob: e5216d39723e9837b948a9546671a2f6b19903b4 [file] [log] [blame]
Laurent Pinchart26e0ca22013-06-04 11:22:30 -03001/*
2 * vsp1_rwpf.c -- R-Car VSP1 Read and Write Pixel Formatters
3 *
Laurent Pinchart8a1edc52014-02-06 14:42:31 -03004 * Copyright (C) 2013-2014 Renesas Electronics Corporation
Laurent Pinchart26e0ca22013-06-04 11:22:30 -03005 *
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/* -----------------------------------------------------------------------------
24 * V4L2 Subdevice Pad Operations
25 */
26
27int vsp1_rwpf_enum_mbus_code(struct v4l2_subdev *subdev,
Hans Verkuilf7234132015-03-04 01:47:54 -080028 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030029 struct v4l2_subdev_mbus_code_enum *code)
30{
31 static const unsigned int codes[] = {
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030032 MEDIA_BUS_FMT_ARGB8888_1X32,
33 MEDIA_BUS_FMT_AYUV8_1X32,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030034 };
35
36 if (code->index >= ARRAY_SIZE(codes))
37 return -EINVAL;
38
39 code->code = codes[code->index];
40
41 return 0;
42}
43
44int vsp1_rwpf_enum_frame_size(struct v4l2_subdev *subdev,
Hans Verkuilf7234132015-03-04 01:47:54 -080045 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030046 struct v4l2_subdev_frame_size_enum *fse)
47{
48 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
Laurent Pincharte790c3c2015-11-15 19:14:22 -020049 struct v4l2_subdev_pad_config *config;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030050 struct v4l2_mbus_framefmt *format;
51
Laurent Pincharte790c3c2015-11-15 19:14:22 -020052 config = vsp1_entity_get_pad_config(&rwpf->entity, cfg, fse->which);
53 if (!config)
54 return -EINVAL;
55
56 format = vsp1_entity_get_pad_format(&rwpf->entity, config, fse->pad);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030057
58 if (fse->index || fse->code != format->code)
59 return -EINVAL;
60
61 if (fse->pad == RWPF_PAD_SINK) {
62 fse->min_width = RWPF_MIN_WIDTH;
63 fse->max_width = rwpf->max_width;
64 fse->min_height = RWPF_MIN_HEIGHT;
65 fse->max_height = rwpf->max_height;
66 } else {
67 /* The size on the source pad are fixed and always identical to
68 * the size on the sink pad.
69 */
70 fse->min_width = format->width;
71 fse->max_width = format->width;
72 fse->min_height = format->height;
73 fse->max_height = format->height;
74 }
75
76 return 0;
77}
78
Laurent Pincharte5ad37b2013-08-24 20:49:58 -030079static struct v4l2_rect *
Laurent Pinchart1bd0a1b2015-11-01 15:18:32 -020080vsp1_rwpf_get_crop(struct vsp1_rwpf *rwpf, struct v4l2_subdev_pad_config *cfg,
81 u32 which)
Laurent Pincharte5ad37b2013-08-24 20:49:58 -030082{
83 switch (which) {
84 case V4L2_SUBDEV_FORMAT_TRY:
Laurent Pinchart1bd0a1b2015-11-01 15:18:32 -020085 return v4l2_subdev_get_try_crop(&rwpf->entity.subdev, cfg,
86 RWPF_PAD_SINK);
Laurent Pincharte5ad37b2013-08-24 20:49:58 -030087 case V4L2_SUBDEV_FORMAT_ACTIVE:
88 return &rwpf->crop;
89 default:
90 return NULL;
91 }
92}
93
Laurent Pinchart1bd0a1b2015-11-01 15:18:32 -020094int vsp1_rwpf_get_format(struct v4l2_subdev *subdev,
95 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030096 struct v4l2_subdev_format *fmt)
97{
98 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
Laurent Pincharte790c3c2015-11-15 19:14:22 -020099 struct v4l2_subdev_pad_config *config;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300100
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200101 config = vsp1_entity_get_pad_config(&rwpf->entity, cfg, fmt->which);
102 if (!config)
103 return -EINVAL;
104
105 fmt->format = *vsp1_entity_get_pad_format(&rwpf->entity, config,
106 fmt->pad);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300107
108 return 0;
109}
110
Laurent Pinchart1bd0a1b2015-11-01 15:18:32 -0200111int vsp1_rwpf_set_format(struct v4l2_subdev *subdev,
112 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300113 struct v4l2_subdev_format *fmt)
114{
115 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200116 struct v4l2_subdev_pad_config *config;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300117 struct v4l2_mbus_framefmt *format;
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300118 struct v4l2_rect *crop;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300119
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200120 config = vsp1_entity_get_pad_config(&rwpf->entity, cfg, fmt->which);
121 if (!config)
122 return -EINVAL;
123
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300124 /* Default to YUV if the requested format is not supported. */
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300125 if (fmt->format.code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
126 fmt->format.code != MEDIA_BUS_FMT_AYUV8_1X32)
127 fmt->format.code = MEDIA_BUS_FMT_AYUV8_1X32;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300128
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200129 format = vsp1_entity_get_pad_format(&rwpf->entity, config, fmt->pad);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300130
131 if (fmt->pad == RWPF_PAD_SOURCE) {
132 /* The RWPF performs format conversion but can't scale, only the
133 * format code can be changed on the source pad.
134 */
135 format->code = fmt->format.code;
136 fmt->format = *format;
137 return 0;
138 }
139
140 format->code = fmt->format.code;
141 format->width = clamp_t(unsigned int, fmt->format.width,
142 RWPF_MIN_WIDTH, rwpf->max_width);
143 format->height = clamp_t(unsigned int, fmt->format.height,
144 RWPF_MIN_HEIGHT, rwpf->max_height);
145 format->field = V4L2_FIELD_NONE;
146 format->colorspace = V4L2_COLORSPACE_SRGB;
147
148 fmt->format = *format;
149
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300150 /* Update the sink crop rectangle. */
Hans Verkuilf7234132015-03-04 01:47:54 -0800151 crop = vsp1_rwpf_get_crop(rwpf, cfg, fmt->which);
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300152 crop->left = 0;
153 crop->top = 0;
154 crop->width = fmt->format.width;
155 crop->height = fmt->format.height;
156
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300157 /* Propagate the format to the source pad. */
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200158 format = vsp1_entity_get_pad_format(&rwpf->entity, config,
159 RWPF_PAD_SOURCE);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300160 *format = fmt->format;
161
162 return 0;
163}
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300164
165int vsp1_rwpf_get_selection(struct v4l2_subdev *subdev,
Hans Verkuilf7234132015-03-04 01:47:54 -0800166 struct v4l2_subdev_pad_config *cfg,
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300167 struct v4l2_subdev_selection *sel)
168{
169 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200170 struct v4l2_subdev_pad_config *config;
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300171 struct v4l2_mbus_framefmt *format;
172
173 /* Cropping is implemented on the sink pad. */
174 if (sel->pad != RWPF_PAD_SINK)
175 return -EINVAL;
176
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200177 config = vsp1_entity_get_pad_config(&rwpf->entity, cfg, sel->which);
178 if (!config)
179 return -EINVAL;
180
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300181 switch (sel->target) {
182 case V4L2_SEL_TGT_CROP:
Hans Verkuilf7234132015-03-04 01:47:54 -0800183 sel->r = *vsp1_rwpf_get_crop(rwpf, cfg, sel->which);
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300184 break;
185
186 case V4L2_SEL_TGT_CROP_BOUNDS:
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200187 format = vsp1_entity_get_pad_format(&rwpf->entity, config,
188 RWPF_PAD_SINK);
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300189 sel->r.left = 0;
190 sel->r.top = 0;
191 sel->r.width = format->width;
192 sel->r.height = format->height;
193 break;
194
195 default:
196 return -EINVAL;
197 }
198
199 return 0;
200}
201
202int vsp1_rwpf_set_selection(struct v4l2_subdev *subdev,
Hans Verkuilf7234132015-03-04 01:47:54 -0800203 struct v4l2_subdev_pad_config *cfg,
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300204 struct v4l2_subdev_selection *sel)
205{
206 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200207 struct v4l2_subdev_pad_config *config;
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300208 struct v4l2_mbus_framefmt *format;
209 struct v4l2_rect *crop;
210
211 /* Cropping is implemented on the sink pad. */
212 if (sel->pad != RWPF_PAD_SINK)
213 return -EINVAL;
214
215 if (sel->target != V4L2_SEL_TGT_CROP)
216 return -EINVAL;
217
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200218 config = vsp1_entity_get_pad_config(&rwpf->entity, cfg, sel->which);
219 if (!config)
220 return -EINVAL;
221
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300222 /* Make sure the crop rectangle is entirely contained in the image. The
223 * WPF top and left offsets are limited to 255.
224 */
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200225 format = vsp1_entity_get_pad_format(&rwpf->entity, config,
226 RWPF_PAD_SINK);
Damian Hobson-Garcia85a06382015-05-28 09:59:39 -0300227
228 /* Restrict the crop rectangle coordinates to multiples of 2 to avoid
229 * shifting the color plane.
230 */
231 if (format->code == MEDIA_BUS_FMT_AYUV8_1X32) {
232 sel->r.left = ALIGN(sel->r.left, 2);
233 sel->r.top = ALIGN(sel->r.top, 2);
234 sel->r.width = round_down(sel->r.width, 2);
235 sel->r.height = round_down(sel->r.height, 2);
236 }
237
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300238 sel->r.left = min_t(unsigned int, sel->r.left, format->width - 2);
239 sel->r.top = min_t(unsigned int, sel->r.top, format->height - 2);
240 if (rwpf->entity.type == VSP1_ENTITY_WPF) {
241 sel->r.left = min_t(unsigned int, sel->r.left, 255);
242 sel->r.top = min_t(unsigned int, sel->r.top, 255);
243 }
244 sel->r.width = min_t(unsigned int, sel->r.width,
245 format->width - sel->r.left);
246 sel->r.height = min_t(unsigned int, sel->r.height,
247 format->height - sel->r.top);
248
Hans Verkuilf7234132015-03-04 01:47:54 -0800249 crop = vsp1_rwpf_get_crop(rwpf, cfg, sel->which);
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300250 *crop = sel->r;
251
252 /* Propagate the format to the source pad. */
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200253 format = vsp1_entity_get_pad_format(&rwpf->entity, config,
254 RWPF_PAD_SOURCE);
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300255 format->width = crop->width;
256 format->height = crop->height;
257
258 return 0;
259}
Laurent Pinchartbd2fdd52015-11-01 12:19:42 -0200260
261/* -----------------------------------------------------------------------------
262 * Controls
263 */
264
265static int vsp1_rwpf_s_ctrl(struct v4l2_ctrl *ctrl)
266{
267 struct vsp1_rwpf *rwpf =
268 container_of(ctrl->handler, struct vsp1_rwpf, ctrls);
269
270 switch (ctrl->id) {
271 case V4L2_CID_ALPHA_COMPONENT:
272 rwpf->alpha = ctrl->val;
273 break;
274 }
275
276 return 0;
277}
278
279static const struct v4l2_ctrl_ops vsp1_rwpf_ctrl_ops = {
280 .s_ctrl = vsp1_rwpf_s_ctrl,
281};
282
283int vsp1_rwpf_init_ctrls(struct vsp1_rwpf *rwpf)
284{
285 rwpf->alpha = 255;
286
287 v4l2_ctrl_handler_init(&rwpf->ctrls, 1);
288 v4l2_ctrl_new_std(&rwpf->ctrls, &vsp1_rwpf_ctrl_ops,
289 V4L2_CID_ALPHA_COMPONENT, 0, 255, 1, 255);
290
291 rwpf->entity.subdev.ctrl_handler = &rwpf->ctrls;
292
293 return rwpf->ctrls.error;
294}