Skip to content

Commit 0d9a693

Browse files
westerirafaeljw
authored andcommitted
gpio / ACPI: Add support for _DSD device properties
With release of ACPI 5.1 and _DSD method we can finally name GPIOs (and other things as well) returned by _CRS. Previously we were only able to use integer index to find the corresponding GPIO, which is pretty error prone if the order changes. With _DSD we can now query GPIOs using name instead of an integer index, like the below example shows: // Bluetooth device with reset and shutdown GPIOs Device (BTH) { Name (_HID, ...) Name (_CRS, ResourceTemplate () { GpioIo (Exclusive, PullUp, 0, 0, IoRestrictionInputOnly, "\\_SB.GPO0", 0, ResourceConsumer) {15} GpioIo (Exclusive, PullUp, 0, 0, IoRestrictionInputOnly, "\\_SB.GPO0", 0, ResourceConsumer) {27, 31} }) Name (_DSD, Package () { ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"), Package () { Package () {"reset-gpio", Package() {^BTH, 1, 1, 0 }}, Package () {"shutdown-gpio", Package() {^BTH, 0, 0, 0 }}, } }) } The format of the supported GPIO property is: Package () { "name", Package () { ref, index, pin, active_low }} ref - The device that has _CRS containing GpioIo()/GpioInt() resources, typically this is the device itself (BTH in our case). index - Index of the GpioIo()/GpioInt() resource in _CRS starting from zero. pin - Pin in the GpioIo()/GpioInt() resource. Typically this is zero. active_low - If 1 the GPIO is marked as active_low. Since ACPI GpioIo() resource does not have field saying whether it is active low or high, the "active_low" argument can be used here. Setting it to 1 marks the GPIO as active low. In our Bluetooth example the "reset-gpio" refers to the second GpioIo() resource, second pin in that resource with the GPIO number of 31. This patch implements necessary support to gpiolib for extracting GPIOs using _DSD device properties. Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com> Acked-by: Linus Walleij <linus.walleij@linaro.org> Acked-by: Grant Likely <grant.likely@linaro.org> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
1 parent f60e707 commit 0d9a693

File tree

4 files changed

+146
-21
lines changed

4 files changed

+146
-21
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
_DSD Device Properties Related to GPIO
2+
--------------------------------------
3+
4+
With the release of ACPI 5.1 and the _DSD configuration objecte names
5+
can finally be given to GPIOs (and other things as well) returned by
6+
_CRS. Previously, we were only able to use an integer index to find
7+
the corresponding GPIO, which is pretty error prone (it depends on
8+
the _CRS output ordering, for example).
9+
10+
With _DSD we can now query GPIOs using a name instead of an integer
11+
index, like the ASL example below shows:
12+
13+
// Bluetooth device with reset and shutdown GPIOs
14+
Device (BTH)
15+
{
16+
Name (_HID, ...)
17+
18+
Name (_CRS, ResourceTemplate ()
19+
{
20+
GpioIo (Exclusive, PullUp, 0, 0, IoRestrictionInputOnly,
21+
"\\_SB.GPO0", 0, ResourceConsumer) {15}
22+
GpioIo (Exclusive, PullUp, 0, 0, IoRestrictionInputOnly,
23+
"\\_SB.GPO0", 0, ResourceConsumer) {27, 31}
24+
})
25+
26+
Name (_DSD, Package ()
27+
{
28+
ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
29+
Package ()
30+
{
31+
Package () {"reset-gpio", Package() {^BTH, 1, 1, 0 }},
32+
Package () {"shutdown-gpio", Package() {^BTH, 0, 0, 0 }},
33+
}
34+
})
35+
}
36+
37+
The format of the supported GPIO property is:
38+
39+
Package () { "name", Package () { ref, index, pin, active_low }}
40+
41+
ref - The device that has _CRS containing GpioIo()/GpioInt() resources,
42+
typically this is the device itself (BTH in our case).
43+
index - Index of the GpioIo()/GpioInt() resource in _CRS starting from zero.
44+
pin - Pin in the GpioIo()/GpioInt() resource. Typically this is zero.
45+
active_low - If 1 the GPIO is marked as active_low.
46+
47+
Since ACPI GpioIo() resource does not have a field saying whether it is
48+
active low or high, the "active_low" argument can be used here. Setting
49+
it to 1 marks the GPIO as active low.
50+
51+
In our Bluetooth example the "reset-gpio" refers to the second GpioIo()
52+
resource, second pin in that resource with the GPIO number of 31.

