e460379de6295cbd5cfa06fdbb4643df841f65e4
[openembedded.git] /
1 From 7ba82399f2d2df6114ad552999f2e1b9a19cb47a Mon Sep 17 00:00:00 2001
2 From: David Brownell <dbrownell@users.sourceforge.net>
3 Date: Sat, 19 Jan 2008 19:41:18 +0300
4 Subject: [PATCH 24/64] Update Documentation/gpio.txt, primarily to include the new "gpiolib"
5  infrastructure.
6
7 Signed-off-by: David Brownell <dbrownell@users.sourceforge.net>
8 Cc: Jean Delvare <khali@linux-fr.org>
9 Cc: Eric Miao <eric.miao@marvell.com>
10 Cc: Sam Ravnborg <sam@ravnborg.org>
11 Cc: Haavard Skinnemoen <hskinnemoen@atmel.com>
12 Cc: Philipp Zabel <philipp.zabel@gmail.com>
13 Cc: Russell King <rmk@arm.linux.org.uk>
14 Cc: Ben Gardner <bgardner@wabtec.com>
15 Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
16 ---
17  Documentation/gpio.txt |  133 +++++++++++++++++++++++++++++++++++++++++++----
18  1 files changed, 121 insertions(+), 12 deletions(-)
19
20 diff --git a/Documentation/gpio.txt b/Documentation/gpio.txt
21 index 6bc2ba2..8da724e 100644
22 --- a/Documentation/gpio.txt
23 +++ b/Documentation/gpio.txt
24 @@ -32,7 +32,7 @@ The exact capabilities of GPIOs vary between systems.  Common options:
25    - Input values are likewise readable (1, 0).  Some chips support readback
26      of pins configured as "output", which is very useful in such "wire-OR"
27      cases (to support bidirectional signaling).  GPIO controllers may have
28 -    input de-glitch logic, sometimes with software controls.
29 +    input de-glitch/debounce logic, sometimes with software controls.
30  
31    - Inputs can often be used as IRQ signals, often edge triggered but
32      sometimes level triggered.  Such IRQs may be configurable as system
33 @@ -60,10 +60,13 @@ used on a board that's wired differently.  Only least-common-denominator
34  functionality can be very portable.  Other features are platform-specific,
35  and that can be critical for glue logic.
36  
37 -Plus, this doesn't define an implementation framework, just an interface.
38 +Plus, this doesn't require any implementation framework, just an interface.
39  One platform might implement it as simple inline functions accessing chip
40  registers; another might implement it by delegating through abstractions
41 -used for several very different kinds of GPIO controller.
42 +used for several very different kinds of GPIO controller.  (There is some
43 +optional code supporting such an implementation strategy, described later
44 +in this document, but drivers acting as clients to the GPIO interface must
45 +not care how it's implemented.)
46  
47  That said, if the convention is supported on their platform, drivers should
48  use it when possible.  Platforms should declare GENERIC_GPIO support in
49 @@ -121,6 +124,11 @@ before tasking is enabled, as part of early board setup.
50  For output GPIOs, the value provided becomes the initial output value.
51  This helps avoid signal glitching during system startup.
52  
53 +For compatibility with legacy interfaces to GPIOs, setting the direction
54 +of a GPIO implicitly requests that GPIO (see below) if it has not been
55 +requested already.  That compatibility may be removed in the future;
56 +explicitly requesting GPIOs is strongly preferred.
57 +
58  Setting the direction can fail if the GPIO number is invalid, or when
59  that particular GPIO can't be used in that mode.  It's generally a bad
60  idea to rely on boot firmware to have set the direction correctly, since
61 @@ -133,6 +141,7 @@ Spinlock-Safe GPIO access
62  -------------------------
63  Most GPIO controllers can be accessed with memory read/write instructions.
64  That doesn't need to sleep, and can safely be done from inside IRQ handlers.
65 +(That includes hardirq contexts on RT kernels.)
66  
67  Use these calls to access such GPIOs:
68  
69 @@ -145,7 +154,7 @@ Use these calls to access such GPIOs:
70  The values are boolean, zero for low, nonzero for high.  When reading the
71  value of an output pin, the value returned should be what's seen on the
72  pin ... that won't always match the specified output value, because of
73 -issues including wire-OR and output latencies.
74 +issues including open-drain signaling and output latencies.
75  
76  The get/set calls have no error returns because "invalid GPIO" should have
77  been reported earlier from gpio_direction_*().  However, note that not all
78 @@ -170,7 +179,8 @@ get to the head of a queue to transmit a command and get its response.
79  This requires sleeping, which can't be done from inside IRQ handlers.
80  
81  Platforms that support this type of GPIO distinguish them from other GPIOs
82 -by returning nonzero from this call:
83 +by returning nonzero from this call (which requires a valid GPIO number,
84 +either explicitly or implicitly requested):
85  
86         int gpio_cansleep(unsigned gpio);
87  
88 @@ -209,8 +219,11 @@ before tasking is enabled, as part of early board setup.
89  These calls serve two basic purposes.  One is marking the signals which
90  are actually in use as GPIOs, for better diagnostics; systems may have
91  several hundred potential GPIOs, but often only a dozen are used on any
92 -given board.  Another is to catch conflicts between drivers, reporting
93 -errors when drivers wrongly think they have exclusive use of that signal.
94 +given board.  Another is to catch conflicts, identifying errors when
95 +(a) two or more drivers wrongly think they have exclusive use of that
96 +signal, or (b) something wrongly believes it's safe to remove drivers
97 +needed to manage a signal that's in active use.  That is, requesting a
98 +GPIO can serve as a kind of lock.
99  
100  These two calls are optional because not not all current Linux platforms
101  offer such functionality in their GPIO support; a valid implementation
102 @@ -223,6 +236,9 @@ Note that requesting a GPIO does NOT cause it to be configured in any
103  way; it just marks that GPIO as in use.  Separate code must handle any
104  pin setup (e.g. controlling which pin the GPIO uses, pullup/pulldown).
105  
106 +Also note that it's your responsibility to have stopped using a GPIO
107 +before you free it.
108 +
109  
110  GPIOs mapped to IRQs
111  --------------------
112 @@ -238,7 +254,7 @@ map between them using calls like:
113  
114  Those return either the corresponding number in the other namespace, or
115  else a negative errno code if the mapping can't be done.  (For example,
116 -some GPIOs can't used as IRQs.)  It is an unchecked error to use a GPIO
117 +some GPIOs can't be used as IRQs.)  It is an unchecked error to use a GPIO
118  number that wasn't set up as an input using gpio_direction_input(), or
119  to use an IRQ number that didn't originally come from gpio_to_irq().
120  
121 @@ -299,17 +315,110 @@ Related to multiplexing is configuration and enabling of the pullups or
122  pulldowns integrated on some platforms.  Not all platforms support them,
123  or support them in the same way; and any given board might use external
124  pullups (or pulldowns) so that the on-chip ones should not be used.
125 +(When a circuit needs 5 kOhm, on-chip 100 kOhm resistors won't do.)
126  
127  There are other system-specific mechanisms that are not specified here,
128  like the aforementioned options for input de-glitching and wire-OR output.
129  Hardware may support reading or writing GPIOs in gangs, but that's usually
130  configuration dependent:  for GPIOs sharing the same bank.  (GPIOs are
131  commonly grouped in banks of 16 or 32, with a given SOC having several such
132 -banks.)  Some systems can trigger IRQs from output GPIOs.  Code relying on
133 -such mechanisms will necessarily be nonportable.
134 +banks.)  Some systems can trigger IRQs from output GPIOs, or read values
135 +from pins not managed as GPIOs.  Code relying on such mechanisms will
136 +necessarily be nonportable.
137  
138 -Dynamic definition of GPIOs is not currently supported; for example, as
139 +Dynamic definition of GPIOs is not currently standard; for example, as
140  a side effect of configuring an add-on board with some GPIO expanders.
141  
142  These calls are purely for kernel space, but a userspace API could be built
143 -on top of it.
144 +on top of them.
145 +
146 +
147 +GPIO implementor's framework (OPTIONAL)
148 +=======================================
149 +As noted earlier, there is an optional implementation framework making it
150 +easier for platforms to support different kinds of GPIO controller using
151 +the same programming interface.
152 +
153 +As a debugging aid, if debugfs is available a /sys/kernel/debug/gpio file
154 +will be found there.  That will list all the controllers registered through
155 +this framework, and the state of the GPIOs currently in use.
156 +
157 +
158 +Controller Drivers: gpio_chip
159 +-----------------------------
160 +In this framework each GPIO controller is packaged as a "struct gpio_chip"
161 +with information common to each controller of that type:
162 +
163 + - methods to establish GPIO direction
164 + - methods used to access GPIO values
165 + - flag saying whether calls to its methods may sleep
166 + - optional debugfs dump method (showing extra state like pullup config)
167 + - label for diagnostics
168 +
169 +There is also per-instance data, which may come from device.platform_data:
170 +the number of its first GPIO, and how many GPIOs it exposes.
171 +
172 +The code implementing a gpio_chip should support multiple instances of the
173 +controller, possibly using the driver model.  That code will configure each
174 +gpio_chip and issue gpiochip_add().  Removing a GPIO controller should be
175 +rare; use gpiochip_remove() when it is unavoidable.
176 +
177 +Most often a gpio_chip is part of an instance-specific structure with state
178 +not exposed by the GPIO interfaces, such as addressing, power management,
179 +and more.  Chips such as codecs will have complex non-GPIO state,
180 +
181 +Any debugfs dump method should normally ignore signals which haven't been
182 +requested as GPIOs.  They can use gpiochip_is_requested(), which returns
183 +either NULL or the label associated with that GPIO when it was requested.
184 +
185 +
186 +Platform Support
187 +----------------
188 +To support this framework, a platform's Kconfig will "select HAVE_GPIO_LIB"
189 +and arrange that its <asm/gpio.h> includes <asm-generic/gpio.h> and defines
190 +three functions: gpio_get_value(), gpio_set_value(), and gpio_cansleep().
191 +They may also want to provide a custom value for ARCH_NR_GPIOS.
192 +
193 +Trivial implementations of those functions can directly use framework
194 +code, which always dispatches through the gpio_chip:
195 +
196 +  #define gpio_get_value       __gpio_get_value
197 +  #define gpio_set_value       __gpio_set_value
198 +  #define gpio_cansleep                __gpio_cansleep
199 +
200 +Fancier implementations could instead define those as inline functions with
201 +logic optimizing access to specific SOC-based GPIOs.  For example, if the
202 +referenced GPIO is the constant "12", getting or setting its value could
203 +cost as little as two or three instructions, never sleeping.  When such an
204 +optimization is not possible those calls must delegate to the framework
205 +code, costing at least a few dozen instructions.  For bitbanged I/O, such
206 +instruction savings can be significant.
207 +
208 +For SOCs, platform-specific code defines and registers gpio_chip instances
209 +for each bank of on-chip GPIOs.  Those GPIOs should be numbered/labeled to
210 +match chip vendor documentation, and directly match board schematics.  They
211 +may well start at zero and go up to a platform-specific limit.  Such GPIOs
212 +are normally integrated into platform initialization to make them always be
213 +available, from arch_initcall() or earlier; they can often serve as IRQs.
214 +
215 +
216 +Board Support
217 +-------------
218 +For external GPIO controllers -- such as I2C or SPI expanders, ASICs, multi
219 +function devices, FPGAs or CPLDs -- most often board-specific code handles
220 +registering controller devices and ensures that their drivers know what GPIO
221 +numbers to use with gpiochip_add().  Their numbers often start right after
222 +platform-specific GPIOs.
223 +
224 +For example, board setup code could create structures identifying the range
225 +of GPIOs that chip will expose, and passes them to each GPIO expander chip
226 +using platform_data.  Then the chip driver's probe() routine could pass that
227 +data to gpiochip_add().
228 +
229 +Initialization order can be important.  For example, when a device relies on
230 +an I2C-based GPIO, its probe() routine should only be called after that GPIO
231 +becomes available.  That may mean the device should not be registered until
232 +calls for that GPIO can work.  One way to address such dependencies is for
233 +such gpio_chip controllers to provide setup() and teardown() callbacks to
234 +board specific code; those board specific callbacks would register devices
235 +once all the necessary resources are available.
236 -- 
237 1.5.3.8
238