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

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

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

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

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

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

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

72 73
int soc_camera_power_on(struct device *dev, struct soc_camera_subdev_desc *ssdd,
			struct v4l2_clk *clk)
74
{
75 76 77 78 79 80 81 82 83 84 85 86 87
	int ret;
	bool clock_toggle;

	if (clk && (!ssdd->unbalanced_power ||
		    !test_and_set_bit(0, &ssdd->clock_state))) {
		ret = v4l2_clk_enable(clk);
		if (ret < 0) {
			dev_err(dev, "Cannot enable clock: %d\n", ret);
			return ret;
		}
		clock_toggle = true;
	} else {
		clock_toggle = false;
88
	}
89

90 91
	ret = regulator_bulk_enable(ssdd->sd_pdata.num_regulators,
				    ssdd->sd_pdata.regulators);
92
	if (ret < 0) {
93
		dev_err(dev, "Cannot enable regulators\n");
94
		goto eregenable;
95
	}
96

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

106 107 108
	return 0;

epwron:
109 110
	regulator_bulk_disable(ssdd->sd_pdata.num_regulators,
			       ssdd->sd_pdata.regulators);
111
eregenable:
112
	if (clock_toggle)
113 114
		v4l2_clk_disable(clk);

115 116
	return ret;
}
117
EXPORT_SYMBOL(soc_camera_power_on);
118

119 120
int soc_camera_power_off(struct device *dev, struct soc_camera_subdev_desc *ssdd,
			 struct v4l2_clk *clk)
121
{
122 123
	int ret = 0;
	int err;
124

125 126
	if (ssdd->power) {
		err = ssdd->power(dev, 0);
127
		if (err < 0) {
128
			dev_err(dev,
129
				"Platform failed to power-off the camera.\n");
130
			ret = err;
131 132 133
		}
	}

134 135
	err = regulator_bulk_disable(ssdd->sd_pdata.num_regulators,
				     ssdd->sd_pdata.regulators);
136
	if (err < 0) {
137
		dev_err(dev, "Cannot disable regulators\n");
138 139
		ret = ret ? : err;
	}
140

141
	if (clk && (!ssdd->unbalanced_power || test_and_clear_bit(0, &ssdd->clock_state)))
142 143
		v4l2_clk_disable(clk);

144
	return ret;
145
}
146 147
EXPORT_SYMBOL(soc_camera_power_off);

148 149
int soc_camera_power_init(struct device *dev, struct soc_camera_subdev_desc *ssdd)
{
150
	/* Should not have any effect in synchronous case */
151 152
	return devm_regulator_bulk_get(dev, ssdd->sd_pdata.num_regulators,
				       ssdd->sd_pdata.regulators);
153 154 155
}
EXPORT_SYMBOL(soc_camera_power_init);

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
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;
}
179

180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
static int soc_camera_clock_start(struct soc_camera_host *ici)
{
	int ret;

	if (!ici->ops->clock_start)
		return 0;

	mutex_lock(&ici->clk_lock);
	ret = ici->ops->clock_start(ici);
	mutex_unlock(&ici->clk_lock);

	return ret;
}

static void soc_camera_clock_stop(struct soc_camera_host *ici)
{
	if (!ici->ops->clock_stop)
		return;

	mutex_lock(&ici->clk_lock);
	ici->ops->clock_stop(ici);
	mutex_unlock(&ici->clk_lock);
}

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

216 217
/**
 * soc_camera_apply_board_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
218
 * @ssdd:	camera platform parameters
219 220 221
 * @cfg:	media bus configuration
 * @return:	resulting flags
 */
222
unsigned long soc_camera_apply_board_flags(struct soc_camera_subdev_desc *ssdd,
223 224 225 226 227
					   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 */
228
	if (ssdd->flags & SOCAM_SENSOR_INVERT_HSYNC) {
229 230 231 232 233
		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;
	}

234
	if (ssdd->flags & SOCAM_SENSOR_INVERT_VSYNC) {
235 236 237 238 239
		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;
	}

240
	if (ssdd->flags & SOCAM_SENSOR_INVERT_PCLK) {
241 242 243 244 245 246 247 248 249
		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);

250 251 252 253 254 255
#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)
{
256
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
257
	const struct soc_camera_format_xlate *xlate;
258 259 260
	struct v4l2_pix_format *pix = &f->fmt.pix;
	int ret;

261
	dev_dbg(icd->pdev, "TRY_FMT(%c%c%c%c, %ux%u)\n",
262 263
		pixfmtstr(pix->pixelformat), pix->width, pix->height);

264 265
	if (pix->pixelformat != V4L2_PIX_FMT_JPEG &&
	    !(ici->capabilities & SOCAM_HOST_CAP_STRIDE)) {
266 267 268
		pix->bytesperline = 0;
		pix->sizeimage = 0;
	}
269 270 271 272 273

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

274 275 276
	xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
	if (!xlate)
		return -EINVAL;
277

278 279 280
	ret = soc_mbus_bytes_per_line(pix->width, xlate->host_fmt);
	if (ret < 0)
		return ret;
281

282 283 284 285 286 287 288 289
	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);
290 291 292 293

	return 0;
}

294
static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
295
				      struct v4l2_format *f)
296
{
297
	struct soc_camera_device *icd = file->private_data;
298 299 300

	WARN_ON(priv != file->private_data);

301 302 303 304
	/* Only single-plane capture is supported so far */
	if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return -EINVAL;

305
	/* limit format to hardware capabilities */
306
	return soc_camera_try_fmt(icd, f);
307 308 309 310 311
}

static int soc_camera_enum_input(struct file *file, void *priv,
				 struct v4l2_input *inp)
{
312 313
	struct soc_camera_device *icd = file->private_data;

314 315 316
	if (inp->index != 0)
		return -EINVAL;

317 318
	/* default is camera */
	inp->type = V4L2_INPUT_TYPE_CAMERA;
319
	inp->std = icd->vdev->tvnorms;
320
	strcpy(inp->name, "Camera");
321

322
	return 0;
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
}

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

340
static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id a)
341
{
342
	struct soc_camera_device *icd = file->private_data;
343
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
344

345
	return v4l2_subdev_call(sd, video, s_std, a);
346 347
}

348 349 350 351 352
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);

353
	return v4l2_subdev_call(sd, video, g_std, a);
354 355
}

356
static int soc_camera_enum_framesizes(struct file *file, void *fh,
357 358 359
					 struct v4l2_frmsizeenum *fsize)
{
	struct soc_camera_device *icd = file->private_data;
360
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
361

362
	return ici->ops->enum_framesizes(icd, fsize);
363 364
}

365 366 367 368
static int soc_camera_reqbufs(struct file *file, void *priv,
			      struct v4l2_requestbuffers *p)
{
	int ret;
369
	struct soc_camera_device *icd = file->private_data;
370
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
371 372 373

	WARN_ON(priv != file->private_data);

374 375 376
	if (icd->streamer && icd->streamer != file)
		return -EBUSY;

377 378 379 380 381 382 383 384 385
	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);
	}
386

387 388
	if (!ret)
		icd->streamer = p->count ? file : NULL;
389
	return ret;
