soc_camera.c 51.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * camera image capture (abstract) bus driver
 *
 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
 *
 * This driver provides an interface between platform-specific camera
 * busses and camera devices. It should be used if the camera is
 * connected not over a "proper" bus like PCI or USB, but over a
 * special bus, like, for example, the Quick Capture interface on PXA270
 * SoCs. Later it should also be used for i.MX31 SoCs from Freescale.
 * It can handle multiple cameras and / or multiple busses, which can
 * be used, e.g., in stereo-vision applications.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/device.h>
#include <linux/err.h>
21 22 23
#include <linux/i2c.h>
#include <linux/init.h>
#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 31
#include <linux/vmalloc.h>

32
#include <media/soc_camera.h>
33 34
#include <media/soc_mediabus.h>
#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/videobuf-core.h>
40
#include <media/videobuf2-core.h>
41

42 43 44 45
/* Default to VGA resolution */
#define DEFAULT_WIDTH	640
#define DEFAULT_HEIGHT	480

46 47 48 49 50
#define is_streaming(ici, icd)				\
	(((ici)->ops->init_videobuf) ?			\
	 (icd)->vb_vidq.streaming :			\
	 vb2_is_streaming(&(icd)->vb2_vidq))

51 52
#define MAP_MAX_NUM 32
static DECLARE_BITMAP(device_map, MAP_MAX_NUM);
53 54
static LIST_HEAD(hosts);
static LIST_HEAD(devices);
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
/*
 * Protects lists and bitmaps of hosts and devices.
 * Lock nesting: Ok to take ->host_lock under list_lock.
 */
static DEFINE_MUTEX(list_lock);

struct soc_camera_async_client {
	struct v4l2_async_subdev *sensor;
	struct v4l2_async_notifier notifier;
	struct platform_device *pdev;
	struct list_head list;		/* needed for clean up */
};

static int soc_camera_video_start(struct soc_camera_device *icd);
static int video_dev_create(struct soc_camera_device *icd);
70

71 72
int soc_camera_power_on(struct device *dev, struct soc_camera_subdev_desc *ssdd,
			struct v4l2_clk *clk)
73
{
74 75
	int ret = clk ? v4l2_clk_enable(clk) : 0;
	if (ret < 0) {
76
		dev_err(dev, "Cannot enable clock: %d\n", ret);
77 78 79
		return ret;
	}
	ret = regulator_bulk_enable(ssdd->num_regulators,
80
					ssdd->regulators);
81
	if (ret < 0) {
82
		dev_err(dev, "Cannot enable regulators\n");
83
		goto eregenable;
84
	}
85

86 87
	if (ssdd->power) {
		ret = ssdd->power(dev, 1);
88
		if (ret < 0) {
89
			dev_err(dev,
90
				"Platform failed to power-on the camera.\n");
91
			goto epwron;
92
		}
93 94
	}

95 96 97 98 99 100 101 102 103
	return 0;

epwron:
	regulator_bulk_disable(ssdd->num_regulators,
			       ssdd->regulators);
eregenable:
	if (clk)
		v4l2_clk_disable(clk);

104 105
	return ret;
}
106
EXPORT_SYMBOL(soc_camera_power_on);
107

108 109
int soc_camera_power_off(struct device *dev, struct soc_camera_subdev_desc *ssdd,
			 struct v4l2_clk *clk)
110
{
111 112
	int ret = 0;
	int err;
113

114 115
	if (ssdd->power) {
		err = ssdd->power(dev, 0);
116
		if (err < 0) {
117
			dev_err(dev,
118
				"Platform failed to power-off the camera.\n");
119
			ret = err;
120 121 122
		}
	}

123 124
	err = regulator_bulk_disable(ssdd->num_regulators,
				     ssdd->regulators);
125
	if (err < 0) {
126
		dev_err(dev, "Cannot disable regulators\n");
127 128
		ret = ret ? : err;
	}
129

130 131 132
	if (clk)
		v4l2_clk_disable(clk);

133
	return ret;
134
}
135 136
EXPORT_SYMBOL(soc_camera_power_off);

137 138 139 140 141 142 143 144
int soc_camera_power_init(struct device *dev, struct soc_camera_subdev_desc *ssdd)
{

	return devm_regulator_bulk_get(dev, ssdd->num_regulators,
				       ssdd->regulators);
}
EXPORT_SYMBOL(soc_camera_power_init);

145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
static int __soc_camera_power_on(struct soc_camera_device *icd)
{
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
	int ret;

	ret = v4l2_subdev_call(sd, core, s_power, 1);
	if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
		return ret;

	return 0;
}

static int __soc_camera_power_off(struct soc_camera_device *icd)
{
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
	int ret;

	ret = v4l2_subdev_call(sd, core, s_power, 0);
	if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
		return ret;

	return 0;
}
168

169 170 171 172 173 174 175 176 177 178 179 180
const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
	struct soc_camera_device *icd, unsigned int fourcc)
{
	unsigned int i;

	for (i = 0; i < icd->num_user_formats; i++)
		if (icd->user_formats[i].host_fmt->fourcc == fourcc)
			return icd->user_formats + i;
	return NULL;
}
EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);

181 182
/**
 * soc_camera_apply_board_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
183
 * @ssdd:	camera platform parameters
184 185 186
 * @cfg:	media bus configuration
 * @return:	resulting flags
 */
187
unsigned long soc_camera_apply_board_flags(struct soc_camera_subdev_desc *ssdd,
188 189 190 191 192
					   const struct v4l2_mbus_config *cfg)
{
	unsigned long f, flags = cfg->flags;

	/* If only one of the two polarities is supported, switch to the opposite */
193
	if (ssdd->flags & SOCAM_SENSOR_INVERT_HSYNC) {
194 195 196 197 198
		f = flags & (V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW);
		if (f == V4L2_MBUS_HSYNC_ACTIVE_HIGH || f == V4L2_MBUS_HSYNC_ACTIVE_LOW)
			flags ^= V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW;
	}

199
	if (ssdd->flags & SOCAM_SENSOR_INVERT_VSYNC) {
200 201 202 203 204
		f = flags & (V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW);
		if (f == V4L2_MBUS_VSYNC_ACTIVE_HIGH || f == V4L2_MBUS_VSYNC_ACTIVE_LOW)
			flags ^= V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW;
	}

205
	if (ssdd->flags & SOCAM_SENSOR_INVERT_PCLK) {
206 207 208 209 210 211 212 213 214
		f = flags & (V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING);
		if (f == V4L2_MBUS_PCLK_SAMPLE_RISING || f == V4L2_MBUS_PCLK_SAMPLE_FALLING)
			flags ^= V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING;
	}

	return flags;
}
EXPORT_SYMBOL(soc_camera_apply_board_flags);

215 216 217 218 219 220
#define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
	((x) >> 24) & 0xff

static int soc_camera_try_fmt(struct soc_camera_device *icd,
			      struct v4l2_format *f)
{
221
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
222
	const struct soc_camera_format_xlate *xlate;
223 224 225
	struct v4l2_pix_format *pix = &f->fmt.pix;
	int ret;

226
	dev_dbg(icd->pdev, "TRY_FMT(%c%c%c%c, %ux%u)\n",
227 228
		pixfmtstr(pix->pixelformat), pix->width, pix->height);

229 230
	if (pix->pixelformat != V4L2_PIX_FMT_JPEG &&
	    !(ici->capabilities & SOCAM_HOST_CAP_STRIDE)) {
231 232 233
		pix->bytesperline = 0;
		pix->sizeimage = 0;
	}
234 235 236 237 238

	ret = ici->ops->try_fmt(icd, f);
	if (ret < 0)
		return ret;

239 240 241
	xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
	if (!xlate)
		return -EINVAL;
242

243 244 245
	ret = soc_mbus_bytes_per_line(pix->width, xlate->host_fmt);
	if (ret < 0)
		return ret;
246

247 248 249 250 251 252 253 254
	pix->bytesperline = max_t(u32, pix->bytesperline, ret);

	ret = soc_mbus_image_size(xlate->host_fmt, pix->bytesperline,
				  pix->height);
	if (ret < 0)
		return ret;

	pix->sizeimage = max_t(u32, pix->sizeimage, ret);
255 256 257 258

	return 0;
}

259
static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
260
				      struct v4l2_format *f)
261
{
262
	struct soc_camera_device *icd = file->private_data;
263 264 265

	WARN_ON(priv != file->private_data);

266 267 268 269
	/* Only single-plane capture is supported so far */
	if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return -EINVAL;

270
	/* limit format to hardware capabilities */
271
	return soc_camera_try_fmt(icd, f);
272 273 274 275 276 277 278 279
}

