OMAP3630 Linux I2C总线驱动分析(2)

4 OMAP3630 I2C device驱动
    在Linux内核中,I2C device驱动位于drivers/i2c/chips目录下,可以看到该目录下有很多相关的device驱动,这里以xxxx项目的mma7455为例介绍device驱动的注册过程,对应的device驱动程序为mma7455.c。
    既然有device驱动,那么必定有相应的device,I2C的device是什么呢?其实就是我们在1.3节中提到的i2c_client,所以在device驱动注册之前先来了解下i2c_client的注册过程。
4.1 Mma7455 device注册
    Mma7455 device即i2c_client的创建以及注册分为两步。
4.1.1 将mma7455设备信息加入到设备链表
    在板级初始化时将I2C device的名称,地址和相关的信息加入到链表__i2c_board_list中,该链表记录了具体开发板上的I2C设备信息。
    在board-xxxx.c中,定义了mma7455的设备信息定义如下:

[cpp]  view plain copy
  1. static struct i2c_board_info __initdata xxxx_i2c_bus3_info[] = {  
  2. ……  
  3. #ifdef CONFIG_SENSORS_MMA7455  
  4.         {  
  5.         I2C_BOARD_INFO("mma7455", 0x1D),  
  6.         .platform_data = &xxxx_mma7455_platform_data,  
  7.         },  
  8. #endif  
  9. };  

    Mma7455加入到设备链表__i2c_board_list的流程图如下图:

 


图4.1 mma7455加入到I2C设备链表的过程

 

    i2c_register_board_info()函数的定义如下:

[cpp]  view plain copy
  1. int __init i2c_register_board_info(int busnum,  
  2.     struct i2c_board_info const *info, unsigned len)  
  3. {  
  4.     ……  
  5.     for (status = 0; len; len--, info++) {  
  6.         struct i2c_devinfo  *devinfo;  
  7.         devinfo = kzalloc(sizeof(*devinfo), GFP_KERNEL);  
  8.         ……  
  9.         devinfo->busnum = busnum;  
  10.         devinfo->board_info = *info;  
  11.         list_add_tail(&devinfo->list, &__i2c_board_list);  
  12.     }  
  13.     ……  
  14. }  

4.1.2 创建并注册i2c_client
    i2c_client的创建和注册在I2C adapter驱动注册过程中完成,I2C adapter驱动的注册可以参考3.2.2节,i2c_add_numbered_adapter()函数在注册I2C adapter驱动的同时会扫描4.1.1中提到的I2C设备链表__i2c_board_list,如果该总线上有对应的I2C设备,则创建相应的i2c_client,并将其注册到I2C core中。流程图如下所示:

图4.2创建并注册i2c_client

 

    相应的代码位于i2c-core.c如下:

[cpp]  view plain copy
  1. static void i2c_scan_static_board_info(struct i2c_adapter *adapter)  
  2. {  
  3.     ……  
  4.     list_for_each_entry(devinfo, &__i2c_board_list, list) {  
  5.         if (devinfo->busnum == adapter->nr  
  6.                 && !i2c_new_device(adapter,  
  7.                         &devinfo->board_info))  
  8.             ……  
  9.     }  
  10.     ……  
  11. }  

    在i2c_scan_static_board_info()函数中遍历I2C设备链表__i2c_board_list,设备的总线号和adapter的总线号相等,则使用函数i2c_new_device()创建该设备。

[cpp]  view plain copy
  1. struct i2c_client *  
  2. i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)  
  3. {  
  4.     ……  
  5.     client = kzalloc(sizeof *client, GFP_KERNEL);  
  6.     if (!client)  
  7.         return NULL;  
  8.     client->adapter = adap;  
  9.     client->dev.platform_data = info->platform_data;  
  10.     if (info->archdata)  
  11.         client->dev.archdata = *info->archdata;  
  12.     client->flags = info->flags;  
  13.     client->addr = info->addr;  
  14.     client->irq = info->irq;  
  15.     strlcpy(client->name, info->type, sizeof(client->name));  
  16.     ……  
  17.     status = i2c_attach_client(client);  
  18.     ……  
  19. }  

     在函数i2c_new_device()中创建一个i2c_client,初始化该结构体的adapter,addr,name等变量,这里的client->name被初始化为info->type,在4.1.1中,info->type初始化为“mma7455”, client->name后面会用于I2C device和I2C driver匹配时使用,最后调用i2c_attach_client()将该client注册到I2C core。

