]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/media/platform/vsp1/vsp1_rwpf.c
04104ef28fb5918cf4e0102ba28c27a1d65f6160
[mirror_ubuntu-bionic-kernel.git] / drivers / media / platform / vsp1 / vsp1_rwpf.c
1 /*
2 * vsp1_rwpf.c -- R-Car VSP1 Read and Write Pixel Formatters
3 *
4 * Copyright (C) 2013-2014 Renesas Electronics Corporation
5 *
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 struct 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
30 /* -----------------------------------------------------------------------------
31 * V4L2 Subdevice Pad Operations
32 */
33
34 static 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)
37 {
38 static const unsigned int codes[] = {
39 MEDIA_BUS_FMT_ARGB8888_1X32,
40 MEDIA_BUS_FMT_AHSV8888_1X32,
41 MEDIA_BUS_FMT_AYUV8_1X32,
42 };
43
44 if (code->index >= ARRAY_SIZE(codes))
45 return -EINVAL;
46
47 code->code = codes[code->index];
48
49 return 0;
50 }
51
52 static int vsp1_rwpf_enum_frame_size(struct v4l2_subdev *subdev,
53 struct v4l2_subdev_pad_config *cfg,
54 struct v4l2_subdev_frame_size_enum *fse)
55 {
56 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
57
58 return vsp1_subdev_enum_frame_size(subdev, cfg, fse, RWPF_MIN_WIDTH,
59 RWPF_MIN_HEIGHT, rwpf->max_width,
60 rwpf->max_height);
61 }
62
63 static int vsp1_rwpf_set_format(struct v4l2_subdev *subdev,
64 struct v4l2_subdev_pad_config *cfg,
65 struct v4l2_subdev_format *fmt)
66 {
67 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
68 struct v4l2_subdev_pad_config *config;
69 struct v4l2_mbus_framefmt *format;
70 int ret = 0;
71
72 mutex_lock(&rwpf->entity.lock);
73
74 config = vsp1_entity_get_pad_config(&rwpf->entity, cfg, fmt->which);
75 if (!config) {
76 ret = -EINVAL;
77 goto done;
78 }
79
80 /* Default to YUV if the requested format is not supported. */
81 if (fmt->format.code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
82 fmt->format.code != MEDIA_BUS_FMT_AHSV8888_1X32 &&
83 fmt->format.code != MEDIA_BUS_FMT_AYUV8_1X32)
84 fmt->format.code = MEDIA_BUS_FMT_AYUV8_1X32;
85
86 format = vsp1_entity_get_pad_format(&rwpf->entity, config, fmt->pad);
87
88 if (fmt->pad == RWPF_PAD_SOURCE) {
89 /* The RWPF performs format conversion but can't scale, only the
90 * format code can be changed on the source pad.
91 */
92 format->code = fmt->format.code;
93 fmt->format = *format;
94 goto done;
95 }
96
97 format->code = fmt->format.code;
98 format->width = clamp_t(unsigned int, fmt->format.width,
99 RWPF_MIN_WIDTH, rwpf->max_width);
100 format->height = clamp_t(unsigned int, fmt->format.height,
101 RWPF_MIN_HEIGHT, rwpf->max_height);
102 format->field = V4L2_FIELD_NONE;
103 format->colorspace = V4L2_COLORSPACE_SRGB;
104
105 fmt->format = *format;
106
107 if (rwpf->entity.type == VSP1_ENTITY_RPF) {
108 struct v4l2_rect *crop;
109
110 /* Update the sink crop rectangle. */
111 crop = vsp1_rwpf_get_crop(rwpf, config);
112 crop->left = 0;
113 crop->top = 0;
114 crop->width = fmt->format.width;
115 crop->height = fmt->format.height;
116 }
117
118 /* Propagate the format to the source pad. */
119 format = vsp1_entity_get_pad_format(&rwpf->entity, config,
120 RWPF_PAD_SOURCE);
121 *format = fmt->format;
122
123 done:
124 mutex_unlock(&rwpf->entity.lock);
125 return ret;
126 }
127
128 static int vsp1_rwpf_get_selection(struct v4l2_subdev *subdev,
129 struct v4l2_subdev_pad_config *cfg,
130 struct v4l2_subdev_selection *sel)
131 {
132 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
133 struct v4l2_subdev_pad_config *config;
134 struct v4l2_mbus_framefmt *format;
135 int ret = 0;
136
137 /*
138 * Cropping is only supported on the RPF and is implemented on the sink
139 * pad.
140 */
141 if (rwpf->entity.type == VSP1_ENTITY_WPF || sel->pad != RWPF_PAD_SINK)
142 return -EINVAL;
143
144 mutex_lock(&rwpf->entity.lock);
145
146 config = vsp1_entity_get_pad_config(&rwpf->entity, cfg, sel->which);
147 if (!config) {
148 ret = -EINVAL;
149 goto done;
150 }
151
152 switch (sel->target) {
153 case V4L2_SEL_TGT_CROP:
154 sel->r = *vsp1_rwpf_get_crop(rwpf, config);
155 break;
156
157 case V4L2_SEL_TGT_CROP_BOUNDS:
158 format = vsp1_entity_get_pad_format(&rwpf->entity, config,
159 RWPF_PAD_SINK);
160 sel->r.left = 0;
161 sel->r.top = 0;
162 sel->r.width = format->width;
163 sel->r.height = format->height;
164 break;
165
166 default:
167 ret = -EINVAL;
168 break;
169 }
170
171 done:
172 mutex_unlock(&rwpf->entity.lock);
173 return ret;
174 }
175
176 static int vsp1_rwpf_set_selection(struct v4l2_subdev *subdev,
177 struct v4l2_subdev_pad_config *cfg,
178 struct v4l2_subdev_selection *sel)
179 {
180 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
181 struct v4l2_subdev_pad_config *config;
182 struct v4l2_mbus_framefmt *format;
183 struct v4l2_rect *crop;
184 int ret = 0;
185
186 /*
187 * Cropping is only supported on the RPF and is implemented on the sink
188 * pad.
189 */
190 if (rwpf->entity.type == VSP1_ENTITY_WPF || sel->pad != RWPF_PAD_SINK)
191 return -EINVAL;
192
193 if (sel->target != V4L2_SEL_TGT_CROP)
194 return -EINVAL;
195
196 mutex_lock(&rwpf->entity.lock);
197
198 config = vsp1_entity_get_pad_config(&rwpf->entity, cfg, sel->which);
199 if (!config) {
200 ret = -EINVAL;
201 goto done;
202 }
203
204 /* Make sure the crop rectangle is entirely contained in the image. */
205 format = vsp1_entity_get_pad_format(&rwpf->entity, config,
206 RWPF_PAD_SINK);
207
208 /* Restrict the crop rectangle coordinates to multiples of 2 to avoid
209 * shifting the color plane.
210 */
211 if (format->code == MEDIA_BUS_FMT_AYUV8_1X32) {
212 sel->r.left = ALIGN(sel->r.left, 2);
213 sel->r.top = ALIGN(sel->r.top, 2);
214 sel->r.width = round_down(sel->r.width, 2);
215 sel->r.height = round_down(sel->r.height, 2);
216 }
217
218 sel->r.left = min_t(unsigned int, sel->r.left, format->width - 2);
219 sel->r.top = min_t(unsigned int, sel->r.top, format->height - 2);
220 sel->r.width = min_t(unsigned int, sel->r.width,
221 format->width - sel->r.left);
222 sel->r.height = min_t(unsigned int, sel->r.height,
223 format->height - sel->r.top);
224
225 crop = vsp1_rwpf_get_crop(rwpf, config);
226 *crop = sel->r;
227
228 /* Propagate the format to the source pad. */
229 format = vsp1_entity_get_pad_format(&rwpf->entity, config,
230 RWPF_PAD_SOURCE);
231 format->width = crop->width;
232 format->height = crop->height;
233
234 done:
235 mutex_unlock(&rwpf->entity.lock);
236 return ret;
237 }
238
239 const struct v4l2_subdev_pad_ops vsp1_rwpf_pad_ops = {
240 .init_cfg = vsp1_entity_init_cfg,
241 .enum_mbus_code = vsp1_rwpf_enum_mbus_code,
242 .enum_frame_size = vsp1_rwpf_enum_frame_size,
243 .get_fmt = vsp1_subdev_get_pad_format,
244 .set_fmt = vsp1_rwpf_set_format,
245 .get_selection = vsp1_rwpf_get_selection,
246 .set_selection = vsp1_rwpf_set_selection,
247 };
248
249 /* -----------------------------------------------------------------------------
250 * Controls
251 */
252
253 static int vsp1_rwpf_s_ctrl(struct v4l2_ctrl *ctrl)
254 {
255 struct vsp1_rwpf *rwpf =
256 container_of(ctrl->handler, struct vsp1_rwpf, ctrls);
257
258 switch (ctrl->id) {
259 case V4L2_CID_ALPHA_COMPONENT:
260 rwpf->alpha = ctrl->val;
261 break;
262 }
263
264 return 0;
265 }
266
267 static const struct v4l2_ctrl_ops vsp1_rwpf_ctrl_ops = {
268 .s_ctrl = vsp1_rwpf_s_ctrl,
269 };
270
271 int vsp1_rwpf_init_ctrls(struct vsp1_rwpf *rwpf, unsigned int ncontrols)
272 {
273 v4l2_ctrl_handler_init(&rwpf->ctrls, ncontrols + 1);
274 v4l2_ctrl_new_std(&rwpf->ctrls, &vsp1_rwpf_ctrl_ops,
275 V4L2_CID_ALPHA_COMPONENT, 0, 255, 1, 255);
276
277 rwpf->entity.subdev.ctrl_handler = &rwpf->ctrls;
278
279 return rwpf->ctrls.error;
280 }