android camera(三):camera V4L2 FIMC

1. V4L2

1)简介

        在Linux中,摄像头方面的标准化程度比较高,这个标准就是V4L2驱动程序,这也是业界比较公认的方式。

        V4L全称是Video for Linux,是Linux内核中标准的关于视频驱动程序,目前使用比较多的版本是Video for Linux 2,简称V4L2。它为Linux下的视频驱动提供了统一的接口,使得应用程序可以使用统一的API操作不同的视频设备。从内核空间到用户空间,主要的数据流和控制类均由V4L2驱动程序的框架来定义。

V4L2驱动程序一般只提供Video数据的获得,而如何实现视频预览,如何向上层发送数据,如何把纯视频流和取景器、视频录制等实际业务组织起来,都是camera的硬件抽象层需要负责的工作。

V4L2驱动核心实现为如下文件:drivers/media/video/v4l2-dev.c。

V4l2-dev.h中定义的video_device是V4L2驱动程序的核心数据结构,它为具体的摄像头sensor驱动提供了接口调用。

V4l2的采集过程(应用程序):

1)     打开设备,获得文件描述符;

2)     设置图片格式;

3)     分配缓冲区;

4)     启动采集过程,读取数据;

5)     停止采集,关闭设备。

 技术分享

 

 

2)数据结构

V4L2的主要数据结构是video_device,定义在v4l2_dev.h中:

[cpp] view plaincopy
 
  1. struct video_device  
  2. {  
  3.     /* device ops */  
  4.     const struct v4l2_file_operations *fops;  /*接口函数指针*/  
  5.   
  6.     /* sysfs */  
  7.     struct device dev;      /* v4l 设备结构 */  
  8.     struct cdev *cdev;      /* 字符设备结构*/  
  9.   
  10.     /* Set either parent or v4l2_dev if your driver uses v4l2_device */  
  11.     struct device *parent;      /* 设备父指针 */  
  12.     struct v4l2_device *v4l2_dev;   /* v4l2设备指针*/  
  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;          /* debug 级别*/  
  30.   
  31.     /* Video 标准变量 */  
  32.     v4l2_std_id tvnorms;        /* Supported tv norms */  
  33.     v4l2_std_id current_norm;   /* Current tvnorm */  
  34.   
  35.     /* 回调函数 */  
  36.     void (*release)(struct video_device *vdev);  
  37.   
  38.     /* ioctl 回调函数 */  
  39.     const struct v4l2_ioctl_ops *ioctl_ops;     
  40. };  

主要接口函数有:

intvideo_register_device(struct video_device *vdev, int type, int nr);

static intv4l2_ioctl(struct inode *inode, struct file *filp,       unsigned int cmd, unsigned long arg);

2.  FIMC                     

1)简介

FIMC这个模块不仅仅是一个摄像头的控制接口,它还承担着V4L2的output功能和overlay的功能。

FIMC的驱动在内核中的位置:drivers/media/video/samsung/fimc

它包含下边的文件:

      fimc_regs.c
        fimc_capture.c
        fimc_dev.c
        fimc_output.c
        fimc_overlay.c
        fimc_v4l2.c

它们的组织关系如下:

技术分享

 

可以看到,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。这样把硬件相关的代码和硬件无关的代码分开来实现是非常好的方式,可以最大限度的实现代码复用。

 2) 数据结构 

 

FIMC的主要数据结构fimc_control,定义在fimc.h中:

[cpp] view plaincopy
 
  1. struct fimc_control {  
  2.     int             id;     /* 控制器 id */  
  3.     char                name[16];  
  4.     atomic_t            in_use;  
  5.     void __iomem            *regs;      /* 寄存器 i/o */  
  6.     struct clk          *clk;       /* interface clock */  
  7.     struct regulator    *regulator;     /* pd regulator */  
  8.     struct fimc_meminfo     mem;        /* for reserved mem */  
  9.   
  10.     /* kernel helpers */  
  11.     struct mutex            lock;       /* controller lock */  
  12.     struct mutex            alloc_lock;  
  13.     struct mutex            v4l2_lock;  
  14.     wait_queue_head_t       wq;  
  15.     struct device           *dev;  
  16.     int             irq;  
  17.   
  18.     /* v4l2 related */  
  19.     struct video_device     *vd;  
  20.     struct v4l2_device      v4l2_dev;  
  21.   
  22.     /* fimc specific */  
  23.     struct fimc_limit       *limit;     /* H/W limitation */  
  24.     struct s3c_platform_camera  *cam;       /* activated camera */  
  25.     struct fimc_capinfo     *cap;       /* capture dev info */  
  26.     struct fimc_outinfo     *out;       /* output dev info */  
  27.     struct fimc_fbinfo      fb;     /* fimd info */  
  28.     struct fimc_scaler      sc;     /* scaler info */  
  29.     struct fimc_effect      fe;     /* fimc effect info */  
  30.   
  31.     enum fimc_status        status;  
  32.     enum fimc_log           log;  
  33.   
  34.     u32             ctx_busy[FIMC_MAX_CTXS];  
  35. };  

