19 #include <linux/device.h>
21 #include <linux/i2c.h>
23 #include <linux/list.h>
25 #include <linux/module.h>
28 #include <linux/slab.h>
41 #define DEFAULT_WIDTH 640
42 #define DEFAULT_HEIGHT 480
44 #define is_streaming(ici, icd) \
45 (((ici)->ops->init_videobuf) ? \
46 (icd)->vb_vidq.streaming : \
47 vb2_is_streaming(&(icd)->vb2_vidq))
58 dev_err(dev,
"Cannot enable regulators\n");
63 ret = icl->
power(dev, 1);
66 "Platform failed to power-on the camera.\n");
82 err = icl->
power(dev, 0);
85 "Platform failed to power-off the camera.\n");
93 dev_err(dev,
"Cannot disable regulators\n");
115 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
171 #define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
178 const struct soc_camera_format_xlate *
xlate;
191 ret = ici->
ops->try_fmt(icd, f);
215 static int soc_camera_try_fmt_vid_cap(
struct file *
file,
void *
priv,
227 return soc_camera_try_fmt(icd, f);
230 static int soc_camera_enum_input(
struct file *file,
void *priv,
244 static int soc_camera_g_input(
struct file *file,
void *priv,
unsigned int *
i)
251 static int soc_camera_s_input(
struct file *file,
void *priv,
unsigned int i)
259 static int soc_camera_s_std(
struct file *file,
void *priv,
v4l2_std_id *
a)
262 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
267 static int soc_camera_g_std(
struct file *file,
void *priv,
v4l2_std_id *
a)
270 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
275 static int soc_camera_enum_framesizes(
struct file *file,
void *fh,
281 return ici->
ops->enum_framesizes(icd, fsize);
284 static int soc_camera_reqbufs(
struct file *file,
void *priv,
296 if (ici->
ops->init_videobuf) {
301 ret = ici->
ops->reqbufs(icd, p);
312 static int soc_camera_querybuf(
struct file *file,
void *priv,
320 if (ici->
ops->init_videobuf)
326 static int soc_camera_qbuf(
struct file *file,
void *priv,
337 if (ici->
ops->init_videobuf)
343 static int soc_camera_dqbuf(
struct file *file,
void *priv,
354 if (ici->
ops->init_videobuf)
360 static int soc_camera_create_bufs(
struct file *file,
void *priv,
367 if (ici->
ops->init_videobuf)
373 static int soc_camera_prepare_buf(
struct file *file,
void *priv,
380 if (ici->
ops->init_videobuf)
389 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
391 unsigned int i, fmts = 0, raw_fmts = 0;
398 if (!ici->
ops->get_formats)
409 for (i = 0; i < raw_fmts; i++) {
410 ret = ici->
ops->get_formats(icd, i,
NULL);
420 vmalloc(fmts *
sizeof(
struct soc_camera_format_xlate));
424 dev_dbg(icd->
pdev,
"Found %d supported formats.\n", fmts);
428 for (i = 0; i < raw_fmts; i++)
429 if (!ici->
ops->get_formats) {
436 ret = ici->
ops->get_formats(icd, i,
458 if (ici->
ops->put_formats)
459 ici->
ops->put_formats(icd);
478 ret = soc_camera_try_fmt(icd, f);
482 ret = ici->
ops->set_fmt(icd, f);
488 "Host driver hasn't set up current format correctly!\n");
498 if (ici->
ops->init_videobuf)
505 return ici->
ops->set_bus_param(icd);
508 static int soc_camera_open(
struct file *file)
516 if (!to_soc_camera_control(icd))
520 ici = to_soc_camera_host(icd->
parent);
524 if (!try_module_get(ici->
ops->owner)) {
525 dev_err(icd->
pdev,
"Couldn't lock capture bus driver.\n");
553 ret = ici->
ops->add(icd);
556 dev_err(icd->
pdev,
"Couldn't activate the camera: %d\n", ret);
560 ret = __soc_camera_power_on(icd);
565 ret = pm_runtime_resume(&icd->
vdev->dev);
566 if (ret < 0 && ret != -
ENOSYS)
575 ret = soc_camera_set_fmt(icd, &f);
579 if (ici->
ops->init_videobuf) {
582 ret = ici->
ops->init_videobuf2(&icd->
vb2_vidq, icd);
601 pm_runtime_disable(&icd->
vdev->dev);
603 __soc_camera_power_off(icd);
605 ici->
ops->remove(icd);
608 module_put(ici->
ops->owner);
615 static int soc_camera_close(
struct file *file)
623 pm_runtime_suspend(&icd->
vdev->dev);
624 pm_runtime_disable(&icd->
vdev->dev);
626 if (ici->
ops->init_videobuf2)
628 ici->
ops->remove(icd);
630 __soc_camera_power_off(icd);
637 module_put(ici->
ops->owner);
644 static ssize_t soc_camera_read(
struct file *file,
char __user *
buf,
645 size_t count, loff_t *ppos)
650 dev_err(icd->
pdev,
"camera device read not implemented\n");
655 static int soc_camera_mmap(
struct file *file,
struct vm_area_struct *vma)
661 dev_dbg(icd->
pdev,
"mmap called, vma=0x%08lx\n", (
unsigned long)vma);
668 if (ici->
ops->init_videobuf)
674 dev_dbg(icd->
pdev,
"vma start=0x%08lx, size=%ld, ret=%d\n",
682 static unsigned int soc_camera_poll(
struct file *file,
poll_table *pt)
692 if (ici->
ops->init_videobuf && list_empty(&icd->
vb_vidq.stream))
693 dev_err(icd->
pdev,
"Trying to poll with no queued buffers!\n");
695 res = ici->
ops->poll(file, pt);
716 .open = soc_camera_open,
717 .release = soc_camera_close,
719 .read = soc_camera_read,
720 .mmap = soc_camera_mmap,
721 .poll = soc_camera_poll,
724 static int soc_camera_s_fmt_vid_cap(
struct file *file,
void *priv,
741 dev_err(icd->
pdev,
"S_FMT denied: queue initialised\n");
745 ret = soc_camera_set_fmt(icd, f);
753 static int soc_camera_enum_fmt_vid_cap(
struct file *file,
void *priv,
772 static int soc_camera_g_fmt_vid_cap(
struct file *file,
void *priv,
790 dev_dbg(icd->
pdev,
"current_fmt->fourcc: 0x%08x\n",
795 static int soc_camera_querycap(
struct file *file,
void *priv,
804 return ici->
ops->querycap(ici, cap);
807 static int soc_camera_streamon(
struct file *file,
void *priv,
812 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
824 if (ici->
ops->init_videobuf)
835 static int soc_camera_streamoff(
struct file *file,
void *priv,
839 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
854 if (ici->
ops->init_videobuf)
864 static int soc_camera_cropcap(
struct file *file,
void *fh,
870 return ici->
ops->cropcap(icd, a);
873 static int soc_camera_g_crop(
struct file *file,
void *fh,
880 ret = ici->
ops->get_crop(icd, a);
890 static int soc_camera_s_crop(
struct file *file,
void *fh,
906 ret = ici->
ops->get_crop(icd, ¤t_crop);
911 "S_CROP denied: getting current crop failed\n");
912 }
else if ((a->
c.width == current_crop.c.width &&
913 a->
c.height == current_crop.c.height) ||
916 ret = ici->
ops->set_crop(icd, a);
917 }
else if (ici->
ops->set_livecrop) {
918 ret = ici->
ops->set_livecrop(icd, a);
921 "S_CROP denied: queue initialised and sizes differ\n");
928 static int soc_camera_g_selection(
struct file *file,
void *fh,
938 if (!ici->
ops->get_selection)
941 return ici->
ops->get_selection(icd, s);
944 static int soc_camera_s_selection(
struct file *file,
void *fh,
972 if (!ici->
ops->set_selection)
975 ret = ici->
ops->set_selection(icd, s);
987 static int soc_camera_g_parm(
struct file *file,
void *fh,
993 if (ici->
ops->get_parm)
994 return ici->
ops->get_parm(icd, a);
999 static int soc_camera_s_parm(
struct file *file,
void *fh,
1005 if (ici->
ops->set_parm)
1006 return ici->
ops->set_parm(icd, a);
1011 static int soc_camera_g_chip_ident(
struct file *file,
void *fh,
1015 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1020 #ifdef CONFIG_VIDEO_ADV_DEBUG
1021 static int soc_camera_g_register(
struct file *file,
void *fh,
1025 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1030 static int soc_camera_s_register(
struct file *file,
void *fh,
1034 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1054 ret = soc_camera_probe(icd);
1061 #ifdef CONFIG_I2C_BOARDINFO
1071 dev_err(icd->
pdev,
"Cannot get I2C adapter #%d. No driver?\n",
1083 client = v4l2_get_subdevdata(subdev);
1107 #define soc_camera_init_i2c(icd, icl) (-ENODEV)
1108 #define soc_camera_free_i2c(icd) do {} while (0)
1120 struct v4l2_mbus_framefmt mf;
1145 ret = ici->
ops->add(icd);
1150 ret = video_dev_create(icd);
1174 control = to_soc_camera_control(icd);
1176 !try_module_get(control->
driver->owner)) {
1183 sd = soc_camera_to_subdev(icd);
1184 sd->
grp_id = soc_camera_grp_id(icd);
1185 v4l2_set_subdev_hostdata(sd, icd);
1192 ret = soc_camera_init_user_formats(icd);
1205 ret = soc_camera_video_start(icd);
1214 icd->
field = mf.field;
1217 ici->
ops->remove(icd);
1225 soc_camera_free_user_formats(icd);
1232 module_put(control->
driver->owner);
1239 ici->
ops->remove(icd);
1267 struct device_driver *drv = to_soc_camera_control(icd)->driver;
1270 module_put(drv->
owner);
1273 soc_camera_free_user_formats(icd);
1283 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1289 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1295 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1302 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1309 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1317 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1318 const struct soc_camera_format_xlate *
xlate;
1332 *fsize = fsize_mbus;
1343 if (!ici || !ici->
ops ||
1344 !ici->
ops->try_fmt ||
1345 !ici->
ops->set_fmt ||
1346 !ici->
ops->set_bus_param ||
1347 !ici->
ops->querycap ||
1348 ((!ici->
ops->init_videobuf ||
1349 !ici->
ops->reqbufs) &&
1350 !ici->
ops->init_videobuf2) ||
1352 !ici->
ops->remove ||
1357 if (!ici->
ops->set_crop)
1358 ici->
ops->set_crop = default_s_crop;
1359 if (!ici->
ops->get_crop)
1360 ici->
ops->get_crop = default_g_crop;
1361 if (!ici->
ops->cropcap)
1362 ici->
ops->cropcap = default_cropcap;
1363 if (!ici->
ops->set_parm)
1364 ici->
ops->set_parm = default_s_parm;
1365 if (!ici->
ops->get_parm)
1366 ici->
ops->get_parm = default_g_parm;
1367 if (!ici->
ops->enum_framesizes)
1368 ici->
ops->enum_framesizes = default_enum_framesizes;
1372 if (ix->
nr == ici->
nr) {
1405 if (icd->
iface == ici->
nr && to_soc_camera_control(icd))
1406 soc_camera_remove(icd);
1420 for (i = 0; i < 256 && num < 0; i++) {
1449 .vidioc_querycap = soc_camera_querycap,
1450 .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
1451 .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
1452 .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
1453 .vidioc_enum_fmt_vid_cap = soc_camera_enum_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_g_std = soc_camera_g_std,
1459 .vidioc_enum_framesizes = soc_camera_enum_framesizes,
1460 .vidioc_reqbufs = soc_camera_reqbufs,
1461 .vidioc_querybuf = soc_camera_querybuf,
1462 .vidioc_qbuf = soc_camera_qbuf,
1463 .vidioc_dqbuf = soc_camera_dqbuf,
1464 .vidioc_create_bufs = soc_camera_create_bufs,
1465 .vidioc_prepare_buf = soc_camera_prepare_buf,
1466 .vidioc_streamon = soc_camera_streamon,
1467 .vidioc_streamoff = soc_camera_streamoff,
1468 .vidioc_cropcap = soc_camera_cropcap,
1469 .vidioc_g_crop = soc_camera_g_crop,
1470 .vidioc_s_crop = soc_camera_s_crop,
1471 .vidioc_g_selection = soc_camera_g_selection,
1472 .vidioc_s_selection = soc_camera_s_selection,
1473 .vidioc_g_parm = soc_camera_g_parm,
1474 .vidioc_s_parm = soc_camera_s_parm,
1475 .vidioc_g_chip_ident = soc_camera_g_chip_ident,
1476 #ifdef CONFIG_VIDEO_ADV_DEBUG
1477 .vidioc_g_register = soc_camera_g_register,
1478 .vidioc_s_register = soc_camera_s_register,
1494 vdev->
fops = &soc_camera_fops;
1495 vdev->
ioctl_ops = &soc_camera_ioctl_ops;
1519 dev_err(icd->
pdev,
"video_register_device failed: %d\n", ret);
1544 platform_set_drvdata(pdev, icd);
1549 return soc_camera_device_register(icd);
1570 .probe = soc_camera_pdrv_probe,
1573 .name =
"soc-camera-pdrv",