linux adc_keys_probe函数分析
时间: 2023-05-26 11:02:56 浏览: 150
adc_keys_probe函数是Linux内核中与ADC按键相关的设备树探测函数。在设备树中,如果有ADC按键的相关信息(如所使用的ADC控制器、引脚等),Linux内核会自动调用该函数进行探测。
其主要功能包括:
1. 读取设备树中ADC按键节点的相关信息,如所用ADC控制器和引脚号;
2. 根据以上信息初始化ADC控制器,并将其与对应的GPIO引脚进行绑定;
3. 注册Linux输入子系统的按键输入设备,并将其与初始化好的ADC控制器进行关联。
下面是该函数的代码实现:
static int adc_keys_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
struct adc_keys_platform_data *pdata = dev_get_platdata(dev);
const char *adc_name = pdata ? pdata->adc_name : NULL;
struct input_dev *input_dev;
struct adc_keys *keys;
int ret, i;
if (!adc_name) {
dev_err(dev, "no ADC controller specified in platform data\n");
return -EINVAL;
}
input_dev = devm_input_allocate_device(dev);
if (!input_dev)
return -ENOMEM;
keys = devm_kzalloc(dev, sizeof(*keys), GFP_KERNEL);
if (!keys)
return -ENOMEM;
platform_set_drvdata(pdev, keys);
keys->input = input_dev;
keys->adc = devm_iio_channel_get(&pdev->dev, "iio");
if (IS_ERR(keys->adc)) {
dev_err(dev, "failed to get ADC channel\n");
ret = PTR_ERR(keys->adc);
goto err_free_mem;
}
input_dev->name = pdev->name;
input_dev->phys = "keys/input0";
input_dev->id.bustype = BUS_HOST;
input_dev->id.vendor = 0x0001;
input_dev->id.product = 0x0001;
input_dev->id.version = 0x0100;
input_set_capability(input_dev, EV_KEY, KEY_POWER);
input_set_capability(input_dev, EV_KEY, KEY_VOLUMEUP);
input_set_capability(input_dev, EV_KEY, KEY_VOLUMEDOWN);
keys->min_val = pdata ? pdata->min_val : ADC_KEYS_DEFAULT_MAX;
keys->max_val = pdata ? pdata->max_val : ADC_KEYS_DEFAULT_MIN;
ret = input_register_device(input_dev);
if (ret) {
dev_err(dev, "failed to register input device\n");
goto err_free_mem;
}
ret = adc_keys_init_dev(keys, adc_name);
if (ret) {
dev_err(dev, "failed to init ADC controller\n");
goto err_free_dev;
}
for (i = 0; i < ARRAY_SIZE(keys->keymap); i++) {
ret = input_register_keycode(input_dev, keys->keymap[i].type,
keys->keymap[i].code, NULL);
if (ret) {
dev_err(dev, "failed to register input keycode\n");
goto err_free_dev;
}
}
ret = adc_keys_set_timer_interval(keys);
if (ret)
dev_warn(dev, "Failed to initialize the polling timer\n");
dev_info(dev, "registered ADC keys input device\n");
return 0;
err_free_dev:
input_unregister_device(input_dev);
err_free_mem:
return ret;
}
该函数通过dev_get_platdata函数读取设备树节点的平台数据信息,获取相关参数。接下来,分别进行输入子系统的相关初始化、iio_channel获取、ADC控制器的初始化、按键注册及关联操作,最终成功时输出相关信息并返回0,失败则进行相应的错误处理操作。
阅读全文