2 * vsp1_sru.c -- R-Car VSP1 Super Resolution Unit
4 * Copyright (C) 2013 Renesas Corporation
6 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
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.
14 #include <linux/device.h>
15 #include <linux/gfp.h>
17 #include <media/v4l2-subdev.h>
22 #define SRU_MIN_SIZE 4U
23 #define SRU_MAX_SIZE 8190U
25 /* -----------------------------------------------------------------------------
29 static inline void vsp1_sru_write(struct vsp1_sru *sru, u32 reg, u32 data)
31 vsp1_mod_write(&sru->entity, reg, data);
34 /* -----------------------------------------------------------------------------
38 #define V4L2_CID_VSP1_SRU_INTENSITY (V4L2_CID_USER_BASE + 1)
40 struct vsp1_sru_param {
45 #define VI6_SRU_CTRL0_PARAMS(p0, p1) \
46 (((p0) << VI6_SRU_CTRL0_PARAM0_SHIFT) | \
47 ((p1) << VI6_SRU_CTRL0_PARAM1_SHIFT))
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))
54 static const struct vsp1_sru_param vsp1_sru_params[] = {
56 .ctrl0 = VI6_SRU_CTRL0_PARAMS(256, 4) | VI6_SRU_CTRL0_EN,
57 .ctrl2 = VI6_SRU_CTRL2_PARAMS(24, 40, 255),
59 .ctrl0 = VI6_SRU_CTRL0_PARAMS(256, 4) | VI6_SRU_CTRL0_EN,
60 .ctrl2 = VI6_SRU_CTRL2_PARAMS(8, 16, 255),
62 .ctrl0 = VI6_SRU_CTRL0_PARAMS(384, 5) | VI6_SRU_CTRL0_EN,
63 .ctrl2 = VI6_SRU_CTRL2_PARAMS(36, 60, 255),
65 .ctrl0 = VI6_SRU_CTRL0_PARAMS(384, 5) | VI6_SRU_CTRL0_EN,
66 .ctrl2 = VI6_SRU_CTRL2_PARAMS(12, 27, 255),
68 .ctrl0 = VI6_SRU_CTRL0_PARAMS(511, 6) | VI6_SRU_CTRL0_EN,
69 .ctrl2 = VI6_SRU_CTRL2_PARAMS(48, 80, 255),
71 .ctrl0 = VI6_SRU_CTRL0_PARAMS(511, 6) | VI6_SRU_CTRL0_EN,
72 .ctrl2 = VI6_SRU_CTRL2_PARAMS(16, 36, 255),
76 static int sru_s_ctrl(struct v4l2_ctrl *ctrl)
78 struct vsp1_sru *sru =
79 container_of(ctrl->handler, struct vsp1_sru, ctrls);
82 case V4L2_CID_VSP1_SRU_INTENSITY:
83 sru->intensity = ctrl->val;
90 static const struct v4l2_ctrl_ops sru_ctrl_ops = {
94 static const struct v4l2_ctrl_config sru_intensity_control = {
96 .id = V4L2_CID_VSP1_SRU_INTENSITY,
98 .type = V4L2_CTRL_TYPE_INTEGER,
105 /* -----------------------------------------------------------------------------
106 * V4L2 Subdevice Core Operations
109 static int sru_s_stream(struct v4l2_subdev *subdev, int enable)
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;
120 input = &sru->entity.formats[SRU_PAD_SINK];
121 output = &sru->entity.formats[SRU_PAD_SOURCE];
123 if (input->code == MEDIA_BUS_FMT_ARGB8888_1X32)
124 ctrl0 = VI6_SRU_CTRL0_PARAM2 | VI6_SRU_CTRL0_PARAM3
125 | VI6_SRU_CTRL0_PARAM4;
127 ctrl0 = VI6_SRU_CTRL0_PARAM3;
129 if (input->width != output->width)
130 ctrl0 |= VI6_SRU_CTRL0_MODE_UPSCALE;
132 param = &vsp1_sru_params[sru->intensity - 1];
134 ctrl0 |= param->ctrl0;
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);
143 /* -----------------------------------------------------------------------------
144 * V4L2 Subdevice Pad Operations
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)
151 static const unsigned int codes[] = {
152 MEDIA_BUS_FMT_ARGB8888_1X32,
153 MEDIA_BUS_FMT_AYUV8_1X32,
155 struct vsp1_sru *sru = to_sru(subdev);
156 struct v4l2_mbus_framefmt *format;
158 if (code->pad == SRU_PAD_SINK) {
159 if (code->index >= ARRAY_SIZE(codes))
162 code->code = codes[code->index];
164 /* The SRU can't perform format conversion, the sink format is
165 * always identical to the source format.
170 format = vsp1_entity_get_pad_format(&sru->entity, cfg,
171 SRU_PAD_SINK, code->which);
172 code->code = format->code;
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)
182 struct vsp1_sru *sru = to_sru(subdev);
183 struct v4l2_mbus_framefmt *format;
185 format = vsp1_entity_get_pad_format(&sru->entity, cfg,
186 SRU_PAD_SINK, fse->which);
188 if (fse->index || fse->code != format->code)
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;
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;
204 fse->max_width = format->width;
205 fse->max_height = format->height;
212 static int sru_get_format(struct v4l2_subdev *subdev, struct v4l2_subdev_pad_config *cfg,
213 struct v4l2_subdev_format *fmt)
215 struct vsp1_sru *sru = to_sru(subdev);
217 fmt->format = *vsp1_entity_get_pad_format(&sru->entity, cfg, fmt->pad,
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)
227 struct v4l2_mbus_framefmt *format;
228 unsigned int input_area;
229 unsigned int output_area;
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;
238 fmt->width = clamp(fmt->width, SRU_MIN_SIZE, SRU_MAX_SIZE);
239 fmt->height = clamp(fmt->height, SRU_MIN_SIZE, SRU_MAX_SIZE);
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;
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.
254 input_area = format->width * format->height;
255 output_area = min(fmt->width, SRU_MAX_SIZE)
256 * min(fmt->height, SRU_MAX_SIZE);
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;
264 fmt->width = format->width;
265 fmt->height = format->height;
270 fmt->field = V4L2_FIELD_NONE;
271 fmt->colorspace = V4L2_COLORSPACE_SRGB;
274 static int sru_set_format(struct v4l2_subdev *subdev, struct v4l2_subdev_pad_config *cfg,
275 struct v4l2_subdev_format *fmt)
277 struct vsp1_sru *sru = to_sru(subdev);
278 struct v4l2_mbus_framefmt *format;
280 sru_try_format(sru, cfg, fmt->pad, &fmt->format, fmt->which);
282 format = vsp1_entity_get_pad_format(&sru->entity, cfg, fmt->pad,
284 *format = fmt->format;
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;
292 sru_try_format(sru, cfg, SRU_PAD_SOURCE, format, fmt->which);
298 /* -----------------------------------------------------------------------------
299 * V4L2 Subdevice Operations
302 static struct v4l2_subdev_video_ops sru_video_ops = {
303 .s_stream = sru_s_stream,
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,
313 static struct v4l2_subdev_ops sru_ops = {
314 .video = &sru_video_ops,
318 /* -----------------------------------------------------------------------------
319 * Initialization and Cleanup
322 struct vsp1_sru *vsp1_sru_create(struct vsp1_device *vsp1)
324 struct v4l2_subdev *subdev;
325 struct vsp1_sru *sru;
328 sru = devm_kzalloc(vsp1->dev, sizeof(*sru), GFP_KERNEL);
330 return ERR_PTR(-ENOMEM);
332 sru->entity.type = VSP1_ENTITY_SRU;
334 ret = vsp1_entity_init(vsp1, &sru->entity, 2);
338 /* Initialize the V4L2 subdev. */
339 subdev = &sru->entity.subdev;
340 v4l2_subdev_init(subdev, &sru_ops);
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;
349 vsp1_entity_init_formats(subdev, NULL);
351 /* Initialize the control handler. */
352 v4l2_ctrl_handler_init(&sru->ctrls, 1);
353 v4l2_ctrl_new_custom(&sru->ctrls, &sru_intensity_control, NULL);
357 sru->entity.subdev.ctrl_handler = &sru->ctrls;
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);