390 391 392 393 394
}

static int soc_camera_querybuf(struct file *file, void *priv,
			       struct v4l2_buffer *p)
{
395
	struct soc_camera_device *icd = file->private_data;
396
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
397 398 399

	WARN_ON(priv != file->private_data);

400 401 402 403
	if (ici->ops->init_videobuf)
		return videobuf_querybuf(&icd->vb_vidq, p);
	else
		return vb2_querybuf(&icd->vb2_vidq, p);
404 405 406 407 408
}

static int soc_camera_qbuf(struct file *file, void *priv,
			   struct v4l2_buffer *p)
{
409
	struct soc_camera_device *icd = file->private_data;
410
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
411 412 413

	WARN_ON(priv != file->private_data);

414 415 416
	if (icd->streamer != file)
		return -EBUSY;

417 418 419 420
	if (ici->ops->init_videobuf)
		return videobuf_qbuf(&icd->vb_vidq, p);
	else
		return vb2_qbuf(&icd->vb2_vidq, p);
421 422 423 424 425
}

static int soc_camera_dqbuf(struct file *file, void *priv,
			    struct v4l2_buffer *p)
{
426
	struct soc_camera_device *icd = file->private_data;
427
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
428 429 430

	WARN_ON(priv != file->private_data);

431 432 433
	if (icd->streamer != file)
		return -EBUSY;

434 435 436 437
	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);
438 439
}

440 441 442 443 444
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);
445
	int ret;
446 447 448

	/* videobuf2 only */
	if (ici->ops->init_videobuf)
449 450 451 452 453 454 455 456 457
		return -ENOTTY;

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

	ret = vb2_create_bufs(&icd->vb2_vidq, create);
	if (!ret)
		icd->streamer = file;
	return ret;
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
}

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

473 474 475 476 477 478 479 480
static int soc_camera_expbuf(struct file *file, void *priv,
			     struct v4l2_exportbuffer *p)
{
	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)
481 482 483 484 485
		return -ENOTTY;

	if (icd->streamer && icd->streamer != file)
		return -EBUSY;
	return vb2_expbuf(&icd->vb2_vidq, p);
486 487
}

488
/* Always entered with .host_lock held */
489 490
static int soc_camera_init_user_formats(struct soc_camera_device *icd)
{
491
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
492
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
493 494
	unsigned int i, fmts = 0, raw_fmts = 0;
	int ret;
495 496 497
	struct v4l2_subdev_mbus_code_enum code = {
		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
	};
498

499
	while (!v4l2_subdev_call(sd, pad, enum_mbus_code, NULL, &code)) {
500
		raw_fmts++;
501 502
		code.index++;
	}
503 504 505 506 507 508

	if (!ici->ops->get_formats)
		/*
		 * Fallback mode - the host will have to serve all
		 * sensor-provided formats one-to-one to the user
		 */
509
		fmts = raw_fmts;
510 511 512 513 514
	else
		/*
		 * First pass - only count formats this host-sensor
		 * configuration can provide
		 */
515
		for (i = 0; i < raw_fmts; i++) {
516 517 518 519 520
			ret = ici->ops->get_formats(icd, i, NULL);
			if (ret < 0)
				return ret;
			fmts += ret;
		}
521 522 523 524 525 526 527 528 529

	if (!fmts)
		return -ENXIO;

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

530
	dev_dbg(icd->pdev, "Found %d supported formats.\n", fmts);
531 532

	/* Second pass - actually fill data formats */
533
	fmts = 0;
534
	for (i = 0; i < raw_fmts; i++)
535
		if (!ici->ops->get_formats) {
536 537
			code.index = i;
			v4l2_subdev_call(sd, pad, enum_mbus_code, NULL, &code);
538
			icd->user_formats[fmts].host_fmt =
539
				soc_mbus_get_fmtdesc(code.code);
540
			if (icd->user_formats[fmts].host_fmt)
541
				icd->user_formats[fmts++].code = code.code;
542
		} else {
543 544 545 546 547
			ret = ici->ops->get_formats(icd, i,
						    &icd->user_formats[fmts]);
			if (ret < 0)
				goto egfmt;
			fmts += ret;
548 549
		}

550
	icd->num_user_formats = fmts;
551
	icd->current_fmt = &icd->user_formats[0];
552 553

	return 0;
554 555 556 557

egfmt:
	vfree(icd->user_formats);
	return ret;
558 559
}

560
/* Always entered with .host_lock held */
561 562
static void soc_camera_free_user_formats(struct soc_camera_device *icd)
{
563
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
564 565 566

	if (ici->ops->put_formats)
		ici->ops->put_formats(icd);
567
	icd->current_fmt = NULL;
568
	icd->num_user_formats = 0;
569
	vfree(icd->user_formats);
570
	icd->user_formats = NULL;
571 572
}

573
/* Called with .vb_lock held, or from the first open(2), see comment there */
574
static int soc_camera_set_fmt(struct soc_camera_device *icd,
575 576
			      struct v4l2_format *f)
{
577
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
578 579 580
	struct v4l2_pix_format *pix = &f->fmt.pix;
	int ret;

581
	dev_dbg(icd->pdev, "S_FMT(%c%c%c%c, %ux%u)\n",
582 583
		pixfmtstr(pix->pixelformat), pix->width, pix->height);

584
	/* We always call try_fmt() before set_fmt() or set_crop() */
585
	ret = soc_camera_try_fmt(icd, f);
586 587 588 589 590 591 592
	if (ret < 0)
		return ret;

	ret = ici->ops->set_fmt(icd, f);
	if (ret < 0) {
		return ret;
	} else if (!icd->current_fmt ||
593
		   icd->current_fmt->host_fmt->fourcc != pix->pixelformat) {
594
		dev_err(icd->pdev,
595 596 597 598
			"Host driver hasn't set up current format correctly!\n");
		return -EINVAL;
	}

599 600
	icd->user_width		= pix->width;
	icd->user_height	= pix->height;
601 602
	icd->bytesperline	= pix->bytesperline;
	icd->sizeimage		= pix->sizeimage;
603
	icd->colorspace		= pix->colorspace;
604 605 606
	icd->field		= pix->field;
	if (ici->ops->init_videobuf)
		icd->vb_vidq.field = pix->field;
607

608
	dev_dbg(icd->pdev, "set width: %d height: %d\n",
609
		icd->user_width, icd->user_height);
610 611

	/* set physical bus parameters */
612
	return ici->ops->set_bus_param(icd);
613 614
}

615 616 617 618 619 620 621 622
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;

623
	if (!icd->clk) {
624
		ret = soc_camera_clock_start(ici);
625 626 627
		if (ret < 0)
			return ret;
	}
628 629 630

	if (ici->ops->add) {
		ret = ici->ops->add(icd);
631
		if (ret < 0)
632
			goto eadd;
633 634 635 636 637
	}

	ici->icd = icd;

	return 0;
638

639
eadd:
640 641
	if (!icd->clk)
		soc_camera_clock_stop(ici);
642 643 644 645 646 647 648 649 650 651
	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;

652 653
	if (ici->ops->remove)
		ici->ops->remove(icd);