static int soc_camera_enum_input(struct file *file, void *priv,
				 struct v4l2_input *inp)
{
	if (inp->index != 0)
		return -EINVAL;

280 281 282
	/* default is camera */
	inp->type = V4L2_INPUT_TYPE_CAMERA;
	strcpy(inp->name, "Camera");
283

284
	return 0;
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
}

static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
{
	*i = 0;

	return 0;
}

static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
{
	if (i > 0)
		return -EINVAL;

	return 0;
}

302
static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id a)
303
{
304
	struct soc_camera_device *icd = file->private_data;
305
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
306

307
	return v4l2_subdev_call(sd, core, s_std, a);
308 309
}

310 311 312 313 314 315 316 317
static int soc_camera_g_std(struct file *file, void *priv, v4l2_std_id *a)
{
	struct soc_camera_device *icd = file->private_data;
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);

	return v4l2_subdev_call(sd, core, g_std, a);
}

318
static int soc_camera_enum_framesizes(struct file *file, void *fh,
319 320 321
					 struct v4l2_frmsizeenum *fsize)
{
	struct soc_camera_device *icd = file->private_data;
322
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
323

324
	return ici->ops->enum_framesizes(icd, fsize);
325 326
}

327 328 329 330
static int soc_camera_reqbufs(struct file *file, void *priv,
			      struct v4l2_requestbuffers *p)
{
	int ret;
331
	struct soc_camera_device *icd = file->private_data;
332
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
333 334 335

	WARN_ON(priv != file->private_data);

336 337 338
	if (icd->streamer && icd->streamer != file)
		return -EBUSY;

339 340 341 342 343 344 345 346 347
	if (ici->ops->init_videobuf) {
		ret = videobuf_reqbufs(&icd->vb_vidq, p);
		if (ret < 0)
			return ret;

		ret = ici->ops->reqbufs(icd, p);
	} else {
		ret = vb2_reqbufs(&icd->vb2_vidq, p);
	}
348

349 350 351 352
	if (!ret && !icd->streamer)
		icd->streamer = file;

	return ret;
353 354 355 356 357
}

static int soc_camera_querybuf(struct file *file, void *priv,
			       struct v4l2_buffer *p)
{
358
	struct soc_camera_device *icd = file->private_data;
359
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
360 361 362

	WARN_ON(priv != file->private_data);

363 364 365 366
	if (ici->ops->init_videobuf)
		return videobuf_querybuf(&icd->vb_vidq, p);
	else
		return vb2_querybuf(&icd->vb2_vidq, p);
367 368 369 370 371
}

static int soc_camera_qbuf(struct file *file, void *priv,
			   struct v4l2_buffer *p)
{
372
	struct soc_camera_device *icd = file->private_data;
373
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
374 375 376

	WARN_ON(priv != file->private_data);

377 378 379
	if (icd->streamer != file)
		return -EBUSY;

380 381 382 383
	if (ici->ops->init_videobuf)
		return videobuf_qbuf(&icd->vb_vidq, p);
	else
		return vb2_qbuf(&icd->vb2_vidq, p);
384 385 386 387 388
}

static int soc_camera_dqbuf(struct file *file, void *priv,
			    struct v4l2_buffer *p)
{
389
	struct soc_camera_device *icd = file->private_data;
390
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
391 392 393

	WARN_ON(priv != file->private_data);

394 395 396
	if (icd->streamer != file)
		return -EBUSY;

397 398 399 400
	if (ici->ops->init_videobuf)
		return videobuf_dqbuf(&icd->vb_vidq, p, file->f_flags & O_NONBLOCK);
	else
		return vb2_dqbuf(&icd->vb2_vidq, p, file->f_flags & O_NONBLOCK);
401 402
}

403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
static int soc_camera_create_bufs(struct file *file, void *priv,
			    struct v4l2_create_buffers *create)
{
	struct soc_camera_device *icd = file->private_data;
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);

	/* videobuf2 only */
	if (ici->ops->init_videobuf)
		return -EINVAL;
	else
		return vb2_create_bufs(&icd->vb2_vidq, create);
}

static int soc_camera_prepare_buf(struct file *file, void *priv,
				  struct v4l2_buffer *b)
{
	struct soc_camera_device *icd = file->private_data;
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);

	/* videobuf2 only */
	if (ici->ops->init_videobuf)
		return -EINVAL;
	else
		return vb2_prepare_buf(&icd->vb2_vidq, b);
}

429
/* Always entered with .host_lock held */
430 431
static int soc_camera_init_user_formats(struct soc_camera_device *icd)
{
432
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
433
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
434 435
	unsigned int i, fmts = 0, raw_fmts = 0;
	int ret;
436 437 438 439
	enum v4l2_mbus_pixelcode code;

	while (!v4l2_subdev_call(sd, video, enum_mbus_fmt, raw_fmts, &code))
		raw_fmts++;
440 441 442 443 444 445

	if (!ici->ops->get_formats)
		/*
		 * Fallback mode - the host will have to serve all
		 * sensor-provided formats one-to-one to the user
		 */
446
		fmts = raw_fmts;
447 448 449 450 451
	else
		/*
		 * First pass - only count formats this host-sensor
		 * configuration can provide
		 */
452
		for (i = 0; i < raw_fmts; i++) {
453 454 455 456 457
			ret = ici->ops->get_formats(icd, i, NULL);
			if (ret < 0)
				return ret;
			fmts += ret;
		}
458 459 460 461 462 463 464 465 466

	if (!fmts)
		return -ENXIO;

	icd->user_formats =
		vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
	if (!icd->user_formats)
		return -ENOMEM;

467
	dev_dbg(icd->pdev, "Found %d supported formats.\n", fmts);
468 469

	/* Second pass - actually fill data formats */
470
	fmts = 0;
471
	for (i = 0; i < raw_fmts; i++)
472
		if (!ici->ops->get_formats) {
473
			v4l2_subdev_call(sd, video, enum_mbus_fmt, i, &code);
474
			icd->user_formats[fmts].host_fmt =
475
				soc_mbus_get_fmtdesc(code);
476 477
			if (icd->user_formats[fmts].host_fmt)
				icd->user_formats[fmts++].code = code;
478
		} else {
479 480 481 482 483
			ret = ici->ops->get_formats(icd, i,
						    &icd->user_formats[fmts]);
			if (ret < 0)
				goto egfmt;
			fmts += ret;
484 485
		}

486
	icd->num_user_formats = fmts;
487
	icd->current_fmt = &icd->user_formats[0];
488 489

	return 0;
490 491 492 493

egfmt:
	vfree(icd->user_formats);
	return ret;
494 495
}

496
/* Always entered with .host_lock held */
497 498
static void soc_camera_free_user_formats(struct soc_camera_device *icd)
{
499
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
500 501 502

	if (ici->ops->put_formats)
		ici->ops->put_formats(icd);
503
	icd->current_fmt = NULL;
504
	icd->num_user_formats = 0;
505
	vfree(icd->user_formats);
506
	icd->user_formats = NULL;
507 508
}

509
/* Called with .vb_lock held, or from the first open(2), see comment there */
510
static int soc_camera_set_fmt(struct soc_camera_device *icd,
511 512
			      struct v4l2_format *f)
{
513
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
514 515 516
	struct v4l2_pix_format *pix = &f->fmt.pix;
	int ret;

517
	dev_dbg(icd->pdev, "S_FMT(%c%c%c%c, %ux%u)\n",
518 519
		pixfmtstr(pix->pixelformat), pix->width, pix->height);

520
	/* We always call try_fmt() before set_fmt() or set_crop() */
521
	ret = soc_camera_try_fmt(icd, f);
522 523 524 525 526 527 528
	if (ret < 0)
		return ret;

	ret = ici->ops->set_fmt(icd, f);
	if (ret < 0) {
		return ret;
	} else if (!icd->current_fmt ||
529
		   icd->current_fmt->host_fmt->fourcc != pix->pixelformat) {
530
		dev_err(icd->pdev,
531 532 533 534
			"Host driver hasn't set up current format correctly!\n");
		return -EINVAL;
	}

535 536
	icd->user_width		= pix->width;
	icd->user_height	= pix->height;
537 538
	icd->bytesperline	= pix->bytesperline;
	icd->sizeimage		= pix->sizeimage;
539
	icd->colorspace		= pix->colorspace;
540 541 542
	icd->field		= pix->field;
	if (ici->ops->init_videobuf)
		icd->vb_vidq.field = pix->field;
543

544
	dev_dbg(icd->pdev, "set width: %d height: %d\n",
545
		icd->user_width, icd->user_height);
546 547

