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