当前位置:   article > 正文

linux I2C二_msgs[1].buf = &data;

msgs[1].buf = &data;

一、linux I2C体系结构


上图中分为用户空间部分、内核空间、硬件。

应用程序是要求实现的应用功能,通过调用系统调用(i2c-dev.c虚拟的I2C设备驱动),内核进行交互。

硬件包括实际的I2C总线和挂接在I2C总线上的设备

内核是描述设备与总线是如何通信的。

举个例子:

假设上层要给设备某地址写值,调用如下

应用层序调用系统函数write>(client)i2cdev_write->(i2c-core.c)i2c_master_send->(适配器)(adap->algo->master_xfer)->(硬件)使I2C产生特定的信号与设备交互


内核的实现:

内核分三大块 1、core   2、适配器管理+总线驱动   3、设备管理+驱动

1、core是核心模块,将适配端与外设相隔离。提供一组不依赖具体硬件的接口函数,实现各种适配器、总线驱动、设备驱动等相同的部分。一种外设可以在不同适配器上使用,一个适配器可以接不同的外设。

具体表现:

1、int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)//接收发送函数

2、int i2c_add_numbered_adapter(struct i2c_adapter *adap)//注册适配器

3、int i2c_del_adapter(struct i2c_adapter *adap)//删除适配器

。。。


2、适配器可以简单理解为cpu上的I2C接口,总线驱动是其工作软件的实现。

3、外设驱动即描述该设备怎么工作。


理解几个结构体:

  1. struct i2c_adapter {
  2. struct module *owner;
  3. unsigned int id;
  4. unsigned int class; /* classes to allow probing for */
  5. const struct i2c_algorithm *algo; /* the algorithm to access the bus */
  6. void *algo_data; //指向某变量或开辟的结构体
  7. /* data fields that are valid for all devices */
  8. struct rt_mutex bus_lock;
  9. int timeout; /* in jiffies */
  10. int retries; //重做次数
  11. struct device dev; /* the adapter device */
  12. int nr; //编号
  13. char name[48]; //名称
  14. struct completion dev_released;
  15. struct mutex userspace_clients_lock;
  16. struct list_head userspace_clients;
  17. };
  18. struct i2c_algorithm {//通信方法,产生特定的信号
  19. /* master_xfer should return the number of messages successfully
  20. processed, or a negative value on error */
  21. int (*master_xfer)(struct i2c_adapter *adap, struct i2c_msg *msgs,
  22. int num);//发送接收函数
  23. 。。。
  24. /* To determine what the adapter supports */
  25. u32 (*functionality) (struct i2c_adapter *);
  26. 。。。
  27. }
  28. struct i2c_driver{
  29. unsigned int class;
  30. /* Notifies the driver that a new bus has appeared or is about to be
  31. * removed. You should avoid using this if you can, it will probably
  32. * be removed in a near future.
  33. */
  34. int (*attach_adapter)(struct i2c_adapter *);//attach to a i2c adapter
  35. int (*detach_adapter)(struct i2c_adapter *);//deattach an adapter which attached
  36. /* Standard driver model interfaces */
  37. int (*probe)(struct i2c_client *, const struct i2c_device_id *);
  38. int (*remove)(struct i2c_client *);
  39. /* driver model interfaces that don't relate to enumeration */
  40. void (*shutdown)(struct i2c_client *);
  41. int (*suspend)(struct i2c_client *, pm_message_t mesg);
  42. int (*resume)(struct i2c_client *);
  43. /* Alert callback, for example for the SMBus alert protocol.
  44. * The format and meaning of the data value depends on the protocol.
  45. * For the SMBus alert protocol, there is a single bit of data passed
  46. * as the alert response's low bit ("event flag").
  47. */
  48. void (*alert)(struct i2c_client *, unsigned int data);
  49. /* a ioctl like command that can be used to perform specific functions
  50. * with the device.
  51. */
  52. int (*command)(struct i2c_client *client, unsigned int cmd, void *arg);
  53. struct device_driver driver;
  54. const struct i2c_device_id *id_table;
  55. /* Device detection callback for automatic device creation */
  56. int (*detect)(struct i2c_client *, struct i2c_board_info *);
  57. const unsigned short *address_list;
  58. struct list_head clients;
  59. };
  60. struct i2c_client {//外设描述
  61. unsigned short flags; /* div., see below */
  62. unsigned short addr; /* chip address - NOTE: 7bit */
  63. /* addresses are stored in the */
  64. /* _LOWER_ 7 bits */
  65. char name[I2C_NAME_SIZE];
  66. struct i2c_adapter *adapter; /* the adapter we sit on */
  67. struct i2c_driver *driver; /* and our access routines */
  68. struct device dev; /* the device structure */
  69. int irq; /* irq issued by device */
  70. struct list_head detected;
  71. };
  72. /*另外再了解一个重要结构体,*/
  73. struct i2c_msg { //通信的基本载体
  74. __u16 addr; /* slave address-器件地址*/
  75. __u16 flags;
  76. #define I2C_M_TEN 0x0010 /* this is a ten bit chip address */
  77. #define I2C_M_RD 0x0001 /* read data, from slave to master */
  78. #define I2C_M_NOSTART 0x4000 /* if I2C_FUNC_PROTOCOL_MANGLING */
  79. #define I2C_M_REV_DIR_ADDR 0x2000 /* if I2C_FUNC_PROTOCOL_MANGLING */
  80. #define I2C_M_IGNORE_NAK 0x1000 /* if I2C_FUNC_PROTOCOL_MANGLING */
  81. #define I2C_M_NO_RD_ACK 0x0800 /* if I2C_FUNC_PROTOCOL_MANGLING */
  82. #define I2C_M_RECV_LEN 0x0400 /* length will be first received byte */
  83. __u16 len; /* msg length--描述buf长度*/
  84. __u8 *buf; /* pointer to msg data--指向数据缓存*/
  85. };

 
 
 
 

  1. 现在说明如何构造i2c_msg,现以write为例说明。
  2. msgs[0].addr = address;
  3. msgs[0].flags = 0; /* write */
  4. msgs[0].buf = (unsigned char *)data;//包含的数据是上图2段,本例中buf包含两个值:register address+data
  5. msgs[0].len = len; //本例中len=2
  6. msgs[0].scl_rate = I2C_RATE;
  7. 因此,
  8. 1、系统开始时先将7位的地址左移1位,并判断msg为WRITE,所以第8位位0.
  9. 2、将地址传输出去,进入中断。在中断函数里判断传输是否正确(比如是否收到ACK信号等)
  10. 3、再依次将buf中的值发送,并用len判断是否传输完毕
  11. 现在说明如何构造i2c_msg,现以read为例说明。
  12. 分两个msg传输:
  13.   msg0对应上图第一段,是写msg。
  14.   msg1对应上图第二段,是读msg。
  15. msgs[0].addr = address;//启动传输时发送
  16. msgs[0].flags = 0; /* write */
  17. msgs[0].buf = reg;//reg表示片内地址
  18. msgs[0].len = 1;//发送长度
  19. msgs[0].scl_rate = SENSOR_I2C_RATE;
  20. msgs[1].addr = address;//重新启动后再发送(见上图段2,见下面分析)。
  21. msgs[1].flags = I2C_M_RD;//上图段2,第一字节第8位。
  22. msgs[1].buf = data;//接收缓存
  23. msgs[1].len = len;//接受长度
  24. msgs[1].scl_rate = SENSOR_I2C_RATE