654 655
	if (!icd->clk)
		soc_camera_clock_stop(ici);
656 657 658
	ici->icd = NULL;
}

659
static int soc_camera_open(struct file *file)
660
{
661
	struct video_device *vdev = video_devdata(file);
662
	struct soc_camera_device *icd;
663
	struct soc_camera_host *ici;
664 665
	int ret;

666 667
	/*
	 * Don't mess with the host during probe: wait until the loop in
668 669
	 * scan_add_host() completes. Also protect against a race with
	 * soc_camera_host_unregister().
670 671 672
	 */
	if (mutex_lock_interruptible(&list_lock))
		return -ERESTARTSYS;
673 674 675 676 677 678

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

679
	icd = video_get_drvdata(vdev);
680
	ici = to_soc_camera_host(icd->parent);
681 682

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

685
	if (ret < 0) {
686
		dev_err(icd->pdev, "Couldn't lock capture bus driver.\n");
687 688 689 690 691 692 693
		return ret;
	}

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

696 697 698 699
	if (mutex_lock_interruptible(&ici->host_lock)) {
		ret = -ERESTARTSYS;
		goto elockhost;
	}
700 701
	icd->use_count++;

702 703
	/* Now we really have to activate the camera */
	if (icd->use_count == 1) {
704
		struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
705
		/* Restore parameters before the last close() per V4L2 API */
706 707 708
		struct v4l2_format f = {
			.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
			.fmt.pix = {
709 710
				.width		= icd->user_width,
				.height		= icd->user_height,
711
				.field		= icd->field,
712 713 714
				.colorspace	= icd->colorspace,
				.pixelformat	=
					icd->current_fmt->host_fmt->fourcc,
715 716 717
			},
		};

718
		/* The camera could have been already on, try to reset */
719
		if (sdesc->subdev_desc.reset)
720 721
			if (icd->control)
				sdesc->subdev_desc.reset(icd->control);
722

723
		ret = soc_camera_add_device(icd);
724
		if (ret < 0) {
725
			dev_err(icd->pdev, "Couldn't activate the camera: %d\n", ret);
726 727
			goto eiciadd;
		}
728

729
		ret = __soc_camera_power_on(icd);
730 731 732
		if (ret < 0)
			goto epower;

733 734 735 736 737
		pm_runtime_enable(&icd->vdev->dev);
		ret = pm_runtime_resume(&icd->vdev->dev);
		if (ret < 0 && ret != -ENOSYS)
			goto eresume;

738 739 740 741
		/*
		 * 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
742
		 * .host_lock is protecting us against it.
743
		 */
744
		ret = soc_camera_set_fmt(icd, &f);
745 746
		if (ret < 0)
			goto esfmt;
747

748 749 750 751 752 753 754
		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;
		}
755
		v4l2_ctrl_handler_setup(&icd->ctrl_handler);
756
	}
757
	mutex_unlock(&ici->host_lock);
758

759
	file->private_data = icd;
760
	dev_dbg(icd->pdev, "camera device open\n");
761 762 763

	return 0;

764
	/*
765 766
	 * All errors are entered with the .host_lock held, first four also
	 * with use_count == 1
767
	 */
768
einitvb:
769
esfmt:
770 771
	pm_runtime_disable(&icd->vdev->dev);
eresume:
772
	__soc_camera_power_off(icd);
773
epower:
774
	soc_camera_remove_device(icd);
775
eiciadd:
776
	icd->use_count--;
777
	mutex_unlock(&ici->host_lock);
778 779 780
elockhost:
econtrol:
	module_put(ici->ops->owner);
781

782 783 784
	return ret;
}

785
static int soc_camera_close(struct file *file)
786
{
787
	struct soc_camera_device *icd = file->private_data;
788
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
789

790
	mutex_lock(&ici->host_lock);
791 792 793 794 795
	if (icd->streamer == file) {
		if (ici->ops->init_videobuf2)
			vb2_queue_release(&icd->vb2_vidq);
		icd->streamer = NULL;
	}
796
	icd->use_count--;
797
	if (!icd->use_count) {
798 799 800
		pm_runtime_suspend(&icd->vdev->dev);
		pm_runtime_disable(&icd->vdev->dev);

801
		__soc_camera_power_off(icd);
802

803
		soc_camera_remove_device(icd);
804
	}
805

806
	mutex_unlock(&ici->host_lock);
807

808
	module_put(ici->ops->owner);
809

810
	dev_dbg(icd->pdev, "camera device close\n");
811 812 813 814

	return 0;
}

815
static ssize_t soc_camera_read(struct file *file, char __user *buf,
816
			       size_t count, loff_t *ppos)
817
{
818
	struct soc_camera_device *icd = file->private_data;
819 820 821 822 823 824 825
	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);
826

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

829
	return -EINVAL;
830 831 832 833
}

static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
{
834
	struct soc_camera_device *icd = file->private_data;
835
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
836 837
	int err;

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

840 841 842
	if (icd->streamer != file)
		return -EBUSY;

843
	if (mutex_lock_interruptible(&ici->host_lock))
844
		return -ERESTARTSYS;
845 846 847 848
	if (ici->ops->init_videobuf)
		err = videobuf_mmap_mapper(&icd->vb_vidq, vma);
	else
		err = vb2_mmap(&icd->vb2_vidq, vma);
849
	mutex_unlock(&ici->host_lock);
850

851
	dev_dbg(icd->pdev, "vma start=0x%08lx, size=%ld, ret=%d\n",
852 853 854 855 856 857 858 859 860
		(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)
{
861
	struct soc_camera_device *icd = file->private_data;
862
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
863
	unsigned res = POLLERR;
864

865
	if (icd->streamer != file)
866 867
		return POLLERR;

868
	mutex_lock(&ici->host_lock);
869 870 871 872
	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);
873
	mutex_unlock(&ici->host_lock);
874
	return res;
875 876
}

877
static struct v4l2_file_operations soc_camera_fops = {
878 879 880
	.owner		= THIS_MODULE,
	.open		= soc_camera_open,
	.release	= soc_camera_close,
881
	.unlocked_ioctl	= video_ioctl2,
882 883 884 885 886
	.read		= soc_camera_read,
	.mmap		= soc_camera_mmap,
	.poll		= soc_camera_poll,
};

887
static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
888
				    struct v4l2_format *f)
889
{
890
	struct soc_camera_device *icd = file->private_data;
891 892 893 894
	int ret;

	WARN_ON(priv != file->private_data);

895
	if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
896
		dev_warn(icd->pdev, "Wrong buf-type %d\n", f->type);
897 898 899
		return -EINVAL;
	}

900 901
	if (icd->streamer && icd->streamer != file)
		return -EBUSY;
902

903 904
	if (is_streaming(to_soc_camera_host(icd->parent), icd)) {
		dev_err(icd->pdev, "S_FMT denied: queue initialised\n");
905
		return -EBUSY;
906 907
	}

908 909 910 911
	ret = soc_camera_set_fmt(icd, f);

	if (!ret && !icd->streamer)
		icd->streamer = file;
912 913

	return ret;
914 915
}

916
static int soc_camera_enum_fmt_vid_cap(struct file *file, void  *priv,
917
				       struct v4l2_fmtdesc *f)
