blob: a3ace8df7f4d60c0c7556ccd37019a54e8a0e0ab [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 Pinchart26e0ca22013-06-04 11:22:30 -030056
Laurent Pinchart076e8342016-02-24 20:25:42 -030057 return vsp1_subdev_enum_frame_size(subdev, cfg, fse, RWPF_MIN_WIDTH,
58 RWPF_MIN_HEIGHT, rwpf->max_width,
59 rwpf->max_height);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030060}
61
Laurent Pinchartc6c8efb2015-11-22 13:37:45 -020062static int vsp1_rwpf_set_format(struct v4l2_subdev *subdev,
63 struct v4l2_subdev_pad_config *cfg,
64 struct v4l2_subdev_format *fmt)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030065{
66 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
Laurent Pincharte790c3c2015-11-15 19:14:22 -020067 struct v4l2_subdev_pad_config *config;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030068 struct v4l2_mbus_framefmt *format;
Laurent Pinchart34e77ed2016-06-26 08:09:31 -030069 int ret = 0;
70
71 mutex_lock(&rwpf->entity.lock);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030072
Laurent Pincharte790c3c2015-11-15 19:14:22 -020073 config = vsp1_entity_get_pad_config(&rwpf->entity, cfg, fmt->which);
Laurent Pinchart34e77ed2016-06-26 08:09:31 -030074 if (!config) {
75 ret = -EINVAL;
76 goto done;
77 }
Laurent Pincharte790c3c2015-11-15 19:14:22 -020078
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030079 /* Default to YUV if the requested format is not supported. */
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030080 if (fmt->format.code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
81 fmt->format.code != MEDIA_BUS_FMT_AYUV8_1X32)
82 fmt->format.code = MEDIA_BUS_FMT_AYUV8_1X32;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030083
Laurent Pincharte790c3c2015-11-15 19:14:22 -020084 format = vsp1_entity_get_pad_format(&rwpf->entity, config, fmt->pad);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030085
86 if (fmt->pad == RWPF_PAD_SOURCE) {
87 /* The RWPF performs format conversion but can't scale, only the
88 * format code can be changed on the source pad.
89 */
90 format->code = fmt->format.code;
91 fmt->format = *format;
Laurent Pinchart34e77ed2016-06-26 08:09:31 -030092 goto done;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030093 }
94
95 format->code = fmt->format.code;
96 format->width = clamp_t(unsigned int, fmt->format.width,
97 RWPF_MIN_WIDTH, rwpf->max_width);
98 format->height = clamp_t(unsigned int, fmt->format.height,
99 RWPF_MIN_HEIGHT, rwpf->max_height);
100 format->field = V4L2_FIELD_NONE;
101 format->colorspace = V4L2_COLORSPACE_SRGB;
102
103 fmt->format = *format;
104
Laurent Pinchartb61bead2016-09-11 22:41:06 -0300105 if (rwpf->entity.type == VSP1_ENTITY_RPF) {
106 struct v4l2_rect *crop;
107
108 /* Update the sink crop rectangle. */
109 crop = vsp1_rwpf_get_crop(rwpf, config);
110 crop->left = 0;
111 crop->top = 0;
112 crop->width = fmt->format.width;
113 crop->height = fmt->format.height;
114 }
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300115
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300116 /* Propagate the format to the source pad. */
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200117 format = vsp1_entity_get_pad_format(&rwpf->entity, config,
118 RWPF_PAD_SOURCE);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300119 *format = fmt->format;
120
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300121done:
122 mutex_unlock(&rwpf->entity.lock);
123 return ret;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300124}
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300125
Laurent Pinchartc6c8efb2015-11-22 13:37:45 -0200126static int vsp1_rwpf_get_selection(struct v4l2_subdev *subdev,
127 struct v4l2_subdev_pad_config *cfg,
128 struct v4l2_subdev_selection *sel)
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300129{
130 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200131 struct v4l2_subdev_pad_config *config;
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300132 struct v4l2_mbus_framefmt *format;
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300133 int ret = 0;
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300134
Laurent Pinchartb61bead2016-09-11 22:41:06 -0300135 /* Cropping is only supported on the RPF and is implemented on the sink
136 * pad.
137 */
138 if (rwpf->entity.type == VSP1_ENTITY_WPF || sel->pad != RWPF_PAD_SINK)
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300139 return -EINVAL;
140
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300141 mutex_lock(&rwpf->entity.lock);
142
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200143 config = vsp1_entity_get_pad_config(&rwpf->entity, cfg, sel->which);
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300144 if (!config) {
145 ret = -EINVAL;
146 goto done;
147 }
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200148
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300149 switch (sel->target) {
150 case V4L2_SEL_TGT_CROP:
Laurent Pinchartb7e51072015-11-15 19:14:22 -0200151 sel->r = *vsp1_rwpf_get_crop(rwpf, config);
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300152 break;
153
154 case V4L2_SEL_TGT_CROP_BOUNDS:
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200155 format = vsp1_entity_get_pad_format(&rwpf->entity, config,
156 RWPF_PAD_SINK);
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300157 sel->r.left = 0;
158 sel->r.top = 0;
159 sel->r.width = format->width;
160 sel->r.height = format->height;
161 break;
162
163 default:
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300164 ret = -EINVAL;
165 break;
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300166 }
167
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300168done:
169 mutex_unlock(&rwpf->entity.lock);
170 return ret;
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300171}
172
Laurent Pinchartc6c8efb2015-11-22 13:37:45 -0200173static int vsp1_rwpf_set_selection(struct v4l2_subdev *subdev,
174 struct v4l2_subdev_pad_config *cfg,
175 struct v4l2_subdev_selection *sel)
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300176{
177 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200178 struct v4l2_subdev_pad_config *config;
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300179 struct v4l2_mbus_framefmt *format;
180 struct v4l2_rect *crop;
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300181 int ret = 0;
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300182
Laurent Pinchartb61bead2016-09-11 22:41:06 -0300183 /* Cropping is only supported on the RPF and is implemented on the sink
184 * pad.
185 */
186 if (rwpf->entity.type == VSP1_ENTITY_WPF || sel->pad != RWPF_PAD_SINK)
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300187 return -EINVAL;
188
189 if (sel->target != V4L2_SEL_TGT_CROP)
190 return -EINVAL;
191
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300192 mutex_lock(&rwpf->entity.lock);
193
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200194 config = vsp1_entity_get_pad_config(&rwpf->entity, cfg, sel->which);
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300195 if (!config) {
196 ret = -EINVAL;
197 goto done;
198 }
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200199
Laurent Pinchartb61bead2016-09-11 22:41:06 -0300200 /* Make sure the crop rectangle is entirely contained in the image. */
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200201 format = vsp1_entity_get_pad_format(&rwpf->entity, config,
202 RWPF_PAD_SINK);
Damian Hobson-Garcia85a06382015-05-28 09:59:39 -0300203
204 /* Restrict the crop rectangle coordinates to multiples of 2 to avoid
205 * shifting the color plane.
206 */
207 if (format->code == MEDIA_BUS_FMT_AYUV8_1X32) {
208 sel->r.left = ALIGN(sel->r.left, 2);
209 sel->r.top = ALIGN(sel->r.top, 2);
210 sel->r.width = round_down(sel->r.width, 2);
211 sel->r.height = round_down(sel->r.height, 2);
212 }
213
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300214 sel->r.left = min_t(unsigned int, sel->r.left, format->width - 2);
215 sel->r.top = min_t(unsigned int, sel->r.top, format->height - 2);
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300216 sel->r.width = min_t(unsigned int, sel->r.width,
217 format->width - sel->r.left);
218 sel->r.height = min_t(unsigned int, sel->r.height,
219 format->height - sel->r.top);
220
Laurent Pinchartb7e51072015-11-15 19:14:22 -0200221 crop = vsp1_rwpf_get_crop(rwpf, config);
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300222 *crop = sel->r;
223
224 /* Propagate the format to the source pad. */
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200225 format = vsp1_entity_get_pad_format(&rwpf->entity, config,
226 RWPF_PAD_SOURCE);
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300227 format->width = crop->width;
228 format->height = crop->height;
229
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300230done:
231 mutex_unlock(&rwpf->entity.lock);
232 return ret;
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300233}
Laurent Pinchartbd2fdd52015-11-01 12:19:42 -0200234
Laurent Pinchartc6c8efb2015-11-22 13:37:45 -0200235const struct v4l2_subdev_pad_ops vsp1_rwpf_pad_ops = {
236 .init_cfg = vsp1_entity_init_cfg,
237 .enum_mbus_code = vsp1_rwpf_enum_mbus_code,
238 .enum_frame_size = vsp1_rwpf_enum_frame_size,
Laurent Pinchart3f557222016-02-24 21:10:13 -0300239 .get_fmt = vsp1_subdev_get_pad_format,
Laurent Pinchartc6c8efb2015-11-22 13:37:45 -0200240 .set_fmt = vsp1_rwpf_set_format,
241 .get_selection = vsp1_rwpf_get_selection,
242 .set_selection = vsp1_rwpf_set_selection,
243};
244
Laurent Pinchartbd2fdd52015-11-01 12:19:42 -0200245/* -----------------------------------------------------------------------------
246 * Controls
247 */
248
249static int vsp1_rwpf_s_ctrl(struct v4l2_ctrl *ctrl)
250{
251 struct vsp1_rwpf *rwpf =
252 container_of(ctrl->handler, struct vsp1_rwpf, ctrls);
253
254 switch (ctrl->id) {
255 case V4L2_CID_ALPHA_COMPONENT:
256 rwpf->alpha = ctrl->val;
257 break;
258 }
259
260 return 0;
261}
262
263static const struct v4l2_ctrl_ops vsp1_rwpf_ctrl_ops = {
264 .s_ctrl = vsp1_rwpf_s_ctrl,
265};
266
Laurent Pinchart894dde52016-05-26 05:14:22 -0300267int vsp1_rwpf_init_ctrls(struct vsp1_rwpf *rwpf, unsigned int ncontrols)
Laurent Pinchartbd2fdd52015-11-01 12:19:42 -0200268{
Laurent Pinchart894dde52016-05-26 05:14:22 -0300269 v4l2_ctrl_handler_init(&rwpf->ctrls, ncontrols + 1);
Laurent Pinchartbd2fdd52015-11-01 12:19:42 -0200270 v4l2_ctrl_new_std(&rwpf->ctrls, &vsp1_rwpf_ctrl_ops,
271 V4L2_CID_ALPHA_COMPONENT, 0, 255, 1, 255);
272
273 rwpf->entity.subdev.ctrl_handler = &rwpf->ctrls;
274
275 return rwpf->ctrls.error;
276}