S5PV210 FIMC驱动和v4l2驱动框架学习



FIMC的驱动在内核中的位置在drivers/media/video/samsung/fimc目录下,其中包含的文件有如下所示:

        fimc40_regs.c
        fimc43_regs.c
        fimc_capture.c
        fimc_dev.c
        fimc_output.c
        fimc_overlay.c
        fimc_v4l2.c


首先看一下drivers/media/video/samsung/fimc/目录下的fimc_dev.c文件,在这里fimc_driver是一个platform_driver驱动,这里面注册了一个platform_driver,这种SOC上的控制器一般都会挂接在platform_bus上以实现在系统初始化时的device和driver的匹配,具体如下所示:

  1. static struct platform_driver fimc_driver = {  
  2.     .probe      = fimc_probe,  
  3.     .remove     = fimc_remove,  
  4.     .suspend    = fimc_suspend,  
  5.     .resume     = fimc_resume,  
  6.     .driver     = {  
  7.         .name   = FIMC_NAME,  
  8.         .owner  = THIS_MODULE,  
  9.     },  
  10. };  
static struct platform_driver fimc_driver = {
	.probe		= fimc_probe,
	.remove		= fimc_remove,
	.suspend	= fimc_suspend,
	.resume		= fimc_resume,
	.driver		= {
		.name	= FIMC_NAME,
		.owner	= THIS_MODULE,
	},
};
在fimc_probe函数中主要完成一些初始化注册功能,在我们的设备上有三套FIMC控制器(fimc0, fimc1,fimc2),在驱动中通过一个数组来描述这三个fimc video设备,如下所示:
  1. struct video_device fimc_video_device[FIMC_DEVICES] = {  
  2.     [0] = {  
  3.         .fops = &fimc_fops,  
  4.         .ioctl_ops = &fimc_v4l2_ops,  
  5.         .release = fimc_vdev_release,  
  6.     },  
  7.     [1] = {  
  8.         .fops = &fimc_fops,  
  9.         .ioctl_ops = &fimc_v4l2_ops,  
  10.         .release = fimc_vdev_release,  
  11.     },  
  12.     [2] = {  
  13.         .fops = &fimc_fops,  
  14.         .ioctl_ops = &fimc_v4l2_ops,  
  15.         .release = fimc_vdev_release,  
  16.     },  
  17. }; 
struct video_device fimc_video_device[FIMC_DEVICES] = {
	[0] = {
		.fops = &fimc_fops,
		.ioctl_ops = &fimc_v4l2_ops,
		.release = fimc_vdev_release,
	},
	[1] = {
		.fops = &fimc_fops,
		.ioctl_ops = &fimc_v4l2_ops,
		.release = fimc_vdev_release,
	},
	[2] = {
		.fops = &fimc_fops,
		.ioctl_ops = &fimc_v4l2_ops,
		.release = fimc_vdev_release,
	},
};


video_device的定义如下所示:
  1. struct video_device  
  2. {  
  3.     /* device ops */  
  4.     const struct v4l2_file_operations *fops;  
  5.   
  6.     /* sysfs */  
  7.     struct device dev;      /* v4l device */  
  8.     struct cdev *cdev;      /* character device */  
  9.   
  10.     /* Set either parent or v4l2_dev if your driver uses v4l2_device */  
  11.     struct device *parent;      /* device parent */  
  12.     struct v4l2_device *v4l2_dev;   /* v4l2_device parent */  
  13.   
  14.     /* device info */  
  15.     char name[32];  
  16.     int vfl_type;  
  17.     /* 'minor' is set to -1 if the registration failed */  
  18.     int minor;  
  19.     u16 num;  
  20.     /* use bitops to set/clear/test flags */  
  21.     unsigned long flags;  
  22.     /* attribute to differentiate multiple indices on one physical device */  
  23.     int index;  
  24.   
  25.     /* V4L2 file handles */  
  26.     spinlock_t      fh_lock; /* Lock for all v4l2_fhs */  
  27.     struct list_head    fh_list; /* List of struct v4l2_fh */  
  28.   
  29.     int debug;          /* Activates debug level*/  
  30.   
  31.     /* Video standard vars */  
  32.     v4l2_std_id tvnorms;        /* Supported tv norms */  
  33.     v4l2_std_id current_norm;   /* Current tvnorm */  
  34.   
  35.     /* callbacks */  
  36.     void (*release)(struct video_device *vdev);  
  37.   
  38.     /* ioctl callbacks */  
  39.     const struct v4l2_ioctl_ops *ioctl_ops;  
  40. };  
struct video_device
{
	/* device ops */
	const struct v4l2_file_operations *fops;

	/* sysfs */
	struct device dev;		/* v4l device */
	struct cdev *cdev;		/* character device */

	/* Set either parent or v4l2_dev if your driver uses v4l2_device */
	struct device *parent;		/* device parent */
	struct v4l2_device *v4l2_dev;	/* v4l2_device parent */

	/* device info */
	char name[32];
	int vfl_type;
	/* 'minor' is set to -1 if the registration failed */
	int minor;
	u16 num;
	/* use bitops to set/clear/test flags */
	unsigned long flags;
	/* attribute to differentiate multiple indices on one physical device */
	int index;

	/* V4L2 file handles */
	spinlock_t		fh_lock; /* Lock for all v4l2_fhs */
	struct list_head	fh_list; /* List of struct v4l2_fh */

	int debug;			/* Activates debug level*/

	/* Video standard vars */
	v4l2_std_id tvnorms;		/* Supported tv norms */
	v4l2_std_id current_norm;	/* Current tvnorm */

	/* callbacks */
	void (*release)(struct video_device *vdev);

	/* ioctl callbacks */
	const struct v4l2_ioctl_ops *ioctl_ops;
};

上面所示,video device是一个字符型的设备(struct cdev *cdev;),这个字符驱动会在执行video_register_device函数时注册
  1. int video_register_device(struct video_device *vdev, int type, int nr)  
  2. {  
  3.     return __video_register_device(vdev, type, nr, 1);  
  4. }  
int video_register_device(struct video_device *vdev, int type, int nr)
{
	return __video_register_device(vdev, type, nr, 1);
}