918
{
919
	struct soc_camera_device *icd = file->private_data;
920
	const struct soc_mbus_pixelfmt *format;
921 922 923

	WARN_ON(priv != file->private_data);

924
	if (f->index >= icd->num_user_formats)
925 926
		return -EINVAL;

927
	format = icd->user_formats[f->index].host_fmt;
928

929 930
	if (format->name)
		strlcpy(f->description, format->name, sizeof(f->description));
931 932 933 934
	f->pixelformat = format->fourcc;
	return 0;
}

935
static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
936
				    struct v4l2_format *f)
937
{
938
	struct soc_camera_device *icd = file->private_data;
939
	struct v4l2_pix_format *pix = &f->fmt.pix;
940 941 942

	WARN_ON(priv != file->private_data);

943 944 945
	if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return -EINVAL;

946 947
	pix->width		= icd->user_width;
	pix->height		= icd->user_height;
948 949
	pix->bytesperline	= icd->bytesperline;
	pix->sizeimage		= icd->sizeimage;
950
	pix->field		= icd->field;
951 952
	pix->pixelformat	= icd->current_fmt->host_fmt->fourcc;
	pix->colorspace		= icd->colorspace;
953
	dev_dbg(icd->pdev, "current_fmt->fourcc: 0x%08x\n",
954
		icd->current_fmt->host_fmt->fourcc);
955 956 957 958 959 960
	return 0;
}

static int soc_camera_querycap(struct file *file, void  *priv,
			       struct v4l2_capability *cap)
{
961
	struct soc_camera_device *icd = file->private_data;
962
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
963 964 965 966

	WARN_ON(priv != file->private_data);

	strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
967
	return ici->ops->querycap(ici, cap);
968 969 970 971 972
}

static int soc_camera_streamon(struct file *file, void *priv,
			       enum v4l2_buf_type i)
{
973
	struct soc_camera_device *icd = file->private_data;
974
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
975
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
976
	int ret;
977 978 979 980 981 982

	WARN_ON(priv != file->private_data);

	if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return -EINVAL;

983 984 985
	if (icd->streamer != file)
		return -EBUSY;

986
	/* This calls buf_queue from host driver's videobuf_queue_ops */
987 988 989 990 991
	if (ici->ops->init_videobuf)
		ret = videobuf_streamon(&icd->vb_vidq);
	else
		ret = vb2_streamon(&icd->vb2_vidq, i);

992 993
	if (!ret)
		v4l2_subdev_call(sd, video, s_stream, 1);
994 995

	return ret;
996 997 998 999 1000
}

static int soc_camera_streamoff(struct file *file, void *priv,
				enum v4l2_buf_type i)
{
1001
	struct soc_camera_device *icd = file->private_data;
1002
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1003
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1004
	int ret;
1005 1006 1007 1008 1009 1010

	WARN_ON(priv != file->private_data);

	if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return -EINVAL;

1011 1012 1013
	if (icd->streamer != file)
		return -EBUSY;

1014 1015 1016 1017
	/*
	 * This calls buf_release from host driver's videobuf_queue_ops for all
	 * remaining buffers. When the last buffer is freed, stop capture
	 */
1018
	if (ici->ops->init_videobuf)
1019
		ret = videobuf_streamoff(&icd->vb_vidq);
1020
	else
1021
		ret = vb2_streamoff(&icd->vb2_vidq, i);
1022

1023
	v4l2_subdev_call(sd, video, s_stream, 0);
1024

1025
	return ret;
1026 1027 1028 1029 1030
}

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

1034
	return ici->ops->cropcap(icd, a);
1035 1036 1037 1038 1039
}

static int soc_camera_g_crop(struct file *file, void *fh,
			     struct v4l2_crop *a)
{
1040
	struct soc_camera_device *icd = file->private_data;
1041
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1042
	int ret;
1043

1044
	ret = ici->ops->get_crop(icd, a);
1045

1046
	return ret;
1047 1048
}

1049 1050 1051
/*
 * 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
1052
 * retrieve it.
1053
 */
1054
static int soc_camera_s_crop(struct file *file, void *fh,
1055
			     const struct v4l2_crop *a)
1056
{
1057
	struct soc_camera_device *icd = file->private_data;
1058
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1059
	const struct v4l2_rect *rect = &a->c;
1060
	struct v4l2_crop current_crop;
1061 1062 1063 1064 1065
	int ret;

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

1066
	dev_dbg(icd->pdev, "S_CROP(%ux%u@%u:%u)\n",
1067 1068
		rect->width, rect->height, rect->left, rect->top);

1069 1070
	current_crop.type = a->type;

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

1074
	/* Prohibit window size change with initialised buffers */
1075
	if (ret < 0) {
1076
		dev_err(icd->pdev,
1077
			"S_CROP denied: getting current crop failed\n");
1078 1079 1080 1081 1082 1083 1084 1085
	} 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 {
1086
		dev_err(icd->pdev,
1087 1088 1089 1090
			"S_CROP denied: queue initialised and sizes differ\n");
		ret = -EBUSY;
	}

1091 1092 1093
	return ret;
}

1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
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 ||
1119 1120
	    (s->target != V4L2_SEL_TGT_COMPOSE &&
	     s->target != V4L2_SEL_TGT_CROP))
1121 1122
		return -EINVAL;

1123
	if (s->target == V4L2_SEL_TGT_COMPOSE) {
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
		/* 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 &&
1143
	    s->target == V4L2_SEL_TGT_COMPOSE) {
1144 1145 1146 1147 1148 1149 1150 1151 1152
		icd->user_width = s->r.width;
		icd->user_height = s->r.height;
		if (!icd->streamer)
			icd->streamer = file;
	}

	return ret;
}

1153 1154 1155
static int soc_camera_g_parm(struct file *file, void *fh,
			     struct v4l2_streamparm *a)
{
1156
	struct soc_camera_device *icd = file->private_data;
1157
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167

	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)
{
1168
	struct soc_camera_device *icd = file->private_data;
1169
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1170 1171 1172 1173 1174 1175 1176

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

	return -ENOIOCTLCMD;
}

1177 1178
static int soc_camera_probe(struct soc_camera_host *ici,
			    struct soc_camera_device *icd);
1179

1180 1181 1182 1183 1184
/* So far this function cannot fail */
static void scan_add_host(struct soc_camera_host *ici)
{
	struct soc_camera_device *icd;

1185
	mutex_lock(&list_lock);
1186

1187
	list_for_each_entry(icd, &devices, list)
1188
		if (icd->iface == ici->nr) {
1189 1190 1191 1192 1193
			struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
			struct soc_camera_subdev_desc *ssdd = &sdesc->subdev_desc;

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

1197
			icd->parent = ici->v4l2_dev.dev;
1198 1199 1200

			/* Ignore errors */
			soc_camera_probe(ici, icd);
1201 1202
		}

1203
	mutex_unlock(&list_lock);
1204 1205
}

1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
/*
 * 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
	 */
1227
	return soc_camera_clock_start(ici);
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
}

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

1240
	soc_camera_clock_stop(ici);
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256

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

