i2c drivers

 

Linux设备驱动程序架构分析之一个I2C驱动实例

 

转载于:http://blog.csdn.net/liuhaoyutz

内核版本:3.10.1

 

编写一个I2C设备驱动程序的工作可分为两部分,一是定义和注册I2C设备,即i2c_client;二是定义和注册I2C设备驱动,即i2c_driver。下面我们就以mini2440的I2C设备at24c08 EEPROM为例,介绍如何完成这两个工作。

 

一、定义和注册I2C设备

步骤1:用i2c_board_info保存I2C设备相关信息

在Linux中,struct i2c_client代表一个I2C设备,该结构体定义在include/linux/i2c.h文件中:

 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. 200/** 
  2. 201 * struct i2c_client - represent an I2Cslave device 
  3. 202 * @flags: I2C_CLIENT_TEN indicates thedevice uses a ten bit chip address; 
  4. 203 * I2C_CLIENT_PEC indicates it uses SMBus Packet Error Checking 
  5. 204 * @addr: Address used on the I2C busconnected to the parent adapter. 
  6. 205 * @name: Indicates the type of thedevice, usually a chip name that's 
  7. 206 * generic enough to hide second-sourcing and compatible revisions. 
  8. 207 * @adapter: manages the bus segmenthosting this I2C device 
  9. 208 * @driver: device's driver, hencepointer to access routines 
  10. 209 * @dev: Driver model device node forthe slave. 
  11. 210 * @irq: indicates the IRQ generated bythis device (if any) 
  12. 211 * @detected: member of ani2c_driver.clients list or i2c-core's 
  13. 212 * userspace_devices list 
  14. 213 * 
  15. 214 * An i2c_client identifies a singledevice (i.e. chip) connected to an 
  16. 215 * i2c bus. The behaviour exposed to Linuxis defined by the driver 
  17. 216 * managing the device. 
  18. 217 */  
  19. 218struct i2c_client {  
  20. 219   unsigned short flags;       /*div., see below      */  
  21. 220   unsigned short addr;        /*chip address - NOTE: 7bit    */  
  22. 221                    /* addresses are stored inthe  */  
  23. 222                    /* _LOWER_ 7 bits       */  
  24. 223   char name[I2C_NAME_SIZE];  
  25. 224   struct i2c_adapter *adapter;    /*the adapter we sit on    */  
  26. 225   struct i2c_driver *driver;  /* andour access routines  */  
  27. 226   struct device dev;      /* thedevice structure     */  
  28. 227   int irq;            /* irq issuedby device     */  
  29. 228   struct list_head detected;  
  30. 229};  


 

我们不用直接定义一个i2c_client结构,Linux会根据I2C设备的相关信息自动创建i2c_client。所以,我们需要提供I2C设备的相关信息。对于Mini2440的I2C设备at24c08 EEPROM,其相关信息保存在arch/arm/mach-s3c24xx/mach-mini2440.c文件中:

 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. 485/* 
  2. 486 * I2C devices 
  3. 487 */  
  4. 488static struct at24_platform_data at24c08= {  
  5. 489   .byte_len   = SZ_8K / 8,  
  6. 490   .page_size  = 16,  
  7. 491};  
  8. 492  
  9. 493static struct i2c_board_infomini2440_i2c_devs[] __initdata = {  
  10. 494   {  
  11. 495       I2C_BOARD_INFO("24c08", 0x50),  
  12. 496       .platform_data = &at24c08,  
  13. 497   },  
  14. 498};  


 

struct i2c_board_info是创建i2c设备的模板,该结构体定义在include/linux/i2c.h文件中:

 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. 251/** 
  2. 252 * struct i2c_board_info - template fordevice creation 
  3. 253 * @type: chip type, to initializei2c_client.name 
  4. 254 * @flags: to initializei2c_client.flags 
  5. 255 * @addr: stored in i2c_client.addr 
  6. 256 * @platform_data: stored ini2c_client.dev.platform_data 
  7. 257 * @archdata: copied intoi2c_client.dev.archdata 
  8. 258 * @of_node: pointer to OpenFirmwaredevice node 
  9. 259 * @acpi_node: ACPI device node 
  10. 260 * @irq: stored in i2c_client.irq 
  11. 261 * 
  12. 262 * I2C doesn't actually support hardwareprobing, although controllers and 
  13. 263 * devices may be able to useI2C_SMBUS_QUICK to tell whether or not there's 
  14. 264 * a device at a given address.  Drivers commonly need more information than 
  15. 265 * that, such as chip type,configuration, associated IRQ, and so on. 
  16. 266 * 
  17. 267 * i2c_board_info is used to buildtables of information listing I2C devices 
  18. 268 * that are present.  This information is used to grow the drivermodel tree. 
  19. 269 * For mainboards this is donestatically using i2c_register_board_info(); 
  20. 270 * bus numbers identify adapters thataren't yet available.  For add-on boards, 
  21. 271 * i2c_new_device() does thisdynamically with the adapter already known. 
  22. 272 */  
  23. 273struct i2c_board_info {  
  24. 274   char        type[I2C_NAME_SIZE];  
  25. 275   unsigned short  flags;  
  26. 276   unsigned short  addr;  
  27. 277   void        *platform_data;  
  28. 278   struct dev_archdata *archdata;  
  29. 279   struct device_node *of_node;  
  30. 280   struct acpi_dev_node acpi_node;  
  31. 281   int     irq;  
  32. 282};  


 

从注释可以看到,i2c_board_info结构体用来保存I2C设备的相关信息,Linux根据这些信息创建I2C设备相关的设备模型树。对于mainboards,通过调用i2c_register_board_info()静态完成。对于add-on boards,通过调用i2c_new_device()动态完成。

i2c_board_info结构体有两个成员必须初始化,一个是type,用来初始化i2c_client.name;另一个是addr,用来初始化i2c_client.addr。其它i2c_board_info结构体成员根据需要赋值或保持为空。

宏I2C_BOARD_INFO定义在include/linux/i2c.h文件中,其内容如下:

 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. 284/** 
  2. 285 * I2C_BOARD_INFO - macro used to listan i2c device and its address 
  3. 286 * @dev_type: identifies the device type 
  4. 287 * @dev_addr: the device's address onthe bus. 
  5. 288 * 
  6. 289 * This macro initializes essentialfields of a struct i2c_board_info, 
  7. 290 * declaring what has been provided on aparticular board.  Optional 
  8. 291 * fields (such as associated irq, ordevice-specific platform_data) 
  9. 292 * are provided using conventionalsyntax. 
  10. 293 */  
  11. 294#define I2C_BOARD_INFO(dev_type,dev_addr) \  
  12. 295   .type = dev_type, .addr = (dev_addr)  


 

从注释可以看到,I2C_BOARD_INFO宏用来初始化i2c_board_info结构体的两个必须初始化的成员变量type和addr。i2c_board_info结构体的其它成员变量使用常规的初始化语法。

struct at24_platform_data定义在include/linux/i2c/at24.h文件中:

