501 struct i2c_client * 502 i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
i2c_new_device(...)依据注册的struct i2c_adapter 和 struct i2c_board_info 来初始化 struct i2c_client 并返回 struct i2c_client ,并且 struct i2c_client 的 client->dev.bus 被初始化为 &i2c_bus_type 。struct i2c_bus_type 成员 i2c_device_probe 用来匹配i2c_driver 和 i2c_client 中的 name 域。
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);
相关函数实现源码如下:
485 /** 486 * i2c_new_device - instantiate an i2c device 487 * @adap: the adapter managing the device 488 * @info: describes one I2C device; bus_num is ignored 489 * Context: can sleep 490 * 491 * Create an i2c device. Binding is handled through driver model 492 * probe()/remove() methods. A driver may be bound to this device when we 493 * return from this function, or any later moment (e.g. maybe hotplugging will 494 * load the driver module). This call is not appropriate for use by mainboard 495 * initialization logic, which usually runs during an arch_initcall() long 496 * before any i2c_adapter could exist. 497 * 498 * This returns the new i2c client, which may be saved for later use with 499 * i2c_unregister_device(); or NULL to indicate an error. 500 */ 501 struct i2c_client * 502 i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info) 503 { 504 struct i2c_client *client; 505 int status; 506 507 client = kzalloc(sizeof *client, GFP_KERNEL); 508 if (!client) 509 return NULL; 510 511 client->adapter = adap; 512 513 client->dev.platform_data = info->platform_data; 514 515 if (info->archdata) 516 client->dev.archdata = *info->archdata; 517 518 client->flags = info->flags; 519 client->addr = info->addr; 520 client->irq = info->irq; 521 522 strlcpy(client->name, info->type, sizeof(client->name)); 523 524 /* Check for address validity */ 525 status = i2c_check_client_addr_validity(client); 526 if (status) { 527 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n", 528 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr); 529 goto out_err_silent; 530 } 531 532 /* Check for address business */ 533 status = i2c_check_addr_busy(adap, client->addr); 534 if (status) 535 goto out_err; 536 537 client->dev.parent = &client->adapter->dev; 538 client->dev.bus = &i2c_bus_type; 539 client->dev.type = &i2c_client_type; 540 client->dev.of_node = info->of_node; 541 542 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap), 543 client->addr); 544 status = device_register(&client->dev); 545 if (status) 546 goto out_err; 547 548 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n", 549 client->name, dev_name(&client->dev)); 550 551 return client; 552 553 out_err: 554 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x " 555 "(%d)\n", client->name, client->addr, status); 556 out_err_silent: 557 kfree(client); 558 return NULL; 559 } 560 EXPORT_SYMBOL_GPL(i2c_new_device);