linux驅動開發學習筆記二十四:I2C驅動框架介紹

I2C 是很常用的一個串行通信接口,用於連接各種外設、傳感器等器件,Linux 下的 I2C 驅動是有框架的,我們需要按照指定的框架去編寫 I2C 設備驅動。

爲了符合 Linux 的驅動分離與分層的思想,Linux內核將 I2C 驅動分爲兩部分:
①I2C 總線驅動,I2C 總線驅動就是 SOC 的 I2C 控制器驅動,也叫做 I2C 適配器驅動。
②I2C 設備驅動,I2C 設備驅動就是針對具體的 I2C 設備而編寫的驅動。

一、I2C 總線驅動

首先來看一下 I2C 總線,在platform中總線是虛擬出來的,目的是實現總線、設備、驅動框架。對於 I2C 而言,不需要虛擬出一條總線,直接使用 I2C總線即可。I2C 總線驅動重點是 I2C 適配器(也就是 SOC 的 I2C 接口控制器)驅動,這裏要用到兩個重要的數據結構:i2c_adapter 和i2c_algorithm,Linux 內核將 SOC 的 I2C 適配器(控制器) 抽象成 i2c_adapter,i2c_adapter 結構體定義在include/linux/i2c.h 文件中,結構體內容如下:

498 struct i2c_adapter {
499 	struct module *owner;
500 	unsigned int class; /* classes to allow probing for */
501 	const struct i2c_algorithm *algo; /* 總線訪問算法 */
502 	void *algo_data;
503     ..................
        ..................
520 };

第 501 行,i2c_algorithm 類型的指針變量 algo,對於一個 I2C 適配器,肯定要對外提供讀寫 API 函數,設備驅動程序可以使用這些 API 函數來完成讀寫操作。i2c_algorithm 就是 I2C 適配器與 IIC 設備進行通信的方法。

i2c_algorithm 結構體定義在 include/linux/i2c.h 文件中,內容如下(刪除條件編譯):

391 struct i2c_algorithm {
			......
398 		int (*master_xfer)(struct i2c_adapter *adap,struct i2c_msg *msgs,
399 		int num);
400 		int (*smbus_xfer) (struct i2c_adapter *adap, u16 addr,
401 		unsigned short flags, char read_write,
402 		u8 command, int size, union i2c_smbus_data *data);
403
404 		/* To determine what the adapter supports */
405 		u32 (*functionality) (struct i2c_adapter *);
			......
411 };

第 398 行,master_xfer 就是 I2C 適配器的傳輸函數,可以通過此函數來完成與 IIC 設備之間的通信。

第 400 行,smbus_xfer 就是 SMBUS 總線的傳輸函數。

綜上所述,I2C 總線驅動,或者說 I2C 適配器驅動的主要工作就是初始化 i2c_adapter 結構體變量,然後設置 i2c_algorithm 中的 master_xfer 函數。完成以後通過 i2c_add_numbered_adapter或 i2c_add_adapter 這兩個函數向系統註冊設置好的 i2c_adapter,這兩個函數的原型如下:

int i2c_add_adapter(struct i2c_adapter *adapter)
int i2c_add_numbered_adapter(struct i2c_adapter *adap)

這兩個函數的區別在於 i2c_add_adapter 使用動態的總線號,而 i2c_add_numbered_adapter使用靜態總線號。

函數參數和返回值含義如下:
adapter 或 adap:要添加到 Linux 內核中的 i2c_adapter,也就是 I2C 適配器。
返回值:0,成功;負值,失敗。

如果要刪除 I2C 適配器的話使用 i2c_del_adapter 函數即可,函數原型如下:

void i2c_del_adapter(struct i2c_adapter * adap)

函數參數和返回值含義如下:
adap:要刪除的 I2C 適配器。
返回值:無。

一般 SOC 的 I2C 總線驅動都是由半導體廠商編寫的,比如 I.MX6U 的 I2C 適配器驅動 NXP 已經編寫好了,這個不需要用戶去編寫。因此 I2C 總線驅動對我們這些 SOC 使用者來說是被屏蔽掉的,我們只要專注於 I2C 設備驅動即可。

二、I2C 設備驅動

I2C 設備驅動重點關注兩個數據結構:i2c_client 和 i2c_driver。i2c_client 就是描述設備信息的,i2c_driver 描述驅動內容,類似於 platform_driver。

  • i2c_client 結構體:i2c_client 結構體定義在 include/linux/i2c.h 文件中,內容如下:
