Merge branch 'v6v7' into devel
[linux/fpc-iii.git] / drivers / media / video / soc_camera.c
bloba66811b43710a690c2a5d7ee77967ce40af1a6d4
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/regulator/consumer.h>
28 #include <linux/slab.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/vmalloc.h>
32 #include <media/soc_camera.h>
33 #include <media/v4l2-common.h>
34 #include <media/v4l2-ioctl.h>
35 #include <media/v4l2-dev.h>
36 #include <media/videobuf-core.h>
37 #include <media/soc_mediabus.h>
39 /* Default to VGA resolution */
40 #define DEFAULT_WIDTH 640
41 #define DEFAULT_HEIGHT 480
43 static LIST_HEAD(hosts);
44 static LIST_HEAD(devices);
45 static DEFINE_MUTEX(list_lock); /* Protects the list of hosts */
47 static int soc_camera_power_set(struct soc_camera_device *icd,
48 struct soc_camera_link *icl,
49 int power_on)
51 int ret;
53 if (power_on) {
54 ret = regulator_bulk_enable(icl->num_regulators,
55 icl->regulators);
56 if (ret < 0) {
57 dev_err(&icd->dev, "Cannot enable regulators\n");
58 return ret;
61 if (icl->power)
62 ret = icl->power(icd->pdev, power_on);
63 if (ret < 0) {
64 dev_err(&icd->dev,
65 "Platform failed to power-on the camera.\n");
67 regulator_bulk_disable(icl->num_regulators,
68 icl->regulators);
69 return ret;
71 } else {
72 ret = 0;
73 if (icl->power)
74 ret = icl->power(icd->pdev, 0);
75 if (ret < 0) {
76 dev_err(&icd->dev,
77 "Platform failed to power-off the camera.\n");
78 return ret;
81 ret = regulator_bulk_disable(icl->num_regulators,
82 icl->regulators);
83 if (ret < 0) {
84 dev_err(&icd->dev, "Cannot disable regulators\n");
85 return ret;
89 return 0;
92 const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
93 struct soc_camera_device *icd, unsigned int fourcc)
95 unsigned int i;
97 for (i = 0; i < icd->num_user_formats; i++)
98 if (icd->user_formats[i].host_fmt->fourcc == fourcc)
99 return icd->user_formats + i;
100 return NULL;
102 EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);
105 * soc_camera_apply_sensor_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
106 * @icl: camera platform parameters
107 * @flags: flags to be inverted according to platform configuration
108 * @return: resulting flags
110 unsigned long soc_camera_apply_sensor_flags(struct soc_camera_link *icl,
111 unsigned long flags)
113 unsigned long f;
115 /* If only one of the two polarities is supported, switch to the opposite */
116 if (icl->flags & SOCAM_SENSOR_INVERT_HSYNC) {
117 f = flags & (SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW);
118 if (f == SOCAM_HSYNC_ACTIVE_HIGH || f == SOCAM_HSYNC_ACTIVE_LOW)
119 flags ^= SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW;
122 if (icl->flags & SOCAM_SENSOR_INVERT_VSYNC) {
123 f = flags & (SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW);
124 if (f == SOCAM_VSYNC_ACTIVE_HIGH || f == SOCAM_VSYNC_ACTIVE_LOW)
125 flags ^= SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW;
128 if (icl->flags & SOCAM_SENSOR_INVERT_PCLK) {
129 f = flags & (SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING);
130 if (f == SOCAM_PCLK_SAMPLE_RISING || f == SOCAM_PCLK_SAMPLE_FALLING)
131 flags ^= SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING;
134 return flags;
136 EXPORT_SYMBOL(soc_camera_apply_sensor_flags);
138 static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
139 struct v4l2_format *f)
141 struct soc_camera_device *icd = file->private_data;
142 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
144 WARN_ON(priv != file->private_data);
146 /* limit format to hardware capabilities */
147 return ici->ops->try_fmt(icd, f);
150 static int soc_camera_enum_input(struct file *file, void *priv,
151 struct v4l2_input *inp)
153 struct soc_camera_device *icd = file->private_data;
154 int ret = 0;
156 if (inp->index != 0)
157 return -EINVAL;
159 if (icd->ops->enum_input)
160 ret = icd->ops->enum_input(icd, inp);
161 else {
162 /* default is camera */
163 inp->type = V4L2_INPUT_TYPE_CAMERA;
164 inp->std = V4L2_STD_UNKNOWN;
165 strcpy(inp->name, "Camera");
168 return ret;
171 static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
173 *i = 0;
175 return 0;
178 static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
180 if (i > 0)
181 return -EINVAL;
183 return 0;
186 static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
188 struct soc_camera_device *icd = file->private_data;
189 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
191 return v4l2_subdev_call(sd, core, s_std, *a);
194 static int soc_camera_reqbufs(struct file *file, void *priv,
195 struct v4l2_requestbuffers *p)
197 int ret;
198 struct soc_camera_device *icd = file->private_data;
199 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
201 WARN_ON(priv != file->private_data);
203 if (icd->streamer && icd->streamer != file)
204 return -EBUSY;
206 ret = videobuf_reqbufs(&icd->vb_vidq, p);
207 if (ret < 0)
208 return ret;
210 ret = ici->ops->reqbufs(icd, p);
211 if (!ret && !icd->streamer)
212 icd->streamer = file;
214 return ret;
217 static int soc_camera_querybuf(struct file *file, void *priv,
218 struct v4l2_buffer *p)
220 struct soc_camera_device *icd = file->private_data;
222 WARN_ON(priv != file->private_data);
224 return videobuf_querybuf(&icd->vb_vidq, p);
227 static int soc_camera_qbuf(struct file *file, void *priv,
228 struct v4l2_buffer *p)
230 struct soc_camera_device *icd = file->private_data;
232 WARN_ON(priv != file->private_data);
234 if (icd->streamer != file)
235 return -EBUSY;
237 return videobuf_qbuf(&icd->vb_vidq, p);
240 static int soc_camera_dqbuf(struct file *file, void *priv,
241 struct v4l2_buffer *p)
243 struct soc_camera_device *icd = file->private_data;
245 WARN_ON(priv != file->private_data);
247 if (icd->streamer != file)
248 return -EBUSY;
250 return videobuf_dqbuf(&icd->vb_vidq, p, file->f_flags & O_NONBLOCK);
253 /* Always entered with .video_lock held */
254 static int soc_camera_init_user_formats(struct soc_camera_device *icd)
256 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
257 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
258 unsigned int i, fmts = 0, raw_fmts = 0;
259 int ret;
260 enum v4l2_mbus_pixelcode code;
262 while (!v4l2_subdev_call(sd, video, enum_mbus_fmt, raw_fmts, &code))
263 raw_fmts++;
265 if (!ici->ops->get_formats)
267 * Fallback mode - the host will have to serve all
268 * sensor-provided formats one-to-one to the user
270 fmts = raw_fmts;
271 else
273 * First pass - only count formats this host-sensor
274 * configuration can provide
276 for (i = 0; i < raw_fmts; i++) {
277 ret = ici->ops->get_formats(icd, i, NULL);
278 if (ret < 0)
279 return ret;
280 fmts += ret;
283 if (!fmts)
284 return -ENXIO;
286 icd->user_formats =
287 vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
288 if (!icd->user_formats)
289 return -ENOMEM;
291 icd->num_user_formats = fmts;
293 dev_dbg(&icd->dev, "Found %d supported formats.\n", fmts);
295 /* Second pass - actually fill data formats */
296 fmts = 0;
297 for (i = 0; i < raw_fmts; i++)
298 if (!ici->ops->get_formats) {
299 v4l2_subdev_call(sd, video, enum_mbus_fmt, i, &code);
300 icd->user_formats[i].host_fmt =
301 soc_mbus_get_fmtdesc(code);
302 icd->user_formats[i].code = code;
303 } else {
304 ret = ici->ops->get_formats(icd, i,
305 &icd->user_formats[fmts]);
306 if (ret < 0)
307 goto egfmt;
308 fmts += ret;
311 icd->current_fmt = &icd->user_formats[0];
313 return 0;
315 egfmt:
316 icd->num_user_formats = 0;
317 vfree(icd->user_formats);
318 return ret;
321 /* Always entered with .video_lock held */
322 static void soc_camera_free_user_formats(struct soc_camera_device *icd)
324 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
326 if (ici->ops->put_formats)
327 ici->ops->put_formats(icd);
328 icd->current_fmt = NULL;
329 icd->num_user_formats = 0;
330 vfree(icd->user_formats);
331 icd->user_formats = NULL;
334 #define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
335 ((x) >> 24) & 0xff
337 /* Called with .vb_lock held, or from the first open(2), see comment there */
338 static int soc_camera_set_fmt(struct soc_camera_device *icd,
339 struct v4l2_format *f)
341 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
342 struct v4l2_pix_format *pix = &f->fmt.pix;
343 int ret;
345 dev_dbg(&icd->dev, "S_FMT(%c%c%c%c, %ux%u)\n",
346 pixfmtstr(pix->pixelformat), pix->width, pix->height);
348 /* We always call try_fmt() before set_fmt() or set_crop() */
349 ret = ici->ops->try_fmt(icd, f);
350 if (ret < 0)
351 return ret;
353 ret = ici->ops->set_fmt(icd, f);
354 if (ret < 0) {
355 return ret;
356 } else if (!icd->current_fmt ||
357 icd->current_fmt->host_fmt->fourcc != pix->pixelformat) {
358 dev_err(&icd->dev,
359 "Host driver hasn't set up current format correctly!\n");
360 return -EINVAL;
363 icd->user_width = pix->width;
364 icd->user_height = pix->height;
365 icd->colorspace = pix->colorspace;
366 icd->vb_vidq.field =
367 icd->field = pix->field;
369 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
370 dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n",
371 f->type);
373 dev_dbg(&icd->dev, "set width: %d height: %d\n",
374 icd->user_width, icd->user_height);
376 /* set physical bus parameters */
377 return ici->ops->set_bus_param(icd, pix->pixelformat);
380 static int soc_camera_open(struct file *file)
382 struct video_device *vdev = video_devdata(file);
383 struct soc_camera_device *icd = container_of(vdev->parent,
384 struct soc_camera_device,
385 dev);
386 struct soc_camera_link *icl = to_soc_camera_link(icd);
387 struct soc_camera_host *ici;
388 int ret;
390 if (!icd->ops)
391 /* No device driver attached */
392 return -ENODEV;
394 ici = to_soc_camera_host(icd->dev.parent);
396 if (!try_module_get(ici->ops->owner)) {
397 dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
398 return -EINVAL;
401 icd->use_count++;
403 /* Now we really have to activate the camera */
404 if (icd->use_count == 1) {
405 /* Restore parameters before the last close() per V4L2 API */
406 struct v4l2_format f = {
407 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
408 .fmt.pix = {
409 .width = icd->user_width,
410 .height = icd->user_height,
411 .field = icd->field,
412 .colorspace = icd->colorspace,
413 .pixelformat =
414 icd->current_fmt->host_fmt->fourcc,
418 ret = soc_camera_power_set(icd, icl, 1);
419 if (ret < 0)
420 goto epower;
422 /* The camera could have been already on, try to reset */
423 if (icl->reset)
424 icl->reset(icd->pdev);
426 ret = ici->ops->add(icd);
427 if (ret < 0) {
428 dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
429 goto eiciadd;
432 pm_runtime_enable(&icd->vdev->dev);
433 ret = pm_runtime_resume(&icd->vdev->dev);
434 if (ret < 0 && ret != -ENOSYS)
435 goto eresume;
438 * Try to configure with default parameters. Notice: this is the
439 * very first open, so, we cannot race against other calls,
440 * apart from someone else calling open() simultaneously, but
441 * .video_lock is protecting us against it.
443 ret = soc_camera_set_fmt(icd, &f);
444 if (ret < 0)
445 goto esfmt;
447 ici->ops->init_videobuf(&icd->vb_vidq, icd);
450 file->private_data = icd;
451 dev_dbg(&icd->dev, "camera device open\n");
453 return 0;
456 * First four errors are entered with the .video_lock held
457 * and use_count == 1
459 esfmt:
460 pm_runtime_disable(&icd->vdev->dev);
461 eresume:
462 ici->ops->remove(icd);
463 eiciadd:
464 soc_camera_power_set(icd, icl, 0);
465 epower:
466 icd->use_count--;
467 module_put(ici->ops->owner);
469 return ret;
472 static int soc_camera_close(struct file *file)
474 struct soc_camera_device *icd = file->private_data;
475 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
477 icd->use_count--;
478 if (!icd->use_count) {
479 struct soc_camera_link *icl = to_soc_camera_link(icd);
481 pm_runtime_suspend(&icd->vdev->dev);
482 pm_runtime_disable(&icd->vdev->dev);
484 ici->ops->remove(icd);
486 soc_camera_power_set(icd, icl, 0);
489 if (icd->streamer == file)
490 icd->streamer = NULL;
492 module_put(ici->ops->owner);
494 dev_dbg(&icd->dev, "camera device close\n");
496 return 0;
499 static ssize_t soc_camera_read(struct file *file, char __user *buf,
500 size_t count, loff_t *ppos)
502 struct soc_camera_device *icd = file->private_data;
503 int err = -EINVAL;
505 dev_err(&icd->dev, "camera device read not implemented\n");
507 return err;
510 static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
512 struct soc_camera_device *icd = file->private_data;
513 int err;
515 dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
517 if (icd->streamer != file)
518 return -EBUSY;
520 err = videobuf_mmap_mapper(&icd->vb_vidq, vma);
522 dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
523 (unsigned long)vma->vm_start,
524 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
525 err);
527 return err;
530 static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
532 struct soc_camera_device *icd = file->private_data;
533 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
535 if (icd->streamer != file)
536 return -EBUSY;
538 if (list_empty(&icd->vb_vidq.stream)) {
539 dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
540 return POLLERR;
543 return ici->ops->poll(file, pt);
546 static struct v4l2_file_operations soc_camera_fops = {
547 .owner = THIS_MODULE,
548 .open = soc_camera_open,
549 .release = soc_camera_close,
550 .unlocked_ioctl = video_ioctl2,
551 .read = soc_camera_read,
552 .mmap = soc_camera_mmap,
553 .poll = soc_camera_poll,
556 static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
557 struct v4l2_format *f)
559 struct soc_camera_device *icd = file->private_data;
560 int ret;
562 WARN_ON(priv != file->private_data);
564 if (icd->streamer && icd->streamer != file)
565 return -EBUSY;
567 if (icd->vb_vidq.bufs[0]) {
568 dev_err(&icd->dev, "S_FMT denied: queue initialised\n");
569 return -EBUSY;
572 ret = soc_camera_set_fmt(icd, f);
574 if (!ret && !icd->streamer)
575 icd->streamer = file;
577 return ret;
580 static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
581 struct v4l2_fmtdesc *f)
583 struct soc_camera_device *icd = file->private_data;
584 const struct soc_mbus_pixelfmt *format;
586 WARN_ON(priv != file->private_data);
588 if (f->index >= icd->num_user_formats)
589 return -EINVAL;
591 format = icd->user_formats[f->index].host_fmt;
593 if (format->name)
594 strlcpy(f->description, format->name, sizeof(f->description));
595 f->pixelformat = format->fourcc;
596 return 0;
599 static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
600 struct v4l2_format *f)
602 struct soc_camera_device *icd = file->private_data;
603 struct v4l2_pix_format *pix = &f->fmt.pix;
605 WARN_ON(priv != file->private_data);
607 pix->width = icd->user_width;
608 pix->height = icd->user_height;
609 pix->field = icd->vb_vidq.field;
610 pix->pixelformat = icd->current_fmt->host_fmt->fourcc;
611 pix->bytesperline = soc_mbus_bytes_per_line(pix->width,
612 icd->current_fmt->host_fmt);
613 pix->colorspace = icd->colorspace;
614 if (pix->bytesperline < 0)
615 return pix->bytesperline;
616 pix->sizeimage = pix->height * pix->bytesperline;
617 dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
618 icd->current_fmt->host_fmt->fourcc);
619 return 0;
622 static int soc_camera_querycap(struct file *file, void *priv,
623 struct v4l2_capability *cap)
625 struct soc_camera_device *icd = file->private_data;
626 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
628 WARN_ON(priv != file->private_data);
630 strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
631 return ici->ops->querycap(ici, cap);
634 static int soc_camera_streamon(struct file *file, void *priv,
635 enum v4l2_buf_type i)
637 struct soc_camera_device *icd = file->private_data;
638 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
639 int ret;
641 WARN_ON(priv != file->private_data);
643 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
644 return -EINVAL;
646 if (icd->streamer != file)
647 return -EBUSY;
649 v4l2_subdev_call(sd, video, s_stream, 1);
651 /* This calls buf_queue from host driver's videobuf_queue_ops */
652 ret = videobuf_streamon(&icd->vb_vidq);
654 return ret;
657 static int soc_camera_streamoff(struct file *file, void *priv,
658 enum v4l2_buf_type i)
660 struct soc_camera_device *icd = file->private_data;
661 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
663 WARN_ON(priv != file->private_data);
665 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
666 return -EINVAL;
668 if (icd->streamer != file)
669 return -EBUSY;
672 * This calls buf_release from host driver's videobuf_queue_ops for all
673 * remaining buffers. When the last buffer is freed, stop capture
675 videobuf_streamoff(&icd->vb_vidq);
677 v4l2_subdev_call(sd, video, s_stream, 0);
679 return 0;
682 static int soc_camera_queryctrl(struct file *file, void *priv,
683 struct v4l2_queryctrl *qc)
685 struct soc_camera_device *icd = file->private_data;
686 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
687 int i;
689 WARN_ON(priv != file->private_data);
691 if (!qc->id)
692 return -EINVAL;
694 /* First check host controls */
695 for (i = 0; i < ici->ops->num_controls; i++)
696 if (qc->id == ici->ops->controls[i].id) {
697 memcpy(qc, &(ici->ops->controls[i]),
698 sizeof(*qc));
699 return 0;
702 /* Then device controls */
703 for (i = 0; i < icd->ops->num_controls; i++)
704 if (qc->id == icd->ops->controls[i].id) {
705 memcpy(qc, &(icd->ops->controls[i]),
706 sizeof(*qc));
707 return 0;
710 return -EINVAL;
713 static int soc_camera_g_ctrl(struct file *file, void *priv,
714 struct v4l2_control *ctrl)
716 struct soc_camera_device *icd = file->private_data;
717 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
718 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
719 int ret;
721 WARN_ON(priv != file->private_data);
723 if (ici->ops->get_ctrl) {
724 ret = ici->ops->get_ctrl(icd, ctrl);
725 if (ret != -ENOIOCTLCMD)
726 return ret;
729 return v4l2_subdev_call(sd, core, g_ctrl, ctrl);
732 static int soc_camera_s_ctrl(struct file *file, void *priv,
733 struct v4l2_control *ctrl)
735 struct soc_camera_device *icd = file->private_data;
736 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
737 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
738 int ret;
740 WARN_ON(priv != file->private_data);
742 if (ici->ops->set_ctrl) {
743 ret = ici->ops->set_ctrl(icd, ctrl);
744 if (ret != -ENOIOCTLCMD)
745 return ret;
748 return v4l2_subdev_call(sd, core, s_ctrl, ctrl);
751 static int soc_camera_cropcap(struct file *file, void *fh,
752 struct v4l2_cropcap *a)
754 struct soc_camera_device *icd = file->private_data;
755 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
757 return ici->ops->cropcap(icd, a);
760 static int soc_camera_g_crop(struct file *file, void *fh,
761 struct v4l2_crop *a)
763 struct soc_camera_device *icd = file->private_data;
764 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
765 int ret;
767 ret = ici->ops->get_crop(icd, a);
769 return ret;
773 * According to the V4L2 API, drivers shall not update the struct v4l2_crop
774 * argument with the actual geometry, instead, the user shall use G_CROP to
775 * retrieve it.
777 static int soc_camera_s_crop(struct file *file, void *fh,
778 struct v4l2_crop *a)
780 struct soc_camera_device *icd = file->private_data;
781 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
782 struct v4l2_rect *rect = &a->c;
783 struct v4l2_crop current_crop;
784 int ret;
786 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
787 return -EINVAL;
789 dev_dbg(&icd->dev, "S_CROP(%ux%u@%u:%u)\n",
790 rect->width, rect->height, rect->left, rect->top);
792 /* If get_crop fails, we'll let host and / or client drivers decide */
793 ret = ici->ops->get_crop(icd, &current_crop);
795 /* Prohibit window size change with initialised buffers */
796 if (ret < 0) {
797 dev_err(&icd->dev,
798 "S_CROP denied: getting current crop failed\n");
799 } else if (icd->vb_vidq.bufs[0] &&
800 (a->c.width != current_crop.c.width ||
801 a->c.height != current_crop.c.height)) {
802 dev_err(&icd->dev,
803 "S_CROP denied: queue initialised and sizes differ\n");
804 ret = -EBUSY;
805 } else {
806 ret = ici->ops->set_crop(icd, a);
809 return ret;
812 static int soc_camera_g_parm(struct file *file, void *fh,
813 struct v4l2_streamparm *a)
815 struct soc_camera_device *icd = file->private_data;
816 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
818 if (ici->ops->get_parm)
819 return ici->ops->get_parm(icd, a);
821 return -ENOIOCTLCMD;
824 static int soc_camera_s_parm(struct file *file, void *fh,
825 struct v4l2_streamparm *a)
827 struct soc_camera_device *icd = file->private_data;
828 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
830 if (ici->ops->set_parm)
831 return ici->ops->set_parm(icd, a);
833 return -ENOIOCTLCMD;
836 static int soc_camera_g_chip_ident(struct file *file, void *fh,
837 struct v4l2_dbg_chip_ident *id)
839 struct soc_camera_device *icd = file->private_data;
840 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
842 return v4l2_subdev_call(sd, core, g_chip_ident, id);
845 #ifdef CONFIG_VIDEO_ADV_DEBUG
846 static int soc_camera_g_register(struct file *file, void *fh,
847 struct v4l2_dbg_register *reg)
849 struct soc_camera_device *icd = file->private_data;
850 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
852 return v4l2_subdev_call(sd, core, g_register, reg);
855 static int soc_camera_s_register(struct file *file, void *fh,
856 struct v4l2_dbg_register *reg)
858 struct soc_camera_device *icd = file->private_data;
859 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
861 return v4l2_subdev_call(sd, core, s_register, reg);
863 #endif
865 /* So far this function cannot fail */
866 static void scan_add_host(struct soc_camera_host *ici)
868 struct soc_camera_device *icd;
870 mutex_lock(&list_lock);
872 list_for_each_entry(icd, &devices, list) {
873 if (icd->iface == ici->nr) {
874 int ret;
875 icd->dev.parent = ici->v4l2_dev.dev;
876 dev_set_name(&icd->dev, "%u-%u", icd->iface,
877 icd->devnum);
878 ret = device_register(&icd->dev);
879 if (ret < 0) {
880 icd->dev.parent = NULL;
881 dev_err(&icd->dev,
882 "Cannot register device: %d\n", ret);
887 mutex_unlock(&list_lock);
890 #ifdef CONFIG_I2C_BOARDINFO
891 static int soc_camera_init_i2c(struct soc_camera_device *icd,
892 struct soc_camera_link *icl)
894 struct i2c_client *client;
895 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
896 struct i2c_adapter *adap = i2c_get_adapter(icl->i2c_adapter_id);
897 struct v4l2_subdev *subdev;
899 if (!adap) {
900 dev_err(&icd->dev, "Cannot get I2C adapter #%d. No driver?\n",
901 icl->i2c_adapter_id);
902 goto ei2cga;
905 icl->board_info->platform_data = icd;
907 subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
908 icl->board_info, NULL);
909 if (!subdev)
910 goto ei2cnd;
912 client = v4l2_get_subdevdata(subdev);
914 /* Use to_i2c_client(dev) to recover the i2c client */
915 dev_set_drvdata(&icd->dev, &client->dev);
917 return 0;
918 ei2cnd:
919 i2c_put_adapter(adap);
920 ei2cga:
921 return -ENODEV;
924 static void soc_camera_free_i2c(struct soc_camera_device *icd)
926 struct i2c_client *client =
927 to_i2c_client(to_soc_camera_control(icd));
928 dev_set_drvdata(&icd->dev, NULL);
929 v4l2_device_unregister_subdev(i2c_get_clientdata(client));
930 i2c_unregister_device(client);
931 i2c_put_adapter(client->adapter);
933 #else
934 #define soc_camera_init_i2c(icd, icl) (-ENODEV)
935 #define soc_camera_free_i2c(icd) do {} while (0)
936 #endif
938 static int soc_camera_video_start(struct soc_camera_device *icd);
939 static int video_dev_create(struct soc_camera_device *icd);
940 /* Called during host-driver probe */
941 static int soc_camera_probe(struct device *dev)
943 struct soc_camera_device *icd = to_soc_camera_dev(dev);
944 struct soc_camera_host *ici = to_soc_camera_host(dev->parent);
945 struct soc_camera_link *icl = to_soc_camera_link(icd);
946 struct device *control = NULL;
947 struct v4l2_subdev *sd;
948 struct v4l2_mbus_framefmt mf;
949 int ret;
951 dev_info(dev, "Probing %s\n", dev_name(dev));
953 ret = regulator_bulk_get(icd->pdev, icl->num_regulators,
954 icl->regulators);
955 if (ret < 0)
956 goto ereg;
958 ret = soc_camera_power_set(icd, icl, 1);
959 if (ret < 0)
960 goto epower;
962 /* The camera could have been already on, try to reset */
963 if (icl->reset)
964 icl->reset(icd->pdev);
966 ret = ici->ops->add(icd);
967 if (ret < 0)
968 goto eadd;
970 /* Must have icd->vdev before registering the device */
971 ret = video_dev_create(icd);
972 if (ret < 0)
973 goto evdc;
975 /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
976 if (icl->board_info) {
977 ret = soc_camera_init_i2c(icd, icl);
978 if (ret < 0)
979 goto eadddev;
980 } else if (!icl->add_device || !icl->del_device) {
981 ret = -EINVAL;
982 goto eadddev;
983 } else {
984 if (icl->module_name)
985 ret = request_module(icl->module_name);
987 ret = icl->add_device(icl, &icd->dev);
988 if (ret < 0)
989 goto eadddev;
992 * FIXME: this is racy, have to use driver-binding notification,
993 * when it is available
995 control = to_soc_camera_control(icd);
996 if (!control || !control->driver || !dev_get_drvdata(control) ||
997 !try_module_get(control->driver->owner)) {
998 icl->del_device(icl);
999 goto enodrv;
1003 /* At this point client .probe() should have run already */
1004 ret = soc_camera_init_user_formats(icd);
1005 if (ret < 0)
1006 goto eiufmt;
1008 icd->field = V4L2_FIELD_ANY;
1010 icd->vdev->lock = &icd->video_lock;
1013 * ..._video_start() will create a device node, video_register_device()
1014 * itself is protected against concurrent open() calls, but we also have
1015 * to protect our data.
1017 mutex_lock(&icd->video_lock);
1019 ret = soc_camera_video_start(icd);
1020 if (ret < 0)
1021 goto evidstart;
1023 /* Try to improve our guess of a reasonable window format */
1024 sd = soc_camera_to_subdev(icd);
1025 if (!v4l2_subdev_call(sd, video, g_mbus_fmt, &mf)) {
1026 icd->user_width = mf.width;
1027 icd->user_height = mf.height;
1028 icd->colorspace = mf.colorspace;
1029 icd->field = mf.field;
1032 /* Do we have to sysfs_remove_link() before device_unregister()? */
1033 if (sysfs_create_link(&icd->dev.kobj, &to_soc_camera_control(icd)->kobj,
1034 "control"))
1035 dev_warn(&icd->dev, "Failed creating the control symlink\n");
1037 ici->ops->remove(icd);
1039 soc_camera_power_set(icd, icl, 0);
1041 mutex_unlock(&icd->video_lock);
1043 return 0;
1045 evidstart:
1046 mutex_unlock(&icd->video_lock);
1047 soc_camera_free_user_formats(icd);
1048 eiufmt:
1049 if (icl->board_info) {
1050 soc_camera_free_i2c(icd);
1051 } else {
1052 icl->del_device(icl);
1053 module_put(control->driver->owner);
1055 enodrv:
1056 eadddev:
1057 video_device_release(icd->vdev);
1058 evdc:
1059 ici->ops->remove(icd);
1060 eadd:
1061 soc_camera_power_set(icd, icl, 0);
1062 epower:
1063 regulator_bulk_free(icl->num_regulators, icl->regulators);
1064 ereg:
1065 return ret;
1069 * This is called on device_unregister, which only means we have to disconnect
1070 * from the host, but not remove ourselves from the device list
1072 static int soc_camera_remove(struct device *dev)
1074 struct soc_camera_device *icd = to_soc_camera_dev(dev);
1075 struct soc_camera_link *icl = to_soc_camera_link(icd);
1076 struct video_device *vdev = icd->vdev;
1078 BUG_ON(!dev->parent);
1080 if (vdev) {
1081 video_unregister_device(vdev);
1082 icd->vdev = NULL;
1085 if (icl->board_info) {
1086 soc_camera_free_i2c(icd);
1087 } else {
1088 struct device_driver *drv = to_soc_camera_control(icd) ?
1089 to_soc_camera_control(icd)->driver : NULL;
1090 if (drv) {
1091 icl->del_device(icl);
1092 module_put(drv->owner);
1095 soc_camera_free_user_formats(icd);
1097 regulator_bulk_free(icl->num_regulators, icl->regulators);
1099 return 0;
1102 static int soc_camera_suspend(struct device *dev, pm_message_t state)
1104 struct soc_camera_device *icd = to_soc_camera_dev(dev);
1105 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1106 int ret = 0;
1108 if (ici->ops->suspend)
1109 ret = ici->ops->suspend(icd, state);
1111 return ret;
1114 static int soc_camera_resume(struct device *dev)
1116 struct soc_camera_device *icd = to_soc_camera_dev(dev);
1117 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1118 int ret = 0;
1120 if (ici->ops->resume)
1121 ret = ici->ops->resume(icd);
1123 return ret;
1126 struct bus_type soc_camera_bus_type = {
1127 .name = "soc-camera",
1128 .probe = soc_camera_probe,
1129 .remove = soc_camera_remove,
1130 .suspend = soc_camera_suspend,
1131 .resume = soc_camera_resume,
1133 EXPORT_SYMBOL_GPL(soc_camera_bus_type);
1135 static struct device_driver ic_drv = {
1136 .name = "camera",
1137 .bus = &soc_camera_bus_type,
1138 .owner = THIS_MODULE,
1141 static void dummy_release(struct device *dev)
1145 static int default_cropcap(struct soc_camera_device *icd,
1146 struct v4l2_cropcap *a)
1148 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1149 return v4l2_subdev_call(sd, video, cropcap, a);
1152 static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1154 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1155 return v4l2_subdev_call(sd, video, g_crop, a);
1158 static int default_s_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1160 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1161 return v4l2_subdev_call(sd, video, s_crop, a);
1164 static int default_g_parm(struct soc_camera_device *icd,
1165 struct v4l2_streamparm *parm)
1167 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1168 return v4l2_subdev_call(sd, video, g_parm, parm);
1171 static int default_s_parm(struct soc_camera_device *icd,
1172 struct v4l2_streamparm *parm)
1174 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1175 return v4l2_subdev_call(sd, video, s_parm, parm);
1178 static void soc_camera_device_init(struct device *dev, void *pdata)
1180 dev->platform_data = pdata;
1181 dev->bus = &soc_camera_bus_type;
1182 dev->release = dummy_release;
1185 int soc_camera_host_register(struct soc_camera_host *ici)
1187 struct soc_camera_host *ix;
1188 int ret;
1190 if (!ici || !ici->ops ||
1191 !ici->ops->try_fmt ||
1192 !ici->ops->set_fmt ||
1193 !ici->ops->set_bus_param ||
1194 !ici->ops->querycap ||
1195 !ici->ops->init_videobuf ||
1196 !ici->ops->reqbufs ||
1197 !ici->ops->add ||
1198 !ici->ops->remove ||
1199 !ici->ops->poll ||
1200 !ici->v4l2_dev.dev)
1201 return -EINVAL;
1203 if (!ici->ops->set_crop)
1204 ici->ops->set_crop = default_s_crop;
1205 if (!ici->ops->get_crop)
1206 ici->ops->get_crop = default_g_crop;
1207 if (!ici->ops->cropcap)
1208 ici->ops->cropcap = default_cropcap;
1209 if (!ici->ops->set_parm)
1210 ici->ops->set_parm = default_s_parm;
1211 if (!ici->ops->get_parm)
1212 ici->ops->get_parm = default_g_parm;
1214 mutex_lock(&list_lock);
1215 list_for_each_entry(ix, &hosts, list) {
1216 if (ix->nr == ici->nr) {
1217 ret = -EBUSY;
1218 goto edevreg;
1222 ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
1223 if (ret < 0)
1224 goto edevreg;
1226 list_add_tail(&ici->list, &hosts);
1227 mutex_unlock(&list_lock);
1229 scan_add_host(ici);
1231 return 0;
1233 edevreg:
1234 mutex_unlock(&list_lock);
1235 return ret;
1237 EXPORT_SYMBOL(soc_camera_host_register);
1239 /* Unregister all clients! */
1240 void soc_camera_host_unregister(struct soc_camera_host *ici)
1242 struct soc_camera_device *icd;
1244 mutex_lock(&list_lock);
1246 list_del(&ici->list);
1248 list_for_each_entry(icd, &devices, list) {
1249 if (icd->iface == ici->nr) {
1250 void *pdata = icd->dev.platform_data;
1251 /* The bus->remove will be called */
1252 device_unregister(&icd->dev);
1254 * Not before device_unregister(), .remove
1255 * needs parent to call ici->ops->remove().
1256 * If the host module is loaded again, device_register()
1257 * would complain "already initialised," since 2.6.32
1258 * this is also needed to prevent use-after-free of the
1259 * device private data.
1261 memset(&icd->dev, 0, sizeof(icd->dev));
1262 soc_camera_device_init(&icd->dev, pdata);
1266 mutex_unlock(&list_lock);
1268 v4l2_device_unregister(&ici->v4l2_dev);
1270 EXPORT_SYMBOL(soc_camera_host_unregister);
1272 /* Image capture device */
1273 static int soc_camera_device_register(struct soc_camera_device *icd)
1275 struct soc_camera_device *ix;
1276 int num = -1, i;
1278 for (i = 0; i < 256 && num < 0; i++) {
1279 num = i;
1280 /* Check if this index is available on this interface */
1281 list_for_each_entry(ix, &devices, list) {
1282 if (ix->iface == icd->iface && ix->devnum == i) {
1283 num = -1;
1284 break;
1289 if (num < 0)
1291 * ok, we have 256 cameras on this host...
1292 * man, stay reasonable...
1294 return -ENOMEM;
1296 icd->devnum = num;
1297 icd->use_count = 0;
1298 icd->host_priv = NULL;
1299 mutex_init(&icd->video_lock);
1301 list_add_tail(&icd->list, &devices);
1303 return 0;
1306 static void soc_camera_device_unregister(struct soc_camera_device *icd)
1308 list_del(&icd->list);
1311 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
1312 .vidioc_querycap = soc_camera_querycap,
1313 .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
1314 .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
1315 .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
1316 .vidioc_enum_input = soc_camera_enum_input,
1317 .vidioc_g_input = soc_camera_g_input,
1318 .vidioc_s_input = soc_camera_s_input,
1319 .vidioc_s_std = soc_camera_s_std,
1320 .vidioc_reqbufs = soc_camera_reqbufs,
1321 .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
1322 .vidioc_querybuf = soc_camera_querybuf,
1323 .vidioc_qbuf = soc_camera_qbuf,
1324 .vidioc_dqbuf = soc_camera_dqbuf,
1325 .vidioc_streamon = soc_camera_streamon,
1326 .vidioc_streamoff = soc_camera_streamoff,
1327 .vidioc_queryctrl = soc_camera_queryctrl,
1328 .vidioc_g_ctrl = soc_camera_g_ctrl,
1329 .vidioc_s_ctrl = soc_camera_s_ctrl,
1330 .vidioc_cropcap = soc_camera_cropcap,
1331 .vidioc_g_crop = soc_camera_g_crop,
1332 .vidioc_s_crop = soc_camera_s_crop,
1333 .vidioc_g_parm = soc_camera_g_parm,
1334 .vidioc_s_parm = soc_camera_s_parm,
1335 .vidioc_g_chip_ident = soc_camera_g_chip_ident,
1336 #ifdef CONFIG_VIDEO_ADV_DEBUG
1337 .vidioc_g_register = soc_camera_g_register,
1338 .vidioc_s_register = soc_camera_s_register,
1339 #endif
1342 static int video_dev_create(struct soc_camera_device *icd)
1344 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1345 struct video_device *vdev = video_device_alloc();
1347 if (!vdev)
1348 return -ENOMEM;
1350 strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
1352 vdev->parent = &icd->dev;
1353 vdev->current_norm = V4L2_STD_UNKNOWN;
1354 vdev->fops = &soc_camera_fops;
1355 vdev->ioctl_ops = &soc_camera_ioctl_ops;
1356 vdev->release = video_device_release;
1357 vdev->tvnorms = V4L2_STD_UNKNOWN;
1359 icd->vdev = vdev;
1361 return 0;
1365 * Called from soc_camera_probe() above (with .video_lock held???)
1367 static int soc_camera_video_start(struct soc_camera_device *icd)
1369 struct device_type *type = icd->vdev->dev.type;
1370 int ret;
1372 if (!icd->dev.parent)
1373 return -ENODEV;
1375 if (!icd->ops ||
1376 !icd->ops->query_bus_param ||
1377 !icd->ops->set_bus_param)
1378 return -EINVAL;
1380 ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
1381 if (ret < 0) {
1382 dev_err(&icd->dev, "video_register_device failed: %d\n", ret);
1383 return ret;
1386 /* Restore device type, possibly set by the subdevice driver */
1387 icd->vdev->dev.type = type;
1389 return 0;
1392 static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev)
1394 struct soc_camera_link *icl = pdev->dev.platform_data;
1395 struct soc_camera_device *icd;
1396 int ret;
1398 if (!icl)
1399 return -EINVAL;
1401 icd = kzalloc(sizeof(*icd), GFP_KERNEL);
1402 if (!icd)
1403 return -ENOMEM;
1405 icd->iface = icl->bus_id;
1406 icd->pdev = &pdev->dev;
1407 platform_set_drvdata(pdev, icd);
1409 ret = soc_camera_device_register(icd);
1410 if (ret < 0)
1411 goto escdevreg;
1413 soc_camera_device_init(&icd->dev, icl);
1415 icd->user_width = DEFAULT_WIDTH;
1416 icd->user_height = DEFAULT_HEIGHT;
1418 return 0;
1420 escdevreg:
1421 kfree(icd);
1423 return ret;
1427 * Only called on rmmod for each platform device, since they are not
1428 * hot-pluggable. Now we know, that all our users - hosts and devices have
1429 * been unloaded already
1431 static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev)
1433 struct soc_camera_device *icd = platform_get_drvdata(pdev);
1435 if (!icd)
1436 return -EINVAL;
1438 soc_camera_device_unregister(icd);
1440 kfree(icd);
1442 return 0;
1445 static struct platform_driver __refdata soc_camera_pdrv = {
1446 .remove = __devexit_p(soc_camera_pdrv_remove),
1447 .driver = {
1448 .name = "soc-camera-pdrv",
1449 .owner = THIS_MODULE,
1453 static int __init soc_camera_init(void)
1455 int ret = bus_register(&soc_camera_bus_type);
1456 if (ret)
1457 return ret;
1458 ret = driver_register(&ic_drv);
1459 if (ret)
1460 goto edrvr;
1462 ret = platform_driver_probe(&soc_camera_pdrv, soc_camera_pdrv_probe);
1463 if (ret)
1464 goto epdr;
1466 return 0;
1468 epdr:
1469 driver_unregister(&ic_drv);
1470 edrvr:
1471 bus_unregister(&soc_camera_bus_type);
1472 return ret;
1475 static void __exit soc_camera_exit(void)
1477 platform_driver_unregister(&soc_camera_pdrv);
1478 driver_unregister(&ic_drv);
1479 bus_unregister(&soc_camera_bus_type);
1482 module_init(soc_camera_init);
1483 module_exit(soc_camera_exit);
1485 MODULE_DESCRIPTION("Image capture bus driver");
1486 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1487 MODULE_LICENSE("GPL");
1488 MODULE_ALIAS("platform:soc-camera-pdrv");