xref: /OK3568_Linux_fs/kernel/Documentation/devicetree/bindings/gpio/gpio.txt (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593SmuzhiyunSpecifying GPIO information for devices
2*4882a593Smuzhiyun=======================================
3*4882a593Smuzhiyun
4*4882a593Smuzhiyun1) gpios property
5*4882a593Smuzhiyun-----------------
6*4882a593Smuzhiyun
7*4882a593SmuzhiyunGPIO properties should be named "[<name>-]gpios", with <name> being the purpose
8*4882a593Smuzhiyunof this GPIO for the device. While a non-existent <name> is considered valid
9*4882a593Smuzhiyunfor compatibility reasons (resolving to the "gpios" property), it is not allowed
10*4882a593Smuzhiyunfor new bindings. Also, GPIO properties named "[<name>-]gpio" are valid and old
11*4882a593Smuzhiyunbindings use it, but are only supported for compatibility reasons and should not
12*4882a593Smuzhiyunbe used for newer bindings since it has been deprecated.
13*4882a593Smuzhiyun
14*4882a593SmuzhiyunGPIO properties can contain one or more GPIO phandles, but only in exceptional
15*4882a593Smuzhiyuncases should they contain more than one. If your device uses several GPIOs with
16*4882a593Smuzhiyundistinct functions, reference each of them under its own property, giving it a
17*4882a593Smuzhiyunmeaningful name. The only case where an array of GPIOs is accepted is when
18*4882a593Smuzhiyunseveral GPIOs serve the same function (e.g. a parallel data line).
19*4882a593Smuzhiyun
20*4882a593SmuzhiyunThe exact purpose of each gpios property must be documented in the device tree
21*4882a593Smuzhiyunbinding of the device.
22*4882a593Smuzhiyun
23*4882a593SmuzhiyunThe following example could be used to describe GPIO pins used as device enable
24*4882a593Smuzhiyunand bit-banged data signals:
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun	gpio1: gpio1 {
27*4882a593Smuzhiyun		gpio-controller;
28*4882a593Smuzhiyun		#gpio-cells = <2>;
29*4882a593Smuzhiyun	};
30*4882a593Smuzhiyun	[...]
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun	data-gpios = <&gpio1 12 0>,
33*4882a593Smuzhiyun		     <&gpio1 13 0>,
34*4882a593Smuzhiyun		     <&gpio1 14 0>,
35*4882a593Smuzhiyun		     <&gpio1 15 0>;
36*4882a593Smuzhiyun
37*4882a593SmuzhiyunIn the above example, &gpio1 uses 2 cells to specify a gpio. The first cell is
38*4882a593Smuzhiyuna local offset to the GPIO line and the second cell represent consumer flags,
39*4882a593Smuzhiyunsuch as if the consumer desire the line to be active low (inverted) or open
40*4882a593Smuzhiyundrain. This is the recommended practice.
41*4882a593Smuzhiyun
42*4882a593SmuzhiyunThe exact meaning of each specifier cell is controller specific, and must be
43*4882a593Smuzhiyundocumented in the device tree binding for the device, but it is strongly
44*4882a593Smuzhiyunrecommended to use the two-cell approach.
45*4882a593Smuzhiyun
46*4882a593SmuzhiyunMost controllers are specifying a generic flag bitfield in the last cell, so
47*4882a593Smuzhiyunfor these, use the macros defined in
48*4882a593Smuzhiyuninclude/dt-bindings/gpio/gpio.h whenever possible:
49*4882a593Smuzhiyun
50*4882a593SmuzhiyunExample of a node using GPIOs:
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun	node {
53*4882a593Smuzhiyun		enable-gpios = <&qe_pio_e 18 GPIO_ACTIVE_HIGH>;
54*4882a593Smuzhiyun	};
55*4882a593Smuzhiyun
56*4882a593SmuzhiyunGPIO_ACTIVE_HIGH is 0, so in this example gpio-specifier is "18 0" and encodes
57*4882a593SmuzhiyunGPIO pin number, and GPIO flags as accepted by the "qe_pio_e" gpio-controller.
58*4882a593Smuzhiyun
59*4882a593SmuzhiyunOptional standard bitfield specifiers for the last cell:
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun- Bit 0: 0 means active high, 1 means active low
62*4882a593Smuzhiyun- Bit 1: 0 mean push-pull wiring, see:
63*4882a593Smuzhiyun           https://en.wikipedia.org/wiki/Push-pull_output
64*4882a593Smuzhiyun         1 means single-ended wiring, see:
65*4882a593Smuzhiyun           https://en.wikipedia.org/wiki/Single-ended_triode
66*4882a593Smuzhiyun- Bit 2: 0 means open-source, 1 means open drain, see:
67*4882a593Smuzhiyun           https://en.wikipedia.org/wiki/Open_collector
68*4882a593Smuzhiyun- Bit 3: 0 means the output should be maintained during sleep/low-power mode
69*4882a593Smuzhiyun         1 means the output state can be lost during sleep/low-power mode
70*4882a593Smuzhiyun- Bit 4: 0 means no pull-up resistor should be enabled
71*4882a593Smuzhiyun         1 means a pull-up resistor should be enabled
72*4882a593Smuzhiyun         This setting only applies to hardware with a simple on/off
73*4882a593Smuzhiyun         control for pull-up configuration. If the hardware has more
74*4882a593Smuzhiyun         elaborate pull-up configuration, it should be represented
75*4882a593Smuzhiyun         using a pin control binding.
76*4882a593Smuzhiyun- Bit 5: 0 means no pull-down resistor should be enabled
77*4882a593Smuzhiyun         1 means a pull-down resistor should be enabled
78*4882a593Smuzhiyun         This setting only applies to hardware with a simple on/off
79*4882a593Smuzhiyun         control for pull-down configuration. If the hardware has more
80*4882a593Smuzhiyun         elaborate pull-down configuration, it should be represented
81*4882a593Smuzhiyun         using a pin control binding.
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun1.1) GPIO specifier best practices
84*4882a593Smuzhiyun----------------------------------
85*4882a593Smuzhiyun
86*4882a593SmuzhiyunA gpio-specifier should contain a flag indicating the GPIO polarity; active-
87*4882a593Smuzhiyunhigh or active-low. If it does, the following best practices should be
88*4882a593Smuzhiyunfollowed:
89*4882a593Smuzhiyun
90*4882a593SmuzhiyunThe gpio-specifier's polarity flag should represent the physical level at the
91*4882a593SmuzhiyunGPIO controller that achieves (or represents, for inputs) a logically asserted
92*4882a593Smuzhiyunvalue at the device. The exact definition of logically asserted should be
93*4882a593Smuzhiyundefined by the binding for the device. If the board inverts the signal between
94*4882a593Smuzhiyunthe GPIO controller and the device, then the gpio-specifier will represent the
95*4882a593Smuzhiyunopposite physical level than the signal at the device's pin.
96*4882a593Smuzhiyun
97*4882a593SmuzhiyunWhen the device's signal polarity is configurable, the binding for the
98*4882a593Smuzhiyundevice must either:
99*4882a593Smuzhiyun
100*4882a593Smuzhiyuna) Define a single static polarity for the signal, with the expectation that
101*4882a593Smuzhiyunany software using that binding would statically program the device to use
102*4882a593Smuzhiyunthat signal polarity.
103*4882a593Smuzhiyun
104*4882a593SmuzhiyunThe static choice of polarity may be either:
105*4882a593Smuzhiyun
106*4882a593Smuzhiyuna1) (Preferred) Dictated by a binding-specific DT property.
107*4882a593Smuzhiyun
108*4882a593Smuzhiyunor:
109*4882a593Smuzhiyun
110*4882a593Smuzhiyuna2) Defined statically by the DT binding itself.
111*4882a593Smuzhiyun
112*4882a593SmuzhiyunIn particular, the polarity cannot be derived from the gpio-specifier, since
113*4882a593Smuzhiyunthat would prevent the DT from separately representing the two orthogonal
114*4882a593Smuzhiyunconcepts of configurable signal polarity in the device, and possible board-
115*4882a593Smuzhiyunlevel signal inversion.
116*4882a593Smuzhiyun
117*4882a593Smuzhiyunor:
118*4882a593Smuzhiyun
119*4882a593Smuzhiyunb) Pick a single option for device signal polarity, and document this choice
120*4882a593Smuzhiyunin the binding. The gpio-specifier should represent the polarity of the signal
121*4882a593Smuzhiyun(at the GPIO controller) assuming that the device is configured for this
122*4882a593Smuzhiyunparticular signal polarity choice. If software chooses to program the device
123*4882a593Smuzhiyunto generate or receive a signal of the opposite polarity, software will be
124*4882a593Smuzhiyunresponsible for correctly interpreting (inverting) the GPIO signal at the GPIO
125*4882a593Smuzhiyuncontroller.
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun2) gpio-controller nodes
128*4882a593Smuzhiyun------------------------
129*4882a593Smuzhiyun
130*4882a593SmuzhiyunEvery GPIO controller node must contain both an empty "gpio-controller"
131*4882a593Smuzhiyunproperty, and a #gpio-cells integer property, which indicates the number of
132*4882a593Smuzhiyuncells in a gpio-specifier.
133*4882a593Smuzhiyun
134*4882a593SmuzhiyunSome system-on-chips (SoCs) use the concept of GPIO banks. A GPIO bank is an
135*4882a593Smuzhiyuninstance of a hardware IP core on a silicon die, usually exposed to the
136*4882a593Smuzhiyunprogrammer as a coherent range of I/O addresses. Usually each such bank is
137*4882a593Smuzhiyunexposed in the device tree as an individual gpio-controller node, reflecting
138*4882a593Smuzhiyunthe fact that the hardware was synthesized by reusing the same IP block a
139*4882a593Smuzhiyunfew times over.
140*4882a593Smuzhiyun
141*4882a593SmuzhiyunOptionally, a GPIO controller may have a "ngpios" property. This property
142*4882a593Smuzhiyunindicates the number of in-use slots of available slots for GPIOs. The
143*4882a593Smuzhiyuntypical example is something like this: the hardware register is 32 bits
144*4882a593Smuzhiyunwide, but only 18 of the bits have a physical counterpart. The driver is
145*4882a593Smuzhiyungenerally written so that all 32 bits can be used, but the IP block is reused
146*4882a593Smuzhiyunin a lot of designs, some using all 32 bits, some using 18 and some using
147*4882a593Smuzhiyun12. In this case, setting "ngpios = <18>;" informs the driver that only the
148*4882a593Smuzhiyunfirst 18 GPIOs, at local offset 0 .. 17, are in use.
149*4882a593Smuzhiyun
150*4882a593SmuzhiyunIf these GPIOs do not happen to be the first N GPIOs at offset 0...N-1, an
151*4882a593Smuzhiyunadditional set of tuples is needed to specify which GPIOs are unusable, with
152*4882a593Smuzhiyunthe gpio-reserved-ranges binding. This property indicates the start and size
153*4882a593Smuzhiyunof the GPIOs that can't be used.
154*4882a593Smuzhiyun
155*4882a593SmuzhiyunOptionally, a GPIO controller may have a "gpio-line-names" property. This is
156*4882a593Smuzhiyunan array of strings defining the names of the GPIO lines going out of the
157*4882a593SmuzhiyunGPIO controller. This name should be the most meaningful producer name
158*4882a593Smuzhiyunfor the system, such as a rail name indicating the usage. Package names
159*4882a593Smuzhiyunsuch as pin name are discouraged: such lines have opaque names (since they
160*4882a593Smuzhiyunare by definition generic purpose) and such names are usually not very
161*4882a593Smuzhiyunhelpful. For example "MMC-CD", "Red LED Vdd" and "ethernet reset" are
162*4882a593Smuzhiyunreasonable line names as they describe what the line is used for. "GPIO0"
163*4882a593Smuzhiyunis not a good name to give to a GPIO line. Placeholders are discouraged:
164*4882a593Smuzhiyunrather use the "" (blank string) if the use of the GPIO line is undefined
165*4882a593Smuzhiyunin your design. The names are assigned starting from line offset 0 from
166*4882a593Smuzhiyunleft to right from the passed array. An incomplete array (where the number
167*4882a593Smuzhiyunof passed named are less than ngpios) will still be used up until the last
168*4882a593Smuzhiyunprovided valid line index.
169*4882a593Smuzhiyun
170*4882a593SmuzhiyunExample:
171*4882a593Smuzhiyun
172*4882a593Smuzhiyungpio-controller@00000000 {
173*4882a593Smuzhiyun	compatible = "foo";
174*4882a593Smuzhiyun	reg = <0x00000000 0x1000>;
175*4882a593Smuzhiyun	gpio-controller;
176*4882a593Smuzhiyun	#gpio-cells = <2>;
177*4882a593Smuzhiyun	ngpios = <18>;
178*4882a593Smuzhiyun	gpio-reserved-ranges = <0 4>, <12 2>;
179*4882a593Smuzhiyun	gpio-line-names = "MMC-CD", "MMC-WP", "VDD eth", "RST eth", "LED R",
180*4882a593Smuzhiyun		"LED G", "LED B", "Col A", "Col B", "Col C", "Col D",
181*4882a593Smuzhiyun		"Row A", "Row B", "Row C", "Row D", "NMI button",
182*4882a593Smuzhiyun		"poweroff", "reset";
183*4882a593Smuzhiyun}
184*4882a593Smuzhiyun
185*4882a593SmuzhiyunThe GPIO chip may contain GPIO hog definitions. GPIO hogging is a mechanism
186*4882a593Smuzhiyunproviding automatic GPIO request and configuration as part of the
187*4882a593Smuzhiyungpio-controller's driver probe function.
188*4882a593Smuzhiyun
189*4882a593SmuzhiyunEach GPIO hog definition is represented as a child node of the GPIO controller.
190*4882a593SmuzhiyunRequired properties:
191*4882a593Smuzhiyun- gpio-hog:   A property specifying that this child node represents a GPIO hog.
192*4882a593Smuzhiyun- gpios:      Store the GPIO information (id, flags, ...) for each GPIO to
193*4882a593Smuzhiyun	      affect. Shall contain an integer multiple of the number of cells
194*4882a593Smuzhiyun	      specified in its parent node (GPIO controller node).
195*4882a593SmuzhiyunOnly one of the following properties scanned in the order shown below.
196*4882a593SmuzhiyunThis means that when multiple properties are present they will be searched
197*4882a593Smuzhiyunin the order presented below and the first match is taken as the intended
198*4882a593Smuzhiyunconfiguration.
199*4882a593Smuzhiyun- input:      A property specifying to set the GPIO direction as input.
200*4882a593Smuzhiyun- output-low  A property specifying to set the GPIO direction as output with
201*4882a593Smuzhiyun	      the value low.
202*4882a593Smuzhiyun- output-high A property specifying to set the GPIO direction as output with
203*4882a593Smuzhiyun	      the value high.
204*4882a593Smuzhiyun
205*4882a593SmuzhiyunOptional properties:
206*4882a593Smuzhiyun- line-name:  The GPIO label name. If not present the node name is used.
207*4882a593Smuzhiyun
208*4882a593SmuzhiyunExample of two SOC GPIO banks defined as gpio-controller nodes:
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun	qe_pio_a: gpio-controller@1400 {
211*4882a593Smuzhiyun		compatible = "fsl,qe-pario-bank-a", "fsl,qe-pario-bank";
212*4882a593Smuzhiyun		reg = <0x1400 0x18>;
213*4882a593Smuzhiyun		gpio-controller;
214*4882a593Smuzhiyun		#gpio-cells = <2>;
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun		line_b {
217*4882a593Smuzhiyun			gpio-hog;
218*4882a593Smuzhiyun			gpios = <6 0>;
219*4882a593Smuzhiyun			output-low;
220*4882a593Smuzhiyun			line-name = "foo-bar-gpio";
221*4882a593Smuzhiyun		};
222*4882a593Smuzhiyun	};
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun	qe_pio_e: gpio-controller@1460 {
225*4882a593Smuzhiyun		compatible = "fsl,qe-pario-bank-e", "fsl,qe-pario-bank";
226*4882a593Smuzhiyun		reg = <0x1460 0x18>;
227*4882a593Smuzhiyun		gpio-controller;
228*4882a593Smuzhiyun		#gpio-cells = <2>;
229*4882a593Smuzhiyun	};
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun2.1) gpio- and pin-controller interaction
232*4882a593Smuzhiyun-----------------------------------------
233*4882a593Smuzhiyun
234*4882a593SmuzhiyunSome or all of the GPIOs provided by a GPIO controller may be routed to pins
235*4882a593Smuzhiyunon the package via a pin controller. This allows muxing those pins between
236*4882a593SmuzhiyunGPIO and other functions. It is a fairly common practice among silicon
237*4882a593Smuzhiyunengineers.
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun2.2) Ordinary (numerical) GPIO ranges
240*4882a593Smuzhiyun-------------------------------------
241*4882a593Smuzhiyun
242*4882a593SmuzhiyunIt is useful to represent which GPIOs correspond to which pins on which pin
243*4882a593Smuzhiyuncontrollers. The gpio-ranges property described below represents this with
244*4882a593Smuzhiyuna discrete set of ranges mapping pins from the pin controller local number space
245*4882a593Smuzhiyunto pins in the GPIO controller local number space.
246*4882a593Smuzhiyun
247*4882a593SmuzhiyunThe format is: <[pin controller phandle], [GPIO controller offset],
248*4882a593Smuzhiyun                [pin controller offset], [number of pins]>;
249*4882a593Smuzhiyun
250*4882a593SmuzhiyunThe GPIO controller offset pertains to the GPIO controller node containing the
251*4882a593Smuzhiyunrange definition.
252*4882a593Smuzhiyun
253*4882a593SmuzhiyunThe pin controller node referenced by the phandle must conform to the bindings
254*4882a593Smuzhiyundescribed in pinctrl/pinctrl-bindings.txt.
255*4882a593Smuzhiyun
256*4882a593SmuzhiyunEach offset runs from 0 to N. It is perfectly fine to pile any number of
257*4882a593Smuzhiyunranges with just one pin-to-GPIO line mapping if the ranges are concocted, but
258*4882a593Smuzhiyunin practice these ranges are often lumped in discrete sets.
259*4882a593Smuzhiyun
260*4882a593SmuzhiyunExample:
261*4882a593Smuzhiyun
262*4882a593Smuzhiyun    gpio-ranges = <&foo 0 20 10>, <&bar 10 50 20>;
263*4882a593Smuzhiyun
264*4882a593SmuzhiyunThis means:
265*4882a593Smuzhiyun- pins 20..29 on pin controller "foo" is mapped to GPIO line 0..9 and
266*4882a593Smuzhiyun- pins 50..69 on pin controller "bar" is mapped to GPIO line 10..29
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun
269*4882a593SmuzhiyunVerbose example:
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun	qe_pio_e: gpio-controller@1460 {
272*4882a593Smuzhiyun		#gpio-cells = <2>;
273*4882a593Smuzhiyun		compatible = "fsl,qe-pario-bank-e", "fsl,qe-pario-bank";
274*4882a593Smuzhiyun		reg = <0x1460 0x18>;
275*4882a593Smuzhiyun		gpio-controller;
276*4882a593Smuzhiyun		gpio-ranges = <&pinctrl1 0 20 10>, <&pinctrl2 10 50 20>;
277*4882a593Smuzhiyun	};
278*4882a593Smuzhiyun
279*4882a593SmuzhiyunHere, a single GPIO controller has GPIOs 0..9 routed to pin controller
280*4882a593Smuzhiyunpinctrl1's pins 20..29, and GPIOs 10..29 routed to pin controller pinctrl2's
281*4882a593Smuzhiyunpins 50..69.
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun2.3) GPIO ranges from named pin groups
285*4882a593Smuzhiyun--------------------------------------
286*4882a593Smuzhiyun
287*4882a593SmuzhiyunIt is also possible to use pin groups for gpio ranges when pin groups are the
288*4882a593Smuzhiyuneasiest and most convenient mapping.
289*4882a593Smuzhiyun
290*4882a593SmuzhiyunBoth both <pinctrl-base> and <count> must set to 0 when using named pin groups
291*4882a593Smuzhiyunnames.
292*4882a593Smuzhiyun
293*4882a593SmuzhiyunThe property gpio-ranges-group-names must contain exactly one string for each
294*4882a593Smuzhiyunrange.
295*4882a593Smuzhiyun
296*4882a593SmuzhiyunElements of gpio-ranges-group-names must contain the name of a pin group
297*4882a593Smuzhiyundefined in the respective pin controller. The number of pins/GPIO lines in the
298*4882a593Smuzhiyunrange is the number of pins in that pin group. The number of pins of that
299*4882a593Smuzhiyungroup is defined int the implementation and not in the device tree.
300*4882a593Smuzhiyun
301*4882a593SmuzhiyunIf numerical and named pin groups are mixed, the string corresponding to a
302*4882a593Smuzhiyunnumerical pin range in gpio-ranges-group-names must be empty.
303*4882a593Smuzhiyun
304*4882a593SmuzhiyunExample:
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun	gpio_pio_i: gpio-controller@14b0 {
307*4882a593Smuzhiyun		#gpio-cells = <2>;
308*4882a593Smuzhiyun		compatible = "fsl,qe-pario-bank-e", "fsl,qe-pario-bank";
309*4882a593Smuzhiyun		reg = <0x1480 0x18>;
310*4882a593Smuzhiyun		gpio-controller;
311*4882a593Smuzhiyun		gpio-ranges =			<&pinctrl1 0 20 10>,
312*4882a593Smuzhiyun						<&pinctrl2 10 0 0>,
313*4882a593Smuzhiyun						<&pinctrl1 15 0 10>,
314*4882a593Smuzhiyun						<&pinctrl2 25 0 0>;
315*4882a593Smuzhiyun		gpio-ranges-group-names =	"",
316*4882a593Smuzhiyun						"foo",
317*4882a593Smuzhiyun						"",
318*4882a593Smuzhiyun						"bar";
319*4882a593Smuzhiyun	};
320*4882a593Smuzhiyun
321*4882a593SmuzhiyunHere, three GPIO ranges are defined referring to two pin controllers.
322*4882a593Smuzhiyun
323*4882a593Smuzhiyunpinctrl1 GPIO ranges are defined using pin numbers whereas the GPIO ranges
324*4882a593Smuzhiyunin pinctrl2 are defined using the pin groups named "foo" and "bar".
325*4882a593Smuzhiyun
326*4882a593SmuzhiyunPrevious versions of this binding required all pin controller nodes that
327*4882a593Smuzhiyunwere referenced by any gpio-ranges property to contain a property named
328*4882a593Smuzhiyun#gpio-range-cells with value <3>. This requirement is now deprecated.
329*4882a593SmuzhiyunHowever, that property may still exist in older device trees for
330*4882a593Smuzhiyuncompatibility reasons, and would still be required even in new device
331*4882a593Smuzhiyuntrees that need to be compatible with older software.
332