	/* set physical bus parameters */
548
	return ici->ops->set_bus_param(icd);
549 550
}

551 552 553 554 555 556 557 558
static int soc_camera_add_device(struct soc_camera_device *icd)
{
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
	int ret;

	if (ici->icd)
		return -EBUSY;

559
	if (!icd->clk) {
560
		mutex_lock(&ici->clk_lock);
561
		ret = ici->ops->clock_start(ici);
562
		mutex_unlock(&ici->clk_lock);
563 564 565
		if (ret < 0)
			return ret;
	}
566 567 568

	if (ici->ops->add) {
		ret = ici->ops->add(icd);
569
		if (ret < 0)
570
			goto eadd;
571 572 573 574 575
	}

	ici->icd = icd;

	return 0;
576

577
eadd:
578 579
	if (!icd->clk) {
		mutex_lock(&ici->clk_lock);
580
		ici->ops->clock_stop(ici);
581 582
		mutex_unlock(&ici->clk_lock);
	}
583 584 585 586 587 588 589 590 591 592
	return ret;
}

static void soc_camera_remove_device(struct soc_camera_device *icd)
{
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);

	if (WARN_ON(icd != ici->icd))
		return;

593 594
	if (ici->ops->remove)
		ici->ops->remove(icd);
595 596
	if (!icd->clk) {
		mutex_lock(&ici->clk_lock);
597
		ici->ops->clock_stop(ici);
598 599
		mutex_unlock(&ici->clk_lock);
	}
600 601 602
	ici->icd = NULL;
}

603
static int soc_camera_open(struct file *file)
604
{
605
	struct video_device *vdev = video_devdata(file);
606
	struct soc_camera_device *icd;
607
	struct soc_camera_host *ici;
608 609
	int ret;

610 611
	/*
	 * Don't mess with the host during probe: wait until the loop in
612 613
	 * scan_add_host() completes. Also protect against a race with
	 * soc_camera_host_unregister().
614 615 616
	 */
	if (mutex_lock_interruptible(&list_lock))
		return -ERESTARTSYS;
617 618 619 620 621 622

	if (!vdev || !video_is_registered(vdev)) {
		mutex_unlock(&list_lock);
		return -ENODEV;
	}

623
	icd = video_get_drvdata(vdev);
624
	ici = to_soc_camera_host(icd->parent);
625 626

	ret = try_module_get(ici->ops->owner) ? 0 : -ENODEV;
627
	mutex_unlock(&list_lock);
628

629
	if (ret < 0) {
630
		dev_err(icd->pdev, "Couldn't lock capture bus driver.\n");
631 632 633 634 635 636 637
		return ret;
	}

	if (!to_soc_camera_control(icd)) {
		/* No device driver attached */
		ret = -ENODEV;
		goto econtrol;
638 639
	}

640 641 642 643
	if (mutex_lock_interruptible(&ici->host_lock)) {
		ret = -ERESTARTSYS;
		goto elockhost;
	}
644 645
	icd->use_count++;

646 647
	/* Now we really have to activate the camera */
	if (icd->use_count == 1) {
648
		struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
649
		/* Restore parameters before the last close() per V4L2 API */
650 651 652
		struct v4l2_format f = {
			.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
			.fmt.pix = {
653 654
				.width		= icd->user_width,
				.height		= icd->user_height,
655
				.field		= icd->field,
656 657 658
				.colorspace	= icd->colorspace,
				.pixelformat	=
					icd->current_fmt->host_fmt->fourcc,
659 660 661
			},
		};

662
		/* The camera could have been already on, try to reset */
663 664
		if (sdesc->subdev_desc.reset)
			sdesc->subdev_desc.reset(icd->pdev);
665

666
		ret = soc_camera_add_device(icd);
667
		if (ret < 0) {
668
			dev_err(icd->pdev, "Couldn't activate the camera: %d\n", ret);
669 670
			goto eiciadd;
		}
671

672
		ret = __soc_camera_power_on(icd);
673 674 675
		if (ret < 0)
			goto epower;

676 677 678 679 680
		pm_runtime_enable(&icd->vdev->dev);
		ret = pm_runtime_resume(&icd->vdev->dev);
		if (ret < 0 && ret != -ENOSYS)
			goto eresume;

681 682 683 684
		/*
		 * Try to configure with default parameters. Notice: this is the
		 * very first open, so, we cannot race against other calls,
		 * apart from someone else calling open() simultaneously, but
685
		 * .host_lock is protecting us against it.
686
		 */
687
		ret = soc_camera_set_fmt(icd, &f);
688 689
		if (ret < 0)
			goto esfmt;
690

691 692 693 694 695 696 697
		if (ici->ops->init_videobuf) {
			ici->ops->init_videobuf(&icd->vb_vidq, icd);
		} else {
			ret = ici->ops->init_videobuf2(&icd->vb2_vidq, icd);
			if (ret < 0)
				goto einitvb;
		}
698
		v4l2_ctrl_handler_setup(&icd->ctrl_handler);
699
	}
700
	mutex_unlock(&ici->host_lock);
701

702
	file->private_data = icd;
703
	dev_dbg(icd->pdev, "camera device open\n");
704 705 706

	return 0;

707
	/*
708 709
	 * All errors are entered with the .host_lock held, first four also
	 * with use_count == 1
710
	 */
711
einitvb:
712
esfmt:
713 714
	pm_runtime_disable(&icd->vdev->dev);
eresume:
715
	__soc_camera_power_off(icd);
716
epower:
717
	soc_camera_remove_device(icd);
718
eiciadd:
719
	icd->use_count--;
720
	mutex_unlock(&ici->host_lock);
721 722 723
elockhost:
econtrol:
	module_put(ici->ops->owner);
724

725 726 727
	return ret;
}

728
static int soc_camera_close(struct file *file)
729
{
730
	struct soc_camera_device *icd = file->private_data;
731
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
732

733
	mutex_lock(&ici->host_lock);
734
	icd->use_count--;
735
	if (!icd->use_count) {
736 737 738
		pm_runtime_suspend(&icd->vdev->dev);
		pm_runtime_disable(&icd->vdev->dev);

739 740
		if (ici->ops->init_videobuf2)
			vb2_queue_release(&icd->vb2_vidq);
741
		__soc_camera_power_off(icd);
742

743
		soc_camera_remove_device(icd);
744
	}
745

746 747
	if (icd->streamer == file)
		icd->streamer = NULL;
748
	mutex_unlock(&ici->host_lock);
749

750
	module_put(ici->ops->owner);
751

752
	dev_dbg(icd->pdev, "camera device close\n");
753 754 755 756

	return 0;
}

757
static ssize_t soc_camera_read(struct file *file, char __user *buf,
758
			       size_t count, loff_t *ppos)
759
{
760
	struct soc_camera_device *icd = file->private_data;
761 762 763 764 765 766 767
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);

	dev_dbg(icd->pdev, "read called, buf %p\n", buf);

	if (ici->ops->init_videobuf2 && icd->vb2_vidq.io_modes & VB2_READ)
		return vb2_read(&icd->vb2_vidq, buf, count, ppos,
				file->f_flags & O_NONBLOCK);
768

769
	dev_err(icd->pdev, "camera device read not implemented\n");
770

771
	return -EINVAL;
772 773 774 775
}

