i2c_driver结构体原型如下:
struct i2c_driver {
 unsigned int class;  int (*attach_adapter)(struct i2c_adapter *);  int (*detach_adapter)(struct i2c_adapter *);  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;  int (*detect)(struct i2c_client *, struct i2c_board_info *);  const unsigned short *address_list;  struct list_head clients; }; |
主要的成员描述如下:
attach_adapter:依附i2c_adapter函数指针
detach_adapter:脱离i2c_adapter函数指针
driver:struct device_driver类型的成员,指定驱动程序的名称和所属的总线类型。
probe:指向设备探测函数的回调函数指针,在设备匹配时调用。
remove:指向设备移除函数的回调函数指针,在设备被卸载时调用。
id_table:指向一个数组的指针,用于匹配驱动程序和I2C设备。
detect:指向设备检测函数的回调函数指针,用于检测特定类型的设备是否存在。
当I2C设备和驱动匹配成功以后probe函数就会执行,和platform驱动一样,简单注册示例如下:
#include <linux/i2c.h>
static int my_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id) { // I2C设备探测函数,处理设备初始化和配置 // ... return 0; } static int my_i2c_remove(struct i2c_client *client) { // I2C设备移除函数,处理设备的清理操作 // ... return 0; } //传统匹配方式 ID 列表 static const struct i2c_device_id my_i2c_id[] = { { "my_i2c_device", 0 }, { } }; //设备树匹配列表 static const struct of_device_id my_i2c_of_match[] = {   { .compatible = "my_i2c_device" },   { /* Sentinel */ } }; MODULE_DEVICE_TABLE(i2c, my_i2c_id); static struct i2c_driver my_i2c_driver = { .driver = { .name = "my_i2c_driver", .owner = THIS_MODULE, .of_match_table = my_i2c_of_match }, .probe = my_i2c_probe, .remove = my_i2c_remove, .id_table = my_i2c_id, }; // 驱动程序初始化函数 static int __init my_i2c_driver_init(void) { return i2c_add_driver(&my_i2c_driver); } // 驱动程序卸载函数 static void __exit my_i2c_driver_exit(void) { i2c_del_driver(&my_i2c_driver); } module_init(my_i2c_driver_init); module_exit(my_i2c_driver_exit); MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("Sample I2C Driver"); MODULE_LICENSE("GPL"); |