调用流程:
在看显示模块的代码时看到一个函数devm_gpiod_get_index(), 之前没接触过,它的调用如下:
devm_gpiod_get_index -> //index为0 gpiod_get_index
可以看到devm_gpiod_get_index只是对gpiod_get_index的包装而已,并且index为0。index参数后面会提。
函数参数:
struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev, const char *con_id, unsigned int idx, enum gpiod_flags flags);
重点关注第二个和第三个参数。
gpiod_get_index()到底用来干什么?
答:gpiod_get_index()本质上和gpio_request()是一个功能,是申请gpio的,只是它是从device tree去查找,
因此看到第二个参数”con_id”是字符串类型,也就是gpio的名字。
例如在SD卡驱动看到的去查找名字为”cd-gpios”的gpio
simple.c:
ctx->cd_gpio = devm_gpiod_get_optional(dev, "cd", 0);
在使用SD卡驱动的主dts就有cd pin的定义
xxx.dts:
cd-gpios = <&gpio2 12 GPIO_ACTIVE_LOW>;
那么index又有什么用呢?
内核文档有个例子,比如gpio如下定义:
led-gpios = <&gpio 15 GPIO_ACTIVE_HIGH>, /* red */ <&gpio 16 GPIO_ACTIVE_HIGH>, /* green */ <&gpio 17 GPIO_ACTIVE_HIGH>; /* blue */
如果index是0,那么对应的就是gpio 15; 如果index是1,那么对应就是gpio 16,以此类推。