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