video device真正的注册在__video_register_device函数中,在__video_register_device这个函数中会初始化并注册一个字符设备驱动,代码段如下所示如下所示:
  1. /* Part 3: Initialize the character device */  
  2. vdev->cdev = cdev_alloc();  
  3. if (vdev->cdev == NULL) {  
  4.     ret = -ENOMEM;  
  5.     goto cleanup;  
  6. }  
  7. if (vdev->fops->unlocked_ioctl)  
  8.     <span style="color: rgb(255, 0, 0);">vdev->cdev->ops = &v4l2_unlocked_fops;</span>  
  9. else  
  10.     <span style="color: rgb(255, 0, 0);">vdev->cdev->ops = &v4l2_fops;</span>  
  11. vdev->cdev->owner = vdev->fops->owner;  
  12. ret = cdev_add(vdev->cdev, MKDEV(VIDEO_MAJOR, vdev->minor), 1);  
  13. if (ret < 0) {  
  14.     printk(KERN_ERR "%s: cdev_add failed\n", __func__);  
  15.     kfree(vdev->cdev);  
  16.     vdev->cdev = NULL;  
  17.     goto cleanup;  
  18. }  
  19.   
  20. /* Part 4: register the device with sysfs */  
  21. memset(&vdev->dev, 0, sizeof(vdev->dev));  
  22. /* The memset above cleared the device's drvdata, so 
  23.     put back the copy we made earlier. */  
  24. video_set_drvdata(vdev, priv);  
  25. vdev->dev.class = &video_class;  
  26. vdev->dev.devt = MKDEV(VIDEO_MAJOR, vdev->minor);  
  27. if (vdev->parent)  
  28.     vdev->dev.parent = vdev->parent;  
  29. dev_set_name(&vdev->dev, "%s%d", name_base, vdev->num);  
  30. ret = device_register(&vdev->dev);  
  31. if (ret < 0) {  
  32.     printk(KERN_ERR "%s: device_register failed\n", __func__);  
  33.     goto cleanup;  
  34. }  
/* Part 3: Initialize the character device */
vdev->cdev = cdev_alloc();
if (vdev->cdev == NULL) {
	ret = -ENOMEM;
	goto cleanup;
}
if (vdev->fops->unlocked_ioctl)
	vdev->cdev->ops = &v4l2_unlocked_fops;
else
	vdev->cdev->ops = &v4l2_fops;
vdev->cdev->owner = vdev->fops->owner;
ret = cdev_add(vdev->cdev, MKDEV(VIDEO_MAJOR, vdev->minor), 1);
if (ret < 0) {
	printk(KERN_ERR "%s: cdev_add failed\n", __func__);
	kfree(vdev->cdev);
	vdev->cdev = NULL;
	goto cleanup;
}

/* Part 4: register the device with sysfs */
memset(&vdev->dev, 0, sizeof(vdev->dev));
/* The memset above cleared the device's drvdata, so
	put back the copy we made earlier. */
video_set_drvdata(vdev, priv);
vdev->dev.class = &video_class;
vdev->dev.devt = MKDEV(VIDEO_MAJOR, vdev->minor);
if (vdev->parent)
	vdev->dev.parent = vdev->parent;
dev_set_name(&vdev->dev, "%s%d", name_base, vdev->num);
ret = device_register(&vdev->dev);
if (ret < 0) {
	printk(KERN_ERR "%s: device_register failed\n", __func__);
	goto cleanup;
}
红颜色所指的地方是指定操作函数接口,定义在v4l2_dev.c文件中,具体定义如下所示:
  1. static const struct file_operations v4l2_unlocked_fops = {  
  2.     .owner = THIS_MODULE,  
  3.     .read = v4l2_read,  
  4.     .write = v4l2_write,  
  5.     .open = v4l2_open,  
  6.     .get_unmapped_area = v4l2_get_unmapped_area,  
  7.     .mmap = v4l2_mmap,  
  8.     .unlocked_ioctl = v4l2_unlocked_ioctl,  
  9. #ifdef CONFIG_COMPAT  
  10.     .compat_ioctl = v4l2_compat_ioctl32,  
  11. #endif  
  12.     .release = v4l2_release,  
  13.     .poll = v4l2_poll,  
  14.     .llseek = no_llseek,  
  15. };  
  16.   
  17. static const struct file_operations v4l2_fops = {  
  18.     .owner = THIS_MODULE,  
  19.     .read = v4l2_read,  
  20.     .write = v4l2_write,  
  21.     .open = v4l2_open,  
  22.     .get_unmapped_area = v4l2_get_unmapped_area,  
  23.     .mmap = v4l2_mmap,  
  24.     .ioctl = v4l2_ioctl,  
  25. #ifdef CONFIG_COMPAT  
  26.     .compat_ioctl = v4l2_compat_ioctl32,  
  27. #endif  
  28.     .release = v4l2_release,  
  29.     .poll = v4l2_poll,  
  30.     .llseek = no_llseek,  
  31. };  
static const struct file_operations v4l2_unlocked_fops = {
	.owner = THIS_MODULE,
	.read = v4l2_read,
	.write = v4l2_write,
	.open = v4l2_open,
	.get_unmapped_area = v4l2_get_unmapped_area,
	.mmap = v4l2_mmap,
	.unlocked_ioctl = v4l2_unlocked_ioctl,
#ifdef CONFIG_COMPAT
	.compat_ioctl = v4l2_compat_ioctl32,
#endif
	.release = v4l2_release,
	.poll = v4l2_poll,
	.llseek = no_llseek,
};

static const struct file_operations v4l2_fops = {
	.owner = THIS_MODULE,
	.read = v4l2_read,
	.write = v4l2_write,
	.open = v4l2_open,
	.get_unmapped_area = v4l2_get_unmapped_area,
	.mmap = v4l2_mmap,
	.ioctl = v4l2_ioctl,
#ifdef CONFIG_COMPAT
	.compat_ioctl = v4l2_compat_ioctl32,
#endif
	.release = v4l2_release,
	.poll = v4l2_poll,
	.llseek = no_llseek,
};

