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