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 | aa380ea | 2015-11-01 10:46:25 -0200 | [diff] [blame] | 24 | #include "vsp1_pipe.h" |
| 25 | |
| 26 | void vsp1_mod_write(struct vsp1_entity *e, u32 reg, u32 data) |
| 27 | { |
| 28 | struct vsp1_pipeline *pipe = to_vsp1_pipeline(&e->subdev.entity); |
| 29 | |
Laurent Pinchart | 351bbf9 | 2015-11-01 15:18:56 -0200 | [diff] [blame] | 30 | vsp1_dl_list_write(pipe->dl, reg, data); |
Laurent Pinchart | aa380ea | 2015-11-01 10:46:25 -0200 | [diff] [blame] | 31 | } |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 32 | |
Laurent Pinchart | 665b693 | 2015-08-02 18:58:31 -0300 | [diff] [blame] | 33 | void vsp1_entity_route_setup(struct vsp1_entity *source) |
| 34 | { |
| 35 | struct vsp1_entity *sink; |
| 36 | |
| 37 | if (source->route->reg == 0) |
| 38 | return; |
| 39 | |
| 40 | sink = container_of(source->sink, struct vsp1_entity, subdev.entity); |
Takashi Saito | 1517b03 | 2015-09-07 01:40:25 -0300 | [diff] [blame] | 41 | vsp1_mod_write(source, source->route->reg, |
| 42 | sink->route->inputs[source->sink_pad]); |
Laurent Pinchart | 665b693 | 2015-08-02 18:58:31 -0300 | [diff] [blame] | 43 | } |
| 44 | |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 45 | /* ----------------------------------------------------------------------------- |
| 46 | * V4L2 Subdevice Operations |
| 47 | */ |
| 48 | |
Laurent Pinchart | e790c3c | 2015-11-15 19:14:22 -0200 | [diff] [blame^] | 49 | /** |
| 50 | * vsp1_entity_get_pad_config - Get the pad configuration for an entity |
| 51 | * @entity: the entity |
| 52 | * @cfg: the TRY pad configuration |
| 53 | * @which: configuration selector (ACTIVE or TRY) |
| 54 | * |
| 55 | * Return the pad configuration requested by the which argument. The TRY |
| 56 | * configuration is passed explicitly to the function through the cfg argument |
| 57 | * and simply returned when requested. The ACTIVE configuration comes from the |
| 58 | * entity structure. |
| 59 | */ |
| 60 | struct v4l2_subdev_pad_config * |
| 61 | vsp1_entity_get_pad_config(struct vsp1_entity *entity, |
| 62 | struct v4l2_subdev_pad_config *cfg, |
| 63 | enum v4l2_subdev_format_whence which) |
| 64 | { |
| 65 | switch (which) { |
| 66 | case V4L2_SUBDEV_FORMAT_ACTIVE: |
| 67 | return entity->config; |
| 68 | case V4L2_SUBDEV_FORMAT_TRY: |
| 69 | default: |
| 70 | return cfg; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * vsp1_entity_get_pad_format - Get a pad format from storage for an entity |
| 76 | * @entity: the entity |
| 77 | * @cfg: the configuration storage |
| 78 | * @pad: the pad number |
| 79 | * |
| 80 | * Return the format stored in the given configuration for an entity's pad. The |
| 81 | * configuration can be an ACTIVE or TRY configuration. |
| 82 | */ |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 83 | struct v4l2_mbus_framefmt * |
| 84 | vsp1_entity_get_pad_format(struct vsp1_entity *entity, |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 85 | struct v4l2_subdev_pad_config *cfg, |
Laurent Pinchart | e790c3c | 2015-11-15 19:14:22 -0200 | [diff] [blame^] | 86 | unsigned int pad) |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 87 | { |
Laurent Pinchart | e790c3c | 2015-11-15 19:14:22 -0200 | [diff] [blame^] | 88 | return v4l2_subdev_get_try_format(&entity->subdev, cfg, pad); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /* |
Laurent Pinchart | 0efdf0f | 2015-11-15 20:09:08 -0200 | [diff] [blame] | 92 | * vsp1_entity_init_cfg - Initialize formats on all pads |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 93 | * @subdev: V4L2 subdevice |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 94 | * @cfg: V4L2 subdev pad configuration |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 95 | * |
Laurent Pinchart | 0efdf0f | 2015-11-15 20:09:08 -0200 | [diff] [blame] | 96 | * Initialize all pad formats with default values in the given pad config. This |
| 97 | * 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] | 98 | */ |
Laurent Pinchart | 0efdf0f | 2015-11-15 20:09:08 -0200 | [diff] [blame] | 99 | int vsp1_entity_init_cfg(struct v4l2_subdev *subdev, |
| 100 | struct v4l2_subdev_pad_config *cfg) |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 101 | { |
| 102 | struct v4l2_subdev_format format; |
| 103 | unsigned int pad; |
| 104 | |
| 105 | for (pad = 0; pad < subdev->entity.num_pads - 1; ++pad) { |
| 106 | memset(&format, 0, sizeof(format)); |
| 107 | |
| 108 | format.pad = pad; |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 109 | format.which = cfg ? V4L2_SUBDEV_FORMAT_TRY |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 110 | : V4L2_SUBDEV_FORMAT_ACTIVE; |
| 111 | |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 112 | v4l2_subdev_call(subdev, pad, set_fmt, cfg, &format); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 113 | } |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 118 | /* ----------------------------------------------------------------------------- |
| 119 | * Media Operations |
| 120 | */ |
| 121 | |
Laurent Pinchart | babca00 | 2015-08-05 17:14:41 -0300 | [diff] [blame] | 122 | int vsp1_entity_link_setup(struct media_entity *entity, |
| 123 | const struct media_pad *local, |
| 124 | const struct media_pad *remote, u32 flags) |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 125 | { |
| 126 | struct vsp1_entity *source; |
| 127 | |
| 128 | if (!(local->flags & MEDIA_PAD_FL_SOURCE)) |
| 129 | return 0; |
| 130 | |
| 131 | source = container_of(local->entity, struct vsp1_entity, subdev.entity); |
| 132 | |
| 133 | if (!source->route) |
| 134 | return 0; |
| 135 | |
| 136 | if (flags & MEDIA_LNK_FL_ENABLED) { |
| 137 | if (source->sink) |
| 138 | return -EBUSY; |
| 139 | source->sink = remote->entity; |
Laurent Pinchart | d9b45ed | 2013-07-10 18:37:27 -0300 | [diff] [blame] | 140 | source->sink_pad = remote->index; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 141 | } else { |
| 142 | source->sink = NULL; |
Laurent Pinchart | d9b45ed | 2013-07-10 18:37:27 -0300 | [diff] [blame] | 143 | source->sink_pad = 0; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 149 | /* ----------------------------------------------------------------------------- |
| 150 | * Initialization |
| 151 | */ |
| 152 | |
Laurent Pinchart | d9b45ed | 2013-07-10 18:37:27 -0300 | [diff] [blame] | 153 | static const struct vsp1_route vsp1_routes[] = { |
Laurent Pinchart | 629bb6d | 2013-07-10 18:03:46 -0300 | [diff] [blame] | 154 | { VSP1_ENTITY_BRU, 0, VI6_DPR_BRU_ROUTE, |
| 155 | { VI6_DPR_NODE_BRU_IN(0), VI6_DPR_NODE_BRU_IN(1), |
Laurent Pinchart | 7f2d50f | 2015-09-07 08:05:39 -0300 | [diff] [blame] | 156 | VI6_DPR_NODE_BRU_IN(2), VI6_DPR_NODE_BRU_IN(3), |
| 157 | VI6_DPR_NODE_BRU_IN(4) } }, |
Laurent Pinchart | d9b45ed | 2013-07-10 18:37:27 -0300 | [diff] [blame] | 158 | { VSP1_ENTITY_HSI, 0, VI6_DPR_HSI_ROUTE, { VI6_DPR_NODE_HSI, } }, |
| 159 | { VSP1_ENTITY_HST, 0, VI6_DPR_HST_ROUTE, { VI6_DPR_NODE_HST, } }, |
| 160 | { VSP1_ENTITY_LIF, 0, 0, { VI6_DPR_NODE_LIF, } }, |
| 161 | { VSP1_ENTITY_LUT, 0, VI6_DPR_LUT_ROUTE, { VI6_DPR_NODE_LUT, } }, |
| 162 | { VSP1_ENTITY_RPF, 0, VI6_DPR_RPF_ROUTE(0), { VI6_DPR_NODE_RPF(0), } }, |
| 163 | { VSP1_ENTITY_RPF, 1, VI6_DPR_RPF_ROUTE(1), { VI6_DPR_NODE_RPF(1), } }, |
| 164 | { VSP1_ENTITY_RPF, 2, VI6_DPR_RPF_ROUTE(2), { VI6_DPR_NODE_RPF(2), } }, |
| 165 | { VSP1_ENTITY_RPF, 3, VI6_DPR_RPF_ROUTE(3), { VI6_DPR_NODE_RPF(3), } }, |
| 166 | { VSP1_ENTITY_RPF, 4, VI6_DPR_RPF_ROUTE(4), { VI6_DPR_NODE_RPF(4), } }, |
| 167 | { VSP1_ENTITY_SRU, 0, VI6_DPR_SRU_ROUTE, { VI6_DPR_NODE_SRU, } }, |
| 168 | { VSP1_ENTITY_UDS, 0, VI6_DPR_UDS_ROUTE(0), { VI6_DPR_NODE_UDS(0), } }, |
| 169 | { VSP1_ENTITY_UDS, 1, VI6_DPR_UDS_ROUTE(1), { VI6_DPR_NODE_UDS(1), } }, |
| 170 | { VSP1_ENTITY_UDS, 2, VI6_DPR_UDS_ROUTE(2), { VI6_DPR_NODE_UDS(2), } }, |
| 171 | { VSP1_ENTITY_WPF, 0, 0, { VI6_DPR_NODE_WPF(0), } }, |
| 172 | { VSP1_ENTITY_WPF, 1, 0, { VI6_DPR_NODE_WPF(1), } }, |
| 173 | { VSP1_ENTITY_WPF, 2, 0, { VI6_DPR_NODE_WPF(2), } }, |
| 174 | { VSP1_ENTITY_WPF, 3, 0, { VI6_DPR_NODE_WPF(3), } }, |
| 175 | }; |
| 176 | |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 177 | int vsp1_entity_init(struct vsp1_device *vsp1, struct vsp1_entity *entity, |
Laurent Pinchart | 823329d | 2015-11-15 19:42:01 -0200 | [diff] [blame] | 178 | const char *name, unsigned int num_pads, |
| 179 | const struct v4l2_subdev_ops *ops) |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 180 | { |
Laurent Pinchart | 823329d | 2015-11-15 19:42:01 -0200 | [diff] [blame] | 181 | struct v4l2_subdev *subdev; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 182 | unsigned int i; |
Laurent Pinchart | 823329d | 2015-11-15 19:42:01 -0200 | [diff] [blame] | 183 | int ret; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 184 | |
Laurent Pinchart | d9b45ed | 2013-07-10 18:37:27 -0300 | [diff] [blame] | 185 | for (i = 0; i < ARRAY_SIZE(vsp1_routes); ++i) { |
| 186 | if (vsp1_routes[i].type == entity->type && |
| 187 | vsp1_routes[i].index == entity->index) { |
| 188 | entity->route = &vsp1_routes[i]; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 189 | break; |
| 190 | } |
| 191 | } |
| 192 | |
Laurent Pinchart | d9b45ed | 2013-07-10 18:37:27 -0300 | [diff] [blame] | 193 | if (i == ARRAY_SIZE(vsp1_routes)) |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 194 | return -EINVAL; |
| 195 | |
| 196 | entity->vsp1 = vsp1; |
| 197 | entity->source_pad = num_pads - 1; |
| 198 | |
Laurent Pinchart | e790c3c | 2015-11-15 19:14:22 -0200 | [diff] [blame^] | 199 | /* Allocate and initialize pads. */ |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 200 | entity->pads = devm_kzalloc(vsp1->dev, num_pads * sizeof(*entity->pads), |
| 201 | GFP_KERNEL); |
| 202 | if (entity->pads == NULL) |
| 203 | return -ENOMEM; |
| 204 | |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 205 | for (i = 0; i < num_pads - 1; ++i) |
| 206 | entity->pads[i].flags = MEDIA_PAD_FL_SINK; |
| 207 | |
| 208 | entity->pads[num_pads - 1].flags = MEDIA_PAD_FL_SOURCE; |
| 209 | |
| 210 | /* Initialize the media entity. */ |
Laurent Pinchart | 823329d | 2015-11-15 19:42:01 -0200 | [diff] [blame] | 211 | ret = media_entity_pads_init(&entity->subdev.entity, num_pads, |
| 212 | entity->pads); |
| 213 | if (ret < 0) |
| 214 | return ret; |
| 215 | |
| 216 | /* Initialize the V4L2 subdev. */ |
| 217 | subdev = &entity->subdev; |
| 218 | v4l2_subdev_init(subdev, ops); |
| 219 | |
| 220 | subdev->entity.ops = &vsp1->media_ops; |
Laurent Pinchart | 823329d | 2015-11-15 19:42:01 -0200 | [diff] [blame] | 221 | subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; |
| 222 | |
| 223 | snprintf(subdev->name, sizeof(subdev->name), "%s %s", |
| 224 | dev_name(vsp1->dev), name); |
| 225 | |
Laurent Pinchart | 0efdf0f | 2015-11-15 20:09:08 -0200 | [diff] [blame] | 226 | vsp1_entity_init_cfg(subdev, NULL); |
Laurent Pinchart | 823329d | 2015-11-15 19:42:01 -0200 | [diff] [blame] | 227 | |
Laurent Pinchart | e790c3c | 2015-11-15 19:14:22 -0200 | [diff] [blame^] | 228 | /* Allocate the pad configuration to store formats and selection |
| 229 | * rectangles. |
| 230 | */ |
| 231 | entity->config = v4l2_subdev_alloc_pad_config(&entity->subdev); |
| 232 | if (entity->config == NULL) { |
| 233 | media_entity_cleanup(&entity->subdev.entity); |
| 234 | return -ENOMEM; |
| 235 | } |
| 236 | |
Laurent Pinchart | 823329d | 2015-11-15 19:42:01 -0200 | [diff] [blame] | 237 | return 0; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | void vsp1_entity_destroy(struct vsp1_entity *entity) |
| 241 | { |
Laurent Pinchart | 5243453 | 2015-11-17 12:23:23 -0200 | [diff] [blame] | 242 | if (entity->ops && entity->ops->destroy) |
| 243 | entity->ops->destroy(entity); |
Laurent Pinchart | a626e64 | 2013-07-10 12:03:30 -0300 | [diff] [blame] | 244 | if (entity->subdev.ctrl_handler) |
| 245 | v4l2_ctrl_handler_free(entity->subdev.ctrl_handler); |
Laurent Pinchart | e790c3c | 2015-11-15 19:14:22 -0200 | [diff] [blame^] | 246 | v4l2_subdev_free_pad_config(entity->config); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 247 | media_entity_cleanup(&entity->subdev.entity); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 248 | } |