linux I2C (1)
I2c-pxa.c
i2c_adap_pxa_init
注册了一个平台驱动
static struct platform_driver i2c_pxa_driver = {
.probe = i2c_pxa_probe,
.remove = i2c_pxa_remove,
.driver = {
.name = "pxa2xx-i2c",
},
};
i2c_pxa_probe 方法
分配IO资源 分配IRQ资源
struct i2c_adapter 标示使用一定算法访问的物理I2C总线
我们在busses下定义了如下的一个i2c-adapter
static struct pxa_i2c i2c_pxa = {
.lock = SPIN_LOCK_UNLOCKED,
.adap = {
.owner = THIS_MODULE,
.algo = &i2c_pxa_algorithm,
.name = "pxa2xx-i2c.0",
.retries = 5,
},
};
现在我们需要把这个adapter注册到core中去
i2c_add_adapter
首先为传入的struct i2c_adapter分配一个idr号
加入到adapters 链表中
设备注册
添加sys文件
static struct platform_device i2c_device = {
.name = "pxa2xx-i2c",
.id = 0,
.resource = i2c_resources,
.num_resources = ARRAY_SIZE(i2c_resources),
};
总线bus安装完成
再来看OV2655的初试化
static int __init i2c_ov2655_init(void)
{
int ret;
if ( (ret = i2c_add_driver(&ov2655_driver)) ) {
printk(KERN_ERR "ov2655: Driver registration failed,"
"module not inserted.\n");
return ret;
}
return 0;
}
注册了这样一个驱动
struct i2c_driver ov2655_driver =
{
.driver = {
.name = "ov2655 i2c client driver",
},
.id = I2C_DRIVERID_OV2655,
.attach_adapter = &i2c_ov2655_attach_adapter,
.detach_client = &i2c_ov2655_detach_client,
};
会遍历每一个注册了的I2C_adapter,调用i2c_ov2655_attach_adapter方法