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