Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2150663
  • 博文数量: 438
  • 博客积分: 3871
  • 博客等级: 中校
  • 技术积分: 6075
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-10 00:11
个人简介

邮箱: wangcong02345@163.com

文章分类

全部博文(438)

文章存档

2017年(15)

2016年(119)

2015年(91)

2014年(62)

2013年(56)

2012年(79)

2011年(16)

分类: LINUX

2013-07-19 17:20:38

一. I2C控制器设备的定义
1.0 I2C控制器设备的定义及注册
a. 在arch/arm/plat-samsung/dev-i2c0.c中,定义了I2C控制器设备的资源,
  1. static struct resource s3c_i2c_resource[] = {
  2.     [0] = {
  3.         .start = S3C_PA_IIC,
  4.         .end = S3C_PA_IIC + SZ_4K - 1,
  5.         .flags = IORESOURCE_MEM,
  6.     },
  7.     [1] = {
  8.         .start = IRQ_IIC,
  9.         .end = IRQ_IIC,
  10.         .flags = IORESOURCE_IRQ,
  11.     },
  12. };

  13. struct platform_device s3c_device_i2c0 = {
  14.     .name         = "s3c2410-i2c",
  15. #ifdef CONFIG_S3C_DEV_I2C1
  16.     .id         = 0,
  17. #else
  18.     .id         = -1,
  19. #endif
  20.     .num_resources     = ARRAY_SIZE(s3c_i2c_resource),
  21.     .resource     = s3c_i2c_resource,
  22. };
  23. I2C控制器设备的私有成员定义
  24. static struct s3c2410_platform_i2c default_i2c_data0 __initdata = {
  25.     .flags        = 0,
  26.     .slave_addr    = 0x10,
  27.     .frequency    = 100*1000,
  28.     .sda_delay    = 100,
  29. };

b. I2C控制器设备的注册

注册过程还是老样子, 在machine_init中完成的
arch/arm/mach-s3c64xx/mach-smdk6410.c
  1. static struct platform_device *smdk6410_devices[] __initdata = {
  2.     &s3c_device_i2c0,
  3. }
  4. static void __init smdk6410_machine_init(void)
  5. {
  6.     s3c_i2c0_set_platdata(NULL);
  7.     platform_add_devices(smdk6410_devices, ARRAY_SIZE(smdk6410_devices));
  8. }

二. I2C控制器设备驱动
2.1 I2C控制器的初始化
a. I2C控制器的驱动在文件, drivers/i2c/busses/i2c-s3c2410.c中
  1. static struct platform_device_id s3c24xx_driver_ids[] = {
  2.     {
  3.         .name        = "s3c2410-i2c",
  4.         .driver_data    = TYPE_S3C2410,
  5.     }, {
  6.         .name        = "s3c2440-i2c",
  7.         .driver_data    = TYPE_S3C2440,
  8.     }, { },
  9. };
  10. MODULE_DEVICE_TABLE(platform, s3c24xx_driver_ids);

  11. static struct platform_driver s3c24xx_i2c_driver = {
  12.     .probe        = s3c24xx_i2c_probe,
  13.     .remove        = s3c24xx_i2c_remove,
  14.     .id_table    = s3c24xx_driver_ids,                //可以实现一个设备驱动对应多个设备
  15.     .driver        = {
  16.         .owner    = THIS_MODULE,
  17.         .name    = "s3c-i2c",
  18.         .pm    = S3C24XX_DEV_PM_OPS,
  19.     },
  20. };

  21. static int __init i2c_adap_s3c_init(void)
  22. {
  23.     return platform_driver_register(&s3c24xx_i2c_driver);
  24. }
