xref: /OK3568_Linux_fs/kernel/Documentation/firmware-guide/acpi/gpio-properties.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun.. SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun
3*4882a593Smuzhiyun======================================
4*4882a593Smuzhiyun_DSD Device Properties Related to GPIO
5*4882a593Smuzhiyun======================================
6*4882a593Smuzhiyun
7*4882a593SmuzhiyunWith the release of ACPI 5.1, the _DSD configuration object finally
8*4882a593Smuzhiyunallows names to be given to GPIOs (and other things as well) returned
9*4882a593Smuzhiyunby _CRS.  Previously, we were only able to use an integer index to find
10*4882a593Smuzhiyunthe corresponding GPIO, which is pretty error prone (it depends on
11*4882a593Smuzhiyunthe _CRS output ordering, for example).
12*4882a593Smuzhiyun
13*4882a593SmuzhiyunWith _DSD we can now query GPIOs using a name instead of an integer
14*4882a593Smuzhiyunindex, like the ASL example below shows::
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun  // Bluetooth device with reset and shutdown GPIOs
17*4882a593Smuzhiyun  Device (BTH)
18*4882a593Smuzhiyun  {
19*4882a593Smuzhiyun      Name (_HID, ...)
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun      Name (_CRS, ResourceTemplate ()
22*4882a593Smuzhiyun      {
23*4882a593Smuzhiyun          GpioIo (Exclusive, PullUp, 0, 0, IoRestrictionOutputOnly,
24*4882a593Smuzhiyun                  "\\_SB.GPO0", 0, ResourceConsumer) {15}
25*4882a593Smuzhiyun          GpioIo (Exclusive, PullUp, 0, 0, IoRestrictionOutputOnly,
26*4882a593Smuzhiyun                  "\\_SB.GPO0", 0, ResourceConsumer) {27, 31}
27*4882a593Smuzhiyun      })
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun      Name (_DSD, Package ()
30*4882a593Smuzhiyun      {
31*4882a593Smuzhiyun          ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
32*4882a593Smuzhiyun          Package ()
33*4882a593Smuzhiyun	  {
34*4882a593Smuzhiyun              Package () {"reset-gpios", Package() {^BTH, 1, 1, 0 }},
35*4882a593Smuzhiyun              Package () {"shutdown-gpios", Package() {^BTH, 0, 0, 0 }},
36*4882a593Smuzhiyun          }
37*4882a593Smuzhiyun      })
38*4882a593Smuzhiyun  }
39*4882a593Smuzhiyun
40*4882a593SmuzhiyunThe format of the supported GPIO property is::
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun  Package () { "name", Package () { ref, index, pin, active_low }}
43*4882a593Smuzhiyun
44*4882a593Smuzhiyunref
45*4882a593Smuzhiyun  The device that has _CRS containing GpioIo()/GpioInt() resources,
46*4882a593Smuzhiyun  typically this is the device itself (BTH in our case).
47*4882a593Smuzhiyunindex
48*4882a593Smuzhiyun  Index of the GpioIo()/GpioInt() resource in _CRS starting from zero.
49*4882a593Smuzhiyunpin
50*4882a593Smuzhiyun  Pin in the GpioIo()/GpioInt() resource. Typically this is zero.
51*4882a593Smuzhiyunactive_low
52*4882a593Smuzhiyun  If 1, the GPIO is marked as active_low.
53*4882a593Smuzhiyun
54*4882a593SmuzhiyunSince ACPI GpioIo() resource does not have a field saying whether it is
55*4882a593Smuzhiyunactive low or high, the "active_low" argument can be used here.  Setting
56*4882a593Smuzhiyunit to 1 marks the GPIO as active low.
57*4882a593Smuzhiyun
58*4882a593SmuzhiyunNote, active_low in _DSD does not make sense for GpioInt() resource and
59*4882a593Smuzhiyunmust be 0. GpioInt() resource has its own means of defining it.
60*4882a593Smuzhiyun
61*4882a593SmuzhiyunIn our Bluetooth example the "reset-gpios" refers to the second GpioIo()
62*4882a593Smuzhiyunresource, second pin in that resource with the GPIO number of 31.
63*4882a593Smuzhiyun
64*4882a593SmuzhiyunThe GpioIo() resource unfortunately doesn't explicitly provide an initial
65*4882a593Smuzhiyunstate of the output pin which driver should use during its initialization.
66*4882a593Smuzhiyun
67*4882a593SmuzhiyunLinux tries to use common sense here and derives the state from the bias
68*4882a593Smuzhiyunand polarity settings. The table below shows the expectations:
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun=========  =============  ==============
71*4882a593SmuzhiyunPull Bias     Polarity     Requested...
72*4882a593Smuzhiyun=========  =============  ==============
73*4882a593SmuzhiyunImplicit     x            AS IS (assumed firmware configured for us)
74*4882a593SmuzhiyunExplicit     x (no _DSD)  as Pull Bias (Up == High, Down == Low),
75*4882a593Smuzhiyun                          assuming non-active (Polarity = !Pull Bias)
76*4882a593SmuzhiyunDown         Low          as low, assuming active
77*4882a593SmuzhiyunDown         High         as low, assuming non-active
78*4882a593SmuzhiyunUp           Low          as high, assuming non-active
79*4882a593SmuzhiyunUp           High         as high, assuming active
80*4882a593Smuzhiyun=========  =============  ==============
81*4882a593Smuzhiyun
82*4882a593SmuzhiyunThat said, for our above example the both GPIOs, since the bias setting
83*4882a593Smuzhiyunis explicit and _DSD is present, will be treated as active with a high
84*4882a593Smuzhiyunpolarity and Linux will configure the pins in this state until a driver
85*4882a593Smuzhiyunreprograms them differently.
86*4882a593Smuzhiyun
87*4882a593SmuzhiyunIt is possible to leave holes in the array of GPIOs. This is useful in
88*4882a593Smuzhiyuncases like with SPI host controllers where some chip selects may be
89*4882a593Smuzhiyunimplemented as GPIOs and some as native signals. For example a SPI host
90*4882a593Smuzhiyuncontroller can have chip selects 0 and 2 implemented as GPIOs and 1 as
91*4882a593Smuzhiyunnative::
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun  Package () {
94*4882a593Smuzhiyun      "cs-gpios",
95*4882a593Smuzhiyun      Package () {
96*4882a593Smuzhiyun          ^GPIO, 19, 0, 0, // chip select 0: GPIO
97*4882a593Smuzhiyun          0,               // chip select 1: native signal
98*4882a593Smuzhiyun          ^GPIO, 20, 0, 0, // chip select 2: GPIO
99*4882a593Smuzhiyun      }
100*4882a593Smuzhiyun  }
101*4882a593Smuzhiyun
102*4882a593SmuzhiyunOther supported properties
103*4882a593Smuzhiyun==========================
104*4882a593Smuzhiyun
105*4882a593SmuzhiyunFollowing Device Tree compatible device properties are also supported by
106*4882a593Smuzhiyun_DSD device properties for GPIO controllers:
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun- gpio-hog
109*4882a593Smuzhiyun- output-high
110*4882a593Smuzhiyun- output-low
111*4882a593Smuzhiyun- input
112*4882a593Smuzhiyun- line-name
113*4882a593Smuzhiyun
114*4882a593SmuzhiyunExample::
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun  Name (_DSD, Package () {
117*4882a593Smuzhiyun      // _DSD Hierarchical Properties Extension UUID
118*4882a593Smuzhiyun      ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
119*4882a593Smuzhiyun      Package () {
120*4882a593Smuzhiyun          Package () {"hog-gpio8", "G8PU"}
121*4882a593Smuzhiyun      }
122*4882a593Smuzhiyun  })
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun  Name (G8PU, Package () {
125*4882a593Smuzhiyun      ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
126*4882a593Smuzhiyun      Package () {
127*4882a593Smuzhiyun          Package () {"gpio-hog", 1},
128*4882a593Smuzhiyun          Package () {"gpios", Package () {8, 0}},
129*4882a593Smuzhiyun          Package () {"output-high", 1},
130*4882a593Smuzhiyun          Package () {"line-name", "gpio8-pullup"},
131*4882a593Smuzhiyun      }
132*4882a593Smuzhiyun  })
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun- gpio-line-names
135*4882a593Smuzhiyun
136*4882a593SmuzhiyunExample::
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun  Package () {
139*4882a593Smuzhiyun      "gpio-line-names",
140*4882a593Smuzhiyun      Package () {
141*4882a593Smuzhiyun          "SPI0_CS_N", "EXP2_INT", "MUX6_IO", "UART0_RXD",
142*4882a593Smuzhiyun          "MUX7_IO", "LVL_C_A1", "MUX0_IO", "SPI1_MISO",
143*4882a593Smuzhiyun      }
144*4882a593Smuzhiyun  }
145*4882a593Smuzhiyun
146*4882a593SmuzhiyunSee Documentation/devicetree/bindings/gpio/gpio.txt for more information
147*4882a593Smuzhiyunabout these properties.
148*4882a593Smuzhiyun
149*4882a593SmuzhiyunACPI GPIO Mappings Provided by Drivers
150*4882a593Smuzhiyun======================================
151*4882a593Smuzhiyun
152*4882a593SmuzhiyunThere are systems in which the ACPI tables do not contain _DSD but provide _CRS
153*4882a593Smuzhiyunwith GpioIo()/GpioInt() resources and device drivers still need to work with
154*4882a593Smuzhiyunthem.
155*4882a593Smuzhiyun
156*4882a593SmuzhiyunIn those cases ACPI device identification objects, _HID, _CID, _CLS, _SUB, _HRV,
157*4882a593Smuzhiyunavailable to the driver can be used to identify the device and that is supposed
158*4882a593Smuzhiyunto be sufficient to determine the meaning and purpose of all of the GPIO lines
159*4882a593Smuzhiyunlisted by the GpioIo()/GpioInt() resources returned by _CRS.  In other words,
160*4882a593Smuzhiyunthe driver is supposed to know what to use the GpioIo()/GpioInt() resources for
161*4882a593Smuzhiyunonce it has identified the device.  Having done that, it can simply assign names
162*4882a593Smuzhiyunto the GPIO lines it is going to use and provide the GPIO subsystem with a
163*4882a593Smuzhiyunmapping between those names and the ACPI GPIO resources corresponding to them.
164*4882a593Smuzhiyun
165*4882a593SmuzhiyunTo do that, the driver needs to define a mapping table as a NULL-terminated
166*4882a593Smuzhiyunarray of struct acpi_gpio_mapping objects that each contains a name, a pointer
167*4882a593Smuzhiyunto an array of line data (struct acpi_gpio_params) objects and the size of that
168*4882a593Smuzhiyunarray.  Each struct acpi_gpio_params object consists of three fields,
169*4882a593Smuzhiyuncrs_entry_index, line_index, active_low, representing the index of the target
170*4882a593SmuzhiyunGpioIo()/GpioInt() resource in _CRS starting from zero, the index of the target
171*4882a593Smuzhiyunline in that resource starting from zero, and the active-low flag for that line,
172*4882a593Smuzhiyunrespectively, in analogy with the _DSD GPIO property format specified above.
173*4882a593Smuzhiyun
174*4882a593SmuzhiyunFor the example Bluetooth device discussed previously the data structures in
175*4882a593Smuzhiyunquestion would look like this::
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun  static const struct acpi_gpio_params reset_gpio = { 1, 1, false };
178*4882a593Smuzhiyun  static const struct acpi_gpio_params shutdown_gpio = { 0, 0, false };
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun  static const struct acpi_gpio_mapping bluetooth_acpi_gpios[] = {
181*4882a593Smuzhiyun    { "reset-gpios", &reset_gpio, 1 },
182*4882a593Smuzhiyun    { "shutdown-gpios", &shutdown_gpio, 1 },
183*4882a593Smuzhiyun    { }
184*4882a593Smuzhiyun  };
185*4882a593Smuzhiyun
186*4882a593SmuzhiyunNext, the mapping table needs to be passed as the second argument to
187*4882a593Smuzhiyunacpi_dev_add_driver_gpios() or its managed analogue that will
188*4882a593Smuzhiyunregister it with the ACPI device object pointed to by its first
189*4882a593Smuzhiyunargument. That should be done in the driver's .probe() routine.
190*4882a593SmuzhiyunOn removal, the driver should unregister its GPIO mapping table by
191*4882a593Smuzhiyuncalling acpi_dev_remove_driver_gpios() on the ACPI device object where that
192*4882a593Smuzhiyuntable was previously registered.
193*4882a593Smuzhiyun
194*4882a593SmuzhiyunUsing the _CRS fallback
195*4882a593Smuzhiyun=======================
196*4882a593Smuzhiyun
197*4882a593SmuzhiyunIf a device does not have _DSD or the driver does not create ACPI GPIO
198*4882a593Smuzhiyunmapping, the Linux GPIO framework refuses to return any GPIOs. This is
199*4882a593Smuzhiyunbecause the driver does not know what it actually gets. For example if we
200*4882a593Smuzhiyunhave a device like below::
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun  Device (BTH)
203*4882a593Smuzhiyun  {
204*4882a593Smuzhiyun      Name (_HID, ...)
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun      Name (_CRS, ResourceTemplate () {
207*4882a593Smuzhiyun          GpioIo (Exclusive, PullNone, 0, 0, IoRestrictionNone,
208*4882a593Smuzhiyun                  "\\_SB.GPO0", 0, ResourceConsumer) {15}
209*4882a593Smuzhiyun          GpioIo (Exclusive, PullNone, 0, 0, IoRestrictionNone,
210*4882a593Smuzhiyun                  "\\_SB.GPO0", 0, ResourceConsumer) {27}
211*4882a593Smuzhiyun      })
212*4882a593Smuzhiyun  }
213*4882a593Smuzhiyun
214*4882a593SmuzhiyunThe driver might expect to get the right GPIO when it does::
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun  desc = gpiod_get(dev, "reset", GPIOD_OUT_LOW);
217*4882a593Smuzhiyun
218*4882a593Smuzhiyunbut since there is no way to know the mapping between "reset" and
219*4882a593Smuzhiyunthe GpioIo() in _CRS desc will hold ERR_PTR(-ENOENT).
220*4882a593Smuzhiyun
221*4882a593SmuzhiyunThe driver author can solve this by passing the mapping explicitly
222*4882a593Smuzhiyun(this is the recommended way and it's documented in the above chapter).
223*4882a593Smuzhiyun
224*4882a593SmuzhiyunThe ACPI GPIO mapping tables should not contaminate drivers that are not
225*4882a593Smuzhiyunknowing about which exact device they are servicing on. It implies that
226*4882a593Smuzhiyunthe ACPI GPIO mapping tables are hardly linked to an ACPI ID and certain
227*4882a593Smuzhiyunobjects, as listed in the above chapter, of the device in question.
228*4882a593Smuzhiyun
229*4882a593SmuzhiyunGetting GPIO descriptor
230*4882a593Smuzhiyun=======================
231*4882a593Smuzhiyun
232*4882a593SmuzhiyunThere are two main approaches to get GPIO resource from ACPI::
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun  desc = gpiod_get(dev, connection_id, flags);
235*4882a593Smuzhiyun  desc = gpiod_get_index(dev, connection_id, index, flags);
236*4882a593Smuzhiyun
237*4882a593SmuzhiyunWe may consider two different cases here, i.e. when connection ID is
238*4882a593Smuzhiyunprovided and otherwise.
239*4882a593Smuzhiyun
240*4882a593SmuzhiyunCase 1::
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun  desc = gpiod_get(dev, "non-null-connection-id", flags);
243*4882a593Smuzhiyun  desc = gpiod_get_index(dev, "non-null-connection-id", index, flags);
244*4882a593Smuzhiyun
245*4882a593SmuzhiyunCase 2::
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun  desc = gpiod_get(dev, NULL, flags);
248*4882a593Smuzhiyun  desc = gpiod_get_index(dev, NULL, index, flags);
249*4882a593Smuzhiyun
250*4882a593SmuzhiyunCase 1 assumes that corresponding ACPI device description must have
251*4882a593Smuzhiyundefined device properties and will prevent to getting any GPIO resources
252*4882a593Smuzhiyunotherwise.
253*4882a593Smuzhiyun
254*4882a593SmuzhiyunCase 2 explicitly tells GPIO core to look for resources in _CRS.
255*4882a593Smuzhiyun
256*4882a593SmuzhiyunBe aware that gpiod_get_index() in cases 1 and 2, assuming that there
257*4882a593Smuzhiyunare two versions of ACPI device description provided and no mapping is
258*4882a593Smuzhiyunpresent in the driver, will return different resources. That's why a
259*4882a593Smuzhiyuncertain driver has to handle them carefully as explained in the previous
260*4882a593Smuzhiyunchapter.
261