因为FIMC一共有三套一样的控制器(fimc0, fimc1, fimc2),所以驱动里使用了一个数组来描述:

[cpp] view plaincopy
 
  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. };  

fb_ops结构体是针对v4l2设备的基本操作,定义如下:

[cpp] view plaincopy
 
  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. };  

3)FIMC初始设置

在S5PV210中,FIMC初始设置代码在 /drivers/ arch/arm/mach-s5pv210/mach-smdkv310.c中:

 

[cpp] view plaincopy
 
  1. static struct s3c_platform_fimc fimc_plat = {  
  2.     .srclk_name = "mout_mpll",  
  3.     .clk_name   = "sclk_fimc",  
  4.     .lclk_name  = "sclk_fimc_lclk",  
  5.     .clk_rate   = 166750000,  
  6.     .default_cam    = CAMERA_CSI_C,  
  7.          .camera        = {  
  8.         &mt9p111,//5M back cam  
  9.         &s5k6aafx,///1.3M front cam  
  10.     },  
  11.     .hw_ver     = 0x43,  
  12. };  

 

 

 对于GPIO的配置代码在 /drivers/ arch/arm/mach-s5pv210/setup-fimc0.c中:

[cpp] view plaincopy
 
  1. oid s3c_fimc0_cfg_gpio(struct platform_device *pdev)  
  2. {  
  3.     int i = 0;  
  4.   
  5.     /* CAM A port(b0010) : PCLK, VSYNC, HREF, DATA[0-4] */  
  6.     for (i = 0; i < 8; i++) {  
  7.         s3c_gpio_cfgpin(S5PV210_GPE0(i), S3C_GPIO_SFN(2));  
  8.         s3c_gpio_setpull(S5PV210_GPE0(i), S3C_GPIO_PULL_NONE);  
  9.     }  
  10.     /* CAM A port(b0010) : DATA[5-7], CLKOUT(MIPI CAM also), FIELD */  
  11.     for (i = 0; i < 5; i++) {  
  12.         s3c_gpio_cfgpin(S5PV210_GPE1(i), S3C_GPIO_SFN(2));  
  13.         s3c_gpio_setpull(S5PV210_GPE1(i), S3C_GPIO_PULL_NONE);  
  14.     }  
  15.     /* CAM B port(b0011) : DATA[0-7] */  
  16.     for (i = 0; i < 8; i++) {  
  17.         s3c_gpio_cfgpin(S5PV210_GPJ0(i), S3C_GPIO_SFN(3));  
  18.         s3c_gpio_setpull(S5PV210_GPJ0(i), S3C_GPIO_PULL_NONE);  
  19.     }  
  20.     /* CAM B port(b0011) : PCLK, VSYNC, HREF, FIELD, CLCKOUT */  
  21.     for (i = 0; i < 5; i++) {  
  22.         s3c_gpio_cfgpin(S5PV210_GPJ1(i), S3C_GPIO_SFN(3));  
  23.         s3c_gpio_setpull(S5PV210_GPJ1(i), S3C_GPIO_PULL_NONE);  
  24.     }  
  25. }  

4)接口函数

FIMC的主要回调函数如下,实现在fimc_v4l2.c中:

[cpp] view plaincopy
 
  1. onst 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. };  

对于寄存器的操作,实现都在fimc_regs.c文件中,如

[cpp] view plaincopy
 
    1. int fimc_hwset_camera_source(struct fimc_control *ctrl)  
    2. {  
    3.     struct s3c_platform_camera *cam = ctrl->cam;  
    4.     u32 cfg = 0;  
    5.   
    6.     cfg |= S3C_CISRCFMT_ITU601_8BIT;  
    7.     cfg |= cam->order422;  
    8.   
    9.     if (cam->type == CAM_TYPE_ITU)  
    10.         cfg |= cam->fmt;  
    11.   
    12.     cfg |= S3C_CISRCFMT_SOURCEHSIZE(cam->width);  
    13.     cfg |= S3C_CISRCFMT_SOURCEVSIZE(cam->height);  
    14.   
    15.     writel(cfg, ctrl->regs + S3C_CISRCFMT);  
    16.   
    17.     return 0;  
    18. }  
    19.   
    20. int fimc_hwset_enable_irq(struct fimc_control *ctrl, int overflow, int level)  
    21. {  
    22.     u32 cfg = readl(ctrl->regs + S3C_CIGCTRL);  
    23.   
    24.     cfg &= ~(S3C_CIGCTRL_IRQ_OVFEN | S3C_CIGCTRL_IRQ_LEVEL);  
    25.     cfg |= S3C_CIGCTRL_IRQ_ENABLE;  
    26.   
    27.     if (overflow)  
    28.         cfg |= S3C_CIGCTRL_IRQ_OVFEN;  
    29.   
    30.     if (level)  
    31.         cfg |= S3C_CIGCTRL_IRQ_LEVEL;  
    32.   
    33.     writel(cfg, ctrl->regs + S3C_CIGCTRL);  
    34.   
    35.     return 0;  
    36. }  

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。