Version:  2.0.40 2.2.26 2.4.37 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17

Linux/drivers/media/platform/soc_camera/soc_camera.c

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

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us