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