1

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. 4/** 
  2. 15 * struct at24_platform_data - data toset up at24 (generic eeprom) driver 
  3. 16 * @byte_len: size of eeprom in byte 
  4. 17 * @page_size: number of byte which canbe written in one go 
  5. 18 * @flags: tunable options, checkAT24_FLAG_* defines 
  6. 19 * @setup: an optional callback invokedafter eeprom is probed; enables kernel 
  7. 20   code to access eeprom via memory_accessor, see example 
  8. 21 * @context: optional parameter passed tosetup() 
  9. 22 * 
  10. 23 * If you set up a custom eeprom type,please double-check the parameters. 
  11. 24 * Especially page_size needs extra care,as you risk data loss if your value 
  12. 25 * is bigger than what the chip actuallysupports! 
  13. 26 * 
  14. 27 * An example in pseudo code for asetup() callback: 
  15. 28 * 
  16. 29 * void get_mac_addr(structmemory_accessor *mem_acc, void *context) 
  17. 30 * { 
  18. 31 * u8 *mac_addr = ethernet_pdata->mac_addr; 
  19. 32 * off_t offset = context; 
  20. 33 * 
  21. 34 * // Read MAC addr from EEPROM 
  22. 35 * if (mem_acc->read(mem_acc, mac_addr, offset, ETH_ALEN) == ETH_ALEN) 
  23. 36 *     pr_info("Read MAC addr from EEPROM: %pM\n", mac_addr); 
  24. 37 * } 
  25. 38 * 
  26. 39 * This function pointer and context cannow be set up in at24_platform_data. 
  27. 40 */  
  28. 41  
  29. 42struct at24_platform_data {  
  30. 43   u32     byte_len;       /* size (sum of all addr) */  
  31. 44   u16     page_size;      /* for writes */  
  32. 45   u8      flags;  
  33. 46#define AT24_FLAG_ADDR16    0x80   /* address pointer is 16 bit */  
  34. 47#define AT24_FLAG_READONLY  0x40   /* sysfs-entry will be read-only */  
  35. 48#define AT24_FLAG_IRUGO     0x20   /* sysfs-entry will be world-readable */  
  36. 49#define AT24_FLAG_TAKE8ADDR 0x10    /* take always 8 addresses (24c00) */  
  37. 50  
  38. 51   void        (*setup)(structmemory_accessor *, void *context);  
  39. 52   void        *context;  
  40. 53};  

 

该结构体用来保存at24系列EEPROM的platform data。byte_len成员变量保存EEPROM的大小,以byte为单位。page_size成员变量用来指定一次最多能写多少个byte。对于Mini2440,定义at24_platform_data结构体变量at24c08:

 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. 488static struct at24_platform_data at24c08= {  
  2. 489   .byte_len   = SZ_8K / 8,  
  3. 490   .page_size  = 16,  
  4. 491};  


 

SZ_8K宏即8K,定义在include/linux/sizes.h文件中:

 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. 25#define SZ_8K               0x00002000  


 

 

步骤2:调用i2c_register_board_info注册i2c设备相关信息

对于Mini2440的I2C设备at24c08 EEPROM,由arch/arm/mach-s3c24xx/mach-mini2440.c文件中的mini2440_init函数调用i2c_register_board_info注册i2c设备相关信息,mini2440_init函数内容如下:

 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. 622static void __init mini2440_init(void)  
  2. 623{  
  3. 624   struct mini2440_features_t features = { 0 };  
  4. 625   int i;  
  5. 626  
  6. 627   printk(KERN_INFO "MINI2440: Option string mini2440=%s\n",  
  7. 628            mini2440_features_str);  
  8. 629  
  9. 630   /* Parse the feature string */  
  10. 631   mini2440_parse_features(&features, mini2440_features_str);  
  11. 632  
  12. 633   /* turn LCD on */  
  13. 634   s3c_gpio_cfgpin(S3C2410_GPC(0), S3C2410_GPC0_LEND);  
  14. 635  
  15. 636   /* Turn the backlight early on */  
  16. 637   WARN_ON(gpio_request_one(S3C2410_GPG(4), GPIOF_OUT_INIT_HIGH, NULL));  
  17. 638   gpio_free(S3C2410_GPG(4));  
  18. 639  
  19. 640   /* remove pullup on optional PWM backlight -- unused on 3.5 and 7"s*/  
  20. 641   gpio_request_one(S3C2410_GPB(1), GPIOF_IN, NULL);  
  21. 642   s3c_gpio_setpull(S3C2410_GPB(1), S3C_GPIO_PULL_UP);  
  22. 643   gpio_free(S3C2410_GPB(1));  
  23. 644  
  24. 645   /* mark the key as input, without pullups (there is one on the board) */  
  25. 646   for (i = 0; i < ARRAY_SIZE(mini2440_buttons); i++) {  
  26. 647       s3c_gpio_setpull(mini2440_buttons[i].gpio, S3C_GPIO_PULL_UP);  
  27. 648       s3c_gpio_cfgpin(mini2440_buttons[i].gpio, S3C2410_GPIO_INPUT);  
  28. 649   }  
  29. 650   if (features.lcd_index != -1) {  
  30. 651       int li;  
  31. 652  
  32. 653       mini2440_fb_info.displays =  
  33. 654           &mini2440_lcd_cfg[features.lcd_index];  
  34. 655  
  35. 656       printk(KERN_INFO "MINI2440: LCD");  
  36. 657       for (li = 0; li < ARRAY_SIZE(mini2440_lcd_cfg); li++)  
  37. 658            if (li == features.lcd_index)  
  38. 659                printk(" [%d:%dx%d]",li,  
  39. 660                   mini2440_lcd_cfg[li].width,  
  40. 661                   mini2440_lcd_cfg[li].height);  
  41. 662            else  
  42. 663                printk(" %d:%dx%d",li,  
  43. 664                    mini2440_lcd_cfg[li].width,  
  44. 665                    mini2440_lcd_cfg[li].height);  
  45. 666       printk("\n");  
  46. 667       s3c24xx_fb_set_platdata(&mini2440_fb_info);  
  47. 668   }  
  48. 669  
  49. 670   s3c24xx_udc_set_platdata(&mini2440_udc_cfg);  
  50. 671   s3c24xx_mci_set_platdata(&mini2440_mmc_cfg);  
  51. 672   s3c_nand_set_platdata(&mini2440_nand_info);  
  52. 673   s3c_i2c0_set_platdata(NULL);  
  53. 674  
  54. 675   i2c_register_board_info(0, mini2440_i2c_devs,  
  55. 676                ARRAY_SIZE(mini2440_i2c_devs));  
  56. 677  
  57. 678   platform_add_devices(mini2440_devices, ARRAY_SIZE(mini2440_devices));  
  58. 679  
  59. 680   if (features.count) /* the optional features */  
  60. 681       platform_add_devices(features.optional, features.count);  
  61. 682  
  62. 683}  


 

这个函数我们这里只需要关注675行,调用i2c_register_board_info注册mini2440_i2c_devs。mini2440_i2c_devs就是上面定义的i2c_board_info结构体数组,其中定义了at24c08相关信息。

i2c_register_board_info函数定义在drivers/i2c/i2c-boardinfo.c文件中,其内容如下:

 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. 42/** 
  2. 43 * i2c_register_board_info - staticallydeclare I2C devices 
  3. 44 * @busnum: identifies the bus to whichthese devices belong 
  4. 45 * @info: vector of i2c devicedescriptors 
  5. 46 * @len: how many descriptors in the vector;may be zero to reserve 
  6. 47 * the specified bus number. 
  7. 48 * 
  8. 49 * Systems using the Linux I2C driverstack can declare tables of board info 
  9. 50 * while they initialize.  This should be done in board-specific initcode 
  10. 51 * near arch_initcall() time, orequivalent, before any I2C adapter driver is 
  11. 52 * registered.  For example, mainboard init code could defineseveral devices, 
  12. 53 * as could the init code for eachdaughtercard in a board stack. 
  13. 54 * 
  14. 55 * The I2C devices will be created later,after the adapter for the relevant 
  15. 56 * bus has been registered.  After that moment, standard driver modeltools 
  16. 57 * are used to bind "new style"I2C drivers to the devices.  The busnumber 
  17. 58 * for any device declared using thisroutine is not available for dynamic 
  18. 59 * allocation. 
  19. 60 * 
  20. 61 * The board info passed can safely be__initdata, but be careful of embedded 
  21. 62 * pointers (for platform_data,functions, etc) since that won't be copied. 
  22. 63 */  
  23. 64int __init  
  24. 65i2c_register_board_info(int busnum,  
  25. 66   struct i2c_board_info const *info, unsigned len)  
  26. 67{  
  27. 68   int status;  
  28. 69  
  29. 70   down_write(&__i2c_board_lock);  
  30. 71  
  31. 72   /* dynamic bus numbers will be assigned after the last static one */  
  32. 73   if (busnum >= __i2c_first_dynamic_bus_num)  
  33. 74       __i2c_first_dynamic_bus_num = busnum + 1;  
  34. 75  
  35. 76   for (status = 0; len; len--, info++) {  
  36. 77       struct i2c_devinfo  *devinfo;  
  37. 78  
  38. 79       devinfo = kzalloc(sizeof(*devinfo), GFP_KERNEL);  
  39. 80       if (!devinfo) {  
  40. 81            pr_debug("i2c-core: can'tregister boardinfo!\n");  
  41. 82            status = -ENOMEM;  
  42. 83            break;  
  43. 84       }  
  44. 85  
  45. 86       devinfo->busnum = busnum;  
  46. 87       devinfo->board_info = *info;  
  47. 88       list_add_tail(&devinfo->list, &__i2c_board_list);  
  48. 89   }  
  49. 90  
  50. 91   up_write(&__i2c_board_lock);  
  51. 92  
  52. 93   return status;  
  53. 94}  


 

