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" |
| 22 | #include "vsp1_entity.h" |
Laurent Pinchart | 1499be6 | 2014-05-28 12:49:13 -0300 | [diff] [blame] | 23 | #include "vsp1_video.h" |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 24 | |
Laurent Pinchart | 960de2c | 2014-05-31 10:40:51 -0300 | [diff] [blame] | 25 | bool vsp1_entity_is_streaming(struct vsp1_entity *entity) |
| 26 | { |
Laurent Pinchart | adb8963 | 2015-04-13 11:43:40 -0300 | [diff] [blame] | 27 | unsigned long flags; |
Laurent Pinchart | 960de2c | 2014-05-31 10:40:51 -0300 | [diff] [blame] | 28 | bool streaming; |
| 29 | |
Laurent Pinchart | adb8963 | 2015-04-13 11:43:40 -0300 | [diff] [blame] | 30 | spin_lock_irqsave(&entity->lock, flags); |
Laurent Pinchart | 960de2c | 2014-05-31 10:40:51 -0300 | [diff] [blame] | 31 | streaming = entity->streaming; |
Laurent Pinchart | adb8963 | 2015-04-13 11:43:40 -0300 | [diff] [blame] | 32 | spin_unlock_irqrestore(&entity->lock, flags); |
Laurent Pinchart | 960de2c | 2014-05-31 10:40:51 -0300 | [diff] [blame] | 33 | |
| 34 | return streaming; |
| 35 | } |
| 36 | |
| 37 | int vsp1_entity_set_streaming(struct vsp1_entity *entity, bool streaming) |
| 38 | { |
Laurent Pinchart | adb8963 | 2015-04-13 11:43:40 -0300 | [diff] [blame] | 39 | unsigned long flags; |
Laurent Pinchart | 960de2c | 2014-05-31 10:40:51 -0300 | [diff] [blame] | 40 | int ret; |
| 41 | |
Laurent Pinchart | adb8963 | 2015-04-13 11:43:40 -0300 | [diff] [blame] | 42 | spin_lock_irqsave(&entity->lock, flags); |
Laurent Pinchart | 960de2c | 2014-05-31 10:40:51 -0300 | [diff] [blame] | 43 | entity->streaming = streaming; |
Laurent Pinchart | adb8963 | 2015-04-13 11:43:40 -0300 | [diff] [blame] | 44 | spin_unlock_irqrestore(&entity->lock, flags); |
Laurent Pinchart | 960de2c | 2014-05-31 10:40:51 -0300 | [diff] [blame] | 45 | |
| 46 | if (!streaming) |
| 47 | return 0; |
| 48 | |
| 49 | if (!entity->subdev.ctrl_handler) |
| 50 | return 0; |
| 51 | |
| 52 | ret = v4l2_ctrl_handler_setup(entity->subdev.ctrl_handler); |
| 53 | if (ret < 0) { |
Laurent Pinchart | adb8963 | 2015-04-13 11:43:40 -0300 | [diff] [blame] | 54 | spin_lock_irqsave(&entity->lock, flags); |
Laurent Pinchart | 960de2c | 2014-05-31 10:40:51 -0300 | [diff] [blame] | 55 | entity->streaming = false; |
Laurent Pinchart | adb8963 | 2015-04-13 11:43:40 -0300 | [diff] [blame] | 56 | spin_unlock_irqrestore(&entity->lock, flags); |
Laurent Pinchart | 960de2c | 2014-05-31 10:40:51 -0300 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | return ret; |
| 60 | } |
| 61 | |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 62 | /* ----------------------------------------------------------------------------- |
| 63 | * V4L2 Subdevice Operations |
| 64 | */ |
| 65 | |
| 66 | struct v4l2_mbus_framefmt * |
| 67 | vsp1_entity_get_pad_format(struct vsp1_entity *entity, |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 68 | struct v4l2_subdev_pad_config *cfg, |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 69 | unsigned int pad, u32 which) |
| 70 | { |
| 71 | switch (which) { |
| 72 | case V4L2_SUBDEV_FORMAT_TRY: |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 73 | return v4l2_subdev_get_try_format(&entity->subdev, cfg, pad); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 74 | case V4L2_SUBDEV_FORMAT_ACTIVE: |
| 75 | return &entity->formats[pad]; |
| 76 | default: |
| 77 | return NULL; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | * vsp1_entity_init_formats - Initialize formats on all pads |
| 83 | * @subdev: V4L2 subdevice |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 84 | * @cfg: V4L2 subdev pad configuration |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 85 | * |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 86 | * Initialize all pad formats with default values. If cfg is not NULL, try |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 87 | * formats are initialized on the file handle. Otherwise active formats are |
| 88 | * initialized on the device. |
| 89 | */ |
| 90 | void vsp1_entity_init_formats(struct v4l2_subdev *subdev, |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 91 | struct v4l2_subdev_pad_config *cfg) |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 92 | { |
| 93 | struct v4l2_subdev_format format; |
| 94 | unsigned int pad; |
| 95 | |
| 96 | for (pad = 0; pad < subdev->entity.num_pads - 1; ++pad) { |
| 97 | memset(&format, 0, sizeof(format)); |
| 98 | |
| 99 | format.pad = pad; |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 100 | format.which = cfg ? V4L2_SUBDEV_FORMAT_TRY |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 101 | : V4L2_SUBDEV_FORMAT_ACTIVE; |
| 102 | |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 103 | v4l2_subdev_call(subdev, pad, set_fmt, cfg, &format); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
| 107 | static int vsp1_entity_open(struct v4l2_subdev *subdev, |
| 108 | struct v4l2_subdev_fh *fh) |
| 109 | { |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 110 | vsp1_entity_init_formats(subdev, fh->pad); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | const struct v4l2_subdev_internal_ops vsp1_subdev_internal_ops = { |
| 116 | .open = vsp1_entity_open, |
| 117 | }; |
| 118 | |
| 119 | /* ----------------------------------------------------------------------------- |
| 120 | * Media Operations |
| 121 | */ |
| 122 | |
| 123 | static int vsp1_entity_link_setup(struct media_entity *entity, |
| 124 | const struct media_pad *local, |
| 125 | const struct media_pad *remote, u32 flags) |
| 126 | { |
| 127 | struct vsp1_entity *source; |
| 128 | |
| 129 | if (!(local->flags & MEDIA_PAD_FL_SOURCE)) |
| 130 | return 0; |
| 131 | |
| 132 | source = container_of(local->entity, struct vsp1_entity, subdev.entity); |
| 133 | |
| 134 | if (!source->route) |
| 135 | return 0; |
| 136 | |
| 137 | if (flags & MEDIA_LNK_FL_ENABLED) { |
| 138 | if (source->sink) |
| 139 | return -EBUSY; |
| 140 | source->sink = remote->entity; |
Laurent Pinchart | d9b45ed | 2013-07-10 18:37:27 -0300 | [diff] [blame] | 141 | source->sink_pad = remote->index; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 142 | } else { |
| 143 | source->sink = NULL; |
Laurent Pinchart | d9b45ed | 2013-07-10 18:37:27 -0300 | [diff] [blame] | 144 | source->sink_pad = 0; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | const struct media_entity_operations vsp1_media_ops = { |
| 151 | .link_setup = vsp1_entity_link_setup, |
| 152 | .link_validate = v4l2_subdev_link_validate, |
| 153 | }; |
| 154 | |
| 155 | /* ----------------------------------------------------------------------------- |
| 156 | * Initialization |
| 157 | */ |
| 158 | |
Laurent Pinchart | d9b45ed | 2013-07-10 18:37:27 -0300 | [diff] [blame] | 159 | static const struct vsp1_route vsp1_routes[] = { |
Laurent Pinchart | 629bb6d | 2013-07-10 18:03:46 -0300 | [diff] [blame] | 160 | { VSP1_ENTITY_BRU, 0, VI6_DPR_BRU_ROUTE, |
| 161 | { VI6_DPR_NODE_BRU_IN(0), VI6_DPR_NODE_BRU_IN(1), |
| 162 | VI6_DPR_NODE_BRU_IN(2), VI6_DPR_NODE_BRU_IN(3), } }, |
Laurent Pinchart | d9b45ed | 2013-07-10 18:37:27 -0300 | [diff] [blame] | 163 | { VSP1_ENTITY_HSI, 0, VI6_DPR_HSI_ROUTE, { VI6_DPR_NODE_HSI, } }, |
| 164 | { VSP1_ENTITY_HST, 0, VI6_DPR_HST_ROUTE, { VI6_DPR_NODE_HST, } }, |
| 165 | { VSP1_ENTITY_LIF, 0, 0, { VI6_DPR_NODE_LIF, } }, |
| 166 | { VSP1_ENTITY_LUT, 0, VI6_DPR_LUT_ROUTE, { VI6_DPR_NODE_LUT, } }, |
| 167 | { VSP1_ENTITY_RPF, 0, VI6_DPR_RPF_ROUTE(0), { VI6_DPR_NODE_RPF(0), } }, |
| 168 | { VSP1_ENTITY_RPF, 1, VI6_DPR_RPF_ROUTE(1), { VI6_DPR_NODE_RPF(1), } }, |
| 169 | { VSP1_ENTITY_RPF, 2, VI6_DPR_RPF_ROUTE(2), { VI6_DPR_NODE_RPF(2), } }, |
| 170 | { VSP1_ENTITY_RPF, 3, VI6_DPR_RPF_ROUTE(3), { VI6_DPR_NODE_RPF(3), } }, |
| 171 | { VSP1_ENTITY_RPF, 4, VI6_DPR_RPF_ROUTE(4), { VI6_DPR_NODE_RPF(4), } }, |
| 172 | { VSP1_ENTITY_SRU, 0, VI6_DPR_SRU_ROUTE, { VI6_DPR_NODE_SRU, } }, |
| 173 | { VSP1_ENTITY_UDS, 0, VI6_DPR_UDS_ROUTE(0), { VI6_DPR_NODE_UDS(0), } }, |
| 174 | { VSP1_ENTITY_UDS, 1, VI6_DPR_UDS_ROUTE(1), { VI6_DPR_NODE_UDS(1), } }, |
| 175 | { VSP1_ENTITY_UDS, 2, VI6_DPR_UDS_ROUTE(2), { VI6_DPR_NODE_UDS(2), } }, |
| 176 | { VSP1_ENTITY_WPF, 0, 0, { VI6_DPR_NODE_WPF(0), } }, |
| 177 | { VSP1_ENTITY_WPF, 1, 0, { VI6_DPR_NODE_WPF(1), } }, |
| 178 | { VSP1_ENTITY_WPF, 2, 0, { VI6_DPR_NODE_WPF(2), } }, |
| 179 | { VSP1_ENTITY_WPF, 3, 0, { VI6_DPR_NODE_WPF(3), } }, |
| 180 | }; |
| 181 | |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 182 | int vsp1_entity_init(struct vsp1_device *vsp1, struct vsp1_entity *entity, |
| 183 | unsigned int num_pads) |
| 184 | { |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 185 | unsigned int i; |
| 186 | |
Laurent Pinchart | d9b45ed | 2013-07-10 18:37:27 -0300 | [diff] [blame] | 187 | for (i = 0; i < ARRAY_SIZE(vsp1_routes); ++i) { |
| 188 | if (vsp1_routes[i].type == entity->type && |
| 189 | vsp1_routes[i].index == entity->index) { |
| 190 | entity->route = &vsp1_routes[i]; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 191 | break; |
| 192 | } |
| 193 | } |
| 194 | |
Laurent Pinchart | d9b45ed | 2013-07-10 18:37:27 -0300 | [diff] [blame] | 195 | if (i == ARRAY_SIZE(vsp1_routes)) |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 196 | return -EINVAL; |
| 197 | |
Laurent Pinchart | adb8963 | 2015-04-13 11:43:40 -0300 | [diff] [blame] | 198 | spin_lock_init(&entity->lock); |
Laurent Pinchart | 960de2c | 2014-05-31 10:40:51 -0300 | [diff] [blame] | 199 | |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 200 | entity->vsp1 = vsp1; |
| 201 | entity->source_pad = num_pads - 1; |
| 202 | |
| 203 | /* Allocate formats and pads. */ |
| 204 | entity->formats = devm_kzalloc(vsp1->dev, |
| 205 | num_pads * sizeof(*entity->formats), |
| 206 | GFP_KERNEL); |
| 207 | if (entity->formats == NULL) |
| 208 | return -ENOMEM; |
| 209 | |
| 210 | entity->pads = devm_kzalloc(vsp1->dev, num_pads * sizeof(*entity->pads), |
| 211 | GFP_KERNEL); |
| 212 | if (entity->pads == NULL) |
| 213 | return -ENOMEM; |
| 214 | |
| 215 | /* Initialize pads. */ |
| 216 | for (i = 0; i < num_pads - 1; ++i) |
| 217 | entity->pads[i].flags = MEDIA_PAD_FL_SINK; |
| 218 | |
| 219 | entity->pads[num_pads - 1].flags = MEDIA_PAD_FL_SOURCE; |
| 220 | |
| 221 | /* Initialize the media entity. */ |
Mauro Carvalho Chehab | ab22e77 | 2015-12-11 07:44:40 -0200 | [diff] [blame^] | 222 | return media_entity_pads_init(&entity->subdev.entity, num_pads, |
Mauro Carvalho Chehab | 1809510 | 2015-08-06 09:25:57 -0300 | [diff] [blame] | 223 | entity->pads); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | void vsp1_entity_destroy(struct vsp1_entity *entity) |
| 227 | { |
Laurent Pinchart | 1499be6 | 2014-05-28 12:49:13 -0300 | [diff] [blame] | 228 | if (entity->video) |
| 229 | vsp1_video_cleanup(entity->video); |
Laurent Pinchart | a626e64 | 2013-07-10 12:03:30 -0300 | [diff] [blame] | 230 | if (entity->subdev.ctrl_handler) |
| 231 | v4l2_ctrl_handler_free(entity->subdev.ctrl_handler); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 232 | media_entity_cleanup(&entity->subdev.entity); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 233 | } |