soc_camera.c 39.7 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-common.h>
34
#include <media/v4l2-ioctl.h>
35
#include <media/v4l2-dev.h>
36
#include <media/videobuf-core.h>
37
#include <media/videobuf2-core.h>
38
#include <media/soc_mediabus.h>
39

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

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

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

53
int soc_camera_power_on(struct device *dev, struct soc_camera_subdev_desc *ssdd)
54
{
55 56
	int ret = regulator_bulk_enable(ssdd->num_regulators,
					ssdd->regulators);
57
	if (ret < 0) {
58
		dev_err(dev, "Cannot enable regulators\n");
59 60
		return ret;
	}
61

62 63
	if (ssdd->power) {
		ret = ssdd->power(dev, 1);
64
		if (ret < 0) {
65
			dev_err(dev,
66
				"Platform failed to power-on the camera.\n");
67 68
			regulator_bulk_disable(ssdd->num_regulators,
					       ssdd->regulators);
69
		}
70 71 72 73
	}

	return ret;
}
74
EXPORT_SYMBOL(soc_camera_power_on);
75

76
int soc_camera_power_off(struct device *dev, struct soc_camera_subdev_desc *ssdd)
77
{
78 79
	int ret = 0;
	int err;
80

81 82
	if (ssdd->power) {
		err = ssdd->power(dev, 0);
83
		if (err < 0) {
84
			dev_err(dev,
85
				"Platform failed to power-off the camera.\n");
86
			ret = err;
87 88 89
		}
	}

90 91
	err = regulator_bulk_disable(ssdd->num_regulators,
				     ssdd->regulators);
92
	if (err < 0) {
93
		dev_err(dev, "Cannot disable regulators\n");
94 95
		ret = ret ? : err;
	}
96 97

	return ret;
98
}
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
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;
}
124

125 126 127 128 129 130 131 132 133 134 135 136
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);

137 138
/**
 * soc_camera_apply_board_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
139
 * @ssdd:	camera platform parameters
140 141 142
 * @cfg:	media bus configuration
 * @return:	resulting flags
 */
143
unsigned long soc_camera_apply_board_flags(struct soc_camera_subdev_desc *ssdd,
144 145 146 147 148
					   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 */
149
	if (ssdd->flags & SOCAM_SENSOR_INVERT_HSYNC) {
150 151 152 153 154
		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;
	}

155
	if (ssdd->flags & SOCAM_SENSOR_INVERT_VSYNC) {
156 157 158 159 160
		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;
	}

161
	if (ssdd->flags & SOCAM_SENSOR_INVERT_PCLK) {
162 163 164 165 166 167 168 169 170
		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);

171 172 173 174 175 176
#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)
{
177
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
178
	const struct soc_camera_format_xlate *xlate;
179 180 181
	struct v4l2_pix_format *pix = &f->fmt.pix;
	int ret;

182
	dev_dbg(icd->pdev, "TRY_FMT(%c%c%c%c, %ux%u)\n",
183 184
		pixfmtstr(pix->pixelformat), pix->width, pix->height);

185 186
	if (pix->pixelformat != V4L2_PIX_FMT_JPEG &&
	    !(ici->capabilities & SOCAM_HOST_CAP_STRIDE)) {
187 188 189
		pix->bytesperline = 0;
		pix->sizeimage = 0;
	}
190 191 192 193 194

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

195 196 197
	xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
	if (!xlate)
		return -EINVAL;
198

199 200 201
	ret = soc_mbus_bytes_per_line(pix->width, xlate->host_fmt);
	if (ret < 0)
		return ret;
202

203 204 205 206 207 208 209 210
	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);
211 212 213 214

	return 0;
}

215
static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
216
				      struct v4l2_format *f)
217
{
218
	struct soc_camera_device *icd = file->private_data;
219 220 221

	WARN_ON(priv != file->private_data);

222 223 224 225
	/* Only single-plane capture is supported so far */
	if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return -EINVAL;

226
	/* limit format to hardware capabilities */
227
	return soc_camera_try_fmt(icd, f);
228 229 230 231 232 233 234 235
}

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

236 237 238
	/* default is camera */
	inp->type = V4L2_INPUT_TYPE_CAMERA;
	strcpy(inp->name, "Camera");
239

240
	return 0;
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
}

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

258
static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id a)
259
{
260
	struct soc_camera_device *icd = file->private_data;
261
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
262

263
	return v4l2_subdev_call(sd, core, s_std, a);
264 265
}

266 267 268 269 270 271 272 273
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);
}

274
static int soc_camera_enum_framesizes(struct file *file, void *fh,
275 276 277
					 struct v4l2_frmsizeenum *fsize)
{
	struct soc_camera_device *icd = file->private_data;
278
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
279

280
	return ici->ops->enum_framesizes(icd, fsize);
281 282
}

283 284 285 286
static int soc_camera_reqbufs(struct file *file, void *priv,
			      struct v4l2_requestbuffers *p)
{
	int ret;
287
	struct soc_camera_device *icd = file->private_data;
288
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
289 290 291