第一个参数busnum表明I2C设备所依附的I2C总线的总线号。

72-74行,如果参数传递进来的busnum 大于等于__i2c_first_dynamic_bus_num,则将__i2c_first_dynamic_bus_num设置为busnum+1。__i2c_first_dynamic_bus_num代表第一个动态分配的I2C总线号,所以,动态分配的I2C总线号总是大于静态分配的I2C总线号。

__i2c_first_dynamic_bus_num定义在drivers/i2c/i2c-boardinfo.c文件中:

 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. 38int __i2c_first_dynamic_bus_num;  


 

因为__i2c_first_dynamic_bus_num是一个全局变量,所以其值被自动初始化为0。所以73行的if判断是成立的。

76-89行,循环遍历info数组,用i2c_board_info变量初始化structi2c_devinfo指针变量devinfo,然后将devinfo放入__i2c_board_list链表中。

i2c_devinfo变量定义在drivers/i2c/i2c-core.h文件中:

 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. 22struct i2c_devinfo {  
  2. 23   struct list_head    list;  
  3. 24   int         busnum;  
  4. 25   struct i2c_board_info  board_info;  
  5. 26};  


 

可以看到i2c_devinfo结构体只有三个成员变量,busnum用来保存I2C设备所依附的I2C总线号。board_info用来保存I2C设备相关信息。list用于链接到全局链表__i2c_board_list中。

__i2c_board_list 链表定义在drivers/i2c/i2c-boardinfo.c文件中:

 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. 35LIST_HEAD(__i2c_board_list);  
  2. 36EXPORT_SYMBOL_GPL(__i2c_board_list);  


 

全局变量__i2c_board_list上挂载着所有的I2C设备的信息,包括I2C设备所在的I2C总线的总线号。

所以,i2c_register_board_info函数并没有创建i2c_client,只是将i2c_devinfo变量放入__i2c_board_list链表中。那么,什么时候才会创建i2c_client呢?这个就与创建I2C设备驱动程序无关了,而是跟I2C adapter驱动程序有关:

注册一个I2C adapter有两种方法,一是调用i2c_add_adapter函数,二是调用i2c_add_numbered_adapter函数。

先来看i2c_add_adapter函数,它定义在drivers/i2c/i2c-core.c文件中:

 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. 1099/** 
  2. 1100 * i2c_add_adapter - declare i2cadapter, use dynamic bus number 
  3. 1101 * @adapter: the adapter to add 
  4. 1102 * Context: can sleep 
  5. 1103 * 
  6. 1104 * This routine is used to declare anI2C adapter when its bus number 
  7. 1105 * doesn't matter or when its busnumber is specified by an dt alias. 
  8. 1106 * Examples of bases when the busnumber doesn't matter: I2C adapters 
  9. 1107 * dynamically added by USB links orPCI plugin cards. 
  10. 1108 * 
  11. 1109 * When this returns zero, a new busnumber was allocated and stored 
  12. 1110 * in adap->nr, and the specifiedadapter became available for clients. 
  13. 1111 * Otherwise, a negative errno value isreturned. 
  14. 1112 */  
  15. 1113int i2c_add_adapter(struct i2c_adapter*adapter)  
  16. 1114{  
  17. 1115   struct device *dev = &adapter->dev;  
  18. 1116   int id;  
  19. 1117  
  20. 1118   if (dev->of_node) {  
  21. 1119       id = of_alias_get_id(dev->of_node, "i2c");  
  22. 1120       if (id >= 0) {  
  23. 1121            adapter->nr = id;  
  24. 1122            return__i2c_add_numbered_adapter(adapter);  
  25. 1123       }  
  26. 1124   }  
  27. 1125  
  28. 1126   mutex_lock(&core_lock);  
  29. 1127   id = idr_alloc(&i2c_adapter_idr, adapter,  
  30. 1128               __i2c_first_dynamic_bus_num, 0,GFP_KERNEL);  
  31. 1129   mutex_unlock(&core_lock);  
  32. 1130   if (id < 0)  
  33. 1131       return id;  
  34. 1132  
  35. 1133   adapter->nr = id;  
  36. 1134  
  37. 1135   return i2c_register_adapter(adapter);  
  38. 1136}  


 

该函数为参数指定的i2c_adapter动态分配的个I2C总线号,并注册该i2c_adapter。

1118-1124行,忽略。

1127-1133行,调用idr_alloc,动态分配一个id号,并将该id号做为i2c_adapter的I2C总线号。

关于idr机制,我们不详细分析,只需要知道它是一种快速索引机制,它将一个整数ID与一个需要被索引的指针建立联系,方便进行查找。例如,这里idr_alloc返回的id与i2c_adapter建立了索引。idr_alloc的第三个参数指定返回ID的最小值(闭区间),第四个参数指定返回ID的最大值(开区间),即idr_alloc返回的ID大于等于第三个参数,而且必须小于第四个参数。在这个函数中,第三个参数为__i2c_first_dynamic_bus_num,所以返回的id必然是大于等于__i2c_first_dynamic_bus_num的,即动态分配的I2C总线号必然大于__i2c_first_dynamic_bus_num。

1135行,调用i2c_register_adapter注册i2c_adapter。

下面我们来看i2c_add_numbered_adapter,该函数定义在drivers/i2c/i2c-core.c文件中:

 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. 1139/** 
  2. 1140 * i2c_add_numbered_adapter - declarei2c adapter, use static bus number 
  3. 1141 * @adap: the adapter to register (withadap->nr initialized) 
  4. 1142 * Context: can sleep 
  5. 1143 * 
  6. 1144 * This routine is used to declare anI2C adapter when its bus number 
  7. 1145 * matters.  For example, use it for I2C adapters fromsystem-on-chip CPUs, 
  8. 1146 * or otherwise built in to thesystem's mainboard, and where i2c_board_info 
  9. 1147 * is used to properly configure I2Cdevices. 
  10. 1148 * 
  11. 1149 * If the requested bus number is setto -1, then this function will behave 
  12. 1150 * identically to i2c_add_adapter, andwill dynamically assign a bus number. 
  13. 1151 * 
  14. 1152 * If no devices have pre-been declaredfor this bus, then be sure to 
  15. 1153 * register the adapter before anydynamically allocated ones.  Otherwise 
  16. 1154 * the required bus ID may not beavailable. 
  17. 1155 * 
  18. 1156 * When this returns zero, thespecified adapter became available for 
  19. 1157 * clients using the bus numberprovided in adap->nr.  Also, the table 
  20. 1158 * of I2C devices pre-declared usingi2c_register_board_info() is scanned, 
  21. 1159 * and the appropriate driver modeldevice nodes are created.  Otherwise, a 
  22. 1160 * negative errno value is returned. 
  23. 1161 */  
  24. 1162int i2c_add_numbered_adapter(structi2c_adapter *adap)  
  25. 1163{  
  26. 1164   if (adap->nr == -1) /* -1 means dynamically assign bus id */  
  27. 1165       return i2c_add_adapter(adap);  
  28. 1166  
  29. 1167   return __i2c_add_numbered_adapter(adap);  
  30. 1168}  


 

