blob: 38893ab06cd9aa8615bf86e5863208e768e3349d [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);
49 struct v4l2_mbus_framefmt *format;
50
Hans Verkuil5778e742015-03-04 01:47:58 -080051 format = vsp1_entity_get_pad_format(&rwpf->entity, cfg, fse->pad,
52 fse->which);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030053
54 if (fse->index || fse->code != format->code)
55 return -EINVAL;
56
57 if (fse->pad == RWPF_PAD_SINK) {
58 fse->min_width = RWPF_MIN_WIDTH;
59 fse->max_width = rwpf->max_width;
60 fse->min_height = RWPF_MIN_HEIGHT;
61 fse->max_height = rwpf->max_height;
62 } else {
63 /* The size on the source pad are fixed and always identical to
64 * the size on the sink pad.
65 */
66 fse->min_width = format->width;
67 fse->max_width = format->width;
68 fse->min_height = format->height;
69 fse->max_height = format->height;
70 }
71
72 return 0;
73}
74
Laurent Pincharte5ad37b2013-08-24 20:49:58 -030075static struct v4l2_rect *
Laurent Pinchart1bd0a1b2015-11-01 15:18:32 -020076vsp1_rwpf_get_crop(struct vsp1_rwpf *rwpf, struct v4l2_subdev_pad_config *cfg,
77 u32 which)
Laurent Pincharte5ad37b2013-08-24 20:49:58 -030078{
79 switch (which) {
80 case V4L2_SUBDEV_FORMAT_TRY:
Laurent Pinchart1bd0a1b2015-11-01 15:18:32 -020081 return v4l2_subdev_get_try_crop(&rwpf->entity.subdev, cfg,
82 RWPF_PAD_SINK);
Laurent Pincharte5ad37b2013-08-24 20:49:58 -030083 case V4L2_SUBDEV_FORMAT_ACTIVE:
84 return &rwpf->crop;
85 default:
86 return NULL;
87 }
88}
89
Laurent Pinchart1bd0a1b2015-11-01 15:18:32 -020090int vsp1_rwpf_get_format(struct v4l2_subdev *subdev,
91 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030092 struct v4l2_subdev_format *fmt)
93{
94 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
95
Hans Verkuilf7234132015-03-04 01:47:54 -080096 fmt->format = *vsp1_entity_get_pad_format(&rwpf->entity, cfg, fmt->pad,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030097 fmt->which);
98
99 return 0;
100}
101
Laurent Pinchart1bd0a1b2015-11-01 15:18:32 -0200102int vsp1_rwpf_set_format(struct v4l2_subdev *subdev,
103 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300104 struct v4l2_subdev_format *fmt)
105{
106 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
107 struct v4l2_mbus_framefmt *format;
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300108 struct v4l2_rect *crop;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300109
110 /* Default to YUV if the requested format is not supported. */
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300111 if (fmt->format.code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
112 fmt->format.code != MEDIA_BUS_FMT_AYUV8_1X32)
113 fmt->format.code = MEDIA_BUS_FMT_AYUV8_1X32;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300114
Hans Verkuilf7234132015-03-04 01:47:54 -0800115 format = vsp1_entity_get_pad_format(&rwpf->entity, cfg, fmt->pad,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300116 fmt->which);
117
118 if (fmt->pad == RWPF_PAD_SOURCE) {
119 /* The RWPF performs format conversion but can't scale, only the
120 * format code can be changed on the source pad.
121 */
122 format->code = fmt->format.code;
123 fmt->format = *format;
124 return 0;
125 }
126
127 format->code = fmt->format.code;
128 format->width = clamp_t(unsigned int, fmt->format.width,
129 RWPF_MIN_WIDTH, rwpf->max_width);
130 format->height = clamp_t(unsigned int, fmt->format.height,
131 RWPF_MIN_HEIGHT, rwpf->max_height);
132 format->field = V4L2_FIELD_NONE;
133 format->colorspace = V4L2_COLORSPACE_SRGB;
134
135 fmt->format = *format;
136
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300137 /* Update the sink crop rectangle. */
Hans Verkuilf7234132015-03-04 01:47:54 -0800138 crop = vsp1_rwpf_get_crop(rwpf, cfg, fmt->which);
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300139 crop->left = 0;
140 crop->top = 0;
141 crop->width = fmt->format.width;
142 crop->height = fmt->format.height;
143
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300144 /* Propagate the format to the source pad. */
Hans Verkuilf7234132015-03-04 01:47:54 -0800145 format = vsp1_entity_get_pad_format(&rwpf->entity, cfg, RWPF_PAD_SOURCE,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300146 fmt->which);
147 *format = fmt->format;
148
149 return 0;
150}
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300151
152int vsp1_rwpf_get_selection(struct v4l2_subdev *subdev,
Hans Verkuilf7234132015-03-04 01:47:54 -0800153 struct v4l2_subdev_pad_config *cfg,
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300154 struct v4l2_subdev_selection *sel)
155{
156 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
157 struct v4l2_mbus_framefmt *format;
158
159 /* Cropping is implemented on the sink pad. */
160 if (sel->pad != RWPF_PAD_SINK)
161 return -EINVAL;
162
163 switch (sel->target) {
164 case V4L2_SEL_TGT_CROP:
Hans Verkuilf7234132015-03-04 01:47:54 -0800165 sel->r = *vsp1_rwpf_get_crop(rwpf, cfg, sel->which);
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300166 break;
167
168 case V4L2_SEL_TGT_CROP_BOUNDS:
Hans Verkuilf7234132015-03-04 01:47:54 -0800169 format = vsp1_entity_get_pad_format(&rwpf->entity, cfg,
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300170 RWPF_PAD_SINK, sel->which);
171 sel->r.left = 0;
172 sel->r.top = 0;
173 sel->r.width = format->width;
174 sel->r.height = format->height;
175 break;
176
177 default:
178 return -EINVAL;
179 }
180
181 return 0;
182}
183
184int vsp1_rwpf_set_selection(struct v4l2_subdev *subdev,
Hans Verkuilf7234132015-03-04 01:47:54 -0800185 struct v4l2_subdev_pad_config *cfg,
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300186 struct v4l2_subdev_selection *sel)
187{
188 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
189 struct v4l2_mbus_framefmt *format;
190 struct v4l2_rect *crop;
191
192 /* Cropping is implemented on the sink pad. */
193 if (sel->pad != RWPF_PAD_SINK)
194 return -EINVAL;
195
196 if (sel->target != V4L2_SEL_TGT_CROP)
197 return -EINVAL;
198
199 /* Make sure the crop rectangle is entirely contained in the image. The
200 * WPF top and left offsets are limited to 255.
201 */
Hans Verkuilf7234132015-03-04 01:47:54 -0800202 format = vsp1_entity_get_pad_format(&rwpf->entity, cfg, RWPF_PAD_SINK,
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300203 sel->which);
Damian Hobson-Garcia85a06382015-05-28 09:59:39 -0300204
205 /* Restrict the crop rectangle coordinates to multiples of 2 to avoid
206 * shifting the color plane.
207 */
208 if (format->code == MEDIA_BUS_FMT_AYUV8_1X32) {
209 sel->r.left = ALIGN(sel->r.left, 2);
210 sel->r.top = ALIGN(sel->r.top, 2);
211 sel->r.width = round_down(sel->r.width, 2);
212 sel->r.height = round_down(sel->r.height, 2);
213 }
214
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300215 sel->r.left = min_t(unsigned int, sel->r.left, format->width - 2);
216 sel->r.top = min_t(unsigned int, sel->r.top, format->height - 2);
217 if (rwpf->entity.type == VSP1_ENTITY_WPF) {
218 sel->r.left = min_t(unsigned int, sel->r.left, 255);
219 sel->r.top = min_t(unsigned int, sel->r.top, 255);
220 }
221 sel->r.width = min_t(unsigned int, sel->r.width,
222 format->width - sel->r.left);
223 sel->r.height = min_t(unsigned int, sel->r.height,
224 format->height - sel->r.top);
225
Hans Verkuilf7234132015-03-04 01:47:54 -0800226 crop = vsp1_rwpf_get_crop(rwpf, cfg, sel->which);
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300227 *crop = sel->r;
228
229 /* Propagate the format to the source pad. */
Hans Verkuilf7234132015-03-04 01:47:54 -0800230 format = vsp1_entity_get_pad_format(&rwpf->entity, cfg, RWPF_PAD_SOURCE,
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300231 sel->which);
232 format->width = crop->width;
233 format->height = crop->height;
234
235 return 0;
236}
Laurent Pinchartbd2fdd52015-11-01 12:19:42 -0200237
238/* -----------------------------------------------------------------------------
239 * Controls
240 */
241
242static int vsp1_rwpf_s_ctrl(struct v4l2_ctrl *ctrl)
243{
244 struct vsp1_rwpf *rwpf =
245 container_of(ctrl->handler, struct vsp1_rwpf, ctrls);
246
247 switch (ctrl->id) {
248 case V4L2_CID_ALPHA_COMPONENT:
249 rwpf->alpha = ctrl->val;
250 break;
251 }
252
253 return 0;
254}
255
256static const struct v4l2_ctrl_ops vsp1_rwpf_ctrl_ops = {
257 .s_ctrl = vsp1_rwpf_s_ctrl,
258};
259
260int vsp1_rwpf_init_ctrls(struct vsp1_rwpf *rwpf)
261{
262 rwpf->alpha = 255;
263
264 v4l2_ctrl_handler_init(&rwpf->ctrls, 1);
265 v4l2_ctrl_new_std(&rwpf->ctrls, &vsp1_rwpf_ctrl_ops,
266 V4L2_CID_ALPHA_COMPONENT, 0, 255, 1, 255);
267
268 rwpf->entity.subdev.ctrl_handler = &rwpf->ctrls;
269
270 return rwpf->ctrls.error;
271}