注意: 在I2C控制器的设备定义中名字是 s2c2410-i2c而这儿是s3c-i2c,两个名字不相同,如何匹配呢?
答案在s3c24xx_driver_ids中,如果没有s3c24xx_driver_ids,那么I2C控制器设备与I2C控制器设备驱动是一一对应的,一个设备对应一个设备驱动;
但是有了s3c24xx_driver_ids之后,一个设备驱动可以对应多个设备
b. 进入probe函数
  1. static int s3c24xx_i2c_probe(struct platform_device *pdev)
  2. {
  3.     struct s3c2410_platform_i2c* pdata = pdev->dev.platform_data;    
  4.     //申请一个s3c24xx_i2c结构体并初始化   
  5.     struct s3c24xx_i2c * i2c = kzalloc(sizeof(struct s3c24xx_i2c), GFP_KERNEL);
  6.     strlcpy(i2c->adap.name, "s3c2410-i2c", sizeof(i2c->adap.name));
  7.     i2c->adap.owner = THIS_MODULE;
  8.     i2c->adap.algo = &s3c24xx_i2c_algorithm;
  9.     i2c->adap.retries = 2;
  10.     i2c->adap.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
  11.     i2c->tx_setup = 50;
  12.     spin_lock_init(&i2c->lock);
  13.     init_waitqueue_head(&i2c->wait);
  14.     i2c->dev = &pdev->dev;
  15.     i2c->clk = clk_get(&pdev->dev, "i2c");             //时钟使能
  16.     clk_enable(i2c->clk);   
  17.     //映射i2c的控制寄存器
  18.     struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);      
  19.     i2c->ioarea = request_mem_region(res->start, resource_size(res), pdev->name);
  20.     i2c->regs = ioremap(res->start, resource_size(res));
  21.     i2c->adap.algo_data = i2c;
  22.     i2c->adap.dev.parent = &pdev->dev;
  23.     ret = s3c24xx_i2c_init(i2c);                        //配置寄存器,初始化i2c   
  24.     //获取中断
  25.     i2c->irq = ret = platform_get_irq(pdev, 0);   
  26.     ret = request_irq(i2c->irq, s3c24xx_i2c_irq, IRQF_DISABLED, dev_name(&pdev->dev), i2c);
  27.     ret = s3c24xx_i2c_register_cpufreq(i2c);
  28.     i2c->adap.nr = pdata->bus_num;
  29.     ret = i2c_add_numbered_adapter(&i2c->adap);         //最关键的一步,把这个i2c控制器添加到adapter中
  30.     platform_set_drvdata(pdev, i2c);
  31.     clk_disable(i2c->clk);
  32.     return 0;
  33. }
c. i2c控制器的初始化函数
s3c24xx_i2c_probe
    --> s3c24xx_i2c_init
  1. static int s3c24xx_i2c_init(struct s3c24xx_i2c *i2c)
  2. {
  3.     unsigned long iicon = S3C2410_IICCON_IRQEN | S3C2410_IICCON_ACKEN;
  4.     struct s3c2410_platform_i2c *pdata;
  5.     unsigned int freq;
  6.     pdata = i2c->dev->platform_data;
  7.     if (pdata->cfg_gpio)
  8.         pdata->cfg_gpio(to_platform_device(i2c->dev));
  9.     writeb(pdata->slave_addr, i2c->regs + S3C2410_IICADD);  
  10.     writel(iicon, i2c->regs + S3C2410_IICCON);
  11.     s3c24xx_i2c_clockrate(i2c, &freq);   
  12.     return 0;
  13. }
d. i2c控制器及其上的设备注册过程
s3c24xx_i2c_probe
    --> i2c_add_numbered_adapter
  1. int i2c_add_numbered_adapter(struct i2c_adapter *adap)
  2. {
  3. retry:
  4.     if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
  5.         return -ENOMEM;

  6.     mutex_lock(&core_lock);
  7.    
  8.     status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
  9.    
  10.     mutex_unlock(&core_lock);
  11.     if (status == -EAGAIN)
  12.         goto retry;

  13.     if (status == 0)
  14.         status = i2c_register_adapter(adap);                //注册i2c总线及其上的设备
  15.     return status;
  16. }
