Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1162674
  • 博文数量: 146
  • 博客积分: 190
  • 博客等级: 入伍新兵
  • 技术积分: 5225
  • 用 户 组: 普通用户
  • 注册时间: 2012-06-06 08:24
个人简介

慢行者

文章分类

全部博文(146)

文章存档

2013年(145)

2012年(1)

分类: LINUX

2013-05-10 14:42:20

原文地址: 作者:


1820人阅读 评论(8) 举报

博主按:大热的天,刚刚负重从五道口走到石板房,大约4公里吧。终于让我找了一个咖啡屋休息一下,继续写这篇驱动分析。单身的生活就是这样无聊啊。 不发牢骚了,活出个样儿来给自己看!千难万险脚下踩,啥也难不倒咱!继续整!~


先说一下,本文中有个疑惑,一直没有搞懂,写在这里,望高人指点一二,不胜感激!

#define I2C_M_NOSTART  0x4000 /* if I2C_FUNC_PROTOCOL_MANGLING */
#define I2C_M_REV_DIR_ADDR 0x2000 /* if I2C_FUNC_PROTOCOL_MANGLING */
#define I2C_M_IGNORE_NAK 0x1000 /* if I2C_FUNC_PROTOCOL_MANGLING */
#define I2C_M_NO_RD_ACK  0x0800 /* if I2C_FUNC_PROTOCOL_MANGLING */

这里I2C_FUNC_PROTOCOL_MANGLING 是什么意思?为什么定义这些东东?看了注释也不太理解。求解释!


3. I2C总线驱动代码分析

   s3c2440的总线驱动代码在i2c-s3c2410.c中。照例先从init看起。

  1. static int __init i2c_adap_s3c_init(void) 
  2. { 
  3.     return platform_driver_register(&s3c24xx_i2c_driver); 
  4. } 
static int __init i2c_adap_s3c_init(void) { return platform_driver_register(&s3c24xx_i2c_driver); }


在init中只是调用了平台驱动注册函数注册了一个i2c的平台驱动s3c24xx_i2c_driver。这个驱动是一个platform_driver的结构体变量。注意这里不是i2c_driver结构体,因为i2c_driver是对设备的驱动,而这里对控制器的驱动要使用platform_driver

  1. static struct platform_driver s3c24xx_i2c_driver = { 
  2.     .probe      = s3c24xx_i2c_probe, 
  3.     .remove     = s3c24xx_i2c_remove, 
  4.     .suspend_late   = s3c24xx_i2c_suspend_late, 
  5.     .resume     = s3c24xx_i2c_resume, 
  6.     .id_table   = s3c24xx_driver_ids, 
  7.     .driver     = { 
  8.         .owner  = THIS_MODULE, 
  9.         .name   = "s3c-i2c", 
  10.     }, 
  11. }; 
static struct platform_driver s3c24xx_i2c_driver = { .probe = s3c24xx_i2c_probe, .remove = s3c24xx_i2c_remove, .suspend_late = s3c24xx_i2c_suspend_late, .resume = s3c24xx_i2c_resume, .id_table = s3c24xx_driver_ids, .driver = { .owner = THIS_MODULE, .name = "s3c-i2c", }, };


同样的,重要的函数还是那几个:probe,remove,suspend_late,resume。再加上一个id_table和device_driver结构体变量。

下面逐个分析:

* probe函数

当调用platform_driver_register函数注册platform_driver结构体时,probe指针指向的s3c24xx_i2c_probe函数将会被调用。这部分详细解释参考本博客另一篇文章《S3C2410看门狗驱动分析》。细心的朋友可能会发现,在s3c24xx_i2c_driver中,驱动的名字是"s3c-i2c",而在板文件中可以看到,设备的名字是"s3c2410-i2c",这两个名字不一样,那驱动和设备是如何match的呢?答案就在于id_table。这个id_table包含了驱动所支持的设备ID表。在match的时候,判断这个表中的名字是不是和设备一致,一致则match成功。这也是为什么一个驱动可以同时match成功多个设备的原因。如果只是靠platform_driver-->driver中的名字来匹配的话,那么驱动和设备只能是一对一的关系了。

  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. }; 
static struct platform_device_id s3c24xx_driver_ids[] = { { .name = "s3c2410-i2c", .driver_data = TYPE_S3C2410, }, { .name = "s3c2440-i2c", .driver_data = TYPE_S3C2440, }, { }, };