217 struct i2c_client {
218 		unsigned short flags; /* 標誌 */
219 		unsigned short addr; /* 芯片地址,7 位,存在低 7 位*/
			......
222 		char name[I2C_NAME_SIZE]; /* 名字 */
223 		struct i2c_adapter *adapter; /* 對應的 I2C 適配器 */
224 		struct device dev; /* 設備結構體 */
225 		int irq; /* 中斷 */
226 		struct list_head detected;
			......
230 };

一個設備對應一個 i2c_client,每檢測到一個 I2C 設備就會給這個 I2C 設備分配一個i2c_client。

  • i2c_driver 結構體:i2c_driver 類似 platform_driver,是我們編寫 I2C 設備驅動重點要處理的內容,i2c_driver 結構體定義在 include/linux/i2c.h 文件中,內容如下:
161 struct i2c_driver {
162 		unsigned int class;
163
164 		/* Notifies the driver that a new bus has appeared. You should 
165 		* avoid using this, it will be removed in a near future.
166 		*/
167 		int (*attach_adapter)(struct i2c_adapter *) __deprecated;
168
169 		/* Standard driver model interfaces */
170 		int (*probe)(struct i2c_client *, const struct i2c_device_id *);
171 		int (*remove)(struct i2c_client *);
172
173 		/* driver model interfaces that don't relate to enumeration */
174 		void (*shutdown)(struct i2c_client *);
175
176 		/* Alert callback, for example for the SMBus alert protocol.
177 		* The format and meaning of the data value depends on the 
178 		* protocol.For the SMBus alert protocol, there is a single bit 
179 		* of data passed as the alert response's low bit ("event 
180 		flag"). */
181 		void (*alert)(struct i2c_client *, unsigned int data);
182
183 		/* a ioctl like command that can be used to perform specific 
184 		* functions with the device.
185 		*/
186 		int (*command)(struct i2c_client *client, unsigned int cmd,void *arg);
187
188 		struct device_driver driver;
189 		const struct i2c_device_id *id_table;
190
191 		/* Device detection callback for automatic device creation */
192 		int (*detect)(struct i2c_client *, struct i2c_board_info *);
193 		const unsigned short *address_list;
194 		struct list_head clients;
195 };

第 170 行,當 I2C 設備和驅動匹配成功以後 probe 函數就會執行,和 platform 驅動一樣。

第 188 行,device_driver 驅動結構體,如果使用設備樹的話,需要設置 device_driver 的of_match_table 成員變量,也就是驅動的兼容(compatible)屬性。

第 189 行,id_table 是傳統的、未使用設備樹的設備匹配 ID 表。

對於我們 I2C 設備驅動編寫人來說,重點工作就是構建i2c_driver,構建完成以後需要向Linux 內核註冊這個 i2c_driver。i2c_driver 註冊函數爲 int i2c_register_driver,此函數原型如下:

int i2c_register_driver(struct module *owner, 
 						struct i2c_driver *driver)

函數參數和返回值含義如下:
owner:一般爲 THIS_MODULE。
driver:要註冊的 i2c_driver。
返回值:0,成功;負值,失敗。

另外 i2c_add_driver 也常常用於註冊 i2c_driver,i2c_add_driver 是一個宏,定義如下:

587 #define i2c_add_driver(driver)  i2c_register_driver(THIS_MODULE, driver) 

i2c_add_driver 就是對 i2c_register_driver 做了一個簡單的封裝,只有一個參數,就是要註冊的 i2c_driver。

註銷 I2C 設備驅動的時候需要將前面註冊的 i2c_driver 從 Linux 內核中註銷掉,需要用到i2c_del_driver 函數,此函數原型如下:

void i2c_del_driver(struct i2c_driver *driver)

函數參數和返回值含義如下:
driver:要註銷的 i2c_driver。
返回值:無。

i2c_driver 的註冊示例代碼如下:

1 /* i2c 驅動的 probe 函數 */
2 static int xxx_probe(struct i2c_client *client,
					   const struct i2c_device_id *id)
