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
);
56 struct v4l2_subdev_pad_config
*config
;
57 struct v4l2_mbus_framefmt
*format
;
59 config
= vsp1_entity_get_pad_config(&rwpf
->entity
, cfg
, fse
->which
);
63 format
= vsp1_entity_get_pad_format(&rwpf
->entity
, config
, fse
->pad
);
65 if (fse
->index
|| fse
->code
!= format
->code
)
68 if (fse
->pad
== RWPF_PAD_SINK
) {
69 fse
->min_width
= RWPF_MIN_WIDTH
;
70 fse
->max_width
= rwpf
->max_width
;
71 fse
->min_height
= RWPF_MIN_HEIGHT
;
72 fse
->max_height
= rwpf
->max_height
;
74 /* The size on the source pad are fixed and always identical to
75 * the size on the sink pad.
77 fse
->min_width
= format
->width
;
78 fse
->max_width
= format
->width
;
79 fse
->min_height
= format
->height
;
80 fse
->max_height
= format
->height
;
86 static int vsp1_rwpf_get_format(struct v4l2_subdev
*subdev
,
87 struct v4l2_subdev_pad_config
*cfg
,
88 struct v4l2_subdev_format
*fmt
)
90 struct vsp1_rwpf
*rwpf
= to_rwpf(subdev
);
91 struct v4l2_subdev_pad_config
*config
;
93 config
= vsp1_entity_get_pad_config(&rwpf
->entity
, cfg
, fmt
->which
);
97 fmt
->format
= *vsp1_entity_get_pad_format(&rwpf
->entity
, config
,
103 static int vsp1_rwpf_set_format(struct v4l2_subdev
*subdev
,
104 struct v4l2_subdev_pad_config
*cfg
,
105 struct v4l2_subdev_format
*fmt
)
107 struct vsp1_rwpf
*rwpf
= to_rwpf(subdev
);
108 struct v4l2_subdev_pad_config
*config
;
109 struct v4l2_mbus_framefmt
*format
;
110 struct v4l2_rect
*crop
;
112 config
= vsp1_entity_get_pad_config(&rwpf
->entity
, cfg
, fmt
->which
);
116 /* Default to YUV if the requested format is not supported. */
117 if (fmt
->format
.code
!= MEDIA_BUS_FMT_ARGB8888_1X32
&&
118 fmt
->format
.code
!= MEDIA_BUS_FMT_AYUV8_1X32
)
119 fmt
->format
.code
= MEDIA_BUS_FMT_AYUV8_1X32
;
121 format
= vsp1_entity_get_pad_format(&rwpf
->entity
, config
, fmt
->pad
);
123 if (fmt
->pad
== RWPF_PAD_SOURCE
) {
124 /* The RWPF performs format conversion but can't scale, only the
125 * format code can be changed on the source pad.
127 format
->code
= fmt
->format
.code
;
128 fmt
->format
= *format
;
132 format
->code
= fmt
->format
.code
;
133 format
->width
= clamp_t(unsigned int, fmt
->format
.width
,
134 RWPF_MIN_WIDTH
, rwpf
->max_width
);
135 format
->height
= clamp_t(unsigned int, fmt
->format
.height
,
136 RWPF_MIN_HEIGHT
, rwpf
->max_height
);
137 format
->field
= V4L2_FIELD_NONE
;
138 format
->colorspace
= V4L2_COLORSPACE_SRGB
;
140 fmt
->format
= *format
;
142 /* Update the sink crop rectangle. */
143 crop
= vsp1_rwpf_get_crop(rwpf
, config
);
146 crop
->width
= fmt
->format
.width
;
147 crop
->height
= fmt
->format
.height
;
149 /* Propagate the format to the source pad. */
150 format
= vsp1_entity_get_pad_format(&rwpf
->entity
, config
,
152 *format
= fmt
->format
;
157 static int vsp1_rwpf_get_selection(struct v4l2_subdev
*subdev
,
158 struct v4l2_subdev_pad_config
*cfg
,
159 struct v4l2_subdev_selection
*sel
)
161 struct vsp1_rwpf
*rwpf
= to_rwpf(subdev
);
162 struct v4l2_subdev_pad_config
*config
;
163 struct v4l2_mbus_framefmt
*format
;
165 /* Cropping is implemented on the sink pad. */
166 if (sel
->pad
!= RWPF_PAD_SINK
)
169 config
= vsp1_entity_get_pad_config(&rwpf
->entity
, cfg
, sel
->which
);
173 switch (sel
->target
) {
174 case V4L2_SEL_TGT_CROP
:
175 sel
->r
= *vsp1_rwpf_get_crop(rwpf
, config
);
178 case V4L2_SEL_TGT_CROP_BOUNDS
:
179 format
= vsp1_entity_get_pad_format(&rwpf
->entity
, config
,
183 sel
->r
.width
= format
->width
;
184 sel
->r
.height
= format
->height
;
194 static int vsp1_rwpf_set_selection(struct v4l2_subdev
*subdev
,
195 struct v4l2_subdev_pad_config
*cfg
,
196 struct v4l2_subdev_selection
*sel
)
198 struct vsp1_rwpf
*rwpf
= to_rwpf(subdev
);
199 struct v4l2_subdev_pad_config
*config
;
200 struct v4l2_mbus_framefmt
*format
;
201 struct v4l2_rect
*crop
;
203 /* Cropping is implemented on the sink pad. */
204 if (sel
->pad
!= RWPF_PAD_SINK
)
207 if (sel
->target
!= V4L2_SEL_TGT_CROP
)
210 config
= vsp1_entity_get_pad_config(&rwpf
->entity
, cfg
, sel
->which
);
214 /* Make sure the crop rectangle is entirely contained in the image. The
215 * WPF top and left offsets are limited to 255.
217 format
= vsp1_entity_get_pad_format(&rwpf
->entity
, config
,
220 /* Restrict the crop rectangle coordinates to multiples of 2 to avoid
221 * shifting the color plane.
223 if (format
->code
== MEDIA_BUS_FMT_AYUV8_1X32
) {
224 sel
->r
.left
= ALIGN(sel
->r
.left
, 2);
225 sel
->r
.top
= ALIGN(sel
->r
.top
, 2);
226 sel
->r
.width
= round_down(sel
->r
.width
, 2);
227 sel
->r
.height
= round_down(sel
->r
.height
, 2);
230 sel
->r
.left
= min_t(unsigned int, sel
->r
.left
, format
->width
- 2);
231 sel
->r
.top
= min_t(unsigned int, sel
->r
.top
, format
->height
- 2);
232 if (rwpf
->entity
.type
== VSP1_ENTITY_WPF
) {
233 sel
->r
.left
= min_t(unsigned int, sel
->r
.left
, 255);
234 sel
->r
.top
= min_t(unsigned int, sel
->r
.top
, 255);
236 sel
->r
.width
= min_t(unsigned int, sel
->r
.width
,
237 format
->width
- sel
->r
.left
);
238 sel
->r
.height
= min_t(unsigned int, sel
->r
.height
,
239 format
->height
- sel
->r
.top
);
241 crop
= vsp1_rwpf_get_crop(rwpf
, config
);
244 /* Propagate the format to the source pad. */
245 format
= vsp1_entity_get_pad_format(&rwpf
->entity
, config
,
247 format
->width
= crop
->width
;
248 format
->height
= crop
->height
;
253 const struct v4l2_subdev_pad_ops vsp1_rwpf_pad_ops
= {
254 .init_cfg
= vsp1_entity_init_cfg
,
255 .enum_mbus_code
= vsp1_rwpf_enum_mbus_code
,
256 .enum_frame_size
= vsp1_rwpf_enum_frame_size
,
257 .get_fmt
= vsp1_rwpf_get_format
,
258 .set_fmt
= vsp1_rwpf_set_format
,
259 .get_selection
= vsp1_rwpf_get_selection
,
260 .set_selection
= vsp1_rwpf_set_selection
,
263 /* -----------------------------------------------------------------------------
267 static int vsp1_rwpf_s_ctrl(struct v4l2_ctrl
*ctrl
)
269 struct vsp1_rwpf
*rwpf
=
270 container_of(ctrl
->handler
, struct vsp1_rwpf
, ctrls
);
273 case V4L2_CID_ALPHA_COMPONENT
:
274 rwpf
->alpha
= ctrl
->val
;
281 static const struct v4l2_ctrl_ops vsp1_rwpf_ctrl_ops
= {
282 .s_ctrl
= vsp1_rwpf_s_ctrl
,
285 int vsp1_rwpf_init_ctrls(struct vsp1_rwpf
*rwpf
)
289 v4l2_ctrl_handler_init(&rwpf
->ctrls
, 1);
290 v4l2_ctrl_new_std(&rwpf
->ctrls
, &vsp1_rwpf_ctrl_ops
,
291 V4L2_CID_ALPHA_COMPONENT
, 0, 255, 1, 255);
293 rwpf
->entity
.subdev
.ctrl_handler
= &rwpf
->ctrls
;
295 return rwpf
->ctrls
.error
;