static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
{
776
	struct soc_camera_device *icd = file->private_data;
777
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
778 779
	int err;

780
	dev_dbg(icd->pdev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
781

782 783 784
	if (icd->streamer != file)
		return -EBUSY;

785
	if (mutex_lock_interruptible(&ici->host_lock))
786
		return -ERESTARTSYS;
787 788 789 790
	if (ici->ops->init_videobuf)
		err = videobuf_mmap_mapper(&icd->vb_vidq, vma);
	else
		err = vb2_mmap(&icd->vb2_vidq, vma);
791
	mutex_unlock(&ici->host_lock);
792

793
	dev_dbg(icd->pdev, "vma start=0x%08lx, size=%ld, ret=%d\n",
794 795 796 797 798 799 800 801 802
		(unsigned long)vma->vm_start,
		(unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
		err);

	return err;
}

static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
{
803
	struct soc_camera_device *icd = file->private_data;
804
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
805
	unsigned res = POLLERR;
806

807
	if (icd->streamer != file)
808 809
		return POLLERR;

810
	mutex_lock(&ici->host_lock);
811 812 813 814
	if (ici->ops->init_videobuf && list_empty(&icd->vb_vidq.stream))
		dev_err(icd->pdev, "Trying to poll with no queued buffers!\n");
	else
		res = ici->ops->poll(file, pt);
815
	mutex_unlock(&ici->host_lock);
816
	return res;
817 818
}

819 820 821
void soc_camera_lock(struct vb2_queue *vq)
{
	struct soc_camera_device *icd = vb2_get_drv_priv(vq);
822 823
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
	mutex_lock(&ici->host_lock);
824 825 826 827 828 829
}
EXPORT_SYMBOL(soc_camera_lock);

void soc_camera_unlock(struct vb2_queue *vq)
{
	struct soc_camera_device *icd = vb2_get_drv_priv(vq);
830 831
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
	mutex_unlock(&ici->host_lock);
832 833 834
}
EXPORT_SYMBOL(soc_camera_unlock);

835
static struct v4l2_file_operations soc_camera_fops = {
836 837 838
	.owner		= THIS_MODULE,
	.open		= soc_camera_open,
	.release	= soc_camera_close,
839
	.unlocked_ioctl	= video_ioctl2,
840 841 842 843 844
	.read		= soc_camera_read,
	.mmap		= soc_camera_mmap,
	.poll		= soc_camera_poll,
};

845
static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
846
				    struct v4l2_format *f)
847
{
848
	struct soc_camera_device *icd = file->private_data;
849 850 851 852
	int ret;

	WARN_ON(priv != file->private_data);

853
	if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
854
		dev_warn(icd->pdev, "Wrong buf-type %d\n", f->type);
855 856 857
		return -EINVAL;
	}

858 859
	if (icd->streamer && icd->streamer != file)
		return -EBUSY;
860

861 862
	if (is_streaming(to_soc_camera_host(icd->parent), icd)) {
		dev_err(icd->pdev, "S_FMT denied: queue initialised\n");
863
		return -EBUSY;
864 865
	}

866 867 868 869
	ret = soc_camera_set_fmt(icd, f);

	if (!ret && !icd->streamer)
		icd->streamer = file;
870 871

	return ret;
872 873
}

874
static int soc_camera_enum_fmt_vid_cap(struct file *file, void  *priv,
875
				       struct v4l2_fmtdesc *f)
876
{
877
	struct soc_camera_device *icd = file->private_data;
878
	const struct soc_mbus_pixelfmt *format;
879 880 881

	WARN_ON(priv != file->private_data);

882
	if (f->index >= icd->num_user_formats)
883 884
		return -EINVAL;

885
	format = icd->user_formats[f->index].host_fmt;
886

887 888
	if (format->name)
		strlcpy(f->description, format->name, sizeof(f->description));
889 890 891 892
	f->pixelformat = format->fourcc;
	return 0;
}

893
static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
894
				    struct v4l2_format *f)
895
{
896
	struct soc_camera_device *icd = file->private_data;
897
	struct v4l2_pix_format *pix = &f->fmt.pix;
898 899 900

	WARN_ON(priv != file->private_data);

901 902 903
	if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return -EINVAL;

904 905
	pix->width		= icd->user_width;
	pix->height		= icd->user_height;
906 907
	pix->bytesperline	= icd->bytesperline;
	pix->sizeimage		= icd->sizeimage;
908
	pix->field		= icd->field;
909 910
	pix->pixelformat	= icd->current_fmt->host_fmt->fourcc;
	pix->colorspace		= icd->colorspace;
911
	dev_dbg(icd->pdev, "current_fmt->fourcc: 0x%08x\n",
912
		icd->current_fmt->host_fmt->fourcc);
913 914 915 916 917 918
	return 0;
}

static int soc_camera_querycap(struct file *file, void  *priv,
			       struct v4l2_capability *cap)
{
919
	struct soc_camera_device *icd = file->private_data;
920
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
921 922 923 924

	WARN_ON(priv != file->private_data);

	strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
925
	return ici->ops->querycap(ici, cap);
926 927 928 929 930
}

static int soc_camera_streamon(struct file *file, void *priv,
			       enum v4l2_buf_type i)
{
931
	struct soc_camera_device *icd = file->private_data;
932
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
933
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
934
	int ret;
935 936 937 938 939 940

	WARN_ON(priv != file->private_data);

	if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return -EINVAL;

941 942 943
	if (icd->streamer != file)
		return -EBUSY;

944
	/* This calls buf_queue from host driver's videobuf_queue_ops */
945 946 947 948 949
	if (ici->ops->init_videobuf)
		ret = videobuf_streamon(&icd->vb_vidq);
	else
		ret = vb2_streamon(&icd->vb2_vidq, i);

950 951
	if (!ret)
		v4l2_subdev_call(sd, video, s_stream, 1);
952 953

	return ret;
954 955 956 957 958
}

static int soc_camera_streamoff(struct file *file, void *priv,
				enum v4l2_buf_type i)
{
959
	struct soc_camera_device *icd = file->private_data;
960
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
961
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
962 963 964 965 966 967

	WARN_ON(priv != file->private_data);

	if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return -EINVAL;

968 969 970
	if (icd->streamer != file)
		return -EBUSY;

971 972 973 974
	/*
	 * This calls buf_release from host driver's videobuf_queue_ops for all
	 * remaining buffers. When the last buffer is freed, stop capture
	 */
975 976 977 978
	if (ici->ops->init_videobuf)
		videobuf_streamoff(&icd->vb_vidq);
	else
		vb2_streamoff(&icd->vb2_vidq, i);
979

980
	v4l2_subdev_call(sd, video, s_stream, 0);
981 982 983 984 985 986 987

	return 0;
}

static int soc_camera_cropcap(struct file *file, void *fh,
			      struct v4l2_cropcap *a)
{
988
	struct soc_camera_device *icd = file->private_data;
989
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
990

991
	return ici->ops->cropcap(icd, a);
992 993 994 995 996
}

static int soc_camera_g_crop(struct file *file, void *fh,
			     struct v4l2_crop *a)
{
997
	struct soc_camera_device *icd = file->private_data;
998
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
999
	int ret;
1000

1001
	ret = ici->ops->get_crop(icd, a);
1002

1003
	return ret;
1004 1005
}

1006 1007 1008
/*
 * According to the V4L2 API, drivers shall not update the struct v4l2_crop
 * argument with the actual geometry, instead, the user shall use G_CROP to
1009
 * retrieve it.
1010
 */
1011
static int soc_camera_s_crop(struct file *file, void *fh,
1012
			     const struct v4l2_crop *a)
1013
{
1014
	struct soc_camera_device *icd = file->private_data;
1015
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1016
	const struct v4l2_rect *rect = &a->c;
1017
	struct v4l2_crop current_crop;
1018 1019 1020 1021 1022
	int ret;

	if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return -EINVAL;

1023
	dev_dbg(icd->pdev, "S_CROP(%ux%u@%u:%u)\n",
1024 1025
		rect->width, rect->height, rect->left, rect->top);

1026 1027
	current_crop.type = a->type;

1028 1029 1030
	/* If get_crop fails, we'll let host and / or client drivers decide */
	ret = ici->ops->get_crop(icd, &current_crop);

1031
	/* Prohibit window size change with initialised buffers */
1032
	if (ret < 0) {
1033
		dev_err(icd->pdev,
1034
			"S_CROP denied: getting current crop failed\n");
1035 1036 1037 1038 1039 1040 1041 1042
	} else if ((a->c.width == current_crop.c.width &&
		    a->c.height == current_crop.c.height) ||
		   !is_streaming(ici, icd)) {
		/* same size or not streaming - use .set_crop() */
		ret = ici->ops->set_crop(icd, a);
	} else if (ici->ops->set_livecrop) {
		ret = ici->ops->set_livecrop(icd, a);
	} else {
1043
		dev_err(icd->pdev,
1044 1045 1046 1047
			"S_CROP denied: queue initialised and sizes differ\n");
		ret = -EBUSY;
	}

1048 1049 1050
	return ret;
}

1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
static int soc_camera_g_selection(struct file *file, void *fh,
				  struct v4l2_selection *s)
{
	struct soc_camera_device *icd = file->private_data;
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);

	/* With a wrong type no need to try to fall back to cropping */
	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return -EINVAL;

	if (!ici->ops->get_selection)
		return -ENOTTY;

	return ici->ops->get_selection(icd, s);
}

static int soc_camera_s_selection(struct file *file, void *fh,
				  struct v4l2_selection *s)
{
	struct soc_camera_device *icd = file->private_data;
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
	int ret;

	/* In all these cases cropping emulation will not help */
	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1076 1077
	    (s->target != V4L2_SEL_TGT_COMPOSE &&
	     s->target != V4L2_SEL_TGT_CROP))
