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