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_set_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
;
92 struct v4l2_mbus_framefmt
*format
;
93 struct v4l2_rect
*crop
;
95 config
= vsp1_entity_get_pad_config(&rwpf
->entity
, cfg
, fmt
->which
);
99 /* Default to YUV if the requested format is not supported. */
100 if (fmt
->format
.code
!= MEDIA_BUS_FMT_ARGB8888_1X32
&&
101 fmt
->format
.code
!= MEDIA_BUS_FMT_AYUV8_1X32
)
102 fmt
->format
.code
= MEDIA_BUS_FMT_AYUV8_1X32
;
104 format
= vsp1_entity_get_pad_format(&rwpf
->entity
, config
, fmt
->pad
);
106 if (fmt
->pad
== RWPF_PAD_SOURCE
) {
107 /* The RWPF performs format conversion but can't scale, only the
108 * format code can be changed on the source pad.
110 format
->code
= fmt
->format
.code
;
111 fmt
->format
= *format
;
115 format
->code
= fmt
->format
.code
;
116 format
->width
= clamp_t(unsigned int, fmt
->format
.width
,
117 RWPF_MIN_WIDTH
, rwpf
->max_width
);
118 format
->height
= clamp_t(unsigned int, fmt
->format
.height
,
119 RWPF_MIN_HEIGHT
, rwpf
->max_height
);
120 format
->field
= V4L2_FIELD_NONE
;
121 format
->colorspace
= V4L2_COLORSPACE_SRGB
;
123 fmt
->format
= *format
;
125 /* Update the sink crop rectangle. */
126 crop
= vsp1_rwpf_get_crop(rwpf
, config
);
129 crop
->width
= fmt
->format
.width
;
130 crop
->height
= fmt
->format
.height
;
132 /* Propagate the format to the source pad. */
133 format
= vsp1_entity_get_pad_format(&rwpf
->entity
, config
,
135 *format
= fmt
->format
;
140 static int vsp1_rwpf_get_selection(struct v4l2_subdev
*subdev
,
141 struct v4l2_subdev_pad_config
*cfg
,
142 struct v4l2_subdev_selection
*sel
)
144 struct vsp1_rwpf
*rwpf
= to_rwpf(subdev
);
145 struct v4l2_subdev_pad_config
*config
;
146 struct v4l2_mbus_framefmt
*format
;
148 /* Cropping is implemented on the sink pad. */
149 if (sel
->pad
!= RWPF_PAD_SINK
)
152 config
= vsp1_entity_get_pad_config(&rwpf
->entity
, cfg
, sel
->which
);
156 switch (sel
->target
) {
157 case V4L2_SEL_TGT_CROP
:
158 sel
->r
= *vsp1_rwpf_get_crop(rwpf
, config
);
161 case V4L2_SEL_TGT_CROP_BOUNDS
:
162 format
= vsp1_entity_get_pad_format(&rwpf
->entity
, config
,
166 sel
->r
.width
= format
->width
;
167 sel
->r
.height
= format
->height
;
177 static int vsp1_rwpf_set_selection(struct v4l2_subdev
*subdev
,
178 struct v4l2_subdev_pad_config
*cfg
,
179 struct v4l2_subdev_selection
*sel
)
181 struct vsp1_rwpf
*rwpf
= to_rwpf(subdev
);
182 struct v4l2_subdev_pad_config
*config
;
183 struct v4l2_mbus_framefmt
*format
;
184 struct v4l2_rect
*crop
;
186 /* Cropping is implemented on the sink pad. */
187 if (sel
->pad
!= RWPF_PAD_SINK
)
190 if (sel
->target
!= V4L2_SEL_TGT_CROP
)
193 config
= vsp1_entity_get_pad_config(&rwpf
->entity
, cfg
, sel
->which
);
197 /* Make sure the crop rectangle is entirely contained in the image. The
198 * WPF top and left offsets are limited to 255.
200 format
= vsp1_entity_get_pad_format(&rwpf
->entity
, config
,
203 /* Restrict the crop rectangle coordinates to multiples of 2 to avoid
204 * shifting the color plane.
206 if (format
->code
== MEDIA_BUS_FMT_AYUV8_1X32
) {
207 sel
->r
.left
= ALIGN(sel
->r
.left
, 2);
208 sel
->r
.top
= ALIGN(sel
->r
.top
, 2);
209 sel
->r
.width
= round_down(sel
->r
.width
, 2);
210 sel
->r
.height
= round_down(sel
->r
.height
, 2);
213 sel
->r
.left
= min_t(unsigned int, sel
->r
.left
, format
->width
- 2);
214 sel
->r
.top
= min_t(unsigned int, sel
->r
.top
, format
->height
- 2);
215 if (rwpf
->entity
.type
== VSP1_ENTITY_WPF
) {
216 sel
->r
.left
= min_t(unsigned int, sel
->r
.left
, 255);
217 sel
->r
.top
= min_t(unsigned int, sel
->r
.top
, 255);
219 sel
->r
.width
= min_t(unsigned int, sel
->r
.width
,
220 format
->width
- sel
->r
.left
);
221 sel
->r
.height
= min_t(unsigned int, sel
->r
.height
,
222 format
->height
- sel
->r
.top
);
224 crop
= vsp1_rwpf_get_crop(rwpf
, config
);
227 /* Propagate the format to the source pad. */
228 format
= vsp1_entity_get_pad_format(&rwpf
->entity
, config
,
230 format
->width
= crop
->width
;
231 format
->height
= crop
->height
;
236 const struct v4l2_subdev_pad_ops vsp1_rwpf_pad_ops
= {
237 .init_cfg
= vsp1_entity_init_cfg
,
238 .enum_mbus_code
= vsp1_rwpf_enum_mbus_code
,
239 .enum_frame_size
= vsp1_rwpf_enum_frame_size
,
240 .get_fmt
= vsp1_subdev_get_pad_format
,
241 .set_fmt
= vsp1_rwpf_set_format
,
242 .get_selection
= vsp1_rwpf_get_selection
,
243 .set_selection
= vsp1_rwpf_set_selection
,
246 /* -----------------------------------------------------------------------------
250 static int vsp1_rwpf_s_ctrl(struct v4l2_ctrl
*ctrl
)
252 struct vsp1_rwpf
*rwpf
=
253 container_of(ctrl
->handler
, struct vsp1_rwpf
, ctrls
);
256 case V4L2_CID_ALPHA_COMPONENT
:
257 rwpf
->alpha
= ctrl
->val
;
264 static const struct v4l2_ctrl_ops vsp1_rwpf_ctrl_ops
= {
265 .s_ctrl
= vsp1_rwpf_s_ctrl
,
268 int vsp1_rwpf_init_ctrls(struct vsp1_rwpf
*rwpf
)
272 v4l2_ctrl_handler_init(&rwpf
->ctrls
, 1);
273 v4l2_ctrl_new_std(&rwpf
->ctrls
, &vsp1_rwpf_ctrl_ops
,
274 V4L2_CID_ALPHA_COMPONENT
, 0, 255, 1, 255);
276 rwpf
->entity
.subdev
.ctrl_handler
= &rwpf
->ctrls
;
278 return rwpf
->ctrls
.error
;