1078 1079
		return -EINVAL;

1080
	if (s->target == V4L2_SEL_TGT_COMPOSE) {
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
		/* No output size change during a running capture! */
		if (is_streaming(ici, icd) &&
		    (icd->user_width != s->r.width ||
		     icd->user_height != s->r.height))
			return -EBUSY;

		/*
		 * Only one user is allowed to change the output format, touch
		 * buffers, start / stop streaming, poll for data
		 */
		if (icd->streamer && icd->streamer != file)
			return -EBUSY;
	}

	if (!ici->ops->set_selection)
		return -ENOTTY;

	ret = ici->ops->set_selection(icd, s);
	if (!ret &&
1100
	    s->target == V4L2_SEL_TGT_COMPOSE) {
1101 1102 1103 1104 1105 1106 1107 1108 1109
		icd->user_width = s->r.width;
		icd->user_height = s->r.height;
		if (!icd->streamer)
			icd->streamer = file;
	}

	return ret;
}

1110 1111 1112
static int soc_camera_g_parm(struct file *file, void *fh,
			     struct v4l2_streamparm *a)
{
1113
	struct soc_camera_device *icd = file->private_data;
1114
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124

	if (ici->ops->get_parm)
		return ici->ops->get_parm(icd, a);

	return -ENOIOCTLCMD;
}

static int soc_camera_s_parm(struct file *file, void *fh,
			     struct v4l2_streamparm *a)
{
1125
	struct soc_camera_device *icd = file->private_data;
1126
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1127 1128 1129 1130 1131 1132 1133

	if (ici->ops->set_parm)
		return ici->ops->set_parm(icd, a);

	return -ENOIOCTLCMD;
}

1134 1135
static int soc_camera_probe(struct soc_camera_host *ici,
			    struct soc_camera_device *icd);
1136

1137 1138 1139 1140 1141
/* So far this function cannot fail */
static void scan_add_host(struct soc_camera_host *ici)
{
	struct soc_camera_device *icd;

1142
	mutex_lock(&list_lock);
1143

1144
	list_for_each_entry(icd, &devices, list)
1145
		if (icd->iface == ici->nr) {
1146 1147 1148 1149 1150 1151 1152
			struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
			struct soc_camera_subdev_desc *ssdd = &sdesc->subdev_desc;

			/* The camera could have been already on, try to reset */
			if (ssdd->reset)
				ssdd->reset(icd->pdev);

1153
			icd->parent = ici->v4l2_dev.dev;
1154 1155 1156

			/* Ignore errors */
			soc_camera_probe(ici, icd);
1157 1158
		}

1159
	mutex_unlock(&list_lock);
1160 1161
}

1162 1163 1164 1165 1166 1167 1168 1169
/*
 * It is invalid to call v4l2_clk_enable() after a successful probing
 * asynchronously outside of V4L2 operations, i.e. with .host_lock not held.
 */
static int soc_camera_clk_enable(struct v4l2_clk *clk)
{
	struct soc_camera_device *icd = clk->priv;
	struct soc_camera_host *ici;
1170
	int ret;
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183

	if (!icd || !icd->parent)
		return -ENODEV;

	ici = to_soc_camera_host(icd->parent);

	if (!try_module_get(ici->ops->owner))
		return -ENODEV;

	/*
	 * If a different client is currently being probed, the host will tell
	 * you to go
	 */
1184 1185 1186 1187
	mutex_lock(&ici->clk_lock);
	ret = ici->ops->clock_start(ici);
	mutex_unlock(&ici->clk_lock);
	return ret;
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
}

static void soc_camera_clk_disable(struct v4l2_clk *clk)
{
	struct soc_camera_device *icd = clk->priv;
	struct soc_camera_host *ici;

	if (!icd || !icd->parent)
		return;

	ici = to_soc_camera_host(icd->parent);

1200
	mutex_lock(&ici->clk_lock);
1201
	ici->ops->clock_stop(ici);
1202
	mutex_unlock(&ici->clk_lock);
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218

	module_put(ici->ops->owner);
}

/*
 * Eventually, it would be more logical to make the respective host the clock
 * owner, but then we would have to copy this struct for each ici. Besides, it
 * would introduce the circular dependency problem, unless we port all client
 * drivers to release the clock, when not in use.
 */
static const struct v4l2_clk_ops soc_camera_clk_ops = {
	.owner = THIS_MODULE,
	.enable = soc_camera_clk_enable,
	.disable = soc_camera_clk_disable,
};

1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
static int soc_camera_dyn_pdev(struct soc_camera_desc *sdesc,
			       struct soc_camera_async_client *sasc)
{
	struct platform_device *pdev;
	int ret, i;

	mutex_lock(&list_lock);
	i = find_first_zero_bit(device_map, MAP_MAX_NUM);
	if (i < MAP_MAX_NUM)
		set_bit(i, device_map);
	mutex_unlock(&list_lock);
	if (i >= MAP_MAX_NUM)
		return -ENOMEM;

	pdev = platform_device_alloc("soc-camera-pdrv", i);
	if (!pdev)
		return -ENOMEM;

	ret = platform_device_add_data(pdev, sdesc, sizeof(*sdesc));
	if (ret < 0) {
		platform_device_put(pdev);
		return ret;
	}

	sasc->pdev = pdev;

	return 0;
}

static struct soc_camera_device *soc_camera_add_pdev(struct soc_camera_async_client *sasc)
{
	struct platform_device *pdev = sasc->pdev;
	int ret;

	ret = platform_device_add(pdev);
	if (ret < 0 || !pdev->dev.driver)
		return NULL;

	return platform_get_drvdata(pdev);
}

/* Locking: called with .host_lock held */
static int soc_camera_probe_finish(struct soc_camera_device *icd)
{
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
	struct v4l2_mbus_framefmt mf;
	int ret;

	sd->grp_id = soc_camera_grp_id(icd);
	v4l2_set_subdev_hostdata(sd, icd);

	ret = v4l2_ctrl_add_handler(&icd->ctrl_handler, sd->ctrl_handler, NULL);
	if (ret < 0)
		return ret;

	ret = soc_camera_add_device(icd);
	if (ret < 0) {
		dev_err(icd->pdev, "Couldn't activate the camera: %d\n", ret);
		return ret;
	}

	/* At this point client .probe() should have run already */
	ret = soc_camera_init_user_formats(icd);
	if (ret < 0)
		goto eusrfmt;

	icd->field = V4L2_FIELD_ANY;

	ret = soc_camera_video_start(icd);
	if (ret < 0)
		goto evidstart;

	/* Try to improve our guess of a reasonable window format */
	if (!v4l2_subdev_call(sd, video, g_mbus_fmt, &mf)) {
		icd->user_width		= mf.width;
		icd->user_height	= mf.height;
		icd->colorspace		= mf.colorspace;
		icd->field		= mf.field;
	}
	soc_camera_remove_device(icd);

	return 0;

evidstart:
	soc_camera_free_user_formats(icd);
eusrfmt:
	soc_camera_remove_device(icd);

	return ret;
}

1310
#ifdef CONFIG_I2C_BOARDINFO
1311
static int soc_camera_i2c_init(struct soc_camera_device *icd,
1312
			       struct soc_camera_desc *sdesc)
1313
{
1314
	struct i2c_client *client;
1315
	struct soc_camera_host *ici;
1316
	struct soc_camera_host_desc *shd = &sdesc->host_desc;
1317
	struct i2c_adapter *adap;
1318
	struct v4l2_subdev *subdev;
1319 1320
	char clk_name[V4L2_SUBDEV_NAME_SIZE];
	int ret;
1321

1322 1323 1324 1325 1326 1327 1328 1329
	/* First find out how we link the main client */
	if (icd->sasc) {
		/* Async non-OF probing handled by the subdevice list */
		return -EPROBE_DEFER;
	}

	ici = to_soc_camera_host(icd->parent);
	adap = i2c_get_adapter(shd->i2c_adapter_id);
1330
	if (!adap) {
1331
		dev_err(icd->pdev, "Cannot get I2C adapter #%d. No driver?\n",
1332
			shd->i2c_adapter_id);
1333
		return -ENODEV;
1334
	}
1335

1336
	shd->board_info->platform_data = &sdesc->subdev_desc;
1337

1338 1339 1340 1341 1342 1343 1344 1345 1346
	snprintf(clk_name, sizeof(clk_name), "%d-%04x",
		 shd->i2c_adapter_id, shd->board_info->addr);

	icd->clk = v4l2_clk_register(&soc_camera_clk_ops, clk_name, "mclk", icd);
	if (IS_ERR(icd->clk)) {
		ret = PTR_ERR(icd->clk);
		goto eclkreg;
	}

1347
	subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
1348
				shd->board_info, NULL);
