i2c设备驱动一般在probe函数的开头都会先获取platform_data数据。
1、在没有使用dts的kernel 驱动中,
要么直接赋值:client->dev.platform_data = pdata;
要么使用接口设置:platform_device_add_data
一般使用如下,例如:
struct ad799x_platform_data *pdata = client->dev.platform_data;
或者struct ad799x_platform_data *pdata = dev_get_platdata(&client->dev);
static int __devinit ad799x_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
//分配一个适配器(i2c主设备)指针,获得一个i2c总线(如总线0,1,2)
struct i2c_adapter *adapter = client->adapter; //其中client是在板级信息初始化的时候自动创建的设备
//从i2c_board_info注册中获取platform_data,这里是电压值
struct ad799x_platform_data *pdata = client->dev.platform_data;
//private data
struct ad799x_data *data;
int err;
//判定适配器能力
if(!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA|
I2C_FUNC_SMBUS_WORD_DATA)){
err = -EOPNOTSUPP; //操作不支持
goto exit;
}
if(!(data = kzalloc(sizeof(struct ad799x_data), GFP_KERNEL))){
err = -ENOMEM; //内存溢出
goto exit;
}
......
arch/arm/mach-mx6/board-xxx.c
static struct i2c_board_info mxc_i2c2_board_info[] __initdata = {
{
I2C_BOARD_INFO("ad799x", 0x48),
.platform_data = &ad799x_pdata,
},
在board-xxx.c中赋值,在probe函数中获取。
2、在使用dts的kernel驱动中,
一般在probe函数中创建。
pdata = devm_kzalloc(&client->dev, sizeof(struct goodix_ts_platform_data), GFP_KERNEL);
例如:
static int goodix_ts_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
struct goodix_ts_platform_data *pdata;
struct goodix_ts_data *ts;
u16 version_info;
int ret;
dev_dbg(&client->dev, "GTP I2C Address: 0x%02x\n", client->addr);
if (client->dev.of_node) {
pdata = devm_kzalloc(&client->dev,
sizeof(struct goodix_ts_platform_data), GFP_KERNEL);
if (!pdata) {
dev_err(&client->dev,
"GTP Failed to allocate memory for pdata\n");
return -ENOMEM;
}
ret = goodix_parse_dt(&client->dev, pdata);
if (ret)
return ret;
} else {
pdata = client->dev.platform_data;
}
if (!pdata) {
dev_err(&client->dev, "GTP invalid pdata\n");
return -EINVAL;
}
......
---------------------
作者:习_霜
来源:CSDN
原文:https://blog.csdn.net/xishuang_gongzi/article/details/52856950
版权声明:本文为博主原创文章,转载请附上博文链接!