	WARN_ON(priv != file->private_data);

292 293 294
	if (icd->streamer && icd->streamer != file)
		return -EBUSY;

295 296 297 298 299 300 301 302 303
	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);
	}
304

305 306 307 308
	if (!ret && !icd->streamer)
		icd->streamer = file;

	return ret;
309 310 311 312 313
}

static int soc_camera_querybuf(struct file *file, void *priv,
			       struct v4l2_buffer *p)
{
314
	struct soc_camera_device *icd = file->private_data;
315
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
316 317 318

	WARN_ON(priv != file->private_data);

319 320 321 322
	if (ici->ops->init_videobuf)
		return videobuf_querybuf(&icd->vb_vidq, p);
	else
		return vb2_querybuf(&icd->vb2_vidq, p);
323 324 325 326 327
}

static int soc_camera_qbuf(struct file *file, void *priv,
			   struct v4l2_buffer *p)
{
328
	struct soc_camera_device *icd = file->private_data;
329
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
330 331 332

	WARN_ON(priv != file->private_data);

333 334 335
	if (icd->streamer != file)
		return -EBUSY;

336 337 338 339
	if (ici->ops->init_videobuf)
		return videobuf_qbuf(&icd->vb_vidq, p);
	else
		return vb2_qbuf(&icd->vb2_vidq, p);
340 341 342 343 344
}

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

	WARN_ON(priv != file->private_data);

350 351 352
	if (icd->streamer != file)
		return -EBUSY;

353 354 355 356
	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);
357 358
}

359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
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);
}

385
/* Always entered with .host_lock held */
386 387
static int soc_camera_init_user_formats(struct soc_camera_device *icd)
{
388
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
389
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
390 391
	unsigned int i, fmts = 0, raw_fmts = 0;
	int ret;
392 393 394 395
	enum v4l2_mbus_pixelcode code;

	while (!v4l2_subdev_call(sd, video, enum_mbus_fmt, raw_fmts, &code))
		raw_fmts++;
396 397 398 399 400 401

	if (!ici->ops->get_formats)
		/*
		 * Fallback mode - the host will have to serve all
		 * sensor-provided formats one-to-one to the user
		 */
402
		fmts = raw_fmts;
403 404 405 406 407
	else
		/*
		 * First pass - only count formats this host-sensor
		 * configuration can provide
		 */
408
		for (i = 0; i < raw_fmts; i++) {
409 410 411 412 413
			ret = ici->ops->get_formats(icd, i, NULL);
			if (ret < 0)
				return ret;
			fmts += ret;
		}
414 415 416 417 418 419 420 421 422

	if (!fmts)
		return -ENXIO;

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

423
	dev_dbg(icd->pdev, "Found %d supported formats.\n", fmts);
424 425

	/* Second pass - actually fill data formats */
426
	fmts = 0;
427
	for (i = 0; i < raw_fmts; i++)
428
		if (!ici->ops->get_formats) {
429
			v4l2_subdev_call(sd, video, enum_mbus_fmt, i, &code);
430
			icd->user_formats[fmts].host_fmt =
431
				soc_mbus_get_fmtdesc(code);
432 433
			if (icd->user_formats[fmts].host_fmt)
				icd->user_formats[fmts++].code = code;
434
		} else {
435 436 437 438 439
			ret = ici->ops->get_formats(icd, i,
						    &icd->user_formats[fmts]);
			if (ret < 0)
				goto egfmt;
			fmts += ret;
440 441
		}

442
	icd->num_user_formats = fmts;
443
	icd->current_fmt = &icd->user_formats[0];
444 445

	return 0;
446 447 448 449

egfmt:
	vfree(icd->user_formats);
	return ret;
450 451
}

452
/* Always entered with .host_lock held */
453 454
static void soc_camera_free_user_formats(struct soc_camera_device *icd)
{
455
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
456 457 458

	if (ici->ops->put_formats)
		ici->ops->put_formats(icd);
459
	icd->current_fmt = NULL;
460
	icd->num_user_formats = 0;
461
	vfree(icd->user_formats);
462
	icd->user_formats = NULL;
463 464
}

465
/* Called with .vb_lock held, or from the first open(2), see comment there */
466
static int soc_camera_set_fmt(struct soc_camera_device *icd,
467 468
			      struct v4l2_format *f)
{
469
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
470 471 472
	struct v4l2_pix_format *pix = &f->fmt.pix;
	int ret;

473
	dev_dbg(icd->pdev, "S_FMT(%c%c%c%c, %ux%u)\n",
474 475
		pixfmtstr(pix->pixelformat), pix->width, pix->height);

476
	/* We always call try_fmt() before set_fmt() or set_crop() */
477
	ret = soc_camera_try_fmt(icd, f);
478 479 480 481 482 483 484
	if (ret < 0)
		return ret;

