blob: 59dd53c0d2be83abd01fb8a95c5f9e4a5e93221c [file] [log] [blame]
Laurent Pinchart26e0ca22013-06-04 11:22:30 -03001/*
2 * vsp1_uds.c -- R-Car VSP1 Up and Down Scaler
3 *
Laurent Pinchart8a1edc52014-02-06 14:42:31 -03004 * Copyright (C) 2013-2014 Renesas Electronics Corporation
Laurent Pinchart26e0ca22013-06-04 11:22:30 -03005 *
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"
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -020020#include "vsp1_dl.h"
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030021#include "vsp1_uds.h"
22
23#define UDS_MIN_SIZE 4U
24#define UDS_MAX_SIZE 8190U
25
26#define UDS_MIN_FACTOR 0x0100
27#define UDS_MAX_FACTOR 0xffff
28
29/* -----------------------------------------------------------------------------
30 * Device Access
31 */
32
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -020033static inline void vsp1_uds_write(struct vsp1_uds *uds, struct vsp1_dl_list *dl,
34 u32 reg, u32 data)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030035{
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -020036 vsp1_dl_list_write(dl, reg + uds->entity.index * VI6_UDS_OFFSET, data);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030037}
38
39/* -----------------------------------------------------------------------------
40 * Scaling Computation
41 */
42
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -020043void vsp1_uds_set_alpha(struct vsp1_uds *uds, struct vsp1_dl_list *dl,
44 unsigned int alpha)
Laurent Pinchartbdc2df62014-05-30 21:45:48 -030045{
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -020046 vsp1_uds_write(uds, dl, VI6_UDS_ALPVAL,
47 alpha << VI6_UDS_ALPVAL_VAL0_SHIFT);
Laurent Pinchartbdc2df62014-05-30 21:45:48 -030048}
49
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030050/*
51 * uds_output_size - Return the output size for an input size and scaling ratio
52 * @input: input size in pixels
53 * @ratio: scaling ratio in U4.12 fixed-point format
54 */
55static unsigned int uds_output_size(unsigned int input, unsigned int ratio)
56{
57 if (ratio > 4096) {
58 /* Down-scaling */
59 unsigned int mp;
60
61 mp = ratio / 4096;
62 mp = mp < 4 ? 1 : (mp < 8 ? 2 : 4);
63
64 return (input - 1) / mp * mp * 4096 / ratio + 1;
65 } else {
66 /* Up-scaling */
67 return (input - 1) * 4096 / ratio + 1;
68 }
69}
70
71/*
72 * uds_output_limits - Return the min and max output sizes for an input size
73 * @input: input size in pixels
74 * @minimum: minimum output size (returned)
75 * @maximum: maximum output size (returned)
76 */
77static void uds_output_limits(unsigned int input,
78 unsigned int *minimum, unsigned int *maximum)
79{
80 *minimum = max(uds_output_size(input, UDS_MAX_FACTOR), UDS_MIN_SIZE);
81 *maximum = min(uds_output_size(input, UDS_MIN_FACTOR), UDS_MAX_SIZE);
82}
83
84/*
85 * uds_passband_width - Return the passband filter width for a scaling ratio
86 * @ratio: scaling ratio in U4.12 fixed-point format
87 */
88static unsigned int uds_passband_width(unsigned int ratio)
89{
90 if (ratio >= 4096) {
91 /* Down-scaling */
92 unsigned int mp;
93
94 mp = ratio / 4096;
95 mp = mp < 4 ? 1 : (mp < 8 ? 2 : 4);
96
97 return 64 * 4096 * mp / ratio;
98 } else {
99 /* Up-scaling */
100 return 64;
101 }
102}
103
104static unsigned int uds_compute_ratio(unsigned int input, unsigned int output)
105{
106 /* TODO: This is an approximation that will need to be refined. */
107 return (input - 1) * 4096 / (output - 1);
108}
109
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300110/* -----------------------------------------------------------------------------
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300111 * V4L2 Subdevice Pad Operations
112 */
113
114static int uds_enum_mbus_code(struct v4l2_subdev *subdev,
Hans Verkuilf7234132015-03-04 01:47:54 -0800115 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300116 struct v4l2_subdev_mbus_code_enum *code)
117{
118 static const unsigned int codes[] = {
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300119 MEDIA_BUS_FMT_ARGB8888_1X32,
120 MEDIA_BUS_FMT_AYUV8_1X32,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300121 };
Hans Verkuil3f1ccf12015-03-04 01:47:57 -0800122 struct vsp1_uds *uds = to_uds(subdev);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300123
124 if (code->pad == UDS_PAD_SINK) {
125 if (code->index >= ARRAY_SIZE(codes))
126 return -EINVAL;
127
128 code->code = codes[code->index];
129 } else {
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200130 struct v4l2_subdev_pad_config *config;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300131 struct v4l2_mbus_framefmt *format;
132
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200133 config = vsp1_entity_get_pad_config(&uds->entity, cfg,
134 code->which);
135 if (!config)
136 return -EINVAL;
137
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300138 /* The UDS can't perform format conversion, the sink format is
139 * always identical to the source format.
140 */
141 if (code->index)
142 return -EINVAL;
143
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200144 format = vsp1_entity_get_pad_format(&uds->entity, config,
145 UDS_PAD_SINK);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300146 code->code = format->code;
147 }
148
149 return 0;
150}
151
152static int uds_enum_frame_size(struct v4l2_subdev *subdev,
Hans Verkuilf7234132015-03-04 01:47:54 -0800153 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300154 struct v4l2_subdev_frame_size_enum *fse)
155{
Hans Verkuil5778e742015-03-04 01:47:58 -0800156 struct vsp1_uds *uds = to_uds(subdev);
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200157 struct v4l2_subdev_pad_config *config;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300158 struct v4l2_mbus_framefmt *format;
159
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200160 config = vsp1_entity_get_pad_config(&uds->entity, cfg, fse->which);
161 if (!config)
162 return -EINVAL;
163
164 format = vsp1_entity_get_pad_format(&uds->entity, config,
165 UDS_PAD_SINK);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300166
167 if (fse->index || fse->code != format->code)
168 return -EINVAL;
169
170 if (fse->pad == UDS_PAD_SINK) {
171 fse->min_width = UDS_MIN_SIZE;
172 fse->max_width = UDS_MAX_SIZE;
173 fse->min_height = UDS_MIN_SIZE;
174 fse->max_height = UDS_MAX_SIZE;
175 } else {
176 uds_output_limits(format->width, &fse->min_width,
177 &fse->max_width);
178 uds_output_limits(format->height, &fse->min_height,
179 &fse->max_height);
180 }
181
182 return 0;
183}
184
Laurent Pinchart1bd0a1b2015-11-01 15:18:32 -0200185static void uds_try_format(struct vsp1_uds *uds,
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200186 struct v4l2_subdev_pad_config *config,
187 unsigned int pad, struct v4l2_mbus_framefmt *fmt)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300188{
189 struct v4l2_mbus_framefmt *format;
190 unsigned int minimum;
191 unsigned int maximum;
192
193 switch (pad) {
194 case UDS_PAD_SINK:
195 /* Default to YUV if the requested format is not supported. */
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300196 if (fmt->code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
197 fmt->code != MEDIA_BUS_FMT_AYUV8_1X32)
198 fmt->code = MEDIA_BUS_FMT_AYUV8_1X32;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300199
200 fmt->width = clamp(fmt->width, UDS_MIN_SIZE, UDS_MAX_SIZE);
201 fmt->height = clamp(fmt->height, UDS_MIN_SIZE, UDS_MAX_SIZE);
202 break;
203
204 case UDS_PAD_SOURCE:
205 /* The UDS scales but can't perform format conversion. */
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200206 format = vsp1_entity_get_pad_format(&uds->entity, config,
207 UDS_PAD_SINK);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300208 fmt->code = format->code;
209
210 uds_output_limits(format->width, &minimum, &maximum);
211 fmt->width = clamp(fmt->width, minimum, maximum);
212 uds_output_limits(format->height, &minimum, &maximum);
213 fmt->height = clamp(fmt->height, minimum, maximum);
214 break;
215 }
216
217 fmt->field = V4L2_FIELD_NONE;
218 fmt->colorspace = V4L2_COLORSPACE_SRGB;
219}
220
Laurent Pinchart1bd0a1b2015-11-01 15:18:32 -0200221static int uds_set_format(struct v4l2_subdev *subdev,
222 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300223 struct v4l2_subdev_format *fmt)
224{
225 struct vsp1_uds *uds = to_uds(subdev);
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200226 struct v4l2_subdev_pad_config *config;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300227 struct v4l2_mbus_framefmt *format;
228
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200229 config = vsp1_entity_get_pad_config(&uds->entity, cfg, fmt->which);
230 if (!config)
231 return -EINVAL;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300232
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200233 uds_try_format(uds, config, fmt->pad, &fmt->format);
234
235 format = vsp1_entity_get_pad_format(&uds->entity, config, fmt->pad);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300236 *format = fmt->format;
237
238 if (fmt->pad == UDS_PAD_SINK) {
239 /* Propagate the format to the source pad. */
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200240 format = vsp1_entity_get_pad_format(&uds->entity, config,
241 UDS_PAD_SOURCE);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300242 *format = fmt->format;
243
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200244 uds_try_format(uds, config, UDS_PAD_SOURCE, format);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300245 }
246
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300247 return 0;
248}
249
250/* -----------------------------------------------------------------------------
251 * V4L2 Subdevice Operations
252 */
253
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300254static struct v4l2_subdev_pad_ops uds_pad_ops = {
Laurent Pinchart0efdf0f2015-11-15 20:09:08 -0200255 .init_cfg = vsp1_entity_init_cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300256 .enum_mbus_code = uds_enum_mbus_code,
257 .enum_frame_size = uds_enum_frame_size,
Laurent Pinchart3f557222016-02-24 21:10:13 -0300258 .get_fmt = vsp1_subdev_get_pad_format,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300259 .set_fmt = uds_set_format,
260};
261
262static struct v4l2_subdev_ops uds_ops = {
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300263 .pad = &uds_pad_ops,
264};
265
266/* -----------------------------------------------------------------------------
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200267 * VSP1 Entity Operations
268 */
269
Laurent Pinchart83dd0192016-01-14 14:17:32 -0200270static void uds_configure(struct vsp1_entity *entity,
271 struct vsp1_pipeline *pipe,
272 struct vsp1_dl_list *dl)
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200273{
274 struct vsp1_uds *uds = to_uds(&entity->subdev);
275 const struct v4l2_mbus_framefmt *output;
276 const struct v4l2_mbus_framefmt *input;
277 unsigned int hscale;
278 unsigned int vscale;
279 bool multitap;
280
281 input = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
282 UDS_PAD_SINK);
283 output = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
284 UDS_PAD_SOURCE);
285
286 hscale = uds_compute_ratio(input->width, output->width);
287 vscale = uds_compute_ratio(input->height, output->height);
288
289 dev_dbg(uds->entity.vsp1->dev, "hscale %u vscale %u\n", hscale, vscale);
290
291 /* Multi-tap scaling can't be enabled along with alpha scaling when
292 * scaling down with a factor lower than or equal to 1/2 in either
293 * direction.
294 */
295 if (uds->scale_alpha && (hscale >= 8192 || vscale >= 8192))
296 multitap = false;
297 else
298 multitap = true;
299
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -0200300 vsp1_uds_write(uds, dl, VI6_UDS_CTRL,
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200301 (uds->scale_alpha ? VI6_UDS_CTRL_AON : 0) |
302 (multitap ? VI6_UDS_CTRL_BC : 0));
303
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -0200304 vsp1_uds_write(uds, dl, VI6_UDS_PASS_BWIDTH,
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200305 (uds_passband_width(hscale)
306 << VI6_UDS_PASS_BWIDTH_H_SHIFT) |
307 (uds_passband_width(vscale)
308 << VI6_UDS_PASS_BWIDTH_V_SHIFT));
309
310 /* Set the scaling ratios and the output size. */
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -0200311 vsp1_uds_write(uds, dl, VI6_UDS_SCALE,
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200312 (hscale << VI6_UDS_SCALE_HFRAC_SHIFT) |
313 (vscale << VI6_UDS_SCALE_VFRAC_SHIFT));
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -0200314 vsp1_uds_write(uds, dl, VI6_UDS_CLIP_SIZE,
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200315 (output->width << VI6_UDS_CLIP_SIZE_HSIZE_SHIFT) |
316 (output->height << VI6_UDS_CLIP_SIZE_VSIZE_SHIFT));
317}
318
319static const struct vsp1_entity_operations uds_entity_ops = {
320 .configure = uds_configure,
321};
322
323/* -----------------------------------------------------------------------------
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300324 * Initialization and Cleanup
325 */
326
327struct vsp1_uds *vsp1_uds_create(struct vsp1_device *vsp1, unsigned int index)
328{
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300329 struct vsp1_uds *uds;
Laurent Pinchart823329d2015-11-15 19:42:01 -0200330 char name[6];
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300331 int ret;
332
333 uds = devm_kzalloc(vsp1->dev, sizeof(*uds), GFP_KERNEL);
334 if (uds == NULL)
335 return ERR_PTR(-ENOMEM);
336
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200337 uds->entity.ops = &uds_entity_ops;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300338 uds->entity.type = VSP1_ENTITY_UDS;
339 uds->entity.index = index;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300340
Laurent Pinchart823329d2015-11-15 19:42:01 -0200341 sprintf(name, "uds.%u", index);
342 ret = vsp1_entity_init(vsp1, &uds->entity, name, 2, &uds_ops);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300343 if (ret < 0)
344 return ERR_PTR(ret);
345
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300346 return uds;
347}