i2c裝置驅動probe函式中platform_data
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 版權宣告:本文為博主原創文章,轉載請附上博文連結!