该函数用于注册参数指定的i2c_adapter,该i2c_adapter的I2C总线号即i2c_adapter.nr必须已经静态设置好了。如果adap->nr为-1,则调用i2c_add_adapter动态分配I2C总线号,再注册。否则,调用__i2c_add_numbered_adapter函数。

__i2c_add_numbered_adapter函数定义在drivers/i2c/i2c-core.c文件中:

 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. 1078/** 
  2. 1079 * __i2c_add_numbered_adapter -i2c_add_numbered_adapter where nr is never -1 
  3. 1080 * @adap: the adapter to register (withadap->nr initialized) 
  4. 1081 * Context: can sleep 
  5. 1082 * 
  6. 1083 * See i2c_add_numbered_adapter() fordetails. 
  7. 1084 */  
  8. 1085static int__i2c_add_numbered_adapter(struct i2c_adapter *adap)  
  9. 1086{  
  10. 1087   int id;  
  11. 1088  
  12. 1089   mutex_lock(&core_lock);  
  13. 1090   id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,  
  14. 1091               GFP_KERNEL);  
  15. 1092   mutex_unlock(&core_lock);  
  16. 1093   if (id < 0)  
  17. 1094       return id == -ENOSPC ? -EBUSY : id;  
  18. 1095  
  19. 1096   return i2c_register_adapter(adap);  
  20. 1097}  


 

1090-1091行,调用idr_alloc函数,建立idr索引关系。返回的id必须大于等于第三个参数adap->nr,同时必须小于第四个参数adap->nr+1,所以返回的id必然等于adap->nr,即静态指定的I2C总线号。

1096行,调用i2c_register_adapter注册i2c_adapter。

到这里,我们就知道了注册一个i2c_adapter可以调用i2c_add_numbered_adapter函数静态指定一个I2C总线号,也可以调用i2c_add_adapter函数动态分配一个I2C总线号。但是不论哪种方法,最后的注册工作都必须通过调用i2c_register_adapter函数完成。

i2c_register_adapter函数定义在drivers/i2c/i2c-core.c文件中,其内容如下:

 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1.  980staticint i2c_register_adapter(struct i2c_adapter *adap)  
  2.  981{  
  3.  982   int res = 0;  
  4.  983  
  5.  984   /* Can't register until after driver model init */  
  6.  985   if (unlikely(WARN_ON(!i2c_bus_type.p))) {  
  7.  986       res = -EAGAIN;  
  8.  987       goto out_list;  
  9.  988    }  
  10.  989  
  11.  990   /* Sanity checks */  
  12.  991   if (unlikely(adap->name[0] == '\0')) {  
  13.  992       pr_err("i2c-core: Attempt to register an adapter with "  
  14.  993               "no name!\n");  
  15.  994       return -EINVAL;  
  16.  995    }  
  17.  996   if (unlikely(!adap->algo)) {  
  18.  997       pr_err("i2c-core: Attempt to register adapter '%s' with "  
  19.  998               "no algo!\n",adap->name);  
  20.  999       return -EINVAL;  
  21. 1000   }  
  22. 1001  
  23. 1002   rt_mutex_init(&adap->bus_lock);  
  24. 1003   mutex_init(&adap->userspace_clients_lock);  
  25. 1004   INIT_LIST_HEAD(&adap->userspace_clients);  
  26. 1005  
  27. 1006   /* Set default timeout to 1 second if not already set */  
  28. 1007   if (adap->timeout == 0)  
  29. 1008       adap->timeout = HZ;  
  30. 1009  
  31. 1010   dev_set_name(&adap->dev, "i2c-%d", adap->nr);  
  32. 1011   adap->dev.bus = &i2c_bus_type;  
  33. 1012   adap->dev.type = &i2c_adapter_type;  
  34. 1013   res = device_register(&adap->dev);  
  35. 1014   if (res)  
  36. 1015       goto out_list;  
  37. 1016  
  38. 1017   dev_dbg(&adap->dev, "adapter [%s] registered\n",adap->name);  
  39. 1018  
  40. 1019#ifdef CONFIG_I2C_COMPAT  
  41. 1020   res = class_compat_create_link(i2c_adapter_compat_class,&adap->dev,  
  42. 1021                       adap->dev.parent);  
  43. 1022   if (res)  
  44. 1023       dev_warn(&adap->dev,  
  45. 1024             "Failed to createcompatibility class link\n");  
  46. 1025#endif  
  47. 1026  
  48. 1027   /* bus recovery specific initialization */  
  49. 1028   if (adap->bus_recovery_info) {  
  50. 1029       struct i2c_bus_recovery_info*bri = adap->bus_recovery_info;  
  51. 1030  
  52. 1031       if (!bri->recover_bus) {  
  53. 1032            dev_err(&adap->dev, "Norecover_bus() found, not using recovery\n");  
  54. 1033            adap->bus_recovery_info = NULL;  
  55. 1034            goto exit_recovery;  
  56. 1035       }  
  57. 1036  
  58. 1037       /* Generic GPIO recovery */  
  59. 1038       if (bri->recover_bus == i2c_generic_gpio_recovery) {  
  60. 1039            if(!gpio_is_valid(bri->scl_gpio)) {  
  61. 1040                dev_err(&adap->dev,"Invalid SCL gpio, not using recovery\n");  
  62. 1041                adap->bus_recovery_info =NULL;  
  63. 1042                goto exit_recovery;  
  64. 1043            }  
  65. 1044  
  66. 1045            if(gpio_is_valid(bri->sda_gpio))  
  67. 1046                bri->get_sda =get_sda_gpio_value;  
  68. 1047           else  
  69. 1048                bri->get_sda = NULL;  
  70. 1049  
  71. 1050            bri->get_scl =get_scl_gpio_value;  
  72. 1051            bri->set_scl =set_scl_gpio_value;  
  73. 1052       } else if (!bri->set_scl || !bri->get_scl) {  
  74. 1053            /* Generic SCL recovery */  
  75. 1054            dev_err(&adap->dev, "No{get|set}_gpio() found, not using recovery\n");  
  76. 1055            adap->bus_recovery_info = NULL;  
  77. 1056       }  
  78. 1057   }  
  79. 1058  
  80. 1059exit_recovery:  
  81. 1060   /* create pre-declared device nodes */  
  82. 1061   if (adap->nr < __i2c_first_dynamic_bus_num)  
  83. 1062       i2c_scan_static_board_info(adap);  
  84. 1063  
  85. 1064   /* Notify drivers */  
  86. 1065   mutex_lock(&core_lock);  
  87. 1066   bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);  
  88. 1067   mutex_unlock(&core_lock);  
  89. 1068  
  90. 1069   return 0;  
  91. 1070  
  92. 1071out_list:  
  93. 1072   mutex_lock(&core_lock);  
  94. 1073   idr_remove(&i2c_adapter_idr, adap->nr);  
  95. 1074   mutex_unlock(&core_lock);  
  96. 1075   return res;  
  97. 1076}  


 

984-1000行,做一些必要的检查工作。

1002-1008行,做一些简单的i2c_adapter成员变量初始化工作。

1010-1013行,初始化i2c_adapter.dev,并调用device_register注册i2c_adapter.dev。

