[media] move soc_camera i2c drivers into its own dir
[deliverable/linux.git] / drivers / media / i2c / soc_camera / 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
8422b087
HV
510 if (mutex_lock_interruptible(&icd->video_lock))
511 return -ERESTARTSYS;
b8d9904c 512 if (!try_module_get(ici->ops->owner)) {
7dfff953 513 dev_err(icd->pdev, "Couldn't lock capture bus driver.\n");
8422b087
HV
514 ret = -EINVAL;
515 goto emodule;
e55222ef
GL
516 }
517
1a0063a9
GL
518 icd->use_count++;
519
9dc4e48f
GL
520 /* Now we really have to activate the camera */
521 if (icd->use_count == 1) {
025c18a1 522 /* Restore parameters before the last close() per V4L2 API */
df2ed070
GL
523 struct v4l2_format f = {
524 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
525 .fmt.pix = {
6a6c8786
GL
526 .width = icd->user_width,
527 .height = icd->user_height,
025c18a1 528 .field = icd->field,
760697be
GL
529 .colorspace = icd->colorspace,
530 .pixelformat =
531 icd->current_fmt->host_fmt->fourcc,
df2ed070
GL
532 },
533 };
534
979ea1dd
GL
535 /* The camera could have been already on, try to reset */
536 if (icl->reset)
537 icl->reset(icd->pdev);
538
2f9a0c88
GL
539 /* Don't mess with the host during probe */
540 mutex_lock(&ici->host_lock);
b8d9904c 541 ret = ici->ops->add(icd);
2f9a0c88 542 mutex_unlock(&ici->host_lock);
9dc4e48f 543 if (ret < 0) {
7dfff953 544 dev_err(icd->pdev, "Couldn't activate the camera: %d\n", ret);
9dc4e48f
GL
545 goto eiciadd;
546 }
df2ed070 547
7705b6d8
GL
548 ret = soc_camera_power_on(icd, icl);
549 if (ret < 0)
550 goto epower;
551
4f9fb5ed
MCC
552 pm_runtime_enable(&icd->vdev->dev);
553 ret = pm_runtime_resume(&icd->vdev->dev);
554 if (ret < 0 && ret != -ENOSYS)
555 goto eresume;
556
760697be
GL
557 /*
558 * Try to configure with default parameters. Notice: this is the
559 * very first open, so, we cannot race against other calls,
560 * apart from someone else calling open() simultaneously, but
561 * .video_lock is protecting us against it.
562 */
57bee29d 563 ret = soc_camera_set_fmt(icd, &f);
df2ed070
GL
564 if (ret < 0)
565 goto esfmt;
24d8c029 566
592c2aba
GL
567 if (ici->ops->init_videobuf) {
568 ici->ops->init_videobuf(&icd->vb_vidq, icd);
569 } else {
570 ret = ici->ops->init_videobuf2(&icd->vb2_vidq, icd);
571 if (ret < 0)
572 goto einitvb;
573 }
ee02da64 574 v4l2_ctrl_handler_setup(&icd->ctrl_handler);
9dc4e48f 575 }
8422b087 576 mutex_unlock(&icd->video_lock);
9dc4e48f 577
57bee29d 578 file->private_data = icd;
7dfff953 579 dev_dbg(icd->pdev, "camera device open\n");
e55222ef 580
e55222ef
GL
581 return 0;
582
df2ed070 583 /*
4f9fb5ed 584 * First four errors are entered with the .video_lock held
df2ed070
GL
585 * and use_count == 1
586 */
592c2aba 587einitvb:
df2ed070 588esfmt:
4f9fb5ed
MCC
589 pm_runtime_disable(&icd->vdev->dev);
590eresume:
3dcc731a 591 soc_camera_power_off(icd, icl);
979ea1dd 592epower:
7705b6d8
GL
593 ici->ops->remove(icd);
594eiciadd:
c2786ad2 595 icd->use_count--;
b8d9904c 596 module_put(ici->ops->owner);
8422b087
HV
597emodule:
598 mutex_unlock(&icd->video_lock);
57bee29d 599
e55222ef
GL
600 return ret;
601}
602
bec43661 603static int soc_camera_close(struct file *file)
e55222ef 604{
57bee29d 605 struct soc_camera_device *icd = file->private_data;
7dfff953 606 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
e55222ef 607
8422b087 608 mutex_lock(&icd->video_lock);
9dc4e48f 609 icd->use_count--;
40e2e092 610 if (!icd->use_count) {
979ea1dd
GL
611 struct soc_camera_link *icl = to_soc_camera_link(icd);
612
4f9fb5ed
MCC
613 pm_runtime_suspend(&icd->vdev->dev);
614 pm_runtime_disable(&icd->vdev->dev);
615
592c2aba
GL
616 if (ici->ops->init_videobuf2)
617 vb2_queue_release(&icd->vb2_vidq);
48ecf9fe 618 ici->ops->remove(icd);
4f9fb5ed 619
3dcc731a 620 soc_camera_power_off(icd, icl);
40e2e092 621 }
025c18a1 622
57bee29d
GL
623 if (icd->streamer == file)
624 icd->streamer = NULL;
8422b087 625 mutex_unlock(&icd->video_lock);
57bee29d 626
b8d9904c 627 module_put(ici->ops->owner);
9dc4e48f 628
7dfff953 629 dev_dbg(icd->pdev, "camera device close\n");
e55222ef
GL
630
631 return 0;
632}
633
aba360d8 634static ssize_t soc_camera_read(struct file *file, char __user *buf,
abe4c471 635 size_t count, loff_t *ppos)
e55222ef 636{
57bee29d 637 struct soc_camera_device *icd = file->private_data;
e55222ef
GL
638 int err = -EINVAL;
639
7dfff953 640 dev_err(icd->pdev, "camera device read not implemented\n");
e55222ef
GL
641
642 return err;
643}
644
645static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
646{
57bee29d 647 struct soc_camera_device *icd = file->private_data;
7dfff953 648 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
e55222ef
GL
649 int err;
650
7dfff953 651 dev_dbg(icd->pdev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
e55222ef 652
57bee29d
GL
653 if (icd->streamer != file)
654 return -EBUSY;
655
8422b087
HV
656 if (mutex_lock_interruptible(&icd->video_lock))
657 return -ERESTARTSYS;
592c2aba
GL
658 if (ici->ops->init_videobuf)
659 err = videobuf_mmap_mapper(&icd->vb_vidq, vma);
660 else
661 err = vb2_mmap(&icd->vb2_vidq, vma);
8422b087 662 mutex_unlock(&icd->video_lock);
e55222ef 663
7dfff953 664 dev_dbg(icd->pdev, "vma start=0x%08lx, size=%ld, ret=%d\n",
e55222ef
GL
665 (unsigned long)vma->vm_start,
666 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
667 err);
668
669 return err;
670}
671
672static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
673{
57bee29d 674 struct soc_camera_device *icd = file->private_data;
7dfff953 675 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
8422b087 676 unsigned res = POLLERR;
e55222ef 677
57bee29d 678 if (icd->streamer != file)
e55222ef 679 return POLLERR;
e55222ef 680
8422b087
HV
681 mutex_lock(&icd->video_lock);
682 if (ici->ops->init_videobuf && list_empty(&icd->vb_vidq.stream))
683 dev_err(icd->pdev, "Trying to poll with no queued buffers!\n");
684 else
685 res = ici->ops->poll(file, pt);
686 mutex_unlock(&icd->video_lock);
687 return res;
e55222ef
GL
688}
689
592c2aba
GL
690void soc_camera_lock(struct vb2_queue *vq)
691{
692 struct soc_camera_device *icd = vb2_get_drv_priv(vq);
693 mutex_lock(&icd->video_lock);
694}
695EXPORT_SYMBOL(soc_camera_lock);
696
697void soc_camera_unlock(struct vb2_queue *vq)
698{
699 struct soc_camera_device *icd = vb2_get_drv_priv(vq);
700 mutex_unlock(&icd->video_lock);
701}
702EXPORT_SYMBOL(soc_camera_unlock);
703
bec43661 704static struct v4l2_file_operations soc_camera_fops = {
e55222ef
GL
705 .owner = THIS_MODULE,
706 .open = soc_camera_open,
707 .release = soc_camera_close,
b6a633c1 708 .unlocked_ioctl = video_ioctl2,
e55222ef
GL
709 .read = soc_camera_read,
710 .mmap = soc_camera_mmap,
711 .poll = soc_camera_poll,
e55222ef
GL
712};
713
72937890 714static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
abe4c471 715 struct v4l2_format *f)
e55222ef 716{
57bee29d 717 struct soc_camera_device *icd = file->private_data;
e55222ef 718 int ret;
e55222ef
GL
719
720 WARN_ON(priv != file->private_data);
721
d366d4a0 722 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
7dfff953 723 dev_warn(icd->pdev, "Wrong buf-type %d\n", f->type);
d366d4a0
GL
724 return -EINVAL;
725 }
726
57bee29d
GL
727 if (icd->streamer && icd->streamer != file)
728 return -EBUSY;
1c3bb743 729
7dfff953
GL
730 if (is_streaming(to_soc_camera_host(icd->parent), icd)) {
731 dev_err(icd->pdev, "S_FMT denied: queue initialised\n");
b6a633c1 732 return -EBUSY;
1c3bb743
GL
733 }
734
57bee29d
GL
735 ret = soc_camera_set_fmt(icd, f);
736
737 if (!ret && !icd->streamer)
738 icd->streamer = file;
1c3bb743 739
1c3bb743 740 return ret;
e55222ef
GL
741}
742
72937890 743static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
abe4c471 744 struct v4l2_fmtdesc *f)
e55222ef 745{
57bee29d 746 struct soc_camera_device *icd = file->private_data;
760697be 747 const struct soc_mbus_pixelfmt *format;
e55222ef
GL
748
749 WARN_ON(priv != file->private_data);
750
c2786ad2 751 if (f->index >= icd->num_user_formats)
e55222ef
GL
752 return -EINVAL;
753
c2786ad2 754 format = icd->user_formats[f->index].host_fmt;
e55222ef 755
760697be
GL
756 if (format->name)
757 strlcpy(f->description, format->name, sizeof(f->description));
e55222ef
GL
758 f->pixelformat = format->fourcc;
759 return 0;
760}
761
72937890 762static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
abe4c471 763 struct v4l2_format *f)
e55222ef 764{
57bee29d 765 struct soc_camera_device *icd = file->private_data;
64f5905e 766 struct v4l2_pix_format *pix = &f->fmt.pix;
e55222ef
GL
767
768 WARN_ON(priv != file->private_data);
769
d366d4a0
GL
770 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
771 return -EINVAL;
772
6a6c8786
GL
773 pix->width = icd->user_width;
774 pix->height = icd->user_height;
0e4c180d
SA
775 pix->bytesperline = icd->bytesperline;
776 pix->sizeimage = icd->sizeimage;
592c2aba 777 pix->field = icd->field;
760697be 778 pix->pixelformat = icd->current_fmt->host_fmt->fourcc;
760697be 779 pix->colorspace = icd->colorspace;
7dfff953 780 dev_dbg(icd->pdev, "current_fmt->fourcc: 0x%08x\n",
760697be 781 icd->current_fmt->host_fmt->fourcc);
e55222ef
GL
782 return 0;
783}
784
785static int soc_camera_querycap(struct file *file, void *priv,
786 struct v4l2_capability *cap)
787{
57bee29d 788 struct soc_camera_device *icd = file->private_data;
7dfff953 789 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
e55222ef
GL
790
791 WARN_ON(priv != file->private_data);
792
793 strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
b8d9904c 794 return ici->ops->querycap(ici, cap);
e55222ef
GL
795}
796
797static int soc_camera_streamon(struct file *file, void *priv,
798 enum v4l2_buf_type i)
799{
57bee29d 800 struct soc_camera_device *icd = file->private_data;
7dfff953 801 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
c9c1f1c0 802 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1c3bb743 803 int ret;
e55222ef
GL
804
805 WARN_ON(priv != file->private_data);
806
e55222ef
GL
807 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
808 return -EINVAL;
809
57bee29d
GL
810 if (icd->streamer != file)
811 return -EBUSY;
812
e55222ef 813 /* This calls buf_queue from host driver's videobuf_queue_ops */
592c2aba
GL
814 if (ici->ops->init_videobuf)
815 ret = videobuf_streamon(&icd->vb_vidq);
816 else
817 ret = vb2_streamon(&icd->vb2_vidq, i);
818
7fdbd85b
AG
819 if (!ret)
820 v4l2_subdev_call(sd, video, s_stream, 1);
1c3bb743 821
1c3bb743 822 return ret;
e55222ef
GL
823}
824
825static int soc_camera_streamoff(struct file *file, void *priv,
826 enum v4l2_buf_type i)
827{
57bee29d 828 struct soc_camera_device *icd = file->private_data;
c9c1f1c0 829 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
7dfff953 830 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
e55222ef
GL
831
832 WARN_ON(priv != file->private_data);
833
e55222ef
GL
834 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
835 return -EINVAL;
836
57bee29d
GL
837 if (icd->streamer != file)
838 return -EBUSY;
839
5d28d525
GL
840 /*
841 * This calls buf_release from host driver's videobuf_queue_ops for all
842 * remaining buffers. When the last buffer is freed, stop capture
843 */
592c2aba
GL
844 if (ici->ops->init_videobuf)
845 videobuf_streamoff(&icd->vb_vidq);
846 else
847 vb2_streamoff(&icd->vb2_vidq, i);
e55222ef 848
c9c1f1c0 849 v4l2_subdev_call(sd, video, s_stream, 0);
e55222ef
GL
850
851 return 0;
852}
853
e55222ef
GL
854static int soc_camera_cropcap(struct file *file, void *fh,
855 struct v4l2_cropcap *a)
856{
57bee29d 857 struct soc_camera_device *icd = file->private_data;
7dfff953 858 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
e55222ef 859
6a6c8786 860 return ici->ops->cropcap(icd, a);
e55222ef
GL
861}
862
863static int soc_camera_g_crop(struct file *file, void *fh,
864 struct v4l2_crop *a)
865{
57bee29d 866 struct soc_camera_device *icd = file->private_data;
7dfff953 867 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
6a6c8786 868 int ret;
e55222ef 869
6a6c8786 870 ret = ici->ops->get_crop(icd, a);
e55222ef 871
6a6c8786 872 return ret;
e55222ef
GL
873}
874
68a54f0e
GL
875/*
876 * According to the V4L2 API, drivers shall not update the struct v4l2_crop
877 * argument with the actual geometry, instead, the user shall use G_CROP to
ab56d5eb 878 * retrieve it.
68a54f0e 879 */
e55222ef
GL
880static int soc_camera_s_crop(struct file *file, void *fh,
881 struct v4l2_crop *a)
882{
57bee29d 883 struct soc_camera_device *icd = file->private_data;
7dfff953 884 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
6a6c8786
GL
885 struct v4l2_rect *rect = &a->c;
886 struct v4l2_crop current_crop;
e55222ef
GL
887 int ret;
888
889 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
890 return -EINVAL;
891
7dfff953 892 dev_dbg(icd->pdev, "S_CROP(%ux%u@%u:%u)\n",
6a6c8786
GL
893 rect->width, rect->height, rect->left, rect->top);
894
6a6c8786
GL
895 /* If get_crop fails, we'll let host and / or client drivers decide */
896 ret = ici->ops->get_crop(icd, &current_crop);
897
b897a91a 898 /* Prohibit window size change with initialised buffers */
103754a0 899 if (ret < 0) {
7dfff953 900 dev_err(icd->pdev,
103754a0 901 "S_CROP denied: getting current crop failed\n");
aee5c2f1
GL
902 } else if ((a->c.width == current_crop.c.width &&
903 a->c.height == current_crop.c.height) ||
904 !is_streaming(ici, icd)) {
905 /* same size or not streaming - use .set_crop() */
906 ret = ici->ops->set_crop(icd, a);
907 } else if (ici->ops->set_livecrop) {
908 ret = ici->ops->set_livecrop(icd, a);
909 } else {
7dfff953 910 dev_err(icd->pdev,
b897a91a
GL
911 "S_CROP denied: queue initialised and sizes differ\n");
912 ret = -EBUSY;
b897a91a
GL
913 }
914
e55222ef
GL
915 return ret;
916}
917
c9f6ef69
GL
918static int soc_camera_g_parm(struct file *file, void *fh,
919 struct v4l2_streamparm *a)
920{
57bee29d 921 struct soc_camera_device *icd = file->private_data;
7dfff953 922 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
c9f6ef69
GL
923
924 if (ici->ops->get_parm)
925 return ici->ops->get_parm(icd, a);
926
927 return -ENOIOCTLCMD;
928}
929
930static int soc_camera_s_parm(struct file *file, void *fh,
931 struct v4l2_streamparm *a)
932{
57bee29d 933 struct soc_camera_device *icd = file->private_data;
7dfff953 934 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
c9f6ef69
GL
935
936 if (ici->ops->set_parm)
937 return ici->ops->set_parm(icd, a);
938
939 return -ENOIOCTLCMD;
940}
941
e55222ef 942static int soc_camera_g_chip_ident(struct file *file, void *fh,
aecde8b5 943 struct v4l2_dbg_chip_ident *id)
e55222ef 944{
57bee29d 945 struct soc_camera_device *icd = file->private_data;
c9c1f1c0 946 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
e55222ef 947
c9c1f1c0 948 return v4l2_subdev_call(sd, core, g_chip_ident, id);
e55222ef
GL
949}
950
951#ifdef CONFIG_VIDEO_ADV_DEBUG
952static int soc_camera_g_register(struct file *file, void *fh,
aecde8b5 953 struct v4l2_dbg_register *reg)
e55222ef 954{
57bee29d 955 struct soc_camera_device *icd = file->private_data;
c9c1f1c0 956 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
e55222ef 957
c9c1f1c0 958 return v4l2_subdev_call(sd, core, g_register, reg);
e55222ef
GL
959}
960
961static int soc_camera_s_register(struct file *file, void *fh,
aecde8b5 962 struct v4l2_dbg_register *reg)
e55222ef 963{
57bee29d 964 struct soc_camera_device *icd = file->private_data;
c9c1f1c0 965 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
e55222ef 966
c9c1f1c0 967 return v4l2_subdev_call(sd, core, s_register, reg);
e55222ef
GL
968}
969#endif
970
7dfff953
GL
971static int soc_camera_probe(struct soc_camera_device *icd);
972
e55222ef
GL
973/* So far this function cannot fail */
974static void scan_add_host(struct soc_camera_host *ici)
975{
976 struct soc_camera_device *icd;
977
2f9a0c88 978 mutex_lock(&ici->host_lock);
e55222ef
GL
979
980 list_for_each_entry(icd, &devices, list) {
981 if (icd->iface == ici->nr) {
40e2e092 982 int ret;
7dfff953
GL
983
984 icd->parent = ici->v4l2_dev.dev;
985 ret = soc_camera_probe(icd);
e55222ef
GL
986 }
987 }
988
2f9a0c88 989 mutex_unlock(&ici->host_lock);
e55222ef
GL
990}
991
40e2e092
GL
992#ifdef CONFIG_I2C_BOARDINFO
993static int soc_camera_init_i2c(struct soc_camera_device *icd,
994 struct soc_camera_link *icl)
e55222ef 995{
40e2e092 996 struct i2c_client *client;
7dfff953 997 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
40e2e092 998 struct i2c_adapter *adap = i2c_get_adapter(icl->i2c_adapter_id);
979ea1dd 999 struct v4l2_subdev *subdev;
e55222ef 1000
40e2e092 1001 if (!adap) {
7dfff953 1002 dev_err(icd->pdev, "Cannot get I2C adapter #%d. No driver?\n",
40e2e092
GL
1003 icl->i2c_adapter_id);
1004 goto ei2cga;
1005 }
e55222ef 1006
2f0babb7 1007 icl->board_info->platform_data = icl;
e55222ef 1008
979ea1dd 1009 subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
9a1f8b34 1010 icl->board_info, NULL);
d74f841c 1011 if (!subdev)
40e2e092 1012 goto ei2cnd;
e55222ef 1013
c4ce6d14 1014 client = v4l2_get_subdevdata(subdev);
979ea1dd 1015
40e2e092 1016 /* Use to_i2c_client(dev) to recover the i2c client */
7dfff953 1017 icd->control = &client->dev;
e55222ef 1018
40e2e092
GL
1019 return 0;
1020ei2cnd:
1021 i2c_put_adapter(adap);
1022ei2cga:
d74f841c 1023 return -ENODEV;
e55222ef
GL
1024}
1025
40e2e092
GL
1026static void soc_camera_free_i2c(struct soc_camera_device *icd)
1027{
1028 struct i2c_client *client =
1029 to_i2c_client(to_soc_camera_control(icd));
c8dd7078 1030 struct i2c_adapter *adap = client->adapter;
7dfff953
GL
1031
1032 icd->control = NULL;
979ea1dd 1033 v4l2_device_unregister_subdev(i2c_get_clientdata(client));
40e2e092 1034 i2c_unregister_device(client);
c8dd7078 1035 i2c_put_adapter(adap);
40e2e092
GL
1036}
1037#else
1038#define soc_camera_init_i2c(icd, icl) (-ENODEV)
1039#define soc_camera_free_i2c(icd) do {} while (0)
1040#endif
1041
979ea1dd 1042static int soc_camera_video_start(struct soc_camera_device *icd);
40e2e092
GL
1043static int video_dev_create(struct soc_camera_device *icd);
1044/* Called during host-driver probe */
7dfff953 1045static int soc_camera_probe(struct soc_camera_device *icd)
e55222ef 1046{
7dfff953 1047 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
40e2e092 1048 struct soc_camera_link *icl = to_soc_camera_link(icd);
979ea1dd 1049 struct device *control = NULL;
6a6c8786 1050 struct v4l2_subdev *sd;
760697be 1051 struct v4l2_mbus_framefmt mf;
e55222ef
GL
1052 int ret;
1053
7dfff953 1054 dev_info(icd->pdev, "Probing %s\n", dev_name(icd->pdev));
1c3bb743 1055
ee02da64
HV
1056 /*
1057 * Currently the subdev with the largest number of controls (13) is
1058 * ov6550. So let's pick 16 as a hint for the control handler. Note
1059 * that this is a hint only: too large and you waste some memory, too
1060 * small and there is a (very) small performance hit when looking up
1061 * controls in the internal hash.
1062 */
1063 ret = v4l2_ctrl_handler_init(&icd->ctrl_handler, 16);
1064 if (ret < 0)
1065 return ret;
1066
96e442c1
AP
1067 ret = regulator_bulk_get(icd->pdev, icl->num_regulators,
1068 icl->regulators);
1069 if (ret < 0)
1070 goto ereg;
1071
7705b6d8
GL
1072 /* The camera could have been already on, try to reset */
1073 if (icl->reset)
1074 icl->reset(icd->pdev);
1075
1076 ret = ici->ops->add(icd);
1077 if (ret < 0)
1078 goto eadd;
1079
7b9d8c3c
GL
1080 /*
1081 * This will not yet call v4l2_subdev_core_ops::s_power(1), because the
1082 * subdevice has not been initialised yet. We'll have to call it once
1083 * again after initialisation, even though it shouldn't be needed, we
1084 * don't do any IO here.
1085 */
3dcc731a 1086 ret = soc_camera_power_on(icd, icl);
96e442c1
AP
1087 if (ret < 0)
1088 goto epower;
979ea1dd 1089
979ea1dd 1090 /* Must have icd->vdev before registering the device */
40e2e092
GL
1091 ret = video_dev_create(icd);
1092 if (ret < 0)
1093 goto evdc;
1c3bb743 1094
40e2e092
GL
1095 /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
1096 if (icl->board_info) {
1097 ret = soc_camera_init_i2c(icd, icl);
1098 if (ret < 0)
1099 goto eadddev;
1100 } else if (!icl->add_device || !icl->del_device) {
1c3bb743 1101 ret = -EINVAL;
40e2e092
GL
1102 goto eadddev;
1103 } else {
979ea1dd
GL
1104 if (icl->module_name)
1105 ret = request_module(icl->module_name);
1106
7dfff953 1107 ret = icl->add_device(icd);
40e2e092
GL
1108 if (ret < 0)
1109 goto eadddev;
1c3bb743 1110
96c75399
GL
1111 /*
1112 * FIXME: this is racy, have to use driver-binding notification,
1113 * when it is available
1114 */
979ea1dd 1115 control = to_soc_camera_control(icd);
08590b96 1116 if (!control || !control->driver || !dev_get_drvdata(control) ||
979ea1dd 1117 !try_module_get(control->driver->owner)) {
7dfff953 1118 icl->del_device(icd);
7ae77ee9 1119 ret = -ENODEV;
979ea1dd
GL
1120 goto enodrv;
1121 }
40e2e092 1122 }
e55222ef 1123
24105ebc 1124 sd = soc_camera_to_subdev(icd);
4c0b036d
GL
1125 sd->grp_id = soc_camera_grp_id(icd);
1126 v4l2_set_subdev_hostdata(sd, icd);
24105ebc 1127
ee02da64
HV
1128 if (v4l2_ctrl_add_handler(&icd->ctrl_handler, sd->ctrl_handler))
1129 goto ectrl;
1130
fa48984e
GL
1131 /* At this point client .probe() should have run already */
1132 ret = soc_camera_init_user_formats(icd);
1133 if (ret < 0)
1134 goto eiufmt;
1135
fa48984e
GL
1136 icd->field = V4L2_FIELD_ANY;
1137
b6a633c1
GL
1138 /*
1139 * ..._video_start() will create a device node, video_register_device()
1140 * itself is protected against concurrent open() calls, but we also have
1141 * to protect our data.
1142 */
979ea1dd
GL
1143 mutex_lock(&icd->video_lock);
1144
1145 ret = soc_camera_video_start(icd);
1146 if (ret < 0)
1147 goto evidstart;
1148
7b9d8c3c
GL
1149 ret = v4l2_subdev_call(sd, core, s_power, 1);
1150 if (ret < 0 && ret != -ENOIOCTLCMD)
1151 goto esdpwr;
1152
6a6c8786 1153 /* Try to improve our guess of a reasonable window format */
760697be
GL
1154 if (!v4l2_subdev_call(sd, video, g_mbus_fmt, &mf)) {
1155 icd->user_width = mf.width;
1156 icd->user_height = mf.height;
1157 icd->colorspace = mf.colorspace;
1158 icd->field = mf.field;
6a6c8786
GL
1159 }
1160
979ea1dd
GL
1161 ici->ops->remove(icd);
1162
3dcc731a 1163 soc_camera_power_off(icd, icl);
979ea1dd
GL
1164
1165 mutex_unlock(&icd->video_lock);
025c18a1 1166
40e2e092 1167 return 0;
e55222ef 1168
7b9d8c3c
GL
1169esdpwr:
1170 video_unregister_device(icd->vdev);
979ea1dd
GL
1171evidstart:
1172 mutex_unlock(&icd->video_lock);
fa48984e
GL
1173 soc_camera_free_user_formats(icd);
1174eiufmt:
ee02da64 1175ectrl:
979ea1dd 1176 if (icl->board_info) {
40e2e092 1177 soc_camera_free_i2c(icd);
979ea1dd 1178 } else {
7dfff953 1179 icl->del_device(icd);
979ea1dd
GL
1180 module_put(control->driver->owner);
1181 }
1182enodrv:
40e2e092
GL
1183eadddev:
1184 video_device_release(icd->vdev);
7b9d8c3c 1185 icd->vdev = NULL;
40e2e092 1186evdc:
3dcc731a 1187 soc_camera_power_off(icd, icl);
979ea1dd 1188epower:
7705b6d8
GL
1189 ici->ops->remove(icd);
1190eadd:
96e442c1
AP
1191 regulator_bulk_free(icl->num_regulators, icl->regulators);
1192ereg:
ee02da64 1193 v4l2_ctrl_handler_free(&icd->ctrl_handler);
e55222ef
GL
1194 return ret;
1195}
1196
5d28d525
GL
1197/*
1198 * This is called on device_unregister, which only means we have to disconnect
1199 * from the host, but not remove ourselves from the device list
1200 */
7dfff953 1201static int soc_camera_remove(struct soc_camera_device *icd)
e55222ef 1202{
40e2e092
GL
1203 struct soc_camera_link *icl = to_soc_camera_link(icd);
1204 struct video_device *vdev = icd->vdev;
e55222ef 1205
7dfff953 1206 BUG_ON(!icd->parent);
e55222ef 1207
ee02da64 1208 v4l2_ctrl_handler_free(&icd->ctrl_handler);
40e2e092 1209 if (vdev) {
40e2e092
GL
1210 video_unregister_device(vdev);
1211 icd->vdev = NULL;
40e2e092
GL
1212 }
1213
979ea1dd 1214 if (icl->board_info) {
40e2e092 1215 soc_camera_free_i2c(icd);
979ea1dd 1216 } else {
7dfff953 1217 struct device_driver *drv = to_soc_camera_control(icd)->driver;
979ea1dd 1218 if (drv) {
7dfff953 1219 icl->del_device(icd);
979ea1dd
GL
1220 module_put(drv->owner);
1221 }
1222 }
fa48984e 1223 soc_camera_free_user_formats(icd);
025c18a1 1224
96e442c1
AP
1225 regulator_bulk_free(icl->num_regulators, icl->regulators);
1226
e55222ef
GL
1227 return 0;
1228}
1229
6a6c8786
GL
1230static int default_cropcap(struct soc_camera_device *icd,
1231 struct v4l2_cropcap *a)
1232{
1233 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1234 return v4l2_subdev_call(sd, video, cropcap, a);
1235}
1236
1237static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1238{
1239 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1240 return v4l2_subdev_call(sd, video, g_crop, a);
1241}
1242
1243static int default_s_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1244{
1245 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1246 return v4l2_subdev_call(sd, video, s_crop, a);
1247}
1248
06e17821
JK
1249static int default_g_parm(struct soc_camera_device *icd,
1250 struct v4l2_streamparm *parm)
1251{
1252 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1253 return v4l2_subdev_call(sd, video, g_parm, parm);
1254}
1255
1256static int default_s_parm(struct soc_camera_device *icd,
1257 struct v4l2_streamparm *parm)
1258{
1259 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1260 return v4l2_subdev_call(sd, video, s_parm, parm);
1261}
1262
ad3537b5
GL
1263static int default_enum_framesizes(struct soc_camera_device *icd,
1264 struct v4l2_frmsizeenum *fsize)
ed5b65dc
QX
1265{
1266 int ret;
1267 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1268 const struct soc_camera_format_xlate *xlate;
1269 __u32 pixfmt = fsize->pixel_format;
1270 struct v4l2_frmsizeenum fsize_mbus = *fsize;
1271
1272 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1273 if (!xlate)
1274 return -EINVAL;
1275 /* map xlate-code to pixel_format, sensor only handle xlate-code*/
1276 fsize_mbus.pixel_format = xlate->code;
1277
9633c086 1278 ret = v4l2_subdev_call(sd, video, enum_framesizes, &fsize_mbus);
ed5b65dc
QX
1279 if (ret < 0)
1280 return ret;
1281
1282 *fsize = fsize_mbus;
1283 fsize->pixel_format = pixfmt;
1284
1285 return 0;
1286}
1287
b8d9904c 1288int soc_camera_host_register(struct soc_camera_host *ici)
e55222ef 1289{
e55222ef 1290 struct soc_camera_host *ix;
979ea1dd 1291 int ret;
e55222ef 1292
64f5905e
GL
1293 if (!ici || !ici->ops ||
1294 !ici->ops->try_fmt ||
1295 !ici->ops->set_fmt ||
1296 !ici->ops->set_bus_param ||
1297 !ici->ops->querycap ||
592c2aba
GL
1298 ((!ici->ops->init_videobuf ||
1299 !ici->ops->reqbufs) &&
1300 !ici->ops->init_videobuf2) ||
64f5905e
GL
1301 !ici->ops->add ||
1302 !ici->ops->remove ||
eff505fa 1303 !ici->ops->poll ||
979ea1dd 1304 !ici->v4l2_dev.dev)
e55222ef
GL
1305 return -EINVAL;
1306
6a6c8786
GL
1307 if (!ici->ops->set_crop)
1308 ici->ops->set_crop = default_s_crop;
1309 if (!ici->ops->get_crop)
1310 ici->ops->get_crop = default_g_crop;
1311 if (!ici->ops->cropcap)
1312 ici->ops->cropcap = default_cropcap;
06e17821
JK
1313 if (!ici->ops->set_parm)
1314 ici->ops->set_parm = default_s_parm;
1315 if (!ici->ops->get_parm)
1316 ici->ops->get_parm = default_g_parm;
ad3537b5
GL
1317 if (!ici->ops->enum_framesizes)
1318 ici->ops->enum_framesizes = default_enum_framesizes;
6a6c8786 1319
e55222ef
GL
1320 mutex_lock(&list_lock);
1321 list_for_each_entry(ix, &hosts, list) {
1322 if (ix->nr == ici->nr) {
979ea1dd
GL
1323 ret = -EBUSY;
1324 goto edevreg;
e55222ef
GL
1325 }
1326 }
1327
979ea1dd
GL
1328 ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
1329 if (ret < 0)
1330 goto edevreg;
eff505fa 1331
e55222ef
GL
1332 list_add_tail(&ici->list, &hosts);
1333 mutex_unlock(&list_lock);
1334
2f9a0c88 1335 mutex_init(&ici->host_lock);
e55222ef
GL
1336 scan_add_host(ici);
1337
1338 return 0;
979ea1dd
GL
1339
1340edevreg:
1341 mutex_unlock(&list_lock);
1342 return ret;
e55222ef
GL
1343}
1344EXPORT_SYMBOL(soc_camera_host_register);
1345
1346/* Unregister all clients! */
1347void soc_camera_host_unregister(struct soc_camera_host *ici)
1348{
1349 struct soc_camera_device *icd;
1350
1351 mutex_lock(&list_lock);
1352
1353 list_del(&ici->list);
7dfff953
GL
1354 list_for_each_entry(icd, &devices, list)
1355 if (icd->iface == ici->nr && to_soc_camera_control(icd))
1356 soc_camera_remove(icd);
e55222ef
GL
1357
1358 mutex_unlock(&list_lock);
1359
979ea1dd 1360 v4l2_device_unregister(&ici->v4l2_dev);
e55222ef
GL
1361}
1362EXPORT_SYMBOL(soc_camera_host_unregister);
1363
1364/* Image capture device */
40e2e092 1365static int soc_camera_device_register(struct soc_camera_device *icd)
e55222ef
GL
1366{
1367 struct soc_camera_device *ix;
1368 int num = -1, i;
1369
e55222ef
GL
1370 for (i = 0; i < 256 && num < 0; i++) {
1371 num = i;
40e2e092 1372 /* Check if this index is available on this interface */
e55222ef
GL
1373 list_for_each_entry(ix, &devices, list) {
1374 if (ix->iface == icd->iface && ix->devnum == i) {
1375 num = -1;
1376 break;
1377 }
1378 }
1379 }
1380
1381 if (num < 0)
5d28d525
GL
1382 /*
1383 * ok, we have 256 cameras on this host...
1384 * man, stay reasonable...
1385 */
e55222ef
GL
1386 return -ENOMEM;
1387
64ff9ba5 1388 icd->devnum = num;
64f5905e
GL
1389 icd->use_count = 0;
1390 icd->host_priv = NULL;
1c3bb743 1391 mutex_init(&icd->video_lock);
e55222ef 1392
40e2e092
GL
1393 list_add_tail(&icd->list, &devices);
1394
1395 return 0;
e55222ef 1396}
e55222ef 1397
a399810c
HV
1398static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
1399 .vidioc_querycap = soc_camera_querycap,
7ae77ee9 1400 .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
a399810c 1401 .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
a399810c 1402 .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
7ae77ee9 1403 .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
a399810c
HV
1404 .vidioc_enum_input = soc_camera_enum_input,
1405 .vidioc_g_input = soc_camera_g_input,
1406 .vidioc_s_input = soc_camera_s_input,
1407 .vidioc_s_std = soc_camera_s_std,
2f0babb7 1408 .vidioc_g_std = soc_camera_g_std,
ad3537b5 1409 .vidioc_enum_framesizes = soc_camera_enum_framesizes,
a399810c 1410 .vidioc_reqbufs = soc_camera_reqbufs,
a399810c
HV
1411 .vidioc_querybuf = soc_camera_querybuf,
1412 .vidioc_qbuf = soc_camera_qbuf,
1413 .vidioc_dqbuf = soc_camera_dqbuf,
7ae77ee9
GL
1414 .vidioc_create_bufs = soc_camera_create_bufs,
1415 .vidioc_prepare_buf = soc_camera_prepare_buf,
a399810c
HV
1416 .vidioc_streamon = soc_camera_streamon,
1417 .vidioc_streamoff = soc_camera_streamoff,
a399810c
HV
1418 .vidioc_cropcap = soc_camera_cropcap,
1419 .vidioc_g_crop = soc_camera_g_crop,
1420 .vidioc_s_crop = soc_camera_s_crop,
c9f6ef69
GL
1421 .vidioc_g_parm = soc_camera_g_parm,
1422 .vidioc_s_parm = soc_camera_s_parm,
a399810c
HV
1423 .vidioc_g_chip_ident = soc_camera_g_chip_ident,
1424#ifdef CONFIG_VIDEO_ADV_DEBUG
1425 .vidioc_g_register = soc_camera_g_register,
1426 .vidioc_s_register = soc_camera_s_register,
1427#endif
1428};
1429
40e2e092 1430static int video_dev_create(struct soc_camera_device *icd)
e55222ef 1431{
7dfff953 1432 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
40e2e092 1433 struct video_device *vdev = video_device_alloc();
e55222ef 1434
e55222ef 1435 if (!vdev)
40e2e092 1436 return -ENOMEM;
e55222ef
GL
1437
1438 strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
1c3bb743 1439
7dfff953 1440 vdev->parent = icd->pdev;
e55222ef
GL
1441 vdev->current_norm = V4L2_STD_UNKNOWN;
1442 vdev->fops = &soc_camera_fops;
a399810c 1443 vdev->ioctl_ops = &soc_camera_ioctl_ops;
e55222ef 1444 vdev->release = video_device_release;
40e2e092 1445 vdev->tvnorms = V4L2_STD_UNKNOWN;
ee02da64 1446 vdev->ctrl_handler = &icd->ctrl_handler;
7a05dfed 1447 vdev->lock = &icd->video_lock;
e55222ef 1448
e55222ef
GL
1449 icd->vdev = vdev;
1450
1451 return 0;
40e2e092 1452}
e55222ef 1453
40e2e092 1454/*
979ea1dd 1455 * Called from soc_camera_probe() above (with .video_lock held???)
40e2e092 1456 */
979ea1dd 1457static int soc_camera_video_start(struct soc_camera_device *icd)
40e2e092 1458{
d364ee4f 1459 const struct device_type *type = icd->vdev->dev.type;
979ea1dd 1460 int ret;
40e2e092 1461
7dfff953 1462 if (!icd->parent)
40e2e092
GL
1463 return -ENODEV;
1464
46b21094 1465 ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
979ea1dd 1466 if (ret < 0) {
7dfff953 1467 dev_err(icd->pdev, "video_register_device failed: %d\n", ret);
979ea1dd
GL
1468 return ret;
1469 }
40e2e092 1470
4f9fb5ed
MCC
1471 /* Restore device type, possibly set by the subdevice driver */
1472 icd->vdev->dev.type = type;
1473
979ea1dd 1474 return 0;
e55222ef 1475}
e55222ef 1476
40e2e092 1477static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev)
0fd327bd 1478{
40e2e092
GL
1479 struct soc_camera_link *icl = pdev->dev.platform_data;
1480 struct soc_camera_device *icd;
c41debaf 1481 int ret;
0fd327bd 1482
40e2e092
GL
1483 if (!icl)
1484 return -EINVAL;
0fd327bd 1485
40e2e092
GL
1486 icd = kzalloc(sizeof(*icd), GFP_KERNEL);
1487 if (!icd)
1488 return -ENOMEM;
0fd327bd 1489
40e2e092 1490 icd->iface = icl->bus_id;
7dfff953 1491 icd->link = icl;
979ea1dd 1492 icd->pdev = &pdev->dev;
40e2e092 1493 platform_set_drvdata(pdev, icd);
0fd327bd 1494
40e2e092
GL
1495 ret = soc_camera_device_register(icd);
1496 if (ret < 0)
1497 goto escdevreg;
0fd327bd 1498
6a6c8786
GL
1499 icd->user_width = DEFAULT_WIDTH;
1500 icd->user_height = DEFAULT_HEIGHT;
1501
40e2e092 1502 return 0;
0fd327bd 1503
40e2e092
GL
1504escdevreg:
1505 kfree(icd);
0fd327bd 1506
40e2e092 1507 return ret;
c41debaf 1508}
0fd327bd 1509
5d28d525
GL
1510/*
1511 * Only called on rmmod for each platform device, since they are not
40e2e092 1512 * hot-pluggable. Now we know, that all our users - hosts and devices have
5d28d525
GL
1513 * been unloaded already
1514 */
40e2e092 1515static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev)
c41debaf 1516{
40e2e092 1517 struct soc_camera_device *icd = platform_get_drvdata(pdev);
c41debaf 1518
40e2e092 1519 if (!icd)
c41debaf
GL
1520 return -EINVAL;
1521
7dfff953 1522 list_del(&icd->list);
c41debaf 1523
40e2e092 1524 kfree(icd);
c41debaf 1525
0fd327bd
GL
1526 return 0;
1527}
1528
1529static struct platform_driver __refdata soc_camera_pdrv = {
1858c99d 1530 .probe = soc_camera_pdrv_probe,
40e2e092
GL
1531 .remove = __devexit_p(soc_camera_pdrv_remove),
1532 .driver = {
1533 .name = "soc-camera-pdrv",
1534 .owner = THIS_MODULE,
0fd327bd
GL
1535 },
1536};
1537
e55222ef
GL
1538static int __init soc_camera_init(void)
1539{
1858c99d 1540 return platform_driver_register(&soc_camera_pdrv);
e55222ef
GL
1541}
1542
1543static void __exit soc_camera_exit(void)
1544{
0fd327bd 1545 platform_driver_unregister(&soc_camera_pdrv);
e55222ef
GL
1546}
1547
1548module_init(soc_camera_init);
1549module_exit(soc_camera_exit);
1550
1551MODULE_DESCRIPTION("Image capture bus driver");
1552MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1553MODULE_LICENSE("GPL");
0fd327bd 1554MODULE_ALIAS("platform:soc-camera-pdrv");
This page took 0.599853 seconds and 5 git commands to generate.