Merge remote-tracking branch 'h8300/h8300-next'
[deliverable/linux.git] / drivers / media / platform / vsp1 / vsp1_uds.c
CommitLineData
26e0ca22
LP
1/*
2 * vsp1_uds.c -- R-Car VSP1 Up and Down Scaler
3 *
8a1edc55 4 * Copyright (C) 2013-2014 Renesas Electronics Corporation
26e0ca22
LP
5 *
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"
5e8dbbf3 20#include "vsp1_dl.h"
26e0ca22
LP
21#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
5e8dbbf3
LP
33static inline void vsp1_uds_write(struct vsp1_uds *uds, struct vsp1_dl_list *dl,
34 u32 reg, u32 data)
26e0ca22 35{
5e8dbbf3 36 vsp1_dl_list_write(dl, reg + uds->entity.index * VI6_UDS_OFFSET, data);
26e0ca22
LP
37}
38
39/* -----------------------------------------------------------------------------
40 * Scaling Computation
41 */
42
07a23c61 43void vsp1_uds_set_alpha(struct vsp1_entity *entity, struct vsp1_dl_list *dl,
5e8dbbf3 44 unsigned int alpha)
bdc2df62 45{
07a23c61
LP
46 struct vsp1_uds *uds = to_uds(&entity->subdev);
47
5e8dbbf3
LP
48 vsp1_uds_write(uds, dl, VI6_UDS_ALPVAL,
49 alpha << VI6_UDS_ALPVAL_VAL0_SHIFT);
bdc2df62
LP
50}
51
26e0ca22
LP
52/*
53 * uds_output_size - Return the output size for an input size and scaling ratio
54 * @input: input size in pixels
55 * @ratio: scaling ratio in U4.12 fixed-point format
56 */
57static unsigned int uds_output_size(unsigned int input, unsigned int ratio)
58{
59 if (ratio > 4096) {
60 /* Down-scaling */
61 unsigned int mp;
62
63 mp = ratio / 4096;
64 mp = mp < 4 ? 1 : (mp < 8 ? 2 : 4);
65
66 return (input - 1) / mp * mp * 4096 / ratio + 1;
67 } else {
68 /* Up-scaling */
69 return (input - 1) * 4096 / ratio + 1;
70 }
71}
72
73/*
74 * uds_output_limits - Return the min and max output sizes for an input size
75 * @input: input size in pixels
76 * @minimum: minimum output size (returned)
77 * @maximum: maximum output size (returned)
78 */
79static void uds_output_limits(unsigned int input,
80 unsigned int *minimum, unsigned int *maximum)
81{
82 *minimum = max(uds_output_size(input, UDS_MAX_FACTOR), UDS_MIN_SIZE);
83 *maximum = min(uds_output_size(input, UDS_MIN_FACTOR), UDS_MAX_SIZE);
84}
85
86/*
87 * uds_passband_width - Return the passband filter width for a scaling ratio
88 * @ratio: scaling ratio in U4.12 fixed-point format
89 */
90static unsigned int uds_passband_width(unsigned int ratio)
91{
92 if (ratio >= 4096) {
93 /* Down-scaling */
94 unsigned int mp;
95
96 mp = ratio / 4096;
97 mp = mp < 4 ? 1 : (mp < 8 ? 2 : 4);
98
99 return 64 * 4096 * mp / ratio;
100 } else {
101 /* Up-scaling */
102 return 64;
103 }
104}
105
106static unsigned int uds_compute_ratio(unsigned int input, unsigned int output)
107{
108 /* TODO: This is an approximation that will need to be refined. */
109 return (input - 1) * 4096 / (output - 1);
110}
111
26e0ca22
LP
112/* -----------------------------------------------------------------------------
113 * V4L2 Subdevice Pad Operations
114 */
115
116static int uds_enum_mbus_code(struct v4l2_subdev *subdev,
f7234138 117 struct v4l2_subdev_pad_config *cfg,
26e0ca22
LP
118 struct v4l2_subdev_mbus_code_enum *code)
119{
120 static const unsigned int codes[] = {
27ffaeb0
BB
121 MEDIA_BUS_FMT_ARGB8888_1X32,
122 MEDIA_BUS_FMT_AYUV8_1X32,
26e0ca22 123 };
26e0ca22 124
6ad9ba9c
LP
125 return vsp1_subdev_enum_mbus_code(subdev, cfg, code, codes,
126 ARRAY_SIZE(codes));
26e0ca22
LP
127}
128
129static int uds_enum_frame_size(struct v4l2_subdev *subdev,
f7234138 130 struct v4l2_subdev_pad_config *cfg,
26e0ca22
LP
131 struct v4l2_subdev_frame_size_enum *fse)
132{
5778e749 133 struct vsp1_uds *uds = to_uds(subdev);
e790c3cb 134 struct v4l2_subdev_pad_config *config;
26e0ca22
LP
135 struct v4l2_mbus_framefmt *format;
136
e790c3cb
LP
137 config = vsp1_entity_get_pad_config(&uds->entity, cfg, fse->which);
138 if (!config)
139 return -EINVAL;
140
141 format = vsp1_entity_get_pad_format(&uds->entity, config,
142 UDS_PAD_SINK);
26e0ca22
LP
143
144 if (fse->index || fse->code != format->code)
145 return -EINVAL;
146
147 if (fse->pad == UDS_PAD_SINK) {
148 fse->min_width = UDS_MIN_SIZE;
149 fse->max_width = UDS_MAX_SIZE;
150 fse->min_height = UDS_MIN_SIZE;
151 fse->max_height = UDS_MAX_SIZE;
152 } else {
153 uds_output_limits(format->width, &fse->min_width,
154 &fse->max_width);
155 uds_output_limits(format->height, &fse->min_height,
156 &fse->max_height);
157 }
158
159 return 0;
160}
161
1bd0a1bd 162static void uds_try_format(struct vsp1_uds *uds,
e790c3cb
LP
163 struct v4l2_subdev_pad_config *config,
164 unsigned int pad, struct v4l2_mbus_framefmt *fmt)
26e0ca22
LP
165{
166 struct v4l2_mbus_framefmt *format;
167 unsigned int minimum;
168 unsigned int maximum;
169
170 switch (pad) {
171 case UDS_PAD_SINK:
172 /* Default to YUV if the requested format is not supported. */
27ffaeb0
BB
173 if (fmt->code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
174 fmt->code != MEDIA_BUS_FMT_AYUV8_1X32)
175 fmt->code = MEDIA_BUS_FMT_AYUV8_1X32;
26e0ca22
LP
176
177 fmt->width = clamp(fmt->width, UDS_MIN_SIZE, UDS_MAX_SIZE);
178 fmt->height = clamp(fmt->height, UDS_MIN_SIZE, UDS_MAX_SIZE);
179 break;
180
181 case UDS_PAD_SOURCE:
182 /* The UDS scales but can't perform format conversion. */
e790c3cb
LP
183 format = vsp1_entity_get_pad_format(&uds->entity, config,
184 UDS_PAD_SINK);
26e0ca22
LP
185 fmt->code = format->code;
186
187 uds_output_limits(format->width, &minimum, &maximum);
188 fmt->width = clamp(fmt->width, minimum, maximum);
189 uds_output_limits(format->height, &minimum, &maximum);
190 fmt->height = clamp(fmt->height, minimum, maximum);
191 break;
192 }
193
194 fmt->field = V4L2_FIELD_NONE;
195 fmt->colorspace = V4L2_COLORSPACE_SRGB;
196}
197
1bd0a1bd
LP
198static int uds_set_format(struct v4l2_subdev *subdev,
199 struct v4l2_subdev_pad_config *cfg,
26e0ca22
LP
200 struct v4l2_subdev_format *fmt)
201{
202 struct vsp1_uds *uds = to_uds(subdev);
e790c3cb 203 struct v4l2_subdev_pad_config *config;
26e0ca22
LP
204 struct v4l2_mbus_framefmt *format;
205
e790c3cb
LP
206 config = vsp1_entity_get_pad_config(&uds->entity, cfg, fmt->which);
207 if (!config)
208 return -EINVAL;
209
210 uds_try_format(uds, config, fmt->pad, &fmt->format);
26e0ca22 211
e790c3cb 212 format = vsp1_entity_get_pad_format(&uds->entity, config, fmt->pad);
26e0ca22
LP
213 *format = fmt->format;
214
215 if (fmt->pad == UDS_PAD_SINK) {
216 /* Propagate the format to the source pad. */
e790c3cb
LP
217 format = vsp1_entity_get_pad_format(&uds->entity, config,
218 UDS_PAD_SOURCE);
26e0ca22
LP
219 *format = fmt->format;
220
e790c3cb 221 uds_try_format(uds, config, UDS_PAD_SOURCE, format);
26e0ca22
LP
222 }
223
26e0ca22
LP
224 return 0;
225}
226
227/* -----------------------------------------------------------------------------
228 * V4L2 Subdevice Operations
229 */
230
eb9163d3 231static const struct v4l2_subdev_pad_ops uds_pad_ops = {
0efdf0f5 232 .init_cfg = vsp1_entity_init_cfg,
26e0ca22
LP
233 .enum_mbus_code = uds_enum_mbus_code,
234 .enum_frame_size = uds_enum_frame_size,
3f557220 235 .get_fmt = vsp1_subdev_get_pad_format,
26e0ca22
LP
236 .set_fmt = uds_set_format,
237};
238
eb9163d3 239static const struct v4l2_subdev_ops uds_ops = {
26e0ca22
LP
240 .pad = &uds_pad_ops,
241};
242
7b905f05
LP
243/* -----------------------------------------------------------------------------
244 * VSP1 Entity Operations
245 */
246
83dd019d
LP
247static void uds_configure(struct vsp1_entity *entity,
248 struct vsp1_pipeline *pipe,
fc845e52 249 struct vsp1_dl_list *dl, bool full)
7b905f05
LP
250{
251 struct vsp1_uds *uds = to_uds(&entity->subdev);
252 const struct v4l2_mbus_framefmt *output;
253 const struct v4l2_mbus_framefmt *input;
254 unsigned int hscale;
255 unsigned int vscale;
256 bool multitap;
257
fc845e52
LP
258 if (!full)
259 return;
260
7b905f05
LP
261 input = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
262 UDS_PAD_SINK);
263 output = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
264 UDS_PAD_SOURCE);
265
266 hscale = uds_compute_ratio(input->width, output->width);
267 vscale = uds_compute_ratio(input->height, output->height);
268
269 dev_dbg(uds->entity.vsp1->dev, "hscale %u vscale %u\n", hscale, vscale);
270
271 /* Multi-tap scaling can't be enabled along with alpha scaling when
272 * scaling down with a factor lower than or equal to 1/2 in either
273 * direction.
274 */
275 if (uds->scale_alpha && (hscale >= 8192 || vscale >= 8192))
276 multitap = false;
277 else
278 multitap = true;
279
5e8dbbf3 280 vsp1_uds_write(uds, dl, VI6_UDS_CTRL,
7b905f05
LP
281 (uds->scale_alpha ? VI6_UDS_CTRL_AON : 0) |
282 (multitap ? VI6_UDS_CTRL_BC : 0));
283
5e8dbbf3 284 vsp1_uds_write(uds, dl, VI6_UDS_PASS_BWIDTH,
7b905f05
LP
285 (uds_passband_width(hscale)
286 << VI6_UDS_PASS_BWIDTH_H_SHIFT) |
287 (uds_passband_width(vscale)
288 << VI6_UDS_PASS_BWIDTH_V_SHIFT));
289
290 /* Set the scaling ratios and the output size. */
5e8dbbf3 291 vsp1_uds_write(uds, dl, VI6_UDS_SCALE,
7b905f05
LP
292 (hscale << VI6_UDS_SCALE_HFRAC_SHIFT) |
293 (vscale << VI6_UDS_SCALE_VFRAC_SHIFT));
5e8dbbf3 294 vsp1_uds_write(uds, dl, VI6_UDS_CLIP_SIZE,
7b905f05
LP
295 (output->width << VI6_UDS_CLIP_SIZE_HSIZE_SHIFT) |
296 (output->height << VI6_UDS_CLIP_SIZE_VSIZE_SHIFT));
297}
298
299static const struct vsp1_entity_operations uds_entity_ops = {
300 .configure = uds_configure,
301};
302
26e0ca22
LP
303/* -----------------------------------------------------------------------------
304 * Initialization and Cleanup
305 */
306
307struct vsp1_uds *vsp1_uds_create(struct vsp1_device *vsp1, unsigned int index)
308{
26e0ca22 309 struct vsp1_uds *uds;
823329df 310 char name[6];
26e0ca22
LP
311 int ret;
312
313 uds = devm_kzalloc(vsp1->dev, sizeof(*uds), GFP_KERNEL);
314 if (uds == NULL)
315 return ERR_PTR(-ENOMEM);
316
7b905f05 317 uds->entity.ops = &uds_entity_ops;
26e0ca22
LP
318 uds->entity.type = VSP1_ENTITY_UDS;
319 uds->entity.index = index;
26e0ca22 320
823329df 321 sprintf(name, "uds.%u", index);
6a8e07b2
LP
322 ret = vsp1_entity_init(vsp1, &uds->entity, name, 2, &uds_ops,
323 MEDIA_ENT_F_PROC_VIDEO_SCALER);
26e0ca22
LP
324 if (ret < 0)
325 return ERR_PTR(ret);
326
26e0ca22
LP
327 return uds;
328}
This page took 0.211175 seconds and 5 git commands to generate.