在v4l2_dev.c中实现了v4l2_open、v4l2_read,v4l2_write、v4l2_ioctl等,奇妙的是在这些实现函数中会调用video的ops结构体重的操作函数,如在v4l2_ioctl等函数中中会调用一句
  1. //v4l2_ioctl中  
  2. return vdev->fops->ioctl(filp, cmd, arg);  
  3. //v4l2_open中  
  4. ret = vdev->fops->open(filp);  
  5. //v4l2_read中  
  6. return vdev->fops->read(filp, buf, sz, off);  
  7. //v4l2_write中  
  8. return vdev->fops->write(filp, buf, sz, off);  
  9. //v4l2_poll中  
  10. return vdev->fops->poll(filp, poll);  
  11. //v4l2_mmap中  
  12. return vdev->fops->mmap(filp, vm);  
//v4l2_ioctl中
return vdev->fops->ioctl(filp, cmd, arg);
//v4l2_open中
ret = vdev->fops->open(filp);
//v4l2_read中
return vdev->fops->read(filp, buf, sz, off);
//v4l2_write中
return vdev->fops->write(filp, buf, sz, off);
//v4l2_poll中
return vdev->fops->poll(filp, poll);
//v4l2_mmap中
return vdev->fops->mmap(filp, vm);

在这里每一个函数中面会调用vdev中的fops所指对应的操作函数,这里的vdev其实就是在video_register_device函数注册时的那个video_device,即上面所指的fimc_video_device[id]数组的其中一个video_device,在这些操作函数中这个设备可以通过如下的操作获取,然后操作,struct video_device *vdev = video_devdata(filp);。如上面fimc_video_device数组定义初始化时所示
  1. struct video_device fimc_video_device[FIMC_DEVICES] = {  
  2.     [0] = {  
  3.         .fops = &fimc_fops,  
  4.         .ioctl_ops = &fimc_v4l2_ops,  
  5.         .release = fimc_vdev_release,  
  6.     },  
  7.     .............  
  8. };  
struct video_device fimc_video_device[FIMC_DEVICES] = {
	[0] = {
		.fops = &fimc_fops,
		.ioctl_ops = &fimc_v4l2_ops,
		.release = fimc_vdev_release,
	},
	.............
};

这样在应用层操作read、write、ioctl等函数时在底层首先调用v4l2_fops中的操作函数,在v4l2_fops的操作函数中会调用video_device中的fops所对应的操作函数,即在我们这里会调用上面代码中的fimc_fops中的操作函数。


下面我们回过头来看看fimc驱动中fimc_fops相关的内容
在probe函数中会调用fimc_register_controller函数,在fimc_register_controller中指定要注册的video device,通过
  1. ctrl->vd = &fimc_video_device[id];  
ctrl->vd = &fimc_video_device[id];
其中的id是就是我们要注册的video device的编号一般为0、1、2,然后在fimc_register_controller函数中获取一些资源等。我们知道在fimc_video_device中定义了三个video_device设备,其中每一个的fops指向了fimc_fops,ioctl_ops指向fimc_v4l2_ops,那么我们看看这个fimc_fops和fimc_v4l_ops是怎么定义和使用的。
fimc_fops的定义(fimc_dev.c)如下所示:
  1. static const struct v4l2_file_operations fimc_fops = {  
  2.     .owner      = THIS_MODULE,  
  3.     .open       = fimc_open,  
  4.     .release    = fimc_release,  
  5.     .ioctl      = video_ioctl2,  
  6.     .read       = fimc_read,  
  7.     .write      = fimc_write,  
  8.     .mmap       = fimc_mmap,  
  9.     .poll       = fimc_poll,  
  10. };  
static const struct v4l2_file_operations fimc_fops = {
	.owner		= THIS_MODULE,
	.open		= fimc_open,
	.release	= fimc_release,
	.ioctl		= video_ioctl2,
	.read		= fimc_read,
	.write		= fimc_write,
	.mmap		= fimc_mmap,
	.poll		= fimc_poll,
};
fimc_v4l2_fops的定义(fimc_v4l2.c)如下所示:
  1. const struct v4l2_ioctl_ops fimc_v4l2_ops = {  
  2.     .vidioc_querycap        = fimc_querycap,  
  3.     .vidioc_reqbufs         = fimc_reqbufs,  
  4.     .vidioc_querybuf        = fimc_querybuf,  
  5.     .vidioc_g_ctrl          = fimc_g_ctrl,  
  6.     .vidioc_s_ctrl          = fimc_s_ctrl,  
  7.     .vidioc_s_ext_ctrls     = fimc_s_ext_ctrls,  
  8.     .vidioc_cropcap         = fimc_cropcap,  
  9.     .vidioc_g_crop          = fimc_g_crop,  
  10.     .vidioc_s_crop          = fimc_s_crop,  
  11.     .vidioc_streamon        = fimc_streamon,  
  12.     .vidioc_streamoff       = fimc_streamoff,  
  13.     .vidioc_qbuf            = fimc_qbuf,  
  14.     .vidioc_dqbuf           = fimc_dqbuf,  
  15.     .vidioc_enum_fmt_vid_cap    = fimc_enum_fmt_vid_capture,  
  16.     .vidioc_g_fmt_vid_cap       = fimc_g_fmt_vid_capture,  
  17.     .vidioc_s_fmt_vid_cap       = fimc_s_fmt_vid_capture,  
  18.     .vidioc_try_fmt_vid_cap     = fimc_try_fmt_vid_capture,  
  19.     .vidioc_enum_input      = fimc_enum_input,  
  20.     .vidioc_g_input         = fimc_g_input,  
  21.     .vidioc_s_input         = fimc_s_input,  
  22.     .vidioc_g_parm          = fimc_g_parm,  
  23.     .vidioc_s_parm          = fimc_s_parm,  
  24.     .vidioc_queryctrl       = fimc_queryctrl,  
  25.     .vidioc_querymenu       = fimc_querymenu,  
  26.     .vidioc_g_fmt_vid_out       = fimc_g_fmt_vid_out,  
  27.     .vidioc_s_fmt_vid_out       = fimc_s_fmt_vid_out,  
  28.     .vidioc_try_fmt_vid_out     = fimc_try_fmt_vid_out,  
  29.     .vidioc_g_fbuf          = fimc_g_fbuf,  
  30.     .vidioc_s_fbuf          = fimc_s_fbuf,  
  31.     .vidioc_try_fmt_vid_overlay = fimc_try_fmt_overlay,  
  32.     .vidioc_g_fmt_vid_overlay   = fimc_g_fmt_vid_overlay,  
  33.     .vidioc_s_fmt_vid_overlay   = fimc_s_fmt_vid_overlay,  
  34. };
