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