Merge branch 'akpm' (patches from Andrew)
[cascardo/linux.git] / drivers / media / platform / vsp1 / vsp1_sru.c
1 /*
2  * vsp1_sru.c  --  R-Car VSP1 Super Resolution Unit
3  *
4  * Copyright (C) 2013 Renesas 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_sru.h"
22
23 #define SRU_MIN_SIZE                            4U
24 #define SRU_MAX_SIZE                            8190U
25
26 /* -----------------------------------------------------------------------------
27  * Device Access
28  */
29
30 static inline void vsp1_sru_write(struct vsp1_sru *sru, struct vsp1_dl_list *dl,
31                                   u32 reg, u32 data)
32 {
33         vsp1_dl_list_write(dl, reg, data);
34 }
35
36 /* -----------------------------------------------------------------------------
37  * Controls
38  */
39
40 #define V4L2_CID_VSP1_SRU_INTENSITY             (V4L2_CID_USER_BASE | 0x1001)
41
42 struct vsp1_sru_param {
43         u32 ctrl0;
44         u32 ctrl2;
45 };
46
47 #define VI6_SRU_CTRL0_PARAMS(p0, p1)                    \
48         (((p0) << VI6_SRU_CTRL0_PARAM0_SHIFT) |         \
49          ((p1) << VI6_SRU_CTRL0_PARAM1_SHIFT))
50
51 #define VI6_SRU_CTRL2_PARAMS(p6, p7, p8)                \
52         (((p6) << VI6_SRU_CTRL2_PARAM6_SHIFT) |         \
53          ((p7) << VI6_SRU_CTRL2_PARAM7_SHIFT) |         \
54          ((p8) << VI6_SRU_CTRL2_PARAM8_SHIFT))
55
56 static const struct vsp1_sru_param vsp1_sru_params[] = {
57         {
58                 .ctrl0 = VI6_SRU_CTRL0_PARAMS(256, 4) | VI6_SRU_CTRL0_EN,
59                 .ctrl2 = VI6_SRU_CTRL2_PARAMS(24, 40, 255),
60         }, {
61                 .ctrl0 = VI6_SRU_CTRL0_PARAMS(256, 4) | VI6_SRU_CTRL0_EN,
62                 .ctrl2 = VI6_SRU_CTRL2_PARAMS(8, 16, 255),
63         }, {
64                 .ctrl0 = VI6_SRU_CTRL0_PARAMS(384, 5) | VI6_SRU_CTRL0_EN,
65                 .ctrl2 = VI6_SRU_CTRL2_PARAMS(36, 60, 255),
66         }, {
67                 .ctrl0 = VI6_SRU_CTRL0_PARAMS(384, 5) | VI6_SRU_CTRL0_EN,
68                 .ctrl2 = VI6_SRU_CTRL2_PARAMS(12, 27, 255),
69         }, {
70                 .ctrl0 = VI6_SRU_CTRL0_PARAMS(511, 6) | VI6_SRU_CTRL0_EN,
71                 .ctrl2 = VI6_SRU_CTRL2_PARAMS(48, 80, 255),
72         }, {
73                 .ctrl0 = VI6_SRU_CTRL0_PARAMS(511, 6) | VI6_SRU_CTRL0_EN,
74                 .ctrl2 = VI6_SRU_CTRL2_PARAMS(16, 36, 255),
75         },
76 };
77
78 static int sru_s_ctrl(struct v4l2_ctrl *ctrl)
79 {
80         struct vsp1_sru *sru =
81                 container_of(ctrl->handler, struct vsp1_sru, ctrls);
82
83         switch (ctrl->id) {
84         case V4L2_CID_VSP1_SRU_INTENSITY:
85                 sru->intensity = ctrl->val;
86                 break;
87         }
88
89         return 0;
90 }
91
92 static const struct v4l2_ctrl_ops sru_ctrl_ops = {
93         .s_ctrl = sru_s_ctrl,
94 };
95
96 static const struct v4l2_ctrl_config sru_intensity_control = {
97         .ops = &sru_ctrl_ops,
98         .id = V4L2_CID_VSP1_SRU_INTENSITY,
99         .name = "Intensity",
100         .type = V4L2_CTRL_TYPE_INTEGER,
101         .min = 1,
102         .max = 6,
103         .def = 1,
104         .step = 1,
105 };
106
107 /* -----------------------------------------------------------------------------
108  * V4L2 Subdevice Operations
109  */
110
111 static int sru_enum_mbus_code(struct v4l2_subdev *subdev,
112                               struct v4l2_subdev_pad_config *cfg,
113                               struct v4l2_subdev_mbus_code_enum *code)
114 {
115         static const unsigned int codes[] = {
116                 MEDIA_BUS_FMT_ARGB8888_1X32,
117                 MEDIA_BUS_FMT_AYUV8_1X32,
118         };
119
120         return vsp1_subdev_enum_mbus_code(subdev, cfg, code, codes,
121                                           ARRAY_SIZE(codes));
122 }
123
124 static int sru_enum_frame_size(struct v4l2_subdev *subdev,
125                                struct v4l2_subdev_pad_config *cfg,
126                                struct v4l2_subdev_frame_size_enum *fse)
127 {
128         struct vsp1_sru *sru = to_sru(subdev);
129         struct v4l2_subdev_pad_config *config;
130         struct v4l2_mbus_framefmt *format;
131         int ret = 0;
132
133         config = vsp1_entity_get_pad_config(&sru->entity, cfg, fse->which);
134         if (!config)
135                 return -EINVAL;
136
137         format = vsp1_entity_get_pad_format(&sru->entity, config, SRU_PAD_SINK);
138
139         mutex_lock(&sru->entity.lock);
140
141         if (fse->index || fse->code != format->code) {
142                 ret = -EINVAL;
143                 goto done;
144         }
145
146         if (fse->pad == SRU_PAD_SINK) {
147                 fse->min_width = SRU_MIN_SIZE;
148                 fse->max_width = SRU_MAX_SIZE;
149                 fse->min_height = SRU_MIN_SIZE;
150                 fse->max_height = SRU_MAX_SIZE;
151         } else {
152                 fse->min_width = format->width;
153                 fse->min_height = format->height;
154                 if (format->width <= SRU_MAX_SIZE / 2 &&
155                     format->height <= SRU_MAX_SIZE / 2) {
156                         fse->max_width = format->width * 2;
157                         fse->max_height = format->height * 2;
158                 } else {
159                         fse->max_width = format->width;
160                         fse->max_height = format->height;
161                 }
162         }
163
164 done:
165         mutex_unlock(&sru->entity.lock);
166         return ret;
167 }
168
169 static void sru_try_format(struct vsp1_sru *sru,
170                            struct v4l2_subdev_pad_config *config,
171                            unsigned int pad, struct v4l2_mbus_framefmt *fmt)
172 {
173         struct v4l2_mbus_framefmt *format;
174         unsigned int input_area;
175         unsigned int output_area;
176
177         switch (pad) {
178         case SRU_PAD_SINK:
179                 /* Default to YUV if the requested format is not supported. */
180                 if (fmt->code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
181                     fmt->code != MEDIA_BUS_FMT_AYUV8_1X32)
182                         fmt->code = MEDIA_BUS_FMT_AYUV8_1X32;
183
184                 fmt->width = clamp(fmt->width, SRU_MIN_SIZE, SRU_MAX_SIZE);
185                 fmt->height = clamp(fmt->height, SRU_MIN_SIZE, SRU_MAX_SIZE);
186                 break;
187
188         case SRU_PAD_SOURCE:
189                 /* The SRU can't perform format conversion. */
190                 format = vsp1_entity_get_pad_format(&sru->entity, config,
191                                                     SRU_PAD_SINK);
192                 fmt->code = format->code;
193
194                 /* We can upscale by 2 in both direction, but not independently.
195                  * Compare the input and output rectangles areas (avoiding
196                  * integer overflows on the output): if the requested output
197                  * area is larger than 1.5^2 the input area upscale by two,
198                  * otherwise don't scale.
199                  */
200                 input_area = format->width * format->height;
201                 output_area = min(fmt->width, SRU_MAX_SIZE)
202                             * min(fmt->height, SRU_MAX_SIZE);
203
204                 if (fmt->width <= SRU_MAX_SIZE / 2 &&
205                     fmt->height <= SRU_MAX_SIZE / 2 &&
206                     output_area > input_area * 9 / 4) {
207                         fmt->width = format->width * 2;
208                         fmt->height = format->height * 2;
209                 } else {
210                         fmt->width = format->width;
211                         fmt->height = format->height;
212                 }
213                 break;
214         }
215
216         fmt->field = V4L2_FIELD_NONE;
217         fmt->colorspace = V4L2_COLORSPACE_SRGB;
218 }
219
220 static int sru_set_format(struct v4l2_subdev *subdev,
221                           struct v4l2_subdev_pad_config *cfg,
222                           struct v4l2_subdev_format *fmt)
223 {
224         struct vsp1_sru *sru = to_sru(subdev);
225         struct v4l2_subdev_pad_config *config;
226         struct v4l2_mbus_framefmt *format;
227         int ret = 0;
228
229         mutex_lock(&sru->entity.lock);
230
231         config = vsp1_entity_get_pad_config(&sru->entity, cfg, fmt->which);
232         if (!config) {
233                 ret = -EINVAL;
234                 goto done;
235         }
236
237         sru_try_format(sru, config, fmt->pad, &fmt->format);
238
239         format = vsp1_entity_get_pad_format(&sru->entity, config, fmt->pad);
240         *format = fmt->format;
241
242         if (fmt->pad == SRU_PAD_SINK) {
243                 /* Propagate the format to the source pad. */
244                 format = vsp1_entity_get_pad_format(&sru->entity, config,
245                                                     SRU_PAD_SOURCE);
246                 *format = fmt->format;
247
248                 sru_try_format(sru, config, SRU_PAD_SOURCE, format);
249         }
250
251 done:
252         mutex_unlock(&sru->entity.lock);
253         return ret;
254 }
255
256 static const struct v4l2_subdev_pad_ops sru_pad_ops = {
257         .init_cfg = vsp1_entity_init_cfg,
258         .enum_mbus_code = sru_enum_mbus_code,
259         .enum_frame_size = sru_enum_frame_size,
260         .get_fmt = vsp1_subdev_get_pad_format,
261         .set_fmt = sru_set_format,
262 };
263
264 static const struct v4l2_subdev_ops sru_ops = {
265         .pad    = &sru_pad_ops,
266 };
267
268 /* -----------------------------------------------------------------------------
269  * VSP1 Entity Operations
270  */
271
272 static void sru_configure(struct vsp1_entity *entity,
273                           struct vsp1_pipeline *pipe,
274                           struct vsp1_dl_list *dl,
275                           enum vsp1_entity_params params)
276 {
277         const struct vsp1_sru_param *param;
278         struct vsp1_sru *sru = to_sru(&entity->subdev);
279         struct v4l2_mbus_framefmt *input;
280         struct v4l2_mbus_framefmt *output;
281         u32 ctrl0;
282
283         if (params != VSP1_ENTITY_PARAMS_INIT)
284                 return;
285
286         input = vsp1_entity_get_pad_format(&sru->entity, sru->entity.config,
287                                            SRU_PAD_SINK);
288         output = vsp1_entity_get_pad_format(&sru->entity, sru->entity.config,
289                                             SRU_PAD_SOURCE);
290
291         if (input->code == MEDIA_BUS_FMT_ARGB8888_1X32)
292                 ctrl0 = VI6_SRU_CTRL0_PARAM2 | VI6_SRU_CTRL0_PARAM3
293                       | VI6_SRU_CTRL0_PARAM4;
294         else
295                 ctrl0 = VI6_SRU_CTRL0_PARAM3;
296
297         if (input->width != output->width)
298                 ctrl0 |= VI6_SRU_CTRL0_MODE_UPSCALE;
299
300         param = &vsp1_sru_params[sru->intensity - 1];
301
302         ctrl0 |= param->ctrl0;
303
304         vsp1_sru_write(sru, dl, VI6_SRU_CTRL0, ctrl0);
305         vsp1_sru_write(sru, dl, VI6_SRU_CTRL1, VI6_SRU_CTRL1_PARAM5);
306         vsp1_sru_write(sru, dl, VI6_SRU_CTRL2, param->ctrl2);
307 }
308
309 static unsigned int sru_max_width(struct vsp1_entity *entity,
310                                   struct vsp1_pipeline *pipe)
311 {
312         struct vsp1_sru *sru = to_sru(&entity->subdev);
313         struct v4l2_mbus_framefmt *input;
314         struct v4l2_mbus_framefmt *output;
315
316         input = vsp1_entity_get_pad_format(&sru->entity, sru->entity.config,
317                                            SRU_PAD_SINK);
318         output = vsp1_entity_get_pad_format(&sru->entity, sru->entity.config,
319                                             SRU_PAD_SOURCE);
320
321         if (input->width != output->width)
322                 return 512;
323         else
324                 return 256;
325 }
326
327 static const struct vsp1_entity_operations sru_entity_ops = {
328         .configure = sru_configure,
329         .max_width = sru_max_width,
330 };
331
332 /* -----------------------------------------------------------------------------
333  * Initialization and Cleanup
334  */
335
336 struct vsp1_sru *vsp1_sru_create(struct vsp1_device *vsp1)
337 {
338         struct vsp1_sru *sru;
339         int ret;
340
341         sru = devm_kzalloc(vsp1->dev, sizeof(*sru), GFP_KERNEL);
342         if (sru == NULL)
343                 return ERR_PTR(-ENOMEM);
344
345         sru->entity.ops = &sru_entity_ops;
346         sru->entity.type = VSP1_ENTITY_SRU;
347
348         ret = vsp1_entity_init(vsp1, &sru->entity, "sru", 2, &sru_ops,
349                                MEDIA_ENT_F_PROC_VIDEO_SCALER);
350         if (ret < 0)
351                 return ERR_PTR(ret);
352
353         /* Initialize the control handler. */
354         v4l2_ctrl_handler_init(&sru->ctrls, 1);
355         v4l2_ctrl_new_custom(&sru->ctrls, &sru_intensity_control, NULL);
356
357         sru->intensity = 1;
358
359         sru->entity.subdev.ctrl_handler = &sru->ctrls;
360
361         if (sru->ctrls.error) {
362                 dev_err(vsp1->dev, "sru: failed to initialize controls\n");
363                 ret = sru->ctrls.error;
364                 vsp1_entity_destroy(&sru->entity);
365                 return ERR_PTR(ret);
366         }
367
368         return sru;
369 }