const struct v4l2_ioctl_ops fimc_v4l2_ops = {
	.vidioc_querycap		= fimc_querycap,
	.vidioc_reqbufs			= fimc_reqbufs,
	.vidioc_querybuf		= fimc_querybuf,
	.vidioc_g_ctrl			= fimc_g_ctrl,
	.vidioc_s_ctrl			= fimc_s_ctrl,
	.vidioc_s_ext_ctrls		= fimc_s_ext_ctrls,
	.vidioc_cropcap			= fimc_cropcap,
	.vidioc_g_crop			= fimc_g_crop,
	.vidioc_s_crop			= fimc_s_crop,
	.vidioc_streamon		= fimc_streamon,
	.vidioc_streamoff		= fimc_streamoff,
	.vidioc_qbuf			= fimc_qbuf,
	.vidioc_dqbuf			= fimc_dqbuf,
	.vidioc_enum_fmt_vid_cap	= fimc_enum_fmt_vid_capture,
	.vidioc_g_fmt_vid_cap		= fimc_g_fmt_vid_capture,
	.vidioc_s_fmt_vid_cap		= fimc_s_fmt_vid_capture,
	.vidioc_try_fmt_vid_cap		= fimc_try_fmt_vid_capture,
	.vidioc_enum_input		= fimc_enum_input,
	.vidioc_g_input			= fimc_g_input,
	.vidioc_s_input			= fimc_s_input,
	.vidioc_g_parm			= fimc_g_parm,
	.vidioc_s_parm			= fimc_s_parm,
	.vidioc_queryctrl		= fimc_queryctrl,
	.vidioc_querymenu		= fimc_querymenu,
	.vidioc_g_fmt_vid_out		= fimc_g_fmt_vid_out,
	.vidioc_s_fmt_vid_out		= fimc_s_fmt_vid_out,
	.vidioc_try_fmt_vid_out		= fimc_try_fmt_vid_out,
	.vidioc_g_fbuf			= fimc_g_fbuf,
	.vidioc_s_fbuf			= fimc_s_fbuf,
	.vidioc_try_fmt_vid_overlay	= fimc_try_fmt_overlay,
	.vidioc_g_fmt_vid_overlay	= fimc_g_fmt_vid_overlay,
	.vidioc_s_fmt_vid_overlay	= fimc_s_fmt_vid_overlay,
};
从上面可以看出,应用层的read、write、mmap、poll等都会在这fimc_fops所指的函数中实现,但是这个video_ioctl2和其他的不同,下面会详细说明这个video_ioctl2是怎么回事。这样看来fimc_fops的定义使用是很好理解的,但是fimc_v4l2_fops是如何如何使用的那?那它里面的内容又是怎么回事那?
在fimc_fops中怎么看video_ioctl2(在v4l2_ioctl.c文件中)这么怪异,仔细一看,原来video_ioctl2在fimc_fops中的起到桥梁的作用,将fimc_fops和fimc_v4l2_fops联系起来,在video_ioctl2中有这样一句,如下:
  1. err = __video_do_ioctl(file, cmd, parg);  
err = __video_do_ioctl(file, cmd, parg);

这就是真正的桥梁,在__video_do_ioctl(在v4l2_ioctl.c文件中)函数中
  1. static long __video_do_ioctl(struct file *file,  
  2.         unsigned int cmd, void *arg)  
  3. {  
  4.     struct video_device *vfd = video_devdata(file);  
  5.     const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;  
  6.     //.................  
  7.     //这里面处理cmd  
  8.     switch (cmd) {  
  9.         case VIDIOC_S_PRIORITY:  
  10.         {  
  11.             //....................  
  12.             ret = ops->vidioc_s_priority(file, fh, *p);  
  13.             break;  
  14.         }  
  15.     }  
  16.     //...............................  
  17.   
  18. }  
static long __video_do_ioctl(struct file *file,
		unsigned int cmd, void *arg)
{
	struct video_device *vfd = video_devdata(file);
	const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
	//.................
	//这里面处理cmd
	switch (cmd) {
		case VIDIOC_S_PRIORITY:
		{
			//....................
			ret = ops->vidioc_s_priority(file, fh, *p);
			break;
		}
	}
	//...............................

}

在这里面会调用ops里面的操作函数,而实际上ops就是指向我们前面的fimc_video_device的ioctl_ops,即fimc_v4l2_fops,到这应用层的ioctl所发的指令什么的都会到fimc_v4l_fops里来处理,fimc_v4l2_ops是定义在fimc_v4l2.c文件中。


由fimc_v4l2.c中的fimc_v4l2_ops可以看到,FIMC的驱动实现了v4l2所有的接口,可以分为v4l2-input设备接口,v4l2-output设备接口以及v4l2-overlay设备接口。这里我们主要关注v4l2-input设备接口,因为摄像头属于视频输入设备。
fimc_v4l2.c里面注册了很多的回调函数,都是用于实现v4l2的标准接口的,但是这些回调函数基本上都不是在fimc_v4l2.c里面实现的,而是有相应的.c分别去实现。比如:
v4l2-input设备的操作实现: fimc_capture.c
        v4l2-output设备的操作实现: fimc_output.c
        v4l2-overlay设备的操作实现: fimc_overlay.c
这些代码其实都是和具体硬件操作无关的,这个驱动把所有操作硬件寄存器的代码都写到一个文件里面了,就是fimc40_regs.c。这样把硬件相关的代码和硬件无关的代码分开来实现是非常好的方式,可以最大限度的实现代码复用。
这些驱动源码的组织关系如下:

                                                   
                                     


接下来在probe中调用v4l2_device_register函数注册一个v4l2设备实例,然后调用fimc_init_global函数,在这个函数中完成摄像头的分配以及时钟的获取:这个函数的原型如下:

  1. static int fimc_init_global( struct platform_device *pdev );  
