1 // SPDX-License-Identifier: GPL-2.0+
3 * vsp1_uds.c -- R-Car VSP1 Up and Down Scaler
5 * Copyright (C) 2013-2014 Renesas Electronics Corporation
7 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
10 #include <linux/device.h>
11 #include <linux/gfp.h>
13 #include <media/v4l2-subdev.h>
17 #include "vsp1_pipe.h"
20 #define UDS_MIN_SIZE 4U
21 #define UDS_MAX_SIZE 8190U
23 #define UDS_MIN_FACTOR 0x0100
24 #define UDS_MAX_FACTOR 0xffff
26 /* -----------------------------------------------------------------------------
30 static inline void vsp1_uds_write(struct vsp1_uds *uds,
31 struct vsp1_dl_body *dlb, u32 reg, u32 data)
33 vsp1_dl_body_write(dlb, reg + uds->entity.index * VI6_UDS_OFFSET, data);
36 /* -----------------------------------------------------------------------------
40 void vsp1_uds_set_alpha(struct vsp1_entity *entity, struct vsp1_dl_body *dlb,
43 struct vsp1_uds *uds = to_uds(&entity->subdev);
45 vsp1_uds_write(uds, dlb, VI6_UDS_ALPVAL,
46 alpha << VI6_UDS_ALPVAL_VAL0_SHIFT);
50 * uds_output_size - Return the output size for an input size and scaling ratio
51 * @input: input size in pixels
52 * @ratio: scaling ratio in U4.12 fixed-point format
54 static unsigned int uds_output_size(unsigned int input, unsigned int ratio)
61 mp = mp < 4 ? 1 : (mp < 8 ? 2 : 4);
63 return (input - 1) / mp * mp * 4096 / ratio + 1;
66 return (input - 1) * 4096 / ratio + 1;
71 * uds_output_limits - Return the min and max output sizes for an input size
72 * @input: input size in pixels
73 * @minimum: minimum output size (returned)
74 * @maximum: maximum output size (returned)
76 static void uds_output_limits(unsigned int input,
77 unsigned int *minimum, unsigned int *maximum)
79 *minimum = max(uds_output_size(input, UDS_MAX_FACTOR), UDS_MIN_SIZE);
80 *maximum = min(uds_output_size(input, UDS_MIN_FACTOR), UDS_MAX_SIZE);
84 * uds_passband_width - Return the passband filter width for a scaling ratio
85 * @ratio: scaling ratio in U4.12 fixed-point format
87 static unsigned int uds_passband_width(unsigned int ratio)
94 mp = mp < 4 ? 1 : (mp < 8 ? 2 : 4);
96 return 64 * 4096 * mp / ratio;
103 static unsigned int uds_compute_ratio(unsigned int input, unsigned int output)
105 /* TODO: This is an approximation that will need to be refined. */
106 return (input - 1) * 4096 / (output - 1);
109 /* -----------------------------------------------------------------------------
110 * V4L2 Subdevice Pad Operations
113 static int uds_enum_mbus_code(struct v4l2_subdev *subdev,
114 struct v4l2_subdev_state *sd_state,
115 struct v4l2_subdev_mbus_code_enum *code)
117 static const unsigned int codes[] = {
118 MEDIA_BUS_FMT_ARGB8888_1X32,
119 MEDIA_BUS_FMT_AYUV8_1X32,
122 return vsp1_subdev_enum_mbus_code(subdev, sd_state, code, codes,
126 static int uds_enum_frame_size(struct v4l2_subdev *subdev,
127 struct v4l2_subdev_state *sd_state,
128 struct v4l2_subdev_frame_size_enum *fse)
130 struct vsp1_uds *uds = to_uds(subdev);
131 struct v4l2_subdev_state *config;
132 struct v4l2_mbus_framefmt *format;
135 config = vsp1_entity_get_pad_config(&uds->entity, sd_state,
140 format = vsp1_entity_get_pad_format(&uds->entity, config,
143 mutex_lock(&uds->entity.lock);
145 if (fse->index || fse->code != format->code) {
150 if (fse->pad == UDS_PAD_SINK) {
151 fse->min_width = UDS_MIN_SIZE;
152 fse->max_width = UDS_MAX_SIZE;
153 fse->min_height = UDS_MIN_SIZE;
154 fse->max_height = UDS_MAX_SIZE;
156 uds_output_limits(format->width, &fse->min_width,
158 uds_output_limits(format->height, &fse->min_height,
163 mutex_unlock(&uds->entity.lock);
167 static void uds_try_format(struct vsp1_uds *uds,
168 struct v4l2_subdev_state *sd_state,
169 unsigned int pad, struct v4l2_mbus_framefmt *fmt)
171 struct v4l2_mbus_framefmt *format;
172 unsigned int minimum;
173 unsigned int maximum;
177 /* Default to YUV if the requested format is not supported. */
178 if (fmt->code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
179 fmt->code != MEDIA_BUS_FMT_AYUV8_1X32)
180 fmt->code = MEDIA_BUS_FMT_AYUV8_1X32;
182 fmt->width = clamp(fmt->width, UDS_MIN_SIZE, UDS_MAX_SIZE);
183 fmt->height = clamp(fmt->height, UDS_MIN_SIZE, UDS_MAX_SIZE);
187 /* The UDS scales but can't perform format conversion. */
188 format = vsp1_entity_get_pad_format(&uds->entity, sd_state,
190 fmt->code = format->code;
192 uds_output_limits(format->width, &minimum, &maximum);
193 fmt->width = clamp(fmt->width, minimum, maximum);
194 uds_output_limits(format->height, &minimum, &maximum);
195 fmt->height = clamp(fmt->height, minimum, maximum);
199 fmt->field = V4L2_FIELD_NONE;
200 fmt->colorspace = V4L2_COLORSPACE_SRGB;
203 static int uds_set_format(struct v4l2_subdev *subdev,
204 struct v4l2_subdev_state *sd_state,
205 struct v4l2_subdev_format *fmt)
207 struct vsp1_uds *uds = to_uds(subdev);
208 struct v4l2_subdev_state *config;
209 struct v4l2_mbus_framefmt *format;
212 mutex_lock(&uds->entity.lock);
214 config = vsp1_entity_get_pad_config(&uds->entity, sd_state,
221 uds_try_format(uds, config, fmt->pad, &fmt->format);
223 format = vsp1_entity_get_pad_format(&uds->entity, config, fmt->pad);
224 *format = fmt->format;
226 if (fmt->pad == UDS_PAD_SINK) {
227 /* Propagate the format to the source pad. */
228 format = vsp1_entity_get_pad_format(&uds->entity, config,
230 *format = fmt->format;
232 uds_try_format(uds, config, UDS_PAD_SOURCE, format);
236 mutex_unlock(&uds->entity.lock);
240 /* -----------------------------------------------------------------------------
241 * V4L2 Subdevice Operations
244 static const struct v4l2_subdev_pad_ops uds_pad_ops = {
245 .init_cfg = vsp1_entity_init_cfg,
246 .enum_mbus_code = uds_enum_mbus_code,
247 .enum_frame_size = uds_enum_frame_size,
248 .get_fmt = vsp1_subdev_get_pad_format,
249 .set_fmt = uds_set_format,
252 static const struct v4l2_subdev_ops uds_ops = {
256 /* -----------------------------------------------------------------------------
257 * VSP1 Entity Operations
260 static void uds_configure_stream(struct vsp1_entity *entity,
261 struct vsp1_pipeline *pipe,
262 struct vsp1_dl_list *dl,
263 struct vsp1_dl_body *dlb)
265 struct vsp1_uds *uds = to_uds(&entity->subdev);
266 const struct v4l2_mbus_framefmt *output;
267 const struct v4l2_mbus_framefmt *input;
272 input = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
274 output = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
277 hscale = uds_compute_ratio(input->width, output->width);
278 vscale = uds_compute_ratio(input->height, output->height);
280 dev_dbg(uds->entity.vsp1->dev, "hscale %u vscale %u\n", hscale, vscale);
283 * Multi-tap scaling can't be enabled along with alpha scaling when
284 * scaling down with a factor lower than or equal to 1/2 in either
287 if (uds->scale_alpha && (hscale >= 8192 || vscale >= 8192))
292 vsp1_uds_write(uds, dlb, VI6_UDS_CTRL,
293 (uds->scale_alpha ? VI6_UDS_CTRL_AON : 0) |
294 (multitap ? VI6_UDS_CTRL_BC : 0));
296 vsp1_uds_write(uds, dlb, VI6_UDS_PASS_BWIDTH,
297 (uds_passband_width(hscale)
298 << VI6_UDS_PASS_BWIDTH_H_SHIFT) |
299 (uds_passband_width(vscale)
300 << VI6_UDS_PASS_BWIDTH_V_SHIFT));
302 /* Set the scaling ratios. */
303 vsp1_uds_write(uds, dlb, VI6_UDS_SCALE,
304 (hscale << VI6_UDS_SCALE_HFRAC_SHIFT) |
305 (vscale << VI6_UDS_SCALE_VFRAC_SHIFT));
308 static void uds_configure_partition(struct vsp1_entity *entity,
309 struct vsp1_pipeline *pipe,
310 struct vsp1_dl_list *dl,
311 struct vsp1_dl_body *dlb)
313 struct vsp1_uds *uds = to_uds(&entity->subdev);
314 struct vsp1_partition *partition = pipe->partition;
315 const struct v4l2_mbus_framefmt *output;
317 output = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
320 /* Input size clipping. */
321 vsp1_uds_write(uds, dlb, VI6_UDS_HSZCLIP, VI6_UDS_HSZCLIP_HCEN |
322 (0 << VI6_UDS_HSZCLIP_HCL_OFST_SHIFT) |
323 (partition->uds_sink.width
324 << VI6_UDS_HSZCLIP_HCL_SIZE_SHIFT));
326 /* Output size clipping. */
327 vsp1_uds_write(uds, dlb, VI6_UDS_CLIP_SIZE,
328 (partition->uds_source.width
329 << VI6_UDS_CLIP_SIZE_HSIZE_SHIFT) |
331 << VI6_UDS_CLIP_SIZE_VSIZE_SHIFT));
334 static unsigned int uds_max_width(struct vsp1_entity *entity,
335 struct vsp1_pipeline *pipe)
337 struct vsp1_uds *uds = to_uds(&entity->subdev);
338 const struct v4l2_mbus_framefmt *output;
339 const struct v4l2_mbus_framefmt *input;
342 input = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
344 output = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
346 hscale = output->width / input->width;
349 * The maximum width of the UDS is 304 pixels. These are input pixels
350 * in the event of up-scaling, and output pixels in the event of
353 * To support overlapping partition windows we clamp at units of 256 and
354 * the remaining pixels are reserved.
358 else if (hscale <= 4)
360 else if (hscale <= 8)
366 /* -----------------------------------------------------------------------------
367 * Partition Algorithm Support
370 static void uds_partition(struct vsp1_entity *entity,
371 struct vsp1_pipeline *pipe,
372 struct vsp1_partition *partition,
373 unsigned int partition_idx,
374 struct vsp1_partition_window *window)
376 struct vsp1_uds *uds = to_uds(&entity->subdev);
377 const struct v4l2_mbus_framefmt *output;
378 const struct v4l2_mbus_framefmt *input;
380 /* Initialise the partition state. */
381 partition->uds_sink = *window;
382 partition->uds_source = *window;
384 input = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
386 output = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
389 partition->uds_sink.width = window->width * input->width
391 partition->uds_sink.left = window->left * input->width
394 *window = partition->uds_sink;
397 static const struct vsp1_entity_operations uds_entity_ops = {
398 .configure_stream = uds_configure_stream,
399 .configure_partition = uds_configure_partition,
400 .max_width = uds_max_width,
401 .partition = uds_partition,
404 /* -----------------------------------------------------------------------------
405 * Initialization and Cleanup
408 struct vsp1_uds *vsp1_uds_create(struct vsp1_device *vsp1, unsigned int index)
410 struct vsp1_uds *uds;
414 uds = devm_kzalloc(vsp1->dev, sizeof(*uds), GFP_KERNEL);
416 return ERR_PTR(-ENOMEM);
418 uds->entity.ops = &uds_entity_ops;
419 uds->entity.type = VSP1_ENTITY_UDS;
420 uds->entity.index = index;
422 sprintf(name, "uds.%u", index);
423 ret = vsp1_entity_init(vsp1, &uds->entity, name, 2, &uds_ops,
424 MEDIA_ENT_F_PROC_VIDEO_SCALER);