3 { 
4 		/* 函數具體程序 */
5 		return 0; 
6 }
7
8 /* i2c 驅動的 remove 函數 */
9 static int xxx_remove(struct i2c_client *client)
10 {
11 		/* 函數具體程序 */
12 		return 0;
13 }
14
15 		/* 傳統匹配方式 ID 列表 */
16 		static const struct i2c_device_id xxx_id[] = {
17 		{"xxx", 0}, 
18 		{}
19 };
21 /* 設備樹匹配列表 */
22 static const struct of_device_id xxx_of_match[] = {
23 		{ .compatible = "xxx" },
24 		{ /* Sentinel */ }
25 };
26
27 /* i2c 驅動結構體 */
28 static struct i2c_driver xxx_driver = {
29 		.probe = xxx_probe,
30 		.remove = xxx_remove,
31 		.driver = {
32 			.owner = THIS_MODULE,
33 			.name = "xxx",
34 			.of_match_table = xxx_of_match,
35		 },
36 		.id_table = xxx_id,
37 };
38 
39 /* 驅動入口函數 */
40 static int __init xxx_init(void)
41 {
42 		int ret = 0;
43
44 		ret = i2c_add_driver(&xxx_driver);
45 		return ret;
46 }
47
48 /* 驅動出口函數 */
49 static void __exit xxx_exit(void)
50 {
51 		i2c_del_driver(&xxx_driver);
52 }
53
54 module_init(xxx_init);
55 module_exit(xxx_exit);

第 16~19 行,i2c_device_id,無設備樹的時候匹配 ID 表。

第 22~25 行,of_device_id,設備樹所使用的匹配表。

第 28~37 行,i2c_driver,當 I2C 設備和 I2C 驅動匹配成功以後 probe 函數就會執行,當註銷xxx_driver的時候remove函數會執行。這些和 platform 驅動一樣,probe 函數裏面基本就是標準的字符設備驅動的初始化和其他一些前期設置。

三、I2C 設備和驅動匹配過程

I2C 設備和驅動的匹配過程是由 I2C 核心來完成的,drivers/i2c/i2c-core.c 就是 I2C 的核心部分,I2C 核心提供了一些與具體硬件無關的 API 函數,比如前面講過的:

  • 1、i2c_adapter 註冊/註銷函數
int i2c_add_adapter(struct i2c_adapter *adapter)
int i2c_add_numbered_adapter(struct i2c_adapter *adap)
void i2c_del_adapter(struct i2c_adapter * adap)
  • 2、i2c_driver 註冊/註銷函數
int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
int i2c_add_driver (struct i2c_driver *driver)
void i2c_del_driver(struct i2c_driver *driver)

設備和驅動的匹配過程也是由 I2C 總線完成的,I2C 總線的數據結構爲 i2c_bus_type,定義在 drivers/i2c/i2c-core.c 文件,i2c_bus_type 內容如下:

736 struct bus_type i2c_bus_type = {
737 		.name = "i2c",
738 		.match = i2c_device_match,
739 		.probe = i2c_device_probe,
740 		.remove = i2c_device_remove,
741 		.shutdown = i2c_device_shutdown,
742 };

.match 就是 I2C 總線的設備和驅動匹配函數,在這裏就是 i2c_device_match 這個函數,此函數內容如下:

457 static int i2c_device_match(struct device *dev, struct device_driver *drv)
458 {
459 		struct i2c_client *client = i2c_verify_client(dev);
460 		struct i2c_driver *driver;
461
462 		if (!client)
463 			return 0;
464
465 		/* Attempt an OF style match */
466 		if (of_driver_match_device(dev, drv))
467 			return 1;
468
469 		/* Then ACPI style match */
470 		if (acpi_driver_match_device(dev, drv))
471 			return 1;
472
473 		driver = to_i2c_driver(drv);
474 		/* match on an id table if there is one */
475 		if (driver->id_table)
476 			return i2c_match_id(driver->id_table, client) != NULL;
477
478 		return 0;
479 }

第 466 行,of_driver_match_device 函數用於完成設備樹設備和驅動匹配。比較 I2C 設備節點的 compatible 屬性和 of_device_id 中的 compatible 屬性是否相等,如果相當的話就表示 I2C設備和驅動匹配。

第 470 行,acpi_driver_match_device 函數用於 ACPI 形式的匹配。

第 476 行,i2c_match_id 函數用於傳統的、無設備樹的 I2C 設備和驅動匹配過程。比較 I2C設備名字和 i2c_device_id 的 name 字段是否相等,相等的話就說明 I2C 設備和驅動匹配。

當然這一部分是不需要我們去編寫的,只是看一下設備和驅動的匹配過程

四、I2C 設備驅動編寫流程

1、 設備信息描述

使用設備樹的時候 I2C 設備信息通過創建相應的節點就行了,假如開發板在 I2C1 上接了 mag3110 磁力計芯片,因此必須在 i2c1 節點下創建 mag3110 子節點,然後在這個子節點內描述 mag3110 這個芯片的相關信息。如下所示:

