blob: 0c5ad023adfbf677480dc211a0889c2c80ebb193 [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 Pinchartb7e51072015-11-15 19:14:22 -020079struct v4l2_rect *vsp1_rwpf_get_crop(struct vsp1_rwpf *rwpf,
80 struct v4l2_subdev_pad_config *config)
Laurent Pincharte5ad37b2013-08-24 20:49:58 -030081{
Laurent Pinchartb7e51072015-11-15 19:14:22 -020082 return v4l2_subdev_get_try_crop(&rwpf->entity.subdev, config,
83 RWPF_PAD_SINK);
Laurent Pincharte5ad37b2013-08-24 20:49:58 -030084}
85
Laurent Pinchart1bd0a1b2015-11-01 15:18:32 -020086int vsp1_rwpf_get_format(struct v4l2_subdev *subdev,
87 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030088 struct v4l2_subdev_format *fmt)
89{
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
Laurent Pincharte790c3c2015-11-15 19:14:22 -020093 config = vsp1_entity_get_pad_config(&rwpf->entity, cfg, fmt->which);
94 if (!config)
95 return -EINVAL;
96
97 fmt->format = *vsp1_entity_get_pad_format(&rwpf->entity, config,
98 fmt->pad);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030099
100 return 0;
101}
102
Laurent Pinchart1bd0a1b2015-11-01 15:18:32 -0200103int vsp1_rwpf_set_format(struct v4l2_subdev *subdev,
104 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300105 struct v4l2_subdev_format *fmt)
106{
107 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200108 struct v4l2_subdev_pad_config *config;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300109 struct v4l2_mbus_framefmt *format;
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300110 struct v4l2_rect *crop;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300111
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200112 config = vsp1_entity_get_pad_config(&rwpf->entity, cfg, fmt->which);
113 if (!config)
114 return -EINVAL;
115
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300116 /* Default to YUV if the requested format is not supported. */
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300117 if (fmt->format.code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
118 fmt->format.code != MEDIA_BUS_FMT_AYUV8_1X32)
119 fmt->format.code = MEDIA_BUS_FMT_AYUV8_1X32;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300120
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200121 format = vsp1_entity_get_pad_format(&rwpf->entity, config, fmt->pad);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300122
123 if (fmt->pad == RWPF_PAD_SOURCE) {
124 /* The RWPF performs format conversion but can't scale, only the
125 * format code can be changed on the source pad.
126 */
127 format->code = fmt->format.code;
128 fmt->format = *format;
129 return 0;
130 }
131
132 format->code = fmt->format.code;
133 format->width = clamp_t(unsigned int, fmt->format.width,
134 RWPF_MIN_WIDTH, rwpf->max_width);
135 format->height = clamp_t(unsigned int, fmt->format.height,
136 RWPF_MIN_HEIGHT, rwpf->max_height);
137 format->field = V4L2_FIELD_NONE;
138 format->colorspace = V4L2_COLORSPACE_SRGB;
139
140 fmt->format = *format;
141
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300142 /* Update the sink crop rectangle. */
Laurent Pinchartb7e51072015-11-15 19:14:22 -0200143 crop = vsp1_rwpf_get_crop(rwpf, config);
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300144 crop->left = 0;
145 crop->top = 0;
146 crop->width = fmt->format.width;
147 crop->height = fmt->format.height;
148
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300149 /* Propagate the format to the source pad. */
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200150 format = vsp1_entity_get_pad_format(&rwpf->entity, config,
151 RWPF_PAD_SOURCE);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300152 *format = fmt->format;
153
154 return 0;
155}
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300156
157int vsp1_rwpf_get_selection(struct v4l2_subdev *subdev,
Hans Verkuilf7234132015-03-04 01:47:54 -0800158 struct v4l2_subdev_pad_config *cfg,
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300159 struct v4l2_subdev_selection *sel)
160{
161 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200162 struct v4l2_subdev_pad_config *config;
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300163 struct v4l2_mbus_framefmt *format;
164
165 /* Cropping is implemented on the sink pad. */
166 if (sel->pad != RWPF_PAD_SINK)
167 return -EINVAL;
168
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200169 config = vsp1_entity_get_pad_config(&rwpf->entity, cfg, sel->which);
170 if (!config)
171 return -EINVAL;
172
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300173 switch (sel->target) {
174 case V4L2_SEL_TGT_CROP:
Laurent Pinchartb7e51072015-11-15 19:14:22 -0200175 sel->r = *vsp1_rwpf_get_crop(rwpf, config);
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300176 break;
177
178 case V4L2_SEL_TGT_CROP_BOUNDS:
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200179 format = vsp1_entity_get_pad_format(&rwpf->entity, config,
180 RWPF_PAD_SINK);
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300181 sel->r.left = 0;
182 sel->r.top = 0;
183 sel->r.width = format->width;
184 sel->r.height = format->height;
185 break;
186
187 default:
188 return -EINVAL;
189 }
190
191 return 0;
192}
193
194int vsp1_rwpf_set_selection(struct v4l2_subdev *subdev,
Hans Verkuilf7234132015-03-04 01:47:54 -0800195 struct v4l2_subdev_pad_config *cfg,
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300196 struct v4l2_subdev_selection *sel)
197{
198 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200199 struct v4l2_subdev_pad_config *config;
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300200 struct v4l2_mbus_framefmt *format;
201 struct v4l2_rect *crop;
202
203 /* Cropping is implemented on the sink pad. */
204 if (sel->pad != RWPF_PAD_SINK)
205 return -EINVAL;
206
207 if (sel->target != V4L2_SEL_TGT_CROP)
208 return -EINVAL;
209
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200210 config = vsp1_entity_get_pad_config(&rwpf->entity, cfg, sel->which);
211 if (!config)
212 return -EINVAL;
213
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300214 /* Make sure the crop rectangle is entirely contained in the image. The
215 * WPF top and left offsets are limited to 255.
216 */
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200217 format = vsp1_entity_get_pad_format(&rwpf->entity, config,
218 RWPF_PAD_SINK);
Damian Hobson-Garcia85a06382015-05-28 09:59:39 -0300219
220 /* Restrict the crop rectangle coordinates to multiples of 2 to avoid
221 * shifting the color plane.
222 */
223 if (format->code == MEDIA_BUS_FMT_AYUV8_1X32) {
224 sel->r.left = ALIGN(sel->r.left, 2);
225 sel->r.top = ALIGN(sel->r.top, 2);
226 sel->r.width = round_down(sel->r.width, 2);
227 sel->r.height = round_down(sel->r.height, 2);
228 }
229
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300230 sel->r.left = min_t(unsigned int, sel->r.left, format->width - 2);
231 sel->r.top = min_t(unsigned int, sel->r.top, format->height - 2);
232 if (rwpf->entity.type == VSP1_ENTITY_WPF) {
233 sel->r.left = min_t(unsigned int, sel->r.left, 255);
234 sel->r.top = min_t(unsigned int, sel->r.top, 255);
235 }
236 sel->r.width = min_t(unsigned int, sel->r.width,
237 format->width - sel->r.left);
238 sel->r.height = min_t(unsigned int, sel->r.height,
239 format->height - sel->r.top);
240
Laurent Pinchartb7e51072015-11-15 19:14:22 -0200241 crop = vsp1_rwpf_get_crop(rwpf, config);
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300242 *crop = sel->r;
243
244 /* Propagate the format to the source pad. */
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200245 format = vsp1_entity_get_pad_format(&rwpf->entity, config,
246 RWPF_PAD_SOURCE);
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300247 format->width = crop->width;
248 format->height = crop->height;
249
250 return 0;
251}
Laurent Pinchartbd2fdd52015-11-01 12:19:42 -0200252
253/* -----------------------------------------------------------------------------
254 * Controls
255 */
256
257static int vsp1_rwpf_s_ctrl(struct v4l2_ctrl *ctrl)
258{
259 struct vsp1_rwpf *rwpf =
260 container_of(ctrl->handler, struct vsp1_rwpf, ctrls);
261
262 switch (ctrl->id) {
263 case V4L2_CID_ALPHA_COMPONENT:
264 rwpf->alpha = ctrl->val;
265 break;
266 }
267
268 return 0;
269}
270
271static const struct v4l2_ctrl_ops vsp1_rwpf_ctrl_ops = {
272 .s_ctrl = vsp1_rwpf_s_ctrl,
273};
274
275int vsp1_rwpf_init_ctrls(struct vsp1_rwpf *rwpf)
276{
277 rwpf->alpha = 255;
278
279 v4l2_ctrl_handler_init(&rwpf->ctrls, 1);
280 v4l2_ctrl_new_std(&rwpf->ctrls, &vsp1_rwpf_ctrl_ops,
281 V4L2_CID_ALPHA_COMPONENT, 0, 255, 1, 255);
282
283 rwpf->entity.subdev.ctrl_handler = &rwpf->ctrls;
284
285 return rwpf->ctrls.error;
286}