例項解析linux核心I2C體系結構(2)
四、在核心裡寫i2c裝置驅動的兩種方式
在一文介紹了利用/dev/i2c-0在應用層完成對i2c裝置的操作,但很多時候我們還是習慣為i2c裝置在核心層編寫驅動程式。目前核心支援兩種編寫i2c驅動程式的方式。下面分別介紹這兩種方式的實現。這裡分別稱這兩種方式為“Adapter方式(LEGACY)”和“Probe方式(new style)”。
(1) Adapter方式(LEGACY)
(下面的例項程式碼是在2.6.27核心的pca953x.c基礎上修改的,原始程式碼採用的是本文將要討論的第2種方式,即Probe方式)
● 構建i2c_driver
static struct i2c_driver pca953x_driver = { .driver = { .name= "pca953x", //名稱 }, .id= ID_PCA9555,//id號 .attach_adapter= pca953x_attach_adapter, //呼叫介面卡連線裝置 .detach_client= pca953x_detach_client,//讓裝置脫離介面卡 };
● 註冊i2c_driver
static int __init pca953x_init(void) { return i2c_add_driver(&pca953x_driver); } module_init(pca953x_init);
● attach_adapter動作
執行i2c_add_driver(&pca953x_driver)後會,如果核心中已經註冊了i2c介面卡,則順序呼叫這些介面卡來連線我們的i2c裝置。此過程是通過呼叫i2c_driver中的attach_adapter方法完成的。具體實現形式如下:
static int pca953x_attach_adapter(struct i2c_adapter *adapter) { return i2c_probe(adapter, &addr_data, pca953x_detect); /* adapter:介面卡 addr_data:地址資訊 pca953x_detect:探測到裝置後呼叫的函式 */ }
地址資訊addr_data是由下面程式碼指定的。 /* Addresses to scan */ static unsigned short normal_i2c[] = {0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,I2C_CLIENT_END}; I2C_CLIENT_INSMOD;
注意:normal_i2c裡的地址必須是你i2c晶片的地址。否則將無法正確探測到裝置。而I2C_ CLIENT_INSMOD是一個巨集,它會利用normal_i2c構建addr_data。
● 構建i2c_client,並註冊字元裝置驅動
i2c_probe在探測到目標裝置後,後呼叫pca953x_detect,並把當時的探測地址address作為引數傳入。
static int pca953x_detect(struct i2c_adapter *adapter, int address, int kind) { struct i2c_client *new_client; struct pca953x_chip *chip; //裝置結構體 int err = 0,result; dev_t pca953x_dev=MKDEV(pca953x_major,0);//構建裝置號,根據具體情況設定,這裡我只考慮了normal_i2c中只有一個地址匹配的情況。 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA| I2C_FUNC_SMBUS_WORD_DATA))//判定介面卡能力 goto exit; if (!(chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL))) { err = -ENOMEM; goto exit; } /****構建i2c-client****/ chip->client=kzalloc(sizeof(struct i2c_client),GFP_KERNEL); new_client = chip->client; i2c_set_clientdata(new_client, chip); new_client->addr = address; new_client->adapter = adapter; new_client->driver = &pca953x_driver; new_client->flags = 0; strlcpy(new_client->name, "pca953x", I2C_NAME_SIZE); if ((err = i2c_attach_client(new_client)))//註冊i2c_client goto exit_kfree; if (err) goto exit_detach; if(pca953x_major) { result=register_chrdev_region(pca953x_dev,1,"pca953x"); } else{ result=alloc_chrdev_region(&pca953x_dev,0,1,"pca953x"); pca953x_major=MAJOR(pca953x_dev); } if (result < 0) { printk(KERN_NOTICE "Unable to get pca953x region, error %d\n", result); return result; } pca953x_setup_cdev(chip,0); //註冊字元裝置,此處不詳解 return 0; exit_detach: i2c_detach_client(new_client); exit_kfree: kfree(chip); exit: return err; }
i2c_check_functionality用來判定設配器的能力,這一點非常重要。你也可以直接檢視對應設配器的能力,如
static const struct i2c_algorithm smbus_algorithm = { .smbus_xfer= i801_access, .functionality= i801_func, }; static u32 i801_func(struct i2c_adapter *adapter) { return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_WRITE_I2C_BLOCK | (isich4 ? I2C_FUNC_SMBUS_HWPEC_CALC : 0); }
● 字元驅動的具體實現
struct file_operations pca953x_fops = { .owner = THIS_MODULE, .ioctl= pca953x_ioctl, .open= pca953x_open, .release =pca953x_release, };
字元裝置驅動本身沒有什麼好說的,這裡主要想說一下,如何在驅動中呼叫i2c設配器幫我們完成資料傳輸。
目前設配器主要支援兩種傳輸方法:smbus_xfer和master_xfer。一般來說,如果設配器支援了master_xfer那麼它也可以模擬支援smbus的傳輸。但如果只實現smbus_xfer,則不支援一些i2c的傳輸。
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);
master_xfer中的引數設定,和前面的使用者空間程式設計一致。現在只是要在驅動中構建相關的引數然後呼叫i2c_transfer來完成傳輸既可。
int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
smbus_xfer中的引數設定及呼叫方法如下:
static int pca953x_write_reg(struct pca953x_chip *chip, int reg, uint16_t val) { int ret; ret = i2c_smbus_write_word_data(chip->client, reg << 1, val); if (ret < 0) { dev_err(&chip->client->dev, "failed writing register\n"); return -EIO; } return 0; }
上面函式完成向晶片的地址為reg的暫存器寫一個16bit的資料。i2c_smbus_write_word_data的實現如下:
s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value) { union i2c_smbus_data data; data.word = value; return i2c_smbus_xfer(client->adapter,client->addr,client->flags, I2C_SMBUS_WRITE,command, I2C_SMBUS_WORD_DATA,&data); }
從中可以看出smbus傳輸一個16位資料的方法。其它操作如:字元寫、字元讀、字讀、塊操作等,可以參考核心的i2c-core.c中提供的方法。
● 登出i2c_driver
static void __exit pca953x_exit(void) { i2c_del_driver(&pca953x_driver); } module_exit(pca953x_exit);
● detach_client動作
順序呼叫核心中註冊的介面卡來斷開我們註冊過的i2c裝置。此過程通過呼叫i2c_driver中的attach_adapter方法完成的。具體實現形式如下:
static int pca953x_detach_client(struct i2c_client *client) { int err; struct pca953x_chip *data; if ((err = i2c_detach_client(client)))//斷開i2c_client return err; data=i2c_get_clientdata(client); cdev_del(&(data->cdev)); unregister_chrdev_region(MKDEV(pca953x_major, 0), 1); kfree(data->client); kfree(data); return 0; }
(2) Probe方式(new style)
● 構建i2c_driver
和LEGACY方式一樣,也需要構建i2c_driver,但是內容有所不同。
static struct i2c_driver pca953x_driver = { .driver = { .name= "pca953x", }, .probe= pca953x_probe, //當有i2c_client和i2c_driver匹配時呼叫 .remove= pca953x_remove,//登出時呼叫 .id_table= pca953x_id,//匹配規則 };
● 註冊i2c_driver
static int __init pca953x_init(void) { return i2c_add_driver(&pca953x_driver); } module_init(pca953x_init);
在註冊i2c_driver的過程中,是將driver註冊到了i2c_bus_type的總線上。此匯流排的匹配規則是:
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_client的名稱和id_table中的名稱做匹配的。本驅動中的id_table為
static const struct i2c_device_id pca953x_id[] = { { "pca9534", 8, }, { "pca9535", 16, }, { "pca9536", 4, }, { "pca9537", 4, }, { "pca9538", 8, }, { "pca9539", 16, }, { "pca9554", 8, }, { "pca9555", 16, }, { "pca9557", 8, }, { "max7310", 8, }, { } };
看到現在我們應該會有這樣的疑問,在Adapter模式中,i2c_client是我們自己構造出來的,而現在的i2c_client是從哪來的呢?看看下面的解釋
● 註冊i2c_board_info
對於Probe模式,通常在平臺程式碼中要完成i2c_board_info的註冊。方法如下:
static struct i2c_board_info __initdata test_i2c_devices[] = { { I2C_BOARD_INFO("pca9555", 0x27),//pca9555為晶片名稱,0x27為晶片地址 .platform_data = &pca9555_data, }, { I2C_BOARD_INFO("mt9v022", 0x48), .platform_data = &iclink[0], /* With extender */ }, { I2C_BOARD_INFO("mt9m001", 0x5d), .platform_data = &iclink[0], /* With extender */ }, }; i2c_register_board_info(0, test_i2c_devices,ARRAY_SIZE(test_i2c_devices)); //註冊
i2c_client就是在註冊過程中構建的。但有一點需要注意的是i2c_register_board_info並沒有EXPORT_SYMBOL給模組使用。
● 字元驅動註冊
在Probe方式下,新增字元驅動的位置在pca953x_probe中。
static int __devinit pca953x_probe(struct i2c_client *client,const struct i2c_device_id *id) { …… /****字元裝置驅動註冊位置****/ …… return 0; }
● 登出i2c_driver
static void __exit pca953x_exit(void) { i2c_del_driver(&pca953x_driver); } module_exit(pca953x_exit);
● 登出字元裝置驅動
在Probe方式下,登出字元驅動的位置在pca953x_remove中。
static int __devinit pca953x_remove (struct i2c_client *client) { …… /****字元裝置驅動登出的位置****/ …… return 0; }
● I2C裝置的資料互動方法(即:呼叫介面卡操作裝置的方法)和Adapter方式下相同。