	ret = ici->ops->set_fmt(icd, f);
	if (ret < 0) {
		return ret;
	} else if (!icd->current_fmt ||
485
		   icd->current_fmt->host_fmt->fourcc != pix->pixelformat) {
486
		dev_err(icd->pdev,
487 488 489 490
			"Host driver hasn't set up current format correctly!\n");
		return -EINVAL;
	}

491 492
	icd->user_width		= pix->width;
	icd->user_height	= pix->height;
493 494
	icd->bytesperline	= pix->bytesperline;
	icd->sizeimage		= pix->sizeimage;
495
	icd->colorspace		= pix->colorspace;
496 497 498
	icd->field		= pix->field;
	if (ici->ops->init_videobuf)
		icd->vb_vidq.field = pix->field;
499

500
	dev_dbg(icd->pdev, "set width: %d height: %d\n",
501
		icd->user_width, icd->user_height);
502 503

	/* set physical bus parameters */
504
	return ici->ops->set_bus_param(icd);
505 506
}

507
static int soc_camera_open(struct file *file)
508
{
509
	struct video_device *vdev = video_devdata(file);
510
	struct soc_camera_device *icd;
511
	struct soc_camera_host *ici;
512 513
	int ret;

514 515
	/*
	 * Don't mess with the host during probe: wait until the loop in
516 517
	 * scan_add_host() completes. Also protect against a race with
	 * soc_camera_host_unregister().
518 519 520
	 */
	if (mutex_lock_interruptible(&list_lock))
		return -ERESTARTSYS;
521 522 523 524 525 526 527

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

	icd = dev_get_drvdata(vdev->parent);
528
	ici = to_soc_camera_host(icd->parent);
529 530

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

533
	if (ret < 0) {
534
		dev_err(icd->pdev, "Couldn't lock capture bus driver.\n");
535 536 537 538 539 540 541
		return ret;
	}

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

544 545 546 547
	if (mutex_lock_interruptible(&ici->host_lock)) {
		ret = -ERESTARTSYS;
		goto elockhost;
	}
548 549
	icd->use_count++;

550 551
	/* Now we really have to activate the camera */
	if (icd->use_count == 1) {
552
		struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
553
		/* Restore parameters before the last close() per V4L2 API */
554 555 556
		struct v4l2_format f = {
			.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
			.fmt.pix = {
557 558
				.width		= icd->user_width,
				.height		= icd->user_height,
559
				.field		= icd->field,
560 561 562
				.colorspace	= icd->colorspace,
				.pixelformat	=
					icd->current_fmt->host_fmt->fourcc,
563 564 565
			},
		};

566
		/* The camera could have been already on, try to reset */
567 568
		if (sdesc->subdev_desc.reset)
			sdesc->subdev_desc.reset(icd->pdev);
569

570
		ret = ici->ops->add(icd);
571
		if (ret < 0) {
572
			dev_err(icd->pdev, "Couldn't activate the camera: %d\n", ret);
573 574
			goto eiciadd;
		}
575

576
		ret = __soc_camera_power_on(icd);
577 578 579
		if (ret < 0)
			goto epower;

580 581 582 583 584
		pm_runtime_enable(&icd->vdev->dev);
		ret = pm_runtime_resume(&icd->vdev->dev);
		if (ret < 0 && ret != -ENOSYS)
			goto eresume;

585 586 587 588
		/*
		 * 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
589
		 * .host_lock is protecting us against it.
590
		 */
591
		ret = soc_camera_set_fmt(icd, &f);
592 593
		if (ret < 0)
			goto esfmt;
594

595 596 597 598 599 600 601
		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;
		}
602
		v4l2_ctrl_handler_setup(&icd->ctrl_handler);
603
	}
604
	mutex_unlock(&ici->host_lock);
605

606
	file->private_data = icd;
607
	dev_dbg(icd->pdev, "camera device open\n");
608 609 610

	return 0;

611
	/*
612
	 * First four errors are entered with the .host_lock held
613 614
	 * and use_count == 1
	 */
615
einitvb:
616
esfmt:
617 618
	pm_runtime_disable(&icd->vdev->dev);
eresume:
619
	__soc_camera_power_off(icd);
620
epower:
621 622
	ici->ops->remove(icd);
eiciadd:
623
	icd->use_count--;
624
	mutex_unlock(&ici->host_lock);
625 626 627
elockhost:
econtrol:
	module_put(ici->ops->owner);
628

629 630 631
	return ret;
}

632
static int soc_camera_close(struct file *file)
633
{
634
	struct soc_camera_device *icd = file->private_data;
635
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
636

637
	mutex_lock(&ici->host_lock);
638
	icd->use_count--;
639
	if (!icd->use_count) {
640 641 642
		pm_runtime_suspend(&icd->vdev->dev);
		pm_runtime_disable(&icd->vdev->dev);

643 644
		if (ici->ops->init_videobuf2)
			vb2_queue_release(&icd->vb2_vidq);
645
		__soc_camera_power_off(icd);
646 647

		ici->ops->remove(icd);
648
	}
649

650 651
	if (icd->streamer == file)
		icd->streamer = NULL;
652
	mutex_unlock(&ici->host_lock);
653

654
	module_put(ici->ops->owner);
655

656
	dev_dbg(icd->pdev, "camera device close\n");
657 658 659 660

	return 0;
}

661
static ssize_t soc_camera_read(struct file *file, char __user *buf,
662
			       size_t count, loff_t *ppos)
