4.1 概述
i2c总线驱动是i2c适配器的软件实现,提供i2c适配器与从设备间完成数据通信的能力,比如起始,停止,应答信号和master_xfer的实现函数。
i2c总线驱动由i2c_adapter和i2c_algorithm来描述
4.2 s3c2440i2c控制器的硬件描述
s3c2440处理器内部集成了一个i2c控制器,通过四个寄存器来进行控制:
iiccon i2c控制寄存器
iicstat i2c状态寄存器
iicds i2c收发数据移位寄存器
iicadd i2c地址寄存器
通过iiccon,iicds,iicadd寄存器操作,可在i2c总线上产生开始位、停止位、数据和地址,而传输的状态则通过iicstat寄存器来获取。
4.3 i2c-s3c2410总线驱动分析(platform_driver)
i2c总线驱动代码在drivers/i2c/busses/i2c-s3c2410.c,这个代码同样支持s3c2410,s3c6410,s5pc110等samsung 系列的芯片。
初始化模块和卸载模块
-
staticint __init i2c_adap_s3c_init(void)
-
{
-
returnplatform_driver_register(&s3c24xx_i2c_driver);
-
}
-
-
staticvoid __exit i2c_adap_s3c_exit(void)
-
{
-
platform_driver_unregister(&s3c24xx_i2c_driver);
-
}
static int __init i2c_adap_s3c_init(void)
{
returnplatform_driver_register(&s3c24xx_i2c_driver);
}
static void __exit i2c_adap_s3c_exit(void)
{
platform_driver_unregister(&s3c24xx_i2c_driver);
}
总线驱动是基于platform来实现的,很符合设备驱动模型的思想。
-
staticstruct platform_drivers3c24xx_i2c_driver = {
-
.probe = s3c24xx_i2c_probe,
-
.remove = s3c24xx_i2c_remove,
-
.id_table = s3c24xx_driver_ids,
-
.driver = {
-
.owner = this_module,
-
.name = "s3c-i2c",
-
.pm = s3c24xx_dev_pm_ops,
-
.of_match_table= s3c24xx_i2c_match,
-
},
-
};
static struct platform_drivers3c24xx_i2c_driver = {
.probe = s3c24xx_i2c_probe,
.remove = s3c24xx_i2c_remove,
.id_table = s3c24xx_driver_ids,
.driver = {
.owner = this_module,
.name = "s3c-i2c",
.pm = s3c24xx_dev_pm_ops,
.of_match_table= s3c24xx_i2c_match,
},
};
s3c24xx_i2c_probe函数
当调用platform_driver_register函数注册platform_driver结构体时,如果platformdevice 和 platform driver匹配成功后,会调用probe函数,来初始化适配器硬件。
-
staticint s3c24xx_i2c_probe(structplatform_device *pdev)
-
{
-
……
-
-
strlcpy(i2c->adap.name,"s3c2410-i2c", sizeof(i2c->adap.name));
-
i2c->adap.owner = this_module;
-
i2c->adap.algo = &s3c24xx_i2c_algorithm;
-
i2c->adap.retries= 2;
-
i2c->adap.class = i2c_class_hwmon | i2c_class_spd;
-
i2c->tx_setup = 50;
-
-
-
spin_lock_init(&i2c->lock);
-
init_waitqueue_head(&i2c->wait);
-
-
-
res= platform_get_resource(pdev, ioresource_mem, 0);
-
i2c->ioarea= request_mem_region(res->start, resource_size(res),
-
pdev->name);
-
i2c->regs= ioremap(res->start, resource_size(res));
-
-
-
i2c->adap.algo_data= i2c;
-
i2c->adap.dev.parent= &pdev->dev;
-
-
-
ret= s3c24xx_i2c_init(i2c);
-
-
-
i2c->irq= ret = platform_get_irq(pdev, 0);
-
ret= request_irq(i2c->irq, s3c24xx_i2c_irq, 0,
-
dev_name(&pdev->dev), i2c);
-
-
-
ret= i2c_add_numbered_adapter(&i2c->adap);
-
……
-
}
static int s3c24xx_i2c_probe(structplatform_device *pdev)
{
……
/*初始化适配器信息 */
strlcpy(i2c->adap.name,"s3c2410-i2c", sizeof(i2c->adap.name));
i2c->adap.owner = this_module;
i2c->adap.algo = &s3c24xx_i2c_algorithm;
i2c->adap.retries= 2;
i2c->adap.class = i2c_class_hwmon | i2c_class_spd;
i2c->tx_setup = 50;
/*初始化自旋锁和等待队列头 */
spin_lock_init(&i2c->lock);
init_waitqueue_head(&i2c->wait);
/*映射寄存器 */
res= platform_get_resource(pdev, ioresource_mem, 0);
i2c->ioarea= request_mem_region(res->start, resource_size(res),
pdev->name);
i2c->regs= ioremap(res->start, resource_size(res));
/*设置i2c核心需要的信息 */
i2c->adap.algo_data= i2c;
i2c->adap.dev.parent= &pdev->dev;
/*初始化i2c控制器 */
ret= s3c24xx_i2c_init(i2c);
/*申请中断 */
i2c->irq= ret = platform_get_irq(pdev, 0);
ret= request_irq(i2c->irq, s3c24xx_i2c_irq, 0,
dev_name(&pdev->dev), i2c);
/* 注册i2c适配器 */
ret= i2c_add_numbered_adapter(&i2c->adap);
……
}
probe主要工作是时能硬件并申请i2c适配器使用的io地址,中断号等,然后向i2c核心添加这个适配器。i2c_adapter注册过程i2c_add_numbered_adapter->i2c_register_adapter
i2c总线通信方法
-
staticconststruct i2c_algorithms3c24xx_i2c_algorithm = {
-
.master_xfer = s3c24xx_i2c_xfer,
-
.functionality = s3c24xx_i2c_func,
-
};
static const struct i2c_algorithms3c24xx_i2c_algorithm = {
.master_xfer = s3c24xx_i2c_xfer,
.functionality = s3c24xx_i2c_func,
};
s3c24xx_i2c_xfer函数是总线通信方式的具体实现,依赖于s3c24xx_i2c_doxfer和s3c24xx_i2c_message_start两个函数;
-
staticint s3c24xx_i2c_doxfer(structs3c24xx_i2c *i2c,
-
struct i2c_msg *msgs, int num)
-
{
-
ret =s3c24xx_i2c_set_master(i2c);
-
-
i2c->msg = msgs;
-
i2c->msg_num= num;
-
i2c->msg_ptr= 0;
-
i2c->msg_idx= 0;
-
i2c->state = state_start;
-
-
s3c24xx_i2c_message_start(i2c,msgs);
-
}
static int s3c24xx_i2c_doxfer(structs3c24xx_i2c *i2c,
struct i2c_msg *msgs, int num)
{
ret =s3c24xx_i2c_set_master(i2c);
i2c->msg = msgs;
i2c->msg_num= num;
i2c->msg_ptr= 0;
i2c->msg_idx= 0;
i2c->state = state_start;
s3c24xx_i2c_message_start(i2c,msgs);
}
首先设置s3c i2c设备器为主设备,然后调用s3c24xx_i2c_message_start函数启动i2c消息传输。
s3c24xx_i2c_func函数返回适配器所支持的通信功能。
4.4 适配器的设备资源(platform_device)
s3c2440的i2c总线驱动是基于platform来实现,前面我们分析了platformdriver部分,再来看下platform device部分。
在arch/arm/plat-samsung/dev-i2c0.c文件中定义了platform_device结构体以及i2c控制器的资源信息:
-
staticstruct resource s3c_i2c_resource[] ={
-
[0]= {
-
.start= s3c_pa_iic,
-
.end = s3c_pa_iic sz_4k - 1,
-
.flags= ioresource_mem,
-
},
-
[1]= {
-
.start= irq_iic,
-
.end = irq_iic,
-
.flags= ioresource_irq,
-
},
-
};
-
-
struct platform_device s3c_device_i2c0 = {
-
.name = "s3c2410-i2c",
-
#ifdef config_s3c_dev_i2c1
-
.id = 0,
-
#else
-
.id = -1,
-
#endif
-
.num_resources =array_size(s3c_i2c_resource),
-
.resource =s3c_i2c_resource,
-
};
-
-
struct s3c2410_platform_i2cdefault_i2c_data __initdata = {
-
.flags = 0,
-
.slave_addr = 0x10,
-
.frequency = 100*1000,
-
.sda_delay = 100,
-
};
-
-
void __init s3c_i2c0_set_platdata(structs3c2410_platform_i2c *pd)
-
{
-
structs3c2410_platform_i2c *npd;
-
-
if(!pd)
-
pd= &default_i2c_data;
-
-
npd= s3c_set_platdata(pd, sizeof(struct s3c2410_platform_i2c),
-
&s3c_device_i2c0);
-
-
if(!npd->cfg_gpio)
-
npd->cfg_gpio= s3c_i2c0_cfg_gpio;
-
}
static struct resource s3c_i2c_resource[] ={
[0]= {
.start= s3c_pa_iic,
.end = s3c_pa_iic sz_4k - 1,
.flags= ioresource_mem,
},
[1]= {
.start= irq_iic,
.end = irq_iic,
.flags= ioresource_irq,
},
};
struct platform_device s3c_device_i2c0 = {
.name = "s3c2410-i2c", /* 设备名 */
#ifdef config_s3c_dev_i2c1
.id = 0,
#else
.id = -1,
#endif
.num_resources =array_size(s3c_i2c_resource),
.resource =s3c_i2c_resource,
};
struct s3c2410_platform_i2cdefault_i2c_data __initdata = {
.flags = 0,
.slave_addr = 0x10, /* i2c适配器的地址 */
.frequency = 100*1000, /* 总线频率 */
.sda_delay = 100, /* sda边沿延迟时间ns */
};
void __init s3c_i2c0_set_platdata(structs3c2410_platform_i2c *pd)
{
structs3c2410_platform_i2c *npd;
if(!pd)
pd= &default_i2c_data;
npd= s3c_set_platdata(pd, sizeof(struct s3c2410_platform_i2c),
&s3c_device_i2c0);
if(!npd->cfg_gpio)
npd->cfg_gpio= s3c_i2c0_cfg_gpio;
}
在板文件中把platform_device注册进内核:
-
staticstruct platform_device*mini2440_devices[] __initdata = {
-
……
-
&s3c_device_i2c0,
-
……
-
};
static struct platform_device*mini2440_devices[] __initdata = {
……
&s3c_device_i2c0,
……
};
调用s3c_i2c0_set_platdata 函数把适配器具体的数据赋值给dev.platform_data:
-
staticvoid __init mini2440_init(void)
-
{
-
……
-
s3c_i2c0_set_platdata(null);
-
}
static void __init mini2440_init(void)
{
……
s3c_i2c0_set_platdata(null);
}
i2c总线驱动就分析到这里。