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