I2C设备ID表的driver_data成员的用法 [英] usage of driver_data member of I2C device id table
问题描述
我正在尝试了解I2C客户端驱动程序.根据我的理解,在注册I2C驱动程序之前,我们必须定义i2c_device_id
表和设备树兼容表.
I am trying to understand I2C client drivers. As per my understanding before registering I2C driver we have to define i2c_device_id
table and device tree compatible table.
我有以下疑问.请帮我理解一下.
I have following doubts. Could please help me to understand.
1)i2c_device_id
结构的定义包含两个成员(name
,driver_data
).第一个成员(name
)用于定义将在驱动程序绑定期间使用的设备名称,第二个成员(driver_data
)的用途是什么.
1) The definition of i2c_device_id
structure contains two members (name
, driver_data
). The 1st member (name
) is used to define the device name which will be used during driver binding, what is the use of the 2nd member (driver_data
).
2)驱动程序绑定将基于i2c_device_id
表或设备树兼容字符串进行.
2) Driver binding will happen based on i2c_device_id
table or device tree compatible string.
谢谢.
推荐答案
1)
i2c_device_id
结构的定义包含两个成员(name
,driver_data
).第一个成员(name
)用于定义将在驱动程序绑定期间使用的设备名称,第二个成员(driver_data
)的用途是什么.
1) The definition of
i2c_device_id
structure contains two members (name
,driver_data
). The 1st member (name
) is used to define the device name which will be used during driver binding, what is the use of the 2nd member (driver_data
).
首先,您定义i2c_device_id
结构的表(数组),就像在 max732x.c 驱动程序:
First you define the table (array) of i2c_device_id
structures, like it's done in max732x.c driver:
static const struct i2c_device_id max732x_id[] = {
{ "max7319", 0 },
{ "max7320", 1 },
{ "max7321", 2 },
{ },
};
MODULE_DEVICE_TABLE(i2c, max732x_id);
在驱动程序探测功能中,您具有该数组的一个元素(用于您的特定设备)作为第二个参数:
In your driver probe function you have one element of this array (for your particular device) as second parameter:
static int max732x_probe(struct i2c_client *client,
const struct i2c_device_id *id)
现在,您可以根据自己的目的使用 id->driver_data
(这对于表中的每个设备都是唯一的).例如.对于"max7320"芯片,driver_data
将为1
.
Now you can use id->driver_data
(which is unique to each device from the table) for your own purposes. E.g. for "max7320" chip driver_data
will be 1
.
例如,如果您具有特定于每个设备的功能,则可以创建如下功能数组:
For example, if you have features which are specific to each device, you can create the array of features like this:
static uint64_t max732x_features[] = {
[0] = FEATURE0,
[1] = FEATURE1 | FEATURE2,
[2] = FEATURE2
};
,您可以从此阵列中获取特定设备的功能,如下所示:
and you can obtain features of your particular device from this array like this:
max732x_features[id->driver_data]
当然,出于相同的原因,您可以使用驱动程序名称.但是,这将需要更多的代码和更多的CPU时间.因此,基本上,如果您不需要驱动程序driver_data
,只需为所有设备(在设备表中)将其设置为0
.
Of course, you can use driver name for the same reason. But it would take more code and more CPU time. So basically if you don't need driver_data
for your driver -- you just make it 0
for all devices (in device table).
2)驱动程序绑定将基于
i2c_device_id
表或设备树兼容字符串进行.
2) Driver binding will happen based on
i2c_device_id
table or device tree compatible string.
要弄清楚这一点,您可以看一下 i2c_device_match()
函数(例如
To figure this out you can take a look at i2c_device_match()
function (for example here). As you can see, first I2C core tries to match device by compatible
string (OF style, which is Device Tree). And if it fails, it then tries to match device by id table.
这篇关于I2C设备ID表的driver_data成员的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!