1349 1350
	if (!subdev) {
		ret = -ENODEV;
1351
		goto ei2cnd;
1352
	}
1353

1354
	client = v4l2_get_subdevdata(subdev);
1355

1356
	/* Use to_i2c_client(dev) to recover the i2c client */
1357
	icd->control = &client->dev;
1358

1359 1360
	return 0;
ei2cnd:
1361 1362
	v4l2_clk_unregister(icd->clk);
eclkreg:
1363
	icd->clk = NULL;
1364
	i2c_put_adapter(adap);
1365
	return ret;
1366 1367
}

1368
static void soc_camera_i2c_free(struct soc_camera_device *icd)
1369 1370 1371
{
	struct i2c_client *client =
		to_i2c_client(to_soc_camera_control(icd));
1372
	struct i2c_adapter *adap;
1373 1374

	icd->control = NULL;
1375 1376 1377 1378
	if (icd->sasc)
		return;

	adap = client->adapter;
1379
	v4l2_device_unregister_subdev(i2c_get_clientdata(client));
1380
	i2c_unregister_device(client);
1381
	i2c_put_adapter(adap);
1382 1383
	v4l2_clk_unregister(icd->clk);
	icd->clk = NULL;
1384
}
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544

/*
 * V4L2 asynchronous notifier callbacks. They are all called under a v4l2-async
 * internal global mutex, therefore cannot race against other asynchronous
 * events. Until notifier->complete() (soc_camera_async_complete()) is called,
 * the video device node is not registered and no V4L fops can occur. Unloading
 * of the host driver also calls a v4l2-async function, so also there we're
 * protected.
 */
static int soc_camera_async_bound(struct v4l2_async_notifier *notifier,
				  struct v4l2_subdev *sd,
				  struct v4l2_async_subdev *asd)
{
	struct soc_camera_async_client *sasc = container_of(notifier,
					struct soc_camera_async_client, notifier);
	struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev);

	if (asd == sasc->sensor && !WARN_ON(icd->control)) {
		struct i2c_client *client = v4l2_get_subdevdata(sd);

		/*
		 * Only now we get subdevice-specific information like
		 * regulators, flags, callbacks, etc.
		 */
		if (client) {
			struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
			struct soc_camera_subdev_desc *ssdd =
				soc_camera_i2c_to_desc(client);
			if (ssdd) {
				memcpy(&sdesc->subdev_desc, ssdd,
				       sizeof(sdesc->subdev_desc));
				if (ssdd->reset)
					ssdd->reset(icd->pdev);
			}

			icd->control = &client->dev;
		}
	}

	return 0;
}

static void soc_camera_async_unbind(struct v4l2_async_notifier *notifier,
				    struct v4l2_subdev *sd,
				    struct v4l2_async_subdev *asd)
{
	struct soc_camera_async_client *sasc = container_of(notifier,
					struct soc_camera_async_client, notifier);
	struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev);

	if (icd->clk) {
		v4l2_clk_unregister(icd->clk);
		icd->clk = NULL;
	}
}

static int soc_camera_async_complete(struct v4l2_async_notifier *notifier)
{
	struct soc_camera_async_client *sasc = container_of(notifier,
					struct soc_camera_async_client, notifier);
	struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev);

	if (to_soc_camera_control(icd)) {
		struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
		int ret;

		mutex_lock(&list_lock);
		ret = soc_camera_probe(ici, icd);
		mutex_unlock(&list_lock);
		if (ret < 0)
			return ret;
	}

	return 0;
}

static int scan_async_group(struct soc_camera_host *ici,
			    struct v4l2_async_subdev **asd, int size)
{
	struct soc_camera_async_subdev *sasd;
	struct soc_camera_async_client *sasc;
	struct soc_camera_device *icd;
	struct soc_camera_desc sdesc = {.host_desc.bus_id = ici->nr,};
	char clk_name[V4L2_SUBDEV_NAME_SIZE];
	int ret, i;

	/* First look for a sensor */
	for (i = 0; i < size; i++) {
		sasd = container_of(asd[i], struct soc_camera_async_subdev, asd);
		if (sasd->role == SOCAM_SUBDEV_DATA_SOURCE)
			break;
	}

	if (i == size || asd[i]->bus_type != V4L2_ASYNC_BUS_I2C) {
		/* All useless */
		dev_err(ici->v4l2_dev.dev, "No I2C data source found!\n");
		return -ENODEV;
	}

	/* Or shall this be managed by the soc-camera device? */
	sasc = devm_kzalloc(ici->v4l2_dev.dev, sizeof(*sasc), GFP_KERNEL);
	if (!sasc)
		return -ENOMEM;

	/* HACK: just need a != NULL */
	sdesc.host_desc.board_info = ERR_PTR(-ENODATA);

	ret = soc_camera_dyn_pdev(&sdesc, sasc);
	if (ret < 0)
		return ret;

	sasc->sensor = &sasd->asd;

	icd = soc_camera_add_pdev(sasc);
	if (!icd) {
		platform_device_put(sasc->pdev);
		return -ENOMEM;
	}

	sasc->notifier.subdev = asd;
	sasc->notifier.num_subdevs = size;
	sasc->notifier.bound = soc_camera_async_bound;
	sasc->notifier.unbind = soc_camera_async_unbind;
	sasc->notifier.complete = soc_camera_async_complete;

	icd->sasc = sasc;
	icd->parent = ici->v4l2_dev.dev;

	snprintf(clk_name, sizeof(clk_name), "%d-%04x",
		 sasd->asd.match.i2c.adapter_id, sasd->asd.match.i2c.address);

	icd->clk = v4l2_clk_register(&soc_camera_clk_ops, clk_name, "mclk", icd);
	if (IS_ERR(icd->clk)) {
		ret = PTR_ERR(icd->clk);
		goto eclkreg;
	}

	ret = v4l2_async_notifier_register(&ici->v4l2_dev, &sasc->notifier);
	if (!ret)
		return 0;

	v4l2_clk_unregister(icd->clk);
eclkreg:
	icd->clk = NULL;
	platform_device_unregister(sasc->pdev);
	dev_err(ici->v4l2_dev.dev, "group probe failed: %d\n", ret);

	return ret;
}

static void scan_async_host(struct soc_camera_host *ici)
{
	struct v4l2_async_subdev **asd;
	int j;

	for (j = 0, asd = ici->asd; ici->asd_sizes[j]; j++) {
		scan_async_group(ici, asd, ici->asd_sizes[j]);
		asd += ici->asd_sizes[j];
	}
}
1545
#else
1546 1547 1548
#define soc_camera_i2c_init(icd, sdesc)	(-ENODEV)
#define soc_camera_i2c_free(icd)	do {} while (0)
#define scan_async_host(ici)		do {} while (0)
1549 1550 1551
#endif

/* Called during host-driver probe */
1552 1553
static int soc_camera_probe(struct soc_camera_host *ici,
			    struct soc_camera_device *icd)
1554
{
1555 1556
	struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
	struct soc_camera_host_desc *shd = &sdesc->host_desc;
1557
	struct device *control = NULL;
1558 1559
	int ret;

1560
	dev_info(icd->pdev, "Probing %s\n", dev_name(icd->pdev));
1561

1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572
	/*
	 * Currently the subdev with the largest number of controls (13) is
	 * ov6550. So let's pick 16 as a hint for the control handler. Note
	 * that this is a hint only: too large and you waste some memory, too
	 * small and there is a (very) small performance hit when looking up
	 * controls in the internal hash.
	 */
	ret = v4l2_ctrl_handler_init(&icd->ctrl_handler, 16);
	if (ret < 0)
		return ret;

1573
	/* Must have icd->vdev before registering the device */
1574 1575 1576
	ret = video_dev_create(icd);
	if (ret < 0)
		goto evdc;
1577

1578 1579 1580 1581 1582 1583
	/*
	 * ..._video_start() will create a device node, video_register_device()
	 * itself is protected against concurrent open() calls, but we also have
	 * to protect our data also during client probing.
	 */

1584
	/* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
1585
	if (shd->board_info) {
1586 1587
		ret = soc_camera_i2c_init(icd, sdesc);
		if (ret < 0 && ret != -EPROBE_DEFER)
1588
			goto eadd;
1589
	} else if (!shd->add_device || !shd->del_device) {
1590
		ret = -EINVAL;
1591
		goto eadd;
1592
	} else {
1593
		mutex_lock(&ici->clk_lock);
1594
		ret = ici->ops->clock_start(ici);
1595
		mutex_unlock(&ici->clk_lock);
1596 1597 1598
		if (ret < 0)
			goto eadd;

1599 1600
		if (shd->module_name)
			ret = request_module(shd->module_name);
1601

1602
		ret = shd->add_device(icd);
1603 1604
		if (ret < 0)
			goto eadddev;
1605

1606 1607 1608 1609
		/*
		 * FIXME: this is racy, have to use driver-binding notification,
		 * when it is available
		 */
1610
		control = to_soc_camera_control(icd);
1611
		if (!control || !control->driver || !dev_get_drvdata(control) ||
1612
		    !try_module_get(control->driver->owner)) {
1613
			shd->del_device(icd);
1614
			ret = -ENODEV;
1615 1616
			goto enodrv;
		}
1617
	}