[cpp]  view plain copy
  1. int i2c_attach_client(struct i2c_client *client)  
  2. {  
  3.     struct i2c_adapter *adapter = client->adapter;  
  4.     ……  
  5.     client->dev.parent = &client->adapter->dev;  
  6.     client->dev.bus = &i2c_bus_type;  
  7.   
  8.     ……  
  9.     res = device_register(&client->dev);  
  10.     ……  
  11. }  

    函数i2c_attach_client()进一步初始化i2c_client结构体,将该设备的总线初始化为i2c_bus_type,说明该设备被放在I2C总线上,用于后面跟I2C driver匹配时使用,最后使用device_register(&client->dev)注册该i2c_client设备。
4.2 Mma7455 device驱动注册
    在mma7455.c中,定义了mma7455的device驱动,代码如下:

[cpp]  view plain copy
  1. static struct i2c_driver mma7455_driver = {  
  2.     .driver     = {  
  3.                 .name = "mma7455",  
  4.             },  
  5.     .class      = I2C_CLASS_HWMON,  
  6.     .probe      = mma7455_probe,  
  7.     .remove     = mma7455_remove,  
  8.     .id_table   = mma7455_id,  
  9.     ……  
  10. };  

    注册的简要示意图如下:

 
图4.3 device驱动的注册

    相应的代码位于mma7455.c和i2c-core.c。

[c-sharp]  view plain copy
  1. static int __init init_mma7455(void)  
  2. {  
  3.     ……  
  4.     res = i2c_add_driver(&mma7455_driver);  
  5.     ……  
  6.     return (res);  
  7. }  

    在模块加载的时候首先调用init_mma7455(),然后init_mma7455()调用函数i2c_add_driver()注册mma7455_driver结构体。

[cpp]  view plain copy
  1. int i2c_register_driver(struct module *owner, struct i2c_driver *driver)  
  2. {  
  3.     …….  
  4.     /* add the driver to the list of i2c drivers in the driver core */  
  5.     driver->driver.owner = owner;  
  6.     driver->driver.bus = &i2c_bus_type;  
  7.   
  8.     ……  
  9.     res = driver_register(&driver->driver);  
  10.     if (res)  
  11.         return res;  
  12.   
  13.     ……  
  14. }  

    函数i2c_register_driver()初始化该驱动的总线为i2c_bus_type,然后使用函数driver_register(&driver->driver)注册该驱动,因此内核会在I2C总线上遍历所有I2C设备,由于该mma7455 device驱动的匹配因子name变量为“mma7455”,因此正好和在4.1.2里创建的name也为“mma7455”的i2c client匹配。因此总线的probe函数将会被调用,I2C总线的probe函数为i2c_device_probe(),具体代码如下:

[cpp]  view plain copy
  1. static int i2c_device_probe(struct device *dev)  
  2. {  
  3.     struct i2c_client   *client = to_i2c_client(dev);  
  4.     struct i2c_driver   *driver = to_i2c_driver(dev->driver);  
  5.     int status;  
  6.   
  7.     if (!driver->probe || !driver->id_table)  
  8.         return -ENODEV;  
  9.     client->driver = driver;  
  10.     …….  
  11.     status = driver->probe(client, i2c_match_id(driver->id_table, client));  
  12.     ……  
  13.     return status;  
  14. }  

    在i2c_device_probe()函数中,语句client->driver = driver将I2C device和I2C driver绑定,然后直接调用具体设备的probe函数,这里即mma7455的probe函数mma7455_probe()。
    在mma7455_probe()函数会完成一些具体I2C设备相关的初始化等操作,这边就不再详述。

5 用户空间的支持
    
图1.1Linux I2C的架构图中的i2c-dev部份是一个通用的I2C设备的驱动程序,通过一个带有操作集file_operations的标准字符设备驱动为用户空间提供了访问接口,使用户空间可以通过I2C core,进而访问I2C adapter。
5.1 I2c-dev的注册
    该部分的源代码位于drivers/i2c/i2c-dev.c,首先定义了I2C device驱动i2cdev_driver:

[c-sharp]  view plain copy
  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. };  

    i2cdev_driver注册代码如下:

[cpp]  view plain copy
  1. static int __init i2c_dev_init(void)  
  2. {  
  3.     ……  
  4.     res = register_chrdev(I2C_MAJOR, "i2c", &i2cdev_fops);  
  5.     ……  
  6.     i2c_dev_class = class_create(THIS_MODULE, "i2c-dev");  
  7.     ……  
  8.     res = i2c_add_driver(&i2cdev_driver);  
  9.     ……  
  10. }  

    首先注册了一个主设备号为I2C_MAJOR,操作集为i2cdev_fops,名字为“i2c”的字符设备。在文件drivers/i2c/i2c-dev.h中,I2C_MAJOR被定义为89。在i2c-dev.c中i2cdev_fops的定义如下:

[c-sharp]  view plain copy
  1. static const struct file_operations i2cdev_fops = {  
  2.     .owner      = THIS_MODULE,  
  3.     .llseek     = no_llseek,  
  4.     .read       = i2cdev_read,  
  5.     .write      = i2cdev_write,  
  6.     .unlocked_ioctl = i2cdev_ioctl,  
  7.     .open       = i2cdev_open,  
  8.     .release    = i2cdev_release,  
  9. };  

     该操作集是用户空间访问该字符设备的接口。
   然后调用函数i2c_add_driver(&i2cdev_driver)将i2cdev_driver驱动注册到i2c core中,i2cdev_driver驱动注册的流程图如下:

图5.1 i2cdev_driver注册过程

 
    注册i2c_driver时,会将驱动和adapter绑定起来,然后将调用i2c_driver 的attach_adapter 方法,即i2cdev_attach_adapter()函数,建立dev设备节点,每个adapter都会对应一个dev设备节点,并维护了一个i2c_dev链表保存设备节点和adapter的关系。
    i2cdev_attach_adapter()函数的代码如下:

 

[cpp]  view plain copy
  1. static int i2cdev_attach_adapter(struct i2c_adapter *adap)  
  2. {  
  3.     ……  
  4.     /* register this i2c device with the driver core */  
  5.     i2c_dev->dev = device_create(i2c_dev_class, &adap->dev,  
  6.                      MKDEV(I2C_MAJOR, adap->nr), NULL,  
  7.                      "i2c-%d", adap->nr);  
  8.     ……  
  9.     res = device_create_file(i2c_dev->dev, &dev_attr_name);  
  10.     ……  
  11. }  

    以I2C_MAJOR和adap->nr为主从设备号创建并注册设备节点,如果系统有udev或者是hotplug,那么就会在/dev下自动创建相关的设备节点了。
5.2 I2c-dev的打开
    I2c-dev的open函数如下:

[cpp]  view plain copy
  1. static int i2cdev_open(struct inode *inode, struct file *file)  
  2. {  
  3.     ……  
  4.     i2c_dev = i2c_dev_get_by_minor(minor);  
  5.     if (!i2c_dev) {  
  6.         ret = -ENODEV;  
  7.         goto out;  
  8.     }  
  9.   
  10.     adap = i2c_get_adapter(i2c_dev->adap->nr);  
  11.     ……  
  12.     client = kzalloc(sizeof(*client), GFP_KERNEL);  
  13.     if (!client) {  
  14.         i2c_put_adapter(adap);  
  15.         ret = -ENOMEM;  
  16.         goto out;  
  17.     }  
  18.     snprintf(client->name, I2C_NAME_SIZE, "i2c-dev %d", adap->nr);  
  19.     client->driver = &i2cdev_driver;  
  20.     client->adapter = adap;  
  21.     file->private_data = client;  
  22.     ……  
  23. }  

    Open操作是用户空间程序和内核驱动交换的第一步,最终返回给用户空间的就是struct file结构体。对于I2C 驱动来说,用户空间所获得的就是client这个关键信息,在其中可以找到所有有关的信息如client所在的adapter及i2c_driver。
    用open函数将i2c-dev设备打开以后,就可以通过ioctl函数的各种命令来设定要访问从设备的地址,I2C设备读写等操作,也可以通过 read和write函数完成对I2C设备的读写。
    对I2C设备的具体操作在这里不再具体阐述,可以参看i2c-dev.c源代码。
