Laurent Pinchart | 1c4b5f4 | 2018-04-22 17:33:20 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 2 | /* |
| 3 | * vsp1_entity.c -- R-Car VSP1 Base Entity |
| 4 | * |
Laurent Pinchart | 8a1edc5 | 2014-02-06 14:42:31 -0300 | [diff] [blame] | 5 | * Copyright (C) 2013-2014 Renesas Electronics Corporation |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 6 | * |
| 7 | * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com) |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/device.h> |
| 11 | #include <linux/gfp.h> |
| 12 | |
| 13 | #include <media/media-entity.h> |
Laurent Pinchart | a626e64 | 2013-07-10 12:03:30 -0300 | [diff] [blame] | 14 | #include <media/v4l2-ctrls.h> |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 15 | #include <media/v4l2-subdev.h> |
| 16 | |
| 17 | #include "vsp1.h" |
Laurent Pinchart | aa380ea | 2015-11-01 10:46:25 -0200 | [diff] [blame] | 18 | #include "vsp1_dl.h" |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 19 | #include "vsp1_entity.h" |
Laurent Pinchart | c8663c8 | 2016-09-07 09:09:53 -0300 | [diff] [blame] | 20 | #include "vsp1_pipe.h" |
| 21 | #include "vsp1_rwpf.h" |
Laurent Pinchart | aa380ea | 2015-11-01 10:46:25 -0200 | [diff] [blame] | 22 | |
Laurent Pinchart | c8663c8 | 2016-09-07 09:09:53 -0300 | [diff] [blame] | 23 | void vsp1_entity_route_setup(struct vsp1_entity *entity, |
| 24 | struct vsp1_pipeline *pipe, |
Laurent Pinchart | 5e8dbbf | 2015-11-22 20:29:25 -0200 | [diff] [blame] | 25 | struct vsp1_dl_list *dl) |
Laurent Pinchart | 665b693 | 2015-08-02 18:58:31 -0300 | [diff] [blame] | 26 | { |
Laurent Pinchart | c8663c8 | 2016-09-07 09:09:53 -0300 | [diff] [blame] | 27 | struct vsp1_entity *source; |
Laurent Pinchart | 6134148 | 2017-05-25 00:16:57 +0300 | [diff] [blame] | 28 | u32 route; |
Laurent Pinchart | 665b693 | 2015-08-02 18:58:31 -0300 | [diff] [blame] | 29 | |
Laurent Pinchart | f242152 | 2016-02-24 20:40:22 -0300 | [diff] [blame] | 30 | if (entity->type == VSP1_ENTITY_HGO) { |
| 31 | u32 smppt; |
| 32 | |
| 33 | /* |
| 34 | * The HGO is a special case, its routing is configured on the |
| 35 | * sink pad. |
| 36 | */ |
Laurent Pinchart | 2275e8d | 2017-06-25 20:15:22 +0300 | [diff] [blame] | 37 | source = entity->sources[0]; |
Laurent Pinchart | f242152 | 2016-02-24 20:40:22 -0300 | [diff] [blame] | 38 | smppt = (pipe->output->entity.index << VI6_DPR_SMPPT_TGW_SHIFT) |
| 39 | | (source->route->output << VI6_DPR_SMPPT_PT_SHIFT); |
| 40 | |
| 41 | vsp1_dl_list_write(dl, VI6_DPR_HGO_SMPPT, smppt); |
| 42 | return; |
Niklas Söderlund | 0ac702d | 2016-09-06 11:38:56 -0300 | [diff] [blame] | 43 | } else if (entity->type == VSP1_ENTITY_HGT) { |
| 44 | u32 smppt; |
| 45 | |
| 46 | /* |
| 47 | * The HGT is a special case, its routing is configured on the |
| 48 | * sink pad. |
| 49 | */ |
Laurent Pinchart | 2275e8d | 2017-06-25 20:15:22 +0300 | [diff] [blame] | 50 | source = entity->sources[0]; |
Niklas Söderlund | 0ac702d | 2016-09-06 11:38:56 -0300 | [diff] [blame] | 51 | smppt = (pipe->output->entity.index << VI6_DPR_SMPPT_TGW_SHIFT) |
| 52 | | (source->route->output << VI6_DPR_SMPPT_PT_SHIFT); |
| 53 | |
| 54 | vsp1_dl_list_write(dl, VI6_DPR_HGT_SMPPT, smppt); |
| 55 | return; |
Laurent Pinchart | f242152 | 2016-02-24 20:40:22 -0300 | [diff] [blame] | 56 | } |
| 57 | |
Laurent Pinchart | c8663c8 | 2016-09-07 09:09:53 -0300 | [diff] [blame] | 58 | source = entity; |
Laurent Pinchart | 665b693 | 2015-08-02 18:58:31 -0300 | [diff] [blame] | 59 | if (source->route->reg == 0) |
| 60 | return; |
| 61 | |
Laurent Pinchart | 6134148 | 2017-05-25 00:16:57 +0300 | [diff] [blame] | 62 | route = source->sink->route->inputs[source->sink_pad]; |
| 63 | /* |
| 64 | * The ILV and BRS share the same data path route. The extra BRSSEL bit |
| 65 | * selects between the ILV and BRS. |
| 66 | */ |
| 67 | if (source->type == VSP1_ENTITY_BRS) |
| 68 | route |= VI6_DPR_ROUTE_BRSSEL; |
| 69 | vsp1_dl_list_write(dl, source->route->reg, route); |
Laurent Pinchart | 665b693 | 2015-08-02 18:58:31 -0300 | [diff] [blame] | 70 | } |
| 71 | |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 72 | /* ----------------------------------------------------------------------------- |
| 73 | * V4L2 Subdevice Operations |
| 74 | */ |
| 75 | |
Laurent Pinchart | e790c3c | 2015-11-15 19:14:22 -0200 | [diff] [blame] | 76 | /** |
| 77 | * vsp1_entity_get_pad_config - Get the pad configuration for an entity |
| 78 | * @entity: the entity |
| 79 | * @cfg: the TRY pad configuration |
| 80 | * @which: configuration selector (ACTIVE or TRY) |
| 81 | * |
Laurent Pinchart | 34e77ed | 2016-06-26 08:09:31 -0300 | [diff] [blame] | 82 | * When called with which set to V4L2_SUBDEV_FORMAT_ACTIVE the caller must hold |
| 83 | * the entity lock to access the returned configuration. |
| 84 | * |
Laurent Pinchart | e790c3c | 2015-11-15 19:14:22 -0200 | [diff] [blame] | 85 | * Return the pad configuration requested by the which argument. The TRY |
| 86 | * configuration is passed explicitly to the function through the cfg argument |
| 87 | * and simply returned when requested. The ACTIVE configuration comes from the |
| 88 | * entity structure. |
| 89 | */ |
| 90 | struct v4l2_subdev_pad_config * |
| 91 | vsp1_entity_get_pad_config(struct vsp1_entity *entity, |
| 92 | struct v4l2_subdev_pad_config *cfg, |
| 93 | enum v4l2_subdev_format_whence which) |
| 94 | { |
| 95 | switch (which) { |
| 96 | case V4L2_SUBDEV_FORMAT_ACTIVE: |
| 97 | return entity->config; |
| 98 | case V4L2_SUBDEV_FORMAT_TRY: |
| 99 | default: |
| 100 | return cfg; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * vsp1_entity_get_pad_format - Get a pad format from storage for an entity |
| 106 | * @entity: the entity |
| 107 | * @cfg: the configuration storage |
| 108 | * @pad: the pad number |
| 109 | * |
| 110 | * Return the format stored in the given configuration for an entity's pad. The |
| 111 | * configuration can be an ACTIVE or TRY configuration. |
| 112 | */ |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 113 | struct v4l2_mbus_framefmt * |
| 114 | vsp1_entity_get_pad_format(struct vsp1_entity *entity, |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 115 | struct v4l2_subdev_pad_config *cfg, |
Laurent Pinchart | e790c3c | 2015-11-15 19:14:22 -0200 | [diff] [blame] | 116 | unsigned int pad) |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 117 | { |
Laurent Pinchart | e790c3c | 2015-11-15 19:14:22 -0200 | [diff] [blame] | 118 | return v4l2_subdev_get_try_format(&entity->subdev, cfg, pad); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 119 | } |
| 120 | |
Laurent Pinchart | ccd3d95 | 2016-03-03 20:17:49 -0300 | [diff] [blame] | 121 | /** |
| 122 | * vsp1_entity_get_pad_selection - Get a pad selection from storage for entity |
| 123 | * @entity: the entity |
| 124 | * @cfg: the configuration storage |
| 125 | * @pad: the pad number |
| 126 | * @target: the selection target |
| 127 | * |
| 128 | * Return the selection rectangle stored in the given configuration for an |
| 129 | * entity's pad. The configuration can be an ACTIVE or TRY configuration. The |
| 130 | * selection target can be COMPOSE or CROP. |
| 131 | */ |
Laurent Pinchart | b7e5107 | 2015-11-15 19:14:22 -0200 | [diff] [blame] | 132 | struct v4l2_rect * |
Laurent Pinchart | ccd3d95 | 2016-03-03 20:17:49 -0300 | [diff] [blame] | 133 | vsp1_entity_get_pad_selection(struct vsp1_entity *entity, |
| 134 | struct v4l2_subdev_pad_config *cfg, |
| 135 | unsigned int pad, unsigned int target) |
Laurent Pinchart | b7e5107 | 2015-11-15 19:14:22 -0200 | [diff] [blame] | 136 | { |
Laurent Pinchart | ccd3d95 | 2016-03-03 20:17:49 -0300 | [diff] [blame] | 137 | switch (target) { |
| 138 | case V4L2_SEL_TGT_COMPOSE: |
| 139 | return v4l2_subdev_get_try_compose(&entity->subdev, cfg, pad); |
| 140 | case V4L2_SEL_TGT_CROP: |
| 141 | return v4l2_subdev_get_try_crop(&entity->subdev, cfg, pad); |
| 142 | default: |
| 143 | return NULL; |
| 144 | } |
Laurent Pinchart | b7e5107 | 2015-11-15 19:14:22 -0200 | [diff] [blame] | 145 | } |
| 146 | |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 147 | /* |
Laurent Pinchart | 0efdf0f | 2015-11-15 20:09:08 -0200 | [diff] [blame] | 148 | * vsp1_entity_init_cfg - Initialize formats on all pads |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 149 | * @subdev: V4L2 subdevice |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 150 | * @cfg: V4L2 subdev pad configuration |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 151 | * |
Laurent Pinchart | 0efdf0f | 2015-11-15 20:09:08 -0200 | [diff] [blame] | 152 | * Initialize all pad formats with default values in the given pad config. This |
| 153 | * function can be used as a handler for the subdev pad::init_cfg operation. |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 154 | */ |
Laurent Pinchart | 0efdf0f | 2015-11-15 20:09:08 -0200 | [diff] [blame] | 155 | int vsp1_entity_init_cfg(struct v4l2_subdev *subdev, |
| 156 | struct v4l2_subdev_pad_config *cfg) |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 157 | { |
| 158 | struct v4l2_subdev_format format; |
| 159 | unsigned int pad; |
| 160 | |
| 161 | for (pad = 0; pad < subdev->entity.num_pads - 1; ++pad) { |
| 162 | memset(&format, 0, sizeof(format)); |
| 163 | |
| 164 | format.pad = pad; |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 165 | format.which = cfg ? V4L2_SUBDEV_FORMAT_TRY |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 166 | : V4L2_SUBDEV_FORMAT_ACTIVE; |
| 167 | |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 168 | v4l2_subdev_call(subdev, pad, set_fmt, cfg, &format); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 169 | } |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
Laurent Pinchart | 3f55722 | 2016-02-24 21:10:13 -0300 | [diff] [blame] | 174 | /* |
| 175 | * vsp1_subdev_get_pad_format - Subdev pad get_fmt handler |
| 176 | * @subdev: V4L2 subdevice |
| 177 | * @cfg: V4L2 subdev pad configuration |
| 178 | * @fmt: V4L2 subdev format |
| 179 | * |
| 180 | * This function implements the subdev get_fmt pad operation. It can be used as |
| 181 | * a direct drop-in for the operation handler. |
| 182 | */ |
| 183 | int vsp1_subdev_get_pad_format(struct v4l2_subdev *subdev, |
| 184 | struct v4l2_subdev_pad_config *cfg, |
| 185 | struct v4l2_subdev_format *fmt) |
| 186 | { |
| 187 | struct vsp1_entity *entity = to_vsp1_entity(subdev); |
| 188 | struct v4l2_subdev_pad_config *config; |
| 189 | |
| 190 | config = vsp1_entity_get_pad_config(entity, cfg, fmt->which); |
| 191 | if (!config) |
| 192 | return -EINVAL; |
| 193 | |
Laurent Pinchart | 34e77ed | 2016-06-26 08:09:31 -0300 | [diff] [blame] | 194 | mutex_lock(&entity->lock); |
Laurent Pinchart | 3f55722 | 2016-02-24 21:10:13 -0300 | [diff] [blame] | 195 | fmt->format = *vsp1_entity_get_pad_format(entity, config, fmt->pad); |
Laurent Pinchart | 34e77ed | 2016-06-26 08:09:31 -0300 | [diff] [blame] | 196 | mutex_unlock(&entity->lock); |
Laurent Pinchart | 3f55722 | 2016-02-24 21:10:13 -0300 | [diff] [blame] | 197 | |
| 198 | return 0; |
| 199 | } |
| 200 | |
Laurent Pinchart | 6ad9ba9 | 2016-02-24 20:25:42 -0300 | [diff] [blame] | 201 | /* |
| 202 | * vsp1_subdev_enum_mbus_code - Subdev pad enum_mbus_code handler |
| 203 | * @subdev: V4L2 subdevice |
| 204 | * @cfg: V4L2 subdev pad configuration |
| 205 | * @code: Media bus code enumeration |
| 206 | * @codes: Array of supported media bus codes |
| 207 | * @ncodes: Number of supported media bus codes |
| 208 | * |
| 209 | * This function implements the subdev enum_mbus_code pad operation for entities |
| 210 | * that do not support format conversion. It enumerates the given supported |
| 211 | * media bus codes on the sink pad and reports a source pad format identical to |
| 212 | * the sink pad. |
| 213 | */ |
| 214 | int vsp1_subdev_enum_mbus_code(struct v4l2_subdev *subdev, |
| 215 | struct v4l2_subdev_pad_config *cfg, |
| 216 | struct v4l2_subdev_mbus_code_enum *code, |
| 217 | const unsigned int *codes, unsigned int ncodes) |
| 218 | { |
| 219 | struct vsp1_entity *entity = to_vsp1_entity(subdev); |
| 220 | |
| 221 | if (code->pad == 0) { |
| 222 | if (code->index >= ncodes) |
| 223 | return -EINVAL; |
| 224 | |
| 225 | code->code = codes[code->index]; |
| 226 | } else { |
| 227 | struct v4l2_subdev_pad_config *config; |
| 228 | struct v4l2_mbus_framefmt *format; |
| 229 | |
Laurent Pinchart | 9dbed95 | 2017-02-26 10:29:50 -0300 | [diff] [blame] | 230 | /* |
| 231 | * The entity can't perform format conversion, the sink format |
Laurent Pinchart | 6ad9ba9 | 2016-02-24 20:25:42 -0300 | [diff] [blame] | 232 | * is always identical to the source format. |
| 233 | */ |
| 234 | if (code->index) |
| 235 | return -EINVAL; |
| 236 | |
| 237 | config = vsp1_entity_get_pad_config(entity, cfg, code->which); |
| 238 | if (!config) |
| 239 | return -EINVAL; |
| 240 | |
Laurent Pinchart | 34e77ed | 2016-06-26 08:09:31 -0300 | [diff] [blame] | 241 | mutex_lock(&entity->lock); |
Laurent Pinchart | 6ad9ba9 | 2016-02-24 20:25:42 -0300 | [diff] [blame] | 242 | format = vsp1_entity_get_pad_format(entity, config, 0); |
| 243 | code->code = format->code; |
Laurent Pinchart | 34e77ed | 2016-06-26 08:09:31 -0300 | [diff] [blame] | 244 | mutex_unlock(&entity->lock); |
Laurent Pinchart | 6ad9ba9 | 2016-02-24 20:25:42 -0300 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | return 0; |
| 248 | } |
| 249 | |
Laurent Pinchart | 076e834 | 2016-02-24 20:25:42 -0300 | [diff] [blame] | 250 | /* |
| 251 | * vsp1_subdev_enum_frame_size - Subdev pad enum_frame_size handler |
| 252 | * @subdev: V4L2 subdevice |
| 253 | * @cfg: V4L2 subdev pad configuration |
| 254 | * @fse: Frame size enumeration |
| 255 | * @min_width: Minimum image width |
| 256 | * @min_height: Minimum image height |
| 257 | * @max_width: Maximum image width |
| 258 | * @max_height: Maximum image height |
| 259 | * |
| 260 | * This function implements the subdev enum_frame_size pad operation for |
| 261 | * entities that do not support scaling or cropping. It reports the given |
| 262 | * minimum and maximum frame width and height on the sink pad, and a fixed |
| 263 | * source pad size identical to the sink pad. |
| 264 | */ |
| 265 | int vsp1_subdev_enum_frame_size(struct v4l2_subdev *subdev, |
| 266 | struct v4l2_subdev_pad_config *cfg, |
| 267 | struct v4l2_subdev_frame_size_enum *fse, |
| 268 | unsigned int min_width, unsigned int min_height, |
| 269 | unsigned int max_width, unsigned int max_height) |
| 270 | { |
| 271 | struct vsp1_entity *entity = to_vsp1_entity(subdev); |
| 272 | struct v4l2_subdev_pad_config *config; |
| 273 | struct v4l2_mbus_framefmt *format; |
Laurent Pinchart | 34e77ed | 2016-06-26 08:09:31 -0300 | [diff] [blame] | 274 | int ret = 0; |
Laurent Pinchart | 076e834 | 2016-02-24 20:25:42 -0300 | [diff] [blame] | 275 | |
| 276 | config = vsp1_entity_get_pad_config(entity, cfg, fse->which); |
| 277 | if (!config) |
| 278 | return -EINVAL; |
| 279 | |
| 280 | format = vsp1_entity_get_pad_format(entity, config, fse->pad); |
| 281 | |
Laurent Pinchart | 34e77ed | 2016-06-26 08:09:31 -0300 | [diff] [blame] | 282 | mutex_lock(&entity->lock); |
| 283 | |
| 284 | if (fse->index || fse->code != format->code) { |
| 285 | ret = -EINVAL; |
| 286 | goto done; |
| 287 | } |
Laurent Pinchart | 076e834 | 2016-02-24 20:25:42 -0300 | [diff] [blame] | 288 | |
| 289 | if (fse->pad == 0) { |
| 290 | fse->min_width = min_width; |
| 291 | fse->max_width = max_width; |
| 292 | fse->min_height = min_height; |
| 293 | fse->max_height = max_height; |
| 294 | } else { |
Laurent Pinchart | 9dbed95 | 2017-02-26 10:29:50 -0300 | [diff] [blame] | 295 | /* |
| 296 | * The size on the source pad are fixed and always identical to |
Laurent Pinchart | 076e834 | 2016-02-24 20:25:42 -0300 | [diff] [blame] | 297 | * the size on the sink pad. |
| 298 | */ |
| 299 | fse->min_width = format->width; |
| 300 | fse->max_width = format->width; |
| 301 | fse->min_height = format->height; |
| 302 | fse->max_height = format->height; |
| 303 | } |
| 304 | |
Laurent Pinchart | 34e77ed | 2016-06-26 08:09:31 -0300 | [diff] [blame] | 305 | done: |
| 306 | mutex_unlock(&entity->lock); |
| 307 | return ret; |
Laurent Pinchart | 076e834 | 2016-02-24 20:25:42 -0300 | [diff] [blame] | 308 | } |
| 309 | |
Laurent Pinchart | b4ccae1 | 2017-11-27 14:59:45 -0500 | [diff] [blame^] | 310 | /* |
| 311 | * vsp1_subdev_set_pad_format - Subdev pad set_fmt handler |
| 312 | * @subdev: V4L2 subdevice |
| 313 | * @cfg: V4L2 subdev pad configuration |
| 314 | * @fmt: V4L2 subdev format |
| 315 | * @codes: Array of supported media bus codes |
| 316 | * @ncodes: Number of supported media bus codes |
| 317 | * @min_width: Minimum image width |
| 318 | * @min_height: Minimum image height |
| 319 | * @max_width: Maximum image width |
| 320 | * @max_height: Maximum image height |
| 321 | * |
| 322 | * This function implements the subdev set_fmt pad operation for entities that |
| 323 | * do not support scaling or cropping. It defaults to the first supplied media |
| 324 | * bus code if the requested code isn't supported, clamps the size to the |
| 325 | * supplied minimum and maximum, and propagates the sink pad format to the |
| 326 | * source pad. |
| 327 | */ |
| 328 | int vsp1_subdev_set_pad_format(struct v4l2_subdev *subdev, |
| 329 | struct v4l2_subdev_pad_config *cfg, |
| 330 | struct v4l2_subdev_format *fmt, |
| 331 | const unsigned int *codes, unsigned int ncodes, |
| 332 | unsigned int min_width, unsigned int min_height, |
| 333 | unsigned int max_width, unsigned int max_height) |
| 334 | { |
| 335 | struct vsp1_entity *entity = to_vsp1_entity(subdev); |
| 336 | struct v4l2_subdev_pad_config *config; |
| 337 | struct v4l2_mbus_framefmt *format; |
| 338 | unsigned int i; |
| 339 | int ret = 0; |
| 340 | |
| 341 | mutex_lock(&entity->lock); |
| 342 | |
| 343 | config = vsp1_entity_get_pad_config(entity, cfg, fmt->which); |
| 344 | if (!config) { |
| 345 | ret = -EINVAL; |
| 346 | goto done; |
| 347 | } |
| 348 | |
| 349 | format = vsp1_entity_get_pad_format(entity, config, fmt->pad); |
| 350 | |
| 351 | if (fmt->pad == entity->source_pad) { |
| 352 | /* The output format can't be modified. */ |
| 353 | fmt->format = *format; |
| 354 | goto done; |
| 355 | } |
| 356 | |
| 357 | /* |
| 358 | * Default to the first media bus code if the requested format is not |
| 359 | * supported. |
| 360 | */ |
| 361 | for (i = 0; i < ncodes; ++i) { |
| 362 | if (fmt->format.code == codes[i]) |
| 363 | break; |
| 364 | } |
| 365 | |
| 366 | format->code = i < ncodes ? codes[i] : codes[0]; |
| 367 | format->width = clamp_t(unsigned int, fmt->format.width, |
| 368 | min_width, max_width); |
| 369 | format->height = clamp_t(unsigned int, fmt->format.height, |
| 370 | min_height, max_height); |
| 371 | format->field = V4L2_FIELD_NONE; |
| 372 | format->colorspace = V4L2_COLORSPACE_SRGB; |
| 373 | |
| 374 | fmt->format = *format; |
| 375 | |
| 376 | /* Propagate the format to the source pad. */ |
| 377 | format = vsp1_entity_get_pad_format(entity, config, entity->source_pad); |
| 378 | *format = fmt->format; |
| 379 | |
| 380 | done: |
| 381 | mutex_unlock(&entity->lock); |
| 382 | return ret; |
| 383 | } |
| 384 | |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 385 | /* ----------------------------------------------------------------------------- |
| 386 | * Media Operations |
| 387 | */ |
| 388 | |
Laurent Pinchart | 2275e8d | 2017-06-25 20:15:22 +0300 | [diff] [blame] | 389 | static inline struct vsp1_entity * |
| 390 | media_entity_to_vsp1_entity(struct media_entity *entity) |
| 391 | { |
| 392 | return container_of(entity, struct vsp1_entity, subdev.entity); |
| 393 | } |
| 394 | |
Laurent Pinchart | c8663c8 | 2016-09-07 09:09:53 -0300 | [diff] [blame] | 395 | static int vsp1_entity_link_setup_source(const struct media_pad *source_pad, |
| 396 | const struct media_pad *sink_pad, |
| 397 | u32 flags) |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 398 | { |
| 399 | struct vsp1_entity *source; |
| 400 | |
Laurent Pinchart | c8663c8 | 2016-09-07 09:09:53 -0300 | [diff] [blame] | 401 | source = media_entity_to_vsp1_entity(source_pad->entity); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 402 | |
| 403 | if (!source->route) |
| 404 | return 0; |
| 405 | |
| 406 | if (flags & MEDIA_LNK_FL_ENABLED) { |
Laurent Pinchart | c8663c8 | 2016-09-07 09:09:53 -0300 | [diff] [blame] | 407 | struct vsp1_entity *sink |
| 408 | = media_entity_to_vsp1_entity(sink_pad->entity); |
| 409 | |
| 410 | /* |
| 411 | * Fan-out is limited to one for the normal data path plus |
| 412 | * optional HGO and HGT. We ignore the HGO and HGT here. |
| 413 | */ |
| 414 | if (sink->type != VSP1_ENTITY_HGO && |
| 415 | sink->type != VSP1_ENTITY_HGT) { |
| 416 | if (source->sink) |
| 417 | return -EBUSY; |
Laurent Pinchart | 2275e8d | 2017-06-25 20:15:22 +0300 | [diff] [blame] | 418 | source->sink = sink; |
Laurent Pinchart | c8663c8 | 2016-09-07 09:09:53 -0300 | [diff] [blame] | 419 | source->sink_pad = sink_pad->index; |
| 420 | } |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 421 | } else { |
| 422 | source->sink = NULL; |
Laurent Pinchart | d9b45ed | 2013-07-10 18:37:27 -0300 | [diff] [blame] | 423 | source->sink_pad = 0; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | return 0; |
| 427 | } |
| 428 | |
Laurent Pinchart | c8663c8 | 2016-09-07 09:09:53 -0300 | [diff] [blame] | 429 | static int vsp1_entity_link_setup_sink(const struct media_pad *source_pad, |
| 430 | const struct media_pad *sink_pad, |
| 431 | u32 flags) |
| 432 | { |
| 433 | struct vsp1_entity *sink; |
Laurent Pinchart | 2275e8d | 2017-06-25 20:15:22 +0300 | [diff] [blame] | 434 | struct vsp1_entity *source; |
Laurent Pinchart | c8663c8 | 2016-09-07 09:09:53 -0300 | [diff] [blame] | 435 | |
| 436 | sink = media_entity_to_vsp1_entity(sink_pad->entity); |
Laurent Pinchart | 2275e8d | 2017-06-25 20:15:22 +0300 | [diff] [blame] | 437 | source = media_entity_to_vsp1_entity(source_pad->entity); |
Laurent Pinchart | c8663c8 | 2016-09-07 09:09:53 -0300 | [diff] [blame] | 438 | |
| 439 | if (flags & MEDIA_LNK_FL_ENABLED) { |
| 440 | /* Fan-in is limited to one. */ |
| 441 | if (sink->sources[sink_pad->index]) |
| 442 | return -EBUSY; |
| 443 | |
Laurent Pinchart | 2275e8d | 2017-06-25 20:15:22 +0300 | [diff] [blame] | 444 | sink->sources[sink_pad->index] = source; |
Laurent Pinchart | c8663c8 | 2016-09-07 09:09:53 -0300 | [diff] [blame] | 445 | } else { |
| 446 | sink->sources[sink_pad->index] = NULL; |
| 447 | } |
| 448 | |
| 449 | return 0; |
| 450 | } |
| 451 | |
| 452 | int vsp1_entity_link_setup(struct media_entity *entity, |
| 453 | const struct media_pad *local, |
| 454 | const struct media_pad *remote, u32 flags) |
| 455 | { |
| 456 | if (local->flags & MEDIA_PAD_FL_SOURCE) |
| 457 | return vsp1_entity_link_setup_source(local, remote, flags); |
| 458 | else |
| 459 | return vsp1_entity_link_setup_sink(remote, local, flags); |
| 460 | } |
| 461 | |
| 462 | /** |
| 463 | * vsp1_entity_remote_pad - Find the pad at the remote end of a link |
| 464 | * @pad: Pad at the local end of the link |
| 465 | * |
| 466 | * Search for a remote pad connected to the given pad by iterating over all |
| 467 | * links originating or terminating at that pad until an enabled link is found. |
| 468 | * |
| 469 | * Our link setup implementation guarantees that the output fan-out will not be |
| 470 | * higher than one for the data pipelines, except for the links to the HGO and |
| 471 | * HGT that can be enabled in addition to a regular data link. When traversing |
| 472 | * outgoing links this function ignores HGO and HGT entities and should thus be |
| 473 | * used in place of the generic media_entity_remote_pad() function to traverse |
| 474 | * data pipelines. |
| 475 | * |
| 476 | * Return a pointer to the pad at the remote end of the first found enabled |
| 477 | * link, or NULL if no enabled link has been found. |
| 478 | */ |
| 479 | struct media_pad *vsp1_entity_remote_pad(struct media_pad *pad) |
| 480 | { |
| 481 | struct media_link *link; |
| 482 | |
| 483 | list_for_each_entry(link, &pad->entity->links, list) { |
| 484 | struct vsp1_entity *entity; |
| 485 | |
| 486 | if (!(link->flags & MEDIA_LNK_FL_ENABLED)) |
| 487 | continue; |
| 488 | |
| 489 | /* If we're the sink the source will never be an HGO or HGT. */ |
| 490 | if (link->sink == pad) |
| 491 | return link->source; |
| 492 | |
| 493 | if (link->source != pad) |
| 494 | continue; |
| 495 | |
| 496 | /* If the sink isn't a subdevice it can't be an HGO or HGT. */ |
| 497 | if (!is_media_entity_v4l2_subdev(link->sink->entity)) |
| 498 | return link->sink; |
| 499 | |
| 500 | entity = media_entity_to_vsp1_entity(link->sink->entity); |
| 501 | if (entity->type != VSP1_ENTITY_HGO && |
| 502 | entity->type != VSP1_ENTITY_HGT) |
| 503 | return link->sink; |
| 504 | } |
| 505 | |
| 506 | return NULL; |
| 507 | |
| 508 | } |
| 509 | |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 510 | /* ----------------------------------------------------------------------------- |
| 511 | * Initialization |
| 512 | */ |
| 513 | |
Laurent Pinchart | 44f4619 | 2016-02-24 19:10:04 -0300 | [diff] [blame] | 514 | #define VSP1_ENTITY_ROUTE(ent) \ |
| 515 | { VSP1_ENTITY_##ent, 0, VI6_DPR_##ent##_ROUTE, \ |
| 516 | { VI6_DPR_NODE_##ent }, VI6_DPR_NODE_##ent } |
| 517 | |
| 518 | #define VSP1_ENTITY_ROUTE_RPF(idx) \ |
| 519 | { VSP1_ENTITY_RPF, idx, VI6_DPR_RPF_ROUTE(idx), \ |
| 520 | { 0, }, VI6_DPR_NODE_RPF(idx) } |
| 521 | |
| 522 | #define VSP1_ENTITY_ROUTE_UDS(idx) \ |
| 523 | { VSP1_ENTITY_UDS, idx, VI6_DPR_UDS_ROUTE(idx), \ |
| 524 | { VI6_DPR_NODE_UDS(idx) }, VI6_DPR_NODE_UDS(idx) } |
| 525 | |
| 526 | #define VSP1_ENTITY_ROUTE_WPF(idx) \ |
| 527 | { VSP1_ENTITY_WPF, idx, 0, \ |
| 528 | { VI6_DPR_NODE_WPF(idx) }, VI6_DPR_NODE_WPF(idx) } |
| 529 | |
Laurent Pinchart | d9b45ed | 2013-07-10 18:37:27 -0300 | [diff] [blame] | 530 | static const struct vsp1_route vsp1_routes[] = { |
Laurent Pinchart | 6134148 | 2017-05-25 00:16:57 +0300 | [diff] [blame] | 531 | { VSP1_ENTITY_BRS, 0, VI6_DPR_ILV_BRS_ROUTE, |
| 532 | { VI6_DPR_NODE_BRS_IN(0), VI6_DPR_NODE_BRS_IN(1) }, 0 }, |
Laurent Pinchart | 629bb6d | 2013-07-10 18:03:46 -0300 | [diff] [blame] | 533 | { VSP1_ENTITY_BRU, 0, VI6_DPR_BRU_ROUTE, |
| 534 | { VI6_DPR_NODE_BRU_IN(0), VI6_DPR_NODE_BRU_IN(1), |
Laurent Pinchart | 7f2d50f | 2015-09-07 08:05:39 -0300 | [diff] [blame] | 535 | VI6_DPR_NODE_BRU_IN(2), VI6_DPR_NODE_BRU_IN(3), |
Laurent Pinchart | 44f4619 | 2016-02-24 19:10:04 -0300 | [diff] [blame] | 536 | VI6_DPR_NODE_BRU_IN(4) }, VI6_DPR_NODE_BRU_OUT }, |
Laurent Pinchart | 1fd87bf | 2015-11-11 23:04:44 -0200 | [diff] [blame] | 537 | VSP1_ENTITY_ROUTE(CLU), |
Laurent Pinchart | f242152 | 2016-02-24 20:40:22 -0300 | [diff] [blame] | 538 | { VSP1_ENTITY_HGO, 0, 0, { 0, }, 0 }, |
Niklas Söderlund | 0ac702d | 2016-09-06 11:38:56 -0300 | [diff] [blame] | 539 | { VSP1_ENTITY_HGT, 0, 0, { 0, }, 0 }, |
Laurent Pinchart | 44f4619 | 2016-02-24 19:10:04 -0300 | [diff] [blame] | 540 | VSP1_ENTITY_ROUTE(HSI), |
| 541 | VSP1_ENTITY_ROUTE(HST), |
Laurent Pinchart | 3be0bf9 | 2017-06-21 16:10:18 +0300 | [diff] [blame] | 542 | { VSP1_ENTITY_LIF, 0, 0, { 0, }, 0 }, |
| 543 | { VSP1_ENTITY_LIF, 1, 0, { 0, }, 0 }, |
Laurent Pinchart | 44f4619 | 2016-02-24 19:10:04 -0300 | [diff] [blame] | 544 | VSP1_ENTITY_ROUTE(LUT), |
| 545 | VSP1_ENTITY_ROUTE_RPF(0), |
| 546 | VSP1_ENTITY_ROUTE_RPF(1), |
| 547 | VSP1_ENTITY_ROUTE_RPF(2), |
| 548 | VSP1_ENTITY_ROUTE_RPF(3), |
| 549 | VSP1_ENTITY_ROUTE_RPF(4), |
| 550 | VSP1_ENTITY_ROUTE(SRU), |
| 551 | VSP1_ENTITY_ROUTE_UDS(0), |
| 552 | VSP1_ENTITY_ROUTE_UDS(1), |
| 553 | VSP1_ENTITY_ROUTE_UDS(2), |
| 554 | VSP1_ENTITY_ROUTE_WPF(0), |
| 555 | VSP1_ENTITY_ROUTE_WPF(1), |
| 556 | VSP1_ENTITY_ROUTE_WPF(2), |
| 557 | VSP1_ENTITY_ROUTE_WPF(3), |
Laurent Pinchart | d9b45ed | 2013-07-10 18:37:27 -0300 | [diff] [blame] | 558 | }; |
| 559 | |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 560 | int vsp1_entity_init(struct vsp1_device *vsp1, struct vsp1_entity *entity, |
Laurent Pinchart | 823329d | 2015-11-15 19:42:01 -0200 | [diff] [blame] | 561 | const char *name, unsigned int num_pads, |
Laurent Pinchart | 6a8e07b | 2016-02-15 22:10:26 -0200 | [diff] [blame] | 562 | const struct v4l2_subdev_ops *ops, u32 function) |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 563 | { |
Laurent Pinchart | 823329d | 2015-11-15 19:42:01 -0200 | [diff] [blame] | 564 | struct v4l2_subdev *subdev; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 565 | unsigned int i; |
Laurent Pinchart | 823329d | 2015-11-15 19:42:01 -0200 | [diff] [blame] | 566 | int ret; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 567 | |
Laurent Pinchart | d9b45ed | 2013-07-10 18:37:27 -0300 | [diff] [blame] | 568 | for (i = 0; i < ARRAY_SIZE(vsp1_routes); ++i) { |
| 569 | if (vsp1_routes[i].type == entity->type && |
| 570 | vsp1_routes[i].index == entity->index) { |
| 571 | entity->route = &vsp1_routes[i]; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 572 | break; |
| 573 | } |
| 574 | } |
| 575 | |
Laurent Pinchart | d9b45ed | 2013-07-10 18:37:27 -0300 | [diff] [blame] | 576 | if (i == ARRAY_SIZE(vsp1_routes)) |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 577 | return -EINVAL; |
| 578 | |
Laurent Pinchart | 34e77ed | 2016-06-26 08:09:31 -0300 | [diff] [blame] | 579 | mutex_init(&entity->lock); |
| 580 | |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 581 | entity->vsp1 = vsp1; |
| 582 | entity->source_pad = num_pads - 1; |
| 583 | |
Laurent Pinchart | e790c3c | 2015-11-15 19:14:22 -0200 | [diff] [blame] | 584 | /* Allocate and initialize pads. */ |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 585 | entity->pads = devm_kzalloc(vsp1->dev, num_pads * sizeof(*entity->pads), |
| 586 | GFP_KERNEL); |
| 587 | if (entity->pads == NULL) |
| 588 | return -ENOMEM; |
| 589 | |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 590 | for (i = 0; i < num_pads - 1; ++i) |
| 591 | entity->pads[i].flags = MEDIA_PAD_FL_SINK; |
| 592 | |
Laurent Pinchart | c8663c8 | 2016-09-07 09:09:53 -0300 | [diff] [blame] | 593 | entity->sources = devm_kcalloc(vsp1->dev, max(num_pads - 1, 1U), |
| 594 | sizeof(*entity->sources), GFP_KERNEL); |
| 595 | if (entity->sources == NULL) |
| 596 | return -ENOMEM; |
| 597 | |
| 598 | /* Single-pad entities only have a sink. */ |
| 599 | entity->pads[num_pads - 1].flags = num_pads > 1 ? MEDIA_PAD_FL_SOURCE |
| 600 | : MEDIA_PAD_FL_SINK; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 601 | |
| 602 | /* Initialize the media entity. */ |
Laurent Pinchart | 823329d | 2015-11-15 19:42:01 -0200 | [diff] [blame] | 603 | ret = media_entity_pads_init(&entity->subdev.entity, num_pads, |
| 604 | entity->pads); |
| 605 | if (ret < 0) |
| 606 | return ret; |
| 607 | |
| 608 | /* Initialize the V4L2 subdev. */ |
| 609 | subdev = &entity->subdev; |
| 610 | v4l2_subdev_init(subdev, ops); |
| 611 | |
Laurent Pinchart | 6a8e07b | 2016-02-15 22:10:26 -0200 | [diff] [blame] | 612 | subdev->entity.function = function; |
Laurent Pinchart | 823329d | 2015-11-15 19:42:01 -0200 | [diff] [blame] | 613 | subdev->entity.ops = &vsp1->media_ops; |
Laurent Pinchart | 823329d | 2015-11-15 19:42:01 -0200 | [diff] [blame] | 614 | subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; |
| 615 | |
| 616 | snprintf(subdev->name, sizeof(subdev->name), "%s %s", |
| 617 | dev_name(vsp1->dev), name); |
| 618 | |
Laurent Pinchart | 0efdf0f | 2015-11-15 20:09:08 -0200 | [diff] [blame] | 619 | vsp1_entity_init_cfg(subdev, NULL); |
Laurent Pinchart | 823329d | 2015-11-15 19:42:01 -0200 | [diff] [blame] | 620 | |
Laurent Pinchart | 9dbed95 | 2017-02-26 10:29:50 -0300 | [diff] [blame] | 621 | /* |
| 622 | * Allocate the pad configuration to store formats and selection |
Laurent Pinchart | e790c3c | 2015-11-15 19:14:22 -0200 | [diff] [blame] | 623 | * rectangles. |
| 624 | */ |
| 625 | entity->config = v4l2_subdev_alloc_pad_config(&entity->subdev); |
| 626 | if (entity->config == NULL) { |
| 627 | media_entity_cleanup(&entity->subdev.entity); |
| 628 | return -ENOMEM; |
| 629 | } |
| 630 | |
Laurent Pinchart | 823329d | 2015-11-15 19:42:01 -0200 | [diff] [blame] | 631 | return 0; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | void vsp1_entity_destroy(struct vsp1_entity *entity) |
| 635 | { |
Laurent Pinchart | 5243453 | 2015-11-17 12:23:23 -0200 | [diff] [blame] | 636 | if (entity->ops && entity->ops->destroy) |
| 637 | entity->ops->destroy(entity); |
Laurent Pinchart | a626e64 | 2013-07-10 12:03:30 -0300 | [diff] [blame] | 638 | if (entity->subdev.ctrl_handler) |
| 639 | v4l2_ctrl_handler_free(entity->subdev.ctrl_handler); |
Laurent Pinchart | e790c3c | 2015-11-15 19:14:22 -0200 | [diff] [blame] | 640 | v4l2_subdev_free_pad_config(entity->config); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 641 | media_entity_cleanup(&entity->subdev.entity); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 642 | } |