PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / media / platform / soc_camera / soc_camera.c
blob4b8c024fc487d3267bd905a0b42533b26e5975f3
1 /*
2 * camera image capture (abstract) bus driver
4 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
6 * This driver provides an interface between platform-specific camera
7 * busses and camera devices. It should be used if the camera is
8 * connected not over a "proper" bus like PCI or USB, but over a
9 * special bus, like, for example, the Quick Capture interface on PXA270
10 * SoCs. Later it should also be used for i.MX31 SoCs from Freescale.
11 * It can handle multiple cameras and / or multiple busses, which can
12 * be used, e.g., in stereo-vision applications.
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
19 #include <linux/device.h>
20 #include <linux/err.h>
21 #include <linux/i2c.h>
22 #include <linux/init.h>
23 #include <linux/list.h>
24 #include <linux/module.h>
25 #include <linux/mutex.h>
26 #include <linux/platform_device.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/regulator/consumer.h>
29 #include <linux/slab.h>
30 #include <linux/vmalloc.h>
32 #include <media/soc_camera.h>
33 #include <media/soc_mediabus.h>
34 #include <media/v4l2-async.h>
35 #include <media/v4l2-clk.h>
36 #include <media/v4l2-common.h>
37 #include <media/v4l2-ioctl.h>
38 #include <media/v4l2-dev.h>
39 #include <media/videobuf-core.h>
40 #include <media/videobuf2-core.h>
42 /* Default to VGA resolution */
43 #define DEFAULT_WIDTH 640
44 #define DEFAULT_HEIGHT 480
46 #define is_streaming(ici, icd) \
47 (((ici)->ops->init_videobuf) ? \
48 (icd)->vb_vidq.streaming : \
49 vb2_is_streaming(&(icd)->vb2_vidq))
51 #define MAP_MAX_NUM 32
52 static DECLARE_BITMAP(device_map, MAP_MAX_NUM);
53 static LIST_HEAD(hosts);
54 static LIST_HEAD(devices);
56 * Protects lists and bitmaps of hosts and devices.
57 * Lock nesting: Ok to take ->host_lock under list_lock.
59 static DEFINE_MUTEX(list_lock);
61 struct soc_camera_async_client {
62 struct v4l2_async_subdev *sensor;
63 struct v4l2_async_notifier notifier;
64 struct platform_device *pdev;
65 struct list_head list; /* needed for clean up */
68 static int soc_camera_video_start(struct soc_camera_device *icd);
69 static int video_dev_create(struct soc_camera_device *icd);
71 int soc_camera_power_on(struct device *dev, struct soc_camera_subdev_desc *ssdd,
72 struct v4l2_clk *clk)
74 int ret;
75 bool clock_toggle;
77 if (clk && (!ssdd->unbalanced_power ||
78 !test_and_set_bit(0, &ssdd->clock_state))) {
79 ret = v4l2_clk_enable(clk);
80 if (ret < 0) {
81 dev_err(dev, "Cannot enable clock: %d\n", ret);
82 return ret;
84 clock_toggle = true;
85 } else {
86 clock_toggle = false;
89 ret = regulator_bulk_enable(ssdd->sd_pdata.num_regulators,
90 ssdd->sd_pdata.regulators);
91 if (ret < 0) {
92 dev_err(dev, "Cannot enable regulators\n");
93 goto eregenable;
96 if (ssdd->power) {
97 ret = ssdd->power(dev, 1);
98 if (ret < 0) {
99 dev_err(dev,
100 "Platform failed to power-on the camera.\n");
101 goto epwron;
105 return 0;
107 epwron:
108 regulator_bulk_disable(ssdd->sd_pdata.num_regulators,
109 ssdd->sd_pdata.regulators);
110 eregenable:
111 if (clock_toggle)
112 v4l2_clk_disable(clk);
114 return ret;
116 EXPORT_SYMBOL(soc_camera_power_on);
118 int soc_camera_power_off(struct device *dev, struct soc_camera_subdev_desc *ssdd,
119 struct v4l2_clk *clk)
121 int ret = 0;
122 int err;
124 if (ssdd->power) {
125 err = ssdd->power(dev, 0);
126 if (err < 0) {
127 dev_err(dev,
128 "Platform failed to power-off the camera.\n");
129 ret = err;
133 err = regulator_bulk_disable(ssdd->sd_pdata.num_regulators,
134 ssdd->sd_pdata.regulators);
135 if (err < 0) {
136 dev_err(dev, "Cannot disable regulators\n");
137 ret = ret ? : err;
140 if (clk && (!ssdd->unbalanced_power || test_and_clear_bit(0, &ssdd->clock_state)))
141 v4l2_clk_disable(clk);
143 return ret;
145 EXPORT_SYMBOL(soc_camera_power_off);
147 int soc_camera_power_init(struct device *dev, struct soc_camera_subdev_desc *ssdd)
149 /* Should not have any effect in synchronous case */
150 return devm_regulator_bulk_get(dev, ssdd->sd_pdata.num_regulators,
151 ssdd->sd_pdata.regulators);
153 EXPORT_SYMBOL(soc_camera_power_init);
155 static int __soc_camera_power_on(struct soc_camera_device *icd)
157 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
158 int ret;
160 ret = v4l2_subdev_call(sd, core, s_power, 1);
161 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
162 return ret;
164 return 0;
167 static int __soc_camera_power_off(struct soc_camera_device *icd)
169 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
170 int ret;
172 ret = v4l2_subdev_call(sd, core, s_power, 0);
173 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
174 return ret;
176 return 0;
179 const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
180 struct soc_camera_device *icd, unsigned int fourcc)
182 unsigned int i;
184 for (i = 0; i < icd->num_user_formats; i++)
185 if (icd->user_formats[i].host_fmt->fourcc == fourcc)
186 return icd->user_formats + i;
187 return NULL;
189 EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);
192 * soc_camera_apply_board_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
193 * @ssdd: camera platform parameters
194 * @cfg: media bus configuration
195 * @return: resulting flags
197 unsigned long soc_camera_apply_board_flags(struct soc_camera_subdev_desc *ssdd,
198 const struct v4l2_mbus_config *cfg)
200 unsigned long f, flags = cfg->flags;
202 /* If only one of the two polarities is supported, switch to the opposite */
203 if (ssdd->flags & SOCAM_SENSOR_INVERT_HSYNC) {
204 f = flags & (V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW);
205 if (f == V4L2_MBUS_HSYNC_ACTIVE_HIGH || f == V4L2_MBUS_HSYNC_ACTIVE_LOW)
206 flags ^= V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW;
209 if (ssdd->flags & SOCAM_SENSOR_INVERT_VSYNC) {
210 f = flags & (V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW);
211 if (f == V4L2_MBUS_VSYNC_ACTIVE_HIGH || f == V4L2_MBUS_VSYNC_ACTIVE_LOW)
212 flags ^= V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW;
215 if (ssdd->flags & SOCAM_SENSOR_INVERT_PCLK) {
216 f = flags & (V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING);
217 if (f == V4L2_MBUS_PCLK_SAMPLE_RISING || f == V4L2_MBUS_PCLK_SAMPLE_FALLING)
218 flags ^= V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING;
221 return flags;
223 EXPORT_SYMBOL(soc_camera_apply_board_flags);
225 #define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
226 ((x) >> 24) & 0xff
228 static int soc_camera_try_fmt(struct soc_camera_device *icd,
229 struct v4l2_format *f)
231 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
232 const struct soc_camera_format_xlate *xlate;
233 struct v4l2_pix_format *pix = &f->fmt.pix;
234 int ret;
236 dev_dbg(icd->pdev, "TRY_FMT(%c%c%c%c, %ux%u)\n",
237 pixfmtstr(pix->pixelformat), pix->width, pix->height);
239 if (pix->pixelformat != V4L2_PIX_FMT_JPEG &&
240 !(ici->capabilities & SOCAM_HOST_CAP_STRIDE)) {
241 pix->bytesperline = 0;
242 pix->sizeimage = 0;
245 ret = ici->ops->try_fmt(icd, f);
246 if (ret < 0)
247 return ret;
249 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
250 if (!xlate)
251 return -EINVAL;
253 ret = soc_mbus_bytes_per_line(pix->width, xlate->host_fmt);
254 if (ret < 0)
255 return ret;
257 pix->bytesperline = max_t(u32, pix->bytesperline, ret);
259 ret = soc_mbus_image_size(xlate->host_fmt, pix->bytesperline,
260 pix->height);
261 if (ret < 0)
262 return ret;
264 pix->sizeimage = max_t(u32, pix->sizeimage, ret);
266 return 0;
269 static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
270 struct v4l2_format *f)
272 struct soc_camera_device *icd = file->private_data;
274 WARN_ON(priv != file->private_data);
276 /* Only single-plane capture is supported so far */
277 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
278 return -EINVAL;
280 /* limit format to hardware capabilities */
281 return soc_camera_try_fmt(icd, f);
284 static int soc_camera_enum_input(struct file *file, void *priv,
285 struct v4l2_input *inp)
287 if (inp->index != 0)
288 return -EINVAL;
290 /* default is camera */
291 inp->type = V4L2_INPUT_TYPE_CAMERA;
292 strcpy(inp->name, "Camera");
294 return 0;
297 static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
299 *i = 0;
301 return 0;
304 static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
306 if (i > 0)
307 return -EINVAL;
309 return 0;
312 static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id a)
314 struct soc_camera_device *icd = file->private_data;
315 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
317 return v4l2_subdev_call(sd, core, s_std, a);
320 static int soc_camera_g_std(struct file *file, void *priv, v4l2_std_id *a)
322 struct soc_camera_device *icd = file->private_data;
323 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
325 return v4l2_subdev_call(sd, core, g_std, a);
328 static int soc_camera_enum_framesizes(struct file *file, void *fh,
329 struct v4l2_frmsizeenum *fsize)
331 struct soc_camera_device *icd = file->private_data;
332 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
334 return ici->ops->enum_framesizes(icd, fsize);
337 static int soc_camera_reqbufs(struct file *file, void *priv,
338 struct v4l2_requestbuffers *p)
340 int ret;
341 struct soc_camera_device *icd = file->private_data;
342 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
344 WARN_ON(priv != file->private_data);
346 if (icd->streamer && icd->streamer != file)
347 return -EBUSY;
349 if (ici->ops->init_videobuf) {
350 ret = videobuf_reqbufs(&icd->vb_vidq, p);
351 if (ret < 0)
352 return ret;
354 ret = ici->ops->reqbufs(icd, p);
355 } else {
356 ret = vb2_reqbufs(&icd->vb2_vidq, p);
359 if (!ret && !icd->streamer)
360 icd->streamer = file;
362 return ret;
365 static int soc_camera_querybuf(struct file *file, void *priv,
366 struct v4l2_buffer *p)
368 struct soc_camera_device *icd = file->private_data;
369 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
371 WARN_ON(priv != file->private_data);
373 if (ici->ops->init_videobuf)
374 return videobuf_querybuf(&icd->vb_vidq, p);
375 else
376 return vb2_querybuf(&icd->vb2_vidq, p);
379 static int soc_camera_qbuf(struct file *file, void *priv,
380 struct v4l2_buffer *p)
382 struct soc_camera_device *icd = file->private_data;
383 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
385 WARN_ON(priv != file->private_data);
387 if (icd->streamer != file)
388 return -EBUSY;
390 if (ici->ops->init_videobuf)
391 return videobuf_qbuf(&icd->vb_vidq, p);
392 else
393 return vb2_qbuf(&icd->vb2_vidq, p);
396 static int soc_camera_dqbuf(struct file *file, void *priv,
397 struct v4l2_buffer *p)
399 struct soc_camera_device *icd = file->private_data;
400 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
402 WARN_ON(priv != file->private_data);
404 if (icd->streamer != file)
405 return -EBUSY;
407 if (ici->ops->init_videobuf)
408 return videobuf_dqbuf(&icd->vb_vidq, p, file->f_flags & O_NONBLOCK);
409 else
410 return vb2_dqbuf(&icd->vb2_vidq, p, file->f_flags & O_NONBLOCK);
413 static int soc_camera_create_bufs(struct file *file, void *priv,
414 struct v4l2_create_buffers *create)
416 struct soc_camera_device *icd = file->private_data;
417 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
419 /* videobuf2 only */
420 if (ici->ops->init_videobuf)
421 return -EINVAL;
422 else
423 return vb2_create_bufs(&icd->vb2_vidq, create);
426 static int soc_camera_prepare_buf(struct file *file, void *priv,
427 struct v4l2_buffer *b)
429 struct soc_camera_device *icd = file->private_data;
430 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
432 /* videobuf2 only */
433 if (ici->ops->init_videobuf)
434 return -EINVAL;
435 else
436 return vb2_prepare_buf(&icd->vb2_vidq, b);
439 /* Always entered with .host_lock held */
440 static int soc_camera_init_user_formats(struct soc_camera_device *icd)
442 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
443 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
444 unsigned int i, fmts = 0, raw_fmts = 0;
445 int ret;
446 enum v4l2_mbus_pixelcode code;
448 while (!v4l2_subdev_call(sd, video, enum_mbus_fmt, raw_fmts, &code))
449 raw_fmts++;
451 if (!ici->ops->get_formats)
453 * Fallback mode - the host will have to serve all
454 * sensor-provided formats one-to-one to the user
456 fmts = raw_fmts;
457 else
459 * First pass - only count formats this host-sensor
460 * configuration can provide
462 for (i = 0; i < raw_fmts; i++) {
463 ret = ici->ops->get_formats(icd, i, NULL);
464 if (ret < 0)
465 return ret;
466 fmts += ret;
469 if (!fmts)
470 return -ENXIO;
472 icd->user_formats =
473 vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
474 if (!icd->user_formats)
475 return -ENOMEM;
477 dev_dbg(icd->pdev, "Found %d supported formats.\n", fmts);
479 /* Second pass - actually fill data formats */
480 fmts = 0;
481 for (i = 0; i < raw_fmts; i++)
482 if (!ici->ops->get_formats) {
483 v4l2_subdev_call(sd, video, enum_mbus_fmt, i, &code);
484 icd->user_formats[fmts].host_fmt =
485 soc_mbus_get_fmtdesc(code);
486 if (icd->user_formats[fmts].host_fmt)
487 icd->user_formats[fmts++].code = code;
488 } else {
489 ret = ici->ops->get_formats(icd, i,
490 &icd->user_formats[fmts]);
491 if (ret < 0)
492 goto egfmt;
493 fmts += ret;
496 icd->num_user_formats = fmts;
497 icd->current_fmt = &icd->user_formats[0];
499 return 0;
501 egfmt:
502 vfree(icd->user_formats);
503 return ret;
506 /* Always entered with .host_lock held */
507 static void soc_camera_free_user_formats(struct soc_camera_device *icd)
509 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
511 if (ici->ops->put_formats)
512 ici->ops->put_formats(icd);
513 icd->current_fmt = NULL;
514 icd->num_user_formats = 0;
515 vfree(icd->user_formats);
516 icd->user_formats = NULL;
519 /* Called with .vb_lock held, or from the first open(2), see comment there */
520 static int soc_camera_set_fmt(struct soc_camera_device *icd,
521 struct v4l2_format *f)
523 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
524 struct v4l2_pix_format *pix = &f->fmt.pix;
525 int ret;
527 dev_dbg(icd->pdev, "S_FMT(%c%c%c%c, %ux%u)\n",
528 pixfmtstr(pix->pixelformat), pix->width, pix->height);
530 /* We always call try_fmt() before set_fmt() or set_crop() */
531 ret = soc_camera_try_fmt(icd, f);
532 if (ret < 0)
533 return ret;
535 ret = ici->ops->set_fmt(icd, f);
536 if (ret < 0) {
537 return ret;
538 } else if (!icd->current_fmt ||
539 icd->current_fmt->host_fmt->fourcc != pix->pixelformat) {
540 dev_err(icd->pdev,
541 "Host driver hasn't set up current format correctly!\n");
542 return -EINVAL;
545 icd->user_width = pix->width;
546 icd->user_height = pix->height;
547 icd->bytesperline = pix->bytesperline;
548 icd->sizeimage = pix->sizeimage;
549 icd->colorspace = pix->colorspace;
550 icd->field = pix->field;
551 if (ici->ops->init_videobuf)
552 icd->vb_vidq.field = pix->field;
554 dev_dbg(icd->pdev, "set width: %d height: %d\n",
555 icd->user_width, icd->user_height);
557 /* set physical bus parameters */
558 return ici->ops->set_bus_param(icd);
561 static int soc_camera_add_device(struct soc_camera_device *icd)
563 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
564 int ret;
566 if (ici->icd)
567 return -EBUSY;
569 if (!icd->clk) {
570 mutex_lock(&ici->clk_lock);
571 ret = ici->ops->clock_start(ici);
572 mutex_unlock(&ici->clk_lock);
573 if (ret < 0)
574 return ret;
577 if (ici->ops->add) {
578 ret = ici->ops->add(icd);
579 if (ret < 0)
580 goto eadd;
583 ici->icd = icd;
585 return 0;
587 eadd:
588 if (!icd->clk) {
589 mutex_lock(&ici->clk_lock);
590 ici->ops->clock_stop(ici);
591 mutex_unlock(&ici->clk_lock);
593 return ret;
596 static void soc_camera_remove_device(struct soc_camera_device *icd)
598 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
600 if (WARN_ON(icd != ici->icd))
601 return;
603 if (ici->ops->remove)
604 ici->ops->remove(icd);
605 if (!icd->clk) {
606 mutex_lock(&ici->clk_lock);
607 ici->ops->clock_stop(ici);
608 mutex_unlock(&ici->clk_lock);
610 ici->icd = NULL;
613 static int soc_camera_open(struct file *file)
615 struct video_device *vdev = video_devdata(file);
616 struct soc_camera_device *icd;
617 struct soc_camera_host *ici;
618 int ret;
621 * Don't mess with the host during probe: wait until the loop in
622 * scan_add_host() completes. Also protect against a race with
623 * soc_camera_host_unregister().
625 if (mutex_lock_interruptible(&list_lock))
626 return -ERESTARTSYS;
628 if (!vdev || !video_is_registered(vdev)) {
629 mutex_unlock(&list_lock);
630 return -ENODEV;
633 icd = video_get_drvdata(vdev);
634 ici = to_soc_camera_host(icd->parent);
636 ret = try_module_get(ici->ops->owner) ? 0 : -ENODEV;
637 mutex_unlock(&list_lock);
639 if (ret < 0) {
640 dev_err(icd->pdev, "Couldn't lock capture bus driver.\n");
641 return ret;
644 if (!to_soc_camera_control(icd)) {
645 /* No device driver attached */
646 ret = -ENODEV;
647 goto econtrol;
650 if (mutex_lock_interruptible(&ici->host_lock)) {
651 ret = -ERESTARTSYS;
652 goto elockhost;
654 icd->use_count++;
656 /* Now we really have to activate the camera */
657 if (icd->use_count == 1) {
658 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
659 /* Restore parameters before the last close() per V4L2 API */
660 struct v4l2_format f = {
661 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
662 .fmt.pix = {
663 .width = icd->user_width,
664 .height = icd->user_height,
665 .field = icd->field,
666 .colorspace = icd->colorspace,
667 .pixelformat =
668 icd->current_fmt->host_fmt->fourcc,
672 /* The camera could have been already on, try to reset */
673 if (sdesc->subdev_desc.reset)
674 sdesc->subdev_desc.reset(icd->pdev);
676 ret = soc_camera_add_device(icd);
677 if (ret < 0) {
678 dev_err(icd->pdev, "Couldn't activate the camera: %d\n", ret);
679 goto eiciadd;
682 ret = __soc_camera_power_on(icd);
683 if (ret < 0)
684 goto epower;
686 pm_runtime_enable(&icd->vdev->dev);
687 ret = pm_runtime_resume(&icd->vdev->dev);
688 if (ret < 0 && ret != -ENOSYS)
689 goto eresume;
692 * Try to configure with default parameters. Notice: this is the
693 * very first open, so, we cannot race against other calls,
694 * apart from someone else calling open() simultaneously, but
695 * .host_lock is protecting us against it.
697 ret = soc_camera_set_fmt(icd, &f);
698 if (ret < 0)
699 goto esfmt;
701 if (ici->ops->init_videobuf) {
702 ici->ops->init_videobuf(&icd->vb_vidq, icd);
703 } else {
704 ret = ici->ops->init_videobuf2(&icd->vb2_vidq, icd);
705 if (ret < 0)
706 goto einitvb;
708 v4l2_ctrl_handler_setup(&icd->ctrl_handler);
710 mutex_unlock(&ici->host_lock);
712 file->private_data = icd;
713 dev_dbg(icd->pdev, "camera device open\n");
715 return 0;
718 * All errors are entered with the .host_lock held, first four also
719 * with use_count == 1
721 einitvb:
722 esfmt:
723 pm_runtime_disable(&icd->vdev->dev);
724 eresume:
725 __soc_camera_power_off(icd);
726 epower:
727 soc_camera_remove_device(icd);
728 eiciadd:
729 icd->use_count--;
730 mutex_unlock(&ici->host_lock);
731 elockhost:
732 econtrol:
733 module_put(ici->ops->owner);
735 return ret;
738 static int soc_camera_close(struct file *file)
740 struct soc_camera_device *icd = file->private_data;
741 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
743 mutex_lock(&ici->host_lock);
744 icd->use_count--;
745 if (!icd->use_count) {
746 pm_runtime_suspend(&icd->vdev->dev);
747 pm_runtime_disable(&icd->vdev->dev);
749 if (ici->ops->init_videobuf2)
750 vb2_queue_release(&icd->vb2_vidq);
751 __soc_camera_power_off(icd);
753 soc_camera_remove_device(icd);
756 if (icd->streamer == file)
757 icd->streamer = NULL;
758 mutex_unlock(&ici->host_lock);
760 module_put(ici->ops->owner);
762 dev_dbg(icd->pdev, "camera device close\n");
764 return 0;
767 static ssize_t soc_camera_read(struct file *file, char __user *buf,
768 size_t count, loff_t *ppos)
770 struct soc_camera_device *icd = file->private_data;
771 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
773 dev_dbg(icd->pdev, "read called, buf %p\n", buf);
775 if (ici->ops->init_videobuf2 && icd->vb2_vidq.io_modes & VB2_READ)
776 return vb2_read(&icd->vb2_vidq, buf, count, ppos,
777 file->f_flags & O_NONBLOCK);
779 dev_err(icd->pdev, "camera device read not implemented\n");
781 return -EINVAL;
784 static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
786 struct soc_camera_device *icd = file->private_data;
787 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
788 int err;
790 dev_dbg(icd->pdev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
792 if (icd->streamer != file)
793 return -EBUSY;
795 if (mutex_lock_interruptible(&ici->host_lock))
796 return -ERESTARTSYS;
797 if (ici->ops->init_videobuf)
798 err = videobuf_mmap_mapper(&icd->vb_vidq, vma);
799 else
800 err = vb2_mmap(&icd->vb2_vidq, vma);
801 mutex_unlock(&ici->host_lock);
803 dev_dbg(icd->pdev, "vma start=0x%08lx, size=%ld, ret=%d\n",
804 (unsigned long)vma->vm_start,
805 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
806 err);
808 return err;
811 static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
813 struct soc_camera_device *icd = file->private_data;
814 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
815 unsigned res = POLLERR;
817 if (icd->streamer != file)
818 return POLLERR;
820 mutex_lock(&ici->host_lock);
821 if (ici->ops->init_videobuf && list_empty(&icd->vb_vidq.stream))
822 dev_err(icd->pdev, "Trying to poll with no queued buffers!\n");
823 else
824 res = ici->ops->poll(file, pt);
825 mutex_unlock(&ici->host_lock);
826 return res;
829 void soc_camera_lock(struct vb2_queue *vq)
831 struct soc_camera_device *icd = vb2_get_drv_priv(vq);
832 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
833 mutex_lock(&ici->host_lock);
835 EXPORT_SYMBOL(soc_camera_lock);
837 void soc_camera_unlock(struct vb2_queue *vq)
839 struct soc_camera_device *icd = vb2_get_drv_priv(vq);
840 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
841 mutex_unlock(&ici->host_lock);
843 EXPORT_SYMBOL(soc_camera_unlock);
845 static struct v4l2_file_operations soc_camera_fops = {
846 .owner = THIS_MODULE,
847 .open = soc_camera_open,
848 .release = soc_camera_close,
849 .unlocked_ioctl = video_ioctl2,
850 .read = soc_camera_read,
851 .mmap = soc_camera_mmap,
852 .poll = soc_camera_poll,
855 static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
856 struct v4l2_format *f)
858 struct soc_camera_device *icd = file->private_data;
859 int ret;
861 WARN_ON(priv != file->private_data);
863 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
864 dev_warn(icd->pdev, "Wrong buf-type %d\n", f->type);
865 return -EINVAL;
868 if (icd->streamer && icd->streamer != file)
869 return -EBUSY;
871 if (is_streaming(to_soc_camera_host(icd->parent), icd)) {
872 dev_err(icd->pdev, "S_FMT denied: queue initialised\n");
873 return -EBUSY;
876 ret = soc_camera_set_fmt(icd, f);
878 if (!ret && !icd->streamer)
879 icd->streamer = file;
881 return ret;
884 static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
885 struct v4l2_fmtdesc *f)
887 struct soc_camera_device *icd = file->private_data;
888 const struct soc_mbus_pixelfmt *format;
890 WARN_ON(priv != file->private_data);
892 if (f->index >= icd->num_user_formats)
893 return -EINVAL;
895 format = icd->user_formats[f->index].host_fmt;
897 if (format->name)
898 strlcpy(f->description, format->name, sizeof(f->description));
899 f->pixelformat = format->fourcc;
900 return 0;
903 static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
904 struct v4l2_format *f)
906 struct soc_camera_device *icd = file->private_data;
907 struct v4l2_pix_format *pix = &f->fmt.pix;
909 WARN_ON(priv != file->private_data);
911 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
912 return -EINVAL;
914 pix->width = icd->user_width;
915 pix->height = icd->user_height;
916 pix->bytesperline = icd->bytesperline;
917 pix->sizeimage = icd->sizeimage;
918 pix->field = icd->field;
919 pix->pixelformat = icd->current_fmt->host_fmt->fourcc;
920 pix->colorspace = icd->colorspace;
921 dev_dbg(icd->pdev, "current_fmt->fourcc: 0x%08x\n",
922 icd->current_fmt->host_fmt->fourcc);
923 return 0;
926 static int soc_camera_querycap(struct file *file, void *priv,
927 struct v4l2_capability *cap)
929 struct soc_camera_device *icd = file->private_data;
930 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
932 WARN_ON(priv != file->private_data);
934 strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
935 return ici->ops->querycap(ici, cap);
938 static int soc_camera_streamon(struct file *file, void *priv,
939 enum v4l2_buf_type i)
941 struct soc_camera_device *icd = file->private_data;
942 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
943 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
944 int ret;
946 WARN_ON(priv != file->private_data);
948 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
949 return -EINVAL;
951 if (icd->streamer != file)
952 return -EBUSY;
954 /* This calls buf_queue from host driver's videobuf_queue_ops */
955 if (ici->ops->init_videobuf)
956 ret = videobuf_streamon(&icd->vb_vidq);
957 else
958 ret = vb2_streamon(&icd->vb2_vidq, i);
960 if (!ret)
961 v4l2_subdev_call(sd, video, s_stream, 1);
963 return ret;
966 static int soc_camera_streamoff(struct file *file, void *priv,
967 enum v4l2_buf_type i)
969 struct soc_camera_device *icd = file->private_data;
970 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
971 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
973 WARN_ON(priv != file->private_data);
975 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
976 return -EINVAL;
978 if (icd->streamer != file)
979 return -EBUSY;
982 * This calls buf_release from host driver's videobuf_queue_ops for all
983 * remaining buffers. When the last buffer is freed, stop capture
985 if (ici->ops->init_videobuf)
986 videobuf_streamoff(&icd->vb_vidq);
987 else
988 vb2_streamoff(&icd->vb2_vidq, i);
990 v4l2_subdev_call(sd, video, s_stream, 0);
992 return 0;
995 static int soc_camera_cropcap(struct file *file, void *fh,
996 struct v4l2_cropcap *a)
998 struct soc_camera_device *icd = file->private_data;
999 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1001 return ici->ops->cropcap(icd, a);
1004 static int soc_camera_g_crop(struct file *file, void *fh,
1005 struct v4l2_crop *a)
1007 struct soc_camera_device *icd = file->private_data;
1008 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1009 int ret;
1011 ret = ici->ops->get_crop(icd, a);
1013 return ret;
1017 * According to the V4L2 API, drivers shall not update the struct v4l2_crop
1018 * argument with the actual geometry, instead, the user shall use G_CROP to
1019 * retrieve it.
1021 static int soc_camera_s_crop(struct file *file, void *fh,
1022 const struct v4l2_crop *a)
1024 struct soc_camera_device *icd = file->private_data;
1025 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1026 const struct v4l2_rect *rect = &a->c;
1027 struct v4l2_crop current_crop;
1028 int ret;
1030 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1031 return -EINVAL;
1033 dev_dbg(icd->pdev, "S_CROP(%ux%u@%u:%u)\n",
1034 rect->width, rect->height, rect->left, rect->top);
1036 current_crop.type = a->type;
1038 /* If get_crop fails, we'll let host and / or client drivers decide */
1039 ret = ici->ops->get_crop(icd, &current_crop);
1041 /* Prohibit window size change with initialised buffers */
1042 if (ret < 0) {
1043 dev_err(icd->pdev,
1044 "S_CROP denied: getting current crop failed\n");
1045 } else if ((a->c.width == current_crop.c.width &&
1046 a->c.height == current_crop.c.height) ||
1047 !is_streaming(ici, icd)) {
1048 /* same size or not streaming - use .set_crop() */
1049 ret = ici->ops->set_crop(icd, a);
1050 } else if (ici->ops->set_livecrop) {
1051 ret = ici->ops->set_livecrop(icd, a);
1052 } else {
1053 dev_err(icd->pdev,
1054 "S_CROP denied: queue initialised and sizes differ\n");
1055 ret = -EBUSY;
1058 return ret;
1061 static int soc_camera_g_selection(struct file *file, void *fh,
1062 struct v4l2_selection *s)
1064 struct soc_camera_device *icd = file->private_data;
1065 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1067 /* With a wrong type no need to try to fall back to cropping */
1068 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1069 return -EINVAL;
1071 if (!ici->ops->get_selection)
1072 return -ENOTTY;
1074 return ici->ops->get_selection(icd, s);
1077 static int soc_camera_s_selection(struct file *file, void *fh,
1078 struct v4l2_selection *s)
1080 struct soc_camera_device *icd = file->private_data;
1081 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1082 int ret;
1084 /* In all these cases cropping emulation will not help */
1085 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1086 (s->target != V4L2_SEL_TGT_COMPOSE &&
1087 s->target != V4L2_SEL_TGT_CROP))
1088 return -EINVAL;
1090 if (s->target == V4L2_SEL_TGT_COMPOSE) {
1091 /* No output size change during a running capture! */
1092 if (is_streaming(ici, icd) &&
1093 (icd->user_width != s->r.width ||
1094 icd->user_height != s->r.height))
1095 return -EBUSY;
1098 * Only one user is allowed to change the output format, touch
1099 * buffers, start / stop streaming, poll for data
1101 if (icd->streamer && icd->streamer != file)
1102 return -EBUSY;
1105 if (!ici->ops->set_selection)
1106 return -ENOTTY;
1108 ret = ici->ops->set_selection(icd, s);
1109 if (!ret &&
1110 s->target == V4L2_SEL_TGT_COMPOSE) {
1111 icd->user_width = s->r.width;
1112 icd->user_height = s->r.height;
1113 if (!icd->streamer)
1114 icd->streamer = file;
1117 return ret;
1120 static int soc_camera_g_parm(struct file *file, void *fh,
1121 struct v4l2_streamparm *a)
1123 struct soc_camera_device *icd = file->private_data;
1124 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1126 if (ici->ops->get_parm)
1127 return ici->ops->get_parm(icd, a);
1129 return -ENOIOCTLCMD;
1132 static int soc_camera_s_parm(struct file *file, void *fh,
1133 struct v4l2_streamparm *a)
1135 struct soc_camera_device *icd = file->private_data;
1136 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1138 if (ici->ops->set_parm)
1139 return ici->ops->set_parm(icd, a);
1141 return -ENOIOCTLCMD;
1144 static int soc_camera_probe(struct soc_camera_host *ici,
1145 struct soc_camera_device *icd);
1147 /* So far this function cannot fail */
1148 static void scan_add_host(struct soc_camera_host *ici)
1150 struct soc_camera_device *icd;
1152 mutex_lock(&list_lock);
1154 list_for_each_entry(icd, &devices, list)
1155 if (icd->iface == ici->nr) {
1156 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
1157 struct soc_camera_subdev_desc *ssdd = &sdesc->subdev_desc;
1159 /* The camera could have been already on, try to reset */
1160 if (ssdd->reset)
1161 ssdd->reset(icd->pdev);
1163 icd->parent = ici->v4l2_dev.dev;
1165 /* Ignore errors */
1166 soc_camera_probe(ici, icd);
1169 mutex_unlock(&list_lock);
1173 * It is invalid to call v4l2_clk_enable() after a successful probing
1174 * asynchronously outside of V4L2 operations, i.e. with .host_lock not held.
1176 static int soc_camera_clk_enable(struct v4l2_clk *clk)
1178 struct soc_camera_device *icd = clk->priv;
1179 struct soc_camera_host *ici;
1180 int ret;
1182 if (!icd || !icd->parent)
1183 return -ENODEV;
1185 ici = to_soc_camera_host(icd->parent);
1187 if (!try_module_get(ici->ops->owner))
1188 return -ENODEV;
1191 * If a different client is currently being probed, the host will tell
1192 * you to go
1194 mutex_lock(&ici->clk_lock);
1195 ret = ici->ops->clock_start(ici);
1196 mutex_unlock(&ici->clk_lock);
1197 return ret;
1200 static void soc_camera_clk_disable(struct v4l2_clk *clk)
1202 struct soc_camera_device *icd = clk->priv;
1203 struct soc_camera_host *ici;
1205 if (!icd || !icd->parent)
1206 return;
1208 ici = to_soc_camera_host(icd->parent);
1210 mutex_lock(&ici->clk_lock);
1211 ici->ops->clock_stop(ici);
1212 mutex_unlock(&ici->clk_lock);
1214 module_put(ici->ops->owner);
1218 * Eventually, it would be more logical to make the respective host the clock
1219 * owner, but then we would have to copy this struct for each ici. Besides, it
1220 * would introduce the circular dependency problem, unless we port all client
1221 * drivers to release the clock, when not in use.
1223 static const struct v4l2_clk_ops soc_camera_clk_ops = {
1224 .owner = THIS_MODULE,
1225 .enable = soc_camera_clk_enable,
1226 .disable = soc_camera_clk_disable,
1229 static int soc_camera_dyn_pdev(struct soc_camera_desc *sdesc,
1230 struct soc_camera_async_client *sasc)
1232 struct platform_device *pdev;
1233 int ret, i;
1235 mutex_lock(&list_lock);
1236 i = find_first_zero_bit(device_map, MAP_MAX_NUM);
1237 if (i < MAP_MAX_NUM)
1238 set_bit(i, device_map);
1239 mutex_unlock(&list_lock);
1240 if (i >= MAP_MAX_NUM)
1241 return -ENOMEM;
1243 pdev = platform_device_alloc("soc-camera-pdrv", i);
1244 if (!pdev)
1245 return -ENOMEM;
1247 ret = platform_device_add_data(pdev, sdesc, sizeof(*sdesc));
1248 if (ret < 0) {
1249 platform_device_put(pdev);
1250 return ret;
1253 sasc->pdev = pdev;
1255 return 0;
1258 static struct soc_camera_device *soc_camera_add_pdev(struct soc_camera_async_client *sasc)
1260 struct platform_device *pdev = sasc->pdev;
1261 int ret;
1263 ret = platform_device_add(pdev);
1264 if (ret < 0 || !pdev->dev.driver)
1265 return NULL;
1267 return platform_get_drvdata(pdev);
1270 /* Locking: called with .host_lock held */
1271 static int soc_camera_probe_finish(struct soc_camera_device *icd)
1273 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1274 struct v4l2_mbus_framefmt mf;
1275 int ret;
1277 sd->grp_id = soc_camera_grp_id(icd);
1278 v4l2_set_subdev_hostdata(sd, icd);
1280 ret = v4l2_ctrl_add_handler(&icd->ctrl_handler, sd->ctrl_handler, NULL);
1281 if (ret < 0)
1282 return ret;
1284 ret = soc_camera_add_device(icd);
1285 if (ret < 0) {
1286 dev_err(icd->pdev, "Couldn't activate the camera: %d\n", ret);
1287 return ret;
1290 /* At this point client .probe() should have run already */
1291 ret = soc_camera_init_user_formats(icd);
1292 if (ret < 0)
1293 goto eusrfmt;
1295 icd->field = V4L2_FIELD_ANY;
1297 ret = soc_camera_video_start(icd);
1298 if (ret < 0)
1299 goto evidstart;
1301 /* Try to improve our guess of a reasonable window format */
1302 if (!v4l2_subdev_call(sd, video, g_mbus_fmt, &mf)) {
1303 icd->user_width = mf.width;
1304 icd->user_height = mf.height;
1305 icd->colorspace = mf.colorspace;
1306 icd->field = mf.field;
1308 soc_camera_remove_device(icd);
1310 return 0;
1312 evidstart:
1313 soc_camera_free_user_formats(icd);
1314 eusrfmt:
1315 soc_camera_remove_device(icd);
1317 return ret;
1320 #ifdef CONFIG_I2C_BOARDINFO
1321 static int soc_camera_i2c_init(struct soc_camera_device *icd,
1322 struct soc_camera_desc *sdesc)
1324 struct soc_camera_subdev_desc *ssdd;
1325 struct i2c_client *client;
1326 struct soc_camera_host *ici;
1327 struct soc_camera_host_desc *shd = &sdesc->host_desc;
1328 struct i2c_adapter *adap;
1329 struct v4l2_subdev *subdev;
1330 char clk_name[V4L2_SUBDEV_NAME_SIZE];
1331 int ret;
1333 /* First find out how we link the main client */
1334 if (icd->sasc) {
1335 /* Async non-OF probing handled by the subdevice list */
1336 return -EPROBE_DEFER;
1339 ici = to_soc_camera_host(icd->parent);
1340 adap = i2c_get_adapter(shd->i2c_adapter_id);
1341 if (!adap) {
1342 dev_err(icd->pdev, "Cannot get I2C adapter #%d. No driver?\n",
1343 shd->i2c_adapter_id);
1344 return -ENODEV;
1347 ssdd = kzalloc(sizeof(*ssdd), GFP_KERNEL);
1348 if (!ssdd) {
1349 ret = -ENOMEM;
1350 goto ealloc;
1353 memcpy(ssdd, &sdesc->subdev_desc, sizeof(*ssdd));
1355 * In synchronous case we request regulators ourselves in
1356 * soc_camera_pdrv_probe(), make sure the subdevice driver doesn't try
1357 * to allocate them again.
1359 ssdd->sd_pdata.num_regulators = 0;
1360 ssdd->sd_pdata.regulators = NULL;
1361 shd->board_info->platform_data = ssdd;
1363 snprintf(clk_name, sizeof(clk_name), "%d-%04x",
1364 shd->i2c_adapter_id, shd->board_info->addr);
1366 icd->clk = v4l2_clk_register(&soc_camera_clk_ops, clk_name, "mclk", icd);
1367 if (IS_ERR(icd->clk)) {
1368 ret = PTR_ERR(icd->clk);
1369 goto eclkreg;
1372 subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
1373 shd->board_info, NULL);
1374 if (!subdev) {
1375 ret = -ENODEV;
1376 goto ei2cnd;
1379 client = v4l2_get_subdevdata(subdev);
1381 /* Use to_i2c_client(dev) to recover the i2c client */
1382 icd->control = &client->dev;
1384 return 0;
1385 ei2cnd:
1386 v4l2_clk_unregister(icd->clk);
1387 icd->clk = NULL;
1388 eclkreg:
1389 kfree(ssdd);
1390 ealloc:
1391 i2c_put_adapter(adap);
1392 return ret;
1395 static void soc_camera_i2c_free(struct soc_camera_device *icd)
1397 struct i2c_client *client =
1398 to_i2c_client(to_soc_camera_control(icd));
1399 struct i2c_adapter *adap;
1400 struct soc_camera_subdev_desc *ssdd;
1402 icd->control = NULL;
1403 if (icd->sasc)
1404 return;
1406 adap = client->adapter;
1407 ssdd = client->dev.platform_data;
1408 v4l2_device_unregister_subdev(i2c_get_clientdata(client));
1409 i2c_unregister_device(client);
1410 i2c_put_adapter(adap);
1411 kfree(ssdd);
1412 v4l2_clk_unregister(icd->clk);
1413 icd->clk = NULL;
1417 * V4L2 asynchronous notifier callbacks. They are all called under a v4l2-async
1418 * internal global mutex, therefore cannot race against other asynchronous
1419 * events. Until notifier->complete() (soc_camera_async_complete()) is called,
1420 * the video device node is not registered and no V4L fops can occur. Unloading
1421 * of the host driver also calls a v4l2-async function, so also there we're
1422 * protected.
1424 static int soc_camera_async_bound(struct v4l2_async_notifier *notifier,
1425 struct v4l2_subdev *sd,
1426 struct v4l2_async_subdev *asd)
1428 struct soc_camera_async_client *sasc = container_of(notifier,
1429 struct soc_camera_async_client, notifier);
1430 struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev);
1432 if (asd == sasc->sensor && !WARN_ON(icd->control)) {
1433 struct i2c_client *client = v4l2_get_subdevdata(sd);
1436 * Only now we get subdevice-specific information like
1437 * regulators, flags, callbacks, etc.
1439 if (client) {
1440 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
1441 struct soc_camera_subdev_desc *ssdd =
1442 soc_camera_i2c_to_desc(client);
1443 if (ssdd) {
1444 memcpy(&sdesc->subdev_desc, ssdd,
1445 sizeof(sdesc->subdev_desc));
1446 if (ssdd->reset)
1447 ssdd->reset(icd->pdev);
1450 icd->control = &client->dev;
1454 return 0;
1457 static void soc_camera_async_unbind(struct v4l2_async_notifier *notifier,
1458 struct v4l2_subdev *sd,
1459 struct v4l2_async_subdev *asd)
1461 struct soc_camera_async_client *sasc = container_of(notifier,
1462 struct soc_camera_async_client, notifier);
1463 struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev);
1465 if (icd->clk) {
1466 v4l2_clk_unregister(icd->clk);
1467 icd->clk = NULL;
1471 static int soc_camera_async_complete(struct v4l2_async_notifier *notifier)
1473 struct soc_camera_async_client *sasc = container_of(notifier,
1474 struct soc_camera_async_client, notifier);
1475 struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev);
1477 if (to_soc_camera_control(icd)) {
1478 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1479 int ret;
1481 mutex_lock(&list_lock);
1482 ret = soc_camera_probe(ici, icd);
1483 mutex_unlock(&list_lock);
1484 if (ret < 0)
1485 return ret;
1488 return 0;
1491 static int scan_async_group(struct soc_camera_host *ici,
1492 struct v4l2_async_subdev **asd, unsigned int size)
1494 struct soc_camera_async_subdev *sasd;
1495 struct soc_camera_async_client *sasc;
1496 struct soc_camera_device *icd;
1497 struct soc_camera_desc sdesc = {.host_desc.bus_id = ici->nr,};
1498 char clk_name[V4L2_SUBDEV_NAME_SIZE];
1499 unsigned int i;
1500 int ret;
1502 /* First look for a sensor */
1503 for (i = 0; i < size; i++) {
1504 sasd = container_of(asd[i], struct soc_camera_async_subdev, asd);
1505 if (sasd->role == SOCAM_SUBDEV_DATA_SOURCE)
1506 break;
1509 if (i >= size || asd[i]->match_type != V4L2_ASYNC_MATCH_I2C) {
1510 /* All useless */
1511 dev_err(ici->v4l2_dev.dev, "No I2C data source found!\n");
1512 return -ENODEV;
1515 /* Or shall this be managed by the soc-camera device? */
1516 sasc = devm_kzalloc(ici->v4l2_dev.dev, sizeof(*sasc), GFP_KERNEL);
1517 if (!sasc)
1518 return -ENOMEM;
1520 /* HACK: just need a != NULL */
1521 sdesc.host_desc.board_info = ERR_PTR(-ENODATA);
1523 ret = soc_camera_dyn_pdev(&sdesc, sasc);
1524 if (ret < 0)
1525 return ret;
1527 sasc->sensor = &sasd->asd;
1529 icd = soc_camera_add_pdev(sasc);
1530 if (!icd) {
1531 platform_device_put(sasc->pdev);
1532 return -ENOMEM;
1535 sasc->notifier.subdevs = asd;
1536 sasc->notifier.num_subdevs = size;
1537 sasc->notifier.bound = soc_camera_async_bound;
1538 sasc->notifier.unbind = soc_camera_async_unbind;
1539 sasc->notifier.complete = soc_camera_async_complete;
1541 icd->sasc = sasc;
1542 icd->parent = ici->v4l2_dev.dev;
1544 snprintf(clk_name, sizeof(clk_name), "%d-%04x",
1545 sasd->asd.match.i2c.adapter_id, sasd->asd.match.i2c.address);
1547 icd->clk = v4l2_clk_register(&soc_camera_clk_ops, clk_name, "mclk", icd);
1548 if (IS_ERR(icd->clk)) {
1549 ret = PTR_ERR(icd->clk);
1550 goto eclkreg;
1553 ret = v4l2_async_notifier_register(&ici->v4l2_dev, &sasc->notifier);
1554 if (!ret)
1555 return 0;
1557 v4l2_clk_unregister(icd->clk);
1558 eclkreg:
1559 icd->clk = NULL;
1560 platform_device_unregister(sasc->pdev);
1561 dev_err(ici->v4l2_dev.dev, "group probe failed: %d\n", ret);
1563 return ret;
1566 static void scan_async_host(struct soc_camera_host *ici)
1568 struct v4l2_async_subdev **asd;
1569 int j;
1571 for (j = 0, asd = ici->asd; ici->asd_sizes[j]; j++) {
1572 scan_async_group(ici, asd, ici->asd_sizes[j]);
1573 asd += ici->asd_sizes[j];
1576 #else
1577 #define soc_camera_i2c_init(icd, sdesc) (-ENODEV)
1578 #define soc_camera_i2c_free(icd) do {} while (0)
1579 #define scan_async_host(ici) do {} while (0)
1580 #endif
1582 /* Called during host-driver probe */
1583 static int soc_camera_probe(struct soc_camera_host *ici,
1584 struct soc_camera_device *icd)
1586 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
1587 struct soc_camera_host_desc *shd = &sdesc->host_desc;
1588 struct device *control = NULL;
1589 int ret;
1591 dev_info(icd->pdev, "Probing %s\n", dev_name(icd->pdev));
1594 * Currently the subdev with the largest number of controls (13) is
1595 * ov6550. So let's pick 16 as a hint for the control handler. Note
1596 * that this is a hint only: too large and you waste some memory, too
1597 * small and there is a (very) small performance hit when looking up
1598 * controls in the internal hash.
1600 ret = v4l2_ctrl_handler_init(&icd->ctrl_handler, 16);
1601 if (ret < 0)
1602 return ret;
1604 /* Must have icd->vdev before registering the device */
1605 ret = video_dev_create(icd);
1606 if (ret < 0)
1607 goto evdc;
1610 * ..._video_start() will create a device node, video_register_device()
1611 * itself is protected against concurrent open() calls, but we also have
1612 * to protect our data also during client probing.
1615 /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
1616 if (shd->board_info) {
1617 ret = soc_camera_i2c_init(icd, sdesc);
1618 if (ret < 0 && ret != -EPROBE_DEFER)
1619 goto eadd;
1620 } else if (!shd->add_device || !shd->del_device) {
1621 ret = -EINVAL;
1622 goto eadd;
1623 } else {
1624 mutex_lock(&ici->clk_lock);
1625 ret = ici->ops->clock_start(ici);
1626 mutex_unlock(&ici->clk_lock);
1627 if (ret < 0)
1628 goto eadd;
1630 if (shd->module_name)
1631 ret = request_module(shd->module_name);
1633 ret = shd->add_device(icd);
1634 if (ret < 0)
1635 goto eadddev;
1638 * FIXME: this is racy, have to use driver-binding notification,
1639 * when it is available
1641 control = to_soc_camera_control(icd);
1642 if (!control || !control->driver || !dev_get_drvdata(control) ||
1643 !try_module_get(control->driver->owner)) {
1644 shd->del_device(icd);
1645 ret = -ENODEV;
1646 goto enodrv;
1650 mutex_lock(&ici->host_lock);
1651 ret = soc_camera_probe_finish(icd);
1652 mutex_unlock(&ici->host_lock);
1653 if (ret < 0)
1654 goto efinish;
1656 return 0;
1658 efinish:
1659 if (shd->board_info) {
1660 soc_camera_i2c_free(icd);
1661 } else {
1662 shd->del_device(icd);
1663 module_put(control->driver->owner);
1664 enodrv:
1665 eadddev:
1666 mutex_lock(&ici->clk_lock);
1667 ici->ops->clock_stop(ici);
1668 mutex_unlock(&ici->clk_lock);
1670 eadd:
1671 video_device_release(icd->vdev);
1672 icd->vdev = NULL;
1673 if (icd->vdev) {
1674 video_device_release(icd->vdev);
1675 icd->vdev = NULL;
1677 evdc:
1678 v4l2_ctrl_handler_free(&icd->ctrl_handler);
1679 return ret;
1683 * This is called on device_unregister, which only means we have to disconnect
1684 * from the host, but not remove ourselves from the device list. With
1685 * asynchronous client probing this can also be called without
1686 * soc_camera_probe_finish() having run. Careful with clean up.
1688 static int soc_camera_remove(struct soc_camera_device *icd)
1690 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
1691 struct video_device *vdev = icd->vdev;
1693 v4l2_ctrl_handler_free(&icd->ctrl_handler);
1694 if (vdev) {
1695 video_unregister_device(vdev);
1696 icd->vdev = NULL;
1699 if (sdesc->host_desc.board_info) {
1700 soc_camera_i2c_free(icd);
1701 } else {
1702 struct device *dev = to_soc_camera_control(icd);
1703 struct device_driver *drv = dev ? dev->driver : NULL;
1704 if (drv) {
1705 sdesc->host_desc.del_device(icd);
1706 module_put(drv->owner);
1710 if (icd->num_user_formats)
1711 soc_camera_free_user_formats(icd);
1713 if (icd->clk) {
1714 /* For the synchronous case */
1715 v4l2_clk_unregister(icd->clk);
1716 icd->clk = NULL;
1719 if (icd->sasc)
1720 platform_device_unregister(icd->sasc->pdev);
1722 return 0;
1725 static int default_cropcap(struct soc_camera_device *icd,
1726 struct v4l2_cropcap *a)
1728 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1729 return v4l2_subdev_call(sd, video, cropcap, a);
1732 static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1734 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1735 return v4l2_subdev_call(sd, video, g_crop, a);
1738 static int default_s_crop(struct soc_camera_device *icd, const struct v4l2_crop *a)
1740 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1741 return v4l2_subdev_call(sd, video, s_crop, a);
1744 static int default_g_parm(struct soc_camera_device *icd,
1745 struct v4l2_streamparm *parm)
1747 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1748 return v4l2_subdev_call(sd, video, g_parm, parm);
1751 static int default_s_parm(struct soc_camera_device *icd,
1752 struct v4l2_streamparm *parm)
1754 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1755 return v4l2_subdev_call(sd, video, s_parm, parm);
1758 static int default_enum_framesizes(struct soc_camera_device *icd,
1759 struct v4l2_frmsizeenum *fsize)
1761 int ret;
1762 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1763 const struct soc_camera_format_xlate *xlate;
1764 __u32 pixfmt = fsize->pixel_format;
1765 struct v4l2_frmsizeenum fsize_mbus = *fsize;
1767 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1768 if (!xlate)
1769 return -EINVAL;
1770 /* map xlate-code to pixel_format, sensor only handle xlate-code*/
1771 fsize_mbus.pixel_format = xlate->code;
1773 ret = v4l2_subdev_call(sd, video, enum_framesizes, &fsize_mbus);
1774 if (ret < 0)
1775 return ret;
1777 *fsize = fsize_mbus;
1778 fsize->pixel_format = pixfmt;
1780 return 0;
1783 int soc_camera_host_register(struct soc_camera_host *ici)
1785 struct soc_camera_host *ix;
1786 int ret;
1788 if (!ici || !ici->ops ||
1789 !ici->ops->try_fmt ||
1790 !ici->ops->set_fmt ||
1791 !ici->ops->set_bus_param ||
1792 !ici->ops->querycap ||
1793 ((!ici->ops->init_videobuf ||
1794 !ici->ops->reqbufs) &&
1795 !ici->ops->init_videobuf2) ||
1796 !ici->ops->clock_start ||
1797 !ici->ops->clock_stop ||
1798 !ici->ops->poll ||
1799 !ici->v4l2_dev.dev)
1800 return -EINVAL;
1802 if (!ici->ops->set_crop)
1803 ici->ops->set_crop = default_s_crop;
1804 if (!ici->ops->get_crop)
1805 ici->ops->get_crop = default_g_crop;
1806 if (!ici->ops->cropcap)
1807 ici->ops->cropcap = default_cropcap;
1808 if (!ici->ops->set_parm)
1809 ici->ops->set_parm = default_s_parm;
1810 if (!ici->ops->get_parm)
1811 ici->ops->get_parm = default_g_parm;
1812 if (!ici->ops->enum_framesizes)
1813 ici->ops->enum_framesizes = default_enum_framesizes;
1815 mutex_lock(&list_lock);
1816 list_for_each_entry(ix, &hosts, list) {
1817 if (ix->nr == ici->nr) {
1818 ret = -EBUSY;
1819 goto edevreg;
1823 ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
1824 if (ret < 0)
1825 goto edevreg;
1827 list_add_tail(&ici->list, &hosts);
1828 mutex_unlock(&list_lock);
1830 mutex_init(&ici->host_lock);
1831 mutex_init(&ici->clk_lock);
1833 if (ici->asd_sizes)
1835 * No OF, host with a list of subdevices. Don't try to mix
1836 * modes by initialising some groups statically and some
1837 * dynamically!
1839 scan_async_host(ici);
1840 else
1841 /* Legacy: static platform devices from board data */
1842 scan_add_host(ici);
1844 return 0;
1846 edevreg:
1847 mutex_unlock(&list_lock);
1848 return ret;
1850 EXPORT_SYMBOL(soc_camera_host_register);
1852 /* Unregister all clients! */
1853 void soc_camera_host_unregister(struct soc_camera_host *ici)
1855 struct soc_camera_device *icd, *tmp;
1856 struct soc_camera_async_client *sasc;
1857 LIST_HEAD(notifiers);
1859 mutex_lock(&list_lock);
1860 list_del(&ici->list);
1861 list_for_each_entry(icd, &devices, list)
1862 if (icd->iface == ici->nr && icd->sasc) {
1863 /* as long as we hold the device, sasc won't be freed */
1864 get_device(icd->pdev);
1865 list_add(&icd->sasc->list, &notifiers);
1867 mutex_unlock(&list_lock);
1869 list_for_each_entry(sasc, &notifiers, list) {
1870 /* Must call unlocked to avoid AB-BA dead-lock */
1871 v4l2_async_notifier_unregister(&sasc->notifier);
1872 put_device(&sasc->pdev->dev);
1875 mutex_lock(&list_lock);
1877 list_for_each_entry_safe(icd, tmp, &devices, list)
1878 if (icd->iface == ici->nr)
1879 soc_camera_remove(icd);
1881 mutex_unlock(&list_lock);
1883 v4l2_device_unregister(&ici->v4l2_dev);
1885 EXPORT_SYMBOL(soc_camera_host_unregister);
1887 /* Image capture device */
1888 static int soc_camera_device_register(struct soc_camera_device *icd)
1890 struct soc_camera_device *ix;
1891 int num = -1, i;
1893 mutex_lock(&list_lock);
1894 for (i = 0; i < 256 && num < 0; i++) {
1895 num = i;
1896 /* Check if this index is available on this interface */
1897 list_for_each_entry(ix, &devices, list) {
1898 if (ix->iface == icd->iface && ix->devnum == i) {
1899 num = -1;
1900 break;
1905 if (num < 0) {
1907 * ok, we have 256 cameras on this host...
1908 * man, stay reasonable...
1910 mutex_unlock(&list_lock);
1911 return -ENOMEM;
1914 icd->devnum = num;
1915 icd->use_count = 0;
1916 icd->host_priv = NULL;
1919 * Dynamically allocated devices set the bit earlier, but it doesn't hurt setting
1920 * it again
1922 i = to_platform_device(icd->pdev)->id;
1923 if (i < 0)
1924 /* One static (legacy) soc-camera platform device */
1925 i = 0;
1926 if (i >= MAP_MAX_NUM) {
1927 mutex_unlock(&list_lock);
1928 return -EBUSY;
1930 set_bit(i, device_map);
1931 list_add_tail(&icd->list, &devices);
1932 mutex_unlock(&list_lock);
1934 return 0;
1937 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
1938 .vidioc_querycap = soc_camera_querycap,
1939 .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
1940 .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
1941 .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
1942 .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
1943 .vidioc_enum_input = soc_camera_enum_input,
1944 .vidioc_g_input = soc_camera_g_input,
1945 .vidioc_s_input = soc_camera_s_input,
1946 .vidioc_s_std = soc_camera_s_std,
1947 .vidioc_g_std = soc_camera_g_std,
1948 .vidioc_enum_framesizes = soc_camera_enum_framesizes,
1949 .vidioc_reqbufs = soc_camera_reqbufs,
1950 .vidioc_querybuf = soc_camera_querybuf,
1951 .vidioc_qbuf = soc_camera_qbuf,
1952 .vidioc_dqbuf = soc_camera_dqbuf,
1953 .vidioc_create_bufs = soc_camera_create_bufs,
1954 .vidioc_prepare_buf = soc_camera_prepare_buf,
1955 .vidioc_streamon = soc_camera_streamon,
1956 .vidioc_streamoff = soc_camera_streamoff,
1957 .vidioc_cropcap = soc_camera_cropcap,
1958 .vidioc_g_crop = soc_camera_g_crop,
1959 .vidioc_s_crop = soc_camera_s_crop,
1960 .vidioc_g_selection = soc_camera_g_selection,
1961 .vidioc_s_selection = soc_camera_s_selection,
1962 .vidioc_g_parm = soc_camera_g_parm,
1963 .vidioc_s_parm = soc_camera_s_parm,
1966 static int video_dev_create(struct soc_camera_device *icd)
1968 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1969 struct video_device *vdev = video_device_alloc();
1971 if (!vdev)
1972 return -ENOMEM;
1974 strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
1976 vdev->v4l2_dev = &ici->v4l2_dev;
1977 vdev->fops = &soc_camera_fops;
1978 vdev->ioctl_ops = &soc_camera_ioctl_ops;
1979 vdev->release = video_device_release;
1980 vdev->ctrl_handler = &icd->ctrl_handler;
1981 vdev->lock = &ici->host_lock;
1983 icd->vdev = vdev;
1985 return 0;
1989 * Called from soc_camera_probe() above with .host_lock held
1991 static int soc_camera_video_start(struct soc_camera_device *icd)
1993 const struct device_type *type = icd->vdev->dev.type;
1994 int ret;
1996 if (!icd->parent)
1997 return -ENODEV;
1999 video_set_drvdata(icd->vdev, icd);
2000 ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
2001 if (ret < 0) {
2002 dev_err(icd->pdev, "video_register_device failed: %d\n", ret);
2003 return ret;
2006 /* Restore device type, possibly set by the subdevice driver */
2007 icd->vdev->dev.type = type;
2009 return 0;
2012 static int soc_camera_pdrv_probe(struct platform_device *pdev)
2014 struct soc_camera_desc *sdesc = pdev->dev.platform_data;
2015 struct soc_camera_subdev_desc *ssdd = &sdesc->subdev_desc;
2016 struct soc_camera_device *icd;
2017 int ret;
2019 if (!sdesc)
2020 return -EINVAL;
2022 icd = devm_kzalloc(&pdev->dev, sizeof(*icd), GFP_KERNEL);
2023 if (!icd)
2024 return -ENOMEM;
2027 * In the asynchronous case ssdd->num_regulators == 0 yet, so, the below
2028 * regulator allocation is a dummy. They are actually requested by the
2029 * subdevice driver, using soc_camera_power_init(). Also note, that in
2030 * that case regulators are attached to the I2C device and not to the
2031 * camera platform device.
2033 ret = devm_regulator_bulk_get(&pdev->dev, ssdd->sd_pdata.num_regulators,
2034 ssdd->sd_pdata.regulators);
2035 if (ret < 0)
2036 return ret;
2038 icd->iface = sdesc->host_desc.bus_id;
2039 icd->sdesc = sdesc;
2040 icd->pdev = &pdev->dev;
2041 platform_set_drvdata(pdev, icd);
2043 icd->user_width = DEFAULT_WIDTH;
2044 icd->user_height = DEFAULT_HEIGHT;
2046 return soc_camera_device_register(icd);
2050 * Only called on rmmod for each platform device, since they are not
2051 * hot-pluggable. Now we know, that all our users - hosts and devices have
2052 * been unloaded already
2054 static int soc_camera_pdrv_remove(struct platform_device *pdev)
2056 struct soc_camera_device *icd = platform_get_drvdata(pdev);
2057 int i;
2059 if (!icd)
2060 return -EINVAL;
2062 i = pdev->id;
2063 if (i < 0)
2064 i = 0;
2067 * In synchronous mode with static platform devices this is called in a
2068 * loop from drivers/base/dd.c::driver_detach(), no parallel execution,
2069 * no need to lock. In asynchronous case the caller -
2070 * soc_camera_host_unregister() - already holds the lock
2072 if (test_bit(i, device_map)) {
2073 clear_bit(i, device_map);
2074 list_del(&icd->list);
2077 return 0;
2080 static struct platform_driver __refdata soc_camera_pdrv = {
2081 .probe = soc_camera_pdrv_probe,
2082 .remove = soc_camera_pdrv_remove,
2083 .driver = {
2084 .name = "soc-camera-pdrv",
2085 .owner = THIS_MODULE,
2089 module_platform_driver(soc_camera_pdrv);
2091 MODULE_DESCRIPTION("Image capture bus driver");
2092 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
2093 MODULE_LICENSE("GPL");
2094 MODULE_ALIAS("platform:soc-camera-pdrv");