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