1028-1057行,I2C总线恢复时相关的初始化工作。

1060-1062行,因为动态分析的I2C总线号必须大于或等于__i2c_first_dynamic_bus_num,所以,如果adap->nr< __i2c_first_dynamic_bus_num,说明是静态指定的I2C总线号,此时,要调用i2c_scan_static_board_info函数探测I2C总线上静态注册的I2C设备。

i2c_scan_static_board_info函数定义在drivers/i2c/i2c-core.c文件中,其内容如下:

 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. 941staticvoid i2c_scan_static_board_info(struct i2c_adapter *adapter)  
  2. 942{  
  3. 943   struct i2c_devinfo  *devinfo;  
  4. 944  
  5. 945   down_read(&__i2c_board_lock);  
  6. 946   list_for_each_entry(devinfo, &__i2c_board_list, list) {  
  7. 947       if (devinfo->busnum == adapter->nr  
  8. 948                &&!i2c_new_device(adapter,  
  9. 949                       &devinfo->board_info))  
  10. 950           dev_err(&adapter->dev,  
  11. 951                "Can't create device at0x%02x\n",  
  12. 952                devinfo->board_info.addr);  
  13. 953    }  
  14. 954   up_read(&__i2c_board_lock);  
  15. 955}  


 

946-953行,遍历__i2c_board_list链表,如果devinfo->busnum等于adapter->nr,即链表节点所代表的I2C设备的I2C总线号等于i2c_adapter的I2C总线号,则调用i2c_new_device创建并注册I2C设备对应的i2c_client结构体。

到这里,就和我们在本文开始部分介绍的静态注册的i2c_board_info信息联系起来了。回忆一下我们通过调用i2c_register_board_info函数将将包含所有I2C设备的i2c_board_info信息的i2c_devinfo变量加入到__i2c_board_list链表中。现在在注册i2c_adater时,又通过调用i2c_scan_static_board_info函数遍历__i2c_board_list链表,查找属于对应i2c_adapter的I2C设备,如果找到,调用i2c_new_device创建对应的i2c_client结构体。

i2c_new_device函数定义在drivers/i2c/i2c-core.c文件中,其内容如下:

 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. 612/** 
  2. 613* i2c_new_device - instantiate an i2c device 
  3. 614* @adap: the adapter managing the device 
  4. 615* @info: describes one I2C device; bus_num is ignored 
  5. 616* Context: can sleep 
  6. 617* 
  7. 618* Create an i2c device. Binding is handled through driver model 
  8. 619* probe()/remove() methods.  A driver maybe bound to this device when we 
  9. 620* return from this function, or any later moment (e.g. maybe hotplugging will 
  10. 621* load the driver module).  This call isnot appropriate for use by mainboard 
  11. 622* initialization logic, which usually runs during an arch_initcall() long 
  12. 623* before any i2c_adapter could exist. 
  13. 624* 
  14. 625* This returns the new i2c client, which may be saved for later use with 
  15. 626* i2c_unregister_device(); or NULL to indicate an error. 
  16. 627*/  
  17. 628struct i2c_client *  
  18. 629i2c_new_device(struct i2c_adapter *adap,struct i2c_board_info const *info)  
  19. 630{  
  20. 631   struct i2c_client   *client;  
  21. 632   int         status;  
  22. 633  
  23. 634   client = kzalloc(sizeof *client, GFP_KERNEL);  
  24. 635   if (!client)  
  25. 636       return NULL;  
  26. 637  
  27. 638   client->adapter = adap;  
  28. 639  
  29. 640   client->dev.platform_data = info->platform_data;  
  30. 641  
  31. 642   if (info->archdata)  
  32. 643       client->dev.archdata = *info->archdata;  
  33. 644  
  34. 645   client->flags = info->flags;  
  35. 646   client->addr = info->addr;  
  36. 647   client->irq = info->irq;  
  37. 648  
  38. 649   strlcpy(client->name, info->type, sizeof(client->name));  
  39. 650  
  40. 651   /* Check for address validity */  
  41. 652   status = i2c_check_client_addr_validity(client);  
  42. 653   if (status) {  
  43. 654        dev_err(&adap->dev,"Invalid %d-bit I2C address 0x%02hx\n",  
  44. 655            client->flags & I2C_CLIENT_TEN ?10 : 7, client->addr);  
  45. 656       goto out_err_silent;  
  46. 657    }  
  47. 658  
  48. 659   /* Check for address business */  
  49. 660   status = i2c_check_addr_busy(adap, client->addr);  
  50. 661   if (status)  
  51. 662       goto out_err;  
  52. 663  
  53. 664   client->dev.parent = &client->adapter->dev;  
  54. 665   client->dev.bus = &i2c_bus_type;  
  55. 666   client->dev.type = &i2c_client_type;  
  56. 667   client->dev.of_node = info->of_node;  
  57. 668   ACPI_HANDLE_SET(&client->dev, info->acpi_node.handle);  
  58. 669  
  59. 670   /* For 10-bit clients, add an arbitrary offset to avoid collisions */  
  60. 671   dev_set_name(&client->dev, "%d-%04x",i2c_adapter_id(adap),  
  61. 672            client->addr | ((client->flags & I2C_CLIENT_TEN)  
  62. 673                     ? 0xa000 : 0));  
  63. 674    status =device_register(&client->dev);  
  64. 675   if (status)  
  65. 676       goto out_err;  
  66. 677  
  67. 678   dev_dbg(&adap->dev, "client [%s] registered with bus id%s\n",  
  68. 679       client->name, dev_name(&client->dev));  
  69. 680  
  70. 681   return client;  
  71. 682  
  72. 683out_err:  
  73. 684   dev_err(&adap->dev, "Failed to register i2c client %s at0x%02x "  
  74. 685       "(%d)\n", client->name, client->addr, status);  
  75. 686out_err_silent:  
  76. 687   kfree(client);  
  77. 688   return NULL;  
  78. 689}  


 

这个函数根据参数传递进来的信息,创建并注册i2c_client。

634行,为i2c_client变量分配内存空间。

638行,将i2c_client.adapter设置为参数传递进来的i2c_adapter。

640 行,将i2c_client.dev.platform_data设置为 info->platform_data;

645行,将i2c_client.flags设置为info->flags;

646行,将i2c_client.addr设置为info->addr;

647行,将i2c_client.irq设置为info->irq;

649行,将i2c_client.name设置为info->type;

651-657行,检查指定的I2C设备地址是否有效。

660-662行,检查指定的I2C设备地址是否忙。

664-674行,初始化i2c_client.dev,并调用device_register注册i2c_client.dev。

i2c_register_adapter函数我们还有一点内容没有分析完,回到i2c_register_adapter函数,上面我们分析到了1062行的i2c_scan_static_board_info(adap),只有在adap->nr小于__i2c_first_dynamic_bus_num即静态指定了i2c_adapter的I2C总线号的情况下,才会调用这个函数。对于动态分配I2C总线号的情况,直接跳过。这就说明,只有在静态指定i2c_adapter的I2C总线号的情况下,才会扫描静态注册的I2C设备,如果动态指定i2c_adapter的I2C总线号,不会扫描静态注册的I2C设备。对于I2C驱动程序,这种静态注册的方式,称为new-style driver;而动态探测的方式,称为legacy driver。

下面我们继续分析i2c_register_adapter函数,前面调用i2c_scan_static_board_info实例化了静态注册的I2C设备,下面要完成对I2C设备的动态探测和实例化:

不论是静态指定还是动态分配I2C总线号,都会调用到1066行的bus_for_each_drv函数。