drivers/gpio/gpiolib-acpi.c

Lines changed: 64 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ void acpi_gpiochip_free_interrupts(struct gpio_chip *chip)
290290
struct acpi_gpio_lookup {
291291
struct acpi_gpio_info info;
292292
int index;
293+
int pin_index;
293294
struct gpio_desc *desc;
294295
int n;
295296
};
@@ -303,54 +304,100 @@ static int acpi_find_gpio(struct acpi_resource *ares, void *data)
303304

304305
if (lookup->n++ == lookup->index && !lookup->desc) {
305306
const struct acpi_resource_gpio *agpio = &ares->data.gpio;
307+
int pin_index = lookup->pin_index;
308+
309+
if (pin_index >= agpio->pin_table_length)
310+
return 1;
306311

307312
lookup->desc = acpi_get_gpiod(agpio->resource_source.string_ptr,
308-
agpio->pin_table[0]);
313+
agpio->pin_table[pin_index]);
309314
lookup->info.gpioint =
310315
agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT;
311-
lookup->info.active_low =
312-
agpio->polarity == ACPI_ACTIVE_LOW;
316+
317+
/*
318+
* ActiveLow is only specified for GpioInt resource. If
319+
* GpioIo is used then the only way to set the flag is
320+
* to use _DSD "gpios" property.
321+
*/
322+
if (lookup->info.gpioint)
323+
lookup->info.active_low =
324+
agpio->polarity == ACPI_ACTIVE_LOW;
313325
}
314326

315327
return 1;
316328
}
317329

