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