Android系统GPIO按键驱动设备树配置
时间: 2023-11-15 22:37:12 浏览: 239
android系统按键驱动
在设备树中配置GPIO按键驱动需要完成以下几个步骤:
1. 定义GPIO节点
在设备树中需要定义GPIO节点,节点包含GPIO的编号、使用模式、中断类型等信息。例如:
gpio-keys {
compatible = "gpio-keys";
pinctrl-names = "default";
pinctrl-0 = <&gpio_keys_pins>;
#address-cells = <1>;
#size-cells = <0>;
power {
label = "Power button";
gpios = <&gpio1 0 GPIO_ACTIVE_LOW>;
linux,code = <KEY_POWER>;
debounce-interval = <50>;
interrupt-parent = <&gpio1>;
interrupts = <0 IRQ_TYPE_EDGE_FALLING>;
};
};
2. 定义中断控制器节点
中断控制器节点是一个必要的节点,它描述了中断控制器的类型、中断号等信息。例如:
gpio_keys_pins: gpio_keys_pins {
gpio-key1 {
gpio-hog;
gpios = <&gpio1 0 GPIO_ACTIVE_LOW>;
output-low;
line-name = "Power Button";
};
};
3. 配置中断控制器
在设备树中需要配置中断控制器,使其能够正确的处理GPIO中断。例如:
gpio1: gpio@4804c000 {
compatible = "ti,omap4-gpio";
reg = <0x4804c000 0x1000>;
interrupts = <34>;
gpio-controller;
#gpio-cells = <2>;
interrupt-controller;
#interrupt-cells = <2>;
interrupt-parent = <&intc>;
};
4. 在驱动中解析设备树
在Linux内核驱动中需要解析设备树,获取GPIO节点的信息,从而正确的控制GPIO。例如:
static int gpio_keys_probe(struct platform_device *pdev)
{
struct device_node *np = pdev->dev.of_node;
struct gpio_keys_drvdata *ddata;
struct input_dev *input_dev;
struct gpio_desc *desc;
int i, err;
ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
if (!ddata)
return -ENOMEM;
input_dev = devm_input_allocate_device(&pdev->dev);
if (!input_dev)
return -ENOMEM;
input_dev->name = "gpio-keys";
input_dev->id.bustype = BUS_GPIO;
for_each_child_of_node(np, desc) {
const char *label;
err = of_property_read_string(desc, "label", &label);
if (err)
continue;
err = of_property_read_u32(desc, "linux,code",
&ddata->keycodes[i]);
if (err)
continue;
ddata->key_count++;
err = gpiod_direction_input(desc);
if (err < 0)
continue;
ddata->gpio_descs[i] = desc;
input_set_capability(input_dev, EV_KEY, ddata->keycodes[i]);
i++;
}
input_dev->keycode = ddata->keycodes;
input_dev->keycodesize = sizeof(ddata->keycodes[0]);
input_dev->keycodemax = ddata->key_count;
ddata->input_dev = input_dev;
platform_set_drvdata(pdev, ddata);
err = input_register_device(input_dev);
if (err) {
dev_err(&pdev->dev, "Failed to register input device: %d\n",
err);
return err;
}
return 0;
}
以上就是在Android系统中配置GPIO按键驱动的设备树方法。
阅读全文