扯远了,还是看看probe的代码吧~

  1. static int s3c24xx_i2c_probe(struct platform_device *pdev) 
  2. { 
  3.     struct s3c24xx_i2c *i2c; 
  4.     struct s3c2410_platform_i2c *pdata; 
  5.     struct resource *res; 
  6.     int ret; 
  7.  
  8.     pdata = pdev->dev.platform_data; 
  9.     if (!pdata) { 
  10.         dev_err(&pdev->dev, "no platform data/n"); 
  11.         return -EINVAL; 
  12.     } 
  13.         //给s3c24xx_i2c结构体申请空间  
  14.     i2c = kzalloc(sizeof(struct s3c24xx_i2c), GFP_KERNEL); 
  15.     if (!i2c) { 
  16.         dev_err(&pdev->dev, "no memory for state/n"); 
  17.         return -ENOMEM; 
  18.     } 
  19.         //填充s3c24xx_i2c结构体中各项,包括名称、所有者、算法、所属class等等  
  20.     strlcpy(i2c->adap.name, "s3c2410-i2c", sizeof(i2c->adap.name)); 
  21.     i2c->adap.owner   = THIS_MODULE; 
  22.     i2c->adap.algo    = &s3c24xx_i2c_algorithm; //这个下面会重点介绍  
  23.     i2c->adap.retries = 2; 
  24.     i2c->adap.class   = I2C_CLASS_HWMON | I2C_CLASS_SPD; 
  25.     i2c->tx_setup     = 50; 
  26.  
  27.     spin_lock_init(&i2c->lock); 
  28.     init_waitqueue_head(&i2c->wait); 
  29.  
  30.     /* find the clock and enable it */ 
  31.         // 找到i2c始终并且使能它  
  32.     i2c->dev = &pdev->dev; 
  33.     i2c->clk = clk_get(&pdev->dev, "i2c"); 
  34.     if (IS_ERR(i2c->clk)) { 
  35.         dev_err(&pdev->dev, "cannot get clock/n"); 
  36.         ret = -ENOENT; 
  37.         goto err_noclk; 
  38.     } 
  39.  
  40.     dev_dbg(&pdev->dev, "clock source %p/n", i2c->clk); 
  41.  
  42.     clk_enable(i2c->clk); 
  43.  
  44.     /* map the registers */ 
  45.         /*映射寄存器*/ 
  46.     res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 
  47.     if (res == NULL) { 
  48.         dev_err(&pdev->dev, "cannot find IO resource/n"); 
  49.         ret = -ENOENT; 
  50.         goto err_clk; 
  51.     } 
  52.  
  53.     i2c->ioarea = request_mem_region(res->start, resource_size(res), 
  54.                      pdev->name); 
  55.  
  56.     if (i2c->ioarea == NULL) { 
  57.         dev_err(&pdev->dev, "cannot request IO/n"); 
  58.         ret = -ENXIO; 
  59.         goto err_clk; 
  60.     } 
  61.  
  62.     i2c->regs = ioremap(res->start, resource_size(res)); 
  63.  
  64.     if (i2c->regs == NULL) { 
  65.         dev_err(&pdev->dev, "cannot map IO/n"); 
  66.         ret = -ENXIO; 
  67.         goto err_ioarea; 
  68.     } 
  69.  
  70.     dev_dbg(&pdev->dev, "registers %p (%p, %p)/n", 
  71.         i2c->regs, i2c->ioarea, res); 
  72.  
  73.     /* setup info block for the i2c core */ 
  74.  
  75.     i2c->adap.algo_data = i2c; 
  76.     i2c->adap.dev.parent = &pdev->dev; 
  77.  
  78.     /* initialise the i2c controller */ 
  79.         /*s3c24xx_i2c结构体变量i2c的必要的信息都填充完了以后,开始进行初始化*/ 
  80.     ret = s3c24xx_i2c_init(i2c); 
  81.     if (ret != 0) 
  82.         goto err_iomap; 
  83.  
  84.     /* find the IRQ for this unit (note, this relies on the init call to
  85.      * ensure no current IRQs pending
  86.      */ 
  87.         //接下来申请中断  
  88.     i2c->irq = ret = platform_get_irq(pdev, 0); 
  89.     if (ret <= 0) { 
  90.         dev_err(&pdev->dev, "cannot find IRQ/n"); 
  91.         goto err_iomap; 
  92.     } 
  93.  
  94.     ret = request_irq(i2c->irq, s3c24xx_i2c_irq, IRQF_DISABLED, 
  95.               dev_name(&pdev->dev), i2c); 
  96.  
  97.     if (ret != 0) { 
  98.         dev_err(&pdev->dev, "cannot claim IRQ %d/n", i2c->irq); 
  99.         goto err_iomap; 
  100.     } 
  101.          
  102.     ret = s3c24xx_i2c_register_cpufreq(i2c); 
  103.     if (ret < 0) { 
  104.         dev_err(&pdev->dev, "failed to register cpufreq notifier/n"); 
  105.         goto err_irq; 
  106.     } 
  107.  
  108.     /* Note, previous versions of the driver used i2c_add_adapter()
  109.      * to add the bus at any number. We now pass the bus number via
  110.      * the platform data, so if unset it will now default to always
  111.      * being bus 0.
  112.      */ 
  113.  
  114.     i2c->adap.nr = pdata->bus_num; 
  115.  
  116.         //看到了吧?下面调用了i2c-core中的i2c_add_adapter函数来添加一个i2c控制器  
  117.           //i2c_add_numbered_adapter和i2c_add_adapter的区别在于前者用来添加一个在CPU内  
  118.           //部集成的适配器,而后者用来添加一个CPU外部的适配器。显然这里应该用前者。  
  119.     ret = i2c_add_numbered_adapter(&i2c->adap); 
  120.     if (ret < 0) { 
  121.         dev_err(&pdev->dev, "failed to add bus to i2c core/n"); 
  122.         goto err_cpufreq; 
  123.     } 
  124.  
  125.     platform_set_drvdata(pdev, i2c); 
  126.  
  127.     dev_info(&pdev->dev, "%s: S3C I2C adapter/n", dev_name(&i2c->adap.dev)); 
  128.     return 0; 
  129.  
  130. err_cpufreq: 
  131.     s3c24xx_i2c_deregister_cpufreq(i2c); 
  132.  
  133. err_irq: 
  134.     free_irq(i2c->irq, i2c); 
  135.  
  136. err_iomap: 
  137.     iounmap(i2c->regs); 
  138.  
  139. err_ioarea: 
  140.     release_resource(i2c->ioarea); 
  141.     kfree(i2c->ioarea); 
  142.  
  143. err_clk: 
  144.     clk_disable(i2c->clk); 
  145.     clk_put(i2c->clk); 
  146.  
  147. err_noclk: 
  148.     kfree(i2c); 
  149.     return ret; 
  150. } 
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; if (!pdata) { dev_err(&pdev->dev, "no platform data/n"); return -EINVAL; }        //给s3c24xx_i2c结构体申请空间 i2c = kzalloc(sizeof(struct s3c24xx_i2c), GFP_KERNEL); if (!i2c) { dev_err(&pdev->dev, "no memory for state/n"); return -ENOMEM; }        //填充s3c24xx_i2c结构体中各项,包括名称、所有者、算法、所属class等等 strlcpy(i2c->adap.name, "s3c2410-i2c", sizeof(i2c->adap.name)); i2c->adap.owner   = THIS_MODULE; i2c->adap.algo    = &s3c24xx_i2c_algorithm; //这个下面会重点介绍 i2c->adap.retries = 2; i2c->adap.class   = I2C_CLASS_HWMON | I2C_CLASS_SPD; i2c->tx_setup     = 50; spin_lock_init(&i2c->lock); init_waitqueue_head(&i2c->wait); /* find the clock and enable it */        // 找到i2c始终并且使能它 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, 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 */        /*s3c24xx_i2c结构体变量i2c的必要的信息都填充完了以后,开始进行初始化*/ 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 */        //接下来申请中断 i2c->irq = ret = platform_get_irq(pdev, 0); if (ret <= 0) { dev_err(&pdev->dev, "cannot find IRQ/n"); goto err_iomap; } 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;        //看到了吧?下面调用了i2c-core中的i2c_add_adapter函数来添加一个i2c控制器          //i2c_add_numbered_adapter和i2c_add_adapter的区别在于前者用来添加一个在CPU内          //部集成的适配器,而后者用来添加一个CPU外部的适配器。显然这里应该用前者。 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)); 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; }



