底层之旅 —— Linux下的I2c体系结构
底层之旅 —— Linux下的I2c体系结构
最近在搞电容式触摸屏驱动,用I2C总线接口来传输数据,所以趁机复习一下I2C总线原理。I2C总线是Philips公司推出的芯片间品德传输总线,它采用两线制,由串行时钟线SCL和串行数据线SDA构成。在电路设计方面,由于I2C总线接口为开漏或开集电极输出,需要加上上拉电阻。
I2C总线通信方式:
(1)I2C采用主/从方式进行双向通信。
(2)I2C总线的时钟线SCL和数据线SDA都是双向传输线。
(3)在标准I2C模式下,数据传输速率可达100Kbps、400Kbps,在高速(HS)模式下可达3.4Mbit/s。
(4)在时钟线SCL保持高电平期间,数据线SDA出现由高电平向低电平变化时为起始信号S,启动I2C工作。若在时钟线SCL保持高电平期间,数据线SDA上出现由低到高的电平变化时为停止信号P,终止I2C总线的数据传送。
(5)I2C总线传送的格式为:开始位以后,主设备送出8位控制字节,以选择从设备并控制总线传送的方向,其后传送数据。
Linux的I2C体系结构分为3个部分:
(1)I2C核心
文件路径:linux / driver / i2c / i2c-core.c 这个文件实现了I2C核心的功能以及 /proc/bus/i2c-xxx 接口。
(2)I2C总线驱动
文件路径:linux / driver / i2c / busses / i2c-xxx.c 这个文件包含了各种arm处理器的I2C总线驱动。I2C总线驱动主要包含了I2C适配器数据结构i2c_adapter、I2C适配器的algorithm数据结构i2c_algorithm和控制I2C适配器产生通信信号的函数。
(3)I2C设备驱动
I2C设备驱动主要包含了数据结构i2c_driver 和 i2c_client 我们需要根据具体设备实现其中的成员函数。
I2C总线驱动中重要的四个数据结构。
i2c适配器结构体:
i2c_adapter对应于物理上的一个适配器。
/* i2c_adapter is the structure used to identify a physical i2c bus along with the access algorithms necessary to access it. */
struct i2c_adapter {
struct module *owner;
unsigned int id;
unsigned int class; /* classes to allow probing for */
const struct i2c_algorithm *algo; /* the algorithm to access the bus */
void *algo_data;
/* data fields that are valid for all devices*/
u8 level; /* nesting level for lockdep */
struct mutex bus_lock;
int timeout;/* in jiffies */
int retries;
struct device dev;/* the adapter device */
int nr;
char name[48];
struct completion dev_released;
};
I2C通信方法结构体:
i2c_algorithm对应于一套通信方法。i2c——algorithm中的关键函数master_xfer()用于产生I2C访问周期需要的信号。
struct i2c_algorithm {
/* If an adapter algorithm can't do I2C-level access, set master_xfer
to NULL. If an adapter algorithm can do SMBus access, set
smbus_xfer. If set to NULL, the SMBus protocol is simulated
using common I2C messages */
/* master_xfer should return the number of messages successfully
processed, or a negative value on error */
int (*master_xfer)(struct i2c_adapter *adap, struct i2c_msg *msgs,
int num);
int (*smbus_xfer) (struct i2c_adapter *adap, u16 addr,
unsigned short flags, char read_write,
u8 command, int size, union i2c_smbus_data *data);
/* To determine what the adapter supports */
u32 (*functionality) (struct i2c_adapter *);
};
I2C设备驱动结构体:
i2c_driver对应一套驱动方法。
/* The i2c_client structure which is handed to the @detect callback is
* not a real i2c_client. It is initialized just enough so that you can
* call i2c_smbus_read_byte_data and friends on it. Don't do anything
* else with it. In particular, calling dev_dbg and friends on it is
* not allowed.
*/
struct i2c_driver {
int id;
unsigned int class;
/* Notifies the driver that a new bus has appeared or is about to be
* removed. You should avoid using this if you can, it will probably
* be removed in a near future.
*/
int (*attach_adapter)(struct i2c_adapter *);
int (*detach_adapter)(struct i2c_adapter *);
/* Standard driver model interfaces */
int (*probe)(struct i2c_client *, const struct i2c_device_id *);
int (*remove)(struct i2c_client *);
/* driver model interfaces that don't relate to enumeration */
void (*shutdown)(struct i2c_client *);
int (*suspend)(struct i2c_client *, pm_message_t mesg);
int (*resume)(struct i2c_client *);
/* a ioctl like command that can be used to perform specific functions
* with the device.
*/
int (*command)(struct i2c_client *client, unsigned int cmd, void *arg);
struct device_driver driver;
const struct i2c_device_id *id_table;
/* Device detection callback for automatic device creation */
int (*detect)(struct i2c_client *, int kind, struct i2c_board_info *);
const struct i2c_client_address_data *address_data;
struct list_head clients;
};
I2C客户端结构体:
i2c_client对应于真实的物理设备,每个I2C设备都需要一个i2c_client来描述。
struct i2c_client {
unsigned short flags;/* div., see below*/
unsigned short addr;/* chip address - NOTE: 7bit*/
/* addresses are stored in the*/
/* _LOWER_ 7 bits*/
char name[I2C_NAME_SIZE];
struct i2c_adapter *adapter;/* the adapter we sit on*/
struct i2c_driver *driver;/* and our access routines*/
struct device dev;/* the device structure*/
int irq; /* irq issued by device*/
struct list_head detected;
};
这四个数据结构之间又有密切联系。一个I2C适配器需要i2c_algorithm中提供的通信函数来控制适配器上产生特定的访问周期。缺少i2c_algorithm的i2c_adapter什么也做不了,因此i2c_adapter中包含其使用的 i2c_algorithm的指针。i2c_driver与i2c_client的关系是一对多,一个i2c_driver上可以支持多个同类型的i2c_client。
评论暂时关闭