318330
/**
319331
* acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources
320-
* @dev: pointer to a device to get GPIO from
332+
* @adev: pointer to a ACPI device to get GPIO from
333+
* @propname: Property name of the GPIO (optional)
321334
* @index: index of GpioIo/GpioInt resource (starting from %0)
322335
* @info: info pointer to fill in (optional)
323336
*
324-
* Function goes through ACPI resources for @dev and based on @index looks
337+
* Function goes through ACPI resources for @adev and based on @index looks
325338
* up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor,
326339
* and returns it. @index matches GpioIo/GpioInt resources only so if there
327340
* are total %3 GPIO resources, the index goes from %0 to %2.
328341
*
342+
* If @propname is specified the GPIO is looked using device property. In
343+
* that case @index is used to select the GPIO entry in the property value
344+
* (in case of multiple).
345+
*
329346
* If the GPIO cannot be translated or there is an error an ERR_PTR is
330347
* returned.
331348
*
332349
* Note: if the GPIO resource has multiple entries in the pin list, this
333350
* function only returns the first.
334351
*/
335-
struct gpio_desc *acpi_get_gpiod_by_index(struct device *dev, int index,
352+
struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev,
353+
const char *propname, int index,
336354
struct acpi_gpio_info *info)
337355
{
338356
struct acpi_gpio_lookup lookup;
339357
struct list_head resource_list;
340-
struct acpi_device *adev;
341-
acpi_handle handle;
358+
bool active_low = false;
342359
int ret;
343360

344-
if (!dev)
345-
return ERR_PTR(-EINVAL);
346-
347-
handle = ACPI_HANDLE(dev);
348-
if (!handle || acpi_bus_get_device(handle, &adev))
361+
if (!adev)
349362
return ERR_PTR(-ENODEV);
350363

351364
memset(&lookup, 0, sizeof(lookup));
352365
lookup.index = index;
353366

367+
if (propname) {
368+
struct acpi_reference_args args;
369+
370+
dev_dbg(&adev->dev, "GPIO: looking up %s\n", propname);
371+
372+
memset(&args, 0, sizeof(args));
373+
ret = acpi_dev_get_property_reference(adev, propname, NULL,
374+
index, &args);
375+
if (ret)
376+
return ERR_PTR(ret);
377+
378+
/*
379+
* The property was found and resolved so need to
380+
* lookup the GPIO based on returned args instead.
381+
*/
382+
adev = args.adev;
383+
if (args.nargs >= 2) {
384+
lookup.index = args.args[0];
385+
lookup.pin_index = args.args[1];
386+
/*
387+
* 3rd argument, if present is used to
388+
* specify active_low.
389+
*/
390+
if (args.nargs >= 3)
391+
active_low = !!args.args[2];
392+
}
393+
394+
dev_dbg(&adev->dev, "GPIO: _DSD returned %s %zd %llu %llu %llu\n",
395+
dev_name(&adev->dev), args.nargs,
396+
args.args[0], args.args[1], args.args[2]);
397+
} else {
398+
dev_dbg(&adev->dev, "GPIO: looking up %d in _CRS\n", index);
399+
}
400+
354401
INIT_LIST_HEAD(&resource_list);
355402
ret = acpi_dev_get_resources(adev, &resource_list, acpi_find_gpio,
356403
&lookup);
@@ -359,8 +406,11 @@ struct gpio_desc *acpi_get_gpiod_by_index(struct device *dev, int index,
359406

360407
acpi_dev_free_resource_list(&resource_list);
361408

362-
if (lookup.desc && info)
409+
if (lookup.desc && info) {
363410
*info = lookup.info;
411+
if (active_low)
412+
info->active_low = active_low;
413+
}
364414

365415
return lookup.desc ? lookup.desc : ERR_PTR(-ENOENT);
366416
}

drivers/gpio/gpiolib.c

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,14 +1505,36 @@ static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
15051505
unsigned int idx,
15061506
enum gpio_lookup_flags *flags)
15071507
{
1508+
static const char * const suffixes[] = { "gpios", "gpio" };
1509+
struct acpi_device *adev = ACPI_COMPANION(dev);
15081510
struct acpi_gpio_info info;
15091511
struct gpio_desc *desc;
1512+
char propname[32];
1513+
int i;
15101514

1511-
desc = acpi_get_gpiod_by_index(dev, idx, &info);
1512-
if (IS_ERR(desc))
1513-
return desc;
1515+
/* Try first from _DSD */
1516+
for (i = 0; i < ARRAY_SIZE(suffixes); i++) {
1517+
if (con_id && strcmp(con_id, "gpios")) {
1518+
snprintf(propname, sizeof(propname), "%s-%s",
1519+
con_id, suffixes[i]);
1520+
} else {
1521+
snprintf(propname, sizeof(propname), "%s",
1522+
suffixes[i]);
1523+
}
1524+
1525+
desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
1526+
if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
1527+
break;
1528+
}
1529+
1530+
/* Then from plain _CRS GPIOs */
1531+
if (IS_ERR(desc)) {
1532+
desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
1533+
if (IS_ERR(desc))
1534+
return desc;
1535+
}
15141536

1515-
if (info.gpioint && info.active_low)
1537+
if (info.active_low)
15161538
*flags |= GPIO_ACTIVE_LOW;
15171539

15181540
return desc;

drivers/gpio/gpiolib.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ void acpi_gpiochip_remove(struct gpio_chip *chip);
3434
void acpi_gpiochip_request_interrupts(struct gpio_chip *chip);
3535
void acpi_gpiochip_free_interrupts(struct gpio_chip *chip);
3636

37-
struct gpio_desc *acpi_get_gpiod_by_index(struct device *dev, int index,
37+
struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev,
38+
const char *propname, int index,
3839
struct acpi_gpio_info *info);
3940
#else
4041
static inline void acpi_gpiochip_add(struct gpio_chip *chip) { }
@@ -47,8 +48,8 @@ static inline void
4748
acpi_gpiochip_free_interrupts(struct gpio_chip *chip) { }
4849

4950
static inline struct gpio_desc *
50-
acpi_get_gpiod_by_index(struct device *dev, int index,
51-
struct acpi_gpio_info *info)
51+
acpi_get_gpiod_by_index(struct acpi_device *adev, const char *propname,
52+
int index, struct acpi_gpio_info *info)
5253
{
5354
return ERR_PTR(-ENOSYS);
5455
}

0 commit comments

Comments
 (0)