V4L/DVB (12535): soc-camera: remove .init() and .release() methods from struct soc_ca...
[linux-2.6/mini2440.git] / drivers / media / video / soc_camera.c
blob27921162514c60d2b62ba159b08512eabd1772e1
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 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
157 return v4l2_subdev_call(sd, core, s_std, *a);
160 static int soc_camera_reqbufs(struct file *file, void *priv,
161 struct v4l2_requestbuffers *p)
163 int ret;
164 struct soc_camera_file *icf = file->private_data;
165 struct soc_camera_device *icd = icf->icd;
166 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
168 WARN_ON(priv != file->private_data);
170 ret = videobuf_reqbufs(&icf->vb_vidq, p);
171 if (ret < 0)
172 return ret;
174 return ici->ops->reqbufs(icf, p);
177 static int soc_camera_querybuf(struct file *file, void *priv,
178 struct v4l2_buffer *p)
180 struct soc_camera_file *icf = file->private_data;
182 WARN_ON(priv != file->private_data);
184 return videobuf_querybuf(&icf->vb_vidq, p);
187 static int soc_camera_qbuf(struct file *file, void *priv,
188 struct v4l2_buffer *p)
190 struct soc_camera_file *icf = file->private_data;
192 WARN_ON(priv != file->private_data);
194 return videobuf_qbuf(&icf->vb_vidq, p);
197 static int soc_camera_dqbuf(struct file *file, void *priv,
198 struct v4l2_buffer *p)
200 struct soc_camera_file *icf = file->private_data;
202 WARN_ON(priv != file->private_data);
204 return videobuf_dqbuf(&icf->vb_vidq, p, file->f_flags & O_NONBLOCK);
207 /* Always entered with .video_lock held */
208 static int soc_camera_init_user_formats(struct soc_camera_device *icd)
210 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
211 int i, fmts = 0, ret;
213 if (!ici->ops->get_formats)
215 * Fallback mode - the host will have to serve all
216 * sensor-provided formats one-to-one to the user
218 fmts = icd->num_formats;
219 else
221 * First pass - only count formats this host-sensor
222 * configuration can provide
224 for (i = 0; i < icd->num_formats; i++) {
225 ret = ici->ops->get_formats(icd, i, NULL);
226 if (ret < 0)
227 return ret;
228 fmts += ret;
231 if (!fmts)
232 return -ENXIO;
234 icd->user_formats =
235 vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
236 if (!icd->user_formats)
237 return -ENOMEM;
239 icd->num_user_formats = fmts;
241 dev_dbg(&icd->dev, "Found %d supported formats.\n", fmts);
243 /* Second pass - actually fill data formats */
244 fmts = 0;
245 for (i = 0; i < icd->num_formats; i++)
246 if (!ici->ops->get_formats) {
247 icd->user_formats[i].host_fmt = icd->formats + i;
248 icd->user_formats[i].cam_fmt = icd->formats + i;
249 icd->user_formats[i].buswidth = icd->formats[i].depth;
250 } else {
251 ret = ici->ops->get_formats(icd, i,
252 &icd->user_formats[fmts]);
253 if (ret < 0)
254 goto egfmt;
255 fmts += ret;
258 icd->current_fmt = icd->user_formats[0].host_fmt;
260 return 0;
262 egfmt:
263 icd->num_user_formats = 0;
264 vfree(icd->user_formats);
265 return ret;
268 /* Always entered with .video_lock held */
269 static void soc_camera_free_user_formats(struct soc_camera_device *icd)
271 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
273 if (ici->ops->put_formats)
274 ici->ops->put_formats(icd);
275 icd->current_fmt = NULL;
276 icd->num_user_formats = 0;
277 vfree(icd->user_formats);
278 icd->user_formats = NULL;
281 #define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
282 ((x) >> 24) & 0xff
284 /* Called with .vb_lock held */
285 static int soc_camera_set_fmt(struct soc_camera_file *icf,
286 struct v4l2_format *f)
288 struct soc_camera_device *icd = icf->icd;
289 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
290 struct v4l2_pix_format *pix = &f->fmt.pix;
291 int ret;
293 dev_dbg(&icd->dev, "S_FMT(%c%c%c%c, %ux%u)\n",
294 pixfmtstr(pix->pixelformat), pix->width, pix->height);
296 /* We always call try_fmt() before set_fmt() or set_crop() */
297 ret = ici->ops->try_fmt(icd, f);
298 if (ret < 0)
299 return ret;
301 ret = ici->ops->set_fmt(icd, f);
302 if (ret < 0) {
303 return ret;
304 } else if (!icd->current_fmt ||
305 icd->current_fmt->fourcc != pix->pixelformat) {
306 dev_err(&icd->dev,
307 "Host driver hasn't set up current format correctly!\n");
308 return -EINVAL;
311 icd->user_width = pix->width;
312 icd->user_height = pix->height;
313 icf->vb_vidq.field =
314 icd->field = pix->field;
316 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
317 dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n",
318 f->type);
320 dev_dbg(&icd->dev, "set width: %d height: %d\n",
321 icd->user_width, icd->user_height);
323 /* set physical bus parameters */
324 return ici->ops->set_bus_param(icd, pix->pixelformat);
327 static int soc_camera_open(struct file *file)
329 struct video_device *vdev = video_devdata(file);
330 struct soc_camera_device *icd = container_of(vdev->parent, struct soc_camera_device, dev);
331 struct soc_camera_link *icl = to_soc_camera_link(icd);
332 struct soc_camera_host *ici;
333 struct soc_camera_file *icf;
334 int ret;
336 if (!icd->ops)
337 /* No device driver attached */
338 return -ENODEV;
340 ici = to_soc_camera_host(icd->dev.parent);
342 icf = vmalloc(sizeof(*icf));
343 if (!icf)
344 return -ENOMEM;
346 if (!try_module_get(ici->ops->owner)) {
347 dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
348 ret = -EINVAL;
349 goto emgi;
352 /* Protect against icd->ops->remove() until we module_get() both drivers. */
353 mutex_lock(&icd->video_lock);
355 icf->icd = icd;
356 icd->use_count++;
358 /* Now we really have to activate the camera */
359 if (icd->use_count == 1) {
360 /* Restore parameters before the last close() per V4L2 API */
361 struct v4l2_format f = {
362 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
363 .fmt.pix = {
364 .width = icd->user_width,
365 .height = icd->user_height,
366 .field = icd->field,
367 .pixelformat = icd->current_fmt->fourcc,
368 .colorspace = icd->current_fmt->colorspace,
372 if (icl->power) {
373 ret = icl->power(icd->pdev, 1);
374 if (ret < 0)
375 goto epower;
378 /* The camera could have been already on, try to reset */
379 if (icl->reset)
380 icl->reset(icd->pdev);
382 ret = ici->ops->add(icd);
383 if (ret < 0) {
384 dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
385 goto eiciadd;
388 /* Try to configure with default parameters */
389 ret = soc_camera_set_fmt(icf, &f);
390 if (ret < 0)
391 goto esfmt;
394 file->private_data = icf;
395 dev_dbg(&icd->dev, "camera device open\n");
397 ici->ops->init_videobuf(&icf->vb_vidq, icd);
399 mutex_unlock(&icd->video_lock);
401 return 0;
404 * First five errors are entered with the .video_lock held
405 * and use_count == 1
407 esfmt:
408 ici->ops->remove(icd);
409 eiciadd:
410 if (icl->power)
411 icl->power(icd->pdev, 0);
412 epower:
413 icd->use_count--;
414 mutex_unlock(&icd->video_lock);
415 module_put(ici->ops->owner);
416 emgi:
417 vfree(icf);
418 return ret;
421 static int soc_camera_close(struct file *file)
423 struct soc_camera_file *icf = file->private_data;
424 struct soc_camera_device *icd = icf->icd;
425 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
427 mutex_lock(&icd->video_lock);
428 icd->use_count--;
429 if (!icd->use_count) {
430 struct soc_camera_link *icl = to_soc_camera_link(icd);
432 ici->ops->remove(icd);
433 if (icl->power)
434 icl->power(icd->pdev, 0);
437 mutex_unlock(&icd->video_lock);
439 module_put(ici->ops->owner);
441 vfree(icf);
443 dev_dbg(&icd->dev, "camera device close\n");
445 return 0;
448 static ssize_t soc_camera_read(struct file *file, char __user *buf,
449 size_t count, loff_t *ppos)
451 struct soc_camera_file *icf = file->private_data;
452 struct soc_camera_device *icd = icf->icd;
453 int err = -EINVAL;
455 dev_err(&icd->dev, "camera device read not implemented\n");
457 return err;
460 static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
462 struct soc_camera_file *icf = file->private_data;
463 struct soc_camera_device *icd = icf->icd;
464 int err;
466 dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
468 err = videobuf_mmap_mapper(&icf->vb_vidq, vma);
470 dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
471 (unsigned long)vma->vm_start,
472 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
473 err);
475 return err;
478 static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
480 struct soc_camera_file *icf = file->private_data;
481 struct soc_camera_device *icd = icf->icd;
482 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
484 if (list_empty(&icf->vb_vidq.stream)) {
485 dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
486 return POLLERR;
489 return ici->ops->poll(file, pt);
492 static struct v4l2_file_operations soc_camera_fops = {
493 .owner = THIS_MODULE,
494 .open = soc_camera_open,
495 .release = soc_camera_close,
496 .ioctl = video_ioctl2,
497 .read = soc_camera_read,
498 .mmap = soc_camera_mmap,
499 .poll = soc_camera_poll,
502 static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
503 struct v4l2_format *f)
505 struct soc_camera_file *icf = file->private_data;
506 struct soc_camera_device *icd = icf->icd;
507 int ret;
509 WARN_ON(priv != file->private_data);
511 mutex_lock(&icf->vb_vidq.vb_lock);
513 if (icf->vb_vidq.bufs[0]) {
514 dev_err(&icd->dev, "S_FMT denied: queue initialised\n");
515 ret = -EBUSY;
516 goto unlock;
519 ret = soc_camera_set_fmt(icf, f);
521 unlock:
522 mutex_unlock(&icf->vb_vidq.vb_lock);
524 return ret;
527 static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
528 struct v4l2_fmtdesc *f)
530 struct soc_camera_file *icf = file->private_data;
531 struct soc_camera_device *icd = icf->icd;
532 const struct soc_camera_data_format *format;
534 WARN_ON(priv != file->private_data);
536 if (f->index >= icd->num_user_formats)
537 return -EINVAL;
539 format = icd->user_formats[f->index].host_fmt;
541 strlcpy(f->description, format->name, sizeof(f->description));
542 f->pixelformat = format->fourcc;
543 return 0;
546 static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
547 struct v4l2_format *f)
549 struct soc_camera_file *icf = file->private_data;
550 struct soc_camera_device *icd = icf->icd;
551 struct v4l2_pix_format *pix = &f->fmt.pix;
553 WARN_ON(priv != file->private_data);
555 pix->width = icd->user_width;
556 pix->height = icd->user_height;
557 pix->field = icf->vb_vidq.field;
558 pix->pixelformat = icd->current_fmt->fourcc;
559 pix->bytesperline = pix->width *
560 DIV_ROUND_UP(icd->current_fmt->depth, 8);
561 pix->sizeimage = pix->height * pix->bytesperline;
562 dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
563 icd->current_fmt->fourcc);
564 return 0;
567 static int soc_camera_querycap(struct file *file, void *priv,
568 struct v4l2_capability *cap)
570 struct soc_camera_file *icf = file->private_data;
571 struct soc_camera_device *icd = icf->icd;
572 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
574 WARN_ON(priv != file->private_data);
576 strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
577 return ici->ops->querycap(ici, cap);
580 static int soc_camera_streamon(struct file *file, void *priv,
581 enum v4l2_buf_type i)
583 struct soc_camera_file *icf = file->private_data;
584 struct soc_camera_device *icd = icf->icd;
585 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
586 int ret;
588 WARN_ON(priv != file->private_data);
590 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
591 return -EINVAL;
593 mutex_lock(&icd->video_lock);
595 v4l2_subdev_call(sd, video, s_stream, 1);
597 /* This calls buf_queue from host driver's videobuf_queue_ops */
598 ret = videobuf_streamon(&icf->vb_vidq);
600 mutex_unlock(&icd->video_lock);
602 return ret;
605 static int soc_camera_streamoff(struct file *file, void *priv,
606 enum v4l2_buf_type i)
608 struct soc_camera_file *icf = file->private_data;
609 struct soc_camera_device *icd = icf->icd;
610 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
612 WARN_ON(priv != file->private_data);
614 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
615 return -EINVAL;
617 mutex_lock(&icd->video_lock);
619 /* This calls buf_release from host driver's videobuf_queue_ops for all
620 * remaining buffers. When the last buffer is freed, stop capture */
621 videobuf_streamoff(&icf->vb_vidq);
623 v4l2_subdev_call(sd, video, s_stream, 0);
625 mutex_unlock(&icd->video_lock);
627 return 0;
630 static int soc_camera_queryctrl(struct file *file, void *priv,
631 struct v4l2_queryctrl *qc)
633 struct soc_camera_file *icf = file->private_data;
634 struct soc_camera_device *icd = icf->icd;
635 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
636 int i;
638 WARN_ON(priv != file->private_data);
640 if (!qc->id)
641 return -EINVAL;
643 /* First check host controls */
644 for (i = 0; i < ici->ops->num_controls; i++)
645 if (qc->id == ici->ops->controls[i].id) {
646 memcpy(qc, &(ici->ops->controls[i]),
647 sizeof(*qc));
648 return 0;
651 /* Then device controls */
652 for (i = 0; i < icd->ops->num_controls; i++)
653 if (qc->id == icd->ops->controls[i].id) {
654 memcpy(qc, &(icd->ops->controls[i]),
655 sizeof(*qc));
656 return 0;
659 return -EINVAL;
662 static int soc_camera_g_ctrl(struct file *file, void *priv,
663 struct v4l2_control *ctrl)
665 struct soc_camera_file *icf = file->private_data;
666 struct soc_camera_device *icd = icf->icd;
667 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
668 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
669 int ret;
671 WARN_ON(priv != file->private_data);
673 switch (ctrl->id) {
674 case V4L2_CID_GAIN:
675 if (icd->gain == (unsigned short)~0)
676 return -EINVAL;
677 ctrl->value = icd->gain;
678 return 0;
679 case V4L2_CID_EXPOSURE:
680 if (icd->exposure == (unsigned short)~0)
681 return -EINVAL;
682 ctrl->value = icd->exposure;
683 return 0;
686 if (ici->ops->get_ctrl) {
687 ret = ici->ops->get_ctrl(icd, ctrl);
688 if (ret != -ENOIOCTLCMD)
689 return ret;
692 return v4l2_subdev_call(sd, core, g_ctrl, ctrl);
695 static int soc_camera_s_ctrl(struct file *file, void *priv,
696 struct v4l2_control *ctrl)
698 struct soc_camera_file *icf = file->private_data;
699 struct soc_camera_device *icd = icf->icd;
700 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
701 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
702 int ret;
704 WARN_ON(priv != file->private_data);
706 if (ici->ops->set_ctrl) {
707 ret = ici->ops->set_ctrl(icd, ctrl);
708 if (ret != -ENOIOCTLCMD)
709 return ret;
712 return v4l2_subdev_call(sd, core, s_ctrl, ctrl);
715 static int soc_camera_cropcap(struct file *file, void *fh,
716 struct v4l2_cropcap *a)
718 struct soc_camera_file *icf = file->private_data;
719 struct soc_camera_device *icd = icf->icd;
720 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
722 return ici->ops->cropcap(icd, a);
725 static int soc_camera_g_crop(struct file *file, void *fh,
726 struct v4l2_crop *a)
728 struct soc_camera_file *icf = file->private_data;
729 struct soc_camera_device *icd = icf->icd;
730 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
731 int ret;
733 mutex_lock(&icf->vb_vidq.vb_lock);
734 ret = ici->ops->get_crop(icd, a);
735 mutex_unlock(&icf->vb_vidq.vb_lock);
737 return ret;
741 * According to the V4L2 API, drivers shall not update the struct v4l2_crop
742 * argument with the actual geometry, instead, the user shall use G_CROP to
743 * retrieve it. However, we expect camera host and client drivers to update
744 * the argument, which we then use internally, but do not return to the user.
746 static int soc_camera_s_crop(struct file *file, void *fh,
747 struct v4l2_crop *a)
749 struct soc_camera_file *icf = file->private_data;
750 struct soc_camera_device *icd = icf->icd;
751 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
752 struct v4l2_rect *rect = &a->c;
753 struct v4l2_crop current_crop;
754 int ret;
756 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
757 return -EINVAL;
759 dev_dbg(&icd->dev, "S_CROP(%ux%u@%u:%u)\n",
760 rect->width, rect->height, rect->left, rect->top);
762 /* Cropping is allowed during a running capture, guard consistency */
763 mutex_lock(&icf->vb_vidq.vb_lock);
765 /* If get_crop fails, we'll let host and / or client drivers decide */
766 ret = ici->ops->get_crop(icd, &current_crop);
768 /* Prohibit window size change with initialised buffers */
769 if (icf->vb_vidq.bufs[0] && !ret &&
770 (a->c.width != current_crop.c.width ||
771 a->c.height != current_crop.c.height)) {
772 dev_err(&icd->dev,
773 "S_CROP denied: queue initialised and sizes differ\n");
774 ret = -EBUSY;
775 } else {
776 ret = ici->ops->set_crop(icd, a);
779 mutex_unlock(&icf->vb_vidq.vb_lock);
781 return ret;
784 static int soc_camera_g_chip_ident(struct file *file, void *fh,
785 struct v4l2_dbg_chip_ident *id)
787 struct soc_camera_file *icf = file->private_data;
788 struct soc_camera_device *icd = icf->icd;
789 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
791 return v4l2_subdev_call(sd, core, g_chip_ident, id);
794 #ifdef CONFIG_VIDEO_ADV_DEBUG
795 static int soc_camera_g_register(struct file *file, void *fh,
796 struct v4l2_dbg_register *reg)
798 struct soc_camera_file *icf = file->private_data;
799 struct soc_camera_device *icd = icf->icd;
800 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
802 return v4l2_subdev_call(sd, core, g_register, reg);
805 static int soc_camera_s_register(struct file *file, void *fh,
806 struct v4l2_dbg_register *reg)
808 struct soc_camera_file *icf = file->private_data;
809 struct soc_camera_device *icd = icf->icd;
810 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
812 return v4l2_subdev_call(sd, core, s_register, reg);
814 #endif
816 /* So far this function cannot fail */
817 static void scan_add_host(struct soc_camera_host *ici)
819 struct soc_camera_device *icd;
821 mutex_lock(&list_lock);
823 list_for_each_entry(icd, &devices, list) {
824 if (icd->iface == ici->nr) {
825 int ret;
826 icd->dev.parent = ici->v4l2_dev.dev;
827 dev_set_name(&icd->dev, "%u-%u", icd->iface,
828 icd->devnum);
829 ret = device_register(&icd->dev);
830 if (ret < 0) {
831 icd->dev.parent = NULL;
832 dev_err(&icd->dev,
833 "Cannot register device: %d\n", ret);
838 mutex_unlock(&list_lock);
841 #ifdef CONFIG_I2C_BOARDINFO
842 static int soc_camera_init_i2c(struct soc_camera_device *icd,
843 struct soc_camera_link *icl)
845 struct i2c_client *client;
846 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
847 struct i2c_adapter *adap = i2c_get_adapter(icl->i2c_adapter_id);
848 struct v4l2_subdev *subdev;
849 int ret;
851 if (!adap) {
852 ret = -ENODEV;
853 dev_err(&icd->dev, "Cannot get I2C adapter #%d. No driver?\n",
854 icl->i2c_adapter_id);
855 goto ei2cga;
858 icl->board_info->platform_data = icd;
860 subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
861 icl->module_name, icl->board_info, NULL);
862 if (!subdev) {
863 ret = -ENOMEM;
864 goto ei2cnd;
867 subdev->grp_id = (__u32)icd;
868 client = subdev->priv;
870 /* Use to_i2c_client(dev) to recover the i2c client */
871 dev_set_drvdata(&icd->dev, &client->dev);
873 return 0;
874 ei2cnd:
875 i2c_put_adapter(adap);
876 ei2cga:
877 return ret;
880 static void soc_camera_free_i2c(struct soc_camera_device *icd)
882 struct i2c_client *client =
883 to_i2c_client(to_soc_camera_control(icd));
884 dev_set_drvdata(&icd->dev, NULL);
885 v4l2_device_unregister_subdev(i2c_get_clientdata(client));
886 i2c_unregister_device(client);
887 i2c_put_adapter(client->adapter);
889 #else
890 #define soc_camera_init_i2c(icd, icl) (-ENODEV)
891 #define soc_camera_free_i2c(icd) do {} while (0)
892 #endif
894 static int soc_camera_video_start(struct soc_camera_device *icd);
895 static int video_dev_create(struct soc_camera_device *icd);
896 /* Called during host-driver probe */
897 static int soc_camera_probe(struct device *dev)
899 struct soc_camera_device *icd = to_soc_camera_dev(dev);
900 struct soc_camera_host *ici = to_soc_camera_host(dev->parent);
901 struct soc_camera_link *icl = to_soc_camera_link(icd);
902 struct device *control = NULL;
903 struct v4l2_subdev *sd;
904 struct v4l2_format f = {.type = V4L2_BUF_TYPE_VIDEO_CAPTURE};
905 int ret;
907 dev_info(dev, "Probing %s\n", dev_name(dev));
909 if (icl->power) {
910 ret = icl->power(icd->pdev, 1);
911 if (ret < 0) {
912 dev_err(dev,
913 "Platform failed to power-on the camera.\n");
914 goto epower;
918 /* The camera could have been already on, try to reset */
919 if (icl->reset)
920 icl->reset(icd->pdev);
922 ret = ici->ops->add(icd);
923 if (ret < 0)
924 goto eadd;
926 /* Must have icd->vdev before registering the device */
927 ret = video_dev_create(icd);
928 if (ret < 0)
929 goto evdc;
931 /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
932 if (icl->board_info) {
933 ret = soc_camera_init_i2c(icd, icl);
934 if (ret < 0)
935 goto eadddev;
936 } else if (!icl->add_device || !icl->del_device) {
937 ret = -EINVAL;
938 goto eadddev;
939 } else {
940 if (icl->module_name)
941 ret = request_module(icl->module_name);
943 ret = icl->add_device(icl, &icd->dev);
944 if (ret < 0)
945 goto eadddev;
947 /* FIXME: this is racy, have to use driver-binding notification */
948 control = to_soc_camera_control(icd);
949 if (!control || !control->driver || !dev_get_drvdata(control) ||
950 !try_module_get(control->driver->owner)) {
951 icl->del_device(icl);
952 goto enodrv;
956 /* At this point client .probe() should have run already */
957 ret = soc_camera_init_user_formats(icd);
958 if (ret < 0)
959 goto eiufmt;
961 icd->field = V4L2_FIELD_ANY;
963 /* ..._video_start() will create a device node, so we have to protect */
964 mutex_lock(&icd->video_lock);
966 ret = soc_camera_video_start(icd);
967 if (ret < 0)
968 goto evidstart;
970 /* Try to improve our guess of a reasonable window format */
971 sd = soc_camera_to_subdev(icd);
972 if (!v4l2_subdev_call(sd, video, g_fmt, &f)) {
973 icd->user_width = f.fmt.pix.width;
974 icd->user_height = f.fmt.pix.height;
977 /* Do we have to sysfs_remove_link() before device_unregister()? */
978 if (sysfs_create_link(&icd->dev.kobj, &to_soc_camera_control(icd)->kobj,
979 "control"))
980 dev_warn(&icd->dev, "Failed creating the control symlink\n");
982 ici->ops->remove(icd);
984 if (icl->power)
985 icl->power(icd->pdev, 0);
987 mutex_unlock(&icd->video_lock);
989 return 0;
991 evidstart:
992 mutex_unlock(&icd->video_lock);
993 soc_camera_free_user_formats(icd);
994 eiufmt:
995 if (icl->board_info) {
996 soc_camera_free_i2c(icd);
997 } else {
998 icl->del_device(icl);
999 module_put(control->driver->owner);
1001 enodrv:
1002 eadddev:
1003 video_device_release(icd->vdev);
1004 evdc:
1005 ici->ops->remove(icd);
1006 eadd:
1007 if (icl->power)
1008 icl->power(icd->pdev, 0);
1009 epower:
1010 return ret;
1013 /* This is called on device_unregister, which only means we have to disconnect
1014 * from the host, but not remove ourselves from the device list */
1015 static int soc_camera_remove(struct device *dev)
1017 struct soc_camera_device *icd = to_soc_camera_dev(dev);
1018 struct soc_camera_link *icl = to_soc_camera_link(icd);
1019 struct video_device *vdev = icd->vdev;
1021 BUG_ON(!dev->parent);
1023 if (vdev) {
1024 mutex_lock(&icd->video_lock);
1025 video_unregister_device(vdev);
1026 icd->vdev = NULL;
1027 mutex_unlock(&icd->video_lock);
1030 if (icl->board_info) {
1031 soc_camera_free_i2c(icd);
1032 } else {
1033 struct device_driver *drv = to_soc_camera_control(icd) ?
1034 to_soc_camera_control(icd)->driver : NULL;
1035 if (drv) {
1036 icl->del_device(icl);
1037 module_put(drv->owner);
1040 soc_camera_free_user_formats(icd);
1042 return 0;
1045 static int soc_camera_suspend(struct device *dev, pm_message_t state)
1047 struct soc_camera_device *icd = to_soc_camera_dev(dev);
1048 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1049 int ret = 0;
1051 if (ici->ops->suspend)
1052 ret = ici->ops->suspend(icd, state);
1054 return ret;
1057 static int soc_camera_resume(struct device *dev)
1059 struct soc_camera_device *icd = to_soc_camera_dev(dev);
1060 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1061 int ret = 0;
1063 if (ici->ops->resume)
1064 ret = ici->ops->resume(icd);
1066 return ret;
1069 static struct bus_type soc_camera_bus_type = {
1070 .name = "soc-camera",
1071 .probe = soc_camera_probe,
1072 .remove = soc_camera_remove,
1073 .suspend = soc_camera_suspend,
1074 .resume = soc_camera_resume,
1077 static struct device_driver ic_drv = {
1078 .name = "camera",
1079 .bus = &soc_camera_bus_type,
1080 .owner = THIS_MODULE,
1083 static void dummy_release(struct device *dev)
1087 static int default_cropcap(struct soc_camera_device *icd,
1088 struct v4l2_cropcap *a)
1090 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1091 return v4l2_subdev_call(sd, video, cropcap, a);
1094 static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1096 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1097 return v4l2_subdev_call(sd, video, g_crop, a);
1100 static int default_s_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1102 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1103 return v4l2_subdev_call(sd, video, s_crop, a);
1106 int soc_camera_host_register(struct soc_camera_host *ici)
1108 struct soc_camera_host *ix;
1109 int ret;
1111 if (!ici || !ici->ops ||
1112 !ici->ops->try_fmt ||
1113 !ici->ops->set_fmt ||
1114 !ici->ops->set_bus_param ||
1115 !ici->ops->querycap ||
1116 !ici->ops->init_videobuf ||
1117 !ici->ops->reqbufs ||
1118 !ici->ops->add ||
1119 !ici->ops->remove ||
1120 !ici->ops->poll ||
1121 !ici->v4l2_dev.dev)
1122 return -EINVAL;
1124 if (!ici->ops->set_crop)
1125 ici->ops->set_crop = default_s_crop;
1126 if (!ici->ops->get_crop)
1127 ici->ops->get_crop = default_g_crop;
1128 if (!ici->ops->cropcap)
1129 ici->ops->cropcap = default_cropcap;
1131 mutex_lock(&list_lock);
1132 list_for_each_entry(ix, &hosts, list) {
1133 if (ix->nr == ici->nr) {
1134 ret = -EBUSY;
1135 goto edevreg;
1139 ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
1140 if (ret < 0)
1141 goto edevreg;
1143 list_add_tail(&ici->list, &hosts);
1144 mutex_unlock(&list_lock);
1146 scan_add_host(ici);
1148 return 0;
1150 edevreg:
1151 mutex_unlock(&list_lock);
1152 return ret;
1154 EXPORT_SYMBOL(soc_camera_host_register);
1156 /* Unregister all clients! */
1157 void soc_camera_host_unregister(struct soc_camera_host *ici)
1159 struct soc_camera_device *icd;
1161 mutex_lock(&list_lock);
1163 list_del(&ici->list);
1165 list_for_each_entry(icd, &devices, list) {
1166 if (icd->iface == ici->nr) {
1167 /* The bus->remove will be called */
1168 device_unregister(&icd->dev);
1169 /* Not before device_unregister(), .remove
1170 * needs parent to call ici->ops->remove() */
1171 icd->dev.parent = NULL;
1173 /* If the host module is loaded again, device_register()
1174 * would complain "already initialised" */
1175 memset(&icd->dev.kobj, 0, sizeof(icd->dev.kobj));
1179 mutex_unlock(&list_lock);
1181 v4l2_device_unregister(&ici->v4l2_dev);
1183 EXPORT_SYMBOL(soc_camera_host_unregister);
1185 /* Image capture device */
1186 static int soc_camera_device_register(struct soc_camera_device *icd)
1188 struct soc_camera_device *ix;
1189 int num = -1, i;
1191 for (i = 0; i < 256 && num < 0; i++) {
1192 num = i;
1193 /* Check if this index is available on this interface */
1194 list_for_each_entry(ix, &devices, list) {
1195 if (ix->iface == icd->iface && ix->devnum == i) {
1196 num = -1;
1197 break;
1202 if (num < 0)
1203 /* ok, we have 256 cameras on this host...
1204 * man, stay reasonable... */
1205 return -ENOMEM;
1207 icd->devnum = num;
1208 icd->dev.bus = &soc_camera_bus_type;
1210 icd->dev.release = dummy_release;
1211 icd->use_count = 0;
1212 icd->host_priv = NULL;
1213 mutex_init(&icd->video_lock);
1215 list_add_tail(&icd->list, &devices);
1217 return 0;
1220 static void soc_camera_device_unregister(struct soc_camera_device *icd)
1222 list_del(&icd->list);
1225 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
1226 .vidioc_querycap = soc_camera_querycap,
1227 .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
1228 .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
1229 .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
1230 .vidioc_enum_input = soc_camera_enum_input,
1231 .vidioc_g_input = soc_camera_g_input,
1232 .vidioc_s_input = soc_camera_s_input,
1233 .vidioc_s_std = soc_camera_s_std,
1234 .vidioc_reqbufs = soc_camera_reqbufs,
1235 .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
1236 .vidioc_querybuf = soc_camera_querybuf,
1237 .vidioc_qbuf = soc_camera_qbuf,
1238 .vidioc_dqbuf = soc_camera_dqbuf,
1239 .vidioc_streamon = soc_camera_streamon,
1240 .vidioc_streamoff = soc_camera_streamoff,
1241 .vidioc_queryctrl = soc_camera_queryctrl,
1242 .vidioc_g_ctrl = soc_camera_g_ctrl,
1243 .vidioc_s_ctrl = soc_camera_s_ctrl,
1244 .vidioc_cropcap = soc_camera_cropcap,
1245 .vidioc_g_crop = soc_camera_g_crop,
1246 .vidioc_s_crop = soc_camera_s_crop,
1247 .vidioc_g_chip_ident = soc_camera_g_chip_ident,
1248 #ifdef CONFIG_VIDEO_ADV_DEBUG
1249 .vidioc_g_register = soc_camera_g_register,
1250 .vidioc_s_register = soc_camera_s_register,
1251 #endif
1254 static int video_dev_create(struct soc_camera_device *icd)
1256 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1257 struct video_device *vdev = video_device_alloc();
1259 if (!vdev)
1260 return -ENOMEM;
1262 strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
1264 vdev->parent = &icd->dev;
1265 vdev->current_norm = V4L2_STD_UNKNOWN;
1266 vdev->fops = &soc_camera_fops;
1267 vdev->ioctl_ops = &soc_camera_ioctl_ops;
1268 vdev->release = video_device_release;
1269 vdev->minor = -1;
1270 vdev->tvnorms = V4L2_STD_UNKNOWN;
1272 icd->vdev = vdev;
1274 return 0;
1278 * Called from soc_camera_probe() above (with .video_lock held???)
1280 static int soc_camera_video_start(struct soc_camera_device *icd)
1282 const struct v4l2_queryctrl *qctrl;
1283 int ret;
1285 if (!icd->dev.parent)
1286 return -ENODEV;
1288 if (!icd->ops ||
1289 !icd->ops->query_bus_param ||
1290 !icd->ops->set_bus_param)
1291 return -EINVAL;
1293 ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER,
1294 icd->vdev->minor);
1295 if (ret < 0) {
1296 dev_err(&icd->dev, "video_register_device failed: %d\n", ret);
1297 return ret;
1300 qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_GAIN);
1301 icd->gain = qctrl ? qctrl->default_value : (unsigned short)~0;
1302 qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
1303 icd->exposure = qctrl ? qctrl->default_value : (unsigned short)~0;
1305 return 0;
1308 static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev)
1310 struct soc_camera_link *icl = pdev->dev.platform_data;
1311 struct soc_camera_device *icd;
1312 int ret;
1314 if (!icl)
1315 return -EINVAL;
1317 icd = kzalloc(sizeof(*icd), GFP_KERNEL);
1318 if (!icd)
1319 return -ENOMEM;
1321 icd->iface = icl->bus_id;
1322 icd->pdev = &pdev->dev;
1323 platform_set_drvdata(pdev, icd);
1324 icd->dev.platform_data = icl;
1326 ret = soc_camera_device_register(icd);
1327 if (ret < 0)
1328 goto escdevreg;
1330 icd->user_width = DEFAULT_WIDTH;
1331 icd->user_height = DEFAULT_HEIGHT;
1333 return 0;
1335 escdevreg:
1336 kfree(icd);
1338 return ret;
1341 /* Only called on rmmod for each platform device, since they are not
1342 * hot-pluggable. Now we know, that all our users - hosts and devices have
1343 * been unloaded already */
1344 static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev)
1346 struct soc_camera_device *icd = platform_get_drvdata(pdev);
1348 if (!icd)
1349 return -EINVAL;
1351 soc_camera_device_unregister(icd);
1353 kfree(icd);
1355 return 0;
1358 static struct platform_driver __refdata soc_camera_pdrv = {
1359 .remove = __devexit_p(soc_camera_pdrv_remove),
1360 .driver = {
1361 .name = "soc-camera-pdrv",
1362 .owner = THIS_MODULE,
1366 static int __init soc_camera_init(void)
1368 int ret = bus_register(&soc_camera_bus_type);
1369 if (ret)
1370 return ret;
1371 ret = driver_register(&ic_drv);
1372 if (ret)
1373 goto edrvr;
1375 ret = platform_driver_probe(&soc_camera_pdrv, soc_camera_pdrv_probe);
1376 if (ret)
1377 goto epdr;
1379 return 0;
1381 epdr:
1382 driver_unregister(&ic_drv);
1383 edrvr:
1384 bus_unregister(&soc_camera_bus_type);
1385 return ret;
1388 static void __exit soc_camera_exit(void)
1390 platform_driver_unregister(&soc_camera_pdrv);
1391 driver_unregister(&ic_drv);
1392 bus_unregister(&soc_camera_bus_type);
1395 module_init(soc_camera_init);
1396 module_exit(soc_camera_exit);
1398 MODULE_DESCRIPTION("Image capture bus driver");
1399 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1400 MODULE_LICENSE("GPL");
1401 MODULE_ALIAS("platform:soc-camera-pdrv");