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