soc_camera.c 42.4 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/mutex.h>
25
#include <linux/module.h>
26
#include <linux/platform_device.h>
27
#include <linux/regulator/consumer.h>
28
#include <linux/slab.h>
29
#include <linux/pm_runtime.h>
30 31
#include <linux/vmalloc.h>

32
#include <media/soc_camera.h>
33
#include <media/v4l2-clk.h>
34
#include <media/v4l2-common.h>
35
#include <media/v4l2-ioctl.h>
36
#include <media/v4l2-dev.h>
37
#include <media/videobuf-core.h>
38
#include <media/videobuf2-core.h>
39
#include <media/soc_mediabus.h>
40

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

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

50 51
static LIST_HEAD(hosts);
static LIST_HEAD(devices);
52
static DEFINE_MUTEX(list_lock);		/* Protects the list of hosts */
53

54 55
int soc_camera_power_on(struct device *dev, struct soc_camera_subdev_desc *ssdd,
			struct v4l2_clk *clk)
56
{
57 58 59 60 61 62
	int ret = clk ? v4l2_clk_enable(clk) : 0;
	if (ret < 0) {
		dev_err(dev, "Cannot enable clock\n");
		return ret;
	}
	ret = regulator_bulk_enable(ssdd->num_regulators,
63
					ssdd->regulators);
64
	if (ret < 0) {
65
		dev_err(dev, "Cannot enable regulators\n");
66
		goto eregenable;;
67
	}
68

69 70
	if (ssdd->power) {
		ret = ssdd->power(dev, 1);
71
		if (ret < 0) {
72
			dev_err(dev,
73
				"Platform failed to power-on the camera.\n");
74
			goto epwron;
75
		}
76 77
	}

78 79 80 81 82 83 84 85 86
	return 0;

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

87 88
	return ret;
}
89
EXPORT_SYMBOL(soc_camera_power_on);
90

91 92
int soc_camera_power_off(struct device *dev, struct soc_camera_subdev_desc *ssdd,
			 struct v4l2_clk *clk)
93
{
94 95
	int ret = 0;
	int err;
96

97 98
	if (ssdd->power) {
		err = ssdd->power(dev, 0);
99
		if (err < 0) {
100
			dev_err(dev,
101
				"Platform failed to power-off the camera.\n");
102
			ret = err;
103 104 105
		}
	}

106 107
	err = regulator_bulk_disable(ssdd->num_regulators,
				     ssdd->regulators);
108
	if (err < 0) {
109
		dev_err(dev, "Cannot disable regulators\n");
110 111
		ret = ret ? : err;
	}
112

113 114 115
	if (clk)
		v4l2_clk_disable(clk);

116
	return ret;
117
}
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
EXPORT_SYMBOL(soc_camera_power_off);

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;
}
143

144 145 146 147 148 149 150 151 152 153 154 155
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);

156 157
/**
 * soc_camera_apply_board_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
158
 * @ssdd:	camera platform parameters
159 160 161
 * @cfg:	media bus configuration
 * @return:	resulting flags
 */
162
unsigned long soc_camera_apply_board_flags(struct soc_camera_subdev_desc *ssdd,
163 164 165 166 167
					   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 */
168
	if (ssdd->flags & SOCAM_SENSOR_INVERT_HSYNC) {
169 170 171 172 173
		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;
	}

174
	if (ssdd->flags & SOCAM_SENSOR_INVERT_VSYNC) {
175 176 177 178 179
		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;
	}

180
	if (ssdd->flags & SOCAM_SENSOR_INVERT_PCLK) {
181 182 183 184 185 186 187 188 189
		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);

190 191 192 193 194 195
#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)
{
196
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
197
	const struct soc_camera_format_xlate *xlate;
198 199 200
	struct v4l2_pix_format *pix = &f->fmt.pix;
	int ret;

201
	dev_dbg(icd->pdev, "TRY_FMT(%c%c%c%c, %ux%u)\n",
202 203
		pixfmtstr(pix->pixelformat), pix->width, pix->height);

204 205
	if (pix->pixelformat != V4L2_PIX_FMT_JPEG &&
	    !(ici->capabilities & SOCAM_HOST_CAP_STRIDE)) {
206 207 208
		pix->bytesperline = 0;
		pix->sizeimage = 0;
	}
209 210 211 212 213

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

214 215 216
	xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
	if (!xlate)
		return -EINVAL;
217

218 219 220
	ret = soc_mbus_bytes_per_line(pix->width, xlate->host_fmt);
	if (ret < 0)
		return ret;
221

222 223 224 225 226 227 228 229
	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);
230 231 232 233

	return 0;
}

234
static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
235
				      struct v4l2_format *f)
236
{
237
	struct soc_camera_device *icd = file->private_data;
238 239 240

	WARN_ON(priv != file->private_data);

241 242 243 244
	/* Only single-plane capture is supported so far */
	if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return -EINVAL;

245
	/* limit format to hardware capabilities */
246
	return soc_camera_try_fmt(icd, f);
247 248 249 250 251 252 253 254
}

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

255 256 257
	/* default is camera */
	inp->type = V4L2_INPUT_TYPE_CAMERA;
	strcpy(inp->name, "Camera");
258

259
	return 0;
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
}

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;
}

277
static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id a)
278
{
279
	struct soc_camera_device *icd = file->private_data;
280
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
281

282
	return v4l2_subdev_call(sd, core, s_std, a);
283 284
}

285 286 287 288 289 290 291 292
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);
}

