手机版
你好,游客 登录 注册
背景:
阅读新闻

Linux IIC框架

[日期:2012-08-13] 来源:Linux社区  作者:vanbreaker [字体:大 中 小]

本节结合i2cdev,来阐述Linux下的IIC是如何进行数据传输的。和spidev类似,i2cdev也是一个通用的设备驱动,但是又有些不同。在spidev中,spidev驱动注册会和相应的从设备绑定,也就是说spidev对应的是一个实际的从设备,而i2cdev只是一个虚拟的从设备,因为它并不对应一个实际的从设备,而是和IIC控制器adapter绑定,只有当用户调用open()打开一个设备文件时,才会创建一个虚拟的client。那么怎么知道该i2cdev对应哪个从设备呢?其实很简单,只需要用户在用户空间多做一个工作,就是通过Iocntl()函数来设定从设备的地址,当然这个从设备必须是挂接在对应的adapter下的。所以i2cdev作为一个虚拟设备,本身并不对应任何设备,但又可以对应任意一个挂接在其依附的adapter下的设备。

先来看下i2cdev的定义

  1. struct i2c_dev {  
  2.     struct list_head list;//用于链入i2c_dev_list   
  3.     struct i2c_adapter *adap;//依附的IIC主控制器   
  4.     struct device *dev;  
  5. };  

第一步,当然是初始化i2cdev模块,将i2cdev作为字符设备注册,创建一个i2cdev类,并且注册i2cdev的驱动

  1. static int __init i2c_dev_init(void)  
  2. {  
  3.     int res;  
  4.   
  5.     printk(KERN_INFO "i2c /dev entries driver\n");  
  6.   
  7.     /*注册i2cdev字符设备*/  
  8.     res = register_chrdev(I2C_MAJOR, "i2c", &i2cdev_fops);  
  9.     if (res)  
  10.         goto out;  
  11.   
  12.          /*创建i2cdev类*/  
  13.     i2c_dev_class = class_create(THIS_MODULE, "i2c-dev");  
  14.     if (IS_ERR(i2c_dev_class)) {  
  15.         res = PTR_ERR(i2c_dev_class);  
  16.         goto out_unreg_chrdev;  
  17.     }  
  18.   
  19.     /*添加i2cdev驱动*/  
  20.     res = i2c_add_driver(&i2cdev_driver);  
  21.     if (res)  
  22.         goto out_unreg_class;  
  23.   
  24.     return 0;  
  25.   
  26. out_unreg_class:  
  27.     class_destroy(i2c_dev_class);  
  28. out_unreg_chrdev:  
  29.     unregister_chrdev(I2C_MAJOR, "i2c");  
  30. out:  
  31.     printk(KERN_ERR "%s: Driver Initialisation failed\n", __FILE__);  
  32.     return res;  
  33. }  

i2c_add_driver()只是i2c_register_driver()的一个封装,进入i2c_register_driver()

  1. int i2c_register_driver(struct module *owner, struct i2c_driver *driver)  
  2. {  
  3.     ...  
  4.          ...  
  5.     ...  
  6.     driver->driver.bus = &i2c_bus_type;  
  7.          ...  
  8.     res = driver_register(&driver->driver);  
  9.          ...  
  10.     /*遍历i2c_adapter_class下的i2c_adapter,并调用__attach_adapter*/  
  11.     class_for_each_device(&i2c_adapter_class, NULL, driver,  
  12.                   __attach_adapter);  
  13.          ...  
  14.     return 0;  
  15. }  

我们可以看到主要操作有两个,一个是driver_register(),还有一个就是遍历adapter,并调用__attach_adapter()

实际上这里的driver_register()并没有太多实际的作用,因为它无法完成i2cdev_driver和从设备的匹配,为什么呢?我们来看一下i2c_bus_type中定义的match函数

  1. static int i2c_device_match(struct device *dev, struct device_driver *drv)  
  2. {  
  3.     struct i2c_client   *client = to_i2c_client(dev);  
  4.     struct i2c_driver   *driver = to_i2c_driver(drv);  
  5.   
  6.     /* make legacy i2c drivers bypass driver model probing entirely; 
  7.      * such drivers scan each i2c adapter/bus themselves. 
  8.      */  
  9.     if (!is_newstyle_driver(driver))  
  10.         return 0;  
  11.   
  12.     /* match on an id table if there is one */  
  13.     if (driver->id_table)  
  14.         return i2c_match_id(driver->id_table, client) != NULL;  
  15.   
  16.     return 0;  
  17. }  

 

  1. static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,  
  2.                         const struct i2c_client *client)  
  3. {  
  4.     while (id->name[0]) {  
  5.         if (strcmp(client->name, id->name) == 0)  
  6.             return id;  
  7.         id++;  
  8.     }  
  9.     return NULL;  
  10. }  

