首先这篇文章主要介绍IIC总线的实现过程,怎么说了,本人也是一个小菜鸟,可能有许多错误在里面,如有大神发现,请指出来,多谢多谢!
注意:平台还是和前面的一样,所以分析三星的iic总线实现,当然这部分,可能不需要咱们驱动工程师实现,但本人认为好好研究这部分内容有助于提高水平,也能更好的理解linux的设备模型,不过今天仅仅讨论三星iic总线驱动具体是怎么操作s5pv210上的iic模块的,至于涉及到的platform总线还有设备模型会在接下来的文章中讨论,希望能给像我一样的菜鸟一点帮助!
1、首先说一下主要的讨论内容,主要是通过解释下面几个问题为指引展开的
(1)怎么把s5pv210上的iic模块注册到linux中的?
(2)iic模块是怎么获得时钟的,以及怎么样才能调节时钟的快慢?
(3)iic的总线驱动是怎么注册到iic-core当中的?
(4)具体怎么把一个字符通过片上iic模块发送出去?
好吧,咱们就按问题来吧。三星的iic总线驱动的位置在:linux-3.0.8/drivers/i2c/busses/i2c-s3c2410.c就是这个文件了,整个程序也就是1000行吧。那先从init和exit看起吧,这是看驱动的出发点嘛,为方便起见,我就直接附上代码了
static int __init i2c_adap_s3c_init(void)
{
return platform_driver_register(&s3c24xx_i2c_driver);
}
subsys_initcall(i2c_adap_s3c_init);
static void __exit i2c_adap_s3c_exit(void)
{
platform_driver_unregister(&s3c24xx_i2c_driver);
}
module_exit(i2c_adap_s3c_exit);
可以很清楚的看到,它使用platform总线以驱动的形式注册到内核里面的。这里注意一下,总线也是设备,当然可能你早已经知道了。是设备嘛,肯定就需要驱动了,要不然linux的应用层就没法用这个iic模块了,那这是驱动,那对应的设备的信息在什么地方了?说明一下,我用的是友善的开发板,所以就直接用友善提供的linux源码包了,你可以在linux-3.0.8/arch/arm/mach-s5pv210/mach-mini210.c这个文件中看到如下的函数:
static struct platform_device *mini210_devices[] __initdata = {
...
&s3c_device_i2c0,
&s3c_device_i2c1,
&s3c_device_i2c2,}
看到了吧,用platform_device结构体定义的这个指针数组中,有上面的三项,这是三项就是s5pv210上面的iic模块的信息,你可以点进去看看。当然,这仅仅是信息,还没有注册到platform总线上了,还是在这个文件中,下面的代码就是注册设备了。
static void __init mini210_machine_init(void)
{
......
platform_add_devices(mini210_devices, ARRAY_SIZE(mini210_devices));
......
}
上面的这个函数就是把刚才那个指针数组中定义的设备信息注册到platform总线上了。至于当给platform总线上注册一个驱动后内核如何进行操作,这里就不讨论了,等那天再写一篇吧。当注册到platform总线上的驱动的名字和已经注册到platform上的设备的名字匹配后,就会调用probe函数,其实许多事都是在probe函数做的,我下面直接把代码附上,直接在代码里做解释吧。
static int s3c24xx_i2c_probe(struct platform_device *pdev)
{
struct s3c24xx_i2c *i2c;
struct s3c2410_platform_i2c *pdata;
struct resource *res;
int ret;
pdata = pdev->dev.platform_data; //通过这里获得了,iic模块的一些附加信息,包括总线设备号、iic的时钟频率等
if (!pdata) {
dev_err(&pdev->dev, "no platform data\n");
return -EINVAL;
}
i2c = kzalloc(sizeof(struct s3c24xx_i2c), GFP_KERNEL);
if (!i2c) {
dev_err(&pdev->dev, "no memory for state\n");
return -ENOMEM;
}
//下面的这几句很关键,你应该还记得iic中的adapter这个结构体吧,它就代表一个iic适配器
strlcpy(i2c->adap.name, "s3c2410-i2c", sizeof(i2c->adap.name));//给总线取名字
i2c->adap.owner = THIS_MODULE;
i2c->adap.algo = &s3c24xx_i2c_algorithm;//这个是最重要的,是iic适配器对应的通信方法,也就是具体怎么把数据通过iic模块传输出去的方法,这也是我们的主要问题之一,所以会在后面好好讨论一下
i2c->adap.retries = 2;
i2c->adap.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
i2c->tx_setup = 50;//这个在通信方法里面可以看到,就是每次发送数据后的要延时的ns数
spin_lock_init(&i2c->lock);
init_waitqueue_head(&i2c->wait);
/* find the clock and enable it */
i2c->dev = &pdev->dev;
i2c->clk = clk_get(&pdev->dev, "i2c");//获取iic适配器的时钟,至于具体通过怎么用名字匹配获取等,这个这里就不说了
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);//这里就把iic设备的时钟给使能了,仅仅是使能,通信的频率不是这里调节的啊,具体怎么调节通信的频率高低我会详细介绍
/* map the registers */
//下面就是通过调用相应的函数获取我们在mach-mini210.c文件里注册到platform总线上的iic适配器的信息了,下面是获取内存资源
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, resource_size(res),
pdev->name);
if (i2c->ioarea == NULL) {
dev_err(&pdev->dev, "cannot request IO\n");
ret = -ENXIO;
goto err_clk;
}
//这里就是用申请到的内存做实际的映射了
i2c->regs = ioremap(res->start, resource_size(res));
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 */
//这个函数里,主要就是通过配置iic适配器的几个寄存器,来完成实际的适配器的初始化,上面的过程主要是获得适配器的资源,比如说地址了,寄存器基地址什么的
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
*/
//下面这句话就是获取iic适配器的中断资源,其实iic适配器实际传输数据时用中断的方式完成的,所以作用是可想而知了
i2c->irq = ret = platform_get_irq(pdev, 0);
if (ret <= 0) {
dev_err(&pdev->dev, "cannot find IRQ\n");
goto err_iomap;
}
//这就是把中断注册到内核中的函数了,注意这里的s3c24xx_i2c_irq这个中断服务函数,其实在这里真真完成数据传输
ret = request_irq(i2c->irq, s3c24xx_i2c_irq, IRQF_DISABLED,
dev_name(&pdev->dev), i2c);
if (ret != 0) {
dev_err(&pdev->dev, "cannot claim IRQ %d\n", i2c->irq);
goto err_iomap;
}
ret = s3c24xx_i2c_register_cpufreq(i2c);
if (ret < 0) {
dev_err(&pdev->dev, "failed to register cpufreq notifier\n");
goto err_irq;
}
/* Note, previous versions of the driver used i2c_add_adapter()
* to add the bus at any number. We now pass the bus number via
* the platform data, so if unset it will now default to always
* being bus 0.
*/
i2c->adap.nr = pdata->bus_num;
//下面这个函数式iic-core里面的函数,这个函数的作用就是把上面获取的iic适配器的注册为iic总线设备,具体的实现可以看看iic-core
ret = i2c_add_numbered_adapter(&i2c->adap);
if (ret < 0) {
dev_err(&pdev->dev, "failed to add bus to i2c core\n");
goto err_cpufreq;
}
platform_set_drvdata(pdev, i2c);
dev_info(&pdev->dev, "%s: S3C I2C adapter\n", dev_name(&i2c->adap.dev));
clk_disable(i2c->clk);
return 0;
err_cpufreq:
s3c24xx_i2c_deregister_cpufreq(i2c);
err_irq:
free_irq(i2c->irq, 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:
kfree(i2c);
return ret;
}
看到上面的用红颜色标出来的s3c24xx_algorithm这个结构里吧,这个结构体实现了iic适配器具体的通信方法,具体如下:
static const struct i2c_algorithm s3c24xx_i2c_algorithm = {
.master_xfer = s3c24xx_i2c_xfer, //这个函数是具体的通信方法
.functionality = s3c24xx_i2c_func, //这个函数描述的iic适配器的功能特性
};
由于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;
clk_enable(i2c->clk); //使能时钟嘛
for (retry = 0; retry < adap->retries; retry++) { //这个循环来完成实际的数据传输,循环的次数可以自定义了,这是为了保证数据传输的可靠性嘛
ret = s3c24xx_i2c_doxfer(i2c, msgs, num);//还记得iic里面重要的那个几个数据结构嘛,其中一个就是msg吧,在linux中,把要通过iic传输的具体数据会封装成msg结构的一个包,它里面包含的要传输数据的长度,设备的地址,要传输的数据。看出来吧,还没到到真真传输数据了,内核会调用这个函数
if (ret != -EAGAIN) { //你看,只要以传输成功,马上就返回了
clk_disable(i2c->clk);
return ret;
}
dev_dbg(i2c->dev, "Retrying transmission (%d)\n", retry);
udelay(100);
}
clk_disable(i2c->clk);
return -EREMOTEIO;
}
好吧,还没到真真传输数据的函数,这黄金一层又一层的,但这样写的确是有道理的,就比如说通过上面的for循环能加强数据传输的可靠性。好吧,咱们就硬着坚硬的头继续吧。
static int s3c24xx_i2c_doxfer(struct s3c24xx_i2c *i2c,
struct i2c_msg *msgs, int num)
{
unsigned long timeout;
int ret;
if (i2c->suspended)
return -EIO;
ret = s3c24xx_i2c_set_master(i2c); //这个函数就是看iic适配器是否在忙,如果在忙就等待,直到把得到这个iic适配器的使用权
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; //上面这几句就是把msg结构体赋值给在前面全局定义的iic结构体,因为还要包含其他信息嘛
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 */
udelay(10);
out:
return ret;
}
哎,还是没有到真真要传输的函数啊,那为什么要写这个函数啊,因为在有三个iic模块嘛,当然多少模块都行,这样写可以让三个模块或更多的模块公用这个函数,减少代码重复量嘛,膜拜一下这些高手吧,好吧,生活还得继续,那咱们也不能不前进啊,看看s3c24xx_i2c_message_irq这个函数吧
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) { //看到了吧,这就是msg结构体里面的flag起的作用,判断是否是接收数据,也就是读数据了
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);
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); //上面这些通过readl和writel来读取和设置了一些iic适配器的寄存器,最后边的这句话,是最关键的,它设置iic适配器为开始状态,这样就能触发中断,来完成实际数据的传输,用中断传输的好处我就不说了,想来大家都会知道啊。
}
这家伙终于启动了中断,要完成数据的传输了。中断具体怎么传输的,这个大家可以看看,做过单片机的人肯定还是挺亲切的。发现说了这么多,其实才把刚开始提出的问题1和问题4讨论一下,2和3还没有详细的讨论,那咱们就在下一篇继续讨论吧。