按部分讲解

1、适配器

dev-i2c0.c

  1. struct platform_device s3c_device_i2c0 = {
  2. .name = "s3c2410-i2c",
  3. #ifdef CONFIG_S3C_DEV_I2C1
  4. .id = 0,
  5. #else
  6. .id = -1,
  7. #endif
  8. .num_resources = ARRAY_SIZE(s3c_i2c_resource),
  9. .resource = s3c_i2c_resource,
  10. };
  11. static struct s3c2410_platform_i2c default_i2c_data0 __initdata = {
  12. .flags = 0,
  13. .slave_addr = 0x10,
  14. .frequency = 100*1000,
  15. .sda_delay = 100,
  16. };
  17. void __init s3c_i2c0_set_platdata(struct s3c2410_platform_i2c *pd)
  18. {
  19. struct s3c2410_platform_i2c *npd;
  20. if (!pd)
  21. pd = &default_i2c_data0;
  22. npd = kmemdup(pd, sizeof(struct s3c2410_platform_i2c), GFP_KERNEL);
  23. if (!npd)
  24. printk(KERN_ERR "%s: no memory for platform data\n", __func__);
  25. else if (!npd->cfg_gpio)
  26. npd->cfg_gpio = s3c_i2c0_cfg_gpio;//set GPIO as I2C func
  27. s3c_device_i2c0.dev.platform_data = npd;}//作私有变量


