V4L/DVB (12513): soc-camera: add support for camera-host controls
[deliverable/linux.git] / drivers / media / video / soc_camera.c
CommitLineData
e55222ef
GL
1/*
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
e55222ef 19#include <linux/device.h>
e55222ef 20#include <linux/err.h>
0fd327bd
GL
21#include <linux/i2c.h>
22#include <linux/init.h>
23#include <linux/list.h>
e55222ef 24#include <linux/mutex.h>
40e2e092 25#include <linux/module.h>
0fd327bd 26#include <linux/platform_device.h>
e55222ef
GL
27#include <linux/vmalloc.h>
28
0fd327bd 29#include <media/soc_camera.h>
e55222ef 30#include <media/v4l2-common.h>
0fd327bd 31#include <media/v4l2-ioctl.h>
40e2e092 32#include <media/v4l2-dev.h>
092d3921 33#include <media/videobuf-core.h>
e55222ef 34
df2ed070
GL
35/* Default to VGA resolution */
36#define DEFAULT_WIDTH 640
37#define DEFAULT_HEIGHT 480
38
e55222ef
GL
39static LIST_HEAD(hosts);
40static LIST_HEAD(devices);
40e2e092 41static DEFINE_MUTEX(list_lock); /* Protects the list of hosts */
e55222ef 42
25c4d74e 43const struct soc_camera_data_format *soc_camera_format_by_fourcc(
abe4c471 44 struct soc_camera_device *icd, unsigned int fourcc)
e55222ef
GL
45{
46 unsigned int i;
47
26f1b942
GL
48 for (i = 0; i < icd->num_formats; i++)
49 if (icd->formats[i].fourcc == fourcc)
50 return icd->formats + i;
e55222ef
GL
51 return NULL;
52}
25c4d74e 53EXPORT_SYMBOL(soc_camera_format_by_fourcc);
e55222ef 54
c2786ad2
GL
55const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
56 struct soc_camera_device *icd, unsigned int fourcc)
57{
58 unsigned int i;
59
60 for (i = 0; i < icd->num_user_formats; i++)
61 if (icd->user_formats[i].host_fmt->fourcc == fourcc)
62 return icd->user_formats + i;
63 return NULL;
64}
65EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);
66
bd73b36f
GL
67/**
68 * soc_camera_apply_sensor_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
69 * @icl: camera platform parameters
70 * @flags: flags to be inverted according to platform configuration
71 * @return: resulting flags
72 */
73unsigned long soc_camera_apply_sensor_flags(struct soc_camera_link *icl,
74 unsigned long flags)
75{
76 unsigned long f;
77
78 /* If only one of the two polarities is supported, switch to the opposite */
79 if (icl->flags & SOCAM_SENSOR_INVERT_HSYNC) {
80 f = flags & (SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW);
81 if (f == SOCAM_HSYNC_ACTIVE_HIGH || f == SOCAM_HSYNC_ACTIVE_LOW)
82 flags ^= SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW;
83 }
84
85 if (icl->flags & SOCAM_SENSOR_INVERT_VSYNC) {
86 f = flags & (SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW);
87 if (f == SOCAM_VSYNC_ACTIVE_HIGH || f == SOCAM_VSYNC_ACTIVE_LOW)
88 flags ^= SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW;
89 }
90
91 if (icl->flags & SOCAM_SENSOR_INVERT_PCLK) {
92 f = flags & (SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING);
93 if (f == SOCAM_PCLK_SAMPLE_RISING || f == SOCAM_PCLK_SAMPLE_FALLING)
94 flags ^= SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING;
95 }
96
97 return flags;
98}
99EXPORT_SYMBOL(soc_camera_apply_sensor_flags);
100
72937890 101static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
abe4c471 102 struct v4l2_format *f)
e55222ef
GL
103{
104 struct soc_camera_file *icf = file->private_data;
105 struct soc_camera_device *icd = icf->icd;
64f5905e 106 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
e55222ef
GL
107
108 WARN_ON(priv != file->private_data);
109
ad5f2e85 110 /* limit format to hardware capabilities */
06daa1af 111 return ici->ops->try_fmt(icd, f);
e55222ef
GL
112}
113
114static int soc_camera_enum_input(struct file *file, void *priv,
115 struct v4l2_input *inp)
116{
34d359db
KM
117 struct soc_camera_file *icf = file->private_data;
118 struct soc_camera_device *icd = icf->icd;
119 int ret = 0;
120
e55222ef
GL
121 if (inp->index != 0)
122 return -EINVAL;
123
34d359db
KM
124 if (icd->ops->enum_input)
125 ret = icd->ops->enum_input(icd, inp);
126 else {
127 /* default is camera */
128 inp->type = V4L2_INPUT_TYPE_CAMERA;
129 inp->std = V4L2_STD_UNKNOWN;
130 strcpy(inp->name, "Camera");
131 }
e55222ef 132
34d359db 133 return ret;
e55222ef
GL
134}
135
136static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
137{
138 *i = 0;
139
140 return 0;
141}
142
143static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
144{
145 if (i > 0)
146 return -EINVAL;
147
148 return 0;
149}
150
151static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
152{
513791ab
KM
153 struct soc_camera_file *icf = file->private_data;
154 struct soc_camera_device *icd = icf->icd;
155 int ret = 0;
156
157 if (icd->ops->set_std)
158 ret = icd->ops->set_std(icd, a);
159
160 return ret;
e55222ef
GL
161}
162
163static int soc_camera_reqbufs(struct file *file, void *priv,
164 struct v4l2_requestbuffers *p)
165{
166 int ret;
167 struct soc_camera_file *icf = file->private_data;
168 struct soc_camera_device *icd = icf->icd;
64f5905e 169 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
e55222ef
GL
170
171 WARN_ON(priv != file->private_data);
172
e55222ef
GL
173 ret = videobuf_reqbufs(&icf->vb_vidq, p);
174 if (ret < 0)
175 return ret;
176
b8d9904c 177 return ici->ops->reqbufs(icf, p);
e55222ef
GL
178}
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
40e2e092 210/* Always entered with .video_lock held */
c2786ad2
GL
211static int soc_camera_init_user_formats(struct soc_camera_device *icd)
212{
213 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
214 int i, fmts = 0;
215
216 if (!ici->ops->get_formats)
217 /*
218 * Fallback mode - the host will have to serve all
219 * sensor-provided formats one-to-one to the user
220 */
221 fmts = icd->num_formats;
222 else
223 /*
224 * First pass - only count formats this host-sensor
225 * configuration can provide
226 */
227 for (i = 0; i < icd->num_formats; i++)
228 fmts += ici->ops->get_formats(icd, i, NULL);
229
230 if (!fmts)
231 return -ENXIO;
232
233 icd->user_formats =
234 vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
235 if (!icd->user_formats)
236 return -ENOMEM;
237
238 icd->num_user_formats = fmts;
c2786ad2
GL
239
240 dev_dbg(&icd->dev, "Found %d supported formats.\n", fmts);
241
242 /* Second pass - actually fill data formats */
14df2cce 243 fmts = 0;
c2786ad2
GL
244 for (i = 0; i < icd->num_formats; i++)
245 if (!ici->ops->get_formats) {
246 icd->user_formats[i].host_fmt = icd->formats + i;
247 icd->user_formats[i].cam_fmt = icd->formats + i;
248 icd->user_formats[i].buswidth = icd->formats[i].depth;
249 } else {
250 fmts += ici->ops->get_formats(icd, i,
251 &icd->user_formats[fmts]);
252 }
253
254 icd->current_fmt = icd->user_formats[0].host_fmt;
255
256 return 0;
257}
258
40e2e092 259/* Always entered with .video_lock held */
c2786ad2
GL
260static void soc_camera_free_user_formats(struct soc_camera_device *icd)
261{
40e2e092 262 icd->current_fmt = NULL;
c2786ad2 263 vfree(icd->user_formats);
40e2e092 264 icd->user_formats = NULL;
c2786ad2
GL
265}
266
df2ed070
GL
267/* Called with .vb_lock held */
268static int soc_camera_set_fmt(struct soc_camera_file *icf,
269 struct v4l2_format *f)
270{
271 struct soc_camera_device *icd = icf->icd;
272 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
273 struct v4l2_pix_format *pix = &f->fmt.pix;
274 int ret;
275
276 /* We always call try_fmt() before set_fmt() or set_crop() */
277 ret = ici->ops->try_fmt(icd, f);
278 if (ret < 0)
279 return ret;
280
281 ret = ici->ops->set_fmt(icd, f);
282 if (ret < 0) {
283 return ret;
284 } else if (!icd->current_fmt ||
285 icd->current_fmt->fourcc != pix->pixelformat) {
979ea1dd 286 dev_err(ici->v4l2_dev.dev,
df2ed070
GL
287 "Host driver hasn't set up current format correctly!\n");
288 return -EINVAL;
289 }
290
291 icd->width = pix->width;
292 icd->height = pix->height;
025c18a1
GL
293 icf->vb_vidq.field =
294 icd->field = pix->field;
295
df2ed070
GL
296 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
297 dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n",
298 f->type);
299
300 dev_dbg(&icd->dev, "set width: %d height: %d\n",
301 icd->width, icd->height);
302
303 /* set physical bus parameters */
304 return ici->ops->set_bus_param(icd, pix->pixelformat);
305}
306
bec43661 307static int soc_camera_open(struct file *file)
e55222ef 308{
979ea1dd
GL
309 struct video_device *vdev = video_devdata(file);
310 struct soc_camera_device *icd = container_of(vdev->parent, struct soc_camera_device, dev);
311 struct soc_camera_link *icl = to_soc_camera_link(icd);
9dc4e48f 312 struct soc_camera_host *ici;
e55222ef
GL
313 struct soc_camera_file *icf;
314 int ret;
315
40e2e092
GL
316 if (!icd->ops)
317 /* No device driver attached */
318 return -ENODEV;
319
9dc4e48f 320 ici = to_soc_camera_host(icd->dev.parent);
e55222ef 321
40e2e092
GL
322 icf = vmalloc(sizeof(*icf));
323 if (!icf)
324 return -ENOMEM;
325
b8d9904c 326 if (!try_module_get(ici->ops->owner)) {
e55222ef
GL
327 dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
328 ret = -EINVAL;
329 goto emgi;
330 }
331
40e2e092 332 /* Protect against icd->ops->remove() until we module_get() both drivers. */
1c3bb743
GL
333 mutex_lock(&icd->video_lock);
334
9dc4e48f 335 icf->icd = icd;
1a0063a9
GL
336 icd->use_count++;
337
9dc4e48f
GL
338 /* Now we really have to activate the camera */
339 if (icd->use_count == 1) {
025c18a1 340 /* Restore parameters before the last close() per V4L2 API */
df2ed070
GL
341 struct v4l2_format f = {
342 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
343 .fmt.pix = {
025c18a1
GL
344 .width = icd->width,
345 .height = icd->height,
346 .field = icd->field,
df2ed070
GL
347 },
348 };
349
40e2e092
GL
350 ret = soc_camera_init_user_formats(icd);
351 if (ret < 0)
352 goto eiufmt;
353
40e2e092
GL
354 f.fmt.pix.pixelformat = icd->current_fmt->fourcc;
355 f.fmt.pix.colorspace = icd->current_fmt->colorspace;
356
979ea1dd
GL
357 if (icl->power) {
358 ret = icl->power(icd->pdev, 1);
359 if (ret < 0)
360 goto epower;
361 }
362
363 /* The camera could have been already on, try to reset */
364 if (icl->reset)
365 icl->reset(icd->pdev);
366
b8d9904c 367 ret = ici->ops->add(icd);
9dc4e48f
GL
368 if (ret < 0) {
369 dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
9dc4e48f
GL
370 goto eiciadd;
371 }
df2ed070 372
979ea1dd
GL
373 if (icd->ops->init) {
374 ret = icd->ops->init(icd);
375 if (ret < 0)
376 goto einit;
377 }
378
df2ed070
GL
379 /* Try to configure with default parameters */
380 ret = soc_camera_set_fmt(icf, &f);
381 if (ret < 0)
382 goto esfmt;
9dc4e48f
GL
383 }
384
e55222ef
GL
385 file->private_data = icf;
386 dev_dbg(&icd->dev, "camera device open\n");
387
a034d1b7 388 ici->ops->init_videobuf(&icf->vb_vidq, icd);
e55222ef 389
979ea1dd
GL
390 mutex_unlock(&icd->video_lock);
391
e55222ef
GL
392 return 0;
393
df2ed070 394 /*
979ea1dd 395 * First five errors are entered with the .video_lock held
df2ed070
GL
396 * and use_count == 1
397 */
398esfmt:
979ea1dd
GL
399 if (icd->ops->release)
400 icd->ops->release(icd);
401einit:
df2ed070 402 ici->ops->remove(icd);
9dc4e48f 403eiciadd:
979ea1dd
GL
404 if (icl->power)
405 icl->power(icd->pdev, 0);
406epower:
40e2e092
GL
407 soc_camera_free_user_formats(icd);
408eiufmt:
c2786ad2 409 icd->use_count--;
1c3bb743 410 mutex_unlock(&icd->video_lock);
b8d9904c 411 module_put(ici->ops->owner);
e55222ef 412emgi:
e55222ef
GL
413 vfree(icf);
414 return ret;
415}
416
bec43661 417static int soc_camera_close(struct file *file)
e55222ef
GL
418{
419 struct soc_camera_file *icf = file->private_data;
420 struct soc_camera_device *icd = icf->icd;
421 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
422 struct video_device *vdev = icd->vdev;
423
1c3bb743 424 mutex_lock(&icd->video_lock);
9dc4e48f 425 icd->use_count--;
40e2e092 426 if (!icd->use_count) {
979ea1dd
GL
427 struct soc_camera_link *icl = to_soc_camera_link(icd);
428
429 if (icd->ops->release)
430 icd->ops->release(icd);
b8d9904c 431 ici->ops->remove(icd);
979ea1dd
GL
432 if (icl->power)
433 icl->power(icd->pdev, 0);
40e2e092
GL
434 soc_camera_free_user_formats(icd);
435 }
025c18a1 436
1c3bb743
GL
437 mutex_unlock(&icd->video_lock);
438
b8d9904c 439 module_put(ici->ops->owner);
9dc4e48f 440
1a0063a9 441 vfree(icf);
e55222ef 442
5e85e732 443 dev_dbg(vdev->parent, "camera device close\n");
e55222ef
GL
444
445 return 0;
446}
447
aba360d8 448static ssize_t soc_camera_read(struct file *file, char __user *buf,
abe4c471 449 size_t count, loff_t *ppos)
e55222ef
GL
450{
451 struct soc_camera_file *icf = file->private_data;
452 struct soc_camera_device *icd = icf->icd;
453 struct video_device *vdev = icd->vdev;
454 int err = -EINVAL;
455
5e85e732 456 dev_err(vdev->parent, "camera device read not implemented\n");
e55222ef
GL
457
458 return err;
459}
460
461static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
462{
463 struct soc_camera_file *icf = file->private_data;
464 struct soc_camera_device *icd = icf->icd;
465 int err;
466
467 dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
468
469 err = videobuf_mmap_mapper(&icf->vb_vidq, vma);
470
471 dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
472 (unsigned long)vma->vm_start,
473 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
474 err);
475
476 return err;
477}
478
479static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
480{
481 struct soc_camera_file *icf = file->private_data;
482 struct soc_camera_device *icd = icf->icd;
64f5905e 483 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
e55222ef
GL
484
485 if (list_empty(&icf->vb_vidq.stream)) {
486 dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
487 return POLLERR;
488 }
489
b8d9904c 490 return ici->ops->poll(file, pt);
e55222ef
GL
491}
492
bec43661 493static struct v4l2_file_operations soc_camera_fops = {
e55222ef
GL
494 .owner = THIS_MODULE,
495 .open = soc_camera_open,
496 .release = soc_camera_close,
497 .ioctl = video_ioctl2,
498 .read = soc_camera_read,
499 .mmap = soc_camera_mmap,
500 .poll = soc_camera_poll,
e55222ef
GL
501};
502
72937890 503static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
abe4c471 504 struct v4l2_format *f)
e55222ef
GL
505{
506 struct soc_camera_file *icf = file->private_data;
507 struct soc_camera_device *icd = icf->icd;
e55222ef 508 int ret;
e55222ef
GL
509
510 WARN_ON(priv != file->private_data);
511
1c3bb743
GL
512 mutex_lock(&icf->vb_vidq.vb_lock);
513
514 if (videobuf_queue_is_busy(&icf->vb_vidq)) {
515 dev_err(&icd->dev, "S_FMT denied: queue busy\n");
516 ret = -EBUSY;
517 goto unlock;
518 }
519
df2ed070 520 ret = soc_camera_set_fmt(icf, f);
1c3bb743
GL
521
522unlock:
523 mutex_unlock(&icf->vb_vidq.vb_lock);
524
525 return ret;
e55222ef
GL
526}
527
72937890 528static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
abe4c471 529 struct v4l2_fmtdesc *f)
e55222ef
GL
530{
531 struct soc_camera_file *icf = file->private_data;
532 struct soc_camera_device *icd = icf->icd;
533 const struct soc_camera_data_format *format;
534
535 WARN_ON(priv != file->private_data);
536
c2786ad2 537 if (f->index >= icd->num_user_formats)
e55222ef
GL
538 return -EINVAL;
539
c2786ad2 540 format = icd->user_formats[f->index].host_fmt;
e55222ef
GL
541
542 strlcpy(f->description, format->name, sizeof(f->description));
543 f->pixelformat = format->fourcc;
544 return 0;
545}
546
72937890 547static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
abe4c471 548 struct v4l2_format *f)
e55222ef
GL
549{
550 struct soc_camera_file *icf = file->private_data;
551 struct soc_camera_device *icd = icf->icd;
64f5905e 552 struct v4l2_pix_format *pix = &f->fmt.pix;
e55222ef
GL
553
554 WARN_ON(priv != file->private_data);
555
64f5905e
GL
556 pix->width = icd->width;
557 pix->height = icd->height;
558 pix->field = icf->vb_vidq.field;
559 pix->pixelformat = icd->current_fmt->fourcc;
560 pix->bytesperline = pix->width *
25c4d74e 561 DIV_ROUND_UP(icd->current_fmt->depth, 8);
64f5905e 562 pix->sizeimage = pix->height * pix->bytesperline;
e55222ef
GL
563 dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
564 icd->current_fmt->fourcc);
565 return 0;
566}
567
568static int soc_camera_querycap(struct file *file, void *priv,
569 struct v4l2_capability *cap)
570{
571 struct soc_camera_file *icf = file->private_data;
572 struct soc_camera_device *icd = icf->icd;
64f5905e 573 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
e55222ef
GL
574
575 WARN_ON(priv != file->private_data);
576
577 strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
b8d9904c 578 return ici->ops->querycap(ici, cap);
e55222ef
GL
579}
580
581static int soc_camera_streamon(struct file *file, void *priv,
582 enum v4l2_buf_type i)
583{
584 struct soc_camera_file *icf = file->private_data;
585 struct soc_camera_device *icd = icf->icd;
979ea1dd 586 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1c3bb743 587 int ret;
e55222ef
GL
588
589 WARN_ON(priv != file->private_data);
590
e55222ef
GL
591 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
592 return -EINVAL;
593
1c3bb743
GL
594 mutex_lock(&icd->video_lock);
595
979ea1dd 596 v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, video, s_stream, 1);
e55222ef
GL
597
598 /* This calls buf_queue from host driver's videobuf_queue_ops */
1c3bb743
GL
599 ret = videobuf_streamon(&icf->vb_vidq);
600
601 mutex_unlock(&icd->video_lock);
602
603 return ret;
e55222ef
GL
604}
605
606static int soc_camera_streamoff(struct file *file, void *priv,
607 enum v4l2_buf_type i)
608{
609 struct soc_camera_file *icf = file->private_data;
610 struct soc_camera_device *icd = icf->icd;
979ea1dd 611 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
e55222ef
GL
612
613 WARN_ON(priv != file->private_data);
614
e55222ef
GL
615 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
616 return -EINVAL;
617
1c3bb743
GL
618 mutex_lock(&icd->video_lock);
619
e55222ef
GL
620 /* This calls buf_release from host driver's videobuf_queue_ops for all
621 * remaining buffers. When the last buffer is freed, stop capture */
622 videobuf_streamoff(&icf->vb_vidq);
623
979ea1dd 624 v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, video, s_stream, 0);
e55222ef 625
1c3bb743
GL
626 mutex_unlock(&icd->video_lock);
627
e55222ef
GL
628 return 0;
629}
630
631static int soc_camera_queryctrl(struct file *file, void *priv,
632 struct v4l2_queryctrl *qc)
633{
634 struct soc_camera_file *icf = file->private_data;
635 struct soc_camera_device *icd = icf->icd;
2840d249 636 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
e55222ef
GL
637 int i;
638
639 WARN_ON(priv != file->private_data);
640
641 if (!qc->id)
642 return -EINVAL;
643
2840d249
GL
644 /* First check host controls */
645 for (i = 0; i < ici->ops->num_controls; i++)
646 if (qc->id == ici->ops->controls[i].id) {
647 memcpy(qc, &(ici->ops->controls[i]),
648 sizeof(*qc));
649 return 0;
650 }
651
652 /* Then device controls */
e55222ef
GL
653 for (i = 0; i < icd->ops->num_controls; i++)
654 if (qc->id == icd->ops->controls[i].id) {
655 memcpy(qc, &(icd->ops->controls[i]),
656 sizeof(*qc));
657 return 0;
658 }
659
660 return -EINVAL;
661}
662
663static int soc_camera_g_ctrl(struct file *file, void *priv,
664 struct v4l2_control *ctrl)
665{
666 struct soc_camera_file *icf = file->private_data;
667 struct soc_camera_device *icd = icf->icd;
979ea1dd 668 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
2840d249 669 int ret;
e55222ef
GL
670
671 WARN_ON(priv != file->private_data);
672
673 switch (ctrl->id) {
674 case V4L2_CID_GAIN:
675 if (icd->gain == (unsigned short)~0)
676 return -EINVAL;
677 ctrl->value = icd->gain;
678 return 0;
679 case V4L2_CID_EXPOSURE:
680 if (icd->exposure == (unsigned short)~0)
681 return -EINVAL;
682 ctrl->value = icd->exposure;
683 return 0;
684 }
685
2840d249
GL
686 if (ici->ops->get_ctrl) {
687 ret = ici->ops->get_ctrl(icd, ctrl);
688 if (ret != -ENOIOCTLCMD)
689 return ret;
690 }
691
979ea1dd 692 return v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, core, g_ctrl, ctrl);
e55222ef
GL
693}
694
695static int soc_camera_s_ctrl(struct file *file, void *priv,
696 struct v4l2_control *ctrl)
697{
698 struct soc_camera_file *icf = file->private_data;
699 struct soc_camera_device *icd = icf->icd;
979ea1dd 700 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
2840d249 701 int ret;
e55222ef
GL
702
703 WARN_ON(priv != file->private_data);
704
2840d249
GL
705 if (ici->ops->set_ctrl) {
706 ret = ici->ops->set_ctrl(icd, ctrl);
707 if (ret != -ENOIOCTLCMD)
708 return ret;
709 }
710
979ea1dd 711 return v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, core, s_ctrl, ctrl);
e55222ef
GL
712}
713
714static int soc_camera_cropcap(struct file *file, void *fh,
715 struct v4l2_cropcap *a)
716{
717 struct soc_camera_file *icf = file->private_data;
718 struct soc_camera_device *icd = icf->icd;
719
720 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
721 a->bounds.left = icd->x_min;
722 a->bounds.top = icd->y_min;
723 a->bounds.width = icd->width_max;
724 a->bounds.height = icd->height_max;
725 a->defrect.left = icd->x_min;
726 a->defrect.top = icd->y_min;
df2ed070
GL
727 a->defrect.width = DEFAULT_WIDTH;
728 a->defrect.height = DEFAULT_HEIGHT;
e55222ef
GL
729 a->pixelaspect.numerator = 1;
730 a->pixelaspect.denominator = 1;
731
732 return 0;
733}
734
735static int soc_camera_g_crop(struct file *file, void *fh,
736 struct v4l2_crop *a)
737{
738 struct soc_camera_file *icf = file->private_data;
739 struct soc_camera_device *icd = icf->icd;
740
741 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
742 a->c.left = icd->x_current;
743 a->c.top = icd->y_current;
744 a->c.width = icd->width;
745 a->c.height = icd->height;
746
747 return 0;
748}
749
750static int soc_camera_s_crop(struct file *file, void *fh,
751 struct v4l2_crop *a)
752{
753 struct soc_camera_file *icf = file->private_data;
754 struct soc_camera_device *icd = icf->icd;
64f5905e 755 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
e55222ef
GL
756 int ret;
757
758 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
759 return -EINVAL;
760
1c3bb743
GL
761 /* Cropping is allowed during a running capture, guard consistency */
762 mutex_lock(&icf->vb_vidq.vb_lock);
763
09e231b3 764 ret = ici->ops->set_crop(icd, &a->c);
e55222ef
GL
765 if (!ret) {
766 icd->width = a->c.width;
767 icd->height = a->c.height;
768 icd->x_current = a->c.left;
769 icd->y_current = a->c.top;
770 }
771
1c3bb743
GL
772 mutex_unlock(&icf->vb_vidq.vb_lock);
773
e55222ef
GL
774 return ret;
775}
776
777static int soc_camera_g_chip_ident(struct file *file, void *fh,
aecde8b5 778 struct v4l2_dbg_chip_ident *id)
e55222ef
GL
779{
780 struct soc_camera_file *icf = file->private_data;
781 struct soc_camera_device *icd = icf->icd;
979ea1dd 782 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
e55222ef 783
979ea1dd 784 return v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, core, g_chip_ident, id);
e55222ef
GL
785}
786
787#ifdef CONFIG_VIDEO_ADV_DEBUG
788static int soc_camera_g_register(struct file *file, void *fh,
aecde8b5 789 struct v4l2_dbg_register *reg)
e55222ef
GL
790{
791 struct soc_camera_file *icf = file->private_data;
792 struct soc_camera_device *icd = icf->icd;
979ea1dd 793 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
e55222ef 794
979ea1dd 795 return v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, core, g_register, reg);
e55222ef
GL
796}
797
798static int soc_camera_s_register(struct file *file, void *fh,
aecde8b5 799 struct v4l2_dbg_register *reg)
e55222ef
GL
800{
801 struct soc_camera_file *icf = file->private_data;
802 struct soc_camera_device *icd = icf->icd;
979ea1dd 803 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
e55222ef 804
979ea1dd 805 return v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, core, s_register, reg);
e55222ef
GL
806}
807#endif
808
e55222ef
GL
809/* So far this function cannot fail */
810static void scan_add_host(struct soc_camera_host *ici)
811{
812 struct soc_camera_device *icd;
813
814 mutex_lock(&list_lock);
815
816 list_for_each_entry(icd, &devices, list) {
817 if (icd->iface == ici->nr) {
40e2e092 818 int ret;
979ea1dd 819 icd->dev.parent = ici->v4l2_dev.dev;
40e2e092
GL
820 dev_set_name(&icd->dev, "%u-%u", icd->iface,
821 icd->devnum);
822 ret = device_register(&icd->dev);
823 if (ret < 0) {
824 icd->dev.parent = NULL;
825 dev_err(&icd->dev,
826 "Cannot register device: %d\n", ret);
827 }
e55222ef
GL
828 }
829 }
830
831 mutex_unlock(&list_lock);
832}
833
40e2e092
GL
834#ifdef CONFIG_I2C_BOARDINFO
835static int soc_camera_init_i2c(struct soc_camera_device *icd,
836 struct soc_camera_link *icl)
e55222ef 837{
40e2e092 838 struct i2c_client *client;
979ea1dd 839 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
40e2e092 840 struct i2c_adapter *adap = i2c_get_adapter(icl->i2c_adapter_id);
979ea1dd 841 struct v4l2_subdev *subdev;
40e2e092 842 int ret;
e55222ef 843
40e2e092
GL
844 if (!adap) {
845 ret = -ENODEV;
846 dev_err(&icd->dev, "Cannot get I2C adapter #%d. No driver?\n",
847 icl->i2c_adapter_id);
848 goto ei2cga;
849 }
e55222ef 850
40e2e092 851 icl->board_info->platform_data = icd;
e55222ef 852
979ea1dd
GL
853 subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
854 icl->module_name, icl->board_info, NULL);
855 if (!subdev) {
40e2e092
GL
856 ret = -ENOMEM;
857 goto ei2cnd;
e55222ef
GL
858 }
859
979ea1dd
GL
860 subdev->grp_id = (__u32)icd;
861 client = subdev->priv;
862
40e2e092
GL
863 /* Use to_i2c_client(dev) to recover the i2c client */
864 dev_set_drvdata(&icd->dev, &client->dev);
e55222ef 865
40e2e092
GL
866 return 0;
867ei2cnd:
868 i2c_put_adapter(adap);
869ei2cga:
e55222ef
GL
870 return ret;
871}
872
40e2e092
GL
873static void soc_camera_free_i2c(struct soc_camera_device *icd)
874{
875 struct i2c_client *client =
876 to_i2c_client(to_soc_camera_control(icd));
877 dev_set_drvdata(&icd->dev, NULL);
979ea1dd 878 v4l2_device_unregister_subdev(i2c_get_clientdata(client));
40e2e092
GL
879 i2c_unregister_device(client);
880 i2c_put_adapter(client->adapter);
881}
882#else
883#define soc_camera_init_i2c(icd, icl) (-ENODEV)
884#define soc_camera_free_i2c(icd) do {} while (0)
885#endif
886
979ea1dd 887static int soc_camera_video_start(struct soc_camera_device *icd);
40e2e092
GL
888static int video_dev_create(struct soc_camera_device *icd);
889/* Called during host-driver probe */
e55222ef
GL
890static int soc_camera_probe(struct device *dev)
891{
892 struct soc_camera_device *icd = to_soc_camera_dev(dev);
979ea1dd 893 struct soc_camera_host *ici = to_soc_camera_host(dev->parent);
40e2e092 894 struct soc_camera_link *icl = to_soc_camera_link(icd);
979ea1dd 895 struct device *control = NULL;
e55222ef
GL
896 int ret;
897
40e2e092 898 dev_info(dev, "Probing %s\n", dev_name(dev));
1c3bb743 899
979ea1dd
GL
900 if (icl->power) {
901 ret = icl->power(icd->pdev, 1);
902 if (ret < 0) {
903 dev_err(dev,
904 "Platform failed to power-on the camera.\n");
905 goto epower;
906 }
907 }
908
909 /* The camera could have been already on, try to reset */
910 if (icl->reset)
911 icl->reset(icd->pdev);
912
913 ret = ici->ops->add(icd);
914 if (ret < 0)
915 goto eadd;
916
917 /* Must have icd->vdev before registering the device */
40e2e092
GL
918 ret = video_dev_create(icd);
919 if (ret < 0)
920 goto evdc;
1c3bb743 921
40e2e092
GL
922 /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
923 if (icl->board_info) {
924 ret = soc_camera_init_i2c(icd, icl);
925 if (ret < 0)
926 goto eadddev;
927 } else if (!icl->add_device || !icl->del_device) {
1c3bb743 928 ret = -EINVAL;
40e2e092
GL
929 goto eadddev;
930 } else {
979ea1dd
GL
931 if (icl->module_name)
932 ret = request_module(icl->module_name);
933
40e2e092
GL
934 ret = icl->add_device(icl, &icd->dev);
935 if (ret < 0)
936 goto eadddev;
1c3bb743 937
979ea1dd
GL
938 /* FIXME: this is racy, have to use driver-binding notification */
939 control = to_soc_camera_control(icd);
940 if (!control || !control->driver ||
941 !try_module_get(control->driver->owner)) {
942 icl->del_device(icl);
943 goto enodrv;
944 }
40e2e092 945 }
e55222ef 946
979ea1dd
GL
947 /* ..._video_start() will create a device node, so we have to protect */
948 mutex_lock(&icd->video_lock);
949
950 ret = soc_camera_video_start(icd);
951 if (ret < 0)
952 goto evidstart;
953
40e2e092
GL
954 /* Do we have to sysfs_remove_link() before device_unregister()? */
955 if (to_soc_camera_control(icd) &&
956 sysfs_create_link(&icd->dev.kobj, &to_soc_camera_control(icd)->kobj,
957 "control"))
958 dev_warn(&icd->dev, "Failed creating the control symlink\n");
025c18a1 959
979ea1dd
GL
960 ici->ops->remove(icd);
961
962 if (icl->power)
963 icl->power(icd->pdev, 0);
964
965 mutex_unlock(&icd->video_lock);
025c18a1 966
40e2e092 967 return 0;
e55222ef 968
979ea1dd
GL
969evidstart:
970 mutex_unlock(&icd->video_lock);
971 if (icl->board_info) {
40e2e092 972 soc_camera_free_i2c(icd);
979ea1dd 973 } else {
40e2e092 974 icl->del_device(icl);
979ea1dd
GL
975 module_put(control->driver->owner);
976 }
977enodrv:
40e2e092
GL
978eadddev:
979 video_device_release(icd->vdev);
980evdc:
979ea1dd
GL
981 ici->ops->remove(icd);
982eadd:
983 if (icl->power)
984 icl->power(icd->pdev, 0);
985epower:
e55222ef
GL
986 return ret;
987}
988
989/* This is called on device_unregister, which only means we have to disconnect
990 * from the host, but not remove ourselves from the device list */
991static int soc_camera_remove(struct device *dev)
992{
993 struct soc_camera_device *icd = to_soc_camera_dev(dev);
40e2e092
GL
994 struct soc_camera_link *icl = to_soc_camera_link(icd);
995 struct video_device *vdev = icd->vdev;
e55222ef 996
40e2e092 997 BUG_ON(!dev->parent);
e55222ef 998
40e2e092
GL
999 if (vdev) {
1000 mutex_lock(&icd->video_lock);
1001 video_unregister_device(vdev);
1002 icd->vdev = NULL;
1003 mutex_unlock(&icd->video_lock);
1004 }
1005
979ea1dd 1006 if (icl->board_info) {
40e2e092 1007 soc_camera_free_i2c(icd);
979ea1dd
GL
1008 } else {
1009 struct device_driver *drv = to_soc_camera_control(icd) ?
1010 to_soc_camera_control(icd)->driver : NULL;
1011 if (drv) {
1012 icl->del_device(icl);
1013 module_put(drv->owner);
1014 }
1015 }
025c18a1 1016
e55222ef
GL
1017 return 0;
1018}
1019
2e521061
RJ
1020static int soc_camera_suspend(struct device *dev, pm_message_t state)
1021{
1022 struct soc_camera_device *icd = to_soc_camera_dev(dev);
1023 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1024 int ret = 0;
1025
1026 if (ici->ops->suspend)
1027 ret = ici->ops->suspend(icd, state);
1028
1029 return ret;
1030}
1031
1032static int soc_camera_resume(struct device *dev)
1033{
1034 struct soc_camera_device *icd = to_soc_camera_dev(dev);
1035 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1036 int ret = 0;
1037
1038 if (ici->ops->resume)
1039 ret = ici->ops->resume(icd);
1040
1041 return ret;
1042}
1043
e55222ef
GL
1044static struct bus_type soc_camera_bus_type = {
1045 .name = "soc-camera",
1046 .probe = soc_camera_probe,
1047 .remove = soc_camera_remove,
2e521061
RJ
1048 .suspend = soc_camera_suspend,
1049 .resume = soc_camera_resume,
e55222ef
GL
1050};
1051
1052static struct device_driver ic_drv = {
1053 .name = "camera",
1054 .bus = &soc_camera_bus_type,
1055 .owner = THIS_MODULE,
1056};
1057
e55222ef
GL
1058static void dummy_release(struct device *dev)
1059{
1060}
1061
b8d9904c 1062int soc_camera_host_register(struct soc_camera_host *ici)
e55222ef 1063{
e55222ef 1064 struct soc_camera_host *ix;
979ea1dd 1065 int ret;
e55222ef 1066
64f5905e
GL
1067 if (!ici || !ici->ops ||
1068 !ici->ops->try_fmt ||
1069 !ici->ops->set_fmt ||
09e231b3 1070 !ici->ops->set_crop ||
64f5905e
GL
1071 !ici->ops->set_bus_param ||
1072 !ici->ops->querycap ||
1073 !ici->ops->init_videobuf ||
1074 !ici->ops->reqbufs ||
1075 !ici->ops->add ||
1076 !ici->ops->remove ||
eff505fa 1077 !ici->ops->poll ||
979ea1dd 1078 !ici->v4l2_dev.dev)
e55222ef
GL
1079 return -EINVAL;
1080
e55222ef
GL
1081 mutex_lock(&list_lock);
1082 list_for_each_entry(ix, &hosts, list) {
1083 if (ix->nr == ici->nr) {
979ea1dd
GL
1084 ret = -EBUSY;
1085 goto edevreg;
e55222ef
GL
1086 }
1087 }
1088
979ea1dd
GL
1089 ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
1090 if (ret < 0)
1091 goto edevreg;
eff505fa 1092
e55222ef
GL
1093 list_add_tail(&ici->list, &hosts);
1094 mutex_unlock(&list_lock);
1095
e55222ef
GL
1096 scan_add_host(ici);
1097
1098 return 0;
979ea1dd
GL
1099
1100edevreg:
1101 mutex_unlock(&list_lock);
1102 return ret;
e55222ef
GL
1103}
1104EXPORT_SYMBOL(soc_camera_host_register);
1105
1106/* Unregister all clients! */
1107void soc_camera_host_unregister(struct soc_camera_host *ici)
1108{
1109 struct soc_camera_device *icd;
1110
1111 mutex_lock(&list_lock);
1112
1113 list_del(&ici->list);
1114
1115 list_for_each_entry(icd, &devices, list) {
979ea1dd 1116 if (icd->iface == ici->nr) {
40e2e092 1117 /* The bus->remove will be called */
e55222ef
GL
1118 device_unregister(&icd->dev);
1119 /* Not before device_unregister(), .remove
b8d9904c 1120 * needs parent to call ici->ops->remove() */
e55222ef 1121 icd->dev.parent = NULL;
40e2e092
GL
1122
1123 /* If the host module is loaded again, device_register()
1124 * would complain "already initialised" */
e55222ef
GL
1125 memset(&icd->dev.kobj, 0, sizeof(icd->dev.kobj));
1126 }
1127 }
1128
1129 mutex_unlock(&list_lock);
1130
979ea1dd 1131 v4l2_device_unregister(&ici->v4l2_dev);
e55222ef
GL
1132}
1133EXPORT_SYMBOL(soc_camera_host_unregister);
1134
1135/* Image capture device */
40e2e092 1136static int soc_camera_device_register(struct soc_camera_device *icd)
e55222ef
GL
1137{
1138 struct soc_camera_device *ix;
1139 int num = -1, i;
1140
e55222ef
GL
1141 for (i = 0; i < 256 && num < 0; i++) {
1142 num = i;
40e2e092 1143 /* Check if this index is available on this interface */
e55222ef
GL
1144 list_for_each_entry(ix, &devices, list) {
1145 if (ix->iface == icd->iface && ix->devnum == i) {
1146 num = -1;
1147 break;
1148 }
1149 }
1150 }
1151
1152 if (num < 0)
1153 /* ok, we have 256 cameras on this host...
1154 * man, stay reasonable... */
1155 return -ENOMEM;
1156
1157 icd->devnum = num;
1158 icd->dev.bus = &soc_camera_bus_type;
e55222ef 1159
64f5905e
GL
1160 icd->dev.release = dummy_release;
1161 icd->use_count = 0;
1162 icd->host_priv = NULL;
1c3bb743 1163 mutex_init(&icd->video_lock);
e55222ef 1164
40e2e092
GL
1165 list_add_tail(&icd->list, &devices);
1166
1167 return 0;
e55222ef 1168}
e55222ef 1169
40e2e092 1170static void soc_camera_device_unregister(struct soc_camera_device *icd)
e55222ef 1171{
e55222ef 1172 list_del(&icd->list);
e55222ef 1173}
e55222ef 1174
a399810c
HV
1175static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
1176 .vidioc_querycap = soc_camera_querycap,
1177 .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
1178 .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
1179 .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
1180 .vidioc_enum_input = soc_camera_enum_input,
1181 .vidioc_g_input = soc_camera_g_input,
1182 .vidioc_s_input = soc_camera_s_input,
1183 .vidioc_s_std = soc_camera_s_std,
1184 .vidioc_reqbufs = soc_camera_reqbufs,
1185 .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
1186 .vidioc_querybuf = soc_camera_querybuf,
1187 .vidioc_qbuf = soc_camera_qbuf,
1188 .vidioc_dqbuf = soc_camera_dqbuf,
1189 .vidioc_streamon = soc_camera_streamon,
1190 .vidioc_streamoff = soc_camera_streamoff,
1191 .vidioc_queryctrl = soc_camera_queryctrl,
1192 .vidioc_g_ctrl = soc_camera_g_ctrl,
1193 .vidioc_s_ctrl = soc_camera_s_ctrl,
1194 .vidioc_cropcap = soc_camera_cropcap,
1195 .vidioc_g_crop = soc_camera_g_crop,
1196 .vidioc_s_crop = soc_camera_s_crop,
1197 .vidioc_g_chip_ident = soc_camera_g_chip_ident,
1198#ifdef CONFIG_VIDEO_ADV_DEBUG
1199 .vidioc_g_register = soc_camera_g_register,
1200 .vidioc_s_register = soc_camera_s_register,
1201#endif
1202};
1203
40e2e092 1204static int video_dev_create(struct soc_camera_device *icd)
e55222ef
GL
1205{
1206 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
40e2e092 1207 struct video_device *vdev = video_device_alloc();
e55222ef 1208
e55222ef 1209 if (!vdev)
40e2e092 1210 return -ENOMEM;
e55222ef
GL
1211
1212 strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
1c3bb743 1213
5e85e732 1214 vdev->parent = &icd->dev;
e55222ef
GL
1215 vdev->current_norm = V4L2_STD_UNKNOWN;
1216 vdev->fops = &soc_camera_fops;
a399810c 1217 vdev->ioctl_ops = &soc_camera_ioctl_ops;
e55222ef
GL
1218 vdev->release = video_device_release;
1219 vdev->minor = -1;
40e2e092 1220 vdev->tvnorms = V4L2_STD_UNKNOWN;
e55222ef 1221
e55222ef
GL
1222 icd->vdev = vdev;
1223
1224 return 0;
40e2e092 1225}
e55222ef 1226
40e2e092 1227/*
979ea1dd 1228 * Called from soc_camera_probe() above (with .video_lock held???)
40e2e092 1229 */
979ea1dd 1230static int soc_camera_video_start(struct soc_camera_device *icd)
40e2e092 1231{
40e2e092 1232 const struct v4l2_queryctrl *qctrl;
979ea1dd 1233 int ret;
40e2e092
GL
1234
1235 if (!icd->dev.parent)
1236 return -ENODEV;
1237
1238 if (!icd->ops ||
40e2e092
GL
1239 !icd->ops->query_bus_param ||
1240 !icd->ops->set_bus_param)
1241 return -EINVAL;
1242
979ea1dd
GL
1243 ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER,
1244 icd->vdev->minor);
1245 if (ret < 0) {
1246 dev_err(&icd->dev, "video_register_device failed: %d\n", ret);
1247 return ret;
1248 }
40e2e092
GL
1249
1250 qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_GAIN);
1251 icd->gain = qctrl ? qctrl->default_value : (unsigned short)~0;
1252 qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
1253 icd->exposure = qctrl ? qctrl->default_value : (unsigned short)~0;
1254
979ea1dd 1255 return 0;
e55222ef 1256}
e55222ef 1257
40e2e092 1258static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev)
0fd327bd 1259{
40e2e092
GL
1260 struct soc_camera_link *icl = pdev->dev.platform_data;
1261 struct soc_camera_device *icd;
c41debaf 1262 int ret;
0fd327bd 1263
40e2e092
GL
1264 if (!icl)
1265 return -EINVAL;
0fd327bd 1266
40e2e092
GL
1267 icd = kzalloc(sizeof(*icd), GFP_KERNEL);
1268 if (!icd)
1269 return -ENOMEM;
0fd327bd 1270
40e2e092 1271 icd->iface = icl->bus_id;
979ea1dd 1272 icd->pdev = &pdev->dev;
40e2e092
GL
1273 platform_set_drvdata(pdev, icd);
1274 icd->dev.platform_data = icl;
0fd327bd 1275
40e2e092
GL
1276 ret = soc_camera_device_register(icd);
1277 if (ret < 0)
1278 goto escdevreg;
0fd327bd 1279
40e2e092 1280 return 0;
0fd327bd 1281
40e2e092
GL
1282escdevreg:
1283 kfree(icd);
0fd327bd 1284
40e2e092 1285 return ret;
c41debaf 1286}
0fd327bd 1287
40e2e092
GL
1288/* Only called on rmmod for each platform device, since they are not
1289 * hot-pluggable. Now we know, that all our users - hosts and devices have
1290 * been unloaded already */
1291static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev)
c41debaf 1292{
40e2e092 1293 struct soc_camera_device *icd = platform_get_drvdata(pdev);
c41debaf 1294
40e2e092 1295 if (!icd)
c41debaf
GL
1296 return -EINVAL;
1297
40e2e092 1298 soc_camera_device_unregister(icd);
c41debaf 1299
40e2e092 1300 kfree(icd);
c41debaf 1301
0fd327bd
GL
1302 return 0;
1303}
1304
1305static struct platform_driver __refdata soc_camera_pdrv = {
40e2e092
GL
1306 .remove = __devexit_p(soc_camera_pdrv_remove),
1307 .driver = {
1308 .name = "soc-camera-pdrv",
1309 .owner = THIS_MODULE,
0fd327bd
GL
1310 },
1311};
1312
e55222ef
GL
1313static int __init soc_camera_init(void)
1314{
1315 int ret = bus_register(&soc_camera_bus_type);
1316 if (ret)
1317 return ret;
1318 ret = driver_register(&ic_drv);
1319 if (ret)
1320 goto edrvr;
e55222ef 1321
40e2e092 1322 ret = platform_driver_probe(&soc_camera_pdrv, soc_camera_pdrv_probe);
0fd327bd
GL
1323 if (ret)
1324 goto epdr;
1325
e55222ef
GL
1326 return 0;
1327
0fd327bd
GL
1328epdr:
1329 driver_unregister(&ic_drv);
e55222ef
GL
1330edrvr:
1331 bus_unregister(&soc_camera_bus_type);
1332 return ret;
1333}
1334
1335static void __exit soc_camera_exit(void)
1336{
0fd327bd 1337 platform_driver_unregister(&soc_camera_pdrv);
e55222ef
GL
1338 driver_unregister(&ic_drv);
1339 bus_unregister(&soc_camera_bus_type);
1340}
1341
1342module_init(soc_camera_init);
1343module_exit(soc_camera_exit);
1344
1345MODULE_DESCRIPTION("Image capture bus driver");
1346MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1347MODULE_LICENSE("GPL");
0fd327bd 1348MODULE_ALIAS("platform:soc-camera-pdrv");
This page took 0.267766 seconds and 5 git commands to generate.