1 // SPDX-License-Identifier: GPL-2.0+
3 * vsp1_rwpf.c -- R-Car VSP1 Read and Write Pixel Formatters
5 * Copyright (C) 2013-2014 Renesas Electronics Corporation
7 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
10 #include <media/v4l2-subdev.h>
13 #include "vsp1_rwpf.h"
14 #include "vsp1_video.h"
16 #define RWPF_MIN_WIDTH 1
17 #define RWPF_MIN_HEIGHT 1
19 struct v4l2_rect
*vsp1_rwpf_get_crop(struct vsp1_rwpf
*rwpf
,
20 struct v4l2_subdev_pad_config
*config
)
22 return v4l2_subdev_get_try_crop(&rwpf
->entity
.subdev
, config
,
26 /* -----------------------------------------------------------------------------
27 * V4L2 Subdevice Pad Operations
30 static int vsp1_rwpf_enum_mbus_code(struct v4l2_subdev
*subdev
,
31 struct v4l2_subdev_pad_config
*cfg
,
32 struct v4l2_subdev_mbus_code_enum
*code
)
34 static const unsigned int codes
[] = {
35 MEDIA_BUS_FMT_ARGB8888_1X32
,
36 MEDIA_BUS_FMT_AHSV8888_1X32
,
37 MEDIA_BUS_FMT_AYUV8_1X32
,
40 if (code
->index
>= ARRAY_SIZE(codes
))
43 code
->code
= codes
[code
->index
];
48 static int vsp1_rwpf_enum_frame_size(struct v4l2_subdev
*subdev
,
49 struct v4l2_subdev_pad_config
*cfg
,
50 struct v4l2_subdev_frame_size_enum
*fse
)
52 struct vsp1_rwpf
*rwpf
= to_rwpf(subdev
);
54 return vsp1_subdev_enum_frame_size(subdev
, cfg
, fse
, RWPF_MIN_WIDTH
,
55 RWPF_MIN_HEIGHT
, rwpf
->max_width
,
59 static int vsp1_rwpf_set_format(struct v4l2_subdev
*subdev
,
60 struct v4l2_subdev_pad_config
*cfg
,
61 struct v4l2_subdev_format
*fmt
)
63 struct vsp1_rwpf
*rwpf
= to_rwpf(subdev
);
64 struct v4l2_subdev_pad_config
*config
;
65 struct v4l2_mbus_framefmt
*format
;
68 mutex_lock(&rwpf
->entity
.lock
);
70 config
= vsp1_entity_get_pad_config(&rwpf
->entity
, cfg
, fmt
->which
);
76 /* Default to YUV if the requested format is not supported. */
77 if (fmt
->format
.code
!= MEDIA_BUS_FMT_ARGB8888_1X32
&&
78 fmt
->format
.code
!= MEDIA_BUS_FMT_AHSV8888_1X32
&&
79 fmt
->format
.code
!= MEDIA_BUS_FMT_AYUV8_1X32
)
80 fmt
->format
.code
= MEDIA_BUS_FMT_AYUV8_1X32
;
82 format
= vsp1_entity_get_pad_format(&rwpf
->entity
, config
, fmt
->pad
);
84 if (fmt
->pad
== RWPF_PAD_SOURCE
) {
86 * The RWPF performs format conversion but can't scale, only the
87 * format code can be changed on the source pad.
89 format
->code
= fmt
->format
.code
;
90 fmt
->format
= *format
;
94 format
->code
= fmt
->format
.code
;
95 format
->width
= clamp_t(unsigned int, fmt
->format
.width
,
96 RWPF_MIN_WIDTH
, rwpf
->max_width
);
97 format
->height
= clamp_t(unsigned int, fmt
->format
.height
,
98 RWPF_MIN_HEIGHT
, rwpf
->max_height
);
99 format
->field
= V4L2_FIELD_NONE
;
100 format
->colorspace
= V4L2_COLORSPACE_SRGB
;
102 fmt
->format
= *format
;
104 if (rwpf
->entity
.type
== VSP1_ENTITY_RPF
) {
105 struct v4l2_rect
*crop
;
107 /* Update the sink crop rectangle. */
108 crop
= vsp1_rwpf_get_crop(rwpf
, config
);
111 crop
->width
= fmt
->format
.width
;
112 crop
->height
= fmt
->format
.height
;
115 /* Propagate the format to the source pad. */
116 format
= vsp1_entity_get_pad_format(&rwpf
->entity
, config
,
118 *format
= fmt
->format
;
120 if (rwpf
->flip
.rotate
) {
121 format
->width
= fmt
->format
.height
;
122 format
->height
= fmt
->format
.width
;
126 mutex_unlock(&rwpf
->entity
.lock
);
130 static int vsp1_rwpf_get_selection(struct v4l2_subdev
*subdev
,
131 struct v4l2_subdev_pad_config
*cfg
,
132 struct v4l2_subdev_selection
*sel
)
134 struct vsp1_rwpf
*rwpf
= to_rwpf(subdev
);
135 struct v4l2_subdev_pad_config
*config
;
136 struct v4l2_mbus_framefmt
*format
;
140 * Cropping is only supported on the RPF and is implemented on the sink
143 if (rwpf
->entity
.type
== VSP1_ENTITY_WPF
|| sel
->pad
!= RWPF_PAD_SINK
)
146 mutex_lock(&rwpf
->entity
.lock
);
148 config
= vsp1_entity_get_pad_config(&rwpf
->entity
, cfg
, sel
->which
);
154 switch (sel
->target
) {
155 case V4L2_SEL_TGT_CROP
:
156 sel
->r
= *vsp1_rwpf_get_crop(rwpf
, config
);
159 case V4L2_SEL_TGT_CROP_BOUNDS
:
160 format
= vsp1_entity_get_pad_format(&rwpf
->entity
, config
,
164 sel
->r
.width
= format
->width
;
165 sel
->r
.height
= format
->height
;
174 mutex_unlock(&rwpf
->entity
.lock
);
178 static int vsp1_rwpf_set_selection(struct v4l2_subdev
*subdev
,
179 struct v4l2_subdev_pad_config
*cfg
,
180 struct v4l2_subdev_selection
*sel
)
182 struct vsp1_rwpf
*rwpf
= to_rwpf(subdev
);
183 struct v4l2_subdev_pad_config
*config
;
184 struct v4l2_mbus_framefmt
*format
;
185 struct v4l2_rect
*crop
;
189 * Cropping is only supported on the RPF and is implemented on the sink
192 if (rwpf
->entity
.type
== VSP1_ENTITY_WPF
|| sel
->pad
!= RWPF_PAD_SINK
)
195 if (sel
->target
!= V4L2_SEL_TGT_CROP
)
198 mutex_lock(&rwpf
->entity
.lock
);
200 config
= vsp1_entity_get_pad_config(&rwpf
->entity
, cfg
, sel
->which
);
206 /* Make sure the crop rectangle is entirely contained in the image. */
207 format
= vsp1_entity_get_pad_format(&rwpf
->entity
, config
,
211 * Restrict the crop rectangle coordinates to multiples of 2 to avoid
212 * shifting the color plane.
214 if (format
->code
== MEDIA_BUS_FMT_AYUV8_1X32
) {
215 sel
->r
.left
= ALIGN(sel
->r
.left
, 2);
216 sel
->r
.top
= ALIGN(sel
->r
.top
, 2);
217 sel
->r
.width
= round_down(sel
->r
.width
, 2);
218 sel
->r
.height
= round_down(sel
->r
.height
, 2);
221 sel
->r
.left
= min_t(unsigned int, sel
->r
.left
, format
->width
- 2);
222 sel
->r
.top
= min_t(unsigned int, sel
->r
.top
, format
->height
- 2);
223 sel
->r
.width
= min_t(unsigned int, sel
->r
.width
,
224 format
->width
- sel
->r
.left
);
225 sel
->r
.height
= min_t(unsigned int, sel
->r
.height
,
226 format
->height
- sel
->r
.top
);
228 crop
= vsp1_rwpf_get_crop(rwpf
, config
);
231 /* Propagate the format to the source pad. */
232 format
= vsp1_entity_get_pad_format(&rwpf
->entity
, config
,
234 format
->width
= crop
->width
;
235 format
->height
= crop
->height
;
238 mutex_unlock(&rwpf
->entity
.lock
);
242 const struct v4l2_subdev_pad_ops vsp1_rwpf_pad_ops
= {
243 .init_cfg
= vsp1_entity_init_cfg
,
244 .enum_mbus_code
= vsp1_rwpf_enum_mbus_code
,
245 .enum_frame_size
= vsp1_rwpf_enum_frame_size
,
246 .get_fmt
= vsp1_subdev_get_pad_format
,
247 .set_fmt
= vsp1_rwpf_set_format
,
248 .get_selection
= vsp1_rwpf_get_selection
,
249 .set_selection
= vsp1_rwpf_set_selection
,
252 /* -----------------------------------------------------------------------------
256 static int vsp1_rwpf_s_ctrl(struct v4l2_ctrl
*ctrl
)
258 struct vsp1_rwpf
*rwpf
=
259 container_of(ctrl
->handler
, struct vsp1_rwpf
, ctrls
);
262 case V4L2_CID_ALPHA_COMPONENT
:
263 rwpf
->alpha
= ctrl
->val
;
270 static const struct v4l2_ctrl_ops vsp1_rwpf_ctrl_ops
= {
271 .s_ctrl
= vsp1_rwpf_s_ctrl
,
274 int vsp1_rwpf_init_ctrls(struct vsp1_rwpf
*rwpf
, unsigned int ncontrols
)
276 v4l2_ctrl_handler_init(&rwpf
->ctrls
, ncontrols
+ 1);
277 v4l2_ctrl_new_std(&rwpf
->ctrls
, &vsp1_rwpf_ctrl_ops
,
278 V4L2_CID_ALPHA_COMPONENT
, 0, 255, 1, 255);
280 rwpf
->entity
.subdev
.ctrl_handler
= &rwpf
->ctrls
;
282 return rwpf
->ctrls
.error
;