1066行,调用bus_for_each_drv函数,该函数定义在drivers/base/bus.c文件中,其内容如下:
  1. 417/** 
  2. 418 * bus_for_each_drv - driver iterator 
  3. 419 * @bus: bus we're dealing with. 
  4. 420 * @start: driver to start iterating on. 
  5. 421 * @data: data to pass to the callback. 
  6. 422 * @fn: function to call for each driver. 
  7. 423 * 
  8. 424 * This is nearly identical to the device iterator above. 
  9. 425 * We iterate over each driver that belongs to @bus, and call 
  10. 426 * @fn for each. If @fn returns anything but 0, we break out 
  11. 427 * and return it. If @start is not NULL, we use it as the head 
  12. 428 * of the list. 
  13. 429 * 
  14. 430 * NOTE: we don't return the driver that returns a non-zero 
  15. 431 * value, nor do we leave the reference count incremented for that 
  16. 432 * driver. If the caller needs to know that info, it must set it 
  17. 433 * in the callback. It must also be sure to increment the refcount 
  18. 434 * so it doesn't disappear before returning to the caller. 
  19. 435 */  
  20. 436int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,  
  21. 437             void *data, int (*fn)(struct device_driver *, void *))  
  22. 438{  
  23. 439    struct klist_iter i;  
  24. 440    struct device_driver *drv;  
  25. 441    int error = 0;  
  26. 442  
  27. 443    if (!bus)  
  28. 444        return -EINVAL;  
  29. 445  
  30. 446    klist_iter_init_node(&bus->p->klist_drivers, &i,  
  31. 447                 start ? &start->p->knode_bus : NULL);  
  32. 448    while ((drv = next_driver(&i)) && !error)  
  33. 449        error = fn(drv, data);  
  34. 450    klist_iter_exit(&i);  
  35. 451    return error;  
  36. 452}  


这个函数我们在《Linux设备模型分析之device(基于3.10.1内核)》一文中已经分析过了。448-449行,这个while循环依次遍历bus->p->klist_drivers中的所有device_driver,对于每个device_driver,调用fn(drv,data)函数。这里,传递过来的fn参数是__process_new_adapter,data参数是adap。这个循环是一个关键点,注册一个新的i2c_adapter后,要为该i2c_adapter上的i2c设备匹配驱动程序,这个匹配过程就是通过这个循环调用__process_new_adapter函数完成的。
__process_new_adapter函数定义在drivers/i2c/i2c-core.c文件中,其内容如下:
  1. 975static int __process_new_adapter(struct device_driver *d, void *data)  
  2. 976{  
  3. 977    return i2c_do_add_adapter(to_i2c_driver(d), data);  
  4. 978}  


注意i2c_do_add_adapter函数的第一个参数,从device_driver转换为i2c_driver。
i2c_do_add_adapter函数定义在drivers/i2c/i2c-core.c文件中,其内容如下:
  1. 957static int i2c_do_add_adapter(struct i2c_driver *driver,  
  2. 958                  struct i2c_adapter *adap)  
  3. 959{  
  4. 960    /* Detect supported devices on that bus, and instantiate them */  
  5. 961    i2c_detect(adap, driver);  
  6. 962  
  7. 963    /* Let legacy drivers scan this bus for matching devices */  
  8. 964    if (driver->attach_adapter) {  
  9. 965        dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",  
  10. 966             driver->driver.name);  
  11. 967        dev_warn(&adap->dev, "Please use another way to instantiate "  
  12. 968             "your i2c_client\n");  
  13. 969        /* We ignore the return code; if it fails, too bad */  
  14. 970        driver->attach_adapter(adap);  
  15. 971    }  
  16. 972    return 0;  
  17. 973}  


961行,调用i2c_detect函数,探测并初始化该i2c总线上的i2c设备。该函数定义在drivers/i2c/i2c-core.c文件中,其内容如下:
  1. 1730static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)  
  2. 1731{  
  3. 1732    const unsigned short *address_list;  
  4. 1733    struct i2c_client *temp_client;  
  5. 1734    int i, err = 0;  
  6. 1735    int adap_id = i2c_adapter_id(adapter);  
  7. 1736  
  8. 1737    address_list = driver->address_list;  
  9. 1738    if (!driver->detect || !address_list)  
  10. 1739        return 0;  
  11. 1740  
  12. 1741    /* Stop here if the classes do not match */  
  13. 1742    if (!(adapter->class & driver->class))  
  14. 1743        return 0;  
  15. 1744  
  16. 1745    /* Set up a temporary client to help detect callback */  
  17. 1746    temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);  
  18. 1747    if (!temp_client)  
  19. 1748        return -ENOMEM;  
  20. 1749    temp_client->adapter = adapter;  
  21. 1750  
  22. 1751    for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {  
  23. 1752        dev_dbg(&adapter->dev, "found normal entry for adapter %d, "  
  24. 1753            "addr 0x%02x\n", adap_id, address_list[i]);  
  25. 1754        temp_client->addr = address_list[i];  
  26. 1755        err = i2c_detect_address(temp_client, driver);  
  27. 1756        if (unlikely(err))  
  28. 1757            break;  
  29. 1758    }  
  30. 1759  
  31. 1760    kfree(temp_client);  
  32. 1761    return err;  
  33. 1762}  


1755行,调用i2c_detect_address,探测指定的地址上的I2C设备是否存在,如果存在,注册该i2c设备。i2c_detect_address函数定义在drivers/i2c/i2c-core.c文件中,其内容如下:
  1. 1675static int i2c_detect_address(struct i2c_client *temp_client,  
  2. 1676                  struct i2c_driver *driver)  
  3. 1677{  
  4. 1678    struct i2c_board_info info;  
  5. 1679    struct i2c_adapter *adapter = temp_client->adapter;  
  6. 1680    int addr = temp_client->addr;  
  7. 1681    int err;  
  8. 1682  
  9. 1683    /* Make sure the address is valid */  
  10. 1684    err = i2c_check_addr_validity(addr);  
  11. 1685    if (err) {  
  12. 1686        dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",  
  13. 1687             addr);  
  14. 1688        return err;  
  15. 1689    }  
  16. 1690  
  17. 1691    /* Skip if already in use */  
  18. 1692    if (i2c_check_addr_busy(adapter, addr))  
  19. 1693        return 0;  
  20. 1694  
  21. 1695    /* Make sure there is something at this address */  
  22. 1696    if (!i2c_default_probe(adapter, addr))  
  23. 1697        return 0;  
  24. 1698  
  25. 1699    /* Finally call the custom detection function */  
  26. 1700    memset(&info, 0, sizeof(struct i2c_board_info));  
  27. 1701    info.addr = addr;  
  28. 1702    err = driver->detect(temp_client, &info);  
  29. 1703    if (err) {  
  30. 1704        /* -ENODEV is returned if the detection fails. We catch it 
  31. 1705           here as this isn't an error. */  
  32. 1706        return err == -ENODEV ? 0 : err;  
  33. 1707    }  
  34. 1708  
  35. 1709    /* Consistency check */  
  36. 1710    if (info.type[0] == '\0') {  
  37. 1711        dev_err(&adapter->dev, "%s detection function provided "  
  38. 1712            "no name for 0x%x\n", driver->driver.name,  
  39. 1713            addr);  
  40. 1714    } else {  
  41. 1715        struct i2c_client *client;  
  42. 1716  
  43. 1717        /* Detection succeeded, instantiate the device */  
  44. 1718        dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",  
  45. 1719            info.type, info.addr);  
  46. 1720        client = i2c_new_device(adapter, &info);  
  47. 1721        if (client)  
  48. 1722            list_add_tail(&client->detected, &driver->clients);  
  49. 1723        else  
  50. 1724            dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",  
  51. 1725                info.type, info.addr);  
  52. 1726    }  
  53. 1727    return 0;  
  54. 1728}  


