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_mbus_framefmt
*format
;
51 format
= vsp1_entity_get_pad_format(&rwpf
->entity
, cfg
, fse
->pad
,
54 if (fse
->index
|| fse
->code
!= format
->code
)
57 if (fse
->pad
== RWPF_PAD_SINK
) {
58 fse
->min_width
= RWPF_MIN_WIDTH
;
59 fse
->max_width
= rwpf
->max_width
;
60 fse
->min_height
= RWPF_MIN_HEIGHT
;
61 fse
->max_height
= rwpf
->max_height
;
63 /* The size on the source pad are fixed and always identical to
64 * the size on the sink pad.
66 fse
->min_width
= format
->width
;
67 fse
->max_width
= format
->width
;
68 fse
->min_height
= format
->height
;
69 fse
->max_height
= format
->height
;
75 static struct v4l2_rect
*
76 vsp1_rwpf_get_crop(struct vsp1_rwpf
*rwpf
, struct v4l2_subdev_pad_config
*cfg
, u32 which
)
79 case V4L2_SUBDEV_FORMAT_TRY
:
80 return v4l2_subdev_get_try_crop(&rwpf
->entity
.subdev
, cfg
, RWPF_PAD_SINK
);
81 case V4L2_SUBDEV_FORMAT_ACTIVE
:
88 int vsp1_rwpf_get_format(struct v4l2_subdev
*subdev
, struct v4l2_subdev_pad_config
*cfg
,
89 struct v4l2_subdev_format
*fmt
)
91 struct vsp1_rwpf
*rwpf
= to_rwpf(subdev
);
93 fmt
->format
= *vsp1_entity_get_pad_format(&rwpf
->entity
, cfg
, fmt
->pad
,
99 int vsp1_rwpf_set_format(struct v4l2_subdev
*subdev
, struct v4l2_subdev_pad_config
*cfg
,
100 struct v4l2_subdev_format
*fmt
)
102 struct vsp1_rwpf
*rwpf
= to_rwpf(subdev
);
103 struct v4l2_mbus_framefmt
*format
;
104 struct v4l2_rect
*crop
;
106 /* Default to YUV if the requested format is not supported. */
107 if (fmt
->format
.code
!= MEDIA_BUS_FMT_ARGB8888_1X32
&&
108 fmt
->format
.code
!= MEDIA_BUS_FMT_AYUV8_1X32
)
109 fmt
->format
.code
= MEDIA_BUS_FMT_AYUV8_1X32
;
111 format
= vsp1_entity_get_pad_format(&rwpf
->entity
, cfg
, fmt
->pad
,
114 if (fmt
->pad
== RWPF_PAD_SOURCE
) {
115 /* The RWPF performs format conversion but can't scale, only the
116 * format code can be changed on the source pad.
118 format
->code
= fmt
->format
.code
;
119 fmt
->format
= *format
;
123 format
->code
= fmt
->format
.code
;
124 format
->width
= clamp_t(unsigned int, fmt
->format
.width
,
125 RWPF_MIN_WIDTH
, rwpf
->max_width
);
126 format
->height
= clamp_t(unsigned int, fmt
->format
.height
,
127 RWPF_MIN_HEIGHT
, rwpf
->max_height
);
128 format
->field
= V4L2_FIELD_NONE
;
129 format
->colorspace
= V4L2_COLORSPACE_SRGB
;
131 fmt
->format
= *format
;
133 /* Update the sink crop rectangle. */
134 crop
= vsp1_rwpf_get_crop(rwpf
, cfg
, fmt
->which
);
137 crop
->width
= fmt
->format
.width
;
138 crop
->height
= fmt
->format
.height
;
140 /* Propagate the format to the source pad. */
141 format
= vsp1_entity_get_pad_format(&rwpf
->entity
, cfg
, RWPF_PAD_SOURCE
,
143 *format
= fmt
->format
;
148 int vsp1_rwpf_get_selection(struct v4l2_subdev
*subdev
,
149 struct v4l2_subdev_pad_config
*cfg
,
150 struct v4l2_subdev_selection
*sel
)
152 struct vsp1_rwpf
*rwpf
= to_rwpf(subdev
);
153 struct v4l2_mbus_framefmt
*format
;
155 /* Cropping is implemented on the sink pad. */
156 if (sel
->pad
!= RWPF_PAD_SINK
)
159 switch (sel
->target
) {
160 case V4L2_SEL_TGT_CROP
:
161 sel
->r
= *vsp1_rwpf_get_crop(rwpf
, cfg
, sel
->which
);
164 case V4L2_SEL_TGT_CROP_BOUNDS
:
165 format
= vsp1_entity_get_pad_format(&rwpf
->entity
, cfg
,
166 RWPF_PAD_SINK
, sel
->which
);
169 sel
->r
.width
= format
->width
;
170 sel
->r
.height
= format
->height
;
180 int vsp1_rwpf_set_selection(struct v4l2_subdev
*subdev
,
181 struct v4l2_subdev_pad_config
*cfg
,
182 struct v4l2_subdev_selection
*sel
)
184 struct vsp1_rwpf
*rwpf
= to_rwpf(subdev
);
185 struct v4l2_mbus_framefmt
*format
;
186 struct v4l2_rect
*crop
;
188 /* Cropping is implemented on the sink pad. */
189 if (sel
->pad
!= RWPF_PAD_SINK
)
192 if (sel
->target
!= V4L2_SEL_TGT_CROP
)
195 /* Make sure the crop rectangle is entirely contained in the image. The
196 * WPF top and left offsets are limited to 255.
198 format
= vsp1_entity_get_pad_format(&rwpf
->entity
, cfg
, RWPF_PAD_SINK
,
201 /* Restrict the crop rectangle coordinates to multiples of 2 to avoid
202 * shifting the color plane.
204 if (format
->code
== MEDIA_BUS_FMT_AYUV8_1X32
) {
205 sel
->r
.left
= ALIGN(sel
->r
.left
, 2);
206 sel
->r
.top
= ALIGN(sel
->r
.top
, 2);
207 sel
->r
.width
= round_down(sel
->r
.width
, 2);
208 sel
->r
.height
= round_down(sel
->r
.height
, 2);
211 sel
->r
.left
= min_t(unsigned int, sel
->r
.left
, format
->width
- 2);
212 sel
->r
.top
= min_t(unsigned int, sel
->r
.top
, format
->height
- 2);
213 if (rwpf
->entity
.type
== VSP1_ENTITY_WPF
) {
214 sel
->r
.left
= min_t(unsigned int, sel
->r
.left
, 255);
215 sel
->r
.top
= min_t(unsigned int, sel
->r
.top
, 255);
217 sel
->r
.width
= min_t(unsigned int, sel
->r
.width
,
218 format
->width
- sel
->r
.left
);
219 sel
->r
.height
= min_t(unsigned int, sel
->r
.height
,
220 format
->height
- sel
->r
.top
);
222 crop
= vsp1_rwpf_get_crop(rwpf
, cfg
, sel
->which
);
225 /* Propagate the format to the source pad. */
226 format
= vsp1_entity_get_pad_format(&rwpf
->entity
, cfg
, RWPF_PAD_SOURCE
,
228 format
->width
= crop
->width
;
229 format
->height
= crop
->height
;