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