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