[media] v4l: vsp1: Create a new configure operation to setup modules
[deliverable/linux.git] / drivers / media / platform / vsp1 / vsp1_uds.c
1 /*
2 * vsp1_uds.c -- R-Car VSP1 Up and Down Scaler
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 <linux/device.h>
15 #include <linux/gfp.h>
16
17 #include <media/v4l2-subdev.h>
18
19 #include "vsp1.h"
20 #include "vsp1_uds.h"
21
22 #define UDS_MIN_SIZE 4U
23 #define UDS_MAX_SIZE 8190U
24
25 #define UDS_MIN_FACTOR 0x0100
26 #define UDS_MAX_FACTOR 0xffff
27
28 /* -----------------------------------------------------------------------------
29 * Device Access
30 */
31
32 static inline void vsp1_uds_write(struct vsp1_uds *uds, u32 reg, u32 data)
33 {
34 vsp1_mod_write(&uds->entity, reg + uds->entity.index * VI6_UDS_OFFSET,
35 data);
36 }
37
38 /* -----------------------------------------------------------------------------
39 * Scaling Computation
40 */
41
42 void vsp1_uds_set_alpha(struct vsp1_uds *uds, unsigned int alpha)
43 {
44 vsp1_uds_write(uds, VI6_UDS_ALPVAL, alpha << VI6_UDS_ALPVAL_VAL0_SHIFT);
45 }
46
47 /*
48 * uds_output_size - Return the output size for an input size and scaling ratio
49 * @input: input size in pixels
50 * @ratio: scaling ratio in U4.12 fixed-point format
51 */
52 static unsigned int uds_output_size(unsigned int input, unsigned int ratio)
53 {
54 if (ratio > 4096) {
55 /* Down-scaling */
56 unsigned int mp;
57
58 mp = ratio / 4096;
59 mp = mp < 4 ? 1 : (mp < 8 ? 2 : 4);
60
61 return (input - 1) / mp * mp * 4096 / ratio + 1;
62 } else {
63 /* Up-scaling */
64 return (input - 1) * 4096 / ratio + 1;
65 }
66 }
67
68 /*
69 * uds_output_limits - Return the min and max output sizes for an input size
70 * @input: input size in pixels
71 * @minimum: minimum output size (returned)
72 * @maximum: maximum output size (returned)
73 */
74 static void uds_output_limits(unsigned int input,
75 unsigned int *minimum, unsigned int *maximum)
76 {
77 *minimum = max(uds_output_size(input, UDS_MAX_FACTOR), UDS_MIN_SIZE);
78 *maximum = min(uds_output_size(input, UDS_MIN_FACTOR), UDS_MAX_SIZE);
79 }
80
81 /*
82 * uds_passband_width - Return the passband filter width for a scaling ratio
83 * @ratio: scaling ratio in U4.12 fixed-point format
84 */
85 static unsigned int uds_passband_width(unsigned int ratio)
86 {
87 if (ratio >= 4096) {
88 /* Down-scaling */
89 unsigned int mp;
90
91 mp = ratio / 4096;
92 mp = mp < 4 ? 1 : (mp < 8 ? 2 : 4);
93
94 return 64 * 4096 * mp / ratio;
95 } else {
96 /* Up-scaling */
97 return 64;
98 }
99 }
100
101 static unsigned int uds_compute_ratio(unsigned int input, unsigned int output)
102 {
103 /* TODO: This is an approximation that will need to be refined. */
104 return (input - 1) * 4096 / (output - 1);
105 }
106
107 /* -----------------------------------------------------------------------------
108 * V4L2 Subdevice Pad Operations
109 */
110
111 static int uds_enum_mbus_code(struct v4l2_subdev *subdev,
112 struct v4l2_subdev_pad_config *cfg,
113 struct v4l2_subdev_mbus_code_enum *code)
114 {
115 static const unsigned int codes[] = {
116 MEDIA_BUS_FMT_ARGB8888_1X32,
117 MEDIA_BUS_FMT_AYUV8_1X32,
118 };
119 struct vsp1_uds *uds = to_uds(subdev);
120
121 if (code->pad == UDS_PAD_SINK) {
122 if (code->index >= ARRAY_SIZE(codes))
123 return -EINVAL;
124
125 code->code = codes[code->index];
126 } else {
127 struct v4l2_subdev_pad_config *config;
128 struct v4l2_mbus_framefmt *format;
129
130 config = vsp1_entity_get_pad_config(&uds->entity, cfg,
131 code->which);
132 if (!config)
133 return -EINVAL;
134
135 /* The UDS can't perform format conversion, the sink format is
136 * always identical to the source format.
137 */
138 if (code->index)
139 return -EINVAL;
140
141 format = vsp1_entity_get_pad_format(&uds->entity, config,
142 UDS_PAD_SINK);
143 code->code = format->code;
144 }
145
146 return 0;
147 }
148
149 static int uds_enum_frame_size(struct v4l2_subdev *subdev,
150 struct v4l2_subdev_pad_config *cfg,
151 struct v4l2_subdev_frame_size_enum *fse)
152 {
153 struct vsp1_uds *uds = to_uds(subdev);
154 struct v4l2_subdev_pad_config *config;
155 struct v4l2_mbus_framefmt *format;
156
157 config = vsp1_entity_get_pad_config(&uds->entity, cfg, fse->which);
158 if (!config)
159 return -EINVAL;
160
161 format = vsp1_entity_get_pad_format(&uds->entity, config,
162 UDS_PAD_SINK);
163
164 if (fse->index || fse->code != format->code)
165 return -EINVAL;
166
167 if (fse->pad == UDS_PAD_SINK) {
168 fse->min_width = UDS_MIN_SIZE;
169 fse->max_width = UDS_MAX_SIZE;
170 fse->min_height = UDS_MIN_SIZE;
171 fse->max_height = UDS_MAX_SIZE;
172 } else {
173 uds_output_limits(format->width, &fse->min_width,
174 &fse->max_width);
175 uds_output_limits(format->height, &fse->min_height,
176 &fse->max_height);
177 }
178
179 return 0;
180 }
181
182 static int uds_get_format(struct v4l2_subdev *subdev,
183 struct v4l2_subdev_pad_config *cfg,
184 struct v4l2_subdev_format *fmt)
185 {
186 struct vsp1_uds *uds = to_uds(subdev);
187 struct v4l2_subdev_pad_config *config;
188
189 config = vsp1_entity_get_pad_config(&uds->entity, cfg, fmt->which);
190 if (!config)
191 return -EINVAL;
192
193 fmt->format = *vsp1_entity_get_pad_format(&uds->entity, config,
194 fmt->pad);
195
196 return 0;
197 }
198
199 static void uds_try_format(struct vsp1_uds *uds,
200 struct v4l2_subdev_pad_config *config,
201 unsigned int pad, struct v4l2_mbus_framefmt *fmt)
202 {
203 struct v4l2_mbus_framefmt *format;
204 unsigned int minimum;
205 unsigned int maximum;
206
207 switch (pad) {
208 case UDS_PAD_SINK:
209 /* Default to YUV if the requested format is not supported. */
210 if (fmt->code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
211 fmt->code != MEDIA_BUS_FMT_AYUV8_1X32)
212 fmt->code = MEDIA_BUS_FMT_AYUV8_1X32;
213
214 fmt->width = clamp(fmt->width, UDS_MIN_SIZE, UDS_MAX_SIZE);
215 fmt->height = clamp(fmt->height, UDS_MIN_SIZE, UDS_MAX_SIZE);
216 break;
217
218 case UDS_PAD_SOURCE:
219 /* The UDS scales but can't perform format conversion. */
220 format = vsp1_entity_get_pad_format(&uds->entity, config,
221 UDS_PAD_SINK);
222 fmt->code = format->code;
223
224 uds_output_limits(format->width, &minimum, &maximum);
225 fmt->width = clamp(fmt->width, minimum, maximum);
226 uds_output_limits(format->height, &minimum, &maximum);
227 fmt->height = clamp(fmt->height, minimum, maximum);
228 break;
229 }
230
231 fmt->field = V4L2_FIELD_NONE;
232 fmt->colorspace = V4L2_COLORSPACE_SRGB;
233 }
234
235 static int uds_set_format(struct v4l2_subdev *subdev,
236 struct v4l2_subdev_pad_config *cfg,
237 struct v4l2_subdev_format *fmt)
238 {
239 struct vsp1_uds *uds = to_uds(subdev);
240 struct v4l2_subdev_pad_config *config;
241 struct v4l2_mbus_framefmt *format;
242
243 config = vsp1_entity_get_pad_config(&uds->entity, cfg, fmt->which);
244 if (!config)
245 return -EINVAL;
246
247 uds_try_format(uds, config, fmt->pad, &fmt->format);
248
249 format = vsp1_entity_get_pad_format(&uds->entity, config, fmt->pad);
250 *format = fmt->format;
251
252 if (fmt->pad == UDS_PAD_SINK) {
253 /* Propagate the format to the source pad. */
254 format = vsp1_entity_get_pad_format(&uds->entity, config,
255 UDS_PAD_SOURCE);
256 *format = fmt->format;
257
258 uds_try_format(uds, config, UDS_PAD_SOURCE, format);
259 }
260
261 return 0;
262 }
263
264 /* -----------------------------------------------------------------------------
265 * V4L2 Subdevice Operations
266 */
267
268 static struct v4l2_subdev_pad_ops uds_pad_ops = {
269 .init_cfg = vsp1_entity_init_cfg,
270 .enum_mbus_code = uds_enum_mbus_code,
271 .enum_frame_size = uds_enum_frame_size,
272 .get_fmt = uds_get_format,
273 .set_fmt = uds_set_format,
274 };
275
276 static struct v4l2_subdev_ops uds_ops = {
277 .pad = &uds_pad_ops,
278 };
279
280 /* -----------------------------------------------------------------------------
281 * VSP1 Entity Operations
282 */
283
284 static void uds_configure(struct vsp1_entity *entity)
285 {
286 struct vsp1_uds *uds = to_uds(&entity->subdev);
287 const struct v4l2_mbus_framefmt *output;
288 const struct v4l2_mbus_framefmt *input;
289 unsigned int hscale;
290 unsigned int vscale;
291 bool multitap;
292
293 input = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
294 UDS_PAD_SINK);
295 output = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
296 UDS_PAD_SOURCE);
297
298 hscale = uds_compute_ratio(input->width, output->width);
299 vscale = uds_compute_ratio(input->height, output->height);
300
301 dev_dbg(uds->entity.vsp1->dev, "hscale %u vscale %u\n", hscale, vscale);
302
303 /* Multi-tap scaling can't be enabled along with alpha scaling when
304 * scaling down with a factor lower than or equal to 1/2 in either
305 * direction.
306 */
307 if (uds->scale_alpha && (hscale >= 8192 || vscale >= 8192))
308 multitap = false;
309 else
310 multitap = true;
311
312 vsp1_uds_write(uds, VI6_UDS_CTRL,
313 (uds->scale_alpha ? VI6_UDS_CTRL_AON : 0) |
314 (multitap ? VI6_UDS_CTRL_BC : 0));
315
316 vsp1_uds_write(uds, VI6_UDS_PASS_BWIDTH,
317 (uds_passband_width(hscale)
318 << VI6_UDS_PASS_BWIDTH_H_SHIFT) |
319 (uds_passband_width(vscale)
320 << VI6_UDS_PASS_BWIDTH_V_SHIFT));
321
322 /* Set the scaling ratios and the output size. */
323 vsp1_uds_write(uds, VI6_UDS_SCALE,
324 (hscale << VI6_UDS_SCALE_HFRAC_SHIFT) |
325 (vscale << VI6_UDS_SCALE_VFRAC_SHIFT));
326 vsp1_uds_write(uds, VI6_UDS_CLIP_SIZE,
327 (output->width << VI6_UDS_CLIP_SIZE_HSIZE_SHIFT) |
328 (output->height << VI6_UDS_CLIP_SIZE_VSIZE_SHIFT));
329 }
330
331 static const struct vsp1_entity_operations uds_entity_ops = {
332 .configure = uds_configure,
333 };
334
335 /* -----------------------------------------------------------------------------
336 * Initialization and Cleanup
337 */
338
339 struct vsp1_uds *vsp1_uds_create(struct vsp1_device *vsp1, unsigned int index)
340 {
341 struct vsp1_uds *uds;
342 char name[6];
343 int ret;
344
345 uds = devm_kzalloc(vsp1->dev, sizeof(*uds), GFP_KERNEL);
346 if (uds == NULL)
347 return ERR_PTR(-ENOMEM);
348
349 uds->entity.ops = &uds_entity_ops;
350 uds->entity.type = VSP1_ENTITY_UDS;
351 uds->entity.index = index;
352
353 sprintf(name, "uds.%u", index);
354 ret = vsp1_entity_init(vsp1, &uds->entity, name, 2, &uds_ops);
355 if (ret < 0)
356 return ERR_PTR(ret);
357
358 return uds;
359 }
This page took 0.04886 seconds and 6 git commands to generate.