1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
static int soc_camera_dyn_pdev(struct soc_camera_desc *sdesc,
			       struct soc_camera_async_client *sasc)
{
	struct platform_device *pdev;
	int ret, i;

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

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

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

	sasc->pdev = pdev;

	return 0;
}

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

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

	return platform_get_drvdata(pdev);
}

/* Locking: called with .host_lock held */
static int soc_camera_probe_finish(struct soc_camera_device *icd)
{
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1302 1303 1304 1305
	struct v4l2_subdev_format fmt = {
		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
	};
	struct v4l2_mbus_framefmt *mf = &fmt.format;
1306 1307 1308 1309 1310
	int ret;

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

1311 1312
	v4l2_subdev_call(sd, video, g_tvnorms, &icd->vdev->tvnorms);

1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
	ret = v4l2_ctrl_add_handler(&icd->ctrl_handler, sd->ctrl_handler, NULL);
	if (ret < 0)
		return ret;

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

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

	icd->field = V4L2_FIELD_ANY;

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

	/* Try to improve our guess of a reasonable window format */
1335 1336 1337 1338 1339
	if (!v4l2_subdev_call(sd, pad, get_fmt, NULL, &fmt)) {
		icd->user_width		= mf->width;
		icd->user_height	= mf->height;
		icd->colorspace		= mf->colorspace;
		icd->field		= mf->field;
1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
	}
	soc_camera_remove_device(icd);

	return 0;

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

	return ret;
}

1353
#ifdef CONFIG_I2C_BOARDINFO
1354
static int soc_camera_i2c_init(struct soc_camera_device *icd,
1355
			       struct soc_camera_desc *sdesc)
1356
{
1357
	struct soc_camera_subdev_desc *ssdd;
1358
	struct i2c_client *client;
1359
	struct soc_camera_host *ici;
1360
	struct soc_camera_host_desc *shd = &sdesc->host_desc;
1361
	struct i2c_adapter *adap;
1362
	struct v4l2_subdev *subdev;
1363 1364
	char clk_name[V4L2_SUBDEV_NAME_SIZE];
	int ret;
1365

1366 1367 1368 1369 1370 1371 1372 1373
	/* First find out how we link the main client */
	if (icd->sasc) {
		/* Async non-OF probing handled by the subdevice list */
		return -EPROBE_DEFER;
	}

	ici = to_soc_camera_host(icd->parent);
	adap = i2c_get_adapter(shd->i2c_adapter_id);
1374
	if (!adap) {
1375
		dev_err(icd->pdev, "Cannot get I2C adapter #%d. No driver?\n",
1376
			shd->i2c_adapter_id);
1377
		return -ENODEV;
1378
	}
1379

1380
	ssdd = kmemdup(&sdesc->subdev_desc, sizeof(*ssdd), GFP_KERNEL);
1381 1382 1383 1384 1385 1386 1387 1388 1389
	if (!ssdd) {
		ret = -ENOMEM;
		goto ealloc;
	}
	/*
	 * In synchronous case we request regulators ourselves in
	 * soc_camera_pdrv_probe(), make sure the subdevice driver doesn't try
	 * to allocate them again.
	 */
1390 1391
	ssdd->sd_pdata.num_regulators = 0;
	ssdd->sd_pdata.regulators = NULL;
1392
	shd->board_info->platform_data = ssdd;
1393

1394 1395 1396
	snprintf(clk_name, sizeof(clk_name), "%d-%04x",
		 shd->i2c_adapter_id, shd->board_info->addr);

1397
	icd->clk = v4l2_clk_register(&soc_camera_clk_ops, clk_name, icd);
1398 1399 1400 1401 1402
	if (IS_ERR(icd->clk)) {
		ret = PTR_ERR(icd->clk);
		goto eclkreg;
	}

1403
	subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
1404
				shd->board_info, NULL);
1405 1406
	if (!subdev) {
		ret = -ENODEV;
1407
		goto ei2cnd;
1408
	}
1409

1410
	client = v4l2_get_subdevdata(subdev);
1411

1412
	/* Use to_i2c_client(dev) to recover the i2c client */
1413
	icd->control = &client->dev;
1414

1415 1416
	return 0;
ei2cnd:
1417
	v4l2_clk_unregister(icd->clk);
1418
	icd->clk = NULL;
1419 1420 1421
eclkreg:
	kfree(ssdd);
ealloc:
1422
	i2c_put_adapter(adap);
1423
	return ret;
1424 1425
}

1426
static void soc_camera_i2c_free(struct soc_camera_device *icd)
1427 1428 1429
{
	struct i2c_client *client =
		to_i2c_client(to_soc_camera_control(icd));
1430
	struct i2c_adapter *adap;
1431
	struct soc_camera_subdev_desc *ssdd;
1432 1433

	icd->control = NULL;
1434 1435 1436 1437
	if (icd->sasc)
		return;

	adap = client->adapter;
1438
	ssdd = client->dev.platform_data;
1439
	v4l2_device_unregister_subdev(i2c_get_clientdata(client));
1440
	i2c_unregister_device(client);
1441
	i2c_put_adapter(adap);
1442
	kfree(ssdd);
1443 1444
	v4l2_clk_unregister(icd->clk);
	icd->clk = NULL;
1445
}
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477

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

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

		/*
		 * Only now we get subdevice-specific information like
		 * regulators, flags, callbacks, etc.
		 */
		if (client) {
			struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
			struct soc_camera_subdev_desc *ssdd =
				soc_camera_i2c_to_desc(client);
			if (ssdd) {
				memcpy(&sdesc->subdev_desc, ssdd,
				       sizeof(sdesc->subdev_desc));
				if (ssdd->reset)
1478
					ssdd->reset(&client->dev);
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522
			}

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

	return 0;
}

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

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

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

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

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

	return 0;
}

static int scan_async_group(struct soc_camera_host *ici,
1523
			    struct v4l2_async_subdev **asd, unsigned int size)
