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