你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux驱动子系统之I2C

子系统

[日期:2011-11-20] 来源:Linux社区  作者:cjok376240497 [字体:大 中 小]

[LinuxI2C子系统架构]

在内核中已经提供I2C子系统,所以在做I2C驱动之前,就必须要熟悉该子系统。


三大组成部分:

1、I2C核心(i2c-core)

I2C核心提供了I2C总线驱动和设备驱动的注册、注销方法,I2C通信方法(algorithm)上层的、与具体适配器无关的代码以及探测设备、检测设备地址的上层代码等。

2、I2C总线驱动(I2Cadapter/Algo driver)

I2C总线驱动是对I2C硬件体系结构中适配器端的实现,适配器可由CPU控制,甚至集成在CPU内部(大多数微控制器都这么做)。适配器就是我们经常所说的控制器。

经由I2C总线驱动的代码,我们可以控制I2C适配器以主控方式产生开始位,停止位,读写周期,以及以从设备方式被读写,产生ACK等。

I2C总线驱动由i2c_adapter和i2c_algorithm来描述

3、I2C客户驱动程序(I2Cclient driver)

I2C客户驱动是对I2C硬件体系结构中设备端的实现,设备一般挂接在收CPU控制的I2C适配器上,通过I2C适配器与CPU交换数据。

I2C客户驱动程序由i2c_driver来描述

所有的I2C驱动代码位于drivers/i2c目录下

I2c-core.c     实现I2C核心的功能

I2c-dev.c      实现I2C适配器设备文件的功能,每一个I2C适配器都被分配一个设备

Chips         特定的I2C设备驱动

Busses        I2C总线的驱动

Algos         实现了一些I2C总线适配器的algorithm

I2C驱动编写的两种方法

从上面的图我们可以看到两种编写驱动方法,一种是利用系统提供的i2c-dev.c来实现一个i2c适配器的设备文件,然后通过在应用层操作I2C适配器来控制I2C从设备;另一种是为I2C从设备独立编写一个设备驱动,不需要i2c-dev.c文件,相对来说简单很多。

[重要的数据结构]

每次分析子系统免不了分析它的数据结构,OK我们先来分析一下。

I2c_adapter结构体代表一个物理的I2C总线控制器

  1. struct i2c_adapter {  
  2.    struct module *owner;  
  3.    unsigned int class;       /*classes to allow probing for */  
  4.    const struct i2c_algorithm *algo; /* the algorithm to access the bus */  
  5.    void *algo_data;        /*algorithm 数据 */  
  6.    
  7.    /* data fields that are valid for all devices   */  
  8.    struct rt_mutex bus_lock;  
  9.    
  10.    int timeout;            /* injiffies */  
  11.    int retries;             /* 重试次数 */  
  12.    struct device dev;      /* theadapter device */  
  13.    
  14.    int nr;  
  15.    char name[48];         /* 适配器名字 */  
  16.    struct completion dev_released;   /* 用于同步 */  
  17.    
  18.    struct mutex userspace_clients_lock;  
  19.    struct list_head userspace_clients;  
  20. };  

I2c_algorithm对应一套通信方法

  1. struct i2c_algorithm {  
  2.          /*I2C传输函数指针 */  
  3.    int (*master_xfer)(struct i2c_adapter *adap, struct i2c_msg *msgs,  
  4.                int num);  
  5.          /*SMBUS传输函数指针 */  
  6.    int (*smbus_xfer) (struct i2c_adapter *adap, u16 addr,  
  7.                unsigned short flags, charread_write,  
  8.                u8 command, int size, unioni2c_smbus_data *data);  
  9.          /*返回适配器支持的功能 */  
  10.    u32 (*functionality) (struct i2c_adapter *);  
  11. };  

I2c_driver代表I2C从设备驱动

  1. struct i2c_driver {  
  2.     unsigned int class;  
  3.   
  4.     /* 这两个接口已经被probe和remove取代 */  
  5.     int (*attach_adapter)(struct i2c_adapter *) __deprecated; /*依附i2c适配器函数指针*/  
  6.     int (*detach_adapter)(struct i2c_adapter *) __deprecated;/*脱离i2c适配器函数指针*/  
  7.   
  8.     /* Standard driver model interfaces */  
  9.     int (*probe)(struct i2c_client *, const struct i2c_device_id *);  
  10.     int (*remove)(struct i2c_client *);  
  11.   
  12.     void (*shutdown)(struct i2c_client *);  
  13.     int (*suspend)(struct i2c_client *, pm_message_t mesg);  
  14.     int (*resume)(struct i2c_client *);  
  15.     void (*alert)(struct i2c_client *, unsigned int data);  
  16.   
  17.     int (*command)(struct i2c_client *client, unsigned int cmd, void *arg);  
  18.   
  19.     struct device_driver driver;  
  20.     const struct i2c_device_id *id_table;  /* 该驱动所支持的设备ID表 */  
  21.   
  22.     /* Device detection callback for automatic device creation */  
  23.     int (*detect)(struct i2c_client *, struct i2c_board_info *);  
  24.     const unsigned short *address_list;  
  25.     struct list_head clients;  
  26. };  

