Linux驱动开发I2C
Posted XXX_UUU_XXX
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux驱动开发I2C相关的知识,希望对你有一定的参考价值。
Linux内核将I2C驱动分成两部分
- I2C总线驱动:SOC的I2C控制器驱动,也称为I2C适配器驱动。半导体厂商编写。
- I2C设备驱动:具体I2C设备的驱动。SOC使用者编写。
I2C总线驱动
Linux内核将SOC的I2C适配器抽象为i2c_adapter,include/linux/i2c.h。
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;
/* data fields that are valid for all devices */
struct rt_mutex bus_lock;
int timeout; /* in jiffies */
int retries;
struct device dev; /* the adapter device */
int nr;
char name[48];
struct completion dev_released;
struct mutex userspace_clients_lock;
struct list_head userspace_clients;
struct i2c_bus_recovery_info *bus_recovery_info;
const struct i2c_adapter_quirks *quirks;
;
- i2c_algorithm类型的指针变量algo,是I2C适配器与I2C设备通信的方法,include/linux/i2c.h。
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 *);
#if IS_ENABLED(CONFIG_I2C_SLAVE)
int (*reg_slave)(struct i2c_client *client);
int (*unreg_slave)(struct i2c_client *client);
#endif
;
- master_xfer:I2C适配器传输函数,与I2C设备进行通信。
- smbus_xfer:SMBUS总线的传输函数。
I2C总线驱动首先初始化i2c_adapter结构体变量,然后设置i2c_algorithm中的master_xfer函数,最后通过i2c_add_adapter或i2c_add_numbered_adapter函数向Linux系统注册。
int i2c_add_adapter(struct i2c_adapter *adapter);
int i2c_add_numbered_adapter(struct i2c_adapter *adap);
- adapter/adap:要注册的I2C适配器。
- 返回值:0,成功;负值,失败。
使用i2c_del_adapter删除I2C适配器。
void i2c_del_adapter(struct i2c_adapter *adap);
- adap:要删除的I2C适配器。
- 返回值:无。
I2C设备驱动
Linux定义i2c_client结构体描述设备信息,include/linux/i2c.h。
/**
* struct i2c_client - represent an I2C slave device
* @flags: I2C_CLIENT_TEN indicates the device uses a ten bit chip address;
* I2C_CLIENT_PEC indicates it uses SMBus Packet Error Checking
* @addr: Address used on the I2C bus connected to the parent adapter.
* @name: Indicates the type of the device, usually a chip name that's
* generic enough to hide second-sourcing and compatible revisions.
* @adapter: manages the bus segment hosting this I2C device
* @dev: Driver model device node for the slave.
* @irq: indicates the IRQ generated by this device (if any)
* @detected: member of an i2c_driver.clients list or i2c-core's
* userspace_devices list
* @slave_cb: Callback when I2C slave mode of an adapter is used. The adapter
* calls it to pass on slave events to the slave driver.
*/
struct i2c_client
unsigned short flags; /* 标志 */
unsigned short addr; /* 芯片地址,低7位 */
char name[I2C_NAME_SIZE]; /* 名字*/
struct i2c_adapter *adapter;/* I2C适配器 */
struct device dev; /* 设备结构体 */
int irq; /* 设备中断 */
struct list_head detected; /* 成员列表 */
#if IS_ENABLED(CONFIG_I2C_SLAVE)
i2c_slave_cb_t slave_cb; /* 回调 */
#endif
;
一个设备对应一个i2c_client,每检测到一个I2C设备就给这个I2C设备分配一个i2c_client。
Linux定义i2c_driver结构体描述驱动信息,include/linux/i2c.h。
/**
* struct i2c_driver - represent an I2C device driver
* @class: What kind of i2c device we instantiate (for detect)
* @attach_adapter: Callback for bus addition (deprecated)
* @probe: Callback for device binding
* @remove: Callback for device unbinding
* @shutdown: Callback for device shutdown
* @alert: Alert callback, for example for the SMBus alert protocol
* @command: Callback for bus-wide signaling (optional)
* @driver: Device driver model driver
* @id_table: List of I2C devices supported by this driver
* @detect: Callback for device detection
* @address_list: The I2C addresses to probe (for detect)
* @clients: List of detected clients we created (for i2c-core use only)
*
* The driver.owner field should be set to the module owner of this driver.
* The driver.name field should be set to the name of this driver.
*
* For automatic device detection, both @detect and @address_list must
* be defined. @class should also be set, otherwise only devices forced
* with module parameters will be created. The detect function must
* fill at least the name field of the i2c_board_info structure it is
* handed upon successful detection, and possibly also the flags field.
*
* If @detect is missing, the driver will still work fine for enumerated
* devices. Detected devices simply won't be supported. This is expected
* for the many I2C/SMBus devices which can't be detected reliably, and
* the ones which can always be enumerated in practice.
*
* 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
unsigned int class;
/* Notifies the driver that a new bus has appeared. You should avoid
* using this, it will be removed in a near future.
*/
int (*attach_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 *);
/* 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;
;
- probe和remove函数在I2C设备匹配成功和关闭驱动后执行。
- device_driver:驱动结构体,使用设备树时需要设置of_match_table成员变量。
- id_table:未使用设备树的匹配列表。
i2c_driver初始化完成后,使用i2c_register_driver向Linux内核注册。
int i2c_register_driver(struct module *owner,
struct i2c_driver *driver);
- owner:THIS_MODULE。
- driver:要注册的i2c_driver。
- 返回值:0,成功;负值,失败。
也可使用宏i2c_add_driver注册,对i2c_register_driver进行封装,只有一个driver参数。
#define i2c_add_driver(driver) i2c_register_driver(THIS_MODULE, driver)
使用i2c_del_driver注销I2C设备驱动。
void i2c_del_driver(struct i2c_driver *driver);
- driver:要注销的i2c_driver。
- 返回值:无。
i2c_driver注册流程
static int xxx_probe(struct i2c_client *client,
const struct i2c_device_id *id)
/* ... */
return 0;
static int xxx_remove(struct i2c_client *client)
/* ... */
return 0;
/* 无设备树匹配ID列表 */
static const struct i2c_device_id xxx_id[] =
"xxx", 0,
;
/* 设备树匹配列表 */
static const struct of_device_id xxx_of_match[] =
.compatible = "xxx",
;
/* I2C驱动结构体 */
static struct i2c_driver xxx_driver =
.probe = xxx_probe,
.remove = xxx_remove,
.driver =
.owner = THIS_MODULE,
.name = "xxx",
.of_match_table = xxx_of_match,
,
.id_table = xxx_id,
;
/* 驱动入口函数 */
static int __init xxx_init(void)
int ret = 0;
ret = i2c_add_driver(&xxx_driver);
return ret;
/* 驱动出口函数 */
static void __exit xxx_exit(void)
i2c_del_driver(&xxx_driver);
module_init(xxx_init);
module_exit(xxx_exit);
以上是关于Linux驱动开发I2C的主要内容,如果未能解决你的问题,请参考以下文章
i.MX6ULL驱动开发 | 11 - Linux I2C 驱动框架
i.MX6ULL驱动开发 | 11 - Linux I2C 驱动框架
Linux——Linux驱动之玩转I2C(中)自己实现一个I2C总线client设备(Linux下I2C驱动框架非设备树下的client设备树下添加I2C设备节点)
Linux——Linux驱动之玩转I2C(上)应用层下操作已有驱动的I2C设备实战(I2C总线特征时序查询I2C设备节点应用层调用I2C步骤)