mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / media / platform / soc_camera / soc_camera.c
blob387a232d95a4b63d74a9e3d2ef8030d8cdfee08c
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 = clk ? v4l2_clk_enable(clk) : 0;
75 if (ret < 0) {
76 dev_err(dev, "Cannot enable clock: %d\n", ret);
77 return ret;
79 ret = regulator_bulk_enable(ssdd->num_regulators,
80 ssdd->regulators);
81 if (ret < 0) {
82 dev_err(dev, "Cannot enable regulators\n");
83 goto eregenable;
86 if (ssdd->power) {
87 ret = ssdd->power(dev, 1);
88 if (ret < 0) {
89 dev_err(dev,
90 "Platform failed to power-on the camera.\n");
91 goto epwron;
95 return 0;
97 epwron:
98 regulator_bulk_disable(ssdd->num_regulators,
99 ssdd->regulators);
100 eregenable:
101 if (clk)
102 v4l2_clk_disable(clk);
104 return ret;
106 EXPORT_SYMBOL(soc_camera_power_on);
108 int soc_camera_power_off(struct device *dev, struct soc_camera_subdev_desc *ssdd,
109 struct v4l2_clk *clk)
111 int ret = 0;
112 int err;
114 if (ssdd->power) {
115 err = ssdd->power(dev, 0);
116 if (err < 0) {
117 dev_err(dev,
118 "Platform failed to power-off the camera.\n");
119 ret = err;
123 err = regulator_bulk_disable(ssdd->num_regulators,
124 ssdd->regulators);
125 if (err < 0) {
126 dev_err(dev, "Cannot disable regulators\n");
127 ret = ret ? : err;
130 if (clk)
131 v4l2_clk_disable(clk);
133 return ret;
135 EXPORT_SYMBOL(soc_camera_power_off);
137 int soc_camera_power_init(struct device *dev, struct soc_camera_subdev_desc *ssdd)
139 /* Should not have any effect in synchronous case */
140 return devm_regulator_bulk_get(dev, ssdd->num_regulators,
141 ssdd->regulators);
143 EXPORT_SYMBOL(soc_camera_power_init);
145 static int __soc_camera_power_on(struct soc_camera_device *icd)
147 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
148 int ret;
150 ret = v4l2_subdev_call(sd, core, s_power, 1);
151 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
152 return ret;
154 return 0;
157 static int __soc_camera_power_off(struct soc_camera_device *icd)
159 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
160 int ret;
162 ret = v4l2_subdev_call(sd, core, s_power, 0);
163 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
164 return ret;
166 return 0;
169 const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
170 struct soc_camera_device *icd, unsigned int fourcc)
172 unsigned int i;
174 for (i = 0; i < icd->num_user_formats; i++)
175 if (icd->user_formats[i].host_fmt->fourcc == fourcc)
176 return icd->user_formats + i;
177 return NULL;
179 EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);
182 * soc_camera_apply_board_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
183 * @ssdd: camera platform parameters
184 * @cfg: media bus configuration
185 * @return: resulting flags
187 unsigned long soc_camera_apply_board_flags(struct soc_camera_subdev_desc *ssdd,
188 const struct v4l2_mbus_config *cfg)
190 unsigned long f, flags = cfg->flags;
192 /* If only one of the two polarities is supported, switch to the opposite */
193 if (ssdd->flags & SOCAM_SENSOR_INVERT_HSYNC) {
194 f = flags & (V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW);
195 if (f == V4L2_MBUS_HSYNC_ACTIVE_HIGH || f == V4L2_MBUS_HSYNC_ACTIVE_LOW)
196 flags ^= V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW;
199 if (ssdd->flags & SOCAM_SENSOR_INVERT_VSYNC) {
200 f = flags & (V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW);
201 if (f == V4L2_MBUS_VSYNC_ACTIVE_HIGH || f == V4L2_MBUS_VSYNC_ACTIVE_LOW)
202 flags ^= V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW;
205 if (ssdd->flags & SOCAM_SENSOR_INVERT_PCLK) {
206 f = flags & (V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING);
207 if (f == V4L2_MBUS_PCLK_SAMPLE_RISING || f == V4L2_MBUS_PCLK_SAMPLE_FALLING)
208 flags ^= V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING;
211 return flags;
213 EXPORT_SYMBOL(soc_camera_apply_board_flags);
215 #define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
216 ((x) >> 24) & 0xff
218 static int soc_camera_try_fmt(struct soc_camera_device *icd,
219 struct v4l2_format *f)
221 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
222 const struct soc_camera_format_xlate *xlate;
223 struct v4l2_pix_format *pix = &f->fmt.pix;
224 int ret;
226 dev_dbg(icd->pdev, "TRY_FMT(%c%c%c%c, %ux%u)\n",
227 pixfmtstr(pix->pixelformat), pix->width, pix->height);
229 if (pix->pixelformat != V4L2_PIX_FMT_JPEG &&
230 !(ici->capabilities & SOCAM_HOST_CAP_STRIDE)) {
231 pix->bytesperline = 0;
232 pix->sizeimage = 0;
235 ret = ici->ops->try_fmt(icd, f);
236 if (ret < 0)
237 return ret;
239 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
240 if (!xlate)
241 return -EINVAL;
243 ret = soc_mbus_bytes_per_line(pix->width, xlate->host_fmt);
244 if (ret < 0)
245 return ret;
247 pix->bytesperline = max_t(u32, pix->bytesperline, ret);
249 ret = soc_mbus_image_size(xlate->host_fmt, pix->bytesperline,
250 pix->height);
251 if (ret < 0)
252 return ret;
254 pix->sizeimage = max_t(u32, pix->sizeimage, ret);
256 return 0;
259 static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
260 struct v4l2_format *f)
262 struct soc_camera_device *icd = file->private_data;
264 WARN_ON(priv != file->private_data);
266 /* Only single-plane capture is supported so far */
267 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
268 return -EINVAL;
270 /* limit format to hardware capabilities */
271 return soc_camera_try_fmt(icd, f);
274 static int soc_camera_enum_input(struct file *file, void *priv,
275 struct v4l2_input *inp)
277 if (inp->index != 0)
278 return -EINVAL;
280 /* default is camera */
281 inp->type = V4L2_INPUT_TYPE_CAMERA;
282 strcpy(inp->name, "Camera");
284 return 0;
287 static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
289 *i = 0;
291 return 0;
294 static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
296 if (i > 0)
297 return -EINVAL;
299 return 0;
302 static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id a)
304 struct soc_camera_device *icd = file->private_data;
305 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
307 return v4l2_subdev_call(sd, core, s_std, a);
310 static int soc_camera_g_std(struct file *file, void *priv, v4l2_std_id *a)
312 struct soc_camera_device *icd = file->private_data;
313 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
315 return v4l2_subdev_call(sd, core, g_std, a);
318 static int soc_camera_enum_framesizes(struct file *file, void *fh,
319 struct v4l2_frmsizeenum *fsize)
321 struct soc_camera_device *icd = file->private_data;
322 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
324 return ici->ops->enum_framesizes(icd, fsize);
327 static int soc_camera_reqbufs(struct file *file, void *priv,
328 struct v4l2_requestbuffers *p)
330 int ret;
331 struct soc_camera_device *icd = file->private_data;
332 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
334 WARN_ON(priv != file->private_data);
336 if (icd->streamer && icd->streamer != file)
337 return -EBUSY;
339 if (ici->ops->init_videobuf) {
340 ret = videobuf_reqbufs(&icd->vb_vidq, p);
341 if (ret < 0)
342 return ret;
344 ret = ici->ops->reqbufs(icd, p);
345 } else {
346 ret = vb2_reqbufs(&icd->vb2_vidq, p);
349 if (!ret && !icd->streamer)
350 icd->streamer = file;
352 return ret;
355 static int soc_camera_querybuf(struct file *file, void *priv,
356 struct v4l2_buffer *p)
358 struct soc_camera_device *icd = file->private_data;
359 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
361 WARN_ON(priv != file->private_data);
363 if (ici->ops->init_videobuf)
364 return videobuf_querybuf(&icd->vb_vidq, p);
365 else
366 return vb2_querybuf(&icd->vb2_vidq, p);
369 static int soc_camera_qbuf(struct file *file, void *priv,
370 struct v4l2_buffer *p)
372 struct soc_camera_device *icd = file->private_data;
373 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
375 WARN_ON(priv != file->private_data);
377 if (icd->streamer != file)
378 return -EBUSY;
380 if (ici->ops->init_videobuf)
381 return videobuf_qbuf(&icd->vb_vidq, p);
382 else
383 return vb2_qbuf(&icd->vb2_vidq, p);
386 static int soc_camera_dqbuf(struct file *file, void *priv,
387 struct v4l2_buffer *p)
389 struct soc_camera_device *icd = file->private_data;
390 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
392 WARN_ON(priv != file->private_data);
394 if (icd->streamer != file)
395 return -EBUSY;
397 if (ici->ops->init_videobuf)
398 return videobuf_dqbuf(&icd->vb_vidq, p, file->f_flags & O_NONBLOCK);
399 else
400 return vb2_dqbuf(&icd->vb2_vidq, p, file->f_flags & O_NONBLOCK);
403 static int soc_camera_create_bufs(struct file *file, void *priv,
404 struct v4l2_create_buffers *create)
406 struct soc_camera_device *icd = file->private_data;
407 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
409 /* videobuf2 only */
410 if (ici->ops->init_videobuf)
411 return -EINVAL;
412 else
413 return vb2_create_bufs(&icd->vb2_vidq, create);
416 static int soc_camera_prepare_buf(struct file *file, void *priv,
417 struct v4l2_buffer *b)
419 struct soc_camera_device *icd = file->private_data;
420 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
422 /* videobuf2 only */
423 if (ici->ops->init_videobuf)
424 return -EINVAL;
425 else
426 return vb2_prepare_buf(&icd->vb2_vidq, b);
429 /* Always entered with .host_lock held */
430 static int soc_camera_init_user_formats(struct soc_camera_device *icd)
432 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
433 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
434 unsigned int i, fmts = 0, raw_fmts = 0;
435 int ret;
436 enum v4l2_mbus_pixelcode code;
438 while (!v4l2_subdev_call(sd, video, enum_mbus_fmt, raw_fmts, &code))
439 raw_fmts++;
441 if (!ici->ops->get_formats)
443 * Fallback mode - the host will have to serve all
444 * sensor-provided formats one-to-one to the user
446 fmts = raw_fmts;
447 else
449 * First pass - only count formats this host-sensor
450 * configuration can provide
452 for (i = 0; i < raw_fmts; i++) {
453 ret = ici->ops->get_formats(icd, i, NULL);
454 if (ret < 0)
455 return ret;
456 fmts += ret;
459 if (!fmts)
460 return -ENXIO;
462 icd->user_formats =
463 vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
464 if (!icd->user_formats)
465 return -ENOMEM;
467 dev_dbg(icd->pdev, "Found %d supported formats.\n", fmts);
469 /* Second pass - actually fill data formats */
470 fmts = 0;
471 for (i = 0; i < raw_fmts; i++)
472 if (!ici->ops->get_formats) {
473 v4l2_subdev_call(sd, video, enum_mbus_fmt, i, &code);
474 icd->user_formats[fmts].host_fmt =
475 soc_mbus_get_fmtdesc(code);
476 if (icd->user_formats[fmts].host_fmt)
477 icd->user_formats[fmts++].code = code;
478 } else {
479 ret = ici->ops->get_formats(icd, i,
480 &icd->user_formats[fmts]);
481 if (ret < 0)
482 goto egfmt;
483 fmts += ret;
486 icd->num_user_formats = fmts;
487 icd->current_fmt = &icd->user_formats[0];
489 return 0;
491 egfmt:
492 vfree(icd->user_formats);
493 return ret;
496 /* Always entered with .host_lock held */
497 static void soc_camera_free_user_formats(struct soc_camera_device *icd)
499 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
501 if (ici->ops->put_formats)
502 ici->ops->put_formats(icd);
503 icd->current_fmt = NULL;
504 icd->num_user_formats = 0;
505 vfree(icd->user_formats);
506 icd->user_formats = NULL;
509 /* Called with .vb_lock held, or from the first open(2), see comment there */
510 static int soc_camera_set_fmt(struct soc_camera_device *icd,
511 struct v4l2_format *f)
513 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
514 struct v4l2_pix_format *pix = &f->fmt.pix;
515 int ret;
517 dev_dbg(icd->pdev, "S_FMT(%c%c%c%c, %ux%u)\n",
518 pixfmtstr(pix->pixelformat), pix->width, pix->height);
520 /* We always call try_fmt() before set_fmt() or set_crop() */
521 ret = soc_camera_try_fmt(icd, f);
522 if (ret < 0)
523 return ret;
525 ret = ici->ops->set_fmt(icd, f);
526 if (ret < 0) {
527 return ret;
528 } else if (!icd->current_fmt ||
529 icd->current_fmt->host_fmt->fourcc != pix->pixelformat) {
530 dev_err(icd->pdev,
531 "Host driver hasn't set up current format correctly!\n");
532 return -EINVAL;
535 icd->user_width = pix->width;
536 icd->user_height = pix->height;
537 icd->bytesperline = pix->bytesperline;
538 icd->sizeimage = pix->sizeimage;
539 icd->colorspace = pix->colorspace;
540 icd->field = pix->field;
541 if (ici->ops->init_videobuf)
542 icd->vb_vidq.field = pix->field;
544 dev_dbg(icd->pdev, "set width: %d height: %d\n",
545 icd->user_width, icd->user_height);
547 /* set physical bus parameters */
548 return ici->ops->set_bus_param(icd);
551 static int soc_camera_add_device(struct soc_camera_device *icd)
553 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
554 int ret;
556 if (ici->icd)
557 return -EBUSY;
559 if (!icd->clk) {
560 mutex_lock(&ici->clk_lock);
561 ret = ici->ops->clock_start(ici);
562 mutex_unlock(&ici->clk_lock);
563 if (ret < 0)
564 return ret;
567 if (ici->ops->add) {
568 ret = ici->ops->add(icd);
569 if (ret < 0)
570 goto eadd;
573 ici->icd = icd;
575 return 0;
577 eadd:
578 if (!icd->clk) {
579 mutex_lock(&ici->clk_lock);
580 ici->ops->clock_stop(ici);
581 mutex_unlock(&ici->clk_lock);
583 return ret;
586 static void soc_camera_remove_device(struct soc_camera_device *icd)
588 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
590 if (WARN_ON(icd != ici->icd))
591 return;
593 if (ici->ops->remove)
594 ici->ops->remove(icd);
595 if (!icd->clk) {
596 mutex_lock(&ici->clk_lock);
597 ici->ops->clock_stop(ici);
598 mutex_unlock(&ici->clk_lock);
600 ici->icd = NULL;
603 static int soc_camera_open(struct file *file)
605 struct video_device *vdev = video_devdata(file);
606 struct soc_camera_device *icd;
607 struct soc_camera_host *ici;
608 int ret;
611 * Don't mess with the host during probe: wait until the loop in
612 * scan_add_host() completes. Also protect against a race with
613 * soc_camera_host_unregister().
615 if (mutex_lock_interruptible(&list_lock))
616 return -ERESTARTSYS;
618 if (!vdev || !video_is_registered(vdev)) {
619 mutex_unlock(&list_lock);
620 return -ENODEV;
623 icd = video_get_drvdata(vdev);
624 ici = to_soc_camera_host(icd->parent);
626 ret = try_module_get(ici->ops->owner) ? 0 : -ENODEV;
627 mutex_unlock(&list_lock);
629 if (ret < 0) {
630 dev_err(icd->pdev, "Couldn't lock capture bus driver.\n");
631 return ret;
634 if (!to_soc_camera_control(icd)) {
635 /* No device driver attached */
636 ret = -ENODEV;
637 goto econtrol;
640 if (mutex_lock_interruptible(&ici->host_lock)) {
641 ret = -ERESTARTSYS;
642 goto elockhost;
644 icd->use_count++;
646 /* Now we really have to activate the camera */
647 if (icd->use_count == 1) {
648 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
649 /* Restore parameters before the last close() per V4L2 API */
650 struct v4l2_format f = {
651 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
652 .fmt.pix = {
653 .width = icd->user_width,
654 .height = icd->user_height,
655 .field = icd->field,
656 .colorspace = icd->colorspace,
657 .pixelformat =
658 icd->current_fmt->host_fmt->fourcc,
662 /* The camera could have been already on, try to reset */
663 if (sdesc->subdev_desc.reset)
664 sdesc->subdev_desc.reset(icd->pdev);
666 ret = soc_camera_add_device(icd);
667 if (ret < 0) {
668 dev_err(icd->pdev, "Couldn't activate the camera: %d\n", ret);
669 goto eiciadd;
672 ret = __soc_camera_power_on(icd);
673 if (ret < 0)
674 goto epower;
676 pm_runtime_enable(&icd->vdev->dev);
677 ret = pm_runtime_resume(&icd->vdev->dev);
678 if (ret < 0 && ret != -ENOSYS)
679 goto eresume;
682 * Try to configure with default parameters. Notice: this is the
683 * very first open, so, we cannot race against other calls,
684 * apart from someone else calling open() simultaneously, but
685 * .host_lock is protecting us against it.
687 ret = soc_camera_set_fmt(icd, &f);
688 if (ret < 0)
689 goto esfmt;
691 if (ici->ops->init_videobuf) {
692 ici->ops->init_videobuf(&icd->vb_vidq, icd);
693 } else {
694 ret = ici->ops->init_videobuf2(&icd->vb2_vidq, icd);
695 if (ret < 0)
696 goto einitvb;
698 v4l2_ctrl_handler_setup(&icd->ctrl_handler);
700 mutex_unlock(&ici->host_lock);
702 file->private_data = icd;
703 dev_dbg(icd->pdev, "camera device open\n");
705 return 0;
708 * All errors are entered with the .host_lock held, first four also
709 * with use_count == 1
711 einitvb:
712 esfmt:
713 pm_runtime_disable(&icd->vdev->dev);
714 eresume:
715 __soc_camera_power_off(icd);
716 epower:
717 soc_camera_remove_device(icd);
718 eiciadd:
719 icd->use_count--;
720 mutex_unlock(&ici->host_lock);
721 elockhost:
722 econtrol:
723 module_put(ici->ops->owner);
725 return ret;
728 static int soc_camera_close(struct file *file)
730 struct soc_camera_device *icd = file->private_data;
731 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
733 mutex_lock(&ici->host_lock);
734 icd->use_count--;
735 if (!icd->use_count) {
736 pm_runtime_suspend(&icd->vdev->dev);
737 pm_runtime_disable(&icd->vdev->dev);
739 if (ici->ops->init_videobuf2)
740 vb2_queue_release(&icd->vb2_vidq);
741 __soc_camera_power_off(icd);
743 soc_camera_remove_device(icd);
746 if (icd->streamer == file)
747 icd->streamer = NULL;
748 mutex_unlock(&ici->host_lock);
750 module_put(ici->ops->owner);
752 dev_dbg(icd->pdev, "camera device close\n");
754 return 0;
757 static ssize_t soc_camera_read(struct file *file, char __user *buf,
758 size_t count, loff_t *ppos)
760 struct soc_camera_device *icd = file->private_data;
761 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
763 dev_dbg(icd->pdev, "read called, buf %p\n", buf);
765 if (ici->ops->init_videobuf2 && icd->vb2_vidq.io_modes & VB2_READ)
766 return vb2_read(&icd->vb2_vidq, buf, count, ppos,
767 file->f_flags & O_NONBLOCK);
769 dev_err(icd->pdev, "camera device read not implemented\n");
771 return -EINVAL;
774 static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
776 struct soc_camera_device *icd = file->private_data;
777 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
778 int err;
780 dev_dbg(icd->pdev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
782 if (icd->streamer != file)
783 return -EBUSY;
785 if (mutex_lock_interruptible(&ici->host_lock))
786 return -ERESTARTSYS;
787 if (ici->ops->init_videobuf)
788 err = videobuf_mmap_mapper(&icd->vb_vidq, vma);
789 else
790 err = vb2_mmap(&icd->vb2_vidq, vma);
791 mutex_unlock(&ici->host_lock);
793 dev_dbg(icd->pdev, "vma start=0x%08lx, size=%ld, ret=%d\n",
794 (unsigned long)vma->vm_start,
795 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
796 err);
798 return err;
801 static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
803 struct soc_camera_device *icd = file->private_data;
804 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
805 unsigned res = POLLERR;
807 if (icd->streamer != file)
808 return POLLERR;
810 mutex_lock(&ici->host_lock);
811 if (ici->ops->init_videobuf && list_empty(&icd->vb_vidq.stream))
812 dev_err(icd->pdev, "Trying to poll with no queued buffers!\n");
813 else
814 res = ici->ops->poll(file, pt);
815 mutex_unlock(&ici->host_lock);
816 return res;
819 void soc_camera_lock(struct vb2_queue *vq)
821 struct soc_camera_device *icd = vb2_get_drv_priv(vq);
822 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
823 mutex_lock(&ici->host_lock);
825 EXPORT_SYMBOL(soc_camera_lock);
827 void soc_camera_unlock(struct vb2_queue *vq)
829 struct soc_camera_device *icd = vb2_get_drv_priv(vq);
830 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
831 mutex_unlock(&ici->host_lock);
833 EXPORT_SYMBOL(soc_camera_unlock);
835 static struct v4l2_file_operations soc_camera_fops = {
836 .owner = THIS_MODULE,
837 .open = soc_camera_open,
838 .release = soc_camera_close,
839 .unlocked_ioctl = video_ioctl2,
840 .read = soc_camera_read,
841 .mmap = soc_camera_mmap,
842 .poll = soc_camera_poll,
845 static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
846 struct v4l2_format *f)
848 struct soc_camera_device *icd = file->private_data;
849 int ret;
851 WARN_ON(priv != file->private_data);
853 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
854 dev_warn(icd->pdev, "Wrong buf-type %d\n", f->type);
855 return -EINVAL;
858 if (icd->streamer && icd->streamer != file)
859 return -EBUSY;
861 if (is_streaming(to_soc_camera_host(icd->parent), icd)) {
862 dev_err(icd->pdev, "S_FMT denied: queue initialised\n");
863 return -EBUSY;
866 ret = soc_camera_set_fmt(icd, f);
868 if (!ret && !icd->streamer)
869 icd->streamer = file;
871 return ret;
874 static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
875 struct v4l2_fmtdesc *f)
877 struct soc_camera_device *icd = file->private_data;
878 const struct soc_mbus_pixelfmt *format;
880 WARN_ON(priv != file->private_data);
882 if (f->index >= icd->num_user_formats)
883 return -EINVAL;
885 format = icd->user_formats[f->index].host_fmt;
887 if (format->name)
888 strlcpy(f->description, format->name, sizeof(f->description));
889 f->pixelformat = format->fourcc;
890 return 0;
893 static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
894 struct v4l2_format *f)
896 struct soc_camera_device *icd = file->private_data;
897 struct v4l2_pix_format *pix = &f->fmt.pix;
899 WARN_ON(priv != file->private_data);
901 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
902 return -EINVAL;
904 pix->width = icd->user_width;
905 pix->height = icd->user_height;
906 pix->bytesperline = icd->bytesperline;
907 pix->sizeimage = icd->sizeimage;
908 pix->field = icd->field;
909 pix->pixelformat = icd->current_fmt->host_fmt->fourcc;
910 pix->colorspace = icd->colorspace;
911 dev_dbg(icd->pdev, "current_fmt->fourcc: 0x%08x\n",
912 icd->current_fmt->host_fmt->fourcc);
913 return 0;
916 static int soc_camera_querycap(struct file *file, void *priv,
917 struct v4l2_capability *cap)
919 struct soc_camera_device *icd = file->private_data;
920 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
922 WARN_ON(priv != file->private_data);
924 strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
925 return ici->ops->querycap(ici, cap);
928 static int soc_camera_streamon(struct file *file, void *priv,
929 enum v4l2_buf_type i)
931 struct soc_camera_device *icd = file->private_data;
932 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
933 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
934 int ret;
936 WARN_ON(priv != file->private_data);
938 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
939 return -EINVAL;
941 if (icd->streamer != file)
942 return -EBUSY;
944 /* This calls buf_queue from host driver's videobuf_queue_ops */
945 if (ici->ops->init_videobuf)
946 ret = videobuf_streamon(&icd->vb_vidq);
947 else
948 ret = vb2_streamon(&icd->vb2_vidq, i);
950 if (!ret)
951 v4l2_subdev_call(sd, video, s_stream, 1);
953 return ret;
956 static int soc_camera_streamoff(struct file *file, void *priv,
957 enum v4l2_buf_type i)
959 struct soc_camera_device *icd = file->private_data;
960 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
961 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
963 WARN_ON(priv != file->private_data);
965 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
966 return -EINVAL;
968 if (icd->streamer != file)
969 return -EBUSY;
972 * This calls buf_release from host driver's videobuf_queue_ops for all
973 * remaining buffers. When the last buffer is freed, stop capture
975 if (ici->ops->init_videobuf)
976 videobuf_streamoff(&icd->vb_vidq);
977 else
978 vb2_streamoff(&icd->vb2_vidq, i);
980 v4l2_subdev_call(sd, video, s_stream, 0);
982 return 0;
985 static int soc_camera_cropcap(struct file *file, void *fh,
986 struct v4l2_cropcap *a)
988 struct soc_camera_device *icd = file->private_data;
989 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
991 return ici->ops->cropcap(icd, a);
994 static int soc_camera_g_crop(struct file *file, void *fh,
995 struct v4l2_crop *a)
997 struct soc_camera_device *icd = file->private_data;
998 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
999 int ret;
1001 ret = ici->ops->get_crop(icd, a);
1003 return ret;
1007 * According to the V4L2 API, drivers shall not update the struct v4l2_crop
1008 * argument with the actual geometry, instead, the user shall use G_CROP to
1009 * retrieve it.
1011 static int soc_camera_s_crop(struct file *file, void *fh,
1012 const struct v4l2_crop *a)
1014 struct soc_camera_device *icd = file->private_data;
1015 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1016 const struct v4l2_rect *rect = &a->c;
1017 struct v4l2_crop current_crop;
1018 int ret;
1020 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1021 return -EINVAL;
1023 dev_dbg(icd->pdev, "S_CROP(%ux%u@%u:%u)\n",
1024 rect->width, rect->height, rect->left, rect->top);
1026 current_crop.type = a->type;
1028 /* If get_crop fails, we'll let host and / or client drivers decide */
1029 ret = ici->ops->get_crop(icd, &current_crop);
1031 /* Prohibit window size change with initialised buffers */
1032 if (ret < 0) {
1033 dev_err(icd->pdev,
1034 "S_CROP denied: getting current crop failed\n");
1035 } else if ((a->c.width == current_crop.c.width &&
1036 a->c.height == current_crop.c.height) ||
1037 !is_streaming(ici, icd)) {
1038 /* same size or not streaming - use .set_crop() */
1039 ret = ici->ops->set_crop(icd, a);
1040 } else if (ici->ops->set_livecrop) {
1041 ret = ici->ops->set_livecrop(icd, a);
1042 } else {
1043 dev_err(icd->pdev,
1044 "S_CROP denied: queue initialised and sizes differ\n");
1045 ret = -EBUSY;
1048 return ret;
1051 static int soc_camera_g_selection(struct file *file, void *fh,
1052 struct v4l2_selection *s)
1054 struct soc_camera_device *icd = file->private_data;
1055 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1057 /* With a wrong type no need to try to fall back to cropping */
1058 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1059 return -EINVAL;
1061 if (!ici->ops->get_selection)
1062 return -ENOTTY;
1064 return ici->ops->get_selection(icd, s);
1067 static int soc_camera_s_selection(struct file *file, void *fh,
1068 struct v4l2_selection *s)
1070 struct soc_camera_device *icd = file->private_data;
1071 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1072 int ret;
1074 /* In all these cases cropping emulation will not help */
1075 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1076 (s->target != V4L2_SEL_TGT_COMPOSE &&
1077 s->target != V4L2_SEL_TGT_CROP))
1078 return -EINVAL;
1080 if (s->target == V4L2_SEL_TGT_COMPOSE) {
1081 /* No output size change during a running capture! */
1082 if (is_streaming(ici, icd) &&
1083 (icd->user_width != s->r.width ||
1084 icd->user_height != s->r.height))
1085 return -EBUSY;
1088 * Only one user is allowed to change the output format, touch
1089 * buffers, start / stop streaming, poll for data
1091 if (icd->streamer && icd->streamer != file)
1092 return -EBUSY;
1095 if (!ici->ops->set_selection)
1096 return -ENOTTY;
1098 ret = ici->ops->set_selection(icd, s);
1099 if (!ret &&
1100 s->target == V4L2_SEL_TGT_COMPOSE) {
1101 icd->user_width = s->r.width;
1102 icd->user_height = s->r.height;
1103 if (!icd->streamer)
1104 icd->streamer = file;
1107 return ret;
1110 static int soc_camera_g_parm(struct file *file, void *fh,
1111 struct v4l2_streamparm *a)
1113 struct soc_camera_device *icd = file->private_data;
1114 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1116 if (ici->ops->get_parm)
1117 return ici->ops->get_parm(icd, a);
1119 return -ENOIOCTLCMD;
1122 static int soc_camera_s_parm(struct file *file, void *fh,
1123 struct v4l2_streamparm *a)
1125 struct soc_camera_device *icd = file->private_data;
1126 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1128 if (ici->ops->set_parm)
1129 return ici->ops->set_parm(icd, a);
1131 return -ENOIOCTLCMD;
1134 static int soc_camera_probe(struct soc_camera_host *ici,
1135 struct soc_camera_device *icd);
1137 /* So far this function cannot fail */
1138 static void scan_add_host(struct soc_camera_host *ici)
1140 struct soc_camera_device *icd;
1142 mutex_lock(&list_lock);
1144 list_for_each_entry(icd, &devices, list)
1145 if (icd->iface == ici->nr) {
1146 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
1147 struct soc_camera_subdev_desc *ssdd = &sdesc->subdev_desc;
1149 /* The camera could have been already on, try to reset */
1150 if (ssdd->reset)
1151 ssdd->reset(icd->pdev);
1153 icd->parent = ici->v4l2_dev.dev;
1155 /* Ignore errors */
1156 soc_camera_probe(ici, icd);
1159 mutex_unlock(&list_lock);
1163 * It is invalid to call v4l2_clk_enable() after a successful probing
1164 * asynchronously outside of V4L2 operations, i.e. with .host_lock not held.
1166 static int soc_camera_clk_enable(struct v4l2_clk *clk)
1168 struct soc_camera_device *icd = clk->priv;
1169 struct soc_camera_host *ici;
1170 int ret;
1172 if (!icd || !icd->parent)
1173 return -ENODEV;
1175 ici = to_soc_camera_host(icd->parent);
1177 if (!try_module_get(ici->ops->owner))
1178 return -ENODEV;
1181 * If a different client is currently being probed, the host will tell
1182 * you to go
1184 mutex_lock(&ici->clk_lock);
1185 ret = ici->ops->clock_start(ici);
1186 mutex_unlock(&ici->clk_lock);
1187 return ret;
1190 static void soc_camera_clk_disable(struct v4l2_clk *clk)
1192 struct soc_camera_device *icd = clk->priv;
1193 struct soc_camera_host *ici;
1195 if (!icd || !icd->parent)
1196 return;
1198 ici = to_soc_camera_host(icd->parent);
1200 mutex_lock(&ici->clk_lock);
1201 ici->ops->clock_stop(ici);
1202 mutex_unlock(&ici->clk_lock);
1204 module_put(ici->ops->owner);
1208 * Eventually, it would be more logical to make the respective host the clock
1209 * owner, but then we would have to copy this struct for each ici. Besides, it
1210 * would introduce the circular dependency problem, unless we port all client
1211 * drivers to release the clock, when not in use.
1213 static const struct v4l2_clk_ops soc_camera_clk_ops = {
1214 .owner = THIS_MODULE,
1215 .enable = soc_camera_clk_enable,
1216 .disable = soc_camera_clk_disable,
1219 static int soc_camera_dyn_pdev(struct soc_camera_desc *sdesc,
1220 struct soc_camera_async_client *sasc)
1222 struct platform_device *pdev;
1223 int ret, i;
1225 mutex_lock(&list_lock);
1226 i = find_first_zero_bit(device_map, MAP_MAX_NUM);
1227 if (i < MAP_MAX_NUM)
1228 set_bit(i, device_map);
1229 mutex_unlock(&list_lock);
1230 if (i >= MAP_MAX_NUM)
1231 return -ENOMEM;
1233 pdev = platform_device_alloc("soc-camera-pdrv", i);
1234 if (!pdev)
1235 return -ENOMEM;
1237 ret = platform_device_add_data(pdev, sdesc, sizeof(*sdesc));
1238 if (ret < 0) {
1239 platform_device_put(pdev);
1240 return ret;
1243 sasc->pdev = pdev;
1245 return 0;
1248 static struct soc_camera_device *soc_camera_add_pdev(struct soc_camera_async_client *sasc)
1250 struct platform_device *pdev = sasc->pdev;
1251 int ret;
1253 ret = platform_device_add(pdev);
1254 if (ret < 0 || !pdev->dev.driver)
1255 return NULL;
1257 return platform_get_drvdata(pdev);
1260 /* Locking: called with .host_lock held */
1261 static int soc_camera_probe_finish(struct soc_camera_device *icd)
1263 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1264 struct v4l2_mbus_framefmt mf;
1265 int ret;
1267 sd->grp_id = soc_camera_grp_id(icd);
1268 v4l2_set_subdev_hostdata(sd, icd);
1270 ret = v4l2_ctrl_add_handler(&icd->ctrl_handler, sd->ctrl_handler, NULL);
1271 if (ret < 0)
1272 return ret;
1274 ret = soc_camera_add_device(icd);
1275 if (ret < 0) {
1276 dev_err(icd->pdev, "Couldn't activate the camera: %d\n", ret);
1277 return ret;
1280 /* At this point client .probe() should have run already */
1281 ret = soc_camera_init_user_formats(icd);
1282 if (ret < 0)
1283 goto eusrfmt;
1285 icd->field = V4L2_FIELD_ANY;
1287 ret = soc_camera_video_start(icd);
1288 if (ret < 0)
1289 goto evidstart;
1291 /* Try to improve our guess of a reasonable window format */
1292 if (!v4l2_subdev_call(sd, video, g_mbus_fmt, &mf)) {
1293 icd->user_width = mf.width;
1294 icd->user_height = mf.height;
1295 icd->colorspace = mf.colorspace;
1296 icd->field = mf.field;
1298 soc_camera_remove_device(icd);
1300 return 0;
1302 evidstart:
1303 soc_camera_free_user_formats(icd);
1304 eusrfmt:
1305 soc_camera_remove_device(icd);
1307 return ret;
1310 #ifdef CONFIG_I2C_BOARDINFO
1311 static int soc_camera_i2c_init(struct soc_camera_device *icd,
1312 struct soc_camera_desc *sdesc)
1314 struct soc_camera_subdev_desc *ssdd;
1315 struct i2c_client *client;
1316 struct soc_camera_host *ici;
1317 struct soc_camera_host_desc *shd = &sdesc->host_desc;
1318 struct i2c_adapter *adap;
1319 struct v4l2_subdev *subdev;
1320 char clk_name[V4L2_SUBDEV_NAME_SIZE];
1321 int ret;
1323 /* First find out how we link the main client */
1324 if (icd->sasc) {
1325 /* Async non-OF probing handled by the subdevice list */
1326 return -EPROBE_DEFER;
1329 ici = to_soc_camera_host(icd->parent);
1330 adap = i2c_get_adapter(shd->i2c_adapter_id);
1331 if (!adap) {
1332 dev_err(icd->pdev, "Cannot get I2C adapter #%d. No driver?\n",
1333 shd->i2c_adapter_id);
1334 return -ENODEV;
1337 ssdd = kzalloc(sizeof(*ssdd), GFP_KERNEL);
1338 if (!ssdd) {
1339 ret = -ENOMEM;
1340 goto ealloc;
1343 memcpy(ssdd, &sdesc->subdev_desc, sizeof(*ssdd));
1345 * In synchronous case we request regulators ourselves in
1346 * soc_camera_pdrv_probe(), make sure the subdevice driver doesn't try
1347 * to allocate them again.
1349 ssdd->num_regulators = 0;
1350 ssdd->regulators = NULL;
1351 shd->board_info->platform_data = ssdd;
1353 snprintf(clk_name, sizeof(clk_name), "%d-%04x",
1354 shd->i2c_adapter_id, shd->board_info->addr);
1356 icd->clk = v4l2_clk_register(&soc_camera_clk_ops, clk_name, "mclk", icd);
1357 if (IS_ERR(icd->clk)) {
1358 ret = PTR_ERR(icd->clk);
1359 goto eclkreg;
1362 subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
1363 shd->board_info, NULL);
1364 if (!subdev) {
1365 ret = -ENODEV;
1366 goto ei2cnd;
1369 client = v4l2_get_subdevdata(subdev);
1371 /* Use to_i2c_client(dev) to recover the i2c client */
1372 icd->control = &client->dev;
1374 return 0;
1375 ei2cnd:
1376 v4l2_clk_unregister(icd->clk);
1377 icd->clk = NULL;
1378 eclkreg:
1379 kfree(ssdd);
1380 ealloc:
1381 i2c_put_adapter(adap);
1382 return ret;
1385 static void soc_camera_i2c_free(struct soc_camera_device *icd)
1387 struct i2c_client *client =
1388 to_i2c_client(to_soc_camera_control(icd));
1389 struct i2c_adapter *adap;
1390 struct soc_camera_subdev_desc *ssdd;
1392 icd->control = NULL;
1393 if (icd->sasc)
1394 return;
1396 adap = client->adapter;
1397 ssdd = client->dev.platform_data;
1398 v4l2_device_unregister_subdev(i2c_get_clientdata(client));
1399 i2c_unregister_device(client);
1400 i2c_put_adapter(adap);
1401 kfree(ssdd);
1402 v4l2_clk_unregister(icd->clk);
1403 icd->clk = NULL;
1407 * V4L2 asynchronous notifier callbacks. They are all called under a v4l2-async
1408 * internal global mutex, therefore cannot race against other asynchronous
1409 * events. Until notifier->complete() (soc_camera_async_complete()) is called,
1410 * the video device node is not registered and no V4L fops can occur. Unloading
1411 * of the host driver also calls a v4l2-async function, so also there we're
1412 * protected.
1414 static int soc_camera_async_bound(struct v4l2_async_notifier *notifier,
1415 struct v4l2_subdev *sd,
1416 struct v4l2_async_subdev *asd)
1418 struct soc_camera_async_client *sasc = container_of(notifier,
1419 struct soc_camera_async_client, notifier);
1420 struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev);
1422 if (asd == sasc->sensor && !WARN_ON(icd->control)) {
1423 struct i2c_client *client = v4l2_get_subdevdata(sd);
1426 * Only now we get subdevice-specific information like
1427 * regulators, flags, callbacks, etc.
1429 if (client) {
1430 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
1431 struct soc_camera_subdev_desc *ssdd =
1432 soc_camera_i2c_to_desc(client);
1433 if (ssdd) {
1434 memcpy(&sdesc->subdev_desc, ssdd,
1435 sizeof(sdesc->subdev_desc));
1436 if (ssdd->reset)
1437 ssdd->reset(icd->pdev);
1440 icd->control = &client->dev;
1444 return 0;
1447 static void soc_camera_async_unbind(struct v4l2_async_notifier *notifier,
1448 struct v4l2_subdev *sd,
1449 struct v4l2_async_subdev *asd)
1451 struct soc_camera_async_client *sasc = container_of(notifier,
1452 struct soc_camera_async_client, notifier);
1453 struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev);
1455 if (icd->clk) {
1456 v4l2_clk_unregister(icd->clk);
1457 icd->clk = NULL;
1461 static int soc_camera_async_complete(struct v4l2_async_notifier *notifier)
1463 struct soc_camera_async_client *sasc = container_of(notifier,
1464 struct soc_camera_async_client, notifier);
1465 struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev);
1467 if (to_soc_camera_control(icd)) {
1468 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1469 int ret;
1471 mutex_lock(&list_lock);
1472 ret = soc_camera_probe(ici, icd);
1473 mutex_unlock(&list_lock);
1474 if (ret < 0)
1475 return ret;
1478 return 0;
1481 static int scan_async_group(struct soc_camera_host *ici,
1482 struct v4l2_async_subdev **asd, unsigned int size)
1484 struct soc_camera_async_subdev *sasd;
1485 struct soc_camera_async_client *sasc;
1486 struct soc_camera_device *icd;
1487 struct soc_camera_desc sdesc = {.host_desc.bus_id = ici->nr,};
1488 char clk_name[V4L2_SUBDEV_NAME_SIZE];
1489 unsigned int i;
1490 int ret;
1492 /* First look for a sensor */
1493 for (i = 0; i < size; i++) {
1494 sasd = container_of(asd[i], struct soc_camera_async_subdev, asd);
1495 if (sasd->role == SOCAM_SUBDEV_DATA_SOURCE)
1496 break;
1499 if (i >= size || asd[i]->match_type != V4L2_ASYNC_MATCH_I2C) {
1500 /* All useless */
1501 dev_err(ici->v4l2_dev.dev, "No I2C data source found!\n");
1502 return -ENODEV;
1505 /* Or shall this be managed by the soc-camera device? */
1506 sasc = devm_kzalloc(ici->v4l2_dev.dev, sizeof(*sasc), GFP_KERNEL);
1507 if (!sasc)
1508 return -ENOMEM;
1510 /* HACK: just need a != NULL */
1511 sdesc.host_desc.board_info = ERR_PTR(-ENODATA);
1513 ret = soc_camera_dyn_pdev(&sdesc, sasc);
1514 if (ret < 0)
1515 return ret;
1517 sasc->sensor = &sasd->asd;
1519 icd = soc_camera_add_pdev(sasc);
1520 if (!icd) {
1521 platform_device_put(sasc->pdev);
1522 return -ENOMEM;
1525 sasc->notifier.subdevs = asd;
1526 sasc->notifier.num_subdevs = size;
1527 sasc->notifier.bound = soc_camera_async_bound;
1528 sasc->notifier.unbind = soc_camera_async_unbind;
1529 sasc->notifier.complete = soc_camera_async_complete;
1531 icd->sasc = sasc;
1532 icd->parent = ici->v4l2_dev.dev;
1534 snprintf(clk_name, sizeof(clk_name), "%d-%04x",
1535 sasd->asd.match.i2c.adapter_id, sasd->asd.match.i2c.address);
1537 icd->clk = v4l2_clk_register(&soc_camera_clk_ops, clk_name, "mclk", icd);
1538 if (IS_ERR(icd->clk)) {
1539 ret = PTR_ERR(icd->clk);
1540 goto eclkreg;
1543 ret = v4l2_async_notifier_register(&ici->v4l2_dev, &sasc->notifier);
1544 if (!ret)
1545 return 0;
1547 v4l2_clk_unregister(icd->clk);
1548 eclkreg:
1549 icd->clk = NULL;
1550 platform_device_unregister(sasc->pdev);
1551 dev_err(ici->v4l2_dev.dev, "group probe failed: %d\n", ret);
1553 return ret;
1556 static void scan_async_host(struct soc_camera_host *ici)
1558 struct v4l2_async_subdev **asd;
1559 int j;
1561 for (j = 0, asd = ici->asd; ici->asd_sizes[j]; j++) {
1562 scan_async_group(ici, asd, ici->asd_sizes[j]);
1563 asd += ici->asd_sizes[j];
1566 #else
1567 #define soc_camera_i2c_init(icd, sdesc) (-ENODEV)
1568 #define soc_camera_i2c_free(icd) do {} while (0)
1569 #define scan_async_host(ici) do {} while (0)
1570 #endif
1572 /* Called during host-driver probe */
1573 static int soc_camera_probe(struct soc_camera_host *ici,
1574 struct soc_camera_device *icd)
1576 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
1577 struct soc_camera_host_desc *shd = &sdesc->host_desc;
1578 struct device *control = NULL;
1579 int ret;
1581 dev_info(icd->pdev, "Probing %s\n", dev_name(icd->pdev));
1584 * Currently the subdev with the largest number of controls (13) is
1585 * ov6550. So let's pick 16 as a hint for the control handler. Note
1586 * that this is a hint only: too large and you waste some memory, too
1587 * small and there is a (very) small performance hit when looking up
1588 * controls in the internal hash.
1590 ret = v4l2_ctrl_handler_init(&icd->ctrl_handler, 16);
1591 if (ret < 0)
1592 return ret;
1594 /* Must have icd->vdev before registering the device */
1595 ret = video_dev_create(icd);
1596 if (ret < 0)
1597 goto evdc;
1600 * ..._video_start() will create a device node, video_register_device()
1601 * itself is protected against concurrent open() calls, but we also have
1602 * to protect our data also during client probing.
1605 /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
1606 if (shd->board_info) {
1607 ret = soc_camera_i2c_init(icd, sdesc);
1608 if (ret < 0 && ret != -EPROBE_DEFER)
1609 goto eadd;
1610 } else if (!shd->add_device || !shd->del_device) {
1611 ret = -EINVAL;
1612 goto eadd;
1613 } else {
1614 mutex_lock(&ici->clk_lock);
1615 ret = ici->ops->clock_start(ici);
1616 mutex_unlock(&ici->clk_lock);
1617 if (ret < 0)
1618 goto eadd;
1620 if (shd->module_name)
1621 ret = request_module(shd->module_name);
1623 ret = shd->add_device(icd);
1624 if (ret < 0)
1625 goto eadddev;
1628 * FIXME: this is racy, have to use driver-binding notification,
1629 * when it is available
1631 control = to_soc_camera_control(icd);
1632 if (!control || !control->driver || !dev_get_drvdata(control) ||
1633 !try_module_get(control->driver->owner)) {
1634 shd->del_device(icd);
1635 ret = -ENODEV;
1636 goto enodrv;
1640 mutex_lock(&ici->host_lock);
1641 ret = soc_camera_probe_finish(icd);
1642 mutex_unlock(&ici->host_lock);
1643 if (ret < 0)
1644 goto efinish;
1646 return 0;
1648 efinish:
1649 if (shd->board_info) {
1650 soc_camera_i2c_free(icd);
1651 } else {
1652 shd->del_device(icd);
1653 module_put(control->driver->owner);
1654 enodrv:
1655 eadddev:
1656 mutex_lock(&ici->clk_lock);
1657 ici->ops->clock_stop(ici);
1658 mutex_unlock(&ici->clk_lock);
1660 eadd:
1661 video_device_release(icd->vdev);
1662 icd->vdev = NULL;
1663 if (icd->vdev) {
1664 video_device_release(icd->vdev);
1665 icd->vdev = NULL;
1667 evdc:
1668 v4l2_ctrl_handler_free(&icd->ctrl_handler);
1669 return ret;
1673 * This is called on device_unregister, which only means we have to disconnect
1674 * from the host, but not remove ourselves from the device list. With
1675 * asynchronous client probing this can also be called without
1676 * soc_camera_probe_finish() having run. Careful with clean up.
1678 static int soc_camera_remove(struct soc_camera_device *icd)
1680 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
1681 struct video_device *vdev = icd->vdev;
1683 v4l2_ctrl_handler_free(&icd->ctrl_handler);
1684 if (vdev) {
1685 video_unregister_device(vdev);
1686 icd->vdev = NULL;
1689 if (sdesc->host_desc.board_info) {
1690 soc_camera_i2c_free(icd);
1691 } else {
1692 struct device *dev = to_soc_camera_control(icd);
1693 struct device_driver *drv = dev ? dev->driver : NULL;
1694 if (drv) {
1695 sdesc->host_desc.del_device(icd);
1696 module_put(drv->owner);
1700 if (icd->num_user_formats)
1701 soc_camera_free_user_formats(icd);
1703 if (icd->clk) {
1704 /* For the synchronous case */
1705 v4l2_clk_unregister(icd->clk);
1706 icd->clk = NULL;
1709 if (icd->sasc)
1710 platform_device_unregister(icd->sasc->pdev);
1712 return 0;
1715 static int default_cropcap(struct soc_camera_device *icd,
1716 struct v4l2_cropcap *a)
1718 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1719 return v4l2_subdev_call(sd, video, cropcap, a);
1722 static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1724 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1725 return v4l2_subdev_call(sd, video, g_crop, a);
1728 static int default_s_crop(struct soc_camera_device *icd, const struct v4l2_crop *a)
1730 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1731 return v4l2_subdev_call(sd, video, s_crop, a);
1734 static int default_g_parm(struct soc_camera_device *icd,
1735 struct v4l2_streamparm *parm)
1737 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1738 return v4l2_subdev_call(sd, video, g_parm, parm);
1741 static int default_s_parm(struct soc_camera_device *icd,
1742 struct v4l2_streamparm *parm)
1744 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1745 return v4l2_subdev_call(sd, video, s_parm, parm);
1748 static int default_enum_framesizes(struct soc_camera_device *icd,
1749 struct v4l2_frmsizeenum *fsize)
1751 int ret;
1752 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1753 const struct soc_camera_format_xlate *xlate;
1754 __u32 pixfmt = fsize->pixel_format;
1755 struct v4l2_frmsizeenum fsize_mbus = *fsize;
1757 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1758 if (!xlate)
1759 return -EINVAL;
1760 /* map xlate-code to pixel_format, sensor only handle xlate-code*/
1761 fsize_mbus.pixel_format = xlate->code;
1763 ret = v4l2_subdev_call(sd, video, enum_framesizes, &fsize_mbus);
1764 if (ret < 0)
1765 return ret;
1767 *fsize = fsize_mbus;
1768 fsize->pixel_format = pixfmt;
1770 return 0;
1773 int soc_camera_host_register(struct soc_camera_host *ici)
1775 struct soc_camera_host *ix;
1776 int ret;
1778 if (!ici || !ici->ops ||
1779 !ici->ops->try_fmt ||
1780 !ici->ops->set_fmt ||
1781 !ici->ops->set_bus_param ||
1782 !ici->ops->querycap ||
1783 ((!ici->ops->init_videobuf ||
1784 !ici->ops->reqbufs) &&
1785 !ici->ops->init_videobuf2) ||
1786 !ici->ops->clock_start ||
1787 !ici->ops->clock_stop ||
1788 !ici->ops->poll ||
1789 !ici->v4l2_dev.dev)
1790 return -EINVAL;
1792 if (!ici->ops->set_crop)
1793 ici->ops->set_crop = default_s_crop;
1794 if (!ici->ops->get_crop)
1795 ici->ops->get_crop = default_g_crop;
1796 if (!ici->ops->cropcap)
1797 ici->ops->cropcap = default_cropcap;
1798 if (!ici->ops->set_parm)
1799 ici->ops->set_parm = default_s_parm;
1800 if (!ici->ops->get_parm)
1801 ici->ops->get_parm = default_g_parm;
1802 if (!ici->ops->enum_framesizes)
1803 ici->ops->enum_framesizes = default_enum_framesizes;
1805 mutex_lock(&list_lock);
1806 list_for_each_entry(ix, &hosts, list) {
1807 if (ix->nr == ici->nr) {
1808 ret = -EBUSY;
1809 goto edevreg;
1813 ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
1814 if (ret < 0)
1815 goto edevreg;
1817 list_add_tail(&ici->list, &hosts);
1818 mutex_unlock(&list_lock);
1820 mutex_init(&ici->host_lock);
1821 mutex_init(&ici->clk_lock);
1823 if (ici->asd_sizes)
1825 * No OF, host with a list of subdevices. Don't try to mix
1826 * modes by initialising some groups statically and some
1827 * dynamically!
1829 scan_async_host(ici);
1830 else
1831 /* Legacy: static platform devices from board data */
1832 scan_add_host(ici);
1834 return 0;
1836 edevreg:
1837 mutex_unlock(&list_lock);
1838 return ret;
1840 EXPORT_SYMBOL(soc_camera_host_register);
1842 /* Unregister all clients! */
1843 void soc_camera_host_unregister(struct soc_camera_host *ici)
1845 struct soc_camera_device *icd, *tmp;
1846 struct soc_camera_async_client *sasc;
1847 LIST_HEAD(notifiers);
1849 mutex_lock(&list_lock);
1850 list_del(&ici->list);
1851 list_for_each_entry(icd, &devices, list)
1852 if (icd->iface == ici->nr && icd->sasc) {
1853 /* as long as we hold the device, sasc won't be freed */
1854 get_device(icd->pdev);
1855 list_add(&icd->sasc->list, &notifiers);
1857 mutex_unlock(&list_lock);
1859 list_for_each_entry(sasc, &notifiers, list) {
1860 /* Must call unlocked to avoid AB-BA dead-lock */
1861 v4l2_async_notifier_unregister(&sasc->notifier);
1862 put_device(&sasc->pdev->dev);
1865 mutex_lock(&list_lock);
1867 list_for_each_entry_safe(icd, tmp, &devices, list)
1868 if (icd->iface == ici->nr)
1869 soc_camera_remove(icd);
1871 mutex_unlock(&list_lock);
1873 v4l2_device_unregister(&ici->v4l2_dev);
1875 EXPORT_SYMBOL(soc_camera_host_unregister);
1877 /* Image capture device */
1878 static int soc_camera_device_register(struct soc_camera_device *icd)
1880 struct soc_camera_device *ix;
1881 int num = -1, i;
1883 mutex_lock(&list_lock);
1884 for (i = 0; i < 256 && num < 0; i++) {
1885 num = i;
1886 /* Check if this index is available on this interface */
1887 list_for_each_entry(ix, &devices, list) {
1888 if (ix->iface == icd->iface && ix->devnum == i) {
1889 num = -1;
1890 break;
1895 if (num < 0) {
1897 * ok, we have 256 cameras on this host...
1898 * man, stay reasonable...
1900 mutex_unlock(&list_lock);
1901 return -ENOMEM;
1904 icd->devnum = num;
1905 icd->use_count = 0;
1906 icd->host_priv = NULL;
1909 * Dynamically allocated devices set the bit earlier, but it doesn't hurt setting
1910 * it again
1912 i = to_platform_device(icd->pdev)->id;
1913 if (i < 0)
1914 /* One static (legacy) soc-camera platform device */
1915 i = 0;
1916 if (i >= MAP_MAX_NUM) {
1917 mutex_unlock(&list_lock);
1918 return -EBUSY;
1920 set_bit(i, device_map);
1921 list_add_tail(&icd->list, &devices);
1922 mutex_unlock(&list_lock);
1924 return 0;
1927 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
1928 .vidioc_querycap = soc_camera_querycap,
1929 .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
1930 .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
1931 .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
1932 .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
1933 .vidioc_enum_input = soc_camera_enum_input,
1934 .vidioc_g_input = soc_camera_g_input,
1935 .vidioc_s_input = soc_camera_s_input,
1936 .vidioc_s_std = soc_camera_s_std,
1937 .vidioc_g_std = soc_camera_g_std,
1938 .vidioc_enum_framesizes = soc_camera_enum_framesizes,
1939 .vidioc_reqbufs = soc_camera_reqbufs,
1940 .vidioc_querybuf = soc_camera_querybuf,
1941 .vidioc_qbuf = soc_camera_qbuf,
1942 .vidioc_dqbuf = soc_camera_dqbuf,
1943 .vidioc_create_bufs = soc_camera_create_bufs,
1944 .vidioc_prepare_buf = soc_camera_prepare_buf,
1945 .vidioc_streamon = soc_camera_streamon,
1946 .vidioc_streamoff = soc_camera_streamoff,
1947 .vidioc_cropcap = soc_camera_cropcap,
1948 .vidioc_g_crop = soc_camera_g_crop,
1949 .vidioc_s_crop = soc_camera_s_crop,
1950 .vidioc_g_selection = soc_camera_g_selection,
1951 .vidioc_s_selection = soc_camera_s_selection,
1952 .vidioc_g_parm = soc_camera_g_parm,
1953 .vidioc_s_parm = soc_camera_s_parm,
1956 static int video_dev_create(struct soc_camera_device *icd)
1958 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1959 struct video_device *vdev = video_device_alloc();
1961 if (!vdev)
1962 return -ENOMEM;
1964 strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
1966 vdev->v4l2_dev = &ici->v4l2_dev;
1967 vdev->fops = &soc_camera_fops;
1968 vdev->ioctl_ops = &soc_camera_ioctl_ops;
1969 vdev->release = video_device_release;
1970 vdev->ctrl_handler = &icd->ctrl_handler;
1971 vdev->lock = &ici->host_lock;
1973 icd->vdev = vdev;
1975 return 0;
1979 * Called from soc_camera_probe() above with .host_lock held
1981 static int soc_camera_video_start(struct soc_camera_device *icd)
1983 const struct device_type *type = icd->vdev->dev.type;
1984 int ret;
1986 if (!icd->parent)
1987 return -ENODEV;
1989 video_set_drvdata(icd->vdev, icd);
1990 ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
1991 if (ret < 0) {
1992 dev_err(icd->pdev, "video_register_device failed: %d\n", ret);
1993 return ret;
1996 /* Restore device type, possibly set by the subdevice driver */
1997 icd->vdev->dev.type = type;
1999 return 0;
2002 static int soc_camera_pdrv_probe(struct platform_device *pdev)
2004 struct soc_camera_desc *sdesc = pdev->dev.platform_data;
2005 struct soc_camera_subdev_desc *ssdd = &sdesc->subdev_desc;
2006 struct soc_camera_device *icd;
2007 int ret;
2009 if (!sdesc)
2010 return -EINVAL;
2012 icd = devm_kzalloc(&pdev->dev, sizeof(*icd), GFP_KERNEL);
2013 if (!icd)
2014 return -ENOMEM;
2017 * In the asynchronous case ssdd->num_regulators == 0 yet, so, the below
2018 * regulator allocation is a dummy. They are actually requested by the
2019 * subdevice driver, using soc_camera_power_init(). Also note, that in
2020 * that case regulators are attached to the I2C device and not to the
2021 * camera platform device.
2023 ret = devm_regulator_bulk_get(&pdev->dev, ssdd->num_regulators,
2024 ssdd->regulators);
2025 if (ret < 0)
2026 return ret;
2028 icd->iface = sdesc->host_desc.bus_id;
2029 icd->sdesc = sdesc;
2030 icd->pdev = &pdev->dev;
2031 platform_set_drvdata(pdev, icd);
2033 icd->user_width = DEFAULT_WIDTH;
2034 icd->user_height = DEFAULT_HEIGHT;
2036 return soc_camera_device_register(icd);
2040 * Only called on rmmod for each platform device, since they are not
2041 * hot-pluggable. Now we know, that all our users - hosts and devices have
2042 * been unloaded already
2044 static int soc_camera_pdrv_remove(struct platform_device *pdev)
2046 struct soc_camera_device *icd = platform_get_drvdata(pdev);
2047 int i;
2049 if (!icd)
2050 return -EINVAL;
2052 i = pdev->id;
2053 if (i < 0)
2054 i = 0;
2057 * In synchronous mode with static platform devices this is called in a
2058 * loop from drivers/base/dd.c::driver_detach(), no parallel execution,
2059 * no need to lock. In asynchronous case the caller -
2060 * soc_camera_host_unregister() - already holds the lock
2062 if (test_bit(i, device_map)) {
2063 clear_bit(i, device_map);
2064 list_del(&icd->list);
2067 return 0;
2070 static struct platform_driver __refdata soc_camera_pdrv = {
2071 .probe = soc_camera_pdrv_probe,
2072 .remove = soc_camera_pdrv_remove,
2073 .driver = {
2074 .name = "soc-camera-pdrv",
2075 .owner = THIS_MODULE,
2079 module_platform_driver(soc_camera_pdrv);
2081 MODULE_DESCRIPTION("Image capture bus driver");
2082 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
2083 MODULE_LICENSE("GPL");
2084 MODULE_ALIAS("platform:soc-camera-pdrv");