static int fimc_init_global( struct platform_device *pdev );
这个platform_device是从内核平台代码中传的过来的,里面包含的就是和平台相关的信息,其中就包含有摄像头的信息,和平台相关的内容放在后面分析,下面看看这个神奇的fimc_init_global函数,函数的实现如下所示:

  1. static int fimc_init_global(struct platform_device *pdev)  
  2. {  
  3.     struct fimc_control *ctrl;  
  4.     struct s3c_platform_fimc *pdata;  
  5.     //这个结构体就是用来描述一个摄像头的,先不管它里面的内容  
  6.     //等会儿在分析平台代码的时候可以看到它是如何被填充的  
  7.     struct s3c_platform_camera *cam;  
  8.     struct clk *srclk;  
  9.     int id, i;  
  10.   
  11.     //获得平台信息  
  12.     pdata = to_fimc_plat(&pdev->dev);  
  13.     id = pdev->id; //id号可能是0,1,2  
  14.     ctrl = get_fimc_ctrl(id); //获得id号对应的fimc_control结构体指针  
  15.   
  16.     /* Registering external camera modules. re-arrange order to be sure */  
  17.     for (i = 0; i < FIMC_MAXCAMS; i++) {  
  18.         cam = pdata->camera[i]; //从平台数据取得camera的信息  
  19.         if (!cam)  
  20.             continue// change break to continue by ys  
  21.   
  22.         /* WriteBack doesn't need clock setting */  
  23.         if(cam->id == CAMERA_WB) {   
  24.             fimc_dev->camera[cam->id] = cam;  
  25.                 break;  
  26.         }  
  27.   
  28.         // 获得时钟源信息  
  29.         srclk = clk_get(&pdev->dev, cam->srclk_name);  
  30.         if (IS_ERR(srclk)) {  
  31.             fimc_err("%s: failed to get mclk source\n", __func__);  
  32.             return -EINVAL;  
  33.         }  
  34.   
  35.         // 获得camera的时钟信息  
  36.         /* mclk */  
  37.         cam->clk = clk_get(&pdev->dev, cam->clk_name);  
  38.         if (IS_ERR(cam->clk)) {  
  39.             fimc_err("%s: failed to get mclk source\n", __func__);  
  40.             return -EINVAL;  
  41.         }  
  42.   
  43.         if (cam->clk->set_parent) {  
  44.             cam->clk->parent = srclk;  
  45.             cam->clk->set_parent(cam->clk, srclk);  
  46.         }  
  47.   
  48.         /* Assign camera device to fimc */  
  49.         fimc_dev->camera[cam->id] = cam; // 将从平台获得的camera分配给全局数据结构  
  50.                                                                                         // fimc_dev  
  51.     }  
  52.   
  53.     fimc_dev->initialized = 1;  
  54.   
  55.     return 0;  
  56. }  
static int fimc_init_global(struct platform_device *pdev)
{
	struct fimc_control *ctrl;
	struct s3c_platform_fimc *pdata;
	//这个结构体就是用来描述一个摄像头的,先不管它里面的内容
	//等会儿在分析平台代码的时候可以看到它是如何被填充的
	struct s3c_platform_camera *cam;
	struct clk *srclk;
	int id, i;

	//获得平台信息
	pdata = to_fimc_plat(&pdev->dev);
	id = pdev->id; //id号可能是0,1,2
	ctrl = get_fimc_ctrl(id); //获得id号对应的fimc_control结构体指针

	/* Registering external camera modules. re-arrange order to be sure */
	for (i = 0; i < FIMC_MAXCAMS; i++) {
		cam = pdata->camera[i]; //从平台数据取得camera的信息
		if (!cam)
			continue; // change break to continue by ys

		/* WriteBack doesn't need clock setting */
		if(cam->id == CAMERA_WB) { 
			fimc_dev->camera[cam->id] = cam;
				break;
		}

		// 获得时钟源信息
		srclk = clk_get(&pdev->dev, cam->srclk_name);
		if (IS_ERR(srclk)) {
			fimc_err("%s: failed to get mclk source\n", __func__);
			return -EINVAL;
		}

		// 获得camera的时钟信息
		/* mclk */
		cam->clk = clk_get(&pdev->dev, cam->clk_name);
		if (IS_ERR(cam->clk)) {
			fimc_err("%s: failed to get mclk source\n", __func__);
			return -EINVAL;
		}

		if (cam->clk->set_parent) {
			cam->clk->parent = srclk;
			cam->clk->set_parent(cam->clk, srclk);
		}

		/* Assign camera device to fimc */
		fimc_dev->camera[cam->id] = cam; // 将从平台获得的camera分配给全局数据结构
                                                                                        // fimc_dev
	}

	fimc_dev->initialized = 1;

	return 0;
}
可以看到这个函数实际上就是把平台数据中的camera信息取出来,然后分配给fimc_dev,fimc_dev定义在fimc.h中。类型为struct fimc_global,原型如下所示:

  1. /* global */  
  2. struct fimc_global {  
  3.     struct fimc_control     ctrl[FIMC_DEVICES];  
  4.     struct s3c_platform_camera  camera[FIMC_MAXCAMS];  
  5.     int             camera_isvalid[FIMC_MAXCAMS];  
  6.     int             active_camera;  
  7.     int             initialized;  
  8. };  
/* global */
struct fimc_global {
	struct fimc_control		ctrl[FIMC_DEVICES];
	struct s3c_platform_camera	camera[FIMC_MAXCAMS];
	int				camera_isvalid[FIMC_MAXCAMS];
	int				active_camera;
	int				initialized;
};
在这里FIMC_MAXCAMS为5,FIMC_DEVICES为3,在ctrl中存放的是fimc控制器的信息,在camera存放的是camera的信息,s3c_platform_camera定义在/arch/arm/plat-s5p/include/plat/fimc.h文件中。

