四、GPIO


参考自linux-2.6.32.61\Documentation\

一、简介

GPIO全称是General Purpose Input/Output,其关联SOC上的一个管脚。平台会分配相应的GPIO和外设关联,诸如audio codec外设,GPIO和平台强相关。

GPIO可由平台配置输入输出,输出即可写,高电平为1,低电平为0。输入即可读,除了读入数据外,输入还能作为中断信号。

二、GPIO标识

GPIO标识有效范围在0..MAX_INT,负数代表该平台不支持(可用作初始化)。

平台定义了它们如何使用这些接口,并且通常为每个GPIO线使用#define宏定义符号,以便单板的启动代码与相关设计直接保持一致。与此相反,驱动应该只使用从setup代码传递给他们的GPIO号码,使用platform_data来保存单板特定的管脚配置数据(与其它所需的单板特定数据一起)。这避免了移植问题。

诸如一个平台使用32-159,另一个平台使用0-64。

int gpio_is_valid(int number);

可用此函数判断此gpio线是否有效。

三、GPIO使用

  • 分配gpio

    /* request GPIO, returning 0 or negative errno.
     * non-null labels may be useful for diagnostics.
     */
    int gpio_request(unsigned gpio, const char *label);

    /* release previously-claimed GPIO */
    void gpio_free(unsigned gpio);

此函数实现两个目的:

Note that requesting a GPIO does NOT cause it to be configured in any way; it just marks that GPIO as in use.  Separate code must handle any pin setup (e.g. controlling which pin the GPIO uses, pullup/pulldown).

Also note that it's your responsibility to have stopped using a GPIO before you free it.

  • 标记gpio的方向

    /* set as input or output, returning 0 or negative errno */
    int gpio_direction_input(unsigned gpio);
    int gpio_direction_output(unsigned gpio, int value);

  • Spinlock-Safe GPIO 访问

    /* GPIO INPUT:  return zero or nonzero */
    int gpio_get_value(unsigned gpio);
    /* GPIO OUTPUT */
    void gpio_set_value(unsigned gpio, int value);

  • 可睡的GPIO访问

    int gpio_cansleep(unsigned gpio);

    /* GPIO INPUT:  return zero or nonzero, might sleep */
    int gpio_get_value_cansleep(unsigned gpio);

    /* GPIO OUTPUT, might sleep */
    void gpio_set_value_cansleep(unsigned gpio, int value);

Other than the fact that these calls might sleep, and will not be ignored for GPIOs that can't be accessed from IRQ handlers, these calls act the same as the spinlock-safe calls.

  • gpio输入作为中断信号

  /* map GPIO numbers to IRQ numbers */
     int gpio_to_irq(unsigned gpio);

  /* map IRQ numbers to GPIO numbers (avoid using this) */
     int irq_to_gpio(unsigned irq);

gpio和中断线都是用整形标识的,其在两个name space中,这两个函数建立了gpio和中断线之间的映射关系

相关内容