6 I2C数据收发的框架
    I2C架构的读写支持两种协议类型,I2C协议与SMBUS协议。
    I2C协议和SMBUS协议不完成等同,SMBUS是I2C的子集,SMBUS由I2C衍生而来。SMBUS总线上传输的数据一定是I2C的格式的,但是SMBUS上传输的数据不一定能满足具体某个I2C从设备的通信要求。
6.1 SMBUS协议的数据收发
    如果控制器不支持SMBUS协议,框架层可以用i2c_transfer模拟SMBUS协议的实现,系统默认的I2C传输函数一般都是基于I2C模拟的SMBUS方法传输的,如i2c_smbus_write_byte_data(),i2c_smbus_read_byte_data()等。
    在设备mma7455的驱动程序中,使用了SMBUS协议,而OMAP3630控制器使用的是I2C协议,因此在mma7455的驱动程序中就用到了基于I2C模拟的SMBUS方法。
    下面以函数i2c_smbus_write_byte_data()为例来说明SMBUS协议下的数据发送的过程。
    在i2c-core.c中,函数i2c_smbus_write_byte_data()的定义如下:

[cpp]  view plain copy
  1. /** 
  2.  * i2c_smbus_write_byte_data - SMBus "write byte" protocol 
  3.  * @client: Handle to slave device 
  4.  * @command: Byte interpreted by slave 
  5.  * @value: Byte being written 
  6.  * 
  7.  * This executes the SMBus "write byte" protocol, returning negative errno 
  8.  * else zero on success. 
  9.  */  
  10. s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)  
  11. {  
  12.     union i2c_smbus_data data;  
  13.     data.byte = value;  
  14.     return i2c_smbus_xfer(client->adapter,client->addr,client->flags,  
  15.                           I2C_SMBUS_WRITE,command,  
  16.                           I2C_SMBUS_BYTE_DATA,&data);  
  17. }  

    函数i2c_smbus_write_byte_data()的调用流程图如下:


图6.1 SMBUS协议下的数据发送过程 

 

    从图6.1可以看到,走哪条分支取决于I2C控制器的i2c_algorithm算法,当定义了方法smbus_xfer,则直接调用该方法,如果没有则通过先调用i2c_smbus_xfer_emulated(),进而通过i2c_transfer()最终调用I2C协议下的master_xfer方法,所以我们说SMBUS总线上传输的数据一定是I2C的格式的。
6.2 I2C协议的数据收发
    I2C协议下的数据收发函数就是常用的I2C传输函数:i2c_master_send()和i2c_master_recv()。
    下面以函数i2c_master_send ()为例来说明I2C协议下的数据发送的过程。
    在i2c-core.c中,函数i2c_master_send()定义如下:

 

[cpp]  view plain copy
  1. /** 
  2.  * i2c_master_send - issue a single I2C message in master transmit mode 
  3.  * @client: Handle to slave device 
  4.  * @buf: Data that will be written to the slave 
  5.  * @count: How many bytes to write 
  6.  * 
  7.  * Returns negative errno, or else the number of bytes written. 
  8.  */  
  9. int i2c_master_send(struct i2c_client *client,const char *buf ,int count)  
  10. {  
  11.     int ret;  
  12.     struct i2c_adapter *adap=client->adapter;  
  13.     struct i2c_msg msg;  
  14.   
  15.     msg.addr = client->addr;  
  16.     msg.flags = client->flags & I2C_M_TEN;  
  17.     msg.len = count;  
  18.     msg.buf = (char *)buf;  
  19.     ret = i2c_transfer(adap, &msg, 1);  
  20.   
  21.     /* If everything went ok (i.e. 1 msg transmitted), return #bytes 
  22.        transmitted, else error code. */  
  23.     return (ret == 1) ? count : ret;  
  24. }  

    从源代码中可以看出,函数i2c_master_send()先构造i2c_msg结构体,然后直接调用函数i2c_transfer。简单的示意图如下:


图6-2 I2C协议下的数据发送过程

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值