下面我们看看在平台文件中怎么定义一个camera的,我们使用的友善的开发板,所以对应的平台代码位于arch/arm/mach-s5pv210/mach-mini210.c。我们就以ov7725为例说明一下在平台代码中怎样描述一个camera,如下所示:

  1. #ifdef CONFIG_VIDEO_OV9650  
  2. static int ov9650_power_en(int onoff)  
  3. {  
  4.     printk("ov9650: power %s\n", onoff ? "ON" : "Off");  
  5.     return 0;  
  6. }  
  7.   
  8. static struct ov9650_platform_data ov9650_plat = {  
  9.     .default_width = 1280,  
  10.     .default_height = 1024,  
  11.     .pixelformat = V4L2_PIX_FMT_YUYV,  
  12.     .freq = 40000000,  
  13.     .is_mipi = 0,  
  14. };  
  15.   
  16. static struct i2c_board_info ov9650_i2c_info = {  
  17.     I2C_BOARD_INFO("ov9650", 0x30),  
  18.     .platform_data = &ov9650_plat,  
  19. };  
  20.   
  21. static struct s3c_platform_camera ov9650 = {  
  22.     #ifdef CAM_ITU_CH_A  
  23.     .id     = CAMERA_PAR_A,  
  24.     #else  
  25.     .id     = CAMERA_PAR_B,  
  26.     #endif  
  27.     .type       = CAM_TYPE_ITU,  
  28.     .fmt        = ITU_601_YCBCR422_8BIT,  
  29.     .order422   = CAM_ORDER422_8BIT_YCBYCR,  
  30.     .i2c_busnum = 0,  
  31.     .info       = &ov9650_i2c_info,  
  32.     .pixelformat    = V4L2_PIX_FMT_YUYV,  
  33.     .srclk_name = "mout_mpll",  
  34.     /* .srclk_name  = "xusbxti", */  
  35.     .clk_name   = "sclk_cam1",  
  36.     .clk_rate   = 40000000,  
  37.     .line_length    = 1920,  
  38.     .width      = 1280,  
  39.     .height     = 1024,  
  40.     .window     = {  
  41.         .left   = 0,  
  42.         .top    = 0,  
  43.         .width  = 1280,  
  44.         .height = 1024,  
  45.     },  
  46.   
  47.     /* Polarity */  
  48.     .inv_pclk   = 1,  
  49.     .inv_vsync  = 1,  
  50.     .inv_href   = 0,  
  51.     .inv_hsync  = 0,  
  52.   
  53.     .initialized    = 0,  
  54.     .cam_power  = ov9650_power_en,  
  55. };  
  56. #endif  
#ifdef CONFIG_VIDEO_OV9650
static int ov9650_power_en(int onoff)
{
	printk("ov9650: power %s\n", onoff ? "ON" : "Off");
	return 0;
}

static struct ov9650_platform_data ov9650_plat = {
	.default_width = 1280,
	.default_height = 1024,
	.pixelformat = V4L2_PIX_FMT_YUYV,
	.freq = 40000000,
	.is_mipi = 0,
};

static struct i2c_board_info ov9650_i2c_info = {
	I2C_BOARD_INFO("ov9650", 0x30),
	.platform_data = &ov9650_plat,
};

static struct s3c_platform_camera ov9650 = {
	#ifdef CAM_ITU_CH_A
	.id		= CAMERA_PAR_A,
	#else
	.id		= CAMERA_PAR_B,
	#endif
	.type		= CAM_TYPE_ITU,
	.fmt		= ITU_601_YCBCR422_8BIT,
	.order422	= CAM_ORDER422_8BIT_YCBYCR,
	.i2c_busnum	= 0,
	.info		= &ov9650_i2c_info,
	.pixelformat	= V4L2_PIX_FMT_YUYV,
	.srclk_name	= "mout_mpll",
	/* .srclk_name	= "xusbxti", */
	.clk_name	= "sclk_cam1",
	.clk_rate	= 40000000,
	.line_length	= 1920,
	.width		= 1280,
	.height		= 1024,
	.window		= {
		.left	= 0,
		.top	= 0,
		.width	= 1280,
		.height	= 1024,
	},

	/* Polarity */
	.inv_pclk	= 1,
	.inv_vsync	= 1,
	.inv_href	= 0,
	.inv_hsync	= 0,

	.initialized	= 0,
	.cam_power	= ov9650_power_en,
};
#endif
在上面描述了一个camera的相关信息,id是指camera的索引,type是代表摄像头的接口,在这里标示ITU接口,fmt表示摄像头输出的帧输出格式,这里的输出格式为ITU_601_YCBCR422_8BIT,order422表示YV分量的顺序,这里是YcbCr,还有时钟源、时钟大小、图像的解析度等,Polarity表示信号的极性,具体要和摄像头本身设置一致。

i2c_busnum是I2C总线编号,我们的camera 传感器连接在0号总线上,所以i2c_busnum为0.