可以看到,只有当driver->id_table不为空时,才会调用实际的匹配函数i2c_match_id(),而i2cdev_driver中并没有定义id_table,因此是无法和任何从设备匹配的。

  1. static struct i2c_driver i2cdev_driver = {  
  2.     .driver = {  
  3.         .name   = "dev_driver",  
  4.     },  
  5.     .attach_adapter = i2cdev_attach_adapter,  
  6.     .detach_adapter = i2cdev_detach_adapter,  
  7. };  

再来看__attach_adapter()

  1. static int i2cdev_attach_adapter(struct i2c_adapter *adap)  
  2. {  
  3.     struct i2c_dev *i2c_dev;  
  4.       
  5.          ...  
  6.     i2c_dev = get_free_i2c_dev(adap);  
  7.     /*注册i2c_dev设备*/  
  8.     i2c_dev->dev = device_create(i2c_dev_class, &adap->dev,  
  9.                      MKDEV(I2C_MAJOR, adap->nr), NULL,  
  10.                      "i2c-%d", adap->nr);  
  11.          ...  
  12.     res = device_create_file(i2c_dev->dev, &dev_attr_name);  
  13.     ...  
  14.          ...      
  15. }  

跟踪get_free_i2c_dev()

  1. static struct i2c_dev *get_free_i2c_dev(struct i2c_adapter *adap)  
  2. {  
  3.     struct i2c_dev *i2c_dev;  
  4.   
  5.     if (adap->nr >= I2C_MINORS) {  
  6.         printk(KERN_ERR "i2c-dev: Out of device minors (%d)\n",  
  7.                adap->nr);  
  8.         return ERR_PTR(-ENODEV);  
  9.     }  
  10.   
  11.     /*为i2c_dev申请空间*/  
  12.     i2c_dev = kzalloc(sizeof(*i2c_dev), GFP_KERNEL);  
  13.     if (!i2c_dev)  
  14.         return ERR_PTR(-ENOMEM);  
  15.     i2c_dev->adap = adap;//设置i2c_dev依附的i2c_adapter   
  16.   
  17.     spin_lock(&i2c_dev_list_lock);  
  18.     list_add_tail(&i2c_dev->list, &i2c_dev_list);//将i2c_dev添加到i2c_dev_list   
  19.     spin_unlock(&i2c_dev_list_lock);  
  20.     return i2c_dev;  
  21. }  

到这里就完成了i2cdev_driver的注册,主要任务就是在每个i2c_adapter下创建一个绑定的i2c_dev。

然后就是通过open来打开一个i2cdev

  1. static int i2cdev_open(struct inode *inode, struct file *file)  
  2. {  
  3.     unsigned int minor = iminor(inode);  
  4.     struct i2c_client *client;  
  5.     struct i2c_adapter *adap;  
  6.     struct i2c_dev *i2c_dev;  
  7.     int ret = 0;  
  8.   
  9.     lock_kernel();  
  10.     /*遍历i2c_dev_list,通过次设备号来定位i2c_dev*/  
  11.     i2c_dev = i2c_dev_get_by_minor(minor);  
  12.     if (!i2c_dev) {  
  13.         ret = -ENODEV;  
  14.         goto out;  
  15.     }  
  16.   
  17.     /*获取i2c_dev对应的i2c_adapter*/  
  18.     adap = i2c_get_adapter(i2c_dev->adap->nr);  
  19.     if (!adap) {  
  20.         ret = -ENODEV;  
  21.         goto out;  
  22.     }  
  23.   
  24.     /* This creates an anonymous i2c_client, which may later be 
  25.      * pointed to some address using I2C_SLAVE or I2C_SLAVE_FORCE. 
  26.      * 
  27.      * This client is ** NEVER REGISTERED ** with the driver model 
  28.      * or I2C core code!!  It just holds private copies of addressing 
  29.      * information and maybe a PEC flag. 
  30.      */  
  31.      /*创建一个client虚拟从设备*/  
  32.     client = kzalloc(sizeof(*client), GFP_KERNEL);  
  33.     if (!client) {  
  34.         i2c_put_adapter(adap);  
  35.         ret = -ENOMEM;  
  36.         goto out;  
  37.     }  
  38.     snprintf(client->name, I2C_NAME_SIZE, "i2c-dev %d", adap->nr);  
  39.     client->driver = &i2cdev_driver;//设定client所依附的driver   
  40.   
  41.     client->adapter = adap;      //设定client所依附的i2c控制器   
  42.     file->private_data = client;  
  43.   
  44. out:  
  45.     unlock_kernel();  
  46.     return ret;  
  47. }  

