實例解析linux內核I2C體系結構(2)
時間:2018-08-16作者:華清遠見(jian)
四、在(zai)內核里寫(xie)i2c設備驅動的兩種方式 在《實例解析linux內核I2C體系結構(1)》一(yi)文介紹(shao)(shao)了(le)利用(yong)/dev/i2c-0在應用(yong)層(ceng)完(wan)成(cheng)對i2c設備的(de)操作,但(dan)很多時候我們還是習(xi)慣(guan)為i2c設備在內核層(ceng)編寫驅動(dong)程序。目(mu)前(qian)內核支持兩種編寫i2c驅動(dong)程序的(de)方(fang)(fang)式(shi)(shi)(shi)(shi)。下面分(fen)別介紹(shao)(shao)這(zhe)兩種方(fang)(fang)式(shi)(shi)(shi)(shi)的(de)實現。這(zhe)里分(fen)別稱這(zhe)兩種方(fang)(fang)式(shi)(shi)(shi)(shi)為“Adapter方(fang)(fang)式(shi)(shi)(shi)(shi)(LEGACY)”和“Probe方(fang)(fang)式(shi)(shi)(shi)(shi)(new style)”。 (1) Adapter方式(LEGACY) (下面的(de)實例代碼是在2.6.27內(nei)核的(de)pca953x.c基礎上(shang)修改的(de),原始代碼采用(yong)的(de)是本(ben)文將(jiang)要討論的(de)第(di)2種方式,即Probe方式) ● 構建(jian)i2c_driver
static struct i2c_driver pca953x_driver = { ● 注(zhu)冊i2c_driver
static int __init pca953x_init(void) ● attach_adapter動作 執行i2c_add_driver(&pca953x_driver)后會(hui),如(ru)果(guo)內核中已經注冊了i2c適配器(qi),則(ze)順序調用(yong)這些(xie)適配器(qi)來(lai)連接我們的(de)i2c設備。此(ci)過程是(shi)通(tong)過調用(yong)i2c_driver中的(de)attach_adapter方法完(wan)成的(de)。具(ju)體(ti)實現(xian)形式如(ru)下:
static int pca953x_attach_adapter(struct i2c_adapter *adapter)
地址信息addr_data是由下面代碼指定的。 注(zhu)意:normal_i2c里的地(di)址(zhi)(zhi)必須是(shi)你i2c芯片(pian)的地(di)址(zhi)(zhi)。否(fou)則將無法(fa)正確探測到(dao)設(she)備。而I2C_ CLIENT_INSMOD是(shi)一個宏,它會(hui)利用(yong)normal_i2c構建(jian)addr_data。 ● 構建i2c_client,并注冊(ce)字符(fu)設(she)備驅動 i2c_probe在(zai)探測到目標設備后,后調用pca953x_detect,并把當(dang)時的探測地(di)址address作為參數傳入(ru)。
static int pca953x_detect(struct i2c_adapter *adapter, int address, int kind) i2c_check_functionality用來(lai)判定設配(pei)器(qi)的能力,這一點非常重要。你也可以直接查看(kan)對應設配(pei)器(qi)的能力,如
static const struct i2c_algorithm smbus_algorithm = { ● 字符(fu)驅動的(de)具(ju)體實現
struct file_operations pca953x_fops = { 字符設(she)備驅(qu)動本身沒(mei)有(you)什么好說的,這里主(zhu)要想說一下(xia),如何(he)在驅(qu)動中(zhong)調(diao)用i2c設(she)配器(qi)幫我們完成數據(ju)傳(chuan)輸。 目前設配器(qi)(qi)主要支(zhi)(zhi)持(chi)兩種傳(chuan)輸(shu)方法:smbus_xfer和master_xfer。一(yi)般來說(shuo),如果設配器(qi)(qi)支(zhi)(zhi)持(chi)了master_xfer那(nei)么它也可以模擬支(zhi)(zhi)持(chi)smbus的傳(chuan)輸(shu)。但如果只實現smbus_xfer,則不支(zhi)(zhi)持(chi)一(yi)些i2c的傳(chuan)輸(shu)。
int (*master_xfer)(struct i2c_adapter *adap,struct i2c_msg *msgs,int num); master_xfer中的參數設(she)置(zhi),和(he)前面的用(yong)戶(hu)空間(jian)編程一致(zhi)。現在(zai)只是要在(zai)驅動(dong)中構建相(xiang)關的參數然后調用(yong)i2c_transfer來(lai)完成傳輸既可。 int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num) smbus_xfer中的參數(shu)設置(zhi)及調用方法(fa)如下:
static int pca953x_write_reg(struct pca953x_chip *chip, int reg, uint16_t val) 上(shang)面函數完(wan)成向芯(xin)片的地址為reg的寄(ji)存器寫一個16bit的數據。i2c_smbus_write_word_data的實現如下:
s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value) 從中(zhong)(zhong)可以(yi)看出smbus傳輸一個16位數據(ju)的(de)(de)方法(fa)。其它(ta)操(cao)作如:字(zi)符寫、字(zi)符讀(du)、字(zi)讀(du)、塊操(cao)作等,可以(yi)參(can)考內核的(de)(de)i2c-core.c中(zhong)(zhong)提供的(de)(de)方法(fa)。 ● 注銷i2c_driver
static void __exit pca953x_exit(void) ● detach_client動作 順序調用內核中注冊的(de)適配器來斷開(kai)我們注冊過的(de)i2c設備。此過程通過調用i2c_driver中的(de)attach_adapter方法完(wan)成的(de)。具體實現形(xing)式如下:
static int pca953x_detach_client(struct i2c_client *client) (2) Probe方式(shi)(new style) ● 構建i2c_driver 和LEGACY方式一樣,也需(xu)要構建i2c_driver,但是(shi)內容有所不同。
static struct i2c_driver pca953x_driver = { ● 注冊i2c_driver
static int __init pca953x_init(void) 在注(zhu)冊i2c_driver的(de)(de)過程中,是(shi)將driver注(zhu)冊到了i2c_bus_type的(de)(de)總(zong)線上(shang)。此總(zong)線的(de)(de)匹配規則(ze)是(shi):
static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id, 可以看(kan)出是利(li)用i2c_client的(de)(de)名(ming)稱和id_table中的(de)(de)名(ming)稱做匹配的(de)(de)。本驅動中的(de)(de)id_table為
static const struct i2c_device_id pca953x_id[] = { 看到現在我們(men)應該會有這樣的(de)疑問,在Adapter模式中,i2c_client是(shi)我們(men)自己構(gou)造出來(lai)的(de),而現在的(de)i2c_client是(shi)從哪來(lai)的(de)呢(ni)?看看下面(mian)的(de)解釋 ● 注冊i2c_board_info 對于Probe模(mo)式(shi),通常在平臺代碼中要完(wan)成i2c_board_info的注冊。方法如下:
static struct i2c_board_info __initdata test_i2c_devices[] = { i2c_client就是(shi)在注冊過程(cheng)中構建的。但有一點需要注意的是(shi)i2c_register_board_info并沒有EXPORT_SYMBOL給模(mo)塊使(shi)用(yong)。 ● 字符驅動注冊(ce) 在(zai)Probe方式下,添加字符驅動(dong)的位置在(zai)pca953x_probe中。
static int __devinit pca953x_probe(struct i2c_client *client,const struct i2c_device_id *id) ● 注銷(xiao)i2c_driver
static void __exit pca953x_exit(void) ● 注銷字符(fu)設備驅動 在(zai)Probe方式下,注(zhu)銷(xiao)字符驅動的位置在(zai)pca953x_remove中。
static int __devinit pca953x_remove (struct i2c_client *client) ● I2C設備(bei)的數據交互方法(即:調用適(shi)配器操作設備(bei)的方法)和Adapter方式下相同。 發表評論
|
全國咨詢電話(hua):400-611-6270,雙休日(ri)及節假日(ri)請致電值班手機:15010390966
在線咨詢: 曹老(lao)(lao)師(shi)(shi)QQ(3337544669), 徐老(lao)(lao)師(shi)(shi)QQ(1462495461), 劉老(lao)(lao)師(shi)(shi) QQ(3108687497)
企(qi)業培訓(xun)洽(qia)談專線(xian):010-82600901,院校合作洽(qia)談專線(xian):010-82600350,在(zai)線(xian)咨詢:QQ(248856300)
Copyright 2004-2018 華清遠(yuan)見(jian)教育科技集團 版(ban)權所(suo)有 ,京(jing)ICP備16055225號,京(jing)公(gong)海網(wang)安備11010802025203號