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 /* -----------------------------------------------------------------------------
24 * V4L2 Subdevice Pad Operations
27 int vsp1_rwpf_enum_mbus_code(struct v4l2_subdev
*subdev
,
28 struct v4l2_subdev_pad_config
*cfg
,
29 struct v4l2_subdev_mbus_code_enum
*code
)
31 static const unsigned int codes
[] = {
32 MEDIA_BUS_FMT_ARGB8888_1X32
,
33 MEDIA_BUS_FMT_AYUV8_1X32
,
36 if (code
->index
>= ARRAY_SIZE(codes
))
39 code
->code
= codes
[code
->index
];
44 int vsp1_rwpf_enum_frame_size(struct v4l2_subdev
*subdev
,
45 struct v4l2_subdev_pad_config
*cfg
,
46 struct v4l2_subdev_frame_size_enum
*fse
)
48 struct vsp1_rwpf
*rwpf
= to_rwpf(subdev
);
49 struct v4l2_subdev_pad_config
*config
;
50 struct v4l2_mbus_framefmt
*format
;
52 config
= vsp1_entity_get_pad_config(&rwpf
->entity
, cfg
, fse
->which
);
56 format
= vsp1_entity_get_pad_format(&rwpf
->entity
, config
, fse
->pad
);
58 if (fse
->index
|| fse
->code
!= format
->code
)
61 if (fse
->pad
== RWPF_PAD_SINK
) {
62 fse
->min_width
= RWPF_MIN_WIDTH
;
63 fse
->max_width
= rwpf
->max_width
;
64 fse
->min_height
= RWPF_MIN_HEIGHT
;
65 fse
->max_height
= rwpf
->max_height
;
67 /* The size on the source pad are fixed and always identical to
68 * the size on the sink pad.
70 fse
->min_width
= format
->width
;
71 fse
->max_width
= format
->width
;
72 fse
->min_height
= format
->height
;
73 fse
->max_height
= format
->height
;
79 static struct v4l2_rect
*
80 vsp1_rwpf_get_crop(struct vsp1_rwpf
*rwpf
, struct v4l2_subdev_pad_config
*cfg
,
84 case V4L2_SUBDEV_FORMAT_TRY
:
85 return v4l2_subdev_get_try_crop(&rwpf
->entity
.subdev
, cfg
,
87 case V4L2_SUBDEV_FORMAT_ACTIVE
:
94 int vsp1_rwpf_get_format(struct v4l2_subdev
*subdev
,
95 struct v4l2_subdev_pad_config
*cfg
,
96 struct v4l2_subdev_format
*fmt
)
98 struct vsp1_rwpf
*rwpf
= to_rwpf(subdev
);
99 struct v4l2_subdev_pad_config
*config
;
101 config
= vsp1_entity_get_pad_config(&rwpf
->entity
, cfg
, fmt
->which
);
105 fmt
->format
= *vsp1_entity_get_pad_format(&rwpf
->entity
, config
,
111 int vsp1_rwpf_set_format(struct v4l2_subdev
*subdev
,
112 struct v4l2_subdev_pad_config
*cfg
,
113 struct v4l2_subdev_format
*fmt
)
115 struct vsp1_rwpf
*rwpf
= to_rwpf(subdev
);
116 struct v4l2_subdev_pad_config
*config
;
117 struct v4l2_mbus_framefmt
*format
;
118 struct v4l2_rect
*crop
;
120 config
= vsp1_entity_get_pad_config(&rwpf
->entity
, cfg
, fmt
->which
);
124 /* Default to YUV if the requested format is not supported. */
125 if (fmt
->format
.code
!= MEDIA_BUS_FMT_ARGB8888_1X32
&&
126 fmt
->format
.code
!= MEDIA_BUS_FMT_AYUV8_1X32
)
127 fmt
->format
.code
= MEDIA_BUS_FMT_AYUV8_1X32
;
129 format
= vsp1_entity_get_pad_format(&rwpf
->entity
, config
, fmt
->pad
);
131 if (fmt
->pad
== RWPF_PAD_SOURCE
) {
132 /* The RWPF performs format conversion but can't scale, only the
133 * format code can be changed on the source pad.
135 format
->code
= fmt
->format
.code
;
136 fmt
->format
= *format
;
140 format
->code
= fmt
->format
.code
;
141 format
->width
= clamp_t(unsigned int, fmt
->format
.width
,
142 RWPF_MIN_WIDTH
, rwpf
->max_width
);
143 format
->height
= clamp_t(unsigned int, fmt
->format
.height
,
144 RWPF_MIN_HEIGHT
, rwpf
->max_height
);
145 format
->field
= V4L2_FIELD_NONE
;
146 format
->colorspace
= V4L2_COLORSPACE_SRGB
;
148 fmt
->format
= *format
;
150 /* Update the sink crop rectangle. */
151 crop
= vsp1_rwpf_get_crop(rwpf
, cfg
, fmt
->which
);
154 crop
->width
= fmt
->format
.width
;
155 crop
->height
= fmt
->format
.height
;
157 /* Propagate the format to the source pad. */
158 format
= vsp1_entity_get_pad_format(&rwpf
->entity
, config
,
160 *format
= fmt
->format
;
165 int vsp1_rwpf_get_selection(struct v4l2_subdev
*subdev
,
166 struct v4l2_subdev_pad_config
*cfg
,
167 struct v4l2_subdev_selection
*sel
)
169 struct vsp1_rwpf
*rwpf
= to_rwpf(subdev
);
170 struct v4l2_subdev_pad_config
*config
;
171 struct v4l2_mbus_framefmt
*format
;
173 /* Cropping is implemented on the sink pad. */
174 if (sel
->pad
!= RWPF_PAD_SINK
)
177 config
= vsp1_entity_get_pad_config(&rwpf
->entity
, cfg
, sel
->which
);
181 switch (sel
->target
) {
182 case V4L2_SEL_TGT_CROP
:
183 sel
->r
= *vsp1_rwpf_get_crop(rwpf
, cfg
, sel
->which
);
186 case V4L2_SEL_TGT_CROP_BOUNDS
:
187 format
= vsp1_entity_get_pad_format(&rwpf
->entity
, config
,
191 sel
->r
.width
= format
->width
;
192 sel
->r
.height
= format
->height
;
202 int vsp1_rwpf_set_selection(struct v4l2_subdev
*subdev
,
203 struct v4l2_subdev_pad_config
*cfg
,
204 struct v4l2_subdev_selection
*sel
)
206 struct vsp1_rwpf
*rwpf
= to_rwpf(subdev
);
207 struct v4l2_subdev_pad_config
*config
;
208 struct v4l2_mbus_framefmt
*format
;
209 struct v4l2_rect
*crop
;
211 /* Cropping is implemented on the sink pad. */
212 if (sel
->pad
!= RWPF_PAD_SINK
)
215 if (sel
->target
!= V4L2_SEL_TGT_CROP
)
218 config
= vsp1_entity_get_pad_config(&rwpf
->entity
, cfg
, sel
->which
);
222 /* Make sure the crop rectangle is entirely contained in the image. The
223 * WPF top and left offsets are limited to 255.
225 format
= vsp1_entity_get_pad_format(&rwpf
->entity
, config
,
228 /* Restrict the crop rectangle coordinates to multiples of 2 to avoid
229 * shifting the color plane.
231 if (format
->code
== MEDIA_BUS_FMT_AYUV8_1X32
) {
232 sel
->r
.left
= ALIGN(sel
->r
.left
, 2);
233 sel
->r
.top
= ALIGN(sel
->r
.top
, 2);
234 sel
->r
.width
= round_down(sel
->r
.width
, 2);
235 sel
->r
.height
= round_down(sel
->r
.height
, 2);
238 sel
->r
.left
= min_t(unsigned int, sel
->r
.left
, format
->width
- 2);
239 sel
->r
.top
= min_t(unsigned int, sel
->r
.top
, format
->height
- 2);
240 if (rwpf
->entity
.type
== VSP1_ENTITY_WPF
) {
241 sel
->r
.left
= min_t(unsigned int, sel
->r
.left
, 255);
242 sel
->r
.top
= min_t(unsigned int, sel
->r
.top
, 255);
244 sel
->r
.width
= min_t(unsigned int, sel
->r
.width
,
245 format
->width
- sel
->r
.left
);
246 sel
->r
.height
= min_t(unsigned int, sel
->r
.height
,
247 format
->height
- sel
->r
.top
);
249 crop
= vsp1_rwpf_get_crop(rwpf
, cfg
, sel
->which
);
252 /* Propagate the format to the source pad. */
253 format
= vsp1_entity_get_pad_format(&rwpf
->entity
, config
,
255 format
->width
= crop
->width
;
256 format
->height
= crop
->height
;
261 /* -----------------------------------------------------------------------------
265 static int vsp1_rwpf_s_ctrl(struct v4l2_ctrl
*ctrl
)
267 struct vsp1_rwpf
*rwpf
=
268 container_of(ctrl
->handler
, struct vsp1_rwpf
, ctrls
);
271 case V4L2_CID_ALPHA_COMPONENT
:
272 rwpf
->alpha
= ctrl
->val
;
279 static const struct v4l2_ctrl_ops vsp1_rwpf_ctrl_ops
= {
280 .s_ctrl
= vsp1_rwpf_s_ctrl
,
283 int vsp1_rwpf_init_ctrls(struct vsp1_rwpf
*rwpf
)
287 v4l2_ctrl_handler_init(&rwpf
->ctrls
, 1);
288 v4l2_ctrl_new_std(&rwpf
->ctrls
, &vsp1_rwpf_ctrl_ops
,
289 V4L2_CID_ALPHA_COMPONENT
, 0, 255, 1, 255);
291 rwpf
->entity
.subdev
.ctrl_handler
= &rwpf
->ctrls
;
293 return rwpf
->ctrls
.error
;