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