歡迎您光臨本站 註冊首頁

實例解析linux內核I2C體系結構(2)

←手機掃碼閱讀     火星人 @ 2014-03-09 , reply:0

作者:劉洪濤,華清遠見嵌入式學院講師.

四、在內核里寫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方式下相同.


[火星人 ] 實例解析linux內核I2C體系結構(2)已經有552次圍觀

http://coctec.com/docs/linux/show-post-50508.html