*remove函数

这是和probe相反的一个函数,在i2c_adap_s3c_exit时调用。主要功能是注销适配器,释放中断,释放内存区域,禁止始终等等。看到上边代码中的err_的各个部分了吧? remove是它们的汇总。


*suspend函数和resume函数

把这两个放一起说吧,挂起和恢复函数。挂起时保存状态并置标志位,恢复时重新初始化i2c适配器并置标志位。


Algorithm

哎呀我去,终于到这了。憋得我难受啊。这里要重点介绍一下,不仅要知其然,还要知其所以然,这样我们以后自己写驱动的时候就有把握了。

  1. static const struct i2c_algorithm s3c24xx_i2c_algorithm = { 
  2.     .master_xfer        = s3c24xx_i2c_xfer, 
  3.     .functionality      = s3c24xx_i2c_func, 
  4. }; 
static const struct i2c_algorithm s3c24xx_i2c_algorithm = { .master_xfer = s3c24xx_i2c_xfer, .functionality = s3c24xx_i2c_func, };


这里实现的就是这个s3c24xx_i2c_xfer。这个是控制器能不能动作的关键,缺了这个,控制器就是废铜烂铁。

  1. static int s3c24xx_i2c_xfer(struct i2c_adapter *adap, 
  2.             struct i2c_msg *msgs, int num) 
  3. { 
  4.     struct s3c24xx_i2c *i2c = (struct s3c24xx_i2c *)adap->algo_data; 
  5.     int retry; 
  6.     int ret; 
  7.  
  8.     for (retry = 0; retry < adap->retries; retry++) { 
  9.  
  10.         ret = s3c24xx_i2c_doxfer(i2c, msgs, num); 
  11.  
  12.         if (ret != -EAGAIN) 
  13.             return ret; 
  14.  
  15.         dev_dbg(i2c->dev, "Retrying transmission (%d)/n", retry); 
  16.  
  17.         udelay(100); 
  18.     } 
  19.  
  20.     return -EREMOTEIO; 
  21. } 
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(),源码清单如下,

  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; 
  20.     i2c->msg_num = num; 
  21.     i2c->msg_ptr = 0; 
  22.     i2c->msg_idx = 0; 
  23.     i2c->state   = STATE_START; 
  24.  
  25.     s3c24xx_i2c_enable_irq(i2c); 
  26.     s3c24xx_i2c_message_start(i2c, msgs); 
  27.     spin_unlock_irq(&i2c->lock); 
  28.  
  29.     timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5); 
  30.  
  31.     ret = i2c->msg_idx; 
  32.  
  33.     /* having these next two as dev_err() makes life very
  34.      * noisy when doing an i2cdetect */ 
  35.  
  36.     if (timeout == 0) 
  37.         dev_dbg(i2c->dev, "timeout/n"); 
  38.     else if (ret != num) 
  39.         dev_dbg(i2c->dev, "incomplete xfer (%d)/n", ret); 
  40.  
  41.     /* ensure the stop has been through the bus */ 
  42.  
  43.     msleep(1); 
  44.  
  45. out: 
  46.     return ret; 
  47. } 
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); 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; }


