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