293
static int soc_camera_enum_framesizes(struct file *file, void *fh,
294 295 296
					 struct v4l2_frmsizeenum *fsize)
{
	struct soc_camera_device *icd = file->private_data;
297
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
298

299
	return ici->ops->enum_framesizes(icd, fsize);
300 301
}

302 303 304 305
static int soc_camera_reqbufs(struct file *file, void *priv,
			      struct v4l2_requestbuffers *p)
{
	int ret;
306
	struct soc_camera_device *icd = file->private_data;
307
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
308 309 310

	WARN_ON(priv != file->private_data);

311 312 313
	if (icd->streamer && icd->streamer != file)
		return -EBUSY;

314 315 316 317 318 319 320 321 322
	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);
	}
323

324 325 326 327
	if (!ret && !icd->streamer)
		icd->streamer = file;

	return ret;
328 329 330 331 332
}

static int soc_camera_querybuf(struct file *file, void *priv,
			       struct v4l2_buffer *p)
{
333
	struct soc_camera_device *icd = file->private_data;
334
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
335 336 337

	WARN_ON(priv != file->private_data);

338 339 340 341
	if (ici->ops->init_videobuf)
		return videobuf_querybuf(&icd->vb_vidq, p);
	else
		return vb2_querybuf(&icd->vb2_vidq, p);
342 343 344 345 346
}

static int soc_camera_qbuf(struct file *file, void *priv,
			   struct v4l2_buffer *p)
{
347
	struct soc_camera_device *icd = file->private_data;
348
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
349 350 351

	WARN_ON(priv != file->private_data);

352 353 354
	if (icd->streamer != file)
		return -EBUSY;

355 356 357 358
	if (ici->ops->init_videobuf)
		return videobuf_qbuf(&icd->vb_vidq, p);
	else
		return vb2_qbuf(&icd->vb2_vidq, p);
359 360 361 362 363
}

static int soc_camera_dqbuf(struct file *file, void *priv,
			    struct v4l2_buffer *p)
{
364
	struct soc_camera_device *icd = file->private_data;
365
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
366 367 368

	WARN_ON(priv != file->private_data);

369 370 371
	if (icd->streamer != file)
		return -EBUSY;

372 373 374 375
	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);
376 377
}

378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
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);
}

404
/* Always entered with .host_lock held */
405 406
static int soc_camera_init_user_formats(struct soc_camera_device *icd)
{
407
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
408
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
409 410
	unsigned int i, fmts = 0, raw_fmts = 0;
	int ret;
411 412 413 414
	enum v4l2_mbus_pixelcode code;

	while (!v4l2_subdev_call(sd, video, enum_mbus_fmt, raw_fmts, &code))
		raw_fmts++;
415 416 417 418 419 420

	if (!ici->ops->get_formats)
		/*
		 * Fallback mode - the host will have to serve all
		 * sensor-provided formats one-to-one to the user
		 */
421
		fmts = raw_fmts;
422 423 424 425 426
	else
		/*
		 * First pass - only count formats this host-sensor
		 * configuration can provide
		 */
427
		for (i = 0; i < raw_fmts; i++) {
428 429 430 431 432
			ret = ici->ops->get_formats(icd, i, NULL);
			if (ret < 0)
				return ret;
			fmts += ret;
		}
433 434 435 436 437 438 439 440 441

	if (!fmts)
		return -ENXIO;

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

442
	dev_dbg(icd->pdev, "Found %d supported formats.\n", fmts);
443 444

	/* Second pass - actually fill data formats */
445
	fmts = 0;
446
	for (i = 0; i < raw_fmts; i++)
447
		if (!ici->ops->get_formats) {
448
			v4l2_subdev_call(sd, video, enum_mbus_fmt, i, &code);
449
			icd->user_formats[fmts].host_fmt =
450
				soc_mbus_get_fmtdesc(code);
451 452
			if (icd->user_formats[fmts].host_fmt)
				icd->user_formats[fmts++].code = code;
453
		} else {
454 455 456 457 458
			ret = ici->ops->get_formats(icd, i,
						    &icd->user_formats[fmts]);
			if (ret < 0)
				goto egfmt;
			fmts += ret;
459 460
		}

461
	icd->num_user_formats = fmts;
462
	icd->current_fmt = &icd->user_formats[0];
463 464

	return 0;
465 466 467 468

egfmt:
	vfree(icd->user_formats);
	return ret;
469 470
}

471
/* Always entered with .host_lock held */
472 473
static void soc_camera_free_user_formats(struct soc_camera_device *icd)
{
474
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
475 476 477

	if (ici->ops->put_formats)
		ici->ops->put_formats(icd);
478
	icd->current_fmt = NULL;
479
	icd->num_user_formats = 0;
480
	vfree(icd->user_formats);
481
	icd->user_formats = NULL;
482 483
}

484
/* Called with .vb_lock held, or from the first open(2), see comment there */
485
static int soc_camera_set_fmt(struct soc_camera_device *icd,
486 487
			      struct v4l2_format *f)
{
488
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
489 490 491
	struct v4l2_pix_format *pix = &f->fmt.pix;
	int ret;

492
	dev_dbg(icd->pdev, "S_FMT(%c%c%c%c, %ux%u)\n",
493 494
		pixfmtstr(pix->pixelformat), pix->width, pix->height);

495
	/* We always call try_fmt() before set_fmt() or set_crop() */
496
	ret = soc_camera_try_fmt(icd, f);
497 498 499 500 501 502 503
	if (ret < 0)
		return ret;