1524 1525 1526 1527 1528 1529
{
	struct soc_camera_async_subdev *sasd;
	struct soc_camera_async_client *sasc;
	struct soc_camera_device *icd;
	struct soc_camera_desc sdesc = {.host_desc.bus_id = ici->nr,};
	char clk_name[V4L2_SUBDEV_NAME_SIZE];
1530 1531
	unsigned int i;
	int ret;
1532 1533 1534 1535 1536 1537 1538 1539

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

1540
	if (i >= size || asd[i]->match_type != V4L2_ASYNC_MATCH_I2C) {
1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555
		/* All useless */
		dev_err(ici->v4l2_dev.dev, "No I2C data source found!\n");
		return -ENODEV;
	}

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

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

	ret = soc_camera_dyn_pdev(&sdesc, sasc);
	if (ret < 0)
1556
		goto eallocpdev;
1557 1558 1559 1560 1561

	sasc->sensor = &sasd->asd;

	icd = soc_camera_add_pdev(sasc);
	if (!icd) {
1562 1563
		ret = -ENOMEM;
		goto eaddpdev;
1564 1565
	}

1566
	sasc->notifier.subdevs = asd;
1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577
	sasc->notifier.num_subdevs = size;
	sasc->notifier.bound = soc_camera_async_bound;
	sasc->notifier.unbind = soc_camera_async_unbind;
	sasc->notifier.complete = soc_camera_async_complete;

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

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

1578
	icd->clk = v4l2_clk_register(&soc_camera_clk_ops, clk_name, icd);
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590
	if (IS_ERR(icd->clk)) {
		ret = PTR_ERR(icd->clk);
		goto eclkreg;
	}

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

	v4l2_clk_unregister(icd->clk);
eclkreg:
	icd->clk = NULL;
1591 1592 1593 1594 1595
	platform_device_del(sasc->pdev);
eaddpdev:
	platform_device_put(sasc->pdev);
eallocpdev:
	devm_kfree(ici->v4l2_dev.dev, sasc);
1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610
	dev_err(ici->v4l2_dev.dev, "group probe failed: %d\n", ret);

	return ret;
}

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

	for (j = 0, asd = ici->asd; ici->asd_sizes[j]; j++) {
		scan_async_group(ici, asd, ici->asd_sizes[j]);
		asd += ici->asd_sizes[j];
	}
}
1611
#else
1612 1613 1614
#define soc_camera_i2c_init(icd, sdesc)	(-ENODEV)
#define soc_camera_i2c_free(icd)	do {} while (0)
#define scan_async_host(ici)		do {} while (0)
1615 1616
#endif

1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633
#ifdef CONFIG_OF

struct soc_of_info {
	struct soc_camera_async_subdev	sasd;
	struct soc_camera_async_client	sasc;
	struct v4l2_async_subdev	*subdev;
};

static int soc_of_bind(struct soc_camera_host *ici,
		       struct device_node *ep,
		       struct device_node *remote)
{
	struct soc_camera_device *icd;
	struct soc_camera_desc sdesc = {.host_desc.bus_id = ici->nr,};
	struct soc_camera_async_client *sasc;
	struct soc_of_info *info;
	struct i2c_client *client;
1634
	char clk_name[V4L2_SUBDEV_NAME_SIZE + 32];
1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682
	int ret;

	/* allocate a new subdev and add match info to it */
	info = devm_kzalloc(ici->v4l2_dev.dev, sizeof(struct soc_of_info),
			    GFP_KERNEL);
	if (!info)
		return -ENOMEM;

	info->sasd.asd.match.of.node = remote;
	info->sasd.asd.match_type = V4L2_ASYNC_MATCH_OF;
	info->subdev = &info->sasd.asd;

	/* Or shall this be managed by the soc-camera device? */
	sasc = &info->sasc;

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

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

	sasc->sensor = &info->sasd.asd;

	icd = soc_camera_add_pdev(sasc);
	if (!icd) {
		ret = -ENOMEM;
		goto eaddpdev;
	}

	sasc->notifier.subdevs = &info->subdev;
	sasc->notifier.num_subdevs = 1;
	sasc->notifier.bound = soc_camera_async_bound;
	sasc->notifier.unbind = soc_camera_async_unbind;
	sasc->notifier.complete = soc_camera_async_complete;

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

	client = of_find_i2c_device_by_node(remote);

	if (client)
		snprintf(clk_name, sizeof(clk_name), "%d-%04x",
			 client->adapter->nr, client->addr);
	else
		snprintf(clk_name, sizeof(clk_name), "of-%s",
			 of_node_full_name(remote));

1683
	icd->clk = v4l2_clk_register(&soc_camera_clk_ops, clk_name, icd);
1684 1685 1686 1687 1688 1689 1690 1691
	if (IS_ERR(icd->clk)) {
		ret = PTR_ERR(icd->clk);
		goto eclkreg;
	}

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

	v4l2_clk_unregister(icd->clk);
1694 1695 1696 1697 1698 1699
eclkreg:
	icd->clk = NULL;
	platform_device_del(sasc->pdev);
eaddpdev:
	platform_device_put(sasc->pdev);
eallocpdev:
1700
	devm_kfree(ici->v4l2_dev.dev, info);
1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735
	dev_err(ici->v4l2_dev.dev, "group probe failed: %d\n", ret);

	return ret;
}

static void scan_of_host(struct soc_camera_host *ici)
{
	struct device *dev = ici->v4l2_dev.dev;
	struct device_node *np = dev->of_node;
	struct device_node *epn = NULL, *ren;
	unsigned int i;

	for (i = 0; ; i++) {
		epn = of_graph_get_next_endpoint(np, epn);
		if (!epn)
			break;

		ren = of_graph_get_remote_port(epn);
		if (!ren) {
			dev_notice(dev, "no remote for %s\n",
				   of_node_full_name(epn));
			continue;
		}

		/* so we now have a remote node to connect */
		if (!i)
			soc_of_bind(ici, epn, ren->parent);

		of_node_put(ren);

		if (i) {
			dev_err(dev, "multiple subdevices aren't supported yet!\n");
			break;
		}
	}
1736 1737

	of_node_put(epn);
1738 1739 1740 1741 1742 1743
}

#else
static inline void scan_of_host(struct soc_camera_host *ici) { }
#endif

1744
/* Called during host-driver probe */
1745 1746
static int soc_camera_probe(struct soc_camera_host *ici,
			    struct soc_camera_device *icd)
1747
{
1748 1749
	struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
	struct soc_camera_host_desc *shd = &sdesc->host_desc;
1750
	struct device *control = NULL;
1751 1752
	int ret;

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

1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765
	/*
	 * 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;

1766
	/* Must have icd->vdev before registering the device */
1767 1768 1769
	ret = video_dev_create(icd);
	if (ret < 0)
		goto evdc;
1770

1771 1772 1773 1774 1775 1776
	/*
	 * ..._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.
	 */

1777
	/* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
1778
	if (shd->board_info) {
1779 1780
		ret = soc_camera_i2c_init(icd, sdesc);
		if (ret < 0 && ret != -EPROBE_DEFER)
1781
			goto eadd;
1782
	} else if (!shd->add_device || !shd->del_device) {
1783
		ret = -EINVAL;
1784
		goto eadd;
1785
	} else {
1786
		ret = soc_camera_clock_start(ici);
1787 1788 1789
		if (ret < 0)
			goto eadd;

1790 1791
		if (shd->module_name)
			ret = request_module(shd->module_name);
1792

1793
		ret = shd->add_device(icd);
1794 1795
		if (ret < 0)
			goto eadddev;
1796

1797 1798 1799 1800
		/*
		 * FIXME: this is racy, have to use driver-binding notification,
		 * when it is available
		 */
1801
		control = to_soc_camera_control(icd);
1802
		if (!control || !control->driver || !dev_get_drvdata(control) ||
1803
		    !try_module_get(control->driver->owner)) {
1804
			shd->del_device(icd);
1805
			ret = -ENODEV;
1806 1807
			goto enodrv;
		}
1808
	}
1809

1810 1811
	mutex_lock(&ici->host_lock);
	ret = soc_camera_probe_finish(icd);
1812
	mutex_unlock(&ici->host_lock);
1813 1814
	if (ret < 0)
		goto efinish;
1815

1816
	return 0;
1817