663
{
664
	struct soc_camera_device *icd = file->private_data;
665 666 667 668 669 670 671
	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);
672

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

675
	return -EINVAL;
676 677 678 679
}

static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
{
680
	struct soc_camera_device *icd = file->private_data;
681
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
682 683
	int err;

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

686 687 688
	if (icd->streamer != file)
		return -EBUSY;

689
	if (mutex_lock_interruptible(&ici->host_lock))
690
		return -ERESTARTSYS;
691 692 693 694
	if (ici->ops->init_videobuf)
		err = videobuf_mmap_mapper(&icd->vb_vidq, vma);
	else
		err = vb2_mmap(&icd->vb2_vidq, vma);
695
	mutex_unlock(&ici->host_lock);
696

697
	dev_dbg(icd->pdev, "vma start=0x%08lx, size=%ld, ret=%d\n",
698 699 700 701 702 703 704 705 706
		(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)
{
707
	struct soc_camera_device *icd = file->private_data;
708
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
709
	unsigned res = POLLERR;
710

711
	if (icd->streamer != file)
712 713
		return POLLERR;

714
	mutex_lock(&ici->host_lock);
715 716 717 718
	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);
719
	mutex_unlock(&ici->host_lock);
720
	return res;
721 722
}

723 724 725
void soc_camera_lock(struct vb2_queue *vq)
{
	struct soc_camera_device *icd = vb2_get_drv_priv(vq);
726 727
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
	mutex_lock(&ici->host_lock);
728 729 730 731 732 733
}
EXPORT_SYMBOL(soc_camera_lock);

void soc_camera_unlock(struct vb2_queue *vq)
{
	struct soc_camera_device *icd = vb2_get_drv_priv(vq);
734 735
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
	mutex_unlock(&ici->host_lock);
736 737 738
}
EXPORT_SYMBOL(soc_camera_unlock);

739
static struct v4l2_file_operations soc_camera_fops = {
740 741 742
	.owner		= THIS_MODULE,
	.open		= soc_camera_open,
	.release	= soc_camera_close,
743
	.unlocked_ioctl	= video_ioctl2,
744 745 746 747 748
	.read		= soc_camera_read,
	.mmap		= soc_camera_mmap,
	.poll		= soc_camera_poll,
};

749
static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
750
				    struct v4l2_format *f)
751
{
752
	struct soc_camera_device *icd = file->private_data;
753 754 755 756
	int ret;

	WARN_ON(priv != file->private_data);

757
	if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
758
		dev_warn(icd->pdev, "Wrong buf-type %d\n", f->type);
759 760 761
		return -EINVAL;
	}

762 763
	if (icd->streamer && icd->streamer != file)
		return -EBUSY;
764

765 766
	if (is_streaming(to_soc_camera_host(icd->parent), icd)) {
		dev_err(icd->pdev, "S_FMT denied: queue initialised\n");
767
		return -EBUSY;
768 769
	}

770 771 772 773
	ret = soc_camera_set_fmt(icd, f);

	if (!ret && !icd->streamer)
		icd->streamer = file;
774 775

	return ret;
776 777
}

778
static int soc_camera_enum_fmt_vid_cap(struct file *file, void  *priv,
779
				       struct v4l2_fmtdesc *f)
780
{
781
	struct soc_camera_device *icd = file->private_data;
782
	const struct soc_mbus_pixelfmt *format;
783 784 785

	WARN_ON(priv != file->private_data);

786
	if (f->index >= icd->num_user_formats)
787 788
		return -EINVAL;

789
	format = icd->user_formats[f->index].host_fmt;
790

791 792
	if (format->name)
		strlcpy(f->description, format->name, sizeof(f->description));
793 794 795 796
	f->pixelformat = format->fourcc;
	return 0;
}

797
static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
798
				    struct v4l2_format *f)
799
{
800
	struct soc_camera_device *icd = file->private_data;
801
	struct v4l2_pix_format *pix = &f->fmt.pix;
802 803 804

	WARN_ON(priv != file->private_data);

805 806 807
	if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return -EINVAL;

808 809
	pix->width		= icd->user_width;
	pix->height		= icd->user_height;
810 811
	pix->bytesperline	= icd->bytesperline;
	pix->sizeimage		= icd->sizeimage;
812
	pix->field		= icd->field;
813 814
	pix->pixelformat	= icd->current_fmt->host_fmt->fourcc;
	pix->colorspace		= icd->colorspace;
815
	dev_dbg(icd->pdev, "current_fmt->fourcc: 0x%08x\n",
816
		icd->current_fmt->host_fmt->fourcc);
817 818 819 820 821 822
	return 0;
}

static int soc_camera_querycap(struct file *file, void  *priv,
			       struct v4l2_capability *cap)
{
823
	struct soc_camera_device *icd = file->private_data;
824
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
825 826 827 828

	WARN_ON(priv != file->private_data);

	strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
829
	return ici->ops->querycap(ici, cap);
830 831 832 833 834
}

