2 * vsp1_rwpf.c -- R-Car VSP1 Read and Write Pixel Formatters
4 * Copyright (C) 2013-2014 Renesas Electronics 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 <media/v4l2-subdev.h>
17 #include "vsp1_rwpf.h"
18 #include "vsp1_video.h"
20 #define RWPF_MIN_WIDTH 1
21 #define RWPF_MIN_HEIGHT 1
23 struct v4l2_rect
*vsp1_rwpf_get_crop(struct vsp1_rwpf
*rwpf
,
24 struct v4l2_subdev_pad_config
*config
)
26 return v4l2_subdev_get_try_crop(&rwpf
->entity
.subdev
, config
,
30 /* -----------------------------------------------------------------------------
31 * V4L2 Subdevice Pad Operations
34 static int vsp1_rwpf_enum_mbus_code(struct v4l2_subdev
*subdev
,
35 struct v4l2_subdev_pad_config
*cfg
,
36 struct v4l2_subdev_mbus_code_enum
*code
)
38 static const unsigned int codes
[] = {
39 MEDIA_BUS_FMT_ARGB8888_1X32
,
40 MEDIA_BUS_FMT_AYUV8_1X32
,
43 if (code
->index
>= ARRAY_SIZE(codes
))
46 code
->code
= codes
[code
->index
];
51 static int vsp1_rwpf_enum_frame_size(struct v4l2_subdev
*subdev
,
52 struct v4l2_subdev_pad_config
*cfg
,
53 struct v4l2_subdev_frame_size_enum
*fse
)
55 struct vsp1_rwpf
*rwpf
= to_rwpf(subdev
);
57 return vsp1_subdev_enum_frame_size(subdev
, cfg
, fse
, RWPF_MIN_WIDTH
,
58 RWPF_MIN_HEIGHT
, rwpf
->max_width
,
62 static int vsp1_rwpf_set_format(struct v4l2_subdev
*subdev
,
63 struct v4l2_subdev_pad_config
*cfg
,
64 struct v4l2_subdev_format
*fmt
)
66 struct vsp1_rwpf
*rwpf
= to_rwpf(subdev
);
67 struct v4l2_subdev_pad_config
*config
;
68 struct v4l2_mbus_framefmt
*format
;
69 struct v4l2_rect
*crop
;
71 config
= vsp1_entity_get_pad_config(&rwpf
->entity
, cfg
, fmt
->which
);
75 /* Default to YUV if the requested format is not supported. */
76 if (fmt
->format
.code
!= MEDIA_BUS_FMT_ARGB8888_1X32
&&
77 fmt
->format
.code
!= MEDIA_BUS_FMT_AYUV8_1X32
)
78 fmt
->format
.code
= MEDIA_BUS_FMT_AYUV8_1X32
;
80 format
= vsp1_entity_get_pad_format(&rwpf
->entity
, config
, fmt
->pad
);
82 if (fmt
->pad
== RWPF_PAD_SOURCE
) {
83 /* The RWPF performs format conversion but can't scale, only the
84 * format code can be changed on the source pad.
86 format
->code
= fmt
->format
.code
;
87 fmt
->format
= *format
;
91 format
->code
= fmt
->format
.code
;
92 format
->width
= clamp_t(unsigned int, fmt
->format
.width
,
93 RWPF_MIN_WIDTH
, rwpf
->max_width
);
94 format
->height
= clamp_t(unsigned int, fmt
->format
.height
,
95 RWPF_MIN_HEIGHT
, rwpf
->max_height
);
96 format
->field
= V4L2_FIELD_NONE
;
97 format
->colorspace
= V4L2_COLORSPACE_SRGB
;
99 fmt
->format
= *format
;
101 /* Update the sink crop rectangle. */
102 crop
= vsp1_rwpf_get_crop(rwpf
, config
);
105 crop
->width
= fmt
->format
.width
;
106 crop
->height
= fmt
->format
.height
;
108 /* Propagate the format to the source pad. */
109 format
= vsp1_entity_get_pad_format(&rwpf
->entity
, config
,
111 *format
= fmt
->format
;
116 static int vsp1_rwpf_get_selection(struct v4l2_subdev
*subdev
,
117 struct v4l2_subdev_pad_config
*cfg
,
118 struct v4l2_subdev_selection
*sel
)
120 struct vsp1_rwpf
*rwpf
= to_rwpf(subdev
);
121 struct v4l2_subdev_pad_config
*config
;
122 struct v4l2_mbus_framefmt
*format
;
124 /* Cropping is implemented on the sink pad. */
125 if (sel
->pad
!= RWPF_PAD_SINK
)
128 config
= vsp1_entity_get_pad_config(&rwpf
->entity
, cfg
, sel
->which
);
132 switch (sel
->target
) {
133 case V4L2_SEL_TGT_CROP
:
134 sel
->r
= *vsp1_rwpf_get_crop(rwpf
, config
);
137 case V4L2_SEL_TGT_CROP_BOUNDS
:
138 format
= vsp1_entity_get_pad_format(&rwpf
->entity
, config
,
142 sel
->r
.width
= format
->width
;
143 sel
->r
.height
= format
->height
;
153 static int vsp1_rwpf_set_selection(struct v4l2_subdev
*subdev
,
154 struct v4l2_subdev_pad_config
*cfg
,
155 struct v4l2_subdev_selection
*sel
)
157 struct vsp1_rwpf
*rwpf
= to_rwpf(subdev
);
158 struct v4l2_subdev_pad_config
*config
;
159 struct v4l2_mbus_framefmt
*format
;
160 struct v4l2_rect
*crop
;
162 /* Cropping is implemented on the sink pad. */
163 if (sel
->pad
!= RWPF_PAD_SINK
)
166 if (sel
->target
!= V4L2_SEL_TGT_CROP
)
169 config
= vsp1_entity_get_pad_config(&rwpf
->entity
, cfg
, sel
->which
);
173 /* Make sure the crop rectangle is entirely contained in the image. The
174 * WPF top and left offsets are limited to 255.
176 format
= vsp1_entity_get_pad_format(&rwpf
->entity
, config
,
179 /* Restrict the crop rectangle coordinates to multiples of 2 to avoid
180 * shifting the color plane.
182 if (format
->code
== MEDIA_BUS_FMT_AYUV8_1X32
) {
183 sel
->r
.left
= ALIGN(sel
->r
.left
, 2);
184 sel
->r
.top
= ALIGN(sel
->r
.top
, 2);
185 sel
->r
.width
= round_down(sel
->r
.width
, 2);
186 sel
->r
.height
= round_down(sel
->r
.height
, 2);
189 sel
->r
.left
= min_t(unsigned int, sel
->r
.left
, format
->width
- 2);
190 sel
->r
.top
= min_t(unsigned int, sel
->r
.top
, format
->height
- 2);
191 if (rwpf
->entity
.type
== VSP1_ENTITY_WPF
) {
192 sel
->r
.left
= min_t(unsigned int, sel
->r
.left
, 255);
193 sel
->r
.top
= min_t(unsigned int, sel
->r
.top
, 255);
195 sel
->r
.width
= min_t(unsigned int, sel
->r
.width
,
196 format
->width
- sel
->r
.left
);
197 sel
->r
.height
= min_t(unsigned int, sel
->r
.height
,
198 format
->height
- sel
->r
.top
);
200 crop
= vsp1_rwpf_get_crop(rwpf
, config
);
203 /* Propagate the format to the source pad. */
204 format
= vsp1_entity_get_pad_format(&rwpf
->entity
, config
,
206 format
->width
= crop
->width
;
207 format
->height
= crop
->height
;
212 const struct v4l2_subdev_pad_ops vsp1_rwpf_pad_ops
= {
213 .init_cfg
= vsp1_entity_init_cfg
,
214 .enum_mbus_code
= vsp1_rwpf_enum_mbus_code
,
215 .enum_frame_size
= vsp1_rwpf_enum_frame_size
,
216 .get_fmt
= vsp1_subdev_get_pad_format
,
217 .set_fmt
= vsp1_rwpf_set_format
,
218 .get_selection
= vsp1_rwpf_get_selection
,
219 .set_selection
= vsp1_rwpf_set_selection
,
222 /* -----------------------------------------------------------------------------
226 static int vsp1_rwpf_s_ctrl(struct v4l2_ctrl
*ctrl
)
228 struct vsp1_rwpf
*rwpf
=
229 container_of(ctrl
->handler
, struct vsp1_rwpf
, ctrls
);
232 case V4L2_CID_ALPHA_COMPONENT
:
233 rwpf
->alpha
= ctrl
->val
;
240 static const struct v4l2_ctrl_ops vsp1_rwpf_ctrl_ops
= {
241 .s_ctrl
= vsp1_rwpf_s_ctrl
,
244 int vsp1_rwpf_init_ctrls(struct vsp1_rwpf
*rwpf
, unsigned int ncontrols
)
246 v4l2_ctrl_handler_init(&rwpf
->ctrls
, ncontrols
+ 1);
247 v4l2_ctrl_new_std(&rwpf
->ctrls
, &vsp1_rwpf_ctrl_ops
,
248 V4L2_CID_ALPHA_COMPONENT
, 0, 255, 1, 255);
250 rwpf
->entity
.subdev
.ctrl_handler
= &rwpf
->ctrls
;
252 return rwpf
->ctrls
.error
;