	ret = ici->ops->set_fmt(icd, f);
	if (ret < 0) {
		return ret;
	} else if (!icd->current_fmt ||
504
		   icd->current_fmt->host_fmt->fourcc != pix->pixelformat) {
505
		dev_err(icd->pdev,
506 507 508 509
			"Host driver hasn't set up current format correctly!\n");
		return -EINVAL;
	}

510 511
	icd->user_width		= pix->width;
	icd->user_height	= pix->height;
512 513
	icd->bytesperline	= pix->bytesperline;
	icd->sizeimage		= pix->sizeimage;
514
	icd->colorspace		= pix->colorspace;
515 516 517
	icd->field		= pix->field;
	if (ici->ops->init_videobuf)
		icd->vb_vidq.field = pix->field;
518

519
	dev_dbg(icd->pdev, "set width: %d height: %d\n",
520
		icd->user_width, icd->user_height);
521 522

	/* set physical bus parameters */
523
	return ici->ops->set_bus_param(icd);
524 525
}

526 527 528 529 530 531 532 533
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;

534 535 536 537 538
	if (!icd->clk) {
		ret = ici->ops->clock_start(ici);
		if (ret < 0)
			return ret;
	}
539 540 541

	if (ici->ops->add) {
		ret = ici->ops->add(icd);
542
		if (ret < 0)
543
			goto eadd;
544 545 546 547 548
	}

	ici->icd = icd;

	return 0;
549

550
eadd:
551 552
	if (!icd->clk)
		ici->ops->clock_stop(ici);
553 554 555 556 557 558 559 560 561 562
	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;

563 564
	if (ici->ops->remove)
		ici->ops->remove(icd);
565 566
	if (!icd->clk)
		ici->ops->clock_stop(ici);
567 568 569
	ici->icd = NULL;
}

570
static int soc_camera_open(struct file *file)
571
{
572
	struct video_device *vdev = video_devdata(file);
573
	struct soc_camera_device *icd;
574
	struct soc_camera_host *ici;
575 576
	int ret;

577 578
	/*
	 * Don't mess with the host during probe: wait until the loop in
579 580
	 * scan_add_host() completes. Also protect against a race with
	 * soc_camera_host_unregister().
581 582 583
	 */
	if (mutex_lock_interruptible(&list_lock))
		return -ERESTARTSYS;
584 585 586 587 588 589

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

590
	icd = video_get_drvdata(vdev);
591
	ici = to_soc_camera_host(icd->parent);
592 593

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

596
	if (ret < 0) {
597
		dev_err(icd->pdev, "Couldn't lock capture bus driver.\n");
598 599 600 601 602 603 604
		return ret;
	}

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

607 608 609 610
	if (mutex_lock_interruptible(&ici->host_lock)) {
		ret = -ERESTARTSYS;
		goto elockhost;
	}
611 612
	icd->use_count++;

613 614
	/* Now we really have to activate the camera */
	if (icd->use_count == 1) {
615
		struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
616
		/* Restore parameters before the last close() per V4L2 API */
617 618 619
		struct v4l2_format f = {
			.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
			.fmt.pix = {
620 621
				.width		= icd->user_width,
				.height		= icd->user_height,
622
				.field		= icd->field,
623 624 625
				.colorspace	= icd->colorspace,
				.pixelformat	=
					icd->current_fmt->host_fmt->fourcc,
626 627 628
			},
		};

629
		/* The camera could have been already on, try to reset */
630 631
		if (sdesc->subdev_desc.reset)
			sdesc->subdev_desc.reset(icd->pdev);
632

633
		ret = soc_camera_add_device(icd);
634
		if (ret < 0) {
635
			dev_err(icd->pdev, "Couldn't activate the camera: %d\n", ret);
636 637
			goto eiciadd;
		}
638

639
		ret = __soc_camera_power_on(icd);
640 641 642
		if (ret < 0)
			goto epower;

643 644 645 646 647
		pm_runtime_enable(&icd->vdev->dev);
		ret = pm_runtime_resume(&icd->vdev->dev);
		if (ret < 0 && ret != -ENOSYS)
			goto eresume;

648 649 650 651
		/*
		 * 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
652
		 * .host_lock is protecting us against it.
653
		 */
654
		ret = soc_camera_set_fmt(icd, &f);
655 656
		if (ret < 0)
			goto esfmt;
657

658 659 660 661 662 663 664
		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;
		}
665
		v4l2_ctrl_handler_setup(&icd->ctrl_handler);
666
	}
667
	mutex_unlock(&ici->host_lock);
668

669
	file->private_data = icd;
670
	dev_dbg(icd->pdev, "camera device open\n");
671 672 673

	return 0;

674
	/*
675
	 * First four errors are entered with the .host_lock held
676 677
	 * and use_count == 1
	 */
678
einitvb:
679
esfmt:
680 681
	pm_runtime_disable(&icd->vdev->dev);
eresume:
682
	__soc_camera_power_off(icd);
683
epower:
684
	soc_camera_remove_device(icd);
685
eiciadd:
686
	icd->use_count--;
687
	mutex_unlock(&ici->host_lock);
688 689 690
elockhost:
econtrol:
	module_put(ici->ops->owner);
691

692 693 694
	return ret;
}