static int soc_camera_streamon(struct file *file, void *priv,
			       enum v4l2_buf_type i)
{
835
	struct soc_camera_device *icd = file->private_data;
836
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
837
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
838
	int ret;
839 840 841 842 843 844

	WARN_ON(priv != file->private_data);

	if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return -EINVAL;

845 846 847
	if (icd->streamer != file)
		return -EBUSY;

848
	/* This calls buf_queue from host driver's videobuf_queue_ops */
849 850 851 852 853
	if (ici->ops->init_videobuf)
		ret = videobuf_streamon(&icd->vb_vidq);
	else
		ret = vb2_streamon(&icd->vb2_vidq, i);

854 855
	if (!ret)
		v4l2_subdev_call(sd, video, s_stream, 1);
856 857

	return ret;
858 859 860 861 862
}

static int soc_camera_streamoff(struct file *file, void *priv,
				enum v4l2_buf_type i)
{
863
	struct soc_camera_device *icd = file->private_data;
864
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
865
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
866 867 868 869 870 871

	WARN_ON(priv != file->private_data);

	if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return -EINVAL;

872 873 874
	if (icd->streamer != file)
		return -EBUSY;

875 876 877 878
	/*
	 * This calls buf_release from host driver's videobuf_queue_ops for all
	 * remaining buffers. When the last buffer is freed, stop capture
	 */
879 880 881 882
	if (ici->ops->init_videobuf)
		videobuf_streamoff(&icd->vb_vidq);
	else
		vb2_streamoff(&icd->vb2_vidq, i);
883

884
	v4l2_subdev_call(sd, video, s_stream, 0);
885 886 887 888 889 890 891

	return 0;
}

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

895
	return ici->ops->cropcap(icd, a);
896 897 898 899 900
}

static int soc_camera_g_crop(struct file *file, void *fh,
			     struct v4l2_crop *a)
{
901
	struct soc_camera_device *icd = file->private_data;
902
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
903
	int ret;
904

905
	ret = ici->ops->get_crop(icd, a);
906

907
	return ret;
908 909
}

910 911 912
/*
 * 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
913
 * retrieve it.
914
 */
915
static int soc_camera_s_crop(struct file *file, void *fh,
916
			     const struct v4l2_crop *a)
917
{
918
	struct soc_camera_device *icd = file->private_data;
919
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
920
	const struct v4l2_rect *rect = &a->c;
921
	struct v4l2_crop current_crop;
922 923 924 925 926
	int ret;

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

927
	dev_dbg(icd->pdev, "S_CROP(%ux%u@%u:%u)\n",
928 929
		rect->width, rect->height, rect->left, rect->top);

930 931
	current_crop.type = a->type;

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

935
	/* Prohibit window size change with initialised buffers */
936
	if (ret < 0) {
937
		dev_err(icd->pdev,
938
			"S_CROP denied: getting current crop failed\n");
939 940 941 942 943 944 945 946
	} 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 {
947
		dev_err(icd->pdev,
948 949 950 951
			"S_CROP denied: queue initialised and sizes differ\n");
		ret = -EBUSY;
	}

952 953 954
	return ret;
}

955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
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 ||
980 981
	    (s->target != V4L2_SEL_TGT_COMPOSE &&
	     s->target != V4L2_SEL_TGT_CROP))
982 983
		return -EINVAL;

984
	if (s->target == V4L2_SEL_TGT_COMPOSE) {
985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
		/* 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 &&
1004
	    s->target == V4L2_SEL_TGT_COMPOSE) {
1005 1006 1007 1008 1009 1010 1011 1012 1013
		icd->user_width = s->r.width;
		icd->user_height = s->r.height;
		if (!icd->streamer)
			icd->streamer = file;
	}

	return ret;
}

1014 1015 1016
static int soc_camera_g_parm(struct file *file, void *fh,
			     struct v4l2_streamparm *a)
{
1017
	struct soc_camera_device *icd = file->private_data;
1018
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028

	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)
{
1029
	struct soc_camera_device *icd = file->private_data;
1030
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1031 1032 1033 1034 1035 1036 1037

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

	return -ENOIOCTLCMD;
}

1038 1039
static int soc_camera_probe(struct soc_camera_device *icd);

1040 1041 1042 1043 1044
/* So far this function cannot fail */
static void scan_add_host(struct soc_camera_host *ici)
{
	struct soc_camera_device *icd;

1045
	mutex_lock(&list_lock);
1046 1047 1048

	list_for_each_entry(icd, &devices, list) {
		if (icd->iface == ici->nr) {
1049
			icd->parent = ici->v4l2_dev.dev;
1050
			soc_camera_probe(icd);
1051 1052 1053
		}
	}

1054
	mutex_unlock(&list_lock);
1055 1056
}

1057 1058
#ifdef CONFIG_I2C_BOARDINFO
static int soc_camera_init_i2c(struct soc_camera_device *icd,
1059
			       struct soc_camera_desc *sdesc)
1060
{
1061
	struct i2c_client *client;
1062
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1063 1064
	struct soc_camera_host_desc *shd = &sdesc->host_desc;
	struct i2c_adapter *adap = i2c_get_adapter(shd->i2c_adapter_id);
1065
	struct v4l2_subdev *subdev;
1066

1067
	if (!adap) {
1068
		dev_err(icd->pdev, "Cannot get I2C adapter #%d. No driver?\n",
1069
			shd->i2c_adapter_id);
1070 1071
		goto ei2cga;
	}
1072

1073
	shd->board_info->platform_data = &sdesc->subdev_desc;
1074

1075
	subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
1076
				shd->board_info, NULL);
1077
	if (!subdev)
1078
		goto ei2cnd;
1079

1080
	client = v4l2_get_subdevdata(subdev);
1081

1082
	/* Use to_i2c_client(dev) to recover the i2c client */
1083
	icd->control = &client->dev;
1084

1085 1086 1087 1088
	return 0;
ei2cnd:
	i2c_put_adapter(adap);
ei2cga:
1089
	return -ENODEV;
1090 1091
}

