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>
23 #define SRU_MIN_SIZE 4U
24 #define SRU_MAX_SIZE 8190U
26 /* -----------------------------------------------------------------------------
30 static inline void vsp1_sru_write(struct vsp1_sru
*sru
, struct vsp1_dl_list
*dl
,
33 vsp1_dl_list_write(dl
, reg
, data
);
36 /* -----------------------------------------------------------------------------
40 #define V4L2_CID_VSP1_SRU_INTENSITY (V4L2_CID_USER_BASE | 0x1001)
42 struct vsp1_sru_param
{
47 #define VI6_SRU_CTRL0_PARAMS(p0, p1) \
48 (((p0) << VI6_SRU_CTRL0_PARAM0_SHIFT) | \
49 ((p1) << VI6_SRU_CTRL0_PARAM1_SHIFT))
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))
56 static const struct vsp1_sru_param vsp1_sru_params
[] = {
58 .ctrl0
= VI6_SRU_CTRL0_PARAMS(256, 4) | VI6_SRU_CTRL0_EN
,
59 .ctrl2
= VI6_SRU_CTRL2_PARAMS(24, 40, 255),
61 .ctrl0
= VI6_SRU_CTRL0_PARAMS(256, 4) | VI6_SRU_CTRL0_EN
,
62 .ctrl2
= VI6_SRU_CTRL2_PARAMS(8, 16, 255),
64 .ctrl0
= VI6_SRU_CTRL0_PARAMS(384, 5) | VI6_SRU_CTRL0_EN
,
65 .ctrl2
= VI6_SRU_CTRL2_PARAMS(36, 60, 255),
67 .ctrl0
= VI6_SRU_CTRL0_PARAMS(384, 5) | VI6_SRU_CTRL0_EN
,
68 .ctrl2
= VI6_SRU_CTRL2_PARAMS(12, 27, 255),
70 .ctrl0
= VI6_SRU_CTRL0_PARAMS(511, 6) | VI6_SRU_CTRL0_EN
,
71 .ctrl2
= VI6_SRU_CTRL2_PARAMS(48, 80, 255),
73 .ctrl0
= VI6_SRU_CTRL0_PARAMS(511, 6) | VI6_SRU_CTRL0_EN
,
74 .ctrl2
= VI6_SRU_CTRL2_PARAMS(16, 36, 255),
78 static int sru_s_ctrl(struct v4l2_ctrl
*ctrl
)
80 struct vsp1_sru
*sru
=
81 container_of(ctrl
->handler
, struct vsp1_sru
, ctrls
);
84 case V4L2_CID_VSP1_SRU_INTENSITY
:
85 sru
->intensity
= ctrl
->val
;
92 static const struct v4l2_ctrl_ops sru_ctrl_ops
= {
96 static const struct v4l2_ctrl_config sru_intensity_control
= {
98 .id
= V4L2_CID_VSP1_SRU_INTENSITY
,
100 .type
= V4L2_CTRL_TYPE_INTEGER
,
107 /* -----------------------------------------------------------------------------
108 * V4L2 Subdevice Operations
111 static int sru_enum_mbus_code(struct v4l2_subdev
*subdev
,
112 struct v4l2_subdev_pad_config
*cfg
,
113 struct v4l2_subdev_mbus_code_enum
*code
)
115 static const unsigned int codes
[] = {
116 MEDIA_BUS_FMT_ARGB8888_1X32
,
117 MEDIA_BUS_FMT_AYUV8_1X32
,
120 return vsp1_subdev_enum_mbus_code(subdev
, cfg
, code
, codes
,
124 static int sru_enum_frame_size(struct v4l2_subdev
*subdev
,
125 struct v4l2_subdev_pad_config
*cfg
,
126 struct v4l2_subdev_frame_size_enum
*fse
)
128 struct vsp1_sru
*sru
= to_sru(subdev
);
129 struct v4l2_subdev_pad_config
*config
;
130 struct v4l2_mbus_framefmt
*format
;
132 config
= vsp1_entity_get_pad_config(&sru
->entity
, cfg
, fse
->which
);
136 format
= vsp1_entity_get_pad_format(&sru
->entity
, config
, SRU_PAD_SINK
);
138 if (fse
->index
|| fse
->code
!= format
->code
)
141 if (fse
->pad
== SRU_PAD_SINK
) {
142 fse
->min_width
= SRU_MIN_SIZE
;
143 fse
->max_width
= SRU_MAX_SIZE
;
144 fse
->min_height
= SRU_MIN_SIZE
;
145 fse
->max_height
= SRU_MAX_SIZE
;
147 fse
->min_width
= format
->width
;
148 fse
->min_height
= format
->height
;
149 if (format
->width
<= SRU_MAX_SIZE
/ 2 &&
150 format
->height
<= SRU_MAX_SIZE
/ 2) {
151 fse
->max_width
= format
->width
* 2;
152 fse
->max_height
= format
->height
* 2;
154 fse
->max_width
= format
->width
;
155 fse
->max_height
= format
->height
;
162 static void sru_try_format(struct vsp1_sru
*sru
,
163 struct v4l2_subdev_pad_config
*config
,
164 unsigned int pad
, struct v4l2_mbus_framefmt
*fmt
)
166 struct v4l2_mbus_framefmt
*format
;
167 unsigned int input_area
;
168 unsigned int output_area
;
172 /* Default to YUV if the requested format is not supported. */
173 if (fmt
->code
!= MEDIA_BUS_FMT_ARGB8888_1X32
&&
174 fmt
->code
!= MEDIA_BUS_FMT_AYUV8_1X32
)
175 fmt
->code
= MEDIA_BUS_FMT_AYUV8_1X32
;
177 fmt
->width
= clamp(fmt
->width
, SRU_MIN_SIZE
, SRU_MAX_SIZE
);
178 fmt
->height
= clamp(fmt
->height
, SRU_MIN_SIZE
, SRU_MAX_SIZE
);
182 /* The SRU can't perform format conversion. */
183 format
= vsp1_entity_get_pad_format(&sru
->entity
, config
,
185 fmt
->code
= format
->code
;
187 /* We can upscale by 2 in both direction, but not independently.
188 * Compare the input and output rectangles areas (avoiding
189 * integer overflows on the output): if the requested output
190 * area is larger than 1.5^2 the input area upscale by two,
191 * otherwise don't scale.
193 input_area
= format
->width
* format
->height
;
194 output_area
= min(fmt
->width
, SRU_MAX_SIZE
)
195 * min(fmt
->height
, SRU_MAX_SIZE
);
197 if (fmt
->width
<= SRU_MAX_SIZE
/ 2 &&
198 fmt
->height
<= SRU_MAX_SIZE
/ 2 &&
199 output_area
> input_area
* 9 / 4) {
200 fmt
->width
= format
->width
* 2;
201 fmt
->height
= format
->height
* 2;
203 fmt
->width
= format
->width
;
204 fmt
->height
= format
->height
;
209 fmt
->field
= V4L2_FIELD_NONE
;
210 fmt
->colorspace
= V4L2_COLORSPACE_SRGB
;
213 static int sru_set_format(struct v4l2_subdev
*subdev
,
214 struct v4l2_subdev_pad_config
*cfg
,
215 struct v4l2_subdev_format
*fmt
)
217 struct vsp1_sru
*sru
= to_sru(subdev
);
218 struct v4l2_subdev_pad_config
*config
;
219 struct v4l2_mbus_framefmt
*format
;
221 config
= vsp1_entity_get_pad_config(&sru
->entity
, cfg
, fmt
->which
);
225 sru_try_format(sru
, config
, fmt
->pad
, &fmt
->format
);
227 format
= vsp1_entity_get_pad_format(&sru
->entity
, config
, fmt
->pad
);
228 *format
= fmt
->format
;
230 if (fmt
->pad
== SRU_PAD_SINK
) {
231 /* Propagate the format to the source pad. */
232 format
= vsp1_entity_get_pad_format(&sru
->entity
, config
,
234 *format
= fmt
->format
;
236 sru_try_format(sru
, config
, SRU_PAD_SOURCE
, format
);
242 static const struct v4l2_subdev_pad_ops sru_pad_ops
= {
243 .init_cfg
= vsp1_entity_init_cfg
,
244 .enum_mbus_code
= sru_enum_mbus_code
,
245 .enum_frame_size
= sru_enum_frame_size
,
246 .get_fmt
= vsp1_subdev_get_pad_format
,
247 .set_fmt
= sru_set_format
,
250 static const struct v4l2_subdev_ops sru_ops
= {
254 /* -----------------------------------------------------------------------------
255 * VSP1 Entity Operations
258 static void sru_configure(struct vsp1_entity
*entity
,
259 struct vsp1_pipeline
*pipe
,
260 struct vsp1_dl_list
*dl
, bool full
)
262 const struct vsp1_sru_param
*param
;
263 struct vsp1_sru
*sru
= to_sru(&entity
->subdev
);
264 struct v4l2_mbus_framefmt
*input
;
265 struct v4l2_mbus_framefmt
*output
;
271 input
= vsp1_entity_get_pad_format(&sru
->entity
, sru
->entity
.config
,
273 output
= vsp1_entity_get_pad_format(&sru
->entity
, sru
->entity
.config
,
276 if (input
->code
== MEDIA_BUS_FMT_ARGB8888_1X32
)
277 ctrl0
= VI6_SRU_CTRL0_PARAM2
| VI6_SRU_CTRL0_PARAM3
278 | VI6_SRU_CTRL0_PARAM4
;
280 ctrl0
= VI6_SRU_CTRL0_PARAM3
;
282 if (input
->width
!= output
->width
)
283 ctrl0
|= VI6_SRU_CTRL0_MODE_UPSCALE
;
285 param
= &vsp1_sru_params
[sru
->intensity
- 1];
287 ctrl0
|= param
->ctrl0
;
289 vsp1_sru_write(sru
, dl
, VI6_SRU_CTRL0
, ctrl0
);
290 vsp1_sru_write(sru
, dl
, VI6_SRU_CTRL1
, VI6_SRU_CTRL1_PARAM5
);
291 vsp1_sru_write(sru
, dl
, VI6_SRU_CTRL2
, param
->ctrl2
);
294 static const struct vsp1_entity_operations sru_entity_ops
= {
295 .configure
= sru_configure
,
298 /* -----------------------------------------------------------------------------
299 * Initialization and Cleanup
302 struct vsp1_sru
*vsp1_sru_create(struct vsp1_device
*vsp1
)
304 struct vsp1_sru
*sru
;
307 sru
= devm_kzalloc(vsp1
->dev
, sizeof(*sru
), GFP_KERNEL
);
309 return ERR_PTR(-ENOMEM
);
311 sru
->entity
.ops
= &sru_entity_ops
;
312 sru
->entity
.type
= VSP1_ENTITY_SRU
;
314 ret
= vsp1_entity_init(vsp1
, &sru
->entity
, "sru", 2, &sru_ops
,
315 MEDIA_ENT_F_PROC_VIDEO_SCALER
);
319 /* Initialize the control handler. */
320 v4l2_ctrl_handler_init(&sru
->ctrls
, 1);
321 v4l2_ctrl_new_custom(&sru
->ctrls
, &sru_intensity_control
, NULL
);
325 sru
->entity
.subdev
.ctrl_handler
= &sru
->ctrls
;
327 if (sru
->ctrls
.error
) {
328 dev_err(vsp1
->dev
, "sru: failed to initialize controls\n");
329 ret
= sru
->ctrls
.error
;
330 vsp1_entity_destroy(&sru
->entity
);