I2c_client代表I2C从设备

  1. struct i2c_client {  
  2. unsigned short flags;                 /*I2C_CLIENT_TEN:使用10位从地址,I2C_CLIENT_PEC:使用SMBus包错误检测*/  
  3.          unsignedshort addr;                 /* chipaddress - NOTE: 7bit    */  
  4.                                                /*addresses are stored in the         */  
  5.                                                /*_LOWER_ 7 bits            */  
  6.          charname[I2C_NAME_SIZE];  
  7.          structi2c_adapter *adapter; /* 依附的i2c_adapter   */  
  8.          structi2c_driver *driver;         /* 依附的i2c_driver*/  
  9.          structdevice dev;             /* the devicestructure             */  
  10.          intirq;                         /* irq issuedby device               */  
  11.          structlist_head detected;  
  12. };  

I2c_adapter与i2c_algorithm

一个I2C适配器需要I2C_algorithm中提供的通信函数来控制适配器上产生特定的访问周期。I2c_algorithm中的关键函数master_xfer()用于产生I2C访问周期需要的信号,以i2c_msg为单位。

  1. struct i2c_msg {  
  2.          __u16addr;     /* slave address                         */  
  3.          __u16flags;    /* 读写标志*/  
  4.          __u16len;                  /* msg length                              */  
  5.          __u8*buf;                 /* pointer to msgdata                       */  
  6. };  

I2c_driver与i2c_client

I2c_driver与i2c_client是一对多的关系,一个i2c_driver上可以支持多个同等类型的i2c_client。

 

I2c_adapter与i2c_client

I2c_adapter与i2c_client的关系与I2C硬件体系中适配器和从设备的关系一致,i2c_client依附在i2c_adapter。

[核心层提供的接口函数]

1、  增加/删除I2C适配器

  1. int i2c_add_adapter(struct i2c_adapter *adapter)  
  2. int i2c_del_adapter(struct i2c_adapter *adap)  

2、  增加/删除I2C从设备驱动

  1. int i2c_register_driver(struct module *owner, structi2c_driver *driver)  
  2. static inline int i2c_add_driver(struct i2c_driver *driver)  
  3. void i2c_del_driver(struct i2c_driver *driver)  

i2c_add_driver是对i2c_register_driver简单的封装

3、  i2c传输,发送和接收

  1. int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg*msgs, int num)  
  2. int i2c_master_send(const struct i2c_client *client, constchar *buf, int count)  
  3. int i2c_master_recv(const struct i2c_client *client, char*buf, int count)  

i2c_master_send和i2c_master_recv是i2c_transfer的封装

tips:3.1.0的内核中已经没有i2c_attach_client和i2c_detach_client接口

[I2C总线通信方法]

我们需要为特定的I2C适配器实现其通信方法,主要实现i2c_algorithm结构体中的两个函数:

  1. struct i2c_algorithm {  
  2.          int(*master_xfer)(struct i2c_adapter *adap, struct i2c_msg *msgs,  
  3.                                int num);  
  4.          u32(*functionality) (struct i2c_adapter *);  
  5. };  

Functionality函数用于返回algorithm所支持的通信协议;

Master_xfer函数在I2C适配器上完成数据的传输;

Master_xfer函数实现模板

  1. static int i2c_adapter_xxx_xfer(structi2c_adapter *adap, struct i2c_msg *msgs, int num)  
  2. {  
  3.    ......  
  4.    for (i = 0; i < num; i++) {  
  5.        i2c_adapter_xxx_start();         /*产生起始位*/  
  6.        if (msgs[i]->flags & I2C_M_RD) {    /*读取*/  
  7.            i2c_adapter_xxx_setaddr((msg->addr << 1) | 1);  /*发送从设备地址*/  
  8.            i2c_adapter_xxx_wait_ack();   /*获得从设备的ACK*/  
  9. i2c_adapter_xxx_readbytes(msgs[i]->buf,msgs[i]->len);  /*读取len长度的数据到buf中*/  
  10.        } else {  
  11.            i2c_adapter_xxx_setaddr(msg->addr << 1);  
  12.            i2c_adapter_xxx_wait_ack();  
  13.            i2c_adapter_xxx_writebytes(msgs[i]->buf, msgs[i]->len);  
  14.        }  
  15.     }  
  16.    i2c_adapter_xxx_stop(); /*产生停止位*/  
  17. }  

上面调用的函数用于完成适配器的底层硬件操作,与I2C适配器和CPU的具体硬件直接相关,需要由工程师根据芯片的数据手册来实现。在内核源码中,针对不同的I2C适配器都有master_xfer的实现,风格与模板不尽相同,但是可以用该模板作为参考来看源代码,受益匪浅。

[I2C从设备驱动与I2C适配器的match]

I2C从设备驱动也就是上图中的I2C客户驱动,适配器与从设备是一对多的关系。在这里极好的体现了外设驱动和主机驱动的分离思想,带来优势是极为明显的。

我们来大致分析一下匹配的过程:

添加一个I2C从设备驱动(i2c_driver)

当调用i2c_add_driver函数向I2C总线(i2c-core.c文件中注册的”i2c”总线)增加一个i2c_driver时,会遍历总线中的所有i2c_client,调用总线注册的match函数I2C适配器上是否有与i2c_driver匹配的i2c_client,如果匹配会调用I2C注册的probe函数,然后再调用i2c_driver定义的probe来进行关联和初始化工作。

如果对Linux设备驱动模型有了解,对上面的分析应该不难理解。

linux
【内容导航】
第1页:硬件特性 第2页:子系统
第3页:i2c-dev 第4页:总线驱动
第5页:客户驱动 第6页:总结
相关资讯       Linux驱动 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款