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