Linux_IIC_Driver系统分析

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()

  1. 在i2c_init()函数中注册 i2c_bus_type
//i2c-core.c
static int __init i2c_init(void)
{
    ...
    bus_register(&i2c_bus_type);
    ...
}
postcore_initcall(i2c_init); //这里表示代码会在内核初始化阶段完成
  1. 然后就要考虑 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 <buserror@gmail.com> */
    .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;
}


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,319评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,801评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,567评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,156评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,019评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,090评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,500评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,192评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,474评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,566评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,338评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,212评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,572评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,890评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,169评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,478评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,661评论 2 335

推荐阅读更多精彩内容

  • linux设备模型bus,device,driver作者 codercjg 在 10 十一月 2015, 2:43...
    codercjg阅读 412评论 0 1
  • 简介 I2C驱动由I2C核心,I2C总线驱动和I2C设备驱动组成.I2C核心是I2C总线驱动和I2C设备驱动的中间...
    傀儡世界阅读 1,078评论 0 1
  • Linux i2c system I2C总线是由PHILIPS公司开发的两线式串行总线,每个连接到总线的器件都可以...
    Creator_Ly阅读 1,800评论 0 8
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,494评论 18 139
  • 参考链接:http://blog.csdn.net/sweetsnow24/article/details/863...
    Tony1213阅读 4,360评论 0 0