一,添加I2C平台设备
linux的设备模型将设备分成了driver,device两大部分,driver即是设备的驱动,device即是硬件设备的具体描述,比如说:设备所拥有的中断,地址等,这里分析一下I2C驱动中device,也即i2c_client的注册,driver 也即是i2c_driver的注册,
i2c_client 的注册,是由
-
int __init
-
i2c_register_board_info(int busnum,
-
struct i2c_board_info const *info, unsigned len)
函数 注册一个 i2c_board_info结构体来完成的。busnum 是总线编号,len设备个数
-
struct i2c_board_info {
-
char type[I2C_NAME_SIZE];
-
unsigned short flags;
-
unsigned short addr;
-
void *platform_data;
-
struct dev_archdata *archdata;
-
struct device_node *of_node;
-
int irq;
-
};
其中最主要的两个参数是type即驱动设备名称,这个名字要与后面提到的i2c_device_id结构体中的名字一致,addr从设备地址。
到此I2C平台设备添加完成。
二,注册I2C驱动
1,初始化一个i2c_driver结构体,定义如下
-
struct i2c_driver {
-
unsigned int class;
-
-
/* Notifies the driver that a new bus has appeared or is about to be
-
* removed. You should avoid using this, it will be removed in a
-
* near future.
-
*/
-
int (*attach_adapter)(struct i2c_adapter *) __deprecated;
-
int (*detach_adapter)(struct i2c_adapter *) __deprecated;
-
-
/* 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 *);
-
-
/* Alert callback, for example for the SMBus alert protocol.
-
* The format and meaning of the data value depends on the protocol.
-
* For the SMBus alert protocol, there is a single bit of data passed
-
* as the alert response's low bit ("event flag").
-
*/
-
void (*alert)(struct i2c_client *, unsigned int data);
-
-
/* 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 *, struct i2c_board_info *);
-
const unsigned short *address_list;
-
struct list_head clients;
-
};
.driver.name 驱动名字。不必和device的name相同。另外几个主要成员 是probe,remove,id_table,
struct i2c_device_id
*id_table 定义实例如下
struct i2c_device_id static const struct i2c_device_id rx8010_id[] = {
{ "rx8010", 0 },// 此名称必须与
i2c_board_info的type一致,系统依据此名称匹配设备和驱动
{ }
};id_table
调用函数 i2c_register_driver(THIS_MODULE, driver) 完成驱动注册。
阅读(1505) | 评论(0) | 转发(0) |