上面代码可以分成几个部分来看:

  * s3c24xx_i2c_set_master() 这个函数每隔1ms查看一次i2c总线状态,timeout是400ms,如果在这期间总线状态不忙,则返回零。否则返回-ETIMEDOUT

  * 将要发送的消息和其他信息付给i2c->msg和其他变量,并将状态设置为STATE_START

  * s3c24xx_i2c_enable_irq() 使能中断

  * s3c24xx_i2c_message_start() 重中之重啊。在看代码之前先来看看2440的datasheet上是怎么说的吧。

代码清单如下:

  1. static void s3c24xx_i2c_message_start(struct s3c24xx_i2c *i2c, 
  2.                       struct i2c_msg *msg) 
  3. { 
  4.     unsigned int addr = (msg->addr & 0x7f) << 1; 
  5.     unsigned long stat; 
  6.     unsigned long iiccon; 
  7.  
  8.     stat = 0; 
  9.     stat |=  S3C2410_IICSTAT_TXRXEN; 
  10.  
  11.     if (msg->flags & I2C_M_RD) {//如果是read data, from slave to master     stat |= S3C2410_IICSTAT_MASTER_RX;  
  12.         addr |= 1; 
  13.     } else 
  14.         stat |= S3C2410_IICSTAT_MASTER_TX; 
  15.  
  16.     if (msg->flags & I2C_M_REV_DIR_ADDR) 
  17.         addr ^= 1; 
  18.  
  19.     /* todo - check for wether ack wanted or not */ 
  20.     s3c24xx_i2c_enable_ack(i2c); 
  21.  
  22.     iiccon = readl(i2c->regs + S3C2410_IICCON); 
  23.     writel(stat, i2c->regs + S3C2410_IICSTAT); 
  24.  
  25.     dev_dbg(i2c->dev, "START: %08lx to IICSTAT, %02x to DS/n", stat, addr); 
  26.     writeb(addr, i2c->regs + S3C2410_IICDS); 
  27.  
  28.     /* delay here to ensure the data byte has gotten onto the bus
  29.      * before the transaction is started */ 
  30.  
  31.     ndelay(i2c->tx_setup); 
  32.  
  33.     dev_dbg(i2c->dev, "iiccon, %08lx/n", iiccon); 
  34.     writel(iiccon, i2c->regs + S3C2410_IICCON); 
  35.  
  36.     stat |= S3C2410_IICSTAT_START; 
  37.     writel(stat, i2c->regs + S3C2410_IICSTAT); 
  38. } 
http://blog.csdn.net/ypoflyer/article/details/6382097
阅读(2492) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~