Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 1 | /* |
| 2 | * vsp1_rwpf.c -- R-Car VSP1 Read and Write Pixel Formatters |
| 3 | * |
Laurent Pinchart | 8a1edc5 | 2014-02-06 14:42:31 -0300 | [diff] [blame] | 4 | * Copyright (C) 2013-2014 Renesas Electronics Corporation |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 5 | * |
| 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 | |
| 27 | int vsp1_rwpf_enum_mbus_code(struct v4l2_subdev *subdev, |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 28 | struct v4l2_subdev_pad_config *cfg, |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 29 | struct v4l2_subdev_mbus_code_enum *code) |
| 30 | { |
| 31 | static const unsigned int codes[] = { |
Boris BREZILLON | 27ffaeb | 2014-11-10 14:28:31 -0300 | [diff] [blame] | 32 | MEDIA_BUS_FMT_ARGB8888_1X32, |
| 33 | MEDIA_BUS_FMT_AYUV8_1X32, |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | if (code->index >= ARRAY_SIZE(codes)) |
| 37 | return -EINVAL; |
| 38 | |
| 39 | code->code = codes[code->index]; |
| 40 | |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | int vsp1_rwpf_enum_frame_size(struct v4l2_subdev *subdev, |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 45 | struct v4l2_subdev_pad_config *cfg, |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 46 | struct v4l2_subdev_frame_size_enum *fse) |
| 47 | { |
| 48 | struct vsp1_rwpf *rwpf = to_rwpf(subdev); |
| 49 | struct v4l2_mbus_framefmt *format; |
| 50 | |
Hans Verkuil | 5778e74 | 2015-03-04 01:47:58 -0800 | [diff] [blame] | 51 | format = vsp1_entity_get_pad_format(&rwpf->entity, cfg, fse->pad, |
| 52 | fse->which); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 53 | |
| 54 | if (fse->index || fse->code != format->code) |
| 55 | return -EINVAL; |
| 56 | |
| 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; |
| 62 | } else { |
| 63 | /* The size on the source pad are fixed and always identical to |
| 64 | * the size on the sink pad. |
| 65 | */ |
| 66 | fse->min_width = format->width; |
| 67 | fse->max_width = format->width; |
| 68 | fse->min_height = format->height; |
| 69 | fse->max_height = format->height; |
| 70 | } |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
Laurent Pinchart | e5ad37b | 2013-08-24 20:49:58 -0300 | [diff] [blame] | 75 | static struct v4l2_rect * |
Laurent Pinchart | 1bd0a1b | 2015-11-01 15:18:32 -0200 | [diff] [blame] | 76 | vsp1_rwpf_get_crop(struct vsp1_rwpf *rwpf, struct v4l2_subdev_pad_config *cfg, |
| 77 | u32 which) |
Laurent Pinchart | e5ad37b | 2013-08-24 20:49:58 -0300 | [diff] [blame] | 78 | { |
| 79 | switch (which) { |
| 80 | case V4L2_SUBDEV_FORMAT_TRY: |
Laurent Pinchart | 1bd0a1b | 2015-11-01 15:18:32 -0200 | [diff] [blame] | 81 | return v4l2_subdev_get_try_crop(&rwpf->entity.subdev, cfg, |
| 82 | RWPF_PAD_SINK); |
Laurent Pinchart | e5ad37b | 2013-08-24 20:49:58 -0300 | [diff] [blame] | 83 | case V4L2_SUBDEV_FORMAT_ACTIVE: |
| 84 | return &rwpf->crop; |
| 85 | default: |
| 86 | return NULL; |
| 87 | } |
| 88 | } |
| 89 | |
Laurent Pinchart | 1bd0a1b | 2015-11-01 15:18:32 -0200 | [diff] [blame] | 90 | int vsp1_rwpf_get_format(struct v4l2_subdev *subdev, |
| 91 | struct v4l2_subdev_pad_config *cfg, |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 92 | struct v4l2_subdev_format *fmt) |
| 93 | { |
| 94 | struct vsp1_rwpf *rwpf = to_rwpf(subdev); |
| 95 | |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 96 | fmt->format = *vsp1_entity_get_pad_format(&rwpf->entity, cfg, fmt->pad, |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 97 | fmt->which); |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |
Laurent Pinchart | 1bd0a1b | 2015-11-01 15:18:32 -0200 | [diff] [blame] | 102 | int vsp1_rwpf_set_format(struct v4l2_subdev *subdev, |
| 103 | struct v4l2_subdev_pad_config *cfg, |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 104 | struct v4l2_subdev_format *fmt) |
| 105 | { |
| 106 | struct vsp1_rwpf *rwpf = to_rwpf(subdev); |
| 107 | struct v4l2_mbus_framefmt *format; |
Laurent Pinchart | e5ad37b | 2013-08-24 20:49:58 -0300 | [diff] [blame] | 108 | struct v4l2_rect *crop; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 109 | |
| 110 | /* Default to YUV if the requested format is not supported. */ |
Boris BREZILLON | 27ffaeb | 2014-11-10 14:28:31 -0300 | [diff] [blame] | 111 | if (fmt->format.code != MEDIA_BUS_FMT_ARGB8888_1X32 && |
| 112 | fmt->format.code != MEDIA_BUS_FMT_AYUV8_1X32) |
| 113 | fmt->format.code = MEDIA_BUS_FMT_AYUV8_1X32; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 114 | |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 115 | format = vsp1_entity_get_pad_format(&rwpf->entity, cfg, fmt->pad, |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 116 | fmt->which); |
| 117 | |
| 118 | if (fmt->pad == RWPF_PAD_SOURCE) { |
| 119 | /* The RWPF performs format conversion but can't scale, only the |
| 120 | * format code can be changed on the source pad. |
| 121 | */ |
| 122 | format->code = fmt->format.code; |
| 123 | fmt->format = *format; |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | format->code = fmt->format.code; |
| 128 | format->width = clamp_t(unsigned int, fmt->format.width, |
| 129 | RWPF_MIN_WIDTH, rwpf->max_width); |
| 130 | format->height = clamp_t(unsigned int, fmt->format.height, |
| 131 | RWPF_MIN_HEIGHT, rwpf->max_height); |
| 132 | format->field = V4L2_FIELD_NONE; |
| 133 | format->colorspace = V4L2_COLORSPACE_SRGB; |
| 134 | |
| 135 | fmt->format = *format; |
| 136 | |
Laurent Pinchart | e5ad37b | 2013-08-24 20:49:58 -0300 | [diff] [blame] | 137 | /* Update the sink crop rectangle. */ |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 138 | crop = vsp1_rwpf_get_crop(rwpf, cfg, fmt->which); |
Laurent Pinchart | e5ad37b | 2013-08-24 20:49:58 -0300 | [diff] [blame] | 139 | crop->left = 0; |
| 140 | crop->top = 0; |
| 141 | crop->width = fmt->format.width; |
| 142 | crop->height = fmt->format.height; |
| 143 | |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 144 | /* Propagate the format to the source pad. */ |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 145 | format = vsp1_entity_get_pad_format(&rwpf->entity, cfg, RWPF_PAD_SOURCE, |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 146 | fmt->which); |
| 147 | *format = fmt->format; |
| 148 | |
| 149 | return 0; |
| 150 | } |
Laurent Pinchart | e5ad37b | 2013-08-24 20:49:58 -0300 | [diff] [blame] | 151 | |
| 152 | int vsp1_rwpf_get_selection(struct v4l2_subdev *subdev, |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 153 | struct v4l2_subdev_pad_config *cfg, |
Laurent Pinchart | e5ad37b | 2013-08-24 20:49:58 -0300 | [diff] [blame] | 154 | struct v4l2_subdev_selection *sel) |
| 155 | { |
| 156 | struct vsp1_rwpf *rwpf = to_rwpf(subdev); |
| 157 | struct v4l2_mbus_framefmt *format; |
| 158 | |
| 159 | /* Cropping is implemented on the sink pad. */ |
| 160 | if (sel->pad != RWPF_PAD_SINK) |
| 161 | return -EINVAL; |
| 162 | |
| 163 | switch (sel->target) { |
| 164 | case V4L2_SEL_TGT_CROP: |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 165 | sel->r = *vsp1_rwpf_get_crop(rwpf, cfg, sel->which); |
Laurent Pinchart | e5ad37b | 2013-08-24 20:49:58 -0300 | [diff] [blame] | 166 | break; |
| 167 | |
| 168 | case V4L2_SEL_TGT_CROP_BOUNDS: |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 169 | format = vsp1_entity_get_pad_format(&rwpf->entity, cfg, |
Laurent Pinchart | e5ad37b | 2013-08-24 20:49:58 -0300 | [diff] [blame] | 170 | RWPF_PAD_SINK, sel->which); |
| 171 | sel->r.left = 0; |
| 172 | sel->r.top = 0; |
| 173 | sel->r.width = format->width; |
| 174 | sel->r.height = format->height; |
| 175 | break; |
| 176 | |
| 177 | default: |
| 178 | return -EINVAL; |
| 179 | } |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | int vsp1_rwpf_set_selection(struct v4l2_subdev *subdev, |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 185 | struct v4l2_subdev_pad_config *cfg, |
Laurent Pinchart | e5ad37b | 2013-08-24 20:49:58 -0300 | [diff] [blame] | 186 | struct v4l2_subdev_selection *sel) |
| 187 | { |
| 188 | struct vsp1_rwpf *rwpf = to_rwpf(subdev); |
| 189 | struct v4l2_mbus_framefmt *format; |
| 190 | struct v4l2_rect *crop; |
| 191 | |
| 192 | /* Cropping is implemented on the sink pad. */ |
| 193 | if (sel->pad != RWPF_PAD_SINK) |
| 194 | return -EINVAL; |
| 195 | |
| 196 | if (sel->target != V4L2_SEL_TGT_CROP) |
| 197 | return -EINVAL; |
| 198 | |
| 199 | /* Make sure the crop rectangle is entirely contained in the image. The |
| 200 | * WPF top and left offsets are limited to 255. |
| 201 | */ |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 202 | format = vsp1_entity_get_pad_format(&rwpf->entity, cfg, RWPF_PAD_SINK, |
Laurent Pinchart | e5ad37b | 2013-08-24 20:49:58 -0300 | [diff] [blame] | 203 | sel->which); |
Damian Hobson-Garcia | 85a0638 | 2015-05-28 09:59:39 -0300 | [diff] [blame] | 204 | |
| 205 | /* Restrict the crop rectangle coordinates to multiples of 2 to avoid |
| 206 | * shifting the color plane. |
| 207 | */ |
| 208 | if (format->code == MEDIA_BUS_FMT_AYUV8_1X32) { |
| 209 | sel->r.left = ALIGN(sel->r.left, 2); |
| 210 | sel->r.top = ALIGN(sel->r.top, 2); |
| 211 | sel->r.width = round_down(sel->r.width, 2); |
| 212 | sel->r.height = round_down(sel->r.height, 2); |
| 213 | } |
| 214 | |
Laurent Pinchart | e5ad37b | 2013-08-24 20:49:58 -0300 | [diff] [blame] | 215 | sel->r.left = min_t(unsigned int, sel->r.left, format->width - 2); |
| 216 | sel->r.top = min_t(unsigned int, sel->r.top, format->height - 2); |
| 217 | if (rwpf->entity.type == VSP1_ENTITY_WPF) { |
| 218 | sel->r.left = min_t(unsigned int, sel->r.left, 255); |
| 219 | sel->r.top = min_t(unsigned int, sel->r.top, 255); |
| 220 | } |
| 221 | sel->r.width = min_t(unsigned int, sel->r.width, |
| 222 | format->width - sel->r.left); |
| 223 | sel->r.height = min_t(unsigned int, sel->r.height, |
| 224 | format->height - sel->r.top); |
| 225 | |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 226 | crop = vsp1_rwpf_get_crop(rwpf, cfg, sel->which); |
Laurent Pinchart | e5ad37b | 2013-08-24 20:49:58 -0300 | [diff] [blame] | 227 | *crop = sel->r; |
| 228 | |
| 229 | /* Propagate the format to the source pad. */ |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 230 | format = vsp1_entity_get_pad_format(&rwpf->entity, cfg, RWPF_PAD_SOURCE, |
Laurent Pinchart | e5ad37b | 2013-08-24 20:49:58 -0300 | [diff] [blame] | 231 | sel->which); |
| 232 | format->width = crop->width; |
| 233 | format->height = crop->height; |
| 234 | |
| 235 | return 0; |
| 236 | } |
Laurent Pinchart | bd2fdd5 | 2015-11-01 12:19:42 -0200 | [diff] [blame] | 237 | |
| 238 | /* ----------------------------------------------------------------------------- |
| 239 | * Controls |
| 240 | */ |
| 241 | |
| 242 | static int vsp1_rwpf_s_ctrl(struct v4l2_ctrl *ctrl) |
| 243 | { |
| 244 | struct vsp1_rwpf *rwpf = |
| 245 | container_of(ctrl->handler, struct vsp1_rwpf, ctrls); |
| 246 | |
| 247 | switch (ctrl->id) { |
| 248 | case V4L2_CID_ALPHA_COMPONENT: |
| 249 | rwpf->alpha = ctrl->val; |
| 250 | break; |
| 251 | } |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | static const struct v4l2_ctrl_ops vsp1_rwpf_ctrl_ops = { |
| 257 | .s_ctrl = vsp1_rwpf_s_ctrl, |
| 258 | }; |
| 259 | |
| 260 | int vsp1_rwpf_init_ctrls(struct vsp1_rwpf *rwpf) |
| 261 | { |
| 262 | rwpf->alpha = 255; |
| 263 | |
| 264 | v4l2_ctrl_handler_init(&rwpf->ctrls, 1); |
| 265 | v4l2_ctrl_new_std(&rwpf->ctrls, &vsp1_rwpf_ctrl_ops, |
| 266 | V4L2_CID_ALPHA_COMPONENT, 0, 255, 1, 255); |
| 267 | |
| 268 | rwpf->entity.subdev.ctrl_handler = &rwpf->ctrls; |
| 269 | |
| 270 | return rwpf->ctrls.error; |
| 271 | } |