/** * gpiod_get - obtain a GPIO for a given GPIO function * @dev: GPIO consumer, can be NULL for system-global GPIOs * @con_id: function within the GPIO consumer * @flags: optional GPIO initialization flags * * Return the GPIO descriptor corresponding to the function con_id of device * dev, -ENOENT if no GPIO has been assigned to the requested function, or * another IS_ERR() code if an error occurred while trying to acquire the GPIO. */ struct gpio_desc *__must_check gpiod_get(struct device *dev, constchar *con_id,enum gpiod_flags flags) { return gpiod_get_index(dev, con_id, 0, flags); } EXPORT_SYMBOL_GPL(gpiod_get);
回到 gpio_get 函数,参数中 dev 就表示使用者设备,Linux 会根据这个设备找到设备树节点。con_id 表示 GPIO 的功能名称,例如 “led”,Linux 就会在刚刚的设备树节点中查找 led-gpios,flags 表示初始化方式,比如常见的 GPIOD_IN、GPIOD_OUT_LOW、GPIOD_OUT_HIGH 等等。 可以看到函数内部只是调用了 gpiod_get_index(),这是一种 Linux 常见的设计模式,这样设计的原因是 Linux 支持多个 GPIO,这样设计省略了 index 参数。当你只有一个 gpio 的时候,可以直接调用 gpiod_get 函数,当你需要多个 GPIO 的时候,就要使用 gpiod_get_index() 来指定 index。
gpiod_set_value
1 2 3 4 5 6 7 8
voidgpiod_set_value(struct gpio_desc *desc, int value) { VALIDATE_DESC_VOID(desc); /* Should be using gpiod_set_value_cansleep() */ WARN_ON(desc->gdev->chip->can_sleep); gpiod_set_value_nocheck(desc, value); } EXPORT_SYMBOL_GPL(gpiod_set_value);