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