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