blob: da8f89a31ea44b88138ed2f3ac0feec34fdbd070 [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"
Kieran Binghamfc6e5142016-09-11 23:26:35 -030021#include "vsp1_pipe.h"
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030022#include "vsp1_uds.h"
23
24#define UDS_MIN_SIZE 4U
25#define UDS_MAX_SIZE 8190U
26
27#define UDS_MIN_FACTOR 0x0100
28#define UDS_MAX_FACTOR 0xffff
29
30/* -----------------------------------------------------------------------------
31 * Device Access
32 */
33
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -020034static inline void vsp1_uds_write(struct vsp1_uds *uds, struct vsp1_dl_list *dl,
35 u32 reg, u32 data)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030036{
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -020037 vsp1_dl_list_write(dl, reg + uds->entity.index * VI6_UDS_OFFSET, data);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030038}
39
40/* -----------------------------------------------------------------------------
41 * Scaling Computation
42 */
43
Laurent Pinchart07a23c62016-06-19 23:19:43 -030044void vsp1_uds_set_alpha(struct vsp1_entity *entity, struct vsp1_dl_list *dl,
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -020045 unsigned int alpha)
Laurent Pinchartbdc2df62014-05-30 21:45:48 -030046{
Laurent Pinchart07a23c62016-06-19 23:19:43 -030047 struct vsp1_uds *uds = to_uds(&entity->subdev);
48
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -020049 vsp1_uds_write(uds, dl, VI6_UDS_ALPVAL,
50 alpha << VI6_UDS_ALPVAL_VAL0_SHIFT);
Laurent Pinchartbdc2df62014-05-30 21:45:48 -030051}
52
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030053/*
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
57 */
58static unsigned int uds_output_size(unsigned int input, unsigned int ratio)
59{
60 if (ratio > 4096) {
61 /* Down-scaling */
62 unsigned int mp;
63
64 mp = ratio / 4096;
65 mp = mp < 4 ? 1 : (mp < 8 ? 2 : 4);
66
67 return (input - 1) / mp * mp * 4096 / ratio + 1;
68 } else {
69 /* Up-scaling */
70 return (input - 1) * 4096 / ratio + 1;
71 }
72}
73
74/*
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)
79 */
80static void uds_output_limits(unsigned int input,
81 unsigned int *minimum, unsigned int *maximum)
82{
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);
85}
86
87/*
88 * uds_passband_width - Return the passband filter width for a scaling ratio
89 * @ratio: scaling ratio in U4.12 fixed-point format
90 */
91static unsigned int uds_passband_width(unsigned int ratio)
92{
93 if (ratio >= 4096) {
94 /* Down-scaling */
95 unsigned int mp;
96
97 mp = ratio / 4096;
98 mp = mp < 4 ? 1 : (mp < 8 ? 2 : 4);
99
100 return 64 * 4096 * mp / ratio;
101 } else {
102 /* Up-scaling */
103 return 64;
104 }
105}
106
107static unsigned int uds_compute_ratio(unsigned int input, unsigned int output)
108{
109 /* TODO: This is an approximation that will need to be refined. */
110 return (input - 1) * 4096 / (output - 1);
111}
112
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300113/* -----------------------------------------------------------------------------
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300114 * V4L2 Subdevice Pad Operations
115 */
116
117static int uds_enum_mbus_code(struct v4l2_subdev *subdev,
Hans Verkuilf7234132015-03-04 01:47:54 -0800118 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300119 struct v4l2_subdev_mbus_code_enum *code)
120{
121 static const unsigned int codes[] = {
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300122 MEDIA_BUS_FMT_ARGB8888_1X32,
123 MEDIA_BUS_FMT_AYUV8_1X32,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300124 };
125
Laurent Pinchart6ad9ba92016-02-24 20:25:42 -0300126 return vsp1_subdev_enum_mbus_code(subdev, cfg, code, codes,
127 ARRAY_SIZE(codes));
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300128}
129
130static int uds_enum_frame_size(struct v4l2_subdev *subdev,
Hans Verkuilf7234132015-03-04 01:47:54 -0800131 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300132 struct v4l2_subdev_frame_size_enum *fse)
133{
Hans Verkuil5778e742015-03-04 01:47:58 -0800134 struct vsp1_uds *uds = to_uds(subdev);
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200135 struct v4l2_subdev_pad_config *config;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300136 struct v4l2_mbus_framefmt *format;
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300137 int ret = 0;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300138
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200139 config = vsp1_entity_get_pad_config(&uds->entity, cfg, fse->which);
140 if (!config)
141 return -EINVAL;
142
143 format = vsp1_entity_get_pad_format(&uds->entity, config,
144 UDS_PAD_SINK);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300145
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300146 mutex_lock(&uds->entity.lock);
147
148 if (fse->index || fse->code != format->code) {
149 ret = -EINVAL;
150 goto done;
151 }
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300152
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;
158 } else {
159 uds_output_limits(format->width, &fse->min_width,
160 &fse->max_width);
161 uds_output_limits(format->height, &fse->min_height,
162 &fse->max_height);
163 }
164
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300165done:
166 mutex_unlock(&uds->entity.lock);
167 return ret;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300168}
169
Laurent Pinchart1bd0a1b2015-11-01 15:18:32 -0200170static void uds_try_format(struct vsp1_uds *uds,
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200171 struct v4l2_subdev_pad_config *config,
172 unsigned int pad, struct v4l2_mbus_framefmt *fmt)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300173{
174 struct v4l2_mbus_framefmt *format;
175 unsigned int minimum;
176 unsigned int maximum;
177
178 switch (pad) {
179 case UDS_PAD_SINK:
180 /* Default to YUV if the requested format is not supported. */
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300181 if (fmt->code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
182 fmt->code != MEDIA_BUS_FMT_AYUV8_1X32)
183 fmt->code = MEDIA_BUS_FMT_AYUV8_1X32;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300184
185 fmt->width = clamp(fmt->width, UDS_MIN_SIZE, UDS_MAX_SIZE);
186 fmt->height = clamp(fmt->height, UDS_MIN_SIZE, UDS_MAX_SIZE);
187 break;
188
189 case UDS_PAD_SOURCE:
190 /* The UDS scales but can't perform format conversion. */
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200191 format = vsp1_entity_get_pad_format(&uds->entity, config,
192 UDS_PAD_SINK);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300193 fmt->code = format->code;
194
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);
199 break;
200 }
201
202 fmt->field = V4L2_FIELD_NONE;
203 fmt->colorspace = V4L2_COLORSPACE_SRGB;
204}
205
Laurent Pinchart1bd0a1b2015-11-01 15:18:32 -0200206static int uds_set_format(struct v4l2_subdev *subdev,
207 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300208 struct v4l2_subdev_format *fmt)
209{
210 struct vsp1_uds *uds = to_uds(subdev);
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200211 struct v4l2_subdev_pad_config *config;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300212 struct v4l2_mbus_framefmt *format;
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300213 int ret = 0;
214
215 mutex_lock(&uds->entity.lock);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300216
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200217 config = vsp1_entity_get_pad_config(&uds->entity, cfg, fmt->which);
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300218 if (!config) {
219 ret = -EINVAL;
220 goto done;
221 }
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300222
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200223 uds_try_format(uds, config, fmt->pad, &fmt->format);
224
225 format = vsp1_entity_get_pad_format(&uds->entity, config, fmt->pad);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300226 *format = fmt->format;
227
228 if (fmt->pad == UDS_PAD_SINK) {
229 /* Propagate the format to the source pad. */
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200230 format = vsp1_entity_get_pad_format(&uds->entity, config,
231 UDS_PAD_SOURCE);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300232 *format = fmt->format;
233
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200234 uds_try_format(uds, config, UDS_PAD_SOURCE, format);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300235 }
236
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300237done:
238 mutex_unlock(&uds->entity.lock);
239 return ret;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300240}
241
242/* -----------------------------------------------------------------------------
243 * V4L2 Subdevice Operations
244 */
245
Laurent Pincharteb9163d2016-06-17 21:11:26 -0300246static const struct v4l2_subdev_pad_ops uds_pad_ops = {
Laurent Pinchart0efdf0f2015-11-15 20:09:08 -0200247 .init_cfg = vsp1_entity_init_cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300248 .enum_mbus_code = uds_enum_mbus_code,
249 .enum_frame_size = uds_enum_frame_size,
Laurent Pinchart3f557222016-02-24 21:10:13 -0300250 .get_fmt = vsp1_subdev_get_pad_format,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300251 .set_fmt = uds_set_format,
252};
253
Laurent Pincharteb9163d2016-06-17 21:11:26 -0300254static const struct v4l2_subdev_ops uds_ops = {
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300255 .pad = &uds_pad_ops,
256};
257
258/* -----------------------------------------------------------------------------
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200259 * VSP1 Entity Operations
260 */
261
Laurent Pinchart83dd0192016-01-14 14:17:32 -0200262static void uds_configure(struct vsp1_entity *entity,
263 struct vsp1_pipeline *pipe,
Laurent Pinchartd21fbbb2016-09-11 19:39:30 -0300264 struct vsp1_dl_list *dl,
265 enum vsp1_entity_params params)
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200266{
267 struct vsp1_uds *uds = to_uds(&entity->subdev);
268 const struct v4l2_mbus_framefmt *output;
269 const struct v4l2_mbus_framefmt *input;
270 unsigned int hscale;
271 unsigned int vscale;
272 bool multitap;
273
Kieran Binghamfc6e5142016-09-11 23:26:35 -0300274 if (params == VSP1_ENTITY_PARAMS_PARTITION) {
275 const struct v4l2_rect *clip = &pipe->partition;
276
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));
280 return;
281 }
282
Laurent Pinchartd21fbbb2016-09-11 19:39:30 -0300283 if (params != VSP1_ENTITY_PARAMS_INIT)
Laurent Pinchartfc845e52016-06-11 04:07:56 -0300284 return;
285
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200286 input = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
287 UDS_PAD_SINK);
288 output = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
289 UDS_PAD_SOURCE);
290
291 hscale = uds_compute_ratio(input->width, output->width);
292 vscale = uds_compute_ratio(input->height, output->height);
293
294 dev_dbg(uds->entity.vsp1->dev, "hscale %u vscale %u\n", hscale, vscale);
295
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
298 * direction.
299 */
300 if (uds->scale_alpha && (hscale >= 8192 || vscale >= 8192))
301 multitap = false;
302 else
303 multitap = true;
304
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -0200305 vsp1_uds_write(uds, dl, VI6_UDS_CTRL,
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200306 (uds->scale_alpha ? VI6_UDS_CTRL_AON : 0) |
307 (multitap ? VI6_UDS_CTRL_BC : 0));
308
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -0200309 vsp1_uds_write(uds, dl, VI6_UDS_PASS_BWIDTH,
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200310 (uds_passband_width(hscale)
311 << VI6_UDS_PASS_BWIDTH_H_SHIFT) |
312 (uds_passband_width(vscale)
313 << VI6_UDS_PASS_BWIDTH_V_SHIFT));
314
Kieran Binghamfc6e5142016-09-11 23:26:35 -0300315 /* Set the scaling ratios. */
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -0200316 vsp1_uds_write(uds, dl, VI6_UDS_SCALE,
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200317 (hscale << VI6_UDS_SCALE_HFRAC_SHIFT) |
318 (vscale << VI6_UDS_SCALE_VFRAC_SHIFT));
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200319}
320
Kieran Binghamdf32c922016-07-12 10:06:34 -0300321static unsigned int uds_max_width(struct vsp1_entity *entity,
322 struct vsp1_pipeline *pipe)
323{
324 struct vsp1_uds *uds = to_uds(&entity->subdev);
325 const struct v4l2_mbus_framefmt *output;
326 const struct v4l2_mbus_framefmt *input;
327 unsigned int hscale;
328
329 input = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
330 UDS_PAD_SINK);
331 output = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
332 UDS_PAD_SOURCE);
333 hscale = output->width / input->width;
334
335 if (hscale <= 2)
336 return 256;
337 else if (hscale <= 4)
338 return 512;
339 else if (hscale <= 8)
340 return 1024;
341 else
342 return 2048;
343}
344
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200345static const struct vsp1_entity_operations uds_entity_ops = {
346 .configure = uds_configure,
Kieran Binghamdf32c922016-07-12 10:06:34 -0300347 .max_width = uds_max_width,
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200348};
349
350/* -----------------------------------------------------------------------------
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300351 * Initialization and Cleanup
352 */
353
354struct vsp1_uds *vsp1_uds_create(struct vsp1_device *vsp1, unsigned int index)
355{
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300356 struct vsp1_uds *uds;
Laurent Pinchart823329d2015-11-15 19:42:01 -0200357 char name[6];
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300358 int ret;
359
360 uds = devm_kzalloc(vsp1->dev, sizeof(*uds), GFP_KERNEL);
361 if (uds == NULL)
362 return ERR_PTR(-ENOMEM);
363
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200364 uds->entity.ops = &uds_entity_ops;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300365 uds->entity.type = VSP1_ENTITY_UDS;
366 uds->entity.index = index;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300367
Laurent Pinchart823329d2015-11-15 19:42:01 -0200368 sprintf(name, "uds.%u", index);
Laurent Pinchart6a8e07b2016-02-15 22:10:26 -0200369 ret = vsp1_entity_init(vsp1, &uds->entity, name, 2, &uds_ops,
370 MEDIA_ENT_F_PROC_VIDEO_SCALER);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300371 if (ret < 0)
372 return ERR_PTR(ret);
373
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300374 return uds;
375}