Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

Linux 下 S3C6410 的 GPIO 子系統:從 gpiolib 到 gpio_chip

S3C6410 的 GPIO 數量多、功能複用複雜,內核用 gpiolib 加上一層 Samsung 自己的包裝來管理。搞懂 s3c_gpio_chip 與 gpio_chip 的關係,之後寫驅動要控制某支腳位就不會迷路。

一、初始化入口

在 arch/arm/plat-s3c64xx/gpiolib.c 中:

arch_initcall(s3c64xx_gpiolib_init);

static __init int s3c64xx_gpiolib_init(void)
{
    s3c64xx_gpiolib_add(gpio_4bit,  ARRAY_SIZE(gpio_4bit),  s3c64xx_gpiolib_add_4bit);
    s3c64xx_gpiolib_add(gpio_4bit2, ARRAY_SIZE(gpio_4bit2), s3c64xx_gpiolib_add_4bit2);
    s3c64xx_gpiolib_add(gpio_2bit,  ARRAY_SIZE(gpio_2bit),  NULL);
    return 0;
}

arch_initcall 表示這段會在系統初始化階段被呼叫。三個 s3c64xx_gpiolib_add() 傳入的 gpio_4bit/gpio_4bit2/gpio_2bit 都是 struct s3c_gpio_chip 陣列,差別在於每個 GPIO 群組的暫存器配置方式:4 bit 型、4 bit 第二型,以及只有 2 bit 的群組,最後一個參數就是對應的註冊函式。

二、Samsung 的包裝:s3c_gpio_chip

/**
 * struct s3c_gpio_chip - 對具體 GPIO 實作的包裝
 * @chip:   要輸出給 gpiolib 的 gpio_chip
 * @base:   GPIO 配置暫存器的基底
 * @config: 特殊功能與上下拉電阻的控制資訊
 */
struct s3c_gpio_chip {
    struct gpio_chip      chip;
    struct s3c_gpio_cfg  *config;
    void __iomem         *base;
};

三、通用抽象:gpio_chip

/**
 * struct gpio_chip - 對 GPIO 控制器的抽象
 * @label:            供診斷使用
 * @dev:              提供 GPIO 的裝置(可選)
 * @owner:            避免仍有 GPIO 被使用時模組被移除
 * @request / @free:  芯片特定的啟用/停用(如電源與時鐘)
 * @direction_input:  把 offset 設為輸入
 * @get:              讀取 offset 的值
 * @direction_output: 把 offset 設為輸出
 * @set:              設定 offset 的輸出值
 * @to_irq:           支援非靜態的 gpio_to_irq() 映射
 * @base:             此控制器負責的第一個 GPIO 號;註冊時為負值則動態配置
 * @ngpio:            此控制器負責的 GPIO 數量
 * @can_sleep:        get()/set() 會睡眠時必須設定
 */
struct gpio_chip {
    const char        *label;
    struct device     *dev;
    struct module     *owner;
    int  (*request)(struct gpio_chip *chip, unsigned offset);
    void (*free)(struct gpio_chip *chip, unsigned offset);
    int  (*direction_input)(struct gpio_chip *chip, unsigned offset);
    int  (*get)(struct gpio_chip *chip, unsigned offset);
    int  (*direction_output)(struct gpio_chip *chip, unsigned offset, int value);
    void (*set)(struct gpio_chip *chip, unsigned offset, int value);
    int  (*to_irq)(struct gpio_chip *chip, unsigned offset);
    void (*dbg_show)(struct seq_file *s, struct gpio_chip *chip);
    int                 base;
    u16                 ngpio;
    unsigned            can_sleep:1;
    unsigned            exported:1;
};

gpio_chip 的價值在於「統一介面」:不論 GPIO 來自 SoC 控制器、FPGA、I2C/SPI 的 GPIO expander 還是多功能晶片,都能用同一套 gpio_get_value()/gpio_set_value() 操作。每個 chip 負責 ngpio 支腳位,以 offset(0 到 ngpio-1)識別;實際呼叫時,offset 就是以 GPIO 號減去 base 算出來的。

四、具體實例:gpio_4bit

static struct s3c_gpio_chip gpio_4bit[] = {
    { .base = S3C64XX_GPA_BASE, .config = &gpio_4bit_cfg_eint0111,
      .chip = { .base = S3C64XX_GPA(0), .ngpio = S3C64XX_GPIO_A_NR, .label = "GPA" } },
    { .base = S3C64XX_GPB_BASE, .config = &gpio_4bit_cfg_eint0111,
      .chip = { .base = S3C64XX_GPB(0), .ngpio = S3C64XX_GPIO_B_NR, .label = "GPB" } },
    { .base = S3C64XX_GPC_BASE, .config = &gpio_4bit_cfg_eint0111,
      .chip = { .base = S3C64XX_GPC(0), .ngpio = S3C64XX_GPIO_C_NR, .label = "GPC" } },
    { .base = S3C64XX_GPD_BASE, .config = &gpio_4bit_cfg_eint0111,
      .chip = { .base = S3C64XX_GPD(0), .ngpio = S3C64XX_GPIO_D_NR, .label = "GPD" } },
    { .base = S3C64XX_GPE_BASE, .config = &gpio_4bit_cfg_noint,
      .chip = { .base = S3C64XX_GPE(0), .ngpio = S3C64XX_GPIO_E_NR, .label = "GPE" } },
    { .base = S3C64XX_GPG_BASE, .config = &gpio_4bit_cfg_eint0111,
      .chip = { .base = S3C64XX_GPG(0), .ngpio = S3C64XX_GPIO_G_NR, .label = "GPG" } },
    { .base = S3C64XX_GPM_BASE, .config = &gpio_4bit_cfg_eint0011,
      .chip = { .base = S3C64XX_GPM(0), .ngpio = S3C64XX_GPIO_M_NR, .label = "GPM" } },
};

從這份表格可以讀出兩個資訊:config 決定了該群組哪些腳位能當外部中斷、觸發方式是什麼(例如 GPA 是 0111、GPM 是 0011);chip.base 則決定了這群腳位在全域 GPIO 號碼空間中的起點。因此 gpio_to_irq() 能否用、/sys/class/gpio 下的編號是多少,都在這裡定案。

小結

整條鏈是:gpiolib 提供標準 API → gpio_chip 定義每個控制器的操作介面 → s3c_gpio_chip 補上 Samsung 特有的暫存器基底與功能設定 → gpio_4bit 等表格把實際群組填進去。要新增或修改某支腳位的行為,改的是表格與 s3c_gpio_cfg,而不是 gpiolib 本身。

Copyright © Linux教程網 All Rights Reserved