aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Documentation/acpi/gpio-properties.txt52
-rw-r--r--drivers/gpio/gpiolib-acpi.c78
-rw-r--r--drivers/gpio/gpiolib.c30
-rw-r--r--drivers/gpio/gpiolib.h7
4 files changed, 146 insertions, 21 deletions
diff --git a/Documentation/acpi/gpio-properties.txt b/Documentation/acpi/gpio-properties.txt
new file mode 100644
index 000000000000..3e45b8b7e4f2
--- /dev/null
+++ b/Documentation/acpi/gpio-properties.txt
@@ -0,0 +1,52 @@
1_DSD Device Properties Related to GPIO
2--------------------------------------
3
4With the release of ACPI 5.1 and the _DSD configuration objecte names
5can 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
7the corresponding GPIO, which is pretty error prone (it depends on
8the _CRS output ordering, for example).
9
10With _DSD we can now query GPIOs using a name instead of an integer
11index, 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
37The 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
47Since ACPI GpioIo() resource does not have a field saying whether it is
48active low or high, the "active_low" argument can be used here. Setting
49it to 1 marks the GPIO as active low.
50
51In our Bluetooth example the "reset-gpio" refers to the second GpioIo()
52resource, second pin in that resource with the GPIO number of 31.
diff --git a/drivers/gpio/gpiolib-acpi.c b/drivers/gpio/gpiolib-acpi.c
index 05c6275da224..8aa6ca473748 100644
--- a/drivers/gpio/gpiolib-acpi.c
+++ b/drivers/gpio/gpiolib-acpi.c
@@ -290,6 +290,7 @@ void acpi_gpiochip_free_interrupts(struct gpio_chip *chip)
290struct acpi_gpio_lookup { 290struct acpi_gpio_lookup {
291 struct acpi_gpio_info info; 291 struct acpi_gpio_info info;
292 int index; 292 int index;
293 int pin_index;
293 struct gpio_desc *desc; 294 struct gpio_desc *desc;
294 int n; 295 int n;
295}; 296};
@@ -303,13 +304,24 @@ static int acpi_find_gpio(struct acpi_resource *ares, void *data)
303 304
304 if (lookup->n++ == lookup->index && !lookup->desc) { 305 if (lookup->n++ == lookup->index && !lookup->desc) {
305 const struct acpi_resource_gpio *agpio = &ares->data.gpio; 306 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;
306 311
307 lookup->desc = acpi_get_gpiod(agpio->resource_source.string_ptr, 312 lookup->desc = acpi_get_gpiod(agpio->resource_source.string_ptr,
308 agpio->pin_table[0]); 313 agpio->pin_table[pin_index]);
309 lookup->info.gpioint = 314 lookup->info.gpioint =
310 agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT; 315 agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT;
311 lookup->info.active_low = 316
312 agpio->polarity == ACPI_ACTIVE_LOW; 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;
313 } 325 }
314 326
315 return 1; 327 return 1;
@@ -317,40 +329,75 @@ static int acpi_find_gpio(struct acpi_resource *ares, void *data)
317 329
318/** 330/**
319 * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources 331 * 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)
321 * @index: index of GpioIo/GpioInt resource (starting from %0) 334 * @index: index of GpioIo/GpioInt resource (starting from %0)
322 * @info: info pointer to fill in (optional) 335 * @info: info pointer to fill in (optional)
323 * 336 *
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
325 * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor, 338 * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor,
326 * and returns it. @index matches GpioIo/GpioInt resources only so if there 339 * and returns it. @index matches GpioIo/GpioInt resources only so if there
327 * are total %3 GPIO resources, the index goes from %0 to %2. 340 * are total %3 GPIO resources, the index goes from %0 to %2.
328 * 341 *
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 *
329 * If the GPIO cannot be translated or there is an error an ERR_PTR is 346 * If the GPIO cannot be translated or there is an error an ERR_PTR is
330 * returned. 347 * returned.
331 * 348 *
332 * Note: if the GPIO resource has multiple entries in the pin list, this 349 * Note: if the GPIO resource has multiple entries in the pin list, this
333 * function only returns the first. 350 * function only returns the first.
334 */ 351 */
335struct gpio_desc *acpi_get_gpiod_by_index(struct device *dev, int index, 352struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev,
353 const char *propname, int index,
336 struct acpi_gpio_info *info) 354 struct acpi_gpio_info *info)
337{ 355{
338 struct acpi_gpio_lookup lookup; 356 struct acpi_gpio_lookup lookup;
339 struct list_head resource_list; 357 struct list_head resource_list;
340 struct acpi_device *adev; 358 bool active_low = false;
341 acpi_handle handle;
342 int ret; 359 int ret;
343 360
344 if (!dev) 361 if (!adev)
345 return ERR_PTR(-EINVAL);
346
347 handle = ACPI_HANDLE(dev);
348 if (!handle || acpi_bus_get_device(handle, &adev))
349 return ERR_PTR(-ENODEV); 362 return ERR_PTR(-ENODEV);
350 363
351 memset(&lookup, 0, sizeof(lookup)); 364 memset(&lookup, 0, sizeof(lookup));
352 lookup.index = index; 365 lookup.index = index;
353 366
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
354 INIT_LIST_HEAD(&resource_list); 401 INIT_LIST_HEAD(&resource_list);
355 ret = acpi_dev_get_resources(adev, &resource_list, acpi_find_gpio, 402 ret = acpi_dev_get_resources(adev, &resource_list, acpi_find_gpio,
356 &lookup); 403 &lookup);
@@ -359,8 +406,11 @@ struct gpio_desc *acpi_get_gpiod_by_index(struct device *dev, int index,
359 406
360 acpi_dev_free_resource_list(&resource_list); 407 acpi_dev_free_resource_list(&resource_list);
361 408
362 if (lookup.desc && info) 409 if (lookup.desc && info) {
363 *info = lookup.info; 410 *info = lookup.info;
411 if (active_low)
412 info->active_low = active_low;
413 }
364 414
365 return lookup.desc ? lookup.desc : ERR_PTR(-ENOENT); 415 return lookup.desc ? lookup.desc : ERR_PTR(-ENOENT);
366} 416}
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index e8e98ca25ec7..2bca0495cb46 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -1505,14 +1505,36 @@ static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
1505 unsigned int idx, 1505 unsigned int idx,
1506 enum gpio_lookup_flags *flags) 1506 enum gpio_lookup_flags *flags)
1507{ 1507{
1508 static const char * const suffixes[] = { "gpios", "gpio" };
1509 struct acpi_device *adev = ACPI_COMPANION(dev);
1508 struct acpi_gpio_info info; 1510 struct acpi_gpio_info info;
1509 struct gpio_desc *desc; 1511 struct gpio_desc *desc;
1512 char propname[32];
1513 int i;
1510 1514
1511 desc = acpi_get_gpiod_by_index(dev, idx, &info); 1515 /* Try first from _DSD */
1512 if (IS_ERR(desc)) 1516 for (i = 0; i < ARRAY_SIZE(suffixes); i++) {
1513 return desc; 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 }
1514 1536
1515 if (info.gpioint && info.active_low) 1537 if (info.active_low)
1516 *flags |= GPIO_ACTIVE_LOW; 1538 *flags |= GPIO_ACTIVE_LOW;
1517 1539
1518 return desc; 1540 return desc;
diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h
index 9db2b6a71c5d..e3a52113a541 100644
--- a/drivers/gpio/gpiolib.h
+++ b/drivers/gpio/gpiolib.h
@@ -34,7 +34,8 @@ void acpi_gpiochip_remove(struct gpio_chip *chip);
34void acpi_gpiochip_request_interrupts(struct gpio_chip *chip); 34void acpi_gpiochip_request_interrupts(struct gpio_chip *chip);
35void acpi_gpiochip_free_interrupts(struct gpio_chip *chip); 35void acpi_gpiochip_free_interrupts(struct gpio_chip *chip);
36 36
37struct gpio_desc *acpi_get_gpiod_by_index(struct device *dev, int index, 37struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev,
38 const char *propname, int index,
38 struct acpi_gpio_info *info); 39 struct acpi_gpio_info *info);
39#else 40#else
40static inline void acpi_gpiochip_add(struct gpio_chip *chip) { } 41static inline void acpi_gpiochip_add(struct gpio_chip *chip) { }
@@ -47,8 +48,8 @@ static inline void
47acpi_gpiochip_free_interrupts(struct gpio_chip *chip) { } 48acpi_gpiochip_free_interrupts(struct gpio_chip *chip) { }
48 49
49static inline struct gpio_desc * 50static inline struct gpio_desc *
50acpi_get_gpiod_by_index(struct device *dev, int index, 51acpi_get_gpiod_by_index(struct acpi_device *adev, const char *propname,
51 struct acpi_gpio_info *info) 52 int index, struct acpi_gpio_info *info)
52{ 53{
53 return ERR_PTR(-ENOSYS); 54 return ERR_PTR(-ENOSYS);
54} 55}