s3c24xx_i2c_probe
    --> i2c_add_numbered_adapter
        --> i2c_register_adapter
  1. static int i2c_register_adapter(struct i2c_adapter *adap)
  2. {
  3.     int res = 0;

  4.     rt_mutex_init(&adap->bus_lock);
  5.     mutex_init(&adap->userspace_clients_lock);
  6.     INIT_LIST_HEAD(&adap->userspace_clients);

  7.     if (adap->timeout == 0)
  8.         adap->timeout = HZ;

  9.     dev_set_name(&adap->dev, "i2c-%d", adap->nr);
  10.     adap->dev.bus = &i2c_bus_type;
  11.     adap->dev.type = &i2c_adapter_type;
  12.     res = device_register(&adap->dev);            //注册i2c控制器
  13.     if (adap->nr < __i2c_first_dynamic_bus_num)
  14.         i2c_scan_static_board_info(adap);        //扫描i2c总线上的所有设备,并注册每一个设备

  15.     mutex_lock(&core_lock);
  16.     bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
  17.     mutex_unlock(&core_lock);

  18.     return 0;
  19. }
s3c24xx_i2c_probe
    --> i2c_add_numbered_adapter
        --> i2c_register_adapter
            --> i2c_scan_static_board_info
  1. static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
  2. {
  3.     //__i2c_board_list是在smdk6410.c中初始化的,是系统中全部i2c设备的列表
  4.     //这儿要扫描全部的i2c设备,并为每一个i2c设备注册
  5.     down_read(&__i2c_board_lock);
  6.     list_for_each_entry(devinfo, &__i2c_board_list, list) {
  7.         if (devinfo->busnum == adapter->nr && !i2c_new_device(adapter, &devinfo->board_info))
  8.             dev_err(&adapter->dev,"Can't create device at 0x%02x\n", devinfo->board_info.addr);
  9.     }
  10.     up_read(&__i2c_board_lock);
  11. }
s3c24xx_i2c_probe
    --> i2c_add_numbered_adapter
        --> i2c_register_adapter
            --> i2c_scan_static_board_info
                --> i2c_new_device
  1. struct i2c_client * i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
  2. {
  3.     struct i2c_client * client = kzalloc(sizeof *client, GFP_KERNEL);          //申请i2c_client结构体的内存
  4.    
  5.     //把i2c_client结构体初始化一下
  6.     client->adapter = adap;
  7.     client->dev.platform_data = info->platform_data;
  8.     if (info->archdata)
  9.         client->dev.archdata = *info->archdata;   
  10.     client->flags = info->flags;
  11.     client->addr = info->addr;
  12.     client->irq = info->irq;
  13.     strlcpy(client->name, info->type, sizeof(client->name));
  14.     status = i2c_check_client_addr_validity(client);
  15.     status = i2c_check_addr_busy(adap, client->addr);
  16.    
  17.     //把client->dev结构体初始化一下
  18.     client->dev.parent = &client->adapter->dev;
  19.     client->dev.bus = &i2c_bus_type;
  20.     client->dev.type = &i2c_client_type;
  21.     client->dev.of_node = info->of_node;
  22.     dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap), client->addr);
  23.     status = device_register(&client->dev);     //注册client->dev
  24.     return client;      //这里虽然返回client结构体,但是没有使用
  25. }
注意: !i2c_new_device(adapter, &devinfo->board_info);
i2c_new_device虽然有个client结构体但是没有人使用,只是把client->dev 注册了一下,client中剩余的部分如变量的addr flags没有人管.
那问题出来了,在使用时如果需要client->addr怎么办? (例如: i2c在数据传输时需要器件地址 addr=0x30)
答: 用小结构体得到大结构体 --> container_of -->struct i2c_client    *client = i2c_verify_client(dev);

