sfc: Don't use enums as a bitmask.
[zen-stable.git] / drivers / media / video / soc_camera.c
blob46284489e4eb8b89e96e354e4defca8bd1710ebd
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/videobuf2-core.h>
38 #include <media/soc_mediabus.h>
40 /* Default to VGA resolution */
41 #define DEFAULT_WIDTH 640
42 #define DEFAULT_HEIGHT 480
44 static LIST_HEAD(hosts);
45 static LIST_HEAD(devices);
46 static DEFINE_MUTEX(list_lock); /* Protects the list of hosts */
48 static int soc_camera_power_set(struct soc_camera_device *icd,
49 struct soc_camera_link *icl,
50 int power_on)
52 int ret;
54 if (power_on) {
55 ret = regulator_bulk_enable(icl->num_regulators,
56 icl->regulators);
57 if (ret < 0) {
58 dev_err(&icd->dev, "Cannot enable regulators\n");
59 return ret;
62 if (icl->power)
63 ret = icl->power(icd->pdev, power_on);
64 if (ret < 0) {
65 dev_err(&icd->dev,
66 "Platform failed to power-on the camera.\n");
68 regulator_bulk_disable(icl->num_regulators,
69 icl->regulators);
70 return ret;
72 } else {
73 ret = 0;
74 if (icl->power)
75 ret = icl->power(icd->pdev, 0);
76 if (ret < 0) {
77 dev_err(&icd->dev,
78 "Platform failed to power-off the camera.\n");
79 return ret;
82 ret = regulator_bulk_disable(icl->num_regulators,
83 icl->regulators);
84 if (ret < 0) {
85 dev_err(&icd->dev, "Cannot disable regulators\n");
86 return ret;
90 return 0;
93 const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
94 struct soc_camera_device *icd, unsigned int fourcc)
96 unsigned int i;
98 for (i = 0; i < icd->num_user_formats; i++)
99 if (icd->user_formats[i].host_fmt->fourcc == fourcc)
100 return icd->user_formats + i;
101 return NULL;
103 EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);
106 * soc_camera_apply_sensor_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
107 * @icl: camera platform parameters
108 * @flags: flags to be inverted according to platform configuration
109 * @return: resulting flags
111 unsigned long soc_camera_apply_sensor_flags(struct soc_camera_link *icl,
112 unsigned long flags)
114 unsigned long f;
116 /* If only one of the two polarities is supported, switch to the opposite */
117 if (icl->flags & SOCAM_SENSOR_INVERT_HSYNC) {
118 f = flags & (SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW);
119 if (f == SOCAM_HSYNC_ACTIVE_HIGH || f == SOCAM_HSYNC_ACTIVE_LOW)
120 flags ^= SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW;
123 if (icl->flags & SOCAM_SENSOR_INVERT_VSYNC) {
124 f = flags & (SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW);
125 if (f == SOCAM_VSYNC_ACTIVE_HIGH || f == SOCAM_VSYNC_ACTIVE_LOW)
126 flags ^= SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW;
129 if (icl->flags & SOCAM_SENSOR_INVERT_PCLK) {
130 f = flags & (SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING);
131 if (f == SOCAM_PCLK_SAMPLE_RISING || f == SOCAM_PCLK_SAMPLE_FALLING)
132 flags ^= SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING;
135 return flags;
137 EXPORT_SYMBOL(soc_camera_apply_sensor_flags);
139 static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
140 struct v4l2_format *f)
142 struct soc_camera_device *icd = file->private_data;
143 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
145 WARN_ON(priv != file->private_data);
147 /* Only single-plane capture is supported so far */
148 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
149 return -EINVAL;
151 /* limit format to hardware capabilities */
152 return ici->ops->try_fmt(icd, f);
155 static int soc_camera_enum_input(struct file *file, void *priv,
156 struct v4l2_input *inp)
158 struct soc_camera_device *icd = file->private_data;
159 int ret = 0;
161 if (inp->index != 0)
162 return -EINVAL;
164 if (icd->ops->enum_input)
165 ret = icd->ops->enum_input(icd, inp);
166 else {
167 /* default is camera */
168 inp->type = V4L2_INPUT_TYPE_CAMERA;
169 inp->std = V4L2_STD_UNKNOWN;
170 strcpy(inp->name, "Camera");
173 return ret;
176 static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
178 *i = 0;
180 return 0;
183 static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
185 if (i > 0)
186 return -EINVAL;
188 return 0;
191 static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
193 struct soc_camera_device *icd = file->private_data;
194 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
196 return v4l2_subdev_call(sd, core, s_std, *a);
199 static int soc_camera_enum_fsizes(struct file *file, void *fh,
200 struct v4l2_frmsizeenum *fsize)
202 struct soc_camera_device *icd = file->private_data;
203 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
205 return ici->ops->enum_fsizes(icd, fsize);
208 static int soc_camera_reqbufs(struct file *file, void *priv,
209 struct v4l2_requestbuffers *p)
211 int ret;
212 struct soc_camera_device *icd = file->private_data;
213 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
215 WARN_ON(priv != file->private_data);
217 if (icd->streamer && icd->streamer != file)
218 return -EBUSY;
220 if (ici->ops->init_videobuf) {
221 ret = videobuf_reqbufs(&icd->vb_vidq, p);
222 if (ret < 0)
223 return ret;
225 ret = ici->ops->reqbufs(icd, p);
226 } else {
227 ret = vb2_reqbufs(&icd->vb2_vidq, p);
230 if (!ret && !icd->streamer)
231 icd->streamer = file;
233 return ret;
236 static int soc_camera_querybuf(struct file *file, void *priv,
237 struct v4l2_buffer *p)
239 struct soc_camera_device *icd = file->private_data;
240 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
242 WARN_ON(priv != file->private_data);
244 if (ici->ops->init_videobuf)
245 return videobuf_querybuf(&icd->vb_vidq, p);
246 else
247 return vb2_querybuf(&icd->vb2_vidq, p);
250 static int soc_camera_qbuf(struct file *file, void *priv,
251 struct v4l2_buffer *p)
253 struct soc_camera_device *icd = file->private_data;
254 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
256 WARN_ON(priv != file->private_data);
258 if (icd->streamer != file)
259 return -EBUSY;
261 if (ici->ops->init_videobuf)
262 return videobuf_qbuf(&icd->vb_vidq, p);
263 else
264 return vb2_qbuf(&icd->vb2_vidq, p);
267 static int soc_camera_dqbuf(struct file *file, void *priv,
268 struct v4l2_buffer *p)
270 struct soc_camera_device *icd = file->private_data;
271 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
273 WARN_ON(priv != file->private_data);
275 if (icd->streamer != file)
276 return -EBUSY;
278 if (ici->ops->init_videobuf)
279 return videobuf_dqbuf(&icd->vb_vidq, p, file->f_flags & O_NONBLOCK);
280 else
281 return vb2_dqbuf(&icd->vb2_vidq, p, file->f_flags & O_NONBLOCK);
284 /* Always entered with .video_lock held */
285 static int soc_camera_init_user_formats(struct soc_camera_device *icd)
287 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
288 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
289 unsigned int i, fmts = 0, raw_fmts = 0;
290 int ret;
291 enum v4l2_mbus_pixelcode code;
293 while (!v4l2_subdev_call(sd, video, enum_mbus_fmt, raw_fmts, &code))
294 raw_fmts++;
296 if (!ici->ops->get_formats)
298 * Fallback mode - the host will have to serve all
299 * sensor-provided formats one-to-one to the user
301 fmts = raw_fmts;
302 else
304 * First pass - only count formats this host-sensor
305 * configuration can provide
307 for (i = 0; i < raw_fmts; i++) {
308 ret = ici->ops->get_formats(icd, i, NULL);
309 if (ret < 0)
310 return ret;
311 fmts += ret;
314 if (!fmts)
315 return -ENXIO;
317 icd->user_formats =
318 vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
319 if (!icd->user_formats)
320 return -ENOMEM;
322 icd->num_user_formats = fmts;
324 dev_dbg(&icd->dev, "Found %d supported formats.\n", fmts);
326 /* Second pass - actually fill data formats */
327 fmts = 0;
328 for (i = 0; i < raw_fmts; i++)
329 if (!ici->ops->get_formats) {
330 v4l2_subdev_call(sd, video, enum_mbus_fmt, i, &code);
331 icd->user_formats[i].host_fmt =
332 soc_mbus_get_fmtdesc(code);
333 icd->user_formats[i].code = code;
334 } else {
335 ret = ici->ops->get_formats(icd, i,
336 &icd->user_formats[fmts]);
337 if (ret < 0)
338 goto egfmt;
339 fmts += ret;
342 icd->current_fmt = &icd->user_formats[0];
344 return 0;
346 egfmt:
347 icd->num_user_formats = 0;
348 vfree(icd->user_formats);
349 return ret;
352 /* Always entered with .video_lock held */
353 static void soc_camera_free_user_formats(struct soc_camera_device *icd)
355 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
357 if (ici->ops->put_formats)
358 ici->ops->put_formats(icd);
359 icd->current_fmt = NULL;
360 icd->num_user_formats = 0;
361 vfree(icd->user_formats);
362 icd->user_formats = NULL;
365 #define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
366 ((x) >> 24) & 0xff
368 /* Called with .vb_lock held, or from the first open(2), see comment there */
369 static int soc_camera_set_fmt(struct soc_camera_device *icd,
370 struct v4l2_format *f)
372 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
373 struct v4l2_pix_format *pix = &f->fmt.pix;
374 int ret;
376 dev_dbg(&icd->dev, "S_FMT(%c%c%c%c, %ux%u)\n",
377 pixfmtstr(pix->pixelformat), pix->width, pix->height);
379 /* We always call try_fmt() before set_fmt() or set_crop() */
380 ret = ici->ops->try_fmt(icd, f);
381 if (ret < 0)
382 return ret;
384 ret = ici->ops->set_fmt(icd, f);
385 if (ret < 0) {
386 return ret;
387 } else if (!icd->current_fmt ||
388 icd->current_fmt->host_fmt->fourcc != pix->pixelformat) {
389 dev_err(&icd->dev,
390 "Host driver hasn't set up current format correctly!\n");
391 return -EINVAL;
394 icd->user_width = pix->width;
395 icd->user_height = pix->height;
396 icd->bytesperline = pix->bytesperline;
397 icd->sizeimage = pix->sizeimage;
398 icd->colorspace = pix->colorspace;
399 icd->field = pix->field;
400 if (ici->ops->init_videobuf)
401 icd->vb_vidq.field = pix->field;
403 dev_dbg(&icd->dev, "set width: %d height: %d\n",
404 icd->user_width, icd->user_height);
406 /* set physical bus parameters */
407 return ici->ops->set_bus_param(icd, pix->pixelformat);
410 static int soc_camera_open(struct file *file)
412 struct video_device *vdev = video_devdata(file);
413 struct soc_camera_device *icd = container_of(vdev->parent,
414 struct soc_camera_device,
415 dev);
416 struct soc_camera_link *icl = to_soc_camera_link(icd);
417 struct soc_camera_host *ici;
418 int ret;
420 if (!icd->ops)
421 /* No device driver attached */
422 return -ENODEV;
424 ici = to_soc_camera_host(icd->dev.parent);
426 if (!try_module_get(ici->ops->owner)) {
427 dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
428 return -EINVAL;
431 icd->use_count++;
433 /* Now we really have to activate the camera */
434 if (icd->use_count == 1) {
435 /* Restore parameters before the last close() per V4L2 API */
436 struct v4l2_format f = {
437 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
438 .fmt.pix = {
439 .width = icd->user_width,
440 .height = icd->user_height,
441 .field = icd->field,
442 .colorspace = icd->colorspace,
443 .pixelformat =
444 icd->current_fmt->host_fmt->fourcc,
448 ret = soc_camera_power_set(icd, icl, 1);
449 if (ret < 0)
450 goto epower;
452 /* The camera could have been already on, try to reset */
453 if (icl->reset)
454 icl->reset(icd->pdev);
456 ret = ici->ops->add(icd);
457 if (ret < 0) {
458 dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
459 goto eiciadd;
462 pm_runtime_enable(&icd->vdev->dev);
463 ret = pm_runtime_resume(&icd->vdev->dev);
464 if (ret < 0 && ret != -ENOSYS)
465 goto eresume;
468 * Try to configure with default parameters. Notice: this is the
469 * very first open, so, we cannot race against other calls,
470 * apart from someone else calling open() simultaneously, but
471 * .video_lock is protecting us against it.
473 ret = soc_camera_set_fmt(icd, &f);
474 if (ret < 0)
475 goto esfmt;
477 if (ici->ops->init_videobuf) {
478 ici->ops->init_videobuf(&icd->vb_vidq, icd);
479 } else {
480 ret = ici->ops->init_videobuf2(&icd->vb2_vidq, icd);
481 if (ret < 0)
482 goto einitvb;
486 file->private_data = icd;
487 dev_dbg(&icd->dev, "camera device open\n");
489 return 0;
492 * First four errors are entered with the .video_lock held
493 * and use_count == 1
495 einitvb:
496 esfmt:
497 pm_runtime_disable(&icd->vdev->dev);
498 eresume:
499 ici->ops->remove(icd);
500 eiciadd:
501 soc_camera_power_set(icd, icl, 0);
502 epower:
503 icd->use_count--;
504 module_put(ici->ops->owner);
506 return ret;
509 static int soc_camera_close(struct file *file)
511 struct soc_camera_device *icd = file->private_data;
512 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
514 icd->use_count--;
515 if (!icd->use_count) {
516 struct soc_camera_link *icl = to_soc_camera_link(icd);
518 pm_runtime_suspend(&icd->vdev->dev);
519 pm_runtime_disable(&icd->vdev->dev);
521 ici->ops->remove(icd);
522 if (ici->ops->init_videobuf2)
523 vb2_queue_release(&icd->vb2_vidq);
525 soc_camera_power_set(icd, icl, 0);
528 if (icd->streamer == file)
529 icd->streamer = NULL;
531 module_put(ici->ops->owner);
533 dev_dbg(&icd->dev, "camera device close\n");
535 return 0;
538 static ssize_t soc_camera_read(struct file *file, char __user *buf,
539 size_t count, loff_t *ppos)
541 struct soc_camera_device *icd = file->private_data;
542 int err = -EINVAL;
544 dev_err(&icd->dev, "camera device read not implemented\n");
546 return err;
549 static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
551 struct soc_camera_device *icd = file->private_data;
552 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
553 int err;
555 dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
557 if (icd->streamer != file)
558 return -EBUSY;
560 if (ici->ops->init_videobuf)
561 err = videobuf_mmap_mapper(&icd->vb_vidq, vma);
562 else
563 err = vb2_mmap(&icd->vb2_vidq, vma);
565 dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
566 (unsigned long)vma->vm_start,
567 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
568 err);
570 return err;
573 static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
575 struct soc_camera_device *icd = file->private_data;
576 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
578 if (icd->streamer != file)
579 return -EBUSY;
581 if (ici->ops->init_videobuf && list_empty(&icd->vb_vidq.stream)) {
582 dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
583 return POLLERR;
586 return ici->ops->poll(file, pt);
589 void soc_camera_lock(struct vb2_queue *vq)
591 struct soc_camera_device *icd = vb2_get_drv_priv(vq);
592 mutex_lock(&icd->video_lock);
594 EXPORT_SYMBOL(soc_camera_lock);
596 void soc_camera_unlock(struct vb2_queue *vq)
598 struct soc_camera_device *icd = vb2_get_drv_priv(vq);
599 mutex_unlock(&icd->video_lock);
601 EXPORT_SYMBOL(soc_camera_unlock);
603 static struct v4l2_file_operations soc_camera_fops = {
604 .owner = THIS_MODULE,
605 .open = soc_camera_open,
606 .release = soc_camera_close,
607 .unlocked_ioctl = video_ioctl2,
608 .read = soc_camera_read,
609 .mmap = soc_camera_mmap,
610 .poll = soc_camera_poll,
613 static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
614 struct v4l2_format *f)
616 struct soc_camera_device *icd = file->private_data;
617 int ret;
619 WARN_ON(priv != file->private_data);
621 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
622 dev_warn(&icd->dev, "Wrong buf-type %d\n", f->type);
623 return -EINVAL;
626 if (icd->streamer && icd->streamer != file)
627 return -EBUSY;
629 if (icd->vb_vidq.bufs[0]) {
630 dev_err(&icd->dev, "S_FMT denied: queue initialised\n");
631 return -EBUSY;
634 ret = soc_camera_set_fmt(icd, f);
636 if (!ret && !icd->streamer)
637 icd->streamer = file;
639 return ret;
642 static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
643 struct v4l2_fmtdesc *f)
645 struct soc_camera_device *icd = file->private_data;
646 const struct soc_mbus_pixelfmt *format;
648 WARN_ON(priv != file->private_data);
650 if (f->index >= icd->num_user_formats)
651 return -EINVAL;
653 format = icd->user_formats[f->index].host_fmt;
655 if (format->name)
656 strlcpy(f->description, format->name, sizeof(f->description));
657 f->pixelformat = format->fourcc;
658 return 0;
661 static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
662 struct v4l2_format *f)
664 struct soc_camera_device *icd = file->private_data;
665 struct v4l2_pix_format *pix = &f->fmt.pix;
667 WARN_ON(priv != file->private_data);
669 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
670 return -EINVAL;
672 pix->width = icd->user_width;
673 pix->height = icd->user_height;
674 pix->bytesperline = icd->bytesperline;
675 pix->sizeimage = icd->sizeimage;
676 pix->field = icd->field;
677 pix->pixelformat = icd->current_fmt->host_fmt->fourcc;
678 pix->colorspace = icd->colorspace;
679 dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
680 icd->current_fmt->host_fmt->fourcc);
681 return 0;
684 static int soc_camera_querycap(struct file *file, void *priv,
685 struct v4l2_capability *cap)
687 struct soc_camera_device *icd = file->private_data;
688 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
690 WARN_ON(priv != file->private_data);
692 strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
693 return ici->ops->querycap(ici, cap);
696 static int soc_camera_streamon(struct file *file, void *priv,
697 enum v4l2_buf_type i)
699 struct soc_camera_device *icd = file->private_data;
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 (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
707 return -EINVAL;
709 if (icd->streamer != file)
710 return -EBUSY;
712 /* This calls buf_queue from host driver's videobuf_queue_ops */
713 if (ici->ops->init_videobuf)
714 ret = videobuf_streamon(&icd->vb_vidq);
715 else
716 ret = vb2_streamon(&icd->vb2_vidq, i);
718 if (!ret)
719 v4l2_subdev_call(sd, video, s_stream, 1);
721 return ret;
724 static int soc_camera_streamoff(struct file *file, void *priv,
725 enum v4l2_buf_type i)
727 struct soc_camera_device *icd = file->private_data;
728 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
729 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
731 WARN_ON(priv != file->private_data);
733 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
734 return -EINVAL;
736 if (icd->streamer != file)
737 return -EBUSY;
740 * This calls buf_release from host driver's videobuf_queue_ops for all
741 * remaining buffers. When the last buffer is freed, stop capture
743 if (ici->ops->init_videobuf)
744 videobuf_streamoff(&icd->vb_vidq);
745 else
746 vb2_streamoff(&icd->vb2_vidq, i);
748 v4l2_subdev_call(sd, video, s_stream, 0);
750 return 0;
753 static int soc_camera_queryctrl(struct file *file, void *priv,
754 struct v4l2_queryctrl *qc)
756 struct soc_camera_device *icd = file->private_data;
757 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
758 int i;
760 WARN_ON(priv != file->private_data);
762 if (!qc->id)
763 return -EINVAL;
765 /* First check host controls */
766 for (i = 0; i < ici->ops->num_controls; i++)
767 if (qc->id == ici->ops->controls[i].id) {
768 memcpy(qc, &(ici->ops->controls[i]),
769 sizeof(*qc));
770 return 0;
773 /* Then device controls */
774 for (i = 0; i < icd->ops->num_controls; i++)
775 if (qc->id == icd->ops->controls[i].id) {
776 memcpy(qc, &(icd->ops->controls[i]),
777 sizeof(*qc));
778 return 0;
781 return -EINVAL;
784 static int soc_camera_g_ctrl(struct file *file, void *priv,
785 struct v4l2_control *ctrl)
787 struct soc_camera_device *icd = file->private_data;
788 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
789 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
790 int ret;
792 WARN_ON(priv != file->private_data);
794 if (ici->ops->get_ctrl) {
795 ret = ici->ops->get_ctrl(icd, ctrl);
796 if (ret != -ENOIOCTLCMD)
797 return ret;
800 return v4l2_subdev_call(sd, core, g_ctrl, ctrl);
803 static int soc_camera_s_ctrl(struct file *file, void *priv,
804 struct v4l2_control *ctrl)
806 struct soc_camera_device *icd = file->private_data;
807 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
808 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
809 int ret;
811 WARN_ON(priv != file->private_data);
813 if (ici->ops->set_ctrl) {
814 ret = ici->ops->set_ctrl(icd, ctrl);
815 if (ret != -ENOIOCTLCMD)
816 return ret;
819 return v4l2_subdev_call(sd, core, s_ctrl, ctrl);
822 static int soc_camera_cropcap(struct file *file, void *fh,
823 struct v4l2_cropcap *a)
825 struct soc_camera_device *icd = file->private_data;
826 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
828 return ici->ops->cropcap(icd, a);
831 static int soc_camera_g_crop(struct file *file, void *fh,
832 struct v4l2_crop *a)
834 struct soc_camera_device *icd = file->private_data;
835 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
836 int ret;
838 ret = ici->ops->get_crop(icd, a);
840 return ret;
844 * According to the V4L2 API, drivers shall not update the struct v4l2_crop
845 * argument with the actual geometry, instead, the user shall use G_CROP to
846 * retrieve it.
848 static int soc_camera_s_crop(struct file *file, void *fh,
849 struct v4l2_crop *a)
851 struct soc_camera_device *icd = file->private_data;
852 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
853 struct v4l2_rect *rect = &a->c;
854 struct v4l2_crop current_crop;
855 int ret;
857 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
858 return -EINVAL;
860 dev_dbg(&icd->dev, "S_CROP(%ux%u@%u:%u)\n",
861 rect->width, rect->height, rect->left, rect->top);
863 /* If get_crop fails, we'll let host and / or client drivers decide */
864 ret = ici->ops->get_crop(icd, &current_crop);
866 /* Prohibit window size change with initialised buffers */
867 if (ret < 0) {
868 dev_err(&icd->dev,
869 "S_CROP denied: getting current crop failed\n");
870 } else if (icd->vb_vidq.bufs[0] &&
871 (a->c.width != current_crop.c.width ||
872 a->c.height != current_crop.c.height)) {
873 dev_err(&icd->dev,
874 "S_CROP denied: queue initialised and sizes differ\n");
875 ret = -EBUSY;
876 } else {
877 ret = ici->ops->set_crop(icd, a);
880 return ret;
883 static int soc_camera_g_parm(struct file *file, void *fh,
884 struct v4l2_streamparm *a)
886 struct soc_camera_device *icd = file->private_data;
887 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
889 if (ici->ops->get_parm)
890 return ici->ops->get_parm(icd, a);
892 return -ENOIOCTLCMD;
895 static int soc_camera_s_parm(struct file *file, void *fh,
896 struct v4l2_streamparm *a)
898 struct soc_camera_device *icd = file->private_data;
899 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
901 if (ici->ops->set_parm)
902 return ici->ops->set_parm(icd, a);
904 return -ENOIOCTLCMD;
907 static int soc_camera_g_chip_ident(struct file *file, void *fh,
908 struct v4l2_dbg_chip_ident *id)
910 struct soc_camera_device *icd = file->private_data;
911 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
913 return v4l2_subdev_call(sd, core, g_chip_ident, id);
916 #ifdef CONFIG_VIDEO_ADV_DEBUG
917 static int soc_camera_g_register(struct file *file, void *fh,
918 struct v4l2_dbg_register *reg)
920 struct soc_camera_device *icd = file->private_data;
921 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
923 return v4l2_subdev_call(sd, core, g_register, reg);
926 static int soc_camera_s_register(struct file *file, void *fh,
927 struct v4l2_dbg_register *reg)
929 struct soc_camera_device *icd = file->private_data;
930 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
932 return v4l2_subdev_call(sd, core, s_register, reg);
934 #endif
936 /* So far this function cannot fail */
937 static void scan_add_host(struct soc_camera_host *ici)
939 struct soc_camera_device *icd;
941 mutex_lock(&list_lock);
943 list_for_each_entry(icd, &devices, list) {
944 if (icd->iface == ici->nr) {
945 int ret;
946 icd->dev.parent = ici->v4l2_dev.dev;
947 dev_set_name(&icd->dev, "%u-%u", icd->iface,
948 icd->devnum);
949 ret = device_register(&icd->dev);
950 if (ret < 0) {
951 icd->dev.parent = NULL;
952 dev_err(&icd->dev,
953 "Cannot register device: %d\n", ret);
958 mutex_unlock(&list_lock);
961 #ifdef CONFIG_I2C_BOARDINFO
962 static int soc_camera_init_i2c(struct soc_camera_device *icd,
963 struct soc_camera_link *icl)
965 struct i2c_client *client;
966 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
967 struct i2c_adapter *adap = i2c_get_adapter(icl->i2c_adapter_id);
968 struct v4l2_subdev *subdev;
970 if (!adap) {
971 dev_err(&icd->dev, "Cannot get I2C adapter #%d. No driver?\n",
972 icl->i2c_adapter_id);
973 goto ei2cga;
976 icl->board_info->platform_data = icd;
978 subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
979 icl->board_info, NULL);
980 if (!subdev)
981 goto ei2cnd;
983 client = v4l2_get_subdevdata(subdev);
985 /* Use to_i2c_client(dev) to recover the i2c client */
986 dev_set_drvdata(&icd->dev, &client->dev);
988 return 0;
989 ei2cnd:
990 i2c_put_adapter(adap);
991 ei2cga:
992 return -ENODEV;
995 static void soc_camera_free_i2c(struct soc_camera_device *icd)
997 struct i2c_client *client =
998 to_i2c_client(to_soc_camera_control(icd));
999 dev_set_drvdata(&icd->dev, NULL);
1000 v4l2_device_unregister_subdev(i2c_get_clientdata(client));
1001 i2c_unregister_device(client);
1002 i2c_put_adapter(client->adapter);
1004 #else
1005 #define soc_camera_init_i2c(icd, icl) (-ENODEV)
1006 #define soc_camera_free_i2c(icd) do {} while (0)
1007 #endif
1009 static int soc_camera_video_start(struct soc_camera_device *icd);
1010 static int video_dev_create(struct soc_camera_device *icd);
1011 /* Called during host-driver probe */
1012 static int soc_camera_probe(struct device *dev)
1014 struct soc_camera_device *icd = to_soc_camera_dev(dev);
1015 struct soc_camera_host *ici = to_soc_camera_host(dev->parent);
1016 struct soc_camera_link *icl = to_soc_camera_link(icd);
1017 struct device *control = NULL;
1018 struct v4l2_subdev *sd;
1019 struct v4l2_mbus_framefmt mf;
1020 int ret;
1022 dev_info(dev, "Probing %s\n", dev_name(dev));
1024 ret = regulator_bulk_get(icd->pdev, icl->num_regulators,
1025 icl->regulators);
1026 if (ret < 0)
1027 goto ereg;
1029 ret = soc_camera_power_set(icd, icl, 1);
1030 if (ret < 0)
1031 goto epower;
1033 /* The camera could have been already on, try to reset */
1034 if (icl->reset)
1035 icl->reset(icd->pdev);
1037 ret = ici->ops->add(icd);
1038 if (ret < 0)
1039 goto eadd;
1041 /* Must have icd->vdev before registering the device */
1042 ret = video_dev_create(icd);
1043 if (ret < 0)
1044 goto evdc;
1046 /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
1047 if (icl->board_info) {
1048 ret = soc_camera_init_i2c(icd, icl);
1049 if (ret < 0)
1050 goto eadddev;
1051 } else if (!icl->add_device || !icl->del_device) {
1052 ret = -EINVAL;
1053 goto eadddev;
1054 } else {
1055 if (icl->module_name)
1056 ret = request_module(icl->module_name);
1058 ret = icl->add_device(icl, &icd->dev);
1059 if (ret < 0)
1060 goto eadddev;
1063 * FIXME: this is racy, have to use driver-binding notification,
1064 * when it is available
1066 control = to_soc_camera_control(icd);
1067 if (!control || !control->driver || !dev_get_drvdata(control) ||
1068 !try_module_get(control->driver->owner)) {
1069 icl->del_device(icl);
1070 goto enodrv;
1074 /* At this point client .probe() should have run already */
1075 ret = soc_camera_init_user_formats(icd);
1076 if (ret < 0)
1077 goto eiufmt;
1079 icd->field = V4L2_FIELD_ANY;
1081 icd->vdev->lock = &icd->video_lock;
1084 * ..._video_start() will create a device node, video_register_device()
1085 * itself is protected against concurrent open() calls, but we also have
1086 * to protect our data.
1088 mutex_lock(&icd->video_lock);
1090 ret = soc_camera_video_start(icd);
1091 if (ret < 0)
1092 goto evidstart;
1094 /* Try to improve our guess of a reasonable window format */
1095 sd = soc_camera_to_subdev(icd);
1096 if (!v4l2_subdev_call(sd, video, g_mbus_fmt, &mf)) {
1097 icd->user_width = mf.width;
1098 icd->user_height = mf.height;
1099 icd->colorspace = mf.colorspace;
1100 icd->field = mf.field;
1103 /* Do we have to sysfs_remove_link() before device_unregister()? */
1104 if (sysfs_create_link(&icd->dev.kobj, &to_soc_camera_control(icd)->kobj,
1105 "control"))
1106 dev_warn(&icd->dev, "Failed creating the control symlink\n");
1108 ici->ops->remove(icd);
1110 soc_camera_power_set(icd, icl, 0);
1112 mutex_unlock(&icd->video_lock);
1114 return 0;
1116 evidstart:
1117 mutex_unlock(&icd->video_lock);
1118 soc_camera_free_user_formats(icd);
1119 eiufmt:
1120 if (icl->board_info) {
1121 soc_camera_free_i2c(icd);
1122 } else {
1123 icl->del_device(icl);
1124 module_put(control->driver->owner);
1126 enodrv:
1127 eadddev:
1128 video_device_release(icd->vdev);
1129 evdc:
1130 ici->ops->remove(icd);
1131 eadd:
1132 soc_camera_power_set(icd, icl, 0);
1133 epower:
1134 regulator_bulk_free(icl->num_regulators, icl->regulators);
1135 ereg:
1136 return ret;
1140 * This is called on device_unregister, which only means we have to disconnect
1141 * from the host, but not remove ourselves from the device list
1143 static int soc_camera_remove(struct device *dev)
1145 struct soc_camera_device *icd = to_soc_camera_dev(dev);
1146 struct soc_camera_link *icl = to_soc_camera_link(icd);
1147 struct video_device *vdev = icd->vdev;
1149 BUG_ON(!dev->parent);
1151 if (vdev) {
1152 video_unregister_device(vdev);
1153 icd->vdev = NULL;
1156 if (icl->board_info) {
1157 soc_camera_free_i2c(icd);
1158 } else {
1159 struct device_driver *drv = to_soc_camera_control(icd) ?
1160 to_soc_camera_control(icd)->driver : NULL;
1161 if (drv) {
1162 icl->del_device(icl);
1163 module_put(drv->owner);
1166 soc_camera_free_user_formats(icd);
1168 regulator_bulk_free(icl->num_regulators, icl->regulators);
1170 return 0;
1173 static int soc_camera_suspend(struct device *dev, pm_message_t state)
1175 struct soc_camera_device *icd = to_soc_camera_dev(dev);
1176 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1177 int ret = 0;
1179 if (ici->ops->suspend)
1180 ret = ici->ops->suspend(icd, state);
1182 return ret;
1185 static int soc_camera_resume(struct device *dev)
1187 struct soc_camera_device *icd = to_soc_camera_dev(dev);
1188 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1189 int ret = 0;
1191 if (ici->ops->resume)
1192 ret = ici->ops->resume(icd);
1194 return ret;
1197 struct bus_type soc_camera_bus_type = {
1198 .name = "soc-camera",
1199 .probe = soc_camera_probe,
1200 .remove = soc_camera_remove,
1201 .suspend = soc_camera_suspend,
1202 .resume = soc_camera_resume,
1204 EXPORT_SYMBOL_GPL(soc_camera_bus_type);
1206 static struct device_driver ic_drv = {
1207 .name = "camera",
1208 .bus = &soc_camera_bus_type,
1209 .owner = THIS_MODULE,
1212 static void dummy_release(struct device *dev)
1216 static int default_cropcap(struct soc_camera_device *icd,
1217 struct v4l2_cropcap *a)
1219 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1220 return v4l2_subdev_call(sd, video, cropcap, a);
1223 static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1225 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1226 return v4l2_subdev_call(sd, video, g_crop, a);
1229 static int default_s_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1231 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1232 return v4l2_subdev_call(sd, video, s_crop, a);
1235 static int default_g_parm(struct soc_camera_device *icd,
1236 struct v4l2_streamparm *parm)
1238 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1239 return v4l2_subdev_call(sd, video, g_parm, parm);
1242 static int default_s_parm(struct soc_camera_device *icd,
1243 struct v4l2_streamparm *parm)
1245 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1246 return v4l2_subdev_call(sd, video, s_parm, parm);
1249 static int default_enum_fsizes(struct soc_camera_device *icd,
1250 struct v4l2_frmsizeenum *fsize)
1252 int ret;
1253 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1254 const struct soc_camera_format_xlate *xlate;
1255 __u32 pixfmt = fsize->pixel_format;
1256 struct v4l2_frmsizeenum fsize_mbus = *fsize;
1258 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1259 if (!xlate)
1260 return -EINVAL;
1261 /* map xlate-code to pixel_format, sensor only handle xlate-code*/
1262 fsize_mbus.pixel_format = xlate->code;
1264 ret = v4l2_subdev_call(sd, video, enum_mbus_fsizes, &fsize_mbus);
1265 if (ret < 0)
1266 return ret;
1268 *fsize = fsize_mbus;
1269 fsize->pixel_format = pixfmt;
1271 return 0;
1274 static void soc_camera_device_init(struct device *dev, void *pdata)
1276 dev->platform_data = pdata;
1277 dev->bus = &soc_camera_bus_type;
1278 dev->release = dummy_release;
1281 int soc_camera_host_register(struct soc_camera_host *ici)
1283 struct soc_camera_host *ix;
1284 int ret;
1286 if (!ici || !ici->ops ||
1287 !ici->ops->try_fmt ||
1288 !ici->ops->set_fmt ||
1289 !ici->ops->set_bus_param ||
1290 !ici->ops->querycap ||
1291 ((!ici->ops->init_videobuf ||
1292 !ici->ops->reqbufs) &&
1293 !ici->ops->init_videobuf2) ||
1294 !ici->ops->add ||
1295 !ici->ops->remove ||
1296 !ici->ops->poll ||
1297 !ici->v4l2_dev.dev)
1298 return -EINVAL;
1300 if (!ici->ops->set_crop)
1301 ici->ops->set_crop = default_s_crop;
1302 if (!ici->ops->get_crop)
1303 ici->ops->get_crop = default_g_crop;
1304 if (!ici->ops->cropcap)
1305 ici->ops->cropcap = default_cropcap;
1306 if (!ici->ops->set_parm)
1307 ici->ops->set_parm = default_s_parm;
1308 if (!ici->ops->get_parm)
1309 ici->ops->get_parm = default_g_parm;
1310 if (!ici->ops->enum_fsizes)
1311 ici->ops->enum_fsizes = default_enum_fsizes;
1313 mutex_lock(&list_lock);
1314 list_for_each_entry(ix, &hosts, list) {
1315 if (ix->nr == ici->nr) {
1316 ret = -EBUSY;
1317 goto edevreg;
1321 ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
1322 if (ret < 0)
1323 goto edevreg;
1325 list_add_tail(&ici->list, &hosts);
1326 mutex_unlock(&list_lock);
1328 scan_add_host(ici);
1330 return 0;
1332 edevreg:
1333 mutex_unlock(&list_lock);
1334 return ret;
1336 EXPORT_SYMBOL(soc_camera_host_register);
1338 /* Unregister all clients! */
1339 void soc_camera_host_unregister(struct soc_camera_host *ici)
1341 struct soc_camera_device *icd;
1343 mutex_lock(&list_lock);
1345 list_del(&ici->list);
1347 list_for_each_entry(icd, &devices, list) {
1348 if (icd->iface == ici->nr) {
1349 void *pdata = icd->dev.platform_data;
1350 /* The bus->remove will be called */
1351 device_unregister(&icd->dev);
1353 * Not before device_unregister(), .remove
1354 * needs parent to call ici->ops->remove().
1355 * If the host module is loaded again, device_register()
1356 * would complain "already initialised," since 2.6.32
1357 * this is also needed to prevent use-after-free of the
1358 * device private data.
1360 memset(&icd->dev, 0, sizeof(icd->dev));
1361 soc_camera_device_init(&icd->dev, pdata);
1365 mutex_unlock(&list_lock);
1367 v4l2_device_unregister(&ici->v4l2_dev);
1369 EXPORT_SYMBOL(soc_camera_host_unregister);
1371 /* Image capture device */
1372 static int soc_camera_device_register(struct soc_camera_device *icd)
1374 struct soc_camera_device *ix;
1375 int num = -1, i;
1377 for (i = 0; i < 256 && num < 0; i++) {
1378 num = i;
1379 /* Check if this index is available on this interface */
1380 list_for_each_entry(ix, &devices, list) {
1381 if (ix->iface == icd->iface && ix->devnum == i) {
1382 num = -1;
1383 break;
1388 if (num < 0)
1390 * ok, we have 256 cameras on this host...
1391 * man, stay reasonable...
1393 return -ENOMEM;
1395 icd->devnum = num;
1396 icd->use_count = 0;
1397 icd->host_priv = NULL;
1398 mutex_init(&icd->video_lock);
1400 list_add_tail(&icd->list, &devices);
1402 return 0;
1405 static void soc_camera_device_unregister(struct soc_camera_device *icd)
1407 list_del(&icd->list);
1410 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
1411 .vidioc_querycap = soc_camera_querycap,
1412 .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
1413 .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
1414 .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
1415 .vidioc_enum_input = soc_camera_enum_input,
1416 .vidioc_g_input = soc_camera_g_input,
1417 .vidioc_s_input = soc_camera_s_input,
1418 .vidioc_s_std = soc_camera_s_std,
1419 .vidioc_enum_framesizes = soc_camera_enum_fsizes,
1420 .vidioc_reqbufs = soc_camera_reqbufs,
1421 .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
1422 .vidioc_querybuf = soc_camera_querybuf,
1423 .vidioc_qbuf = soc_camera_qbuf,
1424 .vidioc_dqbuf = soc_camera_dqbuf,
1425 .vidioc_streamon = soc_camera_streamon,
1426 .vidioc_streamoff = soc_camera_streamoff,
1427 .vidioc_queryctrl = soc_camera_queryctrl,
1428 .vidioc_g_ctrl = soc_camera_g_ctrl,
1429 .vidioc_s_ctrl = soc_camera_s_ctrl,
1430 .vidioc_cropcap = soc_camera_cropcap,
1431 .vidioc_g_crop = soc_camera_g_crop,
1432 .vidioc_s_crop = soc_camera_s_crop,
1433 .vidioc_g_parm = soc_camera_g_parm,
1434 .vidioc_s_parm = soc_camera_s_parm,
1435 .vidioc_g_chip_ident = soc_camera_g_chip_ident,
1436 #ifdef CONFIG_VIDEO_ADV_DEBUG
1437 .vidioc_g_register = soc_camera_g_register,
1438 .vidioc_s_register = soc_camera_s_register,
1439 #endif
1442 static int video_dev_create(struct soc_camera_device *icd)
1444 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1445 struct video_device *vdev = video_device_alloc();
1447 if (!vdev)
1448 return -ENOMEM;
1450 strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
1452 vdev->parent = &icd->dev;
1453 vdev->current_norm = V4L2_STD_UNKNOWN;
1454 vdev->fops = &soc_camera_fops;
1455 vdev->ioctl_ops = &soc_camera_ioctl_ops;
1456 vdev->release = video_device_release;
1457 vdev->tvnorms = V4L2_STD_UNKNOWN;
1459 icd->vdev = vdev;
1461 return 0;
1465 * Called from soc_camera_probe() above (with .video_lock held???)
1467 static int soc_camera_video_start(struct soc_camera_device *icd)
1469 struct device_type *type = icd->vdev->dev.type;
1470 int ret;
1472 if (!icd->dev.parent)
1473 return -ENODEV;
1475 if (!icd->ops ||
1476 !icd->ops->query_bus_param ||
1477 !icd->ops->set_bus_param)
1478 return -EINVAL;
1480 ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
1481 if (ret < 0) {
1482 dev_err(&icd->dev, "video_register_device failed: %d\n", ret);
1483 return ret;
1486 /* Restore device type, possibly set by the subdevice driver */
1487 icd->vdev->dev.type = type;
1489 return 0;
1492 static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev)
1494 struct soc_camera_link *icl = pdev->dev.platform_data;
1495 struct soc_camera_device *icd;
1496 int ret;
1498 if (!icl)
1499 return -EINVAL;
1501 icd = kzalloc(sizeof(*icd), GFP_KERNEL);
1502 if (!icd)
1503 return -ENOMEM;
1505 icd->iface = icl->bus_id;
1506 icd->pdev = &pdev->dev;
1507 platform_set_drvdata(pdev, icd);
1509 ret = soc_camera_device_register(icd);
1510 if (ret < 0)
1511 goto escdevreg;
1513 soc_camera_device_init(&icd->dev, icl);
1515 icd->user_width = DEFAULT_WIDTH;
1516 icd->user_height = DEFAULT_HEIGHT;
1518 return 0;
1520 escdevreg:
1521 kfree(icd);
1523 return ret;
1527 * Only called on rmmod for each platform device, since they are not
1528 * hot-pluggable. Now we know, that all our users - hosts and devices have
1529 * been unloaded already
1531 static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev)
1533 struct soc_camera_device *icd = platform_get_drvdata(pdev);
1535 if (!icd)
1536 return -EINVAL;
1538 soc_camera_device_unregister(icd);
1540 kfree(icd);
1542 return 0;
1545 static struct platform_driver __refdata soc_camera_pdrv = {
1546 .remove = __devexit_p(soc_camera_pdrv_remove),
1547 .driver = {
1548 .name = "soc-camera-pdrv",
1549 .owner = THIS_MODULE,
1553 static int __init soc_camera_init(void)
1555 int ret = bus_register(&soc_camera_bus_type);
1556 if (ret)
1557 return ret;
1558 ret = driver_register(&ic_drv);
1559 if (ret)
1560 goto edrvr;
1562 ret = platform_driver_probe(&soc_camera_pdrv, soc_camera_pdrv_probe);
1563 if (ret)
1564 goto epdr;
1566 return 0;
1568 epdr:
1569 driver_unregister(&ic_drv);
1570 edrvr:
1571 bus_unregister(&soc_camera_bus_type);
1572 return ret;
1575 static void __exit soc_camera_exit(void)
1577 platform_driver_unregister(&soc_camera_pdrv);
1578 driver_unregister(&ic_drv);
1579 bus_unregister(&soc_camera_bus_type);
1582 module_init(soc_camera_init);
1583 module_exit(soc_camera_exit);
1585 MODULE_DESCRIPTION("Image capture bus driver");
1586 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1587 MODULE_LICENSE("GPL");
1588 MODULE_ALIAS("platform:soc-camera-pdrv");