v4l: vsp1: uds: Enable scaling of alpha layer
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / media / platform / vsp1 / vsp1_uds.c
1 /*
2  * vsp1_uds.c  --  R-Car VSP1 Up and Down Scaler
3  *
4  * Copyright (C) 2013-2014 Renesas Electronics Corporation
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"
20 #include "vsp1_uds.h"
21
22 #define UDS_MIN_SIZE                            4U
23 #define UDS_MAX_SIZE                            8190U
24
25 #define UDS_MIN_FACTOR                          0x0100
26 #define UDS_MAX_FACTOR                          0xffff
27
28 /* -----------------------------------------------------------------------------
29  * Device Access
30  */
31
32 static inline u32 vsp1_uds_read(struct vsp1_uds *uds, u32 reg)
33 {
34         return vsp1_read(uds->entity.vsp1,
35                          reg + uds->entity.index * VI6_UDS_OFFSET);
36 }
37
38 static inline void vsp1_uds_write(struct vsp1_uds *uds, u32 reg, u32 data)
39 {
40         vsp1_write(uds->entity.vsp1,
41                    reg + uds->entity.index * VI6_UDS_OFFSET, data);
42 }
43
44 /* -----------------------------------------------------------------------------
45  * Scaling Computation
46  */
47
48 /*
49  * uds_output_size - Return the output size for an input size and scaling ratio
50  * @input: input size in pixels
51  * @ratio: scaling ratio in U4.12 fixed-point format
52  */
53 static unsigned int uds_output_size(unsigned int input, unsigned int ratio)
54 {
55         if (ratio > 4096) {
56                 /* Down-scaling */
57                 unsigned int mp;
58
59                 mp = ratio / 4096;
60                 mp = mp < 4 ? 1 : (mp < 8 ? 2 : 4);
61
62                 return (input - 1) / mp * mp * 4096 / ratio + 1;
63         } else {
64                 /* Up-scaling */
65                 return (input - 1) * 4096 / ratio + 1;
66         }
67 }
68
69 /*
70  * uds_output_limits - Return the min and max output sizes for an input size
71  * @input: input size in pixels
72  * @minimum: minimum output size (returned)
73  * @maximum: maximum output size (returned)
74  */
75 static void uds_output_limits(unsigned int input,
76                               unsigned int *minimum, unsigned int *maximum)
77 {
78         *minimum = max(uds_output_size(input, UDS_MAX_FACTOR), UDS_MIN_SIZE);
79         *maximum = min(uds_output_size(input, UDS_MIN_FACTOR), UDS_MAX_SIZE);
80 }
81
82 /*
83  * uds_passband_width - Return the passband filter width for a scaling ratio
84  * @ratio: scaling ratio in U4.12 fixed-point format
85  */
86 static unsigned int uds_passband_width(unsigned int ratio)
87 {
88         if (ratio >= 4096) {
89                 /* Down-scaling */
90                 unsigned int mp;
91
92                 mp = ratio / 4096;
93                 mp = mp < 4 ? 1 : (mp < 8 ? 2 : 4);
94
95                 return 64 * 4096 * mp / ratio;
96         } else {
97                 /* Up-scaling */
98                 return 64;
99         }
100 }
101
102 static unsigned int uds_compute_ratio(unsigned int input, unsigned int output)
103 {
104         /* TODO: This is an approximation that will need to be refined. */
105         return (input - 1) * 4096 / (output - 1);
106 }
107
108 static void uds_compute_ratios(struct vsp1_uds *uds)
109 {
110         struct v4l2_mbus_framefmt *input = &uds->entity.formats[UDS_PAD_SINK];
111         struct v4l2_mbus_framefmt *output =
112                 &uds->entity.formats[UDS_PAD_SOURCE];
113
114         uds->hscale = uds_compute_ratio(input->width, output->width);
115         uds->vscale = uds_compute_ratio(input->height, output->height);
116
117         dev_dbg(uds->entity.vsp1->dev, "hscale %u vscale %u\n",
118                 uds->hscale, uds->vscale);
119 }
120
121 /* -----------------------------------------------------------------------------
122  * V4L2 Subdevice Core Operations
123  */
124
125 static int uds_s_stream(struct v4l2_subdev *subdev, int enable)
126 {
127         const struct v4l2_mbus_framefmt *format;
128         struct vsp1_uds *uds = to_uds(subdev);
129
130         if (!enable)
131                 return 0;
132
133         /* Enable multi-tap scaling. */
134         vsp1_uds_write(uds, VI6_UDS_CTRL, VI6_UDS_CTRL_AON | VI6_UDS_CTRL_BC);
135
136         vsp1_uds_write(uds, VI6_UDS_PASS_BWIDTH,
137                        (uds_passband_width(uds->hscale)
138                                 << VI6_UDS_PASS_BWIDTH_H_SHIFT) |
139                        (uds_passband_width(uds->vscale)
140                                 << VI6_UDS_PASS_BWIDTH_V_SHIFT));
141
142         /* Set the scaling ratios and the output size. */
143         format = &uds->entity.formats[UDS_PAD_SOURCE];
144
145         vsp1_uds_write(uds, VI6_UDS_SCALE,
146                        (uds->hscale << VI6_UDS_SCALE_HFRAC_SHIFT) |
147                        (uds->vscale << VI6_UDS_SCALE_VFRAC_SHIFT));
148         vsp1_uds_write(uds, VI6_UDS_CLIP_SIZE,
149                        (format->width << VI6_UDS_CLIP_SIZE_HSIZE_SHIFT) |
150                        (format->height << VI6_UDS_CLIP_SIZE_VSIZE_SHIFT));
151
152         return 0;
153 }
154
155 /* -----------------------------------------------------------------------------
156  * V4L2 Subdevice Pad Operations
157  */
158
159 static int uds_enum_mbus_code(struct v4l2_subdev *subdev,
160                               struct v4l2_subdev_fh *fh,
161                               struct v4l2_subdev_mbus_code_enum *code)
162 {
163         static const unsigned int codes[] = {
164                 V4L2_MBUS_FMT_ARGB8888_1X32,
165                 V4L2_MBUS_FMT_AYUV8_1X32,
166         };
167
168         if (code->pad == UDS_PAD_SINK) {
169                 if (code->index >= ARRAY_SIZE(codes))
170                         return -EINVAL;
171
172                 code->code = codes[code->index];
173         } else {
174                 struct v4l2_mbus_framefmt *format;
175
176                 /* The UDS can't perform format conversion, the sink format is
177                  * always identical to the source format.
178                  */
179                 if (code->index)
180                         return -EINVAL;
181
182                 format = v4l2_subdev_get_try_format(fh, UDS_PAD_SINK);
183                 code->code = format->code;
184         }
185
186         return 0;
187 }
188
189 static int uds_enum_frame_size(struct v4l2_subdev *subdev,
190                                struct v4l2_subdev_fh *fh,
191                                struct v4l2_subdev_frame_size_enum *fse)
192 {
193         struct v4l2_mbus_framefmt *format;
194
195         format = v4l2_subdev_get_try_format(fh, UDS_PAD_SINK);
196
197         if (fse->index || fse->code != format->code)
198                 return -EINVAL;
199
200         if (fse->pad == UDS_PAD_SINK) {
201                 fse->min_width = UDS_MIN_SIZE;
202                 fse->max_width = UDS_MAX_SIZE;
203                 fse->min_height = UDS_MIN_SIZE;
204                 fse->max_height = UDS_MAX_SIZE;
205         } else {
206                 uds_output_limits(format->width, &fse->min_width,
207                                   &fse->max_width);
208                 uds_output_limits(format->height, &fse->min_height,
209                                   &fse->max_height);
210         }
211
212         return 0;
213 }
214
215 static int uds_get_format(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh,
216                           struct v4l2_subdev_format *fmt)
217 {
218         struct vsp1_uds *uds = to_uds(subdev);
219
220         fmt->format = *vsp1_entity_get_pad_format(&uds->entity, fh, fmt->pad,
221                                                   fmt->which);
222
223         return 0;
224 }
225
226 static void uds_try_format(struct vsp1_uds *uds, struct v4l2_subdev_fh *fh,
227                            unsigned int pad, struct v4l2_mbus_framefmt *fmt,
228                            enum v4l2_subdev_format_whence which)
229 {
230         struct v4l2_mbus_framefmt *format;
231         unsigned int minimum;
232         unsigned int maximum;
233
234         switch (pad) {
235         case UDS_PAD_SINK:
236                 /* Default to YUV if the requested format is not supported. */
237                 if (fmt->code != V4L2_MBUS_FMT_ARGB8888_1X32 &&
238                     fmt->code != V4L2_MBUS_FMT_AYUV8_1X32)
239                         fmt->code = V4L2_MBUS_FMT_AYUV8_1X32;
240
241                 fmt->width = clamp(fmt->width, UDS_MIN_SIZE, UDS_MAX_SIZE);
242                 fmt->height = clamp(fmt->height, UDS_MIN_SIZE, UDS_MAX_SIZE);
243                 break;
244
245         case UDS_PAD_SOURCE:
246                 /* The UDS scales but can't perform format conversion. */
247                 format = vsp1_entity_get_pad_format(&uds->entity, fh,
248                                                     UDS_PAD_SINK, which);
249                 fmt->code = format->code;
250
251                 uds_output_limits(format->width, &minimum, &maximum);
252                 fmt->width = clamp(fmt->width, minimum, maximum);
253                 uds_output_limits(format->height, &minimum, &maximum);
254                 fmt->height = clamp(fmt->height, minimum, maximum);
255                 break;
256         }
257
258         fmt->field = V4L2_FIELD_NONE;
259         fmt->colorspace = V4L2_COLORSPACE_SRGB;
260 }
261
262 static int uds_set_format(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh,
263                           struct v4l2_subdev_format *fmt)
264 {
265         struct vsp1_uds *uds = to_uds(subdev);
266         struct v4l2_mbus_framefmt *format;
267
268         uds_try_format(uds, fh, fmt->pad, &fmt->format, fmt->which);
269
270         format = vsp1_entity_get_pad_format(&uds->entity, fh, fmt->pad,
271                                             fmt->which);
272         *format = fmt->format;
273
274         if (fmt->pad == UDS_PAD_SINK) {
275                 /* Propagate the format to the source pad. */
276                 format = vsp1_entity_get_pad_format(&uds->entity, fh,
277                                                     UDS_PAD_SOURCE, fmt->which);
278                 *format = fmt->format;
279
280                 uds_try_format(uds, fh, UDS_PAD_SOURCE, format, fmt->which);
281         }
282
283         if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE)
284                 uds_compute_ratios(uds);
285
286         return 0;
287 }
288
289 /* -----------------------------------------------------------------------------
290  * V4L2 Subdevice Operations
291  */
292
293 static struct v4l2_subdev_video_ops uds_video_ops = {
294         .s_stream = uds_s_stream,
295 };
296
297 static struct v4l2_subdev_pad_ops uds_pad_ops = {
298         .enum_mbus_code = uds_enum_mbus_code,
299         .enum_frame_size = uds_enum_frame_size,
300         .get_fmt = uds_get_format,
301         .set_fmt = uds_set_format,
302 };
303
304 static struct v4l2_subdev_ops uds_ops = {
305         .video  = &uds_video_ops,
306         .pad    = &uds_pad_ops,
307 };
308
309 /* -----------------------------------------------------------------------------
310  * Initialization and Cleanup
311  */
312
313 struct vsp1_uds *vsp1_uds_create(struct vsp1_device *vsp1, unsigned int index)
314 {
315         struct v4l2_subdev *subdev;
316         struct vsp1_uds *uds;
317         int ret;
318
319         uds = devm_kzalloc(vsp1->dev, sizeof(*uds), GFP_KERNEL);
320         if (uds == NULL)
321                 return ERR_PTR(-ENOMEM);
322
323         uds->entity.type = VSP1_ENTITY_UDS;
324         uds->entity.index = index;
325         uds->entity.id = VI6_DPR_NODE_UDS(index);
326
327         ret = vsp1_entity_init(vsp1, &uds->entity, 2);
328         if (ret < 0)
329                 return ERR_PTR(ret);
330
331         /* Initialize the V4L2 subdev. */
332         subdev = &uds->entity.subdev;
333         v4l2_subdev_init(subdev, &uds_ops);
334
335         subdev->entity.ops = &vsp1_media_ops;
336         subdev->internal_ops = &vsp1_subdev_internal_ops;
337         snprintf(subdev->name, sizeof(subdev->name), "%s uds.%u",
338                  dev_name(vsp1->dev), index);
339         v4l2_set_subdevdata(subdev, uds);
340         subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
341
342         vsp1_entity_init_formats(subdev, NULL);
343
344         return uds;
345 }