IIC系统分析
by:Cat_With_Apple/吃苹果的猫转载请主名
//只是调用顺序 不是调用关系
mini2440_init()
├──s3c_i2c0_set_platdata()//set bus_number
├──i2c_register_board_info()
├──platform_add_devices()//add i2c设备
s3c24xx_i2c_probe()
├──i2c_register_adapter()
├──2c_scan_static_board_info()
│ ├──2c_new_device()//add client根据bus_number匹配client和adapte
i2c_device_probe()
├──client->driver = driver;
├──driver->probe()
- 在i2c_init()函数中注册 i2c_bus_type
//i2c-core.c
static int __init i2c_init(void)
{
...
bus_register(&i2c_bus_type);
...
}
postcore_initcall(i2c_init); //这里表示代码会在内核初始化阶段完成
- 然后就要考虑 i2c_client i2c_driver adapter是如何注册到i2c_bus_type?
请自己先思考这个问题,然后试着自己追代码,这里这个教程只是提供追代码的思路。
// mach-mini2440.c这个函数在内核完成一定的初始化之后执行
mini2440_init(void)
{
...
s3c_i2c0_set_platdata(NULL); //主要完成adapter额外信息的配制--bus_num = 0;
i2c_register_board_info(0, mini2440_i2c_devs,
ARRAY_SIZE(mini2440_i2c_devs)); //
platform_add_devices(mini2440_devices, ARRAY_SIZE(mini2440_devices));
...
}
MACHINE_START(MINI2440, "MINI2440")
/* Maintainer: Michel Pollet */
.atag_offset = 0x100,
.map_io = mini2440_map_io,
.init_machine = mini2440_init,
.init_irq = s3c24xx_init_irq,
.timer = &s3c24xx_timer,
.restart = s3c244x_restart,
MACHINE_END
s3c_i2c0_set_platdata(NULL);
{
struct s3c2410_platform_i2c *npd;
if (!pd) {
pd = &default_i2c_data;
pd->bus_num = 0; //这里的number会在后面注册adapter时候使用到
}
npd = s3c_set_platdata(pd, sizeof(struct s3c2410_platform_i2c),
&s3c_device_i2c0); //将i2c控制器信息,即adapter,放在s3c_device_i2c0中
if (!npd->cfg_gpio)
npd->cfg_gpio = s3c_i2c0_cfg_gpio;
}
int __init
i2c_register_board_info(int busnum,
struct i2c_board_info const *info, unsigned len)
{
int status;
for (status = 0; len; len--, info++) { //循环info中的内容
struct i2c_devinfo *devinfo;
devinfo = kzalloc(sizeof(*devinfo), GFP_KERNEL);
...
devinfo->busnum = busnum;
devinfo->board_info = *info;
list_add_tail(&devinfo->list, &__i2c_board_list);//将设备信息注册到__i2c_board_list这个链表中
}
return status;
}
//linux-3.4/arch/arm/plat-samsung/devs.c
struct platform_device s3c_device_i2c0 = {
.name = "s3c2410-i2c",
#ifdef CONFIG_S3C_DEV_I2C1
.id = 0,
#else
.id = -1,
#endif
.num_resources = ARRAY_SIZE(s3c_i2c0_resource),
.resource = s3c_i2c0_resource,
};
//mach_mini2440.c
static struct platform_device *mini2440_devices[] __initdata = {
...
&s3c_device_i2c0
...
}
platform_add_devices(mini2440_devices, ARRAY_SIZE(mini2440_devices));
3.接下分析s3c_device_i2c0对应的driver
//linux-3.4/drivers/i2c/busses/i2c-s3c2410.c
struct s3c24xx_i2c {
...
struct i2c_adapter adap;
...
struct s3c2410_platform_i2c *pdata;
...
};
static int s3c24xx_i2c_probe(struct platform_device *pdev)
{
...//
struct s3c2410_platform_i2c *pdata = NULL;
pdata = pdev->dev.platform_data;
...
struct s3c24xx_i2c *i2c;
i2c = devm_kzalloc(&pdev->dev, sizeof(struct s3c24xx_i2c), GFP_KERNEL);
if (pdata)
memcpy(i2c->pdata, pdata, sizeof(*pdata));
...
strlcpy(i2c->adap.name, "s3c2410-i2c", sizeof(i2c->adap.name));
i2c->adap.owner = THIS_MODULE;
i2c->adap.algo = &s3c24xx_i2c_algorithm;
i2c->adap.retries = 2;
i2c->adap.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
...
i2c->adap.nr = i2c->pdata->bus_num; //这里使用前面s3c_i2c0_set_platdata(NULL)提供的bus_num
i2c->adap.dev.of_node = pdev->dev.of_node;
ret = i2c_add_numbered_adapter(&i2c->adap);//注册adapter
}
////linux-3.4/drivers/i2c/i2c-core.c
int i2c_add_numbered_adapter(struct i2c_adapter *adap)
{
...
i2c_register_adapter(adap);
...
}
static int i2c_register_adapter(struct i2c_adapter *adap)
{
...
dev_set_name(&adap->dev, "i2c-%d", adap->nr);
//同i2c_client 和i2c_driver一样是i2c_bus_type
//这样会导致注册这个三个结构体会使用相同的mach函数,在后面会分析内核是如何解决的
adap->dev.bus = &i2c_bus_type;
adap->dev.type = &i2c_adapter_type;
res = device_register(&adap->dev);//注册device到i2c_bus中
...
/*
*这里会使用之前i2c_register_board_info()提供的client信息
*同时注册client到i2c_bus上面
*/
if (adap->nr < __i2c_first_dynamic_bus_num)
i2c_scan_static_board_info(adap);
...
}
static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
{
/*
*遍历__i2c_board_list注册新的client到总线上
*/
list_for_each_entry(devinfo, &__i2c_board_list, list) {
if (devinfo->busnum == adapter->nr
&& !i2c_new_device(adapter,//关键函数
&devinfo->board_info))
}
}
struct i2c_client *
i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
{
struct i2c_client *client;
...
client = kzalloc(sizeof *client, GFP_KERNEL);
client->adapter = adap;//将client和adapter联系到一起
client->addr = info->addr;
client->irq = info->irq;
client->dev.bus = &i2c_bus_type;
client->dev.type = &i2c_client_type;//mach函数根据这个参数判读这个设备是client还是adapter
status = device_register(&client->dev);//注册client
...
return client;
}
4.到这里client和adapter都已经注册到了i2c_bus上了,还差i2c_driver
i2c_driver是在模块的module_init中注册的,不需要分析
5.最后就要看i2c_driver和i2c_client的匹配的
static int i2c_device_match(struct device *dev, struct device_driver *drv)
{
struct i2c_client *client = i2c_verify_client(dev);//这里验证设备是否是i2c_client
struct i2c_driver *driver;
if (!client)//如果不是client,即是adapter,这里直接返回
return 0;
/* Attempt an OF style match */
if (of_driver_match_device(dev, drv))
return 1;
driver = to_i2c_driver(drv);
/* match on an id table if there is one */
if (driver->id_table)
return i2c_match_id(driver->id_table, client) != NULL;
return 0;
}
struct i2c_client *i2c_verify_client(struct device *dev)
{
return (dev->type == &i2c_client_type) //根据dev->type来判断
? to_i2c_client(dev)
: NULL;
}