treewide: Use array_size() in vmalloc()
[linux-2.6-block.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>
40e2e092 24#include <linux/module.h>
e09da11d 25#include <linux/mutex.h>
859969b3 26#include <linux/of_graph.h>
0fd327bd 27#include <linux/platform_device.h>
e09da11d 28#include <linux/pm_runtime.h>
96e442c1 29#include <linux/regulator/consumer.h>
5a0e3ad6 30#include <linux/slab.h>
e55222ef
GL
31#include <linux/vmalloc.h>
32
0fd327bd 33#include <media/soc_camera.h>
d647f0b7 34#include <media/drv-intf/soc_mediabus.h>
e09da11d 35#include <media/v4l2-async.h>
9aea470b 36#include <media/v4l2-clk.h>
e55222ef 37#include <media/v4l2-common.h>
0fd327bd 38#include <media/v4l2-ioctl.h>
40e2e092 39#include <media/v4l2-dev.h>
859969b3 40#include <media/v4l2-fwnode.h>
c139990e 41#include <media/videobuf2-v4l2.h>
e55222ef 42
df2ed070
GL
43/* Default to VGA resolution */
44#define DEFAULT_WIDTH 640
45#define DEFAULT_HEIGHT 480
46
e09da11d
GL
47#define MAP_MAX_NUM 32
48static DECLARE_BITMAP(device_map, MAP_MAX_NUM);
e55222ef
GL
49static LIST_HEAD(hosts);
50static LIST_HEAD(devices);
e09da11d
GL
51/*
52 * Protects lists and bitmaps of hosts and devices.
53 * Lock nesting: Ok to take ->host_lock under list_lock.
54 */
55static DEFINE_MUTEX(list_lock);
56
57struct soc_camera_async_client {
58 struct v4l2_async_subdev *sensor;
59 struct v4l2_async_notifier notifier;
60 struct platform_device *pdev;
61 struct list_head list; /* needed for clean up */
62};
63
64static int soc_camera_video_start(struct soc_camera_device *icd);
65static int video_dev_create(struct soc_camera_device *icd);
e55222ef 66
9aea470b
GL
67int soc_camera_power_on(struct device *dev, struct soc_camera_subdev_desc *ssdd,
68 struct v4l2_clk *clk)
96e442c1 69{
40f07533
GL
70 int ret;
71 bool clock_toggle;
72
73 if (clk && (!ssdd->unbalanced_power ||
74 !test_and_set_bit(0, &ssdd->clock_state))) {
75 ret = v4l2_clk_enable(clk);
76 if (ret < 0) {
77 dev_err(dev, "Cannot enable clock: %d\n", ret);
78 return ret;
79 }
80 clock_toggle = true;
81 } else {
82 clock_toggle = false;
9aea470b 83 }
40f07533 84
d3f884a7
GL
85 ret = regulator_bulk_enable(ssdd->sd_pdata.num_regulators,
86 ssdd->sd_pdata.regulators);
3dcc731a 87 if (ret < 0) {
4ec10bac 88 dev_err(dev, "Cannot enable regulators\n");
e09da11d 89 goto eregenable;
3dcc731a 90 }
96e442c1 91
25a34811
GL
92 if (ssdd->power) {
93 ret = ssdd->power(dev, 1);
96e442c1 94 if (ret < 0) {
4ec10bac 95 dev_err(dev,
96e442c1 96 "Platform failed to power-on the camera.\n");
9aea470b 97 goto epwron;
96e442c1 98 }
3dcc731a
GL
99 }
100
9aea470b
GL
101 return 0;
102
103epwron:
d3f884a7
GL
104 regulator_bulk_disable(ssdd->sd_pdata.num_regulators,
105 ssdd->sd_pdata.regulators);
9aea470b 106eregenable:
40f07533 107 if (clock_toggle)
9aea470b
GL
108 v4l2_clk_disable(clk);
109
3dcc731a
GL
110 return ret;
111}
4ec10bac 112EXPORT_SYMBOL(soc_camera_power_on);
3dcc731a 113
9aea470b
GL
114int soc_camera_power_off(struct device *dev, struct soc_camera_subdev_desc *ssdd,
115 struct v4l2_clk *clk)
3dcc731a 116{
24592adc
LP
117 int ret = 0;
118 int err;
7b9d8c3c 119
25a34811
GL
120 if (ssdd->power) {
121 err = ssdd->power(dev, 0);
24592adc 122 if (err < 0) {
4ec10bac 123 dev_err(dev,
96e442c1 124 "Platform failed to power-off the camera.\n");
4ec10bac 125 ret = err;
96e442c1 126 }
96e442c1
AP
127 }
128
d3f884a7
GL
129 err = regulator_bulk_disable(ssdd->sd_pdata.num_regulators,
130 ssdd->sd_pdata.regulators);
24592adc 131 if (err < 0) {
4ec10bac 132 dev_err(dev, "Cannot disable regulators\n");
24592adc
LP
133 ret = ret ? : err;
134 }
3dcc731a 135
40f07533 136 if (clk && (!ssdd->unbalanced_power || test_and_clear_bit(0, &ssdd->clock_state)))
9aea470b
GL
137 v4l2_clk_disable(clk);
138
3dcc731a 139 return ret;
96e442c1 140}
4ec10bac
LP
141EXPORT_SYMBOL(soc_camera_power_off);
142
e09da11d
GL
143int soc_camera_power_init(struct device *dev, struct soc_camera_subdev_desc *ssdd)
144{
23272427 145 /* Should not have any effect in synchronous case */
d3f884a7
GL
146 return devm_regulator_bulk_get(dev, ssdd->sd_pdata.num_regulators,
147 ssdd->sd_pdata.regulators);
e09da11d
GL
148}
149EXPORT_SYMBOL(soc_camera_power_init);
150
4ec10bac
LP
151static int __soc_camera_power_on(struct soc_camera_device *icd)
152{
153 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
154 int ret;
155
156 ret = v4l2_subdev_call(sd, core, s_power, 1);
157 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
158 return ret;
159
160 return 0;
161}
162
163static int __soc_camera_power_off(struct soc_camera_device *icd)
164{
165 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
166 int ret;
167
168 ret = v4l2_subdev_call(sd, core, s_power, 0);
169 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
170 return ret;
171
172 return 0;
173}
96e442c1 174
3781760a
LP
175static int soc_camera_clock_start(struct soc_camera_host *ici)
176{
177 int ret;
178
179 if (!ici->ops->clock_start)
180 return 0;
181
182 mutex_lock(&ici->clk_lock);
183 ret = ici->ops->clock_start(ici);
184 mutex_unlock(&ici->clk_lock);
185
186 return ret;
187}
188
189static void soc_camera_clock_stop(struct soc_camera_host *ici)
190{
191 if (!ici->ops->clock_stop)
192 return;
193
194 mutex_lock(&ici->clk_lock);
195 ici->ops->clock_stop(ici);
196 mutex_unlock(&ici->clk_lock);
197}
198
c2786ad2
GL
199const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
200 struct soc_camera_device *icd, unsigned int fourcc)
201{
202 unsigned int i;
203
204 for (i = 0; i < icd->num_user_formats; i++)
205 if (icd->user_formats[i].host_fmt->fourcc == fourcc)
206 return icd->user_formats + i;
207 return NULL;
208}
209EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);
210
32c69fcc
GL
211/**
212 * soc_camera_apply_board_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
25a34811 213 * @ssdd: camera platform parameters
32c69fcc
GL
214 * @cfg: media bus configuration
215 * @return: resulting flags
216 */
25a34811 217unsigned long soc_camera_apply_board_flags(struct soc_camera_subdev_desc *ssdd,
32c69fcc
GL
218 const struct v4l2_mbus_config *cfg)
219{
220 unsigned long f, flags = cfg->flags;
221
222 /* If only one of the two polarities is supported, switch to the opposite */
25a34811 223 if (ssdd->flags & SOCAM_SENSOR_INVERT_HSYNC) {
32c69fcc
GL
224 f = flags & (V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW);
225 if (f == V4L2_MBUS_HSYNC_ACTIVE_HIGH || f == V4L2_MBUS_HSYNC_ACTIVE_LOW)
226 flags ^= V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW;
227 }
228
25a34811 229 if (ssdd->flags & SOCAM_SENSOR_INVERT_VSYNC) {
32c69fcc
GL
230 f = flags & (V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW);
231 if (f == V4L2_MBUS_VSYNC_ACTIVE_HIGH || f == V4L2_MBUS_VSYNC_ACTIVE_LOW)
232 flags ^= V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW;
233 }
234
25a34811 235 if (ssdd->flags & SOCAM_SENSOR_INVERT_PCLK) {
32c69fcc
GL
236 f = flags & (V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING);
237 if (f == V4L2_MBUS_PCLK_SAMPLE_RISING || f == V4L2_MBUS_PCLK_SAMPLE_FALLING)
238 flags ^= V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING;
239 }
240
241 return flags;
242}
243EXPORT_SYMBOL(soc_camera_apply_board_flags);
244
dca6b6d1
SA
245#define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
246 ((x) >> 24) & 0xff
247
248static int soc_camera_try_fmt(struct soc_camera_device *icd,
249 struct v4l2_format *f)
250{
7dfff953 251 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
bed8d803 252 const struct soc_camera_format_xlate *xlate;
dca6b6d1
SA
253 struct v4l2_pix_format *pix = &f->fmt.pix;
254 int ret;
255
7dfff953 256 dev_dbg(icd->pdev, "TRY_FMT(%c%c%c%c, %ux%u)\n",
dca6b6d1
SA
257 pixfmtstr(pix->pixelformat), pix->width, pix->height);
258
a70a6c43
AW
259 if (pix->pixelformat != V4L2_PIX_FMT_JPEG &&
260 !(ici->capabilities & SOCAM_HOST_CAP_STRIDE)) {
914f05c8
LP
261 pix->bytesperline = 0;
262 pix->sizeimage = 0;
263 }
dca6b6d1
SA
264
265 ret = ici->ops->try_fmt(icd, f);
266 if (ret < 0)
267 return ret;
268
bed8d803
LP
269 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
270 if (!xlate)
271 return -EINVAL;
dca6b6d1 272
bed8d803
LP
273 ret = soc_mbus_bytes_per_line(pix->width, xlate->host_fmt);
274 if (ret < 0)
275 return ret;
dca6b6d1 276
bed8d803
LP
277 pix->bytesperline = max_t(u32, pix->bytesperline, ret);
278
279 ret = soc_mbus_image_size(xlate->host_fmt, pix->bytesperline,
280 pix->height);
281 if (ret < 0)
282 return ret;
283
284 pix->sizeimage = max_t(u32, pix->sizeimage, ret);
dca6b6d1
SA
285
286 return 0;
287}
288
72937890 289static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
abe4c471 290 struct v4l2_format *f)
e55222ef 291{
57bee29d 292 struct soc_camera_device *icd = file->private_data;
e55222ef
GL
293
294 WARN_ON(priv != file->private_data);
295
d366d4a0
GL
296 /* Only single-plane capture is supported so far */
297 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
298 return -EINVAL;
299
ad5f2e85 300 /* limit format to hardware capabilities */
dca6b6d1 301 return soc_camera_try_fmt(icd, f);
e55222ef
GL
302}
303
304static int soc_camera_enum_input(struct file *file, void *priv,
305 struct v4l2_input *inp)
306{
d53c0f72
HV
307 struct soc_camera_device *icd = file->private_data;
308
e55222ef
GL
309 if (inp->index != 0)
310 return -EINVAL;
311
8318a64b
GL
312 /* default is camera */
313 inp->type = V4L2_INPUT_TYPE_CAMERA;
d53c0f72 314 inp->std = icd->vdev->tvnorms;
8318a64b 315 strcpy(inp->name, "Camera");
e55222ef 316
8318a64b 317 return 0;
e55222ef
GL
318}
319
320static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
321{
322 *i = 0;
323
324 return 0;
325}
326
327static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
328{
329 if (i > 0)
330 return -EINVAL;
331
332 return 0;
333}
334
314527ac 335static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id a)
e55222ef 336{
57bee29d 337 struct soc_camera_device *icd = file->private_data;
c9c1f1c0 338 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
513791ab 339
8774bed9 340 return v4l2_subdev_call(sd, video, s_std, a);
e55222ef
GL
341}
342
2f0babb7
GL
343static int soc_camera_g_std(struct file *file, void *priv, v4l2_std_id *a)
344{
345 struct soc_camera_device *icd = file->private_data;
346 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
347
8774bed9 348 return v4l2_subdev_call(sd, video, g_std, a);
2f0babb7
GL
349}
350
ad3537b5 351static int soc_camera_enum_framesizes(struct file *file, void *fh,
ed5b65dc
QX
352 struct v4l2_frmsizeenum *fsize)
353{
354 struct soc_camera_device *icd = file->private_data;
7dfff953 355 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
ed5b65dc 356
ad3537b5 357 return ici->ops->enum_framesizes(icd, fsize);
ed5b65dc
QX
358}
359
e55222ef
GL
360static int soc_camera_reqbufs(struct file *file, void *priv,
361 struct v4l2_requestbuffers *p)
362{
363 int ret;
57bee29d 364 struct soc_camera_device *icd = file->private_data;
e55222ef
GL
365
366 WARN_ON(priv != file->private_data);
367
57bee29d
GL
368 if (icd->streamer && icd->streamer != file)
369 return -EBUSY;
370
73a01599 371 ret = vb2_reqbufs(&icd->vb2_vidq, p);
9f317de4
HV
372 if (!ret)
373 icd->streamer = p->count ? file : NULL;
57bee29d 374 return ret;
e55222ef
GL
375}
376
377static int soc_camera_querybuf(struct file *file, void *priv,
378 struct v4l2_buffer *p)
379{
57bee29d 380 struct soc_camera_device *icd = file->private_data;
e55222ef
GL
381
382 WARN_ON(priv != file->private_data);
383
73a01599 384 return vb2_querybuf(&icd->vb2_vidq, p);
e55222ef
GL
385}
386
387static int soc_camera_qbuf(struct file *file, void *priv,
388 struct v4l2_buffer *p)
389{
57bee29d 390 struct soc_camera_device *icd = file->private_data;
e55222ef
GL
391
392 WARN_ON(priv != file->private_data);
393
57bee29d
GL
394 if (icd->streamer != file)
395 return -EBUSY;
396
73a01599 397 return vb2_qbuf(&icd->vb2_vidq, p);
e55222ef
GL
398}
399
400static int soc_camera_dqbuf(struct file *file, void *priv,
401 struct v4l2_buffer *p)
402{
57bee29d 403 struct soc_camera_device *icd = file->private_data;
e55222ef
GL
404
405 WARN_ON(priv != file->private_data);
406
57bee29d
GL
407 if (icd->streamer != file)
408 return -EBUSY;
409
73a01599 410 return vb2_dqbuf(&icd->vb2_vidq, p, file->f_flags & O_NONBLOCK);
e55222ef
GL
411}
412
7ae77ee9
GL
413static int soc_camera_create_bufs(struct file *file, void *priv,
414 struct v4l2_create_buffers *create)
415{
416 struct soc_camera_device *icd = file->private_data;
9f317de4 417 int ret;
7ae77ee9 418
9f317de4
HV
419 if (icd->streamer && icd->streamer != file)
420 return -EBUSY;
421
422 ret = vb2_create_bufs(&icd->vb2_vidq, create);
423 if (!ret)
424 icd->streamer = file;
425 return ret;
7ae77ee9
GL
426}
427
428static int soc_camera_prepare_buf(struct file *file, void *priv,
429 struct v4l2_buffer *b)
430{
431 struct soc_camera_device *icd = file->private_data;
7ae77ee9 432
73a01599 433 return vb2_prepare_buf(&icd->vb2_vidq, b);
7ae77ee9
GL
434}
435
c710f591
KK
436static int soc_camera_expbuf(struct file *file, void *priv,
437 struct v4l2_exportbuffer *p)
438{
439 struct soc_camera_device *icd = file->private_data;
eb01b1bc
HV
440
441 if (icd->streamer && icd->streamer != file)
442 return -EBUSY;
443 return vb2_expbuf(&icd->vb2_vidq, p);
c710f591
KK
444}
445
dd669e90 446/* Always entered with .host_lock held */
c2786ad2
GL
447static int soc_camera_init_user_formats(struct soc_camera_device *icd)
448{
760697be 449 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
7dfff953 450 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
3805f201
HV
451 unsigned int i, fmts = 0, raw_fmts = 0;
452 int ret;
ebcff5fc
HV
453 struct v4l2_subdev_mbus_code_enum code = {
454 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
455 };
760697be 456
ebcff5fc 457 while (!v4l2_subdev_call(sd, pad, enum_mbus_code, NULL, &code)) {
760697be 458 raw_fmts++;
ebcff5fc
HV
459 code.index++;
460 }
c2786ad2
GL
461
462 if (!ici->ops->get_formats)
463 /*
464 * Fallback mode - the host will have to serve all
465 * sensor-provided formats one-to-one to the user
466 */
760697be 467 fmts = raw_fmts;
c2786ad2
GL
468 else
469 /*
470 * First pass - only count formats this host-sensor
471 * configuration can provide
472 */
760697be 473 for (i = 0; i < raw_fmts; i++) {
fa48984e
GL
474 ret = ici->ops->get_formats(icd, i, NULL);
475 if (ret < 0)
476 return ret;
477 fmts += ret;
478 }
c2786ad2
GL
479
480 if (!fmts)
481 return -ENXIO;
482
483 icd->user_formats =
42bc47b3
KC
484 vmalloc(array_size(fmts,
485 sizeof(struct soc_camera_format_xlate)));
c2786ad2
GL
486 if (!icd->user_formats)
487 return -ENOMEM;
488
7dfff953 489 dev_dbg(icd->pdev, "Found %d supported formats.\n", fmts);
c2786ad2
GL
490
491 /* Second pass - actually fill data formats */
14df2cce 492 fmts = 0;
760697be 493 for (i = 0; i < raw_fmts; i++)
c2786ad2 494 if (!ici->ops->get_formats) {
ebcff5fc
HV
495 code.index = i;
496 v4l2_subdev_call(sd, pad, enum_mbus_code, NULL, &code);
d2dcad49 497 icd->user_formats[fmts].host_fmt =
ebcff5fc 498 soc_mbus_get_fmtdesc(code.code);
d2dcad49 499 if (icd->user_formats[fmts].host_fmt)
ebcff5fc 500 icd->user_formats[fmts++].code = code.code;
c2786ad2 501 } else {
fa48984e
GL
502 ret = ici->ops->get_formats(icd, i,
503 &icd->user_formats[fmts]);
504 if (ret < 0)
505 goto egfmt;
506 fmts += ret;
c2786ad2
GL
507 }
508
d2dcad49 509 icd->num_user_formats = fmts;
760697be 510 icd->current_fmt = &icd->user_formats[0];
c2786ad2
GL
511
512 return 0;
fa48984e
GL
513
514egfmt:
fa48984e
GL
515 vfree(icd->user_formats);
516 return ret;
c2786ad2
GL
517}
518
dd669e90 519/* Always entered with .host_lock held */
c2786ad2
GL
520static void soc_camera_free_user_formats(struct soc_camera_device *icd)
521{
7dfff953 522 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
fa48984e
GL
523
524 if (ici->ops->put_formats)
525 ici->ops->put_formats(icd);
40e2e092 526 icd->current_fmt = NULL;
fa48984e 527 icd->num_user_formats = 0;
c2786ad2 528 vfree(icd->user_formats);
40e2e092 529 icd->user_formats = NULL;
c2786ad2
GL
530}
531
760697be 532/* Called with .vb_lock held, or from the first open(2), see comment there */
57bee29d 533static int soc_camera_set_fmt(struct soc_camera_device *icd,
df2ed070
GL
534 struct v4l2_format *f)
535{
7dfff953 536 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
df2ed070
GL
537 struct v4l2_pix_format *pix = &f->fmt.pix;
538 int ret;
539
7dfff953 540 dev_dbg(icd->pdev, "S_FMT(%c%c%c%c, %ux%u)\n",
6a6c8786
GL
541 pixfmtstr(pix->pixelformat), pix->width, pix->height);
542
10d5509c 543 /* We always call try_fmt() before set_fmt() or set_selection() */
dca6b6d1 544 ret = soc_camera_try_fmt(icd, f);
df2ed070
GL
545 if (ret < 0)
546 return ret;
547
548 ret = ici->ops->set_fmt(icd, f);
549 if (ret < 0) {
550 return ret;
551 } else if (!icd->current_fmt ||
760697be 552 icd->current_fmt->host_fmt->fourcc != pix->pixelformat) {
7dfff953 553 dev_err(icd->pdev,
df2ed070
GL
554 "Host driver hasn't set up current format correctly!\n");
555 return -EINVAL;
556 }
557
6a6c8786
GL
558 icd->user_width = pix->width;
559 icd->user_height = pix->height;
0e4c180d
SA
560 icd->bytesperline = pix->bytesperline;
561 icd->sizeimage = pix->sizeimage;
760697be 562 icd->colorspace = pix->colorspace;
592c2aba 563 icd->field = pix->field;
025c18a1 564
7dfff953 565 dev_dbg(icd->pdev, "set width: %d height: %d\n",
6a6c8786 566 icd->user_width, icd->user_height);
df2ed070
GL
567
568 /* set physical bus parameters */
8843d119 569 return ici->ops->set_bus_param(icd);
df2ed070
GL
570}
571
f7f6ce2d
GL
572static int soc_camera_add_device(struct soc_camera_device *icd)
573{
574 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
575 int ret;
576
577 if (ici->icd)
578 return -EBUSY;
579
9aea470b 580 if (!icd->clk) {
3781760a 581 ret = soc_camera_clock_start(ici);
9aea470b
GL
582 if (ret < 0)
583 return ret;
584 }
a78fcc11
GL
585
586 if (ici->ops->add) {
587 ret = ici->ops->add(icd);
eb569cf9 588 if (ret < 0)
a78fcc11 589 goto eadd;
eb569cf9
GL
590 }
591
eb569cf9
GL
592 ici->icd = icd;
593
594 return 0;
f7f6ce2d 595
eb569cf9 596eadd:
3781760a
LP
597 if (!icd->clk)
598 soc_camera_clock_stop(ici);
f7f6ce2d
GL
599 return ret;
600}
601
602static void soc_camera_remove_device(struct soc_camera_device *icd)
603{
604 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
605
606 if (WARN_ON(icd != ici->icd))
607 return;
608
a78fcc11
GL
609 if (ici->ops->remove)
610 ici->ops->remove(icd);
3781760a
LP
611 if (!icd->clk)
612 soc_camera_clock_stop(ici);
f7f6ce2d
GL
613 ici->icd = NULL;
614}
615
bec43661 616static int soc_camera_open(struct file *file)
e55222ef 617{
979ea1dd 618 struct video_device *vdev = video_devdata(file);
2400a1f8 619 struct soc_camera_device *icd;
9dc4e48f 620 struct soc_camera_host *ici;
e55222ef
GL
621 int ret;
622
7d051b35
GL
623 /*
624 * Don't mess with the host during probe: wait until the loop in
2400a1f8
GL
625 * scan_add_host() completes. Also protect against a race with
626 * soc_camera_host_unregister().
7d051b35
GL
627 */
628 if (mutex_lock_interruptible(&list_lock))
629 return -ERESTARTSYS;
2400a1f8
GL
630
631 if (!vdev || !video_is_registered(vdev)) {
632 mutex_unlock(&list_lock);
633 return -ENODEV;
634 }
635
14381c26 636 icd = video_get_drvdata(vdev);
7dfff953 637 ici = to_soc_camera_host(icd->parent);
2400a1f8
GL
638
639 ret = try_module_get(ici->ops->owner) ? 0 : -ENODEV;
7d051b35 640 mutex_unlock(&list_lock);
e55222ef 641
2400a1f8 642 if (ret < 0) {
7dfff953 643 dev_err(icd->pdev, "Couldn't lock capture bus driver.\n");
2400a1f8
GL
644 return ret;
645 }
646
647 if (!to_soc_camera_control(icd)) {
648 /* No device driver attached */
649 ret = -ENODEV;
650 goto econtrol;
e55222ef
GL
651 }
652
2400a1f8
GL
653 if (mutex_lock_interruptible(&ici->host_lock)) {
654 ret = -ERESTARTSYS;
655 goto elockhost;
656 }
1a0063a9
GL
657 icd->use_count++;
658
9dc4e48f
GL
659 /* Now we really have to activate the camera */
660 if (icd->use_count == 1) {
2400a1f8 661 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
025c18a1 662 /* Restore parameters before the last close() per V4L2 API */
df2ed070
GL
663 struct v4l2_format f = {
664 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
665 .fmt.pix = {
6a6c8786
GL
666 .width = icd->user_width,
667 .height = icd->user_height,
025c18a1 668 .field = icd->field,
760697be
GL
669 .colorspace = icd->colorspace,
670 .pixelformat =
671 icd->current_fmt->host_fmt->fourcc,
df2ed070
GL
672 },
673 };
674
979ea1dd 675 /* The camera could have been already on, try to reset */
25a34811 676 if (sdesc->subdev_desc.reset)
0b9508aa
JW
677 if (icd->control)
678 sdesc->subdev_desc.reset(icd->control);
979ea1dd 679
f7f6ce2d 680 ret = soc_camera_add_device(icd);
9dc4e48f 681 if (ret < 0) {
7dfff953 682 dev_err(icd->pdev, "Couldn't activate the camera: %d\n", ret);
9dc4e48f
GL
683 goto eiciadd;
684 }
df2ed070 685
4ec10bac 686 ret = __soc_camera_power_on(icd);
7705b6d8
GL
687 if (ret < 0)
688 goto epower;
689
4f9fb5ed
MCC
690 pm_runtime_enable(&icd->vdev->dev);
691 ret = pm_runtime_resume(&icd->vdev->dev);
692 if (ret < 0 && ret != -ENOSYS)
693 goto eresume;
694
760697be
GL
695 /*
696 * Try to configure with default parameters. Notice: this is the
697 * very first open, so, we cannot race against other calls,
698 * apart from someone else calling open() simultaneously, but
dd669e90 699 * .host_lock is protecting us against it.
760697be 700 */
57bee29d 701 ret = soc_camera_set_fmt(icd, &f);
df2ed070
GL
702 if (ret < 0)
703 goto esfmt;
24d8c029 704
73a01599
LP
705 ret = ici->ops->init_videobuf2(&icd->vb2_vidq, icd);
706 if (ret < 0)
707 goto einitvb;
ee02da64 708 v4l2_ctrl_handler_setup(&icd->ctrl_handler);
9dc4e48f 709 }
dd669e90 710 mutex_unlock(&ici->host_lock);
9dc4e48f 711
57bee29d 712 file->private_data = icd;
7dfff953 713 dev_dbg(icd->pdev, "camera device open\n");
e55222ef 714
e55222ef
GL
715 return 0;
716
df2ed070 717 /*
e09da11d
GL
718 * All errors are entered with the .host_lock held, first four also
719 * with use_count == 1
df2ed070 720 */
592c2aba 721einitvb:
df2ed070 722esfmt:
4f9fb5ed
MCC
723 pm_runtime_disable(&icd->vdev->dev);
724eresume:
4ec10bac 725 __soc_camera_power_off(icd);
979ea1dd 726epower:
f7f6ce2d 727 soc_camera_remove_device(icd);
7705b6d8 728eiciadd:
c2786ad2 729 icd->use_count--;
dd669e90 730 mutex_unlock(&ici->host_lock);
2400a1f8
GL
731elockhost:
732econtrol:
733 module_put(ici->ops->owner);
57bee29d 734
e55222ef
GL
735 return ret;
736}
737
bec43661 738static int soc_camera_close(struct file *file)
e55222ef 739{
57bee29d 740 struct soc_camera_device *icd = file->private_data;
7dfff953 741 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
e55222ef 742
dd669e90 743 mutex_lock(&ici->host_lock);
16225ea7
HV
744 if (icd->streamer == file) {
745 if (ici->ops->init_videobuf2)
746 vb2_queue_release(&icd->vb2_vidq);
747 icd->streamer = NULL;
748 }
9dc4e48f 749 icd->use_count--;
40e2e092 750 if (!icd->use_count) {
4f9fb5ed
MCC
751 pm_runtime_suspend(&icd->vdev->dev);
752 pm_runtime_disable(&icd->vdev->dev);
753
4ec10bac 754 __soc_camera_power_off(icd);
af44ad5e 755
f7f6ce2d 756 soc_camera_remove_device(icd);
40e2e092 757 }
025c18a1 758
dd669e90 759 mutex_unlock(&ici->host_lock);
57bee29d 760
b8d9904c 761 module_put(ici->ops->owner);
9dc4e48f 762
7dfff953 763 dev_dbg(icd->pdev, "camera device close\n");
e55222ef
GL
764
765 return 0;
766}
767
aba360d8 768static ssize_t soc_camera_read(struct file *file, char __user *buf,
abe4c471 769 size_t count, loff_t *ppos)
e55222ef 770{
57bee29d 771 struct soc_camera_device *icd = file->private_data;
1438be56
AG
772 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
773
774 dev_dbg(icd->pdev, "read called, buf %p\n", buf);
775
776 if (ici->ops->init_videobuf2 && icd->vb2_vidq.io_modes & VB2_READ)
777 return vb2_read(&icd->vb2_vidq, buf, count, ppos,
778 file->f_flags & O_NONBLOCK);
e55222ef 779
7dfff953 780 dev_err(icd->pdev, "camera device read not implemented\n");
e55222ef 781
1438be56 782 return -EINVAL;
e55222ef
GL
783}
784
785static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
786{
57bee29d 787 struct soc_camera_device *icd = file->private_data;
7dfff953 788 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
e55222ef
GL
789 int err;
790
42eb523f 791 dev_dbg(icd->pdev, "mmap called, vma=%p\n", vma);
e55222ef 792
57bee29d
GL
793 if (icd->streamer != file)
794 return -EBUSY;
795
dd669e90 796 if (mutex_lock_interruptible(&ici->host_lock))
8422b087 797 return -ERESTARTSYS;
73a01599 798 err = vb2_mmap(&icd->vb2_vidq, vma);
dd669e90 799 mutex_unlock(&ici->host_lock);
e55222ef 800
7dfff953 801 dev_dbg(icd->pdev, "vma start=0x%08lx, size=%ld, ret=%d\n",
e55222ef
GL
802 (unsigned long)vma->vm_start,
803 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
804 err);
805
806 return err;
807}
808
c23e0cb8 809static __poll_t soc_camera_poll(struct file *file, poll_table *pt)
e55222ef 810{
57bee29d 811 struct soc_camera_device *icd = file->private_data;
7dfff953 812 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
a9a08845 813 __poll_t res = EPOLLERR;
e55222ef 814
57bee29d 815 if (icd->streamer != file)
a9a08845 816 return EPOLLERR;
e55222ef 817
dd669e90 818 mutex_lock(&ici->host_lock);
73a01599 819 res = ici->ops->poll(file, pt);
dd669e90 820 mutex_unlock(&ici->host_lock);
8422b087 821 return res;
e55222ef
GL
822}
823
6bcc0516 824static const struct v4l2_file_operations soc_camera_fops = {
e55222ef
GL
825 .owner = THIS_MODULE,
826 .open = soc_camera_open,
827 .release = soc_camera_close,
b6a633c1 828 .unlocked_ioctl = video_ioctl2,
e55222ef
GL
829 .read = soc_camera_read,
830 .mmap = soc_camera_mmap,
831 .poll = soc_camera_poll,
e55222ef
GL
832};
833
72937890 834static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
abe4c471 835 struct v4l2_format *f)
e55222ef 836{
57bee29d 837 struct soc_camera_device *icd = file->private_data;
e55222ef 838 int ret;
e55222ef
GL
839
840 WARN_ON(priv != file->private_data);
841
d366d4a0 842 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
7dfff953 843 dev_warn(icd->pdev, "Wrong buf-type %d\n", f->type);
d366d4a0
GL
844 return -EINVAL;
845 }
846
57bee29d
GL
847 if (icd->streamer && icd->streamer != file)
848 return -EBUSY;
1c3bb743 849
73a01599 850 if (vb2_is_streaming(&icd->vb2_vidq)) {
7dfff953 851 dev_err(icd->pdev, "S_FMT denied: queue initialised\n");
b6a633c1 852 return -EBUSY;
1c3bb743
GL
853 }
854
57bee29d
GL
855 ret = soc_camera_set_fmt(icd, f);
856
857 if (!ret && !icd->streamer)
858 icd->streamer = file;
1c3bb743 859
1c3bb743 860 return ret;
e55222ef
GL
861}
862
72937890 863static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
abe4c471 864 struct v4l2_fmtdesc *f)
e55222ef 865{
57bee29d 866 struct soc_camera_device *icd = file->private_data;
760697be 867 const struct soc_mbus_pixelfmt *format;
e55222ef
GL
868
869 WARN_ON(priv != file->private_data);
870
c2786ad2 871 if (f->index >= icd->num_user_formats)
e55222ef
GL
872 return -EINVAL;
873
c2786ad2 874 format = icd->user_formats[f->index].host_fmt;
e55222ef 875
760697be
GL
876 if (format->name)
877 strlcpy(f->description, format->name, sizeof(f->description));
e55222ef
GL
878 f->pixelformat = format->fourcc;
879 return 0;
880}
881
72937890 882static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
abe4c471 883 struct v4l2_format *f)
e55222ef 884{
57bee29d 885 struct soc_camera_device *icd = file->private_data;
64f5905e 886 struct v4l2_pix_format *pix = &f->fmt.pix;
e55222ef
GL
887
888 WARN_ON(priv != file->private_data);
889
d366d4a0
GL
890 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
891 return -EINVAL;
892
6a6c8786
GL
893 pix->width = icd->user_width;
894 pix->height = icd->user_height;
0e4c180d
SA
895 pix->bytesperline = icd->bytesperline;
896 pix->sizeimage = icd->sizeimage;
592c2aba 897 pix->field = icd->field;
760697be 898 pix->pixelformat = icd->current_fmt->host_fmt->fourcc;
760697be 899 pix->colorspace = icd->colorspace;
7dfff953 900 dev_dbg(icd->pdev, "current_fmt->fourcc: 0x%08x\n",
760697be 901 icd->current_fmt->host_fmt->fourcc);
e55222ef
GL
902 return 0;
903}
904
905static int soc_camera_querycap(struct file *file, void *priv,
906 struct v4l2_capability *cap)
907{
57bee29d 908 struct soc_camera_device *icd = file->private_data;
7dfff953 909 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
e55222ef
GL
910
911 WARN_ON(priv != file->private_data);
912
913 strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
b8d9904c 914 return ici->ops->querycap(ici, cap);
e55222ef
GL
915}
916
917static int soc_camera_streamon(struct file *file, void *priv,
918 enum v4l2_buf_type i)
919{
57bee29d 920 struct soc_camera_device *icd = file->private_data;
c9c1f1c0 921 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1c3bb743 922 int ret;
e55222ef
GL
923
924 WARN_ON(priv != file->private_data);
925
e55222ef
GL
926 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
927 return -EINVAL;
928
57bee29d
GL
929 if (icd->streamer != file)
930 return -EBUSY;
931
73a01599
LP
932 /* This calls buf_queue from host driver's videobuf2_queue_ops */
933 ret = vb2_streamon(&icd->vb2_vidq, i);
7fdbd85b
AG
934 if (!ret)
935 v4l2_subdev_call(sd, video, s_stream, 1);
1c3bb743 936
1c3bb743 937 return ret;
e55222ef
GL
938}
939
940static int soc_camera_streamoff(struct file *file, void *priv,
941 enum v4l2_buf_type i)
942{
57bee29d 943 struct soc_camera_device *icd = file->private_data;
c9c1f1c0 944 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
2d703835 945 int ret;
e55222ef
GL
946
947 WARN_ON(priv != file->private_data);
948
e55222ef
GL
949 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
950 return -EINVAL;
951
57bee29d
GL
952 if (icd->streamer != file)
953 return -EBUSY;
954
5d28d525 955 /*
73a01599 956 * This calls buf_release from host driver's videobuf2_queue_ops for all
5d28d525
GL
957 * remaining buffers. When the last buffer is freed, stop capture
958 */
73a01599 959 ret = vb2_streamoff(&icd->vb2_vidq, i);
e55222ef 960
c9c1f1c0 961 v4l2_subdev_call(sd, video, s_stream, 0);
e55222ef 962
2d703835 963 return ret;
e55222ef
GL
964}
965
3bfb4100
GL
966static int soc_camera_g_selection(struct file *file, void *fh,
967 struct v4l2_selection *s)
968{
969 struct soc_camera_device *icd = file->private_data;
970 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
971
972 /* With a wrong type no need to try to fall back to cropping */
973 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
974 return -EINVAL;
975
3bfb4100
GL
976 return ici->ops->get_selection(icd, s);
977}
978
979static int soc_camera_s_selection(struct file *file, void *fh,
980 struct v4l2_selection *s)
981{
982 struct soc_camera_device *icd = file->private_data;
983 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
984 int ret;
985
986 /* In all these cases cropping emulation will not help */
987 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
eb3c311c
SN
988 (s->target != V4L2_SEL_TGT_COMPOSE &&
989 s->target != V4L2_SEL_TGT_CROP))
3bfb4100
GL
990 return -EINVAL;
991
eb3c311c 992 if (s->target == V4L2_SEL_TGT_COMPOSE) {
3bfb4100 993 /* No output size change during a running capture! */
73a01599 994 if (vb2_is_streaming(&icd->vb2_vidq) &&
3bfb4100
GL
995 (icd->user_width != s->r.width ||
996 icd->user_height != s->r.height))
997 return -EBUSY;
998
999 /*
1000 * Only one user is allowed to change the output format, touch
1001 * buffers, start / stop streaming, poll for data
1002 */
1003 if (icd->streamer && icd->streamer != file)
1004 return -EBUSY;
1005 }
1006
73a01599
LP
1007 if (s->target == V4L2_SEL_TGT_CROP &&
1008 vb2_is_streaming(&icd->vb2_vidq) &&
10d5509c
HV
1009 ici->ops->set_liveselection)
1010 ret = ici->ops->set_liveselection(icd, s);
1011 else
1012 ret = ici->ops->set_selection(icd, s);
3bfb4100 1013 if (!ret &&
eb3c311c 1014 s->target == V4L2_SEL_TGT_COMPOSE) {
3bfb4100
GL
1015 icd->user_width = s->r.width;
1016 icd->user_height = s->r.height;
1017 if (!icd->streamer)
1018 icd->streamer = file;
1019 }
1020
1021 return ret;
1022}
1023
c9f6ef69
GL
1024static int soc_camera_g_parm(struct file *file, void *fh,
1025 struct v4l2_streamparm *a)
1026{
57bee29d 1027 struct soc_camera_device *icd = file->private_data;
7dfff953 1028 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
c9f6ef69
GL
1029
1030 if (ici->ops->get_parm)
1031 return ici->ops->get_parm(icd, a);
1032
1033 return -ENOIOCTLCMD;
1034}
1035
1036static int soc_camera_s_parm(struct file *file, void *fh,
1037 struct v4l2_streamparm *a)
1038{
57bee29d 1039 struct soc_camera_device *icd = file->private_data;
7dfff953 1040 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
c9f6ef69
GL
1041
1042 if (ici->ops->set_parm)
1043 return ici->ops->set_parm(icd, a);
1044
1045 return -ENOIOCTLCMD;
1046}
1047
e09da11d
GL
1048static int soc_camera_probe(struct soc_camera_host *ici,
1049 struct soc_camera_device *icd);
7dfff953 1050
e55222ef
GL
1051/* So far this function cannot fail */
1052static void scan_add_host(struct soc_camera_host *ici)
1053{
1054 struct soc_camera_device *icd;
1055
7d051b35 1056 mutex_lock(&list_lock);
e55222ef 1057
e09da11d 1058 list_for_each_entry(icd, &devices, list)
e55222ef 1059 if (icd->iface == ici->nr) {
e09da11d
GL
1060 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
1061 struct soc_camera_subdev_desc *ssdd = &sdesc->subdev_desc;
1062
1063 /* The camera could have been already on, try to reset */
1064 if (ssdd->reset)
0b9508aa
JW
1065 if (icd->control)
1066 ssdd->reset(icd->control);
e09da11d 1067
7dfff953 1068 icd->parent = ici->v4l2_dev.dev;
e09da11d
GL
1069
1070 /* Ignore errors */
1071 soc_camera_probe(ici, icd);
e55222ef 1072 }
e55222ef 1073
7d051b35 1074 mutex_unlock(&list_lock);
e55222ef
GL
1075}
1076
9aea470b
GL
1077/*
1078 * It is invalid to call v4l2_clk_enable() after a successful probing
1079 * asynchronously outside of V4L2 operations, i.e. with .host_lock not held.
1080 */
1081static int soc_camera_clk_enable(struct v4l2_clk *clk)
1082{
1083 struct soc_camera_device *icd = clk->priv;
1084 struct soc_camera_host *ici;
1085
1086 if (!icd || !icd->parent)
1087 return -ENODEV;
1088
1089 ici = to_soc_camera_host(icd->parent);
1090
1091 if (!try_module_get(ici->ops->owner))
1092 return -ENODEV;
1093
1094 /*
1095 * If a different client is currently being probed, the host will tell
1096 * you to go
1097 */
3781760a 1098 return soc_camera_clock_start(ici);
9aea470b
GL
1099}
1100
1101static void soc_camera_clk_disable(struct v4l2_clk *clk)
1102{
1103 struct soc_camera_device *icd = clk->priv;
1104 struct soc_camera_host *ici;
1105
1106 if (!icd || !icd->parent)
1107 return;
1108
1109 ici = to_soc_camera_host(icd->parent);
1110
3781760a 1111 soc_camera_clock_stop(ici);
9aea470b
GL
1112
1113 module_put(ici->ops->owner);
1114}
1115
1116/*
1117 * Eventually, it would be more logical to make the respective host the clock
1118 * owner, but then we would have to copy this struct for each ici. Besides, it
1119 * would introduce the circular dependency problem, unless we port all client
1120 * drivers to release the clock, when not in use.
1121 */
1122static const struct v4l2_clk_ops soc_camera_clk_ops = {
1123 .owner = THIS_MODULE,
1124 .enable = soc_camera_clk_enable,
1125 .disable = soc_camera_clk_disable,
1126};
1127
e09da11d
GL
1128static int soc_camera_dyn_pdev(struct soc_camera_desc *sdesc,
1129 struct soc_camera_async_client *sasc)
1130{
1131 struct platform_device *pdev;
1132 int ret, i;
1133
1134 mutex_lock(&list_lock);
1135 i = find_first_zero_bit(device_map, MAP_MAX_NUM);
1136 if (i < MAP_MAX_NUM)
1137 set_bit(i, device_map);
1138 mutex_unlock(&list_lock);
1139 if (i >= MAP_MAX_NUM)
1140 return -ENOMEM;
1141
1142 pdev = platform_device_alloc("soc-camera-pdrv", i);
1143 if (!pdev)
1144 return -ENOMEM;
1145
1146 ret = platform_device_add_data(pdev, sdesc, sizeof(*sdesc));
1147 if (ret < 0) {
1148 platform_device_put(pdev);
1149 return ret;
1150 }
1151
1152 sasc->pdev = pdev;
1153
1154 return 0;
1155}
1156
1157static struct soc_camera_device *soc_camera_add_pdev(struct soc_camera_async_client *sasc)
1158{
1159 struct platform_device *pdev = sasc->pdev;
1160 int ret;
1161
1162 ret = platform_device_add(pdev);
1163 if (ret < 0 || !pdev->dev.driver)
1164 return NULL;
1165
1166 return platform_get_drvdata(pdev);
1167}
1168
1169/* Locking: called with .host_lock held */
1170static int soc_camera_probe_finish(struct soc_camera_device *icd)
1171{
1172 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
da298c6d
HV
1173 struct v4l2_subdev_format fmt = {
1174 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1175 };
1176 struct v4l2_mbus_framefmt *mf = &fmt.format;
e09da11d
GL
1177 int ret;
1178
1179 sd->grp_id = soc_camera_grp_id(icd);
1180 v4l2_set_subdev_hostdata(sd, icd);
1181
f6cc51a9
HV
1182 v4l2_subdev_call(sd, video, g_tvnorms, &icd->vdev->tvnorms);
1183
e09da11d
GL
1184 ret = v4l2_ctrl_add_handler(&icd->ctrl_handler, sd->ctrl_handler, NULL);
1185 if (ret < 0)
1186 return ret;
1187
1188 ret = soc_camera_add_device(icd);
1189 if (ret < 0) {
1190 dev_err(icd->pdev, "Couldn't activate the camera: %d\n", ret);
1191 return ret;
1192 }
1193
1194 /* At this point client .probe() should have run already */
1195 ret = soc_camera_init_user_formats(icd);
1196 if (ret < 0)
1197 goto eusrfmt;
1198
1199 icd->field = V4L2_FIELD_ANY;
1200
1201 ret = soc_camera_video_start(icd);
1202 if (ret < 0)
1203 goto evidstart;
1204
1205 /* Try to improve our guess of a reasonable window format */
da298c6d
HV
1206 if (!v4l2_subdev_call(sd, pad, get_fmt, NULL, &fmt)) {
1207 icd->user_width = mf->width;
1208 icd->user_height = mf->height;
1209 icd->colorspace = mf->colorspace;
1210 icd->field = mf->field;
e09da11d
GL
1211 }
1212 soc_camera_remove_device(icd);
1213
1214 return 0;
1215
1216evidstart:
1217 soc_camera_free_user_formats(icd);
1218eusrfmt:
1219 soc_camera_remove_device(icd);
1220
1221 return ret;
1222}
1223
40e2e092 1224#ifdef CONFIG_I2C_BOARDINFO
e09da11d 1225static int soc_camera_i2c_init(struct soc_camera_device *icd,
25a34811 1226 struct soc_camera_desc *sdesc)
e55222ef 1227{
23272427 1228 struct soc_camera_subdev_desc *ssdd;
40e2e092 1229 struct i2c_client *client;
e09da11d 1230 struct soc_camera_host *ici;
25a34811 1231 struct soc_camera_host_desc *shd = &sdesc->host_desc;
e09da11d 1232 struct i2c_adapter *adap;
979ea1dd 1233 struct v4l2_subdev *subdev;
3d83078a 1234 char clk_name[V4L2_CLK_NAME_SIZE];
9aea470b 1235 int ret;
e55222ef 1236
e09da11d
GL
1237 /* First find out how we link the main client */
1238 if (icd->sasc) {
1239 /* Async non-OF probing handled by the subdevice list */
1240 return -EPROBE_DEFER;
1241 }
1242
1243 ici = to_soc_camera_host(icd->parent);
1244 adap = i2c_get_adapter(shd->i2c_adapter_id);
40e2e092 1245 if (!adap) {
7dfff953 1246 dev_err(icd->pdev, "Cannot get I2C adapter #%d. No driver?\n",
25a34811 1247 shd->i2c_adapter_id);
9aea470b 1248 return -ENODEV;
40e2e092 1249 }
e55222ef 1250
93623c87 1251 ssdd = kmemdup(&sdesc->subdev_desc, sizeof(*ssdd), GFP_KERNEL);
23272427
GL
1252 if (!ssdd) {
1253 ret = -ENOMEM;
1254 goto ealloc;
1255 }
23272427
GL
1256 /*
1257 * In synchronous case we request regulators ourselves in
1258 * soc_camera_pdrv_probe(), make sure the subdevice driver doesn't try
1259 * to allocate them again.
1260 */
d3f884a7
GL
1261 ssdd->sd_pdata.num_regulators = 0;
1262 ssdd->sd_pdata.regulators = NULL;
23272427 1263 shd->board_info->platform_data = ssdd;
e55222ef 1264
af4d8347
JW
1265 v4l2_clk_name_i2c(clk_name, sizeof(clk_name),
1266 shd->i2c_adapter_id, shd->board_info->addr);
9aea470b 1267
a37462b9 1268 icd->clk = v4l2_clk_register(&soc_camera_clk_ops, clk_name, icd);
9aea470b
GL
1269 if (IS_ERR(icd->clk)) {
1270 ret = PTR_ERR(icd->clk);
1271 goto eclkreg;
1272 }
1273
979ea1dd 1274 subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
25a34811 1275 shd->board_info, NULL);
9aea470b
GL
1276 if (!subdev) {
1277 ret = -ENODEV;
40e2e092 1278 goto ei2cnd;
9aea470b 1279 }
e55222ef 1280
c4ce6d14 1281 client = v4l2_get_subdevdata(subdev);
979ea1dd 1282
40e2e092 1283 /* Use to_i2c_client(dev) to recover the i2c client */
7dfff953 1284 icd->control = &client->dev;
e55222ef 1285
40e2e092
GL
1286 return 0;
1287ei2cnd:
9aea470b 1288 v4l2_clk_unregister(icd->clk);
e09da11d 1289 icd->clk = NULL;
23272427
GL
1290eclkreg:
1291 kfree(ssdd);
1292ealloc:
40e2e092 1293 i2c_put_adapter(adap);
9aea470b 1294 return ret;
e55222ef
GL
1295}
1296
e09da11d 1297static void soc_camera_i2c_free(struct soc_camera_device *icd)
40e2e092
GL
1298{
1299 struct i2c_client *client =
1300 to_i2c_client(to_soc_camera_control(icd));
e09da11d 1301 struct i2c_adapter *adap;
23272427 1302 struct soc_camera_subdev_desc *ssdd;
7dfff953
GL
1303
1304 icd->control = NULL;
e09da11d
GL
1305 if (icd->sasc)
1306 return;
1307
1308 adap = client->adapter;
23272427 1309 ssdd = client->dev.platform_data;
979ea1dd 1310 v4l2_device_unregister_subdev(i2c_get_clientdata(client));
40e2e092 1311 i2c_unregister_device(client);
c8dd7078 1312 i2c_put_adapter(adap);
23272427 1313 kfree(ssdd);
9aea470b
GL
1314 v4l2_clk_unregister(icd->clk);
1315 icd->clk = NULL;
40e2e092 1316}
e09da11d
GL
1317
1318/*
1319 * V4L2 asynchronous notifier callbacks. They are all called under a v4l2-async
1320 * internal global mutex, therefore cannot race against other asynchronous
1321 * events. Until notifier->complete() (soc_camera_async_complete()) is called,
1322 * the video device node is not registered and no V4L fops can occur. Unloading
1323 * of the host driver also calls a v4l2-async function, so also there we're
1324 * protected.
1325 */
1326static int soc_camera_async_bound(struct v4l2_async_notifier *notifier,
1327 struct v4l2_subdev *sd,
1328 struct v4l2_async_subdev *asd)
1329{
1330 struct soc_camera_async_client *sasc = container_of(notifier,
1331 struct soc_camera_async_client, notifier);
1332 struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev);
1333
1334 if (asd == sasc->sensor && !WARN_ON(icd->control)) {
1335 struct i2c_client *client = v4l2_get_subdevdata(sd);
1336
1337 /*
1338 * Only now we get subdevice-specific information like
1339 * regulators, flags, callbacks, etc.
1340 */
1341 if (client) {
1342 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
1343 struct soc_camera_subdev_desc *ssdd =
1344 soc_camera_i2c_to_desc(client);
1345 if (ssdd) {
1346 memcpy(&sdesc->subdev_desc, ssdd,
1347 sizeof(sdesc->subdev_desc));
1348 if (ssdd->reset)
0b9508aa 1349 ssdd->reset(&client->dev);
e09da11d
GL
1350 }
1351
1352 icd->control = &client->dev;
1353 }
1354 }
1355
1356 return 0;
1357}
1358
1359static void soc_camera_async_unbind(struct v4l2_async_notifier *notifier,
1360 struct v4l2_subdev *sd,
1361 struct v4l2_async_subdev *asd)
1362{
1363 struct soc_camera_async_client *sasc = container_of(notifier,
1364 struct soc_camera_async_client, notifier);
1365 struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev);
1366
e920ebc2
WS
1367 icd->control = NULL;
1368
e09da11d
GL
1369 if (icd->clk) {
1370 v4l2_clk_unregister(icd->clk);
1371 icd->clk = NULL;
1372 }
1373}
1374
1375static int soc_camera_async_complete(struct v4l2_async_notifier *notifier)
1376{
1377 struct soc_camera_async_client *sasc = container_of(notifier,
1378 struct soc_camera_async_client, notifier);
1379 struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev);
1380
1381 if (to_soc_camera_control(icd)) {
1382 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1383 int ret;
1384
1385 mutex_lock(&list_lock);
1386 ret = soc_camera_probe(ici, icd);
1387 mutex_unlock(&list_lock);
1388 if (ret < 0)
1389 return ret;
1390 }
1391
1392 return 0;
1393}
1394
b6ee3f0d
LP
1395static const struct v4l2_async_notifier_operations soc_camera_async_ops = {
1396 .bound = soc_camera_async_bound,
1397 .unbind = soc_camera_async_unbind,
1398 .complete = soc_camera_async_complete,
1399};
1400
e09da11d 1401static int scan_async_group(struct soc_camera_host *ici,
f687f326 1402 struct v4l2_async_subdev **asd, unsigned int size)
e09da11d
GL
1403{
1404 struct soc_camera_async_subdev *sasd;
1405 struct soc_camera_async_client *sasc;
1406 struct soc_camera_device *icd;
1407 struct soc_camera_desc sdesc = {.host_desc.bus_id = ici->nr,};
3d83078a 1408 char clk_name[V4L2_CLK_NAME_SIZE];
85e86c6e
HV
1409 unsigned int i;
1410 int ret;
e09da11d
GL
1411
1412 /* First look for a sensor */
1413 for (i = 0; i < size; i++) {
1414 sasd = container_of(asd[i], struct soc_camera_async_subdev, asd);
1415 if (sasd->role == SOCAM_SUBDEV_DATA_SOURCE)
1416 break;
1417 }
1418
85e86c6e 1419 if (i >= size || asd[i]->match_type != V4L2_ASYNC_MATCH_I2C) {
e09da11d
GL
1420 /* All useless */
1421 dev_err(ici->v4l2_dev.dev, "No I2C data source found!\n");
1422 return -ENODEV;
1423 }
1424
1425 /* Or shall this be managed by the soc-camera device? */
1426 sasc = devm_kzalloc(ici->v4l2_dev.dev, sizeof(*sasc), GFP_KERNEL);
1427 if (!sasc)
1428 return -ENOMEM;
1429
1430 /* HACK: just need a != NULL */
1431 sdesc.host_desc.board_info = ERR_PTR(-ENODATA);
1432
1433 ret = soc_camera_dyn_pdev(&sdesc, sasc);
1434 if (ret < 0)
057c2a2e 1435 goto eallocpdev;
e09da11d
GL
1436
1437 sasc->sensor = &sasd->asd;
1438
1439 icd = soc_camera_add_pdev(sasc);
1440 if (!icd) {
057c2a2e
GL
1441 ret = -ENOMEM;
1442 goto eaddpdev;
e09da11d
GL
1443 }
1444
e8419d08 1445 sasc->notifier.subdevs = asd;
e09da11d 1446 sasc->notifier.num_subdevs = size;
b6ee3f0d 1447 sasc->notifier.ops = &soc_camera_async_ops;
e09da11d
GL
1448
1449 icd->sasc = sasc;
1450 icd->parent = ici->v4l2_dev.dev;
1451
af4d8347
JW
1452 v4l2_clk_name_i2c(clk_name, sizeof(clk_name),
1453 sasd->asd.match.i2c.adapter_id,
1454 sasd->asd.match.i2c.address);
e09da11d 1455
a37462b9 1456 icd->clk = v4l2_clk_register(&soc_camera_clk_ops, clk_name, icd);
e09da11d
GL
1457 if (IS_ERR(icd->clk)) {
1458 ret = PTR_ERR(icd->clk);
1459 goto eclkreg;
1460 }
1461
1462 ret = v4l2_async_notifier_register(&ici->v4l2_dev, &sasc->notifier);
1463 if (!ret)
1464 return 0;
1465
1466 v4l2_clk_unregister(icd->clk);
1467eclkreg:
1468 icd->clk = NULL;
057c2a2e
GL
1469 platform_device_del(sasc->pdev);
1470eaddpdev:
1471 platform_device_put(sasc->pdev);
1472eallocpdev:
1473 devm_kfree(ici->v4l2_dev.dev, sasc);
e09da11d
GL
1474 dev_err(ici->v4l2_dev.dev, "group probe failed: %d\n", ret);
1475
1476 return ret;
1477}
1478
1479static void scan_async_host(struct soc_camera_host *ici)
1480{
1481 struct v4l2_async_subdev **asd;
1482 int j;
1483
1484 for (j = 0, asd = ici->asd; ici->asd_sizes[j]; j++) {
1485 scan_async_group(ici, asd, ici->asd_sizes[j]);
1486 asd += ici->asd_sizes[j];
1487 }
1488}
40e2e092 1489#else
e09da11d
GL
1490#define soc_camera_i2c_init(icd, sdesc) (-ENODEV)
1491#define soc_camera_i2c_free(icd) do {} while (0)
1492#define scan_async_host(ici) do {} while (0)
40e2e092
GL
1493#endif
1494
1ddc6a6c
BD
1495#ifdef CONFIG_OF
1496
1497struct soc_of_info {
1498 struct soc_camera_async_subdev sasd;
1499 struct soc_camera_async_client sasc;
1500 struct v4l2_async_subdev *subdev;
1501};
1502
1503static int soc_of_bind(struct soc_camera_host *ici,
1504 struct device_node *ep,
1505 struct device_node *remote)
1506{
1507 struct soc_camera_device *icd;
1508 struct soc_camera_desc sdesc = {.host_desc.bus_id = ici->nr,};
1509 struct soc_camera_async_client *sasc;
1510 struct soc_of_info *info;
1511 struct i2c_client *client;
3d83078a 1512 char clk_name[V4L2_CLK_NAME_SIZE];
1ddc6a6c
BD
1513 int ret;
1514
1515 /* allocate a new subdev and add match info to it */
1516 info = devm_kzalloc(ici->v4l2_dev.dev, sizeof(struct soc_of_info),
1517 GFP_KERNEL);
1518 if (!info)
1519 return -ENOMEM;
1520
4e48afec 1521 info->sasd.asd.match.fwnode = of_fwnode_handle(remote);
859969b3 1522 info->sasd.asd.match_type = V4L2_ASYNC_MATCH_FWNODE;
1ddc6a6c
BD
1523 info->subdev = &info->sasd.asd;
1524
1525 /* Or shall this be managed by the soc-camera device? */
1526 sasc = &info->sasc;
1527
1528 /* HACK: just need a != NULL */
1529 sdesc.host_desc.board_info = ERR_PTR(-ENODATA);
1530
1531 ret = soc_camera_dyn_pdev(&sdesc, sasc);
1532 if (ret < 0)
1533 goto eallocpdev;
1534
1535 sasc->sensor = &info->sasd.asd;
1536
1537 icd = soc_camera_add_pdev(sasc);
1538 if (!icd) {
1539 ret = -ENOMEM;
1540 goto eaddpdev;
1541 }
1542
1543 sasc->notifier.subdevs = &info->subdev;
1544 sasc->notifier.num_subdevs = 1;
b6ee3f0d 1545 sasc->notifier.ops = &soc_camera_async_ops;
1ddc6a6c
BD
1546
1547 icd->sasc = sasc;
1548 icd->parent = ici->v4l2_dev.dev;
1549
1550 client = of_find_i2c_device_by_node(remote);
1551
1552 if (client)
af4d8347
JW
1553 v4l2_clk_name_i2c(clk_name, sizeof(clk_name),
1554 client->adapter->nr, client->addr);
1ddc6a6c 1555 else
68d9c47b 1556 v4l2_clk_name_of(clk_name, sizeof(clk_name), remote);
1ddc6a6c 1557
a37462b9 1558 icd->clk = v4l2_clk_register(&soc_camera_clk_ops, clk_name, icd);
1ddc6a6c
BD
1559 if (IS_ERR(icd->clk)) {
1560 ret = PTR_ERR(icd->clk);
1561 goto eclkreg;
1562 }
1563
1564 ret = v4l2_async_notifier_register(&ici->v4l2_dev, &sasc->notifier);
1565 if (!ret)
1566 return 0;
2af12f02
LP
1567
1568 v4l2_clk_unregister(icd->clk);
1ddc6a6c
BD
1569eclkreg:
1570 icd->clk = NULL;
1571 platform_device_del(sasc->pdev);
1572eaddpdev:
1573 platform_device_put(sasc->pdev);
1574eallocpdev:
8e48a2d5 1575 devm_kfree(ici->v4l2_dev.dev, info);
1ddc6a6c
BD
1576 dev_err(ici->v4l2_dev.dev, "group probe failed: %d\n", ret);
1577
1578 return ret;
1579}
1580
1581static void scan_of_host(struct soc_camera_host *ici)
1582{
1583 struct device *dev = ici->v4l2_dev.dev;
1584 struct device_node *np = dev->of_node;
1585 struct device_node *epn = NULL, *ren;
1586 unsigned int i;
1587
1588 for (i = 0; ; i++) {
1589 epn = of_graph_get_next_endpoint(np, epn);
1590 if (!epn)
1591 break;
1592
1593 ren = of_graph_get_remote_port(epn);
1594 if (!ren) {
68d9c47b 1595 dev_notice(dev, "no remote for %pOF\n", epn);
1ddc6a6c
BD
1596 continue;
1597 }
1598
1599 /* so we now have a remote node to connect */
1600 if (!i)
1601 soc_of_bind(ici, epn, ren->parent);
1602
1ddc6a6c
BD
1603 of_node_put(ren);
1604
1605 if (i) {
1606 dev_err(dev, "multiple subdevices aren't supported yet!\n");
1607 break;
1608 }
1609 }
f033c0bc
PZ
1610
1611 of_node_put(epn);
1ddc6a6c
BD
1612}
1613
1614#else
1615static inline void scan_of_host(struct soc_camera_host *ici) { }
1616#endif
1617
40e2e092 1618/* Called during host-driver probe */
e09da11d
GL
1619static int soc_camera_probe(struct soc_camera_host *ici,
1620 struct soc_camera_device *icd)
e55222ef 1621{
25a34811
GL
1622 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
1623 struct soc_camera_host_desc *shd = &sdesc->host_desc;
979ea1dd 1624 struct device *control = NULL;
e55222ef
GL
1625 int ret;
1626
7dfff953 1627 dev_info(icd->pdev, "Probing %s\n", dev_name(icd->pdev));
1c3bb743 1628
ee02da64
HV
1629 /*
1630 * Currently the subdev with the largest number of controls (13) is
1631 * ov6550. So let's pick 16 as a hint for the control handler. Note
1632 * that this is a hint only: too large and you waste some memory, too
1633 * small and there is a (very) small performance hit when looking up
1634 * controls in the internal hash.
1635 */
1636 ret = v4l2_ctrl_handler_init(&icd->ctrl_handler, 16);
1637 if (ret < 0)
1638 return ret;
1639
979ea1dd 1640 /* Must have icd->vdev before registering the device */
40e2e092
GL
1641 ret = video_dev_create(icd);
1642 if (ret < 0)
1643 goto evdc;
1c3bb743 1644
9aea470b
GL
1645 /*
1646 * ..._video_start() will create a device node, video_register_device()
1647 * itself is protected against concurrent open() calls, but we also have
1648 * to protect our data also during client probing.
1649 */
9aea470b 1650
40e2e092 1651 /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
25a34811 1652 if (shd->board_info) {
e09da11d
GL
1653 ret = soc_camera_i2c_init(icd, sdesc);
1654 if (ret < 0 && ret != -EPROBE_DEFER)
9aea470b 1655 goto eadd;
25a34811 1656 } else if (!shd->add_device || !shd->del_device) {
1c3bb743 1657 ret = -EINVAL;
9aea470b 1658 goto eadd;
40e2e092 1659 } else {
3781760a 1660 ret = soc_camera_clock_start(ici);
9aea470b
GL
1661 if (ret < 0)
1662 goto eadd;
1663
25a34811
GL
1664 if (shd->module_name)
1665 ret = request_module(shd->module_name);
979ea1dd 1666
25a34811 1667 ret = shd->add_device(icd);
40e2e092
GL
1668 if (ret < 0)
1669 goto eadddev;
1c3bb743 1670
96c75399
GL
1671 /*
1672 * FIXME: this is racy, have to use driver-binding notification,
1673 * when it is available
1674 */
979ea1dd 1675 control = to_soc_camera_control(icd);
08590b96 1676 if (!control || !control->driver || !dev_get_drvdata(control) ||
979ea1dd 1677 !try_module_get(control->driver->owner)) {
25a34811 1678 shd->del_device(icd);
7ae77ee9 1679 ret = -ENODEV;
979ea1dd
GL
1680 goto enodrv;
1681 }
40e2e092 1682 }
e55222ef 1683
e09da11d
GL
1684 mutex_lock(&ici->host_lock);
1685 ret = soc_camera_probe_finish(icd);
dd669e90 1686 mutex_unlock(&ici->host_lock);
e09da11d
GL
1687 if (ret < 0)
1688 goto efinish;
025c18a1 1689
40e2e092 1690 return 0;
e55222ef 1691
e09da11d 1692efinish:
25a34811 1693 if (shd->board_info) {
e09da11d 1694 soc_camera_i2c_free(icd);
979ea1dd 1695 } else {
25a34811 1696 shd->del_device(icd);
979ea1dd 1697 module_put(control->driver->owner);
979ea1dd 1698enodrv:
40e2e092 1699eadddev:
3781760a 1700 soc_camera_clock_stop(ici);
9aea470b
GL
1701 }
1702eadd:
e09da11d
GL
1703 if (icd->vdev) {
1704 video_device_release(icd->vdev);
1705 icd->vdev = NULL;
1706 }
9aea470b 1707evdc:
ee02da64 1708 v4l2_ctrl_handler_free(&icd->ctrl_handler);
e55222ef
GL
1709 return ret;
1710}
1711
5d28d525
GL
1712/*
1713 * This is called on device_unregister, which only means we have to disconnect
e09da11d
GL
1714 * from the host, but not remove ourselves from the device list. With
1715 * asynchronous client probing this can also be called without
1716 * soc_camera_probe_finish() having run. Careful with clean up.
5d28d525 1717 */
7dfff953 1718static int soc_camera_remove(struct soc_camera_device *icd)
e55222ef 1719{
25a34811 1720 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
40e2e092 1721 struct video_device *vdev = icd->vdev;
e55222ef 1722
ee02da64 1723 v4l2_ctrl_handler_free(&icd->ctrl_handler);
40e2e092 1724 if (vdev) {
40e2e092
GL
1725 video_unregister_device(vdev);
1726 icd->vdev = NULL;
40e2e092
GL
1727 }
1728
25a34811 1729 if (sdesc->host_desc.board_info) {
e09da11d 1730 soc_camera_i2c_free(icd);
979ea1dd 1731 } else {
e09da11d
GL
1732 struct device *dev = to_soc_camera_control(icd);
1733 struct device_driver *drv = dev ? dev->driver : NULL;
979ea1dd 1734 if (drv) {
25a34811 1735 sdesc->host_desc.del_device(icd);
979ea1dd
GL
1736 module_put(drv->owner);
1737 }
1738 }
e09da11d
GL
1739
1740 if (icd->num_user_formats)
1741 soc_camera_free_user_formats(icd);
1742
1743 if (icd->clk) {
1744 /* For the synchronous case */
1745 v4l2_clk_unregister(icd->clk);
1746 icd->clk = NULL;
1747 }
1748
1749 if (icd->sasc)
1750 platform_device_unregister(icd->sasc->pdev);
025c18a1 1751
e55222ef
GL
1752 return 0;
1753}
1754
10d5509c
HV
1755static int default_g_selection(struct soc_camera_device *icd,
1756 struct v4l2_selection *sel)
6a6c8786
GL
1757{
1758 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
10d5509c
HV
1759 struct v4l2_subdev_selection sdsel = {
1760 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1761 .target = sel->target,
1762 };
1763 int ret;
6a6c8786 1764
10d5509c
HV
1765 ret = v4l2_subdev_call(sd, pad, get_selection, NULL, &sdsel);
1766 if (ret)
1767 return ret;
1768 sel->r = sdsel.r;
1769 return 0;
6a6c8786
GL
1770}
1771
10d5509c
HV
1772static int default_s_selection(struct soc_camera_device *icd,
1773 struct v4l2_selection *sel)
6a6c8786
GL
1774{
1775 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
10d5509c
HV
1776 struct v4l2_subdev_selection sdsel = {
1777 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1778 .target = sel->target,
1779 .flags = sel->flags,
1780 .r = sel->r,
1781 };
1782 int ret;
1783
1784 ret = v4l2_subdev_call(sd, pad, set_selection, NULL, &sdsel);
1785 if (ret)
1786 return ret;
1787 sel->r = sdsel.r;
1788 return 0;
6a6c8786
GL
1789}
1790
06e17821 1791static int default_g_parm(struct soc_camera_device *icd,
4471109e 1792 struct v4l2_streamparm *a)
06e17821
JK
1793{
1794 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
4471109e
HV
1795
1796 return v4l2_g_parm_cap(icd->vdev, sd, a);
06e17821
JK
1797}
1798
1799static int default_s_parm(struct soc_camera_device *icd,
4471109e 1800 struct v4l2_streamparm *a)
06e17821
JK
1801{
1802 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
4471109e
HV
1803
1804 return v4l2_s_parm_cap(icd->vdev, sd, a);
06e17821
JK
1805}
1806
ad3537b5
GL
1807static int default_enum_framesizes(struct soc_camera_device *icd,
1808 struct v4l2_frmsizeenum *fsize)
ed5b65dc
QX
1809{
1810 int ret;
1811 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1812 const struct soc_camera_format_xlate *xlate;
17bef885
HV
1813 struct v4l2_subdev_frame_size_enum fse = {
1814 .index = fsize->index,
1815 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1816 };
ed5b65dc 1817
17bef885 1818 xlate = soc_camera_xlate_by_fourcc(icd, fsize->pixel_format);
ed5b65dc
QX
1819 if (!xlate)
1820 return -EINVAL;
17bef885 1821 fse.code = xlate->code;
ed5b65dc 1822
17bef885 1823 ret = v4l2_subdev_call(sd, pad, enum_frame_size, NULL, &fse);
ed5b65dc
QX
1824 if (ret < 0)
1825 return ret;
1826
17bef885
HV
1827 if (fse.min_width == fse.max_width &&
1828 fse.min_height == fse.max_height) {
1829 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1830 fsize->discrete.width = fse.min_width;
1831 fsize->discrete.height = fse.min_height;
1832 return 0;
1833 }
1834 fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
1835 fsize->stepwise.min_width = fse.min_width;
1836 fsize->stepwise.max_width = fse.max_width;
1837 fsize->stepwise.min_height = fse.min_height;
1838 fsize->stepwise.max_height = fse.max_height;
1839 fsize->stepwise.step_width = 1;
1840 fsize->stepwise.step_height = 1;
ed5b65dc
QX
1841 return 0;
1842}
1843
b8d9904c 1844int soc_camera_host_register(struct soc_camera_host *ici)
e55222ef 1845{
e55222ef 1846 struct soc_camera_host *ix;
979ea1dd 1847 int ret;
e55222ef 1848
64f5905e
GL
1849 if (!ici || !ici->ops ||
1850 !ici->ops->try_fmt ||
1851 !ici->ops->set_fmt ||
1852 !ici->ops->set_bus_param ||
1853 !ici->ops->querycap ||
73a01599 1854 !ici->ops->init_videobuf2 ||
eff505fa 1855 !ici->ops->poll ||
979ea1dd 1856 !ici->v4l2_dev.dev)
e55222ef
GL
1857 return -EINVAL;
1858
10d5509c
HV
1859 if (!ici->ops->set_selection)
1860 ici->ops->set_selection = default_s_selection;
1861 if (!ici->ops->get_selection)
1862 ici->ops->get_selection = default_g_selection;
06e17821
JK
1863 if (!ici->ops->set_parm)
1864 ici->ops->set_parm = default_s_parm;
1865 if (!ici->ops->get_parm)
1866 ici->ops->get_parm = default_g_parm;
ad3537b5
GL
1867 if (!ici->ops->enum_framesizes)
1868 ici->ops->enum_framesizes = default_enum_framesizes;
6a6c8786 1869
e55222ef
GL
1870 mutex_lock(&list_lock);
1871 list_for_each_entry(ix, &hosts, list) {
1872 if (ix->nr == ici->nr) {
979ea1dd
GL
1873 ret = -EBUSY;
1874 goto edevreg;
e55222ef
GL
1875 }
1876 }
1877
979ea1dd
GL
1878 ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
1879 if (ret < 0)
1880 goto edevreg;
eff505fa 1881
e55222ef
GL
1882 list_add_tail(&ici->list, &hosts);
1883 mutex_unlock(&list_lock);
1884
2f9a0c88 1885 mutex_init(&ici->host_lock);
e09da11d
GL
1886 mutex_init(&ici->clk_lock);
1887
1ddc6a6c
BD
1888 if (ici->v4l2_dev.dev->of_node)
1889 scan_of_host(ici);
1890 else if (ici->asd_sizes)
e09da11d
GL
1891 /*
1892 * No OF, host with a list of subdevices. Don't try to mix
1893 * modes by initialising some groups statically and some
1894 * dynamically!
1895 */
1896 scan_async_host(ici);
1897 else
1898 /* Legacy: static platform devices from board data */
1899 scan_add_host(ici);
e55222ef
GL
1900
1901 return 0;
979ea1dd
GL
1902
1903edevreg:
1904 mutex_unlock(&list_lock);
1905 return ret;
e55222ef
GL
1906}
1907EXPORT_SYMBOL(soc_camera_host_register);
1908
1909/* Unregister all clients! */
1910void soc_camera_host_unregister(struct soc_camera_host *ici)
1911{
e09da11d
GL
1912 struct soc_camera_device *icd, *tmp;
1913 struct soc_camera_async_client *sasc;
1914 LIST_HEAD(notifiers);
e55222ef
GL
1915
1916 mutex_lock(&list_lock);
e55222ef 1917 list_del(&ici->list);
7dfff953 1918 list_for_each_entry(icd, &devices, list)
e09da11d
GL
1919 if (icd->iface == ici->nr && icd->sasc) {
1920 /* as long as we hold the device, sasc won't be freed */
1921 get_device(icd->pdev);
1922 list_add(&icd->sasc->list, &notifiers);
1923 }
1924 mutex_unlock(&list_lock);
1925
1926 list_for_each_entry(sasc, &notifiers, list) {
1927 /* Must call unlocked to avoid AB-BA dead-lock */
1928 v4l2_async_notifier_unregister(&sasc->notifier);
1929 put_device(&sasc->pdev->dev);
1930 }
1931
1932 mutex_lock(&list_lock);
1933
1934 list_for_each_entry_safe(icd, tmp, &devices, list)
1935 if (icd->iface == ici->nr)
7dfff953 1936 soc_camera_remove(icd);
e55222ef
GL
1937
1938 mutex_unlock(&list_lock);
1939
979ea1dd 1940 v4l2_device_unregister(&ici->v4l2_dev);
e55222ef
GL
1941}
1942EXPORT_SYMBOL(soc_camera_host_unregister);
1943
1944/* Image capture device */
40e2e092 1945static int soc_camera_device_register(struct soc_camera_device *icd)
e55222ef
GL
1946{
1947 struct soc_camera_device *ix;
1948 int num = -1, i;
1949
e09da11d 1950 mutex_lock(&list_lock);
e55222ef
GL
1951 for (i = 0; i < 256 && num < 0; i++) {
1952 num = i;
40e2e092 1953 /* Check if this index is available on this interface */
e55222ef
GL
1954 list_for_each_entry(ix, &devices, list) {
1955 if (ix->iface == icd->iface && ix->devnum == i) {
1956 num = -1;
1957 break;
1958 }
1959 }
1960 }
1961
e09da11d 1962 if (num < 0) {
5d28d525
GL
1963 /*
1964 * ok, we have 256 cameras on this host...
1965 * man, stay reasonable...
1966 */
e09da11d 1967 mutex_unlock(&list_lock);
e55222ef 1968 return -ENOMEM;
e09da11d 1969 }
e55222ef 1970
64ff9ba5 1971 icd->devnum = num;
64f5905e
GL
1972 icd->use_count = 0;
1973 icd->host_priv = NULL;
e55222ef 1974
e09da11d
GL
1975 /*
1976 * Dynamically allocated devices set the bit earlier, but it doesn't hurt setting
1977 * it again
1978 */
1979 i = to_platform_device(icd->pdev)->id;
1980 if (i < 0)
1981 /* One static (legacy) soc-camera platform device */
1982 i = 0;
1983 if (i >= MAP_MAX_NUM) {
1984 mutex_unlock(&list_lock);
1985 return -EBUSY;
1986 }
1987 set_bit(i, device_map);
40e2e092 1988 list_add_tail(&icd->list, &devices);
e09da11d 1989 mutex_unlock(&list_lock);
40e2e092
GL
1990
1991 return 0;
e55222ef 1992}
e55222ef 1993
a399810c
HV
1994static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
1995 .vidioc_querycap = soc_camera_querycap,
7ae77ee9 1996 .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
a399810c 1997 .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
a399810c 1998 .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
7ae77ee9 1999 .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
a399810c
HV
2000 .vidioc_enum_input = soc_camera_enum_input,
2001 .vidioc_g_input = soc_camera_g_input,
2002 .vidioc_s_input = soc_camera_s_input,
2003 .vidioc_s_std = soc_camera_s_std,
2f0babb7 2004 .vidioc_g_std = soc_camera_g_std,
ad3537b5 2005 .vidioc_enum_framesizes = soc_camera_enum_framesizes,
a399810c 2006 .vidioc_reqbufs = soc_camera_reqbufs,
a399810c
HV
2007 .vidioc_querybuf = soc_camera_querybuf,
2008 .vidioc_qbuf = soc_camera_qbuf,
2009 .vidioc_dqbuf = soc_camera_dqbuf,
7ae77ee9
GL
2010 .vidioc_create_bufs = soc_camera_create_bufs,
2011 .vidioc_prepare_buf = soc_camera_prepare_buf,
c710f591 2012 .vidioc_expbuf = soc_camera_expbuf,
a399810c
HV
2013 .vidioc_streamon = soc_camera_streamon,
2014 .vidioc_streamoff = soc_camera_streamoff,
3bfb4100
GL
2015 .vidioc_g_selection = soc_camera_g_selection,
2016 .vidioc_s_selection = soc_camera_s_selection,
c9f6ef69
GL
2017 .vidioc_g_parm = soc_camera_g_parm,
2018 .vidioc_s_parm = soc_camera_s_parm,
a399810c
HV
2019};
2020
40e2e092 2021static int video_dev_create(struct soc_camera_device *icd)
e55222ef 2022{
7dfff953 2023 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
40e2e092 2024 struct video_device *vdev = video_device_alloc();
e55222ef 2025
e55222ef 2026 if (!vdev)
40e2e092 2027 return -ENOMEM;
e55222ef
GL
2028
2029 strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
1c3bb743 2030
14381c26 2031 vdev->v4l2_dev = &ici->v4l2_dev;
e55222ef 2032 vdev->fops = &soc_camera_fops;
a399810c 2033 vdev->ioctl_ops = &soc_camera_ioctl_ops;
e55222ef 2034 vdev->release = video_device_release;
ee02da64 2035 vdev->ctrl_handler = &icd->ctrl_handler;
dd669e90 2036 vdev->lock = &ici->host_lock;
e55222ef 2037
e55222ef
GL
2038 icd->vdev = vdev;
2039
2040 return 0;
40e2e092 2041}
e55222ef 2042
40e2e092 2043/*
dd669e90 2044 * Called from soc_camera_probe() above with .host_lock held
40e2e092 2045 */
979ea1dd 2046static int soc_camera_video_start(struct soc_camera_device *icd)
40e2e092 2047{
d364ee4f 2048 const struct device_type *type = icd->vdev->dev.type;
979ea1dd 2049 int ret;
40e2e092 2050
7dfff953 2051 if (!icd->parent)
40e2e092
GL
2052 return -ENODEV;
2053
14381c26 2054 video_set_drvdata(icd->vdev, icd);
f6cc51a9
HV
2055 if (icd->vdev->tvnorms == 0) {
2056 /* disable the STD API if there are no tvnorms defined */
2057 v4l2_disable_ioctl(icd->vdev, VIDIOC_G_STD);
2058 v4l2_disable_ioctl(icd->vdev, VIDIOC_S_STD);
2059 v4l2_disable_ioctl(icd->vdev, VIDIOC_ENUMSTD);
2060 }
46b21094 2061 ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
979ea1dd 2062 if (ret < 0) {
7dfff953 2063 dev_err(icd->pdev, "video_register_device failed: %d\n", ret);
979ea1dd
GL
2064 return ret;
2065 }
40e2e092 2066
4f9fb5ed
MCC
2067 /* Restore device type, possibly set by the subdevice driver */
2068 icd->vdev->dev.type = type;
2069
979ea1dd 2070 return 0;
e55222ef 2071}
e55222ef 2072
4c62e976 2073static int soc_camera_pdrv_probe(struct platform_device *pdev)
0fd327bd 2074{
25a34811
GL
2075 struct soc_camera_desc *sdesc = pdev->dev.platform_data;
2076 struct soc_camera_subdev_desc *ssdd = &sdesc->subdev_desc;
40e2e092 2077 struct soc_camera_device *icd;
8a97d4c1 2078 int ret;
0fd327bd 2079
25a34811 2080 if (!sdesc)
40e2e092 2081 return -EINVAL;
0fd327bd 2082
02249369 2083 icd = devm_kzalloc(&pdev->dev, sizeof(*icd), GFP_KERNEL);
40e2e092
GL
2084 if (!icd)
2085 return -ENOMEM;
0fd327bd 2086
e09da11d
GL
2087 /*
2088 * In the asynchronous case ssdd->num_regulators == 0 yet, so, the below
23272427
GL
2089 * regulator allocation is a dummy. They are actually requested by the
2090 * subdevice driver, using soc_camera_power_init(). Also note, that in
2091 * that case regulators are attached to the I2C device and not to the
2092 * camera platform device.
e09da11d 2093 */
d3f884a7
GL
2094 ret = devm_regulator_bulk_get(&pdev->dev, ssdd->sd_pdata.num_regulators,
2095 ssdd->sd_pdata.regulators);
8a97d4c1
GL
2096 if (ret < 0)
2097 return ret;
2098
25a34811
GL
2099 icd->iface = sdesc->host_desc.bus_id;
2100 icd->sdesc = sdesc;
979ea1dd 2101 icd->pdev = &pdev->dev;
40e2e092 2102 platform_set_drvdata(pdev, icd);
0fd327bd 2103
6a6c8786
GL
2104 icd->user_width = DEFAULT_WIDTH;
2105 icd->user_height = DEFAULT_HEIGHT;
2106
02249369 2107 return soc_camera_device_register(icd);
c41debaf 2108}
0fd327bd 2109
5d28d525
GL
2110/*
2111 * Only called on rmmod for each platform device, since they are not
40e2e092 2112 * hot-pluggable. Now we know, that all our users - hosts and devices have
5d28d525
GL
2113 * been unloaded already
2114 */
4c62e976 2115static int soc_camera_pdrv_remove(struct platform_device *pdev)
c41debaf 2116{
40e2e092 2117 struct soc_camera_device *icd = platform_get_drvdata(pdev);
e09da11d 2118 int i;
c41debaf 2119
40e2e092 2120 if (!icd)
c41debaf
GL
2121 return -EINVAL;
2122
e09da11d
GL
2123 i = pdev->id;
2124 if (i < 0)
2125 i = 0;
2126
2127 /*
2128 * In synchronous mode with static platform devices this is called in a
2129 * loop from drivers/base/dd.c::driver_detach(), no parallel execution,
2130 * no need to lock. In asynchronous case the caller -
2131 * soc_camera_host_unregister() - already holds the lock
2132 */
2133 if (test_bit(i, device_map)) {
2134 clear_bit(i, device_map);
2135 list_del(&icd->list);
2136 }
c41debaf 2137
0fd327bd
GL
2138 return 0;
2139}
2140
2141static struct platform_driver __refdata soc_camera_pdrv = {
1858c99d 2142 .probe = soc_camera_pdrv_probe,
4c62e976 2143 .remove = soc_camera_pdrv_remove,
40e2e092
GL
2144 .driver = {
2145 .name = "soc-camera-pdrv",
0fd327bd
GL
2146 },
2147};
2148
ec0341b3 2149module_platform_driver(soc_camera_pdrv);
e55222ef
GL
2150
2151MODULE_DESCRIPTION("Image capture bus driver");
2152MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
2153MODULE_LICENSE("GPL");
0fd327bd 2154MODULE_ALIAS("platform:soc-camera-pdrv");