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