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/module.h> 25#include <linux/mutex.h> 26#include <linux/platform_device.h> 27#include <linux/pm_runtime.h> 28#include <linux/regulator/consumer.h> 29#include <linux/slab.h> 30#include <linux/vmalloc.h> 31 32#include <media/soc_camera.h> 33#include <media/soc_mediabus.h> 34#include <media/v4l2-async.h> 35#include <media/v4l2-clk.h> 36#include <media/v4l2-common.h> 37#include <media/v4l2-ioctl.h> 38#include <media/v4l2-dev.h> 39#include <media/v4l2-of.h> 40#include <media/videobuf-core.h> 41#include <media/videobuf2-core.h> 42 43/* Default to VGA resolution */ 44#define DEFAULT_WIDTH 640 45#define DEFAULT_HEIGHT 480 46 47#define is_streaming(ici, icd) \ 48 (((ici)->ops->init_videobuf) ? \ 49 (icd)->vb_vidq.streaming : \ 50 vb2_is_streaming(&(icd)->vb2_vidq)) 51 52#define MAP_MAX_NUM 32 53static DECLARE_BITMAP(device_map, MAP_MAX_NUM); 54static LIST_HEAD(hosts); 55static LIST_HEAD(devices); 56/* 57 * Protects lists and bitmaps of hosts and devices. 58 * Lock nesting: Ok to take ->host_lock under list_lock. 59 */ 60static DEFINE_MUTEX(list_lock); 61 62struct soc_camera_async_client { 63 struct v4l2_async_subdev *sensor; 64 struct v4l2_async_notifier notifier; 65 struct platform_device *pdev; 66 struct list_head list; /* needed for clean up */ 67}; 68 69static int soc_camera_video_start(struct soc_camera_device *icd); 70static int video_dev_create(struct soc_camera_device *icd); 71 72int soc_camera_power_on(struct device *dev, struct soc_camera_subdev_desc *ssdd, 73 struct v4l2_clk *clk) 74{ 75 int ret; 76 bool clock_toggle; 77 78 if (clk && (!ssdd->unbalanced_power || 79 !test_and_set_bit(0, &ssdd->clock_state))) { 80 ret = v4l2_clk_enable(clk); 81 if (ret < 0) { 82 dev_err(dev, "Cannot enable clock: %d\n", ret); 83 return ret; 84 } 85 clock_toggle = true; 86 } else { 87 clock_toggle = false; 88 } 89 90 ret = regulator_bulk_enable(ssdd->sd_pdata.num_regulators, 91 ssdd->sd_pdata.regulators); 92 if (ret < 0) { 93 dev_err(dev, "Cannot enable regulators\n"); 94 goto eregenable; 95 } 96 97 if (ssdd->power) { 98 ret = ssdd->power(dev, 1); 99 if (ret < 0) { 100 dev_err(dev, 101 "Platform failed to power-on the camera.\n"); 102 goto epwron; 103 } 104 } 105 106 return 0; 107 108epwron: 109 regulator_bulk_disable(ssdd->sd_pdata.num_regulators, 110 ssdd->sd_pdata.regulators); 111eregenable: 112 if (clock_toggle) 113 v4l2_clk_disable(clk); 114 115 return ret; 116} 117EXPORT_SYMBOL(soc_camera_power_on); 118 119int soc_camera_power_off(struct device *dev, struct soc_camera_subdev_desc *ssdd, 120 struct v4l2_clk *clk) 121{ 122 int ret = 0; 123 int err; 124 125 if (ssdd->power) { 126 err = ssdd->power(dev, 0); 127 if (err < 0) { 128 dev_err(dev, 129 "Platform failed to power-off the camera.\n"); 130 ret = err; 131 } 132 } 133 134 err = regulator_bulk_disable(ssdd->sd_pdata.num_regulators, 135 ssdd->sd_pdata.regulators); 136 if (err < 0) { 137 dev_err(dev, "Cannot disable regulators\n"); 138 ret = ret ? : err; 139 } 140 141 if (clk && (!ssdd->unbalanced_power || test_and_clear_bit(0, &ssdd->clock_state))) 142 v4l2_clk_disable(clk); 143 144 return ret; 145} 146EXPORT_SYMBOL(soc_camera_power_off); 147 148int soc_camera_power_init(struct device *dev, struct soc_camera_subdev_desc *ssdd) 149{ 150 /* Should not have any effect in synchronous case */ 151 return devm_regulator_bulk_get(dev, ssdd->sd_pdata.num_regulators, 152 ssdd->sd_pdata.regulators); 153} 154EXPORT_SYMBOL(soc_camera_power_init); 155 156static int __soc_camera_power_on(struct soc_camera_device *icd) 157{ 158 struct v4l2_subdev *sd = soc_camera_to_subdev(icd); 159 int ret; 160 161 ret = v4l2_subdev_call(sd, core, s_power, 1); 162 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV) 163 return ret; 164 165 return 0; 166} 167 168static int __soc_camera_power_off(struct soc_camera_device *icd) 169{ 170 struct v4l2_subdev *sd = soc_camera_to_subdev(icd); 171 int ret; 172 173 ret = v4l2_subdev_call(sd, core, s_power, 0); 174 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV) 175 return ret; 176 177 return 0; 178} 179 180static int soc_camera_clock_start(struct soc_camera_host *ici) 181{ 182 int ret; 183 184 if (!ici->ops->clock_start) 185 return 0; 186 187 mutex_lock(&ici->clk_lock); 188 ret = ici->ops->clock_start(ici); 189 mutex_unlock(&ici->clk_lock); 190 191 return ret; 192} 193 194static void soc_camera_clock_stop(struct soc_camera_host *ici) 195{ 196 if (!ici->ops->clock_stop) 197 return; 198 199 mutex_lock(&ici->clk_lock); 200 ici->ops->clock_stop(ici); 201 mutex_unlock(&ici->clk_lock); 202} 203 204const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc( 205 struct soc_camera_device *icd, unsigned int fourcc) 206{ 207 unsigned int i; 208 209 for (i = 0; i < icd->num_user_formats; i++) 210 if (icd->user_formats[i].host_fmt->fourcc == fourcc) 211 return icd->user_formats + i; 212 return NULL; 213} 214EXPORT_SYMBOL(soc_camera_xlate_by_fourcc); 215 216/** 217 * soc_camera_apply_board_flags() - apply platform SOCAM_SENSOR_INVERT_* flags 218 * @ssdd: camera platform parameters 219 * @cfg: media bus configuration 220 * @return: resulting flags 221 */ 222unsigned long soc_camera_apply_board_flags(struct soc_camera_subdev_desc *ssdd, 223 const struct v4l2_mbus_config *cfg) 224{ 225 unsigned long f, flags = cfg->flags; 226 227 /* If only one of the two polarities is supported, switch to the opposite */ 228 if (ssdd->flags & SOCAM_SENSOR_INVERT_HSYNC) { 229 f = flags & (V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW); 230 if (f == V4L2_MBUS_HSYNC_ACTIVE_HIGH || f == V4L2_MBUS_HSYNC_ACTIVE_LOW) 231 flags ^= V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW; 232 } 233 234 if (ssdd->flags & SOCAM_SENSOR_INVERT_VSYNC) { 235 f = flags & (V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW); 236 if (f == V4L2_MBUS_VSYNC_ACTIVE_HIGH || f == V4L2_MBUS_VSYNC_ACTIVE_LOW) 237 flags ^= V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW; 238 } 239 240 if (ssdd->flags & SOCAM_SENSOR_INVERT_PCLK) { 241 f = flags & (V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING); 242 if (f == V4L2_MBUS_PCLK_SAMPLE_RISING || f == V4L2_MBUS_PCLK_SAMPLE_FALLING) 243 flags ^= V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING; 244 } 245 246 return flags; 247} 248EXPORT_SYMBOL(soc_camera_apply_board_flags); 249 250#define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \ 251 ((x) >> 24) & 0xff 252 253static int soc_camera_try_fmt(struct soc_camera_device *icd, 254 struct v4l2_format *f) 255{ 256 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 257 const struct soc_camera_format_xlate *xlate; 258 struct v4l2_pix_format *pix = &f->fmt.pix; 259 int ret; 260 261 dev_dbg(icd->pdev, "TRY_FMT(%c%c%c%c, %ux%u)\n", 262 pixfmtstr(pix->pixelformat), pix->width, pix->height); 263 264 if (pix->pixelformat != V4L2_PIX_FMT_JPEG && 265 !(ici->capabilities & SOCAM_HOST_CAP_STRIDE)) { 266 pix->bytesperline = 0; 267 pix->sizeimage = 0; 268 } 269 270 ret = ici->ops->try_fmt(icd, f); 271 if (ret < 0) 272 return ret; 273 274 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat); 275 if (!xlate) 276 return -EINVAL; 277 278 ret = soc_mbus_bytes_per_line(pix->width, xlate->host_fmt); 279 if (ret < 0) 280 return ret; 281 282 pix->bytesperline = max_t(u32, pix->bytesperline, ret); 283 284 ret = soc_mbus_image_size(xlate->host_fmt, pix->bytesperline, 285 pix->height); 286 if (ret < 0) 287 return ret; 288 289 pix->sizeimage = max_t(u32, pix->sizeimage, ret); 290 291 return 0; 292} 293 294static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv, 295 struct v4l2_format *f) 296{ 297 struct soc_camera_device *icd = file->private_data; 298 299 WARN_ON(priv != file->private_data); 300 301 /* Only single-plane capture is supported so far */ 302 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 303 return -EINVAL; 304 305 /* limit format to hardware capabilities */ 306 return soc_camera_try_fmt(icd, f); 307} 308 309static int soc_camera_enum_input(struct file *file, void *priv, 310 struct v4l2_input *inp) 311{ 312 if (inp->index != 0) 313 return -EINVAL; 314 315 /* default is camera */ 316 inp->type = V4L2_INPUT_TYPE_CAMERA; 317 strcpy(inp->name, "Camera"); 318 319 return 0; 320} 321 322static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i) 323{ 324 *i = 0; 325 326 return 0; 327} 328 329static int soc_camera_s_input(struct file *file, void *priv, unsigned int i) 330{ 331 if (i > 0) 332 return -EINVAL; 333 334 return 0; 335} 336 337static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id a) 338{ 339 struct soc_camera_device *icd = file->private_data; 340 struct v4l2_subdev *sd = soc_camera_to_subdev(icd); 341 342 return v4l2_subdev_call(sd, video, s_std, a); 343} 344 345static int soc_camera_g_std(struct file *file, void *priv, v4l2_std_id *a) 346{ 347 struct soc_camera_device *icd = file->private_data; 348 struct v4l2_subdev *sd = soc_camera_to_subdev(icd); 349 350 return v4l2_subdev_call(sd, video, g_std, a); 351} 352 353static int soc_camera_enum_framesizes(struct file *file, void *fh, 354 struct v4l2_frmsizeenum *fsize) 355{ 356 struct soc_camera_device *icd = file->private_data; 357 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 358 359 return ici->ops->enum_framesizes(icd, fsize); 360} 361 362static int soc_camera_reqbufs(struct file *file, void *priv, 363 struct v4l2_requestbuffers *p) 364{ 365 int ret; 366 struct soc_camera_device *icd = file->private_data; 367 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 368 369 WARN_ON(priv != file->private_data); 370 371 if (icd->streamer && icd->streamer != file) 372 return -EBUSY; 373 374 if (ici->ops->init_videobuf) { 375 ret = videobuf_reqbufs(&icd->vb_vidq, p); 376 if (ret < 0) 377 return ret; 378 379 ret = ici->ops->reqbufs(icd, p); 380 } else { 381 ret = vb2_reqbufs(&icd->vb2_vidq, p); 382 } 383 384 if (!ret && !icd->streamer) 385 icd->streamer = file; 386 387 return ret; 388} 389 390static int soc_camera_querybuf(struct file *file, void *priv, 391 struct v4l2_buffer *p) 392{ 393 struct soc_camera_device *icd = file->private_data; 394 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 395 396 WARN_ON(priv != file->private_data); 397 398 if (ici->ops->init_videobuf) 399 return videobuf_querybuf(&icd->vb_vidq, p); 400 else 401 return vb2_querybuf(&icd->vb2_vidq, p); 402} 403 404static int soc_camera_qbuf(struct file *file, void *priv, 405 struct v4l2_buffer *p) 406{ 407 struct soc_camera_device *icd = file->private_data; 408 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 409 410 WARN_ON(priv != file->private_data); 411 412 if (icd->streamer != file) 413 return -EBUSY; 414 415 if (ici->ops->init_videobuf) 416 return videobuf_qbuf(&icd->vb_vidq, p); 417 else 418 return vb2_qbuf(&icd->vb2_vidq, p); 419} 420 421static int soc_camera_dqbuf(struct file *file, void *priv, 422 struct v4l2_buffer *p) 423{ 424 struct soc_camera_device *icd = file->private_data; 425 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 426 427 WARN_ON(priv != file->private_data); 428 429 if (icd->streamer != file) 430 return -EBUSY; 431 432 if (ici->ops->init_videobuf) 433 return videobuf_dqbuf(&icd->vb_vidq, p, file->f_flags & O_NONBLOCK); 434 else 435 return vb2_dqbuf(&icd->vb2_vidq, p, file->f_flags & O_NONBLOCK); 436} 437 438static int soc_camera_create_bufs(struct file *file, void *priv, 439 struct v4l2_create_buffers *create) 440{ 441 struct soc_camera_device *icd = file->private_data; 442 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 443 444 /* videobuf2 only */ 445 if (ici->ops->init_videobuf) 446 return -EINVAL; 447 else 448 return vb2_create_bufs(&icd->vb2_vidq, create); 449} 450 451static int soc_camera_prepare_buf(struct file *file, void *priv, 452 struct v4l2_buffer *b) 453{ 454 struct soc_camera_device *icd = file->private_data; 455 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 456 457 /* videobuf2 only */ 458 if (ici->ops->init_videobuf) 459 return -EINVAL; 460 else 461 return vb2_prepare_buf(&icd->vb2_vidq, b); 462} 463 464static int soc_camera_expbuf(struct file *file, void *priv, 465 struct v4l2_exportbuffer *p) 466{ 467 struct soc_camera_device *icd = file->private_data; 468 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 469 470 if (icd->streamer != file) 471 return -EBUSY; 472 473 /* videobuf2 only */ 474 if (ici->ops->init_videobuf) 475 return -EINVAL; 476 else 477 return vb2_expbuf(&icd->vb2_vidq, p); 478} 479 480/* Always entered with .host_lock held */ 481static int soc_camera_init_user_formats(struct soc_camera_device *icd) 482{ 483 struct v4l2_subdev *sd = soc_camera_to_subdev(icd); 484 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 485 unsigned int i, fmts = 0, raw_fmts = 0; 486 int ret; 487 u32 code; 488 489 while (!v4l2_subdev_call(sd, video, enum_mbus_fmt, raw_fmts, &code)) 490 raw_fmts++; 491 492 if (!ici->ops->get_formats) 493 /* 494 * Fallback mode - the host will have to serve all 495 * sensor-provided formats one-to-one to the user 496 */ 497 fmts = raw_fmts; 498 else 499 /* 500 * First pass - only count formats this host-sensor 501 * configuration can provide 502 */ 503 for (i = 0; i < raw_fmts; i++) { 504 ret = ici->ops->get_formats(icd, i, NULL); 505 if (ret < 0) 506 return ret; 507 fmts += ret; 508 } 509 510 if (!fmts) 511 return -ENXIO; 512 513 icd->user_formats = 514 vmalloc(fmts * sizeof(struct soc_camera_format_xlate)); 515 if (!icd->user_formats) 516 return -ENOMEM; 517 518 dev_dbg(icd->pdev, "Found %d supported formats.\n", fmts); 519 520 /* Second pass - actually fill data formats */ 521 fmts = 0; 522 for (i = 0; i < raw_fmts; i++) 523 if (!ici->ops->get_formats) { 524 v4l2_subdev_call(sd, video, enum_mbus_fmt, i, &code); 525 icd->user_formats[fmts].host_fmt = 526 soc_mbus_get_fmtdesc(code); 527 if (icd->user_formats[fmts].host_fmt) 528 icd->user_formats[fmts++].code = code; 529 } else { 530 ret = ici->ops->get_formats(icd, i, 531 &icd->user_formats[fmts]); 532 if (ret < 0) 533 goto egfmt; 534 fmts += ret; 535 } 536 537 icd->num_user_formats = fmts; 538 icd->current_fmt = &icd->user_formats[0]; 539 540 return 0; 541 542egfmt: 543 vfree(icd->user_formats); 544 return ret; 545} 546 547/* Always entered with .host_lock held */ 548static void soc_camera_free_user_formats(struct soc_camera_device *icd) 549{ 550 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 551 552 if (ici->ops->put_formats) 553 ici->ops->put_formats(icd); 554 icd->current_fmt = NULL; 555 icd->num_user_formats = 0; 556 vfree(icd->user_formats); 557 icd->user_formats = NULL; 558} 559 560/* Called with .vb_lock held, or from the first open(2), see comment there */ 561static int soc_camera_set_fmt(struct soc_camera_device *icd, 562 struct v4l2_format *f) 563{ 564 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 565 struct v4l2_pix_format *pix = &f->fmt.pix; 566 int ret; 567 568 dev_dbg(icd->pdev, "S_FMT(%c%c%c%c, %ux%u)\n", 569 pixfmtstr(pix->pixelformat), pix->width, pix->height); 570 571 /* We always call try_fmt() before set_fmt() or set_crop() */ 572 ret = soc_camera_try_fmt(icd, f); 573 if (ret < 0) 574 return ret; 575 576 ret = ici->ops->set_fmt(icd, f); 577 if (ret < 0) { 578 return ret; 579 } else if (!icd->current_fmt || 580 icd->current_fmt->host_fmt->fourcc != pix->pixelformat) { 581 dev_err(icd->pdev, 582 "Host driver hasn't set up current format correctly!\n"); 583 return -EINVAL; 584 } 585 586 icd->user_width = pix->width; 587 icd->user_height = pix->height; 588 icd->bytesperline = pix->bytesperline; 589 icd->sizeimage = pix->sizeimage; 590 icd->colorspace = pix->colorspace; 591 icd->field = pix->field; 592 if (ici->ops->init_videobuf) 593 icd->vb_vidq.field = pix->field; 594 595 dev_dbg(icd->pdev, "set width: %d height: %d\n", 596 icd->user_width, icd->user_height); 597 598 /* set physical bus parameters */ 599 return ici->ops->set_bus_param(icd); 600} 601 602static int soc_camera_add_device(struct soc_camera_device *icd) 603{ 604 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 605 int ret; 606 607 if (ici->icd) 608 return -EBUSY; 609 610 if (!icd->clk) { 611 ret = soc_camera_clock_start(ici); 612 if (ret < 0) 613 return ret; 614 } 615 616 if (ici->ops->add) { 617 ret = ici->ops->add(icd); 618 if (ret < 0) 619 goto eadd; 620 } 621 622 ici->icd = icd; 623 624 return 0; 625 626eadd: 627 if (!icd->clk) 628 soc_camera_clock_stop(ici); 629 return ret; 630} 631 632static void soc_camera_remove_device(struct soc_camera_device *icd) 633{ 634 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 635 636 if (WARN_ON(icd != ici->icd)) 637 return; 638 639 if (ici->ops->remove) 640 ici->ops->remove(icd); 641 if (!icd->clk) 642 soc_camera_clock_stop(ici); 643 ici->icd = NULL; 644} 645 646static int soc_camera_open(struct file *file) 647{ 648 struct video_device *vdev = video_devdata(file); 649 struct soc_camera_device *icd; 650 struct soc_camera_host *ici; 651 int ret; 652 653 /* 654 * Don't mess with the host during probe: wait until the loop in 655 * scan_add_host() completes. Also protect against a race with 656 * soc_camera_host_unregister(). 657 */ 658 if (mutex_lock_interruptible(&list_lock)) 659 return -ERESTARTSYS; 660 661 if (!vdev || !video_is_registered(vdev)) { 662 mutex_unlock(&list_lock); 663 return -ENODEV; 664 } 665 666 icd = video_get_drvdata(vdev); 667 ici = to_soc_camera_host(icd->parent); 668 669 ret = try_module_get(ici->ops->owner) ? 0 : -ENODEV; 670 mutex_unlock(&list_lock); 671 672 if (ret < 0) { 673 dev_err(icd->pdev, "Couldn't lock capture bus driver.\n"); 674 return ret; 675 } 676 677 if (!to_soc_camera_control(icd)) { 678 /* No device driver attached */ 679 ret = -ENODEV; 680 goto econtrol; 681 } 682 683 if (mutex_lock_interruptible(&ici->host_lock)) { 684 ret = -ERESTARTSYS; 685 goto elockhost; 686 } 687 icd->use_count++; 688 689 /* Now we really have to activate the camera */ 690 if (icd->use_count == 1) { 691 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd); 692 /* Restore parameters before the last close() per V4L2 API */ 693 struct v4l2_format f = { 694 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE, 695 .fmt.pix = { 696 .width = icd->user_width, 697 .height = icd->user_height, 698 .field = icd->field, 699 .colorspace = icd->colorspace, 700 .pixelformat = 701 icd->current_fmt->host_fmt->fourcc, 702 }, 703 }; 704 705 /* The camera could have been already on, try to reset */ 706 if (sdesc->subdev_desc.reset) 707 if (icd->control) 708 sdesc->subdev_desc.reset(icd->control); 709 710 ret = soc_camera_add_device(icd); 711 if (ret < 0) { 712 dev_err(icd->pdev, "Couldn't activate the camera: %d\n", ret); 713 goto eiciadd; 714 } 715 716 ret = __soc_camera_power_on(icd); 717 if (ret < 0) 718 goto epower; 719 720 pm_runtime_enable(&icd->vdev->dev); 721 ret = pm_runtime_resume(&icd->vdev->dev); 722 if (ret < 0 && ret != -ENOSYS) 723 goto eresume; 724 725 /* 726 * Try to configure with default parameters. Notice: this is the 727 * very first open, so, we cannot race against other calls, 728 * apart from someone else calling open() simultaneously, but 729 * .host_lock is protecting us against it. 730 */ 731 ret = soc_camera_set_fmt(icd, &f); 732 if (ret < 0) 733 goto esfmt; 734 735 if (ici->ops->init_videobuf) { 736 ici->ops->init_videobuf(&icd->vb_vidq, icd); 737 } else { 738 ret = ici->ops->init_videobuf2(&icd->vb2_vidq, icd); 739 if (ret < 0) 740 goto einitvb; 741 } 742 v4l2_ctrl_handler_setup(&icd->ctrl_handler); 743 } 744 mutex_unlock(&ici->host_lock); 745 746 file->private_data = icd; 747 dev_dbg(icd->pdev, "camera device open\n"); 748 749 return 0; 750 751 /* 752 * All errors are entered with the .host_lock held, first four also 753 * with use_count == 1 754 */ 755einitvb: 756esfmt: 757 pm_runtime_disable(&icd->vdev->dev); 758eresume: 759 __soc_camera_power_off(icd); 760epower: 761 soc_camera_remove_device(icd); 762eiciadd: 763 icd->use_count--; 764 mutex_unlock(&ici->host_lock); 765elockhost: 766econtrol: 767 module_put(ici->ops->owner); 768 769 return ret; 770} 771 772static int soc_camera_close(struct file *file) 773{ 774 struct soc_camera_device *icd = file->private_data; 775 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 776 777 mutex_lock(&ici->host_lock); 778 icd->use_count--; 779 if (!icd->use_count) { 780 pm_runtime_suspend(&icd->vdev->dev); 781 pm_runtime_disable(&icd->vdev->dev); 782 783 if (ici->ops->init_videobuf2) 784 vb2_queue_release(&icd->vb2_vidq); 785 __soc_camera_power_off(icd); 786 787 soc_camera_remove_device(icd); 788 } 789 790 if (icd->streamer == file) 791 icd->streamer = NULL; 792 mutex_unlock(&ici->host_lock); 793 794 module_put(ici->ops->owner); 795 796 dev_dbg(icd->pdev, "camera device close\n"); 797 798 return 0; 799} 800 801static ssize_t soc_camera_read(struct file *file, char __user *buf, 802 size_t count, loff_t *ppos) 803{ 804 struct soc_camera_device *icd = file->private_data; 805 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 806 807 dev_dbg(icd->pdev, "read called, buf %p\n", buf); 808 809 if (ici->ops->init_videobuf2 && icd->vb2_vidq.io_modes & VB2_READ) 810 return vb2_read(&icd->vb2_vidq, buf, count, ppos, 811 file->f_flags & O_NONBLOCK); 812 813 dev_err(icd->pdev, "camera device read not implemented\n"); 814 815 return -EINVAL; 816} 817 818static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma) 819{ 820 struct soc_camera_device *icd = file->private_data; 821 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 822 int err; 823 824 dev_dbg(icd->pdev, "mmap called, vma=0x%08lx\n", (unsigned long)vma); 825 826 if (icd->streamer != file) 827 return -EBUSY; 828 829 if (mutex_lock_interruptible(&ici->host_lock)) 830 return -ERESTARTSYS; 831 if (ici->ops->init_videobuf) 832 err = videobuf_mmap_mapper(&icd->vb_vidq, vma); 833 else 834 err = vb2_mmap(&icd->vb2_vidq, vma); 835 mutex_unlock(&ici->host_lock); 836 837 dev_dbg(icd->pdev, "vma start=0x%08lx, size=%ld, ret=%d\n", 838 (unsigned long)vma->vm_start, 839 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, 840 err); 841 842 return err; 843} 844 845static unsigned int soc_camera_poll(struct file *file, poll_table *pt) 846{ 847 struct soc_camera_device *icd = file->private_data; 848 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 849 unsigned res = POLLERR; 850 851 if (icd->streamer != file) 852 return POLLERR; 853 854 mutex_lock(&ici->host_lock); 855 if (ici->ops->init_videobuf && list_empty(&icd->vb_vidq.stream)) 856 dev_err(icd->pdev, "Trying to poll with no queued buffers!\n"); 857 else 858 res = ici->ops->poll(file, pt); 859 mutex_unlock(&ici->host_lock); 860 return res; 861} 862 863static struct v4l2_file_operations soc_camera_fops = { 864 .owner = THIS_MODULE, 865 .open = soc_camera_open, 866 .release = soc_camera_close, 867 .unlocked_ioctl = video_ioctl2, 868 .read = soc_camera_read, 869 .mmap = soc_camera_mmap, 870 .poll = soc_camera_poll, 871}; 872 873static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv, 874 struct v4l2_format *f) 875{ 876 struct soc_camera_device *icd = file->private_data; 877 int ret; 878 879 WARN_ON(priv != file->private_data); 880 881 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) { 882 dev_warn(icd->pdev, "Wrong buf-type %d\n", f->type); 883 return -EINVAL; 884 } 885 886 if (icd->streamer && icd->streamer != file) 887 return -EBUSY; 888 889 if (is_streaming(to_soc_camera_host(icd->parent), icd)) { 890 dev_err(icd->pdev, "S_FMT denied: queue initialised\n"); 891 return -EBUSY; 892 } 893 894 ret = soc_camera_set_fmt(icd, f); 895 896 if (!ret && !icd->streamer) 897 icd->streamer = file; 898 899 return ret; 900} 901 902static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv, 903 struct v4l2_fmtdesc *f) 904{ 905 struct soc_camera_device *icd = file->private_data; 906 const struct soc_mbus_pixelfmt *format; 907 908 WARN_ON(priv != file->private_data); 909 910 if (f->index >= icd->num_user_formats) 911 return -EINVAL; 912 913 format = icd->user_formats[f->index].host_fmt; 914 915 if (format->name) 916 strlcpy(f->description, format->name, sizeof(f->description)); 917 f->pixelformat = format->fourcc; 918 return 0; 919} 920 921static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv, 922 struct v4l2_format *f) 923{ 924 struct soc_camera_device *icd = file->private_data; 925 struct v4l2_pix_format *pix = &f->fmt.pix; 926 927 WARN_ON(priv != file->private_data); 928 929 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 930 return -EINVAL; 931 932 pix->width = icd->user_width; 933 pix->height = icd->user_height; 934 pix->bytesperline = icd->bytesperline; 935 pix->sizeimage = icd->sizeimage; 936 pix->field = icd->field; 937 pix->pixelformat = icd->current_fmt->host_fmt->fourcc; 938 pix->colorspace = icd->colorspace; 939 dev_dbg(icd->pdev, "current_fmt->fourcc: 0x%08x\n", 940 icd->current_fmt->host_fmt->fourcc); 941 return 0; 942} 943 944static int soc_camera_querycap(struct file *file, void *priv, 945 struct v4l2_capability *cap) 946{ 947 struct soc_camera_device *icd = file->private_data; 948 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 949 950 WARN_ON(priv != file->private_data); 951 952 strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver)); 953 return ici->ops->querycap(ici, cap); 954} 955 956static int soc_camera_streamon(struct file *file, void *priv, 957 enum v4l2_buf_type i) 958{ 959 struct soc_camera_device *icd = file->private_data; 960 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 961 struct v4l2_subdev *sd = soc_camera_to_subdev(icd); 962 int ret; 963 964 WARN_ON(priv != file->private_data); 965 966 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE) 967 return -EINVAL; 968 969 if (icd->streamer != file) 970 return -EBUSY; 971 972 /* This calls buf_queue from host driver's videobuf_queue_ops */ 973 if (ici->ops->init_videobuf) 974 ret = videobuf_streamon(&icd->vb_vidq); 975 else 976 ret = vb2_streamon(&icd->vb2_vidq, i); 977 978 if (!ret) 979 v4l2_subdev_call(sd, video, s_stream, 1); 980 981 return ret; 982} 983 984static int soc_camera_streamoff(struct file *file, void *priv, 985 enum v4l2_buf_type i) 986{ 987 struct soc_camera_device *icd = file->private_data; 988 struct v4l2_subdev *sd = soc_camera_to_subdev(icd); 989 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 990 991 WARN_ON(priv != file->private_data); 992 993 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE) 994 return -EINVAL; 995 996 if (icd->streamer != file) 997 return -EBUSY; 998 999 /* 1000 * This calls buf_release from host driver's videobuf_queue_ops for all 1001 * remaining buffers. When the last buffer is freed, stop capture 1002 */ 1003 if (ici->ops->init_videobuf) 1004 videobuf_streamoff(&icd->vb_vidq); 1005 else 1006 vb2_streamoff(&icd->vb2_vidq, i); 1007 1008 v4l2_subdev_call(sd, video, s_stream, 0); 1009 1010 return 0; 1011} 1012 1013static int soc_camera_cropcap(struct file *file, void *fh, 1014 struct v4l2_cropcap *a) 1015{ 1016 struct soc_camera_device *icd = file->private_data; 1017 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 1018 1019 return ici->ops->cropcap(icd, a); 1020} 1021 1022static int soc_camera_g_crop(struct file *file, void *fh, 1023 struct v4l2_crop *a) 1024{ 1025 struct soc_camera_device *icd = file->private_data; 1026 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 1027 int ret; 1028 1029 ret = ici->ops->get_crop(icd, a); 1030 1031 return ret; 1032} 1033 1034/* 1035 * According to the V4L2 API, drivers shall not update the struct v4l2_crop 1036 * argument with the actual geometry, instead, the user shall use G_CROP to 1037 * retrieve it. 1038 */ 1039static int soc_camera_s_crop(struct file *file, void *fh, 1040 const struct v4l2_crop *a) 1041{ 1042 struct soc_camera_device *icd = file->private_data; 1043 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 1044 const struct v4l2_rect *rect = &a->c; 1045 struct v4l2_crop current_crop; 1046 int ret; 1047 1048 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 1049 return -EINVAL; 1050 1051 dev_dbg(icd->pdev, "S_CROP(%ux%u@%u:%u)\n", 1052 rect->width, rect->height, rect->left, rect->top); 1053 1054 current_crop.type = a->type; 1055 1056 /* If get_crop fails, we'll let host and / or client drivers decide */ 1057 ret = ici->ops->get_crop(icd, ¤t_crop); 1058 1059 /* Prohibit window size change with initialised buffers */ 1060 if (ret < 0) { 1061 dev_err(icd->pdev, 1062 "S_CROP denied: getting current crop failed\n"); 1063 } else if ((a->c.width == current_crop.c.width && 1064 a->c.height == current_crop.c.height) || 1065 !is_streaming(ici, icd)) { 1066 /* same size or not streaming - use .set_crop() */ 1067 ret = ici->ops->set_crop(icd, a); 1068 } else if (ici->ops->set_livecrop) { 1069 ret = ici->ops->set_livecrop(icd, a); 1070 } else { 1071 dev_err(icd->pdev, 1072 "S_CROP denied: queue initialised and sizes differ\n"); 1073 ret = -EBUSY; 1074 } 1075 1076 return ret; 1077} 1078 1079static int soc_camera_g_selection(struct file *file, void *fh, 1080 struct v4l2_selection *s) 1081{ 1082 struct soc_camera_device *icd = file->private_data; 1083 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 1084 1085 /* With a wrong type no need to try to fall back to cropping */ 1086 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 1087 return -EINVAL; 1088 1089 if (!ici->ops->get_selection) 1090 return -ENOTTY; 1091 1092 return ici->ops->get_selection(icd, s); 1093} 1094 1095static int soc_camera_s_selection(struct file *file, void *fh, 1096 struct v4l2_selection *s) 1097{ 1098 struct soc_camera_device *icd = file->private_data; 1099 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 1100 int ret; 1101 1102 /* In all these cases cropping emulation will not help */ 1103 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || 1104 (s->target != V4L2_SEL_TGT_COMPOSE && 1105 s->target != V4L2_SEL_TGT_CROP)) 1106 return -EINVAL; 1107 1108 if (s->target == V4L2_SEL_TGT_COMPOSE) { 1109 /* No output size change during a running capture! */ 1110 if (is_streaming(ici, icd) && 1111 (icd->user_width != s->r.width || 1112 icd->user_height != s->r.height)) 1113 return -EBUSY; 1114 1115 /* 1116 * Only one user is allowed to change the output format, touch 1117 * buffers, start / stop streaming, poll for data 1118 */ 1119 if (icd->streamer && icd->streamer != file) 1120 return -EBUSY; 1121 } 1122 1123 if (!ici->ops->set_selection) 1124 return -ENOTTY; 1125 1126 ret = ici->ops->set_selection(icd, s); 1127 if (!ret && 1128 s->target == V4L2_SEL_TGT_COMPOSE) { 1129 icd->user_width = s->r.width; 1130 icd->user_height = s->r.height; 1131 if (!icd->streamer) 1132 icd->streamer = file; 1133 } 1134 1135 return ret; 1136} 1137 1138static int soc_camera_g_parm(struct file *file, void *fh, 1139 struct v4l2_streamparm *a) 1140{ 1141 struct soc_camera_device *icd = file->private_data; 1142 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 1143 1144 if (ici->ops->get_parm) 1145 return ici->ops->get_parm(icd, a); 1146 1147 return -ENOIOCTLCMD; 1148} 1149 1150static int soc_camera_s_parm(struct file *file, void *fh, 1151 struct v4l2_streamparm *a) 1152{ 1153 struct soc_camera_device *icd = file->private_data; 1154 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 1155 1156 if (ici->ops->set_parm) 1157 return ici->ops->set_parm(icd, a); 1158 1159 return -ENOIOCTLCMD; 1160} 1161 1162static int soc_camera_probe(struct soc_camera_host *ici, 1163 struct soc_camera_device *icd); 1164 1165/* So far this function cannot fail */ 1166static void scan_add_host(struct soc_camera_host *ici) 1167{ 1168 struct soc_camera_device *icd; 1169 1170 mutex_lock(&list_lock); 1171 1172 list_for_each_entry(icd, &devices, list) 1173 if (icd->iface == ici->nr) { 1174 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd); 1175 struct soc_camera_subdev_desc *ssdd = &sdesc->subdev_desc; 1176 1177 /* The camera could have been already on, try to reset */ 1178 if (ssdd->reset) 1179 if (icd->control) 1180 ssdd->reset(icd->control); 1181 1182 icd->parent = ici->v4l2_dev.dev; 1183 1184 /* Ignore errors */ 1185 soc_camera_probe(ici, icd); 1186 } 1187 1188 mutex_unlock(&list_lock); 1189} 1190 1191/* 1192 * It is invalid to call v4l2_clk_enable() after a successful probing 1193 * asynchronously outside of V4L2 operations, i.e. with .host_lock not held. 1194 */ 1195static int soc_camera_clk_enable(struct v4l2_clk *clk) 1196{ 1197 struct soc_camera_device *icd = clk->priv; 1198 struct soc_camera_host *ici; 1199 1200 if (!icd || !icd->parent) 1201 return -ENODEV; 1202 1203 ici = to_soc_camera_host(icd->parent); 1204 1205 if (!try_module_get(ici->ops->owner)) 1206 return -ENODEV; 1207 1208 /* 1209 * If a different client is currently being probed, the host will tell 1210 * you to go 1211 */ 1212 return soc_camera_clock_start(ici); 1213} 1214 1215static void soc_camera_clk_disable(struct v4l2_clk *clk) 1216{ 1217 struct soc_camera_device *icd = clk->priv; 1218 struct soc_camera_host *ici; 1219 1220 if (!icd || !icd->parent) 1221 return; 1222 1223 ici = to_soc_camera_host(icd->parent); 1224 1225 soc_camera_clock_stop(ici); 1226 1227 module_put(ici->ops->owner); 1228} 1229 1230/* 1231 * Eventually, it would be more logical to make the respective host the clock 1232 * owner, but then we would have to copy this struct for each ici. Besides, it 1233 * would introduce the circular dependency problem, unless we port all client 1234 * drivers to release the clock, when not in use. 1235 */ 1236static const struct v4l2_clk_ops soc_camera_clk_ops = { 1237 .owner = THIS_MODULE, 1238 .enable = soc_camera_clk_enable, 1239 .disable = soc_camera_clk_disable, 1240}; 1241 1242static int soc_camera_dyn_pdev(struct soc_camera_desc *sdesc, 1243 struct soc_camera_async_client *sasc) 1244{ 1245 struct platform_device *pdev; 1246 int ret, i; 1247 1248 mutex_lock(&list_lock); 1249 i = find_first_zero_bit(device_map, MAP_MAX_NUM); 1250 if (i < MAP_MAX_NUM) 1251 set_bit(i, device_map); 1252 mutex_unlock(&list_lock); 1253 if (i >= MAP_MAX_NUM) 1254 return -ENOMEM; 1255 1256 pdev = platform_device_alloc("soc-camera-pdrv", i); 1257 if (!pdev) 1258 return -ENOMEM; 1259 1260 ret = platform_device_add_data(pdev, sdesc, sizeof(*sdesc)); 1261 if (ret < 0) { 1262 platform_device_put(pdev); 1263 return ret; 1264 } 1265 1266 sasc->pdev = pdev; 1267 1268 return 0; 1269} 1270 1271static struct soc_camera_device *soc_camera_add_pdev(struct soc_camera_async_client *sasc) 1272{ 1273 struct platform_device *pdev = sasc->pdev; 1274 int ret; 1275 1276 ret = platform_device_add(pdev); 1277 if (ret < 0 || !pdev->dev.driver) 1278 return NULL; 1279 1280 return platform_get_drvdata(pdev); 1281} 1282 1283/* Locking: called with .host_lock held */ 1284static int soc_camera_probe_finish(struct soc_camera_device *icd) 1285{ 1286 struct v4l2_subdev *sd = soc_camera_to_subdev(icd); 1287 struct v4l2_mbus_framefmt mf; 1288 int ret; 1289 1290 sd->grp_id = soc_camera_grp_id(icd); 1291 v4l2_set_subdev_hostdata(sd, icd); 1292 1293 v4l2_subdev_call(sd, video, g_tvnorms, &icd->vdev->tvnorms); 1294 1295 ret = v4l2_ctrl_add_handler(&icd->ctrl_handler, sd->ctrl_handler, NULL); 1296 if (ret < 0) 1297 return ret; 1298 1299 ret = soc_camera_add_device(icd); 1300 if (ret < 0) { 1301 dev_err(icd->pdev, "Couldn't activate the camera: %d\n", ret); 1302 return ret; 1303 } 1304 1305 /* At this point client .probe() should have run already */ 1306 ret = soc_camera_init_user_formats(icd); 1307 if (ret < 0) 1308 goto eusrfmt; 1309 1310 icd->field = V4L2_FIELD_ANY; 1311 1312 ret = soc_camera_video_start(icd); 1313 if (ret < 0) 1314 goto evidstart; 1315 1316 /* Try to improve our guess of a reasonable window format */ 1317 if (!v4l2_subdev_call(sd, video, g_mbus_fmt, &mf)) { 1318 icd->user_width = mf.width; 1319 icd->user_height = mf.height; 1320 icd->colorspace = mf.colorspace; 1321 icd->field = mf.field; 1322 } 1323 soc_camera_remove_device(icd); 1324 1325 return 0; 1326 1327evidstart: 1328 soc_camera_free_user_formats(icd); 1329eusrfmt: 1330 soc_camera_remove_device(icd); 1331 1332 return ret; 1333} 1334 1335#ifdef CONFIG_I2C_BOARDINFO 1336static int soc_camera_i2c_init(struct soc_camera_device *icd, 1337 struct soc_camera_desc *sdesc) 1338{ 1339 struct soc_camera_subdev_desc *ssdd; 1340 struct i2c_client *client; 1341 struct soc_camera_host *ici; 1342 struct soc_camera_host_desc *shd = &sdesc->host_desc; 1343 struct i2c_adapter *adap; 1344 struct v4l2_subdev *subdev; 1345 char clk_name[V4L2_SUBDEV_NAME_SIZE]; 1346 int ret; 1347 1348 /* First find out how we link the main client */ 1349 if (icd->sasc) { 1350 /* Async non-OF probing handled by the subdevice list */ 1351 return -EPROBE_DEFER; 1352 } 1353 1354 ici = to_soc_camera_host(icd->parent); 1355 adap = i2c_get_adapter(shd->i2c_adapter_id); 1356 if (!adap) { 1357 dev_err(icd->pdev, "Cannot get I2C adapter #%d. No driver?\n", 1358 shd->i2c_adapter_id); 1359 return -ENODEV; 1360 } 1361 1362 ssdd = kmemdup(&sdesc->subdev_desc, sizeof(*ssdd), GFP_KERNEL); 1363 if (!ssdd) { 1364 ret = -ENOMEM; 1365 goto ealloc; 1366 } 1367 /* 1368 * In synchronous case we request regulators ourselves in 1369 * soc_camera_pdrv_probe(), make sure the subdevice driver doesn't try 1370 * to allocate them again. 1371 */ 1372 ssdd->sd_pdata.num_regulators = 0; 1373 ssdd->sd_pdata.regulators = NULL; 1374 shd->board_info->platform_data = ssdd; 1375 1376 snprintf(clk_name, sizeof(clk_name), "%d-%04x", 1377 shd->i2c_adapter_id, shd->board_info->addr); 1378 1379 icd->clk = v4l2_clk_register(&soc_camera_clk_ops, clk_name, icd); 1380 if (IS_ERR(icd->clk)) { 1381 ret = PTR_ERR(icd->clk); 1382 goto eclkreg; 1383 } 1384 1385 subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap, 1386 shd->board_info, NULL); 1387 if (!subdev) { 1388 ret = -ENODEV; 1389 goto ei2cnd; 1390 } 1391 1392 client = v4l2_get_subdevdata(subdev); 1393 1394 /* Use to_i2c_client(dev) to recover the i2c client */ 1395 icd->control = &client->dev; 1396 1397 return 0; 1398ei2cnd: 1399 v4l2_clk_unregister(icd->clk); 1400 icd->clk = NULL; 1401eclkreg: 1402 kfree(ssdd); 1403ealloc: 1404 i2c_put_adapter(adap); 1405 return ret; 1406} 1407 1408static void soc_camera_i2c_free(struct soc_camera_device *icd) 1409{ 1410 struct i2c_client *client = 1411 to_i2c_client(to_soc_camera_control(icd)); 1412 struct i2c_adapter *adap; 1413 struct soc_camera_subdev_desc *ssdd; 1414 1415 icd->control = NULL; 1416 if (icd->sasc) 1417 return; 1418 1419 adap = client->adapter; 1420 ssdd = client->dev.platform_data; 1421 v4l2_device_unregister_subdev(i2c_get_clientdata(client)); 1422 i2c_unregister_device(client); 1423 i2c_put_adapter(adap); 1424 kfree(ssdd); 1425 v4l2_clk_unregister(icd->clk); 1426 icd->clk = NULL; 1427} 1428 1429/* 1430 * V4L2 asynchronous notifier callbacks. They are all called under a v4l2-async 1431 * internal global mutex, therefore cannot race against other asynchronous 1432 * events. Until notifier->complete() (soc_camera_async_complete()) is called, 1433 * the video device node is not registered and no V4L fops can occur. Unloading 1434 * of the host driver also calls a v4l2-async function, so also there we're 1435 * protected. 1436 */ 1437static int soc_camera_async_bound(struct v4l2_async_notifier *notifier, 1438 struct v4l2_subdev *sd, 1439 struct v4l2_async_subdev *asd) 1440{ 1441 struct soc_camera_async_client *sasc = container_of(notifier, 1442 struct soc_camera_async_client, notifier); 1443 struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev); 1444 1445 if (asd == sasc->sensor && !WARN_ON(icd->control)) { 1446 struct i2c_client *client = v4l2_get_subdevdata(sd); 1447 1448 /* 1449 * Only now we get subdevice-specific information like 1450 * regulators, flags, callbacks, etc. 1451 */ 1452 if (client) { 1453 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd); 1454 struct soc_camera_subdev_desc *ssdd = 1455 soc_camera_i2c_to_desc(client); 1456 if (ssdd) { 1457 memcpy(&sdesc->subdev_desc, ssdd, 1458 sizeof(sdesc->subdev_desc)); 1459 if (ssdd->reset) 1460 ssdd->reset(&client->dev); 1461 } 1462 1463 icd->control = &client->dev; 1464 } 1465 } 1466 1467 return 0; 1468} 1469 1470static void soc_camera_async_unbind(struct v4l2_async_notifier *notifier, 1471 struct v4l2_subdev *sd, 1472 struct v4l2_async_subdev *asd) 1473{ 1474 struct soc_camera_async_client *sasc = container_of(notifier, 1475 struct soc_camera_async_client, notifier); 1476 struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev); 1477 1478 if (icd->clk) { 1479 v4l2_clk_unregister(icd->clk); 1480 icd->clk = NULL; 1481 } 1482} 1483 1484static int soc_camera_async_complete(struct v4l2_async_notifier *notifier) 1485{ 1486 struct soc_camera_async_client *sasc = container_of(notifier, 1487 struct soc_camera_async_client, notifier); 1488 struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev); 1489 1490 if (to_soc_camera_control(icd)) { 1491 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 1492 int ret; 1493 1494 mutex_lock(&list_lock); 1495 ret = soc_camera_probe(ici, icd); 1496 mutex_unlock(&list_lock); 1497 if (ret < 0) 1498 return ret; 1499 } 1500 1501 return 0; 1502} 1503 1504static int scan_async_group(struct soc_camera_host *ici, 1505 struct v4l2_async_subdev **asd, unsigned int size) 1506{ 1507 struct soc_camera_async_subdev *sasd; 1508 struct soc_camera_async_client *sasc; 1509 struct soc_camera_device *icd; 1510 struct soc_camera_desc sdesc = {.host_desc.bus_id = ici->nr,}; 1511 char clk_name[V4L2_SUBDEV_NAME_SIZE]; 1512 unsigned int i; 1513 int ret; 1514 1515 /* First look for a sensor */ 1516 for (i = 0; i < size; i++) { 1517 sasd = container_of(asd[i], struct soc_camera_async_subdev, asd); 1518 if (sasd->role == SOCAM_SUBDEV_DATA_SOURCE) 1519 break; 1520 } 1521 1522 if (i >= size || asd[i]->match_type != V4L2_ASYNC_MATCH_I2C) { 1523 /* All useless */ 1524 dev_err(ici->v4l2_dev.dev, "No I2C data source found!\n"); 1525 return -ENODEV; 1526 } 1527 1528 /* Or shall this be managed by the soc-camera device? */ 1529 sasc = devm_kzalloc(ici->v4l2_dev.dev, sizeof(*sasc), GFP_KERNEL); 1530 if (!sasc) 1531 return -ENOMEM; 1532 1533 /* HACK: just need a != NULL */ 1534 sdesc.host_desc.board_info = ERR_PTR(-ENODATA); 1535 1536 ret = soc_camera_dyn_pdev(&sdesc, sasc); 1537 if (ret < 0) 1538 goto eallocpdev; 1539 1540 sasc->sensor = &sasd->asd; 1541 1542 icd = soc_camera_add_pdev(sasc); 1543 if (!icd) { 1544 ret = -ENOMEM; 1545 goto eaddpdev; 1546 } 1547 1548 sasc->notifier.subdevs = asd; 1549 sasc->notifier.num_subdevs = size; 1550 sasc->notifier.bound = soc_camera_async_bound; 1551 sasc->notifier.unbind = soc_camera_async_unbind; 1552 sasc->notifier.complete = soc_camera_async_complete; 1553 1554 icd->sasc = sasc; 1555 icd->parent = ici->v4l2_dev.dev; 1556 1557 snprintf(clk_name, sizeof(clk_name), "%d-%04x", 1558 sasd->asd.match.i2c.adapter_id, sasd->asd.match.i2c.address); 1559 1560 icd->clk = v4l2_clk_register(&soc_camera_clk_ops, clk_name, icd); 1561 if (IS_ERR(icd->clk)) { 1562 ret = PTR_ERR(icd->clk); 1563 goto eclkreg; 1564 } 1565 1566 ret = v4l2_async_notifier_register(&ici->v4l2_dev, &sasc->notifier); 1567 if (!ret) 1568 return 0; 1569 1570 v4l2_clk_unregister(icd->clk); 1571eclkreg: 1572 icd->clk = NULL; 1573 platform_device_del(sasc->pdev); 1574eaddpdev: 1575 platform_device_put(sasc->pdev); 1576eallocpdev: 1577 devm_kfree(ici->v4l2_dev.dev, sasc); 1578 dev_err(ici->v4l2_dev.dev, "group probe failed: %d\n", ret); 1579 1580 return ret; 1581} 1582 1583static void scan_async_host(struct soc_camera_host *ici) 1584{ 1585 struct v4l2_async_subdev **asd; 1586 int j; 1587 1588 for (j = 0, asd = ici->asd; ici->asd_sizes[j]; j++) { 1589 scan_async_group(ici, asd, ici->asd_sizes[j]); 1590 asd += ici->asd_sizes[j]; 1591 } 1592} 1593#else 1594#define soc_camera_i2c_init(icd, sdesc) (-ENODEV) 1595#define soc_camera_i2c_free(icd) do {} while (0) 1596#define scan_async_host(ici) do {} while (0) 1597#endif 1598 1599#ifdef CONFIG_OF 1600 1601struct soc_of_info { 1602 struct soc_camera_async_subdev sasd; 1603 struct soc_camera_async_client sasc; 1604 struct v4l2_async_subdev *subdev; 1605}; 1606 1607static int soc_of_bind(struct soc_camera_host *ici, 1608 struct device_node *ep, 1609 struct device_node *remote) 1610{ 1611 struct soc_camera_device *icd; 1612 struct soc_camera_desc sdesc = {.host_desc.bus_id = ici->nr,}; 1613 struct soc_camera_async_client *sasc; 1614 struct soc_of_info *info; 1615 struct i2c_client *client; 1616 char clk_name[V4L2_SUBDEV_NAME_SIZE]; 1617 int ret; 1618 1619 /* allocate a new subdev and add match info to it */ 1620 info = devm_kzalloc(ici->v4l2_dev.dev, sizeof(struct soc_of_info), 1621 GFP_KERNEL); 1622 if (!info) 1623 return -ENOMEM; 1624 1625 info->sasd.asd.match.of.node = remote; 1626 info->sasd.asd.match_type = V4L2_ASYNC_MATCH_OF; 1627 info->subdev = &info->sasd.asd; 1628 1629 /* Or shall this be managed by the soc-camera device? */ 1630 sasc = &info->sasc; 1631 1632 /* HACK: just need a != NULL */ 1633 sdesc.host_desc.board_info = ERR_PTR(-ENODATA); 1634 1635 ret = soc_camera_dyn_pdev(&sdesc, sasc); 1636 if (ret < 0) 1637 goto eallocpdev; 1638 1639 sasc->sensor = &info->sasd.asd; 1640 1641 icd = soc_camera_add_pdev(sasc); 1642 if (!icd) { 1643 ret = -ENOMEM; 1644 goto eaddpdev; 1645 } 1646 1647 sasc->notifier.subdevs = &info->subdev; 1648 sasc->notifier.num_subdevs = 1; 1649 sasc->notifier.bound = soc_camera_async_bound; 1650 sasc->notifier.unbind = soc_camera_async_unbind; 1651 sasc->notifier.complete = soc_camera_async_complete; 1652 1653 icd->sasc = sasc; 1654 icd->parent = ici->v4l2_dev.dev; 1655 1656 client = of_find_i2c_device_by_node(remote); 1657 1658 if (client) 1659 snprintf(clk_name, sizeof(clk_name), "%d-%04x", 1660 client->adapter->nr, client->addr); 1661 else 1662 snprintf(clk_name, sizeof(clk_name), "of-%s", 1663 of_node_full_name(remote)); 1664 1665 icd->clk = v4l2_clk_register(&soc_camera_clk_ops, clk_name, icd); 1666 if (IS_ERR(icd->clk)) { 1667 ret = PTR_ERR(icd->clk); 1668 goto eclkreg; 1669 } 1670 1671 ret = v4l2_async_notifier_register(&ici->v4l2_dev, &sasc->notifier); 1672 if (!ret) 1673 return 0; 1674 1675 v4l2_clk_unregister(icd->clk); 1676eclkreg: 1677 icd->clk = NULL; 1678 platform_device_del(sasc->pdev); 1679eaddpdev: 1680 platform_device_put(sasc->pdev); 1681eallocpdev: 1682 devm_kfree(ici->v4l2_dev.dev, info); 1683 dev_err(ici->v4l2_dev.dev, "group probe failed: %d\n", ret); 1684 1685 return ret; 1686} 1687 1688static void scan_of_host(struct soc_camera_host *ici) 1689{ 1690 struct device *dev = ici->v4l2_dev.dev; 1691 struct device_node *np = dev->of_node; 1692 struct device_node *epn = NULL, *ren; 1693 unsigned int i; 1694 1695 for (i = 0; ; i++) { 1696 epn = of_graph_get_next_endpoint(np, epn); 1697 if (!epn) 1698 break; 1699 1700 ren = of_graph_get_remote_port(epn); 1701 if (!ren) { 1702 dev_notice(dev, "no remote for %s\n", 1703 of_node_full_name(epn)); 1704 continue; 1705 } 1706 1707 /* so we now have a remote node to connect */ 1708 if (!i) 1709 soc_of_bind(ici, epn, ren->parent); 1710 1711 of_node_put(ren); 1712 1713 if (i) { 1714 dev_err(dev, "multiple subdevices aren't supported yet!\n"); 1715 break; 1716 } 1717 } 1718 1719 of_node_put(epn); 1720} 1721 1722#else 1723static inline void scan_of_host(struct soc_camera_host *ici) { } 1724#endif 1725 1726/* Called during host-driver probe */ 1727static int soc_camera_probe(struct soc_camera_host *ici, 1728 struct soc_camera_device *icd) 1729{ 1730 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd); 1731 struct soc_camera_host_desc *shd = &sdesc->host_desc; 1732 struct device *control = NULL; 1733 int ret; 1734 1735 dev_info(icd->pdev, "Probing %s\n", dev_name(icd->pdev)); 1736 1737 /* 1738 * Currently the subdev with the largest number of controls (13) is 1739 * ov6550. So let's pick 16 as a hint for the control handler. Note 1740 * that this is a hint only: too large and you waste some memory, too 1741 * small and there is a (very) small performance hit when looking up 1742 * controls in the internal hash. 1743 */ 1744 ret = v4l2_ctrl_handler_init(&icd->ctrl_handler, 16); 1745 if (ret < 0) 1746 return ret; 1747 1748 /* Must have icd->vdev before registering the device */ 1749 ret = video_dev_create(icd); 1750 if (ret < 0) 1751 goto evdc; 1752 1753 /* 1754 * ..._video_start() will create a device node, video_register_device() 1755 * itself is protected against concurrent open() calls, but we also have 1756 * to protect our data also during client probing. 1757 */ 1758 1759 /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */ 1760 if (shd->board_info) { 1761 ret = soc_camera_i2c_init(icd, sdesc); 1762 if (ret < 0 && ret != -EPROBE_DEFER) 1763 goto eadd; 1764 } else if (!shd->add_device || !shd->del_device) { 1765 ret = -EINVAL; 1766 goto eadd; 1767 } else { 1768 ret = soc_camera_clock_start(ici); 1769 if (ret < 0) 1770 goto eadd; 1771 1772 if (shd->module_name) 1773 ret = request_module(shd->module_name); 1774 1775 ret = shd->add_device(icd); 1776 if (ret < 0) 1777 goto eadddev; 1778 1779 /* 1780 * FIXME: this is racy, have to use driver-binding notification, 1781 * when it is available 1782 */ 1783 control = to_soc_camera_control(icd); 1784 if (!control || !control->driver || !dev_get_drvdata(control) || 1785 !try_module_get(control->driver->owner)) { 1786 shd->del_device(icd); 1787 ret = -ENODEV; 1788 goto enodrv; 1789 } 1790 } 1791 1792 mutex_lock(&ici->host_lock); 1793 ret = soc_camera_probe_finish(icd); 1794 mutex_unlock(&ici->host_lock); 1795 if (ret < 0) 1796 goto efinish; 1797 1798 return 0; 1799 1800efinish: 1801 if (shd->board_info) { 1802 soc_camera_i2c_free(icd); 1803 } else { 1804 shd->del_device(icd); 1805 module_put(control->driver->owner); 1806enodrv: 1807eadddev: 1808 soc_camera_clock_stop(ici); 1809 } 1810eadd: 1811 if (icd->vdev) { 1812 video_device_release(icd->vdev); 1813 icd->vdev = NULL; 1814 } 1815evdc: 1816 v4l2_ctrl_handler_free(&icd->ctrl_handler); 1817 return ret; 1818} 1819 1820/* 1821 * This is called on device_unregister, which only means we have to disconnect 1822 * from the host, but not remove ourselves from the device list. With 1823 * asynchronous client probing this can also be called without 1824 * soc_camera_probe_finish() having run. Careful with clean up. 1825 */ 1826static int soc_camera_remove(struct soc_camera_device *icd) 1827{ 1828 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd); 1829 struct video_device *vdev = icd->vdev; 1830 1831 v4l2_ctrl_handler_free(&icd->ctrl_handler); 1832 if (vdev) { 1833 video_unregister_device(vdev); 1834 icd->vdev = NULL; 1835 } 1836 1837 if (sdesc->host_desc.board_info) { 1838 soc_camera_i2c_free(icd); 1839 } else { 1840 struct device *dev = to_soc_camera_control(icd); 1841 struct device_driver *drv = dev ? dev->driver : NULL; 1842 if (drv) { 1843 sdesc->host_desc.del_device(icd); 1844 module_put(drv->owner); 1845 } 1846 } 1847 1848 if (icd->num_user_formats) 1849 soc_camera_free_user_formats(icd); 1850 1851 if (icd->clk) { 1852 /* For the synchronous case */ 1853 v4l2_clk_unregister(icd->clk); 1854 icd->clk = NULL; 1855 } 1856 1857 if (icd->sasc) 1858 platform_device_unregister(icd->sasc->pdev); 1859 1860 return 0; 1861} 1862 1863static int default_cropcap(struct soc_camera_device *icd, 1864 struct v4l2_cropcap *a) 1865{ 1866 struct v4l2_subdev *sd = soc_camera_to_subdev(icd); 1867 return v4l2_subdev_call(sd, video, cropcap, a); 1868} 1869 1870static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a) 1871{ 1872 struct v4l2_subdev *sd = soc_camera_to_subdev(icd); 1873 return v4l2_subdev_call(sd, video, g_crop, a); 1874} 1875 1876static int default_s_crop(struct soc_camera_device *icd, const struct v4l2_crop *a) 1877{ 1878 struct v4l2_subdev *sd = soc_camera_to_subdev(icd); 1879 return v4l2_subdev_call(sd, video, s_crop, a); 1880} 1881 1882static int default_g_parm(struct soc_camera_device *icd, 1883 struct v4l2_streamparm *parm) 1884{ 1885 struct v4l2_subdev *sd = soc_camera_to_subdev(icd); 1886 return v4l2_subdev_call(sd, video, g_parm, parm); 1887} 1888 1889static int default_s_parm(struct soc_camera_device *icd, 1890 struct v4l2_streamparm *parm) 1891{ 1892 struct v4l2_subdev *sd = soc_camera_to_subdev(icd); 1893 return v4l2_subdev_call(sd, video, s_parm, parm); 1894} 1895 1896static int default_enum_framesizes(struct soc_camera_device *icd, 1897 struct v4l2_frmsizeenum *fsize) 1898{ 1899 int ret; 1900 struct v4l2_subdev *sd = soc_camera_to_subdev(icd); 1901 const struct soc_camera_format_xlate *xlate; 1902 struct v4l2_subdev_frame_size_enum fse = { 1903 .index = fsize->index, 1904 .which = V4L2_SUBDEV_FORMAT_ACTIVE, 1905 }; 1906 1907 xlate = soc_camera_xlate_by_fourcc(icd, fsize->pixel_format); 1908 if (!xlate) 1909 return -EINVAL; 1910 fse.code = xlate->code; 1911 1912 ret = v4l2_subdev_call(sd, pad, enum_frame_size, NULL, &fse); 1913 if (ret < 0) 1914 return ret; 1915 1916 if (fse.min_width == fse.max_width && 1917 fse.min_height == fse.max_height) { 1918 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE; 1919 fsize->discrete.width = fse.min_width; 1920 fsize->discrete.height = fse.min_height; 1921 return 0; 1922 } 1923 fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS; 1924 fsize->stepwise.min_width = fse.min_width; 1925 fsize->stepwise.max_width = fse.max_width; 1926 fsize->stepwise.min_height = fse.min_height; 1927 fsize->stepwise.max_height = fse.max_height; 1928 fsize->stepwise.step_width = 1; 1929 fsize->stepwise.step_height = 1; 1930 return 0; 1931} 1932 1933int soc_camera_host_register(struct soc_camera_host *ici) 1934{ 1935 struct soc_camera_host *ix; 1936 int ret; 1937 1938 if (!ici || !ici->ops || 1939 !ici->ops->try_fmt || 1940 !ici->ops->set_fmt || 1941 !ici->ops->set_bus_param || 1942 !ici->ops->querycap || 1943 ((!ici->ops->init_videobuf || 1944 !ici->ops->reqbufs) && 1945 !ici->ops->init_videobuf2) || 1946 !ici->ops->poll || 1947 !ici->v4l2_dev.dev) 1948 return -EINVAL; 1949 1950 if (!ici->ops->set_crop) 1951 ici->ops->set_crop = default_s_crop; 1952 if (!ici->ops->get_crop) 1953 ici->ops->get_crop = default_g_crop; 1954 if (!ici->ops->cropcap) 1955 ici->ops->cropcap = default_cropcap; 1956 if (!ici->ops->set_parm) 1957 ici->ops->set_parm = default_s_parm; 1958 if (!ici->ops->get_parm) 1959 ici->ops->get_parm = default_g_parm; 1960 if (!ici->ops->enum_framesizes) 1961 ici->ops->enum_framesizes = default_enum_framesizes; 1962 1963 mutex_lock(&list_lock); 1964 list_for_each_entry(ix, &hosts, list) { 1965 if (ix->nr == ici->nr) { 1966 ret = -EBUSY; 1967 goto edevreg; 1968 } 1969 } 1970 1971 ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev); 1972 if (ret < 0) 1973 goto edevreg; 1974 1975 list_add_tail(&ici->list, &hosts); 1976 mutex_unlock(&list_lock); 1977 1978 mutex_init(&ici->host_lock); 1979 mutex_init(&ici->clk_lock); 1980 1981 if (ici->v4l2_dev.dev->of_node) 1982 scan_of_host(ici); 1983 else if (ici->asd_sizes) 1984 /* 1985 * No OF, host with a list of subdevices. Don't try to mix 1986 * modes by initialising some groups statically and some 1987 * dynamically! 1988 */ 1989 scan_async_host(ici); 1990 else 1991 /* Legacy: static platform devices from board data */ 1992 scan_add_host(ici); 1993 1994 return 0; 1995 1996edevreg: 1997 mutex_unlock(&list_lock); 1998 return ret; 1999} 2000EXPORT_SYMBOL(soc_camera_host_register); 2001 2002/* Unregister all clients! */ 2003void soc_camera_host_unregister(struct soc_camera_host *ici) 2004{ 2005 struct soc_camera_device *icd, *tmp; 2006 struct soc_camera_async_client *sasc; 2007 LIST_HEAD(notifiers); 2008 2009 mutex_lock(&list_lock); 2010 list_del(&ici->list); 2011 list_for_each_entry(icd, &devices, list) 2012 if (icd->iface == ici->nr && icd->sasc) { 2013 /* as long as we hold the device, sasc won't be freed */ 2014 get_device(icd->pdev); 2015 list_add(&icd->sasc->list, ¬ifiers); 2016 } 2017 mutex_unlock(&list_lock); 2018 2019 list_for_each_entry(sasc, ¬ifiers, list) { 2020 /* Must call unlocked to avoid AB-BA dead-lock */ 2021 v4l2_async_notifier_unregister(&sasc->notifier); 2022 put_device(&sasc->pdev->dev); 2023 } 2024 2025 mutex_lock(&list_lock); 2026 2027 list_for_each_entry_safe(icd, tmp, &devices, list) 2028 if (icd->iface == ici->nr) 2029 soc_camera_remove(icd); 2030 2031 mutex_unlock(&list_lock); 2032 2033 v4l2_device_unregister(&ici->v4l2_dev); 2034} 2035EXPORT_SYMBOL(soc_camera_host_unregister); 2036 2037/* Image capture device */ 2038static int soc_camera_device_register(struct soc_camera_device *icd) 2039{ 2040 struct soc_camera_device *ix; 2041 int num = -1, i; 2042 2043 mutex_lock(&list_lock); 2044 for (i = 0; i < 256 && num < 0; i++) { 2045 num = i; 2046 /* Check if this index is available on this interface */ 2047 list_for_each_entry(ix, &devices, list) { 2048 if (ix->iface == icd->iface && ix->devnum == i) { 2049 num = -1; 2050 break; 2051 } 2052 } 2053 } 2054 2055 if (num < 0) { 2056 /* 2057 * ok, we have 256 cameras on this host... 2058 * man, stay reasonable... 2059 */ 2060 mutex_unlock(&list_lock); 2061 return -ENOMEM; 2062 } 2063 2064 icd->devnum = num; 2065 icd->use_count = 0; 2066 icd->host_priv = NULL; 2067 2068 /* 2069 * Dynamically allocated devices set the bit earlier, but it doesn't hurt setting 2070 * it again 2071 */ 2072 i = to_platform_device(icd->pdev)->id; 2073 if (i < 0) 2074 /* One static (legacy) soc-camera platform device */ 2075 i = 0; 2076 if (i >= MAP_MAX_NUM) { 2077 mutex_unlock(&list_lock); 2078 return -EBUSY; 2079 } 2080 set_bit(i, device_map); 2081 list_add_tail(&icd->list, &devices); 2082 mutex_unlock(&list_lock); 2083 2084 return 0; 2085} 2086 2087static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = { 2088 .vidioc_querycap = soc_camera_querycap, 2089 .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap, 2090 .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap, 2091 .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap, 2092 .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap, 2093 .vidioc_enum_input = soc_camera_enum_input, 2094 .vidioc_g_input = soc_camera_g_input, 2095 .vidioc_s_input = soc_camera_s_input, 2096 .vidioc_s_std = soc_camera_s_std, 2097 .vidioc_g_std = soc_camera_g_std, 2098 .vidioc_enum_framesizes = soc_camera_enum_framesizes, 2099 .vidioc_reqbufs = soc_camera_reqbufs, 2100 .vidioc_querybuf = soc_camera_querybuf, 2101 .vidioc_qbuf = soc_camera_qbuf, 2102 .vidioc_dqbuf = soc_camera_dqbuf, 2103 .vidioc_create_bufs = soc_camera_create_bufs, 2104 .vidioc_prepare_buf = soc_camera_prepare_buf, 2105 .vidioc_expbuf = soc_camera_expbuf, 2106 .vidioc_streamon = soc_camera_streamon, 2107 .vidioc_streamoff = soc_camera_streamoff, 2108 .vidioc_cropcap = soc_camera_cropcap, 2109 .vidioc_g_crop = soc_camera_g_crop, 2110 .vidioc_s_crop = soc_camera_s_crop, 2111 .vidioc_g_selection = soc_camera_g_selection, 2112 .vidioc_s_selection = soc_camera_s_selection, 2113 .vidioc_g_parm = soc_camera_g_parm, 2114 .vidioc_s_parm = soc_camera_s_parm, 2115}; 2116 2117static int video_dev_create(struct soc_camera_device *icd) 2118{ 2119 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 2120 struct video_device *vdev = video_device_alloc(); 2121 2122 if (!vdev) 2123 return -ENOMEM; 2124 2125 strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name)); 2126 2127 vdev->v4l2_dev = &ici->v4l2_dev; 2128 vdev->fops = &soc_camera_fops; 2129 vdev->ioctl_ops = &soc_camera_ioctl_ops; 2130 vdev->release = video_device_release; 2131 vdev->ctrl_handler = &icd->ctrl_handler; 2132 vdev->lock = &ici->host_lock; 2133 2134 icd->vdev = vdev; 2135 2136 return 0; 2137} 2138 2139/* 2140 * Called from soc_camera_probe() above with .host_lock held 2141 */ 2142static int soc_camera_video_start(struct soc_camera_device *icd) 2143{ 2144 const struct device_type *type = icd->vdev->dev.type; 2145 int ret; 2146 2147 if (!icd->parent) 2148 return -ENODEV; 2149 2150 video_set_drvdata(icd->vdev, icd); 2151 if (icd->vdev->tvnorms == 0) { 2152 /* disable the STD API if there are no tvnorms defined */ 2153 v4l2_disable_ioctl(icd->vdev, VIDIOC_G_STD); 2154 v4l2_disable_ioctl(icd->vdev, VIDIOC_S_STD); 2155 v4l2_disable_ioctl(icd->vdev, VIDIOC_ENUMSTD); 2156 } 2157 ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1); 2158 if (ret < 0) { 2159 dev_err(icd->pdev, "video_register_device failed: %d\n", ret); 2160 return ret; 2161 } 2162 2163 /* Restore device type, possibly set by the subdevice driver */ 2164 icd->vdev->dev.type = type; 2165 2166 return 0; 2167} 2168 2169static int soc_camera_pdrv_probe(struct platform_device *pdev) 2170{ 2171 struct soc_camera_desc *sdesc = pdev->dev.platform_data; 2172 struct soc_camera_subdev_desc *ssdd = &sdesc->subdev_desc; 2173 struct soc_camera_device *icd; 2174 int ret; 2175 2176 if (!sdesc) 2177 return -EINVAL; 2178 2179 icd = devm_kzalloc(&pdev->dev, sizeof(*icd), GFP_KERNEL); 2180 if (!icd) 2181 return -ENOMEM; 2182 2183 /* 2184 * In the asynchronous case ssdd->num_regulators == 0 yet, so, the below 2185 * regulator allocation is a dummy. They are actually requested by the 2186 * subdevice driver, using soc_camera_power_init(). Also note, that in 2187 * that case regulators are attached to the I2C device and not to the 2188 * camera platform device. 2189 */ 2190 ret = devm_regulator_bulk_get(&pdev->dev, ssdd->sd_pdata.num_regulators, 2191 ssdd->sd_pdata.regulators); 2192 if (ret < 0) 2193 return ret; 2194 2195 icd->iface = sdesc->host_desc.bus_id; 2196 icd->sdesc = sdesc; 2197 icd->pdev = &pdev->dev; 2198 platform_set_drvdata(pdev, icd); 2199 2200 icd->user_width = DEFAULT_WIDTH; 2201 icd->user_height = DEFAULT_HEIGHT; 2202 2203 return soc_camera_device_register(icd); 2204} 2205 2206/* 2207 * Only called on rmmod for each platform device, since they are not 2208 * hot-pluggable. Now we know, that all our users - hosts and devices have 2209 * been unloaded already 2210 */ 2211static int soc_camera_pdrv_remove(struct platform_device *pdev) 2212{ 2213 struct soc_camera_device *icd = platform_get_drvdata(pdev); 2214 int i; 2215 2216 if (!icd) 2217 return -EINVAL; 2218 2219 i = pdev->id; 2220 if (i < 0) 2221 i = 0; 2222 2223 /* 2224 * In synchronous mode with static platform devices this is called in a 2225 * loop from drivers/base/dd.c::driver_detach(), no parallel execution, 2226 * no need to lock. In asynchronous case the caller - 2227 * soc_camera_host_unregister() - already holds the lock 2228 */ 2229 if (test_bit(i, device_map)) { 2230 clear_bit(i, device_map); 2231 list_del(&icd->list); 2232 } 2233 2234 return 0; 2235} 2236 2237static struct platform_driver __refdata soc_camera_pdrv = { 2238 .probe = soc_camera_pdrv_probe, 2239 .remove = soc_camera_pdrv_remove, 2240 .driver = { 2241 .name = "soc-camera-pdrv", 2242 }, 2243}; 2244 2245module_platform_driver(soc_camera_pdrv); 2246 2247MODULE_DESCRIPTION("Image capture bus driver"); 2248MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>"); 2249MODULE_LICENSE("GPL"); 2250MODULE_ALIAS("platform:soc-camera-pdrv"); 2251