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 u32
vsp1_sru_read(struct vsp1_sru
*sru
, u32 reg
)
31 return vsp1_read(sru
->entity
.vsp1
, reg
);
34 static inline void vsp1_sru_write(struct vsp1_sru
*sru
, u32 reg
, u32 data
)
36 vsp1_write(sru
->entity
.vsp1
, reg
, data
);
39 /* -----------------------------------------------------------------------------
43 #define V4L2_CID_VSP1_SRU_INTENSITY (V4L2_CID_USER_BASE + 1)
45 static int sru_s_ctrl(struct v4l2_ctrl
*ctrl
)
47 struct vsp1_sru
*sru
=
48 container_of(ctrl
->handler
, struct vsp1_sru
, ctrls
);
51 case V4L2_CID_VSP1_SRU_INTENSITY
:
52 sru
->intensity
= ctrl
->val
;
59 static const struct v4l2_ctrl_ops sru_ctrl_ops
= {
63 static const struct v4l2_ctrl_config sru_intensity_control
= {
65 .id
= V4L2_CID_VSP1_SRU_INTENSITY
,
67 .type
= V4L2_CTRL_TYPE_INTEGER
,
73 /* -----------------------------------------------------------------------------
74 * V4L2 Subdevice Core Operations
77 struct vsp1_sru_param
{
82 #define VI6_SRU_CTRL0_PARAMS(p0, p1) \
83 (((p0) << VI6_SRU_CTRL0_PARAM0_SHIFT) | \
84 ((p1) << VI6_SRU_CTRL0_PARAM1_SHIFT))
86 #define VI6_SRU_CTRL2_PARAMS(p6, p7, p8) \
87 (((p6) << VI6_SRU_CTRL2_PARAM6_SHIFT) | \
88 ((p7) << VI6_SRU_CTRL2_PARAM7_SHIFT) | \
89 ((p8) << VI6_SRU_CTRL2_PARAM8_SHIFT))
91 static const struct vsp1_sru_param vsp1_sru_params
[] = {
93 .ctrl0
= VI6_SRU_CTRL0_PARAMS(256, 4) | VI6_SRU_CTRL0_EN
,
94 .ctrl2
= VI6_SRU_CTRL2_PARAMS(24, 40, 255),
96 .ctrl0
= VI6_SRU_CTRL0_PARAMS(256, 4) | VI6_SRU_CTRL0_EN
,
97 .ctrl2
= VI6_SRU_CTRL2_PARAMS(8, 16, 255),
99 .ctrl0
= VI6_SRU_CTRL0_PARAMS(384, 5) | VI6_SRU_CTRL0_EN
,
100 .ctrl2
= VI6_SRU_CTRL2_PARAMS(36, 60, 255),
102 .ctrl0
= VI6_SRU_CTRL0_PARAMS(384, 5) | VI6_SRU_CTRL0_EN
,
103 .ctrl2
= VI6_SRU_CTRL2_PARAMS(12, 27, 255),
105 .ctrl0
= VI6_SRU_CTRL0_PARAMS(511, 6) | VI6_SRU_CTRL0_EN
,
106 .ctrl2
= VI6_SRU_CTRL2_PARAMS(48, 80, 255),
108 .ctrl0
= VI6_SRU_CTRL0_PARAMS(511, 6) | VI6_SRU_CTRL0_EN
,
109 .ctrl2
= VI6_SRU_CTRL2_PARAMS(16, 36, 255),
113 static int sru_s_stream(struct v4l2_subdev
*subdev
, int enable
)
115 struct vsp1_sru
*sru
= to_sru(subdev
);
116 const struct vsp1_sru_param
*param
;
117 struct v4l2_mbus_framefmt
*input
;
118 struct v4l2_mbus_framefmt
*output
;
125 input
= &sru
->entity
.formats
[SRU_PAD_SINK
];
126 output
= &sru
->entity
.formats
[SRU_PAD_SOURCE
];
127 upscale
= input
->width
!= output
->width
;
128 param
= &vsp1_sru_params
[sru
->intensity
];
130 if (input
->code
== V4L2_MBUS_FMT_ARGB8888_1X32
)
131 ctrl0
= VI6_SRU_CTRL0_PARAM2
| VI6_SRU_CTRL0_PARAM3
132 | VI6_SRU_CTRL0_PARAM4
;
134 ctrl0
= VI6_SRU_CTRL0_PARAM3
;
136 vsp1_sru_write(sru
, VI6_SRU_CTRL0
, param
->ctrl0
| ctrl0
|
137 (upscale
? VI6_SRU_CTRL0_MODE_UPSCALE
: 0));
138 vsp1_sru_write(sru
, VI6_SRU_CTRL1
, VI6_SRU_CTRL1_PARAM5
);
139 vsp1_sru_write(sru
, VI6_SRU_CTRL2
, param
->ctrl2
);
144 /* -----------------------------------------------------------------------------
145 * V4L2 Subdevice Pad Operations
148 static int sru_enum_mbus_code(struct v4l2_subdev
*subdev
,
149 struct v4l2_subdev_fh
*fh
,
150 struct v4l2_subdev_mbus_code_enum
*code
)
152 static const unsigned int codes
[] = {
153 V4L2_MBUS_FMT_ARGB8888_1X32
,
154 V4L2_MBUS_FMT_AYUV8_1X32
,
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
= v4l2_subdev_get_try_format(fh
, SRU_PAD_SINK
);
171 code
->code
= format
->code
;
177 static int sru_enum_frame_size(struct v4l2_subdev
*subdev
,
178 struct v4l2_subdev_fh
*fh
,
179 struct v4l2_subdev_frame_size_enum
*fse
)
181 struct v4l2_mbus_framefmt
*format
;
183 format
= v4l2_subdev_get_try_format(fh
, SRU_PAD_SINK
);
185 if (fse
->index
|| fse
->code
!= format
->code
)
188 if (fse
->pad
== SRU_PAD_SINK
) {
189 fse
->min_width
= SRU_MIN_SIZE
;
190 fse
->max_width
= SRU_MAX_SIZE
;
191 fse
->min_height
= SRU_MIN_SIZE
;
192 fse
->max_height
= SRU_MAX_SIZE
;
194 fse
->min_width
= format
->width
;
195 fse
->min_height
= format
->height
;
196 if (format
->width
<= SRU_MAX_SIZE
/ 2 &&
197 format
->height
<= SRU_MAX_SIZE
/ 2) {
198 fse
->max_width
= format
->width
* 2;
199 fse
->max_height
= format
->height
* 2;
201 fse
->max_width
= format
->width
;
202 fse
->max_height
= format
->height
;
209 static int sru_get_format(struct v4l2_subdev
*subdev
, struct v4l2_subdev_fh
*fh
,
210 struct v4l2_subdev_format
*fmt
)
212 struct vsp1_sru
*sru
= to_sru(subdev
);
214 fmt
->format
= *vsp1_entity_get_pad_format(&sru
->entity
, fh
, fmt
->pad
,
220 static void sru_try_format(struct vsp1_sru
*sru
, struct v4l2_subdev_fh
*fh
,
221 unsigned int pad
, struct v4l2_mbus_framefmt
*fmt
,
222 enum v4l2_subdev_format_whence which
)
224 struct v4l2_mbus_framefmt
*format
;
225 unsigned int input_area
;
226 unsigned int output_area
;
230 /* Default to YUV if the requested format is not supported. */
231 if (fmt
->code
!= V4L2_MBUS_FMT_ARGB8888_1X32
&&
232 fmt
->code
!= V4L2_MBUS_FMT_AYUV8_1X32
)
233 fmt
->code
= V4L2_MBUS_FMT_AYUV8_1X32
;
235 fmt
->width
= clamp(fmt
->width
, SRU_MIN_SIZE
, SRU_MAX_SIZE
);
236 fmt
->height
= clamp(fmt
->height
, SRU_MIN_SIZE
, SRU_MAX_SIZE
);
240 /* The SRU can't perform format conversion. */
241 format
= vsp1_entity_get_pad_format(&sru
->entity
, fh
,
242 SRU_PAD_SINK
, which
);
243 fmt
->code
= format
->code
;
245 /* We can upscale by 2 in both direction, but not independently.
246 * Compare the input and output rectangles areas (avoiding
247 * integer overflows on the output): if the requested output
248 * area is larger than 1.5^2 the input area upscale by two,
249 * otherwise don't scale.
251 input_area
= format
->width
* format
->height
;
252 output_area
= min(fmt
->width
, SRU_MAX_SIZE
)
253 * min(fmt
->height
, SRU_MAX_SIZE
);
255 if (fmt
->width
<= SRU_MAX_SIZE
/ 2 &&
256 fmt
->height
<= SRU_MAX_SIZE
/ 2 &&
257 output_area
> input_area
* 9 / 4) {
258 fmt
->width
= format
->width
* 2;
259 fmt
->height
= format
->height
* 2;
261 fmt
->width
= format
->width
;
262 fmt
->height
= format
->height
;
267 fmt
->field
= V4L2_FIELD_NONE
;
268 fmt
->colorspace
= V4L2_COLORSPACE_SRGB
;
271 static int sru_set_format(struct v4l2_subdev
*subdev
, struct v4l2_subdev_fh
*fh
,
272 struct v4l2_subdev_format
*fmt
)
274 struct vsp1_sru
*sru
= to_sru(subdev
);
275 struct v4l2_mbus_framefmt
*format
;
277 sru_try_format(sru
, fh
, fmt
->pad
, &fmt
->format
, fmt
->which
);
279 format
= vsp1_entity_get_pad_format(&sru
->entity
, fh
, fmt
->pad
,
281 *format
= fmt
->format
;
283 if (fmt
->pad
== SRU_PAD_SINK
) {
284 /* Propagate the format to the source pad. */
285 format
= vsp1_entity_get_pad_format(&sru
->entity
, fh
,
286 SRU_PAD_SOURCE
, fmt
->which
);
287 *format
= fmt
->format
;
289 sru_try_format(sru
, fh
, SRU_PAD_SOURCE
, format
, fmt
->which
);
295 /* -----------------------------------------------------------------------------
296 * V4L2 Subdevice Operations
299 static struct v4l2_subdev_video_ops sru_video_ops
= {
300 .s_stream
= sru_s_stream
,
303 static struct v4l2_subdev_pad_ops sru_pad_ops
= {
304 .enum_mbus_code
= sru_enum_mbus_code
,
305 .enum_frame_size
= sru_enum_frame_size
,
306 .get_fmt
= sru_get_format
,
307 .set_fmt
= sru_set_format
,
310 static struct v4l2_subdev_ops sru_ops
= {
311 .video
= &sru_video_ops
,
315 /* -----------------------------------------------------------------------------
316 * Initialization and Cleanup
319 struct vsp1_sru
*vsp1_sru_create(struct vsp1_device
*vsp1
)
321 struct v4l2_subdev
*subdev
;
322 struct vsp1_sru
*sru
;
325 sru
= devm_kzalloc(vsp1
->dev
, sizeof(*sru
), GFP_KERNEL
);
327 return ERR_PTR(-ENOMEM
);
329 sru
->entity
.type
= VSP1_ENTITY_SRU
;
330 sru
->entity
.id
= VI6_DPR_NODE_SRU
;
332 ret
= vsp1_entity_init(vsp1
, &sru
->entity
, 2);
336 /* Initialize the V4L2 subdev. */
337 subdev
= &sru
->entity
.subdev
;
338 v4l2_subdev_init(subdev
, &sru_ops
);
340 subdev
->entity
.ops
= &vsp1_media_ops
;
341 subdev
->internal_ops
= &vsp1_subdev_internal_ops
;
342 snprintf(subdev
->name
, sizeof(subdev
->name
), "%s sru",
343 dev_name(vsp1
->dev
));
344 v4l2_set_subdevdata(subdev
, sru
);
345 subdev
->flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
347 vsp1_entity_init_formats(subdev
, NULL
);
349 /* Initialize the control handler. */
350 v4l2_ctrl_handler_init(&sru
->ctrls
, 1);
351 v4l2_ctrl_new_custom(&sru
->ctrls
, &sru_intensity_control
, NULL
);
352 v4l2_ctrl_handler_setup(&sru
->ctrls
);
353 sru
->entity
.subdev
.ctrl_handler
= &sru
->ctrls
;