驱动篇:I 2 C 总线驱动实例
控制器硬件描述
S3C2410 处理器内部集成了一个 I 2 C 控制器,通过 4 个寄存器就可方便地对其进行控制,这 4 个寄存器如下。
l IICCON:I 2 C 控制寄存器。
l IICSTAT:I 2 C 状态寄存器。
l IICDS:I 2 C 收发数据移位寄存器。
l IICADD:I 2 C 地址寄存器。
S3C2410 处理器内部集成的 I 2 C 控制器可支持主、从两种模式,我们主要使用其主模式。通过对 IICCON、IICDS 和 IICADD 寄存器的操作,可在 I 2 C 总线上产生开始位、停止位、数据和地址,而传输的状态则通过 IICSTAT 寄存器获取。
I 2 C 总线驱动总体分析
S3C2410的I2C总线驱动设计主要要完成以下工作。
l 设计对应于i2c_adapter_xxx_init()模板的S3C2410 的模块加载函数和对应于i2c_ adapter_xxx_exit()函数模板的模块卸载函数。
l 设计对应于i2c_adapter_xxx_xfer()模板的S3C2410适配器的通信方法函数。针对S3C2410 , functionality() 函数只需简单地返回I2C_FUNC_I2C
| I2C_FUNC_SMBUS_ EMUL | I2C_FUNC_PROTOCOL_MANGLING 表明其支持的功能。
I 2 C 适配器驱动被作为一个单独的模块加载进内核,在模块的加载和卸载函数中,只需注册和注销一个 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);
}
module_init(i2c_adap_s3c_init);
module_exit(i2c_adap_s3c_exit);
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",
},
};
s3c24x x_i 2c_prob e 函数
static int s3c24xx_i2c_probe(struct platform_device *pdev)
{
struct s3c24xx_i2c *i2c = &s3c24xx_i2c;
struct resource *res;
int ret;
/* 使能 I 2 C 的时钟 */
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 out;
}
clk_enable(i2c->clk);
/* 映射寄存器 */
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (res == NULL) {
dev_err(&pdev->dev, "cannot find IO resource\n");
ret = -ENOENT;
goto out;
}
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 out;
}
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 out;
}
/* 设置 I 2 C 的信息块 */
i2c->adap.algo_data = i2c;
i2c->adap.dev.parent = &pdev->dev;
/* 初始化 I 2 C 控制器 */
ret = s3c24xx_i2c_init(i2c);
if (ret != 0)
goto out;
/* 申请中断 */
res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
if (res == NULL) {
ret = -ENOENT;
goto out;
}
ret = request_irq(res->start, s3c24xx_i2c_irq, SA_INTERRUPT,
pdev->name, i2c);
if (ret != 0) {
goto out;
}
i2c->irq = res;
ret = i2c_add_adapter(&i2c->adap); /*添加 i2c_adapter*/
if (ret < 0) {
goto out;
}
platform_set_drvdata(pdev, i2c);
out:
if (ret < 0)
s3c24xx_i2c_free(i2c);
return ret;
}
述代码中的主体工作是使能硬件并申请 I 2 C 适配器使用的 I/O 地址、中断号等,在这些工作都完成无误后,通过 I 2 C 核心提供的 i2c_add_adapter()函数添加这个适配器 。 因 为 S3C2410 内 部 集 成 I 2 C 控 制 器 , 可 以 确 定 I 2 C 适 配 器 一 定 存 在 ,s3c24xx_i2c_probe()函数虽然命名“探测”,但实际没有也不必进行任何探测工作,之所以这样命名完全是一种设计习惯。
s3c24xx_i2c_remove 函数
static int s3c24xx_i2c_remove(struct platform_device *pdev)
{
struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
if (i2c != NULL) {
s3c24xx_i2c_free(i2c);
platform_set_drvdata(pdev, NULL);
}
return 0;
}
代码清单 15.27 和代码清单 15.28 中用到的 s3c24xx_i2c 结构体进行适配器所有信息的封装,类似于私有信息结构体,它与代码清单 15.12 所示的 xxx_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;
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, /*自旋锁未锁定*/
.wait = _ _WAIT_QUEUE_HEAD_INITIALIZER(s3c24xx_i2c.wait),/*等待队列初始化*/
.adap= {
.name = "s3c2410-i2c",
.owner = THIS_MODULE,
.algo = &s3c24xx_i2c_algorithm,
.retries = 2,
.class= I2C_CLASS_HWMON,
},
};
I 2 C 适配器对应的 i2c_algorithm 结构体实例为 s3c24xx_i2c_algorithm
static struct i2c_algorithm s3c24xx_i2c_algorithm = {
.master_xfer= s3c24xx_i2c_xfer,
.functionality= s3c24xx_i2c_func,
};
S3C2410 I 2 C 总线通信传输函数 s3c24xx_i2c_xfer(),这个
函数非常关键,所有 I 2 C 总线上对设备的访问最终应该由它来完成
S3C2410 I 2 C 总线驱动的 master_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;
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) {
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;
if (timeout == 0)
dev_dbg(i2c->dev, "timeout\n");
else if (ret != num)
dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret);
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;
s3c24xx_i2c_enable_ack(i2c);/*如果要使能 ACK,则使能*/
iiccon = readl(i2c->regs + S3C2410_IICCON);
writel(stat, i2c->regs + S3C2410_IICSTAT);
writeb(addr, i2c->regs + S3C2410_IICDS);
udelay(1);/*在发送新的开始位前延迟 1 位*/
writel(iiccon, i2c->regs + S3C2410_IICCON);
stat |= S3C2410_IICSTAT_START;
writel(stat, i2c->regs + S3C2410_IICSTAT);
}
s3c24xx_i2c_xfer()函数调用 s3c24xx_i2c_doxfer()函数传输 I 2 C 消息,s3c24xx_i2c_doxfer()首先将 S3C2410 的 I 2 C 适配器设置为 I 2 C 主设备,其后初始化 s3c24xx_i2c 结构体,使能 I 2 C 中断,并调用 s3c24xx_i2c_message_start()函数启动I 2 C 消息的传输。
s3c24xx_i2c_message_start()函数写 S3C2410 适配器对应的控制寄存器,向 I 2 C 从设备传递开始位和从设备地址。上述代码只是启动了 I 2 C 消息数组的传输周期,并没有完整实现图 15.3 中给出的algorithm master_xfer 流程。这个流程的完整实现需要借助 I 2 C 适配器上的中断来步步推进。
I 2 C 适配器中断处理函数
static irqreturn_t s3c24xx_i2c_irq(int irqno, void *dev_id,struct pt_regs *regs)
{
struct s3c24xx_i2c *i2c = dev_id;
unsigned long status;
unsigned long tmp;
status = readl(i2c->regs + S3C2410_IICSTAT);
if (status & S3C2410_IICSTAT_ARBITR) {
...
}
if (i2c->state == STATE_IDLE) {
tmp = readl(i2c->regs + S3C2410_IICCON);
tmp &= ~S3C2410_IICCON_IRQPEND;
writel(tmp, i2c->regs + S3C2410_IICCON);
goto out;
}
i2s_s3c_irq_nextbyte(i2c, status);/* 把传输工作进一步推进 */
out:
return IRQ_HANDLED;
}
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:
goto out;
break;
case STATE_STOP:
s3c24xx_i2c_disable_irq(i2c);
goto out_ack;
case STATE_START:
/* 我们最近做的一件事是启动一个新 I 2 C 消息*/
if (iicstat & S3C2410_IICSTAT_LASTBIT &&
!(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
/* 没有收到 ACK */
s3c24xx_i2c_stop(i2c, -EREMOTEIO);
goto out_ack;
}
if (i2c->msg->flags & I2C_M_RD)
i2c->state = STATE_READ;
else
i2c->state = STATE_WRITE;
/* 仅一条消息,而且长度为 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;
/* 进入写状态 */
case STATE_WRITE:
retry_write:
if (!is_msgend(i2c)) {
byte = i2c->msg->buf[i2c->msg_ptr++];
writeb(byte, i2c->regs + S3C2410_IICDS);
} else if (!is_lastmsg(i2c)) {
/* 推进到下一条消息 */
i2c->msg_ptr = 0;
i2c->msg_idx ++;
i2c->msg++;
/* 检查是否要为该消息产生开始位 */
if (i2c->msg->flags & I2C_M_NOSTART) {
if (i2c->msg->flags & I2C_M_RD) {
s3c24xx_i2c_stop(i2c, -EINVAL);
}
goto retry_write;
} else {
/* 发送新的开始位 */
s3c24xx_i2c_message_start(i2c, i2c->msg);
i2c->state = STATE_START;
}
} else {
s3c24xx_i2c_stop(i2c, 0);/* send stop */
}
break;
case STATE_READ:
/* 有一个字节可读,看是否还有消息要处理*/
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)) {
/* 还有消息要处理吗? */
if (is_lastmsg(i2c)) {
s3c24xx_i2c_stop(i2c, 0);/* last message, send stop andcomplete */
} else {
/* 推进到下一条消息 */
i2c->msg_ptr = 0;
i2c->msg_idx++;
i2c->msg++;
}
}
break;
}
/* irq 清除 */
out_ack:
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 的状态与硬件寄存器应该处于的状态是否一致,如果不一致,则证明有误,直接返回。当 I 2 C 处于读状态 STATE_READ 或写状态 STATE_WRITE 时,通过 is_lastmsg()函数判断是否传输的是最后一条 I 2 C 消息, 如果是, 则产生停止位, 否则通过 i2c->msg_idx++、 i2c->msg++推进到下一条消息。