1818
efinish:
1819
	if (shd->board_info) {
1820
		soc_camera_i2c_free(icd);
1821
	} else {
1822
		shd->del_device(icd);
1823 1824
		module_put(control->driver->owner);
enodrv:
1825
eadddev:
1826
		soc_camera_clock_stop(ici);
1827 1828
	}
eadd:
1829 1830 1831 1832
	if (icd->vdev) {
		video_device_release(icd->vdev);
		icd->vdev = NULL;
	}
1833
evdc:
1834
	v4l2_ctrl_handler_free(&icd->ctrl_handler);
1835 1836 1837
	return ret;
}

1838 1839
/*
 * This is called on device_unregister, which only means we have to disconnect
1840 1841 1842
 * from the host, but not remove ourselves from the device list. With
 * asynchronous client probing this can also be called without
 * soc_camera_probe_finish() having run. Careful with clean up.
1843
 */
1844
static int soc_camera_remove(struct soc_camera_device *icd)
1845
{
1846
	struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
1847
	struct video_device *vdev = icd->vdev;
1848

1849
	v4l2_ctrl_handler_free(&icd->ctrl_handler);
1850 1851 1852 1853 1854
	if (vdev) {
		video_unregister_device(vdev);
		icd->vdev = NULL;
	}

1855
	if (sdesc->host_desc.board_info) {
1856
		soc_camera_i2c_free(icd);
1857
	} else {
1858 1859
		struct device *dev = to_soc_camera_control(icd);
		struct device_driver *drv = dev ? dev->driver : NULL;
1860
		if (drv) {
1861
			sdesc->host_desc.del_device(icd);
1862 1863 1864
			module_put(drv->owner);
		}
	}
1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876

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

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

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

1878 1879 1880
	return 0;
}

1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893
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);
}

1894
static int default_s_crop(struct soc_camera_device *icd, const struct v4l2_crop *a)
1895 1896 1897 1898 1899
{
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
	return v4l2_subdev_call(sd, video, s_crop, a);
}

1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913
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);
}

1914 1915
static int default_enum_framesizes(struct soc_camera_device *icd,
				   struct v4l2_frmsizeenum *fsize)
1916 1917 1918 1919
{
	int ret;
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
	const struct soc_camera_format_xlate *xlate;
1920 1921 1922 1923
	struct v4l2_subdev_frame_size_enum fse = {
		.index = fsize->index,
		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
	};
1924

1925
	xlate = soc_camera_xlate_by_fourcc(icd, fsize->pixel_format);
1926 1927
	if (!xlate)
		return -EINVAL;
1928
	fse.code = xlate->code;
1929

1930
	ret = v4l2_subdev_call(sd, pad, enum_frame_size, NULL, &fse);
1931 1932 1933
	if (ret < 0)
		return ret;

1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947
	if (fse.min_width == fse.max_width &&
	    fse.min_height == fse.max_height) {
		fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
		fsize->discrete.width = fse.min_width;
		fsize->discrete.height = fse.min_height;
		return 0;
	}
	fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
	fsize->stepwise.min_width = fse.min_width;
	fsize->stepwise.max_width = fse.max_width;
	fsize->stepwise.min_height = fse.min_height;
	fsize->stepwise.max_height = fse.max_height;
	fsize->stepwise.step_width = 1;
	fsize->stepwise.step_height = 1;
1948 1949 1950
	return 0;
}

1951
int soc_camera_host_register(struct soc_camera_host *ici)
1952 1953
{
	struct soc_camera_host *ix;
1954
	int ret;
1955

1956 1957 1958 1959 1960
	if (!ici || !ici->ops ||
	    !ici->ops->try_fmt ||
	    !ici->ops->set_fmt ||
	    !ici->ops->set_bus_param ||
	    !ici->ops->querycap ||
1961 1962 1963
	    ((!ici->ops->init_videobuf ||
	      !ici->ops->reqbufs) &&
	     !ici->ops->init_videobuf2) ||
1964
	    !ici->ops->poll ||
1965
	    !ici->v4l2_dev.dev)
1966 1967
		return -EINVAL;

1968 1969 1970 1971 1972 1973
	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;
1974 1975 1976 1977
	if (!ici->ops->set_parm)
		ici->ops->set_parm = default_s_parm;
	if (!ici->ops->get_parm)
		ici->ops->get_parm = default_g_parm;
1978 1979
	if (!ici->ops->enum_framesizes)
		ici->ops->enum_framesizes = default_enum_framesizes;
1980

1981 1982 1983
	mutex_lock(&list_lock);
	list_for_each_entry(ix, &hosts, list) {
		if (ix->nr == ici->nr) {
1984 1985
			ret = -EBUSY;
			goto edevreg;
1986 1987 1988
		}
	}

1989 1990 1991
	ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
	if (ret < 0)
		goto edevreg;
1992

1993 1994 1995
	list_add_tail(&ici->list, &hosts);
	mutex_unlock(&list_lock);

1996
	mutex_init(&ici->host_lock);
1997 1998
	mutex_init(&ici->clk_lock);

1999 2000 2001
	if (ici->v4l2_dev.dev->of_node)
		scan_of_host(ici);
	else if (ici->asd_sizes)
2002 2003 2004 2005 2006 2007 2008 2009 2010
		/*
		 * No OF, host with a list of subdevices. Don't try to mix
		 * modes by initialising some groups statically and some
		 * dynamically!
		 */
		scan_async_host(ici);
	else
		/* Legacy: static platform devices from board data */
		scan_add_host(ici);
2011 2012

	return 0;
2013 2014 2015 2016

edevreg:
	mutex_unlock(&list_lock);
	return ret;
2017 2018 2019 2020 2021 2022
}
EXPORT_SYMBOL(soc_camera_host_register);

/* Unregister all clients! */
void soc_camera_host_unregister(struct soc_camera_host *ici)
{
2023 2024 2025
	struct soc_camera_device *icd, *tmp;
	struct soc_camera_async_client *sasc;
	LIST_HEAD(notifiers);
2026 2027 2028

	mutex_lock(&list_lock);
	list_del(&ici->list);
2029
	list_for_each_entry(icd, &devices, list)
2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046
		if (icd->iface == ici->nr && icd->sasc) {
			/* as long as we hold the device, sasc won't be freed */
			get_device(icd->pdev);
			list_add(&icd->sasc->list, &notifiers);
		}
	mutex_unlock(&list_lock);

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

	mutex_lock(&list_lock);

	list_for_each_entry_safe(icd, tmp, &devices, list)
		if (icd->iface == ici->nr)
2047
			soc_camera_remove(icd);
2048 2049 2050

	mutex_unlock(&list_lock);

2051
	v4l2_device_unregister(&ici->v4l2_dev);
2052 2053 2054 2055
}
EXPORT_SYMBOL(soc_camera_host_unregister);