1092 1093 1094 1095
static void soc_camera_free_i2c(struct soc_camera_device *icd)
{
	struct i2c_client *client =
		to_i2c_client(to_soc_camera_control(icd));
1096
	struct i2c_adapter *adap = client->adapter;
1097 1098

	icd->control = NULL;
1099
	v4l2_device_unregister_subdev(i2c_get_clientdata(client));
1100
	i2c_unregister_device(client);
1101
	i2c_put_adapter(adap);
1102 1103
}
#else
1104
#define soc_camera_init_i2c(icd, sdesc)	(-ENODEV)
1105 1106 1107
#define soc_camera_free_i2c(icd)	do {} while (0)
#endif

1108
static int soc_camera_video_start(struct soc_camera_device *icd);
1109 1110
static int video_dev_create(struct soc_camera_device *icd);
/* Called during host-driver probe */
1111
static int soc_camera_probe(struct soc_camera_device *icd)
1112
{
1113
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1114 1115 1116
	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;
1117
	struct device *control = NULL;
1118
	struct v4l2_subdev *sd;
1119
	struct v4l2_mbus_framefmt mf;
1120 1121
	int ret;

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

1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
	/*
	 * 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;

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

1139
	mutex_lock(&ici->host_lock);
1140
	ret = ici->ops->add(icd);
1141
	mutex_unlock(&ici->host_lock);
1142 1143 1144
	if (ret < 0)
		goto eadd;

1145
	/* Must have icd->vdev before registering the device */
1146 1147 1148
	ret = video_dev_create(icd);
	if (ret < 0)
		goto evdc;
1149

1150
	/* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
1151 1152
	if (shd->board_info) {
		ret = soc_camera_init_i2c(icd, sdesc);
1153 1154
		if (ret < 0)
			goto eadddev;
1155
	} else if (!shd->add_device || !shd->del_device) {
1156
		ret = -EINVAL;
1157 1158
		goto eadddev;
	} else {
1159 1160
		if (shd->module_name)
			ret = request_module(shd->module_name);
1161

1162
		ret = shd->add_device(icd);
1163 1164
		if (ret < 0)
			goto eadddev;
1165

1166 1167 1168 1169
		/*
		 * FIXME: this is racy, have to use driver-binding notification,
		 * when it is available
		 */
1170
		control = to_soc_camera_control(icd);
1171
		if (!control || !control->driver || !dev_get_drvdata(control) ||
1172
		    !try_module_get(control->driver->owner)) {
1173
			shd->del_device(icd);
1174
			ret = -ENODEV;
1175 1176
			goto enodrv;
		}
1177
	}
1178

1179
	sd = soc_camera_to_subdev(icd);
1180 1181
	sd->grp_id = soc_camera_grp_id(icd);
	v4l2_set_subdev_hostdata(sd, icd);
1182

1183 1184
	ret = v4l2_ctrl_add_handler(&icd->ctrl_handler, sd->ctrl_handler, NULL);
	if (ret < 0)
1185 1186
		goto ectrl;

1187 1188 1189 1190 1191 1192 1193
	/* 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;

1194 1195 1196 1197 1198
	/*
	 * ..._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.
	 */
1199
	mutex_lock(&ici->host_lock);
1200 1201 1202 1203 1204

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

1205
	/* Try to improve our guess of a reasonable window format */
1206 1207 1208 1209 1210
	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;
1211 1212
	}

1213 1214
	ici->ops->remove(icd);

1215
	mutex_unlock(&ici->host_lock);
1216

1217
	return 0;
1218

1219
evidstart:
1220
	mutex_unlock(&ici->host_lock);
1221 1222
	soc_camera_free_user_formats(icd);
eiufmt:
1223
ectrl:
1224
	if (shd->board_info) {
1225
		soc_camera_free_i2c(icd);
1226
	} else {
1227
		shd->del_device(icd);
1228 1229 1230
		module_put(control->driver->owner);
	}
enodrv:
1231 1232
eadddev:
	video_device_release(icd->vdev);
1233
	icd->vdev = NULL;
1234
evdc:
1235
	mutex_lock(&ici->host_lock);
1236
	ici->ops->remove(icd);
1237
	mutex_unlock(&ici->host_lock);