此时已经建立了一个虚拟的client,下一步就是要让其对应一个实际的从设备,那么就要在用户空间通过iocntl()指定从设备的地址

  1. static long i2cdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)  
  2. {  
  3.     ...  
  4.     switch ( cmd ) {  
  5.     case I2C_SLAVE:  
  6.     case I2C_SLAVE_FORCE:  
  7.         if ((arg > 0x3ff) ||  
  8.             (((client->flags & I2C_M_TEN) == 0) && arg > 0x7f))  
  9.             return -EINVAL;  
  10.         if (cmd == I2C_SLAVE && i2cdev_check_addr(client->adapter, arg))  
  11.             return -EBUSY;  
  12.         /* REVISIT: address could become busy later */  
  13.         client->addr = arg;  
  14.         return 0;  
  15.     ...  
  16. }  

然后还需要设置超时等待的时间,和重试次数等等……就不赘述了。

然后就可以通过read()/write()函数来进行一个message的读写操作,也可以通过iocntl()函数控制进行一个消息组的传输,我们通过write()来分析消息传输的机制

  1. static ssize_t i2cdev_write (struct file *file, const char __user *buf, size_t count,  
  2.                              loff_t *offset)  
  3. {  
  4.     int ret;  
  5.     char *tmp;  
  6.     struct i2c_client *client = (struct i2c_client *)file->private_data;  
  7.   
  8.     if (count > 8192)  
  9.         count = 8192;  
  10.   
  11.     tmp = kmalloc(count,GFP_KERNEL);  
  12.     if (tmp==NULL)  
  13.         return -ENOMEM;  
  14.     if (copy_from_user(tmp,buf,count)) {//从用户空间获取数据   
  15.         kfree(tmp);  
  16.         return -EFAULT;  
  17.     }  
  18.   
  19.     pr_debug("i2c-dev: i2c-%d writing %zu bytes.\n",  
  20.         iminor(file->f_path.dentry->d_inode), count);  
  21.   
  22.     ret = i2c_master_send(client,tmp,count);//调用IIC核心层函数进行发送处理   
  23.     kfree(tmp);  
  24.     return ret;  
  25. }  

 

  1. int i2c_master_send(struct i2c_client *client,const char *buf ,int count)  
  2. {  
  3.     int ret;  
  4.     struct i2c_adapter *adap=client->adapter;  
  5.     struct i2c_msg msg;  
  6.   
  7.     msg.addr = client->addr;//从设备地址   
  8.     msg.flags = client->flags & I2C_M_TEN;  
  9.     msg.len = count;      //数据传输长度   
  10.     msg.buf = (char *)buf;//数据缓冲区   
  11.   
  12.     ret = i2c_transfer(adap, &msg, 1);  
  13.   
  14.     /* If everything went ok (i.e. 1 msg transmitted), return #bytes 
  15.        transmitted, else error code. */  
  16.     return (ret == 1) ? count : ret;  
  17. }  

IIC核心层函数i2c_transfer会调用相应的adap->algo中定义的传输函数master_xfer()进行传输,s3c24xx中的master_xfer()函数为s3c24xx_i2c_xfer(),s3c24xx_i2c_xfer()会调用s3c24xx_i2c_doxfer()进行实际的传输处理

  1. static int s3c24xx_i2c_doxfer(struct s3c24xx_i2c *i2c,  
  2.                   struct i2c_msg *msgs, int num)  
  3. {  
  4.     unsigned long timeout;  
  5.     int ret;  
  6.   
  7.     if (i2c->suspended)  
  8.         return -EIO;  
  9.   
  10.     ret = s3c24xx_i2c_set_master(i2c);  
  11.     if (ret != 0) {  
  12.         dev_err(i2c->dev, "cannot get bus (error %d)\n", ret);  
  13.         ret = -EAGAIN;  
  14.         goto out;  
  15.     }  
  16.   
  17.     spin_lock_irq(&i2c->lock);  
  18.   
  19.     i2c->msg     = msgs;//保存msg起始地址   
  20.     i2c->msg_num = num; //保存msg的数量   
  21.     i2c->msg_ptr = 0;   //已传输的msg数量为0   
  22.     i2c->msg_idx = 0;   //单个msg中已传输的字节为0   
  23.     i2c->state   = STATE_START;//设置状态为start   
  24.   
  25.     s3c24xx_i2c_enable_irq(i2c);//使能i2c中断   
  26.     /*确定是读还是写,并设定相关寄存器,准备开始传输数据*/  
  27.     s3c24xx_i2c_message_start(i2c, msgs);  
  28.     spin_unlock_irq(&i2c->lock);  
  29.   
  30.     /*阻塞进程,直到i2c中断完成传输任务或超时*/  
  31.     timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);  
  32.   
  33.     ret = i2c->msg_idx;  
  34.   
  35.     /* having these next two as dev_err() makes life very 
  36.      * noisy when doing an i2cdetect */  
  37.   
  38.     if (timeout == 0)  
  39.         dev_dbg(i2c->dev, "timeout\n");  
  40.     else if (ret != num)  
  41.         dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret);  
  42.   
  43.     /* ensure the stop has been through the bus */  
  44.   
  45.     msleep(1);  
  46.   
  47.  out:  
  48.     return ret;  
  49. }  

