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