GPIO(General Purpose I/O) 對嵌入式系統的開發者應該是很熟悉的東西,之前有轉錄一篇 傳統 gpio 的控制方法

但這是立基於SoC provider 的所提供的 API,並不具備一個標準的介面。而且在嵌入式系統大力發展下,越來越多的

SoC 推出,加上SoC要連接越來越多的周邊,會使用FPGA或是 I/O expander 來擴充 GPIO 數量。因此除了SoC

本身的介面之外,開發者還必須藉由另外一套或更多的介面來使用 GPIO,這種沒有共通標準的東西勢必會造成開發上的

一些困擾,所以這幾年 linux 發展出一套新的 gpiolib 來解決這問題。作者 David Brownell 的patch 是這樣敘述的:

   gpiolib: add gpio provider infrastructure
    Provide new implementation infrastructure that platforms may choose to use
    when implementing the GPIO programming interface.  Platforms can update their
    GPIO support to use this.  In many cases the incremental cost to access a
    non-inlined GPIO should be less than a dozen instructions, with the memory
    cost being about a page (total) of extra data and code.  The upside is:
      * Providing two features which were "want to have (but OK to defer)" when
        GPIO interfaces were first discussed in November 2006:
        -  A "struct gpio_chip" to plug in GPIOs that aren't directly supported
        by SOC platforms, but come from FPGAs or other multifunction devices
        using conventional device registers
(like UCB-1x00 or SM501 GPIOs,
        and southbridges in PCs with more open specs than usual).
        -  Full support for message-based GPIO expanders, where registers are
        accessed through sleeping I/O calls.
 Previous support for these
        "cansleep" calls was just stubs.  (One example: the widely used
        pcf8574 I2C chips, with 8 GPIOs each.)
      * Including a non-stub implementation of the gpio_{request,free}() calls,
        making those calls much more useful.  The diagnostic labels are also
        recorded given DEBUG_FS, so /sys/kernel/debug/gpio can show a snapshot
        of all GPIOs known to this infrastructure.
    The driver programming interfaces introduced in 2.6.21 do not change at all;
    this infrastructure is entirely below those covers.


gpiolib 雖於 2008 的2.6.21 就引進,但在Android 上開始使用大約是2.6.32 的版本,因此建議 Kernel 版本最好是

2.6.32 以上。

首先還是讀一下 kernel document 了解概況,其位置在 kernel/Documentation/gpio.txt


gpiolib 的實作則是在drivers/gpio/gpiolib.c


- 待續 -


由於一般多是使用 i2c 或其他的 bus 去控制這些 gpio expander,所以必須考慮到的東西相對就多了,例如 sleep 就


- gpio_set_value_cansleep()/gpio_get_value_cansleep() 這組 function




創作者 huenlil 的頭像

H's 手札

huenlil 發表在 痞客邦 留言(0) 人氣()