从零开始之驱动发开、linux驱动(五十八、linux4.19的IIC驱动的编写和使用2)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_16777851/article/details/89102912

下面说的这种方式其实和前面的很相似

1.在设备树文件中,设备所在的i2c总线下面定义好设备


&i2c0 {
    status = "okay";

    eeprom@50 {
        compatible = "atmel,24c02";
        reg = <0x50>;
    };  
};

驱动只需要增加一个of_match_table就可以了

/*
 * i2c驱动是根据下面这个i2c_device_id表中的名字来匹配,client的名字的
 */
static const struct i2c_device_id at24cxx_id_table[] = {
    { "at24cxx", 0 },
    {}
};


/*
 * 有限使用设备树中的compatible文件来匹配,匹配不上才使用上面的
 */
static const struct of_device_id at24_of_match[] = { 
    { .compatible = "atmel,24c02",      .data = NULL },
    {}, 
};

static struct i2c_driver at24cxx_driver = { 
    .driver = { 
        .name   = "at24cxx",    /* 这个随便起名字 */
        .of_match_table = at24_of_match,
    },  
    .probe      = at24cxx_probe,
    .remove     = at24cxx_remove,
    .id_table   = at24cxx_id_table,
    .address_list = normal_i2c,
};

简单测试,可以看到系统启动后就已经创建了设备。

装载驱动后,也是可以匹配的。

测试,写单个字符也是ok的

可以看到,写的话,如果地址大于本页,就会出现页翻转。当然写操作是没问题的。

编译,测试通过的源码已经上传到gitbub

https://github.com/To-run-away/linux-i2c-driver/tree/master/i2c_device_tree_create_client

猜你喜欢

转载自blog.csdn.net/qq_16777851/article/details/89102912