系列:
內核版本:v4.14
構成
在Linux的I2C架構如圖:
內核空間部分可以分為:i2c設備驅動、i2c核心以及i2c總線驅動。
- i2c核心:框架的實現;提供i2c總線驅動和設備驅動的注冊、注銷方法;i2c通信方法(algorithm)上層的,與具體適配器無關的代碼以及探測設備、檢測設備地址的上層代碼等。這一部分的工作由內核開發者完成。
- i2c總線驅動:具體控制器的實現;i2c總線驅動是對i2c硬件體系結構中適配器端的實現,說白了,就是怎么操作i2c模塊工作。適配器可由CPU控制,甚至直接集成到cpu里面( algorithm driver
adapter driver) - i2c設備:對i2c硬件體系結構中設備端的實現,比如說板上的EEPROM設備等。設備一般掛接在cpu控制的i2c適配器上,通過i2c適配器與cpu交換數據。( chip drivers, 包括多種類型,如RTC, EEPROM, I/O expander, hardware monitoring, sound, video等)
名詞解釋:
- i2c-adapter(適配器):指的是CPU實際的I2C控制器(例如I2C0,I2C1);
- i2c-device(設備):指的是I2C總線上的從設備(例如某片EEPROM,某個觸摸屏);
- i2c algorithm(算法、實現方法):這里指的是對i2c設備一套對應的通信方法。
分層的好處:
- 讓工程師們各司其職,只關心自己應該實現的部分
- 不需要為每一個i2c控制器編寫所有從設備的控制代碼,只需要分別完成n個控制器的控制接口,m個從設備的訪問實現,即可實現任意的控制器訪問任意的從設備(假設硬件連接支持)
原型
以下原型均定義在 include/linux/i2c.h
中,隨着內核版本的不同有差異,但差異不大。
i2c 設備驅動
i2c_driver:代表一個i2c設備驅動;
i2c 設備驅動要使用i2c_driver 和i2c_client數據結構並填充i2c_driver中的成員函數
/**
* 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 - soon to be deprecated
* @probe_new: New 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)
* @disable_i2c_core_irq_mapping: Tell the i2c-core to not do irq-mapping
*
* 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 *); // 解除綁定時調用的回調函數
/* New driver model interface to aid the seamless removal of the
* current probe()'s, more commonly unused than used second parameter.
*/
int (*probe_new)(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").
* For the SMBus Host Notify protocol, the data corresponds to the
* 16-bit payload data reported by the slave device acting as master.
*/
void (*alert)(struct i2c_client *, enum i2c_alert_protocol protocol,
unsigned int data); // 警告回調函數(例如SMBus警報協議)
/* 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); // 類似於ioctl 的命令控制函數
struct device_driver driver; // 設備驅動模型中的驅動
const struct i2c_device_id *id_table; // 這個i2c驅動支持的設備鏈表
/* Device detection callback for automatic device creation */
int (*detect)(struct i2c_client *, struct i2c_board_info *); // 檢測設備的回調函數;
const unsigned short *address_list; // 要探測的I2C地址(用於檢測)
struct list_head clients; // 我們創建的檢測到的clients(僅供i2c核心使用)
bool disable_i2c_core_irq_mapping;
};
例如:RTC設備的驅動
/* drivers/rtc/rtc-ds1307.c */
static struct i2c_driver ds1307_driver = {
.driver = {
.name = "rtc-ds1307",
.of_match_table = of_match_ptr(ds1307_of_match),
.acpi_match_table = ACPI_PTR(ds1307_acpi_ids),
},
.probe = ds1307_probe,
.id_table = ds1307_id,
};
i2c 客戶端
i2c_client:代表一個連接到i2c_bus總線上的從設備。
/**
* struct i2c_client - represent an I2C slave device
* @flags:
- I2C_CLIENT_TEN : the device uses a ten bit chip address; 表示i2c從設備使用的芯片地址為10bit
- I2C_CLIENT_PEC : it uses SMBus Packet Error Checking; 表示設備使用SMBus錯誤檢查
* @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.
*
* An i2c_client identifies a single device (i.e. chip) connected to an
* i2c bus. The behaviour exposed to Linux is defined by the driver
* managing the device.
*/
struct i2c_client {
unsigned short flags; // 一個標示,豐富這個設備的特殊細節
unsigned short addr; /* chip address - NOTE: 7bit;addresses are stored in the _LOWER_ 7 bits */ // 從設備在連接到相應適配器總線上使用的地址;默認使用低七位。
char name[I2C_NAME_SIZE]; // 設備的名字;
struct i2c_adapter *adapter; /* the adapter we sit on */ // 掛接設備的適配器;
struct device dev; /* the device structure */ // 訪問設備的驅動;
int irq; /* irq issued by device */ // 表明由設備產生的中斷;
struct list_head detected; // 一個i2c_driver支持的client的數量或i2c-core的用戶空間設備的鏈表。
#if IS_ENABLED(CONFIG_I2C_SLAVE)
i2c_slave_cb_t slave_cb; /* callback for slave mode */ // 從模式下的回調函數
#endif
};
i2c_client的信息通常在BSP的板文件中通過i2c_board_info填充, 如下面的代碼就定義了一個I2C設備的ID為“wm8580”、 地址為0x1b、 的i2c_client:
static struct i2c_board_info i2c_devs0[] __initdata = {
{
I2C_BOARD_INFO("wm8580", 0x1b),
},
};
struct i2c_board_info {
char type[I2C_NAME_SIZE];
unsigned short flags;
unsigned short addr;
void *platform_data;
struct dev_archdata *archdata;
#ifdef CONFIG_OF
struct device_node *of_node;
#endif
int irq;
};
i2c適配器
i2c_adapter:一個用於標識物理總線(也就是i2c總線)連同訪問它必要的算法的一個結構
/*
* i2c_adapter is the structure used to identify a physical i2c bus along
* with the access algorithms necessary to access it.
*/
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 */
const struct i2c_lock_operations *lock_ops;
struct rt_mutex bus_lock;
struct rt_mutex mux_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;
struct irq_domain *host_notify_domain;
};
i2c_algorithm中的關鍵函數master_xfer() 用於產生I2C訪問周期需要的信號, 以i2c_msg(即I2C消息) 為單位(i2c_msg中的成員表明了I2C的傳輸地址、 方向、 緩沖區、 緩沖區長度等信息) 。
/**
* struct i2c_msg - an I2C transaction segment beginning with START
* @addr: Slave address, either seven or ten bits. When this is a ten
* bit address, I2C_M_TEN must be set in @flags and the adapter
* must support I2C_FUNC_10BIT_ADDR.
* @flags: I2C_M_RD is handled by all adapters. No other flags may be
* provided unless the adapter exported the relevant I2C_FUNC_*
* flags through i2c_check_functionality().
* @len: Number of data bytes in @buf being read from or written to the
* I2C slave address. For read transactions where I2C_M_RECV_LEN
* is set, the caller guarantees that this buffer can hold up to
* 32 bytes in addition to the initial length byte sent by the
* slave (plus, if used, the SMBus PEC); and this value will be
* incremented by the number of block data bytes received.
* @buf: The buffer into which data is read, or from which it's written.
*
* An i2c_msg is the low level representation of one segment of an I2C
* transaction. It is visible to drivers in the @i2c_transfer() procedure,
* to userspace from i2c-dev, and to I2C adapter drivers through the
* @i2c_adapter.@master_xfer() method.
*
* Except when I2C "protocol mangling" is used, all I2C adapters implement
* the standard rules for I2C transactions. Each transaction begins with a
* START. That is followed by the slave address, and a bit encoding read
* versus write. Then follow all the data bytes, possibly including a byte
* with SMBus PEC. The transfer terminates with a NAK, or when all those
* bytes have been transferred and ACKed. If this is the last message in a
* group, it is followed by a STOP. Otherwise it is followed by the next
* @i2c_msg transaction segment, beginning with a (repeated) START.
*
* Alternatively, when the adapter supports I2C_FUNC_PROTOCOL_MANGLING then
* passing certain @flags may have changed those standard protocol behaviors.
* Those flags are only for use with broken/nonconforming slaves, and with
* adapters which are known to support the specific mangling options they
* need (one or more of IGNORE_NAK, NO_RD_ACK, NOSTART, and REV_DIR_ADDR).
*/
struct i2c_msg {
__u16 addr; /* slave address */
__u16 flags;
#define I2C_M_RD 0x0001 /* read data, from slave to master */
/* I2C_M_RD is guaranteed to be 0x0001! */
#define I2C_M_TEN 0x0010 /* this is a ten bit chip address */
#define I2C_M_RECV_LEN 0x0400 /* length will be first received byte */
#define I2C_M_NO_RD_ACK 0x0800 /* if I2C_FUNC_PROTOCOL_MANGLING */
#define I2C_M_IGNORE_NAK 0x1000 /* if I2C_FUNC_PROTOCOL_MANGLING */
#define I2C_M_REV_DIR_ADDR 0x2000 /* if I2C_FUNC_PROTOCOL_MANGLING */
#define I2C_M_NOSTART 0x4000 /* if I2C_FUNC_NOSTART */
#define I2C_M_STOP 0x8000 /* if I2C_FUNC_PROTOCOL_MANGLING */
__u16 len; /* msg length */
__u8 *buf; /* pointer to msg data */
};
i2c通信方法
i2c_algorithm是為一類使用相同總線算法尋址的一個接口。
-
當適配器不能使用i2c訪問設備時,把master_xfer設置為NULL
-
如果一個適配器可以做SMBus訪問時,設置smbus_xfer;如果把smbus_xfer設置成NULL,SMBus協議使用通用I2C模擬的消息。
/**
* struct i2c_algorithm - represent I2C transfer method
* @master_xfer:
Issue a set of i2c transactions to the given I2C adapter defined by the
msgs array, with num messages available to transfer via the adapter
specified by adap.
* @smbus_xfer:
Issue smbus transactions to the given I2C adapter. If this is not present,
then the bus layer will try and convert the SMBus calls into I2C transfers
instead.
* @functionality: Return the flags that this algorithm/adapter pair supports
* from the I2C_FUNC_* flags.
* @reg_slave: Register given client to I2C slave mode of this adapter
* @unreg_slave: Unregister given client from I2C slave mode of this adapter
*
* The following structs are for those who like to implement new bus drivers:
* i2c_algorithm is the interface to a class of hardware solutions which can
* be addressed using the same bus algorithms - i.e. bit-banging or the PCF8584
* to name two of the most common.
*
* The return codes from the @master_xfer field should indicate the type of
* error code that occurred during the transfer, as documented in the kernel
* Documentation file Documentation/i2c/fault-codes.
*/
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);
// 向msgs數組定義的給定i2c適配器發出一組i2c事務,其中num條消息可通過adap指定的適配器傳輸。
int (*smbus_xfer) (struct i2c_adapter *adap, u16 addr,
unsigned short flags, char read_write,
u8 command, int size, union i2c_smbus_data *data);
// 向給定的I2C適配器發出smbus事務。如果這不存在,那么總線層將嘗試將SMBus調用轉換為I2C傳輸。
/* 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
};
對象之間的關系
i2c_adapter和i2c_algorithm
由於i2c_adapter對應與物理上的一個適配器,而i2c_algorithm對應一套通信方法。
一個i2c適配器需要i2c_algorithm中提供的通信函數來控制適配器上產生特定的訪問周期。
缺少i2c_algorithm的i2c_adapter什么也做不了,因此i2c_adapter中包含其使用i2c_algorithm的指針。
i2c_driver和i2c_client
i2c_driver對應於一套驅動方法, 其主要成員函數是probe()
、remove()
、 suspend()
、resume()
等;
另外, struct i2c_device_id
形式的id_table
是該驅動所支持的I2C設備的ID表。 i2c_client對應於真實的物理設備, 每個I2C設備都需要一個i2c_client
來描述。 i2c_driver
與i2c_client
的關系是一對多, 一個i2c_driver可以支持多個同類型的i2c_client
。
每個探測到的設備通過在client數據結構中得到自己的數據
在I2C總線驅動i2c_bus_type的match()
函數i2c_device_match()
中, 會調用i2c_match_id()
函數匹配在板文件中定義的ID和i2c_driver所支持的ID表。
static int i2c_device_match(struct device *dev, struct device_driver *drv)
{
struct i2c_client *client = i2c_verify_client(dev);
struct i2c_driver *driver;
if (!client)
return 0;
driver = to_i2c_driver(drv);
/* match on an id table if there is one */
if (driver->id_table)
return i2c_match_id(driver->id_table, client) != NULL;
return 0;
}
static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
const struct i2c_client *client)
{
while (id->name[0]) {
if (strcmp(client->name, id->name) == 0)
return id;
id++;
}
return NULL;
}
i2c_adpater與i2c_client
i2c_adpater與i2c_client的關系與I2C硬件體系中適配器和設備的關系一致, 即i2c_client依附於i2c_adpater。 由於一個適配器可以連接多個I2C設備, 所以一個i2c_adpater也可以被多個i2c_client依附,i2c_adpater中包括依附於它的i2c_client的鏈表。
參考
https://i2c.wiki.kernel.org/index.php/Driver_Architecture
https://blog.csdn.net/xie0812/article/details/22942375