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