2.2 I2C控制器的algorithm
s3c24xx_i2c_probe中注册了i2c控制器的algorithm是s3c24xx_i2c_algorithm
  1. static const struct i2c_algorithm s3c24xx_i2c_algorithm = {
  2.     .master_xfer        = s3c24xx_i2c_xfer,
  3.     .functionality        = s3c24xx_i2c_func,
  4. };
a.下面就看一下,s3c24xx_i2c_xfer
  1. static int s3c24xx_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  2. {
  3.     struct s3c24xx_i2c *i2c = (struct s3c24xx_i2c *)adap->algo_data;
  4.     clk_enable(i2c->clk);              //使能i2c时钟
  5.     //调用s3c24xx_i2c_doxfer行进传输
  6.     //重复adap->retries次,如果不成功则sleep 100us
  7.     for (retry = 0; retry < adap->retries; retry++) {
  8.         ret = s3c24xx_i2c_doxfer(i2c, msgs, num);      //传输msg,但是真正的数据传输的过程是中断
  9.         if (ret != -EAGAIN) {
  10.             clk_disable(i2c->clk);
  11.             return ret;
  12.         }
  13.         udelay(100);
  14.     }
  15.     clk_disable(i2c->clk);              //关闭i2c时钟
  16.     return -EREMOTEIO;
  17. }
b. 进入开始传输状态
s3c24xx_i2c_xfer
    --> s3c24xx_i2c_doxfer
  1. static int s3c24xx_i2c_doxfer(struct s3c24xx_i2c *i2c, struct i2c_msg *msgs, int num)
  2. {
  3.     unsigned long iicstat, timeout;
  4.     int spins = 20;
  5.     int ret;

  6.     if (i2c->suspended)
  7.         return -EIO;

  8.     ret = s3c24xx_i2c_set_master(i2c);       //等侍I2C总线为空闲状态
  9.     spin_lock_irq(&i2c->lock);
  10.     i2c->msg = msgs;
  11.     i2c->msg_num = num;
  12.     i2c->msg_ptr = 0;
  13.     i2c->msg_idx = 0;
  14.     i2c->state = STATE_START;                //设定此时状态为START
  15.     s3c24xx_i2c_enable_irq(i2c);             //开中断  
  16.     s3c24xx_i2c_message_start(i2c, msgs);    //进入start传输状态,剩下的事就交给中断了
  17.     spin_unlock_irq(&i2c->lock);

  18.     timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);   //进入等侍状态,直到传输结束,产生stop信号将其唤醒
  19.     ret = i2c->msg_idx;
  20.     do {
  21.         iicstat = readl(i2c->regs + S3C2410_IICSTAT);
  22.     } while ((iicstat & S3C2410_IICSTAT_START) && --spins);
  23.   
  24.     if (!spins) {
  25.         msleep(1);
  26.         iicstat = readl(i2c->regs + S3C2410_IICSTAT);
  27.     }
  28.  out:
  29.     return ret;
  30. }
注意 I2C的状态:一个是通过i2c->state可以叫做软件层的状态标志,另一个就是读取i2c的IICSTAT寄存器标志着当前IIC控制器总线的状态是空闲还是忙
c. 等侍IIC总结空闲
s3c24xx_i2c_xfer
    --> s3c24xx_i2c_doxfer
            --> s3c24xx_i2c_set_master
  1. static int s3c24xx_i2c_set_master(struct s3c24xx_i2c *i2c)
  2. {
  3.     //读取IICSTAT寄存器的状态,直到IIC总线是空闲状态
  4.     //重复读取400次,每次不成功sleep 1ms
  5.     unsigned long iicstat;
  6.     int timeout = 400;
  7.     while (timeout-- > 0) {
  8.         iicstat = readl(i2c->regs + S3C2410_IICSTAT);
  9.         if (!(iicstat & S3C2410_IICSTAT_BUSBUSY))
  10.             return 0;
  11.         msleep(1);
  12.     }
  13.     return -ETIMEDOUT;
  14. }