1702行,调用driver->detect。
1720行,如果探测到i2c设备确实存在,调用i2c_new_device函数初始化对应的i2c_client结构体并注册。i2c_new_device函数我们在前面已经分析过。
至此,i2c_adapter的注册过程我们就清楚了。同时,我们对I2C设备的注册定义和注册也就完全清楚了。

 

二、定义和注册I2C设备驱动

步骤1:定义i2c_driver结构体变量。

Linux I2C设备驱动程序对应的结构体是structi2c_driver,定义在include/linux/i2c.h文件中:

 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. 124/** 
  2. 125 * struct i2c_driver - represent an I2Cdevice driver 
  3. 126 * @class: What kind of i2c device weinstantiate (for detect) 
  4. 127 * @attach_adapter: Callback for busaddition (deprecated) 
  5. 128 * @probe: Callback for device binding 
  6. 129 * @remove: Callback for deviceunbinding 
  7. 130 * @shutdown: Callback for deviceshutdown 
  8. 131 * @suspend: Callback for device suspend 
  9. 132 * @resume: Callback for device resume 
  10. 133 * @alert: Alert callback, for examplefor the SMBus alert protocol 
  11. 134 * @command: Callback for bus-wide signaling(optional) 
  12. 135 * @driver: Device driver model driver 
  13. 136 * @id_table: List of I2C devicessupported by this driver 
  14. 137 * @detect: Callback for devicedetection 
  15. 138 * @address_list: The I2C addresses toprobe (for detect) 
  16. 139 * @clients: List of detected clients wecreated (for i2c-core use only) 
  17. 140 * 
  18. 141 * The driver.owner field should be setto the module owner of this driver. 
  19. 142 * The driver.name field should be setto the name of this driver. 
  20. 143 * 
  21. 144 * For automatic device detection, both @detectand @address_list must 
  22. 145 * be defined. @class should also beset, otherwise only devices forced 
  23. 146 * with module parameters will becreated. The detect function must 
  24. 147 * fill at least the name field of thei2c_board_info structure it is 
  25. 148 * handed upon successful detection, andpossibly also the flags field. 
  26. 149 * 
  27. 150 * If @detect is missing, the driverwill still work fine for enumerated 
  28. 151 * devices. Detected devices simplywon't be supported. This is expected 
  29. 152 * for the many I2C/SMBus devices whichcan't be detected reliably, and 
  30. 153 * the ones which can always beenumerated in practice. 
  31. 154 * 
  32. 155 * The i2c_client structure which ishanded to the @detect callback is 
  33. 156 * not a real i2c_client. It isinitialized just enough so that you can 
  34. 157 * call i2c_smbus_read_byte_data andfriends on it. Don't do anything 
  35. 158 * else with it. In particular, callingdev_dbg and friends on it is 
  36. 159 * not allowed. 
  37. 160 */  
  38. 161struct i2c_driver {  
  39. 162   unsigned int class;  
  40. 163  
  41. 164   /* Notifies the driver that a new bus has appeared. You should avoid 
  42. 165    * using this, it will be removed in a near future. 
  43. 166    */  
  44. 167   int (*attach_adapter)(struct i2c_adapter *) __deprecated;  
  45. 168  
  46. 169   /* Standard driver model interfaces */  
  47. 170   int (*probe)(struct i2c_client *, const struct i2c_device_id *);  
  48. 171   int (*remove)(struct i2c_client *);  
  49. 172  
  50. 173   /* driver model interfaces that don't relate to enumeration  */  
  51. 174   void (*shutdown)(struct i2c_client *);  
  52. 175   int (*suspend)(struct i2c_client *, pm_message_t mesg);  
  53. 176   int (*resume)(struct i2c_client *);  
  54. 177  
  55. 178   /* Alert callback, for example for the SMBus alert protocol. 
  56. 179    * The format and meaning of the data value depends on the protocol. 
  57. 180    * For the SMBus alert protocol, there is a single bit of data passed 
  58. 181    * as the alert response's low bit ("event flag"). 
  59. 182    */  
  60. 183   void (*alert)(struct i2c_client *, unsigned int data);  
  61. 184  
  62. 185   /* a ioctl like command that can be used to perform specific functions 
  63. 186    * with the device. 
  64. 187    */  
  65. 188   int (*command)(struct i2c_client *client, unsigned int cmd, void *arg);  
  66. 189  
  67. 190   struct device_driver driver;  
  68. 191   const struct i2c_device_id *id_table;  
  69. 192  
  70. 193   /* Device detection callback for automatic device creation */  
  71. 194   int (*detect)(struct i2c_client *, struct i2c_board_info *);  
  72. 195   const unsigned short *address_list;  
  73. 196   struct list_head clients;  
  74. 197};  


 

注意注释中的说明,driver.name和driver.owner必须初始化。

id_table成员变量是i2c_device_id结构体指针,它用来保存该i2c_driver支持的I2C设备列表。

struct i2c_device_id定义在include/linux/mod_devicetable.h文件中,其内容如下:

 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. 419struct i2c_device_id {  
  2. 420   char name[I2C_NAME_SIZE];  
  3. 421   kernel_ulong_t driver_data; /* Data private to the driver */  
  4. 422};  


 

对于Mini2440的I2C设备at24c08 EEPROM,其对应的i2c_driver结构体变量实现在drivers/misc/eeprom/at24.c文件中:

 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. 677static struct i2c_driver at24_driver = {  
  2. 678   .driver = {  
  3. 679       .name = "at24",  
  4. 680       .owner = THIS_MODULE,  
  5. 681   },  
  6. 682   .probe = at24_probe,  
  7. 683   .remove = at24_remove,  
  8. 684   .id_table = at24_ids,  
  9. 685};  


 

678-681行,初始化了要求必须初始化的i2c_driver.driver.name和i2c_driver.driver.owner。

682行,指定i2c_driver.probe函数为at24_probe。

683行,指定i2c_driver.remove函数为at24_remove。

684行,指定i2c_driver.id_table为at24_ids。

at24_ids定义在drivers/misc/eeprom/at24.c文件中,其内容如下:

 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. 99#define AT24_SIZE_BYTELEN 5  
  2. 100#define AT24_SIZE_FLAGS 8  
  3. 101  
  4. 102#define AT24_BITMASK(x) (BIT(x) - 1)  
  5. 103  
  6. 104/* create non-zero magic value for giveneeprom parameters */  
  7. 105#define AT24_DEVICE_MAGIC(_len,_flags)         \  
  8. 106   ((1 << AT24_SIZE_FLAGS | (_flags))      \  
  9. 107       << AT24_SIZE_BYTELEN | ilog2(_len))  
  10. 108  
  11. 109static const struct i2c_device_idat24_ids[] = {  
  12. 110   /* needs 8 addresses as A0-A2 are ignored */  
  13. 111   { "24c00", AT24_DEVICE_MAGIC(128 / 8, AT24_FLAG_TAKE8ADDR) },  
  14. 112   /* old variants can't be handled with this generic entry! */  
  15. 113   { "24c01", AT24_DEVICE_MAGIC(1024 / 8, 0) },  
  16. 114   { "24c02", AT24_DEVICE_MAGIC(2048 / 8, 0) },  
  17. 115   /* spd is a 24c02 in memory DIMMs */  
  18. 116   { "spd", AT24_DEVICE_MAGIC(2048 / 8,  
  19. 117       AT24_FLAG_READONLY | AT24_FLAG_IRUGO) },  
  20. 118   { "24c04", AT24_DEVICE_MAGIC(4096 / 8, 0) },  
  21. 119   /* 24rf08 quirk is handled at i2c-core */  
  22. 120   { "24c08", AT24_DEVICE_MAGIC(8192 / 8, 0) },  
  23. 121   { "24c16", AT24_DEVICE_MAGIC(16384 / 8, 0) },  
  24. 122   { "24c32", AT24_DEVICE_MAGIC(32768 / 8, AT24_FLAG_ADDR16) },  
  25. 123   { "24c64", AT24_DEVICE_MAGIC(65536 / 8, AT24_FLAG_ADDR16) },  
  26. 124   { "24c128", AT24_DEVICE_MAGIC(131072 / 8, AT24_FLAG_ADDR16) },  
  27. 125   { "24c256", AT24_DEVICE_MAGIC(262144 / 8, AT24_FLAG_ADDR16) },  
  28. 126   { "24c512", AT24_DEVICE_MAGIC(524288 / 8, AT24_FLAG_ADDR16) },  
  29. 127   { "24c1024", AT24_DEVICE_MAGIC(1048576 / 8, AT24_FLAG_ADDR16)},  
  30. 128   { "at24", 0 },  
  31. 129   { /* END OF LIST */ }  
  32. 130};  


 

