4576be93d2b7166696f0436cc35743bf467c4aa2
[firefly-linux-kernel-4.4.55.git] / drivers / media / platform / soc_camera / soc_camera.c
1 /*
2  * camera image capture (abstract) bus driver
3  *
4  * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
5  *
6  * This driver provides an interface between platform-specific camera
7  * busses and camera devices. It should be used if the camera is
8  * connected not over a "proper" bus like PCI or USB, but over a
9  * special bus, like, for example, the Quick Capture interface on PXA270
10  * SoCs. Later it should also be used for i.MX31 SoCs from Freescale.
11  * It can handle multiple cameras and / or multiple busses, which can
12  * be used, e.g., in stereo-vision applications.
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License version 2 as
16  * published by the Free Software Foundation.
17  */
18
19 #include <linux/device.h>
20 #include <linux/err.h>
21 #include <linux/i2c.h>
22 #include <linux/init.h>
23 #include <linux/list.h>
24 #include <linux/mutex.h>
25 #include <linux/module.h>
26 #include <linux/platform_device.h>
27 #include <linux/regulator/consumer.h>
28 #include <linux/slab.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/vmalloc.h>
31
32 #include <media/soc_camera.h>
33 #include <media/v4l2-common.h>
34 #include <media/v4l2-ioctl.h>
35 #include <media/v4l2-dev.h>
36 #include <media/videobuf-core.h>
37 #include <media/videobuf2-core.h>
38 #include <media/soc_mediabus.h>
39
40 /* Default to VGA resolution */
41 #define DEFAULT_WIDTH   640
42 #define DEFAULT_HEIGHT  480
43
44 #define is_streaming(ici, icd)                          \
45         (((ici)->ops->init_videobuf) ?                  \
46          (icd)->vb_vidq.streaming :                     \
47          vb2_is_streaming(&(icd)->vb2_vidq))
48
49 static LIST_HEAD(hosts);
50 static LIST_HEAD(devices);
51 static DEFINE_MUTEX(list_lock);         /* Protects the list of hosts */
52
53 int soc_camera_power_on(struct device *dev, struct soc_camera_subdev_desc *ssdd)
54 {
55         int ret = regulator_bulk_enable(ssdd->num_regulators,
56                                         ssdd->regulators);
57         if (ret < 0) {
58                 dev_err(dev, "Cannot enable regulators\n");
59                 return ret;
60         }
61
62         if (ssdd->power) {
63                 ret = ssdd->power(dev, 1);
64                 if (ret < 0) {
65                         dev_err(dev,
66                                 "Platform failed to power-on the camera.\n");
67                         regulator_bulk_disable(ssdd->num_regulators,
68                                                ssdd->regulators);
69                 }
70         }
71
72         return ret;
73 }
74 EXPORT_SYMBOL(soc_camera_power_on);
75
76 int soc_camera_power_off(struct device *dev, struct soc_camera_subdev_desc *ssdd)
77 {
78         int ret = 0;
79         int err;
80
81         if (ssdd->power) {
82                 err = ssdd->power(dev, 0);
83                 if (err < 0) {
84                         dev_err(dev,
85                                 "Platform failed to power-off the camera.\n");
86                         ret = err;
87                 }
88         }
89
90         err = regulator_bulk_disable(ssdd->num_regulators,
91                                      ssdd->regulators);
92         if (err < 0) {
93                 dev_err(dev, "Cannot disable regulators\n");
94                 ret = ret ? : err;
95         }
96
97         return ret;
98 }
99 EXPORT_SYMBOL(soc_camera_power_off);
100
101 static int __soc_camera_power_on(struct soc_camera_device *icd)
102 {
103         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
104         int ret;
105
106         ret = v4l2_subdev_call(sd, core, s_power, 1);
107         if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
108                 return ret;
109
110         return 0;
111 }
112
113 static int __soc_camera_power_off(struct soc_camera_device *icd)
114 {
115         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
116         int ret;
117
118         ret = v4l2_subdev_call(sd, core, s_power, 0);
119         if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
120                 return ret;
121
122         return 0;
123 }
124
125 const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
126         struct soc_camera_device *icd, unsigned int fourcc)
127 {
128         unsigned int i;
129
130         for (i = 0; i < icd->num_user_formats; i++)
131                 if (icd->user_formats[i].host_fmt->fourcc == fourcc)
132                         return icd->user_formats + i;
133         return NULL;
134 }
135 EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);
136
137 /**
138  * soc_camera_apply_board_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
139  * @ssdd:       camera platform parameters
140  * @cfg:        media bus configuration
141  * @return:     resulting flags
142  */
143 unsigned long soc_camera_apply_board_flags(struct soc_camera_subdev_desc *ssdd,
144                                            const struct v4l2_mbus_config *cfg)
145 {
146         unsigned long f, flags = cfg->flags;
147
148         /* If only one of the two polarities is supported, switch to the opposite */
149         if (ssdd->flags & SOCAM_SENSOR_INVERT_HSYNC) {
150                 f = flags & (V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW);
151                 if (f == V4L2_MBUS_HSYNC_ACTIVE_HIGH || f == V4L2_MBUS_HSYNC_ACTIVE_LOW)
152                         flags ^= V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW;
153         }
154
155         if (ssdd->flags & SOCAM_SENSOR_INVERT_VSYNC) {
156                 f = flags & (V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW);
157                 if (f == V4L2_MBUS_VSYNC_ACTIVE_HIGH || f == V4L2_MBUS_VSYNC_ACTIVE_LOW)
158                         flags ^= V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW;
159         }
160
161         if (ssdd->flags & SOCAM_SENSOR_INVERT_PCLK) {
162                 f = flags & (V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING);
163                 if (f == V4L2_MBUS_PCLK_SAMPLE_RISING || f == V4L2_MBUS_PCLK_SAMPLE_FALLING)
164                         flags ^= V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING;
165         }
166
167         return flags;
168 }
169 EXPORT_SYMBOL(soc_camera_apply_board_flags);
170
171 #define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
172         ((x) >> 24) & 0xff
173
174 static int soc_camera_try_fmt(struct soc_camera_device *icd,
175                               struct v4l2_format *f)
176 {
177         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
178         const struct soc_camera_format_xlate *xlate;
179         struct v4l2_pix_format *pix = &f->fmt.pix;
180         int ret;
181
182         dev_dbg(icd->pdev, "TRY_FMT(%c%c%c%c, %ux%u)\n",
183                 pixfmtstr(pix->pixelformat), pix->width, pix->height);
184
185         if (pix->pixelformat != V4L2_PIX_FMT_JPEG &&
186             !(ici->capabilities & SOCAM_HOST_CAP_STRIDE)) {
187                 pix->bytesperline = 0;
188                 pix->sizeimage = 0;
189         }
190
191         ret = ici->ops->try_fmt(icd, f);
192         if (ret < 0)
193                 return ret;
194
195         xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
196         if (!xlate)
197                 return -EINVAL;
198
199         ret = soc_mbus_bytes_per_line(pix->width, xlate->host_fmt);
200         if (ret < 0)
201                 return ret;
202
203         pix->bytesperline = max_t(u32, pix->bytesperline, ret);
204
205         ret = soc_mbus_image_size(xlate->host_fmt, pix->bytesperline,
206                                   pix->height);
207         if (ret < 0)
208                 return ret;
209
210         pix->sizeimage = max_t(u32, pix->sizeimage, ret);
211
212         return 0;
213 }
214
215 static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
216                                       struct v4l2_format *f)
217 {
218         struct soc_camera_device *icd = file->private_data;
219
220         WARN_ON(priv != file->private_data);
221
222         /* Only single-plane capture is supported so far */
223         if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
224                 return -EINVAL;
225
226         /* limit format to hardware capabilities */
227         return soc_camera_try_fmt(icd, f);
228 }
229
230 static int soc_camera_enum_input(struct file *file, void *priv,
231                                  struct v4l2_input *inp)
232 {
233         if (inp->index != 0)
234                 return -EINVAL;
235
236         /* default is camera */
237         inp->type = V4L2_INPUT_TYPE_CAMERA;
238         inp->std  = V4L2_STD_UNKNOWN;
239         strcpy(inp->name, "Camera");
240
241         return 0;
242 }
243
244 static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
245 {
246         *i = 0;
247
248         return 0;
249 }
250
251 static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
252 {
253         if (i > 0)
254                 return -EINVAL;
255
256         return 0;
257 }
258
259 static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id a)
260 {
261         struct soc_camera_device *icd = file->private_data;
262         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
263
264         return v4l2_subdev_call(sd, core, s_std, a);
265 }
266
267 static int soc_camera_g_std(struct file *file, void *priv, v4l2_std_id *a)
268 {
269         struct soc_camera_device *icd = file->private_data;
270         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
271
272         return v4l2_subdev_call(sd, core, g_std, a);
273 }
274
275 static int soc_camera_enum_framesizes(struct file *file, void *fh,
276                                          struct v4l2_frmsizeenum *fsize)
277 {
278         struct soc_camera_device *icd = file->private_data;
279         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
280
281         return ici->ops->enum_framesizes(icd, fsize);
282 }
283
284 static int soc_camera_reqbufs(struct file *file, void *priv,
285                               struct v4l2_requestbuffers *p)
286 {
287         int ret;
288         struct soc_camera_device *icd = file->private_data;
289         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
290
291         WARN_ON(priv != file->private_data);
292
293         if (icd->streamer && icd->streamer != file)
294                 return -EBUSY;
295
296         if (ici->ops->init_videobuf) {
297                 ret = videobuf_reqbufs(&icd->vb_vidq, p);
298                 if (ret < 0)
299                         return ret;
300
301                 ret = ici->ops->reqbufs(icd, p);
302         } else {
303                 ret = vb2_reqbufs(&icd->vb2_vidq, p);
304         }
305
306         if (!ret && !icd->streamer)
307                 icd->streamer = file;
308
309         return ret;
310 }
311
312 static int soc_camera_querybuf(struct file *file, void *priv,
313                                struct v4l2_buffer *p)
314 {
315         struct soc_camera_device *icd = file->private_data;
316         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
317
318         WARN_ON(priv != file->private_data);
319
320         if (ici->ops->init_videobuf)
321                 return videobuf_querybuf(&icd->vb_vidq, p);
322         else
323                 return vb2_querybuf(&icd->vb2_vidq, p);
324 }
325
326 static int soc_camera_qbuf(struct file *file, void *priv,
327                            struct v4l2_buffer *p)
328 {
329         struct soc_camera_device *icd = file->private_data;
330         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
331
332         WARN_ON(priv != file->private_data);
333
334         if (icd->streamer != file)
335                 return -EBUSY;
336
337         if (ici->ops->init_videobuf)
338                 return videobuf_qbuf(&icd->vb_vidq, p);
339         else
340                 return vb2_qbuf(&icd->vb2_vidq, p);
341 }
342
343 static int soc_camera_dqbuf(struct file *file, void *priv,
344                             struct v4l2_buffer *p)
345 {
346         struct soc_camera_device *icd = file->private_data;
347         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
348
349         WARN_ON(priv != file->private_data);
350
351         if (icd->streamer != file)
352                 return -EBUSY;
353
354         if (ici->ops->init_videobuf)
355                 return videobuf_dqbuf(&icd->vb_vidq, p, file->f_flags & O_NONBLOCK);
356         else
357                 return vb2_dqbuf(&icd->vb2_vidq, p, file->f_flags & O_NONBLOCK);
358 }
359
360 static int soc_camera_create_bufs(struct file *file, void *priv,
361                             struct v4l2_create_buffers *create)
362 {
363         struct soc_camera_device *icd = file->private_data;
364         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
365
366         /* videobuf2 only */
367         if (ici->ops->init_videobuf)
368                 return -EINVAL;
369         else
370                 return vb2_create_bufs(&icd->vb2_vidq, create);
371 }
372
373 static int soc_camera_prepare_buf(struct file *file, void *priv,
374                                   struct v4l2_buffer *b)
375 {
376         struct soc_camera_device *icd = file->private_data;
377         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
378
379         /* videobuf2 only */
380         if (ici->ops->init_videobuf)
381                 return -EINVAL;
382         else
383                 return vb2_prepare_buf(&icd->vb2_vidq, b);
384 }
385
386 /* Always entered with .host_lock held */
387 static int soc_camera_init_user_formats(struct soc_camera_device *icd)
388 {
389         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
390         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
391         unsigned int i, fmts = 0, raw_fmts = 0;
392         int ret;
393         enum v4l2_mbus_pixelcode code;
394
395         while (!v4l2_subdev_call(sd, video, enum_mbus_fmt, raw_fmts, &code))
396                 raw_fmts++;
397
398         if (!ici->ops->get_formats)
399                 /*
400                  * Fallback mode - the host will have to serve all
401                  * sensor-provided formats one-to-one to the user
402                  */
403                 fmts = raw_fmts;
404         else
405                 /*
406                  * First pass - only count formats this host-sensor
407                  * configuration can provide
408                  */
409                 for (i = 0; i < raw_fmts; i++) {
410                         ret = ici->ops->get_formats(icd, i, NULL);
411                         if (ret < 0)
412                                 return ret;
413                         fmts += ret;
414                 }
415
416         if (!fmts)
417                 return -ENXIO;
418
419         icd->user_formats =
420                 vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
421         if (!icd->user_formats)
422                 return -ENOMEM;
423
424         dev_dbg(icd->pdev, "Found %d supported formats.\n", fmts);
425
426         /* Second pass - actually fill data formats */
427         fmts = 0;
428         for (i = 0; i < raw_fmts; i++)
429                 if (!ici->ops->get_formats) {
430                         v4l2_subdev_call(sd, video, enum_mbus_fmt, i, &code);
431                         icd->user_formats[fmts].host_fmt =
432                                 soc_mbus_get_fmtdesc(code);
433                         if (icd->user_formats[fmts].host_fmt)
434                                 icd->user_formats[fmts++].code = code;
435                 } else {
436                         ret = ici->ops->get_formats(icd, i,
437                                                     &icd->user_formats[fmts]);
438                         if (ret < 0)
439                                 goto egfmt;
440                         fmts += ret;
441                 }
442
443         icd->num_user_formats = fmts;
444         icd->current_fmt = &icd->user_formats[0];
445
446         return 0;
447
448 egfmt:
449         vfree(icd->user_formats);
450         return ret;
451 }
452
453 /* Always entered with .host_lock held */
454 static void soc_camera_free_user_formats(struct soc_camera_device *icd)
455 {
456         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
457
458         if (ici->ops->put_formats)
459                 ici->ops->put_formats(icd);
460         icd->current_fmt = NULL;
461         icd->num_user_formats = 0;
462         vfree(icd->user_formats);
463         icd->user_formats = NULL;
464 }
465
466 /* Called with .vb_lock held, or from the first open(2), see comment there */
467 static int soc_camera_set_fmt(struct soc_camera_device *icd,
468                               struct v4l2_format *f)
469 {
470         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
471         struct v4l2_pix_format *pix = &f->fmt.pix;
472         int ret;
473
474         dev_dbg(icd->pdev, "S_FMT(%c%c%c%c, %ux%u)\n",
475                 pixfmtstr(pix->pixelformat), pix->width, pix->height);
476
477         /* We always call try_fmt() before set_fmt() or set_crop() */
478         ret = soc_camera_try_fmt(icd, f);
479         if (ret < 0)
480                 return ret;
481
482         ret = ici->ops->set_fmt(icd, f);
483         if (ret < 0) {
484                 return ret;
485         } else if (!icd->current_fmt ||
486                    icd->current_fmt->host_fmt->fourcc != pix->pixelformat) {
487                 dev_err(icd->pdev,
488                         "Host driver hasn't set up current format correctly!\n");
489                 return -EINVAL;
490         }
491
492         icd->user_width         = pix->width;
493         icd->user_height        = pix->height;
494         icd->bytesperline       = pix->bytesperline;
495         icd->sizeimage          = pix->sizeimage;
496         icd->colorspace         = pix->colorspace;
497         icd->field              = pix->field;
498         if (ici->ops->init_videobuf)
499                 icd->vb_vidq.field = pix->field;
500
501         dev_dbg(icd->pdev, "set width: %d height: %d\n",
502                 icd->user_width, icd->user_height);
503
504         /* set physical bus parameters */
505         return ici->ops->set_bus_param(icd, pix->pixelformat);//yzm
506 }
507
508 static int soc_camera_open(struct file *file)
509 {
510         struct video_device *vdev = video_devdata(file);
511         struct soc_camera_device *icd;
512         struct soc_camera_host *ici;
513         int ret;
514
515         /*
516          * Don't mess with the host during probe: wait until the loop in
517          * scan_add_host() completes. Also protect against a race with
518          * soc_camera_host_unregister().
519          */
520         if (mutex_lock_interruptible(&list_lock))
521                 return -ERESTARTSYS;
522
523         if (!vdev || !video_is_registered(vdev)) {
524                 mutex_unlock(&list_lock);
525                 return -ENODEV;
526         }
527
528         icd = dev_get_drvdata(vdev->parent);
529         ici = to_soc_camera_host(icd->parent);
530
531         ret = try_module_get(ici->ops->owner) ? 0 : -ENODEV;
532         mutex_unlock(&list_lock);
533
534         if (ret < 0) {
535                 dev_err(icd->pdev, "Couldn't lock capture bus driver.\n");
536                 return ret;
537         }
538
539         if (!to_soc_camera_control(icd)) {
540                 /* No device driver attached */
541                 ret = -ENODEV;
542                 goto econtrol;
543         }
544
545         if (mutex_lock_interruptible(&ici->host_lock)) {
546                 ret = -ERESTARTSYS;
547                 goto elockhost;
548         }
549         icd->use_count++;
550
551         /* Now we really have to activate the camera */
552         if (icd->use_count == 1) {
553                 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
554                 /* Restore parameters before the last close() per V4L2 API */
555                 struct v4l2_format f = {
556                         .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
557                         .fmt.pix = {
558                                 .width          = icd->user_width,
559                                 .height         = icd->user_height,
560                                 .field          = icd->field,
561                                 .colorspace     = icd->colorspace,
562                                 .pixelformat    =
563                                         icd->current_fmt->host_fmt->fourcc,
564                         },
565                 };
566
567                 /* The camera could have been already on, try to reset */
568                 if (sdesc->subdev_desc.reset)
569                         sdesc->subdev_desc.reset(icd->pdev);
570
571                 ret = ici->ops->add(icd);
572                 if (ret < 0) {
573                         dev_err(icd->pdev, "Couldn't activate the camera: %d\n", ret);
574                         goto eiciadd;
575                 }
576
577                 ret = __soc_camera_power_on(icd);
578                 if (ret < 0)
579                         goto epower;
580
581                 pm_runtime_enable(&icd->vdev->dev);
582                 ret = pm_runtime_resume(&icd->vdev->dev);
583                 if (ret < 0 && ret != -ENOSYS)
584                         goto eresume;
585
586                 /*
587                  * Try to configure with default parameters. Notice: this is the
588                  * very first open, so, we cannot race against other calls,
589                  * apart from someone else calling open() simultaneously, but
590                  * .host_lock is protecting us against it.
591                  */
592                 ret = soc_camera_set_fmt(icd, &f);
593                 if (ret < 0)
594                         goto esfmt;
595
596                 if (ici->ops->init_videobuf) {
597                         ici->ops->init_videobuf(&icd->vb_vidq, icd);
598                 } else {
599                         ret = ici->ops->init_videobuf2(&icd->vb2_vidq, icd);
600                         if (ret < 0)
601                                 goto einitvb;
602                 }
603                 v4l2_ctrl_handler_setup(&icd->ctrl_handler);
604         }
605         mutex_unlock(&ici->host_lock);
606
607         file->private_data = icd;
608         dev_dbg(icd->pdev, "camera device open\n");
609
610         return 0;
611
612         /*
613          * First four errors are entered with the .host_lock held
614          * and use_count == 1
615          */
616 einitvb:
617 esfmt:
618         pm_runtime_disable(&icd->vdev->dev);
619 eresume:
620         __soc_camera_power_off(icd);
621 epower:
622         ici->ops->remove(icd);
623 eiciadd:
624         icd->use_count--;
625         mutex_unlock(&ici->host_lock);
626 elockhost:
627 econtrol:
628         module_put(ici->ops->owner);
629
630         return ret;
631 }
632
633 static int soc_camera_close(struct file *file)
634 {
635         struct soc_camera_device *icd = file->private_data;
636         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
637
638         mutex_lock(&ici->host_lock);
639         icd->use_count--;
640         if (!icd->use_count) {
641                 pm_runtime_suspend(&icd->vdev->dev);
642                 pm_runtime_disable(&icd->vdev->dev);
643
644                 if (ici->ops->init_videobuf2)
645                         vb2_queue_release(&icd->vb2_vidq);
646                 __soc_camera_power_off(icd);
647
648                 ici->ops->remove(icd);
649         }
650
651         if (icd->streamer == file)
652                 icd->streamer = NULL;
653         mutex_unlock(&ici->host_lock);
654
655         module_put(ici->ops->owner);
656
657         dev_dbg(icd->pdev, "camera device close\n");
658
659         return 0;
660 }
661
662 static ssize_t soc_camera_read(struct file *file, char __user *buf,
663                                size_t count, loff_t *ppos)
664 {
665         struct soc_camera_device *icd = file->private_data;
666         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
667
668         dev_dbg(icd->pdev, "read called, buf %p\n", buf);
669
670         if (ici->ops->init_videobuf2 && icd->vb2_vidq.io_modes & VB2_READ)
671                 return vb2_read(&icd->vb2_vidq, buf, count, ppos,
672                                 file->f_flags & O_NONBLOCK);
673
674         dev_err(icd->pdev, "camera device read not implemented\n");
675
676         return -EINVAL;
677 }
678
679 static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
680 {
681         struct soc_camera_device *icd = file->private_data;
682         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
683         int err;
684
685         dev_dbg(icd->pdev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
686
687         if (icd->streamer != file)
688                 return -EBUSY;
689
690         if (mutex_lock_interruptible(&ici->host_lock))
691                 return -ERESTARTSYS;
692         if (ici->ops->init_videobuf)
693                 err = videobuf_mmap_mapper(&icd->vb_vidq, vma);
694         else
695                 err = vb2_mmap(&icd->vb2_vidq, vma);
696         mutex_unlock(&ici->host_lock);
697
698         dev_dbg(icd->pdev, "vma start=0x%08lx, size=%ld, ret=%d\n",
699                 (unsigned long)vma->vm_start,
700                 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
701                 err);
702
703         return err;
704 }
705
706 static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
707 {
708         struct soc_camera_device *icd = file->private_data;
709         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
710         unsigned res = POLLERR;
711
712         if (icd->streamer != file)
713                 return POLLERR;
714
715         mutex_lock(&ici->host_lock);
716         if (ici->ops->init_videobuf && list_empty(&icd->vb_vidq.stream))
717                 dev_err(icd->pdev, "Trying to poll with no queued buffers!\n");
718         else
719                 res = ici->ops->poll(file, pt);
720         mutex_unlock(&ici->host_lock);
721         return res;
722 }
723
724 void soc_camera_lock(struct vb2_queue *vq)
725 {
726         struct soc_camera_device *icd = vb2_get_drv_priv(vq);
727         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
728         mutex_lock(&ici->host_lock);
729 }
730 EXPORT_SYMBOL(soc_camera_lock);
731
732 void soc_camera_unlock(struct vb2_queue *vq)
733 {
734         struct soc_camera_device *icd = vb2_get_drv_priv(vq);
735         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
736         mutex_unlock(&ici->host_lock);
737 }
738 EXPORT_SYMBOL(soc_camera_unlock);
739
740 static struct v4l2_file_operations soc_camera_fops = {
741         .owner          = THIS_MODULE,
742         .open           = soc_camera_open,
743         .release        = soc_camera_close,
744         .unlocked_ioctl = video_ioctl2,
745         .read           = soc_camera_read,
746         .mmap           = soc_camera_mmap,
747         .poll           = soc_camera_poll,
748 };
749
750 static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
751                                     struct v4l2_format *f)
752 {
753         struct soc_camera_device *icd = file->private_data;
754         int ret;
755
756         WARN_ON(priv != file->private_data);
757
758         if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
759                 dev_warn(icd->pdev, "Wrong buf-type %d\n", f->type);
760                 return -EINVAL;
761         }
762
763         if (icd->streamer && icd->streamer != file)
764                 return -EBUSY;
765
766         if (is_streaming(to_soc_camera_host(icd->parent), icd)) {
767                 dev_err(icd->pdev, "S_FMT denied: queue initialised\n");
768                 return -EBUSY;
769         }
770
771         ret = soc_camera_set_fmt(icd, f);
772
773         if (!ret && !icd->streamer)
774                 icd->streamer = file;
775
776         return ret;
777 }
778
779 static int soc_camera_enum_fmt_vid_cap(struct file *file, void  *priv,
780                                        struct v4l2_fmtdesc *f)
781 {
782         struct soc_camera_device *icd = file->private_data;
783         const struct soc_mbus_pixelfmt *format;
784
785         WARN_ON(priv != file->private_data);
786
787         if (f->index >= icd->num_user_formats)
788                 return -EINVAL;
789
790         format = icd->user_formats[f->index].host_fmt;
791
792         if (format->name)
793                 strlcpy(f->description, format->name, sizeof(f->description));
794         f->pixelformat = format->fourcc;
795         return 0;
796 }
797
798 static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
799                                     struct v4l2_format *f)
800 {
801         struct soc_camera_device *icd = file->private_data;
802         struct v4l2_pix_format *pix = &f->fmt.pix;
803
804         WARN_ON(priv != file->private_data);
805
806         if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
807                 return -EINVAL;
808
809         pix->width              = icd->user_width;
810         pix->height             = icd->user_height;
811         pix->bytesperline       = icd->bytesperline;
812         pix->sizeimage          = icd->sizeimage;
813         pix->field              = icd->field;
814         pix->pixelformat        = icd->current_fmt->host_fmt->fourcc;
815         pix->colorspace         = icd->colorspace;
816         dev_dbg(icd->pdev, "current_fmt->fourcc: 0x%08x\n",
817                 icd->current_fmt->host_fmt->fourcc);
818         return 0;
819 }
820
821 static int soc_camera_querycap(struct file *file, void  *priv,
822                                struct v4l2_capability *cap)
823 {
824         struct soc_camera_device *icd = file->private_data;
825         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
826
827         WARN_ON(priv != file->private_data);
828
829         strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
830         return ici->ops->querycap(ici, cap);
831 }
832
833 static int soc_camera_streamon(struct file *file, void *priv,
834                                enum v4l2_buf_type i)
835 {
836         struct soc_camera_device *icd = file->private_data;
837         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
838         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
839         int ret;
840
841         WARN_ON(priv != file->private_data);
842
843         if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
844                 return -EINVAL;
845
846         if (icd->streamer != file)
847                 return -EBUSY;
848
849         /* This calls buf_queue from host driver's videobuf_queue_ops */
850         if (ici->ops->init_videobuf)
851                 ret = videobuf_streamon(&icd->vb_vidq);
852         else
853                 ret = vb2_streamon(&icd->vb2_vidq, i);
854
855         if (!ret)
856                 v4l2_subdev_call(sd, video, s_stream, 1);
857
858         return ret;
859 }
860
861 static int soc_camera_streamoff(struct file *file, void *priv,
862                                 enum v4l2_buf_type i)
863 {
864         struct soc_camera_device *icd = file->private_data;
865         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
866         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
867
868         WARN_ON(priv != file->private_data);
869
870         if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
871                 return -EINVAL;
872
873         if (icd->streamer != file)
874                 return -EBUSY;
875
876         /*
877          * This calls buf_release from host driver's videobuf_queue_ops for all
878          * remaining buffers. When the last buffer is freed, stop capture
879          */
880         if (ici->ops->init_videobuf)
881                 videobuf_streamoff(&icd->vb_vidq);
882         else
883                 vb2_streamoff(&icd->vb2_vidq, i);
884
885         v4l2_subdev_call(sd, video, s_stream, 0);
886
887         return 0;
888 }
889
890 static int soc_camera_cropcap(struct file *file, void *fh,
891                               struct v4l2_cropcap *a)
892 {
893         struct soc_camera_device *icd = file->private_data;
894         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
895
896         return ici->ops->cropcap(icd, a);
897 }
898
899 static int soc_camera_g_crop(struct file *file, void *fh,
900                              struct v4l2_crop *a)
901 {
902         struct soc_camera_device *icd = file->private_data;
903         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
904         int ret;
905
906         ret = ici->ops->get_crop(icd, a);
907
908         return ret;
909 }
910
911 /*
912  * According to the V4L2 API, drivers shall not update the struct v4l2_crop
913  * argument with the actual geometry, instead, the user shall use G_CROP to
914  * retrieve it.
915  */
916 static int soc_camera_s_crop(struct file *file, void *fh,
917                              const struct v4l2_crop *a)
918 {
919         struct soc_camera_device *icd = file->private_data;
920         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
921         const struct v4l2_rect *rect = &a->c;
922         struct v4l2_crop current_crop;
923         int ret;
924
925         if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
926                 return -EINVAL;
927
928         dev_dbg(icd->pdev, "S_CROP(%ux%u@%u:%u)\n",
929                 rect->width, rect->height, rect->left, rect->top);
930
931         current_crop.type = a->type;
932
933         /* If get_crop fails, we'll let host and / or client drivers decide */
934         ret = ici->ops->get_crop(icd, &current_crop);
935
936         /* Prohibit window size change with initialised buffers */
937         if (ret < 0) {
938                 dev_err(icd->pdev,
939                         "S_CROP denied: getting current crop failed\n");
940         } else if ((a->c.width == current_crop.c.width &&
941                     a->c.height == current_crop.c.height) ||
942                    !is_streaming(ici, icd)) {
943                 /* same size or not streaming - use .set_crop() */
944                 ret = ici->ops->set_crop(icd, a);
945         } else if (ici->ops->set_livecrop) {
946                 ret = ici->ops->set_livecrop(icd, a);
947         } else {
948                 dev_err(icd->pdev,
949                         "S_CROP denied: queue initialised and sizes differ\n");
950                 ret = -EBUSY;
951         }
952
953         return ret;
954 }
955
956 static int soc_camera_g_selection(struct file *file, void *fh,
957                                   struct v4l2_selection *s)
958 {
959         struct soc_camera_device *icd = file->private_data;
960         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
961
962         /* With a wrong type no need to try to fall back to cropping */
963         if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
964                 return -EINVAL;
965
966         if (!ici->ops->get_selection)
967                 return -ENOTTY;
968
969         return ici->ops->get_selection(icd, s);
970 }
971
972 static int soc_camera_s_selection(struct file *file, void *fh,
973                                   struct v4l2_selection *s)
974 {
975         struct soc_camera_device *icd = file->private_data;
976         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
977         int ret;
978
979         /* In all these cases cropping emulation will not help */
980         if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
981             (s->target != V4L2_SEL_TGT_COMPOSE &&
982              s->target != V4L2_SEL_TGT_CROP))
983                 return -EINVAL;
984
985         if (s->target == V4L2_SEL_TGT_COMPOSE) {
986                 /* No output size change during a running capture! */
987                 if (is_streaming(ici, icd) &&
988                     (icd->user_width != s->r.width ||
989                      icd->user_height != s->r.height))
990                         return -EBUSY;
991
992                 /*
993                  * Only one user is allowed to change the output format, touch
994                  * buffers, start / stop streaming, poll for data
995                  */
996                 if (icd->streamer && icd->streamer != file)
997                         return -EBUSY;
998         }
999
1000         if (!ici->ops->set_selection)
1001                 return -ENOTTY;
1002
1003         ret = ici->ops->set_selection(icd, s);
1004         if (!ret &&
1005             s->target == V4L2_SEL_TGT_COMPOSE) {
1006                 icd->user_width = s->r.width;
1007                 icd->user_height = s->r.height;
1008                 if (!icd->streamer)
1009                         icd->streamer = file;
1010         }
1011
1012         return ret;
1013 }
1014
1015 static int soc_camera_g_parm(struct file *file, void *fh,
1016                              struct v4l2_streamparm *a)
1017 {
1018         struct soc_camera_device *icd = file->private_data;
1019         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1020
1021         if (ici->ops->get_parm)
1022                 return ici->ops->get_parm(icd, a);
1023
1024         return -ENOIOCTLCMD;
1025 }
1026
1027 static int soc_camera_s_parm(struct file *file, void *fh,
1028                              struct v4l2_streamparm *a)
1029 {
1030         struct soc_camera_device *icd = file->private_data;
1031         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1032
1033         if (ici->ops->set_parm)
1034                 return ici->ops->set_parm(icd, a);
1035
1036         return -ENOIOCTLCMD;
1037 }
1038
1039 static int soc_camera_g_chip_ident(struct file *file, void *fh,
1040                                    struct v4l2_dbg_chip_ident *id)
1041 {
1042         struct soc_camera_device *icd = file->private_data;
1043         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1044
1045         return v4l2_subdev_call(sd, core, g_chip_ident, id);
1046 }
1047
1048 #ifdef CONFIG_VIDEO_ADV_DEBUG
1049 static int soc_camera_g_register(struct file *file, void *fh,
1050                                  struct v4l2_dbg_register *reg)
1051 {
1052         struct soc_camera_device *icd = file->private_data;
1053         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1054
1055         return v4l2_subdev_call(sd, core, g_register, reg);
1056 }
1057
1058 static int soc_camera_s_register(struct file *file, void *fh,
1059                                  const struct v4l2_dbg_register *reg)
1060 {
1061         struct soc_camera_device *icd = file->private_data;
1062         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1063
1064         return v4l2_subdev_call(sd, core, s_register, reg);
1065 }
1066 #endif
1067
1068 static int soc_camera_probe(struct soc_camera_device *icd);
1069
1070 /* So far this function cannot fail */
1071 static void scan_add_host(struct soc_camera_host *ici)
1072 {
1073         struct soc_camera_device *icd;
1074
1075         mutex_lock(&list_lock);
1076
1077         list_for_each_entry(icd, &devices, list) {
1078                 if (icd->iface == ici->nr) {
1079                         icd->parent = ici->v4l2_dev.dev;
1080                         soc_camera_probe(icd);
1081                 }
1082         }
1083
1084         mutex_unlock(&list_lock);
1085 }
1086
1087 #ifdef CONFIG_I2C_BOARDINFO
1088 static int soc_camera_init_i2c(struct soc_camera_device *icd,
1089                                struct soc_camera_desc *sdesc)
1090 {
1091         struct i2c_client *client;
1092         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1093         struct soc_camera_host_desc *shd = &sdesc->host_desc;
1094         struct i2c_adapter *adap = i2c_get_adapter(shd->i2c_adapter_id);
1095         struct v4l2_subdev *subdev;
1096
1097         if (!adap) {
1098                 dev_err(icd->pdev, "Cannot get I2C adapter #%d. No driver?\n",
1099                         shd->i2c_adapter_id);
1100                 goto ei2cga;
1101         }
1102
1103         shd->board_info->platform_data = &sdesc->subdev_desc;
1104
1105         subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
1106                                 shd->board_info, NULL);
1107         if (!subdev)
1108                 goto ei2cnd;
1109
1110         client = v4l2_get_subdevdata(subdev);
1111
1112         /* Use to_i2c_client(dev) to recover the i2c client */
1113         icd->control = &client->dev;
1114
1115         return 0;
1116 ei2cnd:
1117         i2c_put_adapter(adap);
1118 ei2cga:
1119         return -ENODEV;
1120 }
1121
1122 static void soc_camera_free_i2c(struct soc_camera_device *icd)
1123 {
1124         struct i2c_client *client =
1125                 to_i2c_client(to_soc_camera_control(icd));
1126         struct i2c_adapter *adap = client->adapter;
1127
1128         icd->control = NULL;
1129         v4l2_device_unregister_subdev(i2c_get_clientdata(client));
1130         i2c_unregister_device(client);
1131         i2c_put_adapter(adap);
1132 }
1133 #else
1134 #define soc_camera_init_i2c(icd, sdesc) (-ENODEV)
1135 #define soc_camera_free_i2c(icd)        do {} while (0)
1136 #endif
1137
1138 static int soc_camera_video_start(struct soc_camera_device *icd);
1139 static int video_dev_create(struct soc_camera_device *icd);
1140 /* Called during host-driver probe */
1141 static int soc_camera_probe(struct soc_camera_device *icd)
1142 {
1143         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1144         struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
1145         struct soc_camera_host_desc *shd = &sdesc->host_desc;
1146         struct soc_camera_subdev_desc *ssdd = &sdesc->subdev_desc;
1147         struct device *control = NULL;
1148         struct v4l2_subdev *sd;
1149         struct v4l2_mbus_framefmt mf;
1150         int ret;
1151
1152         dev_info(icd->pdev, "Probing %s\n", dev_name(icd->pdev));
1153
1154         /*
1155          * Currently the subdev with the largest number of controls (13) is
1156          * ov6550. So let's pick 16 as a hint for the control handler. Note
1157          * that this is a hint only: too large and you waste some memory, too
1158          * small and there is a (very) small performance hit when looking up
1159          * controls in the internal hash.
1160          */
1161         ret = v4l2_ctrl_handler_init(&icd->ctrl_handler, 16);
1162         if (ret < 0)
1163                 return ret;
1164
1165         /* The camera could have been already on, try to reset */
1166         if (ssdd->reset)
1167                 ssdd->reset(icd->pdev);
1168         /*********yzm**********/        
1169         ret = soc_camera_power_on(icd->pdev,ssdd);
1170         if (ret < 0)
1171                 goto eadd;
1172         /*********yzm*********/
1173         mutex_lock(&ici->host_lock);
1174         ret = ici->ops->add(icd);
1175         mutex_unlock(&ici->host_lock);
1176         if (ret < 0)
1177                 goto eadd;
1178
1179         /* Must have icd->vdev before registering the device */
1180         ret = video_dev_create(icd);
1181         if (ret < 0)
1182                 goto evdc;
1183         ssdd->socdev = icd;//yzm
1184         /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
1185         if (shd->board_info) {
1186                 ret = soc_camera_init_i2c(icd, sdesc);
1187                 if (ret < 0)
1188                         goto eadddev;
1189         } else if (!shd->add_device || !shd->del_device) {
1190                 ret = -EINVAL;
1191                 goto eadddev;
1192         } else {
1193                 if (shd->module_name)
1194                         ret = request_module(shd->module_name);
1195
1196                 ret = shd->add_device(icd);
1197                 if (ret < 0)
1198                         goto eadddev;
1199
1200                 /*
1201                  * FIXME: this is racy, have to use driver-binding notification,
1202                  * when it is available
1203                  */
1204                 control = to_soc_camera_control(icd);
1205                 if (!control || !control->driver || !dev_get_drvdata(control) ||
1206                     !try_module_get(control->driver->owner)) {
1207                         shd->del_device(icd);
1208                         ret = -ENODEV;
1209                         goto enodrv;
1210                 }
1211         }
1212
1213         sd = soc_camera_to_subdev(icd);
1214         sd->grp_id = soc_camera_grp_id(icd);
1215         v4l2_set_subdev_hostdata(sd, icd);
1216
1217         ret = v4l2_ctrl_add_handler(&icd->ctrl_handler, sd->ctrl_handler, NULL);
1218         if (ret < 0)
1219                 goto ectrl;
1220
1221         /* At this point client .probe() should have run already */
1222         ret = soc_camera_init_user_formats(icd);
1223         if (ret < 0)
1224                 goto eiufmt;
1225
1226         icd->field = V4L2_FIELD_ANY;
1227
1228         /*
1229          * ..._video_start() will create a device node, video_register_device()
1230          * itself is protected against concurrent open() calls, but we also have
1231          * to protect our data.
1232          */
1233         mutex_lock(&ici->host_lock);
1234
1235         ret = soc_camera_video_start(icd);
1236         if (ret < 0)
1237                 goto evidstart;
1238
1239         /* Try to improve our guess of a reasonable window format */
1240         if (!v4l2_subdev_call(sd, video, g_mbus_fmt, &mf)) {
1241                 icd->user_width         = mf.width;
1242                 icd->user_height        = mf.height;
1243                 icd->colorspace         = mf.colorspace;
1244                 icd->field              = mf.field;
1245         }
1246
1247         ici->ops->remove(icd);
1248
1249         mutex_unlock(&ici->host_lock);
1250
1251         return 0;
1252
1253 evidstart:
1254         mutex_unlock(&ici->host_lock);
1255         soc_camera_free_user_formats(icd);
1256 eiufmt:
1257 ectrl:
1258         if (shd->board_info) {
1259                 soc_camera_free_i2c(icd);
1260         } else {
1261                 shd->del_device(icd);
1262                 module_put(control->driver->owner);
1263         }
1264 enodrv:
1265 eadddev:
1266         video_device_release(icd->vdev);
1267         icd->vdev = NULL;
1268 evdc:
1269         mutex_lock(&ici->host_lock);
1270         ici->ops->remove(icd);
1271         soc_camera_power_off(icd->pdev,ssdd);//yzm
1272         mutex_unlock(&ici->host_lock);
1273 eadd:
1274         v4l2_ctrl_handler_free(&icd->ctrl_handler);
1275         return ret;
1276 }
1277
1278 /*
1279  * This is called on device_unregister, which only means we have to disconnect
1280  * from the host, but not remove ourselves from the device list
1281  */
1282 static int soc_camera_remove(struct soc_camera_device *icd)
1283 {
1284         struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
1285         struct video_device *vdev = icd->vdev;
1286
1287         BUG_ON(!icd->parent);
1288
1289         v4l2_ctrl_handler_free(&icd->ctrl_handler);
1290         if (vdev) {
1291                 video_unregister_device(vdev);
1292                 icd->vdev = NULL;
1293         }
1294
1295         if (sdesc->host_desc.board_info) {
1296                 soc_camera_free_i2c(icd);
1297         } else {
1298                 struct device_driver *drv = to_soc_camera_control(icd)->driver;
1299                 if (drv) {
1300                         sdesc->host_desc.del_device(icd);
1301                         module_put(drv->owner);
1302                 }
1303         }
1304         soc_camera_free_user_formats(icd);
1305
1306         return 0;
1307 }
1308
1309 static int default_cropcap(struct soc_camera_device *icd,
1310                            struct v4l2_cropcap *a)
1311 {
1312         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1313         return v4l2_subdev_call(sd, video, cropcap, a);
1314 }
1315
1316 static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1317 {
1318         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1319         return v4l2_subdev_call(sd, video, g_crop, a);
1320 }
1321
1322 static int default_s_crop(struct soc_camera_device *icd, const struct v4l2_crop *a)
1323 {
1324         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1325         return v4l2_subdev_call(sd, video, s_crop, a);
1326 }
1327
1328 static int default_g_parm(struct soc_camera_device *icd,
1329                           struct v4l2_streamparm *parm)
1330 {
1331         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1332         return v4l2_subdev_call(sd, video, g_parm, parm);
1333 }
1334
1335 static int default_s_parm(struct soc_camera_device *icd,
1336                           struct v4l2_streamparm *parm)
1337 {
1338         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1339         return v4l2_subdev_call(sd, video, s_parm, parm);
1340 }
1341
1342 static int default_enum_framesizes(struct soc_camera_device *icd,
1343                                    struct v4l2_frmsizeenum *fsize)
1344 {
1345         int ret;
1346         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1347         const struct soc_camera_format_xlate *xlate;
1348         __u32 pixfmt = fsize->pixel_format;
1349         struct v4l2_frmsizeenum fsize_mbus = *fsize;
1350
1351         xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1352         if (!xlate)
1353                 return -EINVAL;
1354         /* map xlate-code to pixel_format, sensor only handle xlate-code*/
1355         fsize_mbus.pixel_format = xlate->code;
1356
1357         ret = v4l2_subdev_call(sd, video, enum_framesizes, &fsize_mbus);
1358         if (ret < 0)
1359                 return ret;
1360
1361         *fsize = fsize_mbus;
1362         fsize->pixel_format = pixfmt;
1363
1364         return 0;
1365 }
1366
1367 int soc_camera_host_register(struct soc_camera_host *ici)
1368 {
1369         struct soc_camera_host *ix;
1370         int ret;
1371
1372         if (!ici || !ici->ops ||
1373             !ici->ops->try_fmt ||
1374             !ici->ops->set_fmt ||
1375             !ici->ops->set_bus_param ||
1376             !ici->ops->querycap ||
1377             ((!ici->ops->init_videobuf ||
1378               !ici->ops->reqbufs) &&
1379              !ici->ops->init_videobuf2) ||
1380             !ici->ops->add ||
1381             !ici->ops->remove ||
1382             !ici->ops->poll ||
1383             !ici->v4l2_dev.dev)
1384                 return -EINVAL;
1385
1386         if (!ici->ops->set_crop)
1387                 ici->ops->set_crop = default_s_crop;
1388         if (!ici->ops->get_crop)
1389                 ici->ops->get_crop = default_g_crop;
1390         if (!ici->ops->cropcap)
1391                 ici->ops->cropcap = default_cropcap;
1392         if (!ici->ops->set_parm)
1393                 ici->ops->set_parm = default_s_parm;
1394         if (!ici->ops->get_parm)
1395                 ici->ops->get_parm = default_g_parm;
1396         if (!ici->ops->enum_framesizes)
1397                 ici->ops->enum_framesizes = default_enum_framesizes;
1398
1399         mutex_lock(&list_lock);
1400         list_for_each_entry(ix, &hosts, list) {
1401                 if (ix->nr == ici->nr) {
1402                         ret = -EBUSY;
1403                         goto edevreg;
1404                 }
1405         }
1406
1407         ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
1408         if (ret < 0)
1409                 goto edevreg;
1410
1411         list_add_tail(&ici->list, &hosts);
1412         mutex_unlock(&list_lock);
1413
1414         mutex_init(&ici->host_lock);
1415         scan_add_host(ici);
1416
1417         return 0;
1418
1419 edevreg:
1420         mutex_unlock(&list_lock);
1421         return ret;
1422 }
1423 EXPORT_SYMBOL(soc_camera_host_register);
1424
1425 /* Unregister all clients! */
1426 void soc_camera_host_unregister(struct soc_camera_host *ici)
1427 {
1428         struct soc_camera_device *icd;
1429
1430         mutex_lock(&list_lock);
1431
1432         list_del(&ici->list);
1433         list_for_each_entry(icd, &devices, list)
1434                 if (icd->iface == ici->nr && to_soc_camera_control(icd))
1435                         soc_camera_remove(icd);
1436
1437         mutex_unlock(&list_lock);
1438
1439         v4l2_device_unregister(&ici->v4l2_dev);
1440 }
1441 EXPORT_SYMBOL(soc_camera_host_unregister);
1442
1443 /* Image capture device */
1444 static int soc_camera_device_register(struct soc_camera_device *icd)
1445 {
1446         struct soc_camera_device *ix;
1447         int num = -1, i;
1448
1449         for (i = 0; i < 256 && num < 0; i++) {
1450                 num = i;
1451                 /* Check if this index is available on this interface */
1452                 list_for_each_entry(ix, &devices, list) {
1453                         if (ix->iface == icd->iface && ix->devnum == i) {
1454                                 num = -1;
1455                                 break;
1456                         }
1457                 }
1458         }
1459
1460         if (num < 0)
1461                 /*
1462                  * ok, we have 256 cameras on this host...
1463                  * man, stay reasonable...
1464                  */
1465                 return -ENOMEM;
1466
1467         icd->devnum             = num;
1468         icd->use_count          = 0;
1469         icd->host_priv          = NULL;
1470
1471         list_add_tail(&icd->list, &devices);
1472
1473         return 0;
1474 }
1475
1476 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
1477         .vidioc_querycap         = soc_camera_querycap,
1478         .vidioc_try_fmt_vid_cap  = soc_camera_try_fmt_vid_cap,
1479         .vidioc_g_fmt_vid_cap    = soc_camera_g_fmt_vid_cap,
1480         .vidioc_s_fmt_vid_cap    = soc_camera_s_fmt_vid_cap,
1481         .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
1482         .vidioc_enum_input       = soc_camera_enum_input,
1483         .vidioc_g_input          = soc_camera_g_input,
1484         .vidioc_s_input          = soc_camera_s_input,
1485         .vidioc_s_std            = soc_camera_s_std,
1486         .vidioc_g_std            = soc_camera_g_std,
1487         .vidioc_enum_framesizes  = soc_camera_enum_framesizes,
1488         .vidioc_reqbufs          = soc_camera_reqbufs,
1489         .vidioc_querybuf         = soc_camera_querybuf,
1490         .vidioc_qbuf             = soc_camera_qbuf,
1491         .vidioc_dqbuf            = soc_camera_dqbuf,
1492         .vidioc_create_bufs      = soc_camera_create_bufs,
1493         .vidioc_prepare_buf      = soc_camera_prepare_buf,
1494         .vidioc_streamon         = soc_camera_streamon,
1495         .vidioc_streamoff        = soc_camera_streamoff,
1496         .vidioc_cropcap          = soc_camera_cropcap,
1497         .vidioc_g_crop           = soc_camera_g_crop,
1498         .vidioc_s_crop           = soc_camera_s_crop,
1499         .vidioc_g_selection      = soc_camera_g_selection,
1500         .vidioc_s_selection      = soc_camera_s_selection,
1501         .vidioc_g_parm           = soc_camera_g_parm,
1502         .vidioc_s_parm           = soc_camera_s_parm,
1503         .vidioc_g_chip_ident     = soc_camera_g_chip_ident,
1504 #ifdef CONFIG_VIDEO_ADV_DEBUG
1505         .vidioc_g_register       = soc_camera_g_register,
1506         .vidioc_s_register       = soc_camera_s_register,
1507 #endif
1508 };
1509
1510 static int video_dev_create(struct soc_camera_device *icd)
1511 {
1512         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1513         struct video_device *vdev = video_device_alloc();
1514
1515         if (!vdev)
1516                 return -ENOMEM;
1517
1518         strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
1519
1520         vdev->parent            = icd->pdev;
1521         vdev->current_norm      = V4L2_STD_UNKNOWN;
1522         vdev->fops              = &soc_camera_fops;
1523         vdev->ioctl_ops         = &soc_camera_ioctl_ops;
1524         vdev->release           = video_device_release;
1525         vdev->tvnorms           = V4L2_STD_UNKNOWN;
1526         vdev->ctrl_handler      = &icd->ctrl_handler;
1527         vdev->lock              = &ici->host_lock;
1528
1529         icd->vdev = vdev;
1530
1531         return 0;
1532 }
1533
1534 /*
1535  * Called from soc_camera_probe() above with .host_lock held
1536  */
1537 static int soc_camera_video_start(struct soc_camera_device *icd)
1538 {
1539         const struct device_type *type = icd->vdev->dev.type;
1540         int ret;
1541
1542         if (!icd->parent)
1543                 return -ENODEV;
1544
1545         ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
1546         if (ret < 0) {
1547                 dev_err(icd->pdev, "video_register_device failed: %d\n", ret);
1548                 return ret;
1549         }
1550
1551         /* Restore device type, possibly set by the subdevice driver */
1552         icd->vdev->dev.type = type;
1553
1554         return 0;
1555 }
1556
1557 static int soc_camera_pdrv_probe(struct platform_device *pdev)
1558 {
1559         struct soc_camera_desc *sdesc = pdev->dev.platform_data;
1560         struct soc_camera_subdev_desc *ssdd = &sdesc->subdev_desc;
1561         struct soc_camera_device *icd;
1562         int ret;
1563
1564         if (!sdesc)
1565                 return -EINVAL;
1566
1567         icd = devm_kzalloc(&pdev->dev, sizeof(*icd), GFP_KERNEL);
1568         if (!icd)
1569                 return -ENOMEM;
1570
1571         ret = devm_regulator_bulk_get(&pdev->dev, ssdd->num_regulators,
1572                                       ssdd->regulators);
1573         if (ret < 0)
1574                 return ret;
1575
1576         icd->iface = sdesc->host_desc.bus_id;
1577         icd->sdesc = sdesc;
1578         icd->pdev = &pdev->dev;
1579         platform_set_drvdata(pdev, icd);
1580
1581         icd->user_width         = DEFAULT_WIDTH;
1582         icd->user_height        = DEFAULT_HEIGHT;
1583
1584         return soc_camera_device_register(icd);
1585 }
1586
1587 /*
1588  * Only called on rmmod for each platform device, since they are not
1589  * hot-pluggable. Now we know, that all our users - hosts and devices have
1590  * been unloaded already
1591  */
1592 static int soc_camera_pdrv_remove(struct platform_device *pdev)
1593 {
1594         struct soc_camera_device *icd = platform_get_drvdata(pdev);
1595
1596         if (!icd)
1597                 return -EINVAL;
1598
1599         list_del(&icd->list);
1600
1601         return 0;
1602 }
1603
1604 static struct platform_driver __refdata soc_camera_pdrv = {
1605         .probe = soc_camera_pdrv_probe,
1606         .remove  = soc_camera_pdrv_remove,
1607         .driver  = {
1608                 .name   = "soc-camera-pdrv",
1609                 .owner  = THIS_MODULE,
1610         },
1611 };
1612
1613 module_platform_driver(soc_camera_pdrv);
1614
1615 MODULE_DESCRIPTION("Image capture bus driver");
1616 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1617 MODULE_LICENSE("GPL");
1618 MODULE_ALIAS("platform:soc-camera-pdrv");