1238
eadd:
1239
	v4l2_ctrl_handler_free(&icd->ctrl_handler);
1240 1241 1242
	return ret;
}

1243 1244 1245 1246
/*
 * This is called on device_unregister, which only means we have to disconnect
 * from the host, but not remove ourselves from the device list
 */
1247
static int soc_camera_remove(struct soc_camera_device *icd)
1248
{
1249
	struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
1250
	struct video_device *vdev = icd->vdev;
1251

1252
	BUG_ON(!icd->parent);
1253

1254
	v4l2_ctrl_handler_free(&icd->ctrl_handler);
1255 1256 1257 1258 1259
	if (vdev) {
		video_unregister_device(vdev);
		icd->vdev = NULL;
	}

1260
	if (sdesc->host_desc.board_info) {
1261
		soc_camera_free_i2c(icd);
1262
	} else {
1263
		struct device_driver *drv = to_soc_camera_control(icd)->driver;
1264
		if (drv) {
1265
			sdesc->host_desc.del_device(icd);
1266 1267 1268
			module_put(drv->owner);
		}
	}
1269
	soc_camera_free_user_formats(icd);
1270

1271 1272 1273
	return 0;
}

1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
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);
}

1287
static int default_s_crop(struct soc_camera_device *icd, const struct v4l2_crop *a)
1288 1289 1290 1291 1292
{
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
	return v4l2_subdev_call(sd, video, s_crop, a);
}

1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
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);
}

1307 1308
static int default_enum_framesizes(struct soc_camera_device *icd,
				   struct v4l2_frmsizeenum *fsize)
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321
{
	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;

1322
	ret = v4l2_subdev_call(sd, video, enum_framesizes, &fsize_mbus);
1323 1324 1325 1326 1327 1328 1329 1330 1331
	if (ret < 0)
		return ret;

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

	return 0;
}

1332
int soc_camera_host_register(struct soc_camera_host *ici)
1333 1334
{
	struct soc_camera_host *ix;
1335
	int ret;
1336

1337 1338 1339 1340 1341
	if (!ici || !ici->ops ||
	    !ici->ops->try_fmt ||
	    !ici->ops->set_fmt ||
	    !ici->ops->set_bus_param ||
	    !ici->ops->querycap ||
1342 1343 1344
	    ((!ici->ops->init_videobuf ||
	      !ici->ops->reqbufs) &&
	     !ici->ops->init_videobuf2) ||
1345 1346
	    !ici->ops->add ||
	    !ici->ops->remove ||
1347
	    !ici->ops->poll ||
1348
	    !ici->v4l2_dev.dev)
1349 1350
		return -EINVAL;

1351 1352 1353 1354 1355 1356
	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;
1357 1358 1359 1360
	if (!ici->ops->set_parm)
		ici->ops->set_parm = default_s_parm;
	if (!ici->ops->get_parm)
		ici->ops->get_parm = default_g_parm;
1361 1362
	if (!ici->ops->enum_framesizes)
		ici->ops->enum_framesizes = default_enum_framesizes;
1363

1364 1365 1366
	mutex_lock(&list_lock);
	list_for_each_entry(ix, &hosts, list) {
		if (ix->nr == ici->nr) {
1367 1368
			ret = -EBUSY;
			goto edevreg;
1369 1370 1371
		}
	}

1372 1373 1374
	ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
	if (ret < 0)
		goto edevreg;
1375

1376 1377 1378
	list_add_tail(&ici->list, &hosts);
	mutex_unlock(&list_lock);

1379
	mutex_init(&ici->host_lock);
1380 1381 1382
	scan_add_host(ici);

	return 0;
1383 1384 1385 1386

edevreg:
	mutex_unlock(&list_lock);
	return ret;
1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
}
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);
1398 1399 1400
	list_for_each_entry(icd, &devices, list)
		if (icd->iface == ici->nr && to_soc_camera_control(icd))
			soc_camera_remove(icd);
1401 1402 1403

	mutex_unlock(&list_lock);

1404
	v4l2_device_unregister(&ici->v4l2_dev);
1405 1406 1407 1408
}
EXPORT_SYMBOL(soc_camera_host_unregister);

/* Image capture device */
1409
static int soc_camera_device_register(struct soc_camera_device *icd)
1410 1411 1412 1413 1414 1415
{
	struct soc_camera_device *ix;
	int num = -1, i;

	for (i = 0; i < 256 && num < 0; i++) {
		num = i;
1416
		/* Check if this index is available on this interface */
1417 1418 1419 1420 1421 1422 1423 1424 1425
		list_for_each_entry(ix, &devices, list) {
			if (ix->iface == icd->iface && ix->devnum == i) {
				num = -1;
				break;
			}
		}
	}

	if (num < 0)
1426 1427 1428 1429
		/*
		 * ok, we have 256 cameras on this host...
		 * man, stay reasonable...
		 */
1430 1431
		return -ENOMEM;

1432
	icd->devnum		= num;
1433 1434
	icd->use_count		= 0;
	icd->host_priv		= NULL;
1435

1436 1437 1438
	list_add_tail(&icd->list, &devices);

	return 0;
1439 1440
}

