Helen Fornazier | 5ba0ae4 | 2017-06-19 14:00:11 -0300 | [diff] [blame] | 1 | /* |
| 2 | * vimc-common.c Virtual Media Controller Driver |
| 3 | * |
| 4 | * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | */ |
| 17 | |
Helen Fornazier | 4a29b70 | 2017-06-19 14:00:18 -0300 | [diff] [blame] | 18 | #include <linux/init.h> |
| 19 | #include <linux/module.h> |
| 20 | |
Helen Fornazier | 5ba0ae4 | 2017-06-19 14:00:11 -0300 | [diff] [blame] | 21 | #include "vimc-common.h" |
| 22 | |
Helen Fornazier | 6856ba7 | 2017-06-19 14:00:20 -0300 | [diff] [blame] | 23 | /* |
| 24 | * NOTE: non-bayer formats need to come first (necessary for enum_mbus_code |
| 25 | * in the scaler) |
| 26 | */ |
Helen Fornazier | 5ba0ae4 | 2017-06-19 14:00:11 -0300 | [diff] [blame] | 27 | static const struct vimc_pix_map vimc_pix_map_list[] = { |
| 28 | /* TODO: add all missing formats */ |
| 29 | |
| 30 | /* RGB formats */ |
| 31 | { |
| 32 | .code = MEDIA_BUS_FMT_BGR888_1X24, |
| 33 | .pixelformat = V4L2_PIX_FMT_BGR24, |
| 34 | .bpp = 3, |
Helen Fornazier | 6856ba7 | 2017-06-19 14:00:20 -0300 | [diff] [blame] | 35 | .bayer = false, |
Helen Fornazier | 5ba0ae4 | 2017-06-19 14:00:11 -0300 | [diff] [blame] | 36 | }, |
| 37 | { |
| 38 | .code = MEDIA_BUS_FMT_RGB888_1X24, |
| 39 | .pixelformat = V4L2_PIX_FMT_RGB24, |
| 40 | .bpp = 3, |
Helen Fornazier | 6856ba7 | 2017-06-19 14:00:20 -0300 | [diff] [blame] | 41 | .bayer = false, |
Helen Fornazier | 5ba0ae4 | 2017-06-19 14:00:11 -0300 | [diff] [blame] | 42 | }, |
| 43 | { |
| 44 | .code = MEDIA_BUS_FMT_ARGB8888_1X32, |
| 45 | .pixelformat = V4L2_PIX_FMT_ARGB32, |
| 46 | .bpp = 4, |
Helen Fornazier | 6856ba7 | 2017-06-19 14:00:20 -0300 | [diff] [blame] | 47 | .bayer = false, |
Helen Fornazier | 5ba0ae4 | 2017-06-19 14:00:11 -0300 | [diff] [blame] | 48 | }, |
| 49 | |
| 50 | /* Bayer formats */ |
| 51 | { |
| 52 | .code = MEDIA_BUS_FMT_SBGGR8_1X8, |
| 53 | .pixelformat = V4L2_PIX_FMT_SBGGR8, |
| 54 | .bpp = 1, |
Helen Fornazier | 6856ba7 | 2017-06-19 14:00:20 -0300 | [diff] [blame] | 55 | .bayer = true, |
Helen Fornazier | 5ba0ae4 | 2017-06-19 14:00:11 -0300 | [diff] [blame] | 56 | }, |
| 57 | { |
| 58 | .code = MEDIA_BUS_FMT_SGBRG8_1X8, |
| 59 | .pixelformat = V4L2_PIX_FMT_SGBRG8, |
| 60 | .bpp = 1, |
Helen Fornazier | 6856ba7 | 2017-06-19 14:00:20 -0300 | [diff] [blame] | 61 | .bayer = true, |
Helen Fornazier | 5ba0ae4 | 2017-06-19 14:00:11 -0300 | [diff] [blame] | 62 | }, |
| 63 | { |
| 64 | .code = MEDIA_BUS_FMT_SGRBG8_1X8, |
| 65 | .pixelformat = V4L2_PIX_FMT_SGRBG8, |
| 66 | .bpp = 1, |
Helen Fornazier | 6856ba7 | 2017-06-19 14:00:20 -0300 | [diff] [blame] | 67 | .bayer = true, |
Helen Fornazier | 5ba0ae4 | 2017-06-19 14:00:11 -0300 | [diff] [blame] | 68 | }, |
| 69 | { |
| 70 | .code = MEDIA_BUS_FMT_SRGGB8_1X8, |
| 71 | .pixelformat = V4L2_PIX_FMT_SRGGB8, |
| 72 | .bpp = 1, |
Helen Fornazier | 6856ba7 | 2017-06-19 14:00:20 -0300 | [diff] [blame] | 73 | .bayer = true, |
Helen Fornazier | 5ba0ae4 | 2017-06-19 14:00:11 -0300 | [diff] [blame] | 74 | }, |
| 75 | { |
| 76 | .code = MEDIA_BUS_FMT_SBGGR10_1X10, |
| 77 | .pixelformat = V4L2_PIX_FMT_SBGGR10, |
| 78 | .bpp = 2, |
Helen Fornazier | 6856ba7 | 2017-06-19 14:00:20 -0300 | [diff] [blame] | 79 | .bayer = true, |
Helen Fornazier | 5ba0ae4 | 2017-06-19 14:00:11 -0300 | [diff] [blame] | 80 | }, |
| 81 | { |
| 82 | .code = MEDIA_BUS_FMT_SGBRG10_1X10, |
| 83 | .pixelformat = V4L2_PIX_FMT_SGBRG10, |
| 84 | .bpp = 2, |
Helen Fornazier | 6856ba7 | 2017-06-19 14:00:20 -0300 | [diff] [blame] | 85 | .bayer = true, |
Helen Fornazier | 5ba0ae4 | 2017-06-19 14:00:11 -0300 | [diff] [blame] | 86 | }, |
| 87 | { |
| 88 | .code = MEDIA_BUS_FMT_SGRBG10_1X10, |
| 89 | .pixelformat = V4L2_PIX_FMT_SGRBG10, |
| 90 | .bpp = 2, |
Helen Fornazier | 6856ba7 | 2017-06-19 14:00:20 -0300 | [diff] [blame] | 91 | .bayer = true, |
Helen Fornazier | 5ba0ae4 | 2017-06-19 14:00:11 -0300 | [diff] [blame] | 92 | }, |
| 93 | { |
| 94 | .code = MEDIA_BUS_FMT_SRGGB10_1X10, |
| 95 | .pixelformat = V4L2_PIX_FMT_SRGGB10, |
| 96 | .bpp = 2, |
Helen Fornazier | 6856ba7 | 2017-06-19 14:00:20 -0300 | [diff] [blame] | 97 | .bayer = true, |
Helen Fornazier | 5ba0ae4 | 2017-06-19 14:00:11 -0300 | [diff] [blame] | 98 | }, |
| 99 | |
| 100 | /* 10bit raw bayer a-law compressed to 8 bits */ |
| 101 | { |
| 102 | .code = MEDIA_BUS_FMT_SBGGR10_ALAW8_1X8, |
| 103 | .pixelformat = V4L2_PIX_FMT_SBGGR10ALAW8, |
| 104 | .bpp = 1, |
Helen Fornazier | 6856ba7 | 2017-06-19 14:00:20 -0300 | [diff] [blame] | 105 | .bayer = true, |
Helen Fornazier | 5ba0ae4 | 2017-06-19 14:00:11 -0300 | [diff] [blame] | 106 | }, |
| 107 | { |
| 108 | .code = MEDIA_BUS_FMT_SGBRG10_ALAW8_1X8, |
| 109 | .pixelformat = V4L2_PIX_FMT_SGBRG10ALAW8, |
| 110 | .bpp = 1, |
Helen Fornazier | 6856ba7 | 2017-06-19 14:00:20 -0300 | [diff] [blame] | 111 | .bayer = true, |
Helen Fornazier | 5ba0ae4 | 2017-06-19 14:00:11 -0300 | [diff] [blame] | 112 | }, |
| 113 | { |
| 114 | .code = MEDIA_BUS_FMT_SGRBG10_ALAW8_1X8, |
| 115 | .pixelformat = V4L2_PIX_FMT_SGRBG10ALAW8, |
| 116 | .bpp = 1, |
Helen Fornazier | 6856ba7 | 2017-06-19 14:00:20 -0300 | [diff] [blame] | 117 | .bayer = true, |
Helen Fornazier | 5ba0ae4 | 2017-06-19 14:00:11 -0300 | [diff] [blame] | 118 | }, |
| 119 | { |
| 120 | .code = MEDIA_BUS_FMT_SRGGB10_ALAW8_1X8, |
| 121 | .pixelformat = V4L2_PIX_FMT_SRGGB10ALAW8, |
| 122 | .bpp = 1, |
Helen Fornazier | 6856ba7 | 2017-06-19 14:00:20 -0300 | [diff] [blame] | 123 | .bayer = true, |
Helen Fornazier | 5ba0ae4 | 2017-06-19 14:00:11 -0300 | [diff] [blame] | 124 | }, |
| 125 | |
| 126 | /* 10bit raw bayer DPCM compressed to 8 bits */ |
| 127 | { |
| 128 | .code = MEDIA_BUS_FMT_SBGGR10_DPCM8_1X8, |
| 129 | .pixelformat = V4L2_PIX_FMT_SBGGR10DPCM8, |
| 130 | .bpp = 1, |
Helen Fornazier | 6856ba7 | 2017-06-19 14:00:20 -0300 | [diff] [blame] | 131 | .bayer = true, |
Helen Fornazier | 5ba0ae4 | 2017-06-19 14:00:11 -0300 | [diff] [blame] | 132 | }, |
| 133 | { |
| 134 | .code = MEDIA_BUS_FMT_SGBRG10_DPCM8_1X8, |
| 135 | .pixelformat = V4L2_PIX_FMT_SGBRG10DPCM8, |
| 136 | .bpp = 1, |
Helen Fornazier | 6856ba7 | 2017-06-19 14:00:20 -0300 | [diff] [blame] | 137 | .bayer = true, |
Helen Fornazier | 5ba0ae4 | 2017-06-19 14:00:11 -0300 | [diff] [blame] | 138 | }, |
| 139 | { |
| 140 | .code = MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8, |
| 141 | .pixelformat = V4L2_PIX_FMT_SGRBG10DPCM8, |
| 142 | .bpp = 1, |
Helen Fornazier | 6856ba7 | 2017-06-19 14:00:20 -0300 | [diff] [blame] | 143 | .bayer = true, |
Helen Fornazier | 5ba0ae4 | 2017-06-19 14:00:11 -0300 | [diff] [blame] | 144 | }, |
| 145 | { |
| 146 | .code = MEDIA_BUS_FMT_SRGGB10_DPCM8_1X8, |
| 147 | .pixelformat = V4L2_PIX_FMT_SRGGB10DPCM8, |
| 148 | .bpp = 1, |
Helen Fornazier | 6856ba7 | 2017-06-19 14:00:20 -0300 | [diff] [blame] | 149 | .bayer = true, |
Helen Fornazier | 5ba0ae4 | 2017-06-19 14:00:11 -0300 | [diff] [blame] | 150 | }, |
| 151 | { |
| 152 | .code = MEDIA_BUS_FMT_SBGGR12_1X12, |
| 153 | .pixelformat = V4L2_PIX_FMT_SBGGR12, |
| 154 | .bpp = 2, |
Helen Fornazier | 6856ba7 | 2017-06-19 14:00:20 -0300 | [diff] [blame] | 155 | .bayer = true, |
Helen Fornazier | 5ba0ae4 | 2017-06-19 14:00:11 -0300 | [diff] [blame] | 156 | }, |
| 157 | { |
| 158 | .code = MEDIA_BUS_FMT_SGBRG12_1X12, |
| 159 | .pixelformat = V4L2_PIX_FMT_SGBRG12, |
| 160 | .bpp = 2, |
Helen Fornazier | 6856ba7 | 2017-06-19 14:00:20 -0300 | [diff] [blame] | 161 | .bayer = true, |
Helen Fornazier | 5ba0ae4 | 2017-06-19 14:00:11 -0300 | [diff] [blame] | 162 | }, |
| 163 | { |
| 164 | .code = MEDIA_BUS_FMT_SGRBG12_1X12, |
| 165 | .pixelformat = V4L2_PIX_FMT_SGRBG12, |
| 166 | .bpp = 2, |
Helen Fornazier | 6856ba7 | 2017-06-19 14:00:20 -0300 | [diff] [blame] | 167 | .bayer = true, |
Helen Fornazier | 5ba0ae4 | 2017-06-19 14:00:11 -0300 | [diff] [blame] | 168 | }, |
| 169 | { |
| 170 | .code = MEDIA_BUS_FMT_SRGGB12_1X12, |
| 171 | .pixelformat = V4L2_PIX_FMT_SRGGB12, |
| 172 | .bpp = 2, |
Helen Fornazier | 6856ba7 | 2017-06-19 14:00:20 -0300 | [diff] [blame] | 173 | .bayer = true, |
Helen Fornazier | 5ba0ae4 | 2017-06-19 14:00:11 -0300 | [diff] [blame] | 174 | }, |
| 175 | }; |
| 176 | |
Helen Fornazier | 88ad71a | 2017-06-19 14:00:16 -0300 | [diff] [blame] | 177 | const struct vimc_pix_map *vimc_pix_map_by_index(unsigned int i) |
| 178 | { |
| 179 | if (i >= ARRAY_SIZE(vimc_pix_map_list)) |
| 180 | return NULL; |
| 181 | |
| 182 | return &vimc_pix_map_list[i]; |
| 183 | } |
Helen Fornazier | 4a29b70 | 2017-06-19 14:00:18 -0300 | [diff] [blame] | 184 | EXPORT_SYMBOL_GPL(vimc_pix_map_by_index); |
Helen Fornazier | 88ad71a | 2017-06-19 14:00:16 -0300 | [diff] [blame] | 185 | |
Helen Fornazier | 5ba0ae4 | 2017-06-19 14:00:11 -0300 | [diff] [blame] | 186 | const struct vimc_pix_map *vimc_pix_map_by_code(u32 code) |
| 187 | { |
| 188 | unsigned int i; |
| 189 | |
| 190 | for (i = 0; i < ARRAY_SIZE(vimc_pix_map_list); i++) { |
| 191 | if (vimc_pix_map_list[i].code == code) |
| 192 | return &vimc_pix_map_list[i]; |
| 193 | } |
| 194 | return NULL; |
| 195 | } |
Helen Fornazier | 4a29b70 | 2017-06-19 14:00:18 -0300 | [diff] [blame] | 196 | EXPORT_SYMBOL_GPL(vimc_pix_map_by_code); |
Helen Fornazier | 5ba0ae4 | 2017-06-19 14:00:11 -0300 | [diff] [blame] | 197 | |
| 198 | const struct vimc_pix_map *vimc_pix_map_by_pixelformat(u32 pixelformat) |
| 199 | { |
| 200 | unsigned int i; |
| 201 | |
| 202 | for (i = 0; i < ARRAY_SIZE(vimc_pix_map_list); i++) { |
| 203 | if (vimc_pix_map_list[i].pixelformat == pixelformat) |
| 204 | return &vimc_pix_map_list[i]; |
| 205 | } |
| 206 | return NULL; |
| 207 | } |
Helen Fornazier | 4a29b70 | 2017-06-19 14:00:18 -0300 | [diff] [blame] | 208 | EXPORT_SYMBOL_GPL(vimc_pix_map_by_pixelformat); |
Helen Fornazier | 5ba0ae4 | 2017-06-19 14:00:11 -0300 | [diff] [blame] | 209 | |
| 210 | int vimc_propagate_frame(struct media_pad *src, const void *frame) |
| 211 | { |
| 212 | struct media_link *link; |
| 213 | |
| 214 | if (!(src->flags & MEDIA_PAD_FL_SOURCE)) |
| 215 | return -EINVAL; |
| 216 | |
| 217 | /* Send this frame to all sink pads that are direct linked */ |
| 218 | list_for_each_entry(link, &src->entity->links, list) { |
| 219 | if (link->source == src && |
| 220 | (link->flags & MEDIA_LNK_FL_ENABLED)) { |
| 221 | struct vimc_ent_device *ved = NULL; |
| 222 | struct media_entity *entity = link->sink->entity; |
| 223 | |
| 224 | if (is_media_entity_v4l2_subdev(entity)) { |
| 225 | struct v4l2_subdev *sd = |
| 226 | container_of(entity, struct v4l2_subdev, |
| 227 | entity); |
| 228 | ved = v4l2_get_subdevdata(sd); |
| 229 | } else if (is_media_entity_v4l2_video_device(entity)) { |
| 230 | struct video_device *vdev = |
| 231 | container_of(entity, |
| 232 | struct video_device, |
| 233 | entity); |
| 234 | ved = video_get_drvdata(vdev); |
| 235 | } |
| 236 | if (ved && ved->process_frame) |
| 237 | ved->process_frame(ved, link->sink, frame); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | return 0; |
| 242 | } |
Helen Fornazier | 4a29b70 | 2017-06-19 14:00:18 -0300 | [diff] [blame] | 243 | EXPORT_SYMBOL_GPL(vimc_propagate_frame); |
Helen Fornazier | 5ba0ae4 | 2017-06-19 14:00:11 -0300 | [diff] [blame] | 244 | |
| 245 | /* Helper function to allocate and initialize pads */ |
| 246 | struct media_pad *vimc_pads_init(u16 num_pads, const unsigned long *pads_flag) |
| 247 | { |
| 248 | struct media_pad *pads; |
| 249 | unsigned int i; |
| 250 | |
| 251 | /* Allocate memory for the pads */ |
| 252 | pads = kcalloc(num_pads, sizeof(*pads), GFP_KERNEL); |
| 253 | if (!pads) |
| 254 | return ERR_PTR(-ENOMEM); |
| 255 | |
| 256 | /* Initialize the pads */ |
| 257 | for (i = 0; i < num_pads; i++) { |
| 258 | pads[i].index = i; |
| 259 | pads[i].flags = pads_flag[i]; |
| 260 | } |
| 261 | |
| 262 | return pads; |
| 263 | } |
Helen Fornazier | 4a29b70 | 2017-06-19 14:00:18 -0300 | [diff] [blame] | 264 | EXPORT_SYMBOL_GPL(vimc_pads_init); |
Helen Fornazier | c149543 | 2017-06-19 14:00:12 -0300 | [diff] [blame] | 265 | |
Helen Fornazier | bf5fb95 | 2017-06-19 14:00:13 -0300 | [diff] [blame] | 266 | int vimc_pipeline_s_stream(struct media_entity *ent, int enable) |
| 267 | { |
| 268 | struct v4l2_subdev *sd; |
| 269 | struct media_pad *pad; |
| 270 | unsigned int i; |
| 271 | int ret; |
| 272 | |
| 273 | for (i = 0; i < ent->num_pads; i++) { |
| 274 | if (ent->pads[i].flags & MEDIA_PAD_FL_SOURCE) |
| 275 | continue; |
| 276 | |
| 277 | /* Start the stream in the subdevice direct connected */ |
| 278 | pad = media_entity_remote_pad(&ent->pads[i]); |
Helen Fornazier | e159b60 | 2018-12-07 12:56:41 -0500 | [diff] [blame^] | 279 | if (!pad) |
| 280 | continue; |
Helen Fornazier | bf5fb95 | 2017-06-19 14:00:13 -0300 | [diff] [blame] | 281 | |
Helen Fornazier | 4a29b70 | 2017-06-19 14:00:18 -0300 | [diff] [blame] | 282 | if (!is_media_entity_v4l2_subdev(pad->entity)) |
| 283 | return -EINVAL; |
Helen Fornazier | bf5fb95 | 2017-06-19 14:00:13 -0300 | [diff] [blame] | 284 | |
| 285 | sd = media_entity_to_v4l2_subdev(pad->entity); |
| 286 | ret = v4l2_subdev_call(sd, video, s_stream, enable); |
| 287 | if (ret && ret != -ENOIOCTLCMD) |
| 288 | return ret; |
| 289 | } |
| 290 | |
| 291 | return 0; |
| 292 | } |
Helen Fornazier | 4a29b70 | 2017-06-19 14:00:18 -0300 | [diff] [blame] | 293 | EXPORT_SYMBOL_GPL(vimc_pipeline_s_stream); |
Helen Fornazier | bf5fb95 | 2017-06-19 14:00:13 -0300 | [diff] [blame] | 294 | |
Helen Fornazier | 288a22d | 2017-06-19 14:00:14 -0300 | [diff] [blame] | 295 | static int vimc_get_mbus_format(struct media_pad *pad, |
| 296 | struct v4l2_subdev_format *fmt) |
| 297 | { |
| 298 | if (is_media_entity_v4l2_subdev(pad->entity)) { |
| 299 | struct v4l2_subdev *sd = |
| 300 | media_entity_to_v4l2_subdev(pad->entity); |
| 301 | int ret; |
| 302 | |
| 303 | fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE; |
| 304 | fmt->pad = pad->index; |
| 305 | |
| 306 | ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, fmt); |
| 307 | if (ret) |
| 308 | return ret; |
| 309 | |
| 310 | } else if (is_media_entity_v4l2_video_device(pad->entity)) { |
| 311 | struct video_device *vdev = container_of(pad->entity, |
| 312 | struct video_device, |
| 313 | entity); |
| 314 | struct vimc_ent_device *ved = video_get_drvdata(vdev); |
| 315 | const struct vimc_pix_map *vpix; |
| 316 | struct v4l2_pix_format vdev_fmt; |
| 317 | |
| 318 | if (!ved->vdev_get_format) |
| 319 | return -ENOIOCTLCMD; |
| 320 | |
| 321 | ved->vdev_get_format(ved, &vdev_fmt); |
| 322 | vpix = vimc_pix_map_by_pixelformat(vdev_fmt.pixelformat); |
| 323 | v4l2_fill_mbus_format(&fmt->format, &vdev_fmt, vpix->code); |
| 324 | } else { |
| 325 | return -EINVAL; |
| 326 | } |
| 327 | |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | int vimc_link_validate(struct media_link *link) |
| 332 | { |
| 333 | struct v4l2_subdev_format source_fmt, sink_fmt; |
| 334 | int ret; |
| 335 | |
Helen Fornazier | 288a22d | 2017-06-19 14:00:14 -0300 | [diff] [blame] | 336 | ret = vimc_get_mbus_format(link->source, &source_fmt); |
| 337 | if (ret) |
| 338 | return ret; |
| 339 | |
| 340 | ret = vimc_get_mbus_format(link->sink, &sink_fmt); |
| 341 | if (ret) |
| 342 | return ret; |
| 343 | |
| 344 | pr_info("vimc link validate: " |
| 345 | "%s:src:%dx%d (0x%x, %d, %d, %d, %d) " |
| 346 | "%s:snk:%dx%d (0x%x, %d, %d, %d, %d)\n", |
| 347 | /* src */ |
| 348 | link->source->entity->name, |
| 349 | source_fmt.format.width, source_fmt.format.height, |
| 350 | source_fmt.format.code, source_fmt.format.colorspace, |
| 351 | source_fmt.format.quantization, source_fmt.format.xfer_func, |
| 352 | source_fmt.format.ycbcr_enc, |
| 353 | /* sink */ |
| 354 | link->sink->entity->name, |
| 355 | sink_fmt.format.width, sink_fmt.format.height, |
| 356 | sink_fmt.format.code, sink_fmt.format.colorspace, |
| 357 | sink_fmt.format.quantization, sink_fmt.format.xfer_func, |
| 358 | sink_fmt.format.ycbcr_enc); |
| 359 | |
| 360 | /* The width, height and code must match. */ |
| 361 | if (source_fmt.format.width != sink_fmt.format.width |
| 362 | || source_fmt.format.height != sink_fmt.format.height |
| 363 | || source_fmt.format.code != sink_fmt.format.code) |
| 364 | return -EPIPE; |
| 365 | |
| 366 | /* |
| 367 | * The field order must match, or the sink field order must be NONE |
| 368 | * to support interlaced hardware connected to bridges that support |
| 369 | * progressive formats only. |
| 370 | */ |
| 371 | if (source_fmt.format.field != sink_fmt.format.field && |
| 372 | sink_fmt.format.field != V4L2_FIELD_NONE) |
| 373 | return -EPIPE; |
| 374 | |
| 375 | /* |
| 376 | * If colorspace is DEFAULT, then assume all the colorimetry is also |
| 377 | * DEFAULT, return 0 to skip comparing the other colorimetry parameters |
| 378 | */ |
| 379 | if (source_fmt.format.colorspace == V4L2_COLORSPACE_DEFAULT |
| 380 | || sink_fmt.format.colorspace == V4L2_COLORSPACE_DEFAULT) |
| 381 | return 0; |
| 382 | |
| 383 | /* Colorspace must match. */ |
| 384 | if (source_fmt.format.colorspace != sink_fmt.format.colorspace) |
| 385 | return -EPIPE; |
| 386 | |
| 387 | /* Colorimetry must match if they are not set to DEFAULT */ |
| 388 | if (source_fmt.format.ycbcr_enc != V4L2_YCBCR_ENC_DEFAULT |
| 389 | && sink_fmt.format.ycbcr_enc != V4L2_YCBCR_ENC_DEFAULT |
| 390 | && source_fmt.format.ycbcr_enc != sink_fmt.format.ycbcr_enc) |
| 391 | return -EPIPE; |
| 392 | |
| 393 | if (source_fmt.format.quantization != V4L2_QUANTIZATION_DEFAULT |
| 394 | && sink_fmt.format.quantization != V4L2_QUANTIZATION_DEFAULT |
| 395 | && source_fmt.format.quantization != sink_fmt.format.quantization) |
| 396 | return -EPIPE; |
| 397 | |
| 398 | if (source_fmt.format.xfer_func != V4L2_XFER_FUNC_DEFAULT |
| 399 | && sink_fmt.format.xfer_func != V4L2_XFER_FUNC_DEFAULT |
| 400 | && source_fmt.format.xfer_func != sink_fmt.format.xfer_func) |
| 401 | return -EPIPE; |
| 402 | |
| 403 | return 0; |
| 404 | } |
Helen Fornazier | 4a29b70 | 2017-06-19 14:00:18 -0300 | [diff] [blame] | 405 | EXPORT_SYMBOL_GPL(vimc_link_validate); |
Helen Fornazier | 288a22d | 2017-06-19 14:00:14 -0300 | [diff] [blame] | 406 | |
Helen Fornazier | c149543 | 2017-06-19 14:00:12 -0300 | [diff] [blame] | 407 | static const struct media_entity_operations vimc_ent_sd_mops = { |
Helen Fornazier | 288a22d | 2017-06-19 14:00:14 -0300 | [diff] [blame] | 408 | .link_validate = vimc_link_validate, |
Helen Fornazier | c149543 | 2017-06-19 14:00:12 -0300 | [diff] [blame] | 409 | }; |
| 410 | |
| 411 | int vimc_ent_sd_register(struct vimc_ent_device *ved, |
| 412 | struct v4l2_subdev *sd, |
| 413 | struct v4l2_device *v4l2_dev, |
| 414 | const char *const name, |
| 415 | u32 function, |
| 416 | u16 num_pads, |
| 417 | const unsigned long *pads_flag, |
Helen Fornazier | 4a29b70 | 2017-06-19 14:00:18 -0300 | [diff] [blame] | 418 | const struct v4l2_subdev_ops *sd_ops) |
Helen Fornazier | c149543 | 2017-06-19 14:00:12 -0300 | [diff] [blame] | 419 | { |
| 420 | int ret; |
| 421 | |
| 422 | /* Allocate the pads */ |
| 423 | ved->pads = vimc_pads_init(num_pads, pads_flag); |
| 424 | if (IS_ERR(ved->pads)) |
| 425 | return PTR_ERR(ved->pads); |
| 426 | |
| 427 | /* Fill the vimc_ent_device struct */ |
Helen Fornazier | c149543 | 2017-06-19 14:00:12 -0300 | [diff] [blame] | 428 | ved->ent = &sd->entity; |
| 429 | |
| 430 | /* Initialize the subdev */ |
| 431 | v4l2_subdev_init(sd, sd_ops); |
| 432 | sd->entity.function = function; |
| 433 | sd->entity.ops = &vimc_ent_sd_mops; |
| 434 | sd->owner = THIS_MODULE; |
Mauro Carvalho Chehab | c0decac | 2018-09-10 08:19:14 -0400 | [diff] [blame] | 435 | strscpy(sd->name, name, sizeof(sd->name)); |
Helen Fornazier | c149543 | 2017-06-19 14:00:12 -0300 | [diff] [blame] | 436 | v4l2_set_subdevdata(sd, ved); |
| 437 | |
| 438 | /* Expose this subdev to user space */ |
Hans Verkuil | 3da7ee9 | 2018-02-02 08:00:32 -0500 | [diff] [blame] | 439 | sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; |
| 440 | if (sd->ctrl_handler) |
| 441 | sd->flags |= V4L2_SUBDEV_FL_HAS_EVENTS; |
Helen Fornazier | c149543 | 2017-06-19 14:00:12 -0300 | [diff] [blame] | 442 | |
| 443 | /* Initialize the media entity */ |
| 444 | ret = media_entity_pads_init(&sd->entity, num_pads, ved->pads); |
| 445 | if (ret) |
| 446 | goto err_clean_pads; |
| 447 | |
| 448 | /* Register the subdev with the v4l2 and the media framework */ |
| 449 | ret = v4l2_device_register_subdev(v4l2_dev, sd); |
| 450 | if (ret) { |
| 451 | dev_err(v4l2_dev->dev, |
| 452 | "%s: subdev register failed (err=%d)\n", |
| 453 | name, ret); |
| 454 | goto err_clean_m_ent; |
| 455 | } |
| 456 | |
| 457 | return 0; |
| 458 | |
| 459 | err_clean_m_ent: |
| 460 | media_entity_cleanup(&sd->entity); |
| 461 | err_clean_pads: |
| 462 | vimc_pads_cleanup(ved->pads); |
| 463 | return ret; |
| 464 | } |
Helen Fornazier | 4a29b70 | 2017-06-19 14:00:18 -0300 | [diff] [blame] | 465 | EXPORT_SYMBOL_GPL(vimc_ent_sd_register); |
Helen Fornazier | c149543 | 2017-06-19 14:00:12 -0300 | [diff] [blame] | 466 | |
| 467 | void vimc_ent_sd_unregister(struct vimc_ent_device *ved, struct v4l2_subdev *sd) |
| 468 | { |
| 469 | v4l2_device_unregister_subdev(sd); |
| 470 | media_entity_cleanup(ved->ent); |
| 471 | vimc_pads_cleanup(ved->pads); |
| 472 | } |
Helen Fornazier | 4a29b70 | 2017-06-19 14:00:18 -0300 | [diff] [blame] | 473 | EXPORT_SYMBOL_GPL(vimc_ent_sd_unregister); |
| 474 | |
| 475 | MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC) Common"); |
| 476 | MODULE_AUTHOR("Helen Koike <helen.fornazier@gmail.com>"); |
| 477 | MODULE_LICENSE("GPL"); |