blob: 64d649a1bcf5ef36513338ded58a76fa72e3ff81 [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
Laurent Pinchartc6c8efb2015-11-22 13:37:45 -020023struct 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
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030030/* -----------------------------------------------------------------------------
31 * V4L2 Subdevice Pad Operations
32 */
33
Laurent Pinchartc6c8efb2015-11-22 13:37:45 -020034static 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)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030037{
38 static const unsigned int codes[] = {
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030039 MEDIA_BUS_FMT_ARGB8888_1X32,
40 MEDIA_BUS_FMT_AYUV8_1X32,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030041 };
42
43 if (code->index >= ARRAY_SIZE(codes))
44 return -EINVAL;
45
46 code->code = codes[code->index];
47
48 return 0;
49}
50
Laurent Pinchartc6c8efb2015-11-22 13:37:45 -020051static 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)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030054{
55 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
Laurent Pincharte790c3c2015-11-15 19:14:22 -020056 struct v4l2_subdev_pad_config *config;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030057 struct v4l2_mbus_framefmt *format;
58
Laurent Pincharte790c3c2015-11-15 19:14:22 -020059 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);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030064
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
Laurent Pinchartc6c8efb2015-11-22 13:37:45 -020086static int vsp1_rwpf_set_format(struct v4l2_subdev *subdev,
87 struct v4l2_subdev_pad_config *cfg,
88 struct v4l2_subdev_format *fmt)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030089{
90 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
Laurent Pincharte790c3c2015-11-15 19:14:22 -020091 struct v4l2_subdev_pad_config *config;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030092 struct v4l2_mbus_framefmt *format;
Laurent Pincharte5ad37b2013-08-24 20:49:58 -030093 struct v4l2_rect *crop;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030094
Laurent Pincharte790c3c2015-11-15 19:14:22 -020095 config = vsp1_entity_get_pad_config(&rwpf->entity, cfg, fmt->which);
96 if (!config)
97 return -EINVAL;
98
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030099 /* Default to YUV if the requested format is not supported. */
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300100 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;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300103
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200104 format = vsp1_entity_get_pad_format(&rwpf->entity, config, fmt->pad);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300105
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
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300125 /* Update the sink crop rectangle. */
Laurent Pinchartb7e51072015-11-15 19:14:22 -0200126 crop = vsp1_rwpf_get_crop(rwpf, config);
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300127 crop->left = 0;
128 crop->top = 0;
129 crop->width = fmt->format.width;
130 crop->height = fmt->format.height;
131
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300132 /* Propagate the format to the source pad. */
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200133 format = vsp1_entity_get_pad_format(&rwpf->entity, config,
134 RWPF_PAD_SOURCE);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300135 *format = fmt->format;
136
137 return 0;
138}
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300139
Laurent Pinchartc6c8efb2015-11-22 13:37:45 -0200140static int vsp1_rwpf_get_selection(struct v4l2_subdev *subdev,
141 struct v4l2_subdev_pad_config *cfg,
142 struct v4l2_subdev_selection *sel)
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300143{
144 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200145 struct v4l2_subdev_pad_config *config;
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300146 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
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200152 config = vsp1_entity_get_pad_config(&rwpf->entity, cfg, sel->which);
153 if (!config)
154 return -EINVAL;
155
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300156 switch (sel->target) {
157 case V4L2_SEL_TGT_CROP:
Laurent Pinchartb7e51072015-11-15 19:14:22 -0200158 sel->r = *vsp1_rwpf_get_crop(rwpf, config);
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300159 break;
160
161 case V4L2_SEL_TGT_CROP_BOUNDS:
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200162 format = vsp1_entity_get_pad_format(&rwpf->entity, config,
163 RWPF_PAD_SINK);
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300164 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
Laurent Pinchartc6c8efb2015-11-22 13:37:45 -0200177static int vsp1_rwpf_set_selection(struct v4l2_subdev *subdev,
178 struct v4l2_subdev_pad_config *cfg,
179 struct v4l2_subdev_selection *sel)
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300180{
181 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200182 struct v4l2_subdev_pad_config *config;
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300183 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
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200193 config = vsp1_entity_get_pad_config(&rwpf->entity, cfg, sel->which);
194 if (!config)
195 return -EINVAL;
196
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300197 /* Make sure the crop rectangle is entirely contained in the image. The
198 * WPF top and left offsets are limited to 255.
199 */
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200200 format = vsp1_entity_get_pad_format(&rwpf->entity, config,
201 RWPF_PAD_SINK);
Damian Hobson-Garcia85a06382015-05-28 09:59:39 -0300202
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
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300213 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
Laurent Pinchartb7e51072015-11-15 19:14:22 -0200224 crop = vsp1_rwpf_get_crop(rwpf, config);
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300225 *crop = sel->r;
226
227 /* Propagate the format to the source pad. */
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200228 format = vsp1_entity_get_pad_format(&rwpf->entity, config,
229 RWPF_PAD_SOURCE);
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300230 format->width = crop->width;
231 format->height = crop->height;
232
233 return 0;
234}
Laurent Pinchartbd2fdd52015-11-01 12:19:42 -0200235
Laurent Pinchartc6c8efb2015-11-22 13:37:45 -0200236const 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,
Laurent Pinchart3f557222016-02-24 21:10:13 -0300240 .get_fmt = vsp1_subdev_get_pad_format,
Laurent Pinchartc6c8efb2015-11-22 13:37:45 -0200241 .set_fmt = vsp1_rwpf_set_format,
242 .get_selection = vsp1_rwpf_get_selection,
243 .set_selection = vsp1_rwpf_set_selection,
244};
245
Laurent Pinchartbd2fdd52015-11-01 12:19:42 -0200246/* -----------------------------------------------------------------------------
247 * Controls
248 */
249
250static 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
264static const struct v4l2_ctrl_ops vsp1_rwpf_ctrl_ops = {
265 .s_ctrl = vsp1_rwpf_s_ctrl,
266};
267
268int 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}