到这里,传输工作就交给IIC中断函数进行处理了

  1. static irqreturn_t s3c24xx_i2c_irq(int irqno, void *dev_id)  
  2. {  
  3.     struct s3c24xx_i2c *i2c = dev_id;  
  4.     unsigned long status;  
  5.     unsigned long tmp;  
  6.   
  7.     status = readl(i2c->regs + S3C2410_IICSTAT);  
  8.   
  9.     if (status & S3C2410_IICSTAT_ARBITR) {  
  10.         /* deal with arbitration loss */  
  11.         dev_err(i2c->dev, "deal with arbitration loss\n");  
  12.     }  
  13.   
  14.     if (i2c->state == STATE_IDLE) {  
  15.         dev_dbg(i2c->dev, "IRQ: error i2c->state == IDLE\n");  
  16.   
  17.         tmp = readl(i2c->regs + S3C2410_IICCON);  
  18.         tmp &= ~S3C2410_IICCON_IRQPEND;  
  19.         writel(tmp, i2c->regs +  S3C2410_IICCON);  
  20.         goto out;  
  21.     }  
  22.   
  23.     /* pretty much this leaves us with the fact that we've 
  24.      * transmitted or received whatever byte we last sent */  
  25.   
  26.     i2s_s3c_irq_nextbyte(i2c, status);  
  27.   
  28.  out:  
  29.     return IRQ_HANDLED;  
  30. }  

