1 // SPDX-License-Identifier: GPL-2.0+
3 * vsp1_sru.c -- R-Car VSP1 Super Resolution Unit
5 * Copyright (C) 2013 Renesas Corporation
10 #include <linux/device.h>
11 #include <linux/gfp.h>
13 #include <media/v4l2-subdev.h>
17 #include "vsp1_pipe.h"
20 #define SRU_MIN_SIZE 4U
21 #define SRU_MAX_SIZE 8190U
23 /* -----------------------------------------------------------------------------
27 static inline void vsp1_sru_write(struct vsp1_sru *sru,
28 struct vsp1_dl_body *dlb, u32 reg, u32 data)
30 vsp1_dl_body_write(dlb, reg, data);
33 /* -----------------------------------------------------------------------------
37 #define V4L2_CID_VSP1_SRU_INTENSITY (V4L2_CID_USER_BASE | 0x1001)
39 struct vsp1_sru_param {
44 #define VI6_SRU_CTRL0_PARAMS(p0, p1) \
45 (((p0) << VI6_SRU_CTRL0_PARAM0_SHIFT) | \
46 ((p1) << VI6_SRU_CTRL0_PARAM1_SHIFT))
48 #define VI6_SRU_CTRL2_PARAMS(p6, p7, p8) \
49 (((p6) << VI6_SRU_CTRL2_PARAM6_SHIFT) | \
50 ((p7) << VI6_SRU_CTRL2_PARAM7_SHIFT) | \
51 ((p8) << VI6_SRU_CTRL2_PARAM8_SHIFT))
53 static const struct vsp1_sru_param vsp1_sru_params[] = {
55 .ctrl0 = VI6_SRU_CTRL0_PARAMS(256, 4) | VI6_SRU_CTRL0_EN,
56 .ctrl2 = VI6_SRU_CTRL2_PARAMS(24, 40, 255),
58 .ctrl0 = VI6_SRU_CTRL0_PARAMS(256, 4) | VI6_SRU_CTRL0_EN,
59 .ctrl2 = VI6_SRU_CTRL2_PARAMS(8, 16, 255),
61 .ctrl0 = VI6_SRU_CTRL0_PARAMS(384, 5) | VI6_SRU_CTRL0_EN,
62 .ctrl2 = VI6_SRU_CTRL2_PARAMS(36, 60, 255),
64 .ctrl0 = VI6_SRU_CTRL0_PARAMS(384, 5) | VI6_SRU_CTRL0_EN,
65 .ctrl2 = VI6_SRU_CTRL2_PARAMS(12, 27, 255),
67 .ctrl0 = VI6_SRU_CTRL0_PARAMS(511, 6) | VI6_SRU_CTRL0_EN,
68 .ctrl2 = VI6_SRU_CTRL2_PARAMS(48, 80, 255),
70 .ctrl0 = VI6_SRU_CTRL0_PARAMS(511, 6) | VI6_SRU_CTRL0_EN,
71 .ctrl2 = VI6_SRU_CTRL2_PARAMS(16, 36, 255),
75 static int sru_s_ctrl(struct v4l2_ctrl *ctrl)
77 struct vsp1_sru *sru =
78 container_of(ctrl->handler, struct vsp1_sru, ctrls);
81 case V4L2_CID_VSP1_SRU_INTENSITY:
82 sru->intensity = ctrl->val;
89 static const struct v4l2_ctrl_ops sru_ctrl_ops = {
93 static const struct v4l2_ctrl_config sru_intensity_control = {
95 .id = V4L2_CID_VSP1_SRU_INTENSITY,
97 .type = V4L2_CTRL_TYPE_INTEGER,
104 /* -----------------------------------------------------------------------------
105 * V4L2 Subdevice Operations
108 static int sru_enum_mbus_code(struct v4l2_subdev *subdev,
109 struct v4l2_subdev_state *sd_state,
110 struct v4l2_subdev_mbus_code_enum *code)
112 static const unsigned int codes[] = {
113 MEDIA_BUS_FMT_ARGB8888_1X32,
114 MEDIA_BUS_FMT_AYUV8_1X32,
117 return vsp1_subdev_enum_mbus_code(subdev, sd_state, code, codes,
121 static int sru_enum_frame_size(struct v4l2_subdev *subdev,
122 struct v4l2_subdev_state *sd_state,
123 struct v4l2_subdev_frame_size_enum *fse)
125 struct vsp1_sru *sru = to_sru(subdev);
126 struct v4l2_subdev_state *state;
127 struct v4l2_mbus_framefmt *format;
130 state = vsp1_entity_get_state(&sru->entity, sd_state, fse->which);
134 format = v4l2_subdev_state_get_format(state, SRU_PAD_SINK);
136 mutex_lock(&sru->entity.lock);
138 if (fse->index || fse->code != format->code) {
143 if (fse->pad == SRU_PAD_SINK) {
144 fse->min_width = SRU_MIN_SIZE;
145 fse->max_width = SRU_MAX_SIZE;
146 fse->min_height = SRU_MIN_SIZE;
147 fse->max_height = SRU_MAX_SIZE;
149 fse->min_width = format->width;
150 fse->min_height = format->height;
151 if (format->width <= SRU_MAX_SIZE / 2 &&
152 format->height <= SRU_MAX_SIZE / 2) {
153 fse->max_width = format->width * 2;
154 fse->max_height = format->height * 2;
156 fse->max_width = format->width;
157 fse->max_height = format->height;
162 mutex_unlock(&sru->entity.lock);
166 static void sru_try_format(struct vsp1_sru *sru,
167 struct v4l2_subdev_state *sd_state,
168 unsigned int pad, struct v4l2_mbus_framefmt *fmt)
170 struct v4l2_mbus_framefmt *format;
171 unsigned int input_area;
172 unsigned int output_area;
176 /* Default to YUV if the requested format is not supported. */
177 if (fmt->code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
178 fmt->code != MEDIA_BUS_FMT_AYUV8_1X32)
179 fmt->code = MEDIA_BUS_FMT_AYUV8_1X32;
181 fmt->width = clamp(fmt->width, SRU_MIN_SIZE, SRU_MAX_SIZE);
182 fmt->height = clamp(fmt->height, SRU_MIN_SIZE, SRU_MAX_SIZE);
186 /* The SRU can't perform format conversion. */
187 format = v4l2_subdev_state_get_format(sd_state, SRU_PAD_SINK);
188 fmt->code = format->code;
191 * We can upscale by 2 in both direction, but not independently.
192 * Compare the input and output rectangles areas (avoiding
193 * integer overflows on the output): if the requested output
194 * area is larger than 1.5^2 the input area upscale by two,
195 * otherwise don't scale.
197 input_area = format->width * format->height;
198 output_area = min(fmt->width, SRU_MAX_SIZE)
199 * min(fmt->height, SRU_MAX_SIZE);
201 if (fmt->width <= SRU_MAX_SIZE / 2 &&
202 fmt->height <= SRU_MAX_SIZE / 2 &&
203 output_area > input_area * 9 / 4) {
204 fmt->width = format->width * 2;
205 fmt->height = format->height * 2;
207 fmt->width = format->width;
208 fmt->height = format->height;
213 fmt->field = V4L2_FIELD_NONE;
214 fmt->colorspace = V4L2_COLORSPACE_SRGB;
217 static int sru_set_format(struct v4l2_subdev *subdev,
218 struct v4l2_subdev_state *sd_state,
219 struct v4l2_subdev_format *fmt)
221 struct vsp1_sru *sru = to_sru(subdev);
222 struct v4l2_subdev_state *state;
223 struct v4l2_mbus_framefmt *format;
226 mutex_lock(&sru->entity.lock);
228 state = vsp1_entity_get_state(&sru->entity, sd_state, fmt->which);
234 sru_try_format(sru, state, fmt->pad, &fmt->format);
236 format = v4l2_subdev_state_get_format(state, fmt->pad);
237 *format = fmt->format;
239 if (fmt->pad == SRU_PAD_SINK) {
240 /* Propagate the format to the source pad. */
241 format = v4l2_subdev_state_get_format(state, SRU_PAD_SOURCE);
242 *format = fmt->format;
244 sru_try_format(sru, state, SRU_PAD_SOURCE, format);
248 mutex_unlock(&sru->entity.lock);
252 static const struct v4l2_subdev_pad_ops sru_pad_ops = {
253 .enum_mbus_code = sru_enum_mbus_code,
254 .enum_frame_size = sru_enum_frame_size,
255 .get_fmt = vsp1_subdev_get_pad_format,
256 .set_fmt = sru_set_format,
259 static const struct v4l2_subdev_ops sru_ops = {
263 /* -----------------------------------------------------------------------------
264 * VSP1 Entity Operations
267 static void sru_configure_stream(struct vsp1_entity *entity,
268 struct v4l2_subdev_state *state,
269 struct vsp1_pipeline *pipe,
270 struct vsp1_dl_list *dl,
271 struct vsp1_dl_body *dlb)
273 const struct vsp1_sru_param *param;
274 struct vsp1_sru *sru = to_sru(&entity->subdev);
275 struct v4l2_mbus_framefmt *input;
276 struct v4l2_mbus_framefmt *output;
279 input = v4l2_subdev_state_get_format(state, SRU_PAD_SINK);
280 output = v4l2_subdev_state_get_format(state, SRU_PAD_SOURCE);
282 if (input->code == MEDIA_BUS_FMT_ARGB8888_1X32)
283 ctrl0 = VI6_SRU_CTRL0_PARAM2 | VI6_SRU_CTRL0_PARAM3
284 | VI6_SRU_CTRL0_PARAM4;
286 ctrl0 = VI6_SRU_CTRL0_PARAM3;
288 if (input->width != output->width)
289 ctrl0 |= VI6_SRU_CTRL0_MODE_UPSCALE;
291 param = &vsp1_sru_params[sru->intensity - 1];
293 ctrl0 |= param->ctrl0;
295 vsp1_sru_write(sru, dlb, VI6_SRU_CTRL0, ctrl0);
296 vsp1_sru_write(sru, dlb, VI6_SRU_CTRL1, VI6_SRU_CTRL1_PARAM5);
297 vsp1_sru_write(sru, dlb, VI6_SRU_CTRL2, param->ctrl2);
300 static unsigned int sru_max_width(struct vsp1_entity *entity,
301 struct v4l2_subdev_state *state,
302 struct vsp1_pipeline *pipe)
304 struct v4l2_mbus_framefmt *input;
305 struct v4l2_mbus_framefmt *output;
307 input = v4l2_subdev_state_get_format(state, SRU_PAD_SINK);
308 output = v4l2_subdev_state_get_format(state, SRU_PAD_SOURCE);
311 * The maximum input width of the SRU is 288 input pixels, but 32
312 * pixels are reserved to support overlapping partition windows when
315 if (input->width != output->width)
321 static void sru_partition(struct vsp1_entity *entity,
322 struct v4l2_subdev_state *state,
323 struct vsp1_pipeline *pipe,
324 struct vsp1_partition *partition,
325 unsigned int partition_idx,
326 struct v4l2_rect *window)
328 struct v4l2_mbus_framefmt *input;
329 struct v4l2_mbus_framefmt *output;
331 input = v4l2_subdev_state_get_format(state, SRU_PAD_SINK);
332 output = v4l2_subdev_state_get_format(state, SRU_PAD_SOURCE);
334 /* Adapt if SRUx2 is enabled. */
335 if (input->width != output->width) {
342 partition->sru = *window;
345 static const struct vsp1_entity_operations sru_entity_ops = {
346 .configure_stream = sru_configure_stream,
347 .max_width = sru_max_width,
348 .partition = sru_partition,
351 /* -----------------------------------------------------------------------------
352 * Initialization and Cleanup
355 struct vsp1_sru *vsp1_sru_create(struct vsp1_device *vsp1)
357 struct vsp1_sru *sru;
360 sru = devm_kzalloc(vsp1->dev, sizeof(*sru), GFP_KERNEL);
362 return ERR_PTR(-ENOMEM);
364 sru->entity.ops = &sru_entity_ops;
365 sru->entity.type = VSP1_ENTITY_SRU;
367 ret = vsp1_entity_init(vsp1, &sru->entity, "sru", 2, &sru_ops,
368 MEDIA_ENT_F_PROC_VIDEO_SCALER);
372 /* Initialize the control handler. */
373 v4l2_ctrl_handler_init(&sru->ctrls, 1);
374 v4l2_ctrl_new_custom(&sru->ctrls, &sru_intensity_control, NULL);
378 sru->entity.subdev.ctrl_handler = &sru->ctrls;
380 if (sru->ctrls.error) {
381 dev_err(vsp1->dev, "sru: failed to initialize controls\n");
382 ret = sru->ctrls.error;
383 vsp1_entity_destroy(&sru->entity);