695
static int soc_camera_close(struct file *file)
696
{
697
	struct soc_camera_device *icd = file->private_data;
698
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
699

700
	mutex_lock(&ici->host_lock);
701
	icd->use_count--;
702
	if (!icd->use_count) {
703 704 705
		pm_runtime_suspend(&icd->vdev->dev);
		pm_runtime_disable(&icd->vdev->dev);

706 707
		if (ici->ops->init_videobuf2)
			vb2_queue_release(&icd->vb2_vidq);
708
		__soc_camera_power_off(icd);
709

710
		soc_camera_remove_device(icd);
711
	}
712

713 714
	if (icd->streamer == file)
		icd->streamer = NULL;
715
	mutex_unlock(&ici->host_lock);
716

717
	module_put(ici->ops->owner);
718

719
	dev_dbg(icd->pdev, "camera device close\n");
720 721 722 723

	return 0;
}

724
static ssize_t soc_camera_read(struct file *file, char __user *buf,
725
			       size_t count, loff_t *ppos)
726
{
727
	struct soc_camera_device *icd = file->private_data;
728 729 730 731 732 733 734
	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);
735

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

738
	return -EINVAL;
739 740 741 742
}

static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
{
743
	struct soc_camera_device *icd = file->private_data;
744
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
745 746
	int err;

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

749 750 751
	if (icd->streamer != file)
		return -EBUSY;

752
	if (mutex_lock_interruptible(&ici->host_lock))
753
		return -ERESTARTSYS;
754 755 756 757
	if (ici->ops->init_videobuf)
		err = videobuf_mmap_mapper(&icd->vb_vidq, vma);
	else
		err = vb2_mmap(&icd->vb2_vidq, vma);
758
	mutex_unlock(&ici->host_lock);
759

760
	dev_dbg(icd->pdev, "vma start=0x%08lx, size=%ld, ret=%d\n",
761 762 763 764 765 766 767 768 769
		(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)
{
770
	struct soc_camera_device *icd = file->private_data;
771
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
772
	unsigned res = POLLERR;
773

774
	if (icd->streamer != file)
775 776
		return POLLERR;

777
	mutex_lock(&ici->host_lock);
778 779 780 781
	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);
782
	mutex_unlock(&ici->host_lock);
783
	return res;
784 785
}

786 787 788
void soc_camera_lock(struct vb2_queue *vq)
{
	struct soc_camera_device *icd = vb2_get_drv_priv(vq);
789 790
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
	mutex_lock(&ici->host_lock);
791 792 793 794 795 796
}
EXPORT_SYMBOL(soc_camera_lock);

void soc_camera_unlock(struct vb2_queue *vq)
{
	struct soc_camera_device *icd = vb2_get_drv_priv(vq);
797 798
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
	mutex_unlock(&ici->host_lock);
799 800 801
}
EXPORT_SYMBOL(soc_camera_unlock);

802
static struct v4l2_file_operations soc_camera_fops = {
803 804 805
	.owner		= THIS_MODULE,
	.open		= soc_camera_open,
	.release	= soc_camera_close,
806
	.unlocked_ioctl	= video_ioctl2,
807 808 809 810 811
	.read		= soc_camera_read,
	.mmap		= soc_camera_mmap,
	.poll		= soc_camera_poll,
};

812
static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
813
				    struct v4l2_format *f)
814
{
815
	struct soc_camera_device *icd = file->private_data;
816 817 818 819
	int ret;

	WARN_ON(priv != file->private_data);

820
	if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
821
		dev_warn(icd->pdev, "Wrong buf-type %d\n", f->type);
822 823 824
		return -EINVAL;
	}

825 826
	if (icd->streamer && icd->streamer != file)
		return -EBUSY;
827

828 829
	if (is_streaming(to_soc_camera_host(icd->parent), icd)) {
		dev_err(icd->pdev, "S_FMT denied: queue initialised\n");
830
		return -EBUSY;
831 832
	}

833 834 835 836
	ret = soc_camera_set_fmt(icd, f);

	if (!ret && !icd->streamer)
		icd->streamer = file;
837 838

	return ret;
839 840
}

841
static int soc_camera_enum_fmt_vid_cap(struct file *file, void  *priv,
842
				       struct v4l2_fmtdesc *f)
843
{
844
	struct soc_camera_device *icd = file->private_data;
845
	const struct soc_mbus_pixelfmt *format;
846 847 848

	WARN_ON(priv != file->private_data);

849
	if (f->index >= icd->num_user_formats)
850 851
		return -EINVAL;

852
	format = icd->user_formats[f->index].host_fmt;
853

854 855
	if (format->name)
		strlcpy(f->description, format->name, sizeof(f->description));
856 857 858 859
	f->pixelformat = format->fourcc;
	return 0;
}

860
static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
861
				    struct v4l2_format *f)
862
{
863
	struct soc_camera_device *icd = file->private_data;
864
	struct v4l2_pix_format *pix = &f->fmt.pix;
865 866 867

	WARN_ON(priv != file->private_data);

868 869 870
	if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return -EINVAL;

871 872
	pix->width		= icd->user_width;
	pix->height		= icd->user_height;
873 874
	pix->bytesperline	= icd->bytesperline;
	pix->sizeimage		= icd->sizeimage;
875
	pix->field		= icd->field;
876 877
	pix->pixelformat	= icd->current_fmt->host_fmt->fourcc;
	pix->colorspace		= icd->colorspace;
878
	dev_dbg(icd->pdev, "current_fmt->fourcc: 0x%08x\n",
879
		icd->current_fmt->host_fmt->fourcc);
880 881 882 883 884 885
	return 0;
}

