总线驱动和具体的平台CPU有关,以2440为例:drivers\i2c\busses\I2c-s3c2410.c
I2C 适配器驱动加载与卸载
I2C 总线驱动模块的 加载函数要完成两个工作。1.初始化 I2C 适配器所使用的硬件资源,如申请 I/O 地址、中断号等。
2.通过 i2c_add_adapter()添加 i2c_adapter 的数据结构,当然这个 i2c_adapter 数据结构的成员已经被 xxx 适配器的相应函数指针所初始化。
代码如下:
static int __init i2c_adap_s3c_init(void)
{
int ret;
//这个文件支持2440和2410的i2c驱动
ret = platform_driver_register(&s3c2410_i2c_driver);
if (ret == 0) {
ret = platform_driver_register(&s3c2440_i2c_driver);
if (ret)
platform_driver_unregister(&s3c2410_i2c_driver);
}
return ret;
}
看下i2c_driver的定义:
static struct platform_driver s3c2440_i2c_driver = {
.probe = s3c24xx_i2c_probe,//看这个函数中如何初始化
.remove = s3c24xx_i2c_remove,
.resume = s3c24xx_i2c_resume,
.driver = {
.owner = THIS_MODULE,
.name = "s3c2440-i2c",
},
};
s3c24xx_i2c_probe
static int s3c24xx_i2c_probe(struct platform_device *pdev)
{
struct s3c24xx_i2c *i2c = &s3c24xx_i2c;
struct resource *res;
int ret;
/* find the clock and enable it */
i2c->dev = &pdev->dev;
i2c->clk = clk_get(&pdev->dev, "i2c");
if (IS_ERR(i2c->clk)) {
dev_err(&pdev->dev, "cannot get clock\n");
ret = -ENOENT;
goto err_noclk;
}
dev_dbg(&pdev->dev, "clock source %p\n", i2c->clk);
clk_enable(i2c->clk);
/* map the registers */
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (res == NULL) {
dev_err(&pdev->dev, "cannot find IO resource\n");
ret = -ENOENT;
goto err_clk;
}
i2c->ioarea = request_mem_region(res->start, (res->end-res->start)+1,
pdev->name);
if (i2c->ioarea == NULL) {
dev_err(&pdev->dev, "cannot request IO\n");
ret = -ENXIO;
goto err_clk;
}
i2c->regs = ioremap(res->start, (res->end-res->start)+1);
if (i2c->regs == NULL) {
dev_err(&pdev->dev, "cannot map IO\n");
ret = -ENXIO;
goto err_ioarea;
}
dev_dbg(&pdev->dev, "registers %p (%p, %p)\n", i2c->regs, i2c->ioarea, res);
/* setup info block for the i2c core */
i2c->adap.algo_data = i2c;
i2c->adap.dev.parent = &pdev->dev;
/* initialise the i2c controller */
ret = s3c24xx_i2c_init(i2c);//对2440控制器 io的进行初始化设置,很重要,和具体的硬件寄存器相关
if (ret != 0)
goto err_iomap;
/* find the IRQ for this unit (note, this relies on the init call to
* ensure no current IRQs pending
*/
res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
if (res == NULL) {
dev_err(&pdev->dev, "cannot find IRQ\n");
ret = -ENOENT;
goto err_iomap;
}
ret = request_irq(res->start, s3c24xx_i2c_irq, IRQF_DISABLED,
pdev->name, i2c);//申请中断
if (ret != 0) {
dev_err(&pdev->dev, "cannot claim IRQ\n");
goto err_iomap;
}
i2c->irq = res;
dev_dbg(&pdev->dev, "irq resource %p (%lu)\n", res,
(unsigned long)res->start);
ret = i2c_add_adapter(&i2c->adap);//添加adapter,很重要,和其他数据结构联系
if (ret < 0) {
dev_err(&pdev->dev, "failed to add bus to i2c core\n");
goto err_irq;
}
platform_set_drvdata(pdev, i2c);
dev_info(&pdev->dev, "%s: S3C I2C adapter\n", i2c->adap.dev.bus_id);
return 0;
err_irq:
free_irq(i2c->irq->start, i2c);
err_iomap:
iounmap(i2c->regs);
err_ioarea:
release_resource(i2c->ioarea);
kfree(i2c->ioarea);
err_clk:
clk_disable(i2c->clk);
clk_put(i2c->clk);
err_noclk:
return ret;
}
I2C 总线驱动模块的卸载函数要完成的工作与加载函数相反(卸载就不对2440分析了)。
1.释放 I2C 适配器所使用的硬件资源,如释放 I/O 地址、中断号等。
2.通过 i2c_del_adapter()删除 i2c_adapter 的数据结构。
下面 所示为 I2C 适配器驱动的模块加载和卸载函数的模板。
static int _ _init i2c_adapter_xxx_init(void)
{
xxx_adpater_hw_init();
i2c_add_adapter(&xxx_adapter);
}
static void _ _exit i2c_adapter_xxx_exit(void)
{
xxx_adpater_hw_free();
i2c_del_adapter(&xxx_adapter);
}
上述代码中
xxx_adpater_hw_init()和 xxx_adpater_hw_free()函数的实现都与具体的CPU 和 I2C 设备硬件直接相关和上面所列出的2440代码
s3c24xx_i2c_init非常类似。
I2C 总线通信方法
我们需要为特定的 I2C 适配器实现其通信方法,主要实现 i2c_algorithm 的master_xfer()函数和 functionality()函数。
functionality()函数非常简单,用于返回algorithm所支持的通信协议,如I2C_FUNC_I2C、I2C_FUNC_10BIT_ADDR、I2C_FUNC_SMBUS_READ_BYTE、I2C_FUNC_SMBUS_WRITE_BYTE 等。
下面以2440为例看下实现:
定义通信方法:
static const struct i2c_algorithm s3c24xx_i2c_algorithm = {
.master_xfer = s3c24xx_i2c_xfer,
.functionality = s3c24xx_i2c_func,
};
s3c24xx_i2c_xfer的实现:
static int s3c24xx_i2c_xfer(struct i2c_adapter *adap,
struct i2c_msg *msgs, int num)
{
struct s3c24xx_i2c *i2c = (struct s3c24xx_i2c *)adap->algo_data;
int retry;
int ret;
for (retry = 0; retry < adap->retries; retry++) {
ret = s3c24xx_i2c_doxfer(i2c, msgs, num);//传输函数
if (ret != -EAGAIN)
return ret;
dev_dbg(i2c->dev, "Retrying transmission (%d)\n", retry);
udelay(100);
}
return -EREMOTEIO;
}
s3c24xx_i2c_doxfer的实现
static int s3c24xx_i2c_doxfer(struct s3c24xx_i2c *i2c, struct i2c_msg *msgs, int num)
{
unsigned long timeout;
int ret;
ret = s3c24xx_i2c_set_master(i2c);//获取i2c总线,先判断i2c是否处于busy状态
if (ret != 0) {
dev_err(i2c->dev, "cannot get bus (error %d)\n", ret);
ret = -EAGAIN;
goto out;
}
spin_lock_irq(&i2c->lock);
i2c->msg = msgs;
i2c->msg_num = num;
i2c->msg_ptr = 0;
i2c->msg_idx = 0;
i2c->state = STATE_START;
//以下对中断,ack,读写就行相关的设置,是不是控制器设置好后就会自动的发送相关的信号?
s3c24xx_i2c_enable_irq(i2c);//使能中断,
s3c24xx_i2c_message_start(i2c, msgs);//相关的设置
spin_unlock_irq(&i2c->lock);
timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
ret = i2c->msg_idx;
/* having these next two as dev_err() makes life very
* noisy when doing an i2cdetect */
if (timeout == 0)
dev_dbg(i2c->dev, "timeout\n");
else if (ret != num)
dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret);
/* ensure the stop has been through the bus */
msleep(1);
out:
return ret;
}
s3c24xx_i2c_message_start
static void s3c24xx_i2c_message_start(struct s3c24xx_i2c *i2c,
struct i2c_msg *msg)
{
unsigned int addr = (msg->addr & 0x7f) << 1;
unsigned long stat;
unsigned long iiccon;
stat = 0;
stat |= S3C2410_IICSTAT_TXRXEN;//读写
if (msg->flags & I2C_M_RD) {//判断是读还是写
stat |= S3C2410_IICSTAT_MASTER_RX;
addr |= 1;
} else
stat |= S3C2410_IICSTAT_MASTER_TX;
if (msg->flags & I2C_M_REV_DIR_ADDR)
addr ^= 1;
// todo - check for wether ack wanted or not
s3c24xx_i2c_enable_ack(i2c);//使能ack
iiccon = readl(i2c->regs + S3C2410_IICCON);
writel(stat, i2c->regs + S3C2410_IICSTAT);
dev_dbg(i2c->dev, "START: %08lx to IICSTAT, %02x to DS\n", stat, addr);
writeb(addr, i2c->regs + S3C2410_IICDS);//发送地址
/* delay here to ensure the data byte has gotten onto the bus
* before the transaction is started */
ndelay(i2c->tx_setup);
dev_dbg(i2c->dev, "iiccon, %08lx\n", iiccon);
writel(iiccon, i2c->regs + S3C2410_IICCON);
stat |= S3C2410_IICSTAT_START;
writel(stat, i2c->regs + S3C2410_IICSTAT);
}
从上面的分析可以看到master_xfer并没有完成i2c的整个传输,只是发送一个起始条件和从设备地址,那么传输数据是如何完成的??
先给出i2c传输的模板:
master_xfer()函数在 I2C 适配器上完成传递给它的 i2c_msg 数组中的每个 I2C 消息,下面所示为 xxx 设备的 master_xfer()函数模板。
static int i2c_adapter_xxx_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,int num)
{
...
for (i = 0; i < num; i++)
{
i2c_adapter_xxx_start(); /*产生开始位*/
/*是读消息*/
if(msgs[i]->flags &I2C_M_RD)
{
i2c_adapter_xxx_setaddr((msg->addr << 1) | 1); /*发送从设备读地址*/
i2c_adapter_xxx_wait_ack(); /*获得从设备的 ack*/
i2c_adapter_xxx_readbytes(msgs[i]->buf, msgs[i]->len); /*读取msgs[i] ->len长的数据到 msgs[i]->buf*/
}
else/*是写消息*/
{
i2c_adapter_xxx_setaddr(msg->addr << 1); /*发送从设备写地址*/
i2c_adapter_xxx_wait_ack(); /*获得从设备的 ack*/
i2c_adapter_xxx_writebytes(msgs[i]->buf, msgs[i]->len); /*读取 msgs[i] ->len长的数据到 msgs[i]->buf*/
}
}
i2c_adapter_xxx_stop(); /*产生停止位*/
}
上述代码实际上给出了一个 master_xfer()函数处理 I2C 消息数组的流程,对于数组中的每个消息,判断消息类型,若为
读消息,则赋从设备地址为
(msg->addr << 1)|1,否则为
msg->addr << 1。对每个消息产生一个开始位,紧接着传送从设备地址,然后开始数据的发送或接收,对最后的消息还需产生一个停止位。
如下图所示为整个 master_xfer()完成的时序。
master_xfer()函数模板中的 i2c_adapter_xxx_start()、i2c_adapter_xxx_setaddr()、i2c_adapter_ xxx_wait_ack()、
i2c_adapter_xxx_readbytes()、i2c_adapter_xxx_writebytes()和 i2c_adapter_xxx_stop()函数用于完成适配器的底层硬件操作,与 I2C 适配器和 CPU的具体硬件直接相关,需要由工程师根据芯片的数据手册来实现
i2c_adapter_xxx_readbytes() 用 于 从 从 设 备 上 接 收 一 串 数 据 ,
i2c_adapter_xxx_writebytes()用于向从设备写入一串数据,这两个函数的内部也会涉及I2C 总线协议中的 ACK 应答。
下面看下S3C2410 I2C 总线驱动实例
S3C2410 I2C 控制器硬件描述
S3C2410 处理器内部集成了一个 I2C 控制器,通过 4 个寄存器就可方便地对其进行控制,这 4 个寄存器如下。
1.IICCON:I2C 控制寄存器。
2.IICSTAT:I2C 状态寄存器。
3.IICDS:I2C 收发数据移位寄存器。
4.IICADD:I2C 地址寄存器。
S3C2410 处理器内部集成的 I2C 控制器可支持主、从两种模式,我们主要使用其主模式。通过对 IICCON、IICDS 和 IICADD 寄存器的操作,可在 I2C 总线上产生开始位、停止位、数据和地址,而传输的状态则通过 IICSTAT 寄存器获取。
S3C2410 I2C 总线驱动总体分析
S3C2410 的 I2C 总线驱动设计主要要完成以下工作。
1.设计对应于 i2c_adapter_xxx_init()模板的 S3C2410 的模块加载函数和对应于i2c_ adapter_xxx_exit()函数模板的模块卸载函数。
2.设计对应于 i2c_adapter_xxx_xfer()模板的 S3C2410 适配器的通信方法函数。针 对 S3C2410 , functionality() 函 数 只 需 简 单 地 返 回 I2C_FUNC_I2C | I2C_FUNC_SMBUS_ EMUL | I2C_FUNC_PROTOCOL_MANGLING 表明其支持的功能。
这里分析内核中自带的 Ben Dooks 版本驱动,它同时支持 S3C2410 和 S3C2440。
下图给出了 S3C2410 驱动中的主要函数与 上面模板函数的对应关系,由于实现通信方法的方式不一样,模板的一个函数可能对应于 S3C2410 I2C 总线驱动的多个函数。
S3C2410 I2C 适配器驱动的模块加载与卸载
I2C 适配器驱动被作为一个单独的模块加载进内核,在模块的加载和卸载函数中,只需注册和注销一个 platform_driver 结构体,如代码清单如下所示:
static int __init i2c_adap_s3c_init(void)
{
int ret;
ret = platform_driver_register(&s3c2410_i2c_driver);
if (ret == 0) {
ret = platform_driver_register(&s3c2440_i2c_driver);
if (ret)
platform_driver_unregister(&s3c2410_i2c_driver);
}
return ret;
}
static void __exit i2c_adap_s3c_exit(void)
{
platform_driver_unregister(&s3c2410_i2c_driver);
platform_driver_unregister(&s3c2440_i2c_driver);
}
platform_driver 结构体包含了具体适配器的 probe()函数、remove()函数、resume()函数指针等信息,它需要被定义和赋值,如代码清单如下所示:
static struct platform_driver s3c2410_i2c_driver = {
.probe = s3c24xx_i2c_probe,
.remove = s3c24xx_i2c_remove,
.resume = s3c24xx_i2c_resume,
.driver = {
.owner = THIS_MODULE,
.name = "s3c2410-i2c",
},
};
当 通 过 Linux 内 核 源 代 码 /drivers/base/platform.c 文 件 中 定 义platform_driver_unregister()函数注册 platform_driver 结构体时,其中 probe 指针指向的s3c24xx_i2c_probe()函数将被调用,
以初始化适配器硬件,如代码清单如下所示。
static int s3c24xx_i2c_probe(struct platform_device *pdev)
{
struct s3c24xx_i2c *i2c = &s3c24xx_i2c;
struct resource *res;
int ret;
/* find the clock and enable it */
i2c->dev = &pdev->dev;
i2c->clk = clk_get(&pdev->dev, "i2c");
if (IS_ERR(i2c->clk)) {
dev_err(&pdev->dev, "cannot get clock\n");
ret = -ENOENT;
goto err_noclk;
}
dev_dbg(&pdev->dev, "clock source %p\n", i2c->clk);
clk_enable(i2c->clk);
/* map the registers */
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (res == NULL) {
dev_err(&pdev->dev, "cannot find IO resource\n");
ret = -ENOENT;
goto err_clk;
}
i2c->ioarea = request_mem_region(res->start, (res->end-res->start)+1,
pdev->name);
if (i2c->ioarea == NULL) {
dev_err(&pdev->dev, "cannot request IO\n");
ret = -ENXIO;
goto err_clk;
}
i2c->regs = ioremap(res->start, (res->end-res->start)+1);
if (i2c->regs == NULL) {
dev_err(&pdev->dev, "cannot map IO\n");
ret = -ENXIO;
goto err_ioarea;
}
dev_dbg(&pdev->dev, "registers %p (%p, %p)\n", i2c->regs, i2c->ioarea, res);
/* setup info block for the i2c core */
i2c->adap.algo_data = i2c;
i2c->adap.dev.parent = &pdev->dev;
/* initialise the i2c controller */
ret = s3c24xx_i2c_init(i2c);
if (ret != 0)
goto err_iomap;
/* find the IRQ for this unit (note, this relies on the init call to
* ensure no current IRQs pending
*/
res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
if (res == NULL) {
dev_err(&pdev->dev, "cannot find IRQ\n");
ret = -ENOENT;
goto err_iomap;
}
ret = request_irq(res->start, s3c24xx_i2c_irq, IRQF_DISABLED,
pdev->name, i2c);
if (ret != 0) {
dev_err(&pdev->dev, "cannot claim IRQ\n");
goto err_iomap;
}
i2c->irq = res;
dev_dbg(&pdev->dev, "irq resource %p (%lu)\n", res,
(unsigned long)res->start);
ret = i2c_add_adapter(&i2c->adap);
if (ret < 0) {
dev_err(&pdev->dev, "failed to add bus to i2c core\n");
goto err_irq;
}
platform_set_drvdata(pdev, i2c);
dev_info(&pdev->dev, "%s: S3C I2C adapter\n", i2c->adap.dev.bus_id);
return 0;
err_irq:
free_irq(i2c->irq->start, i2c);
err_iomap:
iounmap(i2c->regs);
err_ioarea:
release_resource(i2c->ioarea);
kfree(i2c->ioarea);
err_clk:
clk_disable(i2c->clk);
clk_put(i2c->clk);
err_noclk:
return ret;
}
上述代码中的主体工作是使能硬件并申请 I2C 适配器使用的 I/O 地址、在这些工作都完成无误后,通过 I2C 核心提供的
i2c_add_adapter()函数添加这个适配器 。 因 为 S3C2410 内 部 集 成 I2C 控 制 器 , 可 以 确 定 I2C 适 配 器 一 定 存 在 ,
s3c24xx_i2c_probe()函数虽然命名“探测”,但实际没有也不必进行任何探测工作,之所以这样命名完全是一种设计习惯。
与 s3c24xx_i2c_probe()函数完成相反功能的函数是 s3c24xx_i2c_remove()函数,它在 适 配 器 模 块 卸 载 函 数 调 用 platform_driver_unregister() 函 数 时 所 示 通 过platform_driver 的 remove 指针方式被调用。xxx_i2c_remove()的设计模板如代码清单如下所示。
static int s3c24xx_i2c_remove(struct platform_device *pdev)
{
struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
i2c_del_adapter(&i2c->adap);
free_irq(i2c->irq->start, i2c);
clk_disable(i2c->clk);
clk_put(i2c->clk);
iounmap(i2c->regs);
release_resource(i2c->ioarea);
kfree(i2c->ioarea);
return 0;
}
上面代码用到的 s3c24xx_i2c 结构体进行适配器所有信息的封装,类似于私有信息结构体,下面所示的 s3c24xx_i2c 结构体的定义,以及驱动模块定义的一个s3c24xx_i2c 结构体全局实例。
struct s3c24xx_i2c {
spinlock_t lock;
wait_queue_head_t wait;//等待队列
struct i2c_msg *msg;//传输的信息结构体
unsigned int msg_num;
unsigned int msg_idx;
unsigned int msg_ptr;
unsigned int tx_setup;
enum s3c24xx_i2c_state state;
void __iomem *regs;
struct clk *clk;
struct device *dev;
struct resource *irq;
struct resource *ioarea;
struct i2c_adapter adap;//适配器
};
static struct s3c24xx_i2c s3c24xx_i2c = {
.lock = __SPIN_LOCK_UNLOCKED(s3c24xx_i2c.lock),
.wait = __WAIT_QUEUE_HEAD_INITIALIZER(s3c24xx_i2c.wait),
.tx_setup = 50,
.adap = {
.name = "s3c2410-i2c",
.owner = THIS_MODULE,
.algo = &s3c24xx_i2c_algorithm,
.retries = 2,
.class = I2C_CLASS_HWMON,
},
};
S3C2410 I2C 总线通信方法
从上面代码可以看出,I2C 适配器对应的 i2c_algorithm 结构体实例为 s3c24xx_i2c_algorithm,下面代码清单 所示 s3c24xx_i2c_algorithm 的定义。
static const struct i2c_algorithm s3c24xx_i2c_algorithm = {
.master_xfer = s3c24xx_i2c_xfer,
.functionality = s3c24xx_i2c_func,
};
上述代码指定了 S3C2410 I2C 总线通信传输函数
s3c24xx_i2c_xfer(),这个函数非常关键,所有 I2C 总线上对设备的访问最终应该由它来完成,下面代码所 示 为 这 个 重 要 函 数 以 及 其 依 赖 的 s3c24xx_i2c_doxfer() 函 数 和s3c24xx_i2c_message_start()函数的源代码。
static int s3c24xx_i2c_xfer(struct i2c_adapter *adap,
struct i2c_msg *msgs, int num)
{
struct s3c24xx_i2c *i2c = (struct s3c24xx_i2c *)adap->algo_data;
int retry;
int ret;
for (retry = 0; retry < adap->retries; retry++) {
ret = s3c24xx_i2c_doxfer(i2c, msgs, num);//重要传输函数
if (ret != -EAGAIN)
return ret;
dev_dbg(i2c->dev, "Retrying transmission (%d)\n", retry);
udelay(100);
}
return -EREMOTEIO;
}
static int s3c24xx_i2c_doxfer(struct s3c24xx_i2c *i2c, struct i2c_msg *msgs, int num)
{
unsigned long timeout;
int ret;
ret = s3c24xx_i2c_set_master(i2c);
if (ret != 0) {
dev_err(i2c->dev, "cannot get bus (error %d)\n", ret);
ret = -EAGAIN;
goto out;
}
spin_lock_irq(&i2c->lock);
i2c->msg = msgs;
i2c->msg_num = num;
i2c->msg_ptr = 0;
i2c->msg_idx = 0;
i2c->state = STATE_START;
s3c24xx_i2c_enable_irq(i2c);
s3c24xx_i2c_message_start(i2c, msgs);//重要函数
spin_unlock_irq(&i2c->lock);
timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
ret = i2c->msg_idx;
/* having these next two as dev_err() makes life very
* noisy when doing an i2cdetect */
if (timeout == 0)
dev_dbg(i2c->dev, "timeout\n");
else if (ret != num)
dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret);
/* ensure the stop has been through the bus */
msleep(1);//确保停止位被发送
out:
return ret;
}
static void s3c24xx_i2c_message_start(struct s3c24xx_i2c *i2c,
struct i2c_msg *msg)
{
unsigned int addr = (msg->addr & 0x7f) << 1;
unsigned long stat;
unsigned long iiccon;
stat = 0;
stat |= S3C2410_IICSTAT_TXRXEN;
if (msg->flags & I2C_M_RD) {
stat |= S3C2410_IICSTAT_MASTER_RX;
addr |= 1;
} else
stat |= S3C2410_IICSTAT_MASTER_TX;
if (msg->flags & I2C_M_REV_DIR_ADDR)
addr ^= 1;
// todo - check for wether ack wanted or not
s3c24xx_i2c_enable_ack(i2c);/*如果要使能 ACK,则使能*/
iiccon = readl(i2c->regs + S3C2410_IICCON);
writel(stat, i2c->regs + S3C2410_IICSTAT);
dev_dbg(i2c->dev, "START: %08lx to IICSTAT, %02x to DS\n", stat, addr);
writeb(addr, i2c->regs + S3C2410_IICDS);
/* delay here to ensure the data byte has gotten onto the bus
* before the transaction is started */
/*在发送新的开始位前延迟 i2c->tx_setup 位*/
ndelay(i2c->tx_setup);
dev_dbg(i2c->dev, "iiccon, %08lx\n", iiccon);
writel(iiccon, i2c->regs + S3C2410_IICCON);
stat |= S3C2410_IICSTAT_START;
writel(stat, i2c->regs + S3C2410_IICSTAT);//发送开始位
}
s3c24xx_i2c_xfer()函数调用 s3c24xx_i2c_doxfer()函数传输 I2C 消息,for循环意味着最多可以重试 adap->retries 次。
s3c24xx_i2c_doxfer()首先将 S3C2410 的 I2C 适配器设置为 I2C 主设备,其后初始化 s3c24xx_i2c 结构体,使能 I2C 中断,并调用 s3c24xx_i2c_message_start()函数启动I2C 消息的传输。s3c24xx_i2c_message_start()函数写 S3C2410 适配器对应的控制寄存器,向 I2C 从设备传递开始位和从设备地址。
上述代码只是启动了 I2C 消息数组的传输周期,并没有完整实现模板图中给出的algorithm master_xfer 流程。 这个流程的完整实现需要借助 I2C 适配器上的中断来步步推进。下面代码所示为 S3C2410 I2C 适配器中断处理函数以及其依赖的 i2c_s3c_irq_nextbyte()函数的源代码。
static irqreturn_t s3c24xx_i2c_irq(int irqno, void *dev_id)
{
struct s3c24xx_i2c *i2c = dev_id;
unsigned long status;
unsigned long tmp;
status = readl(i2c->regs + S3C2410_IICSTAT);
if (status & S3C2410_IICSTAT_ARBITR) {
// deal with arbitration loss
dev_err(i2c->dev, "deal with arbitration loss\n");
}
if (i2c->state == STATE_IDLE) {
dev_dbg(i2c->dev, "IRQ: error i2c->state == IDLE\n");
tmp = readl(i2c->regs + S3C2410_IICCON);
tmp &= ~S3C2410_IICCON_IRQPEND;
writel(tmp, i2c->regs + S3C2410_IICCON);
goto out;
}
/* pretty much this leaves us with the fact that we've
* transmitted or received whatever byte we last sent */
i2s_s3c_irq_nextbyte(i2c, status);//重要的推进函数
out:
return IRQ_HANDLED;
}
i2s_s3c_irq_nextbyte
static int i2s_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat)
{
unsigned long tmp;
unsigned char byte;
int ret = 0;
switch (i2c->state) {
case STATE_IDLE://空闲状态
dev_err(i2c->dev, "%s: called in STATE_IDLE\n", __FUNCTION__);
goto out;
break;
case STATE_STOP://传输停止
dev_err(i2c->dev, "%s: called in STATE_STOP\n", __FUNCTION__);
s3c24xx_i2c_disable_irq(i2c);//关闭中断
goto out_ack;
case STATE_START://开始传输
/* last thing we did was send a start condition on the
* bus, or started a new i2c message
*/
if (iicstat & S3C2410_IICSTAT_LASTBIT &&
!(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
/* ack was not received... */
/* 没有收到 ACK */
dev_dbg(i2c->dev, "ack was not received\n");
s3c24xx_i2c_stop(i2c, -EREMOTEIO);//停止i2c传输
goto out_ack;
}
if (i2c->msg->flags & I2C_M_RD) //检查是读还是写
i2c->state = STATE_READ;
else
i2c->state = STATE_WRITE;
/* terminate the transfer if there is nothing to do
* (used by the i2c probe to find devices */
/* 仅一条消息,而且长度为 0(主要用于适配器探测),发送停止位*/
if (is_lastmsg(i2c) && i2c->msg->len == 0) {
s3c24xx_i2c_stop(i2c, 0);
goto out_ack;
}
if (i2c->state == STATE_READ)
goto prepare_read;
/* fall through to the write state, as we will need to
* send a byte as well */
case STATE_WRITE:/* 进入写状态 */
/* we are writing data to the device... check for the
* end of the message, and if so, work out what to do
*/
retry_write:
if (!is_msgend(i2c)) {
byte = i2c->msg->buf[i2c->msg_ptr++];
writeb(byte, i2c->regs + S3C2410_IICDS);
/* delay after writing the byte to allow the
* data setup time on the bus, as writing the
* data to the register causes the first bit
* to appear on SDA, and SCL will change as
* soon as the interrupt is acknowledged */
ndelay(i2c->tx_setup);
} else if (!is_lastmsg(i2c)) {/* 推进到下一条消息 */
/* we need to go to the next i2c message */
dev_dbg(i2c->dev, "WRITE: Next Message\n");
i2c->msg_ptr = 0;
i2c->msg_idx ++;
i2c->msg++;
/* 检查是否要为该消息产生开始位 */
/* check to see if we need to do another message */
if (i2c->msg->flags & I2C_M_NOSTART) {
if (i2c->msg->flags & I2C_M_RD) {
/* cannot do this, the controller
* forces us to send a new START
* when we change direction */
s3c24xx_i2c_stop(i2c, -EINVAL);
}
goto retry_write;
} else {
/* 发送新的开始位 */
/* send the new start */
s3c24xx_i2c_message_start(i2c, i2c->msg);
i2c->state = STATE_START;
}
} else {
/* send stop */
s3c24xx_i2c_stop(i2c, 0);
}
break;
case STATE_READ:
/* we have a byte of data in the data register, do
* something with it, and then work out wether we are
* going to do any more read/write
*/
/* 有一个字节可读,看是否还有消息要处理*/
if (!(i2c->msg->flags & I2C_M_IGNORE_NAK) &&
!(is_msglast(i2c) && is_lastmsg(i2c))) {
if (iicstat & S3C2410_IICSTAT_LASTBIT) {
dev_dbg(i2c->dev, "READ: No Ack\n");
s3c24xx_i2c_stop(i2c, -ECONNREFUSED);
goto out_ack;
}
}
byte = readb(i2c->regs + S3C2410_IICDS);
i2c->msg->buf[i2c->msg_ptr++] = byte;
prepare_read:
if (is_msglast(i2c)) {
/* last byte of buffer */
if (is_lastmsg(i2c))
s3c24xx_i2c_disable_ack(i2c);
} else if (is_msgend(i2c)) {/* 还有消息要处理吗? */
/* ok, we've read the entire buffer, see if there
* is anything else we need to do */
if (is_lastmsg(i2c)) {
/* last message, send stop and complete */
dev_dbg(i2c->dev, "READ: Send Stop\n");
s3c24xx_i2c_stop(i2c, 0);
} else {
/* go to the next transfer *//* 推进到下一条消息 */
dev_dbg(i2c->dev, "READ: Next Transfer\n");
i2c->msg_ptr = 0;
i2c->msg_idx++;
i2c->msg++;
}
}
break;
}
/* acknowlegde the IRQ and get back on with the work */
out_ack:/* irq 清除 */
tmp = readl(i2c->regs + S3C2410_IICCON);
tmp &= ~S3C2410_IICCON_IRQPEND;
writel(tmp, i2c->regs + S3C2410_IICCON);
out:
return ret;
}
中断处理函数 s3c24xx_i2c_irq()主要通过调用 i2s_s3c_irq_nextbyte()函数进行传输工 作 的 进 一 步 推 进 。 i2s_s3c_irq_nextbyte() 函 数 通 过 switch(i2c->state) 语 句 分 成i2c->state 的不同状态进行处理,在每种状态下,先检查 i2c->state 的状态与硬件寄存器应该处于的状态是否一致,如果不一致,则证明有误,直接返回。当 I2C 处于读状态 STATE_READ 或写状态 STATE_WRITE 时,通过 is_lastmsg()函数判断是否传输的是最后一条 I2C 消息,如果是,则产生停止位,否则通过 i2c->msg_idx++、i2c->msg++推进到下一条消息。