V4L/DVB (12506): soc-camera: convert to platform device
[linux-2.6/mini2440.git] / drivers / media / video / soc_camera.c
blob20ef5c773fae53839c89cd7be7ba40486cbe4598
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/mutex.h>
25 #include <linux/module.h>
26 #include <linux/platform_device.h>
27 #include <linux/vmalloc.h>
29 #include <media/soc_camera.h>
30 #include <media/v4l2-common.h>
31 #include <media/v4l2-ioctl.h>
32 #include <media/v4l2-dev.h>
33 #include <media/videobuf-core.h>
35 /* Default to VGA resolution */
36 #define DEFAULT_WIDTH 640
37 #define DEFAULT_HEIGHT 480
39 static LIST_HEAD(hosts);
40 static LIST_HEAD(devices);
41 static DEFINE_MUTEX(list_lock); /* Protects the list of hosts */
43 const struct soc_camera_data_format *soc_camera_format_by_fourcc(
44 struct soc_camera_device *icd, unsigned int fourcc)
46 unsigned int i;
48 for (i = 0; i < icd->num_formats; i++)
49 if (icd->formats[i].fourcc == fourcc)
50 return icd->formats + i;
51 return NULL;
53 EXPORT_SYMBOL(soc_camera_format_by_fourcc);
55 const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
56 struct soc_camera_device *icd, unsigned int fourcc)
58 unsigned int i;
60 for (i = 0; i < icd->num_user_formats; i++)
61 if (icd->user_formats[i].host_fmt->fourcc == fourcc)
62 return icd->user_formats + i;
63 return NULL;
65 EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);
67 /**
68 * soc_camera_apply_sensor_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
69 * @icl: camera platform parameters
70 * @flags: flags to be inverted according to platform configuration
71 * @return: resulting flags
73 unsigned long soc_camera_apply_sensor_flags(struct soc_camera_link *icl,
74 unsigned long flags)
76 unsigned long f;
78 /* If only one of the two polarities is supported, switch to the opposite */
79 if (icl->flags & SOCAM_SENSOR_INVERT_HSYNC) {
80 f = flags & (SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW);
81 if (f == SOCAM_HSYNC_ACTIVE_HIGH || f == SOCAM_HSYNC_ACTIVE_LOW)
82 flags ^= SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW;
85 if (icl->flags & SOCAM_SENSOR_INVERT_VSYNC) {
86 f = flags & (SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW);
87 if (f == SOCAM_VSYNC_ACTIVE_HIGH || f == SOCAM_VSYNC_ACTIVE_LOW)
88 flags ^= SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW;
91 if (icl->flags & SOCAM_SENSOR_INVERT_PCLK) {
92 f = flags & (SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING);
93 if (f == SOCAM_PCLK_SAMPLE_RISING || f == SOCAM_PCLK_SAMPLE_FALLING)
94 flags ^= SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING;
97 return flags;
99 EXPORT_SYMBOL(soc_camera_apply_sensor_flags);
101 static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
102 struct v4l2_format *f)
104 struct soc_camera_file *icf = file->private_data;
105 struct soc_camera_device *icd = icf->icd;
106 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
108 WARN_ON(priv != file->private_data);
110 /* limit format to hardware capabilities */
111 return ici->ops->try_fmt(icd, f);
114 static int soc_camera_enum_input(struct file *file, void *priv,
115 struct v4l2_input *inp)
117 struct soc_camera_file *icf = file->private_data;
118 struct soc_camera_device *icd = icf->icd;
119 int ret = 0;
121 if (inp->index != 0)
122 return -EINVAL;
124 if (icd->ops->enum_input)
125 ret = icd->ops->enum_input(icd, inp);
126 else {
127 /* default is camera */
128 inp->type = V4L2_INPUT_TYPE_CAMERA;
129 inp->std = V4L2_STD_UNKNOWN;
130 strcpy(inp->name, "Camera");
133 return ret;
136 static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
138 *i = 0;
140 return 0;
143 static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
145 if (i > 0)
146 return -EINVAL;
148 return 0;
151 static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
153 struct soc_camera_file *icf = file->private_data;
154 struct soc_camera_device *icd = icf->icd;
155 int ret = 0;
157 if (icd->ops->set_std)
158 ret = icd->ops->set_std(icd, a);
160 return ret;
163 static int soc_camera_reqbufs(struct file *file, void *priv,
164 struct v4l2_requestbuffers *p)
166 int ret;
167 struct soc_camera_file *icf = file->private_data;
168 struct soc_camera_device *icd = icf->icd;
169 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
171 WARN_ON(priv != file->private_data);
173 dev_dbg(&icd->dev, "%s: %d\n", __func__, p->memory);
175 ret = videobuf_reqbufs(&icf->vb_vidq, p);
176 if (ret < 0)
177 return ret;
179 return ici->ops->reqbufs(icf, p);
182 static int soc_camera_querybuf(struct file *file, void *priv,
183 struct v4l2_buffer *p)
185 struct soc_camera_file *icf = file->private_data;
187 WARN_ON(priv != file->private_data);
189 return videobuf_querybuf(&icf->vb_vidq, p);
192 static int soc_camera_qbuf(struct file *file, void *priv,
193 struct v4l2_buffer *p)
195 struct soc_camera_file *icf = file->private_data;
197 WARN_ON(priv != file->private_data);
199 return videobuf_qbuf(&icf->vb_vidq, p);
202 static int soc_camera_dqbuf(struct file *file, void *priv,
203 struct v4l2_buffer *p)
205 struct soc_camera_file *icf = file->private_data;
207 WARN_ON(priv != file->private_data);
209 return videobuf_dqbuf(&icf->vb_vidq, p, file->f_flags & O_NONBLOCK);
212 /* Always entered with .video_lock held */
213 static int soc_camera_init_user_formats(struct soc_camera_device *icd)
215 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
216 int i, fmts = 0;
218 if (!ici->ops->get_formats)
220 * Fallback mode - the host will have to serve all
221 * sensor-provided formats one-to-one to the user
223 fmts = icd->num_formats;
224 else
226 * First pass - only count formats this host-sensor
227 * configuration can provide
229 for (i = 0; i < icd->num_formats; i++)
230 fmts += ici->ops->get_formats(icd, i, NULL);
232 if (!fmts)
233 return -ENXIO;
235 icd->user_formats =
236 vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
237 if (!icd->user_formats)
238 return -ENOMEM;
240 icd->num_user_formats = fmts;
242 dev_dbg(&icd->dev, "Found %d supported formats.\n", fmts);
244 /* Second pass - actually fill data formats */
245 fmts = 0;
246 for (i = 0; i < icd->num_formats; i++)
247 if (!ici->ops->get_formats) {
248 icd->user_formats[i].host_fmt = icd->formats + i;
249 icd->user_formats[i].cam_fmt = icd->formats + i;
250 icd->user_formats[i].buswidth = icd->formats[i].depth;
251 } else {
252 fmts += ici->ops->get_formats(icd, i,
253 &icd->user_formats[fmts]);
256 icd->current_fmt = icd->user_formats[0].host_fmt;
258 return 0;
261 /* Always entered with .video_lock held */
262 static void soc_camera_free_user_formats(struct soc_camera_device *icd)
264 icd->current_fmt = NULL;
265 vfree(icd->user_formats);
266 icd->user_formats = NULL;
269 /* Called with .vb_lock held */
270 static int soc_camera_set_fmt(struct soc_camera_file *icf,
271 struct v4l2_format *f)
273 struct soc_camera_device *icd = icf->icd;
274 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
275 struct v4l2_pix_format *pix = &f->fmt.pix;
276 int ret;
278 /* We always call try_fmt() before set_fmt() or set_crop() */
279 ret = ici->ops->try_fmt(icd, f);
280 if (ret < 0)
281 return ret;
283 ret = ici->ops->set_fmt(icd, f);
284 if (ret < 0) {
285 return ret;
286 } else if (!icd->current_fmt ||
287 icd->current_fmt->fourcc != pix->pixelformat) {
288 dev_err(ici->dev,
289 "Host driver hasn't set up current format correctly!\n");
290 return -EINVAL;
293 icd->width = pix->width;
294 icd->height = pix->height;
295 icf->vb_vidq.field =
296 icd->field = pix->field;
298 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
299 dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n",
300 f->type);
302 dev_dbg(&icd->dev, "set width: %d height: %d\n",
303 icd->width, icd->height);
305 /* set physical bus parameters */
306 return ici->ops->set_bus_param(icd, pix->pixelformat);
309 static int soc_camera_open(struct file *file)
311 struct video_device *vdev;
312 struct soc_camera_device *icd;
313 struct soc_camera_host *ici;
314 struct soc_camera_file *icf;
315 int ret;
318 * It is safe to dereference these pointers now as long as a user has
319 * the video device open - we are protected by the held cdev reference.
322 vdev = video_devdata(file);
323 icd = container_of(vdev->parent, struct soc_camera_device, dev);
325 if (!icd->ops)
326 /* No device driver attached */
327 return -ENODEV;
329 ici = to_soc_camera_host(icd->dev.parent);
331 icf = vmalloc(sizeof(*icf));
332 if (!icf)
333 return -ENOMEM;
335 if (!try_module_get(icd->ops->owner)) {
336 dev_err(&icd->dev, "Couldn't lock sensor driver.\n");
337 ret = -EINVAL;
338 goto emgd;
341 if (!try_module_get(ici->ops->owner)) {
342 dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
343 ret = -EINVAL;
344 goto emgi;
347 /* Protect against icd->ops->remove() until we module_get() both drivers. */
348 mutex_lock(&icd->video_lock);
350 icf->icd = icd;
351 icd->use_count++;
353 /* Now we really have to activate the camera */
354 if (icd->use_count == 1) {
355 /* Restore parameters before the last close() per V4L2 API */
356 struct v4l2_format f = {
357 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
358 .fmt.pix = {
359 .width = icd->width,
360 .height = icd->height,
361 .field = icd->field,
365 ret = soc_camera_init_user_formats(icd);
366 if (ret < 0)
367 goto eiufmt;
369 dev_dbg(&icd->dev, "Using fmt %x\n", icd->current_fmt->fourcc);
371 f.fmt.pix.pixelformat = icd->current_fmt->fourcc;
372 f.fmt.pix.colorspace = icd->current_fmt->colorspace;
374 ret = ici->ops->add(icd);
375 if (ret < 0) {
376 dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
377 goto eiciadd;
380 /* Try to configure with default parameters */
381 ret = soc_camera_set_fmt(icf, &f);
382 if (ret < 0)
383 goto esfmt;
386 mutex_unlock(&icd->video_lock);
388 file->private_data = icf;
389 dev_dbg(&icd->dev, "camera device open\n");
391 ici->ops->init_videobuf(&icf->vb_vidq, icd);
393 return 0;
396 * First three errors are entered with the .video_lock held
397 * and use_count == 1
399 esfmt:
400 ici->ops->remove(icd);
401 eiciadd:
402 soc_camera_free_user_formats(icd);
403 eiufmt:
404 icd->use_count--;
405 mutex_unlock(&icd->video_lock);
406 module_put(ici->ops->owner);
407 emgi:
408 module_put(icd->ops->owner);
409 emgd:
410 vfree(icf);
411 return ret;
414 static int soc_camera_close(struct file *file)
416 struct soc_camera_file *icf = file->private_data;
417 struct soc_camera_device *icd = icf->icd;
418 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
419 struct video_device *vdev = icd->vdev;
421 mutex_lock(&icd->video_lock);
422 icd->use_count--;
423 if (!icd->use_count) {
424 ici->ops->remove(icd);
425 soc_camera_free_user_formats(icd);
428 mutex_unlock(&icd->video_lock);
430 module_put(icd->ops->owner);
431 module_put(ici->ops->owner);
433 vfree(icf);
435 dev_dbg(vdev->parent, "camera device close\n");
437 return 0;
440 static ssize_t soc_camera_read(struct file *file, char __user *buf,
441 size_t count, loff_t *ppos)
443 struct soc_camera_file *icf = file->private_data;
444 struct soc_camera_device *icd = icf->icd;
445 struct video_device *vdev = icd->vdev;
446 int err = -EINVAL;
448 dev_err(vdev->parent, "camera device read not implemented\n");
450 return err;
453 static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
455 struct soc_camera_file *icf = file->private_data;
456 struct soc_camera_device *icd = icf->icd;
457 int err;
459 dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
461 err = videobuf_mmap_mapper(&icf->vb_vidq, vma);
463 dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
464 (unsigned long)vma->vm_start,
465 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
466 err);
468 return err;
471 static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
473 struct soc_camera_file *icf = file->private_data;
474 struct soc_camera_device *icd = icf->icd;
475 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
477 if (list_empty(&icf->vb_vidq.stream)) {
478 dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
479 return POLLERR;
482 return ici->ops->poll(file, pt);
485 static struct v4l2_file_operations soc_camera_fops = {
486 .owner = THIS_MODULE,
487 .open = soc_camera_open,
488 .release = soc_camera_close,
489 .ioctl = video_ioctl2,
490 .read = soc_camera_read,
491 .mmap = soc_camera_mmap,
492 .poll = soc_camera_poll,
495 static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
496 struct v4l2_format *f)
498 struct soc_camera_file *icf = file->private_data;
499 struct soc_camera_device *icd = icf->icd;
500 int ret;
502 WARN_ON(priv != file->private_data);
504 mutex_lock(&icf->vb_vidq.vb_lock);
506 if (videobuf_queue_is_busy(&icf->vb_vidq)) {
507 dev_err(&icd->dev, "S_FMT denied: queue busy\n");
508 ret = -EBUSY;
509 goto unlock;
512 ret = soc_camera_set_fmt(icf, f);
514 unlock:
515 mutex_unlock(&icf->vb_vidq.vb_lock);
517 return ret;
520 static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
521 struct v4l2_fmtdesc *f)
523 struct soc_camera_file *icf = file->private_data;
524 struct soc_camera_device *icd = icf->icd;
525 const struct soc_camera_data_format *format;
527 WARN_ON(priv != file->private_data);
529 if (f->index >= icd->num_user_formats)
530 return -EINVAL;
532 format = icd->user_formats[f->index].host_fmt;
534 strlcpy(f->description, format->name, sizeof(f->description));
535 f->pixelformat = format->fourcc;
536 return 0;
539 static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
540 struct v4l2_format *f)
542 struct soc_camera_file *icf = file->private_data;
543 struct soc_camera_device *icd = icf->icd;
544 struct v4l2_pix_format *pix = &f->fmt.pix;
546 WARN_ON(priv != file->private_data);
548 pix->width = icd->width;
549 pix->height = icd->height;
550 pix->field = icf->vb_vidq.field;
551 pix->pixelformat = icd->current_fmt->fourcc;
552 pix->bytesperline = pix->width *
553 DIV_ROUND_UP(icd->current_fmt->depth, 8);
554 pix->sizeimage = pix->height * pix->bytesperline;
555 dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
556 icd->current_fmt->fourcc);
557 return 0;
560 static int soc_camera_querycap(struct file *file, void *priv,
561 struct v4l2_capability *cap)
563 struct soc_camera_file *icf = file->private_data;
564 struct soc_camera_device *icd = icf->icd;
565 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
567 WARN_ON(priv != file->private_data);
569 strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
570 return ici->ops->querycap(ici, cap);
573 static int soc_camera_streamon(struct file *file, void *priv,
574 enum v4l2_buf_type i)
576 struct soc_camera_file *icf = file->private_data;
577 struct soc_camera_device *icd = icf->icd;
578 int ret;
580 WARN_ON(priv != file->private_data);
582 dev_dbg(&icd->dev, "%s\n", __func__);
584 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
585 return -EINVAL;
587 mutex_lock(&icd->video_lock);
589 icd->ops->start_capture(icd);
591 /* This calls buf_queue from host driver's videobuf_queue_ops */
592 ret = videobuf_streamon(&icf->vb_vidq);
594 mutex_unlock(&icd->video_lock);
596 return ret;
599 static int soc_camera_streamoff(struct file *file, void *priv,
600 enum v4l2_buf_type i)
602 struct soc_camera_file *icf = file->private_data;
603 struct soc_camera_device *icd = icf->icd;
605 WARN_ON(priv != file->private_data);
607 dev_dbg(&icd->dev, "%s\n", __func__);
609 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
610 return -EINVAL;
612 mutex_lock(&icd->video_lock);
614 /* This calls buf_release from host driver's videobuf_queue_ops for all
615 * remaining buffers. When the last buffer is freed, stop capture */
616 videobuf_streamoff(&icf->vb_vidq);
618 icd->ops->stop_capture(icd);
620 mutex_unlock(&icd->video_lock);
622 return 0;
625 static int soc_camera_queryctrl(struct file *file, void *priv,
626 struct v4l2_queryctrl *qc)
628 struct soc_camera_file *icf = file->private_data;
629 struct soc_camera_device *icd = icf->icd;
630 int i;
632 WARN_ON(priv != file->private_data);
634 if (!qc->id)
635 return -EINVAL;
637 for (i = 0; i < icd->ops->num_controls; i++)
638 if (qc->id == icd->ops->controls[i].id) {
639 memcpy(qc, &(icd->ops->controls[i]),
640 sizeof(*qc));
641 return 0;
644 return -EINVAL;
647 static int soc_camera_g_ctrl(struct file *file, void *priv,
648 struct v4l2_control *ctrl)
650 struct soc_camera_file *icf = file->private_data;
651 struct soc_camera_device *icd = icf->icd;
653 WARN_ON(priv != file->private_data);
655 switch (ctrl->id) {
656 case V4L2_CID_GAIN:
657 if (icd->gain == (unsigned short)~0)
658 return -EINVAL;
659 ctrl->value = icd->gain;
660 return 0;
661 case V4L2_CID_EXPOSURE:
662 if (icd->exposure == (unsigned short)~0)
663 return -EINVAL;
664 ctrl->value = icd->exposure;
665 return 0;
668 if (icd->ops->get_control)
669 return icd->ops->get_control(icd, ctrl);
670 return -EINVAL;
673 static int soc_camera_s_ctrl(struct file *file, void *priv,
674 struct v4l2_control *ctrl)
676 struct soc_camera_file *icf = file->private_data;
677 struct soc_camera_device *icd = icf->icd;
679 WARN_ON(priv != file->private_data);
681 if (icd->ops->set_control)
682 return icd->ops->set_control(icd, ctrl);
683 return -EINVAL;
686 static int soc_camera_cropcap(struct file *file, void *fh,
687 struct v4l2_cropcap *a)
689 struct soc_camera_file *icf = file->private_data;
690 struct soc_camera_device *icd = icf->icd;
692 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
693 a->bounds.left = icd->x_min;
694 a->bounds.top = icd->y_min;
695 a->bounds.width = icd->width_max;
696 a->bounds.height = icd->height_max;
697 a->defrect.left = icd->x_min;
698 a->defrect.top = icd->y_min;
699 a->defrect.width = DEFAULT_WIDTH;
700 a->defrect.height = DEFAULT_HEIGHT;
701 a->pixelaspect.numerator = 1;
702 a->pixelaspect.denominator = 1;
704 return 0;
707 static int soc_camera_g_crop(struct file *file, void *fh,
708 struct v4l2_crop *a)
710 struct soc_camera_file *icf = file->private_data;
711 struct soc_camera_device *icd = icf->icd;
713 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
714 a->c.left = icd->x_current;
715 a->c.top = icd->y_current;
716 a->c.width = icd->width;
717 a->c.height = icd->height;
719 return 0;
722 static int soc_camera_s_crop(struct file *file, void *fh,
723 struct v4l2_crop *a)
725 struct soc_camera_file *icf = file->private_data;
726 struct soc_camera_device *icd = icf->icd;
727 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
728 int ret;
730 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
731 return -EINVAL;
733 /* Cropping is allowed during a running capture, guard consistency */
734 mutex_lock(&icf->vb_vidq.vb_lock);
736 ret = ici->ops->set_crop(icd, &a->c);
737 if (!ret) {
738 icd->width = a->c.width;
739 icd->height = a->c.height;
740 icd->x_current = a->c.left;
741 icd->y_current = a->c.top;
744 mutex_unlock(&icf->vb_vidq.vb_lock);
746 return ret;
749 static int soc_camera_g_chip_ident(struct file *file, void *fh,
750 struct v4l2_dbg_chip_ident *id)
752 struct soc_camera_file *icf = file->private_data;
753 struct soc_camera_device *icd = icf->icd;
755 if (!icd->ops->get_chip_id)
756 return -EINVAL;
758 return icd->ops->get_chip_id(icd, id);
761 #ifdef CONFIG_VIDEO_ADV_DEBUG
762 static int soc_camera_g_register(struct file *file, void *fh,
763 struct v4l2_dbg_register *reg)
765 struct soc_camera_file *icf = file->private_data;
766 struct soc_camera_device *icd = icf->icd;
768 if (!icd->ops->get_register)
769 return -EINVAL;
771 return icd->ops->get_register(icd, reg);
774 static int soc_camera_s_register(struct file *file, void *fh,
775 struct v4l2_dbg_register *reg)
777 struct soc_camera_file *icf = file->private_data;
778 struct soc_camera_device *icd = icf->icd;
780 if (!icd->ops->set_register)
781 return -EINVAL;
783 return icd->ops->set_register(icd, reg);
785 #endif
787 /* So far this function cannot fail */
788 static void scan_add_host(struct soc_camera_host *ici)
790 struct soc_camera_device *icd;
792 mutex_lock(&list_lock);
794 list_for_each_entry(icd, &devices, list) {
795 if (icd->iface == ici->nr) {
796 int ret;
797 icd->dev.parent = ici->dev;
798 dev_set_name(&icd->dev, "%u-%u", icd->iface,
799 icd->devnum);
800 ret = device_register(&icd->dev);
801 if (ret < 0) {
802 icd->dev.parent = NULL;
803 dev_err(&icd->dev,
804 "Cannot register device: %d\n", ret);
809 mutex_unlock(&list_lock);
812 #ifdef CONFIG_I2C_BOARDINFO
813 static int soc_camera_init_i2c(struct soc_camera_device *icd,
814 struct soc_camera_link *icl)
816 struct i2c_client *client;
817 struct i2c_adapter *adap = i2c_get_adapter(icl->i2c_adapter_id);
818 int ret;
820 if (!adap) {
821 ret = -ENODEV;
822 dev_err(&icd->dev, "Cannot get I2C adapter #%d. No driver?\n",
823 icl->i2c_adapter_id);
824 goto ei2cga;
827 icl->board_info->platform_data = icd;
829 client = i2c_new_device(adap, icl->board_info);
830 if (!client) {
831 ret = -ENOMEM;
832 goto ei2cnd;
836 * We set icd drvdata at two locations - here and in
837 * soc_camera_video_start(). Depending on the module loading /
838 * initialisation order one of these locations will be entered first
840 /* Use to_i2c_client(dev) to recover the i2c client */
841 dev_set_drvdata(&icd->dev, &client->dev);
843 return 0;
844 ei2cnd:
845 i2c_put_adapter(adap);
846 ei2cga:
847 return ret;
850 static void soc_camera_free_i2c(struct soc_camera_device *icd)
852 struct i2c_client *client =
853 to_i2c_client(to_soc_camera_control(icd));
854 dev_set_drvdata(&icd->dev, NULL);
855 i2c_unregister_device(client);
856 i2c_put_adapter(client->adapter);
858 #else
859 #define soc_camera_init_i2c(icd, icl) (-ENODEV)
860 #define soc_camera_free_i2c(icd) do {} while (0)
861 #endif
863 static int video_dev_create(struct soc_camera_device *icd);
864 /* Called during host-driver probe */
865 static int soc_camera_probe(struct device *dev)
867 struct soc_camera_device *icd = to_soc_camera_dev(dev);
868 struct soc_camera_link *icl = to_soc_camera_link(icd);
869 int ret;
871 dev_info(dev, "Probing %s\n", dev_name(dev));
873 ret = video_dev_create(icd);
874 if (ret < 0)
875 goto evdc;
877 /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
878 if (icl->board_info) {
879 ret = soc_camera_init_i2c(icd, icl);
880 if (ret < 0)
881 goto eadddev;
882 } else if (!icl->add_device || !icl->del_device) {
883 ret = -EINVAL;
884 goto eadddev;
885 } else {
886 ret = icl->add_device(icl, &icd->dev);
887 if (ret < 0)
888 goto eadddev;
891 ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, icd->vdev->minor);
892 if (ret < 0) {
893 dev_err(&icd->dev, "video_register_device failed: %d\n", ret);
894 goto evidregd;
897 /* Do we have to sysfs_remove_link() before device_unregister()? */
898 if (to_soc_camera_control(icd) &&
899 sysfs_create_link(&icd->dev.kobj, &to_soc_camera_control(icd)->kobj,
900 "control"))
901 dev_warn(&icd->dev, "Failed creating the control symlink\n");
904 return 0;
906 evidregd:
907 if (icl->board_info)
908 soc_camera_free_i2c(icd);
909 else
910 icl->del_device(icl);
911 eadddev:
912 video_device_release(icd->vdev);
913 evdc:
914 return ret;
917 /* This is called on device_unregister, which only means we have to disconnect
918 * from the host, but not remove ourselves from the device list */
919 static int soc_camera_remove(struct device *dev)
921 struct soc_camera_device *icd = to_soc_camera_dev(dev);
922 struct soc_camera_link *icl = to_soc_camera_link(icd);
923 struct video_device *vdev = icd->vdev;
925 BUG_ON(!dev->parent);
927 if (vdev) {
928 mutex_lock(&icd->video_lock);
929 video_unregister_device(vdev);
930 icd->vdev = NULL;
931 mutex_unlock(&icd->video_lock);
934 if (icl->board_info)
935 soc_camera_free_i2c(icd);
936 else
937 icl->del_device(icl);
939 return 0;
942 static int soc_camera_suspend(struct device *dev, pm_message_t state)
944 struct soc_camera_device *icd = to_soc_camera_dev(dev);
945 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
946 int ret = 0;
948 if (ici->ops->suspend)
949 ret = ici->ops->suspend(icd, state);
951 return ret;
954 static int soc_camera_resume(struct device *dev)
956 struct soc_camera_device *icd = to_soc_camera_dev(dev);
957 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
958 int ret = 0;
960 if (ici->ops->resume)
961 ret = ici->ops->resume(icd);
963 return ret;
966 static struct bus_type soc_camera_bus_type = {
967 .name = "soc-camera",
968 .probe = soc_camera_probe,
969 .remove = soc_camera_remove,
970 .suspend = soc_camera_suspend,
971 .resume = soc_camera_resume,
974 static struct device_driver ic_drv = {
975 .name = "camera",
976 .bus = &soc_camera_bus_type,
977 .owner = THIS_MODULE,
980 static void dummy_release(struct device *dev)
984 int soc_camera_host_register(struct soc_camera_host *ici)
986 struct soc_camera_host *ix;
988 if (!ici || !ici->ops ||
989 !ici->ops->try_fmt ||
990 !ici->ops->set_fmt ||
991 !ici->ops->set_crop ||
992 !ici->ops->set_bus_param ||
993 !ici->ops->querycap ||
994 !ici->ops->init_videobuf ||
995 !ici->ops->reqbufs ||
996 !ici->ops->add ||
997 !ici->ops->remove ||
998 !ici->ops->poll ||
999 !ici->dev)
1000 return -EINVAL;
1002 mutex_lock(&list_lock);
1003 list_for_each_entry(ix, &hosts, list) {
1004 if (ix->nr == ici->nr) {
1005 mutex_unlock(&list_lock);
1006 return -EBUSY;
1010 dev_set_drvdata(ici->dev, ici);
1012 list_add_tail(&ici->list, &hosts);
1013 mutex_unlock(&list_lock);
1015 scan_add_host(ici);
1017 return 0;
1019 EXPORT_SYMBOL(soc_camera_host_register);
1021 /* Unregister all clients! */
1022 void soc_camera_host_unregister(struct soc_camera_host *ici)
1024 struct soc_camera_device *icd;
1026 mutex_lock(&list_lock);
1028 list_del(&ici->list);
1030 list_for_each_entry(icd, &devices, list) {
1031 if (icd->dev.parent == ici->dev) {
1032 /* The bus->remove will be called */
1033 device_unregister(&icd->dev);
1034 /* Not before device_unregister(), .remove
1035 * needs parent to call ici->ops->remove() */
1036 icd->dev.parent = NULL;
1038 /* If the host module is loaded again, device_register()
1039 * would complain "already initialised" */
1040 memset(&icd->dev.kobj, 0, sizeof(icd->dev.kobj));
1044 mutex_unlock(&list_lock);
1046 dev_set_drvdata(ici->dev, NULL);
1048 EXPORT_SYMBOL(soc_camera_host_unregister);
1050 /* Image capture device */
1051 static int soc_camera_device_register(struct soc_camera_device *icd)
1053 struct soc_camera_device *ix;
1054 int num = -1, i;
1056 for (i = 0; i < 256 && num < 0; i++) {
1057 num = i;
1058 /* Check if this index is available on this interface */
1059 list_for_each_entry(ix, &devices, list) {
1060 if (ix->iface == icd->iface && ix->devnum == i) {
1061 num = -1;
1062 break;
1067 if (num < 0)
1068 /* ok, we have 256 cameras on this host...
1069 * man, stay reasonable... */
1070 return -ENOMEM;
1072 icd->devnum = num;
1073 icd->dev.bus = &soc_camera_bus_type;
1075 icd->dev.release = dummy_release;
1076 icd->use_count = 0;
1077 icd->host_priv = NULL;
1078 mutex_init(&icd->video_lock);
1080 list_add_tail(&icd->list, &devices);
1082 return 0;
1085 static void soc_camera_device_unregister(struct soc_camera_device *icd)
1087 list_del(&icd->list);
1090 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
1091 .vidioc_querycap = soc_camera_querycap,
1092 .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
1093 .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
1094 .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
1095 .vidioc_enum_input = soc_camera_enum_input,
1096 .vidioc_g_input = soc_camera_g_input,
1097 .vidioc_s_input = soc_camera_s_input,
1098 .vidioc_s_std = soc_camera_s_std,
1099 .vidioc_reqbufs = soc_camera_reqbufs,
1100 .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
1101 .vidioc_querybuf = soc_camera_querybuf,
1102 .vidioc_qbuf = soc_camera_qbuf,
1103 .vidioc_dqbuf = soc_camera_dqbuf,
1104 .vidioc_streamon = soc_camera_streamon,
1105 .vidioc_streamoff = soc_camera_streamoff,
1106 .vidioc_queryctrl = soc_camera_queryctrl,
1107 .vidioc_g_ctrl = soc_camera_g_ctrl,
1108 .vidioc_s_ctrl = soc_camera_s_ctrl,
1109 .vidioc_cropcap = soc_camera_cropcap,
1110 .vidioc_g_crop = soc_camera_g_crop,
1111 .vidioc_s_crop = soc_camera_s_crop,
1112 .vidioc_g_chip_ident = soc_camera_g_chip_ident,
1113 #ifdef CONFIG_VIDEO_ADV_DEBUG
1114 .vidioc_g_register = soc_camera_g_register,
1115 .vidioc_s_register = soc_camera_s_register,
1116 #endif
1119 static int video_dev_create(struct soc_camera_device *icd)
1121 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1122 struct video_device *vdev = video_device_alloc();
1124 if (!vdev)
1125 return -ENOMEM;
1126 dev_dbg(ici->dev, "Allocated video_device %p\n", vdev);
1128 strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
1130 vdev->parent = &icd->dev;
1131 vdev->current_norm = V4L2_STD_UNKNOWN;
1132 vdev->fops = &soc_camera_fops;
1133 vdev->ioctl_ops = &soc_camera_ioctl_ops;
1134 vdev->release = video_device_release;
1135 vdev->minor = -1;
1136 vdev->tvnorms = V4L2_STD_UNKNOWN;
1138 icd->vdev = vdev;
1140 return 0;
1144 * Usually called from the struct soc_camera_ops .probe() method, i.e., from
1145 * soc_camera_probe() above with .video_lock held
1147 int soc_camera_video_start(struct soc_camera_device *icd, struct device *dev)
1149 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1150 const struct v4l2_queryctrl *qctrl;
1152 if (!icd->dev.parent)
1153 return -ENODEV;
1155 if (!icd->ops ||
1156 !icd->ops->init ||
1157 !icd->ops->release ||
1158 !icd->ops->start_capture ||
1159 !icd->ops->stop_capture ||
1160 !icd->ops->set_fmt ||
1161 !icd->ops->try_fmt ||
1162 !icd->ops->query_bus_param ||
1163 !icd->ops->set_bus_param)
1164 return -EINVAL;
1166 /* See comment in soc_camera_probe() */
1167 dev_set_drvdata(&icd->dev, dev);
1169 qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_GAIN);
1170 icd->gain = qctrl ? qctrl->default_value : (unsigned short)~0;
1171 qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
1172 icd->exposure = qctrl ? qctrl->default_value : (unsigned short)~0;
1174 return ici->ops->add(icd);
1176 EXPORT_SYMBOL(soc_camera_video_start);
1178 /* Called from client .remove() methods with .video_lock held */
1179 void soc_camera_video_stop(struct soc_camera_device *icd)
1181 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1183 dev_dbg(&icd->dev, "%s\n", __func__);
1185 ici->ops->remove(icd);
1187 EXPORT_SYMBOL(soc_camera_video_stop);
1189 static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev)
1191 struct soc_camera_link *icl = pdev->dev.platform_data;
1192 struct soc_camera_device *icd;
1193 int ret;
1195 if (!icl)
1196 return -EINVAL;
1198 icd = kzalloc(sizeof(*icd), GFP_KERNEL);
1199 if (!icd)
1200 return -ENOMEM;
1202 icd->iface = icl->bus_id;
1203 platform_set_drvdata(pdev, icd);
1204 icd->dev.platform_data = icl;
1206 ret = soc_camera_device_register(icd);
1207 if (ret < 0)
1208 goto escdevreg;
1210 return 0;
1212 escdevreg:
1213 kfree(icd);
1215 return ret;
1218 /* Only called on rmmod for each platform device, since they are not
1219 * hot-pluggable. Now we know, that all our users - hosts and devices have
1220 * been unloaded already */
1221 static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev)
1223 struct soc_camera_device *icd = platform_get_drvdata(pdev);
1225 if (!icd)
1226 return -EINVAL;
1228 soc_camera_device_unregister(icd);
1230 kfree(icd);
1232 return 0;
1235 static struct platform_driver __refdata soc_camera_pdrv = {
1236 .remove = __devexit_p(soc_camera_pdrv_remove),
1237 .driver = {
1238 .name = "soc-camera-pdrv",
1239 .owner = THIS_MODULE,
1243 static int __init soc_camera_init(void)
1245 int ret = bus_register(&soc_camera_bus_type);
1246 if (ret)
1247 return ret;
1248 ret = driver_register(&ic_drv);
1249 if (ret)
1250 goto edrvr;
1252 ret = platform_driver_probe(&soc_camera_pdrv, soc_camera_pdrv_probe);
1253 if (ret)
1254 goto epdr;
1256 return 0;
1258 epdr:
1259 driver_unregister(&ic_drv);
1260 edrvr:
1261 bus_unregister(&soc_camera_bus_type);
1262 return ret;
1265 static void __exit soc_camera_exit(void)
1267 platform_driver_unregister(&soc_camera_pdrv);
1268 driver_unregister(&ic_drv);
1269 bus_unregister(&soc_camera_bus_type);
1272 module_init(soc_camera_init);
1273 module_exit(soc_camera_exit);
1275 MODULE_DESCRIPTION("Image capture bus driver");
1276 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1277 MODULE_LICENSE("GPL");
1278 MODULE_ALIAS("platform:soc-camera-pdrv");