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>
21 #include "vsp1_pipe.h"
24 #define UDS_MIN_SIZE 4U
25 #define UDS_MAX_SIZE 8190U
27 #define UDS_MIN_FACTOR 0x0100
28 #define UDS_MAX_FACTOR 0xffff
30 /* -----------------------------------------------------------------------------
34 static inline void vsp1_uds_write(struct vsp1_uds
*uds
, struct vsp1_dl_list
*dl
,
37 vsp1_dl_list_write(dl
, reg
+ uds
->entity
.index
* VI6_UDS_OFFSET
, data
);
40 /* -----------------------------------------------------------------------------
44 void vsp1_uds_set_alpha(struct vsp1_entity
*entity
, struct vsp1_dl_list
*dl
,
47 struct vsp1_uds
*uds
= to_uds(&entity
->subdev
);
49 vsp1_uds_write(uds
, dl
, VI6_UDS_ALPVAL
,
50 alpha
<< VI6_UDS_ALPVAL_VAL0_SHIFT
);
54 * uds_output_size - Return the output size for an input size and scaling ratio
55 * @input: input size in pixels
56 * @ratio: scaling ratio in U4.12 fixed-point format
58 static unsigned int uds_output_size(unsigned int input
, unsigned int ratio
)
65 mp
= mp
< 4 ? 1 : (mp
< 8 ? 2 : 4);
67 return (input
- 1) / mp
* mp
* 4096 / ratio
+ 1;
70 return (input
- 1) * 4096 / ratio
+ 1;
75 * uds_output_limits - Return the min and max output sizes for an input size
76 * @input: input size in pixels
77 * @minimum: minimum output size (returned)
78 * @maximum: maximum output size (returned)
80 static void uds_output_limits(unsigned int input
,
81 unsigned int *minimum
, unsigned int *maximum
)
83 *minimum
= max(uds_output_size(input
, UDS_MAX_FACTOR
), UDS_MIN_SIZE
);
84 *maximum
= min(uds_output_size(input
, UDS_MIN_FACTOR
), UDS_MAX_SIZE
);
88 * uds_passband_width - Return the passband filter width for a scaling ratio
89 * @ratio: scaling ratio in U4.12 fixed-point format
91 static unsigned int uds_passband_width(unsigned int ratio
)
98 mp
= mp
< 4 ? 1 : (mp
< 8 ? 2 : 4);
100 return 64 * 4096 * mp
/ ratio
;
107 static unsigned int uds_compute_ratio(unsigned int input
, unsigned int output
)
109 /* TODO: This is an approximation that will need to be refined. */
110 return (input
- 1) * 4096 / (output
- 1);
113 /* -----------------------------------------------------------------------------
114 * V4L2 Subdevice Pad Operations
117 static int uds_enum_mbus_code(struct v4l2_subdev
*subdev
,
118 struct v4l2_subdev_pad_config
*cfg
,
119 struct v4l2_subdev_mbus_code_enum
*code
)
121 static const unsigned int codes
[] = {
122 MEDIA_BUS_FMT_ARGB8888_1X32
,
123 MEDIA_BUS_FMT_AYUV8_1X32
,
126 return vsp1_subdev_enum_mbus_code(subdev
, cfg
, code
, codes
,
130 static int uds_enum_frame_size(struct v4l2_subdev
*subdev
,
131 struct v4l2_subdev_pad_config
*cfg
,
132 struct v4l2_subdev_frame_size_enum
*fse
)
134 struct vsp1_uds
*uds
= to_uds(subdev
);
135 struct v4l2_subdev_pad_config
*config
;
136 struct v4l2_mbus_framefmt
*format
;
139 config
= vsp1_entity_get_pad_config(&uds
->entity
, cfg
, fse
->which
);
143 format
= vsp1_entity_get_pad_format(&uds
->entity
, config
,
146 mutex_lock(&uds
->entity
.lock
);
148 if (fse
->index
|| fse
->code
!= format
->code
) {
153 if (fse
->pad
== UDS_PAD_SINK
) {
154 fse
->min_width
= UDS_MIN_SIZE
;
155 fse
->max_width
= UDS_MAX_SIZE
;
156 fse
->min_height
= UDS_MIN_SIZE
;
157 fse
->max_height
= UDS_MAX_SIZE
;
159 uds_output_limits(format
->width
, &fse
->min_width
,
161 uds_output_limits(format
->height
, &fse
->min_height
,
166 mutex_unlock(&uds
->entity
.lock
);
170 static void uds_try_format(struct vsp1_uds
*uds
,
171 struct v4l2_subdev_pad_config
*config
,
172 unsigned int pad
, struct v4l2_mbus_framefmt
*fmt
)
174 struct v4l2_mbus_framefmt
*format
;
175 unsigned int minimum
;
176 unsigned int maximum
;
180 /* Default to YUV if the requested format is not supported. */
181 if (fmt
->code
!= MEDIA_BUS_FMT_ARGB8888_1X32
&&
182 fmt
->code
!= MEDIA_BUS_FMT_AYUV8_1X32
)
183 fmt
->code
= MEDIA_BUS_FMT_AYUV8_1X32
;
185 fmt
->width
= clamp(fmt
->width
, UDS_MIN_SIZE
, UDS_MAX_SIZE
);
186 fmt
->height
= clamp(fmt
->height
, UDS_MIN_SIZE
, UDS_MAX_SIZE
);
190 /* The UDS scales but can't perform format conversion. */
191 format
= vsp1_entity_get_pad_format(&uds
->entity
, config
,
193 fmt
->code
= format
->code
;
195 uds_output_limits(format
->width
, &minimum
, &maximum
);
196 fmt
->width
= clamp(fmt
->width
, minimum
, maximum
);
197 uds_output_limits(format
->height
, &minimum
, &maximum
);
198 fmt
->height
= clamp(fmt
->height
, minimum
, maximum
);
202 fmt
->field
= V4L2_FIELD_NONE
;
203 fmt
->colorspace
= V4L2_COLORSPACE_SRGB
;
206 static int uds_set_format(struct v4l2_subdev
*subdev
,
207 struct v4l2_subdev_pad_config
*cfg
,
208 struct v4l2_subdev_format
*fmt
)
210 struct vsp1_uds
*uds
= to_uds(subdev
);
211 struct v4l2_subdev_pad_config
*config
;
212 struct v4l2_mbus_framefmt
*format
;
215 mutex_lock(&uds
->entity
.lock
);
217 config
= vsp1_entity_get_pad_config(&uds
->entity
, cfg
, fmt
->which
);
223 uds_try_format(uds
, config
, fmt
->pad
, &fmt
->format
);
225 format
= vsp1_entity_get_pad_format(&uds
->entity
, config
, fmt
->pad
);
226 *format
= fmt
->format
;
228 if (fmt
->pad
== UDS_PAD_SINK
) {
229 /* Propagate the format to the source pad. */
230 format
= vsp1_entity_get_pad_format(&uds
->entity
, config
,
232 *format
= fmt
->format
;
234 uds_try_format(uds
, config
, UDS_PAD_SOURCE
, format
);
238 mutex_unlock(&uds
->entity
.lock
);
242 /* -----------------------------------------------------------------------------
243 * V4L2 Subdevice Operations
246 static const struct v4l2_subdev_pad_ops uds_pad_ops
= {
247 .init_cfg
= vsp1_entity_init_cfg
,
248 .enum_mbus_code
= uds_enum_mbus_code
,
249 .enum_frame_size
= uds_enum_frame_size
,
250 .get_fmt
= vsp1_subdev_get_pad_format
,
251 .set_fmt
= uds_set_format
,
254 static const struct v4l2_subdev_ops uds_ops
= {
258 /* -----------------------------------------------------------------------------
259 * VSP1 Entity Operations
262 static void uds_configure(struct vsp1_entity
*entity
,
263 struct vsp1_pipeline
*pipe
,
264 struct vsp1_dl_list
*dl
,
265 enum vsp1_entity_params params
)
267 struct vsp1_uds
*uds
= to_uds(&entity
->subdev
);
268 const struct v4l2_mbus_framefmt
*output
;
269 const struct v4l2_mbus_framefmt
*input
;
274 if (params
== VSP1_ENTITY_PARAMS_PARTITION
) {
275 const struct v4l2_rect
*clip
= &pipe
->partition
;
277 vsp1_uds_write(uds
, dl
, VI6_UDS_CLIP_SIZE
,
278 (clip
->width
<< VI6_UDS_CLIP_SIZE_HSIZE_SHIFT
) |
279 (clip
->height
<< VI6_UDS_CLIP_SIZE_VSIZE_SHIFT
));
283 if (params
!= VSP1_ENTITY_PARAMS_INIT
)
286 input
= vsp1_entity_get_pad_format(&uds
->entity
, uds
->entity
.config
,
288 output
= vsp1_entity_get_pad_format(&uds
->entity
, uds
->entity
.config
,
291 hscale
= uds_compute_ratio(input
->width
, output
->width
);
292 vscale
= uds_compute_ratio(input
->height
, output
->height
);
294 dev_dbg(uds
->entity
.vsp1
->dev
, "hscale %u vscale %u\n", hscale
, vscale
);
296 /* Multi-tap scaling can't be enabled along with alpha scaling when
297 * scaling down with a factor lower than or equal to 1/2 in either
300 if (uds
->scale_alpha
&& (hscale
>= 8192 || vscale
>= 8192))
305 vsp1_uds_write(uds
, dl
, VI6_UDS_CTRL
,
306 (uds
->scale_alpha
? VI6_UDS_CTRL_AON
: 0) |
307 (multitap
? VI6_UDS_CTRL_BC
: 0));
309 vsp1_uds_write(uds
, dl
, VI6_UDS_PASS_BWIDTH
,
310 (uds_passband_width(hscale
)
311 << VI6_UDS_PASS_BWIDTH_H_SHIFT
) |
312 (uds_passband_width(vscale
)
313 << VI6_UDS_PASS_BWIDTH_V_SHIFT
));
315 /* Set the scaling ratios. */
316 vsp1_uds_write(uds
, dl
, VI6_UDS_SCALE
,
317 (hscale
<< VI6_UDS_SCALE_HFRAC_SHIFT
) |
318 (vscale
<< VI6_UDS_SCALE_VFRAC_SHIFT
));
321 static unsigned int uds_max_width(struct vsp1_entity
*entity
,
322 struct vsp1_pipeline
*pipe
)
324 struct vsp1_uds
*uds
= to_uds(&entity
->subdev
);
325 const struct v4l2_mbus_framefmt
*output
;
326 const struct v4l2_mbus_framefmt
*input
;
329 input
= vsp1_entity_get_pad_format(&uds
->entity
, uds
->entity
.config
,
331 output
= vsp1_entity_get_pad_format(&uds
->entity
, uds
->entity
.config
,
333 hscale
= output
->width
/ input
->width
;
337 else if (hscale
<= 4)
339 else if (hscale
<= 8)
345 static const struct vsp1_entity_operations uds_entity_ops
= {
346 .configure
= uds_configure
,
347 .max_width
= uds_max_width
,
350 /* -----------------------------------------------------------------------------
351 * Initialization and Cleanup
354 struct vsp1_uds
*vsp1_uds_create(struct vsp1_device
*vsp1
, unsigned int index
)
356 struct vsp1_uds
*uds
;
360 uds
= devm_kzalloc(vsp1
->dev
, sizeof(*uds
), GFP_KERNEL
);
362 return ERR_PTR(-ENOMEM
);
364 uds
->entity
.ops
= &uds_entity_ops
;
365 uds
->entity
.type
= VSP1_ENTITY_UDS
;
366 uds
->entity
.index
= index
;
368 sprintf(name
, "uds.%u", index
);
369 ret
= vsp1_entity_init(vsp1
, &uds
->entity
, name
, 2, &uds_ops
,
370 MEDIA_ENT_F_PROC_VIDEO_SCALER
);