mach-mini2440.c //描述板子上的硬件设备信息

  1. static struct i2c_board_info mini2440_i2c_devs[] __initdata = {
  2. {
  3. I2C_BOARD_INFO("24c08", 0x50), //type=24c08--device name
  4. .platform_data = &at24c08, //addr=0x50--device addr
  5. },
  6. };
  7. i2c_register_board_info(0, mini2440_i2c_devs,
  8. ARRAY_SIZE(mini2440_i2c_devs));//添加i2c外设
  9. 。。。
  10. platform_add_devices(&s3c_device_i2c0);
  11. 。。。


 

i2c-s3c2410.c

 

  1. /* device driver for platform bus bits */
  2. static struct platform_device_id s3c24xx_driver_ids[] = {
  3. {
  4. .name = "s3c2410-i2c",
  5. .driver_data = TYPE_S3C2410,
  6. }, {
  7. .name = "s3c2440-i2c",
  8. .driver_data = TYPE_S3C2440,
  9. }, { },
  10. };
  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. };








  1. <pre name="code" class="cpp">/*platform_bus_type的match函数*/
  2. static int platform_match(struct device *dev, struct device_driver *drv)
  3. {
  4. struct platform_device *pdev = to_platform_device(dev);
  5. struct platform_driver *pdrv = to_platform_driver(drv);
  6. /* match against the id table first */
  7. if (pdrv->id_table)
  8. return platform_match_id(pdrv->id_table, pdev) != NULL;
  9. /* fall-back to driver name match */
  10. return (strcmp(pdev->name, drv->name) == 0);
  11. }
  12. static const struct platform_device_id *platform_match_id(
  13. struct platform_device_id *id,
  14. struct platform_device *pdev)
  15. {
  16. while (id->name[0]) {
  17. /*比较pdev设备名和驱动支持列表*/
  18. if (strcmp(pdev->name, id->name) == 0) {
  19. pdev->id_entry = id;
  20. return id;
  21. }
  22. id++;
  23. }
  24. return NULL;
  25. }
  26. 由上面可以看出,设备名要在驱动支持列表里头才可以配对
  27. /* s3c24xx_i2c_probe
  28.  *
  29.  * called by the bus driver when a suitable device is found
  30. */
  31. static int s3c24xx_i2c_probe(struct platform_device *pdev)
  32. {
  33.     struct s3c24xx_i2c *i2c;
  34.     struct s3c2410_platform_i2c *pdata;
  35.     struct resource *res;
  36.     int ret;
  37.   /*取得私有变量--有时是获得指针,有时是获得变量,这里是获得已经初始化好的变量*/
  38.     pdata = pdev->dev.platform_data;
  39.     if (!pdata) {
  40.         dev_err(&pdev->dev, "no platform data\n");
  41.         return -EINVAL;
  42.     }
  43.     i2c = kzalloc(sizeof(struct s3c24xx_i2c), GFP_KERNEL);
  44.     if (!i2c) {
  45.         dev_err(&pdev->dev, "no memory for state\n");
  46.         return -ENOMEM;
  47.     }
  48.   /*初始化adapter*/ 
  49.     strlcpy(i2c->adap.name, "s3c2410-i2c", sizeof(i2c->adap.name));
  50.     i2c->adap.owner   = THIS_MODULE;
  51.     i2c->adap.algo    = &s3c24xx_i2c_algorithm;//通信方法,当有数据要传输时都会调用该函数
  52.     i2c->adap.retries = 2;
  53.     i2c->adap.class   = I2C_CLASS_HWMON | I2C_CLASS_SPD;
  54.     i2c->tx_setup     = 50;
  55.     spin_lock_init(&i2c->lock);
  56.     init_waitqueue_head(&i2c->wait);
  57.     /* find the clock and enable it */
  58.     i2c->dev = &pdev->dev;
  59.     i2c->clk = clk_get(&pdev->dev, "i2c");
  60.     if (IS_ERR(i2c->clk)) {
  61.         dev_err(&pdev->dev, "cannot get clock\n");
  62.         ret = -ENOENT;
  63.         goto err_noclk;
  64.     }
  65.     dev_dbg(&pdev->dev, "clock source %p\n", i2c->clk);
  66.     clk_enable(i2c->clk);
  67.     /* map the registers */
  68.     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  69.     if (res == NULL) {
  70.         dev_err(&pdev->dev, "cannot find IO resource\n");
  71.         ret = -ENOENT;
  72.         goto err_clk;
  73.     }
  74.     i2c->ioarea = request_mem_region(res->start, resource_size(res),
  75.                      pdev->name);
  76.     if (i2c->ioarea == NULL) {
  77.         dev_err(&pdev->dev, "cannot request IO\n");
  78.         ret = -ENXIO;
  79.         goto err_clk;
  80.     }
  81.     i2c->regs = ioremap(res->start, resource_size(res));
  82.     if (i2c->regs == NULL) {
  83.         dev_err(&pdev->dev, "cannot map IO\n");
  84.         ret = -ENXIO;
  85.         goto err_ioarea;
  86.     }
  87.     dev_dbg(&pdev->dev, "registers %p (%p, %p)\n",
  88.         i2c->regs, i2c->ioarea, res);
  89.     /* setup info block for the i2c core */
  90.     i2c->adap.algo_data = i2c;
  91.     i2c->adap.dev.parent = &pdev->dev;
  92.     /* initialise the i2c controller */
  93.     ret = s3c24xx_i2c_init(i2c);//初始化i2c频率
  94.     if (ret != 0)
  95.         goto err_iomap;
  96.     /* find the IRQ for this unit (note, this relies on the init call to
  97.      * ensure no current IRQs pending
  98.      */
  99.     i2c->irq = ret = platform_get_irq(pdev, 0);
  100.     if (ret <= 0) {
  101.         dev_err(&pdev->dev, "cannot find IRQ\n");
  102.         goto err_iomap;
  103.     }
  104. /*注册中断,每当发送或者接受一个完整的字节时就会进入中断*/
  105.     ret = request_irq(i2c->irq, s3c24xx_i2c_irq, IRQF_DISABLED,
  106.               dev_name(&pdev->dev), i2c);
  107.     if (ret != 0) {
  108.         dev_err(&pdev->dev, "cannot claim IRQ %d\n", i2c->irq);
  109.         goto err_iomap;
  110.     }
  111.     /* Note, previous versions of the driver used i2c_add_adapter()
  112.      * to add the bus at any number. We now pass the bus number via
  113.      * the platform data, so if unset it will now default to always
  114.      * being bus 0.
  115.      */
  116.     i2c->adap.nr = pdata->bus_num;
  117.   /*向系统添加i2c适配器*/ 
  118.     ret = i2c_add_numbered_adapter(&i2c->adap);
  119.     if (ret < 0) {
  120.         dev_err(&pdev->dev, "failed to add bus to i2c core\n");
  121.         goto err_cpufreq;
  122.     }
  123.     platform_set_drvdata(pdev, i2c);
  124.     dev_info(&pdev->dev, "%s: S3C I2C adapter\n", dev_name(&i2c->adap.dev));
  125.     return 0;
  126.  err_cpufreq:
  127.     s3c24xx_i2c_deregister_cpufreq(i2c);
  128.  err_irq:
  129.     free_irq(i2c->irq, i2c);
  130.  err_iomap:
  131.     iounmap(i2c->regs);
  132.  err_ioarea:
  133.     release_resource(i2c->ioarea);
  134.     kfree(i2c->ioarea);
  135.  err_clk:
  136.     clk_disable(i2c->clk);
  137.     clk_put(i2c->clk);
  138.  err_noclk:
  139.     kfree(i2c);
  140.     return ret;
  141. }
  142. /* i2c bus registration info */
  143. static const struct i2c_algorithm s3c24xx_i2c_algorithm = {
  144.     .master_xfer        = s3c24xx_i2c_xfer,//通信方法
  145.     .functionality        = s3c24xx_i2c_func,
  146. };
  147. /* s3c24xx_i2c_xfer
  148.  *
  149.  * first port of call from the i2c bus code when an message needs
  150.  * transferring across the i2c bus.
  151. */
  152. static int s3c24xx_i2c_xfer(struct i2c_adapter *adap,
  153.             struct i2c_msg *msgs, int num)
  154. {
  155.     struct s3c24xx_i2c *i2c = (struct s3c24xx_i2c *)adap->algo_data;
  156.     int retry;
  157.     int ret;
  158.     for (retry = 0; retry < adap->retries; retry++) {
  159.         ret = s3c24xx_i2c_doxfer(i2c, msgs, num);//继续往下调用
  160.         if (ret != -EAGAIN)
  161.             return ret;
  162.         dev_dbg(i2c->dev, "Retrying transmission (%d)\n", retry);
  163.         udelay(100);
  164.     }
  165.     return -EREMOTEIO;
  166. }
  167. /* s3c24xx_i2c_doxfer
  168.  *
  169.  * this starts an i2c transfer
  170. */
  171. static int s3c24xx_i2c_doxfer(struct s3c24xx_i2c *i2c,
  172.                   struct i2c_msg *msgs, int num)
  173. {
  174.     unsigned long timeout;
  175.     unsigned long iicstat;
  176.     int ret;
  177.     if (i2c->suspended)
  178.         return -EIO;
  179.     ret = s3c24xx_i2c_set_master(i2c);
  180.     if (ret != 0) {
  181.         dev_err(i2c->dev, "cannot get bus (error %d)\n", ret);
  182.         ret = -EAGAIN;
  183.         goto out;
  184.     }
  185.     spin_lock_irq(&i2c->lock);
  186.   //获取待传输的msg 
  187.     i2c->msg     = msgs;//
  188.     i2c->msg_num = num;
  189.     i2c->msg_ptr = 0;
  190.     i2c->msg_idx = 0;
  191.     i2c->state   = STATE_START;
  192.     s3c24xx_i2c_enable_irq(i2c);
  193.     s3c24xx_i2c_message_start(i2c, msgs);//启动发送,发送address。当address传输完后,进入中断,在中断里判断是否传输错误
  194.     spin_unlock_irq(&i2c->lock);
  195.     timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 1);//等待超时或者传输完成。
  196.     ret = i2c->msg_idx;
  197.     /* having these next two as dev_err() makes life very
  198.      * noisy when doing an i2cdetect */
  199.     if (timeout == 0) {
  200.         dev_dbg(i2c->dev, "timeout\n");
  201.         iicstat = readl(i2c->regs + S3C2410_IICSTAT);
  202.         /* stop the transfer */
  203.         iicstat &= ~(S3C2410_IICSTAT_START | S3C2410_IICSTAT_TXRXEN);
  204.         writel(iicstat, i2c->regs + S3C2410_IICSTAT);
  205.         i2c->state = STATE_STOP;
  206.         s3c24xx_i2c_master_complete(i2c, ret);
  207.         s3c24xx_i2c_disable_irq(i2c);
  208.         goto out;
  209.     }
  210.     else if (ret != num)
  211.         dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret);
  212.     /* ensure the stop has been through the bus */
  213.     udelay(50);
  214.  out:
  215.     return ret;
  216. }
  217. /* s3c24xx_i2c_message_start
  218.  *
  219.  * put the start of a message onto the bus
  220. */
  221. static void s3c24xx_i2c_message_start(struct s3c24xx_i2c *i2c,
  222.                       struct i2c_msg *msg)
  223. {
  224.     unsigned int addr = (msg->addr & 0x7f) << 1;//获取address并左移一位,第8位表示读写
  225.     unsigned long stat;
  226.     unsigned long iiccon;
  227.     stat = 0;
  228.     stat |=  S3C2410_IICSTAT_TXRXEN;
  229.     if (msg->flags & I2C_M_RD) {
  230.         stat |= S3C2410_IICSTAT_MASTER_RX;
  231.         addr |= 1;//赋值第8位
  232.     } else
  233.         stat |= S3C2410_IICSTAT_MASTER_TX;
  234.     if (msg->flags & I2C_M_REV_DIR_ADDR)
  235.         addr ^= 1;
  236.     /* todo - check for wether ack wanted or not */
  237.     s3c24xx_i2c_enable_ack(i2c);
  238.     iiccon = readl(i2c->regs + S3C2410_IICCON);
  239.     writel(stat, i2c->regs + S3C2410_IICSTAT);//启动传输
  240.     dev_dbg(i2c->dev, "START: %08lx to IICSTAT, %02x to DS\n", stat, addr);
  241.     writeb(addr, i2c->regs + S3C2410_IICDS);//将地址写到IICDS,通过SDA发送
  242.     /* delay here to ensure the data byte has gotten onto the bus
  243.      * before the transaction is started */
  244.     ndelay(i2c->tx_setup);
  245.     //dev_dbg(i2c->dev, "iiccon, %08lx\n", iiccon);
  246.     //writel(iiccon, i2c->regs + S3C2410_IICCON);
  247.     stat |= S3C2410_IICSTAT_START;
  248.     writel(stat, i2c->regs + S3C2410_IICSTAT);
  249. }
  250. /*发送完一字节进中断*/
  251. static irqreturn_t s3c24xx_i2c_irq(int irqno, void *dev_id)
  252. {
  253.     struct s3c24xx_i2c *i2c = dev_id;
  254.     unsigned long status;
  255.     unsigned long tmp;
  256.     status = readl(i2c->regs + S3C2410_IICSTAT);//以下是判断是否传输有错误
  257.     if (status & S3C2410_IICSTAT_ARBITR) {
  258.         /* deal with arbitration loss */
  259.         dev_err(i2c->dev, "deal with arbitration loss\n");
  260.     }
  261.     if (i2c->state == STATE_IDLE) {
  262.         dev_dbg(i2c->dev, "IRQ: error i2c->state == IDLE\n");
  263.         tmp = readl(i2c->regs + S3C2410_IICCON);
  264.         tmp &= ~S3C2410_IICCON_IRQPEND;
  265.         writel(tmp, i2c->regs +  S3C2410_IICCON);
  266.         goto out;
  267.     }
  268.     /* pretty much this leaves us with the fact that we've
  269.      * transmitted or received whatever byte we last sent */
  270.     i2c_s3c_irq_nextbyte(i2c, status);//传送下一个字节
  271.  out:
  272.     return IRQ_HANDLED;
  273. }
  274. static int i2c_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat)
  275. {
  276.     unsigned long tmp;
  277.     unsigned char byte;
  278.     int ret = 0;
  279.     switch (i2c->state) {
  280.     case STATE_IDLE:
  281.         dev_err(i2c->dev, "%s: called in STATE_IDLE\n", __func__);
  282.         goto out;
  283.         break;
  284.     case STATE_STOP:
  285.         dev_err(i2c->dev, "%s: called in STATE_STOP\n", __func__);
  286.         s3c24xx_i2c_disable_irq(i2c);
  287.         goto out_ack;
  288.     case STATE_START://标志为START有两种情况:1、刚开始,2、READ的重启时候
  289.         /* last thing we did was send a start condition on the
  290.          * bus, or started a new i2c message
  291.          */
  292.         if (iicstat & S3C2410_IICSTAT_LASTBIT &&
  293.             !(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
  294.             /* ack was not received... */
  295.             dev_dbg(i2c->dev, "ack was not received\n");
  296.             s3c24xx_i2c_stop(i2c, -ENXIO);
  297.             goto out_ack;
  298.         }
  299.         if (i2c->msg->flags & I2C_M_RD)
  300.             i2c->state = STATE_READ;//READ的重启时候        
  301. else
  302.             i2c->state = STATE_WRITE;//刚开始传输
  303.         /* terminate the transfer if there is nothing to do
  304.          * as this is used by the i2c probe to find devices. */
  305.         if (is_lastmsg(i2c) && i2c->msg->len == 0) {
  306.             s3c24xx_i2c_stop(i2c, 0);
  307.             goto out_ack;
  308.         }
  309.         if (i2c->state == STATE_READ)//如果是READ,先要未读准备一下。如只读一个字节,如果是则要设置读完后主机不发送ACK等
  310.             goto prepare_read;
  311.         /* fall through to the write state, as we will need to
  312.          * send a byte as well */
  313.     case STATE_WRITE:
  314.         /* we are writing data to the device... check for the
  315.          * end of the message, and if so, work out what to do
  316.          */
  317.         if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
  318.             if (iicstat & S3C2410_IICSTAT_LASTBIT) {
  319.                 dev_dbg(i2c->dev, "WRITE: No Ack\n");
  320.                 s3c24xx_i2c_stop(i2c, -ECONNREFUSED);
  321.                 goto out_ack;
  322.             }
  323.         }
  324.  retry_write:
  325.         if (!is_msgend(i2c)) {//msg是否传完数据
  326.             byte = i2c->msg->buf[i2c->msg_ptr++];//指针自加1
  327.             writeb(byte, i2c->regs + S3C2410_IICDS);//未传完数据,则要将数据写到CDS,通过SDA发送
  328.             /* delay after writing the byte to allow the
  329.              * data setup time on the bus, as writing the
  330.              * data to the register causes the first bit
  331.              * to appear on SDA, and SCL will change as
  332.              * soon as the interrupt is acknowledged */
  333.             ndelay(i2c->tx_setup);
  334.         } else if (!is_lastmsg(i2c)) {//如果msg数据传完,且后面还有msg,则要跳到下一个msg
  335.             /* we need to go to the next i2c message */
  336.             dev_dbg(i2c->dev, "WRITE: Next Message\n");
  337.             i2c->msg_ptr = 0;
  338.             i2c->msg_idx++;
  339.             i2c->msg++;//跳到下一个msg
  340.             /* check to see if we need to do another message */
  341.             if (i2c->msg->flags & I2C_M_NOSTART) {
  342.                 if (i2c->msg->flags & I2C_M_RD) {
  343.                     /* cannot do this, the controller
  344.                      * forces us to send a new START
  345.                      * when we change direction */
  346.                     s3c24xx_i2c_stop(i2c, -EINVAL);
  347.                 }
  348.                 goto retry_write;
  349.             } else {
  350.                 /* send the new start */
  351.                 s3c24xx_i2c_message_start(i2c, i2c->msg);//这里是在case WRITE里,该msg数据传完(片内地址传完),且后面还有msg待传输,说明是主机要向从机读数据。因此重启I2C
  352.                 i2c->state = STATE_START;//设置I2C状态
  353.             }
  354.         } else {
  355.             /* send stop */
  356.             s3c24xx_i2c_stop(i2c, 0);
  357.         }
  358.         break;
  359.     case STATE_READ:
  360.         /* we have a byte of data in the data register, do
  361.          * something with it, and then work out wether we are
  362.          * going to do any more read/write
  363.          */
  364.         byte = readb(i2c->regs + S3C2410_IICDS);//(接受一个字节会进中断,且将值放在CDS里),获取数据
  365.         i2c->msg->buf[i2c->msg_ptr++] = byte;//ptr指向下一个字节
  366.  prepare_read:
  367.         if (is_msglast(i2c)) {//仅剩最后一个字节未读取
  368.             /* last byte of buffer */
  369.             if (is_lastmsg(i2c))
  370.                 s3c24xx_i2c_disable_ack(i2c);//到时不发送ACK
  371.         }
  372.      else if (is_msgend(i2c)) //数据接受完毕
  373.      {
  374.             /* ok, we've read the entire buffer, see if there
  375.              * is anything else we need to do */
  376.             if (is_lastmsg(i2c)) {//最后一个msg ?
  377.                 /* last message, send stop and complete */
  378.                 dev_dbg(i2c->dev, "READ: Send Stop\n");
  379.                 s3c24xx_i2c_stop(i2c, 0);//说明已经全部接受完毕,主机给从机发送停止信号
  380.             } else {
  381.                 /* go to the next transfer */
  382.                 dev_dbg(i2c->dev, "READ: Next Transfer\n");
  383.                 i2c->msg_ptr = 0;
  384.                 i2c->msg_idx++;
  385.                 i2c->msg++;
  386.             }
  387.         }
  388.         break;
  389.     }
  390.     /* acknowlegde the IRQ and get back on with the work */
  391. //清中断,重新传输
  392.  out_ack:
  393.     tmp = readl(i2c->regs + S3C2410_IICCON);
  394.     tmp &= ~S3C2410_IICCON_IRQPEND;
  395.     writel(tmp, i2c->regs + S3C2410_IICCON);
  396.  out:
  397.     return ret;
  398. }
 
 
 
 
 2、core 

