| 1 | /* |
| 2 | * camera image capture (abstract) bus driver |
| 3 | * |
| 4 | * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de> |
| 5 | * |
| 6 | * This driver provides an interface between platform-specific camera |
| 7 | * busses and camera devices. It should be used if the camera is |
| 8 | * connected not over a "proper" bus like PCI or USB, but over a |
| 9 | * special bus, like, for example, the Quick Capture interface on PXA270 |
| 10 | * SoCs. Later it should also be used for i.MX31 SoCs from Freescale. |
| 11 | * It can handle multiple cameras and / or multiple busses, which can |
| 12 | * be used, e.g., in stereo-vision applications. |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License version 2 as |
| 16 | * published by the Free Software Foundation. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/device.h> |
| 20 | #include <linux/err.h> |
| 21 | #include <linux/i2c.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/list.h> |
| 24 | #include <linux/mutex.h> |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/platform_device.h> |
| 27 | #include <linux/regulator/consumer.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include <linux/pm_runtime.h> |
| 30 | #include <linux/vmalloc.h> |
| 31 | |
| 32 | #include <media/soc_camera.h> |
| 33 | #include <media/v4l2-common.h> |
| 34 | #include <media/v4l2-ioctl.h> |
| 35 | #include <media/v4l2-dev.h> |
| 36 | #include <media/videobuf-core.h> |
| 37 | #include <media/videobuf2-core.h> |
| 38 | #include <media/soc_mediabus.h> |
| 39 | |
| 40 | /* Default to VGA resolution */ |
| 41 | #define DEFAULT_WIDTH 640 |
| 42 | #define DEFAULT_HEIGHT 480 |
| 43 | |
| 44 | #define is_streaming(ici, icd) \ |
| 45 | (((ici)->ops->init_videobuf) ? \ |
| 46 | (icd)->vb_vidq.streaming : \ |
| 47 | vb2_is_streaming(&(icd)->vb2_vidq)) |
| 48 | |
| 49 | static LIST_HEAD(hosts); |
| 50 | static LIST_HEAD(devices); |
| 51 | static DEFINE_MUTEX(list_lock); /* Protects the list of hosts */ |
| 52 | |
| 53 | static int soc_camera_power_on(struct soc_camera_device *icd, |
| 54 | struct soc_camera_link *icl) |
| 55 | { |
| 56 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
| 57 | int ret = regulator_bulk_enable(icl->num_regulators, |
| 58 | icl->regulators); |
| 59 | if (ret < 0) { |
| 60 | dev_err(icd->pdev, "Cannot enable regulators\n"); |
| 61 | return ret; |
| 62 | } |
| 63 | |
| 64 | if (icl->power) { |
| 65 | ret = icl->power(icd->control, 1); |
| 66 | if (ret < 0) { |
| 67 | dev_err(icd->pdev, |
| 68 | "Platform failed to power-on the camera.\n"); |
| 69 | goto elinkpwr; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | ret = v4l2_subdev_call(sd, core, s_power, 1); |
| 74 | if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV) |
| 75 | goto esdpwr; |
| 76 | |
| 77 | return 0; |
| 78 | |
| 79 | esdpwr: |
| 80 | if (icl->power) |
| 81 | icl->power(icd->control, 0); |
| 82 | elinkpwr: |
| 83 | regulator_bulk_disable(icl->num_regulators, |
| 84 | icl->regulators); |
| 85 | return ret; |
| 86 | } |
| 87 | |
| 88 | static int soc_camera_power_off(struct soc_camera_device *icd, |
| 89 | struct soc_camera_link *icl) |
| 90 | { |
| 91 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
| 92 | int ret = v4l2_subdev_call(sd, core, s_power, 0); |
| 93 | |
| 94 | if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV) |
| 95 | return ret; |
| 96 | |
| 97 | if (icl->power) { |
| 98 | ret = icl->power(icd->control, 0); |
| 99 | if (ret < 0) { |
| 100 | dev_err(icd->pdev, |
| 101 | "Platform failed to power-off the camera.\n"); |
| 102 | return ret; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | ret = regulator_bulk_disable(icl->num_regulators, |
| 107 | icl->regulators); |
| 108 | if (ret < 0) |
| 109 | dev_err(icd->pdev, "Cannot disable regulators\n"); |
| 110 | |
| 111 | return ret; |
| 112 | } |
| 113 | |
| 114 | const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc( |
| 115 | struct soc_camera_device *icd, unsigned int fourcc) |
| 116 | { |
| 117 | unsigned int i; |
| 118 | |
| 119 | for (i = 0; i < icd->num_user_formats; i++) |
| 120 | if (icd->user_formats[i].host_fmt->fourcc == fourcc) |
| 121 | return icd->user_formats + i; |
| 122 | return NULL; |
| 123 | } |
| 124 | EXPORT_SYMBOL(soc_camera_xlate_by_fourcc); |
| 125 | |
| 126 | /** |
| 127 | * soc_camera_apply_board_flags() - apply platform SOCAM_SENSOR_INVERT_* flags |
| 128 | * @icl: camera platform parameters |
| 129 | * @cfg: media bus configuration |
| 130 | * @return: resulting flags |
| 131 | */ |
| 132 | unsigned long soc_camera_apply_board_flags(struct soc_camera_link *icl, |
| 133 | const struct v4l2_mbus_config *cfg) |
| 134 | { |
| 135 | unsigned long f, flags = cfg->flags; |
| 136 | |
| 137 | /* If only one of the two polarities is supported, switch to the opposite */ |
| 138 | if (icl->flags & SOCAM_SENSOR_INVERT_HSYNC) { |
| 139 | f = flags & (V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW); |
| 140 | if (f == V4L2_MBUS_HSYNC_ACTIVE_HIGH || f == V4L2_MBUS_HSYNC_ACTIVE_LOW) |
| 141 | flags ^= V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW; |
| 142 | } |
| 143 | |
| 144 | if (icl->flags & SOCAM_SENSOR_INVERT_VSYNC) { |
| 145 | f = flags & (V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW); |
| 146 | if (f == V4L2_MBUS_VSYNC_ACTIVE_HIGH || f == V4L2_MBUS_VSYNC_ACTIVE_LOW) |
| 147 | flags ^= V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW; |
| 148 | } |
| 149 | |
| 150 | if (icl->flags & SOCAM_SENSOR_INVERT_PCLK) { |
| 151 | f = flags & (V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING); |
| 152 | if (f == V4L2_MBUS_PCLK_SAMPLE_RISING || f == V4L2_MBUS_PCLK_SAMPLE_FALLING) |
| 153 | flags ^= V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING; |
| 154 | } |
| 155 | |
| 156 | return flags; |
| 157 | } |
| 158 | EXPORT_SYMBOL(soc_camera_apply_board_flags); |
| 159 | |
| 160 | #define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \ |
| 161 | ((x) >> 24) & 0xff |
| 162 | |
| 163 | static int soc_camera_try_fmt(struct soc_camera_device *icd, |
| 164 | struct v4l2_format *f) |
| 165 | { |
| 166 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
| 167 | const struct soc_camera_format_xlate *xlate; |
| 168 | struct v4l2_pix_format *pix = &f->fmt.pix; |
| 169 | int ret; |
| 170 | |
| 171 | dev_dbg(icd->pdev, "TRY_FMT(%c%c%c%c, %ux%u)\n", |
| 172 | pixfmtstr(pix->pixelformat), pix->width, pix->height); |
| 173 | |
| 174 | if (!(ici->capabilities & SOCAM_HOST_CAP_STRIDE)) { |
| 175 | pix->bytesperline = 0; |
| 176 | pix->sizeimage = 0; |
| 177 | } |
| 178 | |
| 179 | ret = ici->ops->try_fmt(icd, f); |
| 180 | if (ret < 0) |
| 181 | return ret; |
| 182 | |
| 183 | xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat); |
| 184 | if (!xlate) |
| 185 | return -EINVAL; |
| 186 | |
| 187 | ret = soc_mbus_bytes_per_line(pix->width, xlate->host_fmt); |
| 188 | if (ret < 0) |
| 189 | return ret; |
| 190 | |
| 191 | pix->bytesperline = max_t(u32, pix->bytesperline, ret); |
| 192 | |
| 193 | ret = soc_mbus_image_size(xlate->host_fmt, pix->bytesperline, |
| 194 | pix->height); |
| 195 | if (ret < 0) |
| 196 | return ret; |
| 197 | |
| 198 | pix->sizeimage = max_t(u32, pix->sizeimage, ret); |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv, |
| 204 | struct v4l2_format *f) |
| 205 | { |
| 206 | struct soc_camera_device *icd = file->private_data; |
| 207 | |
| 208 | WARN_ON(priv != file->private_data); |
| 209 | |
| 210 | /* Only single-plane capture is supported so far */ |
| 211 | if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 212 | return -EINVAL; |
| 213 | |
| 214 | /* limit format to hardware capabilities */ |
| 215 | return soc_camera_try_fmt(icd, f); |
| 216 | } |
| 217 | |
| 218 | static int soc_camera_enum_input(struct file *file, void *priv, |
| 219 | struct v4l2_input *inp) |
| 220 | { |
| 221 | if (inp->index != 0) |
| 222 | return -EINVAL; |
| 223 | |
| 224 | /* default is camera */ |
| 225 | inp->type = V4L2_INPUT_TYPE_CAMERA; |
| 226 | inp->std = V4L2_STD_UNKNOWN; |
| 227 | strcpy(inp->name, "Camera"); |
| 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i) |
| 233 | { |
| 234 | *i = 0; |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | static int soc_camera_s_input(struct file *file, void *priv, unsigned int i) |
| 240 | { |
| 241 | if (i > 0) |
| 242 | return -EINVAL; |
| 243 | |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a) |
| 248 | { |
| 249 | struct soc_camera_device *icd = file->private_data; |
| 250 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
| 251 | |
| 252 | return v4l2_subdev_call(sd, core, s_std, *a); |
| 253 | } |
| 254 | |
| 255 | static int soc_camera_g_std(struct file *file, void *priv, v4l2_std_id *a) |
| 256 | { |
| 257 | struct soc_camera_device *icd = file->private_data; |
| 258 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
| 259 | |
| 260 | return v4l2_subdev_call(sd, core, g_std, a); |
| 261 | } |
| 262 | |
| 263 | static int soc_camera_enum_framesizes(struct file *file, void *fh, |
| 264 | struct v4l2_frmsizeenum *fsize) |
| 265 | { |
| 266 | struct soc_camera_device *icd = file->private_data; |
| 267 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
| 268 | |
| 269 | return ici->ops->enum_framesizes(icd, fsize); |
| 270 | } |
| 271 | |
| 272 | static int soc_camera_reqbufs(struct file *file, void *priv, |
| 273 | struct v4l2_requestbuffers *p) |
| 274 | { |
| 275 | int ret; |
| 276 | struct soc_camera_device *icd = file->private_data; |
| 277 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
| 278 | |
| 279 | WARN_ON(priv != file->private_data); |
| 280 | |
| 281 | if (icd->streamer && icd->streamer != file) |
| 282 | return -EBUSY; |
| 283 | |
| 284 | if (ici->ops->init_videobuf) { |
| 285 | ret = videobuf_reqbufs(&icd->vb_vidq, p); |
| 286 | if (ret < 0) |
| 287 | return ret; |
| 288 | |
| 289 | ret = ici->ops->reqbufs(icd, p); |
| 290 | } else { |
| 291 | ret = vb2_reqbufs(&icd->vb2_vidq, p); |
| 292 | } |
| 293 | |
| 294 | if (!ret && !icd->streamer) |
| 295 | icd->streamer = file; |
| 296 | |
| 297 | return ret; |
| 298 | } |
| 299 | |
| 300 | static int soc_camera_querybuf(struct file *file, void *priv, |
| 301 | struct v4l2_buffer *p) |
| 302 | { |
| 303 | struct soc_camera_device *icd = file->private_data; |
| 304 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
| 305 | |
| 306 | WARN_ON(priv != file->private_data); |
| 307 | |
| 308 | if (ici->ops->init_videobuf) |
| 309 | return videobuf_querybuf(&icd->vb_vidq, p); |
| 310 | else |
| 311 | return vb2_querybuf(&icd->vb2_vidq, p); |
| 312 | } |
| 313 | |
| 314 | static int soc_camera_qbuf(struct file *file, void *priv, |
| 315 | struct v4l2_buffer *p) |
| 316 | { |
| 317 | struct soc_camera_device *icd = file->private_data; |
| 318 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
| 319 | |
| 320 | WARN_ON(priv != file->private_data); |
| 321 | |
| 322 | if (icd->streamer != file) |
| 323 | return -EBUSY; |
| 324 | |
| 325 | if (ici->ops->init_videobuf) |
| 326 | return videobuf_qbuf(&icd->vb_vidq, p); |
| 327 | else |
| 328 | return vb2_qbuf(&icd->vb2_vidq, p); |
| 329 | } |
| 330 | |
| 331 | static int soc_camera_dqbuf(struct file *file, void *priv, |
| 332 | struct v4l2_buffer *p) |
| 333 | { |
| 334 | struct soc_camera_device *icd = file->private_data; |
| 335 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
| 336 | |
| 337 | WARN_ON(priv != file->private_data); |
| 338 | |
| 339 | if (icd->streamer != file) |
| 340 | return -EBUSY; |
| 341 | |
| 342 | if (ici->ops->init_videobuf) |
| 343 | return videobuf_dqbuf(&icd->vb_vidq, p, file->f_flags & O_NONBLOCK); |
| 344 | else |
| 345 | return vb2_dqbuf(&icd->vb2_vidq, p, file->f_flags & O_NONBLOCK); |
| 346 | } |
| 347 | |
| 348 | static int soc_camera_create_bufs(struct file *file, void *priv, |
| 349 | struct v4l2_create_buffers *create) |
| 350 | { |
| 351 | struct soc_camera_device *icd = file->private_data; |
| 352 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
| 353 | |
| 354 | /* videobuf2 only */ |
| 355 | if (ici->ops->init_videobuf) |
| 356 | return -EINVAL; |
| 357 | else |
| 358 | return vb2_create_bufs(&icd->vb2_vidq, create); |
| 359 | } |
| 360 | |
| 361 | static int soc_camera_prepare_buf(struct file *file, void *priv, |
| 362 | struct v4l2_buffer *b) |
| 363 | { |
| 364 | struct soc_camera_device *icd = file->private_data; |
| 365 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
| 366 | |
| 367 | /* videobuf2 only */ |
| 368 | if (ici->ops->init_videobuf) |
| 369 | return -EINVAL; |
| 370 | else |
| 371 | return vb2_prepare_buf(&icd->vb2_vidq, b); |
| 372 | } |
| 373 | |
| 374 | /* Always entered with .video_lock held */ |
| 375 | static int soc_camera_init_user_formats(struct soc_camera_device *icd) |
| 376 | { |
| 377 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
| 378 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
| 379 | unsigned int i, fmts = 0, raw_fmts = 0; |
| 380 | int ret; |
| 381 | enum v4l2_mbus_pixelcode code; |
| 382 | |
| 383 | while (!v4l2_subdev_call(sd, video, enum_mbus_fmt, raw_fmts, &code)) |
| 384 | raw_fmts++; |
| 385 | |
| 386 | if (!ici->ops->get_formats) |
| 387 | /* |
| 388 | * Fallback mode - the host will have to serve all |
| 389 | * sensor-provided formats one-to-one to the user |
| 390 | */ |
| 391 | fmts = raw_fmts; |
| 392 | else |
| 393 | /* |
| 394 | * First pass - only count formats this host-sensor |
| 395 | * configuration can provide |
| 396 | */ |
| 397 | for (i = 0; i < raw_fmts; i++) { |
| 398 | ret = ici->ops->get_formats(icd, i, NULL); |
| 399 | if (ret < 0) |
| 400 | return ret; |
| 401 | fmts += ret; |
| 402 | } |
| 403 | |
| 404 | if (!fmts) |
| 405 | return -ENXIO; |
| 406 | |
| 407 | icd->user_formats = |
| 408 | vmalloc(fmts * sizeof(struct soc_camera_format_xlate)); |
| 409 | if (!icd->user_formats) |
| 410 | return -ENOMEM; |
| 411 | |
| 412 | dev_dbg(icd->pdev, "Found %d supported formats.\n", fmts); |
| 413 | |
| 414 | /* Second pass - actually fill data formats */ |
| 415 | fmts = 0; |
| 416 | for (i = 0; i < raw_fmts; i++) |
| 417 | if (!ici->ops->get_formats) { |
| 418 | v4l2_subdev_call(sd, video, enum_mbus_fmt, i, &code); |
| 419 | icd->user_formats[fmts].host_fmt = |
| 420 | soc_mbus_get_fmtdesc(code); |
| 421 | if (icd->user_formats[fmts].host_fmt) |
| 422 | icd->user_formats[fmts++].code = code; |
| 423 | } else { |
| 424 | ret = ici->ops->get_formats(icd, i, |
| 425 | &icd->user_formats[fmts]); |
| 426 | if (ret < 0) |
| 427 | goto egfmt; |
| 428 | fmts += ret; |
| 429 | } |
| 430 | |
| 431 | icd->num_user_formats = fmts; |
| 432 | icd->current_fmt = &icd->user_formats[0]; |
| 433 | |
| 434 | return 0; |
| 435 | |
| 436 | egfmt: |
| 437 | vfree(icd->user_formats); |
| 438 | return ret; |
| 439 | } |
| 440 | |
| 441 | /* Always entered with .video_lock held */ |
| 442 | static void soc_camera_free_user_formats(struct soc_camera_device *icd) |
| 443 | { |
| 444 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
| 445 | |
| 446 | if (ici->ops->put_formats) |
| 447 | ici->ops->put_formats(icd); |
| 448 | icd->current_fmt = NULL; |
| 449 | icd->num_user_formats = 0; |
| 450 | vfree(icd->user_formats); |
| 451 | icd->user_formats = NULL; |
| 452 | } |
| 453 | |
| 454 | /* Called with .vb_lock held, or from the first open(2), see comment there */ |
| 455 | static int soc_camera_set_fmt(struct soc_camera_device *icd, |
| 456 | struct v4l2_format *f) |
| 457 | { |
| 458 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
| 459 | struct v4l2_pix_format *pix = &f->fmt.pix; |
| 460 | int ret; |
| 461 | |
| 462 | dev_dbg(icd->pdev, "S_FMT(%c%c%c%c, %ux%u)\n", |
| 463 | pixfmtstr(pix->pixelformat), pix->width, pix->height); |
| 464 | |
| 465 | /* We always call try_fmt() before set_fmt() or set_crop() */ |
| 466 | ret = soc_camera_try_fmt(icd, f); |
| 467 | if (ret < 0) |
| 468 | return ret; |
| 469 | |
| 470 | ret = ici->ops->set_fmt(icd, f); |
| 471 | if (ret < 0) { |
| 472 | return ret; |
| 473 | } else if (!icd->current_fmt || |
| 474 | icd->current_fmt->host_fmt->fourcc != pix->pixelformat) { |
| 475 | dev_err(icd->pdev, |
| 476 | "Host driver hasn't set up current format correctly!\n"); |
| 477 | return -EINVAL; |
| 478 | } |
| 479 | |
| 480 | icd->user_width = pix->width; |
| 481 | icd->user_height = pix->height; |
| 482 | icd->bytesperline = pix->bytesperline; |
| 483 | icd->sizeimage = pix->sizeimage; |
| 484 | icd->colorspace = pix->colorspace; |
| 485 | icd->field = pix->field; |
| 486 | if (ici->ops->init_videobuf) |
| 487 | icd->vb_vidq.field = pix->field; |
| 488 | |
| 489 | dev_dbg(icd->pdev, "set width: %d height: %d\n", |
| 490 | icd->user_width, icd->user_height); |
| 491 | |
| 492 | /* set physical bus parameters */ |
| 493 | return ici->ops->set_bus_param(icd); |
| 494 | } |
| 495 | |
| 496 | static int soc_camera_open(struct file *file) |
| 497 | { |
| 498 | struct video_device *vdev = video_devdata(file); |
| 499 | struct soc_camera_device *icd = dev_get_drvdata(vdev->parent); |
| 500 | struct soc_camera_link *icl = to_soc_camera_link(icd); |
| 501 | struct soc_camera_host *ici; |
| 502 | int ret; |
| 503 | |
| 504 | if (!to_soc_camera_control(icd)) |
| 505 | /* No device driver attached */ |
| 506 | return -ENODEV; |
| 507 | |
| 508 | ici = to_soc_camera_host(icd->parent); |
| 509 | |
| 510 | if (mutex_lock_interruptible(&icd->video_lock)) |
| 511 | return -ERESTARTSYS; |
| 512 | if (!try_module_get(ici->ops->owner)) { |
| 513 | dev_err(icd->pdev, "Couldn't lock capture bus driver.\n"); |
| 514 | ret = -EINVAL; |
| 515 | goto emodule; |
| 516 | } |
| 517 | |
| 518 | icd->use_count++; |
| 519 | |
| 520 | /* Now we really have to activate the camera */ |
| 521 | if (icd->use_count == 1) { |
| 522 | /* Restore parameters before the last close() per V4L2 API */ |
| 523 | struct v4l2_format f = { |
| 524 | .type = V4L2_BUF_TYPE_VIDEO_CAPTURE, |
| 525 | .fmt.pix = { |
| 526 | .width = icd->user_width, |
| 527 | .height = icd->user_height, |
| 528 | .field = icd->field, |
| 529 | .colorspace = icd->colorspace, |
| 530 | .pixelformat = |
| 531 | icd->current_fmt->host_fmt->fourcc, |
| 532 | }, |
| 533 | }; |
| 534 | |
| 535 | /* The camera could have been already on, try to reset */ |
| 536 | if (icl->reset) |
| 537 | icl->reset(icd->pdev); |
| 538 | |
| 539 | /* Don't mess with the host during probe */ |
| 540 | mutex_lock(&ici->host_lock); |
| 541 | ret = ici->ops->add(icd); |
| 542 | mutex_unlock(&ici->host_lock); |
| 543 | if (ret < 0) { |
| 544 | dev_err(icd->pdev, "Couldn't activate the camera: %d\n", ret); |
| 545 | goto eiciadd; |
| 546 | } |
| 547 | |
| 548 | ret = soc_camera_power_on(icd, icl); |
| 549 | if (ret < 0) |
| 550 | goto epower; |
| 551 | |
| 552 | pm_runtime_enable(&icd->vdev->dev); |
| 553 | ret = pm_runtime_resume(&icd->vdev->dev); |
| 554 | if (ret < 0 && ret != -ENOSYS) |
| 555 | goto eresume; |
| 556 | |
| 557 | /* |
| 558 | * Try to configure with default parameters. Notice: this is the |
| 559 | * very first open, so, we cannot race against other calls, |
| 560 | * apart from someone else calling open() simultaneously, but |
| 561 | * .video_lock is protecting us against it. |
| 562 | */ |
| 563 | ret = soc_camera_set_fmt(icd, &f); |
| 564 | if (ret < 0) |
| 565 | goto esfmt; |
| 566 | |
| 567 | if (ici->ops->init_videobuf) { |
| 568 | ici->ops->init_videobuf(&icd->vb_vidq, icd); |
| 569 | } else { |
| 570 | ret = ici->ops->init_videobuf2(&icd->vb2_vidq, icd); |
| 571 | if (ret < 0) |
| 572 | goto einitvb; |
| 573 | } |
| 574 | v4l2_ctrl_handler_setup(&icd->ctrl_handler); |
| 575 | } |
| 576 | mutex_unlock(&icd->video_lock); |
| 577 | |
| 578 | file->private_data = icd; |
| 579 | dev_dbg(icd->pdev, "camera device open\n"); |
| 580 | |
| 581 | return 0; |
| 582 | |
| 583 | /* |
| 584 | * First four errors are entered with the .video_lock held |
| 585 | * and use_count == 1 |
| 586 | */ |
| 587 | einitvb: |
| 588 | esfmt: |
| 589 | pm_runtime_disable(&icd->vdev->dev); |
| 590 | eresume: |
| 591 | soc_camera_power_off(icd, icl); |
| 592 | epower: |
| 593 | ici->ops->remove(icd); |
| 594 | eiciadd: |
| 595 | icd->use_count--; |
| 596 | module_put(ici->ops->owner); |
| 597 | emodule: |
| 598 | mutex_unlock(&icd->video_lock); |
| 599 | |
| 600 | return ret; |
| 601 | } |
| 602 | |
| 603 | static int soc_camera_close(struct file *file) |
| 604 | { |
| 605 | struct soc_camera_device *icd = file->private_data; |
| 606 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
| 607 | |
| 608 | mutex_lock(&icd->video_lock); |
| 609 | icd->use_count--; |
| 610 | if (!icd->use_count) { |
| 611 | struct soc_camera_link *icl = to_soc_camera_link(icd); |
| 612 | |
| 613 | pm_runtime_suspend(&icd->vdev->dev); |
| 614 | pm_runtime_disable(&icd->vdev->dev); |
| 615 | |
| 616 | if (ici->ops->init_videobuf2) |
| 617 | vb2_queue_release(&icd->vb2_vidq); |
| 618 | ici->ops->remove(icd); |
| 619 | |
| 620 | soc_camera_power_off(icd, icl); |
| 621 | } |
| 622 | |
| 623 | if (icd->streamer == file) |
| 624 | icd->streamer = NULL; |
| 625 | mutex_unlock(&icd->video_lock); |
| 626 | |
| 627 | module_put(ici->ops->owner); |
| 628 | |
| 629 | dev_dbg(icd->pdev, "camera device close\n"); |
| 630 | |
| 631 | return 0; |
| 632 | } |
| 633 | |
| 634 | static ssize_t soc_camera_read(struct file *file, char __user *buf, |
| 635 | size_t count, loff_t *ppos) |
| 636 | { |
| 637 | struct soc_camera_device *icd = file->private_data; |
| 638 | int err = -EINVAL; |
| 639 | |
| 640 | dev_err(icd->pdev, "camera device read not implemented\n"); |
| 641 | |
| 642 | return err; |
| 643 | } |
| 644 | |
| 645 | static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma) |
| 646 | { |
| 647 | struct soc_camera_device *icd = file->private_data; |
| 648 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
| 649 | int err; |
| 650 | |
| 651 | dev_dbg(icd->pdev, "mmap called, vma=0x%08lx\n", (unsigned long)vma); |
| 652 | |
| 653 | if (icd->streamer != file) |
| 654 | return -EBUSY; |
| 655 | |
| 656 | if (mutex_lock_interruptible(&icd->video_lock)) |
| 657 | return -ERESTARTSYS; |
| 658 | if (ici->ops->init_videobuf) |
| 659 | err = videobuf_mmap_mapper(&icd->vb_vidq, vma); |
| 660 | else |
| 661 | err = vb2_mmap(&icd->vb2_vidq, vma); |
| 662 | mutex_unlock(&icd->video_lock); |
| 663 | |
| 664 | dev_dbg(icd->pdev, "vma start=0x%08lx, size=%ld, ret=%d\n", |
| 665 | (unsigned long)vma->vm_start, |
| 666 | (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, |
| 667 | err); |
| 668 | |
| 669 | return err; |
| 670 | } |
| 671 | |
| 672 | static unsigned int soc_camera_poll(struct file *file, poll_table *pt) |
| 673 | { |
| 674 | struct soc_camera_device *icd = file->private_data; |
| 675 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
| 676 | unsigned res = POLLERR; |
| 677 | |
| 678 | if (icd->streamer != file) |
| 679 | return POLLERR; |
| 680 | |
| 681 | mutex_lock(&icd->video_lock); |
| 682 | if (ici->ops->init_videobuf && list_empty(&icd->vb_vidq.stream)) |
| 683 | dev_err(icd->pdev, "Trying to poll with no queued buffers!\n"); |
| 684 | else |
| 685 | res = ici->ops->poll(file, pt); |
| 686 | mutex_unlock(&icd->video_lock); |
| 687 | return res; |
| 688 | } |
| 689 | |
| 690 | void soc_camera_lock(struct vb2_queue *vq) |
| 691 | { |
| 692 | struct soc_camera_device *icd = vb2_get_drv_priv(vq); |
| 693 | mutex_lock(&icd->video_lock); |
| 694 | } |
| 695 | EXPORT_SYMBOL(soc_camera_lock); |
| 696 | |
| 697 | void soc_camera_unlock(struct vb2_queue *vq) |
| 698 | { |
| 699 | struct soc_camera_device *icd = vb2_get_drv_priv(vq); |
| 700 | mutex_unlock(&icd->video_lock); |
| 701 | } |
| 702 | EXPORT_SYMBOL(soc_camera_unlock); |
| 703 | |
| 704 | static struct v4l2_file_operations soc_camera_fops = { |
| 705 | .owner = THIS_MODULE, |
| 706 | .open = soc_camera_open, |
| 707 | .release = soc_camera_close, |
| 708 | .unlocked_ioctl = video_ioctl2, |
| 709 | .read = soc_camera_read, |
| 710 | .mmap = soc_camera_mmap, |
| 711 | .poll = soc_camera_poll, |
| 712 | }; |
| 713 | |
| 714 | static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv, |
| 715 | struct v4l2_format *f) |
| 716 | { |
| 717 | struct soc_camera_device *icd = file->private_data; |
| 718 | int ret; |
| 719 | |
| 720 | WARN_ON(priv != file->private_data); |
| 721 | |
| 722 | if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) { |
| 723 | dev_warn(icd->pdev, "Wrong buf-type %d\n", f->type); |
| 724 | return -EINVAL; |
| 725 | } |
| 726 | |
| 727 | if (icd->streamer && icd->streamer != file) |
| 728 | return -EBUSY; |
| 729 | |
| 730 | if (is_streaming(to_soc_camera_host(icd->parent), icd)) { |
| 731 | dev_err(icd->pdev, "S_FMT denied: queue initialised\n"); |
| 732 | return -EBUSY; |
| 733 | } |
| 734 | |
| 735 | ret = soc_camera_set_fmt(icd, f); |
| 736 | |
| 737 | if (!ret && !icd->streamer) |
| 738 | icd->streamer = file; |
| 739 | |
| 740 | return ret; |
| 741 | } |
| 742 | |
| 743 | static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv, |
| 744 | struct v4l2_fmtdesc *f) |
| 745 | { |
| 746 | struct soc_camera_device *icd = file->private_data; |
| 747 | const struct soc_mbus_pixelfmt *format; |
| 748 | |
| 749 | WARN_ON(priv != file->private_data); |
| 750 | |
| 751 | if (f->index >= icd->num_user_formats) |
| 752 | return -EINVAL; |
| 753 | |
| 754 | format = icd->user_formats[f->index].host_fmt; |
| 755 | |
| 756 | if (format->name) |
| 757 | strlcpy(f->description, format->name, sizeof(f->description)); |
| 758 | f->pixelformat = format->fourcc; |
| 759 | return 0; |
| 760 | } |
| 761 | |
| 762 | static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv, |
| 763 | struct v4l2_format *f) |
| 764 | { |
| 765 | struct soc_camera_device *icd = file->private_data; |
| 766 | struct v4l2_pix_format *pix = &f->fmt.pix; |
| 767 | |
| 768 | WARN_ON(priv != file->private_data); |
| 769 | |
| 770 | if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 771 | return -EINVAL; |
| 772 | |
| 773 | pix->width = icd->user_width; |
| 774 | pix->height = icd->user_height; |
| 775 | pix->bytesperline = icd->bytesperline; |
| 776 | pix->sizeimage = icd->sizeimage; |
| 777 | pix->field = icd->field; |
| 778 | pix->pixelformat = icd->current_fmt->host_fmt->fourcc; |
| 779 | pix->colorspace = icd->colorspace; |
| 780 | dev_dbg(icd->pdev, "current_fmt->fourcc: 0x%08x\n", |
| 781 | icd->current_fmt->host_fmt->fourcc); |
| 782 | return 0; |
| 783 | } |
| 784 | |
| 785 | static int soc_camera_querycap(struct file *file, void *priv, |
| 786 | struct v4l2_capability *cap) |
| 787 | { |
| 788 | struct soc_camera_device *icd = file->private_data; |
| 789 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
| 790 | |
| 791 | WARN_ON(priv != file->private_data); |
| 792 | |
| 793 | strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver)); |
| 794 | return ici->ops->querycap(ici, cap); |
| 795 | } |
| 796 | |
| 797 | static int soc_camera_streamon(struct file *file, void *priv, |
| 798 | enum v4l2_buf_type i) |
| 799 | { |
| 800 | struct soc_camera_device *icd = file->private_data; |
| 801 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
| 802 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
| 803 | int ret; |
| 804 | |
| 805 | WARN_ON(priv != file->private_data); |
| 806 | |
| 807 | if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 808 | return -EINVAL; |
| 809 | |
| 810 | if (icd->streamer != file) |
| 811 | return -EBUSY; |
| 812 | |
| 813 | /* This calls buf_queue from host driver's videobuf_queue_ops */ |
| 814 | if (ici->ops->init_videobuf) |
| 815 | ret = videobuf_streamon(&icd->vb_vidq); |
| 816 | else |
| 817 | ret = vb2_streamon(&icd->vb2_vidq, i); |
| 818 | |
| 819 | if (!ret) |
| 820 | v4l2_subdev_call(sd, video, s_stream, 1); |
| 821 | |
| 822 | return ret; |
| 823 | } |
| 824 | |
| 825 | static int soc_camera_streamoff(struct file *file, void *priv, |
| 826 | enum v4l2_buf_type i) |
| 827 | { |
| 828 | struct soc_camera_device *icd = file->private_data; |
| 829 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
| 830 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
| 831 | |
| 832 | WARN_ON(priv != file->private_data); |
| 833 | |
| 834 | if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 835 | return -EINVAL; |
| 836 | |
| 837 | if (icd->streamer != file) |
| 838 | return -EBUSY; |
| 839 | |
| 840 | /* |
| 841 | * This calls buf_release from host driver's videobuf_queue_ops for all |
| 842 | * remaining buffers. When the last buffer is freed, stop capture |
| 843 | */ |
| 844 | if (ici->ops->init_videobuf) |
| 845 | videobuf_streamoff(&icd->vb_vidq); |
| 846 | else |
| 847 | vb2_streamoff(&icd->vb2_vidq, i); |
| 848 | |
| 849 | v4l2_subdev_call(sd, video, s_stream, 0); |
| 850 | |
| 851 | return 0; |
| 852 | } |
| 853 | |
| 854 | static int soc_camera_cropcap(struct file *file, void *fh, |
| 855 | struct v4l2_cropcap *a) |
| 856 | { |
| 857 | struct soc_camera_device *icd = file->private_data; |
| 858 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
| 859 | |
| 860 | return ici->ops->cropcap(icd, a); |
| 861 | } |
| 862 | |
| 863 | static int soc_camera_g_crop(struct file *file, void *fh, |
| 864 | struct v4l2_crop *a) |
| 865 | { |
| 866 | struct soc_camera_device *icd = file->private_data; |
| 867 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
| 868 | int ret; |
| 869 | |
| 870 | ret = ici->ops->get_crop(icd, a); |
| 871 | |
| 872 | return ret; |
| 873 | } |
| 874 | |
| 875 | /* |
| 876 | * According to the V4L2 API, drivers shall not update the struct v4l2_crop |
| 877 | * argument with the actual geometry, instead, the user shall use G_CROP to |
| 878 | * retrieve it. |
| 879 | */ |
| 880 | static int soc_camera_s_crop(struct file *file, void *fh, |
| 881 | struct v4l2_crop *a) |
| 882 | { |
| 883 | struct soc_camera_device *icd = file->private_data; |
| 884 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
| 885 | struct v4l2_rect *rect = &a->c; |
| 886 | struct v4l2_crop current_crop; |
| 887 | int ret; |
| 888 | |
| 889 | if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 890 | return -EINVAL; |
| 891 | |
| 892 | dev_dbg(icd->pdev, "S_CROP(%ux%u@%u:%u)\n", |
| 893 | rect->width, rect->height, rect->left, rect->top); |
| 894 | |
| 895 | /* If get_crop fails, we'll let host and / or client drivers decide */ |
| 896 | ret = ici->ops->get_crop(icd, ¤t_crop); |
| 897 | |
| 898 | /* Prohibit window size change with initialised buffers */ |
| 899 | if (ret < 0) { |
| 900 | dev_err(icd->pdev, |
| 901 | "S_CROP denied: getting current crop failed\n"); |
| 902 | } else if ((a->c.width == current_crop.c.width && |
| 903 | a->c.height == current_crop.c.height) || |
| 904 | !is_streaming(ici, icd)) { |
| 905 | /* same size or not streaming - use .set_crop() */ |
| 906 | ret = ici->ops->set_crop(icd, a); |
| 907 | } else if (ici->ops->set_livecrop) { |
| 908 | ret = ici->ops->set_livecrop(icd, a); |
| 909 | } else { |
| 910 | dev_err(icd->pdev, |
| 911 | "S_CROP denied: queue initialised and sizes differ\n"); |
| 912 | ret = -EBUSY; |
| 913 | } |
| 914 | |
| 915 | return ret; |
| 916 | } |
| 917 | |
| 918 | static int soc_camera_g_parm(struct file *file, void *fh, |
| 919 | struct v4l2_streamparm *a) |
| 920 | { |
| 921 | struct soc_camera_device *icd = file->private_data; |
| 922 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
| 923 | |
| 924 | if (ici->ops->get_parm) |
| 925 | return ici->ops->get_parm(icd, a); |
| 926 | |
| 927 | return -ENOIOCTLCMD; |
| 928 | } |
| 929 | |
| 930 | static int soc_camera_s_parm(struct file *file, void *fh, |
| 931 | struct v4l2_streamparm *a) |
| 932 | { |
| 933 | struct soc_camera_device *icd = file->private_data; |
| 934 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
| 935 | |
| 936 | if (ici->ops->set_parm) |
| 937 | return ici->ops->set_parm(icd, a); |
| 938 | |
| 939 | return -ENOIOCTLCMD; |
| 940 | } |
| 941 | |
| 942 | static int soc_camera_g_chip_ident(struct file *file, void *fh, |
| 943 | struct v4l2_dbg_chip_ident *id) |
| 944 | { |
| 945 | struct soc_camera_device *icd = file->private_data; |
| 946 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
| 947 | |
| 948 | return v4l2_subdev_call(sd, core, g_chip_ident, id); |
| 949 | } |
| 950 | |
| 951 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 952 | static int soc_camera_g_register(struct file *file, void *fh, |
| 953 | struct v4l2_dbg_register *reg) |
| 954 | { |
| 955 | struct soc_camera_device *icd = file->private_data; |
| 956 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
| 957 | |
| 958 | return v4l2_subdev_call(sd, core, g_register, reg); |
| 959 | } |
| 960 | |
| 961 | static int soc_camera_s_register(struct file *file, void *fh, |
| 962 | struct v4l2_dbg_register *reg) |
| 963 | { |
| 964 | struct soc_camera_device *icd = file->private_data; |
| 965 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
| 966 | |
| 967 | return v4l2_subdev_call(sd, core, s_register, reg); |
| 968 | } |
| 969 | #endif |
| 970 | |
| 971 | static int soc_camera_probe(struct soc_camera_device *icd); |
| 972 | |
| 973 | /* So far this function cannot fail */ |
| 974 | static void scan_add_host(struct soc_camera_host *ici) |
| 975 | { |
| 976 | struct soc_camera_device *icd; |
| 977 | |
| 978 | mutex_lock(&ici->host_lock); |
| 979 | |
| 980 | list_for_each_entry(icd, &devices, list) { |
| 981 | if (icd->iface == ici->nr) { |
| 982 | int ret; |
| 983 | |
| 984 | icd->parent = ici->v4l2_dev.dev; |
| 985 | ret = soc_camera_probe(icd); |
| 986 | } |
| 987 | } |
| 988 | |
| 989 | mutex_unlock(&ici->host_lock); |
| 990 | } |
| 991 | |
| 992 | #ifdef CONFIG_I2C_BOARDINFO |
| 993 | static int soc_camera_init_i2c(struct soc_camera_device *icd, |
| 994 | struct soc_camera_link *icl) |
| 995 | { |
| 996 | struct i2c_client *client; |
| 997 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
| 998 | struct i2c_adapter *adap = i2c_get_adapter(icl->i2c_adapter_id); |
| 999 | struct v4l2_subdev *subdev; |
| 1000 | |
| 1001 | if (!adap) { |
| 1002 | dev_err(icd->pdev, "Cannot get I2C adapter #%d. No driver?\n", |
| 1003 | icl->i2c_adapter_id); |
| 1004 | goto ei2cga; |
| 1005 | } |
| 1006 | |
| 1007 | icl->board_info->platform_data = icl; |
| 1008 | |
| 1009 | subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap, |
| 1010 | icl->board_info, NULL); |
| 1011 | if (!subdev) |
| 1012 | goto ei2cnd; |
| 1013 | |
| 1014 | client = v4l2_get_subdevdata(subdev); |
| 1015 | |
| 1016 | /* Use to_i2c_client(dev) to recover the i2c client */ |
| 1017 | icd->control = &client->dev; |
| 1018 | |
| 1019 | return 0; |
| 1020 | ei2cnd: |
| 1021 | i2c_put_adapter(adap); |
| 1022 | ei2cga: |
| 1023 | return -ENODEV; |
| 1024 | } |
| 1025 | |
| 1026 | static void soc_camera_free_i2c(struct soc_camera_device *icd) |
| 1027 | { |
| 1028 | struct i2c_client *client = |
| 1029 | to_i2c_client(to_soc_camera_control(icd)); |
| 1030 | struct i2c_adapter *adap = client->adapter; |
| 1031 | |
| 1032 | icd->control = NULL; |
| 1033 | v4l2_device_unregister_subdev(i2c_get_clientdata(client)); |
| 1034 | i2c_unregister_device(client); |
| 1035 | i2c_put_adapter(adap); |
| 1036 | } |
| 1037 | #else |
| 1038 | #define soc_camera_init_i2c(icd, icl) (-ENODEV) |
| 1039 | #define soc_camera_free_i2c(icd) do {} while (0) |
| 1040 | #endif |
| 1041 | |
| 1042 | static int soc_camera_video_start(struct soc_camera_device *icd); |
| 1043 | static int video_dev_create(struct soc_camera_device *icd); |
| 1044 | /* Called during host-driver probe */ |
| 1045 | static int soc_camera_probe(struct soc_camera_device *icd) |
| 1046 | { |
| 1047 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
| 1048 | struct soc_camera_link *icl = to_soc_camera_link(icd); |
| 1049 | struct device *control = NULL; |
| 1050 | struct v4l2_subdev *sd; |
| 1051 | struct v4l2_mbus_framefmt mf; |
| 1052 | int ret; |
| 1053 | |
| 1054 | dev_info(icd->pdev, "Probing %s\n", dev_name(icd->pdev)); |
| 1055 | |
| 1056 | /* |
| 1057 | * Currently the subdev with the largest number of controls (13) is |
| 1058 | * ov6550. So let's pick 16 as a hint for the control handler. Note |
| 1059 | * that this is a hint only: too large and you waste some memory, too |
| 1060 | * small and there is a (very) small performance hit when looking up |
| 1061 | * controls in the internal hash. |
| 1062 | */ |
| 1063 | ret = v4l2_ctrl_handler_init(&icd->ctrl_handler, 16); |
| 1064 | if (ret < 0) |
| 1065 | return ret; |
| 1066 | |
| 1067 | ret = regulator_bulk_get(icd->pdev, icl->num_regulators, |
| 1068 | icl->regulators); |
| 1069 | if (ret < 0) |
| 1070 | goto ereg; |
| 1071 | |
| 1072 | /* The camera could have been already on, try to reset */ |
| 1073 | if (icl->reset) |
| 1074 | icl->reset(icd->pdev); |
| 1075 | |
| 1076 | ret = ici->ops->add(icd); |
| 1077 | if (ret < 0) |
| 1078 | goto eadd; |
| 1079 | |
| 1080 | /* |
| 1081 | * This will not yet call v4l2_subdev_core_ops::s_power(1), because the |
| 1082 | * subdevice has not been initialised yet. We'll have to call it once |
| 1083 | * again after initialisation, even though it shouldn't be needed, we |
| 1084 | * don't do any IO here. |
| 1085 | */ |
| 1086 | ret = soc_camera_power_on(icd, icl); |
| 1087 | if (ret < 0) |
| 1088 | goto epower; |
| 1089 | |
| 1090 | /* Must have icd->vdev before registering the device */ |
| 1091 | ret = video_dev_create(icd); |
| 1092 | if (ret < 0) |
| 1093 | goto evdc; |
| 1094 | |
| 1095 | /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */ |
| 1096 | if (icl->board_info) { |
| 1097 | ret = soc_camera_init_i2c(icd, icl); |
| 1098 | if (ret < 0) |
| 1099 | goto eadddev; |
| 1100 | } else if (!icl->add_device || !icl->del_device) { |
| 1101 | ret = -EINVAL; |
| 1102 | goto eadddev; |
| 1103 | } else { |
| 1104 | if (icl->module_name) |
| 1105 | ret = request_module(icl->module_name); |
| 1106 | |
| 1107 | ret = icl->add_device(icd); |
| 1108 | if (ret < 0) |
| 1109 | goto eadddev; |
| 1110 | |
| 1111 | /* |
| 1112 | * FIXME: this is racy, have to use driver-binding notification, |
| 1113 | * when it is available |
| 1114 | */ |
| 1115 | control = to_soc_camera_control(icd); |
| 1116 | if (!control || !control->driver || !dev_get_drvdata(control) || |
| 1117 | !try_module_get(control->driver->owner)) { |
| 1118 | icl->del_device(icd); |
| 1119 | ret = -ENODEV; |
| 1120 | goto enodrv; |
| 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | sd = soc_camera_to_subdev(icd); |
| 1125 | sd->grp_id = soc_camera_grp_id(icd); |
| 1126 | v4l2_set_subdev_hostdata(sd, icd); |
| 1127 | |
| 1128 | if (v4l2_ctrl_add_handler(&icd->ctrl_handler, sd->ctrl_handler)) |
| 1129 | goto ectrl; |
| 1130 | |
| 1131 | /* At this point client .probe() should have run already */ |
| 1132 | ret = soc_camera_init_user_formats(icd); |
| 1133 | if (ret < 0) |
| 1134 | goto eiufmt; |
| 1135 | |
| 1136 | icd->field = V4L2_FIELD_ANY; |
| 1137 | |
| 1138 | /* |
| 1139 | * ..._video_start() will create a device node, video_register_device() |
| 1140 | * itself is protected against concurrent open() calls, but we also have |
| 1141 | * to protect our data. |
| 1142 | */ |
| 1143 | mutex_lock(&icd->video_lock); |
| 1144 | |
| 1145 | ret = soc_camera_video_start(icd); |
| 1146 | if (ret < 0) |
| 1147 | goto evidstart; |
| 1148 | |
| 1149 | /* Try to improve our guess of a reasonable window format */ |
| 1150 | if (!v4l2_subdev_call(sd, video, g_mbus_fmt, &mf)) { |
| 1151 | icd->user_width = mf.width; |
| 1152 | icd->user_height = mf.height; |
| 1153 | icd->colorspace = mf.colorspace; |
| 1154 | icd->field = mf.field; |
| 1155 | } |
| 1156 | |
| 1157 | ici->ops->remove(icd); |
| 1158 | |
| 1159 | soc_camera_power_off(icd, icl); |
| 1160 | |
| 1161 | mutex_unlock(&icd->video_lock); |
| 1162 | |
| 1163 | return 0; |
| 1164 | |
| 1165 | evidstart: |
| 1166 | mutex_unlock(&icd->video_lock); |
| 1167 | soc_camera_free_user_formats(icd); |
| 1168 | eiufmt: |
| 1169 | ectrl: |
| 1170 | if (icl->board_info) { |
| 1171 | soc_camera_free_i2c(icd); |
| 1172 | } else { |
| 1173 | icl->del_device(icd); |
| 1174 | module_put(control->driver->owner); |
| 1175 | } |
| 1176 | enodrv: |
| 1177 | eadddev: |
| 1178 | video_device_release(icd->vdev); |
| 1179 | icd->vdev = NULL; |
| 1180 | evdc: |
| 1181 | soc_camera_power_off(icd, icl); |
| 1182 | epower: |
| 1183 | ici->ops->remove(icd); |
| 1184 | eadd: |
| 1185 | regulator_bulk_free(icl->num_regulators, icl->regulators); |
| 1186 | ereg: |
| 1187 | v4l2_ctrl_handler_free(&icd->ctrl_handler); |
| 1188 | return ret; |
| 1189 | } |
| 1190 | |
| 1191 | /* |
| 1192 | * This is called on device_unregister, which only means we have to disconnect |
| 1193 | * from the host, but not remove ourselves from the device list |
| 1194 | */ |
| 1195 | static int soc_camera_remove(struct soc_camera_device *icd) |
| 1196 | { |
| 1197 | struct soc_camera_link *icl = to_soc_camera_link(icd); |
| 1198 | struct video_device *vdev = icd->vdev; |
| 1199 | |
| 1200 | BUG_ON(!icd->parent); |
| 1201 | |
| 1202 | v4l2_ctrl_handler_free(&icd->ctrl_handler); |
| 1203 | if (vdev) { |
| 1204 | video_unregister_device(vdev); |
| 1205 | icd->vdev = NULL; |
| 1206 | } |
| 1207 | |
| 1208 | if (icl->board_info) { |
| 1209 | soc_camera_free_i2c(icd); |
| 1210 | } else { |
| 1211 | struct device_driver *drv = to_soc_camera_control(icd)->driver; |
| 1212 | if (drv) { |
| 1213 | icl->del_device(icd); |
| 1214 | module_put(drv->owner); |
| 1215 | } |
| 1216 | } |
| 1217 | soc_camera_free_user_formats(icd); |
| 1218 | |
| 1219 | regulator_bulk_free(icl->num_regulators, icl->regulators); |
| 1220 | |
| 1221 | return 0; |
| 1222 | } |
| 1223 | |
| 1224 | static int default_cropcap(struct soc_camera_device *icd, |
| 1225 | struct v4l2_cropcap *a) |
| 1226 | { |
| 1227 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
| 1228 | return v4l2_subdev_call(sd, video, cropcap, a); |
| 1229 | } |
| 1230 | |
| 1231 | static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a) |
| 1232 | { |
| 1233 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
| 1234 | return v4l2_subdev_call(sd, video, g_crop, a); |
| 1235 | } |
| 1236 | |
| 1237 | static int default_s_crop(struct soc_camera_device *icd, struct v4l2_crop *a) |
| 1238 | { |
| 1239 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
| 1240 | return v4l2_subdev_call(sd, video, s_crop, a); |
| 1241 | } |
| 1242 | |
| 1243 | static int default_g_parm(struct soc_camera_device *icd, |
| 1244 | struct v4l2_streamparm *parm) |
| 1245 | { |
| 1246 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
| 1247 | return v4l2_subdev_call(sd, video, g_parm, parm); |
| 1248 | } |
| 1249 | |
| 1250 | static int default_s_parm(struct soc_camera_device *icd, |
| 1251 | struct v4l2_streamparm *parm) |
| 1252 | { |
| 1253 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
| 1254 | return v4l2_subdev_call(sd, video, s_parm, parm); |
| 1255 | } |
| 1256 | |
| 1257 | static int default_enum_framesizes(struct soc_camera_device *icd, |
| 1258 | struct v4l2_frmsizeenum *fsize) |
| 1259 | { |
| 1260 | int ret; |
| 1261 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
| 1262 | const struct soc_camera_format_xlate *xlate; |
| 1263 | __u32 pixfmt = fsize->pixel_format; |
| 1264 | struct v4l2_frmsizeenum fsize_mbus = *fsize; |
| 1265 | |
| 1266 | xlate = soc_camera_xlate_by_fourcc(icd, pixfmt); |
| 1267 | if (!xlate) |
| 1268 | return -EINVAL; |
| 1269 | /* map xlate-code to pixel_format, sensor only handle xlate-code*/ |
| 1270 | fsize_mbus.pixel_format = xlate->code; |
| 1271 | |
| 1272 | ret = v4l2_subdev_call(sd, video, enum_framesizes, &fsize_mbus); |
| 1273 | if (ret < 0) |
| 1274 | return ret; |
| 1275 | |
| 1276 | *fsize = fsize_mbus; |
| 1277 | fsize->pixel_format = pixfmt; |
| 1278 | |
| 1279 | return 0; |
| 1280 | } |
| 1281 | |
| 1282 | int soc_camera_host_register(struct soc_camera_host *ici) |
| 1283 | { |
| 1284 | struct soc_camera_host *ix; |
| 1285 | int ret; |
| 1286 | |
| 1287 | if (!ici || !ici->ops || |
| 1288 | !ici->ops->try_fmt || |
| 1289 | !ici->ops->set_fmt || |
| 1290 | !ici->ops->set_bus_param || |
| 1291 | !ici->ops->querycap || |
| 1292 | ((!ici->ops->init_videobuf || |
| 1293 | !ici->ops->reqbufs) && |
| 1294 | !ici->ops->init_videobuf2) || |
| 1295 | !ici->ops->add || |
| 1296 | !ici->ops->remove || |
| 1297 | !ici->ops->poll || |
| 1298 | !ici->v4l2_dev.dev) |
| 1299 | return -EINVAL; |
| 1300 | |
| 1301 | if (!ici->ops->set_crop) |
| 1302 | ici->ops->set_crop = default_s_crop; |
| 1303 | if (!ici->ops->get_crop) |
| 1304 | ici->ops->get_crop = default_g_crop; |
| 1305 | if (!ici->ops->cropcap) |
| 1306 | ici->ops->cropcap = default_cropcap; |
| 1307 | if (!ici->ops->set_parm) |
| 1308 | ici->ops->set_parm = default_s_parm; |
| 1309 | if (!ici->ops->get_parm) |
| 1310 | ici->ops->get_parm = default_g_parm; |
| 1311 | if (!ici->ops->enum_framesizes) |
| 1312 | ici->ops->enum_framesizes = default_enum_framesizes; |
| 1313 | |
| 1314 | mutex_lock(&list_lock); |
| 1315 | list_for_each_entry(ix, &hosts, list) { |
| 1316 | if (ix->nr == ici->nr) { |
| 1317 | ret = -EBUSY; |
| 1318 | goto edevreg; |
| 1319 | } |
| 1320 | } |
| 1321 | |
| 1322 | ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev); |
| 1323 | if (ret < 0) |
| 1324 | goto edevreg; |
| 1325 | |
| 1326 | list_add_tail(&ici->list, &hosts); |
| 1327 | mutex_unlock(&list_lock); |
| 1328 | |
| 1329 | mutex_init(&ici->host_lock); |
| 1330 | scan_add_host(ici); |
| 1331 | |
| 1332 | return 0; |
| 1333 | |
| 1334 | edevreg: |
| 1335 | mutex_unlock(&list_lock); |
| 1336 | return ret; |
| 1337 | } |
| 1338 | EXPORT_SYMBOL(soc_camera_host_register); |
| 1339 | |
| 1340 | /* Unregister all clients! */ |
| 1341 | void soc_camera_host_unregister(struct soc_camera_host *ici) |
| 1342 | { |
| 1343 | struct soc_camera_device *icd; |
| 1344 | |
| 1345 | mutex_lock(&list_lock); |
| 1346 | |
| 1347 | list_del(&ici->list); |
| 1348 | list_for_each_entry(icd, &devices, list) |
| 1349 | if (icd->iface == ici->nr && to_soc_camera_control(icd)) |
| 1350 | soc_camera_remove(icd); |
| 1351 | |
| 1352 | mutex_unlock(&list_lock); |
| 1353 | |
| 1354 | v4l2_device_unregister(&ici->v4l2_dev); |
| 1355 | } |
| 1356 | EXPORT_SYMBOL(soc_camera_host_unregister); |
| 1357 | |
| 1358 | /* Image capture device */ |
| 1359 | static int soc_camera_device_register(struct soc_camera_device *icd) |
| 1360 | { |
| 1361 | struct soc_camera_device *ix; |
| 1362 | int num = -1, i; |
| 1363 | |
| 1364 | for (i = 0; i < 256 && num < 0; i++) { |
| 1365 | num = i; |
| 1366 | /* Check if this index is available on this interface */ |
| 1367 | list_for_each_entry(ix, &devices, list) { |
| 1368 | if (ix->iface == icd->iface && ix->devnum == i) { |
| 1369 | num = -1; |
| 1370 | break; |
| 1371 | } |
| 1372 | } |
| 1373 | } |
| 1374 | |
| 1375 | if (num < 0) |
| 1376 | /* |
| 1377 | * ok, we have 256 cameras on this host... |
| 1378 | * man, stay reasonable... |
| 1379 | */ |
| 1380 | return -ENOMEM; |
| 1381 | |
| 1382 | icd->devnum = num; |
| 1383 | icd->use_count = 0; |
| 1384 | icd->host_priv = NULL; |
| 1385 | mutex_init(&icd->video_lock); |
| 1386 | |
| 1387 | list_add_tail(&icd->list, &devices); |
| 1388 | |
| 1389 | return 0; |
| 1390 | } |
| 1391 | |
| 1392 | static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = { |
| 1393 | .vidioc_querycap = soc_camera_querycap, |
| 1394 | .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap, |
| 1395 | .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap, |
| 1396 | .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap, |
| 1397 | .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap, |
| 1398 | .vidioc_enum_input = soc_camera_enum_input, |
| 1399 | .vidioc_g_input = soc_camera_g_input, |
| 1400 | .vidioc_s_input = soc_camera_s_input, |
| 1401 | .vidioc_s_std = soc_camera_s_std, |
| 1402 | .vidioc_g_std = soc_camera_g_std, |
| 1403 | .vidioc_enum_framesizes = soc_camera_enum_framesizes, |
| 1404 | .vidioc_reqbufs = soc_camera_reqbufs, |
| 1405 | .vidioc_querybuf = soc_camera_querybuf, |
| 1406 | .vidioc_qbuf = soc_camera_qbuf, |
| 1407 | .vidioc_dqbuf = soc_camera_dqbuf, |
| 1408 | .vidioc_create_bufs = soc_camera_create_bufs, |
| 1409 | .vidioc_prepare_buf = soc_camera_prepare_buf, |
| 1410 | .vidioc_streamon = soc_camera_streamon, |
| 1411 | .vidioc_streamoff = soc_camera_streamoff, |
| 1412 | .vidioc_cropcap = soc_camera_cropcap, |
| 1413 | .vidioc_g_crop = soc_camera_g_crop, |
| 1414 | .vidioc_s_crop = soc_camera_s_crop, |
| 1415 | .vidioc_g_parm = soc_camera_g_parm, |
| 1416 | .vidioc_s_parm = soc_camera_s_parm, |
| 1417 | .vidioc_g_chip_ident = soc_camera_g_chip_ident, |
| 1418 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 1419 | .vidioc_g_register = soc_camera_g_register, |
| 1420 | .vidioc_s_register = soc_camera_s_register, |
| 1421 | #endif |
| 1422 | }; |
| 1423 | |
| 1424 | static int video_dev_create(struct soc_camera_device *icd) |
| 1425 | { |
| 1426 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
| 1427 | struct video_device *vdev = video_device_alloc(); |
| 1428 | |
| 1429 | if (!vdev) |
| 1430 | return -ENOMEM; |
| 1431 | |
| 1432 | strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name)); |
| 1433 | |
| 1434 | vdev->parent = icd->pdev; |
| 1435 | vdev->current_norm = V4L2_STD_UNKNOWN; |
| 1436 | vdev->fops = &soc_camera_fops; |
| 1437 | vdev->ioctl_ops = &soc_camera_ioctl_ops; |
| 1438 | vdev->release = video_device_release; |
| 1439 | vdev->tvnorms = V4L2_STD_UNKNOWN; |
| 1440 | vdev->ctrl_handler = &icd->ctrl_handler; |
| 1441 | vdev->lock = &icd->video_lock; |
| 1442 | |
| 1443 | icd->vdev = vdev; |
| 1444 | |
| 1445 | return 0; |
| 1446 | } |
| 1447 | |
| 1448 | /* |
| 1449 | * Called from soc_camera_probe() above (with .video_lock held???) |
| 1450 | */ |
| 1451 | static int soc_camera_video_start(struct soc_camera_device *icd) |
| 1452 | { |
| 1453 | const struct device_type *type = icd->vdev->dev.type; |
| 1454 | int ret; |
| 1455 | |
| 1456 | if (!icd->parent) |
| 1457 | return -ENODEV; |
| 1458 | |
| 1459 | ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1); |
| 1460 | if (ret < 0) { |
| 1461 | dev_err(icd->pdev, "video_register_device failed: %d\n", ret); |
| 1462 | return ret; |
| 1463 | } |
| 1464 | |
| 1465 | /* Restore device type, possibly set by the subdevice driver */ |
| 1466 | icd->vdev->dev.type = type; |
| 1467 | |
| 1468 | return 0; |
| 1469 | } |
| 1470 | |
| 1471 | static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev) |
| 1472 | { |
| 1473 | struct soc_camera_link *icl = pdev->dev.platform_data; |
| 1474 | struct soc_camera_device *icd; |
| 1475 | int ret; |
| 1476 | |
| 1477 | if (!icl) |
| 1478 | return -EINVAL; |
| 1479 | |
| 1480 | icd = kzalloc(sizeof(*icd), GFP_KERNEL); |
| 1481 | if (!icd) |
| 1482 | return -ENOMEM; |
| 1483 | |
| 1484 | icd->iface = icl->bus_id; |
| 1485 | icd->link = icl; |
| 1486 | icd->pdev = &pdev->dev; |
| 1487 | platform_set_drvdata(pdev, icd); |
| 1488 | |
| 1489 | ret = soc_camera_device_register(icd); |
| 1490 | if (ret < 0) |
| 1491 | goto escdevreg; |
| 1492 | |
| 1493 | icd->user_width = DEFAULT_WIDTH; |
| 1494 | icd->user_height = DEFAULT_HEIGHT; |
| 1495 | |
| 1496 | return 0; |
| 1497 | |
| 1498 | escdevreg: |
| 1499 | kfree(icd); |
| 1500 | |
| 1501 | return ret; |
| 1502 | } |
| 1503 | |
| 1504 | /* |
| 1505 | * Only called on rmmod for each platform device, since they are not |
| 1506 | * hot-pluggable. Now we know, that all our users - hosts and devices have |
| 1507 | * been unloaded already |
| 1508 | */ |
| 1509 | static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev) |
| 1510 | { |
| 1511 | struct soc_camera_device *icd = platform_get_drvdata(pdev); |
| 1512 | |
| 1513 | if (!icd) |
| 1514 | return -EINVAL; |
| 1515 | |
| 1516 | list_del(&icd->list); |
| 1517 | |
| 1518 | kfree(icd); |
| 1519 | |
| 1520 | return 0; |
| 1521 | } |
| 1522 | |
| 1523 | static struct platform_driver __refdata soc_camera_pdrv = { |
| 1524 | .probe = soc_camera_pdrv_probe, |
| 1525 | .remove = __devexit_p(soc_camera_pdrv_remove), |
| 1526 | .driver = { |
| 1527 | .name = "soc-camera-pdrv", |
| 1528 | .owner = THIS_MODULE, |
| 1529 | }, |
| 1530 | }; |
| 1531 | |
| 1532 | static int __init soc_camera_init(void) |
| 1533 | { |
| 1534 | return platform_driver_register(&soc_camera_pdrv); |
| 1535 | } |
| 1536 | |
| 1537 | static void __exit soc_camera_exit(void) |
| 1538 | { |
| 1539 | platform_driver_unregister(&soc_camera_pdrv); |
| 1540 | } |
| 1541 | |
| 1542 | module_init(soc_camera_init); |
| 1543 | module_exit(soc_camera_exit); |
| 1544 | |
| 1545 | MODULE_DESCRIPTION("Image capture bus driver"); |
| 1546 | MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>"); |
| 1547 | MODULE_LICENSE("GPL"); |
| 1548 | MODULE_ALIAS("platform:soc-camera-pdrv"); |