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 struct vsp1_sru_param
{
50 #define VI6_SRU_CTRL0_PARAMS(p0, p1) \
51 (((p0) << VI6_SRU_CTRL0_PARAM0_SHIFT) | \
52 ((p1) << VI6_SRU_CTRL0_PARAM1_SHIFT))
54 #define VI6_SRU_CTRL2_PARAMS(p6, p7, p8) \
55 (((p6) << VI6_SRU_CTRL2_PARAM6_SHIFT) | \
56 ((p7) << VI6_SRU_CTRL2_PARAM7_SHIFT) | \
57 ((p8) << VI6_SRU_CTRL2_PARAM8_SHIFT))
59 static const struct vsp1_sru_param vsp1_sru_params
[] = {
61 .ctrl0
= VI6_SRU_CTRL0_PARAMS(256, 4) | VI6_SRU_CTRL0_EN
,
62 .ctrl2
= VI6_SRU_CTRL2_PARAMS(24, 40, 255),
64 .ctrl0
= VI6_SRU_CTRL0_PARAMS(256, 4) | VI6_SRU_CTRL0_EN
,
65 .ctrl2
= VI6_SRU_CTRL2_PARAMS(8, 16, 255),
67 .ctrl0
= VI6_SRU_CTRL0_PARAMS(384, 5) | VI6_SRU_CTRL0_EN
,
68 .ctrl2
= VI6_SRU_CTRL2_PARAMS(36, 60, 255),
70 .ctrl0
= VI6_SRU_CTRL0_PARAMS(384, 5) | VI6_SRU_CTRL0_EN
,
71 .ctrl2
= VI6_SRU_CTRL2_PARAMS(12, 27, 255),
73 .ctrl0
= VI6_SRU_CTRL0_PARAMS(511, 6) | VI6_SRU_CTRL0_EN
,
74 .ctrl2
= VI6_SRU_CTRL2_PARAMS(48, 80, 255),
76 .ctrl0
= VI6_SRU_CTRL0_PARAMS(511, 6) | VI6_SRU_CTRL0_EN
,
77 .ctrl2
= VI6_SRU_CTRL2_PARAMS(16, 36, 255),
81 static int sru_s_ctrl(struct v4l2_ctrl
*ctrl
)
83 struct vsp1_sru
*sru
=
84 container_of(ctrl
->handler
, struct vsp1_sru
, ctrls
);
85 const struct vsp1_sru_param
*param
;
89 case V4L2_CID_VSP1_SRU_INTENSITY
:
90 param
= &vsp1_sru_params
[ctrl
->val
- 1];
92 value
= vsp1_sru_read(sru
, VI6_SRU_CTRL0
);
93 value
&= ~(VI6_SRU_CTRL0_PARAM0_MASK
|
94 VI6_SRU_CTRL0_PARAM1_MASK
);
95 value
|= param
->ctrl0
;
96 vsp1_sru_write(sru
, VI6_SRU_CTRL0
, value
);
98 vsp1_sru_write(sru
, VI6_SRU_CTRL2
, param
->ctrl2
);
105 static const struct v4l2_ctrl_ops sru_ctrl_ops
= {
106 .s_ctrl
= sru_s_ctrl
,
109 static const struct v4l2_ctrl_config sru_intensity_control
= {
110 .ops
= &sru_ctrl_ops
,
111 .id
= V4L2_CID_VSP1_SRU_INTENSITY
,
113 .type
= V4L2_CTRL_TYPE_INTEGER
,
120 /* -----------------------------------------------------------------------------
121 * V4L2 Subdevice Core Operations
124 static int sru_s_stream(struct v4l2_subdev
*subdev
, int enable
)
126 struct vsp1_sru
*sru
= to_sru(subdev
);
127 struct v4l2_mbus_framefmt
*input
;
128 struct v4l2_mbus_framefmt
*output
;
132 ret
= vsp1_entity_set_streaming(&sru
->entity
, enable
);
139 input
= &sru
->entity
.formats
[SRU_PAD_SINK
];
140 output
= &sru
->entity
.formats
[SRU_PAD_SOURCE
];
142 if (input
->code
== MEDIA_BUS_FMT_ARGB8888_1X32
)
143 ctrl0
= VI6_SRU_CTRL0_PARAM2
| VI6_SRU_CTRL0_PARAM3
144 | VI6_SRU_CTRL0_PARAM4
;
146 ctrl0
= VI6_SRU_CTRL0_PARAM3
;
148 if (input
->width
!= output
->width
)
149 ctrl0
|= VI6_SRU_CTRL0_MODE_UPSCALE
;
151 /* Take the control handler lock to ensure that the CTRL0 value won't be
152 * changed behind our back by a set control operation.
154 mutex_lock(sru
->ctrls
.lock
);
155 ctrl0
|= vsp1_sru_read(sru
, VI6_SRU_CTRL0
)
156 & (VI6_SRU_CTRL0_PARAM0_MASK
| VI6_SRU_CTRL0_PARAM1_MASK
);
157 mutex_unlock(sru
->ctrls
.lock
);
159 vsp1_sru_write(sru
, VI6_SRU_CTRL1
, VI6_SRU_CTRL1_PARAM5
);
164 /* -----------------------------------------------------------------------------
165 * V4L2 Subdevice Pad Operations
168 static int sru_enum_mbus_code(struct v4l2_subdev
*subdev
,
169 struct v4l2_subdev_pad_config
*cfg
,
170 struct v4l2_subdev_mbus_code_enum
*code
)
172 static const unsigned int codes
[] = {
173 MEDIA_BUS_FMT_ARGB8888_1X32
,
174 MEDIA_BUS_FMT_AYUV8_1X32
,
176 struct vsp1_sru
*sru
= to_sru(subdev
);
177 struct v4l2_mbus_framefmt
*format
;
179 if (code
->pad
== SRU_PAD_SINK
) {
180 if (code
->index
>= ARRAY_SIZE(codes
))
183 code
->code
= codes
[code
->index
];
185 /* The SRU can't perform format conversion, the sink format is
186 * always identical to the source format.
191 format
= vsp1_entity_get_pad_format(&sru
->entity
, cfg
,
192 SRU_PAD_SINK
, code
->which
);
193 code
->code
= format
->code
;
199 static int sru_enum_frame_size(struct v4l2_subdev
*subdev
,
200 struct v4l2_subdev_pad_config
*cfg
,
201 struct v4l2_subdev_frame_size_enum
*fse
)
203 struct vsp1_sru
*sru
= to_sru(subdev
);
204 struct v4l2_mbus_framefmt
*format
;
206 format
= vsp1_entity_get_pad_format(&sru
->entity
, cfg
,
207 SRU_PAD_SINK
, fse
->which
);
209 if (fse
->index
|| fse
->code
!= format
->code
)
212 if (fse
->pad
== SRU_PAD_SINK
) {
213 fse
->min_width
= SRU_MIN_SIZE
;
214 fse
->max_width
= SRU_MAX_SIZE
;
215 fse
->min_height
= SRU_MIN_SIZE
;
216 fse
->max_height
= SRU_MAX_SIZE
;
218 fse
->min_width
= format
->width
;
219 fse
->min_height
= format
->height
;
220 if (format
->width
<= SRU_MAX_SIZE
/ 2 &&
221 format
->height
<= SRU_MAX_SIZE
/ 2) {
222 fse
->max_width
= format
->width
* 2;
223 fse
->max_height
= format
->height
* 2;
225 fse
->max_width
= format
->width
;
226 fse
->max_height
= format
->height
;
233 static int sru_get_format(struct v4l2_subdev
*subdev
, struct v4l2_subdev_pad_config
*cfg
,
234 struct v4l2_subdev_format
*fmt
)
236 struct vsp1_sru
*sru
= to_sru(subdev
);
238 fmt
->format
= *vsp1_entity_get_pad_format(&sru
->entity
, cfg
, fmt
->pad
,
244 static void sru_try_format(struct vsp1_sru
*sru
, struct v4l2_subdev_pad_config
*cfg
,
245 unsigned int pad
, struct v4l2_mbus_framefmt
*fmt
,
246 enum v4l2_subdev_format_whence which
)
248 struct v4l2_mbus_framefmt
*format
;
249 unsigned int input_area
;
250 unsigned int output_area
;
254 /* Default to YUV if the requested format is not supported. */
255 if (fmt
->code
!= MEDIA_BUS_FMT_ARGB8888_1X32
&&
256 fmt
->code
!= MEDIA_BUS_FMT_AYUV8_1X32
)
257 fmt
->code
= MEDIA_BUS_FMT_AYUV8_1X32
;
259 fmt
->width
= clamp(fmt
->width
, SRU_MIN_SIZE
, SRU_MAX_SIZE
);
260 fmt
->height
= clamp(fmt
->height
, SRU_MIN_SIZE
, SRU_MAX_SIZE
);
264 /* The SRU can't perform format conversion. */
265 format
= vsp1_entity_get_pad_format(&sru
->entity
, cfg
,
266 SRU_PAD_SINK
, which
);
267 fmt
->code
= format
->code
;
269 /* We can upscale by 2 in both direction, but not independently.
270 * Compare the input and output rectangles areas (avoiding
271 * integer overflows on the output): if the requested output
272 * area is larger than 1.5^2 the input area upscale by two,
273 * otherwise don't scale.
275 input_area
= format
->width
* format
->height
;
276 output_area
= min(fmt
->width
, SRU_MAX_SIZE
)
277 * min(fmt
->height
, SRU_MAX_SIZE
);
279 if (fmt
->width
<= SRU_MAX_SIZE
/ 2 &&
280 fmt
->height
<= SRU_MAX_SIZE
/ 2 &&
281 output_area
> input_area
* 9 / 4) {
282 fmt
->width
= format
->width
* 2;
283 fmt
->height
= format
->height
* 2;
285 fmt
->width
= format
->width
;
286 fmt
->height
= format
->height
;
291 fmt
->field
= V4L2_FIELD_NONE
;
292 fmt
->colorspace
= V4L2_COLORSPACE_SRGB
;
295 static int sru_set_format(struct v4l2_subdev
*subdev
, struct v4l2_subdev_pad_config
*cfg
,
296 struct v4l2_subdev_format
*fmt
)
298 struct vsp1_sru
*sru
= to_sru(subdev
);
299 struct v4l2_mbus_framefmt
*format
;
301 sru_try_format(sru
, cfg
, fmt
->pad
, &fmt
->format
, fmt
->which
);
303 format
= vsp1_entity_get_pad_format(&sru
->entity
, cfg
, fmt
->pad
,
305 *format
= fmt
->format
;
307 if (fmt
->pad
== SRU_PAD_SINK
) {
308 /* Propagate the format to the source pad. */
309 format
= vsp1_entity_get_pad_format(&sru
->entity
, cfg
,
310 SRU_PAD_SOURCE
, fmt
->which
);
311 *format
= fmt
->format
;
313 sru_try_format(sru
, cfg
, SRU_PAD_SOURCE
, format
, fmt
->which
);
319 /* -----------------------------------------------------------------------------
320 * V4L2 Subdevice Operations
323 static struct v4l2_subdev_video_ops sru_video_ops
= {
324 .s_stream
= sru_s_stream
,
327 static struct v4l2_subdev_pad_ops sru_pad_ops
= {
328 .enum_mbus_code
= sru_enum_mbus_code
,
329 .enum_frame_size
= sru_enum_frame_size
,
330 .get_fmt
= sru_get_format
,
331 .set_fmt
= sru_set_format
,
334 static struct v4l2_subdev_ops sru_ops
= {
335 .video
= &sru_video_ops
,
339 /* -----------------------------------------------------------------------------
340 * Initialization and Cleanup
343 struct vsp1_sru
*vsp1_sru_create(struct vsp1_device
*vsp1
)
345 struct v4l2_subdev
*subdev
;
346 struct vsp1_sru
*sru
;
349 sru
= devm_kzalloc(vsp1
->dev
, sizeof(*sru
), GFP_KERNEL
);
351 return ERR_PTR(-ENOMEM
);
353 sru
->entity
.type
= VSP1_ENTITY_SRU
;
355 ret
= vsp1_entity_init(vsp1
, &sru
->entity
, 2);
359 /* Initialize the V4L2 subdev. */
360 subdev
= &sru
->entity
.subdev
;
361 v4l2_subdev_init(subdev
, &sru_ops
);
363 subdev
->entity
.ops
= &vsp1_media_ops
;
364 subdev
->internal_ops
= &vsp1_subdev_internal_ops
;
365 snprintf(subdev
->name
, sizeof(subdev
->name
), "%s sru",
366 dev_name(vsp1
->dev
));
367 v4l2_set_subdevdata(subdev
, sru
);
368 subdev
->flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
370 vsp1_entity_init_formats(subdev
, NULL
);
372 /* Initialize the control handler. */
373 v4l2_ctrl_handler_init(&sru
->ctrls
, 1);
374 v4l2_ctrl_new_custom(&sru
->ctrls
, &sru_intensity_control
, NULL
);
376 sru
->entity
.subdev
.ctrl_handler
= &sru
->ctrls
;
378 if (sru
->ctrls
.error
) {
379 dev_err(vsp1
->dev
, "sru: failed to initialize controls\n");
380 ret
= sru
->ctrls
.error
;
381 vsp1_entity_destroy(&sru
->entity
);