media: atomisp: Remove unnecessary NULL checks in ia_css_pipe_load_extension
[linux-2.6-microblaze.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         f->pixelformat = format->fourcc;
873         return 0;
874 }
875
876 static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
877                                     struct v4l2_format *f)
878 {
879         struct soc_camera_device *icd = file->private_data;
880         struct v4l2_pix_format *pix = &f->fmt.pix;
881
882         WARN_ON(priv != file->private_data);
883
884         if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
885                 return -EINVAL;
886
887         pix->width              = icd->user_width;
888         pix->height             = icd->user_height;
889         pix->bytesperline       = icd->bytesperline;
890         pix->sizeimage          = icd->sizeimage;
891         pix->field              = icd->field;
892         pix->pixelformat        = icd->current_fmt->host_fmt->fourcc;
893         pix->colorspace         = icd->colorspace;
894         dev_dbg(icd->pdev, "current_fmt->fourcc: 0x%08x\n",
895                 icd->current_fmt->host_fmt->fourcc);
896         return 0;
897 }
898
899 static int soc_camera_querycap(struct file *file, void  *priv,
900                                struct v4l2_capability *cap)
901 {
902         struct soc_camera_device *icd = file->private_data;
903         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
904
905         WARN_ON(priv != file->private_data);
906
907         strscpy(cap->driver, ici->drv_name, sizeof(cap->driver));
908         return ici->ops->querycap(ici, cap);
909 }
910
911 static int soc_camera_streamon(struct file *file, void *priv,
912                                enum v4l2_buf_type i)
913 {
914         struct soc_camera_device *icd = file->private_data;
915         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
916         int ret;
917
918         WARN_ON(priv != file->private_data);
919
920         if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
921                 return -EINVAL;
922
923         if (icd->streamer != file)
924                 return -EBUSY;
925
926         /* This calls buf_queue from host driver's videobuf2_queue_ops */
927         ret = vb2_streamon(&icd->vb2_vidq, i);
928         if (!ret)
929                 v4l2_subdev_call(sd, video, s_stream, 1);
930
931         return ret;
932 }
933
934 static int soc_camera_streamoff(struct file *file, void *priv,
935                                 enum v4l2_buf_type i)
936 {
937         struct soc_camera_device *icd = file->private_data;
938         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
939         int ret;
940
941         WARN_ON(priv != file->private_data);
942
943         if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
944                 return -EINVAL;
945
946         if (icd->streamer != file)
947                 return -EBUSY;
948
949         /*
950          * This calls buf_release from host driver's videobuf2_queue_ops for all
951          * remaining buffers. When the last buffer is freed, stop capture
952          */
953         ret = vb2_streamoff(&icd->vb2_vidq, i);
954
955         v4l2_subdev_call(sd, video, s_stream, 0);
956
957         return ret;
958 }
959
960 static int soc_camera_g_selection(struct file *file, void *fh,
961                                   struct v4l2_selection *s)
962 {
963         struct soc_camera_device *icd = file->private_data;
964         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
965
966         /* With a wrong type no need to try to fall back to cropping */
967         if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
968                 return -EINVAL;
969
970         return ici->ops->get_selection(icd, s);
971 }
972
973 static int soc_camera_s_selection(struct file *file, void *fh,
974                                   struct v4l2_selection *s)
975 {
976         struct soc_camera_device *icd = file->private_data;
977         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
978         int ret;
979
980         /* In all these cases cropping emulation will not help */
981         if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
982             (s->target != V4L2_SEL_TGT_COMPOSE &&
983              s->target != V4L2_SEL_TGT_CROP))
984                 return -EINVAL;
985
986         if (s->target == V4L2_SEL_TGT_COMPOSE) {
987                 /* No output size change during a running capture! */
988                 if (vb2_is_streaming(&icd->vb2_vidq) &&
989                     (icd->user_width != s->r.width ||
990                      icd->user_height != s->r.height))
991                         return -EBUSY;
992
993                 /*
994                  * Only one user is allowed to change the output format, touch
995                  * buffers, start / stop streaming, poll for data
996                  */
997                 if (icd->streamer && icd->streamer != file)
998                         return -EBUSY;
999         }
1000
1001         if (s->target == V4L2_SEL_TGT_CROP &&
1002             vb2_is_streaming(&icd->vb2_vidq) &&
1003             ici->ops->set_liveselection)
1004                 ret = ici->ops->set_liveselection(icd, s);
1005         else
1006                 ret = ici->ops->set_selection(icd, s);
1007         if (!ret &&
1008             s->target == V4L2_SEL_TGT_COMPOSE) {
1009                 icd->user_width = s->r.width;
1010                 icd->user_height = s->r.height;
1011                 if (!icd->streamer)
1012                         icd->streamer = file;
1013         }
1014
1015         return ret;
1016 }
1017
1018 static int soc_camera_g_parm(struct file *file, void *fh,
1019                              struct v4l2_streamparm *a)
1020 {
1021         struct soc_camera_device *icd = file->private_data;
1022         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1023
1024         if (ici->ops->get_parm)
1025                 return ici->ops->get_parm(icd, a);
1026
1027         return -ENOIOCTLCMD;
1028 }
1029
1030 static int soc_camera_s_parm(struct file *file, void *fh,
1031                              struct v4l2_streamparm *a)
1032 {
1033         struct soc_camera_device *icd = file->private_data;
1034         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1035
1036         if (ici->ops->set_parm)
1037                 return ici->ops->set_parm(icd, a);
1038
1039         return -ENOIOCTLCMD;
1040 }
1041
1042 static int soc_camera_probe(struct soc_camera_host *ici,
1043                             struct soc_camera_device *icd);
1044
1045 /* So far this function cannot fail */
1046 static void scan_add_host(struct soc_camera_host *ici)
1047 {
1048         struct soc_camera_device *icd;
1049
1050         mutex_lock(&list_lock);
1051
1052         list_for_each_entry(icd, &devices, list)
1053                 if (icd->iface == ici->nr) {
1054                         struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
1055                         struct soc_camera_subdev_desc *ssdd = &sdesc->subdev_desc;
1056
1057                         /* The camera could have been already on, try to reset */
1058                         if (ssdd->reset)
1059                                 if (icd->control)
1060                                         ssdd->reset(icd->control);
1061
1062                         icd->parent = ici->v4l2_dev.dev;
1063
1064                         /* Ignore errors */
1065                         soc_camera_probe(ici, icd);
1066                 }
1067
1068         mutex_unlock(&list_lock);
1069 }
1070
1071 /*
1072  * It is invalid to call v4l2_clk_enable() after a successful probing
1073  * asynchronously outside of V4L2 operations, i.e. with .host_lock not held.
1074  */
1075 static int soc_camera_clk_enable(struct v4l2_clk *clk)
1076 {
1077         struct soc_camera_device *icd = clk->priv;
1078         struct soc_camera_host *ici;
1079
1080         if (!icd || !icd->parent)
1081                 return -ENODEV;
1082
1083         ici = to_soc_camera_host(icd->parent);
1084
1085         if (!try_module_get(ici->ops->owner))
1086                 return -ENODEV;
1087
1088         /*
1089          * If a different client is currently being probed, the host will tell
1090          * you to go
1091          */
1092         return soc_camera_clock_start(ici);
1093 }
1094
1095 static void soc_camera_clk_disable(struct v4l2_clk *clk)
1096 {
1097         struct soc_camera_device *icd = clk->priv;
1098         struct soc_camera_host *ici;
1099
1100         if (!icd || !icd->parent)
1101                 return;
1102
1103         ici = to_soc_camera_host(icd->parent);
1104
1105         soc_camera_clock_stop(ici);
1106
1107         module_put(ici->ops->owner);
1108 }
1109
1110 /*
1111  * Eventually, it would be more logical to make the respective host the clock
1112  * owner, but then we would have to copy this struct for each ici. Besides, it
1113  * would introduce the circular dependency problem, unless we port all client
1114  * drivers to release the clock, when not in use.
1115  */
1116 static const struct v4l2_clk_ops soc_camera_clk_ops = {
1117         .owner = THIS_MODULE,
1118         .enable = soc_camera_clk_enable,
1119         .disable = soc_camera_clk_disable,
1120 };
1121
1122 static int soc_camera_dyn_pdev(struct soc_camera_desc *sdesc,
1123                                struct soc_camera_async_client *sasc)
1124 {
1125         struct platform_device *pdev;
1126         int ret, i;
1127
1128         mutex_lock(&list_lock);
1129         i = find_first_zero_bit(device_map, MAP_MAX_NUM);
1130         if (i < MAP_MAX_NUM)
1131                 set_bit(i, device_map);
1132         mutex_unlock(&list_lock);
1133         if (i >= MAP_MAX_NUM)
1134                 return -ENOMEM;
1135
1136         pdev = platform_device_alloc("soc-camera-pdrv", i);
1137         if (!pdev)
1138                 return -ENOMEM;
1139
1140         ret = platform_device_add_data(pdev, sdesc, sizeof(*sdesc));
1141         if (ret < 0) {
1142                 platform_device_put(pdev);
1143                 return ret;
1144         }
1145
1146         sasc->pdev = pdev;
1147
1148         return 0;
1149 }
1150
1151 static struct soc_camera_device *soc_camera_add_pdev(struct soc_camera_async_client *sasc)
1152 {
1153         struct platform_device *pdev = sasc->pdev;
1154         int ret;
1155
1156         ret = platform_device_add(pdev);
1157         if (ret < 0 || !pdev->dev.driver)
1158                 return NULL;
1159
1160         return platform_get_drvdata(pdev);
1161 }
1162
1163 /* Locking: called with .host_lock held */
1164 static int soc_camera_probe_finish(struct soc_camera_device *icd)
1165 {
1166         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1167         struct v4l2_subdev_format fmt = {
1168                 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1169         };
1170         struct v4l2_mbus_framefmt *mf = &fmt.format;
1171         int ret;
1172
1173         sd->grp_id = soc_camera_grp_id(icd);
1174         v4l2_set_subdev_hostdata(sd, icd);
1175
1176         v4l2_subdev_call(sd, video, g_tvnorms, &icd->vdev->tvnorms);
1177
1178         ret = v4l2_ctrl_add_handler(&icd->ctrl_handler, sd->ctrl_handler,
1179                                     NULL, true);
1180         if (ret < 0)
1181                 return ret;
1182
1183         ret = soc_camera_add_device(icd);
1184         if (ret < 0) {
1185                 dev_err(icd->pdev, "Couldn't activate the camera: %d\n", ret);
1186                 return ret;
1187         }
1188
1189         /* At this point client .probe() should have run already */
1190         ret = soc_camera_init_user_formats(icd);
1191         if (ret < 0)
1192                 goto eusrfmt;
1193
1194         icd->field = V4L2_FIELD_ANY;
1195
1196         ret = soc_camera_video_start(icd);
1197         if (ret < 0)
1198                 goto evidstart;
1199
1200         /* Try to improve our guess of a reasonable window format */
1201         if (!v4l2_subdev_call(sd, pad, get_fmt, NULL, &fmt)) {
1202                 icd->user_width         = mf->width;
1203                 icd->user_height        = mf->height;
1204                 icd->colorspace         = mf->colorspace;
1205                 icd->field              = mf->field;
1206         }
1207         soc_camera_remove_device(icd);
1208
1209         return 0;
1210
1211 evidstart:
1212         soc_camera_free_user_formats(icd);
1213 eusrfmt:
1214         soc_camera_remove_device(icd);
1215
1216         return ret;
1217 }
1218
1219 #ifdef CONFIG_I2C_BOARDINFO
1220 static int soc_camera_i2c_init(struct soc_camera_device *icd,
1221                                struct soc_camera_desc *sdesc)
1222 {
1223         struct soc_camera_subdev_desc *ssdd;
1224         struct i2c_client *client;
1225         struct soc_camera_host *ici;
1226         struct soc_camera_host_desc *shd = &sdesc->host_desc;
1227         struct i2c_adapter *adap;
1228         struct v4l2_subdev *subdev;
1229         char clk_name[V4L2_CLK_NAME_SIZE];
1230         int ret;
1231
1232         /* First find out how we link the main client */
1233         if (icd->sasc) {
1234                 /* Async non-OF probing handled by the subdevice list */
1235                 return -EPROBE_DEFER;
1236         }
1237
1238         ici = to_soc_camera_host(icd->parent);
1239         adap = i2c_get_adapter(shd->i2c_adapter_id);
1240         if (!adap) {
1241                 dev_err(icd->pdev, "Cannot get I2C adapter #%d. No driver?\n",
1242                         shd->i2c_adapter_id);
1243                 return -ENODEV;
1244         }
1245
1246         ssdd = kmemdup(&sdesc->subdev_desc, sizeof(*ssdd), GFP_KERNEL);
1247         if (!ssdd) {
1248                 ret = -ENOMEM;
1249                 goto ealloc;
1250         }
1251         /*
1252          * In synchronous case we request regulators ourselves in
1253          * soc_camera_pdrv_probe(), make sure the subdevice driver doesn't try
1254          * to allocate them again.
1255          */
1256         ssdd->sd_pdata.num_regulators = 0;
1257         ssdd->sd_pdata.regulators = NULL;
1258         shd->board_info->platform_data = ssdd;
1259
1260         v4l2_clk_name_i2c(clk_name, sizeof(clk_name),
1261                           shd->i2c_adapter_id, shd->board_info->addr);
1262
1263         icd->clk = v4l2_clk_register(&soc_camera_clk_ops, clk_name, icd);
1264         if (IS_ERR(icd->clk)) {
1265                 ret = PTR_ERR(icd->clk);
1266                 goto eclkreg;
1267         }
1268
1269         subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
1270                                 shd->board_info, NULL);
1271         if (!subdev) {
1272                 ret = -ENODEV;
1273                 goto ei2cnd;
1274         }
1275
1276         client = v4l2_get_subdevdata(subdev);
1277
1278         /* Use to_i2c_client(dev) to recover the i2c client */
1279         icd->control = &client->dev;
1280
1281         return 0;
1282 ei2cnd:
1283         v4l2_clk_unregister(icd->clk);
1284         icd->clk = NULL;
1285 eclkreg:
1286         kfree(ssdd);
1287 ealloc:
1288         i2c_put_adapter(adap);
1289         return ret;
1290 }
1291
1292 static void soc_camera_i2c_free(struct soc_camera_device *icd)
1293 {
1294         struct i2c_client *client =
1295                 to_i2c_client(to_soc_camera_control(icd));
1296         struct i2c_adapter *adap;
1297         struct soc_camera_subdev_desc *ssdd;
1298
1299         icd->control = NULL;
1300         if (icd->sasc)
1301                 return;
1302
1303         adap = client->adapter;
1304         ssdd = client->dev.platform_data;
1305         v4l2_device_unregister_subdev(i2c_get_clientdata(client));
1306         i2c_unregister_device(client);
1307         i2c_put_adapter(adap);
1308         kfree(ssdd);
1309         v4l2_clk_unregister(icd->clk);
1310         icd->clk = NULL;
1311 }
1312
1313 /*
1314  * V4L2 asynchronous notifier callbacks. They are all called under a v4l2-async
1315  * internal global mutex, therefore cannot race against other asynchronous
1316  * events. Until notifier->complete() (soc_camera_async_complete()) is called,
1317  * the video device node is not registered and no V4L fops can occur. Unloading
1318  * of the host driver also calls a v4l2-async function, so also there we're
1319  * protected.
1320  */
1321 static int soc_camera_async_bound(struct v4l2_async_notifier *notifier,
1322                                   struct v4l2_subdev *sd,
1323                                   struct v4l2_async_subdev *asd)
1324 {
1325         struct soc_camera_async_client *sasc = container_of(notifier,
1326                                         struct soc_camera_async_client, notifier);
1327         struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev);
1328
1329         if (asd == sasc->sensor && !WARN_ON(icd->control)) {
1330                 struct i2c_client *client = v4l2_get_subdevdata(sd);
1331
1332                 /*
1333                  * Only now we get subdevice-specific information like
1334                  * regulators, flags, callbacks, etc.
1335                  */
1336                 if (client) {
1337                         struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
1338                         struct soc_camera_subdev_desc *ssdd =
1339                                 soc_camera_i2c_to_desc(client);
1340                         if (ssdd) {
1341                                 memcpy(&sdesc->subdev_desc, ssdd,
1342                                        sizeof(sdesc->subdev_desc));
1343                                 if (ssdd->reset)
1344                                         ssdd->reset(&client->dev);
1345                         }
1346
1347                         icd->control = &client->dev;
1348                 }
1349         }
1350
1351         return 0;
1352 }
1353
1354 static void soc_camera_async_unbind(struct v4l2_async_notifier *notifier,
1355                                     struct v4l2_subdev *sd,
1356                                     struct v4l2_async_subdev *asd)
1357 {
1358         struct soc_camera_async_client *sasc = container_of(notifier,
1359                                         struct soc_camera_async_client, notifier);
1360         struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev);
1361
1362         icd->control = NULL;
1363
1364         if (icd->clk) {
1365                 v4l2_clk_unregister(icd->clk);
1366                 icd->clk = NULL;
1367         }
1368 }
1369
1370 static int soc_camera_async_complete(struct v4l2_async_notifier *notifier)
1371 {
1372         struct soc_camera_async_client *sasc = container_of(notifier,
1373                                         struct soc_camera_async_client, notifier);
1374         struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev);
1375
1376         if (to_soc_camera_control(icd)) {
1377                 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1378                 int ret;
1379
1380                 mutex_lock(&list_lock);
1381                 ret = soc_camera_probe(ici, icd);
1382                 mutex_unlock(&list_lock);
1383                 if (ret < 0)
1384                         return ret;
1385         }
1386
1387         return 0;
1388 }
1389
1390 static const struct v4l2_async_notifier_operations soc_camera_async_ops = {
1391         .bound = soc_camera_async_bound,
1392         .unbind = soc_camera_async_unbind,
1393         .complete = soc_camera_async_complete,
1394 };
1395
1396 static int scan_async_group(struct soc_camera_host *ici,
1397                             struct v4l2_async_subdev **asd, unsigned int size)
1398 {
1399         struct soc_camera_async_subdev *sasd;
1400         struct soc_camera_async_client *sasc;
1401         struct soc_camera_device *icd;
1402         struct soc_camera_desc sdesc = {.host_desc.bus_id = ici->nr,};
1403         char clk_name[V4L2_CLK_NAME_SIZE];
1404         unsigned int i;
1405         int ret;
1406
1407         /* First look for a sensor */
1408         for (i = 0; i < size; i++) {
1409                 sasd = container_of(asd[i], struct soc_camera_async_subdev, asd);
1410                 if (sasd->role == SOCAM_SUBDEV_DATA_SOURCE)
1411                         break;
1412         }
1413
1414         if (i >= size || asd[i]->match_type != V4L2_ASYNC_MATCH_I2C) {
1415                 /* All useless */
1416                 dev_err(ici->v4l2_dev.dev, "No I2C data source found!\n");
1417                 return -ENODEV;
1418         }
1419
1420         /* Or shall this be managed by the soc-camera device? */
1421         sasc = devm_kzalloc(ici->v4l2_dev.dev, sizeof(*sasc), GFP_KERNEL);
1422         if (!sasc)
1423                 return -ENOMEM;
1424
1425         /* HACK: just need a != NULL */
1426         sdesc.host_desc.board_info = ERR_PTR(-ENODATA);
1427
1428         ret = soc_camera_dyn_pdev(&sdesc, sasc);
1429         if (ret < 0)
1430                 goto eallocpdev;
1431
1432         sasc->sensor = &sasd->asd;
1433
1434         icd = soc_camera_add_pdev(sasc);
1435         if (!icd) {
1436                 ret = -ENOMEM;
1437                 goto eaddpdev;
1438         }
1439
1440         v4l2_async_notifier_init(&sasc->notifier);
1441
1442         for (i = 0; i < size; i++) {
1443                 ret = v4l2_async_notifier_add_subdev(&sasc->notifier, asd[i]);
1444                 if (ret)
1445                         goto eaddasd;
1446         }
1447
1448         sasc->notifier.ops = &soc_camera_async_ops;
1449
1450         icd->sasc = sasc;
1451         icd->parent = ici->v4l2_dev.dev;
1452
1453         v4l2_clk_name_i2c(clk_name, sizeof(clk_name),
1454                           sasd->asd.match.i2c.adapter_id,
1455                           sasd->asd.match.i2c.address);
1456
1457         icd->clk = v4l2_clk_register(&soc_camera_clk_ops, clk_name, icd);
1458         if (IS_ERR(icd->clk)) {
1459                 ret = PTR_ERR(icd->clk);
1460                 goto eclkreg;
1461         }
1462
1463         ret = v4l2_async_notifier_register(&ici->v4l2_dev, &sasc->notifier);
1464         if (!ret)
1465                 return 0;
1466
1467         v4l2_clk_unregister(icd->clk);
1468 eclkreg:
1469         icd->clk = NULL;
1470 eaddasd:
1471         v4l2_async_notifier_cleanup(&sasc->notifier);
1472         platform_device_del(sasc->pdev);
1473 eaddpdev:
1474         platform_device_put(sasc->pdev);
1475 eallocpdev:
1476         devm_kfree(ici->v4l2_dev.dev, sasc);
1477         dev_err(ici->v4l2_dev.dev, "group probe failed: %d\n", ret);
1478
1479         return ret;
1480 }
1481
1482 static void scan_async_host(struct soc_camera_host *ici)
1483 {
1484         struct v4l2_async_subdev **asd;
1485         int j;
1486
1487         for (j = 0, asd = ici->asd; ici->asd_sizes[j]; j++) {
1488                 scan_async_group(ici, asd, ici->asd_sizes[j]);
1489                 asd += ici->asd_sizes[j];
1490         }
1491 }
1492 #else
1493 #define soc_camera_i2c_init(icd, sdesc) (-ENODEV)
1494 #define soc_camera_i2c_free(icd)        do {} while (0)
1495 #define scan_async_host(ici)            do {} while (0)
1496 #endif
1497
1498 #ifdef CONFIG_OF
1499
1500 struct soc_of_info {
1501         struct soc_camera_async_subdev  sasd;
1502         struct soc_camera_async_client  sasc;
1503         struct v4l2_async_subdev        *subdev;
1504 };
1505
1506 static int soc_of_bind(struct soc_camera_host *ici,
1507                        struct device_node *ep,
1508                        struct device_node *remote)
1509 {
1510         struct soc_camera_device *icd;
1511         struct soc_camera_desc sdesc = {.host_desc.bus_id = ici->nr,};
1512         struct soc_camera_async_client *sasc;
1513         struct soc_of_info *info;
1514         struct i2c_client *client;
1515         char clk_name[V4L2_CLK_NAME_SIZE];
1516         int ret;
1517
1518         /* allocate a new subdev and add match info to it */
1519         info = devm_kzalloc(ici->v4l2_dev.dev, sizeof(struct soc_of_info),
1520                             GFP_KERNEL);
1521         if (!info)
1522                 return -ENOMEM;
1523
1524         info->sasd.asd.match.fwnode = of_fwnode_handle(remote);
1525         info->sasd.asd.match_type = V4L2_ASYNC_MATCH_FWNODE;
1526         info->subdev = &info->sasd.asd;
1527
1528         /* Or shall this be managed by the soc-camera device? */
1529         sasc = &info->sasc;
1530
1531         /* HACK: just need a != NULL */
1532         sdesc.host_desc.board_info = ERR_PTR(-ENODATA);
1533
1534         ret = soc_camera_dyn_pdev(&sdesc, sasc);
1535         if (ret < 0)
1536                 goto eallocpdev;
1537
1538         sasc->sensor = &info->sasd.asd;
1539
1540         icd = soc_camera_add_pdev(sasc);
1541         if (!icd) {
1542                 ret = -ENOMEM;
1543                 goto eaddpdev;
1544         }
1545
1546         v4l2_async_notifier_init(&sasc->notifier);
1547
1548         ret = v4l2_async_notifier_add_subdev(&sasc->notifier, info->subdev);
1549         if (ret) {
1550                 of_node_put(remote);
1551                 goto eaddasd;
1552         }
1553
1554         sasc->notifier.ops = &soc_camera_async_ops;
1555
1556         icd->sasc = sasc;
1557         icd->parent = ici->v4l2_dev.dev;
1558
1559         client = of_find_i2c_device_by_node(remote);
1560
1561         if (client)
1562                 v4l2_clk_name_i2c(clk_name, sizeof(clk_name),
1563                                   client->adapter->nr, client->addr);
1564         else
1565                 v4l2_clk_name_of(clk_name, sizeof(clk_name), remote);
1566
1567         icd->clk = v4l2_clk_register(&soc_camera_clk_ops, clk_name, icd);
1568         if (IS_ERR(icd->clk)) {
1569                 ret = PTR_ERR(icd->clk);
1570                 goto eclkreg;
1571         }
1572
1573         ret = v4l2_async_notifier_register(&ici->v4l2_dev, &sasc->notifier);
1574         if (!ret)
1575                 return 0;
1576
1577         v4l2_clk_unregister(icd->clk);
1578 eclkreg:
1579         icd->clk = NULL;
1580 eaddasd:
1581         v4l2_async_notifier_cleanup(&sasc->notifier);
1582         platform_device_del(sasc->pdev);
1583 eaddpdev:
1584         platform_device_put(sasc->pdev);
1585 eallocpdev:
1586         devm_kfree(ici->v4l2_dev.dev, info);
1587         dev_err(ici->v4l2_dev.dev, "group probe failed: %d\n", ret);
1588
1589         return ret;
1590 }
1591
1592 static void scan_of_host(struct soc_camera_host *ici)
1593 {
1594         struct device *dev = ici->v4l2_dev.dev;
1595         struct device_node *np = dev->of_node;
1596         struct device_node *epn = NULL, *rem;
1597         unsigned int i;
1598
1599         for (i = 0; ; i++) {
1600                 epn = of_graph_get_next_endpoint(np, epn);
1601                 if (!epn)
1602                         break;
1603
1604                 rem = of_graph_get_remote_port_parent(epn);
1605                 if (!rem) {
1606                         dev_notice(dev, "no remote for %pOF\n", epn);
1607                         continue;
1608                 }
1609
1610                 /* so we now have a remote node to connect */
1611                 if (!i)
1612                         soc_of_bind(ici, epn, rem);
1613
1614                 if (i) {
1615                         dev_err(dev, "multiple subdevices aren't supported yet!\n");
1616                         break;
1617                 }
1618         }
1619
1620         of_node_put(epn);
1621 }
1622
1623 #else
1624 static inline void scan_of_host(struct soc_camera_host *ici) { }
1625 #endif
1626
1627 /* Called during host-driver probe */
1628 static int soc_camera_probe(struct soc_camera_host *ici,
1629                             struct soc_camera_device *icd)
1630 {
1631         struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
1632         struct soc_camera_host_desc *shd = &sdesc->host_desc;
1633         struct device *control = NULL;
1634         int ret;
1635
1636         dev_info(icd->pdev, "Probing %s\n", dev_name(icd->pdev));
1637
1638         /*
1639          * Currently the subdev with the largest number of controls (13) is
1640          * ov6550. So let's pick 16 as a hint for the control handler. Note
1641          * that this is a hint only: too large and you waste some memory, too
1642          * small and there is a (very) small performance hit when looking up
1643          * controls in the internal hash.
1644          */
1645         ret = v4l2_ctrl_handler_init(&icd->ctrl_handler, 16);
1646         if (ret < 0)
1647                 return ret;
1648
1649         /* Must have icd->vdev before registering the device */
1650         ret = video_dev_create(icd);
1651         if (ret < 0)
1652                 goto evdc;
1653
1654         /*
1655          * ..._video_start() will create a device node, video_register_device()
1656          * itself is protected against concurrent open() calls, but we also have
1657          * to protect our data also during client probing.
1658          */
1659
1660         /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
1661         if (shd->board_info) {
1662                 ret = soc_camera_i2c_init(icd, sdesc);
1663                 if (ret < 0 && ret != -EPROBE_DEFER)
1664                         goto eadd;
1665         } else if (!shd->add_device || !shd->del_device) {
1666                 ret = -EINVAL;
1667                 goto eadd;
1668         } else {
1669                 ret = soc_camera_clock_start(ici);
1670                 if (ret < 0)
1671                         goto eadd;
1672
1673                 if (shd->module_name)
1674                         ret = request_module(shd->module_name);
1675
1676                 ret = shd->add_device(icd);
1677                 if (ret < 0)
1678                         goto eadddev;
1679
1680                 /*
1681                  * FIXME: this is racy, have to use driver-binding notification,
1682                  * when it is available
1683                  */
1684                 control = to_soc_camera_control(icd);
1685                 if (!control || !control->driver || !dev_get_drvdata(control) ||
1686                     !try_module_get(control->driver->owner)) {
1687                         shd->del_device(icd);
1688                         ret = -ENODEV;
1689                         goto enodrv;
1690                 }
1691         }
1692
1693         mutex_lock(&ici->host_lock);
1694         ret = soc_camera_probe_finish(icd);
1695         mutex_unlock(&ici->host_lock);
1696         if (ret < 0)
1697                 goto efinish;
1698
1699         return 0;
1700
1701 efinish:
1702         if (shd->board_info) {
1703                 soc_camera_i2c_free(icd);
1704         } else {
1705                 shd->del_device(icd);
1706                 module_put(control->driver->owner);
1707 enodrv:
1708 eadddev:
1709                 soc_camera_clock_stop(ici);
1710         }
1711 eadd:
1712         if (icd->vdev) {
1713                 video_device_release(icd->vdev);
1714                 icd->vdev = NULL;
1715         }
1716 evdc:
1717         v4l2_ctrl_handler_free(&icd->ctrl_handler);
1718         return ret;
1719 }
1720
1721 /*
1722  * This is called on device_unregister, which only means we have to disconnect
1723  * from the host, but not remove ourselves from the device list. With
1724  * asynchronous client probing this can also be called without
1725  * soc_camera_probe_finish() having run. Careful with clean up.
1726  */
1727 static int soc_camera_remove(struct soc_camera_device *icd)
1728 {
1729         struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
1730         struct video_device *vdev = icd->vdev;
1731
1732         v4l2_ctrl_handler_free(&icd->ctrl_handler);
1733         if (vdev) {
1734                 video_unregister_device(vdev);
1735                 icd->vdev = NULL;
1736         }
1737
1738         if (sdesc->host_desc.board_info) {
1739                 soc_camera_i2c_free(icd);
1740         } else {
1741                 struct device *dev = to_soc_camera_control(icd);
1742                 struct device_driver *drv = dev ? dev->driver : NULL;
1743                 if (drv) {
1744                         sdesc->host_desc.del_device(icd);
1745                         module_put(drv->owner);
1746                 }
1747         }
1748
1749         if (icd->num_user_formats)
1750                 soc_camera_free_user_formats(icd);
1751
1752         if (icd->clk) {
1753                 /* For the synchronous case */
1754                 v4l2_clk_unregister(icd->clk);
1755                 icd->clk = NULL;
1756         }
1757
1758         if (icd->sasc)
1759                 platform_device_unregister(icd->sasc->pdev);
1760
1761         return 0;
1762 }
1763
1764 static int default_g_selection(struct soc_camera_device *icd,
1765                                struct v4l2_selection *sel)
1766 {
1767         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1768         struct v4l2_subdev_selection sdsel = {
1769                 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1770                 .target = sel->target,
1771         };
1772         int ret;
1773
1774         ret = v4l2_subdev_call(sd, pad, get_selection, NULL, &sdsel);
1775         if (ret)
1776                 return ret;
1777         sel->r = sdsel.r;
1778         return 0;
1779 }
1780
1781 static int default_s_selection(struct soc_camera_device *icd,
1782                                struct v4l2_selection *sel)
1783 {
1784         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1785         struct v4l2_subdev_selection sdsel = {
1786                 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1787                 .target = sel->target,
1788                 .flags = sel->flags,
1789                 .r = sel->r,
1790         };
1791         int ret;
1792
1793         ret = v4l2_subdev_call(sd, pad, set_selection, NULL, &sdsel);
1794         if (ret)
1795                 return ret;
1796         sel->r = sdsel.r;
1797         return 0;
1798 }
1799
1800 static int default_g_parm(struct soc_camera_device *icd,
1801                           struct v4l2_streamparm *a)
1802 {
1803         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1804
1805         return v4l2_g_parm_cap(icd->vdev, sd, a);
1806 }
1807
1808 static int default_s_parm(struct soc_camera_device *icd,
1809                           struct v4l2_streamparm *a)
1810 {
1811         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1812
1813         return v4l2_s_parm_cap(icd->vdev, sd, a);
1814 }
1815
1816 static int default_enum_framesizes(struct soc_camera_device *icd,
1817                                    struct v4l2_frmsizeenum *fsize)
1818 {
1819         int ret;
1820         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1821         const struct soc_camera_format_xlate *xlate;
1822         struct v4l2_subdev_frame_size_enum fse = {
1823                 .index = fsize->index,
1824                 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1825         };
1826
1827         xlate = soc_camera_xlate_by_fourcc(icd, fsize->pixel_format);
1828         if (!xlate)
1829                 return -EINVAL;
1830         fse.code = xlate->code;
1831
1832         ret = v4l2_subdev_call(sd, pad, enum_frame_size, NULL, &fse);
1833         if (ret < 0)
1834                 return ret;
1835
1836         if (fse.min_width == fse.max_width &&
1837             fse.min_height == fse.max_height) {
1838                 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1839                 fsize->discrete.width = fse.min_width;
1840                 fsize->discrete.height = fse.min_height;
1841                 return 0;
1842         }
1843         fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
1844         fsize->stepwise.min_width = fse.min_width;
1845         fsize->stepwise.max_width = fse.max_width;
1846         fsize->stepwise.min_height = fse.min_height;
1847         fsize->stepwise.max_height = fse.max_height;
1848         fsize->stepwise.step_width = 1;
1849         fsize->stepwise.step_height = 1;
1850         return 0;
1851 }
1852
1853 int soc_camera_host_register(struct soc_camera_host *ici)
1854 {
1855         struct soc_camera_host *ix;
1856         int ret;
1857
1858         if (!ici || !ici->ops ||
1859             !ici->ops->try_fmt ||
1860             !ici->ops->set_fmt ||
1861             !ici->ops->set_bus_param ||
1862             !ici->ops->querycap ||
1863             !ici->ops->init_videobuf2 ||
1864             !ici->ops->poll ||
1865             !ici->v4l2_dev.dev)
1866                 return -EINVAL;
1867
1868         if (!ici->ops->set_selection)
1869                 ici->ops->set_selection = default_s_selection;
1870         if (!ici->ops->get_selection)
1871                 ici->ops->get_selection = default_g_selection;
1872         if (!ici->ops->set_parm)
1873                 ici->ops->set_parm = default_s_parm;
1874         if (!ici->ops->get_parm)
1875                 ici->ops->get_parm = default_g_parm;
1876         if (!ici->ops->enum_framesizes)
1877                 ici->ops->enum_framesizes = default_enum_framesizes;
1878
1879         mutex_lock(&list_lock);
1880         list_for_each_entry(ix, &hosts, list) {
1881                 if (ix->nr == ici->nr) {
1882                         ret = -EBUSY;
1883                         goto edevreg;
1884                 }
1885         }
1886
1887         ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
1888         if (ret < 0)
1889                 goto edevreg;
1890
1891         list_add_tail(&ici->list, &hosts);
1892         mutex_unlock(&list_lock);
1893
1894         mutex_init(&ici->host_lock);
1895         mutex_init(&ici->clk_lock);
1896
1897         if (ici->v4l2_dev.dev->of_node)
1898                 scan_of_host(ici);
1899         else if (ici->asd_sizes)
1900                 /*
1901                  * No OF, host with a list of subdevices. Don't try to mix
1902                  * modes by initialising some groups statically and some
1903                  * dynamically!
1904                  */
1905                 scan_async_host(ici);
1906         else
1907                 /* Legacy: static platform devices from board data */
1908                 scan_add_host(ici);
1909
1910         return 0;
1911
1912 edevreg:
1913         mutex_unlock(&list_lock);
1914         return ret;
1915 }
1916 EXPORT_SYMBOL(soc_camera_host_register);
1917
1918 /* Unregister all clients! */
1919 void soc_camera_host_unregister(struct soc_camera_host *ici)
1920 {
1921         struct soc_camera_device *icd, *tmp;
1922         struct soc_camera_async_client *sasc;
1923         LIST_HEAD(notifiers);
1924
1925         mutex_lock(&list_lock);
1926         list_del(&ici->list);
1927         list_for_each_entry(icd, &devices, list)
1928                 if (icd->iface == ici->nr && icd->sasc) {
1929                         /* as long as we hold the device, sasc won't be freed */
1930                         get_device(icd->pdev);
1931                         list_add(&icd->sasc->list, &notifiers);
1932                 }
1933         mutex_unlock(&list_lock);
1934
1935         list_for_each_entry(sasc, &notifiers, list) {
1936                 /* Must call unlocked to avoid AB-BA dead-lock */
1937                 v4l2_async_notifier_unregister(&sasc->notifier);
1938                 v4l2_async_notifier_cleanup(&sasc->notifier);
1939                 put_device(&sasc->pdev->dev);
1940         }
1941
1942         mutex_lock(&list_lock);
1943
1944         list_for_each_entry_safe(icd, tmp, &devices, list)
1945                 if (icd->iface == ici->nr)
1946                         soc_camera_remove(icd);
1947
1948         mutex_unlock(&list_lock);
1949
1950         v4l2_device_unregister(&ici->v4l2_dev);
1951 }
1952 EXPORT_SYMBOL(soc_camera_host_unregister);
1953
1954 /* Image capture device */
1955 static int soc_camera_device_register(struct soc_camera_device *icd)
1956 {
1957         struct soc_camera_device *ix;
1958         int num = -1, i;
1959
1960         mutex_lock(&list_lock);
1961         for (i = 0; i < 256 && num < 0; i++) {
1962                 num = i;
1963                 /* Check if this index is available on this interface */
1964                 list_for_each_entry(ix, &devices, list) {
1965                         if (ix->iface == icd->iface && ix->devnum == i) {
1966                                 num = -1;
1967                                 break;
1968                         }
1969                 }
1970         }
1971
1972         if (num < 0) {
1973                 /*
1974                  * ok, we have 256 cameras on this host...
1975                  * man, stay reasonable...
1976                  */
1977                 mutex_unlock(&list_lock);
1978                 return -ENOMEM;
1979         }
1980
1981         icd->devnum             = num;
1982         icd->use_count          = 0;
1983         icd->host_priv          = NULL;
1984
1985         /*
1986          * Dynamically allocated devices set the bit earlier, but it doesn't hurt setting
1987          * it again
1988          */
1989         i = to_platform_device(icd->pdev)->id;
1990         if (i < 0)
1991                 /* One static (legacy) soc-camera platform device */
1992                 i = 0;
1993         if (i >= MAP_MAX_NUM) {
1994                 mutex_unlock(&list_lock);
1995                 return -EBUSY;
1996         }
1997         set_bit(i, device_map);
1998         list_add_tail(&icd->list, &devices);
1999         mutex_unlock(&list_lock);
2000
2001         return 0;
2002 }
2003
2004 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
2005         .vidioc_querycap         = soc_camera_querycap,
2006         .vidioc_try_fmt_vid_cap  = soc_camera_try_fmt_vid_cap,
2007         .vidioc_g_fmt_vid_cap    = soc_camera_g_fmt_vid_cap,
2008         .vidioc_s_fmt_vid_cap    = soc_camera_s_fmt_vid_cap,
2009         .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
2010         .vidioc_enum_input       = soc_camera_enum_input,
2011         .vidioc_g_input          = soc_camera_g_input,
2012         .vidioc_s_input          = soc_camera_s_input,
2013         .vidioc_s_std            = soc_camera_s_std,
2014         .vidioc_g_std            = soc_camera_g_std,
2015         .vidioc_enum_framesizes  = soc_camera_enum_framesizes,
2016         .vidioc_reqbufs          = soc_camera_reqbufs,
2017         .vidioc_querybuf         = soc_camera_querybuf,
2018         .vidioc_qbuf             = soc_camera_qbuf,
2019         .vidioc_dqbuf            = soc_camera_dqbuf,
2020         .vidioc_create_bufs      = soc_camera_create_bufs,
2021         .vidioc_prepare_buf      = soc_camera_prepare_buf,
2022         .vidioc_expbuf           = soc_camera_expbuf,
2023         .vidioc_streamon         = soc_camera_streamon,
2024         .vidioc_streamoff        = soc_camera_streamoff,
2025         .vidioc_g_selection      = soc_camera_g_selection,
2026         .vidioc_s_selection      = soc_camera_s_selection,
2027         .vidioc_g_parm           = soc_camera_g_parm,
2028         .vidioc_s_parm           = soc_camera_s_parm,
2029 };
2030
2031 static int video_dev_create(struct soc_camera_device *icd)
2032 {
2033         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
2034         struct video_device *vdev = video_device_alloc();
2035
2036         if (!vdev)
2037                 return -ENOMEM;
2038
2039         strscpy(vdev->name, ici->drv_name, sizeof(vdev->name));
2040
2041         vdev->v4l2_dev          = &ici->v4l2_dev;
2042         vdev->fops              = &soc_camera_fops;
2043         vdev->ioctl_ops         = &soc_camera_ioctl_ops;
2044         vdev->release           = video_device_release;
2045         vdev->ctrl_handler      = &icd->ctrl_handler;
2046         vdev->lock              = &ici->host_lock;
2047
2048         icd->vdev = vdev;
2049
2050         return 0;
2051 }
2052
2053 /*
2054  * Called from soc_camera_probe() above with .host_lock held
2055  */
2056 static int soc_camera_video_start(struct soc_camera_device *icd)
2057 {
2058         const struct device_type *type = icd->vdev->dev.type;
2059         int ret;
2060
2061         if (!icd->parent)
2062                 return -ENODEV;
2063
2064         video_set_drvdata(icd->vdev, icd);
2065         if (icd->vdev->tvnorms == 0) {
2066                 /* disable the STD API if there are no tvnorms defined */
2067                 v4l2_disable_ioctl(icd->vdev, VIDIOC_G_STD);
2068                 v4l2_disable_ioctl(icd->vdev, VIDIOC_S_STD);
2069                 v4l2_disable_ioctl(icd->vdev, VIDIOC_ENUMSTD);
2070         }
2071         ret = video_register_device(icd->vdev, VFL_TYPE_VIDEO, -1);
2072         if (ret < 0) {
2073                 dev_err(icd->pdev, "video_register_device failed: %d\n", ret);
2074                 return ret;
2075         }
2076
2077         /* Restore device type, possibly set by the subdevice driver */
2078         icd->vdev->dev.type = type;
2079
2080         return 0;
2081 }
2082
2083 static int soc_camera_pdrv_probe(struct platform_device *pdev)
2084 {
2085         struct soc_camera_desc *sdesc = pdev->dev.platform_data;
2086         struct soc_camera_subdev_desc *ssdd = &sdesc->subdev_desc;
2087         struct soc_camera_device *icd;
2088         int ret;
2089
2090         if (!sdesc)
2091                 return -EINVAL;
2092
2093         icd = devm_kzalloc(&pdev->dev, sizeof(*icd), GFP_KERNEL);
2094         if (!icd)
2095                 return -ENOMEM;
2096
2097         /*
2098          * In the asynchronous case ssdd->num_regulators == 0 yet, so, the below
2099          * regulator allocation is a dummy. They are actually requested by the
2100          * subdevice driver, using soc_camera_power_init(). Also note, that in
2101          * that case regulators are attached to the I2C device and not to the
2102          * camera platform device.
2103          */
2104         ret = devm_regulator_bulk_get(&pdev->dev, ssdd->sd_pdata.num_regulators,
2105                                       ssdd->sd_pdata.regulators);
2106         if (ret < 0)
2107                 return ret;
2108
2109         icd->iface = sdesc->host_desc.bus_id;
2110         icd->sdesc = sdesc;
2111         icd->pdev = &pdev->dev;
2112         platform_set_drvdata(pdev, icd);
2113
2114         icd->user_width         = DEFAULT_WIDTH;
2115         icd->user_height        = DEFAULT_HEIGHT;
2116
2117         return soc_camera_device_register(icd);
2118 }
2119
2120 /*
2121  * Only called on rmmod for each platform device, since they are not
2122  * hot-pluggable. Now we know, that all our users - hosts and devices have
2123  * been unloaded already
2124  */
2125 static int soc_camera_pdrv_remove(struct platform_device *pdev)
2126 {
2127         struct soc_camera_device *icd = platform_get_drvdata(pdev);
2128         int i;
2129
2130         if (!icd)
2131                 return -EINVAL;
2132
2133         i = pdev->id;
2134         if (i < 0)
2135                 i = 0;
2136
2137         /*
2138          * In synchronous mode with static platform devices this is called in a
2139          * loop from drivers/base/dd.c::driver_detach(), no parallel execution,
2140          * no need to lock. In asynchronous case the caller -
2141          * soc_camera_host_unregister() - already holds the lock
2142          */
2143         if (test_bit(i, device_map)) {
2144                 clear_bit(i, device_map);
2145                 list_del(&icd->list);
2146         }
2147
2148         return 0;
2149 }
2150
2151 static struct platform_driver __refdata soc_camera_pdrv = {
2152         .probe = soc_camera_pdrv_probe,
2153         .remove  = soc_camera_pdrv_remove,
2154         .driver  = {
2155                 .name   = "soc-camera-pdrv",
2156         },
2157 };
2158
2159 module_platform_driver(soc_camera_pdrv);
2160
2161 MODULE_DESCRIPTION("Image capture bus driver");
2162 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
2163 MODULE_LICENSE("GPL");
2164 MODULE_ALIAS("platform:soc-camera-pdrv");