1618

1619 1620
	mutex_lock(&ici->host_lock);
	ret = soc_camera_probe_finish(icd);
1621
	mutex_unlock(&ici->host_lock);
1622 1623
	if (ret < 0)
		goto efinish;
1624

1625
	return 0;
1626

1627
efinish:
1628
	if (shd->board_info) {
1629
		soc_camera_i2c_free(icd);
1630
	} else {
1631
		shd->del_device(icd);
1632 1633
		module_put(control->driver->owner);
enodrv:
1634
eadddev:
1635
		mutex_lock(&ici->clk_lock);
1636
		ici->ops->clock_stop(ici);
1637
		mutex_unlock(&ici->clk_lock);
1638 1639
	}
eadd:
1640
	video_device_release(icd->vdev);
1641
	icd->vdev = NULL;
1642 1643 1644 1645
	if (icd->vdev) {
		video_device_release(icd->vdev);
		icd->vdev = NULL;
	}
1646
evdc:
1647
	v4l2_ctrl_handler_free(&icd->ctrl_handler);
1648 1649 1650
	return ret;
}

1651 1652
/*
 * This is called on device_unregister, which only means we have to disconnect
1653 1654 1655
 * from the host, but not remove ourselves from the device list. With
 * asynchronous client probing this can also be called without
 * soc_camera_probe_finish() having run. Careful with clean up.
1656
 */
1657
static int soc_camera_remove(struct soc_camera_device *icd)
1658
{
1659
	struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
1660
	struct video_device *vdev = icd->vdev;
1661

1662
	v4l2_ctrl_handler_free(&icd->ctrl_handler);
1663 1664 1665 1666 1667
	if (vdev) {
		video_unregister_device(vdev);
		icd->vdev = NULL;
	}

1668
	if (sdesc->host_desc.board_info) {
1669
		soc_camera_i2c_free(icd);
1670
	} else {
1671 1672
		struct device *dev = to_soc_camera_control(icd);
		struct device_driver *drv = dev ? dev->driver : NULL;
1673
		if (drv) {
1674
			sdesc->host_desc.del_device(icd);
1675 1676 1677
			module_put(drv->owner);
		}
	}
1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689

	if (icd->num_user_formats)
		soc_camera_free_user_formats(icd);

	if (icd->clk) {
		/* For the synchronous case */
		v4l2_clk_unregister(icd->clk);
		icd->clk = NULL;
	}

	if (icd->sasc)
		platform_device_unregister(icd->sasc->pdev);
1690

1691 1692 1693
	return 0;
}

1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706
static int default_cropcap(struct soc_camera_device *icd,
			   struct v4l2_cropcap *a)
{
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
	return v4l2_subdev_call(sd, video, cropcap, a);
}

static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
{
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
	return v4l2_subdev_call(sd, video, g_crop, a);
}

1707
static int default_s_crop(struct soc_camera_device *icd, const struct v4l2_crop *a)
1708 1709 1710 1711 1712
{
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
	return v4l2_subdev_call(sd, video, s_crop, a);
}

1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726
static int default_g_parm(struct soc_camera_device *icd,
			  struct v4l2_streamparm *parm)
{
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
	return v4l2_subdev_call(sd, video, g_parm, parm);
}

static int default_s_parm(struct soc_camera_device *icd,
			  struct v4l2_streamparm *parm)
{
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
	return v4l2_subdev_call(sd, video, s_parm, parm);
}

1727 1728
static int default_enum_framesizes(struct soc_camera_device *icd,
				   struct v4l2_frmsizeenum *fsize)
1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741
{
	int ret;
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
	const struct soc_camera_format_xlate *xlate;
	__u32 pixfmt = fsize->pixel_format;
	struct v4l2_frmsizeenum fsize_mbus = *fsize;

	xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
	if (!xlate)
		return -EINVAL;
	/* map xlate-code to pixel_format, sensor only handle xlate-code*/
	fsize_mbus.pixel_format = xlate->code;

1742
	ret = v4l2_subdev_call(sd, video, enum_framesizes, &fsize_mbus);
1743 1744 1745 1746 1747 1748 1749 1750 1751
	if (ret < 0)
		return ret;

	*fsize = fsize_mbus;
	fsize->pixel_format = pixfmt;

	return 0;
}

1752
int soc_camera_host_register(struct soc_camera_host *ici)
1753 1754
{
	struct soc_camera_host *ix;
1755
	int ret;
1756

1757 1758 1759 1760 1761
	if (!ici || !ici->ops ||
	    !ici->ops->try_fmt ||
	    !ici->ops->set_fmt ||
	    !ici->ops->set_bus_param ||
	    !ici->ops->querycap ||
1762 1763 1764
	    ((!ici->ops->init_videobuf ||
	      !ici->ops->reqbufs) &&
	     !ici->ops->init_videobuf2) ||
1765 1766
	    !ici->ops->clock_start ||
	    !ici->ops->clock_stop ||
1767
	    !ici->ops->poll ||
1768
	    !ici->v4l2_dev.dev)
1769 1770
		return -EINVAL;

1771 1772 1773 1774 1775 1776
	if (!ici->ops->set_crop)
		ici->ops->set_crop = default_s_crop;
	if (!ici->ops->get_crop)
		ici->ops->get_crop = default_g_crop;
	if (!ici->ops->cropcap)
		ici->ops->cropcap = default_cropcap;
1777 1778 1779 1780
	if (!ici->ops->set_parm)
		ici->ops->set_parm = default_s_parm;
	if (!ici->ops->get_parm)
		ici->ops->get_parm = default_g_parm;
1781 1782
	if (!ici->ops->enum_framesizes)
		ici->ops->enum_framesizes = default_enum_framesizes;
1783

1784 1785 1786
	mutex_lock(&list_lock);
	list_for_each_entry(ix, &hosts, list) {
		if (ix->nr == ici->nr) {
1787 1788
			ret = -EBUSY;
			goto edevreg;
1789 1790 1791
		}
	}

1792 1793 1794
	ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
	if (ret < 0)
		goto edevreg;
1795

1796 1797 1798
	list_add_tail(&ici->list, &hosts);
	mutex_unlock(&list_lock);

1799
	mutex_init(&ici->host_lock);
1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811
	mutex_init(&ici->clk_lock);

	if (ici->asd_sizes)
		/*
		 * No OF, host with a list of subdevices. Don't try to mix
		 * modes by initialising some groups statically and some
		 * dynamically!
		 */
		scan_async_host(ici);
	else
		/* Legacy: static platform devices from board data */
		scan_add_host(ici);
1812 1813

	return 0;
1814 1815 1816 1817

edevreg:
	mutex_unlock(&list_lock);
	return ret;
1818 1819 1820 1821 1822 1823
}
EXPORT_SYMBOL(soc_camera_host_register);

/* Unregister all clients! */
void soc_camera_host_unregister(struct soc_camera_host *ici)
{
1824 1825 1826
	struct soc_camera_device *icd, *tmp;
	struct soc_camera_async_client *sasc;
	LIST_HEAD(notifiers);
1827 1828 1829

	mutex_lock(&list_lock);
	list_del(&ici->list);
1830
	list_for_each_entry(icd, &devices, list)
1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847
		if (icd->iface == ici->nr && icd->sasc) {
			/* as long as we hold the device, sasc won't be freed */
			get_device(icd->pdev);
			list_add(&icd->sasc->list, &notifiers);
		}
	mutex_unlock(&list_lock);

	list_for_each_entry(sasc, &notifiers, list) {
		/* Must call unlocked to avoid AB-BA dead-lock */
		v4l2_async_notifier_unregister(&sasc->notifier);
		put_device(&sasc->pdev->dev);
	}

	mutex_lock(&list_lock);

	list_for_each_entry_safe(icd, tmp, &devices, list)
		if (icd->iface == ici->nr)
1848
			soc_camera_remove(icd);
1849 1850 1851

	mutex_unlock(&list_lock);

1852
	v4l2_device_unregister(&ici->v4l2_dev);
1853 1854 1855 1856
}
EXPORT_SYMBOL(soc_camera_host_unregister);

