Commit | Line | Data |
---|---|---|
26e0ca22 LP |
1 | /* |
2 | * vsp1_uds.c -- R-Car VSP1 Up and Down Scaler | |
3 | * | |
8a1edc55 | 4 | * Copyright (C) 2013-2014 Renesas Electronics Corporation |
26e0ca22 LP |
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 | ||
26e0ca22 LP |
32 | static inline void vsp1_uds_write(struct vsp1_uds *uds, u32 reg, u32 data) |
33 | { | |
773abafe LP |
34 | vsp1_mod_write(&uds->entity, reg + uds->entity.index * VI6_UDS_OFFSET, |
35 | data); | |
26e0ca22 LP |
36 | } |
37 | ||
38 | /* ----------------------------------------------------------------------------- | |
39 | * Scaling Computation | |
40 | */ | |
41 | ||
bdc2df62 LP |
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 | ||
26e0ca22 LP |
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 | ||
26e0ca22 LP |
107 | /* ----------------------------------------------------------------------------- |
108 | * V4L2 Subdevice Core Operations | |
109 | */ | |
110 | ||
111 | static int uds_s_stream(struct v4l2_subdev *subdev, int enable) | |
112 | { | |
26e0ca22 | 113 | struct vsp1_uds *uds = to_uds(subdev); |
bdc2df62 LP |
114 | const struct v4l2_mbus_framefmt *output; |
115 | const struct v4l2_mbus_framefmt *input; | |
116 | unsigned int hscale; | |
117 | unsigned int vscale; | |
118 | bool multitap; | |
26e0ca22 LP |
119 | |
120 | if (!enable) | |
121 | return 0; | |
122 | ||
e790c3cb LP |
123 | input = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config, |
124 | UDS_PAD_SINK); | |
125 | output = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config, | |
126 | UDS_PAD_SOURCE); | |
bdc2df62 LP |
127 | |
128 | hscale = uds_compute_ratio(input->width, output->width); | |
129 | vscale = uds_compute_ratio(input->height, output->height); | |
130 | ||
131 | dev_dbg(uds->entity.vsp1->dev, "hscale %u vscale %u\n", hscale, vscale); | |
132 | ||
133 | /* Multi-tap scaling can't be enabled along with alpha scaling when | |
134 | * scaling down with a factor lower than or equal to 1/2 in either | |
135 | * direction. | |
136 | */ | |
137 | if (uds->scale_alpha && (hscale >= 8192 || vscale >= 8192)) | |
138 | multitap = false; | |
139 | else | |
140 | multitap = true; | |
141 | ||
142 | vsp1_uds_write(uds, VI6_UDS_CTRL, | |
143 | (uds->scale_alpha ? VI6_UDS_CTRL_AON : 0) | | |
144 | (multitap ? VI6_UDS_CTRL_BC : 0)); | |
26e0ca22 LP |
145 | |
146 | vsp1_uds_write(uds, VI6_UDS_PASS_BWIDTH, | |
bdc2df62 | 147 | (uds_passband_width(hscale) |
26e0ca22 | 148 | << VI6_UDS_PASS_BWIDTH_H_SHIFT) | |
bdc2df62 | 149 | (uds_passband_width(vscale) |
26e0ca22 LP |
150 | << VI6_UDS_PASS_BWIDTH_V_SHIFT)); |
151 | ||
26e0ca22 | 152 | /* Set the scaling ratios and the output size. */ |
26e0ca22 | 153 | vsp1_uds_write(uds, VI6_UDS_SCALE, |
bdc2df62 LP |
154 | (hscale << VI6_UDS_SCALE_HFRAC_SHIFT) | |
155 | (vscale << VI6_UDS_SCALE_VFRAC_SHIFT)); | |
26e0ca22 | 156 | vsp1_uds_write(uds, VI6_UDS_CLIP_SIZE, |
bdc2df62 LP |
157 | (output->width << VI6_UDS_CLIP_SIZE_HSIZE_SHIFT) | |
158 | (output->height << VI6_UDS_CLIP_SIZE_VSIZE_SHIFT)); | |
26e0ca22 LP |
159 | |
160 | return 0; | |
161 | } | |
162 | ||
163 | /* ----------------------------------------------------------------------------- | |
164 | * V4L2 Subdevice Pad Operations | |
165 | */ | |
166 | ||
167 | static int uds_enum_mbus_code(struct v4l2_subdev *subdev, | |
f7234138 | 168 | struct v4l2_subdev_pad_config *cfg, |
26e0ca22 LP |
169 | struct v4l2_subdev_mbus_code_enum *code) |
170 | { | |
171 | static const unsigned int codes[] = { | |
27ffaeb0 BB |
172 | MEDIA_BUS_FMT_ARGB8888_1X32, |
173 | MEDIA_BUS_FMT_AYUV8_1X32, | |
26e0ca22 | 174 | }; |
3f1ccf16 | 175 | struct vsp1_uds *uds = to_uds(subdev); |
26e0ca22 LP |
176 | |
177 | if (code->pad == UDS_PAD_SINK) { | |
178 | if (code->index >= ARRAY_SIZE(codes)) | |
179 | return -EINVAL; | |
180 | ||
181 | code->code = codes[code->index]; | |
182 | } else { | |
e790c3cb | 183 | struct v4l2_subdev_pad_config *config; |
26e0ca22 LP |
184 | struct v4l2_mbus_framefmt *format; |
185 | ||
e790c3cb LP |
186 | config = vsp1_entity_get_pad_config(&uds->entity, cfg, |
187 | code->which); | |
188 | if (!config) | |
189 | return -EINVAL; | |
190 | ||
26e0ca22 LP |
191 | /* The UDS can't perform format conversion, the sink format is |
192 | * always identical to the source format. | |
193 | */ | |
194 | if (code->index) | |
195 | return -EINVAL; | |
196 | ||
e790c3cb LP |
197 | format = vsp1_entity_get_pad_format(&uds->entity, config, |
198 | UDS_PAD_SINK); | |
26e0ca22 LP |
199 | code->code = format->code; |
200 | } | |
201 | ||
202 | return 0; | |
203 | } | |
204 | ||
205 | static int uds_enum_frame_size(struct v4l2_subdev *subdev, | |
f7234138 | 206 | struct v4l2_subdev_pad_config *cfg, |
26e0ca22 LP |
207 | struct v4l2_subdev_frame_size_enum *fse) |
208 | { | |
5778e749 | 209 | struct vsp1_uds *uds = to_uds(subdev); |
e790c3cb | 210 | struct v4l2_subdev_pad_config *config; |
26e0ca22 LP |
211 | struct v4l2_mbus_framefmt *format; |
212 | ||
e790c3cb LP |
213 | config = vsp1_entity_get_pad_config(&uds->entity, cfg, fse->which); |
214 | if (!config) | |
215 | return -EINVAL; | |
216 | ||
217 | format = vsp1_entity_get_pad_format(&uds->entity, config, | |
218 | UDS_PAD_SINK); | |
26e0ca22 LP |
219 | |
220 | if (fse->index || fse->code != format->code) | |
221 | return -EINVAL; | |
222 | ||
223 | if (fse->pad == UDS_PAD_SINK) { | |
224 | fse->min_width = UDS_MIN_SIZE; | |
225 | fse->max_width = UDS_MAX_SIZE; | |
226 | fse->min_height = UDS_MIN_SIZE; | |
227 | fse->max_height = UDS_MAX_SIZE; | |
228 | } else { | |
229 | uds_output_limits(format->width, &fse->min_width, | |
230 | &fse->max_width); | |
231 | uds_output_limits(format->height, &fse->min_height, | |
232 | &fse->max_height); | |
233 | } | |
234 | ||
235 | return 0; | |
236 | } | |
237 | ||
1bd0a1bd LP |
238 | static int uds_get_format(struct v4l2_subdev *subdev, |
239 | struct v4l2_subdev_pad_config *cfg, | |
26e0ca22 LP |
240 | struct v4l2_subdev_format *fmt) |
241 | { | |
242 | struct vsp1_uds *uds = to_uds(subdev); | |
e790c3cb | 243 | struct v4l2_subdev_pad_config *config; |
26e0ca22 | 244 | |
e790c3cb LP |
245 | config = vsp1_entity_get_pad_config(&uds->entity, cfg, fmt->which); |
246 | if (!config) | |
247 | return -EINVAL; | |
248 | ||
249 | fmt->format = *vsp1_entity_get_pad_format(&uds->entity, config, | |
250 | fmt->pad); | |
26e0ca22 LP |
251 | |
252 | return 0; | |
253 | } | |
254 | ||
1bd0a1bd | 255 | static void uds_try_format(struct vsp1_uds *uds, |
e790c3cb LP |
256 | struct v4l2_subdev_pad_config *config, |
257 | unsigned int pad, struct v4l2_mbus_framefmt *fmt) | |
26e0ca22 LP |
258 | { |
259 | struct v4l2_mbus_framefmt *format; | |
260 | unsigned int minimum; | |
261 | unsigned int maximum; | |
262 | ||
263 | switch (pad) { | |
264 | case UDS_PAD_SINK: | |
265 | /* Default to YUV if the requested format is not supported. */ | |
27ffaeb0 BB |
266 | if (fmt->code != MEDIA_BUS_FMT_ARGB8888_1X32 && |
267 | fmt->code != MEDIA_BUS_FMT_AYUV8_1X32) | |
268 | fmt->code = MEDIA_BUS_FMT_AYUV8_1X32; | |
26e0ca22 LP |
269 | |
270 | fmt->width = clamp(fmt->width, UDS_MIN_SIZE, UDS_MAX_SIZE); | |
271 | fmt->height = clamp(fmt->height, UDS_MIN_SIZE, UDS_MAX_SIZE); | |
272 | break; | |
273 | ||
274 | case UDS_PAD_SOURCE: | |
275 | /* The UDS scales but can't perform format conversion. */ | |
e790c3cb LP |
276 | format = vsp1_entity_get_pad_format(&uds->entity, config, |
277 | UDS_PAD_SINK); | |
26e0ca22 LP |
278 | fmt->code = format->code; |
279 | ||
280 | uds_output_limits(format->width, &minimum, &maximum); | |
281 | fmt->width = clamp(fmt->width, minimum, maximum); | |
282 | uds_output_limits(format->height, &minimum, &maximum); | |
283 | fmt->height = clamp(fmt->height, minimum, maximum); | |
284 | break; | |
285 | } | |
286 | ||
287 | fmt->field = V4L2_FIELD_NONE; | |
288 | fmt->colorspace = V4L2_COLORSPACE_SRGB; | |
289 | } | |
290 | ||
1bd0a1bd LP |
291 | static int uds_set_format(struct v4l2_subdev *subdev, |
292 | struct v4l2_subdev_pad_config *cfg, | |
26e0ca22 LP |
293 | struct v4l2_subdev_format *fmt) |
294 | { | |
295 | struct vsp1_uds *uds = to_uds(subdev); | |
e790c3cb | 296 | struct v4l2_subdev_pad_config *config; |
26e0ca22 LP |
297 | struct v4l2_mbus_framefmt *format; |
298 | ||
e790c3cb LP |
299 | config = vsp1_entity_get_pad_config(&uds->entity, cfg, fmt->which); |
300 | if (!config) | |
301 | return -EINVAL; | |
302 | ||
303 | uds_try_format(uds, config, fmt->pad, &fmt->format); | |
26e0ca22 | 304 | |
e790c3cb | 305 | format = vsp1_entity_get_pad_format(&uds->entity, config, fmt->pad); |
26e0ca22 LP |
306 | *format = fmt->format; |
307 | ||
308 | if (fmt->pad == UDS_PAD_SINK) { | |
309 | /* Propagate the format to the source pad. */ | |
e790c3cb LP |
310 | format = vsp1_entity_get_pad_format(&uds->entity, config, |
311 | UDS_PAD_SOURCE); | |
26e0ca22 LP |
312 | *format = fmt->format; |
313 | ||
e790c3cb | 314 | uds_try_format(uds, config, UDS_PAD_SOURCE, format); |
26e0ca22 LP |
315 | } |
316 | ||
26e0ca22 LP |
317 | return 0; |
318 | } | |
319 | ||
320 | /* ----------------------------------------------------------------------------- | |
321 | * V4L2 Subdevice Operations | |
322 | */ | |
323 | ||
324 | static struct v4l2_subdev_video_ops uds_video_ops = { | |
325 | .s_stream = uds_s_stream, | |
326 | }; | |
327 | ||
328 | static struct v4l2_subdev_pad_ops uds_pad_ops = { | |
0efdf0f5 | 329 | .init_cfg = vsp1_entity_init_cfg, |
26e0ca22 LP |
330 | .enum_mbus_code = uds_enum_mbus_code, |
331 | .enum_frame_size = uds_enum_frame_size, | |
332 | .get_fmt = uds_get_format, | |
333 | .set_fmt = uds_set_format, | |
334 | }; | |
335 | ||
336 | static struct v4l2_subdev_ops uds_ops = { | |
337 | .video = &uds_video_ops, | |
338 | .pad = &uds_pad_ops, | |
339 | }; | |
340 | ||
341 | /* ----------------------------------------------------------------------------- | |
342 | * Initialization and Cleanup | |
343 | */ | |
344 | ||
345 | struct vsp1_uds *vsp1_uds_create(struct vsp1_device *vsp1, unsigned int index) | |
346 | { | |
26e0ca22 | 347 | struct vsp1_uds *uds; |
823329df | 348 | char name[6]; |
26e0ca22 LP |
349 | int ret; |
350 | ||
351 | uds = devm_kzalloc(vsp1->dev, sizeof(*uds), GFP_KERNEL); | |
352 | if (uds == NULL) | |
353 | return ERR_PTR(-ENOMEM); | |
354 | ||
355 | uds->entity.type = VSP1_ENTITY_UDS; | |
356 | uds->entity.index = index; | |
26e0ca22 | 357 | |
823329df LP |
358 | sprintf(name, "uds.%u", index); |
359 | ret = vsp1_entity_init(vsp1, &uds->entity, name, 2, &uds_ops); | |
26e0ca22 LP |
360 | if (ret < 0) |
361 | return ERR_PTR(ret); | |
362 | ||
26e0ca22 LP |
363 | return uds; |
364 | } |