linux3.5下I2C驱动

  
                          
               

知识背景:

1. I2C协议

2. 4412处理器I2C接口说明

3. bus-dev-drv模型(详见文章-Linux下驱动:分层、分离机制学习笔记)

4. linux内核下驱动设计基本知识


一、驱动框架

      以4412+linux3.5平台为例,说明Linux下I2C设备驱动程序。

      如果一条I2C总线上只连接一个I2C设备,那么只需要以字符型设备驱动框架来写驱动即可(填充file_opreoration结构体中的各个成员),那是典型的字符型设备驱动程序。

      实际上,一款处理器可能有多条I2C总线,多个I2C控制器,每条总线上可能挂接多个设备,按照典型的字符型驱动框架写驱动就要为每个这样的设备写一个驱动程序,而且要做很多重复性的工作。

      所以,有很多东西可以抽象的就抽象出来,实现一种框架,这种框架模型还是bus-dev-drv模型。4412处理器有8条可用I2C总线,每条总线上挂接的每个I2C设备都明白它们跟4412通信数据的含义。4412访问I2C设备时,都需要发出Start信号,都需要发出设备地址等等,这些共性的东西就可以抽象出来用一些函数实现,这些函数就是在另外一层即核心层驱动程序。

      核心层驱动程序提供统一的I2C设备操作函数,比如读写I2C设备的操作函数;还有设备驱动层(file_opreoration结构体相关);另外一层就是适配层,这一层是处理器的每个I2C适配器(也叫控制器)的驱动,适配层驱动程序提供统一的处理器I2C接口硬件操作函数。

       应用程序需要操作I2C设备时,根据设备节点调用读写函数(系统调用),然后就会执行设备驱动层的相应读写函数(它们是file_opreoration结构体的成员),这些读写函数里会调用核心层提供的统一的I2C设备读写操作函数,这些函数最终会通过适配器(I2C控制器)给连接在I2C总线上的I2C设备发送相应读写命令。适配器那一层驱动程序就是些I2C控制器的硬件操作,它是根据I2C协议来的(这里根据的I2C协议往往是硬件已经做好了的,软件只需要配置CPU的I2C控制器的某个寄存器即可实现I2C协议),是最底层,当I2C控制器的某个寄存器被控制后,I2C控制器就会自动地往I2C线上根据I2C协议发出相应信号与2IC设备通信。

       由此可见,写设备驱动程序即实现设备驱动层时,只要弄清楚bus-dev-drv驱动模型,(调用了probe函数后)剩下的就是典型的字符型设备驱动那一套了(构建file_opreoration结构体、分配主次设备号、创建设备节点),file_opreoration的读写函数调用的方法也由核心层提供。图一。


      上述三层是通过总线-设备-驱动模型融合到一起的。Linux内核中构建了许多总线,但并不是都是真实的,比如平台总线就是虚拟的,平台总线中也有设备链表,驱动链表。

      针对I2C总线,也有一个I2C总线的结构即i2c_bus_type结构,此结构里面也有设备链表和也有驱动链表。设备链表里存放i2c_client的结构体,这些结构体是注册i2c_client时加入的,不但要加入这些结构体,还会在总线的驱动链表中一个一个地比较drv即i2c_driver来判断是否有匹配的,如果有将调用drv里面的probe函数,匹配函数由总线提供;驱动链表里存放i2c_driver结构体,这些结构体是注册i2c_driver时加入的,不但要加入这些结构体,还会在总线的设备链表中一个一个地比较dev即比较i2c_client来判断是否有匹配的,如果匹配将调用drv里面的probe函数。

     上述的匹配函数就是i2c_bus_type结构里面的i2c_device_match函数。i2c_device_match函数通过id_table(i2c_driver结构的成员,它表示能支持哪些设备)来比较dev与drv是否匹配,具体方法是用id_table的name去比较,name分别是i2c_client结构和i2c_driver结构的成员。如果名字相同,就表示此驱动i2c_driver能支持这个设备i2c_client。

      总的说来,I2C基于bus-dev-drv模型的构建过程如下:

(1)左边注册一个设备,i2c_client

(2)右边注册一个驱动,i2c_driver

(3)比较它们的名字,如果相同,则调用driver的probe函数。

(4)probe函数里面做的事情由用户决定(比如注册、构建设备节点等,这些属于设备驱动)。


二、设置和注册i2c_client结构体(EEPROM为例)

      在Linux内核文档(/Documentation/i2c/instantiating-devices)中,介绍了注册即构造一个i2c_client的4种方法,并且建议使用如下前三种方法,后一种较复杂,万不得已才用。

1.  通过总线号声明一个I2C设备

      通过总线号声明一个I2C设备即构造一个i2c_board_info结构体,它里面有名字和地址,名字可以用来找到drv结构,地址可以用来访问哪个I2C设备,还可以放其他信息,这些信息会被将来的probe函数用到,也就是说在probe函数中要使用到哪些信息,这里面就可以增加哪些信息。然后使用i2c_register_board_info函数注册这个结构体,此函数的第一个参数即总线号,表示此设备属于哪条总线(这就是标题通过总线号声明一个i2c设备的含义),在此函数中会把此结构体放入链表__i2c_board_list

      然后在i2c_scan_static_board_info函数中使用到__i2c_board_list链表,即调用i2c_new_device函数把链表中的每个成员构造成一个i2c_client,并放入bus-dev-drv模型中总线中设备链表中去。在i2c_new_device函数中,分配一个i2c_client结构体后,设置它,并调用device_register函数注册,此函数最终会调用前面文章中提到device_add函数。i2c_scan_static_board_info函数是被i2c_register_adapter函数调用的,所以这里总的过程为i2c_register_adapter  > i2c_scan_static_board_info > i2c_new_device。

     ft5x06_ts类型的I2C触摸屏驱动中就是使用的这种方法。在内核文件arch/arm/mach-exynos/mach-tiny4412.c中,对应触摸屏的i2c_register_board_info函数调用过程如下:

MACHINE_START(TINY4412, "TINY4412")
    .xxx
    .init_machine    = smdk4x12_machine_init,
    .xxx
MACHINE_END
                                 ↓

         调用smdk4x12_machine_init函数

                                 ↓

调用i2c_register_board_info(1, smdk4x12_i2c_devs1,ARRAY_SIZE(smdk4x12_i2c_devs1));

       可见,上述i2c_register_board_info函数的第一个参数为1,表示ft5x06_ts类型的I2C触摸屏挂接在处理器的I2C1那条总线上。


这种方法使用限制:在注册I2C适配器之前注册i2c_board_info结构体,即必须在 i2c_register_adapter 之前 i2c_register_board_info,所以不适合动态加载insmod。


2.  直接创建设备(直接调用i2c_new_device、i2c_new_probed_device)

(1)i2c_new_device方法

      第一种方法显得有些麻烦,这里就直接调用 i2c_new_device或i2c_new_probed_device函数实现。

      i2c_new_device函数总共有两个参数,第一个为要指定的适配器i2c_adapter(一个用来标识物理I2C总线结构,即用哪个I2C控制器发出I2C信号,某些CPU有多个I2C适配器),即要把i2c设备跟哪个适配器相连,这样以后在访问I2C设备时,就知道使用哪个适配器的操作函数了。第二个参数是用来描述I2C设备的相关信息的,即i2c_board_info结构体。所以这种方法需要在dev程序中定义i2c_adapter和i2c_board_info结构,i2c_board_info结构可以直接在dev程序中定义,i2c_adapter创建的代码如下:

    struct   i2c_adapter *i2c_adap;
    i2c_adap = i2c_get_adapter(0);
    at24cxx_client = i2c_new_device(i2c_adap, &at24cxx_info);
    i2c_put_adapter(i2c_adap);

     先定义一个i2c_adapter结构指针存放i2c_get_adapter(0)的返回值,此函数的参数为0,表示第0条总线,i2c_new_device调用完后要调用  i2c_put_adapter函数。通过这样的方式,在第0条总线下创建了一个新设备,以后就可以使用i2c_adap这个i2c_adapter的操作函数发出I2C的信号了,比如起始信号、停止信号等。实验代码如下:

at24cxx_dev.c :


     
     
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/platform_device.h>
  4. #include <linux/i2c.h>
  5. #include <linux/err.h>
  6. #include <linux/regmap.h>
  7. #include <linux/slab.h>
  8. //0x50表示I2C设备的地址,一般在 I2C设备芯片手册可以查到
  9. static struct i2c_board_info at24cxx_info = { 
  10.  I2C_BOARD_INFO( "at24c08", 0x50), //这个名字要和drv程序中的id_table中名字要一样
  11. };
  12. static struct i2c_client *at24cxx_client;
  13. static int at24cxx_dev_init(void)
  14. {
  15.   struct i2c_adapter *i2c_adap;
  16.  i2c_adap = i2c_get_adapter( 0); //这里要实验的EEPROM是挂接在第0条I2C总线上的,所以这里的参数是0
  17.  at24cxx_client = i2c_new_device(i2c_adap, &at24cxx_info);
  18.  i2c_put_adapter(i2c_adap);
  19.  
  20.   return 0;
  21. }
  22. static void at24cxx_dev_exit(void)
  23. {
  24.  i2c_unregister_device(at24cxx_client);
  25. }
  26. module_init(at24cxx_dev_init);
  27. module_exit(at24cxx_dev_exit);
  28. MODULE_LICENSE( "GPL");
