4 OMAP3630 I2C device驱动在Linux内核中,I2C device驱动位于drivers/i2c/chips目录下,可以看到该目录下有很多相关的device驱动,这里以xxxx项目的mma7455为例介绍device驱动的注册过程,对应的device驱动程序为mma7455.c。
既然有device驱动,那么必定有相应的device,I2C的device是什么呢?其实就是我们在1.3节中提到的i2c_client,所以在device驱动注册之前先来了解下i2c_client的注册过程。4.1 Mma7455 device注册 Mma7455 device即i2c_client的创建以及注册分为两步。4.1.1 将mma7455设备信息加入到设备链表 在板级初始化时将I2C device的名称,地址和相关的信息加入到链表__i2c_board_list中,该链表记录了具体开发板上的I2C设备信息。
在board-xxxx.c中,定义了mma7455的设备信息定义如下:
staticstructi2c_board_info __initdata xxxx_i2c_bus3_info[] = {
……
#ifdef CONFIG_SENSORS_MMA7455
{
I2C_BOARD_INFO("mma7455", 0x1D),
.platform_data = &xxxx_mma7455_platform_data,
},
#endif
};
Mma7455加入到设备链表__i2c_board_list的流程图如下图:
图4.1 mma7455加入到I2C设备链表的过程
i2c_register_board_info()函数的定义如下:
int__init i2c_register_board_info(intbusnum,
structi2c_board_infoconst*info, unsigned len)
{
……
for(status = 0; len; len--, info++) {
structi2c_devinfo *devinfo;
devinfo = kzalloc(sizeof(*devinfo), GFP_KERNEL);
……
devinfo->busnum = busnum;
devinfo->board_info = *info;
list_add_tail(&devinfo->list, &__i2c_board_list);
}
……
}
4.1.2 创建并注册i2c_client i2c_client的创建和注册在I2C adapter驱动注册过程中完成,I2C adapter驱动的注册可以参考3.2.2节,i2c_add_numbered_adapter()函数在注册I2C adapter驱动的同时会扫描4.1.1中提到的I2C设备链表__i2c_board_list,如果该总线上有对应的I2C设备,则创建相应的i2c_client,并将其注册到I2C core中。流程图如下所示:
图4.2创建并注册i2c_client
相应的代码位于i2c-core.c如下:
staticvoidi2c_scan_static_board_info(structi2c_adapter *adapter)
{
……
list_for_each_entry(devinfo, &__i2c_board_list, list) {
if(devinfo->busnum == adapter->nr
&& !i2c_new_device(adapter,
&devinfo->board_info))
……
}
……
}
在i2c_scan_static_board_info()函数中遍历I2C设备链表__i2c_board_list,设备的总线号和adapter的总线号相等,则使用函数i2c_new_device()创建该设备。
structi2c_client *
i2c_new_device(structi2c_adapter *adap,structi2c_board_infoconst*info)
{
……
client = kzalloc(sizeof*client, GFP_KERNEL);
if(!client)
returnNULL;
client->adapter = adap;
client->dev.platform_data = info->platform_data;
if(info->archdata)
client->dev.archdata = *info->archdata;
client->flags = info->flags;
client->addr = info->addr;
client->irq = info->irq;
strlcpy(client->name, info->type,sizeof(client->name));
……
status = i2c_attach_client(client);
……
}
在函数i2c_new_device()中创建一个i2c_client,初始化该结构体的adapter,addr,name等变量,这里的client->name被初始化为info->type,在4.1.1中,info->type初始化为“mma7455”, client->name后面会用于I2C device和I2C driver匹配时使用,最后调用i2c_attach_client()将该client注册到I2C core。
inti2c_attach_client(structi2c_client *client)
{
structi2c_adapter *adapter = client->adapter;
……
client->dev.parent = &client->adapter->dev;
client->dev.bus = &i2c_bus_type;
……
res = device_register(&client->dev);
……
}
函数i2c_attach_client()进一步初始化i2c_client结构体,将该设备的总线初始化为i2c_bus_type,说明该设备被放在I2C总线上,用于后面跟I2C driver匹配时使用,最后使用device_register(&client->dev)注册该i2c_client设备。4.2 Mma7455 device驱动注册 在mma7455.c中,定义了mma7455的device驱动,代码如下:
staticstructi2c_driver mma7455_driver = {
.driver = {
.name ="mma7455",
},
.class= I2C_CLASS_HWMON,
.probe = mma7455_probe,
.remove = mma7455_remove,
.id_table = mma7455_id,
……
};
注册的简要示意图如下:
图4.3 device驱动的注册
相应的代码位于mma7455.c和i2c-core.c。
staticint__init init_mma7455(void)
{
……
res = i2c_add_driver(&mma7455_driver);
……
return(res);
}
在模块加载的时候首先调用init_mma7455(),然后init_mma7455()调用函数i2c_add_driver()注册mma7455_driver结构体。
inti2c_register_driver(structmodule *owner,structi2c_driver *driver)
{
…….
/* add the driver to the list of i2c drivers in the driver core */
driver->driver.owner = owner;
driver->driver.bus = &i2c_bus_type;
……
res = driver_register(&driver->driver);
if(res)
returnres;
……
}
函数i2c_register_driver()初始化该驱动的总线为i2c_bus_type,然后使用函数driver_register(&driver->driver)注册该驱动,因此内核会在I2C总线上遍历所有I2C设备,由于该mma7455 device驱动的匹配因子name变量为“mma7455”,因此正好和在4.1.2里创建的name也为“mma7455”的i2c client匹配。因此总线的probe函数将会被调用,I2C总线的probe函数为i2c_device_probe(),具体代码如下:
staticinti2c_device_probe(structdevice *dev)
{
structi2c_client *client = to_i2c_client(dev);
structi2c_driver *driver = to_i2c_driver(dev->driver);
intstatus;
if(!driver->probe || !driver->id_table)
return-ENODEV;
client->driver = driver;
…….
status = driver->probe(client, i2c_match_id(driver->id_table, client));
……
returnstatus;
}
在i2c_device_probe()函数中,语句client->driver = driver将I2C device和I2C driver绑定,然后直接调用具体设备的probe函数,这里即mma7455的probe函数mma7455_probe()。
在mma7455_probe()函数会完成一些具体I2C设备相关的初始化等操作,这边就不再详述。