static int soc_camera_querycap(struct file *file, void  *priv,
			       struct v4l2_capability *cap)
{
886
	struct soc_camera_device *icd = file->private_data;
887
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
888 889 890 891

	WARN_ON(priv != file->private_data);

	strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
892
	return ici->ops->querycap(ici, cap);
893 894 895 896 897
}

static int soc_camera_streamon(struct file *file, void *priv,
			       enum v4l2_buf_type i)
{
898
	struct soc_camera_device *icd = file->private_data;
899
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
900
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
901
	int ret;
902 903 904 905 906 907

	WARN_ON(priv != file->private_data);

	if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return -EINVAL;

908 909 910
	if (icd->streamer != file)
		return -EBUSY;

911
	/* This calls buf_queue from host driver's videobuf_queue_ops */
912 913 914 915 916
	if (ici->ops->init_videobuf)
		ret = videobuf_streamon(&icd->vb_vidq);
	else
		ret = vb2_streamon(&icd->vb2_vidq, i);

917 918
	if (!ret)
		v4l2_subdev_call(sd, video, s_stream, 1);
919 920

	return ret;
921 922 923 924 925
}

static int soc_camera_streamoff(struct file *file, void *priv,
				enum v4l2_buf_type i)
{
926
	struct soc_camera_device *icd = file->private_data;
927
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
928
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
929 930 931 932 933 934

	WARN_ON(priv != file->private_data);

	if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return -EINVAL;

935 936 937
	if (icd->streamer != file)
		return -EBUSY;

938 939 940 941
	/*
	 * This calls buf_release from host driver's videobuf_queue_ops for all
	 * remaining buffers. When the last buffer is freed, stop capture
	 */
942 943 944 945
	if (ici->ops->init_videobuf)
		videobuf_streamoff(&icd->vb_vidq);
	else
		vb2_streamoff(&icd->vb2_vidq, i);
946

947
	v4l2_subdev_call(sd, video, s_stream, 0);
948 949 950 951 952 953 954

	return 0;
}

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

958
	return ici->ops->cropcap(icd, a);
959 960 961 962 963
}

static int soc_camera_g_crop(struct file *file, void *fh,
			     struct v4l2_crop *a)
{
964
	struct soc_camera_device *icd = file->private_data;
965
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
966
	int ret;
967

968
	ret = ici->ops->get_crop(icd, a);
969

970
	return ret;
971 972
}

973 974 975
/*
 * 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
976
 * retrieve it.
977
 */
978
static int soc_camera_s_crop(struct file *file, void *fh,
979
			     const struct v4l2_crop *a)
980
{
981
	struct soc_camera_device *icd = file->private_data;
982
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
983
	const struct v4l2_rect *rect = &a->c;
984
	struct v4l2_crop current_crop;
985 986 987 988 989
	int ret;

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

990
	dev_dbg(icd->pdev, "S_CROP(%ux%u@%u:%u)\n",
991 992
		rect->width, rect->height, rect->left, rect->top);

993 994
	current_crop.type = a->type;

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

998
	/* Prohibit window size change with initialised buffers */
999
	if (ret < 0) {
1000
		dev_err(icd->pdev,
1001
			"S_CROP denied: getting current crop failed\n");
1002 1003 1004 1005 1006 1007 1008 1009
	} 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 {
1010
		dev_err(icd->pdev,
1011 1012 1013 1014
			"S_CROP denied: queue initialised and sizes differ\n");
		ret = -EBUSY;
	}

1015 1016 1017
	return ret;
}

1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
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 ||
1043 1044
	    (s->target != V4L2_SEL_TGT_COMPOSE &&
	     s->target != V4L2_SEL_TGT_CROP))
1045 1046
		return -EINVAL;

1047
	if (s->target == V4L2_SEL_TGT_COMPOSE) {
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
		/* 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 &&
1067
	    s->target == V4L2_SEL_TGT_COMPOSE) {
1068 1069 1070 1071 1072 1073 1074 1075 1076
		icd->user_width = s->r.width;
		icd->user_height = s->r.height;
		if (!icd->streamer)
			icd->streamer = file;
	}

	return ret;
}

1077 1078 1079
static int soc_camera_g_parm(struct file *file, void *fh,
			     struct v4l2_streamparm *a)
{
1080
	struct soc_camera_device *icd = file->private_data;
1081
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091

	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)
{
1092
	struct soc_camera_device *icd = file->private_data;
1093
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1094 1095 1096 1097 1098 1099 1100

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

	return -ENOIOCTLCMD;
}

1101 1102
static int soc_camera_probe(struct soc_camera_device *icd);

1103 1104 1105 1106 1107
/* So far this function cannot fail */
static void scan_add_host(struct soc_camera_host *ici)
{
	struct soc_camera_device *icd;

1108
	mutex_lock(&list_lock);
1109 1110 1111

	list_for_each_entry(icd, &devices, list) {
		if (icd->iface == ici->nr) {
1112
			icd->parent = ici->v4l2_dev.dev;
1113
			soc_camera_probe(icd);
1114 1115 1116
		}
	}

1117
	mutex_unlock(&list_lock);
1118 1119
}

1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
/*
 * 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;

	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
	 */
	return ici->ops->clock_start(ici);
}

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);

	ici->ops->clock_stop(ici);

	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,
};

1171 1172
#ifdef CONFIG_I2C_BOARDINFO
static int soc_camera_init_i2c(struct soc_camera_device *icd,
1173
			       struct soc_camera_desc *sdesc)
