一、軟件環境
Andorid Version:5.0
Cpu & Arch:Intel X86_64
二、File Path
include/linux/gpio.h
include/asm-generic/gpio.h
drivers/gpio/gpiolib.c
三、代碼分析
int gpio_request(unsigned gpio, const char *label)
{
<span style="white-space:pre"> </span>return gpiod_request(gpio_to_desc(gpio), label);
}
EXPORT_SYMBOL_GPL(gpio_request);
(1)根據gpio序號獲取相應的描述gpio的結構體desc
static struct gpio_desc *gpio_to_desc(unsigned gpio)
{ //<span style="font-family: Arial, Helvetica, sans-serif;">條件爲真時(</span><span style="font-family: Arial, Helvetica, sans-serif;">0 <= gpio < ARCH_NR_GPIOS)輸出Debug信息,且返回NULL</span>
if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio))
return NULL;
else
return &gpio_desc[gpio];
}
(2)根據desc與label,調用gpiod_request註冊gpio口
static int gpiod_request(struct gpio_desc *desc, const char *label)
{
struct gpio_chip *chip;
int status = -EPROBE_DEFER;
unsigned long flags;
if (!desc) {
pr_warn("%s: invalid GPIO\n", __func__);
return -EINVAL;
}
spin_lock_irqsave(&gpio_lock, flags);
chip = desc->chip;
if (chip == NULL)
goto done;
if (!try_module_get(chip->owner))
goto done;
/* NOTE: gpio_request() can be called in early boot,
* before IRQs are enabled, for non-sleeping (SOC) GPIOs.
*/
if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {//設置flags的第0位爲1,並返回原來的值
desc_set_label(desc, label ? : "?");
status = 0;
} else {
status = -EBUSY;
module_put(chip->owner);
goto done;
}
if (chip->request) {
/* chip->request may sleep */
spin_unlock_irqrestore(&gpio_lock, flags);
status = chip->request(chip, gpio_chip_hwgpio(desc));
spin_lock_irqsave(&gpio_lock, flags);
if (status < 0) {
desc_set_label(desc, NULL);
module_put(chip->owner);
clear_bit(FLAG_REQUESTED, &desc->flags);
goto done;
}
}
if (chip->get_direction) {
/* chip->get_direction may sleep */
spin_unlock_irqrestore(&gpio_lock, flags);
gpiod_get_direction(desc);
spin_lock_irqsave(&gpio_lock, flags);
}
done:
if (status)
pr_debug("_gpio_request: gpio-%d (%s) status %d\n",
desc_to_gpio(desc), label ? : "?", status);
spin_unlock_irqrestore(&gpio_lock, flags);
return status;
}
Q&A
==================================================================================================
一、驅動中是如何調到gpiolib.c中的gpio_reuqset函數的?
(1)頭文件包含
#inlcude <linux/gpio.h>
#ifdef CONFIG_GPIOLIB
#ifdef CONFIG_ARCH_HAVE_CUSTOM_GPIO_H
#include <asm/gpio.h>
#else
#include <asm-generic/gpio.h>
#endif /* ! CONFIG_ARCH_HAVE_CUSTOM_GPIO_H */
#else /* ! CONFIG_GPIOLIB */
......
#endif /* ! CONFIG_GPIOLIB */
(2)函數聲明
include/asm-generic/gpio.h
extern int gpio_request(unsigned gpio, const char *label);
(3)符號導出
drivers/gpio/gpiolib.c
EXPORT_SYMBOL_GPL(gpio_request);
二、gpio_requset的返回值
根據官方文檔Documentation/gpio.txt可知返回值有如下關係
0 :成功
負值 :失敗
具體分析gpiod_request函數
(1)
if (!desc) {
pr_warn("%s: invalid GPIO\n", __func__);
return -EINVAL;
}
(2)
if (chip == NULL)
return -EPROBE_DEFER;
(3)
if (!try_module_get(chip->owner))
return -EPROBE_DEFER;
(4)
if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0)
return -EBUSY;
因此,在寫驅動代碼時如下判錯是錯誤的!
ret = gpio_request(RST_PORT, "GSL_RST");
if (ret) { //錯誤的寫法,判錯條件應爲ret < 0
printk( "ret = %d : could not req gpio reset\n", ret);
}
三、當gpio被其他模塊佔用時怎麼快速定位
根據gpio序號,在註冊的函數中加入log可以打印申請該gpio序號的label
int gpio_request(unsigned gpio, const char *label)
{
<span style="color:#ff0000;"> if (gpio == 128) {
printk("gpio[%d] label = %s\n",label);
} </span>
return gpiod_request(gpio_to_desc(gpio), label);
}