Linux-libre 4.14.69-gnu
[librecmc/linux-libre.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_dl.h"
21 #include "vsp1_pipe.h"
22 #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
34 static inline void vsp1_uds_write(struct vsp1_uds *uds, struct vsp1_dl_list *dl,
35                                   u32 reg, u32 data)
36 {
37         vsp1_dl_list_write(dl, reg + uds->entity.index * VI6_UDS_OFFSET, data);
38 }
39
40 /* -----------------------------------------------------------------------------
41  * Scaling Computation
42  */
43
44 void vsp1_uds_set_alpha(struct vsp1_entity *entity, struct vsp1_dl_list *dl,
45                         unsigned int alpha)
46 {
47         struct vsp1_uds *uds = to_uds(&entity->subdev);
48
49         vsp1_uds_write(uds, dl, VI6_UDS_ALPVAL,
50                        alpha << VI6_UDS_ALPVAL_VAL0_SHIFT);
51 }
52
53 /*
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  */
58 static 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  */
80 static 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  */
91 static 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
107 static 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
113 /* -----------------------------------------------------------------------------
114  * V4L2 Subdevice Pad Operations
115  */
116
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)
120 {
121         static const unsigned int codes[] = {
122                 MEDIA_BUS_FMT_ARGB8888_1X32,
123                 MEDIA_BUS_FMT_AYUV8_1X32,
124         };
125
126         return vsp1_subdev_enum_mbus_code(subdev, cfg, code, codes,
127                                           ARRAY_SIZE(codes));
128 }
129
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)
133 {
134         struct vsp1_uds *uds = to_uds(subdev);
135         struct v4l2_subdev_pad_config *config;
136         struct v4l2_mbus_framefmt *format;
137         int ret = 0;
138
139         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);
145
146         mutex_lock(&uds->entity.lock);
147
148         if (fse->index || fse->code != format->code) {
149                 ret = -EINVAL;
150                 goto done;
151         }
152
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
165 done:
166         mutex_unlock(&uds->entity.lock);
167         return ret;
168 }
169
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)
173 {
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. */
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;
184
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. */
191                 format = vsp1_entity_get_pad_format(&uds->entity, config,
192                                                     UDS_PAD_SINK);
193                 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
206 static int uds_set_format(struct v4l2_subdev *subdev,
207                           struct v4l2_subdev_pad_config *cfg,
208                           struct v4l2_subdev_format *fmt)
209 {
210         struct vsp1_uds *uds = to_uds(subdev);
211         struct v4l2_subdev_pad_config *config;
212         struct v4l2_mbus_framefmt *format;
213         int ret = 0;
214
215         mutex_lock(&uds->entity.lock);
216
217         config = vsp1_entity_get_pad_config(&uds->entity, cfg, fmt->which);
218         if (!config) {
219                 ret = -EINVAL;
220                 goto done;
221         }
222
223         uds_try_format(uds, config, fmt->pad, &fmt->format);
224
225         format = vsp1_entity_get_pad_format(&uds->entity, config, fmt->pad);
226         *format = fmt->format;
227
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,
231                                                     UDS_PAD_SOURCE);
232                 *format = fmt->format;
233
234                 uds_try_format(uds, config, UDS_PAD_SOURCE, format);
235         }
236
237 done:
238         mutex_unlock(&uds->entity.lock);
239         return ret;
240 }
241
242 /* -----------------------------------------------------------------------------
243  * V4L2 Subdevice Operations
244  */
245
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,
252 };
253
254 static const struct v4l2_subdev_ops uds_ops = {
255         .pad    = &uds_pad_ops,
256 };
257
258 /* -----------------------------------------------------------------------------
259  * VSP1 Entity Operations
260  */
261
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)
266 {
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
274         input = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
275                                            UDS_PAD_SINK);
276         output = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
277                                             UDS_PAD_SOURCE);
278
279         if (params == VSP1_ENTITY_PARAMS_PARTITION) {
280                 struct vsp1_partition *partition = pipe->partition;
281
282                 /* Input size clipping */
283                 vsp1_uds_write(uds, dl, VI6_UDS_HSZCLIP, VI6_UDS_HSZCLIP_HCEN |
284                                (0 << VI6_UDS_HSZCLIP_HCL_OFST_SHIFT) |
285                                (partition->uds_sink.width
286                                         << VI6_UDS_HSZCLIP_HCL_SIZE_SHIFT));
287
288                 /* Output size clipping */
289                 vsp1_uds_write(uds, dl, VI6_UDS_CLIP_SIZE,
290                                (partition->uds_source.width
291                                         << VI6_UDS_CLIP_SIZE_HSIZE_SHIFT) |
292                                (output->height
293                                         << VI6_UDS_CLIP_SIZE_VSIZE_SHIFT));
294                 return;
295         }
296
297         if (params != VSP1_ENTITY_PARAMS_INIT)
298                 return;
299
300         hscale = uds_compute_ratio(input->width, output->width);
301         vscale = uds_compute_ratio(input->height, output->height);
302
303         dev_dbg(uds->entity.vsp1->dev, "hscale %u vscale %u\n", hscale, vscale);
304
305         /*
306          * Multi-tap scaling can't be enabled along with alpha scaling when
307          * scaling down with a factor lower than or equal to 1/2 in either
308          * direction.
309          */
310         if (uds->scale_alpha && (hscale >= 8192 || vscale >= 8192))
311                 multitap = false;
312         else
313                 multitap = true;
314
315         vsp1_uds_write(uds, dl, VI6_UDS_CTRL,
316                        (uds->scale_alpha ? VI6_UDS_CTRL_AON : 0) |
317                        (multitap ? VI6_UDS_CTRL_BC : 0));
318
319         vsp1_uds_write(uds, dl, VI6_UDS_PASS_BWIDTH,
320                        (uds_passband_width(hscale)
321                                 << VI6_UDS_PASS_BWIDTH_H_SHIFT) |
322                        (uds_passband_width(vscale)
323                                 << VI6_UDS_PASS_BWIDTH_V_SHIFT));
324
325         /* Set the scaling ratios. */
326         vsp1_uds_write(uds, dl, VI6_UDS_SCALE,
327                        (hscale << VI6_UDS_SCALE_HFRAC_SHIFT) |
328                        (vscale << VI6_UDS_SCALE_VFRAC_SHIFT));
329 }
330
331 static unsigned int uds_max_width(struct vsp1_entity *entity,
332                                   struct vsp1_pipeline *pipe)
333 {
334         struct vsp1_uds *uds = to_uds(&entity->subdev);
335         const struct v4l2_mbus_framefmt *output;
336         const struct v4l2_mbus_framefmt *input;
337         unsigned int hscale;
338
339         input = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
340                                            UDS_PAD_SINK);
341         output = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
342                                             UDS_PAD_SOURCE);
343         hscale = output->width / input->width;
344
345         if (hscale <= 2)
346                 return 256;
347         else if (hscale <= 4)
348                 return 512;
349         else if (hscale <= 8)
350                 return 1024;
351         else
352                 return 2048;
353 }
354
355 /* -----------------------------------------------------------------------------
356  * Partition Algorithm Support
357  */
358
359 static void uds_partition(struct vsp1_entity *entity,
360                           struct vsp1_pipeline *pipe,
361                           struct vsp1_partition *partition,
362                           unsigned int partition_idx,
363                           struct vsp1_partition_window *window)
364 {
365         struct vsp1_uds *uds = to_uds(&entity->subdev);
366         const struct v4l2_mbus_framefmt *output;
367         const struct v4l2_mbus_framefmt *input;
368
369         /* Initialise the partition state */
370         partition->uds_sink = *window;
371         partition->uds_source = *window;
372
373         input = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
374                                            UDS_PAD_SINK);
375         output = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
376                                             UDS_PAD_SOURCE);
377
378         partition->uds_sink.width = window->width * input->width
379                                   / output->width;
380         partition->uds_sink.left = window->left * input->width
381                                  / output->width;
382
383         *window = partition->uds_sink;
384 }
385
386 static const struct vsp1_entity_operations uds_entity_ops = {
387         .configure = uds_configure,
388         .max_width = uds_max_width,
389         .partition = uds_partition,
390 };
391
392 /* -----------------------------------------------------------------------------
393  * Initialization and Cleanup
394  */
395
396 struct vsp1_uds *vsp1_uds_create(struct vsp1_device *vsp1, unsigned int index)
397 {
398         struct vsp1_uds *uds;
399         char name[6];
400         int ret;
401
402         uds = devm_kzalloc(vsp1->dev, sizeof(*uds), GFP_KERNEL);
403         if (uds == NULL)
404                 return ERR_PTR(-ENOMEM);
405
406         uds->entity.ops = &uds_entity_ops;
407         uds->entity.type = VSP1_ENTITY_UDS;
408         uds->entity.index = index;
409
410         sprintf(name, "uds.%u", index);
411         ret = vsp1_entity_init(vsp1, &uds->entity, name, 2, &uds_ops,
412                                MEDIA_ENT_F_PROC_VIDEO_SCALER);
413         if (ret < 0)
414                 return ERR_PTR(ret);
415
416         return uds;
417 }