blob: a083d85e84b4666d2a647ec8f6aa1dcad7dc10cd [file] [log] [blame]
Laurent Pinchart26e0ca22013-06-04 11:22:30 -03001/*
2 * vsp1_rwpf.c -- R-Car VSP1 Read and Write Pixel Formatters
3 *
Laurent Pinchart8a1edc52014-02-06 14:42:31 -03004 * Copyright (C) 2013-2014 Renesas Electronics Corporation
Laurent Pinchart26e0ca22013-06-04 11:22:30 -03005 *
6 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7 *
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.
12 */
13
14#include <media/v4l2-subdev.h>
15
16#include "vsp1.h"
17#include "vsp1_rwpf.h"
18#include "vsp1_video.h"
19
20#define RWPF_MIN_WIDTH 1
21#define RWPF_MIN_HEIGHT 1
22
23/* -----------------------------------------------------------------------------
24 * V4L2 Subdevice Pad Operations
25 */
26
27int vsp1_rwpf_enum_mbus_code(struct v4l2_subdev *subdev,
Hans Verkuilf7234132015-03-04 01:47:54 -080028 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030029 struct v4l2_subdev_mbus_code_enum *code)
30{
31 static const unsigned int codes[] = {
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030032 MEDIA_BUS_FMT_ARGB8888_1X32,
33 MEDIA_BUS_FMT_AYUV8_1X32,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030034 };
35
36 if (code->index >= ARRAY_SIZE(codes))
37 return -EINVAL;
38
39 code->code = codes[code->index];
40
41 return 0;
42}
43
44int vsp1_rwpf_enum_frame_size(struct v4l2_subdev *subdev,
Hans Verkuilf7234132015-03-04 01:47:54 -080045 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030046 struct v4l2_subdev_frame_size_enum *fse)
47{
48 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
49 struct v4l2_mbus_framefmt *format;
50
Hans Verkuilf7234132015-03-04 01:47:54 -080051 format = v4l2_subdev_get_try_format(subdev, cfg, fse->pad);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030052
53 if (fse->index || fse->code != format->code)
54 return -EINVAL;
55
56 if (fse->pad == RWPF_PAD_SINK) {
57 fse->min_width = RWPF_MIN_WIDTH;
58 fse->max_width = rwpf->max_width;
59 fse->min_height = RWPF_MIN_HEIGHT;
60 fse->max_height = rwpf->max_height;
61 } else {
62 /* The size on the source pad are fixed and always identical to
63 * the size on the sink pad.
64 */
65 fse->min_width = format->width;
66 fse->max_width = format->width;
67 fse->min_height = format->height;
68 fse->max_height = format->height;
69 }
70
71 return 0;
72}
73
Laurent Pincharte5ad37b2013-08-24 20:49:58 -030074static struct v4l2_rect *
Hans Verkuilf7234132015-03-04 01:47:54 -080075vsp1_rwpf_get_crop(struct vsp1_rwpf *rwpf, struct v4l2_subdev_pad_config *cfg, u32 which)
Laurent Pincharte5ad37b2013-08-24 20:49:58 -030076{
77 switch (which) {
78 case V4L2_SUBDEV_FORMAT_TRY:
Hans Verkuilf7234132015-03-04 01:47:54 -080079 return v4l2_subdev_get_try_crop(&rwpf->entity.subdev, cfg, RWPF_PAD_SINK);
Laurent Pincharte5ad37b2013-08-24 20:49:58 -030080 case V4L2_SUBDEV_FORMAT_ACTIVE:
81 return &rwpf->crop;
82 default:
83 return NULL;
84 }
85}
86
Hans Verkuilf7234132015-03-04 01:47:54 -080087int vsp1_rwpf_get_format(struct v4l2_subdev *subdev, struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030088 struct v4l2_subdev_format *fmt)
89{
90 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
91
Hans Verkuilf7234132015-03-04 01:47:54 -080092 fmt->format = *vsp1_entity_get_pad_format(&rwpf->entity, cfg, fmt->pad,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030093 fmt->which);
94
95 return 0;
96}
97
Hans Verkuilf7234132015-03-04 01:47:54 -080098int vsp1_rwpf_set_format(struct v4l2_subdev *subdev, struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030099 struct v4l2_subdev_format *fmt)
100{
101 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
102 struct v4l2_mbus_framefmt *format;
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300103 struct v4l2_rect *crop;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300104
105 /* Default to YUV if the requested format is not supported. */
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300106 if (fmt->format.code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
107 fmt->format.code != MEDIA_BUS_FMT_AYUV8_1X32)
108 fmt->format.code = MEDIA_BUS_FMT_AYUV8_1X32;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300109
Hans Verkuilf7234132015-03-04 01:47:54 -0800110 format = vsp1_entity_get_pad_format(&rwpf->entity, cfg, fmt->pad,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300111 fmt->which);
112
113 if (fmt->pad == RWPF_PAD_SOURCE) {
114 /* The RWPF performs format conversion but can't scale, only the
115 * format code can be changed on the source pad.
116 */
117 format->code = fmt->format.code;
118 fmt->format = *format;
119 return 0;
120 }
121
122 format->code = fmt->format.code;
123 format->width = clamp_t(unsigned int, fmt->format.width,
124 RWPF_MIN_WIDTH, rwpf->max_width);
125 format->height = clamp_t(unsigned int, fmt->format.height,
126 RWPF_MIN_HEIGHT, rwpf->max_height);
127 format->field = V4L2_FIELD_NONE;
128 format->colorspace = V4L2_COLORSPACE_SRGB;
129
130 fmt->format = *format;
131
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300132 /* Update the sink crop rectangle. */
Hans Verkuilf7234132015-03-04 01:47:54 -0800133 crop = vsp1_rwpf_get_crop(rwpf, cfg, fmt->which);
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300134 crop->left = 0;
135 crop->top = 0;
136 crop->width = fmt->format.width;
137 crop->height = fmt->format.height;
138
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300139 /* Propagate the format to the source pad. */
Hans Verkuilf7234132015-03-04 01:47:54 -0800140 format = vsp1_entity_get_pad_format(&rwpf->entity, cfg, RWPF_PAD_SOURCE,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300141 fmt->which);
142 *format = fmt->format;
143
144 return 0;
145}
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300146
147int vsp1_rwpf_get_selection(struct v4l2_subdev *subdev,
Hans Verkuilf7234132015-03-04 01:47:54 -0800148 struct v4l2_subdev_pad_config *cfg,
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300149 struct v4l2_subdev_selection *sel)
150{
151 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
152 struct v4l2_mbus_framefmt *format;
153
154 /* Cropping is implemented on the sink pad. */
155 if (sel->pad != RWPF_PAD_SINK)
156 return -EINVAL;
157
158 switch (sel->target) {
159 case V4L2_SEL_TGT_CROP:
Hans Verkuilf7234132015-03-04 01:47:54 -0800160 sel->r = *vsp1_rwpf_get_crop(rwpf, cfg, sel->which);
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300161 break;
162
163 case V4L2_SEL_TGT_CROP_BOUNDS:
Hans Verkuilf7234132015-03-04 01:47:54 -0800164 format = vsp1_entity_get_pad_format(&rwpf->entity, cfg,
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300165 RWPF_PAD_SINK, sel->which);
166 sel->r.left = 0;
167 sel->r.top = 0;
168 sel->r.width = format->width;
169 sel->r.height = format->height;
170 break;
171
172 default:
173 return -EINVAL;
174 }
175
176 return 0;
177}
178
179int vsp1_rwpf_set_selection(struct v4l2_subdev *subdev,
Hans Verkuilf7234132015-03-04 01:47:54 -0800180 struct v4l2_subdev_pad_config *cfg,
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300181 struct v4l2_subdev_selection *sel)
182{
183 struct vsp1_rwpf *rwpf = to_rwpf(subdev);
184 struct v4l2_mbus_framefmt *format;
185 struct v4l2_rect *crop;
186
187 /* Cropping is implemented on the sink pad. */
188 if (sel->pad != RWPF_PAD_SINK)
189 return -EINVAL;
190
191 if (sel->target != V4L2_SEL_TGT_CROP)
192 return -EINVAL;
193
194 /* Make sure the crop rectangle is entirely contained in the image. The
195 * WPF top and left offsets are limited to 255.
196 */
Hans Verkuilf7234132015-03-04 01:47:54 -0800197 format = vsp1_entity_get_pad_format(&rwpf->entity, cfg, RWPF_PAD_SINK,
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300198 sel->which);
199 sel->r.left = min_t(unsigned int, sel->r.left, format->width - 2);
200 sel->r.top = min_t(unsigned int, sel->r.top, format->height - 2);
201 if (rwpf->entity.type == VSP1_ENTITY_WPF) {
202 sel->r.left = min_t(unsigned int, sel->r.left, 255);
203 sel->r.top = min_t(unsigned int, sel->r.top, 255);
204 }
205 sel->r.width = min_t(unsigned int, sel->r.width,
206 format->width - sel->r.left);
207 sel->r.height = min_t(unsigned int, sel->r.height,
208 format->height - sel->r.top);
209
Hans Verkuilf7234132015-03-04 01:47:54 -0800210 crop = vsp1_rwpf_get_crop(rwpf, cfg, sel->which);
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300211 *crop = sel->r;
212
213 /* Propagate the format to the source pad. */
Hans Verkuilf7234132015-03-04 01:47:54 -0800214 format = vsp1_entity_get_pad_format(&rwpf->entity, cfg, RWPF_PAD_SOURCE,
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300215 sel->which);
216 format->width = crop->width;
217 format->height = crop->height;
218
219 return 0;
220}