可以看到,at24_driver支持很多设备,其中包括Mini2440使用的24c08 EEPROM。

 

步骤2:调用i2c_add_driver注册i2c_driver结构体变量。

对于Mini2440的I2C设备at24c08 EEPROM,这一步工作是在drivers/misc/eeprom/at24.c文件中的at24_init函数中完成的:

 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. 687static int __init at24_init(void)  
  2. 688{  
  3. 689   if (!io_limit) {  
  4. 690       pr_err("at24: io_limit must not be 0!\n");  
  5. 691       return -EINVAL;  
  6. 692   }  
  7. 693  
  8. 694   io_limit = rounddown_pow_of_two(io_limit);  
  9. 695   return i2c_add_driver(&at24_driver);  
  10. 696}  


 

695行,调用i2c_add_driver注册上面定义的at24_driver。

i2c_add_driver是一个宏,定义在include/linux/i2c.h文件中:
  1. 497/* use a define to avoid include chaining to get THIS_MODULE */  
  2. 498#define i2c_add_driver(driver) \  
  3. 499    i2c_register_driver(THIS_MODULE, driver)  


i2c_register_driver函数定义在drivers/i2c/i2c-core.c文件中,其内容如下:
  1. 1307/* 
  2. 1308 * An i2c_driver is used with one or more i2c_client (device) nodes to access 
  3. 1309 * i2c slave chips, on a bus instance associated with some i2c_adapter. 
  4. 1310 */  
  5. 1311  
  6. 1312int i2c_register_driver(struct module *owner, struct i2c_driver *driver)  
  7. 1313{  
  8. 1314    int res;  
  9. 1315  
  10. 1316    /* Can't register until after driver model init */  
  11. 1317    if (unlikely(WARN_ON(!i2c_bus_type.p)))  
  12. 1318        return -EAGAIN;  
  13. 1319  
  14. 1320    /* add the driver to the list of i2c drivers in the driver core */  
  15. 1321    driver->driver.owner = owner;  
  16. 1322    driver->driver.bus = &i2c_bus_type;  
  17. 1323  
  18. 1324    /* When registration returns, the driver core 
  19. 1325     * will have called probe() for all matching-but-unbound devices. 
  20. 1326     */  
  21. 1327    res = driver_register(&driver->driver);  
  22. 1328    if (res)  
  23. 1329        return res;  
  24. 1330  
  25. 1331    /* Drivers should switch to dev_pm_ops instead. */  
  26. 1332    if (driver->suspend)  
  27. 1333        pr_warn("i2c-core: driver [%s] using legacy suspend method\n",  
  28. 1334            driver->driver.name);  
  29. 1335    if (driver->resume)  
  30. 1336        pr_warn("i2c-core: driver [%s] using legacy resume method\n",  
  31. 1337            driver->driver.name);  
  32. 1338  
  33. 1339    pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);  
  34. 1340  
  35. 1341    INIT_LIST_HEAD(&driver->clients);  
  36. 1342    /* Walk the adapters that are already present */  
  37. 1343    i2c_for_each_dev(driver, __process_new_driver);  
  38. 1344  
  39. 1345    return 0;  
  40. 1346}  


1327行,调用driver_register注册i2c_driver.driver。参考《 Linux设备模型分析之device_driver(基于3.10.1内核)》对Linux设备模型的分析,在driver_register执行过程中,如果I2C总线上找到了与该驱动匹配的I2C设备,则i2c_driver.probe函数会被调用执行。

1343行,调用i2c_for_each_dev遍历所有已存在的i2c_adapter。该函数定义在drivers/i2c/i2c-core.c文件中,其内容如下:
  1. 1288int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))  
  2. 1289{  
  3. 1290    int res;  
  4. 1291  
  5. 1292    mutex_lock(&core_lock);  
  6. 1293    res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);  
  7. 1294    mutex_unlock(&core_lock);  
  8. 1295  
  9. 1296    return res;  
  10. 1297}  


1293行,调用bus_for_each_dev,这个函数定义在drivers/base/bus.c文件中,其内容如下:

 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. 286intbus_for_each_dev(struct bus_type *bus, struct device *start,  
  2.  287            void *data, int (*fn)(struct device *, void *))  
  3.  288{  
  4.  289   struct klist_iter i;  
  5.  290   struct device *dev;  
  6.  291   int error = 0;  
  7.  292  
  8.  293   if (!bus || !bus->p)  
  9.  294       return -EINVAL;  
  10.  295  
  11.  296   klist_iter_init_node(&bus->p->klist_devices, &i,  
  12.  297                 (start ?&start->p->knode_bus : NULL));  
  13.  298   while ((dev = next_device(&i)) && !error)  
  14.  299       error = fn(dev, data);  
  15.  300   klist_iter_exit(&i);  
  16.  301   return error;  
  17.  302}  

 

我们在《 Linux设备模型分析之device_driver(基于3.10.1内核)》一文中已经分析过这个函数。这里,传递过来的data参数是要注册的i2c_driver,fn参数是__process_new_driver函数,所以我们来看__process_new_driver函数,该函数定义在drivers/i2c/i2c-core.c文件中,其内容如下:

 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. 1300static int __process_new_driver(structdevice *dev, void *data)  
  2. 1301{  
  3. 1302   if (dev->type != &i2c_adapter_type)  
  4. 1303       return 0;  
  5. 1304   return i2c_do_add_adapter(data, to_i2c_adapter(dev));  
  6. 1305}  


 

i2c_do_add_adapter函数定义在drivers/i2c/i2c-core.c文件中,其内容如下:

 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. 957static int i2c_do_add_adapter(structi2c_driver *driver,  
  2.  958                  struct i2c_adapter *adap)  
  3.  959{  
  4.  960   /* Detect supported devices on that bus, and instantiate them */  
  5.  961   i2c_detect(adap, driver);  
  6.  962  
  7.  963   /* Let legacy drivers scan this bus for matching devices */  
  8.  964   if (driver->attach_adapter) {  
  9.  965       dev_warn(&adap->dev, "%s: attach_adapter method isdeprecated\n",  
  10.  966            driver->driver.name);  
  11.  967       dev_warn(&adap->dev, "Please use another way to instantiate"  
  12.  968            "your i2c_client\n");  
  13.  969       /* We ignore the return code; if it fails, too bad */  
  14.  970       driver->attach_adapter(adap);  
  15.  971    }  
  16.  972   return 0;  
  17.  973}  


 

这个函数我们在分析i2c_adapter的注册过程时已经分析过了,它主要完成i2c_driver与i2c_adapter上的i2c设备的匹配工作,如果匹配成功,初始化并注册对应的i2c_client。

至此,i2c_driver的注册过程我们就清楚了。

 

步骤3:实现i2c_driver中要示实现的函数

对于Mini2440的I2C设备at24c08 EEPROM,at24_driver要求实现的是i2c_driver.probe和i2c_driver.remove函数。

转载于:https://www.cnblogs.com/haoxing990/p/4717995.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值