I²C architecture is divided into three parts: I²c core, bus drive, device driver
I²c Core :
I²c Core provides a set of interface functions that do not depend on hardware, and the I²c-bus driver and device driver depend on the core of I²c as a link.
(1) Add/Remove I2c_adapter
int I2c_add_adapter (struct i2c_adapter *adap);
int I2c_del_adapter (struct i2c_adapter *adap);
(2) Add/Remove I2c_driver
int i2c_register_driver (struct module *owner, struct i2c_driver *drever);
int i2c_del_driver (struct i2c_driver *drever);
inline int i2c_add_driver (struct i2c_driver *drever);
(3) I2c_client attachment/Detachment
int i2c_attach_client (struct i2c_client *client);
int i2c_detach_client (struct i2c_client *client);
(4) I²c transmit, send and receive
int I2c_transfer (struct i2c_adapter *adap, struct i2c_msg *msgs, int num),/* for communication between the I²C adapter and the I²C device */
int i2c_master_send (struct i2c_client *client, const char *buf, int count);//Call I2c_transfer () function
int i2c_master_recv (struct i2c_client *client, char *buf, int count);
I²c Bus driver:
The I²C bus driver is the implementation of the adapter in the I²C hardware system. Contains the algorithm structure of the adapter data structure I2c_adapter and adapters primarily i2c_algorithm
I2c_adapter
1 structI2c_adapter2 {3 structModule *owner;/*Owning Module*/4UnsignedintId/*the type of algorithm, defined in I2c-id.h, starts with I2c_algo_*/5Unsignedint class;6 structI2c_algorithm *algo;/*Bus communication method struct body pointer*/7 void*algo_data;/*algorithm Data*/8 int(*client_register) (structI2c_client *);/*called when client registers*/9 int(*client_unregister) (structI2c_client *);/*called when client logs off*/Ten structSemaphore Bus_lock;/*Spin lock to control concurrent access*/ One structsemaphore Clist_lock; A inttimeout; - intretries;/*Retry Count*/ - structDevice Dev;/*Adapter Device*/ the structClass_device Class_dev;/*class Equipment*/ - intnr; - structList_head clients;/*Client Link Table header*/ - structlist_head list; + CharName[i2c_name_size];/*Adapter Name*/ - structCompletion dev_released;/*for synchronization*/ + structcompletion class_dev_released; A};
I2c_algorithm
1 structI2c_algorithm2 {3 int(*master_xfer) (structI2c_adapter *adap,structI2c_msg *msgs,intnum); 4 int(*smbus_xfer) (structI2c_adapter *adap, U16 addr, unsigned ShortFlagsCharRead_write, U8 Command,intSize, Union I2c_smbus_data *data); 5U32 (*functionality) (structI2c_adapter *); 6};
The I2C_ALGORITHM structure mainly implements the Master_xfer () function and the Functionalityh () function, where the Master_xfer () function defines the communication mode between the adapter and the device, FUNCTIONALITYH () The function is used to return algorithm supported communication protocols.
The i2c_adapter corresponds to a physical adapter, and the i2c_algorithm corresponds to a set of communication methods.
I²c Device driver:
The I²C device driver consists of two data structures i2c_driver and i2c_client.
I2c_driver
1 structI2c_driver2 {3 intID;4Unsignedint class;5 int(*attach_adapter) (structI2c_adapter *);/*dependent i2c_adapter function pointer*/6 int(*detach_adapter) (structI2c_adapter *);/*out of i2c_adapter function pointers*/7 int(*detach_client) (structI2c_client *);/*i²c client out of function pointer*/8 int(*probe) (structI2c_client *,Const structI2C_DEVICE_ID *);//current generic interface functions that are bound to the corresponding device9 int(*remove) (structI2c_client *);//An interface function for the current general and corresponding device to unbindTen void(*shutdown) (structI2c_client *);//turn off the device One int(*suspend) (structI2c_client *, pm_message_t MESG);//suspend the device, related to power management, to save power A int(*resume) (structI2c_client *);//recovering from a suspended state - void(*alert) (structI2c_client *, unsignedintdata); - int(*command) (structI2c_client *client, unsignedintCmdvoid*Arg); the structDevice_driver driver;//Drive model of I²C device - Const structI2C_DEVICE_ID *id_table;//Matching Device list - int(*detect) (structI2c_client *,structI2c_board_info *); - ConstUnsigned Short*address_list; + structlist_head clients; -};
I2c_client
1 structi2c_client2 {3Unsigned ShortFlags//I2c_client_ten indicates that the device uses a 10bit slave address, i2c_client_pec indicates that the device is using SMBus error detection4Unsigned ShortAddr//device from address, 7bit. Here is why the 7-bit, because the last thought 0 means write, 1 is read, by the 7bit address shift processing can be. Addr<<1 & 0x0 Write, addr<<1 | 0X01 is read. 5 CharName[i2c_name_size];//from the device name6 structI2c_adapter *adapter;//which adapter is attached to from the device7 structI2c_driver *driver;//the corresponding I²c drive pointer for this device8 structDevice Dev;//Device Model9 intIrq//interrupt number used by the deviceTen structList_head detected;//for linked list operations One};
I2c_driver corresponds to a set of drive methods, i2c_client corresponds to real physical equipment, each i²c device needs a i2c_client to describe.
LINUXI2C core, bus drive and device driver