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 | |
Laurent Pinchart | c6c8efb | 2015-11-22 13:37:45 -0200 | [diff] [blame] | 23 | struct v4l2_rect *vsp1_rwpf_get_crop(struct vsp1_rwpf *rwpf, |
| 24 | struct v4l2_subdev_pad_config *config) |
| 25 | { |
| 26 | return v4l2_subdev_get_try_crop(&rwpf->entity.subdev, config, |
| 27 | RWPF_PAD_SINK); |
| 28 | } |
| 29 | |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 30 | /* ----------------------------------------------------------------------------- |
| 31 | * V4L2 Subdevice Pad Operations |
| 32 | */ |
| 33 | |
Laurent Pinchart | c6c8efb | 2015-11-22 13:37:45 -0200 | [diff] [blame] | 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) |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 37 | { |
| 38 | static const unsigned int codes[] = { |
Boris BREZILLON | 27ffaeb | 2014-11-10 14:28:31 -0300 | [diff] [blame] | 39 | MEDIA_BUS_FMT_ARGB8888_1X32, |
| 40 | MEDIA_BUS_FMT_AYUV8_1X32, |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | if (code->index >= ARRAY_SIZE(codes)) |
| 44 | return -EINVAL; |
| 45 | |
| 46 | code->code = codes[code->index]; |
| 47 | |
| 48 | return 0; |
| 49 | } |
| 50 | |
Laurent Pinchart | c6c8efb | 2015-11-22 13:37:45 -0200 | [diff] [blame] | 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) |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 54 | { |
| 55 | struct vsp1_rwpf *rwpf = to_rwpf(subdev); |
Laurent Pinchart | e790c3c | 2015-11-15 19:14:22 -0200 | [diff] [blame] | 56 | struct v4l2_subdev_pad_config *config; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 57 | struct v4l2_mbus_framefmt *format; |
| 58 | |
Laurent Pinchart | e790c3c | 2015-11-15 19:14:22 -0200 | [diff] [blame] | 59 | config = vsp1_entity_get_pad_config(&rwpf->entity, cfg, fse->which); |
| 60 | if (!config) |
| 61 | return -EINVAL; |
| 62 | |
| 63 | format = vsp1_entity_get_pad_format(&rwpf->entity, config, fse->pad); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 64 | |
| 65 | if (fse->index || fse->code != format->code) |
| 66 | return -EINVAL; |
| 67 | |
| 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; |
| 73 | } else { |
| 74 | /* The size on the source pad are fixed and always identical to |
| 75 | * the size on the sink pad. |
| 76 | */ |
| 77 | fse->min_width = format->width; |
| 78 | fse->max_width = format->width; |
| 79 | fse->min_height = format->height; |
| 80 | fse->max_height = format->height; |
| 81 | } |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
Laurent Pinchart | c6c8efb | 2015-11-22 13:37:45 -0200 | [diff] [blame] | 86 | static int vsp1_rwpf_set_format(struct v4l2_subdev *subdev, |
| 87 | struct v4l2_subdev_pad_config *cfg, |
| 88 | struct v4l2_subdev_format *fmt) |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 89 | { |
| 90 | struct vsp1_rwpf *rwpf = to_rwpf(subdev); |
Laurent Pinchart | e790c3c | 2015-11-15 19:14:22 -0200 | [diff] [blame] | 91 | struct v4l2_subdev_pad_config *config; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 92 | struct v4l2_mbus_framefmt *format; |
Laurent Pinchart | e5ad37b | 2013-08-24 20:49:58 -0300 | [diff] [blame] | 93 | struct v4l2_rect *crop; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 94 | |
Laurent Pinchart | e790c3c | 2015-11-15 19:14:22 -0200 | [diff] [blame] | 95 | config = vsp1_entity_get_pad_config(&rwpf->entity, cfg, fmt->which); |
| 96 | if (!config) |
| 97 | return -EINVAL; |
| 98 | |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 99 | /* Default to YUV if the requested format is not supported. */ |
Boris BREZILLON | 27ffaeb | 2014-11-10 14:28:31 -0300 | [diff] [blame] | 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; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 103 | |
Laurent Pinchart | e790c3c | 2015-11-15 19:14:22 -0200 | [diff] [blame] | 104 | format = vsp1_entity_get_pad_format(&rwpf->entity, config, fmt->pad); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 105 | |
| 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. |
| 109 | */ |
| 110 | format->code = fmt->format.code; |
| 111 | fmt->format = *format; |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 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; |
| 122 | |
| 123 | fmt->format = *format; |
| 124 | |
Laurent Pinchart | e5ad37b | 2013-08-24 20:49:58 -0300 | [diff] [blame] | 125 | /* Update the sink crop rectangle. */ |
Laurent Pinchart | b7e5107 | 2015-11-15 19:14:22 -0200 | [diff] [blame] | 126 | crop = vsp1_rwpf_get_crop(rwpf, config); |
Laurent Pinchart | e5ad37b | 2013-08-24 20:49:58 -0300 | [diff] [blame] | 127 | crop->left = 0; |
| 128 | crop->top = 0; |
| 129 | crop->width = fmt->format.width; |
| 130 | crop->height = fmt->format.height; |
| 131 | |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 132 | /* Propagate the format to the source pad. */ |
Laurent Pinchart | e790c3c | 2015-11-15 19:14:22 -0200 | [diff] [blame] | 133 | format = vsp1_entity_get_pad_format(&rwpf->entity, config, |
| 134 | RWPF_PAD_SOURCE); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 135 | *format = fmt->format; |
| 136 | |
| 137 | return 0; |
| 138 | } |
Laurent Pinchart | e5ad37b | 2013-08-24 20:49:58 -0300 | [diff] [blame] | 139 | |
Laurent Pinchart | c6c8efb | 2015-11-22 13:37:45 -0200 | [diff] [blame] | 140 | static int vsp1_rwpf_get_selection(struct v4l2_subdev *subdev, |
| 141 | struct v4l2_subdev_pad_config *cfg, |
| 142 | struct v4l2_subdev_selection *sel) |
Laurent Pinchart | e5ad37b | 2013-08-24 20:49:58 -0300 | [diff] [blame] | 143 | { |
| 144 | struct vsp1_rwpf *rwpf = to_rwpf(subdev); |
Laurent Pinchart | e790c3c | 2015-11-15 19:14:22 -0200 | [diff] [blame] | 145 | struct v4l2_subdev_pad_config *config; |
Laurent Pinchart | e5ad37b | 2013-08-24 20:49:58 -0300 | [diff] [blame] | 146 | struct v4l2_mbus_framefmt *format; |
| 147 | |
| 148 | /* Cropping is implemented on the sink pad. */ |
| 149 | if (sel->pad != RWPF_PAD_SINK) |
| 150 | return -EINVAL; |
| 151 | |
Laurent Pinchart | e790c3c | 2015-11-15 19:14:22 -0200 | [diff] [blame] | 152 | config = vsp1_entity_get_pad_config(&rwpf->entity, cfg, sel->which); |
| 153 | if (!config) |
| 154 | return -EINVAL; |
| 155 | |
Laurent Pinchart | e5ad37b | 2013-08-24 20:49:58 -0300 | [diff] [blame] | 156 | switch (sel->target) { |
| 157 | case V4L2_SEL_TGT_CROP: |
Laurent Pinchart | b7e5107 | 2015-11-15 19:14:22 -0200 | [diff] [blame] | 158 | sel->r = *vsp1_rwpf_get_crop(rwpf, config); |
Laurent Pinchart | e5ad37b | 2013-08-24 20:49:58 -0300 | [diff] [blame] | 159 | break; |
| 160 | |
| 161 | case V4L2_SEL_TGT_CROP_BOUNDS: |
Laurent Pinchart | e790c3c | 2015-11-15 19:14:22 -0200 | [diff] [blame] | 162 | format = vsp1_entity_get_pad_format(&rwpf->entity, config, |
| 163 | RWPF_PAD_SINK); |
Laurent Pinchart | e5ad37b | 2013-08-24 20:49:58 -0300 | [diff] [blame] | 164 | sel->r.left = 0; |
| 165 | sel->r.top = 0; |
| 166 | sel->r.width = format->width; |
| 167 | sel->r.height = format->height; |
| 168 | break; |
| 169 | |
| 170 | default: |
| 171 | return -EINVAL; |
| 172 | } |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
Laurent Pinchart | c6c8efb | 2015-11-22 13:37:45 -0200 | [diff] [blame] | 177 | static int vsp1_rwpf_set_selection(struct v4l2_subdev *subdev, |
| 178 | struct v4l2_subdev_pad_config *cfg, |
| 179 | struct v4l2_subdev_selection *sel) |
Laurent Pinchart | e5ad37b | 2013-08-24 20:49:58 -0300 | [diff] [blame] | 180 | { |
| 181 | struct vsp1_rwpf *rwpf = to_rwpf(subdev); |
Laurent Pinchart | e790c3c | 2015-11-15 19:14:22 -0200 | [diff] [blame] | 182 | struct v4l2_subdev_pad_config *config; |
Laurent Pinchart | e5ad37b | 2013-08-24 20:49:58 -0300 | [diff] [blame] | 183 | struct v4l2_mbus_framefmt *format; |
| 184 | struct v4l2_rect *crop; |
| 185 | |
| 186 | /* Cropping is implemented on the sink pad. */ |
| 187 | if (sel->pad != RWPF_PAD_SINK) |
| 188 | return -EINVAL; |
| 189 | |
| 190 | if (sel->target != V4L2_SEL_TGT_CROP) |
| 191 | return -EINVAL; |
| 192 | |
Laurent Pinchart | e790c3c | 2015-11-15 19:14:22 -0200 | [diff] [blame] | 193 | config = vsp1_entity_get_pad_config(&rwpf->entity, cfg, sel->which); |
| 194 | if (!config) |
| 195 | return -EINVAL; |
| 196 | |
Laurent Pinchart | e5ad37b | 2013-08-24 20:49:58 -0300 | [diff] [blame] | 197 | /* Make sure the crop rectangle is entirely contained in the image. The |
| 198 | * WPF top and left offsets are limited to 255. |
| 199 | */ |
Laurent Pinchart | e790c3c | 2015-11-15 19:14:22 -0200 | [diff] [blame] | 200 | format = vsp1_entity_get_pad_format(&rwpf->entity, config, |
| 201 | RWPF_PAD_SINK); |
Damian Hobson-Garcia | 85a0638 | 2015-05-28 09:59:39 -0300 | [diff] [blame] | 202 | |
| 203 | /* Restrict the crop rectangle coordinates to multiples of 2 to avoid |
| 204 | * shifting the color plane. |
| 205 | */ |
| 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); |
| 211 | } |
| 212 | |
Laurent Pinchart | e5ad37b | 2013-08-24 20:49:58 -0300 | [diff] [blame] | 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); |
| 218 | } |
| 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); |
| 223 | |
Laurent Pinchart | b7e5107 | 2015-11-15 19:14:22 -0200 | [diff] [blame] | 224 | crop = vsp1_rwpf_get_crop(rwpf, config); |
Laurent Pinchart | e5ad37b | 2013-08-24 20:49:58 -0300 | [diff] [blame] | 225 | *crop = sel->r; |
| 226 | |
| 227 | /* Propagate the format to the source pad. */ |
Laurent Pinchart | e790c3c | 2015-11-15 19:14:22 -0200 | [diff] [blame] | 228 | format = vsp1_entity_get_pad_format(&rwpf->entity, config, |
| 229 | RWPF_PAD_SOURCE); |
Laurent Pinchart | e5ad37b | 2013-08-24 20:49:58 -0300 | [diff] [blame] | 230 | format->width = crop->width; |
| 231 | format->height = crop->height; |
| 232 | |
| 233 | return 0; |
| 234 | } |
Laurent Pinchart | bd2fdd5 | 2015-11-01 12:19:42 -0200 | [diff] [blame] | 235 | |
Laurent Pinchart | c6c8efb | 2015-11-22 13:37:45 -0200 | [diff] [blame] | 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, |
Laurent Pinchart | 3f55722 | 2016-02-24 21:10:13 -0300 | [diff] [blame^] | 240 | .get_fmt = vsp1_subdev_get_pad_format, |
Laurent Pinchart | c6c8efb | 2015-11-22 13:37:45 -0200 | [diff] [blame] | 241 | .set_fmt = vsp1_rwpf_set_format, |
| 242 | .get_selection = vsp1_rwpf_get_selection, |
| 243 | .set_selection = vsp1_rwpf_set_selection, |
| 244 | }; |
| 245 | |
Laurent Pinchart | bd2fdd5 | 2015-11-01 12:19:42 -0200 | [diff] [blame] | 246 | /* ----------------------------------------------------------------------------- |
| 247 | * Controls |
| 248 | */ |
| 249 | |
| 250 | static int vsp1_rwpf_s_ctrl(struct v4l2_ctrl *ctrl) |
| 251 | { |
| 252 | struct vsp1_rwpf *rwpf = |
| 253 | container_of(ctrl->handler, struct vsp1_rwpf, ctrls); |
| 254 | |
| 255 | switch (ctrl->id) { |
| 256 | case V4L2_CID_ALPHA_COMPONENT: |
| 257 | rwpf->alpha = ctrl->val; |
| 258 | break; |
| 259 | } |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | static const struct v4l2_ctrl_ops vsp1_rwpf_ctrl_ops = { |
| 265 | .s_ctrl = vsp1_rwpf_s_ctrl, |
| 266 | }; |
| 267 | |
| 268 | int vsp1_rwpf_init_ctrls(struct vsp1_rwpf *rwpf) |
| 269 | { |
| 270 | rwpf->alpha = 255; |
| 271 | |
| 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); |
| 275 | |
| 276 | rwpf->entity.subdev.ctrl_handler = &rwpf->ctrls; |
| 277 | |
| 278 | return rwpf->ctrls.error; |
| 279 | } |