我们来看中断的实际处理函数i2s_s3c_irq_nextbyte(),不知道这里为什么是i2s-.-,难道是内核开发者的笔误?

  1. static int i2s_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat)  
  2. {  
  3.     unsigned long tmp;  
  4.     unsigned char byte;  
  5.     int ret = 0;  
  6.   
  7.     switch (i2c->state) {  
  8.   
  9.     case STATE_IDLE:  
  10.         dev_err(i2c->dev, "%s: called in STATE_IDLE\n", __func__);  
  11.         goto out;  
  12.         break;  
  13.   
  14.     case STATE_STOP:  //停止传输,禁止中断   
  15.         dev_err(i2c->dev, "%s: called in STATE_STOP\n", __func__);  
  16.         s3c24xx_i2c_disable_irq(i2c);  
  17.         goto out_ack;  
  18.   
  19.     case STATE_START:  
  20.         /* last thing we did was send a start condition on the 
  21.          * bus, or started a new i2c message 
  22.          */  
  23.   
  24.         if (iicstat & S3C2410_IICSTAT_LASTBIT &&  
  25.             !(i2c->msg->flags & I2C_M_IGNORE_NAK)) {  
  26.             /* ack was not received... */  
  27.   
  28.             dev_dbg(i2c->dev, "ack was not received\n");  
  29.             s3c24xx_i2c_stop(i2c, -ENXIO);  
  30.             goto out_ack;  
  31.         }  
  32.   
  33.         /*确定读写状态*/  
  34.         if (i2c->msg->flags & I2C_M_RD)  
  35.             i2c->state = STATE_READ;  
  36.         else  
  37.             i2c->state = STATE_WRITE;  
  38.   
  39.         /* terminate the transfer if there is nothing to do 
  40.          * as this is used by the i2c probe to find devices. */  
  41.   
  42.         /*如果已经是最后一条消息并且该消息已经发送完,则停止传输*/  
  43.         if (is_lastmsg(i2c) && i2c->msg->len == 0) {  
  44.             s3c24xx_i2c_stop(i2c, 0);  
  45.             goto out_ack;  
  46.         }  
  47.   
  48.         if (i2c->state == STATE_READ)  
  49.             goto prepare_read;  
  50.   
  51.         /* fall through to the write state, as we will need to 
  52.          * send a byte as well */  
  53.   
  54.     case STATE_WRITE:  
  55.         /* we are writing data to the device... check for the 
  56.          * end of the message, and if so, work out what to do 
  57.          */  
  58.   
  59.         if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) {  
  60.             if (iicstat & S3C2410_IICSTAT_LASTBIT) {  
  61.                 dev_dbg(i2c->dev, "WRITE: No Ack\n");  
  62.   
  63.                 s3c24xx_i2c_stop(i2c, -ECONNREFUSED);  
  64.                 goto out_ack;  
  65.             }  
  66.         }  
  67.   
  68.  retry_write:  
  69.   
  70.         if (!is_msgend(i2c)) {//该消息还未发送完毕   
  71.             byte = i2c->msg->buf[i2c->msg_ptr++];  
  72.             writeb(byte, i2c->regs + S3C2410_IICDS);//将数据写入寄存器发送   
  73.   
  74.             /* delay after writing the byte to allow the 
  75.              * data setup time on the bus, as writing the 
  76.              * data to the register causes the first bit 
  77.              * to appear on SDA, and SCL will change as 
  78.              * soon as the interrupt is acknowledged */  
  79.   
  80.             ndelay(i2c->tx_setup);  
  81.   
  82.         } else if (!is_lastmsg(i2c)) {//不是最后一条消息,则取下一条消息   
  83.             /* we need to go to the next i2c message */  
  84.   
  85.             dev_dbg(i2c->dev, "WRITE: Next Message\n");  
  86.   
  87.             i2c->msg_ptr = 0;  
  88.             i2c->msg_idx++;  
  89.             i2c->msg++; //取下一条消息进行发送   
  90.   
  91.             /* check to see if we need to do another message */  
  92.             if (i2c->msg->flags & I2C_M_NOSTART) {  
  93.   
  94.                 if (i2c->msg->flags & I2C_M_RD) {  
  95.                     /* cannot do this, the controller 
  96.                      * forces us to send a new START 
  97.                      * when we change direction */  
  98.   
  99.                     s3c24xx_i2c_stop(i2c, -EINVAL);  
  100.                 }  
  101.   
  102.                 goto retry_write;  
  103.             } else {/*开始新一轮的发送*/  
  104.                 /* send the new start */  
  105.                 s3c24xx_i2c_message_start(i2c, i2c->msg);  
  106.                 i2c->state = STATE_START;  
  107.             }  
  108.   
  109.         } else {  
  110.             /* send stop */  
  111.   
  112.             s3c24xx_i2c_stop(i2c, 0);  
  113.         }  
  114.         break;  
  115.   
  116.     case STATE_READ:  
  117.         /* we have a byte of data in the data register, do 
  118.          * something with it, and then work out wether we are 
  119.          * going to do any more read/write 
  120.          */  
  121.   
  122.         byte = readb(i2c->regs + S3C2410_IICDS);//读取数据   
  123.         i2c->msg->buf[i2c->msg_ptr++] = byte;   //存放数据   
  124.   
  125.  prepare_read:  
  126.         if (is_msglast(i2c)) {//如果是最后一条消息   
  127.             /* last byte of buffer */  
  128.   
  129.             if (is_lastmsg(i2c))//如果消息发送完毕   
  130.                 s3c24xx_i2c_disable_ack(i2c);//失能ack   
  131.   
  132.         } else if (is_msgend(i2c)) {//消息读取完毕   
  133.             /* ok, we've read the entire buffer, see if there 
  134.              * is anything else we need to do */  
  135.   
  136.             if (is_lastmsg(i2c)) {//如果是最后一条消息则停止传输   
  137.                 /* last message, send stop and complete */  
  138.                 dev_dbg(i2c->dev, "READ: Send Stop\n");  
  139.   
  140.                 s3c24xx_i2c_stop(i2c, 0);  
  141.             } else {//否则取下一个消息存放读取的数据   
  142.                 /* go to the next transfer */  
  143.                 dev_dbg(i2c->dev, "READ: Next Transfer\n");  
  144.   
  145.                 i2c->msg_ptr = 0;  
  146.                 i2c->msg_idx++;  
  147.                 i2c->msg++;  
  148.             }  
  149.         }  
  150.   
  151.         break;  
  152.     }  
  153.   
  154.     /* acknowlegde the IRQ and get back on with the work */  
  155.   
  156.  out_ack:  
  157.     tmp = readl(i2c->regs + S3C2410_IICCON);  
  158.     tmp &= ~S3C2410_IICCON_IRQPEND;  
  159.     writel(tmp, i2c->regs + S3C2410_IICCON);  
  160.  out:  
  161.     return ret;  
  162. }

IIC的框架介绍到此为止。

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款