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" | |
5e8dbbf3 | 20 | #include "vsp1_dl.h" |
a626e64e LP |
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 | ||
5e8dbbf3 LP |
30 | static inline void vsp1_sru_write(struct vsp1_sru *sru, struct vsp1_dl_list *dl, |
31 | u32 reg, u32 data) | |
a626e64e | 32 | { |
5e8dbbf3 | 33 | vsp1_dl_list_write(dl, reg, data); |
a626e64e LP |
34 | } |
35 | ||
36 | /* ----------------------------------------------------------------------------- | |
37 | * Controls | |
38 | */ | |
39 | ||
40 | #define V4L2_CID_VSP1_SRU_INTENSITY (V4L2_CID_USER_BASE + 1) | |
41 | ||
a626e64e LP |
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 | ||
58f896d8 LP |
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); | |
58f896d8 LP |
82 | |
83 | switch (ctrl->id) { | |
84 | case V4L2_CID_VSP1_SRU_INTENSITY: | |
d884a8b2 | 85 | sru->intensity = ctrl->val; |
58f896d8 LP |
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 | /* ----------------------------------------------------------------------------- | |
7b905f05 | 108 | * V4L2 Subdevice Operations |
a626e64e LP |
109 | */ |
110 | ||
111 | static int sru_enum_mbus_code(struct v4l2_subdev *subdev, | |
f7234138 | 112 | struct v4l2_subdev_pad_config *cfg, |
a626e64e LP |
113 | struct v4l2_subdev_mbus_code_enum *code) |
114 | { | |
115 | static const unsigned int codes[] = { | |
27ffaeb0 BB |
116 | MEDIA_BUS_FMT_ARGB8888_1X32, |
117 | MEDIA_BUS_FMT_AYUV8_1X32, | |
a626e64e | 118 | }; |
3f1ccf16 | 119 | struct vsp1_sru *sru = to_sru(subdev); |
a626e64e LP |
120 | |
121 | if (code->pad == SRU_PAD_SINK) { | |
122 | if (code->index >= ARRAY_SIZE(codes)) | |
123 | return -EINVAL; | |
124 | ||
125 | code->code = codes[code->index]; | |
126 | } else { | |
e790c3cb LP |
127 | struct v4l2_subdev_pad_config *config; |
128 | struct v4l2_mbus_framefmt *format; | |
129 | ||
a626e64e LP |
130 | /* The SRU can't perform format conversion, the sink format is |
131 | * always identical to the source format. | |
132 | */ | |
133 | if (code->index) | |
134 | return -EINVAL; | |
135 | ||
e790c3cb LP |
136 | config = vsp1_entity_get_pad_config(&sru->entity, cfg, |
137 | code->which); | |
138 | if (!config) | |
139 | return -EINVAL; | |
140 | ||
141 | format = vsp1_entity_get_pad_format(&sru->entity, config, | |
142 | SRU_PAD_SINK); | |
a626e64e LP |
143 | code->code = format->code; |
144 | } | |
145 | ||
146 | return 0; | |
147 | } | |
148 | ||
149 | static int sru_enum_frame_size(struct v4l2_subdev *subdev, | |
f7234138 | 150 | struct v4l2_subdev_pad_config *cfg, |
a626e64e LP |
151 | struct v4l2_subdev_frame_size_enum *fse) |
152 | { | |
5778e749 | 153 | struct vsp1_sru *sru = to_sru(subdev); |
e790c3cb | 154 | struct v4l2_subdev_pad_config *config; |
a626e64e LP |
155 | struct v4l2_mbus_framefmt *format; |
156 | ||
e790c3cb LP |
157 | config = vsp1_entity_get_pad_config(&sru->entity, cfg, fse->which); |
158 | if (!config) | |
159 | return -EINVAL; | |
160 | ||
161 | format = vsp1_entity_get_pad_format(&sru->entity, config, SRU_PAD_SINK); | |
a626e64e LP |
162 | |
163 | if (fse->index || fse->code != format->code) | |
164 | return -EINVAL; | |
165 | ||
166 | if (fse->pad == SRU_PAD_SINK) { | |
167 | fse->min_width = SRU_MIN_SIZE; | |
168 | fse->max_width = SRU_MAX_SIZE; | |
169 | fse->min_height = SRU_MIN_SIZE; | |
170 | fse->max_height = SRU_MAX_SIZE; | |
171 | } else { | |
172 | fse->min_width = format->width; | |
173 | fse->min_height = format->height; | |
174 | if (format->width <= SRU_MAX_SIZE / 2 && | |
175 | format->height <= SRU_MAX_SIZE / 2) { | |
176 | fse->max_width = format->width * 2; | |
177 | fse->max_height = format->height * 2; | |
178 | } else { | |
179 | fse->max_width = format->width; | |
180 | fse->max_height = format->height; | |
181 | } | |
182 | } | |
183 | ||
184 | return 0; | |
185 | } | |
186 | ||
1bd0a1bd LP |
187 | static int sru_get_format(struct v4l2_subdev *subdev, |
188 | struct v4l2_subdev_pad_config *cfg, | |
a626e64e LP |
189 | struct v4l2_subdev_format *fmt) |
190 | { | |
191 | struct vsp1_sru *sru = to_sru(subdev); | |
e790c3cb LP |
192 | struct v4l2_subdev_pad_config *config; |
193 | ||
194 | config = vsp1_entity_get_pad_config(&sru->entity, cfg, fmt->which); | |
195 | if (!config) | |
196 | return -EINVAL; | |
a626e64e | 197 | |
e790c3cb LP |
198 | fmt->format = *vsp1_entity_get_pad_format(&sru->entity, config, |
199 | fmt->pad); | |
a626e64e LP |
200 | |
201 | return 0; | |
202 | } | |
203 | ||
1bd0a1bd | 204 | static void sru_try_format(struct vsp1_sru *sru, |
e790c3cb LP |
205 | struct v4l2_subdev_pad_config *config, |
206 | unsigned int pad, struct v4l2_mbus_framefmt *fmt) | |
a626e64e LP |
207 | { |
208 | struct v4l2_mbus_framefmt *format; | |
209 | unsigned int input_area; | |
210 | unsigned int output_area; | |
211 | ||
212 | switch (pad) { | |
213 | case SRU_PAD_SINK: | |
214 | /* Default to YUV if the requested format is not supported. */ | |
27ffaeb0 BB |
215 | if (fmt->code != MEDIA_BUS_FMT_ARGB8888_1X32 && |
216 | fmt->code != MEDIA_BUS_FMT_AYUV8_1X32) | |
217 | fmt->code = MEDIA_BUS_FMT_AYUV8_1X32; | |
a626e64e LP |
218 | |
219 | fmt->width = clamp(fmt->width, SRU_MIN_SIZE, SRU_MAX_SIZE); | |
220 | fmt->height = clamp(fmt->height, SRU_MIN_SIZE, SRU_MAX_SIZE); | |
221 | break; | |
222 | ||
223 | case SRU_PAD_SOURCE: | |
224 | /* The SRU can't perform format conversion. */ | |
e790c3cb LP |
225 | format = vsp1_entity_get_pad_format(&sru->entity, config, |
226 | SRU_PAD_SINK); | |
a626e64e LP |
227 | fmt->code = format->code; |
228 | ||
229 | /* We can upscale by 2 in both direction, but not independently. | |
230 | * Compare the input and output rectangles areas (avoiding | |
231 | * integer overflows on the output): if the requested output | |
232 | * area is larger than 1.5^2 the input area upscale by two, | |
233 | * otherwise don't scale. | |
234 | */ | |
235 | input_area = format->width * format->height; | |
236 | output_area = min(fmt->width, SRU_MAX_SIZE) | |
237 | * min(fmt->height, SRU_MAX_SIZE); | |
238 | ||
239 | if (fmt->width <= SRU_MAX_SIZE / 2 && | |
240 | fmt->height <= SRU_MAX_SIZE / 2 && | |
241 | output_area > input_area * 9 / 4) { | |
242 | fmt->width = format->width * 2; | |
243 | fmt->height = format->height * 2; | |
244 | } else { | |
245 | fmt->width = format->width; | |
246 | fmt->height = format->height; | |
247 | } | |
248 | break; | |
249 | } | |
250 | ||
251 | fmt->field = V4L2_FIELD_NONE; | |
252 | fmt->colorspace = V4L2_COLORSPACE_SRGB; | |
253 | } | |
254 | ||
1bd0a1bd LP |
255 | static int sru_set_format(struct v4l2_subdev *subdev, |
256 | struct v4l2_subdev_pad_config *cfg, | |
a626e64e LP |
257 | struct v4l2_subdev_format *fmt) |
258 | { | |
259 | struct vsp1_sru *sru = to_sru(subdev); | |
e790c3cb | 260 | struct v4l2_subdev_pad_config *config; |
a626e64e LP |
261 | struct v4l2_mbus_framefmt *format; |
262 | ||
e790c3cb LP |
263 | config = vsp1_entity_get_pad_config(&sru->entity, cfg, fmt->which); |
264 | if (!config) | |
265 | return -EINVAL; | |
266 | ||
267 | sru_try_format(sru, config, fmt->pad, &fmt->format); | |
a626e64e | 268 | |
e790c3cb | 269 | format = vsp1_entity_get_pad_format(&sru->entity, config, fmt->pad); |
a626e64e LP |
270 | *format = fmt->format; |
271 | ||
272 | if (fmt->pad == SRU_PAD_SINK) { | |
273 | /* Propagate the format to the source pad. */ | |
e790c3cb LP |
274 | format = vsp1_entity_get_pad_format(&sru->entity, config, |
275 | SRU_PAD_SOURCE); | |
a626e64e LP |
276 | *format = fmt->format; |
277 | ||
e790c3cb | 278 | sru_try_format(sru, config, SRU_PAD_SOURCE, format); |
a626e64e LP |
279 | } |
280 | ||
281 | return 0; | |
282 | } | |
283 | ||
a626e64e | 284 | static struct v4l2_subdev_pad_ops sru_pad_ops = { |
0efdf0f5 | 285 | .init_cfg = vsp1_entity_init_cfg, |
a626e64e LP |
286 | .enum_mbus_code = sru_enum_mbus_code, |
287 | .enum_frame_size = sru_enum_frame_size, | |
288 | .get_fmt = sru_get_format, | |
289 | .set_fmt = sru_set_format, | |
290 | }; | |
291 | ||
292 | static struct v4l2_subdev_ops sru_ops = { | |
a626e64e LP |
293 | .pad = &sru_pad_ops, |
294 | }; | |
295 | ||
7b905f05 LP |
296 | /* ----------------------------------------------------------------------------- |
297 | * VSP1 Entity Operations | |
298 | */ | |
299 | ||
5e8dbbf3 | 300 | static void sru_configure(struct vsp1_entity *entity, struct vsp1_dl_list *dl) |
7b905f05 LP |
301 | { |
302 | const struct vsp1_sru_param *param; | |
303 | struct vsp1_sru *sru = to_sru(&entity->subdev); | |
304 | struct v4l2_mbus_framefmt *input; | |
305 | struct v4l2_mbus_framefmt *output; | |
306 | u32 ctrl0; | |
307 | ||
308 | input = vsp1_entity_get_pad_format(&sru->entity, sru->entity.config, | |
309 | SRU_PAD_SINK); | |
310 | output = vsp1_entity_get_pad_format(&sru->entity, sru->entity.config, | |
311 | SRU_PAD_SOURCE); | |
312 | ||
313 | if (input->code == MEDIA_BUS_FMT_ARGB8888_1X32) | |
314 | ctrl0 = VI6_SRU_CTRL0_PARAM2 | VI6_SRU_CTRL0_PARAM3 | |
315 | | VI6_SRU_CTRL0_PARAM4; | |
316 | else | |
317 | ctrl0 = VI6_SRU_CTRL0_PARAM3; | |
318 | ||
319 | if (input->width != output->width) | |
320 | ctrl0 |= VI6_SRU_CTRL0_MODE_UPSCALE; | |
321 | ||
322 | param = &vsp1_sru_params[sru->intensity - 1]; | |
323 | ||
324 | ctrl0 |= param->ctrl0; | |
325 | ||
5e8dbbf3 LP |
326 | vsp1_sru_write(sru, dl, VI6_SRU_CTRL0, ctrl0); |
327 | vsp1_sru_write(sru, dl, VI6_SRU_CTRL1, VI6_SRU_CTRL1_PARAM5); | |
328 | vsp1_sru_write(sru, dl, VI6_SRU_CTRL2, param->ctrl2); | |
7b905f05 LP |
329 | } |
330 | ||
331 | static const struct vsp1_entity_operations sru_entity_ops = { | |
332 | .configure = sru_configure, | |
333 | }; | |
334 | ||
a626e64e LP |
335 | /* ----------------------------------------------------------------------------- |
336 | * Initialization and Cleanup | |
337 | */ | |
338 | ||
339 | struct vsp1_sru *vsp1_sru_create(struct vsp1_device *vsp1) | |
340 | { | |
a626e64e LP |
341 | struct vsp1_sru *sru; |
342 | int ret; | |
343 | ||
344 | sru = devm_kzalloc(vsp1->dev, sizeof(*sru), GFP_KERNEL); | |
345 | if (sru == NULL) | |
346 | return ERR_PTR(-ENOMEM); | |
347 | ||
7b905f05 | 348 | sru->entity.ops = &sru_entity_ops; |
a626e64e | 349 | sru->entity.type = VSP1_ENTITY_SRU; |
a626e64e | 350 | |
823329df | 351 | ret = vsp1_entity_init(vsp1, &sru->entity, "sru", 2, &sru_ops); |
a626e64e LP |
352 | if (ret < 0) |
353 | return ERR_PTR(ret); | |
354 | ||
a626e64e LP |
355 | /* Initialize the control handler. */ |
356 | v4l2_ctrl_handler_init(&sru->ctrls, 1); | |
357 | v4l2_ctrl_new_custom(&sru->ctrls, &sru_intensity_control, NULL); | |
a1606102 | 358 | |
d884a8b2 LP |
359 | sru->intensity = 1; |
360 | ||
a626e64e LP |
361 | sru->entity.subdev.ctrl_handler = &sru->ctrls; |
362 | ||
a1606102 LP |
363 | if (sru->ctrls.error) { |
364 | dev_err(vsp1->dev, "sru: failed to initialize controls\n"); | |
365 | ret = sru->ctrls.error; | |
366 | vsp1_entity_destroy(&sru->entity); | |
367 | return ERR_PTR(ret); | |
368 | } | |
369 | ||
a626e64e LP |
370 | return sru; |
371 | } |