at24cxx_drv.c :

     
     
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/platform_device.h>
  4. #include <linux/i2c.h>
  5. #include <linux/err.h>
  6. #include <linux/regmap.h>
  7. #include <linux/slab.h>
  8. static int __ devinit at24cxx_probe(struct i2c_client *client,
  9.       const struct i2c_device_id *id)
  10. {
  11.  printk( "%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
  12.   return 0;
  13. }
  14. static int __ devexit at24cxx_remove(struct i2c_client *client)
  15. {
  16.  printk( "%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
  17.   return 0;
  18. }
  19. static const struct i2c_device_id at24cxx_id_table[] = {
  20.  { "at24c08", 0 }, //用到哪些就声明哪些内容,比如driver_data用不到,所以这里就写0
  21.  {}
  22. };
  23. /* 1. 分配/设置i2c_driver */
  24. static struct i2c_driver at24cxx_driver = {
  25.  .driver = {
  26.   .name = "100ask", //在这里,这个名字并不重要,重要的是id_table里面的名字,所以这里可以随便起
  27.   .owner = THIS_MODULE,
  28.  },
  29.  .probe  = at24cxx_probe,
  30.  .remove  = __devexit_p(at24cxx_remove),
  31.  .id_table = at24cxx_id_table,
  32. };
  33. static int at24cxx_drv_init(void)
  34. {
  35.   /* 2. 注册i2c_driver */
  36.  i2c_add_driver(&at24cxx_driver); //实际使用时一定要判断返回值
  37.  
  38.   return 0;
  39. }
  40. static void at24cxx_drv_exit(void)
  41. {
  42.  i2c_del_driver(&at24cxx_driver);
  43. }
  44. module_init(at24cxx_drv_init);
  45. module_exit(at24cxx_drv_exit);
  46. MODULE_LICENSE( "GPL");<span style= "display: none; width: 0px; height: 0px;" id= "transmark"></span>

Makefile :


     
     
  1. KERN_DIR = /home/samba/linuxKernel_ext4Fs_src/linux -3.5 -2015 -8
  2. all:
  3.  make -C $(KERN_DIR) M=`pwd` modules
  4. clean:
  5.  make -C $(KERN_DIR) M=`pwd` modules clean
  6.  rm -rf modules.order
  7. obj-m += at24cxx_dev.o
  8. obj-m += at24cxx_drv.o
 
    

      分别加载at24cxx_dev.ko和at24cxx_drv.ko,at24cxx_probe函数会被调用,随意修改i2c_board_info结构里面的地址,at24cxx_probe函数照样被调用。所以这种方法不会去真正地验证连接的I2C设备的地址是否为i2c_board_info结构里面的地址。

(2) i2c_new_probed_device方法

     i2c_new_device : 认为设备肯定存在,实验时可以用这种方法改掉I2C设备的地址,改成任意值都是可以的。     i2c_new_probed_device :对于"已经识别出来的设备"(probed_device),才会创建("new")     i2c_new_probed_device函数主要做如下三件事:                probe(adap, addr_list[i])   /* 如果i2c_new_probed_device最后个参数中没有指定probe函数,将使用默认probe函数。不管哪个,它都要确定设备是否真实存在 */               info->addr = addr_list[i];   /* 如果在addr_list中找到了一个地址和现实中连接在I2C总线上的设备匹配,将这个地址放入i2c_board_info结构,并传给i2c_new_device */                i2c_new_device(adap, info);     为了实验,分别编译如下代码,然后分别加载at24cxx_dev.ko和at24cxx_drv.ko,如果在addr_list数组中有一个地址是I2C总线上设备的地址,那么在加载at24cxx_dev.ko驱动模块时,能加载成功,并且加载at24cxx_drv.ko模块后,将调用drv的probe函数。如果没有那个地址,那么在加载at24cxx_dev.ko驱动模块时会失败,提示如下信息:

insmod: can't insert 'at24cxx_dev.ko': No such device

     这就是直接使用i2c_new_device和使用i2c_new_probed_device创建i2c_client的区别。

at24cxx_dev.c :


     
     
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/platform_device.h>
  4. #include <linux/i2c.h>
  5. #include <linux/err.h>
  6. #include <linux/regmap.h>
  7. #include <linux/slab.h>
  8. static struct i2c_client *at24cxx_client;
  9. //如果挂接在I2C总线上的i2c设备的地址在此数组里面都找不到,将不能加载此驱动模块
  10. static const unsigned short addr_list[] = { 0x60, 0x50,I2C_CLIENT_END };
  11. static int at24cxx_dev_init(void)
  12. {
  13.   struct i2c_adapter *i2c_adap;
  14.   struct i2c_board_info at24cxx_info;
  15.   memset(&at24cxx_info, 0, sizeof(struct i2c_board_info)); 
  16.  strlcpy(at24cxx_info.type, "at24c08", I2C_NAME_SIZE);
  17.  i2c_adap = i2c_get_adapter( 0);
  18.  at24cxx_client = i2c_new_probed_device(i2c_adap, &at24cxx_info, addr_list, NULL);
  19.  i2c_put_adapter(i2c_adap);
  20.   if (at24cxx_client)
  21.    return 0;
  22.   else
  23.    return -ENODEV;
  24. }
  25. static void at24cxx_dev_exit(void)
  26. {
  27.  i2c_unregister_device(at24cxx_client);
  28. }
  29. module_init(at24cxx_dev_init);
  30. module_exit(at24cxx_dev_exit);
  31. MODULE_LICENSE( "GPL");

at24cxx_drv.c :


     
     
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/platform_device.h>
  4. #include <linux/i2c.h>
  5. #include <linux/err.h>
  6. #include <linux/regmap.h>
  7. #include <linux/slab.h>
  8. static int __ devinit at24cxx_probe(struct i2c_client *client,
  9.                        const struct i2c_device_id *id)
  10. {
  11.  printk( "%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
  12.   return 0;
  13. }
  14. static int __ devexit at24cxx_remove(struct i2c_client *client)
  15. {
  16.  printk( "%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
  17.   return 0;
  18. }
  19. static const struct i2c_device_id at24cxx_id_table[] = {
  20.  { "at24c08", 0 }, //用到哪些就声明哪些内容,比如driver_data用不到,所以这里就写0
  21.  {}
  22. };
  23. /* 1. 分配/设置i2c_driver */
  24. static struct i2c_driver at24cxx_driver = {
  25.  .driver = {
  26.   .name = "100ask", //在这里,这个名字并不重要,重要的是id_table里面的名字,所以这里可以随便起
  27.   .owner = THIS_MODULE,
  28.  },
  29.  .probe  = at24cxx_probe,
  30.  .remove  = __devexit_p(at24cxx_remove),
  31.  .id_table = at24cxx_id_table,
  32. };
  33. static int at24cxx_drv_init(void)
  34. {
  35.   /* 2. 注册i2c_driver */
  36.  i2c_add_driver(&at24cxx_driver); //一定要判断返回值
  37.  
  38.   return 0;
  39. }
  40. static void at24cxx_drv_exit(void)
  41. {
  42.  i2c_del_driver(&at24cxx_driver);
  43. }
  44. module_init(at24cxx_drv_init);
  45. module_exit(at24cxx_drv_exit);
  46. MODULE_LICENSE( "GPL");

3.  从用户空间创建设备(详细阅读/Documentation/i2c/instantiating-devices文档)

      执行命令cd /sys/class/i2c-adapter/,可以看到内容i2c-0  i2c-1  i2c-2  i2c-3  i2c-7  i2c-8,说明有多款适配器,即多个I2C控制器,即多条I2C总线。其中EEPROM是挂接在I2C-0下面的(看板子原理图)。
     < 做下面实验需要把内核中静态编译进的drv驱动给去掉,然后加载自己的drv驱动>

创建设备
       echo at24c08 0x50 > /sys/class/i2c-adapter/i2c-0/new_device,导致i2c_new_device被调用,最后drv里的probe函数就不会被调用。如果把地址改为0x51,那么也会在bus的                                                                                                            dev链表中增加一个dev结构,所以这种方法也是不会判断地址是否正确。

删除设备
       echo 0x50 > /sys/class/i2c-adapter/i2c-0/delete_device,导致i2c_unregister_device。       

4.  注册设置i2c_client的第四种方法(此方法交复杂,前三种都不行时才用)

     上述三种方法都是知道I2C设备属于哪个适配器,即知道连在了哪条I2C总线上,如果不知道属于哪个适配器的情况下(4412有多个I2C适配器)就需要用本方法,本方法可以参考例子/drivers/hwmon/lm90.c。

     如果事先并不知道这个I2C设备在哪个适配器上,怎么办?去class表示的所有的适配器上查找。
     有些I2C设备的地址是一样,怎么继续区分它是哪一款?用detect函数。

     此方法过程:   
     由于事先并不知道I2C设备在哪个适配器上,所以去"class表示的那一类"I2C适配器上找,用"detect函数"来确定能否找到"address_list里的设备",如果能找到就调用i2c_new_device来注册i2c_client, 这会和i2c_driver的id_table比较,如果匹配,调用probe。      

     详细代码调用过程:
i2c_add_driver
    i2c_register_driver
        a. at24cxx_driver放入i2c_bus_type的drv链表
           并且从dev链表里取出能匹配的i2c_client并调用probe
           driver_register
           
        b. 对于每一个适配器,调用__process_new_driver(在i2c_bus_type的dev链表中不但要挂i2c_client外,还会挂i2c_adpter。当drv和dev链表比较的时候,drv不会跟i2c_adpter比较,只会跟i2c_client比较,因为i2c_adpter的.type成员可以用来分辨是i2c_adpter还是i2c_client)。
           
           对于每一个适配器,调用它的函数确定address_list里的设备是否存在(确定的方法是给I2C设备发一个地址,看它是否回应ACK,即SDA是否被拉低),即是否支持这个设备。如果存在,再调用detect进一步确定、设置以确定是哪类设备,因为有些设备地址一样,单从地址是没办法分辨是哪类设备的(详细可以阅读内核文档
           /Documentation/i2c/instantiating-devices)。然后i2c_new_device
        /* Walk the adapters that are already present */
        i2c_for_each_dev(driver, __process_new_driver);
            __process_new_driver
                i2c_do_add_adapter
                    /* Detect supported devices on that bus, and instantiate them */
                    i2c_detect(adap, driver);
                        for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
                            err = i2c_detect_address(temp_client, driver);
                                        /* 判断这个设备是否存在:简单的发出S信号确定有ACK */
                                        if (!i2c_default_probe(adapter, addr))
                                            return 0;
                                        
                                        memset(&info, 0, sizeof(struct i2c_board_info));
                                        info.addr = addr;    
                                        
                                        // 设置info.type,调用strlcpy函数拷贝
                                        err = driver->detect(temp_client, &info);
                    
                                        i2c_new_device(最终注册设置i2c_client)   


at24cxx_drv.c :


     
     
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/platform_device.h>
  4. #include <linux/i2c.h>
  5. #include <linux/err.h>
  6. #include <linux/regmap.h>
  7. #include <linux/slab.h>
  8. static int __ devinit at24cxx_probe(struct i2c_client *client,
  9.       const struct i2c_device_id *id)
  10. {
  11.  printk( "%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
  12.   return 0;
  13. }
  14. static int __ devexit at24cxx_remove(struct i2c_client *client)
  15. {
  16.  printk( "%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
  17.   return 0;
  18. }
  19. static const struct i2c_device_id at24cxx_id_table[] = {
  20.  { "at24c08", 0 },
  21.  {}
  22. };
  23. static int at24cxx_detect(struct i2c_client *client,
  24.          struct i2c_board_info *info)
  25. {
  26.   /* 能运行到这里, 表示该addr的设备是存在的,即dev链表中是有这个设备的
  27.   * 但是有些设备单凭地址无法分辨(A芯片的地址是0x50, B芯片的地址也是0x50,当发0x50后它们都会回应,这个时候还是不能区分到底是A还是B,A和B是不可能同时挂在一条总线上的)
  28.   * 还需要进一步读写I2C设备来分辨是哪款芯片,比如读A芯片可能有一些值,读B芯片就会有另外一些值
  29.   * detect就是用来进一步分辨这个芯片是哪一款,并且设置info->type
  30.   */
  31.  
  32.  printk( "at24cxx_detect : addr = 0x%x\n", client->addr);
  33.   /* 这里应该进一步判断是哪一款,这里不用判断 */
  34.  
  35.  strlcpy(info->type, "at24c08", I2C_NAME_SIZE);
  36.   return 0;
  37. }
  38. //0x60、0x50表示I2C设备的地址
  39. static const unsigned short addr_list[] = { 0x60, 0x50, I2C_CLIENT_END };
  40. /* 1. 分配/设置i2c_driver */
  41. static struct i2c_driver at24cxx_driver = {
  42.  .class  = I2C_CLASS_HWMON, /* 表示去哪些适配器上找设备 */
  43.  .driver = {
  44.   .name = "100ask",
  45.   .owner = THIS_MODULE,
  46.  },
  47.  .probe  = at24cxx_probe,
  48.  .remove  = __devexit_p(at24cxx_remove),
  49.  .id_table = at24cxx_id_table,
  50.  .detect     = at24cxx_detect,  /* 用这个函数来检测设备确实存在 */
  51.  .address_list = addr_list,   /* 这些设备的地址 */
  52. };
  53. static int at24cxx_drv_init(void)
  54. {
  55.   /* 2. 注册i2c_driver */
  56.  i2c_add_driver(&at24cxx_driver);
  57.  
  58.   return 0;
  59. }
  60. static void at24cxx_drv_exit(void)
  61. {
  62.  i2c_del_driver(&at24cxx_driver);
  63. }
  64. module_init(at24cxx_drv_init);
  65. module_exit(at24cxx_drv_exit);
  66. MODULE_LICENSE( "GPL");

三、I2C设备驱动程序的编写

      上面主要介绍了注册i2c_client结构的四种方法,并伴随测试程序at24cxx_dev.c和at24cxx_drv.c。

      然而,我们的目的是应用程序能通过系统调用读写EEPROM存储器,这就需要实现图一中的设备驱动程序。实现的地方就是在probe函数中,当i2c_client结构和i2c_driver结构都注册后,在i2c_bus_type结构的i2c_client链表中就会有dev,i2c_driver链表中有drv,bus的i2c_device_match函数中匹配dev和drv,成功将调用probe函数。在上面测试过程中probe函数基本上什么也没做,原因在于为了测试i2c_client结构的注册,这里的目的是实现设备驱动层,所以会在里面实现注册设备驱动、创建设备节点等操作。

      注意,针对i2c_driver结构的probe成员,也就是上面说的probe函数的参数也是非常有用的。当probe函数成功调用后,它的第一个参数就记录了对应的I2C设备,也就是i2c_client结构体,第二个参数记录对应I2C设备的i2c_device_id。在后面设备驱动读写函数中将调用核心层的读写函数,这些函数的第一个参数就是要知道是哪个I2C设备,即要传入i2c_client结构体。所以,在probe函数中,可以定义结构体指针指向probe函数参数,通过这样的方式记录保存了i2c_client和i2c_device_id。

     这样,图一中的设备驱动层就实现了,而核心层和适配器层都是内核自带的,主要是提供接口供设备驱动使用。下面是应用层操作EEPROM将要用到的所有完整代码:

at24cxx_dev.c :


     
     
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/platform_device.h>
  4. #include <linux/i2c.h>
  5. #include <linux/err.h>
  6. #include <linux/regmap.h>
  7. #include <linux/slab.h>
  8. //0x50表示I2C设备的地址,一般在 I2C设备芯片手册可以查到
  9. static struct i2c_board_info at24cxx_info = { 
  10.  I2C_BOARD_INFO( "at24c08", 0x50), //这个名字要和drv程序中的id_table中名字要一样
  11. };
  12. static struct i2c_client *at24cxx_client;
  13. static int at24cxx_dev_init(void)
  14. {
  15.   struct i2c_adapter *i2c_adap;
  16.   int busNum = 0 ; //把这个总线号改为1,也能成功加载此驱动,原因在于i2c_new_device而不是i2c_new_probed_device方法
  17.  
  18.  printk( "at24cxx dev of bus-dev-drv module_init!\n");
  19.  
  20.  i2c_adap = i2c_get_adapter(busNum); //这里要实验的EEPROM是挂接在第0条I2C总线上的,所以这里的参数是0
  21.   if (!i2c_adap) {
  22.   pr_err( "failed to get adapter i2c%d\n", busNum);
  23.    return -ENODEV;
  24.  }
  25.  
  26.  at24cxx_client = i2c_new_device(i2c_adap, &at24cxx_info); //设置和注册i2c_client结构体
  27.   if (!at24cxx_client){
  28.    //pr_err("failed to register %s to i2c%d\n",at24cxx_info.type, busNum);
  29.   pr_err( "failed to register at24c08 to i2c%d\n",busNum);
  30.    return -ENODEV;
  31.  } 
  32.  
  33.  i2c_put_adapter(i2c_adap);
  34.  
  35.   return 0;
  36. }
  37. static void at24cxx_dev_exit(void)
  38. {
  39.  printk( "at24cxx dev of bus-dev-drv module_exit!\n");
  40.  i2c_unregister_device(at24cxx_client);
  41. }
  42. module_init(at24cxx_dev_init);
  43. module_exit(at24cxx_dev_exit);
  44. MODULE_LICENSE( "GPL");

at24cxx_drv.c :


     
     
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/platform_device.h>
  4. #include <linux/i2c.h>
  5. #include <linux/err.h>
  6. #include <linux/regmap.h>
  7. #include <linux/slab.h>
  8. #include <linux/fs.h>
  9. #include <asm/uaccess.h>
  10. static int major;
  11. static struct class *class;
  12. static struct i2c_client *at24cxx_client;
  13. /* 传入: buf[0] : addr, 即将访问I2C设备的地址
  14. * 输出: buf[0] : data
  15. */
  16. static ssize_t at24cxx_read(struct file * file, char __user *buf, size_t count, loff_t *off)
  17. {
  18.   unsigned char addr, data;
  19.  
  20.   if (copy_from_user(&addr, buf, 1)){
  21.   printk( "at24cxx_read: copy_from_user error\n");
  22.    return -EFAULT;
  23.  }
  24. /*
  25.       根据EEPROM读时序,在smbus-protocol文档中找到i2c_smbus_read_byte_data
  26.    函数。
  27. */ 
  28.  data = i2c_smbus_read_byte_data(at24cxx_client, addr);
  29.   if (data < 0) {
  30.   printk( "at24cxx_read: i2c_smbus_read_byte_data error\n");
  31.    return data;
  32.  }
  33.  
  34.   if (copy_to_user(buf, &data, 1)){
  35.   printk( "at24cxx_read: copy_to_user error\n");
  36.    return -EFAULT;
  37.  }
  38.  
  39.   return 1;
  40. }
  41. /* buf[0] : addr, 即将访问I2C设备的地址
  42. * buf[1] : data
  43. */
  44. static ssize_t at24cxx_write(struct file *file, const char __user *buf, size_t count, loff_t *off)
  45. {
  46.   unsigned char ker_buf[ 2];
  47.   unsigned char addr, data;
  48.   if (copy_from_user(ker_buf, buf, 2)){
  49.   printk( "at24cxx_write: copy_from_user error\n");
  50.    return -EFAULT;
  51.  }
  52.  
  53.  addr = ker_buf[ 0];
  54.  data = ker_buf[ 1];
  55.  printk( "addr = 0x%02x, data = 0x%02x\n", addr, data);
  56.   /*
  57.      读写操作函数由核心层提供,有两种方式:
  58.   1. SMBUS协议,系统管理总线,内核文档建议用个方式,原因详见文档<smbus-protocol>
  59.   2. I2C_transfer
  60.      根据smbus-protocol文档,i2c_smbus_write_byte_data函数的时序和
  61.   EEPROM的写操作时序完全一样,所以用它。第一个参数at24cxx_client在
  62.   probe函数中已经做了记录。
  63.  */
  64.   if (!i2c_smbus_write_byte_data(at24cxx_client, addr, data)){
  65.    return 2; //如果写成功,返回写成功字节
  66.  }
  67.   else
  68.  {
  69.   printk( "at24cxx_write: i2c_smbus_write_byte_data error\n");
  70.    return -EIO; 
  71.     }
  72. }
  73. static struct file_operations at24cxx_fops = {
  74.  .owner = THIS_MODULE,
  75.  .read  = at24cxx_read,
  76.  .write = at24cxx_write,
  77. };
  78. static int __ devinit at24cxx_probe(struct i2c_client *client,
  79.       const struct i2c_device_id *id)
  80. {
  81.   struct device *class_dev = NULL;
  82.  
  83.  at24cxx_client = client; //记录i2c_client结构,调用SMBUS方法的时候方便使用
  84.   
  85.   //printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
  86.  major = register_chrdev( 0, "at24cxx", &at24cxx_fops);
  87.   if (major < 0) {
  88.   printk( "at24cxx_probe: can't register at24cxx char device\n");
  89.    return major;
  90.  }
  91.  
  92.   class = class_create(THIS_MODULE, "at24cxx"); //创建类
  93.   if (IS_ERR(class)) {
  94.   printk( "at24cxx_probe: class create failed\n");
  95.   unregister_chrdev(major, "at24cxx");
  96.    return PTR_ERR(class);
  97.  }
  98.  
  99.   /* 在类下面创建设备,自动创建设备节点。device_create会调用那个mdev,
  100.     mdev就会根据环境变量创建设备节点*/
  101.   /* /dev/at24cxx */
  102.  class_dev = device_create(class, NULL,MKDEV(major, 0), NULL, "at24cxx");
  103.   if (IS_ERR(class_dev)) {
  104.   printk( "at24cxx_probe: class device create failed\n");
  105.   unregister_chrdev(major, "at24cxx");
  106.    return PTR_ERR(class_dev);
  107.  }
  108.  
  109.   return 0;
  110. }
  111. static int __ devexit at24cxx_remove(struct i2c_client *client)
  112. {
  113.  printk( "at24cxx drv of bus-dev-drv at24cxx_remove!\n");
  114.  device_destroy(class, MKDEV(major, 0)); //删除设备节点
  115.  class_destroy(class); //摧毁类
  116.  unregister_chrdev(major, "at24cxx");
  117.   
  118.   return 0;
  119. }
  120. static const struct i2c_device_id at24cxx_id_table[] = {
  121.  { "at24c08", 0 }, //用到哪些就声明哪些内容,比如driver_data用不到,所以这里就写0
  122.  {}
  123. };
  124. /* 1. 分配/设置i2c_driver */
  125. static struct i2c_driver at24cxx_driver = {
  126.  .driver = {
  127.   .name = "100ask", //在这里,这个名字并不重要,重要的是id_table里面的名字,所以这里可以随便起
  128.   .owner = THIS_MODULE,
  129.  },
  130.  .probe  = at24cxx_probe,
  131.  .remove  = __devexit_p(at24cxx_remove),
  132.  .id_table = at24cxx_id_table,
  133. };
  134. static int at24cxx_drv_init(void)
  135. {
  136.   int ret;
  137.  
  138.  printk( "at24cxx drv of bus-dev-drv module_init!\n");
  139.  
  140.   /* 2. 注册i2c_driver */
  141.  ret = i2c_add_driver(&at24cxx_driver);
  142.   if (ret != 0){
  143.   pr_err( "Failed to register at24cxx I2C driver: %d\n", ret);
  144.  }
  145.   
  146.   return 0;
  147. }
  148. static void at24cxx_drv_exit(void)
  149. {
  150.  printk( "at24cxx drv of bus-dev-drv module_exit!\n");
  151.  i2c_del_driver(&at24cxx_driver);
  152. }
  153. module_init(at24cxx_drv_init);
  154. module_exit(at24cxx_drv_exit);
  155. MODULE_LICENSE( "GPL");

4th_device_driver_i2c_test.c:


     
     
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. /* i2c_test r addr
  8. * i2c_test w addr val
  9. */
  10. void print_usage(char *file)
  11. {
  12.   printf( "%s r addr\n", file);
  13.   printf( "%s w addr val\n", file);
  14. }
  15. int main(int argc, char **argv)
  16. {
  17.   int fd;
  18.   unsigned char buf[ 2];
  19.  
  20.   if ((argc != 3) && (argc != 4))
  21.  {
  22.   print_usage(argv[ 0]);
  23.    return -1;
  24.  }
  25.  fd = open( "/dev/at24cxx", O_RDWR);
  26.   if (fd < 0)
  27.  {
  28.    printf( "can't open /dev/at24cxx\n");
  29.    return -1;
  30.  }
  31.   if ( strcmp(argv[ 1], "r") == 0)
  32.  {
  33.   buf[ 0] = strtoul(argv[ 2], NULL, 0);
  34.   read(fd, buf, 1);
  35.    printf( "data: %c, %d, 0x%2x\n", buf[ 0], buf[ 0], buf[ 0]);
  36.  }
  37.   else if (( strcmp(argv[ 1], "w") == 0) && (argc == 4))
  38.  {
  39.   buf[ 0] = strtoul(argv[ 2], NULL, 0);
  40.   buf[ 1] = strtoul(argv[ 3], NULL, 0);
  41.    if (write(fd, buf, 2) != 2)
  42.     printf( "write err, addr = 0x%02x, data = 0x%02x\n", buf[ 0], buf[ 1]);
  43.  }
  44.   else
  45.  {
  46.   print_usage(argv[ 0]);
  47.    return -1;
  48.  }
  49.  
  50.   return 0;
  51. }

四、不自己写驱动直接访问

      在图一中,核心层已经提供了统一的I2C设备操作函数:
(1)SMBUS协议方式(SMBUS协议,是I2C协议的子集,某些设备只支持此协议,所以内核文档smbus-protocol建议优先使用此方法)
(2)i2c_transfer方式(I2C协议)
      应用程序可以直接通过上述两种方式访问I2C设备,因为内核已经封装了一套驱动程序,应用程序只需要使用那套驱动就可以访问I2C设备了。应用程序设计方法可以参考文档dev-interface,这里给出测试代码。

Device Drivers
     I2C support
        <*>   I2C device interface

i2c_usr_test.c :


     
     
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include "i2c-dev.h"
  8. /* i2c_usr_test </dev/i2c-0> <dev_addr> r addr
  9. * i2c_usr_test </dev/i2c-0> <dev_addr> w addr val
  10. */
  11. void print_usage(char *file)
  12. {
  13.   printf( "%s </dev/i2c-0> <dev_addr> r addr\n", file);
  14.   printf( "%s </dev/i2c-0> <dev_addr> w addr val\n", file);
  15. }
  16. int main(int argc, char **argv)
  17. {
  18.   int fd;
  19.   unsigned char addr, data;
  20.   int dev_addr;
  21.  
  22.   if ((argc != 5) && (argc != 6))
  23.  {
  24.   print_usage(argv[ 0]);
  25.    return -1;
  26.  }
  27.  fd = open(argv[ 1], O_RDWR);
  28.   if (fd < 0)
  29.  {
  30.    printf( "can't open %s\n", argv[ 1]);
  31.    return -1;
  32.  }
  33.  dev_addr = strtoul(argv[ 2], NULL, 0);
  34.   if (ioctl(fd, I2C_SLAVE, dev_addr) < 0)
  35.  {   
  36.    /* ERROR HANDLING; you can check errno to see what went wrong */   
  37.    printf( "set addr error!\n");
  38.    return -1;
  39.  }
  40.   if ( strcmp(argv[ 3], "r") == 0)
  41.  {
  42.   addr = strtoul(argv[ 4], NULL, 0);
  43.   
  44.   data = i2c_smbus_read_word_data(fd, addr);
  45.    
  46.    printf( "data: %c, %d, 0x%2x\n", data, data, data);
  47.  }
  48.   else if (( strcmp(argv[ 3], "w") == 0) && (argc == 6))
  49.  {
  50.   addr = strtoul(argv[ 4], NULL, 0);
  51.   data = strtoul(argv[ 5], NULL, 0);
  52.   i2c_smbus_write_byte_data(fd, addr, data);  
  53.  }
  54.   else
  55.  {
  56.   print_usage(argv[ 0]);
  57.    return -1;
  58.  }
  59.  
  60.   return 0;
  61. }

五、编写"总线(适配器adapter)"驱动

       应用程序调用设备驱动程序,设备驱动程序会调用核心层提供的某些函数(如SMBUS相关函数),核心层的这些函数最终会调用到适配器层的相关函数,这些函数是处理器I2C接口的相关硬件操作,它们会根据I2C协议向I2C设备发出相应信号达到控制I2C设备的目的。
      这里设计的驱动即设计适配器驱动程序,先找到Linux3.5中内核自带的适配器驱动。
在make menuconfig后,找到如下选项:
Device Drivers
     I2C support
         I2C Hardware Bus support
             < > S3C2410 I2C Driver
       选中S3C2410 I2C Driver,按下键盘上的h键,找到第一行出现的那个宏 CONFIG_I2C_S3C2410 , 然后在内核源码目录下执行 grep "CONFIG_I2C_S3C2410" -R *后,找到如下信息:
drivers/i2c/busses/Makefile:obj-$(CONFIG_I2C_S3C2410)   += i2c-s3c2410.o
      从上面信息中就可以知道I2C总线适配器对应的驱动程序文件为i2c-s3c2410.c文件。然后分析此文件,分析类似驱动程序都是先从入口函数看。下面给出测试代码(基于2440):


     
     
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/i2c.h>
  4. #include <linux/init.h>
  5. #include <linux/time.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/delay.h>
  8. #include <linux/errno.h>
  9. #include <linux/err.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/pm_runtime.h>
  12. #include <linux/clk.h>
  13. #include <linux/cpufreq.h>
  14. #include <linux/slab.h>
  15. #include <linux/io.h>
  16. #include <linux/of_i2c.h>
  17. #include <linux/of_gpio.h>
  18. #include <plat/gpio-cfg.h>
  19. #include <mach/regs-gpio.h>
  20. #include <asm/irq.h>
  21. #include <plat/regs-iic.h>
  22. #include <plat/iic.h>
  23. //#define PRINTK printk
  24. #define PRINTK(...)
  25. enum s3c24xx_i2c_state {
  26.  STATE_IDLE,
  27.  STATE_START,
  28.  STATE_READ,
  29.  STATE_WRITE,
  30.  STATE_STOP
  31. };
  32. struct s3c2440_i2c_regs {
  33.   unsigned int iiccon;
  34.   unsigned int iicstat;
  35.   unsigned int iicadd;
  36.   unsigned int iicds;
  37.   unsigned int iiclc;
  38. };
  39. struct s3c2440_i2c_xfer_data {
  40.   struct i2c_msg *msgs;
  41.   int msn_num;
  42.   int cur_msg;
  43.   int cur_ptr;
  44.   int state;
  45.   int err;
  46.   wait_queue_head_t wait;
  47. };
  48. static struct s3c2440_i2c_xfer_data s3c2440_i2c_xfer_data;
  49. static struct s3c2440_i2c_regs *s3c2440_i2c_regs;
  50. static void s3c2440_i2c_start(void)
  51. {
  52.  s3c2440_i2c_xfer_data.state = STATE_START;
  53.  
  54.   if (s3c2440_i2c_xfer_data.msgs->flags & I2C_M_RD) /* 读 */
  55.  {
  56.   s3c2440_i2c_regs->iicds   = s3c2440_i2c_xfer_data.msgs->addr << 1;
  57.   s3c2440_i2c_regs->iicstat   = 0xb0// 主机接收,启动
  58.  }
  59.   else /* 写 */
  60.  {
  61.   s3c2440_i2c_regs->iicds   = s3c2440_i2c_xfer_data.msgs->addr << 1;
  62.   s3c2440_i2c_regs->iicstat    = 0xf0;    // 主机发送,启动
  63.  }
  64. }
  65. static void s3c2440_i2c_stop(int err)
  66. {
  67.  s3c2440_i2c_xfer_data.state = STATE_STOP;
  68.  s3c2440_i2c_xfer_data.err   = err;
  69.  PRINTK( "STATE_STOP, err = %d\n", err);
  70.   if (s3c2440_i2c_xfer_data.msgs->flags & I2C_M_RD) /* 读 */
  71.  {
  72.    // 下面两行恢复I2C操作,发出P信号
  73.   s3c2440_i2c_regs->iicstat = 0x90;
  74.   s3c2440_i2c_regs->iiccon  = 0xaf;
  75.   ndelay( 50);  // 等待一段时间以便P信号已经发出
  76.  }
  77.   else /* 写 */
  78.  {
  79.    // 下面两行用来恢复I2C操作,发出P信号
  80.   s3c2440_i2c_regs->iicstat = 0xd0;
  81.   s3c2440_i2c_regs->iiccon  = 0xaf;
  82.   ndelay( 50);  // 等待一段时间以便P信号已经发出
  83.  }
  84.   /* 唤醒 */
  85.  wake_up(&s3c2440_i2c_xfer_data.wait);
  86.  
  87. }
  88. static int s3c2440_i2c_xfer(struct i2c_adapter *adap,
  89.    struct i2c_msg *msgs, int num)
  90. {
  91.   unsigned long timeout;
  92.  
  93.   /* 把num个msg的I2C数据发送出去/读进来 */
  94.  s3c2440_i2c_xfer_data.msgs    = msgs;
  95.  s3c2440_i2c_xfer_data.msn_num = num;
  96.  s3c2440_i2c_xfer_data.cur_msg = 0;
  97.  s3c2440_i2c_xfer_data.cur_ptr = 0;
  98.  s3c2440_i2c_xfer_data.err     = -ENODEV;
  99.  s3c2440_i2c_start();
  100.   /* 休眠 */
  101.  timeout = wait_event_timeout(s3c2440_i2c_xfer_data.wait, (s3c2440_i2c_xfer_data.state == STATE_STOP), HZ * 5);
  102.   if ( 0 == timeout)
  103.  {
  104.   printk( "s3c2440_i2c_xfer time out\n");
  105.    return -ETIMEDOUT;
  106.  }
  107.   else
  108.  {
  109.    return s3c2440_i2c_xfer_data.err;
  110.  }
  111. }
  112. static u32 s3c2440_i2c_func(struct i2c_adapter *adap)
  113. {
  114.   return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_PROTOCOL_MANGLING;
  115. }
  116. static const struct i2c_algorithm s3c2440_i2c_algo = {
  117. // .smbus_xfer     = ,
  118.  .master_xfer = s3c2440_i2c_xfer,
  119.  .functionality = s3c2440_i2c_func,
  120. };
  121. /* 1. 分配/设置i2c_adapter
  122. */
  123. static struct i2c_adapter s3c2440_i2c_adapter = {
  124. .name    = "s3c2440_100ask",
  125. .algo    = &s3c2440_i2c_algo,
  126. .owner    = THIS_MODULE,
  127. };
  128. static int isLastMsg(void)
  129. {
  130.   return (s3c2440_i2c_xfer_data.cur_msg == s3c2440_i2c_xfer_data.msn_num - 1);
  131. }
  132. static int isEndData(void)
  133. {
  134.   return (s3c2440_i2c_xfer_data.cur_ptr >= s3c2440_i2c_xfer_data.msgs->len);
  135. }
  136. static int isLastData(void)
  137. {
  138.   return (s3c2440_i2c_xfer_data.cur_ptr == s3c2440_i2c_xfer_data.msgs->len - 1);
  139. }
  140. static irqreturn_t s3c2440_i2c_xfer_irq(int irq, void *dev_id)
  141. {
  142.   unsigned int iicSt;
  143.     iicSt  = s3c2440_i2c_regs->iicstat;
  144.   if(iicSt & 0x8){ printk( "Bus arbitration failed\n\r"); }
  145.   switch (s3c2440_i2c_xfer_data.state)
  146.  {
  147.    case STATE_START : /* 发出S和设备地址后,产生中断 */
  148.   {
  149.    PRINTK( "Start\n");
  150.     /* 如果没有ACK, 返回错误 */
  151.     if (iicSt & S3C2410_IICSTAT_LASTBIT)
  152.    {
  153.     s3c2440_i2c_stop(-ENODEV);
  154.      break;
  155.    }
  156.     if (isLastMsg() && isEndData())
  157.    {
  158.     s3c2440_i2c_stop( 0);
  159.      break;
  160.    }
  161.     /* 进入下一个状态 */
  162.     if (s3c2440_i2c_xfer_data.msgs->flags & I2C_M_RD) /* 读 */
  163.    {
  164.     s3c2440_i2c_xfer_data.state = STATE_READ;
  165.      goto next_read;
  166.    }
  167.     else
  168.    {
  169.     s3c2440_i2c_xfer_data.state = STATE_WRITE;
  170.    } 
  171.   }
  172.    case STATE_WRITE:
  173.   {
  174.    PRINTK( "STATE_WRITE\n");
  175.     /* 如果没有ACK, 返回错误 */
  176.     if (iicSt & S3C2410_IICSTAT_LASTBIT)
  177.    {
  178.     s3c2440_i2c_stop(-ENODEV);
  179.      break;
  180.    }
  181.     if (!isEndData())  /* 如果当前msg还有数据要发送 */
  182.    {
  183.     s3c2440_i2c_regs->iicds = s3c2440_i2c_xfer_data.msgs->buf[s3c2440_i2c_xfer_data.cur_ptr];
  184.     s3c2440_i2c_xfer_data.cur_ptr++;
  185.     
  186.      // 将数据写入IICDS后,需要一段时间才能出现在SDA线上
  187.     ndelay( 50); 
  188.     
  189.     s3c2440_i2c_regs->iiccon = 0xaf;   // 恢复I2C传输
  190.      break;    
  191.    }
  192.     else if (!isLastMsg())
  193.    {
  194.      /* 开始处理下一个消息 */
  195.     s3c2440_i2c_xfer_data.msgs++;
  196.     s3c2440_i2c_xfer_data.cur_msg++;
  197.     s3c2440_i2c_xfer_data.cur_ptr = 0;
  198.     s3c2440_i2c_xfer_data.state = STATE_START;
  199.      /* 发出START信号和发出设备地址 */
  200.     s3c2440_i2c_start();
  201.      break;
  202.    }
  203.     else
  204.    {
  205.      /* 是最后一个消息的最后一个数据 */
  206.     s3c2440_i2c_stop( 0);
  207.      break;    
  208.    }
  209.     break;
  210.   }
  211.    case STATE_READ:
  212.   {
  213.    PRINTK( "STATE_READ\n");
  214.     /* 读出数据 */
  215.    s3c2440_i2c_xfer_data.msgs->buf[s3c2440_i2c_xfer_data.cur_ptr] = s3c2440_i2c_regs->iicds;   
  216.    s3c2440_i2c_xfer_data.cur_ptr++;
  217. next_read:
  218.     if (!isEndData()) /* 如果数据没读写, 继续发起读操作 */
  219.    {
  220.      if (isLastData())  /* 如果即将读的数据是最后一个, 不发ack */
  221.     {
  222.      s3c2440_i2c_regs->iiccon = 0x2f;   // 恢复I2C传输,接收到下一数据时无ACK
  223.     }
  224.      else
  225.     {
  226.      s3c2440_i2c_regs->iiccon = 0xaf;   // 恢复I2C传输,接收到下一数据时发出ACK
  227.     }    
  228.      break;
  229.    }
  230.     else if (!isLastMsg())
  231.    {
  232.      /* 开始处理下一个消息 */
  233.     s3c2440_i2c_xfer_data.msgs++;
  234.     s3c2440_i2c_xfer_data.cur_msg++;
  235.     s3c2440_i2c_xfer_data.cur_ptr = 0;
  236.     s3c2440_i2c_xfer_data.state = STATE_START;
  237.      /* 发出START信号和发出设备地址 */
  238.     s3c2440_i2c_start();
  239.      break;
  240.    }
  241.     else
  242.    {
  243.      /* 是最后一个消息的最后一个数据 */
  244.     s3c2440_i2c_stop( 0);
  245.      break;        
  246.    }
  247.     break;
  248.   }
  249.    default: break;
  250.  }
  251.   /* 清中断 */
  252.  s3c2440_i2c_regs->iiccon &= ~(S3C2410_IICCON_IRQPEND);
  253.   return IRQ_HANDLED; 
  254. }
  255. /*
  256. * I2C初始化
  257. */
  258. static void s3c2440_i2c_init(void)
  259. {
  260.   struct clk *clk;
  261.  clk = clk_get( NULL, "i2c");
  262.  clk_enable(clk);
  263.  
  264.     // 选择引脚功能:GPE15:IICSDA, GPE14:IICSCL
  265.     s3c_gpio_cfgpin(S3C2410_GPE( 14), S3C2410_GPE14_IICSCL);
  266.  s3c_gpio_cfgpin(S3C2410_GPE( 15), S3C2410_GPE15_IICSDA);
  267.     /* bit[7] = 1, 使能ACK
  268.      * bit[6] = 0, IICCLK = PCLK/16
  269.      * bit[5] = 1, 使能中断
  270.      * bit[3:0] = 0xf, Tx clock = IICCLK/16
  271.      * PCLK = 50MHz, IICCLK = 3.125MHz, Tx Clock = 0.195MHz
  272.      */
  273.     s3c2440_i2c_regs->iiccon = ( 1<< 7) | ( 0<< 6) | ( 1<< 5) | ( 0xf);  // 0xaf
  274.     s3c2440_i2c_regs->iicadd  = 0x10;     // S3C24xx slave address = [7:1]
  275.     s3c2440_i2c_regs->iicstat = 0x10;     // I2C串行输出使能(Rx/Tx)
  276. }
  277. static int i2c_bus_s3c2440_init(void)
  278. {
  279.   /* 2. 硬件相关的设置 */
  280.  s3c2440_i2c_regs = ioremap( 0x54000000, sizeof(struct s3c2440_i2c_regs));
  281.  
  282.  s3c2440_i2c_init();
  283.  request_irq(IRQ_IIC, s3c2440_i2c_xfer_irq, 0, "s3c2440-i2c", NULL);
  284.  init_waitqueue_head(&s3c2440_i2c_xfer_data.wait);
  285.  
  286.   /* 3. 注册i2c_adapter */
  287.  i2c_add_adapter(&s3c2440_i2c_adapter);
  288.  
  289.   return 0;
  290. }
  291. static void i2c_bus_s3c2440_exit(void)
  292. {
  293.  i2c_del_adapter(&s3c2440_i2c_adapter); 
  294.  free_irq(IRQ_IIC, NULL);
  295.  iounmap(s3c2440_i2c_regs);
  296. }
  297. module_init(i2c_bus_s3c2440_init);
  298. module_exit(i2c_bus_s3c2440_exit);
  299. MODULE_LICENSE( "GPL");

六、总结

      通过上述分析,可以总结出一个设计驱动程序的方法,这个方法适用于Linux内核的各个版本:

1.  如果要设计I2C设备驱动,看 Documentation/i2c目录下的相关内核文档;I2C协议;处理器的I2C接口;掌握bus-dev-drv模型;掌握驱动程序设计相关知识。

2.  实现bus-dev-drv模型驱动。比如要先注册i2c_client,而内核文档中instantiating-devices(构造设备)文件就说明了方法。然后根据方法,搜索内核中对应的例子,模仿就可以        设计出需要的驱动;然后要注册i2c_driver,也是模仿其他如何设计即可。总之,Linux是非常庞大的系统,里面有很多例子可以参考的。

3. 理清思路,搞清楚框架中哪些工作是需要做的,哪些是不需要做的。


    《smbus-protocol》中介绍了核心层各种读写接口,这些接口被设备驱动的读写接口调用。根据此文档,下面举例说明这些接口的应用:


SMBus Read Byte:  i2c_smbus_read_byte_data()
============================================

This reads a single byte from a device, from a designated register.
The register is specified through the Comm byte.

S Addr Wr [A] Comm [A] S Addr Rd [A] [Data] NA P

   

      i2c_smbus_read_byte_data函数在Linux内核中原型如下:

s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
{
    union i2c_smbus_data data;
    int status;

    status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
                I2C_SMBUS_READ, command,
                I2C_SMBUS_BYTE_DATA, &data);
    return (status < 0) ? status : data.byte;
}

      从字面意思上就可以猜测i2c_smbus_read_byte_data函数是从I2C的子集smbus上读取一个字节的数据,它的第一个参数为i2c_client结构体,这个结构在probe函数被调用后就被记录了(记录方法就是用一个i2c_client结构体指针指向probe函数的参数),第二个参数的含义是明确读取寄存器的地址,比如用户要读取EEPROM地址为1地方的数据,那么这里就把1传给内核空间并传给此函数。i2c_smbus_read_byte_data函数的返回值即从它的第二个参数Comm为地址的地方读出的数据。

     然而,对于4412上的I2C总线0上接的EEPROM设备来说,为什么使用这个函数呢?原因在于这个函数的时序和EEPROM芯片手册中读时序类似,下图是EEPROM读时序:


       根据上图,对照smbus-protocol文档中对i2c_smbus_read_byte_data()函数的介绍,即S Addr Wr [A] Comm [A] S Addr Rd [A] [Data] NA P,下面是详细说明:

       第一个为S表示起始信号;

       第二个为Addr表示I2C设备的地址,这里就是EEPOM的地址0x50;

       第三个为Wr表示操作方向为写;

       第四个为[A]表示EERPOM给处理器I2C控制器的回应,实际上就是把SDA拉低而已;

       第五个为Comm,写地址。表示要读取EEPROM中哪个地方的数据,是个地址,也是i2c_smbus_read_byte_data函数的第二个参数,

                     一般由用户空间提供;

       第六个为[A]表示EERPOM给处理器I2C控制器的回应,实际上就是把SDA拉低而已;

       第七个为S表示重新发送起始信号;

       第八个为Addr表示I2C设备的地址,这里就是EEPOM的地址0x50;

       第九个为Rd表示操作方向为读;

       第十个为[A]表示EERPOM给处理器I2C控制器的回应,实际上就是把SDA拉低而已;

       第十一个为[Data],表示从EEPROM中Comm地址处读取的数据;

       第十二个为NA,表示上述读取的数据为最后一个数据,这个时候处理器不用给eeprom回应,即NO ACK;    

       第十三个为P,表示由主机给出的停止信号,结束对EEPROM的操作。


















           
               
         
      
                          
               

知识背景:

1. I2C协议

2. 4412处理器I2C接口说明

3. bus-dev-drv模型(详见文章-Linux下驱动:分层、分离机制学习笔记)

4. linux内核下驱动设计基本知识


一、驱动框架

      以4412+linux3.5平台为例,说明Linux下I2C设备驱动程序。

      如果一条I2C总线上只连接一个I2C设备,那么只需要以字符型设备驱动框架来写驱动即可(填充file_opreoration结构体中的各个成员),那是典型的字符型设备驱动程序。

      实际上,一款处理器可能有多条I2C总线,多个I2C控制器,每条总线上可能挂接多个设备,按照典型的字符型驱动框架写驱动就要为每个这样的设备写一个驱动程序,而且要做很多重复性的工作。

      所以,有很多东西可以抽象的就抽象出来,实现一种框架,这种框架模型还是bus-dev-drv模型。4412处理器有8条可用I2C总线,每条总线上挂接的每个I2C设备都明白它们跟4412通信数据的含义。4412访问I2C设备时,都需要发出Start信号,都需要发出设备地址等等,这些共性的东西就可以抽象出来用一些函数实现,这些函数就是在另外一层即核心层驱动程序。

      核心层驱动程序提供统一的I2C设备操作函数,比如读写I2C设备的操作函数;还有设备驱动层(file_opreoration结构体相关);另外一层就是适配层,这一层是处理器的每个I2C适配器(也叫控制器)的驱动,适配层驱动程序提供统一的处理器I2C接口硬件操作函数。

       应用程序需要操作I2C设备时,根据设备节点调用读写函数(系统调用),然后就会执行设备驱动层的相应读写函数(它们是file_opreoration结构体的成员),这些读写函数里会调用核心层提供的统一的I2C设备读写操作函数,这些函数最终会通过适配器(I2C控制器)给连接在I2C总线上的I2C设备发送相应读写命令。适配器那一层驱动程序就是些I2C控制器的硬件操作,它是根据I2C协议来的(这里根据的I2C协议往往是硬件已经做好了的,软件只需要配置CPU的I2C控制器的某个寄存器即可实现I2C协议),是最底层,当I2C控制器的某个寄存器被控制后,I2C控制器就会自动地往I2C线上根据I2C协议发出相应信号与2IC设备通信。

       由此可见,写设备驱动程序即实现设备驱动层时,只要弄清楚bus-dev-drv驱动模型,(调用了probe函数后)剩下的就是典型的字符型设备驱动那一套了(构建file_opreoration结构体、分配主次设备号、创建设备节点),file_opreoration的读写函数调用的方法也由核心层提供。图一。


      上述三层是通过总线-设备-驱动模型融合到一起的。Linux内核中构建了许多总线,但并不是都是真实的,比如平台总线就是虚拟的,平台总线中也有设备链表,驱动链表。

      针对I2C总线,也有一个I2C总线的结构即i2c_bus_type结构,此结构里面也有设备链表和也有驱动链表。设备链表里存放i2c_client的结构体,这些结构体是注册i2c_client时加入的,不但要加入这些结构体,还会在总线的驱动链表中一个一个地比较drv即i2c_driver来判断是否有匹配的,如果有将调用drv里面的probe函数,匹配函数由总线提供;驱动链表里存放i2c_driver结构体,这些结构体是注册i2c_driver时加入的,不但要加入这些结构体,还会在总线的设备链表中一个一个地比较dev即比较i2c_client来判断是否有匹配的,如果匹配将调用drv里面的probe函数。

     上述的匹配函数就是i2c_bus_type结构里面的i2c_device_match函数。i2c_device_match函数通过id_table(i2c_driver结构的成员,它表示能支持哪些设备)来比较dev与drv是否匹配,具体方法是用id_table的name去比较,name分别是i2c_client结构和i2c_driver结构的成员。如果名字相同,就表示此驱动i2c_driver能支持这个设备i2c_client。

      总的说来,I2C基于bus-dev-drv模型的构建过程如下:

(1)左边注册一个设备,i2c_client

(2)右边注册一个驱动,i2c_driver

(3)比较它们的名字,如果相同,则调用driver的probe函数。

(4)probe函数里面做的事情由用户决定(比如注册、构建设备节点等,这些属于设备驱动)。


二、设置和注册i2c_client结构体(EEPROM为例)

      在Linux内核文档(/Documentation/i2c/instantiating-devices)中,介绍了注册即构造一个i2c_client的4种方法,并且建议使用如下前三种方法,后一种较复杂,万不得已才用。

1.  通过总线号声明一个I2C设备

      通过总线号声明一个I2C设备即构造一个i2c_board_info结构体,它里面有名字和地址,名字可以用来找到drv结构,地址可以用来访问哪个I2C设备,还可以放其他信息,这些信息会被将来的probe函数用到,也就是说在probe函数中要使用到哪些信息,这里面就可以增加哪些信息。然后使用i2c_register_board_info函数注册这个结构体,此函数的第一个参数即总线号,表示此设备属于哪条总线(这就是标题通过总线号声明一个i2c设备的含义),在此函数中会把此结构体放入链表__i2c_board_list

      然后在i2c_scan_static_board_info函数中使用到__i2c_board_list链表,即调用i2c_new_device函数把链表中的每个成员构造成一个i2c_client,并放入bus-dev-drv模型中总线中设备链表中去。在i2c_new_device函数中,分配一个i2c_client结构体后,设置它,并调用device_register函数注册,此函数最终会调用前面文章中提到device_add函数。i2c_scan_static_board_info函数是被i2c_register_adapter函数调用的,所以这里总的过程为i2c_register_adapter  > i2c_scan_static_board_info > i2c_new_device。

     ft5x06_ts类型的I2C触摸屏驱动中就是使用的这种方法。在内核文件arch/arm/mach-exynos/mach-tiny4412.c中,对应触摸屏的i2c_register_board_info函数调用过程如下:

MACHINE_START(TINY4412, "TINY4412")
    .xxx
    .init_machine    = smdk4x12_machine_init,
    .xxx
MACHINE_END
                                 ↓

         调用smdk4x12_machine_init函数

                                 ↓

调用i2c_register_board_info(1, smdk4x12_i2c_devs1,ARRAY_SIZE(smdk4x12_i2c_devs1));

       可见,上述i2c_register_board_info函数的第一个参数为1,表示ft5x06_ts类型的I2C触摸屏挂接在处理器的I2C1那条总线上。


这种方法使用限制:在注册I2C适配器之前注册i2c_board_info结构体,即必须在 i2c_register_adapter 之前 i2c_register_board_info,所以不适合动态加载insmod。


2.  直接创建设备(直接调用i2c_new_device、i2c_new_probed_device)

(1)i2c_new_device方法

      第一种方法显得有些麻烦,这里就直接调用 i2c_new_device或i2c_new_probed_device函数实现。

      i2c_new_device函数总共有两个参数,第一个为要指定的适配器i2c_adapter(一个用来标识物理I2C总线结构,即用哪个I2C控制器发出I2C信号,某些CPU有多个I2C适配器),即要把i2c设备跟哪个适配器相连,这样以后在访问I2C设备时,就知道使用哪个适配器的操作函数了。第二个参数是用来描述I2C设备的相关信息的,即i2c_board_info结构体。所以这种方法需要在dev程序中定义i2c_adapter和i2c_board_info结构,i2c_board_info结构可以直接在dev程序中定义,i2c_adapter创建的代码如下:

    struct   i2c_adapter *i2c_adap;
    i2c_adap = i2c_get_adapter(0);
    at24cxx_client = i2c_new_device(i2c_adap, &at24cxx_info);
    i2c_put_adapter(i2c_adap);

     先定义一个i2c_adapter结构指针存放i2c_get_adapter(0)的返回值,此函数的参数为0,表示第0条总线,i2c_new_device调用完后要调用  i2c_put_adapter函数。通过这样的方式,在第0条总线下创建了一个新设备,以后就可以使用i2c_adap这个i2c_adapter的操作函数发出I2C的信号了,比如起始信号、停止信号等。实验代码如下:

at24cxx_dev.c :


  
  
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/platform_device.h>
  4. #include <linux/i2c.h>
  5. #include <linux/err.h>
  6. #include <linux/regmap.h>
  7. #include <linux/slab.h>
  8. //0x50表示I2C设备的地址,一般在 I2C设备芯片手册可以查到
  9. static struct i2c_board_info at24cxx_info = { 
  10.  I2C_BOARD_INFO( "at24c08", 0x50), //这个名字要和drv程序中的id_table中名字要一样
  11. };
  12. static struct i2c_client *at24cxx_client;
  13. static int at24cxx_dev_init(void)
  14. {
  15.   struct i2c_adapter *i2c_adap;
  16.  i2c_adap = i2c_get_adapter( 0); //这里要实验的EEPROM是挂接在第0条I2C总线上的,所以这里的参数是0
  17.  at24cxx_client = i2c_new_device(i2c_adap, &at24cxx_info);
  18.  i2c_put_adapter(i2c_adap);
  19.  
  20.   return 0;
  21. }
  22. static void at24cxx_dev_exit(void)
  23. {
  24.  i2c_unregister_device(at24cxx_client);
  25. }
  26. module_init(at24cxx_dev_init);
  27. module_exit(at24cxx_dev_exit);
  28. MODULE_LICENSE( "GPL");
at24cxx_drv.c :

  
  
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/platform_device.h>
  4. #include <linux/i2c.h>
  5. #include <linux/err.h>
  6. #include <linux/regmap.h>
  7. #include <linux/slab.h>
  8. static int __ devinit at24cxx_probe(struct i2c_client *client,
  9.       const struct i2c_device_id *id)
  10. {
  11.  printk( "%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
  12.   return 0;
  13. }
  14. static int __ devexit at24cxx_remove(struct i2c_client *client)
  15. {
  16.  printk( "%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
  17.   return 0;
  18. }
  19. static const struct i2c_device_id at24cxx_id_table[] = {
  20.  { "at24c08", 0 }, //用到哪些就声明哪些内容,比如driver_data用不到,所以这里就写0
  21.  {}
  22. };
  23. /* 1. 分配/设置i2c_driver */
  24. static struct i2c_driver at24cxx_driver = {
  25.  .driver = {
  26.   .name = "100ask", //在这里,这个名字并不重要,重要的是id_table里面的名字,所以这里可以随便起
  27.   .owner = THIS_MODULE,
  28.  },
  29.  .probe  = at24cxx_probe,
  30.  .remove  = __devexit_p(at24cxx_remove),
  31.  .id_table = at24cxx_id_table,
  32. };
  33. static int at24cxx_drv_init(void)
  34. {
  35.   /* 2. 注册i2c_driver */
  36.  i2c_add_driver(&at24cxx_driver); //实际使用时一定要判断返回值
  37.  
  38.   return 0;
  39. }
  40. static void at24cxx_drv_exit(void)
  41. {
  42.  i2c_del_driver(&at24cxx_driver);
  43. }
  44. module_init(at24cxx_drv_init);
  45. module_exit(at24cxx_drv_exit);
  46. MODULE_LICENSE( "GPL");<span style= "display: none; width: 0px; height: 0px;" id= "transmark"></span>

Makefile :


  
  
  1. KERN_DIR = /home/samba/linuxKernel_ext4Fs_src/linux -3.5 -2015 -8
  2. all:
  3.  make -C $(KERN_DIR) M=`pwd` modules
  4. clean:
  5.  make -C $(KERN_DIR) M=`pwd` modules clean
  6.  rm -rf modules.order
  7. obj-m += at24cxx_dev.o
  8. obj-m += at24cxx_drv.o
 
 

      分别加载at24cxx_dev.ko和at24cxx_drv.ko,at24cxx_probe函数会被调用,随意修改i2c_board_info结构里面的地址,at24cxx_probe函数照样被调用。所以这种方法不会去真正地验证连接的I2C设备的地址是否为i2c_board_info结构里面的地址。

(2) i2c_new_probed_device方法

     i2c_new_device : 认为设备肯定存在,实验时可以用这种方法改掉I2C设备的地址,改成任意值都是可以的。     i2c_new_probed_device :对于"已经识别出来的设备"(probed_device),才会创建("new")     i2c_new_probed_device函数主要做如下三件事:                probe(adap, addr_list[i])   /* 如果i2c_new_probed_device最后个参数中没有指定probe函数,将使用默认probe函数。不管哪个,它都要确定设备是否真实存在 */               info->addr = addr_list[i];   /* 如果在addr_list中找到了一个地址和现实中连接在I2C总线上的设备匹配,将这个地址放入i2c_board_info结构,并传给i2c_new_device */                i2c_new_device(adap, info);     为了实验,分别编译如下代码,然后分别加载at24cxx_dev.ko和at24cxx_drv.ko,如果在addr_list数组中有一个地址是I2C总线上设备的地址,那么在加载at24cxx_dev.ko驱动模块时,能加载成功,并且加载at24cxx_drv.ko模块后,将调用drv的probe函数。如果没有那个地址,那么在加载at24cxx_dev.ko驱动模块时会失败,提示如下信息:

insmod: can't insert 'at24cxx_dev.ko': No such device

     这就是直接使用i2c_new_device和使用i2c_new_probed_device创建i2c_client的区别。

at24cxx_dev.c :


  
  
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/platform_device.h>
  4. #include <linux/i2c.h>
  5. #include <linux/err.h>
  6. #include <linux/regmap.h>
  7. #include <linux/slab.h>
  8. static struct i2c_client *at24cxx_client;
  9. //如果挂接在I2C总线上的i2c设备的地址在此数组里面都找不到,将不能加载此驱动模块
  10. static const unsigned short addr_list[] = { 0x60, 0x50,I2C_CLIENT_END };
  11. static int at24cxx_dev_init(void)
  12. {
  13.   struct i2c_adapter *i2c_adap;
  14.   struct i2c_board_info at24cxx_info;
  15.   memset(&at24cxx_info, 0, sizeof(struct i2c_board_info)); 
  16.  strlcpy(at24cxx_info.type, "at24c08", I2C_NAME_SIZE);
  17.  i2c_adap = i2c_get_adapter( 0);
  18.  at24cxx_client = i2c_new_probed_device(i2c_adap, &at24cxx_info, addr_list, NULL);
  19.  i2c_put_adapter(i2c_adap);
  20.   if (at24cxx_client)
  21.    return 0;
  22.   else
  23.    return -ENODEV;
  24. }
  25. static void at24cxx_dev_exit(void)
  26. {
  27.  i2c_unregister_device(at24cxx_client);
  28. }
  29. module_init(at24cxx_dev_init);
  30. module_exit(at24cxx_dev_exit);
  31. MODULE_LICENSE( "GPL");

at24cxx_drv.c :


  
  
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/platform_device.h>
  4. #include <linux/i2c.h>
  5. #include <linux/err.h>
  6. #include <linux/regmap.h>
  7. #include <linux/slab.h>
  8. static int __ devinit at24cxx_probe(struct i2c_client *client,
  9.                        const struct i2c_device_id *id)
  10. {
  11.  printk( "%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
  12.   return 0;
  13. }
  14. static int __ devexit at24cxx_remove(struct i2c_client *client)
  15. {
  16.  printk( "%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
  17.   return 0;
  18. }
  19. static const struct i2c_device_id at24cxx_id_table[] = {
  20.  { "at24c08", 0 }, //用到哪些就声明哪些内容,比如driver_data用不到,所以这里就写0
  21.  {}
  22. };
  23. /* 1. 分配/设置i2c_driver */
  24. static struct i2c_driver at24cxx_driver = {
  25.  .driver = {
  26.   .name = "100ask", //在这里,这个名字并不重要,重要的是id_table里面的名字,所以这里可以随便起
  27.   .owner = THIS_MODULE,
  28.  },
  29.  .probe  = at24cxx_probe,
  30.  .remove  = __devexit_p(at24cxx_remove),
  31.  .id_table = at24cxx_id_table,
  32. };
  33. static int at24cxx_drv_init(void)
  34. {
  35.   /* 2. 注册i2c_driver */
  36.  i2c_add_driver(&at24cxx_driver); //一定要判断返回值
  37.  
  38.   return 0;
  39. }
  40. static void at24cxx_drv_exit(void)
  41. {
  42.  i2c_del_driver(&at24cxx_driver);
  43. }
  44. module_init(at24cxx_drv_init);
  45. module_exit(at24cxx_drv_exit);
  46. MODULE_LICENSE( "GPL");

3.  从用户空间创建设备(详细阅读/Documentation/i2c/instantiating-devices文档)

      执行命令cd /sys/class/i2c-adapter/,可以看到内容i2c-0  i2c-1  i2c-2  i2c-3  i2c-7  i2c-8,说明有多款适配器,即多个I2C控制器,即多条I2C总线。其中EEPROM是挂接在I2C-0下面的(看板子原理图)。
     < 做下面实验需要把内核中静态编译进的drv驱动给去掉,然后加载自己的drv驱动>

创建设备
       echo at24c08 0x50 > /sys/class/i2c-adapter/i2c-0/new_device,导致i2c_new_device被调用,最后drv里的probe函数就不会被调用。如果把地址改为0x51,那么也会在bus的                                                                                                            dev链表中增加一个dev结构,所以这种方法也是不会判断地址是否正确。

删除设备
       echo 0x50 > /sys/class/i2c-adapter/i2c-0/delete_device,导致i2c_unregister_device。       

4.  注册设置i2c_client的第四种方法(此方法交复杂,前三种都不行时才用)

     上述三种方法都是知道I2C设备属于哪个适配器,即知道连在了哪条I2C总线上,如果不知道属于哪个适配器的情况下(4412有多个I2C适配器)就需要用本方法,本方法可以参考例子/drivers/hwmon/lm90.c。

     如果事先并不知道这个I2C设备在哪个适配器上,怎么办?去class表示的所有的适配器上查找。
     有些I2C设备的地址是一样,怎么继续区分它是哪一款?用detect函数。

     此方法过程:   
     由于事先并不知道I2C设备在哪个适配器上,所以去"class表示的那一类"I2C适配器上找,用"detect函数"来确定能否找到"address_list里的设备",如果能找到就调用i2c_new_device来注册i2c_client, 这会和i2c_driver的id_table比较,如果匹配,调用probe。      

     详细代码调用过程:
i2c_add_driver
    i2c_register_driver
        a. at24cxx_driver放入i2c_bus_type的drv链表
           并且从dev链表里取出能匹配的i2c_client并调用probe
           driver_register
           
        b. 对于每一个适配器,调用__process_new_driver(在i2c_bus_type的dev链表中不但要挂i2c_client外,还会挂i2c_adpter。当drv和dev链表比较的时候,drv不会跟i2c_adpter比较,只会跟i2c_client比较,因为i2c_adpter的.type成员可以用来分辨是i2c_adpter还是i2c_client)。
           
           对于每一个适配器,调用它的函数确定address_list里的设备是否存在(确定的方法是给I2C设备发一个地址,看它是否回应ACK,即SDA是否被拉低),即是否支持这个设备。如果存在,再调用detect进一步确定、设置以确定是哪类设备,因为有些设备地址一样,单从地址是没办法分辨是哪类设备的(详细可以阅读内核文档
           /Documentation/i2c/instantiating-devices)。然后i2c_new_device
        /* Walk the adapters that are already present */
        i2c_for_each_dev(driver, __process_new_driver);
            __process_new_driver
                i2c_do_add_adapter
                    /* Detect supported devices on that bus, and instantiate them */
                    i2c_detect(adap, driver);
                        for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
                            err = i2c_detect_address(temp_client, driver);
                                        /* 判断这个设备是否存在:简单的发出S信号确定有ACK */
                                        if (!i2c_default_probe(adapter, addr))
                                            return 0;
                                        
                                        memset(&info, 0, sizeof(struct i2c_board_info));
                                        info.addr = addr;    
                                        
                                        // 设置info.type,调用strlcpy函数拷贝
                                        err = driver->detect(temp_client, &info);
                    
                                        i2c_new_device(最终注册设置i2c_client)   


at24cxx_drv.c :


  
  
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/platform_device.h>
  4. #include <linux/i2c.h>
  5. #include <linux/err.h>
  6. #include <linux/regmap.h>
  7. #include <linux/slab.h>
  8. static int __ devinit at24cxx_probe(struct i2c_client *client,
  9.       const struct i2c_device_id *id)
  10. {
  11.  printk( "%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
  12.   return 0;
  13. }
  14. static int __ devexit at24cxx_remove(struct i2c_client *client)
  15. {
  16.  printk( "%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
  17.   return 0;
  18. }
  19. static const struct i2c_device_id at24cxx_id_table[] = {
  20.  { "at24c08", 0 },
  21.  {}
  22. };
  23. static int at24cxx_detect(struct i2c_client *client,
  24.          struct i2c_board_info *info)
  25. {
  26.   /* 能运行到这里, 表示该addr的设备是存在的,即dev链表中是有这个设备的
  27.   * 但是有些设备单凭地址无法分辨(A芯片的地址是0x50, B芯片的地址也是0x50,当发0x50后它们都会回应,这个时候还是不能区分到底是A还是B,A和B是不可能同时挂在一条总线上的)
  28.   * 还需要进一步读写I2C设备来分辨是哪款芯片,比如读A芯片可能有一些值,读B芯片就会有另外一些值
  29.   * detect就是用来进一步分辨这个芯片是哪一款,并且设置info->type
  30.   */
  31.  
  32.  printk( "at24cxx_detect : addr = 0x%x\n", client->addr);
  33.   /* 这里应该进一步判断是哪一款,这里不用判断 */
  34.  
  35.  strlcpy(info->type, "at24c08", I2C_NAME_SIZE);
  36.   return 0;
  37. }
  38. //0x60、0x50表示I2C设备的地址
  39. static const unsigned short addr_list[] = { 0x60, 0x50, I2C_CLIENT_END };
  40. /* 1. 分配/设置i2c_driver */
  41. static struct i2c_driver at24cxx_driver = {
  42.  .class  = I2C_CLASS_HWMON, /* 表示去哪些适配器上找设备 */
  43.  .driver = {
  44.   .name = "100ask",
  45.   .owner = THIS_MODULE,
  46.  },
  47.  .probe  = at24cxx_probe,
  48.  .remove  = __devexit_p(at24cxx_remove),
  49.  .id_table = at24cxx_id_table,
  50.  .detect     = at24cxx_detect,  /* 用这个函数来检测设备确实存在 */
  51.  .address_list = addr_list,   /* 这些设备的地址 */
  52. };
  53. static int at24cxx_drv_init(void)
  54. {
  55.   /* 2. 注册i2c_driver */
  56.  i2c_add_driver(&at24cxx_driver);
  57.  
  58.   return 0;
  59. }
  60. static void at24cxx_drv_exit(void)
  61. {
  62.  i2c_del_driver(&at24cxx_driver);
  63. }
  64. module_init(at24cxx_drv_init);
  65. module_exit(at24cxx_drv_exit);
  66. MODULE_LICENSE( "GPL");

三、I2C设备驱动程序的编写

      上面主要介绍了注册i2c_client结构的四种方法,并伴随测试程序at24cxx_dev.c和at24cxx_drv.c。

      然而,我们的目的是应用程序能通过系统调用读写EEPROM存储器,这就需要实现图一中的设备驱动程序。实现的地方就是在probe函数中,当i2c_client结构和i2c_driver结构都注册后,在i2c_bus_type结构的i2c_client链表中就会有dev,i2c_driver链表中有drv,bus的i2c_device_match函数中匹配dev和drv,成功将调用probe函数。在上面测试过程中probe函数基本上什么也没做,原因在于为了测试i2c_client结构的注册,这里的目的是实现设备驱动层,所以会在里面实现注册设备驱动、创建设备节点等操作。

      注意,针对i2c_driver结构的probe成员,也就是上面说的probe函数的参数也是非常有用的。当probe函数成功调用后,它的第一个参数就记录了对应的I2C设备,也就是i2c_client结构体,第二个参数记录对应I2C设备的i2c_device_id。在后面设备驱动读写函数中将调用核心层的读写函数,这些函数的第一个参数就是要知道是哪个I2C设备,即要传入i2c_client结构体。所以,在probe函数中,可以定义结构体指针指向probe函数参数,通过这样的方式记录保存了i2c_client和i2c_device_id。

     这样,图一中的设备驱动层就实现了,而核心层和适配器层都是内核自带的,主要是提供接口供设备驱动使用。下面是应用层操作EEPROM将要用到的所有完整代码:

at24cxx_dev.c :


  
  
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/platform_device.h>
  4. #include <linux/i2c.h>
  5. #include <linux/err.h>
  6. #include <linux/regmap.h>
  7. #include <linux/slab.h>
  8. //0x50表示I2C设备的地址,一般在 I2C设备芯片手册可以查到
  9. static struct i2c_board_info at24cxx_info = { 
  10.  I2C_BOARD_INFO( "at24c08", 0x50), //这个名字要和drv程序中的id_table中名字要一样
  11. };
  12. static struct i2c_client *at24cxx_client;
  13. static int at24cxx_dev_init(void)
  14. {
  15.   struct i2c_adapter *i2c_adap;
  16.   int busNum = 0 ; //把这个总线号改为1,也能成功加载此驱动,原因在于i2c_new_device而不是i2c_new_probed_device方法
  17.  
  18.  printk( "at24cxx dev of bus-dev-drv module_init!\n");
  19.  
  20.  i2c_adap = i2c_get_adapter(busNum); //这里要实验的EEPROM是挂接在第0条I2C总线上的,所以这里的参数是0
  21.   if (!i2c_adap) {
  22.   pr_err( "failed to get adapter i2c%d\n", busNum);
  23.    return -ENODEV;
  24.  }
  25.  
  26.  at24cxx_client = i2c_new_device(i2c_adap, &at24cxx_info); //设置和注册i2c_client结构体
  27.   if (!at24cxx_client){
  28.    //pr_err("failed to register %s to i2c%d\n",at24cxx_info.type, busNum);
  29.   pr_err( "failed to register at24c08 to i2c%d\n",busNum);
  30.    return -ENODEV;
  31.  } 
  32.  
  33.  i2c_put_adapter(i2c_adap);
  34.  
  35.   return 0;
  36. }
  37. static void at24cxx_dev_exit(void)
  38. {
  39.  printk( "at24cxx dev of bus-dev-drv module_exit!\n");
  40.  i2c_unregister_device(at24cxx_client);
  41. }
  42. module_init(at24cxx_dev_init);
  43. module_exit(at24cxx_dev_exit);
  44. MODULE_LICENSE( "GPL");

at24cxx_drv.c :


  
  
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/platform_device.h>
  4. #include <linux/i2c.h>
  5. #include <linux/err.h>
  6. #include <linux/regmap.h>
  7. #include <linux/slab.h>
  8. #include <linux/fs.h>
  9. #include <asm/uaccess.h>
  10. static int major;
  11. static struct class *class;
  12. static struct i2c_client *at24cxx_client;
  13. /* 传入: buf[0] : addr, 即将访问I2C设备的地址
  14. * 输出: buf[0] : data
  15. */
  16. static ssize_t at24cxx_read(struct file * file, char __user *buf, size_t count, loff_t *off)
  17. {
  18.   unsigned char addr, data;
  19.  
  20.   if (copy_from_user(&addr, buf, 1)){
  21.   printk( "at24cxx_read: copy_from_user error\n");
  22.    return -EFAULT;
  23.  }
  24. /*
  25.       根据EEPROM读时序,在smbus-protocol文档中找到i2c_smbus_read_byte_data
  26.    函数。
  27. */ 
  28.  data = i2c_smbus_read_byte_data(at24cxx_client, addr);
  29.   if (data < 0) {
  30.   printk( "at24cxx_read: i2c_smbus_read_byte_data error\n");
  31.    return data;
  32.  }
  33.  
  34.   if (copy_to_user(buf, &data, 1)){
  35.   printk( "at24cxx_read: copy_to_user error\n");
  36.    return -EFAULT;
  37.  }
  38.  
  39.   return 1;
  40. }
  41. /* buf[0] : addr, 即将访问I2C设备的地址
  42. * buf[1] : data
  43. */
  44. static ssize_t at24cxx_write(struct file *file, const char __user *buf, size_t count, loff_t *off)
  45. {
  46.   unsigned char ker_buf[ 2];
  47.   unsigned char addr, data;
  48.   if (copy_from_user(ker_buf, buf, 2)){
  49.   printk( "at24cxx_write: copy_from_user error\n");
  50.    return -EFAULT;
  51.  }
  52.  
  53.  addr = ker_buf[ 0];
  54.  data = ker_buf[ 1];
  55.  printk( "addr = 0x%02x, data = 0x%02x\n", addr, data);
  56.   /*
  57.      读写操作函数由核心层提供,有两种方式:
  58.   1. SMBUS协议,系统管理总线,内核文档建议用个方式,原因详见文档<smbus-protocol>
  59.   2. I2C_transfer
  60.      根据smbus-protocol文档,i2c_smbus_write_byte_data函数的时序和
  61.   EEPROM的写操作时序完全一样,所以用它。第一个参数at24cxx_client在
  62.   probe函数中已经做了记录。
  63.  */
  64.   if (!i2c_smbus_write_byte_data(at24cxx_client, addr, data)){
  65.    return 2; //如果写成功,返回写成功字节
  66.  }
  67.   else
  68.  {
  69.   printk( "at24cxx_write: i2c_smbus_write_byte_data error\n");
  70.    return -EIO; 
  71.     }
  72. }
  73. static struct file_operations at24cxx_fops = {
  74.  .owner = THIS_MODULE,
  75.  .read  = at24cxx_read,
  76.  .write = at24cxx_write,
  77. };
  78. static int __ devinit at24cxx_probe(struct i2c_client *client,
  79.       const struct i2c_device_id *id)
  80. {
  81.   struct device *class_dev = NULL;
  82.  
  83.  at24cxx_client = client; //记录i2c_client结构,调用SMBUS方法的时候方便使用
  84.   
  85.   //printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
  86.  major = register_chrdev( 0, "at24cxx", &at24cxx_fops);
  87.   if (major < 0) {
  88.   printk( "at24cxx_probe: can't register at24cxx char device\n");
  89.    return major;
  90.  }
  91.  
  92.   class = class_create(THIS_MODULE, "at24cxx"); //创建类
  93.   if (IS_ERR(class)) {
  94.   printk( "at24cxx_probe: class create failed\n");
  95.   unregister_chrdev(major, "at24cxx");
  96.    return PTR_ERR(class);
  97.  }
  98.  
  99.   /* 在类下面创建设备,自动创建设备节点。device_create会调用那个mdev,
  100.     mdev就会根据环境变量创建设备节点*/
  101.   /* /dev/at24cxx */
  102.  class_dev = device_create(class, NULL,MKDEV(major, 0), NULL, "at24cxx");
  103.   if (IS_ERR(class_dev)) {
  104.   printk( "at24cxx_probe: class device create failed\n");
  105.   unregister_chrdev(major, "at24cxx");
  106.    return PTR_ERR(class_dev);
  107.  }
  108.  
  109.   return 0;
  110. }
  111. static int __ devexit at24cxx_remove(struct i2c_client *client)
  112. {
  113.  printk( "at24cxx drv of bus-dev-drv at24cxx_remove!\n");
  114.  device_destroy(class, MKDEV(major, 0)); //删除设备节点
  115.  class_destroy(class); //摧毁类
  116.  unregister_chrdev(major, "at24cxx");
  117.   
  118.   return 0;
  119. }
  120. static const struct i2c_device_id at24cxx_id_table[] = {
  121.  { "at24c08", 0 }, //用到哪些就声明哪些内容,比如driver_data用不到,所以这里就写0
  122.  {}
  123. };
  124. /* 1. 分配/设置i2c_driver */
  125. static struct i2c_driver at24cxx_driver = {
  126.  .driver = {
  127.   .name = "100ask", //在这里,这个名字并不重要,重要的是id_table里面的名字,所以这里可以随便起
  128.   .owner = THIS_MODULE,
  129.  },
  130.  .probe  = at24cxx_probe,
  131.  .remove  = __devexit_p(at24cxx_remove),
  132.  .id_table = at24cxx_id_table,
  133. };
  134. static int at24cxx_drv_init(void)
  135. {
  136.   int ret;
  137.  
  138.  printk( "at24cxx drv of bus-dev-drv module_init!\n");
  139.  
  140.   /* 2. 注册i2c_driver */
  141.  ret = i2c_add_driver(&at24cxx_driver);
  142.   if (ret != 0){
  143.   pr_err( "Failed to register at24cxx I2C driver: %d\n", ret);
  144.  }
  145.   
  146.   return 0;
  147. }
  148. static void at24cxx_drv_exit(void)
  149. {
  150.  printk( "at24cxx drv of bus-dev-drv module_exit!\n");
  151.  i2c_del_driver(&at24cxx_driver);
  152. }
  153. module_init(at24cxx_drv_init);
  154. module_exit(at24cxx_drv_exit);
  155. MODULE_LICENSE( "GPL");

4th_device_driver_i2c_test.c:


  
  
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. /* i2c_test r addr
  8. * i2c_test w addr val
  9. */
  10. void print_usage(char *file)
  11. {
  12.   printf( "%s r addr\n", file);
  13.   printf( "%s w addr val\n", file);
  14. }
  15. int main(int argc, char **argv)
  16. {
  17.   int fd;
  18.   unsigned char buf[ 2];
  19.  
  20.   if ((argc != 3) && (argc != 4))
  21.  {
  22.   print_usage(argv[ 0]);
  23.    return -1;
  24.  }
  25.  fd = open( "/dev/at24cxx", O_RDWR);
  26.   if (fd < 0)
  27.  {
  28.    printf( "can't open /dev/at24cxx\n");
  29.    return -1;
  30.  }
  31.   if ( strcmp(argv[ 1], "r") == 0)
  32.  {
  33.   buf[ 0] = strtoul(argv[ 2], NULL, 0);
  34.   read(fd, buf, 1);
  35.    printf( "data: %c, %d, 0x%2x\n", buf[ 0], buf[ 0], buf[ 0]);
  36.  }
  37.   else if (( strcmp(argv[ 1], "w") == 0) && (argc == 4))
  38.  {
  39.   buf[ 0] = strtoul(argv[ 2], NULL, 0);
  40.   buf[ 1] = strtoul(argv[ 3], NULL, 0);
  41.    if (write(fd, buf, 2) != 2)
  42.     printf( "write err, addr = 0x%02x, data = 0x%02x\n", buf[ 0], buf[ 1]);
  43.  }
  44.   else
  45.  {
  46.   print_usage(argv[ 0]);
  47.    return -1;
  48.  }
  49.  
  50.   return 0;
  51. }

四、不自己写驱动直接访问

      在图一中,核心层已经提供了统一的I2C设备操作函数:
(1)SMBUS协议方式(SMBUS协议,是I2C协议的子集,某些设备只支持此协议,所以内核文档smbus-protocol建议优先使用此方法)
(2)i2c_transfer方式(I2C协议)
      应用程序可以直接通过上述两种方式访问I2C设备,因为内核已经封装了一套驱动程序,应用程序只需要使用那套驱动就可以访问I2C设备了。应用程序设计方法可以参考文档dev-interface,这里给出测试代码。

Device Drivers
     I2C support
        <*>   I2C device interface

i2c_usr_test.c :


  
  
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include "i2c-dev.h"
  8. /* i2c_usr_test </dev/i2c-0> <dev_addr> r addr
  9. * i2c_usr_test </dev/i2c-0> <dev_addr> w addr val
  10. */
  11. void print_usage(char *file)
  12. {
  13.   printf( "%s </dev/i2c-0> <dev_addr> r addr\n", file);
  14.   printf( "%s </dev/i2c-0> <dev_addr> w addr val\n", file);
  15. }
  16. int main(int argc, char **argv)
  17. {
  18.   int fd;
  19.   unsigned char addr, data;
  20.   int dev_addr;
  21.  
  22.   if ((argc != 5) && (argc != 6))
  23.  {
  24.   print_usage(argv[ 0]);
  25.    return -1;
  26.  }
  27.  fd = open(argv[ 1], O_RDWR);
  28.   if (fd < 0)
  29.  {
  30.    printf( "can't open %s\n", argv[ 1]);
  31.    return -1;
  32.  }
  33.  dev_addr = strtoul(argv[ 2], NULL, 0);
  34.   if (ioctl(fd, I2C_SLAVE, dev_addr) < 0)
  35.  {   
  36.    /* ERROR HANDLING; you can check errno to see what went wrong */   
  37.    printf( "set addr error!\n");
  38.    return -1;
  39.  }
  40.   if ( strcmp(argv[ 3], "r") == 0)
  41.  {
  42.   addr = strtoul(argv[ 4], NULL, 0);
  43.   
  44.   data = i2c_smbus_read_word_data(fd, addr);
  45.    
  46.    printf( "data: %c, %d, 0x%2x\n", data, data, data);
  47.  }
  48.   else if (( strcmp(argv[ 3], "w") == 0) && (argc == 6))
  49.  {
  50.   addr = strtoul(argv[ 4], NULL, 0);
  51.   data = strtoul(argv[ 5], NULL, 0);
  52.   i2c_smbus_write_byte_data(fd, addr, data);  
  53.  }
  54.   else
  55.  {
  56.   print_usage(argv[ 0]);
  57.    return -1;
  58.  }
  59.  
  60.   return 0;
  61. }

五、编写"总线(适配器adapter)"驱动

       应用程序调用设备驱动程序,设备驱动程序会调用核心层提供的某些函数(如SMBUS相关函数),核心层的这些函数最终会调用到适配器层的相关函数,这些函数是处理器I2C接口的相关硬件操作,它们会根据I2C协议向I2C设备发出相应信号达到控制I2C设备的目的。
      这里设计的驱动即设计适配器驱动程序,先找到Linux3.5中内核自带的适配器驱动。
在make menuconfig后,找到如下选项:
Device Drivers
     I2C support
         I2C Hardware Bus support
             < > S3C2410 I2C Driver
       选中S3C2410 I2C Driver,按下键盘上的h键,找到第一行出现的那个宏 CONFIG_I2C_S3C2410 , 然后在内核源码目录下执行 grep "CONFIG_I2C_S3C2410" -R *后,找到如下信息:
drivers/i2c/busses/Makefile:obj-$(CONFIG_I2C_S3C2410)   += i2c-s3c2410.o
      从上面信息中就可以知道I2C总线适配器对应的驱动程序文件为i2c-s3c2410.c文件。然后分析此文件,分析类似驱动程序都是先从入口函数看。下面给出测试代码(基于2440):


  
  
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/i2c.h>
  4. #include <linux/init.h>
  5. #include <linux/time.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/delay.h>
  8. #include <linux/errno.h>
  9. #include <linux/err.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/pm_runtime.h>
  12. #include <linux/clk.h>
  13. #include <linux/cpufreq.h>
  14. #include <linux/slab.h>
  15. #include <linux/io.h>
  16. #include <linux/of_i2c.h>
  17. #include <linux/of_gpio.h>
  18. #include <plat/gpio-cfg.h>
  19. #include <mach/regs-gpio.h>
  20. #include <asm/irq.h>
  21. #include <plat/regs-iic.h>
  22. #include <plat/iic.h>
  23. //#define PRINTK printk
  24. #define PRINTK(...)
  25. enum s3c24xx_i2c_state {
  26.  STATE_IDLE,
  27.  STATE_START,
  28.  STATE_READ,
  29.  STATE_WRITE,
  30.  STATE_STOP
  31. };
  32. struct s3c2440_i2c_regs {
  33.   unsigned int iiccon;
  34.   unsigned int iicstat;
  35.   unsigned int iicadd;
  36.   unsigned int iicds;
  37.   unsigned int iiclc;
  38. };
  39. struct s3c2440_i2c_xfer_data {
  40.   struct i2c_msg *msgs;
  41.   int msn_num;
  42.   int cur_msg;
  43.   int cur_ptr;
  44.   int state;
  45.   int err;
  46.   wait_queue_head_t wait;
  47. };
  48. static struct s3c2440_i2c_xfer_data s3c2440_i2c_xfer_data;
  49. static struct s3c2440_i2c_regs *s3c2440_i2c_regs;
  50. static void s3c2440_i2c_start(void)
  51. {
  52.  s3c2440_i2c_xfer_data.state = STATE_START;
  53.  
  54.   if (s3c2440_i2c_xfer_data.msgs->flags & I2C_M_RD) /* 读 */
  55.  {
  56.   s3c2440_i2c_regs->iicds   = s3c2440_i2c_xfer_data.msgs->addr << 1;
  57.   s3c2440_i2c_regs->iicstat   = 0xb0// 主机接收,启动
  58.  }
  59.   else /* 写 */
  60.  {
  61.   s3c2440_i2c_regs->iicds   = s3c2440_i2c_xfer_data.msgs->addr << 1;
  62.   s3c2440_i2c_regs->iicstat    = 0xf0;    // 主机发送,启动
  63.  }
  64. }
  65. static void s3c2440_i2c_stop(int err)
  66. {
  67.  s3c2440_i2c_xfer_data.state = STATE_STOP;
  68.  s3c2440_i2c_xfer_data.err   = err;
  69.  PRINTK( "STATE_STOP, err = %d\n", err);
  70.   if (s3c2440_i2c_xfer_data.msgs->flags & I2C_M_RD) /* 读 */
  71.  {
  72.    // 下面两行恢复I2C操作,发出P信号
  73.   s3c2440_i2c_regs->iicstat = 0x90;
  74.   s3c2440_i2c_regs->iiccon  = 0xaf;
  75.   ndelay( 50);  // 等待一段时间以便P信号已经发出
  76.  }
  77.   else /* 写 */
  78.  {
  79.    // 下面两行用来恢复I2C操作,发出P信号
  80.   s3c2440_i2c_regs->iicstat = 0xd0;
  81.   s3c2440_i2c_regs->iiccon  = 0xaf;
  82.   ndelay( 50);  // 等待一段时间以便P信号已经发出
  83.  }
  84.   /* 唤醒 */
  85.  wake_up(&s3c2440_i2c_xfer_data.wait);
  86.  
  87. }
  88. static int s3c2440_i2c_xfer(struct i2c_adapter *adap,
  89.    struct i2c_msg *msgs, int num)
  90. {
  91.   unsigned long timeout;
  92.  
  93.   /* 把num个msg的I2C数据发送出去/读进来 */
  94.  s3c2440_i2c_xfer_data.msgs    = msgs;
  95.  s3c2440_i2c_xfer_data.msn_num = num;
  96.  s3c2440_i2c_xfer_data.cur_msg = 0;
  97.  s3c2440_i2c_xfer_data.cur_ptr = 0;
  98.  s3c2440_i2c_xfer_data.err     = -ENODEV;
  99.  s3c2440_i2c_start();
  100.   /* 休眠 */
  101.  timeout = wait_event_timeout(s3c2440_i2c_xfer_data.wait, (s3c2440_i2c_xfer_data.state == STATE_STOP), HZ * 5);
  102.   if ( 0 == timeout)
  103.  {
  104.   printk( "s3c2440_i2c_xfer time out\n");
  105.    return -ETIMEDOUT;
  106.  }
  107.   else
  108.  {
  109.    return s3c2440_i2c_xfer_data.err;
  110.  }
  111. }
  112. static u32 s3c2440_i2c_func(struct i2c_adapter *adap)
  113. {
  114.   return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_PROTOCOL_MANGLING;
  115. }
  116. static const struct i2c_algorithm s3c2440_i2c_algo = {
  117. // .smbus_xfer     = ,
  118.  .master_xfer = s3c2440_i2c_xfer,
  119.  .functionality = s3c2440_i2c_func,
  120. };
  121. /* 1. 分配/设置i2c_adapter
  122. */
  123. static struct i2c_adapter s3c2440_i2c_adapter = {
  124. .name    = "s3c2440_100ask",
  125. .algo    = &s3c2440_i2c_algo,
  126. .owner    = THIS_MODULE,
  127. };
  128. static int isLastMsg(void)
  129. {
  130.   return (s3c2440_i2c_xfer_data.cur_msg == s3c2440_i2c_xfer_data.msn_num - 1);
  131. }
  132. static int isEndData(void)
  133. {
  134.   return (s3c2440_i2c_xfer_data.cur_ptr >= s3c2440_i2c_xfer_data.msgs->len);
  135. }
  136. static int isLastData(void)
  137. {
  138.   return (s3c2440_i2c_xfer_data.cur_ptr == s3c2440_i2c_xfer_data.msgs->len - 1);
  139. }
  140. static irqreturn_t s3c2440_i2c_xfer_irq(int irq, void *dev_id)
  141. {
  142.   unsigned int iicSt;
  143.     iicSt  = s3c2440_i2c_regs->iicstat;
  144.   if(iicSt & 0x8){ printk( "Bus arbitration failed\n\r"); }
  145.   switch (s3c2440_i2c_xfer_data.state)
  146.  {
  147.    case STATE_START : /* 发出S和设备地址后,产生中断 */
  148.   {
  149.    PRINTK( "Start\n");
  150.     /* 如果没有ACK, 返回错误 */
  151.     if (iicSt & S3C2410_IICSTAT_LASTBIT)
  152.    {
  153.     s3c2440_i2c_stop(-ENODEV);
  154.      break;
  155.    }
  156.     if (isLastMsg() && isEndData())
  157.    {
  158.     s3c2440_i2c_stop( 0);
  159.      break;
  160.    }
  161.     /* 进入下一个状态 */
  162.     if (s3c2440_i2c_xfer_data.msgs->flags & I2C_M_RD) /* 读 */
  163.    {
  164.     s3c2440_i2c_xfer_data.state = STATE_READ;
  165.      goto next_read;
  166.    }
  167.     else
  168.    {
  169.     s3c2440_i2c_xfer_data.state = STATE_WRITE;
  170.    } 
  171.   }
  172.    case STATE_WRITE:
  173.   {
  174.    PRINTK( "STATE_WRITE\n");
  175.     /* 如果没有ACK, 返回错误 */
  176.     if (iicSt & S3C2410_IICSTAT_LASTBIT)
  177.    {
  178.     s3c2440_i2c_stop(-ENODEV);
  179.      break;
  180.    }
  181.     if (!isEndData())  /* 如果当前msg还有数据要发送 */
  182.    {
  183.     s3c2440_i2c_regs->iicds = s3c2440_i2c_xfer_data.msgs->buf[s3c2440_i2c_xfer_data.cur_ptr];
  184.     s3c2440_i2c_xfer_data.cur_ptr++;
  185.     
  186.      // 将数据写入IICDS后,需要一段时间才能出现在SDA线上
  187.     ndelay( 50); 
  188.     
  189.     s3c2440_i2c_regs->iiccon = 0xaf;   // 恢复I2C传输
  190.      break;    
  191.    }
  192.     else if (!isLastMsg())
  193.    {
  194.      /* 开始处理下一个消息 */
  195.     s3c2440_i2c_xfer_data.msgs++;
  196.     s3c2440_i2c_xfer_data.cur_msg++;
  197.     s3c2440_i2c_xfer_data.cur_ptr = 0;
  198.     s3c2440_i2c_xfer_data.state = STATE_START;
  199.      /* 发出START信号和发出设备地址 */
  200.     s3c2440_i2c_start();
  201.      break;
  202.    }
  203.     else
  204.    {
  205.      /* 是最后一个消息的最后一个数据 */
  206.     s3c2440_i2c_stop( 0);
  207.      break;    
  208.    }
  209.     break;
  210.   }
  211.    case STATE_READ:
  212.   {
  213.    PRINTK( "STATE_READ\n");
  214.     /* 读出数据 */
  215.    s3c2440_i2c_xfer_data.msgs->buf[s3c2440_i2c_xfer_data.cur_ptr] = s3c2440_i2c_regs->iicds;   
  216.    s3c2440_i2c_xfer_data.cur_ptr++;
  217. next_read:
  218.     if (!isEndData()) /* 如果数据没读写, 继续发起读操作 */
  219.    {
  220.      if (isLastData())  /* 如果即将读的数据是最后一个, 不发ack */
  221.     {
  222.      s3c2440_i2c_regs->iiccon = 0x2f;   // 恢复I2C传输,接收到下一数据时无ACK
  223.     }
  224.      else
  225.     {
  226.      s3c2440_i2c_regs->iiccon = 0xaf;   // 恢复I2C传输,接收到下一数据时发出ACK
  227.     }    
  228.      break;
  229.    }
  230.     else if (!isLastMsg())
  231.    {
  232.      /* 开始处理下一个消息 */
  233.     s3c2440_i2c_xfer_data.msgs++;
  234.     s3c2440_i2c_xfer_data.cur_msg++;
  235.     s3c2440_i2c_xfer_data.cur_ptr = 0;
  236.     s3c2440_i2c_xfer_data.state = STATE_START;
  237.      /* 发出START信号和发出设备地址 */
  238.     s3c2440_i2c_start();
  239.      break;
  240.    }
  241.     else
  242.    {
  243.      /* 是最后一个消息的最后一个数据 */
  244.     s3c2440_i2c_stop( 0);
  245.      break;        
  246.    }
  247.     break;
  248.   }
  249.    default: break;
  250.  }
  251.   /* 清中断 */
  252.  s3c2440_i2c_regs->iiccon &= ~(S3C2410_IICCON_IRQPEND);
  253.   return IRQ_HANDLED; 
  254. }
  255. /*
  256. * I2C初始化
  257. */
  258. static void s3c2440_i2c_init(void)
  259. {
  260.   struct clk *clk;
  261.  clk = clk_get( NULL, "i2c");
  262.  clk_enable(clk);
  263.  
  264.     // 选择引脚功能:GPE15:IICSDA, GPE14:IICSCL
  265.     s3c_gpio_cfgpin(S3C2410_GPE( 14), S3C2410_GPE14_IICSCL);
  266.  s3c_gpio_cfgpin(S3C2410_GPE( 15), S3C2410_GPE15_IICSDA);
  267.     /* bit[7] = 1, 使能ACK
  268.      * bit[6] = 0, IICCLK = PCLK/16
  269.      * bit[5] = 1, 使能中断
  270.      * bit[3:0] = 0xf, Tx clock = IICCLK/16
  271.      * PCLK = 50MHz, IICCLK = 3.125MHz, Tx Clock = 0.195MHz
  272.      */
  273.     s3c2440_i2c_regs->iiccon = ( 1<< 7) | ( 0<< 6) | ( 1<< 5) | ( 0xf);  // 0xaf
  274.     s3c2440_i2c_regs->iicadd  = 0x10;     // S3C24xx slave address = [7:1]
  275.     s3c2440_i2c_regs->iicstat = 0x10;     // I2C串行输出使能(Rx/Tx)
  276. }
  277. static int i2c_bus_s3c2440_init(void)
  278. {
  279.   /* 2. 硬件相关的设置 */
  280.  s3c2440_i2c_regs = ioremap( 0x54000000, sizeof(struct s3c2440_i2c_regs));
  281.  
  282.  s3c2440_i2c_init();
  283.  request_irq(IRQ_IIC, s3c2440_i2c_xfer_irq, 0, "s3c2440-i2c", NULL);
  284.  init_waitqueue_head(&s3c2440_i2c_xfer_data.wait);
  285.  
  286.   /* 3. 注册i2c_adapter */
  287.  i2c_add_adapter(&s3c2440_i2c_adapter);
  288.  
  289.   return 0;
  290. }
  291. static void i2c_bus_s3c2440_exit(void)
  292. {
  293.  i2c_del_adapter(&s3c2440_i2c_adapter); 
  294.  free_irq(IRQ_IIC, NULL);
  295.  iounmap(s3c2440_i2c_regs);
  296. }
  297. module_init(i2c_bus_s3c2440_init);
  298. module_exit(i2c_bus_s3c2440_exit);
  299. MODULE_LICENSE( "GPL");

六、总结

      通过上述分析,可以总结出一个设计驱动程序的方法,这个方法适用于Linux内核的各个版本:

1.  如果要设计I2C设备驱动,看 Documentation/i2c目录下的相关内核文档;I2C协议;处理器的I2C接口;掌握bus-dev-drv模型;掌握驱动程序设计相关知识。

2.  实现bus-dev-drv模型驱动。比如要先注册i2c_client,而内核文档中instantiating-devices(构造设备)文件就说明了方法。然后根据方法,搜索内核中对应的例子,模仿就可以        设计出需要的驱动;然后要注册i2c_driver,也是模仿其他如何设计即可。总之,Linux是非常庞大的系统,里面有很多例子可以参考的。

3. 理清思路,搞清楚框架中哪些工作是需要做的,哪些是不需要做的。


    《smbus-protocol》中介绍了核心层各种读写接口,这些接口被设备驱动的读写接口调用。根据此文档,下面举例说明这些接口的应用:


SMBus Read Byte:  i2c_smbus_read_byte_data()
============================================

This reads a single byte from a device, from a designated register.
The register is specified through the Comm byte.

S Addr Wr [A] Comm [A] S Addr Rd [A] [Data] NA P

   

      i2c_smbus_read_byte_data函数在Linux内核中原型如下:

s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
{
    union i2c_smbus_data data;
    int status;

    status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
                I2C_SMBUS_READ, command,
                I2C_SMBUS_BYTE_DATA, &data);
    return (status < 0) ? status : data.byte;
}

      从字面意思上就可以猜测i2c_smbus_read_byte_data函数是从I2C的子集smbus上读取一个字节的数据,它的第一个参数为i2c_client结构体,这个结构在probe函数被调用后就被记录了(记录方法就是用一个i2c_client结构体指针指向probe函数的参数),第二个参数的含义是明确读取寄存器的地址,比如用户要读取EEPROM地址为1地方的数据,那么这里就把1传给内核空间并传给此函数。i2c_smbus_read_byte_data函数的返回值即从它的第二个参数Comm为地址的地方读出的数据。

     然而,对于4412上的I2C总线0上接的EEPROM设备来说,为什么使用这个函数呢?原因在于这个函数的时序和EEPROM芯片手册中读时序类似,下图是EEPROM读时序:


       根据上图,对照smbus-protocol文档中对i2c_smbus_read_byte_data()函数的介绍,即S Addr Wr [A] Comm [A] S Addr Rd [A] [Data] NA P,下面是详细说明:

       第一个为S表示起始信号;

       第二个为Addr表示I2C设备的地址,这里就是EEPOM的地址0x50;

       第三个为Wr表示操作方向为写;

       第四个为[A]表示EERPOM给处理器I2C控制器的回应,实际上就是把SDA拉低而已;

       第五个为Comm,写地址。表示要读取EEPROM中哪个地方的数据,是个地址,也是i2c_smbus_read_byte_data函数的第二个参数,

                     一般由用户空间提供;

       第六个为[A]表示EERPOM给处理器I2C控制器的回应,实际上就是把SDA拉低而已;

       第七个为S表示重新发送起始信号;

       第八个为Addr表示I2C设备的地址,这里就是EEPOM的地址0x50;

       第九个为Rd表示操作方向为读;

       第十个为[A]表示EERPOM给处理器I2C控制器的回应,实际上就是把SDA拉低而已;

       第十一个为[Data],表示从EEPROM中Comm地址处读取的数据;

       第十二个为NA,表示上述读取的数据为最后一个数据,这个时候处理器不用给eeprom回应,即NO ACK;    

       第十三个为P,表示由主机给出的停止信号,结束对EEPROM的操作。


















           
               

猜你喜欢

转载自blog.csdn.net/qq_40788950/article/details/83925768
今日推荐