/* Image capture device */
1857
static int soc_camera_device_register(struct soc_camera_device *icd)
1858 1859 1860 1861
{
	struct soc_camera_device *ix;
	int num = -1, i;

1862
	mutex_lock(&list_lock);
1863 1864
	for (i = 0; i < 256 && num < 0; i++) {
		num = i;
1865
		/* Check if this index is available on this interface */
1866 1867 1868 1869 1870 1871 1872 1873
		list_for_each_entry(ix, &devices, list) {
			if (ix->iface == icd->iface && ix->devnum == i) {
				num = -1;
				break;
			}
		}
	}

1874
	if (num < 0) {
1875 1876 1877 1878
		/*
		 * ok, we have 256 cameras on this host...
		 * man, stay reasonable...
		 */
1879
		mutex_unlock(&list_lock);
1880
		return -ENOMEM;
1881
	}
1882

1883
	icd->devnum		= num;
1884 1885
	icd->use_count		= 0;
	icd->host_priv		= NULL;
1886

1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899
	/*
	 * Dynamically allocated devices set the bit earlier, but it doesn't hurt setting
	 * it again
	 */
	i = to_platform_device(icd->pdev)->id;
	if (i < 0)
		/* One static (legacy) soc-camera platform device */
		i = 0;
	if (i >= MAP_MAX_NUM) {
		mutex_unlock(&list_lock);
		return -EBUSY;
	}
	set_bit(i, device_map);
1900
	list_add_tail(&icd->list, &devices);
1901
	mutex_unlock(&list_lock);
1902 1903

	return 0;
1904 1905
}

1906 1907
static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
	.vidioc_querycap	 = soc_camera_querycap,
1908
	.vidioc_try_fmt_vid_cap  = soc_camera_try_fmt_vid_cap,
1909 1910
	.vidioc_g_fmt_vid_cap    = soc_camera_g_fmt_vid_cap,
	.vidioc_s_fmt_vid_cap    = soc_camera_s_fmt_vid_cap,
1911
	.vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
1912 1913 1914 1915
	.vidioc_enum_input	 = soc_camera_enum_input,
	.vidioc_g_input		 = soc_camera_g_input,
	.vidioc_s_input		 = soc_camera_s_input,
	.vidioc_s_std		 = soc_camera_s_std,
1916
	.vidioc_g_std		 = soc_camera_g_std,
1917
	.vidioc_enum_framesizes  = soc_camera_enum_framesizes,
1918 1919 1920 1921
	.vidioc_reqbufs		 = soc_camera_reqbufs,
	.vidioc_querybuf	 = soc_camera_querybuf,
	.vidioc_qbuf		 = soc_camera_qbuf,
	.vidioc_dqbuf		 = soc_camera_dqbuf,
1922 1923
	.vidioc_create_bufs	 = soc_camera_create_bufs,
	.vidioc_prepare_buf	 = soc_camera_prepare_buf,
1924 1925 1926 1927 1928
	.vidioc_streamon	 = soc_camera_streamon,
	.vidioc_streamoff	 = soc_camera_streamoff,
	.vidioc_cropcap		 = soc_camera_cropcap,
	.vidioc_g_crop		 = soc_camera_g_crop,
	.vidioc_s_crop		 = soc_camera_s_crop,
1929 1930
	.vidioc_g_selection	 = soc_camera_g_selection,
	.vidioc_s_selection	 = soc_camera_s_selection,
1931 1932
	.vidioc_g_parm		 = soc_camera_g_parm,
	.vidioc_s_parm		 = soc_camera_s_parm,
1933 1934
};

1935
static int video_dev_create(struct soc_camera_device *icd)
1936
{
1937
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1938
	struct video_device *vdev = video_device_alloc();
1939 1940

	if (!vdev)
1941
		return -ENOMEM;
1942 1943

	strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
1944

1945
	vdev->v4l2_dev		= &ici->v4l2_dev;
1946
	vdev->fops		= &soc_camera_fops;
1947
	vdev->ioctl_ops		= &soc_camera_ioctl_ops;
1948
	vdev->release		= video_device_release;
1949
	vdev->ctrl_handler	= &icd->ctrl_handler;
1950
	vdev->lock		= &ici->host_lock;
1951 1952 1953 1954

	icd->vdev = vdev;

	return 0;
1955
}
1956

1957
/*
1958
 * Called from soc_camera_probe() above with .host_lock held
1959
 */
1960
static int soc_camera_video_start(struct soc_camera_device *icd)
1961
{
1962
	const struct device_type *type = icd->vdev->dev.type;
1963
	int ret;
1964

1965
	if (!icd->parent)
1966 1967
		return -ENODEV;

1968
	video_set_drvdata(icd->vdev, icd);
1969
	ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
1970
	if (ret < 0) {
1971
		dev_err(icd->pdev, "video_register_device failed: %d\n", ret);
1972 1973
		return ret;
	}
1974

1975 1976 1977
	/* Restore device type, possibly set by the subdevice driver */
	icd->vdev->dev.type = type;

1978
	return 0;
1979 1980
}

1981
static int soc_camera_pdrv_probe(struct platform_device *pdev)
1982
{
1983 1984
	struct soc_camera_desc *sdesc = pdev->dev.platform_data;
	struct soc_camera_subdev_desc *ssdd = &sdesc->subdev_desc;
1985
	struct soc_camera_device *icd;
1986
	int ret;
1987

1988
	if (!sdesc)
1989
		return -EINVAL;
1990

1991
	icd = devm_kzalloc(&pdev->dev, sizeof(*icd), GFP_KERNEL);
1992 1993
	if (!icd)
		return -ENOMEM;
1994

1995 1996 1997 1998 1999 2000
	/*
	 * In the asynchronous case ssdd->num_regulators == 0 yet, so, the below
	 * regulator allocation is a dummy. They will be really requested later
	 * in soc_camera_async_bind(). Also note, that in that case regulators
	 * are attached to the I2C device and not to the camera platform device.
	 */
2001 2002
	ret = devm_regulator_bulk_get(&pdev->dev, ssdd->num_regulators,
				      ssdd->regulators);
2003 2004 2005
	if (ret < 0)
		return ret;

2006 2007
	icd->iface = sdesc->host_desc.bus_id;
	icd->sdesc = sdesc;
2008
	icd->pdev = &pdev->dev;
2009
	platform_set_drvdata(pdev, icd);
2010

2011 2012 2013
	icd->user_width		= DEFAULT_WIDTH;
	icd->user_height	= DEFAULT_HEIGHT;

2014
	return soc_camera_device_register(icd);
2015
}
2016

2017 2018
/*
 * Only called on rmmod for each platform device, since they are not
2019
 * hot-pluggable. Now we know, that all our users - hosts and devices have
2020 2021
 * been unloaded already
 */
2022
static int soc_camera_pdrv_remove(struct platform_device *pdev)
2023
{
2024
	struct soc_camera_device *icd = platform_get_drvdata(pdev);
2025
	int i;
2026

2027
	if (!icd)
2028 2029
		return -EINVAL;

2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043
	i = pdev->id;
	if (i < 0)
		i = 0;

	/*
	 * In synchronous mode with static platform devices this is called in a
	 * loop from drivers/base/dd.c::driver_detach(), no parallel execution,
	 * no need to lock. In asynchronous case the caller -
	 * soc_camera_host_unregister() - already holds the lock
	 */
	if (test_bit(i, device_map)) {
		clear_bit(i, device_map);
		list_del(&icd->list);
	}
2044

2045 2046 2047 2048
	return 0;
}

static struct platform_driver __refdata soc_camera_pdrv = {
2049
	.probe = soc_camera_pdrv_probe,
2050
	.remove  = soc_camera_pdrv_remove,
2051 2052 2053
	.driver  = {
		.name	= "soc-camera-pdrv",
		.owner	= THIS_MODULE,
2054 2055 2056
	},
};

2057
module_platform_driver(soc_camera_pdrv);
2058 2059 2060 2061

MODULE_DESCRIPTION("Image capture bus driver");
MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
MODULE_LICENSE("GPL");
2062
MODULE_ALIAS("platform:soc-camera-pdrv");