然后再看一下下面这块代码:

  1. /* Interface setting */  
  2. static struct s3c_platform_fimc fimc_plat_lsi = {  
  3.     .srclk_name = "mout_mpll",  
  4.     .clk_name   = "sclk_fimc",  
  5.     .lclk_name  = "sclk_fimc_lclk",  
  6.     .clk_rate   = 166750000,  
  7. #if defined(CONFIG_VIDEO_S5K4EA)  
  8.     .default_cam    = CAMERA_CSI_C,  
  9. #else  
  10. #ifdef CAM_ITU_CH_A  
  11.     .default_cam    = CAMERA_PAR_A,  
  12. #else  
  13.     .default_cam    = CAMERA_PAR_B,  
  14. #endif  
  15. #endif  
  16.     .camera     = {  
  17. //..............................  
  18. #ifdef CONFIG_VIDEO_OV9650  
  19.             &ov9650,  
  20. #endif  
  21. //..............................  
  22.   
  23.     .hw_ver     = 0x43,  
  24. };  
/* Interface setting */
static struct s3c_platform_fimc fimc_plat_lsi = {
	.srclk_name	= "mout_mpll",
	.clk_name	= "sclk_fimc",
	.lclk_name	= "sclk_fimc_lclk",
	.clk_rate	= 166750000,
#if defined(CONFIG_VIDEO_S5K4EA)
	.default_cam	= CAMERA_CSI_C,
#else
#ifdef CAM_ITU_CH_A
	.default_cam	= CAMERA_PAR_A,
#else
	.default_cam	= CAMERA_PAR_B,
#endif
#endif
	.camera		= {
//..............................
#ifdef CONFIG_VIDEO_OV9650
			&ov9650,
#endif
//..............................

	.hw_ver		= 0x43,
};
在上面可以看出我们的camera最终会放在这个fimc_plat_lsi中的camera中,上面的struct s3c_platform_fimc这个结构体其实是fimc对应平台的数据结构,具体定义如下所示:

  1. /* For camera interface driver */  
  2. struct s3c_platform_fimc {  
  3.     const char          srclk_name[16];     /* source of interface clock name */  
  4.     const char          clk_name[16];       /* interface clock name */  
  5.     const char          lclk_name[16];      /* interface clock name */  
  6.     u32             clk_rate;       /* clockrate for interface clock */  
  7.     enum fimc_cam_index     default_cam;        /* index of default cam */  
  8.     struct s3c_platform_camera  *camera[5];     /* FIXME */  
  9.     int             hw_ver;  
  10.     phys_addr_t         pmem_start;     /* starting physical address of memory region */  
  11.     size_t              pmem_size;      /* size of memory region */  
  12.   
  13.     void                (*cfg_gpio)(struct platform_device *pdev);  
  14.     int             (*clk_on)(struct platform_device *pdev, struct clk *clk);  
  15.     int             (*clk_off)(struct platform_device *pdev, struct clk *clk);  
  16. };  
/* For camera interface driver */
struct s3c_platform_fimc {
	const char			srclk_name[16];		/* source of interface clock name */
	const char			clk_name[16];		/* interface clock name */
	const char			lclk_name[16];		/* interface clock name */
	u32				clk_rate;		/* clockrate for interface clock */
	enum fimc_cam_index		default_cam;		/* index of default cam */
	struct s3c_platform_camera	*camera[5];		/* FIXME */
	int				hw_ver;
	phys_addr_t			pmem_start;		/* starting physical address of memory region */
	size_t				pmem_size;		/* size of memory region */

	void				(*cfg_gpio)(struct platform_device *pdev);
	int				(*clk_on)(struct platform_device *pdev, struct clk *clk);
	int				(*clk_off)(struct platform_device *pdev, struct clk *clk);
};
这个结构体定义在/arch/arm/plat-s5p/include/plat/fimc.h文件中。

在mach-mini210.c文件的  函数中有如下一段代码:

  1. #ifdef CONFIG_VIDEO_FIMC  
  2.     /* fimc */  
  3.     s3c_fimc0_set_platdata(&fimc_plat_lsi);  
  4.     s3c_fimc1_set_platdata(&fimc_plat_lsi);  
  5.     s3c_fimc2_set_platdata(&fimc_plat_lsi);  
  6.     /* external camera */  
  7.     /* smdkv210_cam0_power(1); */  
  8.     /* smdkv210_cam1_power(1); */  
  9. #endif  
#ifdef CONFIG_VIDEO_FIMC
	/* fimc */
	s3c_fimc0_set_platdata(&fimc_plat_lsi);
	s3c_fimc1_set_platdata(&fimc_plat_lsi);
	s3c_fimc2_set_platdata(&fimc_plat_lsi);
	/* external camera */
	/* smdkv210_cam0_power(1); */
	/* smdkv210_cam1_power(1); */
#endif
在s3c_fimcx_set_platdata函数中会进行对应fimc的中一些配置,如给cfg_gpio、clk_on以及clk_off赋值,还有pmem_start给定预留的空间,并pmem_size赋值空间大小。正如s3c_fimcx_setplatdata函数名所示,这个函数就是为对应的s3c_device_fimcx设备设置数据,就s3c_device_fimc0来说吧,如下所示:

  1. void __init s3c_fimc0_set_platdata(struct s3c_platform_fimc *pd)  
  2. {  
  3.     struct s3c_platform_fimc *npd;  
  4.   
  5.     if (!pd)  
  6.         pd = &default_fimc0_data;  
  7.   
  8.     npd = kmemdup(pd, sizeof(struct s3c_platform_fimc), GFP_KERNEL);  
  9.     if (!npd)  
  10.         printk(KERN_ERR "%s: no memory for platform data\n", __func__);  
  11.     else {  
  12.         if (!npd->cfg_gpio)  
  13.             npd->cfg_gpio = s3c_fimc0_cfg_gpio;  
  14.   
  15.         if (!npd->clk_on)  
  16.             npd->clk_on = s3c_fimc_clk_on;  
  17.   
  18.         if (!npd->clk_off)  
  19.             npd->clk_off = s3c_fimc_clk_off;  
  20.   
  21.         npd->hw_ver = 0x45;  
  22.   
  23.         /* starting physical address of memory region */  
  24.         npd->pmem_start = s5p_get_media_memory_bank(S5P_MDEV_FIMC0, 1);  
  25.         /* size of memory region */  
  26.         npd->pmem_size = s5p_get_media_memsize_bank(S5P_MDEV_FIMC0, 1);  
  27.   
  28.         s3c_device_fimc0.dev.platform_data = npd;  
  29.     }  
  30. }  
void __init s3c_fimc0_set_platdata(struct s3c_platform_fimc *pd)
{
	struct s3c_platform_fimc *npd;

	if (!pd)
		pd = &default_fimc0_data;

	npd = kmemdup(pd, sizeof(struct s3c_platform_fimc), GFP_KERNEL);
	if (!npd)
		printk(KERN_ERR "%s: no memory for platform data\n", __func__);
	else {
		if (!npd->cfg_gpio)
			npd->cfg_gpio = s3c_fimc0_cfg_gpio;

		if (!npd->clk_on)
			npd->clk_on = s3c_fimc_clk_on;

		if (!npd->clk_off)
			npd->clk_off = s3c_fimc_clk_off;

		npd->hw_ver = 0x45;

		/* starting physical address of memory region */
		npd->pmem_start = s5p_get_media_memory_bank(S5P_MDEV_FIMC0, 1);
		/* size of memory region */
		npd->pmem_size = s5p_get_media_memsize_bank(S5P_MDEV_FIMC0, 1);

		s3c_device_fimc0.dev.platform_data = npd;
	}
}
上面的函数会为cfg_gpio、clk_on、clk_off指定函数,最后三句意思是获取给fimc0预留的空间地址和空间大小,然后将这些给s3c_device_fimc0这个设备。

s3c_device_fimc0是一个platform_device设备,这个设备的定义以及其一些资源都在arch/arm/plat-s5p/devs.c文件中,如下所示:

  1. #ifdef CONFIG_VIDEO_FIMC  
  2. static struct resource s3c_fimc0_resource[] = {  
  3.     [0] = {  
  4.         .start  = S5P_PA_FIMC0,  
  5.         .end    = S5P_PA_FIMC0 + S5P_SZ_FIMC0 - 1,  
  6.         .flags  = IORESOURCE_MEM,  
  7.     },  
  8.     [1] = {  
  9.         .start  = IRQ_FIMC0,  
  10.         .end    = IRQ_FIMC0,  
  11.         .flags  = IORESOURCE_IRQ,  
  12.     },  
  13. };  
  14.   
  15. struct platform_device s3c_device_fimc0 = {  
  16.     .name       = "s3c-fimc",  
  17.     .id     = 0,  
  18.     .num_resources  = ARRAY_SIZE(s3c_fimc0_resource),  
  19.     .resource   = s3c_fimc0_resource,  
  20. };  
  21.   
  22. static struct s3c_platform_fimc default_fimc0_data __initdata = {  
  23.     .default_cam    = CAMERA_PAR_A,  
  24.     .hw_ver = 0x45,  
  25. };  
  26.   
  27. void __init s3c_fimc0_set_platdata(struct s3c_platform_fimc *pd)  
  28. {  
  29. //.........................  
  30. }  
  31.   
  32.   
  33. //s3c_device_fimc1相关的,省略了  
  34. //..............................  
  35.   
  36. //s3c_device_fimc2相关的,省略了  
  37. //...............................  
  38.   
  39. #endif 
#ifdef CONFIG_VIDEO_FIMC
static struct resource s3c_fimc0_resource[] = {
	[0] = {
		.start	= S5P_PA_FIMC0,
		.end	= S5P_PA_FIMC0 + S5P_SZ_FIMC0 - 1,
		.flags	= IORESOURCE_MEM,
	},
	[1] = {
		.start	= IRQ_FIMC0,
		.end	= IRQ_FIMC0,
		.flags	= IORESOURCE_IRQ,
	},
};

struct platform_device s3c_device_fimc0 = {
	.name		= "s3c-fimc",
	.id		= 0,
	.num_resources	= ARRAY_SIZE(s3c_fimc0_resource),
	.resource	= s3c_fimc0_resource,
};

static struct s3c_platform_fimc default_fimc0_data __initdata = {
	.default_cam	= CAMERA_PAR_A,
	.hw_ver	= 0x45,
};

void __init s3c_fimc0_set_platdata(struct s3c_platform_fimc *pd)
{
//.........................
}


//s3c_device_fimc1相关的,省略了
//..............................

//s3c_device_fimc2相关的,省略了
//...............................

#endif

platform_device会在下面代码的init_machine所指的函数被执行时注册初始化,

  1. MACHINE_START(MINI210, "MINI210")  
  2.     /* Maintainer: Kukjin Kim <kgene.kim@samsung.com> */  
  3.     .phys_io    = S3C_PA_UART & 0xfff00000,  
  4.     .io_pg_offst    = (((u32)S3C_VA_UART) >> 18) & 0xfffc,  
  5.     .boot_params    = S5P_PA_SDRAM + 0x100,  
  6.     .fixup      = mini210_fixup,  
  7.     .init_irq   = s5pv210_init_irq,  
  8.     .map_io     = mini210_map_io,  
  9.     .init_machine   = mini210_machine_init,  
  10.     .timer      = &s5p_systimer,  
  11. MACHINE_END  
MACHINE_START(MINI210, "MINI210")
	/* Maintainer: Kukjin Kim <kgene.kim@samsung.com> */
	.phys_io	= S3C_PA_UART & 0xfff00000,
	.io_pg_offst	= (((u32)S3C_VA_UART) >> 18) & 0xfffc,
	.boot_params	= S5P_PA_SDRAM + 0x100,
	.fixup		= mini210_fixup,
	.init_irq	= s5pv210_init_irq,
	.map_io		= mini210_map_io,
	.init_machine	= mini210_machine_init,
	.timer		= &s5p_systimer,
MACHINE_END
也就是说在执行mini210_machine_init函数时注册初始化这些platform_devide,在mini210_machine_init函数中会执行下面一句,在这里会注册我们的 s3c_device_fimcx (x=0、1、2)这三个设备,如下所示:

  1. platform_add_devices(mini210_devices, ARRAY_SIZE(mini210_devices)); 

platform_add_devices(mini210_devices, ARRAY_SIZE(mini210_devices));
这里的mini210_devices定义如下所示:

  1. static struct platform_device *mini210_devices[] __initdata = {  
  2.   
  3. //..................................  
  4.   
  5. #ifdef CONFIG_VIDEO_FIMC  
  6.     &s3c_device_fimc0,  
  7.     &s3c_device_fimc1,  
  8.     &s3c_device_fimc2,  
  9. #endif  
  10.   
  11. //..................................  
  12. }  

static struct platform_device *mini210_devices[] __initdata = {

//..................................

#ifdef CONFIG_VIDEO_FIMC
	&s3c_device_fimc0,
	&s3c_device_fimc1,
	&s3c_device_fimc2,
#endif

//..................................
}
在执行platform_add_devices时,会注册mini210_devices中的所有的platform_device设备,也就是说也会注册我们的s3c_device_fimcx(x=0、1、2)这三个设备。platform_add_device其实也是调用platform_device_register来注册platform device,如下所示:

  1. /** 
  2.  * platform_add_devices - add a numbers of platform devices 
  3.  * @devs: array of platform devices to add 
  4.  * @num: number of platform devices in array 
  5.  */  
  6. int platform_add_devices(struct platform_device **devs, int num)  
  7. {  
  8.     int i, ret = 0;  
  9.   
  10.     for (i = 0; i < num; i++) {  
  11.         ret = platform_device_register(devs[i]);  
  12.         if (ret) {  
  13.             while (--i >= 0)  
  14.                 platform_device_unregister(devs[i]);  
  15.             break;  
  16.         }  
  17.     }  
  18.   
  19.     return ret;  
  20. }  
/**
 * platform_add_devices - add a numbers of platform devices
 * @devs: array of platform devices to add
 * @num: number of platform devices in array
 */
int platform_add_devices(struct platform_device **devs, int num)
{
	int i, ret = 0;

	for (i = 0; i < num; i++) {
		ret = platform_device_register(devs[i]);
		if (ret) {
			while (--i >= 0)
				platform_device_unregister(devs[i]);
			break;
		}
	}

	return ret;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值