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>
21 #include "vsp1_pipe.h"
24 #define SRU_MIN_SIZE 4U
25 #define SRU_MAX_SIZE 8190U
27 /* -----------------------------------------------------------------------------
31 static inline void vsp1_sru_write(struct vsp1_sru *sru, struct vsp1_dl_list *dl,
34 vsp1_dl_list_write(dl, reg, data);
37 /* -----------------------------------------------------------------------------
41 #define V4L2_CID_VSP1_SRU_INTENSITY (V4L2_CID_USER_BASE | 0x1001)
43 struct vsp1_sru_param {
48 #define VI6_SRU_CTRL0_PARAMS(p0, p1) \
49 (((p0) << VI6_SRU_CTRL0_PARAM0_SHIFT) | \
50 ((p1) << VI6_SRU_CTRL0_PARAM1_SHIFT))
52 #define VI6_SRU_CTRL2_PARAMS(p6, p7, p8) \
53 (((p6) << VI6_SRU_CTRL2_PARAM6_SHIFT) | \
54 ((p7) << VI6_SRU_CTRL2_PARAM7_SHIFT) | \
55 ((p8) << VI6_SRU_CTRL2_PARAM8_SHIFT))
57 static const struct vsp1_sru_param vsp1_sru_params[] = {
59 .ctrl0 = VI6_SRU_CTRL0_PARAMS(256, 4) | VI6_SRU_CTRL0_EN,
60 .ctrl2 = VI6_SRU_CTRL2_PARAMS(24, 40, 255),
62 .ctrl0 = VI6_SRU_CTRL0_PARAMS(256, 4) | VI6_SRU_CTRL0_EN,
63 .ctrl2 = VI6_SRU_CTRL2_PARAMS(8, 16, 255),
65 .ctrl0 = VI6_SRU_CTRL0_PARAMS(384, 5) | VI6_SRU_CTRL0_EN,
66 .ctrl2 = VI6_SRU_CTRL2_PARAMS(36, 60, 255),
68 .ctrl0 = VI6_SRU_CTRL0_PARAMS(384, 5) | VI6_SRU_CTRL0_EN,
69 .ctrl2 = VI6_SRU_CTRL2_PARAMS(12, 27, 255),
71 .ctrl0 = VI6_SRU_CTRL0_PARAMS(511, 6) | VI6_SRU_CTRL0_EN,
72 .ctrl2 = VI6_SRU_CTRL2_PARAMS(48, 80, 255),
74 .ctrl0 = VI6_SRU_CTRL0_PARAMS(511, 6) | VI6_SRU_CTRL0_EN,
75 .ctrl2 = VI6_SRU_CTRL2_PARAMS(16, 36, 255),
79 static int sru_s_ctrl(struct v4l2_ctrl *ctrl)
81 struct vsp1_sru *sru =
82 container_of(ctrl->handler, struct vsp1_sru, ctrls);
85 case V4L2_CID_VSP1_SRU_INTENSITY:
86 sru->intensity = ctrl->val;
93 static const struct v4l2_ctrl_ops sru_ctrl_ops = {
97 static const struct v4l2_ctrl_config sru_intensity_control = {
99 .id = V4L2_CID_VSP1_SRU_INTENSITY,
101 .type = V4L2_CTRL_TYPE_INTEGER,
108 /* -----------------------------------------------------------------------------
109 * V4L2 Subdevice Operations
112 static int sru_enum_mbus_code(struct v4l2_subdev *subdev,
113 struct v4l2_subdev_pad_config *cfg,
114 struct v4l2_subdev_mbus_code_enum *code)
116 static const unsigned int codes[] = {
117 MEDIA_BUS_FMT_ARGB8888_1X32,
118 MEDIA_BUS_FMT_AYUV8_1X32,
121 return vsp1_subdev_enum_mbus_code(subdev, cfg, code, codes,
125 static int sru_enum_frame_size(struct v4l2_subdev *subdev,
126 struct v4l2_subdev_pad_config *cfg,
127 struct v4l2_subdev_frame_size_enum *fse)
129 struct vsp1_sru *sru = to_sru(subdev);
130 struct v4l2_subdev_pad_config *config;
131 struct v4l2_mbus_framefmt *format;
134 config = vsp1_entity_get_pad_config(&sru->entity, cfg, fse->which);
138 format = vsp1_entity_get_pad_format(&sru->entity, config, SRU_PAD_SINK);
140 mutex_lock(&sru->entity.lock);
142 if (fse->index || fse->code != format->code) {
147 if (fse->pad == SRU_PAD_SINK) {
148 fse->min_width = SRU_MIN_SIZE;
149 fse->max_width = SRU_MAX_SIZE;
150 fse->min_height = SRU_MIN_SIZE;
151 fse->max_height = SRU_MAX_SIZE;
153 fse->min_width = format->width;
154 fse->min_height = format->height;
155 if (format->width <= SRU_MAX_SIZE / 2 &&
156 format->height <= SRU_MAX_SIZE / 2) {
157 fse->max_width = format->width * 2;
158 fse->max_height = format->height * 2;
160 fse->max_width = format->width;
161 fse->max_height = format->height;
166 mutex_unlock(&sru->entity.lock);
170 static void sru_try_format(struct vsp1_sru *sru,
171 struct v4l2_subdev_pad_config *config,
172 unsigned int pad, struct v4l2_mbus_framefmt *fmt)
174 struct v4l2_mbus_framefmt *format;
175 unsigned int input_area;
176 unsigned int output_area;
180 /* Default to YUV if the requested format is not supported. */
181 if (fmt->code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
182 fmt->code != MEDIA_BUS_FMT_AYUV8_1X32)
183 fmt->code = MEDIA_BUS_FMT_AYUV8_1X32;
185 fmt->width = clamp(fmt->width, SRU_MIN_SIZE, SRU_MAX_SIZE);
186 fmt->height = clamp(fmt->height, SRU_MIN_SIZE, SRU_MAX_SIZE);
190 /* The SRU can't perform format conversion. */
191 format = vsp1_entity_get_pad_format(&sru->entity, config,
193 fmt->code = format->code;
196 * We can upscale by 2 in both direction, but not independently.
197 * Compare the input and output rectangles areas (avoiding
198 * integer overflows on the output): if the requested output
199 * area is larger than 1.5^2 the input area upscale by two,
200 * otherwise don't scale.
202 input_area = format->width * format->height;
203 output_area = min(fmt->width, SRU_MAX_SIZE)
204 * min(fmt->height, SRU_MAX_SIZE);
206 if (fmt->width <= SRU_MAX_SIZE / 2 &&
207 fmt->height <= SRU_MAX_SIZE / 2 &&
208 output_area > input_area * 9 / 4) {
209 fmt->width = format->width * 2;
210 fmt->height = format->height * 2;
212 fmt->width = format->width;
213 fmt->height = format->height;
218 fmt->field = V4L2_FIELD_NONE;
219 fmt->colorspace = V4L2_COLORSPACE_SRGB;
222 static int sru_set_format(struct v4l2_subdev *subdev,
223 struct v4l2_subdev_pad_config *cfg,
224 struct v4l2_subdev_format *fmt)
226 struct vsp1_sru *sru = to_sru(subdev);
227 struct v4l2_subdev_pad_config *config;
228 struct v4l2_mbus_framefmt *format;
231 mutex_lock(&sru->entity.lock);
233 config = vsp1_entity_get_pad_config(&sru->entity, cfg, fmt->which);
239 sru_try_format(sru, config, fmt->pad, &fmt->format);
241 format = vsp1_entity_get_pad_format(&sru->entity, config, fmt->pad);
242 *format = fmt->format;
244 if (fmt->pad == SRU_PAD_SINK) {
245 /* Propagate the format to the source pad. */
246 format = vsp1_entity_get_pad_format(&sru->entity, config,
248 *format = fmt->format;
250 sru_try_format(sru, config, SRU_PAD_SOURCE, format);
254 mutex_unlock(&sru->entity.lock);
258 static const struct v4l2_subdev_pad_ops sru_pad_ops = {
259 .init_cfg = vsp1_entity_init_cfg,
260 .enum_mbus_code = sru_enum_mbus_code,
261 .enum_frame_size = sru_enum_frame_size,
262 .get_fmt = vsp1_subdev_get_pad_format,
263 .set_fmt = sru_set_format,
266 static const struct v4l2_subdev_ops sru_ops = {
270 /* -----------------------------------------------------------------------------
271 * VSP1 Entity Operations
274 static void sru_configure(struct vsp1_entity *entity,
275 struct vsp1_pipeline *pipe,
276 struct vsp1_dl_list *dl,
277 enum vsp1_entity_params params)
279 const struct vsp1_sru_param *param;
280 struct vsp1_sru *sru = to_sru(&entity->subdev);
281 struct v4l2_mbus_framefmt *input;
282 struct v4l2_mbus_framefmt *output;
285 if (params != VSP1_ENTITY_PARAMS_INIT)
288 input = vsp1_entity_get_pad_format(&sru->entity, sru->entity.config,
290 output = vsp1_entity_get_pad_format(&sru->entity, sru->entity.config,
293 if (input->code == MEDIA_BUS_FMT_ARGB8888_1X32)
294 ctrl0 = VI6_SRU_CTRL0_PARAM2 | VI6_SRU_CTRL0_PARAM3
295 | VI6_SRU_CTRL0_PARAM4;
297 ctrl0 = VI6_SRU_CTRL0_PARAM3;
299 if (input->width != output->width)
300 ctrl0 |= VI6_SRU_CTRL0_MODE_UPSCALE;
302 param = &vsp1_sru_params[sru->intensity - 1];
304 ctrl0 |= param->ctrl0;
306 vsp1_sru_write(sru, dl, VI6_SRU_CTRL0, ctrl0);
307 vsp1_sru_write(sru, dl, VI6_SRU_CTRL1, VI6_SRU_CTRL1_PARAM5);
308 vsp1_sru_write(sru, dl, VI6_SRU_CTRL2, param->ctrl2);
311 static unsigned int sru_max_width(struct vsp1_entity *entity,
312 struct vsp1_pipeline *pipe)
314 struct vsp1_sru *sru = to_sru(&entity->subdev);
315 struct v4l2_mbus_framefmt *input;
316 struct v4l2_mbus_framefmt *output;
318 input = vsp1_entity_get_pad_format(&sru->entity, sru->entity.config,
320 output = vsp1_entity_get_pad_format(&sru->entity, sru->entity.config,
323 if (input->width != output->width)
329 static void sru_partition(struct vsp1_entity *entity,
330 struct vsp1_pipeline *pipe,
331 struct vsp1_partition *partition,
332 unsigned int partition_idx,
333 struct vsp1_partition_window *window)
335 struct vsp1_sru *sru = to_sru(&entity->subdev);
336 struct v4l2_mbus_framefmt *input;
337 struct v4l2_mbus_framefmt *output;
339 input = vsp1_entity_get_pad_format(&sru->entity, sru->entity.config,
341 output = vsp1_entity_get_pad_format(&sru->entity, sru->entity.config,
344 /* Adapt if SRUx2 is enabled */
345 if (input->width != output->width) {
350 partition->sru = *window;
353 static const struct vsp1_entity_operations sru_entity_ops = {
354 .configure = sru_configure,
355 .max_width = sru_max_width,
356 .partition = sru_partition,
359 /* -----------------------------------------------------------------------------
360 * Initialization and Cleanup
363 struct vsp1_sru *vsp1_sru_create(struct vsp1_device *vsp1)
365 struct vsp1_sru *sru;
368 sru = devm_kzalloc(vsp1->dev, sizeof(*sru), GFP_KERNEL);
370 return ERR_PTR(-ENOMEM);
372 sru->entity.ops = &sru_entity_ops;
373 sru->entity.type = VSP1_ENTITY_SRU;
375 ret = vsp1_entity_init(vsp1, &sru->entity, "sru", 2, &sru_ops,
376 MEDIA_ENT_F_PROC_VIDEO_SCALER);
380 /* Initialize the control handler. */
381 v4l2_ctrl_handler_init(&sru->ctrls, 1);
382 v4l2_ctrl_new_custom(&sru->ctrls, &sru_intensity_control, NULL);
386 sru->entity.subdev.ctrl_handler = &sru->ctrls;
388 if (sru->ctrls.error) {
389 dev_err(vsp1->dev, "sru: failed to initialize controls\n");
390 ret = sru->ctrls.error;
391 vsp1_entity_destroy(&sru->entity);