零. Linux I2C驱动框架
一. I2C device Driver in Linux Kernel注册过程
- Get the I2C adapter.
- Create the
oled_i2c_board_info
structure and create a device using that. - Create the
i2c_device_id
for your slave device and register that. - Create the
i2c_driver
structure and add that to the I2C subsystem. - Now the driver is ready. So you can transfer the data between master and slave.
- Once you are done, then remove the device.
1.Get the I2C adapter
You can check that using the command ls -al /sys/bus/i2c/devices/
.
struct i2c_adapter *i2c_get_adapter(int nr);
2.Create the board info(是一个结构体)
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;
struct fwnode_handle * fwnode;
int irq;
};
You can use I2C_BOARD_INFO
macro to initialize the essential fields of struct i2c_board_info
.
I2C_BOARD_INFO ( dev_type, dev_addr);
dev_type – identifies the device type
dev_addr – the device’s address on the bus
3.Create Device
struct i2c_client * i2c_new_device ( struct i2c_adapter * adap, struct i2c_board_info const * info);
*adap – Adapter structure that we got from i2c_get_adapter()
*info – Board info structure that we have created
This will return the i2c_client structure. We can use this client for our future transfers.
4.Create the device id(一个结构)
struct i2c_device_id {
char name[I2C_NAME_SIZE];
kernel_ulong_t driver_data;
};
4.Create the i2c_driver(一个结构)
struct i2c_driver {
unsigned int class;
int (* attach_adapter) (struct i2c_adapter *);
int (* probe) (struct i2c_client *, const struct i2c_device_id *);
int (* remove) (struct i2c_client *);
void (* shutdown) (struct i2c_client *);
void (* alert) (struct i2c_client *, unsigned int data);
int (* command) (struct i2c_client *client, unsigned i