1 &i2c1 {
2 		clock-frequency = <100000>;
3 		pinctrl-names = "default"; 
4 		pinctrl-0 = <&pinctrl_i2c1>;
5 		status = "okay"; 
6 
7 		mag3110@0e { 
8 			compatible = "fsl,mag3110"; 
9 			reg = <0x0e>;
10 			position = <2>;
11 		};
......
20 };

第 7~11 行,向 i2c1 添加 mag3110 子節點,第 7 行“mag3110@0e”是子節點名字,“@”後面的“0e”就是 mag3110 的 I2C 器件地址。第 8 行設置 compatible 屬性值爲“fsl,mag3110”。第 9 行的 reg 屬性也是設置 mag3110 的器件地址的,因此值爲 0x0e。I2C 設備節點的創建重點是 compatible 屬性和 reg 屬性的設置,一個用於匹配驅動,一個用於設置器件地址。

2、 設備數據收發處理流程

I2C 設備驅動首先要做的就是初始化 i2c_driver 並向 Linux 內核註冊。當設備和驅動匹配以後 i2c_driver 裏面的 probe 函數就會執行,probe 函數裏面所做的就是字符設備驅動那一套了。一般需要在 probe 函數裏面初始化 I2C 設備,要初始化 I2C 設備就必須能夠對 I2C 設備寄存器進行讀寫操作,這裏就要用到 i2c_transfer 函數了。i2c_transfer 函數最終會調用 I2C 適配器中 i2c_algorithm 裏面的 master_xfer 函數,i2c_transfer 函數原型如下:

int i2c_transfer(struct i2c_adapter *adap, 
				 struct i2c_msg *msgs, 
				 int num)

adap:所使用的 I2C 適配器,i2c_client 會保存其對應的 i2c_adapter。
msgs:I2C 要發送的一個或多個消息。
num:消息數量,也就是 msgs 的數量。
返回值:負值,失敗,其他非負值,發送的 msgs 數量。

我們重點來看一下 msgs 這個參數,這是一個 i2c_msg 類型的指針參數,I2C 進行數據收發說白了就是消息的傳遞,Linux 內核使用 i2c_msg 結構體來描述一個消息。i2c_msg 結構體定義在 include/uapi/linux/i2c.h 文件中,結構體內容如下:

68 struct i2c_msg {
69 		__u16 addr; /* 從機地址 */
70 		__u16 flags; /* 標誌 */
71 		#define I2C_M_TEN 0x0010
72		#define I2C_M_RD 0x0001
73 		#define I2C_M_STOP 0x8000
74 		#define I2C_M_NOSTART 0x4000
75 		#define I2C_M_REV_DIR_ADDR 0x2000 
76 		#define I2C_M_IGNORE_NAK 0x1000 
77 		#define I2C_M_NO_RD_ACK 0x0800
78 		#define I2C_M_RECV_LEN 0x0400
79 		__u16 len; /* 消息(本 msg)長度 */
80 		__u8 *buf; /* 消息數據 */
81 };

使用 i2c_transfer 函數發送數據之前要先構建好 i2c_msg,使用 i2c_transfer 進行 I2C 數據收發的示例代碼如下:

1 /* 設備結構體 */
2 struct xxx_dev { 
3 		......
4 		void *private_data; /* 私有數據,一般會設置爲 i2c_client */
5 };
6 
7 /*
8 * @description : 讀取 I2C 設備多個寄存器數據
9 * @param – dev : I2C 設備
10 * @param – reg : 要讀取的寄存器首地址
11 * @param – val : 讀取到的數據
12 * @param – len : 要讀取的數據長度
13 * @return : 操作結果
14 */
15 static int xxx_read_regs(struct xxx_dev *dev, u8 reg, void *val, int len)
16 {
17 		int ret;
18 		struct i2c_msg msg[2];
19 		struct i2c_client *client = (struct i2c_client *) dev->private_data;
20
21 		/* msg[0],第一條寫消息,發送要讀取的寄存器首地址 */
22 		msg[0].addr = client->addr; /* I2C 器件地址 */
23 		msg[0].flags = 0; /* 標記爲發送數據 */
24 		msg[0].buf = &reg; /* 讀取的首地址 */
25 		msg[0].len = 1; /* reg 長度 */
26
27 		/* msg[1],第二條讀消息,讀取寄存器數據 */
28 		msg[1].addr = client->addr; /* I2C 器件地址 */
29 		msg[1].flags = I2C_M_RD; /* 標記爲讀取數據 */
30 		msg[1].buf = val; /* 讀取數據緩衝區 */
31 		msg[1].len = len; /* 要讀取的數據長度 */
32
33 	    ret = i2c_transfer(client->adapter, msg, 2);
34 		if(ret == 2) {
35 			ret = 0;
36 		} else {
37 			ret = -EREMOTEIO;
38		}
39 		return ret;
40 }