IICSTAT[bit5] -->  read 0 --> not busy
IICSTAT[bit5] -->  read 1 --> busy
IICSTAT[bit5] -->  write 0 --> 产生一个stop signal
IICSTAT[bit5] -->  write 1 --> 产生一个start signal
d. 进行传输开始状态
  1. static void s3c24xx_i2c_message_start(struct s3c24xx_i2c *i2c, struct i2c_msg *msg)
  2. {
  3.     unsigned int addr = (msg->addr & 0x7f) << 1;
  4.     unsigned long stat;
  5.     unsigned long iiccon;

  6.     stat = 0;
  7.     stat |= S3C2410_IICSTAT_TXRXEN;

  8.     if (msg->flags & I2C_M_RD) {
  9.         stat |= S3C2410_IICSTAT_MASTER_RX;
  10.         addr |= 1;
  11.     } else
  12.         stat |= S3C2410_IICSTAT_MASTER_TX;

  13.     if (msg->flags & I2C_M_REV_DIR_ADDR)
  14.         addr ^= 1;

  15.     /* todo - check for wether ack wanted or not */
  16.     s3c24xx_i2c_enable_ack(i2c);

  17.     iiccon = readl(i2c->regs + S3C2410_IICCON);
  18.     writel(stat, i2c->regs + S3C2410_IICSTAT);
  19.   
  20.     writeb(addr, i2c->regs + S3C2410_IICDS);
  21.  
  22.     ndelay(i2c->tx_setup);
  23.    
  24.     writel(iiccon, i2c->regs + S3C2410_IICCON);

  25.     //IICSTAT[bit5] --> write 1 --> 产生一个start signal,下面就进入中断了
  26.     stat |= S3C2410_IICSTAT_START;   
  27.     writel(stat, i2c->regs + S3C2410_IICSTAT);      
  28. }
e. 在中断中进行实际的传输
IIC产生中断的条件:
   
第1种情况是: 产生了start信号
    第2种情况是: IIC仲裁失败
    第3种情况是: 产生了stop信号
  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.     status = readl(i2c->regs + S3C2410_IICSTAT);
  7.    
  8.     //如果是第2种情况--> IIC仲裁失败,则打印出错信息
  9.     if (status & S3C2410_IICSTAT_ARBITR)      
  10.         dev_err(i2c->dev, "deal with arbitration loss\n");
  11.     //如果当前的状态是IDLE,说明状态出错了
  12.     if (i2c->state == STATE_IDLE) {
  13.         tmp = readl(i2c->regs + S3C2410_IICCON);
  14.         tmp &= ~S3C2410_IICCON_IRQPEND;
  15.         writel(tmp, i2c->regs + S3C2410_IICCON);
  16.         goto out;
  17.     }
  18.     //进入中断调用过程
  19.     i2c_s3c_irq_nextbyte(i2c, status);
  20.  out:
  21.     return IRQ_HANDLED;
  22. }

