2 * vsp1_uds.c -- R-Car VSP1 Up and Down Scaler
4 * Copyright (C) 2013-2014 Renesas Electronics Corporation
6 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
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.
14 #include <linux/device.h>
15 #include <linux/gfp.h>
17 #include <media/v4l2-subdev.h>
22 #define UDS_MIN_SIZE 4U
23 #define UDS_MAX_SIZE 8190U
25 #define UDS_MIN_FACTOR 0x0100
26 #define UDS_MAX_FACTOR 0xffff
28 /* -----------------------------------------------------------------------------
32 static inline void vsp1_uds_write(struct vsp1_uds *uds, u32 reg, u32 data)
34 vsp1_mod_write(&uds->entity, reg + uds->entity.index * VI6_UDS_OFFSET,
38 /* -----------------------------------------------------------------------------
42 void vsp1_uds_set_alpha(struct vsp1_uds *uds, unsigned int alpha)
44 vsp1_uds_write(uds, VI6_UDS_ALPVAL, alpha << VI6_UDS_ALPVAL_VAL0_SHIFT);
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
52 static unsigned int uds_output_size(unsigned int input, unsigned int ratio)
59 mp = mp < 4 ? 1 : (mp < 8 ? 2 : 4);
61 return (input - 1) / mp * mp * 4096 / ratio + 1;
64 return (input - 1) * 4096 / ratio + 1;
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)
74 static void uds_output_limits(unsigned int input,
75 unsigned int *minimum, unsigned int *maximum)
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);
82 * uds_passband_width - Return the passband filter width for a scaling ratio
83 * @ratio: scaling ratio in U4.12 fixed-point format
85 static unsigned int uds_passband_width(unsigned int ratio)
92 mp = mp < 4 ? 1 : (mp < 8 ? 2 : 4);
94 return 64 * 4096 * mp / ratio;
101 static unsigned int uds_compute_ratio(unsigned int input, unsigned int output)
103 /* TODO: This is an approximation that will need to be refined. */
104 return (input - 1) * 4096 / (output - 1);
107 /* -----------------------------------------------------------------------------
108 * V4L2 Subdevice Core Operations
111 static int uds_s_stream(struct v4l2_subdev *subdev, int enable)
113 struct vsp1_uds *uds = to_uds(subdev);
114 const struct v4l2_mbus_framefmt *output;
115 const struct v4l2_mbus_framefmt *input;
123 input = &uds->entity.formats[UDS_PAD_SINK];
124 output = &uds->entity.formats[UDS_PAD_SOURCE];
126 hscale = uds_compute_ratio(input->width, output->width);
127 vscale = uds_compute_ratio(input->height, output->height);
129 dev_dbg(uds->entity.vsp1->dev, "hscale %u vscale %u\n", hscale, vscale);
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
135 if (uds->scale_alpha && (hscale >= 8192 || vscale >= 8192))
140 vsp1_uds_write(uds, VI6_UDS_CTRL,
141 (uds->scale_alpha ? VI6_UDS_CTRL_AON : 0) |
142 (multitap ? VI6_UDS_CTRL_BC : 0));
144 vsp1_uds_write(uds, VI6_UDS_PASS_BWIDTH,
145 (uds_passband_width(hscale)
146 << VI6_UDS_PASS_BWIDTH_H_SHIFT) |
147 (uds_passband_width(vscale)
148 << VI6_UDS_PASS_BWIDTH_V_SHIFT));
150 /* Set the scaling ratios and the output size. */
151 vsp1_uds_write(uds, VI6_UDS_SCALE,
152 (hscale << VI6_UDS_SCALE_HFRAC_SHIFT) |
153 (vscale << VI6_UDS_SCALE_VFRAC_SHIFT));
154 vsp1_uds_write(uds, VI6_UDS_CLIP_SIZE,
155 (output->width << VI6_UDS_CLIP_SIZE_HSIZE_SHIFT) |
156 (output->height << VI6_UDS_CLIP_SIZE_VSIZE_SHIFT));
161 /* -----------------------------------------------------------------------------
162 * V4L2 Subdevice Pad Operations
165 static int uds_enum_mbus_code(struct v4l2_subdev *subdev,
166 struct v4l2_subdev_pad_config *cfg,
167 struct v4l2_subdev_mbus_code_enum *code)
169 static const unsigned int codes[] = {
170 MEDIA_BUS_FMT_ARGB8888_1X32,
171 MEDIA_BUS_FMT_AYUV8_1X32,
173 struct vsp1_uds *uds = to_uds(subdev);
175 if (code->pad == UDS_PAD_SINK) {
176 if (code->index >= ARRAY_SIZE(codes))
179 code->code = codes[code->index];
181 struct v4l2_mbus_framefmt *format;
183 /* The UDS can't perform format conversion, the sink format is
184 * always identical to the source format.
189 format = vsp1_entity_get_pad_format(&uds->entity, cfg,
190 UDS_PAD_SINK, code->which);
191 code->code = format->code;
197 static int uds_enum_frame_size(struct v4l2_subdev *subdev,
198 struct v4l2_subdev_pad_config *cfg,
199 struct v4l2_subdev_frame_size_enum *fse)
201 struct vsp1_uds *uds = to_uds(subdev);
202 struct v4l2_mbus_framefmt *format;
204 format = vsp1_entity_get_pad_format(&uds->entity, cfg,
205 UDS_PAD_SINK, fse->which);
207 if (fse->index || fse->code != format->code)
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;
216 uds_output_limits(format->width, &fse->min_width,
218 uds_output_limits(format->height, &fse->min_height,
225 static int uds_get_format(struct v4l2_subdev *subdev,
226 struct v4l2_subdev_pad_config *cfg,
227 struct v4l2_subdev_format *fmt)
229 struct vsp1_uds *uds = to_uds(subdev);
231 fmt->format = *vsp1_entity_get_pad_format(&uds->entity, cfg, fmt->pad,
237 static void uds_try_format(struct vsp1_uds *uds,
238 struct v4l2_subdev_pad_config *cfg,
239 unsigned int pad, struct v4l2_mbus_framefmt *fmt,
240 enum v4l2_subdev_format_whence which)
242 struct v4l2_mbus_framefmt *format;
243 unsigned int minimum;
244 unsigned int maximum;
248 /* Default to YUV if the requested format is not supported. */
249 if (fmt->code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
250 fmt->code != MEDIA_BUS_FMT_AYUV8_1X32)
251 fmt->code = MEDIA_BUS_FMT_AYUV8_1X32;
253 fmt->width = clamp(fmt->width, UDS_MIN_SIZE, UDS_MAX_SIZE);
254 fmt->height = clamp(fmt->height, UDS_MIN_SIZE, UDS_MAX_SIZE);
258 /* The UDS scales but can't perform format conversion. */
259 format = vsp1_entity_get_pad_format(&uds->entity, cfg,
260 UDS_PAD_SINK, which);
261 fmt->code = format->code;
263 uds_output_limits(format->width, &minimum, &maximum);
264 fmt->width = clamp(fmt->width, minimum, maximum);
265 uds_output_limits(format->height, &minimum, &maximum);
266 fmt->height = clamp(fmt->height, minimum, maximum);
270 fmt->field = V4L2_FIELD_NONE;
271 fmt->colorspace = V4L2_COLORSPACE_SRGB;
274 static int uds_set_format(struct v4l2_subdev *subdev,
275 struct v4l2_subdev_pad_config *cfg,
276 struct v4l2_subdev_format *fmt)
278 struct vsp1_uds *uds = to_uds(subdev);
279 struct v4l2_mbus_framefmt *format;
281 uds_try_format(uds, cfg, fmt->pad, &fmt->format, fmt->which);
283 format = vsp1_entity_get_pad_format(&uds->entity, cfg, fmt->pad,
285 *format = fmt->format;
287 if (fmt->pad == UDS_PAD_SINK) {
288 /* Propagate the format to the source pad. */
289 format = vsp1_entity_get_pad_format(&uds->entity, cfg,
290 UDS_PAD_SOURCE, fmt->which);
291 *format = fmt->format;
293 uds_try_format(uds, cfg, UDS_PAD_SOURCE, format, fmt->which);
299 /* -----------------------------------------------------------------------------
300 * V4L2 Subdevice Operations
303 static struct v4l2_subdev_video_ops uds_video_ops = {
304 .s_stream = uds_s_stream,
307 static struct v4l2_subdev_pad_ops uds_pad_ops = {
308 .init_cfg = vsp1_entity_init_cfg,
309 .enum_mbus_code = uds_enum_mbus_code,
310 .enum_frame_size = uds_enum_frame_size,
311 .get_fmt = uds_get_format,
312 .set_fmt = uds_set_format,
315 static struct v4l2_subdev_ops uds_ops = {
316 .video = &uds_video_ops,
320 /* -----------------------------------------------------------------------------
321 * Initialization and Cleanup
324 struct vsp1_uds *vsp1_uds_create(struct vsp1_device *vsp1, unsigned int index)
326 struct vsp1_uds *uds;
330 uds = devm_kzalloc(vsp1->dev, sizeof(*uds), GFP_KERNEL);
332 return ERR_PTR(-ENOMEM);
334 uds->entity.type = VSP1_ENTITY_UDS;
335 uds->entity.index = index;
337 sprintf(name, "uds.%u", index);
338 ret = vsp1_entity_init(vsp1, &uds->entity, name, 2, &uds_ops);