• i2c驱动扫描注册


    https://blog.csdn.net/uunubt/article/details/127959575
    i2c驱动扫描注册
    在《i2c设备与驱动匹配过程》中说到,i2c 驱动注册时会使用两种匹配方法去寻找i2c设备,代码如下:

    struct bus_type i2c_bus_type = {
    .name = “i2c”,
    .match = i2c_device_match,
    .probe = i2c_device_probe,
    .remove = i2c_device_remove,
    .shutdown = i2c_device_shutdown,
    .pm = &i2c_device_pm_ops,
    };
    EXPORT_SYMBOL_GPL(i2c_bus_type);

    int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
    {
    driver->driver.bus = &i2c_bus_type;//添加总线

    res = driver_register(&driver->driver);//驱动注册核心函数,注意只传入了driver成员
    
    /* 遍历所有挂在总线上的iic适配器,用它们去探测driver中指定的iic设备地址列表 */
    i2c_for_each_dev(driver, __process_new_driver);
    
    • 1
    • 2
    • 3
    • 4

    }

    driver_register 函数已将讲解过,现在来分析 i2c_for_each_dev 函数,

    int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
    {
    int res;
    mutex_lock(&core_lock);
    res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
    mutex_unlock(&core_lock);
    return res;
    }

    int bus_for_each_dev(struct bus_type *bus, struct device *start,
    void *data, int (*fn)(struct device *, void *))
    {
    struct klist_iter i;
    struct device *dev;
    int error = 0;
    if (!bus || !bus->p)
    return -EINVAL;

    klist_iter_init_node(&bus->p->klist_devices, &i, (start ? &start->p->knode_bus : NULL));
    
    while (!error && (dev = next_device(&i)))
        error = fn(dev, data);
    klist_iter_exit(&i);
    return error;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    }

    最终调用 __process_new_driver 函数,使用 i2c 总线上所有 i2c 适配器去探测i2c驱动中的设备地址数组!

    static struct device_type i2c_client_type = {
    .groups = i2c_dev_attr_groups,
    .uevent = i2c_device_uevent,
    .release = i2c_client_dev_release,
    };
    struct device_type i2c_adapter_type = {
    .groups = i2c_adapter_attr_groups,
    .release = i2c_adapter_dev_release,
    };
    static int __process_new_driver(struct device *dev, void *data)
    {
    if (dev->type != &i2c_adapter_type)
    return 0;
    return i2c_do_add_adapter(data, to_i2c_adapter(dev));
    }
    入口先判断传入的设备是不是i2c适配器(i2c控制器),因为在《i2c设备与驱动匹配过程》中说到,i2c 适配器和 i2c 设备一样,都会挂在 i2c 总线上,它们是通过 dev->type 项区分的。

    static int i2c_do_add_adapter(struct i2c_driver *driver, struct i2c_adapter adap)
    {
    /
    Detect supported devices on that bus, and instantiate them */
    i2c_detect(adap, driver);

    }
    最终调用i2c_detect函数,函数简化后如下:

    static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
    {
    int adap_id = i2c_adapter_id(adapter);

    address_list = driver->address_list;
    if (!driver->detect || !address_list)
        return 0;
    
    temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
    itemp_client->adapter = adapter;
    
    for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1)
    {
        temp_client->addr = address_list[i];
        err = i2c_detect_address(temp_client, driver);
        if (unlikely(err))
            break;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    }

    如果 i2c 驱动的设备地址数组为空或 detect 函数不存在,则结束返回,否则临时实例化一个 temp_client 设备,赋值 adapter 为当前 i2c 控制器,然后在使用该 i2c 控制器去探测 i2c 驱动设备地址数组中的所有地址,关键函数是 i2c_detect_address 如下(简化后):

    static int i2c_detect_address(struct i2c_client *temp_client, struct i2c_driver *driver)
    {
    struct i2c_board_info info;
    struct i2c_adapter *adapter = temp_client->adapter;
    int addr = temp_client->addr;
    int err;

    err = i2c_check_7bit_addr_validity_strict(addr);//检查地址是否有效,即7位有效地址
    if (err) {
        return err;
    }
    
    if (i2c_check_addr_busy(adapter, addr))//跳过已经使用的i2c设备
        return 0;
    
    if (!i2c_default_probe(adapter, addr))//检查这个地址是否有回应
        return 0;
    
    memset(&info, 0, sizeof(struct i2c_board_info));
    info.addr = addr;
    err = driver->detect(temp_client, &info);
    if (err) {
        return err == -ENODEV ? 0 : err;
    }
    
    if (info.type[0] == '\0')
    {
    }
    else
    {
        struct i2c_client *client;
        client = i2c_new_device(adapter, &info);
        if (client)
            list_add_tail(&client->detected, &driver->clients);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    }

    首先检查有效性、是否有设备回应、是否被使用,之后初始化了i2c_board_info 结构,注意只初始化了地址(实例化设备必须还要名字),然后调用了 i2c 驱动中的 detect 函数,如果成功则调用 i2c_new_device函数真正实例化i2c设备,并且将i2c设备挂在i2c驱动的链表上!注意:只有这种方式添加的i2c设备才会挂在驱动的链表上!

    仔细思考上面就能发现,i2c驱动中的detect函数必须要填写i2c_board_info结构体中name,i2c_new_device才能实例化i2c设备。

    所以,使用i2c驱动扫描注册设备时,需要按如下格式编写驱动!

    #include
    #include
    #include
    #include

    static int __devinit my_i2c_drv_probe(struct i2c_client *client, const struct i2c_device_id *id)
    {
    return 0;
    }

    static int __devexit my_i2c_drv_remove(struct i2c_client *client)
    {
    return 0;
    }

    static const struct i2c_device_id my_dev_id_table[] = {
    { “my_i2c_dev”, 0 },
    {}
    };//这里的名字很重要,驱动第一种匹配设备的方式要用到

    static int my_i2c_drv_detect(struct i2c_client *client, struct i2c_board_info info)
    {
    /
    能运行到这里, 表示该addr的设备是存在的
    * 但是有些设备单凭地址无法分辨(A芯片的地址是0x50, B芯片的地址也是0x50)
    * 还需要进一步读写I2C设备来分辨是哪款芯片,自己写方法
    * detect就是用来进一步分辨这个芯片是哪一款,并且设置info->type,也就是设备名字
    */
    printk(“my_i2c_drv_detect: addr = 0x%x\n”, client->addr);

    /* 进一步判断是哪一款 */
    strlcpy(info->type, "my_i2c_dev", I2C_NAME_SIZE);
    return 0;
    
    • 1
    • 2
    • 3

    }

    static const unsigned short addr_list[] = { 0x46, 0x48, I2C_CLIENT_END };//必须使用I2C_CLIENT_END宏结尾

    /* 1. 分配/设置i2c_driver /
    static struct i2c_driver my_i2c_driver = {
    .class = I2C_CLASS_HWMON, /
    表示去哪些适配器上找设备,不是对应类将不会调用匹配 /
    .driver = {
    .name = “my_i2c_dev”,
    .owner = THIS_MODULE,
    },
    .probe = my_i2c_drv_probe,
    .remove = __devexit_p(my_i2c_drv_remove),
    .id_table = my_dev_id_table,
    .detect = my_i2c_drv_detect, /
    用这个函数来检测设备确实存在 ,并填充设备名字*/
    .address_list = addr_list, /* 这些设备的地址 */
    };

    static int my_i2c_drv_init(void)
    {
    /* 2. 注册i2c_driver */
    i2c_add_driver(&my_i2c_driver);

    return 0;
    
    • 1

    }

    static void my_i2c_drv_exit(void)
    {
    i2c_del_driver(&my_i2cc_driver);
    }

    module_init(my_i2c_drv_init);
    module_exit(my_i2c_drv_exit);
    MODULE_LICENSE(“GPL”);

    /*

    • Driver for the ADT7411 (I2C/SPI 8 channel 10 bit ADC & temperature-sensor)
    • Copyright © 2008, 2010 Pengutronix
    • This program is free software; you can redistribute it and/or modify
    • it under the terms of the GNU General Public License version 2 as
    • published by the Free Software Foundation.
    • TODO: SPI, support for external temperature sensor
    • use power-down mode for suspend?, interrupt handling?
      */

    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include

    #define ADT7411_REG_INT_TEMP_VDD_LSB 0x03
    #define ADT7411_REG_EXT_TEMP_AIN14_LSB 0x04
    #define ADT7411_REG_VDD_MSB 0x06
    #define ADT7411_REG_INT_TEMP_MSB 0x07
    #define ADT7411_REG_EXT_TEMP_AIN1_MSB 0x08

    #define ADT7411_REG_CFG1 0x18
    #define ADT7411_CFG1_START_MONITOR (1 << 0)

    #define ADT7411_REG_CFG2 0x19
    #define ADT7411_CFG2_DISABLE_AVG (1 << 5)

    #define ADT7411_REG_CFG3 0x1a
    #define ADT7411_CFG3_ADC_CLK_225 (1 << 0)
    #define ADT7411_CFG3_REF_VDD (1 << 4)

    #define ADT7411_REG_DEVICE_ID 0x4d
    #define ADT7411_REG_MANUFACTURER_ID 0x4e

    #define ADT7411_DEVICE_ID 0x2
    #define ADT7411_MANUFACTURER_ID 0x41

    static const unsigned short normal_i2c[] = { 0x48, 0x4a, 0x4b, I2C_CLIENT_END };

    struct adt7411_data {
    struct mutex device_lock; /* for “atomic” device accesses */
    struct mutex update_lock;
    unsigned long next_update;
    int vref_cached;
    struct device *hwmon_dev;
    };

    /*

    • When reading a register containing (up to 4) lsb, all associated

    • msb-registers get locked by the hardware. After one of those msb is read,

    • all are unlocked. In order to use this locking correctly, reading lsb/msb

    • is protected here with a mutex, too.
      */
      static int adt7411_read_10_bit(struct i2c_client *client, u8 lsb_reg,
      u8 msb_reg, u8 lsb_shift)
      {
      struct adt7411_data *data = i2c_get_clientdata(client);
      int val, tmp;

      mutex_lock(&data->device_lock);

      val = i2c_smbus_read_byte_data(client, lsb_reg);
      if (val < 0)
      goto exit_unlock;

      tmp = (val >> lsb_shift) & 3;
      val = i2c_smbus_read_byte_data(client, msb_reg);

      if (val >= 0)
      val = (val << 2) | tmp;

    exit_unlock:
    mutex_unlock(&data->device_lock);

    return val;
    
    • 1

    }

    static int adt7411_modify_bit(struct i2c_client *client, u8 reg, u8 bit,
    bool flag)
    {
    struct adt7411_data *data = i2c_get_clientdata(client);
    int ret, val;

    mutex_lock(&data->device_lock);
    
    ret = i2c_smbus_read_byte_data(client, reg);
    if (ret < 0)
    	goto exit_unlock;
    
    if (flag)
    	val = ret | bit;
    else
    	val = ret & ~bit;
    
    ret = i2c_smbus_write_byte_data(client, reg, val);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    exit_unlock:
    mutex_unlock(&data->device_lock);
    return ret;
    }

    static ssize_t adt7411_show_vdd(struct device *dev,
    struct device_attribute *attr, char *buf)
    {
    struct i2c_client *client = to_i2c_client(dev);
    int ret = adt7411_read_10_bit(client, ADT7411_REG_INT_TEMP_VDD_LSB,
    ADT7411_REG_VDD_MSB, 2);

    return ret < 0 ? ret : sprintf(buf, "%u\n", ret * 7000 / 1024);
    
    • 1

    }

    static ssize_t adt7411_show_temp(struct device *dev,
    struct device_attribute *attr, char *buf)
    {
    struct i2c_client *client = to_i2c_client(dev);
    int val = adt7411_read_10_bit(client, ADT7411_REG_INT_TEMP_VDD_LSB,
    ADT7411_REG_INT_TEMP_MSB, 0);

    if (val < 0)
    	return val;
    
    val = val & 0x200 ? val - 0x400 : val; /* 10 bit signed */
    
    return sprintf(buf, "%d\n", val * 250);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    }

    static ssize_t adt7411_show_input(struct device *dev,
    struct device_attribute *attr, char *buf)
    {
    int nr = to_sensor_dev_attr(attr)->index;
    struct i2c_client *client = to_i2c_client(dev);
    struct adt7411_data *data = i2c_get_clientdata(client);
    int val;
    u8 lsb_reg, lsb_shift;

    mutex_lock(&data->update_lock);
    if (time_after_eq(jiffies, data->next_update)) {
    	val = i2c_smbus_read_byte_data(client, ADT7411_REG_CFG3);
    	if (val < 0)
    		goto exit_unlock;
    
    	if (val & ADT7411_CFG3_REF_VDD) {
    		val = adt7411_read_10_bit(client,
    				ADT7411_REG_INT_TEMP_VDD_LSB,
    				ADT7411_REG_VDD_MSB, 2);
    		if (val < 0)
    			goto exit_unlock;
    
    		data->vref_cached = val * 7000 / 1024;
    	} else {
    		data->vref_cached = 2250;
    	}
    
    	data->next_update = jiffies + HZ;
    }
    
    lsb_reg = ADT7411_REG_EXT_TEMP_AIN14_LSB + (nr >> 2);
    lsb_shift = 2 * (nr & 0x03);
    val = adt7411_read_10_bit(client, lsb_reg,
    		ADT7411_REG_EXT_TEMP_AIN1_MSB + nr, lsb_shift);
    if (val < 0)
    	goto exit_unlock;
    
    val = sprintf(buf, "%u\n", val * data->vref_cached / 1024);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    exit_unlock:
    mutex_unlock(&data->update_lock);
    return val;
    }

    static ssize_t adt7411_show_bit(struct device *dev,
    struct device_attribute *attr, char *buf)
    {
    struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(attr);
    struct i2c_client *client = to_i2c_client(dev);
    int ret = i2c_smbus_read_byte_data(client, attr2->index);

    return ret < 0 ? ret : sprintf(buf, "%u\n", !!(ret & attr2->nr));
    
    • 1

    }

    static ssize_t adt7411_set_bit(struct device *dev,
    struct device_attribute *attr, const char *buf,
    size_t count)
    {
    struct sensor_device_attribute_2 *s_attr2 = to_sensor_dev_attr_2(attr);
    struct i2c_client *client = to_i2c_client(dev);
    struct adt7411_data *data = i2c_get_clientdata(client);
    int ret;
    unsigned long flag;

    ret = strict_strtoul(buf, 0, &flag);
    if (ret || flag > 1)
    	return -EINVAL;
    
    ret = adt7411_modify_bit(client, s_attr2->index, s_attr2->nr, flag);
    
    /* force update */
    mutex_lock(&data->update_lock);
    data->next_update = jiffies;
    mutex_unlock(&data->update_lock);
    
    return ret < 0 ? ret : count;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    }

    #define ADT7411_BIT_ATTR(__name, __reg, __bit)
    SENSOR_DEVICE_ATTR_2(__name, S_IRUGO | S_IWUSR, adt7411_show_bit,
    adt7411_set_bit, __bit, __reg)

    static DEVICE_ATTR(temp1_input, S_IRUGO, adt7411_show_temp, NULL);
    static DEVICE_ATTR(in0_input, S_IRUGO, adt7411_show_vdd, NULL);
    static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, adt7411_show_input, NULL, 0);
    static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, adt7411_show_input, NULL, 1);
    static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, adt7411_show_input, NULL, 2);
    static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, adt7411_show_input, NULL, 3);
    static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, adt7411_show_input, NULL, 4);
    static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, adt7411_show_input, NULL, 5);
    static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, adt7411_show_input, NULL, 6);
    static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, adt7411_show_input, NULL, 7);
    static ADT7411_BIT_ATTR(no_average, ADT7411_REG_CFG2, ADT7411_CFG2_DISABLE_AVG);
    static ADT7411_BIT_ATTR(fast_sampling, ADT7411_REG_CFG3, ADT7411_CFG3_ADC_CLK_225);
    static ADT7411_BIT_ATTR(adc_ref_vdd, ADT7411_REG_CFG3, ADT7411_CFG3_REF_VDD);

    static struct attribute *adt7411_attrs[] = {
    &dev_attr_temp1_input.attr,
    &dev_attr_in0_input.attr,
    &sensor_dev_attr_in1_input.dev_attr.attr,
    &sensor_dev_attr_in2_input.dev_attr.attr,
    &sensor_dev_attr_in3_input.dev_attr.attr,
    &sensor_dev_attr_in4_input.dev_attr.attr,
    &sensor_dev_attr_in5_input.dev_attr.attr,
    &sensor_dev_attr_in6_input.dev_attr.attr,
    &sensor_dev_attr_in7_input.dev_attr.attr,
    &sensor_dev_attr_in8_input.dev_attr.attr,
    &sensor_dev_attr_no_average.dev_attr.attr,
    &sensor_dev_attr_fast_sampling.dev_attr.attr,
    &sensor_dev_attr_adc_ref_vdd.dev_attr.attr,
    NULL
    };

    static const struct attribute_group adt7411_attr_grp = {
    .attrs = adt7411_attrs,
    };

    static int adt7411_detect(struct i2c_client *client,
    struct i2c_board_info *info)
    {
    int val;

    if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
    	return -ENODEV;
    
    val = i2c_smbus_read_byte_data(client, ADT7411_REG_MANUFACTURER_ID);
    if (val < 0 || val != ADT7411_MANUFACTURER_ID) {
    	dev_dbg(&client->dev, "Wrong manufacturer ID. Got %d, "
    		"expected %d\n", val, ADT7411_MANUFACTURER_ID);
    	return -ENODEV;
    }
    
    val = i2c_smbus_read_byte_data(client, ADT7411_REG_DEVICE_ID);
    if (val < 0 || val != ADT7411_DEVICE_ID) {
    	dev_dbg(&client->dev, "Wrong device ID. Got %d, "
    		"expected %d\n", val, ADT7411_DEVICE_ID);
    	return -ENODEV;
    }
    
    strlcpy(info->type, "adt7411", I2C_NAME_SIZE);
    
    return 0;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    }

    static int __devinit adt7411_probe(struct i2c_client *client,
    const struct i2c_device_id *id)
    {
    struct adt7411_data *data;
    int ret;

    data = kzalloc(sizeof(*data), GFP_KERNEL);
    if (!data)
    	return -ENOMEM;
    
    i2c_set_clientdata(client, data);
    mutex_init(&data->device_lock);
    mutex_init(&data->update_lock);
    
    ret = adt7411_modify_bit(client, ADT7411_REG_CFG1,
    			 ADT7411_CFG1_START_MONITOR, 1);
    if (ret < 0)
    	goto exit_free;
    
    /* force update on first occasion */
    data->next_update = jiffies;
    
    ret = sysfs_create_group(&client->dev.kobj, &adt7411_attr_grp);
    if (ret)
    	goto exit_free;
    
    data->hwmon_dev = hwmon_device_register(&client->dev);
    if (IS_ERR(data->hwmon_dev)) {
    	ret = PTR_ERR(data->hwmon_dev);
    	goto exit_remove;
    }
    
    dev_info(&client->dev, "successfully registered\n");
    
    return 0;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    exit_remove:
    sysfs_remove_group(&client->dev.kobj, &adt7411_attr_grp);
    exit_free:
    kfree(data);
    return ret;
    }

    static int __devexit adt7411_remove(struct i2c_client *client)
    {
    struct adt7411_data *data = i2c_get_clientdata(client);

    hwmon_device_unregister(data->hwmon_dev);
    sysfs_remove_group(&client->dev.kobj, &adt7411_attr_grp);
    kfree(data);
    return 0;
    
    • 1
    • 2
    • 3
    • 4

    }

    static const struct i2c_device_id adt7411_id[] = {
    { “adt7411”, 0 },
    { }
    };
    MODULE_DEVICE_TABLE(i2c, adt7411_id);

    static struct i2c_driver adt7411_driver = {
    .driver = {
    .name = “adt7411”,
    },
    .probe = adt7411_probe,
    .remove = __devexit_p(adt7411_remove),
    .id_table = adt7411_id,
    .detect = adt7411_detect,
    .address_list = normal_i2c,
    .class = I2C_CLASS_HWMON,
    };

    static int __init sensors_adt7411_init(void)
    {
    return i2c_add_driver(&adt7411_driver);
    }
    module_init(sensors_adt7411_init)

    static void __exit sensors_adt7411_exit(void)
    {
    i2c_del_driver(&adt7411_driver);
    }
    module_exit(sensors_adt7411_exit)

    MODULE_AUTHOR("Sascha Hauer s.hauer@pengutronix.de and "
    “Wolfram Sang w.sang@pengutronix.de”);
    MODULE_DESCRIPTION(“ADT7411 driver”);
    MODULE_LICENSE(“GPL v2”);

  • 相关阅读:
    刚参加工作的表弟问我枚举跟常量的使用场景
    解决 IllegalArgumentException: 代码点[26,143]处的Unicode字符[星]无法编码,因为它超出了允许的0到255范围 问题
    可解释的图像分类,提高组织表征的可信度论文速读
    使用ABAP Profiling
    MySQL场景面试,你是如何进行SQL优化的?
    [汇编语言王爽]笔记2--p18-p22
    K8s 场景下 Logtail 组件可观测方案升级-Logtail 事件监控发布
    【Python3】列表字典集合元组
    【Hyper-V】Windows的Hyper-V管理器创建的虚拟机上怎么复制粘贴文件
    LDAP服务器如何重启
  • 原文地址:https://blog.csdn.net/katerdaisy/article/details/133769727