1174
{
1175
	struct i2c_client *client;
1176
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1177 1178
	struct soc_camera_host_desc *shd = &sdesc->host_desc;
	struct i2c_adapter *adap = i2c_get_adapter(shd->i2c_adapter_id);
1179
	struct v4l2_subdev *subdev;
1180 1181
	char clk_name[V4L2_SUBDEV_NAME_SIZE];
	int ret;
1182

1183
	if (!adap) {
1184
		dev_err(icd->pdev, "Cannot get I2C adapter #%d. No driver?\n",
1185
			shd->i2c_adapter_id);
1186
		return -ENODEV;
1187
	}
1188

1189
	shd->board_info->platform_data = &sdesc->subdev_desc;
1190

1191 1192 1193 1194 1195 1196 1197 1198 1199
	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;
	}

1200
	subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
1201
				shd->board_info, NULL);
1202 1203
	if (!subdev) {
		ret = -ENODEV;
1204
		goto ei2cnd;
1205
	}
1206

1207
	client = v4l2_get_subdevdata(subdev);
1208

1209
	/* Use to_i2c_client(dev) to recover the i2c client */
1210
	icd->control = &client->dev;
1211

1212 1213
	return 0;
ei2cnd:
1214 1215 1216
	v4l2_clk_unregister(icd->clk);
	icd->clk = NULL;
eclkreg:
1217
	i2c_put_adapter(adap);
1218
	return ret;
1219 1220
}

1221 1222 1223 1224
static void soc_camera_free_i2c(struct soc_camera_device *icd)
{
	struct i2c_client *client =
		to_i2c_client(to_soc_camera_control(icd));
1225
	struct i2c_adapter *adap = client->adapter;
1226 1227

	icd->control = NULL;
1228
	v4l2_device_unregister_subdev(i2c_get_clientdata(client));
1229
	i2c_unregister_device(client);
1230
	i2c_put_adapter(adap);
1231 1232
	v4l2_clk_unregister(icd->clk);
	icd->clk = NULL;
1233 1234
}
#else
1235
#define soc_camera_init_i2c(icd, sdesc)	(-ENODEV)
1236 1237 1238
#define soc_camera_free_i2c(icd)	do {} while (0)
#endif

1239
static int soc_camera_video_start(struct soc_camera_device *icd);
1240 1241
static int video_dev_create(struct soc_camera_device *icd);
/* Called during host-driver probe */
1242
static int soc_camera_probe(struct soc_camera_device *icd)
1243
{
1244
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1245 1246 1247
	struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
	struct soc_camera_host_desc *shd = &sdesc->host_desc;
	struct soc_camera_subdev_desc *ssdd = &sdesc->subdev_desc;
1248
	struct device *control = NULL;
1249
	struct v4l2_subdev *sd;
1250
	struct v4l2_mbus_framefmt mf;
1251 1252
	int ret;

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

1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
	/*
	 * 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;

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

1270
	/* Must have icd->vdev before registering the device */
1271 1272 1273
	ret = video_dev_create(icd);
	if (ret < 0)
		goto evdc;
1274

1275 1276 1277 1278 1279 1280 1281
	/*
	 * ..._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.
	 */
	mutex_lock(&ici->host_lock);

1282
	/* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
1283 1284
	if (shd->board_info) {
		ret = soc_camera_init_i2c(icd, sdesc);
1285
		if (ret < 0)
1286
			goto eadd;
1287
	} else if (!shd->add_device || !shd->del_device) {
1288
		ret = -EINVAL;
1289
		goto eadd;
1290
	} else {
1291 1292 1293 1294
		ret = ici->ops->clock_start(ici);
		if (ret < 0)
			goto eadd;

1295 1296
		if (shd->module_name)
			ret = request_module(shd->module_name);
1297

1298
		ret = shd->add_device(icd);
1299 1300
		if (ret < 0)
			goto eadddev;
1301

1302 1303 1304 1305
		/*
		 * FIXME: this is racy, have to use driver-binding notification,
		 * when it is available
		 */
1306
		control = to_soc_camera_control(icd);
1307
		if (!control || !control->driver || !dev_get_drvdata(control) ||
1308
		    !try_module_get(control->driver->owner)) {
1309
			shd->del_device(icd);
1310
			ret = -ENODEV;
1311 1312
			goto enodrv;
		}
1313
	}
1314

1315
	sd = soc_camera_to_subdev(icd);
1316 1317
	sd->grp_id = soc_camera_grp_id(icd);
	v4l2_set_subdev_hostdata(sd, icd);
1318

1319 1320
	ret = v4l2_ctrl_add_handler(&icd->ctrl_handler, sd->ctrl_handler, NULL);
	if (ret < 0)
1321 1322
		goto ectrl;

1323 1324 1325 1326 1327 1328 1329
	/* At this point client .probe() should have run already */
	ret = soc_camera_init_user_formats(icd);
	if (ret < 0)
		goto eiufmt;

	icd->field = V4L2_FIELD_ANY;

1330 1331 1332 1333
	ret = soc_camera_video_start(icd);
	if (ret < 0)
		goto evidstart;

1334
	/* Try to improve our guess of a reasonable window format */
1335 1336 1337 1338 1339
	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;
1340 1341
	}

1342 1343
	if (!shd->board_info)
		ici->ops->clock_stop(ici);
1344

1345
	mutex_unlock(&ici->host_lock);
1346

1347
	return 0;
1348

1349
evidstart:
1350 1351
	soc_camera_free_user_formats(icd);