1441 1442
static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
	.vidioc_querycap	 = soc_camera_querycap,
1443
	.vidioc_try_fmt_vid_cap  = soc_camera_try_fmt_vid_cap,
1444 1445
	.vidioc_g_fmt_vid_cap    = soc_camera_g_fmt_vid_cap,
	.vidioc_s_fmt_vid_cap    = soc_camera_s_fmt_vid_cap,
1446
	.vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
1447 1448 1449 1450
	.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,
1451
	.vidioc_g_std		 = soc_camera_g_std,
1452
	.vidioc_enum_framesizes  = soc_camera_enum_framesizes,
1453 1454 1455 1456
	.vidioc_reqbufs		 = soc_camera_reqbufs,
	.vidioc_querybuf	 = soc_camera_querybuf,
	.vidioc_qbuf		 = soc_camera_qbuf,
	.vidioc_dqbuf		 = soc_camera_dqbuf,
1457 1458
	.vidioc_create_bufs	 = soc_camera_create_bufs,
	.vidioc_prepare_buf	 = soc_camera_prepare_buf,
1459 1460 1461 1462 1463
	.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,
1464 1465
	.vidioc_g_selection	 = soc_camera_g_selection,
	.vidioc_s_selection	 = soc_camera_s_selection,
1466 1467
	.vidioc_g_parm		 = soc_camera_g_parm,
	.vidioc_s_parm		 = soc_camera_s_parm,
1468 1469
};

1470
static int video_dev_create(struct soc_camera_device *icd)
1471
{
1472
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1473
	struct video_device *vdev = video_device_alloc();
1474 1475

	if (!vdev)
1476
		return -ENOMEM;
1477 1478

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

1480
	vdev->parent		= icd->pdev;
1481
	vdev->fops		= &soc_camera_fops;
1482
	vdev->ioctl_ops		= &soc_camera_ioctl_ops;
1483
	vdev->release		= video_device_release;
1484
	vdev->ctrl_handler	= &icd->ctrl_handler;
1485
	vdev->lock		= &ici->host_lock;
1486 1487 1488 1489

	icd->vdev = vdev;

	return 0;
1490
}
1491

1492
/*
1493
 * Called from soc_camera_probe() above with .host_lock held
1494
 */
1495
static int soc_camera_video_start(struct soc_camera_device *icd)
1496
{
1497
	const struct device_type *type = icd->vdev->dev.type;
1498
	int ret;
1499

1500
	if (!icd->parent)
1501 1502
		return -ENODEV;

1503
	ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
1504
	if (ret < 0) {
1505
		dev_err(icd->pdev, "video_register_device failed: %d\n", ret);
1506 1507
		return ret;
	}
1508

1509 1510 1511
	/* Restore device type, possibly set by the subdevice driver */
	icd->vdev->dev.type = type;

1512
	return 0;
1513 1514
}

1515
static int soc_camera_pdrv_probe(struct platform_device *pdev)
1516
{
1517 1518
	struct soc_camera_desc *sdesc = pdev->dev.platform_data;
	struct soc_camera_subdev_desc *ssdd = &sdesc->subdev_desc;
1519
	struct soc_camera_device *icd;
1520
	int ret;
1521

1522
	if (!sdesc)
1523
		return -EINVAL;
1524

1525
	icd = devm_kzalloc(&pdev->dev, sizeof(*icd), GFP_KERNEL);
1526 1527
	if (!icd)
		return -ENOMEM;
1528

1529 1530
	ret = devm_regulator_bulk_get(&pdev->dev, ssdd->num_regulators,
				      ssdd->regulators);
1531 1532 1533
	if (ret < 0)
		return ret;

1534 1535
	icd->iface = sdesc->host_desc.bus_id;
	icd->sdesc = sdesc;
1536
	icd->pdev = &pdev->dev;
1537
	platform_set_drvdata(pdev, icd);
1538

1539 1540 1541
	icd->user_width		= DEFAULT_WIDTH;
	icd->user_height	= DEFAULT_HEIGHT;

1542
	return soc_camera_device_register(icd);
1543
}
1544

1545 1546
/*
 * Only called on rmmod for each platform device, since they are not
1547
 * hot-pluggable. Now we know, that all our users - hosts and devices have
1548 1549
 * been unloaded already
 */
1550
static int soc_camera_pdrv_remove(struct platform_device *pdev)
1551
{
1552
	struct soc_camera_device *icd = platform_get_drvdata(pdev);
1553

1554
	if (!icd)
1555 1556
		return -EINVAL;

1557
	list_del(&icd->list);
1558

1559 1560 1561 1562
	return 0;
}

static struct platform_driver __refdata soc_camera_pdrv = {
1563
	.probe = soc_camera_pdrv_probe,
1564
	.remove  = soc_camera_pdrv_remove,
1565 1566 1567
	.driver  = {
		.name	= "soc-camera-pdrv",
		.owner	= THIS_MODULE,
1568 1569 1570
	},
};

1571
module_platform_driver(soc_camera_pdrv);
1572 1573 1574 1575

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