Merge tag 'hwmon-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/groeck...
[linux-drm-fsl-dcu.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, core, 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, core, 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         ret = v4l2_ctrl_add_handler(&icd->ctrl_handler, sd->ctrl_handler, NULL);
1281         if (ret < 0)
1282                 return ret;
1283
1284         ret = soc_camera_add_device(icd);
1285         if (ret < 0) {
1286                 dev_err(icd->pdev, "Couldn't activate the camera: %d\n", ret);
1287                 return ret;
1288         }
1289
1290         /* At this point client .probe() should have run already */
1291         ret = soc_camera_init_user_formats(icd);
1292         if (ret < 0)
1293                 goto eusrfmt;
1294
1295         icd->field = V4L2_FIELD_ANY;
1296
1297         ret = soc_camera_video_start(icd);
1298         if (ret < 0)
1299                 goto evidstart;
1300
1301         /* Try to improve our guess of a reasonable window format */
1302         if (!v4l2_subdev_call(sd, video, g_mbus_fmt, &mf)) {
1303                 icd->user_width         = mf.width;
1304                 icd->user_height        = mf.height;
1305                 icd->colorspace         = mf.colorspace;
1306                 icd->field              = mf.field;
1307         }
1308         soc_camera_remove_device(icd);
1309
1310         return 0;
1311
1312 evidstart:
1313         soc_camera_free_user_formats(icd);
1314 eusrfmt:
1315         soc_camera_remove_device(icd);
1316
1317         return ret;
1318 }
1319
1320 #ifdef CONFIG_I2C_BOARDINFO
1321 static int soc_camera_i2c_init(struct soc_camera_device *icd,
1322                                struct soc_camera_desc *sdesc)
1323 {
1324         struct soc_camera_subdev_desc *ssdd;
1325         struct i2c_client *client;
1326         struct soc_camera_host *ici;
1327         struct soc_camera_host_desc *shd = &sdesc->host_desc;
1328         struct i2c_adapter *adap;
1329         struct v4l2_subdev *subdev;
1330         char clk_name[V4L2_SUBDEV_NAME_SIZE];
1331         int ret;
1332
1333         /* First find out how we link the main client */
1334         if (icd->sasc) {
1335                 /* Async non-OF probing handled by the subdevice list */
1336                 return -EPROBE_DEFER;
1337         }
1338
1339         ici = to_soc_camera_host(icd->parent);
1340         adap = i2c_get_adapter(shd->i2c_adapter_id);
1341         if (!adap) {
1342                 dev_err(icd->pdev, "Cannot get I2C adapter #%d. No driver?\n",
1343                         shd->i2c_adapter_id);
1344                 return -ENODEV;
1345         }
1346
1347         ssdd = kzalloc(sizeof(*ssdd), GFP_KERNEL);
1348         if (!ssdd) {
1349                 ret = -ENOMEM;
1350                 goto ealloc;
1351         }
1352
1353         memcpy(ssdd, &sdesc->subdev_desc, sizeof(*ssdd));
1354         /*
1355          * In synchronous case we request regulators ourselves in
1356          * soc_camera_pdrv_probe(), make sure the subdevice driver doesn't try
1357          * to allocate them again.
1358          */
1359         ssdd->sd_pdata.num_regulators = 0;
1360         ssdd->sd_pdata.regulators = NULL;
1361         shd->board_info->platform_data = ssdd;
1362
1363         snprintf(clk_name, sizeof(clk_name), "%d-%04x",
1364                  shd->i2c_adapter_id, shd->board_info->addr);
1365
1366         icd->clk = v4l2_clk_register(&soc_camera_clk_ops, clk_name, "mclk", icd);
1367         if (IS_ERR(icd->clk)) {
1368                 ret = PTR_ERR(icd->clk);
1369                 goto eclkreg;
1370         }
1371
1372         subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
1373                                 shd->board_info, NULL);
1374         if (!subdev) {
1375                 ret = -ENODEV;
1376                 goto ei2cnd;
1377         }
1378
1379         client = v4l2_get_subdevdata(subdev);
1380
1381         /* Use to_i2c_client(dev) to recover the i2c client */
1382         icd->control = &client->dev;
1383
1384         return 0;
1385 ei2cnd:
1386         v4l2_clk_unregister(icd->clk);
1387         icd->clk = NULL;
1388 eclkreg:
1389         kfree(ssdd);
1390 ealloc:
1391         i2c_put_adapter(adap);
1392         return ret;
1393 }
1394
1395 static void soc_camera_i2c_free(struct soc_camera_device *icd)
1396 {
1397         struct i2c_client *client =
1398                 to_i2c_client(to_soc_camera_control(icd));
1399         struct i2c_adapter *adap;
1400         struct soc_camera_subdev_desc *ssdd;
1401
1402         icd->control = NULL;
1403         if (icd->sasc)
1404                 return;
1405
1406         adap = client->adapter;
1407         ssdd = client->dev.platform_data;
1408         v4l2_device_unregister_subdev(i2c_get_clientdata(client));
1409         i2c_unregister_device(client);
1410         i2c_put_adapter(adap);
1411         kfree(ssdd);
1412         v4l2_clk_unregister(icd->clk);
1413         icd->clk = NULL;
1414 }
1415
1416 /*
1417  * V4L2 asynchronous notifier callbacks. They are all called under a v4l2-async
1418  * internal global mutex, therefore cannot race against other asynchronous
1419  * events. Until notifier->complete() (soc_camera_async_complete()) is called,
1420  * the video device node is not registered and no V4L fops can occur. Unloading
1421  * of the host driver also calls a v4l2-async function, so also there we're
1422  * protected.
1423  */
1424 static int soc_camera_async_bound(struct v4l2_async_notifier *notifier,
1425                                   struct v4l2_subdev *sd,
1426                                   struct v4l2_async_subdev *asd)
1427 {
1428         struct soc_camera_async_client *sasc = container_of(notifier,
1429                                         struct soc_camera_async_client, notifier);
1430         struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev);
1431
1432         if (asd == sasc->sensor && !WARN_ON(icd->control)) {
1433                 struct i2c_client *client = v4l2_get_subdevdata(sd);
1434
1435                 /*
1436                  * Only now we get subdevice-specific information like
1437                  * regulators, flags, callbacks, etc.
1438                  */
1439                 if (client) {
1440                         struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
1441                         struct soc_camera_subdev_desc *ssdd =
1442                                 soc_camera_i2c_to_desc(client);
1443                         if (ssdd) {
1444                                 memcpy(&sdesc->subdev_desc, ssdd,
1445                                        sizeof(sdesc->subdev_desc));
1446                                 if (ssdd->reset)
1447                                         ssdd->reset(icd->pdev);
1448                         }
1449
1450                         icd->control = &client->dev;
1451                 }
1452         }
1453
1454         return 0;
1455 }
1456
1457 static void soc_camera_async_unbind(struct v4l2_async_notifier *notifier,
1458                                     struct v4l2_subdev *sd,
1459                                     struct v4l2_async_subdev *asd)
1460 {
1461         struct soc_camera_async_client *sasc = container_of(notifier,
1462                                         struct soc_camera_async_client, notifier);
1463         struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev);
1464
1465         if (icd->clk) {
1466                 v4l2_clk_unregister(icd->clk);
1467                 icd->clk = NULL;
1468         }
1469 }
1470
1471 static int soc_camera_async_complete(struct v4l2_async_notifier *notifier)
1472 {
1473         struct soc_camera_async_client *sasc = container_of(notifier,
1474                                         struct soc_camera_async_client, notifier);
1475         struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev);
1476
1477         if (to_soc_camera_control(icd)) {
1478                 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1479                 int ret;
1480
1481                 mutex_lock(&list_lock);
1482                 ret = soc_camera_probe(ici, icd);
1483                 mutex_unlock(&list_lock);
1484                 if (ret < 0)
1485                         return ret;
1486         }
1487
1488         return 0;
1489 }
1490
1491 static int scan_async_group(struct soc_camera_host *ici,
1492                             struct v4l2_async_subdev **asd, unsigned int size)
1493 {
1494         struct soc_camera_async_subdev *sasd;
1495         struct soc_camera_async_client *sasc;
1496         struct soc_camera_device *icd;
1497         struct soc_camera_desc sdesc = {.host_desc.bus_id = ici->nr,};
1498         char clk_name[V4L2_SUBDEV_NAME_SIZE];
1499         unsigned int i;
1500         int ret;
1501
1502         /* First look for a sensor */
1503         for (i = 0; i < size; i++) {
1504                 sasd = container_of(asd[i], struct soc_camera_async_subdev, asd);
1505                 if (sasd->role == SOCAM_SUBDEV_DATA_SOURCE)
1506                         break;
1507         }
1508
1509         if (i >= size || asd[i]->match_type != V4L2_ASYNC_MATCH_I2C) {
1510                 /* All useless */
1511                 dev_err(ici->v4l2_dev.dev, "No I2C data source found!\n");
1512                 return -ENODEV;
1513         }
1514
1515         /* Or shall this be managed by the soc-camera device? */
1516         sasc = devm_kzalloc(ici->v4l2_dev.dev, sizeof(*sasc), GFP_KERNEL);
1517         if (!sasc)
1518                 return -ENOMEM;
1519
1520         /* HACK: just need a != NULL */
1521         sdesc.host_desc.board_info = ERR_PTR(-ENODATA);
1522
1523         ret = soc_camera_dyn_pdev(&sdesc, sasc);
1524         if (ret < 0)
1525                 return ret;
1526
1527         sasc->sensor = &sasd->asd;
1528
1529         icd = soc_camera_add_pdev(sasc);
1530         if (!icd) {
1531                 platform_device_put(sasc->pdev);
1532                 return -ENOMEM;
1533         }
1534
1535         sasc->notifier.subdevs = asd;
1536         sasc->notifier.num_subdevs = size;
1537         sasc->notifier.bound = soc_camera_async_bound;
1538         sasc->notifier.unbind = soc_camera_async_unbind;
1539         sasc->notifier.complete = soc_camera_async_complete;
1540
1541         icd->sasc = sasc;
1542         icd->parent = ici->v4l2_dev.dev;
1543
1544         snprintf(clk_name, sizeof(clk_name), "%d-%04x",
1545                  sasd->asd.match.i2c.adapter_id, sasd->asd.match.i2c.address);
1546
1547         icd->clk = v4l2_clk_register(&soc_camera_clk_ops, clk_name, "mclk", icd);
1548         if (IS_ERR(icd->clk)) {
1549                 ret = PTR_ERR(icd->clk);
1550                 goto eclkreg;
1551         }
1552
1553         ret = v4l2_async_notifier_register(&ici->v4l2_dev, &sasc->notifier);
1554         if (!ret)
1555                 return 0;
1556
1557         v4l2_clk_unregister(icd->clk);
1558 eclkreg:
1559         icd->clk = NULL;
1560         platform_device_unregister(sasc->pdev);
1561         dev_err(ici->v4l2_dev.dev, "group probe failed: %d\n", ret);
1562
1563         return ret;
1564 }
1565
1566 static void scan_async_host(struct soc_camera_host *ici)
1567 {
1568         struct v4l2_async_subdev **asd;
1569         int j;
1570
1571         for (j = 0, asd = ici->asd; ici->asd_sizes[j]; j++) {
1572                 scan_async_group(ici, asd, ici->asd_sizes[j]);
1573                 asd += ici->asd_sizes[j];
1574         }
1575 }
1576 #else
1577 #define soc_camera_i2c_init(icd, sdesc) (-ENODEV)
1578 #define soc_camera_i2c_free(icd)        do {} while (0)
1579 #define scan_async_host(ici)            do {} while (0)
1580 #endif
1581
1582 /* Called during host-driver probe */
1583 static int soc_camera_probe(struct soc_camera_host *ici,
1584                             struct soc_camera_device *icd)
1585 {
1586         struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
1587         struct soc_camera_host_desc *shd = &sdesc->host_desc;
1588         struct device *control = NULL;
1589         int ret;
1590
1591         dev_info(icd->pdev, "Probing %s\n", dev_name(icd->pdev));
1592
1593         /*
1594          * Currently the subdev with the largest number of controls (13) is
1595          * ov6550. So let's pick 16 as a hint for the control handler. Note
1596          * that this is a hint only: too large and you waste some memory, too
1597          * small and there is a (very) small performance hit when looking up
1598          * controls in the internal hash.
1599          */
1600         ret = v4l2_ctrl_handler_init(&icd->ctrl_handler, 16);
1601         if (ret < 0)
1602                 return ret;
1603
1604         /* Must have icd->vdev before registering the device */
1605         ret = video_dev_create(icd);
1606         if (ret < 0)
1607                 goto evdc;
1608
1609         /*
1610          * ..._video_start() will create a device node, video_register_device()
1611          * itself is protected against concurrent open() calls, but we also have
1612          * to protect our data also during client probing.
1613          */
1614
1615         /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
1616         if (shd->board_info) {
1617                 ret = soc_camera_i2c_init(icd, sdesc);
1618                 if (ret < 0 && ret != -EPROBE_DEFER)
1619                         goto eadd;
1620         } else if (!shd->add_device || !shd->del_device) {
1621                 ret = -EINVAL;
1622                 goto eadd;
1623         } else {
1624                 mutex_lock(&ici->clk_lock);
1625                 ret = ici->ops->clock_start(ici);
1626                 mutex_unlock(&ici->clk_lock);
1627                 if (ret < 0)
1628                         goto eadd;
1629
1630                 if (shd->module_name)
1631                         ret = request_module(shd->module_name);
1632
1633                 ret = shd->add_device(icd);
1634                 if (ret < 0)
1635                         goto eadddev;
1636
1637                 /*
1638                  * FIXME: this is racy, have to use driver-binding notification,
1639                  * when it is available
1640                  */
1641                 control = to_soc_camera_control(icd);
1642                 if (!control || !control->driver || !dev_get_drvdata(control) ||
1643                     !try_module_get(control->driver->owner)) {
1644                         shd->del_device(icd);
1645                         ret = -ENODEV;
1646                         goto enodrv;
1647                 }
1648         }
1649
1650         mutex_lock(&ici->host_lock);
1651         ret = soc_camera_probe_finish(icd);
1652         mutex_unlock(&ici->host_lock);
1653         if (ret < 0)
1654                 goto efinish;
1655
1656         return 0;
1657
1658 efinish:
1659         if (shd->board_info) {
1660                 soc_camera_i2c_free(icd);
1661         } else {
1662                 shd->del_device(icd);
1663                 module_put(control->driver->owner);
1664 enodrv:
1665 eadddev:
1666                 mutex_lock(&ici->clk_lock);
1667                 ici->ops->clock_stop(ici);
1668                 mutex_unlock(&ici->clk_lock);
1669         }
1670 eadd:
1671         video_device_release(icd->vdev);
1672         icd->vdev = NULL;
1673         if (icd->vdev) {
1674                 video_device_release(icd->vdev);
1675                 icd->vdev = NULL;
1676         }
1677 evdc:
1678         v4l2_ctrl_handler_free(&icd->ctrl_handler);
1679         return ret;
1680 }
1681
1682 /*
1683  * This is called on device_unregister, which only means we have to disconnect
1684  * from the host, but not remove ourselves from the device list. With
1685  * asynchronous client probing this can also be called without
1686  * soc_camera_probe_finish() having run. Careful with clean up.
1687  */
1688 static int soc_camera_remove(struct soc_camera_device *icd)
1689 {
1690         struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
1691         struct video_device *vdev = icd->vdev;
1692
1693         v4l2_ctrl_handler_free(&icd->ctrl_handler);
1694         if (vdev) {
1695                 video_unregister_device(vdev);
1696                 icd->vdev = NULL;
1697         }
1698
1699         if (sdesc->host_desc.board_info) {
1700                 soc_camera_i2c_free(icd);
1701         } else {
1702                 struct device *dev = to_soc_camera_control(icd);
1703                 struct device_driver *drv = dev ? dev->driver : NULL;
1704                 if (drv) {
1705                         sdesc->host_desc.del_device(icd);
1706                         module_put(drv->owner);
1707                 }
1708         }
1709
1710         if (icd->num_user_formats)
1711                 soc_camera_free_user_formats(icd);
1712
1713         if (icd->clk) {
1714                 /* For the synchronous case */
1715                 v4l2_clk_unregister(icd->clk);
1716                 icd->clk = NULL;
1717         }
1718
1719         if (icd->sasc)
1720                 platform_device_unregister(icd->sasc->pdev);
1721
1722         return 0;
1723 }
1724
1725 static int default_cropcap(struct soc_camera_device *icd,
1726                            struct v4l2_cropcap *a)
1727 {
1728         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1729         return v4l2_subdev_call(sd, video, cropcap, a);
1730 }
1731
1732 static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1733 {
1734         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1735         return v4l2_subdev_call(sd, video, g_crop, a);
1736 }
1737
1738 static int default_s_crop(struct soc_camera_device *icd, const struct v4l2_crop *a)
1739 {
1740         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1741         return v4l2_subdev_call(sd, video, s_crop, a);
1742 }
1743
1744 static int default_g_parm(struct soc_camera_device *icd,
1745                           struct v4l2_streamparm *parm)
1746 {
1747         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1748         return v4l2_subdev_call(sd, video, g_parm, parm);
1749 }
1750
1751 static int default_s_parm(struct soc_camera_device *icd,
1752                           struct v4l2_streamparm *parm)
1753 {
1754         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1755         return v4l2_subdev_call(sd, video, s_parm, parm);
1756 }
1757
1758 static int default_enum_framesizes(struct soc_camera_device *icd,
1759                                    struct v4l2_frmsizeenum *fsize)
1760 {
1761         int ret;
1762         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1763         const struct soc_camera_format_xlate *xlate;
1764         __u32 pixfmt = fsize->pixel_format;
1765         struct v4l2_frmsizeenum fsize_mbus = *fsize;
1766
1767         xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1768         if (!xlate)
1769                 return -EINVAL;
1770         /* map xlate-code to pixel_format, sensor only handle xlate-code*/
1771         fsize_mbus.pixel_format = xlate->code;
1772
1773         ret = v4l2_subdev_call(sd, video, enum_framesizes, &fsize_mbus);
1774         if (ret < 0)
1775                 return ret;
1776
1777         *fsize = fsize_mbus;
1778         fsize->pixel_format = pixfmt;
1779
1780         return 0;
1781 }
1782
1783 int soc_camera_host_register(struct soc_camera_host *ici)
1784 {
1785         struct soc_camera_host *ix;
1786         int ret;
1787
1788         if (!ici || !ici->ops ||
1789             !ici->ops->try_fmt ||
1790             !ici->ops->set_fmt ||
1791             !ici->ops->set_bus_param ||
1792             !ici->ops->querycap ||
1793             ((!ici->ops->init_videobuf ||
1794               !ici->ops->reqbufs) &&
1795              !ici->ops->init_videobuf2) ||
1796             !ici->ops->clock_start ||
1797             !ici->ops->clock_stop ||
1798             !ici->ops->poll ||
1799             !ici->v4l2_dev.dev)
1800                 return -EINVAL;
1801
1802         if (!ici->ops->set_crop)
1803                 ici->ops->set_crop = default_s_crop;
1804         if (!ici->ops->get_crop)
1805                 ici->ops->get_crop = default_g_crop;
1806         if (!ici->ops->cropcap)
1807                 ici->ops->cropcap = default_cropcap;
1808         if (!ici->ops->set_parm)
1809                 ici->ops->set_parm = default_s_parm;
1810         if (!ici->ops->get_parm)
1811                 ici->ops->get_parm = default_g_parm;
1812         if (!ici->ops->enum_framesizes)
1813                 ici->ops->enum_framesizes = default_enum_framesizes;
1814
1815         mutex_lock(&list_lock);
1816         list_for_each_entry(ix, &hosts, list) {
1817                 if (ix->nr == ici->nr) {
1818                         ret = -EBUSY;
1819                         goto edevreg;
1820                 }
1821         }
1822
1823         ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
1824         if (ret < 0)
1825                 goto edevreg;
1826
1827         list_add_tail(&ici->list, &hosts);
1828         mutex_unlock(&list_lock);
1829
1830         mutex_init(&ici->host_lock);
1831         mutex_init(&ici->clk_lock);
1832
1833         if (ici->asd_sizes)
1834                 /*
1835                  * No OF, host with a list of subdevices. Don't try to mix
1836                  * modes by initialising some groups statically and some
1837                  * dynamically!
1838                  */
1839                 scan_async_host(ici);
1840         else
1841                 /* Legacy: static platform devices from board data */
1842                 scan_add_host(ici);
1843
1844         return 0;
1845
1846 edevreg:
1847         mutex_unlock(&list_lock);
1848         return ret;
1849 }
1850 EXPORT_SYMBOL(soc_camera_host_register);
1851
1852 /* Unregister all clients! */
1853 void soc_camera_host_unregister(struct soc_camera_host *ici)
1854 {
1855         struct soc_camera_device *icd, *tmp;
1856         struct soc_camera_async_client *sasc;
1857         LIST_HEAD(notifiers);
1858
1859         mutex_lock(&list_lock);
1860         list_del(&ici->list);
1861         list_for_each_entry(icd, &devices, list)
1862                 if (icd->iface == ici->nr && icd->sasc) {
1863                         /* as long as we hold the device, sasc won't be freed */
1864                         get_device(icd->pdev);
1865                         list_add(&icd->sasc->list, &notifiers);
1866                 }
1867         mutex_unlock(&list_lock);
1868
1869         list_for_each_entry(sasc, &notifiers, list) {
1870                 /* Must call unlocked to avoid AB-BA dead-lock */
1871                 v4l2_async_notifier_unregister(&sasc->notifier);
1872                 put_device(&sasc->pdev->dev);
1873         }
1874
1875         mutex_lock(&list_lock);
1876
1877         list_for_each_entry_safe(icd, tmp, &devices, list)
1878                 if (icd->iface == ici->nr)
1879                         soc_camera_remove(icd);
1880
1881         mutex_unlock(&list_lock);
1882
1883         v4l2_device_unregister(&ici->v4l2_dev);
1884 }
1885 EXPORT_SYMBOL(soc_camera_host_unregister);
1886
1887 /* Image capture device */
1888 static int soc_camera_device_register(struct soc_camera_device *icd)
1889 {
1890         struct soc_camera_device *ix;
1891         int num = -1, i;
1892
1893         mutex_lock(&list_lock);
1894         for (i = 0; i < 256 && num < 0; i++) {
1895                 num = i;
1896                 /* Check if this index is available on this interface */
1897                 list_for_each_entry(ix, &devices, list) {
1898                         if (ix->iface == icd->iface && ix->devnum == i) {
1899                                 num = -1;
1900                                 break;
1901                         }
1902                 }
1903         }
1904
1905         if (num < 0) {
1906                 /*
1907                  * ok, we have 256 cameras on this host...
1908                  * man, stay reasonable...
1909                  */
1910                 mutex_unlock(&list_lock);
1911                 return -ENOMEM;
1912         }
1913
1914         icd->devnum             = num;
1915         icd->use_count          = 0;
1916         icd->host_priv          = NULL;
1917
1918         /*
1919          * Dynamically allocated devices set the bit earlier, but it doesn't hurt setting
1920          * it again
1921          */
1922         i = to_platform_device(icd->pdev)->id;
1923         if (i < 0)
1924                 /* One static (legacy) soc-camera platform device */
1925                 i = 0;
1926         if (i >= MAP_MAX_NUM) {
1927                 mutex_unlock(&list_lock);
1928                 return -EBUSY;
1929         }
1930         set_bit(i, device_map);
1931         list_add_tail(&icd->list, &devices);
1932         mutex_unlock(&list_lock);
1933
1934         return 0;
1935 }
1936
1937 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
1938         .vidioc_querycap         = soc_camera_querycap,
1939         .vidioc_try_fmt_vid_cap  = soc_camera_try_fmt_vid_cap,
1940         .vidioc_g_fmt_vid_cap    = soc_camera_g_fmt_vid_cap,
1941         .vidioc_s_fmt_vid_cap    = soc_camera_s_fmt_vid_cap,
1942         .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
1943         .vidioc_enum_input       = soc_camera_enum_input,
1944         .vidioc_g_input          = soc_camera_g_input,
1945         .vidioc_s_input          = soc_camera_s_input,
1946         .vidioc_s_std            = soc_camera_s_std,
1947         .vidioc_g_std            = soc_camera_g_std,
1948         .vidioc_enum_framesizes  = soc_camera_enum_framesizes,
1949         .vidioc_reqbufs          = soc_camera_reqbufs,
1950         .vidioc_querybuf         = soc_camera_querybuf,
1951         .vidioc_qbuf             = soc_camera_qbuf,
1952         .vidioc_dqbuf            = soc_camera_dqbuf,
1953         .vidioc_create_bufs      = soc_camera_create_bufs,
1954         .vidioc_prepare_buf      = soc_camera_prepare_buf,
1955         .vidioc_streamon         = soc_camera_streamon,
1956         .vidioc_streamoff        = soc_camera_streamoff,
1957         .vidioc_cropcap          = soc_camera_cropcap,
1958         .vidioc_g_crop           = soc_camera_g_crop,
1959         .vidioc_s_crop           = soc_camera_s_crop,
1960         .vidioc_g_selection      = soc_camera_g_selection,
1961         .vidioc_s_selection      = soc_camera_s_selection,
1962         .vidioc_g_parm           = soc_camera_g_parm,
1963         .vidioc_s_parm           = soc_camera_s_parm,
1964 };
1965
1966 static int video_dev_create(struct soc_camera_device *icd)
1967 {
1968         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1969         struct video_device *vdev = video_device_alloc();
1970
1971         if (!vdev)
1972                 return -ENOMEM;
1973
1974         strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
1975
1976         vdev->v4l2_dev          = &ici->v4l2_dev;
1977         vdev->fops              = &soc_camera_fops;
1978         vdev->ioctl_ops         = &soc_camera_ioctl_ops;
1979         vdev->release           = video_device_release;
1980         vdev->ctrl_handler      = &icd->ctrl_handler;
1981         vdev->lock              = &ici->host_lock;
1982
1983         icd->vdev = vdev;
1984
1985         return 0;
1986 }
1987
1988 /*
1989  * Called from soc_camera_probe() above with .host_lock held
1990  */
1991 static int soc_camera_video_start(struct soc_camera_device *icd)
1992 {
1993         const struct device_type *type = icd->vdev->dev.type;
1994         int ret;
1995
1996         if (!icd->parent)
1997                 return -ENODEV;
1998
1999         video_set_drvdata(icd->vdev, icd);
2000         ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
2001         if (ret < 0) {
2002                 dev_err(icd->pdev, "video_register_device failed: %d\n", ret);
2003                 return ret;
2004         }
2005
2006         /* Restore device type, possibly set by the subdevice driver */
2007         icd->vdev->dev.type = type;
2008
2009         return 0;
2010 }
2011
2012 static int soc_camera_pdrv_probe(struct platform_device *pdev)
2013 {
2014         struct soc_camera_desc *sdesc = pdev->dev.platform_data;
2015         struct soc_camera_subdev_desc *ssdd = &sdesc->subdev_desc;
2016         struct soc_camera_device *icd;
2017         int ret;
2018
2019         if (!sdesc)
2020                 return -EINVAL;
2021
2022         icd = devm_kzalloc(&pdev->dev, sizeof(*icd), GFP_KERNEL);
2023         if (!icd)
2024                 return -ENOMEM;
2025
2026         /*
2027          * In the asynchronous case ssdd->num_regulators == 0 yet, so, the below
2028          * regulator allocation is a dummy. They are actually requested by the
2029          * subdevice driver, using soc_camera_power_init(). Also note, that in
2030          * that case regulators are attached to the I2C device and not to the
2031          * camera platform device.
2032          */
2033         ret = devm_regulator_bulk_get(&pdev->dev, ssdd->sd_pdata.num_regulators,
2034                                       ssdd->sd_pdata.regulators);
2035         if (ret < 0)
2036                 return ret;
2037
2038         icd->iface = sdesc->host_desc.bus_id;
2039         icd->sdesc = sdesc;
2040         icd->pdev = &pdev->dev;
2041         platform_set_drvdata(pdev, icd);
2042
2043         icd->user_width         = DEFAULT_WIDTH;
2044         icd->user_height        = DEFAULT_HEIGHT;
2045
2046         return soc_camera_device_register(icd);
2047 }
2048
2049 /*
2050  * Only called on rmmod for each platform device, since they are not
2051  * hot-pluggable. Now we know, that all our users - hosts and devices have
2052  * been unloaded already
2053  */
2054 static int soc_camera_pdrv_remove(struct platform_device *pdev)
2055 {
2056         struct soc_camera_device *icd = platform_get_drvdata(pdev);
2057         int i;
2058
2059         if (!icd)
2060                 return -EINVAL;
2061
2062         i = pdev->id;
2063         if (i < 0)
2064                 i = 0;
2065
2066         /*
2067          * In synchronous mode with static platform devices this is called in a
2068          * loop from drivers/base/dd.c::driver_detach(), no parallel execution,
2069          * no need to lock. In asynchronous case the caller -
2070          * soc_camera_host_unregister() - already holds the lock
2071          */
2072         if (test_bit(i, device_map)) {
2073                 clear_bit(i, device_map);
2074                 list_del(&icd->list);
2075         }
2076
2077         return 0;
2078 }
2079
2080 static struct platform_driver __refdata soc_camera_pdrv = {
2081         .probe = soc_camera_pdrv_probe,
2082         .remove  = soc_camera_pdrv_remove,
2083         .driver  = {
2084                 .name   = "soc-camera-pdrv",
2085                 .owner  = THIS_MODULE,
2086         },
2087 };
2088
2089 module_platform_driver(soc_camera_pdrv);
2090
2091 MODULE_DESCRIPTION("Image capture bus driver");
2092 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
2093 MODULE_LICENSE("GPL");
2094 MODULE_ALIAS("platform:soc-camera-pdrv");