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_AHSV8888_1X32
,
41 MEDIA_BUS_FMT_AYUV8_1X32
,
44 if (code
->index
>= ARRAY_SIZE(codes
))
47 code
->code
= codes
[code
->index
];
52 static int vsp1_rwpf_enum_frame_size(struct v4l2_subdev
*subdev
,
53 struct v4l2_subdev_pad_config
*cfg
,
54 struct v4l2_subdev_frame_size_enum
*fse
)
56 struct vsp1_rwpf
*rwpf
= to_rwpf(subdev
);
58 return vsp1_subdev_enum_frame_size(subdev
, cfg
, fse
, RWPF_MIN_WIDTH
,
59 RWPF_MIN_HEIGHT
, rwpf
->max_width
,
63 static int vsp1_rwpf_set_format(struct v4l2_subdev
*subdev
,
64 struct v4l2_subdev_pad_config
*cfg
,
65 struct v4l2_subdev_format
*fmt
)
67 struct vsp1_rwpf
*rwpf
= to_rwpf(subdev
);
68 struct v4l2_subdev_pad_config
*config
;
69 struct v4l2_mbus_framefmt
*format
;
72 mutex_lock(&rwpf
->entity
.lock
);
74 config
= vsp1_entity_get_pad_config(&rwpf
->entity
, cfg
, fmt
->which
);
80 /* Default to YUV if the requested format is not supported. */
81 if (fmt
->format
.code
!= MEDIA_BUS_FMT_ARGB8888_1X32
&&
82 fmt
->format
.code
!= MEDIA_BUS_FMT_AHSV8888_1X32
&&
83 fmt
->format
.code
!= MEDIA_BUS_FMT_AYUV8_1X32
)
84 fmt
->format
.code
= MEDIA_BUS_FMT_AYUV8_1X32
;
86 format
= vsp1_entity_get_pad_format(&rwpf
->entity
, config
, fmt
->pad
);
88 if (fmt
->pad
== RWPF_PAD_SOURCE
) {
89 /* The RWPF performs format conversion but can't scale, only the
90 * format code can be changed on the source pad.
92 format
->code
= fmt
->format
.code
;
93 fmt
->format
= *format
;
97 format
->code
= fmt
->format
.code
;
98 format
->width
= clamp_t(unsigned int, fmt
->format
.width
,
99 RWPF_MIN_WIDTH
, rwpf
->max_width
);
100 format
->height
= clamp_t(unsigned int, fmt
->format
.height
,
101 RWPF_MIN_HEIGHT
, rwpf
->max_height
);
102 format
->field
= V4L2_FIELD_NONE
;
103 format
->colorspace
= V4L2_COLORSPACE_SRGB
;
105 fmt
->format
= *format
;
107 if (rwpf
->entity
.type
== VSP1_ENTITY_RPF
) {
108 struct v4l2_rect
*crop
;
110 /* Update the sink crop rectangle. */
111 crop
= vsp1_rwpf_get_crop(rwpf
, config
);
114 crop
->width
= fmt
->format
.width
;
115 crop
->height
= fmt
->format
.height
;
118 /* Propagate the format to the source pad. */
119 format
= vsp1_entity_get_pad_format(&rwpf
->entity
, config
,
121 *format
= fmt
->format
;
124 mutex_unlock(&rwpf
->entity
.lock
);
128 static int vsp1_rwpf_get_selection(struct v4l2_subdev
*subdev
,
129 struct v4l2_subdev_pad_config
*cfg
,
130 struct v4l2_subdev_selection
*sel
)
132 struct vsp1_rwpf
*rwpf
= to_rwpf(subdev
);
133 struct v4l2_subdev_pad_config
*config
;
134 struct v4l2_mbus_framefmt
*format
;
138 * Cropping is only supported on the RPF and is implemented on the sink
141 if (rwpf
->entity
.type
== VSP1_ENTITY_WPF
|| sel
->pad
!= RWPF_PAD_SINK
)
144 mutex_lock(&rwpf
->entity
.lock
);
146 config
= vsp1_entity_get_pad_config(&rwpf
->entity
, cfg
, sel
->which
);
152 switch (sel
->target
) {
153 case V4L2_SEL_TGT_CROP
:
154 sel
->r
= *vsp1_rwpf_get_crop(rwpf
, config
);
157 case V4L2_SEL_TGT_CROP_BOUNDS
:
158 format
= vsp1_entity_get_pad_format(&rwpf
->entity
, config
,
162 sel
->r
.width
= format
->width
;
163 sel
->r
.height
= format
->height
;
172 mutex_unlock(&rwpf
->entity
.lock
);
176 static int vsp1_rwpf_set_selection(struct v4l2_subdev
*subdev
,
177 struct v4l2_subdev_pad_config
*cfg
,
178 struct v4l2_subdev_selection
*sel
)
180 struct vsp1_rwpf
*rwpf
= to_rwpf(subdev
);
181 struct v4l2_subdev_pad_config
*config
;
182 struct v4l2_mbus_framefmt
*format
;
183 struct v4l2_rect
*crop
;
187 * Cropping is only supported on the RPF and is implemented on the sink
190 if (rwpf
->entity
.type
== VSP1_ENTITY_WPF
|| sel
->pad
!= RWPF_PAD_SINK
)
193 if (sel
->target
!= V4L2_SEL_TGT_CROP
)
196 mutex_lock(&rwpf
->entity
.lock
);
198 config
= vsp1_entity_get_pad_config(&rwpf
->entity
, cfg
, sel
->which
);
204 /* Make sure the crop rectangle is entirely contained in the image. */
205 format
= vsp1_entity_get_pad_format(&rwpf
->entity
, config
,
208 /* Restrict the crop rectangle coordinates to multiples of 2 to avoid
209 * shifting the color plane.
211 if (format
->code
== MEDIA_BUS_FMT_AYUV8_1X32
) {
212 sel
->r
.left
= ALIGN(sel
->r
.left
, 2);
213 sel
->r
.top
= ALIGN(sel
->r
.top
, 2);
214 sel
->r
.width
= round_down(sel
->r
.width
, 2);
215 sel
->r
.height
= round_down(sel
->r
.height
, 2);
218 sel
->r
.left
= min_t(unsigned int, sel
->r
.left
, format
->width
- 2);
219 sel
->r
.top
= min_t(unsigned int, sel
->r
.top
, format
->height
- 2);
220 sel
->r
.width
= min_t(unsigned int, sel
->r
.width
,
221 format
->width
- sel
->r
.left
);
222 sel
->r
.height
= min_t(unsigned int, sel
->r
.height
,
223 format
->height
- sel
->r
.top
);
225 crop
= vsp1_rwpf_get_crop(rwpf
, config
);
228 /* Propagate the format to the source pad. */
229 format
= vsp1_entity_get_pad_format(&rwpf
->entity
, config
,
231 format
->width
= crop
->width
;
232 format
->height
= crop
->height
;
235 mutex_unlock(&rwpf
->entity
.lock
);
239 const struct v4l2_subdev_pad_ops vsp1_rwpf_pad_ops
= {
240 .init_cfg
= vsp1_entity_init_cfg
,
241 .enum_mbus_code
= vsp1_rwpf_enum_mbus_code
,
242 .enum_frame_size
= vsp1_rwpf_enum_frame_size
,
243 .get_fmt
= vsp1_subdev_get_pad_format
,
244 .set_fmt
= vsp1_rwpf_set_format
,
245 .get_selection
= vsp1_rwpf_get_selection
,
246 .set_selection
= vsp1_rwpf_set_selection
,
249 /* -----------------------------------------------------------------------------
253 static int vsp1_rwpf_s_ctrl(struct v4l2_ctrl
*ctrl
)
255 struct vsp1_rwpf
*rwpf
=
256 container_of(ctrl
->handler
, struct vsp1_rwpf
, ctrls
);
259 case V4L2_CID_ALPHA_COMPONENT
:
260 rwpf
->alpha
= ctrl
->val
;
267 static const struct v4l2_ctrl_ops vsp1_rwpf_ctrl_ops
= {
268 .s_ctrl
= vsp1_rwpf_s_ctrl
,
271 int vsp1_rwpf_init_ctrls(struct vsp1_rwpf
*rwpf
, unsigned int ncontrols
)
273 v4l2_ctrl_handler_init(&rwpf
->ctrls
, ncontrols
+ 1);
274 v4l2_ctrl_new_std(&rwpf
->ctrls
, &vsp1_rwpf_ctrl_ops
,
275 V4L2_CID_ALPHA_COMPONENT
, 0, 255, 1, 255);
277 rwpf
->entity
.subdev
.ctrl_handler
= &rwpf
->ctrls
;
279 return rwpf
->ctrls
.error
;