eiufmt:
1352
ectrl:
1353
	if (shd->board_info) {
1354
		soc_camera_free_i2c(icd);
1355
	} else {
1356
		shd->del_device(icd);
1357 1358
		module_put(control->driver->owner);
enodrv:
1359
eadddev:
1360 1361 1362
		ici->ops->clock_stop(ici);
	}
eadd:
1363
	video_device_release(icd->vdev);
1364
	icd->vdev = NULL;
1365
	mutex_unlock(&ici->host_lock);
1366
evdc:
1367
	v4l2_ctrl_handler_free(&icd->ctrl_handler);
1368 1369 1370
	return ret;
}

1371 1372 1373 1374
/*
 * This is called on device_unregister, which only means we have to disconnect
 * from the host, but not remove ourselves from the device list
 */
1375
static int soc_camera_remove(struct soc_camera_device *icd)
1376
{
1377
	struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
1378
	struct video_device *vdev = icd->vdev;
1379

1380
	BUG_ON(!icd->parent);
1381

1382
	v4l2_ctrl_handler_free(&icd->ctrl_handler);
1383 1384 1385 1386 1387
	if (vdev) {
		video_unregister_device(vdev);
		icd->vdev = NULL;
	}

1388
	if (sdesc->host_desc.board_info) {
1389
		soc_camera_free_i2c(icd);
1390
	} else {
1391
		struct device_driver *drv = to_soc_camera_control(icd)->driver;
1392
		if (drv) {
1393
			sdesc->host_desc.del_device(icd);
1394 1395 1396
			module_put(drv->owner);
		}
	}
1397
	soc_camera_free_user_formats(icd);
1398

1399 1400 1401
	return 0;
}

1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
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);
}

1415
static int default_s_crop(struct soc_camera_device *icd, const struct v4l2_crop *a)
1416 1417 1418 1419 1420
{
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
	return v4l2_subdev_call(sd, video, s_crop, a);
}

1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
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);
}

1435 1436
static int default_enum_framesizes(struct soc_camera_device *icd,
				   struct v4l2_frmsizeenum *fsize)
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449
{
	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;

1450
	ret = v4l2_subdev_call(sd, video, enum_framesizes, &fsize_mbus);
1451 1452 1453 1454 1455 1456 1457 1458 1459
	if (ret < 0)
		return ret;

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

	return 0;
}

1460
int soc_camera_host_register(struct soc_camera_host *ici)
1461 1462
{
	struct soc_camera_host *ix;
1463
	int ret;
1464

1465 1466 1467 1468 1469
	if (!ici || !ici->ops ||
	    !ici->ops->try_fmt ||
	    !ici->ops->set_fmt ||
	    !ici->ops->set_bus_param ||
	    !ici->ops->querycap ||
1470 1471 1472
	    ((!ici->ops->init_videobuf ||
	      !ici->ops->reqbufs) &&
	     !ici->ops->init_videobuf2) ||
1473 1474
	    !ici->ops->clock_start ||
	    !ici->ops->clock_stop ||
1475
	    !ici->ops->poll ||
1476
	    !ici->v4l2_dev.dev)
1477 1478
		return -EINVAL;

1479 1480 1481 1482 1483 1484
	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;
1485 1486 1487 1488
	if (!ici->ops->set_parm)
		ici->ops->set_parm = default_s_parm;
	if (!ici->ops->get_parm)
		ici->ops->get_parm = default_g_parm;
1489 1490
	if (!ici->ops->enum_framesizes)
		ici->ops->enum_framesizes = default_enum_framesizes;
1491

1492 1493 1494
	mutex_lock(&list_lock);
	list_for_each_entry(ix, &hosts, list) {
		if (ix->nr == ici->nr) {
1495 1496
			ret = -EBUSY;
			goto edevreg;
1497 1498 1499
		}
	}

1500 1501 1502
	ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
	if (ret < 0)
		goto edevreg;
1503

1504 1505 1506
	list_add_tail(&ici->list, &hosts);
	mutex_unlock(&list_lock);

1507
	mutex_init(&ici->host_lock);
1508 1509 1510
	scan_add_host(ici);

	return 0;
1511 1512 1513 1514

edevreg:
	mutex_unlock(&list_lock);
	return ret;
1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525
}
EXPORT_SYMBOL(soc_camera_host_register);

/* Unregister all clients! */
void soc_camera_host_unregister(struct soc_camera_host *ici)
{
	struct soc_camera_device *icd;

	mutex_lock(&list_lock);

	list_del(&ici->list);
1526 1527 1528
	list_for_each_entry(icd, &devices, list)
		if (icd->iface == ici->nr && to_soc_camera_control(icd))
			soc_camera_remove(icd);
1529 1530 1531

	mutex_unlock(&list_lock);

1532
	v4l2_device_unregister(&ici->v4l2_dev);
1533 1534 1535 1536
}
EXPORT_SYMBOL(soc_camera_host_unregister);

/* Image capture device */
1537
static int soc_camera_device_register(struct soc_camera_device *icd)
1538 1539 1540 1541 1542 1543
{
	struct soc_camera_device *ix;
	int num = -1, i;

	for (i = 0; i < 256 && num < 0; i++) {
		num = i;
1544
		/* Check if this index is available on this interface */
1545 1546 1547 1548 1549 1550 1551 1552 1553
		list_for_each_entry(ix, &devices, list) {
			if (ix->iface == icd->iface && ix->devnum == i) {
				num = -1;
				break;
			}
		}
	}

	if (num < 0)
1554 1555 1556 1557
		/*
		 * ok, we have 256 cameras on this host...
		 * man, stay reasonable...
		 */
1558 1559
		return -ENOMEM;

1560
	icd->devnum		= num;
1561 1562
	icd->use_count		= 0;
	icd->host_priv		= NULL;
1563

1564 1565 1566
	list_add_tail(&icd->list, &devices);

	return 0;
1567 1568
}