//除了添加adapter外还初始化并注册了挂接在这个adapter上的设备

  1. int i2c_add_numbered_adapter(struct i2c_adapter *adap)
  2. {//we have already set the adap->nr
  3. int id;
  4. int status;
  5. if (adap->nr & ~MAX_ID_MASK)
  6. return -EINVAL;
  7. retry:
  8. if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
  9. return -ENOMEM;
  10. mutex_lock(&core_lock);
  11. /* "above" here means "above or equal to", sigh;
  12. * we need the "equal to" result to force the result
  13. */
  14. status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);//建立id与adap的关系
  15. if (status == 0 && id != adap->nr) {
  16. status = -EBUSY;
  17. idr_remove(&i2c_adapter_idr, id);
  18. }
  19. mutex_unlock(&core_lock);
  20. if (status == -EAGAIN)
  21. goto retry;
  22. if (status == 0)
  23. <span style="color:#ff0000;">status = i2c_register_adapter(adap);</span>
  24. return status;
  25. }
  26. static int i2c_register_adapter(struct i2c_adapter *adap)
  27. {
  28. int res = 0;
  29. /* Can't register until after driver model init */
  30. if (unlikely(WARN_ON(!i2c_bus_type.p))) {
  31. res = -EAGAIN;
  32. goto out_list;
  33. }
  34. rt_mutex_init(&adap->bus_lock);
  35. mutex_init(&adap->userspace_clients_lock);
  36. INIT_LIST_HEAD(&adap->userspace_clients);
  37. /* Set default timeout to 1 second if not already set */
  38. if (adap->timeout == 0)
  39. adap->timeout = HZ;
  40. //adap->dev.name=i2c0
  41. dev_set_name(&adap->dev, "i2c-%d", adap->nr);//i2c-0
  42. adap->dev.bus = &i2c_bus_type;
  43. adap->dev.type = &i2c_adapter_type;
  44. res = device_register(&adap->dev);//register adapter dev
  45. if (res)
  46. goto out_list;
  47. dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
  48. #ifdef CONFIG_I2C_COMPAT
  49. res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
  50. adap->dev.parent);
  51. if (res)
  52. dev_warn(&adap->dev,
  53. "Failed to create compatibility class link\n");
  54. #endif
  55. /* create pre-declared device nodes */
  56. if (adap->nr < __i2c_first_dynamic_bus_num) //will call this
  57. <span style="color:#ff0000;"><strong>i2c_scan_static_board_info(adap);</strong></span>
  58. /* Notify drivers */
  59. mutex_lock(&core_lock); //will not do this part
  60. bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
  61. mutex_unlock(&core_lock);
  62. return 0;
  63. out_list:
  64. mutex_lock(&core_lock);
  65. idr_remove(&i2c_adapter_idr, adap->nr);
  66. mutex_unlock(&core_lock);
  67. return res;
  68. }
  69. static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
  70. {
  71. struct i2c_devinfo *devinfo;
  72. down_read(&__i2c_board_lock);
  73. list_for_each_entry(devinfo, &__i2c_board_list, list) {
  74. if (devinfo->busnum == adapter->nr
  75. && !i2c_new_device(adapter,
  76. &devinfo->board_info)) //initilize the device
  77. dev_err(&adapter->dev,
  78. "Can't create device at 0x%02x\n",
  79. devinfo->board_info.addr);
  80. }
  81. up_read(&__i2c_board_lock);
  82. }
  83. struct i2c_client *
  84. i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
  85. {
  86. struct i2c_client *client;
  87. int status;
  88. client = kzalloc(sizeof *client, GFP_KERNEL);
  89. if (!client)
  90. return NULL;
  91. client->adapter = adap;
  92. client->dev.platform_data = info->platform_data;
  93. if (info->archdata)
  94. client->dev.archdata = *info->archdata;
  95. client->flags = info->flags;
  96. client->addr = info->addr;
  97. client->irq = info->irq;
  98. strlcpy(client->name, info->type, sizeof(client->name));//set client name
  99. /* Check for address validity */
  100. status = i2c_check_client_addr_validity(client);
  101. if (status) {
  102. dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
  103. client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
  104. goto out_err_silent;
  105. }
  106. /* Check for address business */
  107. status = i2c_check_addr_busy(adap, client->addr);//check the addr occupied?
  108. if (status)
  109. goto out_err;
  110. client->dev.parent = &client->adapter->dev;//add on adapter
  111. client->dev.bus = &i2c_bus_type;
  112. client->dev.type = &i2c_client_type;
  113. #ifdef CONFIG_OF
  114. client->dev.of_node = info->of_node;
  115. #endif
  116. dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
  117. client->addr);//set clinet->dev.name=0-0x45
  118. status = device_register(&client->dev);//add the device first
  119. if (status)
  120. goto out_err;
  121. dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
  122. client->name, dev_name(&client->dev));
  123. return client;
  124. out_err:
  125. dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
  126. "(%d)\n", client->name, client->addr, status);
  127. out_err_silent:
  128. kfree(client);
  129. return NULL;
  130. }
  131. int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  132. {
  133. unsigned long orig_jiffies;
  134. int ret, try;
  135. if (adap->algo->master_xfer) {
  136. #ifdef DEBUG
  137. for (ret = 0; ret < num; ret++) {
  138. dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
  139. "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
  140. ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
  141. (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
  142. }
  143. #endif
  144. if (in_atomic() || irqs_disabled()) {
  145. ret = i2c_trylock_adapter(adap);
  146. if (!ret)
  147. /* I2C activity is ongoing. */
  148. return -EAGAIN;
  149. } else {
  150. i2c_lock_adapter(adap);
  151. }/* Retry automatically on arbitration loss */
  152. orig_jiffies = jiffies;
  153. for (ret = 0, try = 0; try <= adap->retries; try++) {
  154. ret = adap->algo->master_xfer(adap, msgs, num);//transfer msg--见上面
  155. if (ret != -EAGAIN)
  156. break;
  157. if (time_after(jiffies, orig_jiffies + adap->timeout))
  158. break;
  159. }
  160. i2c_unlock_adapter(adap);
  161. return ret;
  162. } else {
  163. dev_dbg(&adap->dev, "I2C level transfers not supported\n");
  164. return -EOPNOTSUPP;
  165. }
  166. }
 
 
   




   

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/750382
推荐阅读
相关标签
  

闽ICP备14008679号