[問題] 請教linux i2c newstyle驅動的問題
在下最近在研究newstyle i2c驅動,網路文章看了一些,
有一點心得,但由於身邊朋友同學並沒有做embedded system
的人,所以想跟各位先進請益一下,我先上一個google 到的sample
,我是把它編譯成module,也就是ko檔。
這個程式稍微改一下就真的可以用insmod掛起來了,本身是屬於動
態偵測i2c device的做法,不過我有一些問題如下:
希望各位大大可以給我一些方向。感謝。
1.掛起來之後,我要怎麼在application層透過這個驅動程式對i2c
裝置進行讀寫動作?因為我看這個例子裡面完全沒有實作跟application
溝通的程式碼?
後來有google到一些思路如下:
a.先將這個struct定義出來,並且把最基本的open,read,write完成
static const struct file_operations i2c_fops = {
.owner = THIS_MODULE,
.open = i2c_open,
.read = i2c_read,
.write = i2c_write,
.unlocked_ioctl = i2c_ioctl,
.release = i2c_release,
};
b.註冊成char device,那麼理論上就可以在application去對driver做讀寫。
register_chrdev(I2C_MAJOR,DEVICE_NAME,&i2c_fops)
2.因為我沒有真的在application進行讀寫device的動作,所以我也不確
定這個程式碼是不是真的ok呢?
#include <linux/i2c.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
Static int i2c_driver_demo_probe(struct i2c_client *client, const struct
i2c_device_id *id)
{
Return 0;
}
static int __devexit i2c_driver_demo_remove(struct i2c_client *client)
{
return 0;
}
static const truct i2c_device_id i2c_driver_demo_id[] = {
{“XXXX”,0},
{}
} ;
MODULE_DEVICE_TABLE(i2c, i2c_driver_demo_id);
int i2c_driver_demo_detect(struct i2c_client *client, struct i2c_board_info
*info)
{
struct i2c_adapter *adapter = client->adapter;
int vendor,device,revision;
if(!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Return -ENODEV;
/*方法1:获取设备特定寄存器的值,该值要能反映出设备的信息,判断设
备,例如如下代码段
*/
vendor=i2c_smbus_read_byte_data(client,XXXX_REG_VENDOR);
if(vendor!=XXXX_VENDOR)
return-ENODEV;
device=i2c_smbus_read_byte_data(client,XXXX_REG_DEVICE);
if(device!=XXXX_DEVICE)
return -ENODEV;
revision = i2c_smbus_read_byte_data(client, XXXX_REG_REVISION);
if(revision != XXXX_REVISION)
return -ENODEV;
/*方法二:獲取設備的CHIP_ID,判斷設備,例如以下代碼*/
}
/*0x60為I2C設備位址*/
static const unsigned short normal_i2c[] = {0x60 , I2C_CLIENT_END};
static struct i2c_driver i2c_driver_demo = {
.class = I2C_CLASS_HWMON,
.probe = i2c_driver_demo_probe,
.remove = __devexit_p(i2c_driver_demo_remove),
.ip_table = i2c_driver_demo_id,
.driver = {
.name = “XXXX”
.owner = THIS_MODULE,
},
.detect = i2c_driver_demo_detect,
.address_list = normal_i2c,
};
static int __init i2c_driver_demo_init(void)
{
return i2c_add_driver(&i2c_driver_demo);
}
module_init(i2c_driver_demo_init);
module_exit(i2c_driver_demo_exit);
MODULE_AUTHOR("anchor");
MODULE_DESCRIPTION("I2Cdevicedriverdemo");
MODULE_LICENSE("GPL");
补充说明:若I2C设备驱动不能在detect回调函数里访问硬件,可采用如下形式解决,例
如:
inti2c_driver_demo_detect(structi2c_client*client,structi2c_board_info*info){
structi2c_adapter*adapter=client->adapter;
if(2 == adapter->nr)
{ constchar*type_name="XXXX";
strlcpy(info->type,type_name,I2C_NAME_SIZE);
return0;
}else{
return-ENODEV;
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.249.145.253
※ 文章網址: https://www.ptt.cc/bbs/LinuxDev/M.1469602758.A.AF2.html
※ 編輯: cutem (60.249.145.253), 07/27/2016 15:00:53
※ 編輯: cutem (60.249.145.253), 07/27/2016 15:03:10
※ 編輯: cutem (60.249.145.253), 07/27/2016 15:04:16
※ 編輯: cutem (60.249.145.253), 07/27/2016 17:36:13
→
07/29 18:56, , 1F
07/29 18:56, 1F
→
07/29 18:57, , 2F
07/29 18:57, 2F
→
07/29 19:00, , 3F
07/29 19:00, 3F
→
07/29 19:01, , 4F
07/29 19:01, 4F
→
07/29 19:02, , 5F
07/29 19:02, 5F
※ 編輯: cutem (1.174.250.56), 07/29/2016 23:17:20
→
07/29 23:18, , 6F
07/29 23:18, 6F
→
07/29 23:19, , 7F
07/29 23:19, 7F
推
07/30 00:33, , 8F
07/30 00:33, 8F
→
07/30 00:35, , 9F
07/30 00:35, 9F
→
07/30 00:36, , 10F
07/30 00:36, 10F
→
07/30 00:51, , 11F
07/30 00:51, 11F
→
07/30 13:41, , 12F
07/30 13:41, 12F
→
07/30 20:39, , 13F
07/30 20:39, 13F
→
07/30 20:40, , 14F
07/30 20:40, 14F
→
07/30 20:40, , 15F
07/30 20:40, 15F
→
07/30 20:41, , 16F
07/30 20:41, 16F
→
07/30 20:42, , 17F
07/30 20:42, 17F
→
07/30 20:42, , 18F
07/30 20:42, 18F
推
07/30 23:12, , 19F
07/30 23:12, 19F
※ 編輯: cutem (1.175.233.29), 07/30/2016 23:19:39
→
07/30 23:20, , 20F
07/30 23:20, 20F
→
07/30 23:20, , 21F
07/30 23:20, 21F
推
07/31 06:44, , 22F
07/31 06:44, 22F
→
07/31 06:45, , 23F
07/31 06:45, 23F
LinuxDev 近期熱門文章
PTT數位生活區 即時熱門文章