第2-5行,設備結構體,在設備結構體裏面添加一個執行void的指針成員變量private_data,此成員變量用於保存設備的私有數據。在 I2C 設備驅動中我們一般將其指向 I2C 設備對應的i2c_client。 第 15~40 行,xxx_read_regs 函數用於讀取 I2C 設備多個寄存器數據。第 18 行定義了一個i2c_msg 數組,2 個數組元素,因爲 I2C 讀取數據的時候要先發送要讀取的寄存器地址,然後再讀取數據,所以需要準備兩個 i2c_msg。一個用於發送寄存器地址,一個用於讀取寄存器值。對於 msg[0],將 flags 設置爲 0,表示寫數據。msg[0]的 addr 是 I2C 設備的器件地址,msg[0]的 buf成員變量就是要讀取的寄存器地址。對於 msg[1],將 flags 設置爲 I2C_M_RD,表示讀取數據。msg[1]的 buf 成員變量用於保存讀取到的數據,len 成員變量就是要讀取的數據長度。調用i2c_transfer 函數完成 I2C 數據讀操作。

42 /*
43 * @description : 向 I2C 設備多個寄存器寫入數據
44 * @param – dev : 要寫入的設備結構體
45 * @param – reg : 要寫入的寄存器首地址
46 * @param – val : 要寫入的數據緩衝區
47 * @param – len : 要寫入的數據長度
48 * @return : 操作結果
49 */
50 static s32 xxx_write_regs(struct xxx_dev *dev, u8 reg, u8 *buf, u8 len)
51 {
52 		u8 b[256];
53 		struct i2c_msg msg;
54 		struct i2c_client *client = (struct i2c_client *) dev->private_data;
55 
56 		b[0] = reg; /* 寄存器首地址 */
57 		memcpy(&b[1],buf,len); /* 將要發送的數據拷貝到數組 b 裏面 */
58 
59 		msg.addr = client->addr; /* I2C 器件地址 */
60 		msg.flags = 0; /* 標記爲寫數據 */
61
62 		msg.buf = b; /* 要發送的數據緩衝區 */
63 		msg.len = len + 1; /* 要發送的數據長度 */
64
65 		return i2c_transfer(client->adapter, &msg, 1);
66 }

第 50~66 行,xxx_write_regs 函數用於向 I2C 設備多個寄存器寫數據,I2C 寫操作要比讀操作簡單一點,因此一個 i2c_msg 即可。數組 b 用於存放寄存器首地址和要發送的數據,第 59 行設置 msg 的 addr 爲 I2C 器件地址。第 60 行設置 msg 的 flags 爲 0,也就是寫數據。第 62 行設置要發送的數據,也就是數組 b。第 63 行設置 msg 的 len 爲 len+1,因爲要加上一個字節的寄存器地址。最後通過 i2c_transfer 函數完成向 I2C 設備的寫操作。

另外還有兩個API函數分別用於I2C數據的收發操作,這兩個函數最終都會調用i2c_transfer。

  • 首先來看一下 I2C 數據發送函數 i2c_master_send,函數原型如下:
int i2c_master_send(const struct i2c_client *client, 
					const char *buf, 
					int count)

函數參數和返回值含義如下:
client:I2C 設備對應的 i2c_client。
buf:要發送的數據。
count:要發送的數據字節數,要小於 64KB,因爲 i2c_msg 的 len 成員變量是一個 u16(無符號 16 位)類型的數據。
返回值:負值,失敗,其他非負值,發送的字節數。

  • I2C 數據接收函數爲 i2c_master_recv,函數原型如下:
int i2c_master_recv(const struct i2c_client *client, 
					char *buf, 
					int count)

函數參數和返回值含義如下:
client:I2C 設備對應的 i2c_client。
buf:要接收的數據。
count:要接收的數據字節數,要小於 64KB,因爲 i2c_msg 的 len 成員變量是一個 u16(無符號 16 位)類型的數據。
返回值:負值,失敗,其他非負值,發送的字節數

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章