1569 1570
static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
	.vidioc_querycap	 = soc_camera_querycap,
1571
	.vidioc_try_fmt_vid_cap  = soc_camera_try_fmt_vid_cap,
1572 1573
	.vidioc_g_fmt_vid_cap    = soc_camera_g_fmt_vid_cap,
	.vidioc_s_fmt_vid_cap    = soc_camera_s_fmt_vid_cap,
1574
	.vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
1575 1576 1577 1578
	.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,
1579
	.vidioc_g_std		 = soc_camera_g_std,
1580
	.vidioc_enum_framesizes  = soc_camera_enum_framesizes,
1581 1582 1583 1584
	.vidioc_reqbufs		 = soc_camera_reqbufs,
	.vidioc_querybuf	 = soc_camera_querybuf,
	.vidioc_qbuf		 = soc_camera_qbuf,
	.vidioc_dqbuf		 = soc_camera_dqbuf,
1585 1586
	.vidioc_create_bufs	 = soc_camera_create_bufs,
	.vidioc_prepare_buf	 = soc_camera_prepare_buf,
1587 1588 1589 1590 1591
	.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,
1592 1593
	.vidioc_g_selection	 = soc_camera_g_selection,
	.vidioc_s_selection	 = soc_camera_s_selection,
1594 1595
	.vidioc_g_parm		 = soc_camera_g_parm,
	.vidioc_s_parm		 = soc_camera_s_parm,
1596 1597
};

1598
static int video_dev_create(struct soc_camera_device *icd)
1599
{
1600
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1601
	struct video_device *vdev = video_device_alloc();
1602 1603

	if (!vdev)
1604
		return -ENOMEM;
1605 1606

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

1608
	vdev->v4l2_dev		= &ici->v4l2_dev;
1609
	vdev->fops		= &soc_camera_fops;
1610
	vdev->ioctl_ops		= &soc_camera_ioctl_ops;
1611
	vdev->release		= video_device_release;
1612
	vdev->ctrl_handler	= &icd->ctrl_handler;
1613
	vdev->lock		= &ici->host_lock;
1614 1615 1616 1617

	icd->vdev = vdev;

	return 0;
1618
}
1619

1620
/*
1621
 * Called from soc_camera_probe() above with .host_lock held
1622
 */
1623
static int soc_camera_video_start(struct soc_camera_device *icd)
1624
{
1625
	const struct device_type *type = icd->vdev->dev.type;
1626
	int ret;
1627

1628
	if (!icd->parent)
1629 1630
		return -ENODEV;

1631
	video_set_drvdata(icd->vdev, icd);
1632
	ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
1633
	if (ret < 0) {
1634
		dev_err(icd->pdev, "video_register_device failed: %d\n", ret);
1635 1636
		return ret;
	}
1637

1638 1639 1640
	/* Restore device type, possibly set by the subdevice driver */
	icd->vdev->dev.type = type;

1641
	return 0;
1642 1643
}

1644
static int soc_camera_pdrv_probe(struct platform_device *pdev)
1645
{
1646 1647
	struct soc_camera_desc *sdesc = pdev->dev.platform_data;
	struct soc_camera_subdev_desc *ssdd = &sdesc->subdev_desc;
1648
	struct soc_camera_device *icd;
1649
	int ret;
1650

1651
	if (!sdesc)
1652
		return -EINVAL;
1653

1654
	icd = devm_kzalloc(&pdev->dev, sizeof(*icd), GFP_KERNEL);
1655 1656
	if (!icd)
		return -ENOMEM;
1657

1658 1659
	ret = devm_regulator_bulk_get(&pdev->dev, ssdd->num_regulators,
				      ssdd->regulators);
1660 1661 1662
	if (ret < 0)
		return ret;

1663 1664
	icd->iface = sdesc->host_desc.bus_id;
	icd->sdesc = sdesc;
1665
	icd->pdev = &pdev->dev;
1666
	platform_set_drvdata(pdev, icd);
1667

1668 1669 1670
	icd->user_width		= DEFAULT_WIDTH;
	icd->user_height	= DEFAULT_HEIGHT;

1671
	return soc_camera_device_register(icd);
1672
}
1673

1674 1675
/*
 * Only called on rmmod for each platform device, since they are not
1676
 * hot-pluggable. Now we know, that all our users - hosts and devices have
1677 1678
 * been unloaded already
 */
1679
static int soc_camera_pdrv_remove(struct platform_device *pdev)
1680
{
1681
	struct soc_camera_device *icd = platform_get_drvdata(pdev);
1682

1683
	if (!icd)
1684 1685
		return -EINVAL;

1686
	list_del(&icd->list);
1687

1688 1689 1690 1691
	return 0;
}

static struct platform_driver __refdata soc_camera_pdrv = {
1692
	.probe = soc_camera_pdrv_probe,
1693
	.remove  = soc_camera_pdrv_remove,
1694 1695 1696
	.driver  = {
		.name	= "soc-camera-pdrv",
		.owner	= THIS_MODULE,
1697 1698 1699
	},
};

1700
module_platform_driver(soc_camera_pdrv);
1701 1702 1703 1704

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