f. 中断调用过程
s3c24xx_i2c_irq
    --> i2c_s3c_irq_nextbyte
  1. static int i2c_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat)
  2. {
  3.     unsigned long tmp;
  4.     unsigned char byte;
  5.     int ret = 0;

  6.     switch (i2c->state) {
  7.     case STATE_IDLE:       
  8.         goto out;

  9.     case STATE_STOP:
  10.         s3c24xx_i2c_disable_irq(i2c);
  11.         goto out_ack;

  12.     case STATE_START:
  13.         if (iicstat & S3C2410_IICSTAT_LASTBIT && !(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
  14.             s3c24xx_i2c_stop(i2c, -ENXIO);
  15.             goto out_ack;
  16.         }

  17.         if (i2c->msg->flags & I2C_M_RD)
  18.             i2c->state = STATE_READ;
  19.         else
  20.             i2c->state = STATE_WRITE;

  21.         if (is_lastmsg(i2c) && i2c->msg->len == 0) {
  22.             s3c24xx_i2c_stop(i2c, 0);
  23.             goto out_ack;
  24.         }

  25.         if (i2c->state == STATE_READ)
  26.             goto prepare_read;
  27.                                                     //注意:这儿没有break,所以直接到STATE_WRITE或者prepare_read                                   
  28.     case STATE_WRITE:      
  29.         if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
  30.             if (iicstat & S3C2410_IICSTAT_LASTBIT) {
  31.                 s3c24xx_i2c_stop(i2c, -ECONNREFUSED);
  32.                 goto out_ack;
  33.             }
  34.         }
  35.  retry_write:
  36.         if (!is_msgend(i2c)) {                         //如果这个msg中的数据还没有传完
  37.             byte = i2c->msg->buf[i2c->msg_ptr++];       //获取下一个要传输的字节
  38.             writeb(byte, i2c->regs + S3C2410_IICDS);    //向IIC中写入数据
  39.             ndelay(i2c->tx_setup);
  40.         } else if (!is_lastmsg(i2c)) {                  //如果还有其它的msg  
  41.             i2c->msg_ptr = 0;
  42.             i2c->msg_idx++;
  43.             i2c->msg++;                                //移动到下一个msg
  44.             if (i2c->msg->flags & I2C_M_NOSTART) {
  45.                 if (i2c->msg->flags & I2C_M_RD)
  46.                     s3c24xx_i2c_stop(i2c, -EINVAL);
  47.                 goto retry_write;
  48.             } else {                                   //产生一个start信号
  49.                 s3c24xx_i2c_message_start(i2c, i2c->msg);
  50.                 i2c->state = STATE_START;
  51.             }
  52.         } else {
  53.             s3c24xx_i2c_stop(i2c, 0);                //终于传完了,就发送结束singal,并关中断
  54.         }                                           
  55.         break;
  56.     case STATE_READ:
  57.         byte = readb(i2c->regs + S3C2410_IICDS);
  58.         i2c->msg->buf[i2c->msg_ptr++] = byte;

  59.  prepare_read:
  60.         if (is_msglast(i2c)) {
  61.             if (is_lastmsg(i2c))
  62.                 s3c24xx_i2c_disable_ack(i2c);
  63.         } else if (is_msgend(i2c)) {
  64.             if (is_lastmsg(i2c))
  65.                 s3c24xx_i2c_stop(i2c, 0);
  66.             else {               
  67.                 i2c->msg_ptr = 0;
  68.                 i2c->msg_idx++;
  69.                 i2c->msg++;
  70.             }
  71.         }
  72.         break;
  73.     }
  74.  out_ack:
  75.     tmp = readl(i2c->regs + S3C2410_IICCON);
  76.     tmp &= ~S3C2410_IICCON_IRQPEND;
  77.     writel(tmp, i2c->regs + S3C2410_IICCON);
  78.  out:
  79.     return ret;
  80. }
s3c24xx_i2c_irq
    --> i2c_s3c_irq_nextbyte
        --> s3c24xx_i2c_stop
  1. static inline void s3c24xx_i2c_stop(struct s3c24xx_i2c *i2c, int ret)
  2. {
  3.     unsigned long iicstat = readl(i2c->regs + S3C2410_IICSTAT);

  4.     iicstat &= ~S3C2410_IICSTAT_START;
  5.     writel(iicstat, i2c->regs + S3C2410_IICSTAT);

  6.     i2c->state = STATE_STOP;                    //状态改为STOP

  7.     s3c24xx_i2c_master_complete(i2c, ret);       //wake_up等侍队列,这时候函数s3c24xx_i2c_doxfer就可以返回了
  8.     s3c24xx_i2c_disable_irq(i2c);                //关中断,不能再让中断产生了
  9. }



三. 总结



阅读(1551) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~