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
,
213 struct v4l2_subdev_pad_config
*cfg
,
214 struct v4l2_subdev_format
*fmt
)
216 struct vsp1_sru
*sru
= to_sru(subdev
);
218 fmt
->format
= *vsp1_entity_get_pad_format(&sru
->entity
, cfg
, fmt
->pad
,
224 static void sru_try_format(struct vsp1_sru
*sru
,
225 struct v4l2_subdev_pad_config
*cfg
,
226 unsigned int pad
, struct v4l2_mbus_framefmt
*fmt
,
227 enum v4l2_subdev_format_whence which
)
229 struct v4l2_mbus_framefmt
*format
;
230 unsigned int input_area
;
231 unsigned int output_area
;
235 /* Default to YUV if the requested format is not supported. */
236 if (fmt
->code
!= MEDIA_BUS_FMT_ARGB8888_1X32
&&
237 fmt
->code
!= MEDIA_BUS_FMT_AYUV8_1X32
)
238 fmt
->code
= MEDIA_BUS_FMT_AYUV8_1X32
;
240 fmt
->width
= clamp(fmt
->width
, SRU_MIN_SIZE
, SRU_MAX_SIZE
);
241 fmt
->height
= clamp(fmt
->height
, SRU_MIN_SIZE
, SRU_MAX_SIZE
);
245 /* The SRU can't perform format conversion. */
246 format
= vsp1_entity_get_pad_format(&sru
->entity
, cfg
,
247 SRU_PAD_SINK
, which
);
248 fmt
->code
= format
->code
;
250 /* We can upscale by 2 in both direction, but not independently.
251 * Compare the input and output rectangles areas (avoiding
252 * integer overflows on the output): if the requested output
253 * area is larger than 1.5^2 the input area upscale by two,
254 * otherwise don't scale.
256 input_area
= format
->width
* format
->height
;
257 output_area
= min(fmt
->width
, SRU_MAX_SIZE
)
258 * min(fmt
->height
, SRU_MAX_SIZE
);
260 if (fmt
->width
<= SRU_MAX_SIZE
/ 2 &&
261 fmt
->height
<= SRU_MAX_SIZE
/ 2 &&
262 output_area
> input_area
* 9 / 4) {
263 fmt
->width
= format
->width
* 2;
264 fmt
->height
= format
->height
* 2;
266 fmt
->width
= format
->width
;
267 fmt
->height
= format
->height
;
272 fmt
->field
= V4L2_FIELD_NONE
;
273 fmt
->colorspace
= V4L2_COLORSPACE_SRGB
;
276 static int sru_set_format(struct v4l2_subdev
*subdev
,
277 struct v4l2_subdev_pad_config
*cfg
,
278 struct v4l2_subdev_format
*fmt
)
280 struct vsp1_sru
*sru
= to_sru(subdev
);
281 struct v4l2_mbus_framefmt
*format
;
283 sru_try_format(sru
, cfg
, fmt
->pad
, &fmt
->format
, fmt
->which
);
285 format
= vsp1_entity_get_pad_format(&sru
->entity
, cfg
, fmt
->pad
,
287 *format
= fmt
->format
;
289 if (fmt
->pad
== SRU_PAD_SINK
) {
290 /* Propagate the format to the source pad. */
291 format
= vsp1_entity_get_pad_format(&sru
->entity
, cfg
,
292 SRU_PAD_SOURCE
, fmt
->which
);
293 *format
= fmt
->format
;
295 sru_try_format(sru
, cfg
, SRU_PAD_SOURCE
, format
, fmt
->which
);
301 /* -----------------------------------------------------------------------------
302 * V4L2 Subdevice Operations
305 static struct v4l2_subdev_video_ops sru_video_ops
= {
306 .s_stream
= sru_s_stream
,
309 static struct v4l2_subdev_pad_ops sru_pad_ops
= {
310 .enum_mbus_code
= sru_enum_mbus_code
,
311 .enum_frame_size
= sru_enum_frame_size
,
312 .get_fmt
= sru_get_format
,
313 .set_fmt
= sru_set_format
,
316 static struct v4l2_subdev_ops sru_ops
= {
317 .video
= &sru_video_ops
,
321 /* -----------------------------------------------------------------------------
322 * Initialization and Cleanup
325 struct vsp1_sru
*vsp1_sru_create(struct vsp1_device
*vsp1
)
327 struct vsp1_sru
*sru
;
330 sru
= devm_kzalloc(vsp1
->dev
, sizeof(*sru
), GFP_KERNEL
);
332 return ERR_PTR(-ENOMEM
);
334 sru
->entity
.type
= VSP1_ENTITY_SRU
;
336 ret
= vsp1_entity_init(vsp1
, &sru
->entity
, "sru", 2, &sru_ops
);
340 /* Initialize the control handler. */
341 v4l2_ctrl_handler_init(&sru
->ctrls
, 1);
342 v4l2_ctrl_new_custom(&sru
->ctrls
, &sru_intensity_control
, NULL
);
346 sru
->entity
.subdev
.ctrl_handler
= &sru
->ctrls
;
348 if (sru
->ctrls
.error
) {
349 dev_err(vsp1
->dev
, "sru: failed to initialize controls\n");
350 ret
= sru
->ctrls
.error
;
351 vsp1_entity_destroy(&sru
->entity
);