68 static int i2c_device_match(struct device *dev, struct device_driver *drv) 69 { 70 struct i2c_client *client = i2c_verify_client(dev); 71 struct i2c_driver *driver; 72 73 if (!client) 74 return 0; 75 76 /* Attempt an OF style match */ 77 if (of_driver_match_device(dev, drv)) 78 return 1; 79 80 driver = to_i2c_driver(drv); 81 /* match on an id table if there is one */ 82 if (driver->id_table) 83 return i2c_match_id(driver->id_table, client) != NULL; 84 85 return 0; 86 }
106 static int i2c_device_probe(struct device *dev) 107 { 108 struct i2c_client *client = i2c_verify_client(dev); 109 struct i2c_driver *driver; 110 int status; 111 112 if (!client) 113 return 0; 114 115 driver = to_i2c_driver(dev->driver); 116 if (!driver->probe || !driver->id_table) 117 return -ENODEV; 118 client->driver = driver; 119 if (!device_can_wakeup(&client->dev)) 120 device_init_wakeup(&client->dev, 121 client->flags & I2C_CLIENT_WAKE); 122 dev_dbg(dev, "probe\n"); 123 124 status = driver->probe(client, i2c_match_id(driver->id_table, client));// 调用底层驱动的i2c_driver结构 //实现的probe函数,i2c_match_id()函数比较的是i2c_driver结构中id_table成员的name是否与client结 //构的name一致 125 if (status) { 126 client->driver = NULL; 127 i2c_set_clientdata(client, NULL); 128 } 129 return status; 130 }
315 struct bus_type i2c_bus_type = { 316 .name = "i2c", 317 .match = i2c_device_match, 318 .probe = i2c_device_probe, 319 .remove = i2c_device_remove, 320 .shutdown = i2c_device_shutdown, 321 .pm = &i2c_device_pm_ops, 322 }; 323 EXPORT_SYMBOL_GPL(i2c_bus_type); 324 325 static struct device_type i2c_client_type = { 326 .groups = i2c_dev_attr_groups, 327 .uevent = i2c_device_uevent, 328 .release = i2c_client_dev_release, 329 };
函数原型分析:
1、i2c_verify_client(struct device *dev)
332 /** 333 * i2c_verify_client - return parameter as i2c_client, or NULL 334 * @dev: device, probably from some driver model iterator 335 * 336 * When traversing the driver model tree, perhaps using driver model 337 * iterators like @device_for_each_child(), you can't assume very much 338 * about the nodes you find. Use this function to avoid oopses caused 339 * by wrongly treating some non-I2C device as an i2c_client. 340 */ 341 struct i2c_client *i2c_verify_client(struct device *dev) 342 { 343 return (dev->type == &i2c_client_type) 344 ? to_i2c_client(dev) 345 : NULL; 346 } 347 EXPORT_SYMBOL(i2c_verify_client);
2、i2c_chech_client_addr_validity(const struct i2c_client *client)
i2c地址有效性检查,分10位地址、7位地址两种情况,7位地址范围为:0x00< i2c_addr <=0x7f
350 /* This is a permissive address validity check, I2C address map constraints 351 * are purposely not enforced, except for the general call address. */ 352 static int i2c_check_client_addr_validity(const struct i2c_client *client) 353 { 354 if (client->flags & I2C_CLIENT_TEN) { 355 /* 10-bit address, all values are valid */ 356 if (client->addr > 0x3ff) 357 return -EINVAL; 358 } else { 359 /* 7-bit address, reject the general call address */ 360 if (client->addr == 0x00 || client->addr > 0x7f) 361 return -EINVAL; 362 } 363 return 0; 364 }