3.4.2內(nèi)核下的I2C驅(qū)動(dòng)框架解析
at24cxx_drv.c源碼:
#include "linux/kernel.h"
#include "linux/module.h"
#include "linux/platform_device.h"
#include "linux/i2c.h"
#include "linux/err.h"
#include "linux/regmap.h"
#include "linux/slab.h"
static int __devinit at24cxx_probe(struct i2c_client *client,
{
}
static int __devexit at24cxx_remove(struct i2c_client *client)
{
}
static const struct i2c_device_id at24cxx_id_table[] = {
};
// 1. 分配/設(shè)置i2c_driver //
static struct i2c_driver at24cxx_driver = {
};
static int at24cxx_drv_init(void)
{
}
static void at24cxx_drv_exit(void)
{
}
module_init(at24cxx_drv_init);
module_exit(at24cxx_drv_exit);
MODULE_LICENSE("GPL");
at24cxx_dev.c源碼:
#include "linux/kernel.h"
#include "linux/module.h"
#include "linux/platform_device.h"
#include "linux/i2c.h"
#include "linux/err.h"
#include "linux/regmap.h"
#include "linux/slab.h"
static struct i2c_board_info at24cxx_info = {
};
static struct i2c_client *at24cxx_client;
static int at24cxx_dev_init(void)
{
}
static void at24cxx_dev_exit(void)
{
}
module_init(at24cxx_dev_init);
module_exit(at24cxx_dev_exit);
MODULE_LICENSE("GPL");
===============================================================
第二種方法:
at24cxx_drv.c源碼:
#include "linux/kernel.h"
#include "linux/module.h"
#include "linux/platform_device.h"
#include "linux/i2c.h"
#include "linux/err.h"
#include "linux/regmap.h"
#include "linux/slab.h"
static int __devinit at24cxx_probe(struct i2c_client *client,
{
}
static int __devexit at24cxx_remove(struct i2c_client *client)
{
}
static const struct i2c_device_id at24cxx_id_table[] = {
};
// 1. 分配/設(shè)置i2c_driver //
static struct i2c_driver at24cxx_driver = {
};
static int at24cxx_drv_init(void)
{
}
static void at24cxx_drv_exit(void)
{
}
module_init(at24cxx_drv_init);
module_exit(at24cxx_drv_exit);
MODULE_LICENSE("GPL");
at24cxx_dev.c源碼:
#include "linux/kernel.h"
#include "linux/module.h"
#include "linux/platform_device.h"
#include "linux/i2c.h"
#include "linux/err.h"
#include "linux/regmap.h"
#include "linux/slab.h"
static struct i2c_client *at24cxx_client;
static const unsigned short addr_list[] = { 0x60, 0x50, I2C_CLIENT_END };
static int at24cxx_dev_init(void)
{
}
static void at24cxx_dev_exit(void)
{
}
module_init(at24cxx_dev_init);
module_exit(at24cxx_dev_exit);
MODULE_LICENSE("GPL");
=============================================================
第四種方法:
at24cxx_drv.c源碼:
#include "linux/kernel.h"
#include "linux/module.h"
#include "linux/platform_device.h"
#include "linux/i2c.h"
#include "linux/err.h"
#include "linux/regmap.h"
#include "linux/slab.h"
static int __devinit at24cxx_probe(struct i2c_client *client,
{
}
static int __devexit at24cxx_remove(struct i2c_client *client)
{
}
static const struct i2c_device_id at24cxx_id_table[] = {
};
static int at24cxx_detect(struct i2c_client *client,
{
}
static const unsigned short addr_list[] = { 0x60, 0x50, I2C_CLIENT_END };
// 1. 分配/設(shè)置i2c_driver //
static struct i2c_driver at24cxx_driver = {
};
static int at24cxx_drv_init(void)
{
}
static void at24cxx_drv_exit(void)
{
}
module_init(at24cxx_drv_init);
module_exit(at24cxx_drv_exit);
MODULE_LICENSE("GPL");
===============================================================
1. 框架
1.1 硬件協(xié)議簡(jiǎn)介
1.2 驅(qū)動(dòng)框架
1.3 bus-drv-dev模型及寫程序
a. 設(shè)備的4種構(gòu)建方法(對(duì)于以下4種方法建議使用前3種,第四種方法迫不得已情況下使用)
a.1 定義一個(gè)i2c_board_info, 里面有:名字, 設(shè)備地址
a.2 直接i2c_new_device, i2c_new_probed_device
a.2.1 i2c_new_device : 認(rèn)為設(shè)備肯定存在
a.2.2 i2c_new_probed_device :對(duì)于"已經(jīng)識(shí)別出來的設(shè)備"(probed_device),才會(huì)創(chuàng)建("new")
i2c_new_probed_device
a.3 從用戶空間創(chuàng)建設(shè)備
創(chuàng)建設(shè)備
echo at24c08 0x50 > /sys/class/i2c-adapter/i2c-0/new_device
導(dǎo)致i2c_new_device被調(diào)用
刪除設(shè)備
echo 0x50 > /sys/class/i2c-adapter/i2c-0/delete_device
導(dǎo)致i2c_unregister_device
a.4 前面的3種方法都要事先確定適配器(I2C總線,I2C控制器)
static struct i2c_driver at24cxx_driver = {
};
去"class表示的這一類"I2C適配器,用"detect函數(shù)"來確定能否找到"address_list里的設(shè)備",
如果能找到就調(diào)用i2c_new_device來注冊(cè)i2c_client, 這會(huì)和i2c_driver的id_table比較,
如果匹配,調(diào)用probe
i2c_add_driver
b. 驅(qū)動(dòng)的寫法
2. 完善設(shè)備驅(qū)動(dòng)程序
3. 不自己寫驅(qū)動(dòng)直接訪問
Device Drivers
4. 編寫"總線(適配器adapter)"驅(qū)動(dòng)
Device Drivers
nfs 30000000 192.168.1.123:/work/nfs_root/uImage_noi2cbus; bootm 30000000
評(píng)論