/* Image capture device */
2056
static int soc_camera_device_register(struct soc_camera_device *icd)
2057 2058 2059 2060
{
	struct soc_camera_device *ix;
	int num = -1, i;

2061
	mutex_lock(&list_lock);
2062 2063
	for (i = 0; i < 256 && num < 0; i++) {
		num = i;
2064
		/* Check if this index is available on this interface */
2065 2066 2067 2068 2069 2070 2071 2072
		list_for_each_entry(ix, &devices, list) {
			if (ix->iface == icd->iface && ix->devnum == i) {
				num = -1;
				break;
			}
		}
	}

2073
	if (num < 0) {
2074 2075 2076 2077
		/*
		 * ok, we have 256 cameras on this host...
		 * man, stay reasonable...
		 */
2078
		mutex_unlock(&list_lock);
2079
		return -ENOMEM;
2080
	}
2081

2082
	icd->devnum		= num;
2083 2084
	icd->use_count		= 0;
	icd->host_priv		= NULL;
2085

2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098
	/*
	 * Dynamically allocated devices set the bit earlier, but it doesn't hurt setting
	 * it again
	 */
	i = to_platform_device(icd->pdev)->id;
	if (i < 0)
		/* One static (legacy) soc-camera platform device */
		i = 0;
	if (i >= MAP_MAX_NUM) {
		mutex_unlock(&list_lock);
		return -EBUSY;
	}
	set_bit(i, device_map);
2099
	list_add_tail(&icd->list, &devices);
2100
	mutex_unlock(&list_lock);
2101 2102

	return 0;
2103 2104
}

2105 2106
static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
	.vidioc_querycap	 = soc_camera_querycap,
2107
	.vidioc_try_fmt_vid_cap  = soc_camera_try_fmt_vid_cap,
2108 2109
	.vidioc_g_fmt_vid_cap    = soc_camera_g_fmt_vid_cap,
	.vidioc_s_fmt_vid_cap    = soc_camera_s_fmt_vid_cap,
2110
	.vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
2111 2112 2113 2114
	.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,
2115
	.vidioc_g_std		 = soc_camera_g_std,
2116
	.vidioc_enum_framesizes  = soc_camera_enum_framesizes,
2117 2118 2119 2120
	.vidioc_reqbufs		 = soc_camera_reqbufs,
	.vidioc_querybuf	 = soc_camera_querybuf,
	.vidioc_qbuf		 = soc_camera_qbuf,
	.vidioc_dqbuf		 = soc_camera_dqbuf,
2121 2122
	.vidioc_create_bufs	 = soc_camera_create_bufs,
	.vidioc_prepare_buf	 = soc_camera_prepare_buf,
2123
	.vidioc_expbuf		 = soc_camera_expbuf,
2124 2125 2126 2127 2128
	.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,
2129 2130
	.vidioc_g_selection	 = soc_camera_g_selection,
	.vidioc_s_selection	 = soc_camera_s_selection,
2131 2132
	.vidioc_g_parm		 = soc_camera_g_parm,
	.vidioc_s_parm		 = soc_camera_s_parm,
2133 2134
};

2135
static int video_dev_create(struct soc_camera_device *icd)
2136
{
2137
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
2138
	struct video_device *vdev = video_device_alloc();
2139 2140

	if (!vdev)
2141
		return -ENOMEM;
2142 2143

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

2145
	vdev->v4l2_dev		= &ici->v4l2_dev;
2146
	vdev->fops		= &soc_camera_fops;
2147
	vdev->ioctl_ops		= &soc_camera_ioctl_ops;
2148
	vdev->release		= video_device_release;
2149
	vdev->ctrl_handler	= &icd->ctrl_handler;
2150
	vdev->lock		= &ici->host_lock;
2151 2152 2153 2154

	icd->vdev = vdev;

	return 0;
2155
}
2156

2157
/*
2158
 * Called from soc_camera_probe() above with .host_lock held
2159
 */
2160
static int soc_camera_video_start(struct soc_camera_device *icd)
2161
{
2162
	const struct device_type *type = icd->vdev->dev.type;
2163
	int ret;
2164

2165
	if (!icd->parent)
2166 2167
		return -ENODEV;

2168
	video_set_drvdata(icd->vdev, icd);
2169 2170 2171 2172 2173 2174
	if (icd->vdev->tvnorms == 0) {
		/* disable the STD API if there are no tvnorms defined */
		v4l2_disable_ioctl(icd->vdev, VIDIOC_G_STD);
		v4l2_disable_ioctl(icd->vdev, VIDIOC_S_STD);
		v4l2_disable_ioctl(icd->vdev, VIDIOC_ENUMSTD);
	}
2175
	ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
2176
	if (ret < 0) {
2177
		dev_err(icd->pdev, "video_register_device failed: %d\n", ret);
2178 2179
		return ret;
	}
2180

2181 2182 2183
	/* Restore device type, possibly set by the subdevice driver */
	icd->vdev->dev.type = type;

2184
	return 0;
2185 2186
}

2187
static int soc_camera_pdrv_probe(struct platform_device *pdev)
2188
{
2189 2190
	struct soc_camera_desc *sdesc = pdev->dev.platform_data;
	struct soc_camera_subdev_desc *ssdd = &sdesc->subdev_desc;
2191
	struct soc_camera_device *icd;
2192
	int ret;
2193

2194
	if (!sdesc)
2195
		return -EINVAL;
2196

2197
	icd = devm_kzalloc(&pdev->dev, sizeof(*icd), GFP_KERNEL);
2198 2199
	if (!icd)
		return -ENOMEM;
2200

2201 2202
	/*
	 * In the asynchronous case ssdd->num_regulators == 0 yet, so, the below
2203 2204 2205 2206
	 * regulator allocation is a dummy. They are actually requested by the
	 * subdevice driver, using soc_camera_power_init(). Also note, that in
	 * that case regulators are attached to the I2C device and not to the
	 * camera platform device.
2207
	 */
2208 2209
	ret = devm_regulator_bulk_get(&pdev->dev, ssdd->sd_pdata.num_regulators,
				      ssdd->sd_pdata.regulators);
2210 2211 2212
	if (ret < 0)
		return ret;

2213 2214
	icd->iface = sdesc->host_desc.bus_id;
	icd->sdesc = sdesc;
2215
	icd->pdev = &pdev->dev;
2216
	platform_set_drvdata(pdev, icd);
2217

2218 2219 2220
	icd->user_width		= DEFAULT_WIDTH;
	icd->user_height	= DEFAULT_HEIGHT;

2221
	return soc_camera_device_register(icd);
2222
}
2223

2224 2225
/*
 * Only called on rmmod for each platform device, since they are not
2226
 * hot-pluggable. Now we know, that all our users - hosts and devices have
2227 2228
 * been unloaded already
 */
2229
static int soc_camera_pdrv_remove(struct platform_device *pdev)
2230
{
2231
	struct soc_camera_device *icd = platform_get_drvdata(pdev);
2232
	int i;
2233

2234
	if (!icd)
2235 2236
		return -EINVAL;

2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250
	i = pdev->id;
	if (i < 0)
		i = 0;

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

2252 2253 2254 2255
	return 0;
}

static struct platform_driver __refdata soc_camera_pdrv = {
2256
	.probe = soc_camera_pdrv_probe,
2257
	.remove  = soc_camera_pdrv_remove,
2258 2259
	.driver  = {
		.name	= "soc-camera-pdrv",
2260 2261 2262
	},
};

2263
module_platform_driver(soc_camera_pdrv);
2264 2265 2266 2267

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