blob: 176017501055cdc1500063bbb21812ac340cc6b0 [file] [log] [blame]
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001/*
2 * camera image capture (abstract) bus driver
3 *
4 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
5 *
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.
13 *
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.
17 */
18
19#include <linux/module.h>
20#include <linux/init.h>
21#include <linux/device.h>
22#include <linux/list.h>
23#include <linux/err.h>
24#include <linux/mutex.h>
25#include <linux/vmalloc.h>
26
27#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030028#include <media/v4l2-ioctl.h>
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030029#include <media/v4l2-dev.h>
Paulius Zaleckas092d3922008-07-11 20:50:31 -030030#include <media/videobuf-core.h>
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030031#include <media/soc_camera.h>
32
33static LIST_HEAD(hosts);
34static LIST_HEAD(devices);
35static DEFINE_MUTEX(list_lock);
36static DEFINE_MUTEX(video_lock);
37
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -030038const struct soc_camera_data_format *soc_camera_format_by_fourcc(
Guennadi Liakhovetskiabe4c472008-12-01 09:44:56 -030039 struct soc_camera_device *icd, unsigned int fourcc)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030040{
41 unsigned int i;
42
Guennadi Liakhovetski26f1b942008-03-24 12:18:36 -030043 for (i = 0; i < icd->num_formats; i++)
44 if (icd->formats[i].fourcc == fourcc)
45 return icd->formats + i;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030046 return NULL;
47}
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -030048EXPORT_SYMBOL(soc_camera_format_by_fourcc);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030049
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -030050const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
51 struct soc_camera_device *icd, unsigned int fourcc)
52{
53 unsigned int i;
54
55 for (i = 0; i < icd->num_user_formats; i++)
56 if (icd->user_formats[i].host_fmt->fourcc == fourcc)
57 return icd->user_formats + i;
58 return NULL;
59}
60EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);
61
Guennadi Liakhovetskibd73b362008-12-23 05:54:45 -030062/**
63 * soc_camera_apply_sensor_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
64 * @icl: camera platform parameters
65 * @flags: flags to be inverted according to platform configuration
66 * @return: resulting flags
67 */
68unsigned long soc_camera_apply_sensor_flags(struct soc_camera_link *icl,
69 unsigned long flags)
70{
71 unsigned long f;
72
73 /* If only one of the two polarities is supported, switch to the opposite */
74 if (icl->flags & SOCAM_SENSOR_INVERT_HSYNC) {
75 f = flags & (SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW);
76 if (f == SOCAM_HSYNC_ACTIVE_HIGH || f == SOCAM_HSYNC_ACTIVE_LOW)
77 flags ^= SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW;
78 }
79
80 if (icl->flags & SOCAM_SENSOR_INVERT_VSYNC) {
81 f = flags & (SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW);
82 if (f == SOCAM_VSYNC_ACTIVE_HIGH || f == SOCAM_VSYNC_ACTIVE_LOW)
83 flags ^= SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW;
84 }
85
86 if (icl->flags & SOCAM_SENSOR_INVERT_PCLK) {
87 f = flags & (SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING);
88 if (f == SOCAM_PCLK_SAMPLE_RISING || f == SOCAM_PCLK_SAMPLE_FALLING)
89 flags ^= SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING;
90 }
91
92 return flags;
93}
94EXPORT_SYMBOL(soc_camera_apply_sensor_flags);
95
Hans Verkuil72937892008-06-01 10:35:18 -030096static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
Guennadi Liakhovetskiabe4c472008-12-01 09:44:56 -030097 struct v4l2_format *f)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030098{
99 struct soc_camera_file *icf = file->private_data;
100 struct soc_camera_device *icd = icf->icd;
101 struct soc_camera_host *ici =
102 to_soc_camera_host(icd->dev.parent);
103 enum v4l2_field field;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300104 int ret;
105
106 WARN_ON(priv != file->private_data);
107
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -0300108 /*
109 * TODO: this might also have to migrate to host-drivers, if anyone
110 * wishes to support other fields
111 */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300112 field = f->fmt.pix.field;
113
114 if (field == V4L2_FIELD_ANY) {
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -0300115 f->fmt.pix.field = V4L2_FIELD_NONE;
116 } else if (field != V4L2_FIELD_NONE) {
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300117 dev_err(&icd->dev, "Field type invalid.\n");
118 return -EINVAL;
119 }
120
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300121 /* limit format to hardware capabilities */
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -0300122 ret = ici->ops->try_fmt(icd, f);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300123
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300124 return ret;
125}
126
127static int soc_camera_enum_input(struct file *file, void *priv,
128 struct v4l2_input *inp)
129{
130 if (inp->index != 0)
131 return -EINVAL;
132
133 inp->type = V4L2_INPUT_TYPE_CAMERA;
134 inp->std = V4L2_STD_UNKNOWN;
135 strcpy(inp->name, "Camera");
136
137 return 0;
138}
139
140static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
141{
142 *i = 0;
143
144 return 0;
145}
146
147static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
148{
149 if (i > 0)
150 return -EINVAL;
151
152 return 0;
153}
154
155static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
156{
157 return 0;
158}
159
160static int soc_camera_reqbufs(struct file *file, void *priv,
161 struct v4l2_requestbuffers *p)
162{
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 =
167 to_soc_camera_host(icd->dev.parent);
168
169 WARN_ON(priv != file->private_data);
170
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300171 dev_dbg(&icd->dev, "%s: %d\n", __func__, p->memory);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300172
173 ret = videobuf_reqbufs(&icf->vb_vidq, p);
174 if (ret < 0)
175 return ret;
176
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300177 return ici->ops->reqbufs(icf, p);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300178}
179
180static int soc_camera_querybuf(struct file *file, void *priv,
181 struct v4l2_buffer *p)
182{
183 struct soc_camera_file *icf = file->private_data;
184
185 WARN_ON(priv != file->private_data);
186
187 return videobuf_querybuf(&icf->vb_vidq, p);
188}
189
190static int soc_camera_qbuf(struct file *file, void *priv,
191 struct v4l2_buffer *p)
192{
193 struct soc_camera_file *icf = file->private_data;
194
195 WARN_ON(priv != file->private_data);
196
197 return videobuf_qbuf(&icf->vb_vidq, p);
198}
199
200static int soc_camera_dqbuf(struct file *file, void *priv,
201 struct v4l2_buffer *p)
202{
203 struct soc_camera_file *icf = file->private_data;
204
205 WARN_ON(priv != file->private_data);
206
207 return videobuf_dqbuf(&icf->vb_vidq, p, file->f_flags & O_NONBLOCK);
208}
209
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300210static int soc_camera_init_user_formats(struct soc_camera_device *icd)
211{
212 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
213 int i, fmts = 0;
214
215 if (!ici->ops->get_formats)
216 /*
217 * Fallback mode - the host will have to serve all
218 * sensor-provided formats one-to-one to the user
219 */
220 fmts = icd->num_formats;
221 else
222 /*
223 * First pass - only count formats this host-sensor
224 * configuration can provide
225 */
226 for (i = 0; i < icd->num_formats; i++)
227 fmts += ici->ops->get_formats(icd, i, NULL);
228
229 if (!fmts)
230 return -ENXIO;
231
232 icd->user_formats =
233 vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
234 if (!icd->user_formats)
235 return -ENOMEM;
236
237 icd->num_user_formats = fmts;
238 fmts = 0;
239
240 dev_dbg(&icd->dev, "Found %d supported formats.\n", fmts);
241
242 /* Second pass - actually fill data formats */
243 for (i = 0; i < icd->num_formats; i++)
244 if (!ici->ops->get_formats) {
245 icd->user_formats[i].host_fmt = icd->formats + i;
246 icd->user_formats[i].cam_fmt = icd->formats + i;
247 icd->user_formats[i].buswidth = icd->formats[i].depth;
248 } else {
249 fmts += ici->ops->get_formats(icd, i,
250 &icd->user_formats[fmts]);
251 }
252
253 icd->current_fmt = icd->user_formats[0].host_fmt;
254
255 return 0;
256}
257
258static void soc_camera_free_user_formats(struct soc_camera_device *icd)
259{
260 vfree(icd->user_formats);
261}
262
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300263static int soc_camera_open(struct inode *inode, struct file *file)
264{
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300265 struct video_device *vdev;
266 struct soc_camera_device *icd;
267 struct soc_camera_host *ici;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300268 struct soc_camera_file *icf;
269 int ret;
270
271 icf = vmalloc(sizeof(*icf));
272 if (!icf)
273 return -ENOMEM;
274
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300275 /* Protect against icd->remove() until we module_get() both drivers. */
276 mutex_lock(&video_lock);
277
278 vdev = video_devdata(file);
Hans Verkuil5e85e732008-07-20 06:31:39 -0300279 icd = container_of(vdev->parent, struct soc_camera_device, dev);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300280 ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300281
282 if (!try_module_get(icd->ops->owner)) {
283 dev_err(&icd->dev, "Couldn't lock sensor driver.\n");
284 ret = -EINVAL;
285 goto emgd;
286 }
287
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300288 if (!try_module_get(ici->ops->owner)) {
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300289 dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
290 ret = -EINVAL;
291 goto emgi;
292 }
293
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300294 icf->icd = icd;
Guennadi Liakhovetski1a0063a2008-04-04 13:46:34 -0300295 icd->use_count++;
296
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300297 /* Now we really have to activate the camera */
298 if (icd->use_count == 1) {
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300299 ret = soc_camera_init_user_formats(icd);
300 if (ret < 0)
301 goto eiufmt;
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300302 ret = ici->ops->add(icd);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300303 if (ret < 0) {
304 dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300305 goto eiciadd;
306 }
307 }
308
309 mutex_unlock(&video_lock);
310
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300311 file->private_data = icf;
312 dev_dbg(&icd->dev, "camera device open\n");
313
Magnus Damma034d1b2008-07-11 20:59:34 -0300314 ici->ops->init_videobuf(&icf->vb_vidq, icd);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300315
316 return 0;
317
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300318 /* All errors are entered with the video_lock held */
319eiciadd:
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300320 soc_camera_free_user_formats(icd);
321eiufmt:
322 icd->use_count--;
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300323 module_put(ici->ops->owner);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300324emgi:
325 module_put(icd->ops->owner);
326emgd:
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300327 mutex_unlock(&video_lock);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300328 vfree(icf);
329 return ret;
330}
331
332static int soc_camera_close(struct inode *inode, struct file *file)
333{
334 struct soc_camera_file *icf = file->private_data;
335 struct soc_camera_device *icd = icf->icd;
336 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
337 struct video_device *vdev = icd->vdev;
338
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300339 mutex_lock(&video_lock);
340 icd->use_count--;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300341 if (!icd->use_count) {
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300342 ici->ops->remove(icd);
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300343 soc_camera_free_user_formats(icd);
344 }
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300345 module_put(icd->ops->owner);
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300346 module_put(ici->ops->owner);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300347 mutex_unlock(&video_lock);
348
Guennadi Liakhovetski1a0063a2008-04-04 13:46:34 -0300349 vfree(icf);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300350
Hans Verkuil5e85e732008-07-20 06:31:39 -0300351 dev_dbg(vdev->parent, "camera device close\n");
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300352
353 return 0;
354}
355
Andrew Mortonaba360d2008-04-22 14:45:59 -0300356static ssize_t soc_camera_read(struct file *file, char __user *buf,
Guennadi Liakhovetskiabe4c472008-12-01 09:44:56 -0300357 size_t count, loff_t *ppos)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300358{
359 struct soc_camera_file *icf = file->private_data;
360 struct soc_camera_device *icd = icf->icd;
361 struct video_device *vdev = icd->vdev;
362 int err = -EINVAL;
363
Hans Verkuil5e85e732008-07-20 06:31:39 -0300364 dev_err(vdev->parent, "camera device read not implemented\n");
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300365
366 return err;
367}
368
369static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
370{
371 struct soc_camera_file *icf = file->private_data;
372 struct soc_camera_device *icd = icf->icd;
373 int err;
374
375 dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
376
377 err = videobuf_mmap_mapper(&icf->vb_vidq, vma);
378
379 dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
380 (unsigned long)vma->vm_start,
381 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
382 err);
383
384 return err;
385}
386
387static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
388{
389 struct soc_camera_file *icf = file->private_data;
390 struct soc_camera_device *icd = icf->icd;
391 struct soc_camera_host *ici =
392 to_soc_camera_host(icd->dev.parent);
393
394 if (list_empty(&icf->vb_vidq.stream)) {
395 dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
396 return POLLERR;
397 }
398
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300399 return ici->ops->poll(file, pt);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300400}
401
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300402static struct file_operations soc_camera_fops = {
403 .owner = THIS_MODULE,
404 .open = soc_camera_open,
405 .release = soc_camera_close,
406 .ioctl = video_ioctl2,
407 .read = soc_camera_read,
408 .mmap = soc_camera_mmap,
409 .poll = soc_camera_poll,
410 .llseek = no_llseek,
411};
412
Hans Verkuil72937892008-06-01 10:35:18 -0300413static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
Guennadi Liakhovetskiabe4c472008-12-01 09:44:56 -0300414 struct v4l2_format *f)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300415{
416 struct soc_camera_file *icf = file->private_data;
417 struct soc_camera_device *icd = icf->icd;
418 struct soc_camera_host *ici =
419 to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300420 __u32 pixfmt = f->fmt.pix.pixelformat;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300421 int ret;
422 struct v4l2_rect rect;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300423
424 WARN_ON(priv != file->private_data);
425
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -0300426 ret = soc_camera_try_fmt_vid_cap(file, priv, f);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300427 if (ret < 0)
428 return ret;
429
430 rect.left = icd->x_current;
431 rect.top = icd->y_current;
432 rect.width = f->fmt.pix.width;
433 rect.height = f->fmt.pix.height;
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -0300434 ret = ici->ops->set_fmt(icd, f->fmt.pix.pixelformat, &rect);
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -0300435 if (ret < 0) {
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300436 return ret;
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -0300437 } else if (!icd->current_fmt ||
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300438 icd->current_fmt->fourcc != pixfmt) {
439 dev_err(&ici->dev,
440 "Host driver hasn't set up current format correctly!\n");
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -0300441 return -EINVAL;
442 }
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300443
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300444 icd->width = rect.width;
445 icd->height = rect.height;
446 icf->vb_vidq.field = f->fmt.pix.field;
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -0300447 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300448 dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n",
449 f->type);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300450
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300451 dev_dbg(&icd->dev, "set width: %d height: %d\n",
452 icd->width, icd->height);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300453
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300454 /* set physical bus parameters */
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300455 return ici->ops->set_bus_param(icd, pixfmt);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300456}
457
Hans Verkuil72937892008-06-01 10:35:18 -0300458static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
Guennadi Liakhovetskiabe4c472008-12-01 09:44:56 -0300459 struct v4l2_fmtdesc *f)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300460{
461 struct soc_camera_file *icf = file->private_data;
462 struct soc_camera_device *icd = icf->icd;
463 const struct soc_camera_data_format *format;
464
465 WARN_ON(priv != file->private_data);
466
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300467 if (f->index >= icd->num_user_formats)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300468 return -EINVAL;
469
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300470 format = icd->user_formats[f->index].host_fmt;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300471
472 strlcpy(f->description, format->name, sizeof(f->description));
473 f->pixelformat = format->fourcc;
474 return 0;
475}
476
Hans Verkuil72937892008-06-01 10:35:18 -0300477static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
Guennadi Liakhovetskiabe4c472008-12-01 09:44:56 -0300478 struct v4l2_format *f)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300479{
480 struct soc_camera_file *icf = file->private_data;
481 struct soc_camera_device *icd = icf->icd;
482
483 WARN_ON(priv != file->private_data);
484
485 f->fmt.pix.width = icd->width;
486 f->fmt.pix.height = icd->height;
487 f->fmt.pix.field = icf->vb_vidq.field;
488 f->fmt.pix.pixelformat = icd->current_fmt->fourcc;
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -0300489 f->fmt.pix.bytesperline = f->fmt.pix.width *
490 DIV_ROUND_UP(icd->current_fmt->depth, 8);
491 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300492 dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
493 icd->current_fmt->fourcc);
494 return 0;
495}
496
497static int soc_camera_querycap(struct file *file, void *priv,
498 struct v4l2_capability *cap)
499{
500 struct soc_camera_file *icf = file->private_data;
501 struct soc_camera_device *icd = icf->icd;
502 struct soc_camera_host *ici =
503 to_soc_camera_host(icd->dev.parent);
504
505 WARN_ON(priv != file->private_data);
506
507 strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300508 return ici->ops->querycap(ici, cap);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300509}
510
511static int soc_camera_streamon(struct file *file, void *priv,
512 enum v4l2_buf_type i)
513{
514 struct soc_camera_file *icf = file->private_data;
515 struct soc_camera_device *icd = icf->icd;
516
517 WARN_ON(priv != file->private_data);
518
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300519 dev_dbg(&icd->dev, "%s\n", __func__);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300520
521 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
522 return -EINVAL;
523
524 icd->ops->start_capture(icd);
525
526 /* This calls buf_queue from host driver's videobuf_queue_ops */
527 return videobuf_streamon(&icf->vb_vidq);
528}
529
530static int soc_camera_streamoff(struct file *file, void *priv,
531 enum v4l2_buf_type i)
532{
533 struct soc_camera_file *icf = file->private_data;
534 struct soc_camera_device *icd = icf->icd;
535
536 WARN_ON(priv != file->private_data);
537
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300538 dev_dbg(&icd->dev, "%s\n", __func__);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300539
540 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
541 return -EINVAL;
542
543 /* This calls buf_release from host driver's videobuf_queue_ops for all
544 * remaining buffers. When the last buffer is freed, stop capture */
545 videobuf_streamoff(&icf->vb_vidq);
546
547 icd->ops->stop_capture(icd);
548
549 return 0;
550}
551
552static int soc_camera_queryctrl(struct file *file, void *priv,
553 struct v4l2_queryctrl *qc)
554{
555 struct soc_camera_file *icf = file->private_data;
556 struct soc_camera_device *icd = icf->icd;
557 int i;
558
559 WARN_ON(priv != file->private_data);
560
561 if (!qc->id)
562 return -EINVAL;
563
564 for (i = 0; i < icd->ops->num_controls; i++)
565 if (qc->id == icd->ops->controls[i].id) {
566 memcpy(qc, &(icd->ops->controls[i]),
567 sizeof(*qc));
568 return 0;
569 }
570
571 return -EINVAL;
572}
573
574static int soc_camera_g_ctrl(struct file *file, void *priv,
575 struct v4l2_control *ctrl)
576{
577 struct soc_camera_file *icf = file->private_data;
578 struct soc_camera_device *icd = icf->icd;
579
580 WARN_ON(priv != file->private_data);
581
582 switch (ctrl->id) {
583 case V4L2_CID_GAIN:
584 if (icd->gain == (unsigned short)~0)
585 return -EINVAL;
586 ctrl->value = icd->gain;
587 return 0;
588 case V4L2_CID_EXPOSURE:
589 if (icd->exposure == (unsigned short)~0)
590 return -EINVAL;
591 ctrl->value = icd->exposure;
592 return 0;
593 }
594
595 if (icd->ops->get_control)
596 return icd->ops->get_control(icd, ctrl);
597 return -EINVAL;
598}
599
600static int soc_camera_s_ctrl(struct file *file, void *priv,
601 struct v4l2_control *ctrl)
602{
603 struct soc_camera_file *icf = file->private_data;
604 struct soc_camera_device *icd = icf->icd;
605
606 WARN_ON(priv != file->private_data);
607
608 if (icd->ops->set_control)
609 return icd->ops->set_control(icd, ctrl);
610 return -EINVAL;
611}
612
613static int soc_camera_cropcap(struct file *file, void *fh,
614 struct v4l2_cropcap *a)
615{
616 struct soc_camera_file *icf = file->private_data;
617 struct soc_camera_device *icd = icf->icd;
618
619 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
620 a->bounds.left = icd->x_min;
621 a->bounds.top = icd->y_min;
622 a->bounds.width = icd->width_max;
623 a->bounds.height = icd->height_max;
624 a->defrect.left = icd->x_min;
625 a->defrect.top = icd->y_min;
626 a->defrect.width = 640;
627 a->defrect.height = 480;
628 a->pixelaspect.numerator = 1;
629 a->pixelaspect.denominator = 1;
630
631 return 0;
632}
633
634static int soc_camera_g_crop(struct file *file, void *fh,
635 struct v4l2_crop *a)
636{
637 struct soc_camera_file *icf = file->private_data;
638 struct soc_camera_device *icd = icf->icd;
639
640 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
641 a->c.left = icd->x_current;
642 a->c.top = icd->y_current;
643 a->c.width = icd->width;
644 a->c.height = icd->height;
645
646 return 0;
647}
648
649static int soc_camera_s_crop(struct file *file, void *fh,
650 struct v4l2_crop *a)
651{
652 struct soc_camera_file *icf = file->private_data;
653 struct soc_camera_device *icd = icf->icd;
654 struct soc_camera_host *ici =
655 to_soc_camera_host(icd->dev.parent);
656 int ret;
657
658 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
659 return -EINVAL;
660
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -0300661 ret = ici->ops->set_fmt(icd, 0, &a->c);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300662 if (!ret) {
663 icd->width = a->c.width;
664 icd->height = a->c.height;
665 icd->x_current = a->c.left;
666 icd->y_current = a->c.top;
667 }
668
669 return ret;
670}
671
672static int soc_camera_g_chip_ident(struct file *file, void *fh,
673 struct v4l2_chip_ident *id)
674{
675 struct soc_camera_file *icf = file->private_data;
676 struct soc_camera_device *icd = icf->icd;
677
678 if (!icd->ops->get_chip_id)
679 return -EINVAL;
680
681 return icd->ops->get_chip_id(icd, id);
682}
683
684#ifdef CONFIG_VIDEO_ADV_DEBUG
685static int soc_camera_g_register(struct file *file, void *fh,
686 struct v4l2_register *reg)
687{
688 struct soc_camera_file *icf = file->private_data;
689 struct soc_camera_device *icd = icf->icd;
690
691 if (!icd->ops->get_register)
692 return -EINVAL;
693
694 return icd->ops->get_register(icd, reg);
695}
696
697static int soc_camera_s_register(struct file *file, void *fh,
698 struct v4l2_register *reg)
699{
700 struct soc_camera_file *icf = file->private_data;
701 struct soc_camera_device *icd = icf->icd;
702
703 if (!icd->ops->set_register)
704 return -EINVAL;
705
706 return icd->ops->set_register(icd, reg);
707}
708#endif
709
710static int device_register_link(struct soc_camera_device *icd)
711{
712 int ret = device_register(&icd->dev);
713
714 if (ret < 0) {
715 /* Prevent calling device_unregister() */
716 icd->dev.parent = NULL;
717 dev_err(&icd->dev, "Cannot register device: %d\n", ret);
718 /* Even if probe() was unsuccessful for all registered drivers,
719 * device_register() returns 0, and we add the link, just to
720 * document this camera's control device */
721 } else if (icd->control)
722 /* Have to sysfs_remove_link() before device_unregister()? */
723 if (sysfs_create_link(&icd->dev.kobj, &icd->control->kobj,
724 "control"))
725 dev_warn(&icd->dev,
726 "Failed creating the control symlink\n");
727 return ret;
728}
729
730/* So far this function cannot fail */
731static void scan_add_host(struct soc_camera_host *ici)
732{
733 struct soc_camera_device *icd;
734
735 mutex_lock(&list_lock);
736
737 list_for_each_entry(icd, &devices, list) {
738 if (icd->iface == ici->nr) {
739 icd->dev.parent = &ici->dev;
740 device_register_link(icd);
741 }
742 }
743
744 mutex_unlock(&list_lock);
745}
746
747/* return: 0 if no match found or a match found and
748 * device_register() successful, error code otherwise */
749static int scan_add_device(struct soc_camera_device *icd)
750{
751 struct soc_camera_host *ici;
752 int ret = 0;
753
754 mutex_lock(&list_lock);
755
756 list_add_tail(&icd->list, &devices);
757
758 /* Watch out for class_for_each_device / class_find_device API by
759 * Dave Young <hidave.darkstar@gmail.com> */
760 list_for_each_entry(ici, &hosts, list) {
761 if (icd->iface == ici->nr) {
762 ret = 1;
763 icd->dev.parent = &ici->dev;
764 break;
765 }
766 }
767
768 mutex_unlock(&list_lock);
769
770 if (ret)
771 ret = device_register_link(icd);
772
773 return ret;
774}
775
776static int soc_camera_probe(struct device *dev)
777{
778 struct soc_camera_device *icd = to_soc_camera_dev(dev);
779 struct soc_camera_host *ici =
780 to_soc_camera_host(icd->dev.parent);
781 int ret;
782
Guennadi Liakhovetski26f1b942008-03-24 12:18:36 -0300783 if (!icd->ops->probe)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300784 return -ENODEV;
785
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300786 /* We only call ->add() here to activate and probe the camera.
787 * We shall ->remove() and deactivate it immediately afterwards. */
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300788 ret = ici->ops->add(icd);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300789 if (ret < 0)
790 return ret;
791
Guennadi Liakhovetski26f1b942008-03-24 12:18:36 -0300792 ret = icd->ops->probe(icd);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300793 if (ret >= 0) {
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300794 const struct v4l2_queryctrl *qctrl;
795
796 qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_GAIN);
797 icd->gain = qctrl ? qctrl->default_value : (unsigned short)~0;
798 qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
799 icd->exposure = qctrl ? qctrl->default_value :
800 (unsigned short)~0;
801 }
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300802 ici->ops->remove(icd);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300803
804 return ret;
805}
806
807/* This is called on device_unregister, which only means we have to disconnect
808 * from the host, but not remove ourselves from the device list */
809static int soc_camera_remove(struct device *dev)
810{
811 struct soc_camera_device *icd = to_soc_camera_dev(dev);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300812
Guennadi Liakhovetski26f1b942008-03-24 12:18:36 -0300813 if (icd->ops->remove)
814 icd->ops->remove(icd);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300815
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300816 return 0;
817}
818
Robert Jarzmik2e521062008-08-01 20:14:50 -0300819static int soc_camera_suspend(struct device *dev, pm_message_t state)
820{
821 struct soc_camera_device *icd = to_soc_camera_dev(dev);
822 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
823 int ret = 0;
824
825 if (ici->ops->suspend)
826 ret = ici->ops->suspend(icd, state);
827
828 return ret;
829}
830
831static int soc_camera_resume(struct device *dev)
832{
833 struct soc_camera_device *icd = to_soc_camera_dev(dev);
834 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
835 int ret = 0;
836
837 if (ici->ops->resume)
838 ret = ici->ops->resume(icd);
839
840 return ret;
841}
842
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300843static struct bus_type soc_camera_bus_type = {
844 .name = "soc-camera",
845 .probe = soc_camera_probe,
846 .remove = soc_camera_remove,
Robert Jarzmik2e521062008-08-01 20:14:50 -0300847 .suspend = soc_camera_suspend,
848 .resume = soc_camera_resume,
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300849};
850
851static struct device_driver ic_drv = {
852 .name = "camera",
853 .bus = &soc_camera_bus_type,
854 .owner = THIS_MODULE,
855};
856
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300857static void dummy_release(struct device *dev)
858{
859}
860
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300861int soc_camera_host_register(struct soc_camera_host *ici)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300862{
863 int ret;
864 struct soc_camera_host *ix;
865
Paulius Zaleckas092d3922008-07-11 20:50:31 -0300866 if (!ici->ops->init_videobuf || !ici->ops->add || !ici->ops->remove)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300867 return -EINVAL;
868
869 /* Number might be equal to the platform device ID */
Kay Sieversaf128a12008-10-30 00:51:46 -0300870 dev_set_name(&ici->dev, "camera_host%d", ici->nr);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300871
872 mutex_lock(&list_lock);
873 list_for_each_entry(ix, &hosts, list) {
874 if (ix->nr == ici->nr) {
875 mutex_unlock(&list_lock);
876 return -EBUSY;
877 }
878 }
879
880 list_add_tail(&ici->list, &hosts);
881 mutex_unlock(&list_lock);
882
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300883 ici->dev.release = dummy_release;
884
885 ret = device_register(&ici->dev);
886
887 if (ret)
888 goto edevr;
889
890 scan_add_host(ici);
891
892 return 0;
893
894edevr:
895 mutex_lock(&list_lock);
896 list_del(&ici->list);
897 mutex_unlock(&list_lock);
898
899 return ret;
900}
901EXPORT_SYMBOL(soc_camera_host_register);
902
903/* Unregister all clients! */
904void soc_camera_host_unregister(struct soc_camera_host *ici)
905{
906 struct soc_camera_device *icd;
907
908 mutex_lock(&list_lock);
909
910 list_del(&ici->list);
911
912 list_for_each_entry(icd, &devices, list) {
913 if (icd->dev.parent == &ici->dev) {
914 device_unregister(&icd->dev);
915 /* Not before device_unregister(), .remove
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300916 * needs parent to call ici->ops->remove() */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300917 icd->dev.parent = NULL;
918 memset(&icd->dev.kobj, 0, sizeof(icd->dev.kobj));
919 }
920 }
921
922 mutex_unlock(&list_lock);
923
924 device_unregister(&ici->dev);
925}
926EXPORT_SYMBOL(soc_camera_host_unregister);
927
928/* Image capture device */
929int soc_camera_device_register(struct soc_camera_device *icd)
930{
931 struct soc_camera_device *ix;
932 int num = -1, i;
933
934 if (!icd)
935 return -EINVAL;
936
937 for (i = 0; i < 256 && num < 0; i++) {
938 num = i;
939 list_for_each_entry(ix, &devices, list) {
940 if (ix->iface == icd->iface && ix->devnum == i) {
941 num = -1;
942 break;
943 }
944 }
945 }
946
947 if (num < 0)
948 /* ok, we have 256 cameras on this host...
949 * man, stay reasonable... */
950 return -ENOMEM;
951
952 icd->devnum = num;
953 icd->dev.bus = &soc_camera_bus_type;
Kay Sieversaf128a12008-10-30 00:51:46 -0300954 dev_set_name(&icd->dev, "%u-%u", icd->iface, icd->devnum);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300955
956 icd->dev.release = dummy_release;
957
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300958 return scan_add_device(icd);
959}
960EXPORT_SYMBOL(soc_camera_device_register);
961
962void soc_camera_device_unregister(struct soc_camera_device *icd)
963{
964 mutex_lock(&list_lock);
965 list_del(&icd->list);
966
967 /* The bus->remove will be eventually called */
968 if (icd->dev.parent)
969 device_unregister(&icd->dev);
970 mutex_unlock(&list_lock);
971}
972EXPORT_SYMBOL(soc_camera_device_unregister);
973
Hans Verkuila3998102008-07-21 02:57:38 -0300974static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
975 .vidioc_querycap = soc_camera_querycap,
976 .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
977 .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
978 .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
979 .vidioc_enum_input = soc_camera_enum_input,
980 .vidioc_g_input = soc_camera_g_input,
981 .vidioc_s_input = soc_camera_s_input,
982 .vidioc_s_std = soc_camera_s_std,
983 .vidioc_reqbufs = soc_camera_reqbufs,
984 .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
985 .vidioc_querybuf = soc_camera_querybuf,
986 .vidioc_qbuf = soc_camera_qbuf,
987 .vidioc_dqbuf = soc_camera_dqbuf,
988 .vidioc_streamon = soc_camera_streamon,
989 .vidioc_streamoff = soc_camera_streamoff,
990 .vidioc_queryctrl = soc_camera_queryctrl,
991 .vidioc_g_ctrl = soc_camera_g_ctrl,
992 .vidioc_s_ctrl = soc_camera_s_ctrl,
993 .vidioc_cropcap = soc_camera_cropcap,
994 .vidioc_g_crop = soc_camera_g_crop,
995 .vidioc_s_crop = soc_camera_s_crop,
996 .vidioc_g_chip_ident = soc_camera_g_chip_ident,
997#ifdef CONFIG_VIDEO_ADV_DEBUG
998 .vidioc_g_register = soc_camera_g_register,
999 .vidioc_s_register = soc_camera_s_register,
1000#endif
1001};
1002
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001003int soc_camera_video_start(struct soc_camera_device *icd)
1004{
1005 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1006 int err = -ENOMEM;
1007 struct video_device *vdev;
1008
1009 if (!icd->dev.parent)
1010 return -ENODEV;
1011
1012 vdev = video_device_alloc();
1013 if (!vdev)
1014 goto evidallocd;
1015 dev_dbg(&ici->dev, "Allocated video_device %p\n", vdev);
1016
1017 strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
1018 /* Maybe better &ici->dev */
Hans Verkuil5e85e732008-07-20 06:31:39 -03001019 vdev->parent = &icd->dev;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001020 vdev->current_norm = V4L2_STD_UNKNOWN;
1021 vdev->fops = &soc_camera_fops;
Hans Verkuila3998102008-07-21 02:57:38 -03001022 vdev->ioctl_ops = &soc_camera_ioctl_ops;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001023 vdev->release = video_device_release;
1024 vdev->minor = -1;
1025 vdev->tvnorms = V4L2_STD_UNKNOWN,
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001026
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001027 err = video_register_device(vdev, VFL_TYPE_GRABBER, vdev->minor);
1028 if (err < 0) {
Hans Verkuil5e85e732008-07-20 06:31:39 -03001029 dev_err(vdev->parent, "video_register_device failed\n");
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001030 goto evidregd;
1031 }
1032 icd->vdev = vdev;
1033
1034 return 0;
1035
1036evidregd:
1037 video_device_release(vdev);
1038evidallocd:
1039 return err;
1040}
1041EXPORT_SYMBOL(soc_camera_video_start);
1042
1043void soc_camera_video_stop(struct soc_camera_device *icd)
1044{
1045 struct video_device *vdev = icd->vdev;
1046
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001047 dev_dbg(&icd->dev, "%s\n", __func__);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001048
1049 if (!icd->dev.parent || !vdev)
1050 return;
1051
1052 mutex_lock(&video_lock);
1053 video_unregister_device(vdev);
1054 icd->vdev = NULL;
1055 mutex_unlock(&video_lock);
1056}
1057EXPORT_SYMBOL(soc_camera_video_stop);
1058
1059static int __init soc_camera_init(void)
1060{
1061 int ret = bus_register(&soc_camera_bus_type);
1062 if (ret)
1063 return ret;
1064 ret = driver_register(&ic_drv);
1065 if (ret)
1066 goto edrvr;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001067
1068 return 0;
1069
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001070edrvr:
1071 bus_unregister(&soc_camera_bus_type);
1072 return ret;
1073}
1074
1075static void __exit soc_camera_exit(void)
1076{
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001077 driver_unregister(&ic_drv);
1078 bus_unregister(&soc_camera_bus_type);
1079}
1080
1081module_init(soc_camera_init);
1082module_exit(soc_camera_exit);
1083
1084MODULE_DESCRIPTION("Image capture bus driver");
1085MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1086MODULE_LICENSE("GPL");