[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总线控制器
- struct i2c_adapter {
- struct module *owner;
- unsigned int class; /*classes to allow probing for */
- const struct i2c_algorithm *algo; /* the algorithm to access the bus */
- void *algo_data; /*algorithm 数据 */
- /* data fields that are valid for all devices */
- struct rt_mutex bus_lock;
- int timeout; /* injiffies */
- int retries; /* 重试次数 */
- struct device dev; /* theadapter device */
- int nr;
- char name[48]; /* 适配器名字 */
- struct completion dev_released; /* 用于同步 */
- struct mutex userspace_clients_lock;
- struct list_head userspace_clients;
- };
I2c_algorithm对应一套通信方法
- struct i2c_algorithm {
- /*I2C传输函数指针 */
- int (*master_xfer)(struct i2c_adapter *adap, struct i2c_msg *msgs,
- int num);
- /*SMBUS传输函数指针 */
- int (*smbus_xfer) (struct i2c_adapter *adap, u16 addr,
- unsigned short flags, charread_write,
- u8 command, int size, unioni2c_smbus_data *data);
- /*返回适配器支持的功能 */
- u32 (*functionality) (struct i2c_adapter *);
- };
I2c_driver代表I2C从设备驱动
- struct i2c_driver {
- unsigned int class;
- /* 这两个接口已经被probe和remove取代 */
- int (*attach_adapter)(struct i2c_adapter *) __deprecated; /*依附i2c适配器函数指针*/
- int (*detach_adapter)(struct i2c_adapter *) __deprecated;/*脱离i2c适配器函数指针*/
- /* Standard driver model interfaces */
- int (*probe)(struct i2c_client *, const struct i2c_device_id *);
- int (*remove)(struct i2c_client *);
- void (*shutdown)(struct i2c_client *);
- int (*suspend)(struct i2c_client *, pm_message_t mesg);
- int (*resume)(struct i2c_client *);
- void (*alert)(struct i2c_client *, unsigned int data);
- int (*command)(struct i2c_client *client, unsigned int cmd, void *arg);
- struct device_driver driver;
- const struct i2c_device_id *id_table; /* 该驱动所支持的设备ID表 */
- /* Device detection callback for automatic device creation */
- int (*detect)(struct i2c_client *, struct i2c_board_info *);
- const unsigned short *address_list;
- struct list_head clients;
- };
I2c_client代表I2C从设备
- struct i2c_client {
- unsigned short flags; /*I2C_CLIENT_TEN:使用10位从地址,I2C_CLIENT_PEC:使用SMBus包错误检测*/
- unsignedshort addr; /* chipaddress - NOTE: 7bit */
- /*addresses are stored in the */
- /*_LOWER_ 7 bits */
- charname[I2C_NAME_SIZE];
- structi2c_adapter *adapter; /* 依附的i2c_adapter */
- structi2c_driver *driver; /* 依附的i2c_driver*/
- structdevice dev; /* the devicestructure */
- intirq; /* irq issuedby device */
- structlist_head detected;
- };
I2c_adapter与i2c_algorithm
一个I2C适配器需要I2C_algorithm中提供的通信函数来控制适配器上产生特定的访问周期。I2c_algorithm中的关键函数master_xfer()用于产生I2C访问周期需要的信号,以i2c_msg为单位。
- struct i2c_msg {
- __u16addr; /* slave address */
- __u16flags; /* 读写标志*/
- __u16len; /* msg length */
- __u8*buf; /* pointer to msgdata */
- };
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适配器
- int i2c_add_adapter(struct i2c_adapter *adapter)
- int i2c_del_adapter(struct i2c_adapter *adap)
2、 增加/删除I2C从设备驱动
- int i2c_register_driver(struct module *owner, structi2c_driver *driver)
- static inline int i2c_add_driver(struct i2c_driver *driver)
- void i2c_del_driver(struct i2c_driver *driver)
i2c_add_driver是对i2c_register_driver简单的封装
3、 i2c传输,发送和接收
- int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg*msgs, int num)
- int i2c_master_send(const struct i2c_client *client, constchar *buf, int count)
- 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结构体中的两个函数:
- struct i2c_algorithm {
- int(*master_xfer)(struct i2c_adapter *adap, struct i2c_msg *msgs,
- int num);
- u32(*functionality) (struct i2c_adapter *);
- };
Functionality函数用于返回algorithm所支持的通信协议;
Master_xfer函数在I2C适配器上完成数据的传输;
Master_xfer函数实现模板
- static int i2c_adapter_xxx_xfer(structi2c_adapter *adap, struct i2c_msg *msgs, int num)
- {
- ......
- for (i = 0; i < num; i++) {
- i2c_adapter_xxx_start(); /*产生起始位*/
- if (msgs[i]->flags & I2C_M_RD) { /*读取*/
- i2c_adapter_xxx_setaddr((msg->addr << 1) | 1); /*发送从设备地址*/
- i2c_adapter_xxx_wait_ack(); /*获得从设备的ACK*/
- i2c_adapter_xxx_readbytes(msgs[i]->buf,msgs[i]->len); /*读取len长度的数据到buf中*/
- } else {
- i2c_adapter_xxx_setaddr(msg->addr << 1);
- i2c_adapter_xxx_wait_ack();
- i2c_adapter_xxx_writebytes(msgs[i]->buf, msgs[i]->len);
- }
- }
- i2c_adapter_xxx_stop(); /*产生停止位*/
- }
上面调用的函数用于完成适配器的底层硬件操作,与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设备驱动模型有了解,对上面的分析应该不难理解。
