hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
kernel/drivers/gpio/gpiolib-acpi.c
....@@ -1,18 +1,14 @@
1
+// SPDX-License-Identifier: GPL-2.0
12 /*
23 * ACPI helpers for GPIO API
34 *
45 * Copyright (C) 2012, Intel Corporation
56 * Authors: Mathias Nyman <mathias.nyman@linux.intel.com>
67 * Mika Westerberg <mika.westerberg@linux.intel.com>
7
- *
8
- * This program is free software; you can redistribute it and/or modify
9
- * it under the terms of the GNU General Public License version 2 as
10
- * published by the Free Software Foundation.
118 */
129
1310 #include <linux/dmi.h>
1411 #include <linux/errno.h>
15
-#include <linux/gpio.h>
1612 #include <linux/gpio/consumer.h>
1713 #include <linux/gpio/driver.h>
1814 #include <linux/gpio/machine.h>
....@@ -23,6 +19,7 @@
2319 #include <linux/pinctrl/pinctrl.h>
2420
2521 #include "gpiolib.h"
22
+#include "gpiolib-acpi.h"
2623
2724 static int run_edge_events_on_boot = -1;
2825 module_param(run_edge_events_on_boot, int, 0444);
....@@ -45,13 +42,13 @@
4542 *
4643 * @node: list-entry of the events list of the struct acpi_gpio_chip
4744 * @handle: handle of ACPI method to execute when the IRQ triggers
48
- * @handler: irq_handler to pass to request_irq when requesting the IRQ
49
- * @pin: GPIO pin number on the gpio_chip
50
- * @irq: Linux IRQ number for the event, for request_ / free_irq
51
- * @irqflags: flags to pass to request_irq when requesting the IRQ
45
+ * @handler: handler function to pass to request_irq() when requesting the IRQ
46
+ * @pin: GPIO pin number on the struct gpio_chip
47
+ * @irq: Linux IRQ number for the event, for request_irq() / free_irq()
48
+ * @irqflags: flags to pass to request_irq() when requesting the IRQ
5249 * @irq_is_wake: If the ACPI flags indicate the IRQ is a wakeup source
53
- * @is_requested: True if request_irq has been done
54
- * @desc: gpio_desc for the GPIO pin for this event
50
+ * @irq_requested:True if request_irq() has been done
51
+ * @desc: struct gpio_desc for the GPIO pin for this event
5552 */
5653 struct acpi_gpio_event {
5754 struct list_head node;
....@@ -86,10 +83,10 @@
8683 };
8784
8885 /*
89
- * For gpiochips which call acpi_gpiochip_request_interrupts() before late_init
86
+ * For GPIO chips which call acpi_gpiochip_request_interrupts() before late_init
9087 * (so builtin drivers) we register the ACPI GpioInt IRQ handlers from a
91
- * late_initcall_sync handler, so that other builtin drivers can register their
92
- * OpRegions before the event handlers can run. This list contains gpiochips
88
+ * late_initcall_sync() handler, so that other builtin drivers can register their
89
+ * OpRegions before the event handlers can run. This list contains GPIO chips
9390 * for which the acpi_gpiochip_request_irqs() call has been deferred.
9491 */
9592 static DEFINE_MUTEX(acpi_gpio_deferred_req_irqs_lock);
....@@ -111,7 +108,7 @@
111108 *
112109 * Return: GPIO descriptor to use with Linux generic GPIO API, or ERR_PTR
113110 * error value. Specifically returns %-EPROBE_DEFER if the referenced GPIO
114
- * controller does not have gpiochip registered at the moment. This is to
111
+ * controller does not have GPIO chip registered at the moment. This is to
115112 * support probe deferral.
116113 */
117114 static struct gpio_desc *acpi_get_gpiod(char *path, int pin)
....@@ -259,6 +256,7 @@
259256 return true;
260257 }
261258
259
+/* Always returns AE_OK so that we keep looping over the resources */
262260 static acpi_status acpi_gpiochip_alloc_event(struct acpi_resource *ares,
263261 void *context)
264262 {
....@@ -278,8 +276,8 @@
278276 pin = agpio->pin_table[0];
279277
280278 if (pin <= 255) {
281
- char ev_name[5];
282
- sprintf(ev_name, "_%c%02hhX",
279
+ char ev_name[8];
280
+ sprintf(ev_name, "_%c%02X",
283281 agpio->triggering == ACPI_EDGE_SENSITIVE ? 'E' : 'L',
284282 pin);
285283 if (ACPI_SUCCESS(acpi_get_handle(handle, ev_name, &evt_handle)))
....@@ -292,23 +290,28 @@
292290 if (!handler)
293291 return AE_OK;
294292
295
- desc = gpiochip_request_own_desc(chip, pin, "ACPI:Event");
293
+ desc = gpiochip_request_own_desc(chip, pin, "ACPI:Event",
294
+ GPIO_ACTIVE_HIGH, GPIOD_IN);
296295 if (IS_ERR(desc)) {
297
- dev_err(chip->parent, "Failed to request GPIO\n");
298
- return AE_ERROR;
296
+ dev_err(chip->parent,
297
+ "Failed to request GPIO for pin 0x%04X, err %ld\n",
298
+ pin, PTR_ERR(desc));
299
+ return AE_OK;
299300 }
300
-
301
- gpiod_direction_input(desc);
302301
303302 ret = gpiochip_lock_as_irq(chip, pin);
304303 if (ret) {
305
- dev_err(chip->parent, "Failed to lock GPIO as interrupt\n");
304
+ dev_err(chip->parent,
305
+ "Failed to lock GPIO pin 0x%04X as interrupt, err %d\n",
306
+ pin, ret);
306307 goto fail_free_desc;
307308 }
308309
309310 irq = gpiod_to_irq(desc);
310311 if (irq < 0) {
311
- dev_err(chip->parent, "Failed to translate GPIO to IRQ\n");
312
+ dev_err(chip->parent,
313
+ "Failed to translate GPIO pin 0x%04X to IRQ, err %d\n",
314
+ pin, irq);
312315 goto fail_unlock_irq;
313316 }
314317
....@@ -353,7 +356,7 @@
353356 fail_free_desc:
354357 gpiochip_free_own_desc(desc);
355358
356
- return AE_ERROR;
359
+ return AE_OK;
357360 }
358361
359362 /**
....@@ -362,9 +365,9 @@
362365 *
363366 * ACPI5 platforms can use GPIO signaled ACPI events. These GPIO interrupts are
364367 * handled by ACPI event methods which need to be called from the GPIO
365
- * chip's interrupt handler. acpi_gpiochip_request_interrupts finds out which
366
- * gpio pins have acpi event methods and assigns interrupt handlers that calls
367
- * the acpi event methods for those pins.
368
+ * chip's interrupt handler. acpi_gpiochip_request_interrupts() finds out which
369
+ * GPIO pins have ACPI event methods and assigns interrupt handlers that calls
370
+ * the ACPI event methods for those pins.
368371 */
369372 void acpi_gpiochip_request_interrupts(struct gpio_chip *chip)
370373 {
....@@ -432,8 +435,6 @@
432435 mutex_unlock(&acpi_gpio_deferred_req_irqs_lock);
433436
434437 list_for_each_entry_safe_reverse(event, ep, &acpi_gpio->events, node) {
435
- struct gpio_desc *desc;
436
-
437438 if (event->irq_requested) {
438439 if (event->irq_is_wake)
439440 disable_irq_wake(event->irq);
....@@ -441,11 +442,8 @@
441442 free_irq(event->irq, event);
442443 }
443444
444
- desc = event->desc;
445
- if (WARN_ON(IS_ERR(desc)))
446
- continue;
447445 gpiochip_unlock_as_irq(chip, event->pin);
448
- gpiochip_free_own_desc(desc);
446
+ gpiochip_free_own_desc(event->desc);
449447 list_del(&event->node);
450448 kfree(event);
451449 }
....@@ -462,6 +460,13 @@
462460 return -EINVAL;
463461 }
464462 EXPORT_SYMBOL_GPL(acpi_dev_add_driver_gpios);
463
+
464
+void acpi_dev_remove_driver_gpios(struct acpi_device *adev)
465
+{
466
+ if (adev)
467
+ adev->driver_gpios = NULL;
468
+}
469
+EXPORT_SYMBOL_GPL(acpi_dev_remove_driver_gpios);
465470
466471 static void devm_acpi_dev_release_driver_gpios(struct device *dev, void *res)
467472 {
....@@ -524,8 +529,6 @@
524529 static enum gpiod_flags
525530 acpi_gpio_to_gpiod_flags(const struct acpi_resource_gpio *agpio)
526531 {
527
- bool pull_up = agpio->pin_config == ACPI_PIN_CONFIG_PULLUP;
528
-
529532 switch (agpio->io_restriction) {
530533 case ACPI_IO_RESTRICT_INPUT:
531534 return GPIOD_IN;
....@@ -534,21 +537,34 @@
534537 * ACPI GPIO resources don't contain an initial value for the
535538 * GPIO. Therefore we deduce that value from the pull field
536539 * instead. If the pin is pulled up we assume default to be
537
- * high, otherwise low.
540
+ * high, if it is pulled down we assume default to be low,
541
+ * otherwise we leave pin untouched.
538542 */
539
- return pull_up ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
543
+ switch (agpio->pin_config) {
544
+ case ACPI_PIN_CONFIG_PULLUP:
545
+ return GPIOD_OUT_HIGH;
546
+ case ACPI_PIN_CONFIG_PULLDOWN:
547
+ return GPIOD_OUT_LOW;
548
+ default:
549
+ break;
550
+ }
540551 default:
541
- /*
542
- * Assume that the BIOS has configured the direction and pull
543
- * accordingly.
544
- */
545
- return GPIOD_ASIS;
552
+ break;
546553 }
554
+
555
+ /*
556
+ * Assume that the BIOS has configured the direction and pull
557
+ * accordingly.
558
+ */
559
+ return GPIOD_ASIS;
547560 }
548561
549562 static int
550563 __acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, enum gpiod_flags update)
551564 {
565
+ const enum gpiod_flags mask =
566
+ GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT |
567
+ GPIOD_FLAGS_BIT_DIR_VAL;
552568 int ret = 0;
553569
554570 /*
....@@ -569,7 +585,7 @@
569585 if (((*flags & GPIOD_FLAGS_BIT_DIR_SET) && (diff & GPIOD_FLAGS_BIT_DIR_OUT)) ||
570586 ((*flags & GPIOD_FLAGS_BIT_DIR_OUT) && (diff & GPIOD_FLAGS_BIT_DIR_VAL)))
571587 ret = -EINVAL;
572
- *flags = update;
588
+ *flags = (*flags & ~mask) | (update & mask);
573589 }
574590 return ret;
575591 }
....@@ -594,6 +610,26 @@
594610 return ret;
595611 }
596612
613
+int acpi_gpio_update_gpiod_lookup_flags(unsigned long *lookupflags,
614
+ struct acpi_gpio_info *info)
615
+{
616
+ switch (info->pin_config) {
617
+ case ACPI_PIN_CONFIG_PULLUP:
618
+ *lookupflags |= GPIO_PULL_UP;
619
+ break;
620
+ case ACPI_PIN_CONFIG_PULLDOWN:
621
+ *lookupflags |= GPIO_PULL_DOWN;
622
+ break;
623
+ default:
624
+ break;
625
+ }
626
+
627
+ if (info->polarity == GPIO_ACTIVE_LOW)
628
+ *lookupflags |= GPIO_ACTIVE_LOW;
629
+
630
+ return 0;
631
+}
632
+
597633 struct acpi_gpio_lookup {
598634 struct acpi_gpio_info info;
599635 int index;
....@@ -610,17 +646,30 @@
610646 if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
611647 return 1;
612648
613
- if (lookup->n++ == lookup->index && !lookup->desc) {
649
+ if (!lookup->desc) {
614650 const struct acpi_resource_gpio *agpio = &ares->data.gpio;
615
- int pin_index = lookup->pin_index;
651
+ bool gpioint = agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT;
652
+ struct gpio_desc *desc;
653
+ int pin_index;
616654
655
+ if (lookup->info.quirks & ACPI_GPIO_QUIRK_ONLY_GPIOIO && gpioint)
656
+ lookup->index++;
657
+
658
+ if (lookup->n++ != lookup->index)
659
+ return 1;
660
+
661
+ pin_index = lookup->pin_index;
617662 if (pin_index >= agpio->pin_table_length)
618663 return 1;
619664
620
- lookup->desc = acpi_get_gpiod(agpio->resource_source.string_ptr,
665
+ if (lookup->info.quirks & ACPI_GPIO_QUIRK_ABSOLUTE_NUMBER)
666
+ desc = gpio_to_desc(agpio->pin_table[pin_index]);
667
+ else
668
+ desc = acpi_get_gpiod(agpio->resource_source.string_ptr,
621669 agpio->pin_table[pin_index]);
622
- lookup->info.gpioint =
623
- agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT;
670
+ lookup->desc = desc;
671
+ lookup->info.pin_config = agpio->pin_config;
672
+ lookup->info.gpioint = gpioint;
624673
625674 /*
626675 * Polarity and triggering are only specified for GpioInt
....@@ -723,7 +772,7 @@
723772 * that case @index is used to select the GPIO entry in the property value
724773 * (in case of multiple).
725774 *
726
- * If the GPIO cannot be translated or there is an error an ERR_PTR is
775
+ * If the GPIO cannot be translated or there is an error, an ERR_PTR is
727776 * returned.
728777 *
729778 * Note: if the GPIO resource has multiple entries in the pin list, this
....@@ -762,11 +811,21 @@
762811 return ret ? ERR_PTR(ret) : lookup.desc;
763812 }
764813
814
+static bool acpi_can_fallback_to_crs(struct acpi_device *adev,
815
+ const char *con_id)
816
+{
817
+ /* Never allow fallback if the device has properties */
818
+ if (acpi_dev_has_props(adev) || adev->driver_gpios)
819
+ return false;
820
+
821
+ return con_id == NULL;
822
+}
823
+
765824 struct gpio_desc *acpi_find_gpio(struct device *dev,
766825 const char *con_id,
767826 unsigned int idx,
768827 enum gpiod_flags *dflags,
769
- enum gpio_lookup_flags *lookupflags)
828
+ unsigned long *lookupflags)
770829 {
771830 struct acpi_device *adev = ACPI_COMPANION(dev);
772831 struct acpi_gpio_info info;
....@@ -807,10 +866,8 @@
807866 return ERR_PTR(-ENOENT);
808867 }
809868
810
- if (info.polarity == GPIO_ACTIVE_LOW)
811
- *lookupflags |= GPIO_ACTIVE_LOW;
812
-
813869 acpi_gpio_update_gpiod_flags(dflags, &info);
870
+ acpi_gpio_update_gpiod_lookup_flags(lookupflags, &info);
814871 return desc;
815872 }
816873
....@@ -821,10 +878,13 @@
821878 * @index: index of GpioIo/GpioInt resource (starting from %0)
822879 * @info: info pointer to fill in (optional)
823880 *
824
- * If @fwnode is an ACPI device object, call %acpi_get_gpiod_by_index() for it.
825
- * Otherwise (ie. it is a data-only non-device object), use the property-based
881
+ * If @fwnode is an ACPI device object, call acpi_get_gpiod_by_index() for it.
882
+ * Otherwise (i.e. it is a data-only non-device object), use the property-based
826883 * GPIO lookup to get to the GPIO resource with the relevant information and use
827884 * that to obtain the GPIO descriptor to return.
885
+ *
886
+ * If the GPIO cannot be translated or there is an error an ERR_PTR is
887
+ * returned.
828888 */
829889 struct gpio_desc *acpi_node_get_gpiod(struct fwnode_handle *fwnode,
830890 const char *propname, int index,
....@@ -856,8 +916,9 @@
856916 }
857917
858918 /**
859
- * acpi_dev_gpio_irq_get() - Find GpioInt and translate it to Linux IRQ number
919
+ * acpi_dev_gpio_irq_get_by() - Find GpioInt and translate it to Linux IRQ number
860920 * @adev: pointer to a ACPI device to get IRQ from
921
+ * @name: optional name of GpioInt resource
861922 * @index: index of GpioInt resource (starting from %0)
862923 *
863924 * If the device has one or more GpioInt resources, this function can be
....@@ -867,9 +928,12 @@
867928 * The function is idempotent, though each time it runs it will configure GPIO
868929 * pin direction according to the flags in GpioInt resource.
869930 *
931
+ * The function takes optional @name parameter. If the resource has a property
932
+ * name, then only those will be taken into account.
933
+ *
870934 * Return: Linux IRQ number (> %0) on success, negative errno on failure.
871935 */
872
-int acpi_dev_gpio_irq_get(struct acpi_device *adev, int index)
936
+int acpi_dev_gpio_irq_get_by(struct acpi_device *adev, const char *name, int index)
873937 {
874938 int idx, i;
875939 unsigned int irq_flags;
....@@ -879,13 +943,14 @@
879943 struct acpi_gpio_info info;
880944 struct gpio_desc *desc;
881945
882
- desc = acpi_get_gpiod_by_index(adev, NULL, i, &info);
946
+ desc = acpi_get_gpiod_by_index(adev, name, i, &info);
883947
884948 /* Ignore -EPROBE_DEFER, it only matters if idx matches */
885949 if (IS_ERR(desc) && PTR_ERR(desc) != -EPROBE_DEFER)
886950 return PTR_ERR(desc);
887951
888952 if (info.gpioint && idx++ == index) {
953
+ unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
889954 char label[32];
890955 int irq;
891956
....@@ -897,7 +962,7 @@
897962 return irq;
898963
899964 snprintf(label, sizeof(label), "GpioInt() %d", index);
900
- ret = gpiod_configure_flags(desc, label, 0, info.flags);
965
+ ret = gpiod_configure_flags(desc, label, lflags, info.flags);
901966 if (ret < 0)
902967 return ret;
903968
....@@ -922,7 +987,7 @@
922987 }
923988 return -ENOENT;
924989 }
925
-EXPORT_SYMBOL_GPL(acpi_dev_gpio_irq_get);
990
+EXPORT_SYMBOL_GPL(acpi_dev_gpio_irq_get_by);
926991
927992 static acpi_status
928993 acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
....@@ -979,7 +1044,7 @@
9791044 * event but only if the access here is ACPI_READ. In that
9801045 * case we "borrow" the event GPIO instead.
9811046 */
982
- if (!found && agpio->sharable == ACPI_SHARED &&
1047
+ if (!found && agpio->shareable == ACPI_SHARED &&
9831048 function == ACPI_READ) {
9841049 struct acpi_gpio_event *event;
9851050
....@@ -995,19 +1060,12 @@
9951060 if (!found) {
9961061 enum gpiod_flags flags = acpi_gpio_to_gpiod_flags(agpio);
9971062 const char *label = "ACPI:OpRegion";
998
- int err;
9991063
1000
- desc = gpiochip_request_own_desc(chip, pin, label);
1064
+ desc = gpiochip_request_own_desc(chip, pin, label,
1065
+ GPIO_ACTIVE_HIGH,
1066
+ flags);
10011067 if (IS_ERR(desc)) {
10021068 status = AE_ERROR;
1003
- mutex_unlock(&achip->conn_lock);
1004
- goto out;
1005
- }
1006
-
1007
- err = gpiod_configure_flags(desc, label, 0, flags);
1008
- if (err < 0) {
1009
- status = AE_NOT_CONFIGURED;
1010
- gpiochip_free_own_desc(desc);
10111069 mutex_unlock(&achip->conn_lock);
10121070 goto out;
10131071 }
....@@ -1077,16 +1135,19 @@
10771135 }
10781136 }
10791137
1080
-static struct gpio_desc *acpi_gpiochip_parse_own_gpio(
1081
- struct acpi_gpio_chip *achip, struct fwnode_handle *fwnode,
1082
- const char **name, unsigned int *lflags, unsigned int *dflags)
1138
+static struct gpio_desc *
1139
+acpi_gpiochip_parse_own_gpio(struct acpi_gpio_chip *achip,
1140
+ struct fwnode_handle *fwnode,
1141
+ const char **name,
1142
+ unsigned long *lflags,
1143
+ enum gpiod_flags *dflags)
10831144 {
10841145 struct gpio_chip *chip = achip->chip;
10851146 struct gpio_desc *desc;
10861147 u32 gpios[2];
10871148 int ret;
10881149
1089
- *lflags = 0;
1150
+ *lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
10901151 *dflags = 0;
10911152 *name = NULL;
10921153
....@@ -1122,7 +1183,8 @@
11221183 struct fwnode_handle *fwnode;
11231184
11241185 device_for_each_child_node(chip->parent, fwnode) {
1125
- unsigned int lflags, dflags;
1186
+ unsigned long lflags;
1187
+ enum gpiod_flags dflags;
11261188 struct gpio_desc *desc;
11271189 const char *name;
11281190 int ret;
....@@ -1175,9 +1237,6 @@
11751237 return;
11761238 }
11771239
1178
- if (!chip->names)
1179
- devprop_gpiochip_set_names(chip, dev_fwnode(chip->parent));
1180
-
11811240 acpi_gpiochip_request_regions(acpi_gpio);
11821241 acpi_gpiochip_scan_gpios(acpi_gpio);
11831242 acpi_walk_dep_device_list(handle);
....@@ -1218,7 +1277,7 @@
12181277 switch (element->type) {
12191278 case ACPI_TYPE_LOCAL_REFERENCE:
12201279 element += 3;
1221
- /* Fallthrough */
1280
+ fallthrough;
12221281 case ACPI_TYPE_INTEGER:
12231282 element++;
12241283 count++;
....@@ -1243,11 +1302,13 @@
12431302 }
12441303
12451304 /**
1246
- * acpi_gpio_count - return the number of GPIOs associated with a
1247
- * device / function or -ENOENT if no GPIO has been
1248
- * assigned to the requested function.
1249
- * @dev: GPIO consumer, can be NULL for system-global GPIOs
1305
+ * acpi_gpio_count - count the GPIOs associated with a device / function
1306
+ * @dev: GPIO consumer, can be %NULL for system-global GPIOs
12501307 * @con_id: function within the GPIO consumer
1308
+ *
1309
+ * Return:
1310
+ * The number of GPIOs associated with a device / function or %-ENOENT,
1311
+ * if no GPIO has been assigned to the requested function.
12511312 */
12521313 int acpi_gpio_count(struct device *dev, const char *con_id)
12531314 {
....@@ -1304,17 +1365,8 @@
13041365 return count ? count : -ENOENT;
13051366 }
13061367
1307
-bool acpi_can_fallback_to_crs(struct acpi_device *adev, const char *con_id)
1308
-{
1309
- /* Never allow fallback if the device has properties */
1310
- if (adev->data.properties || adev->driver_gpios)
1311
- return false;
1312
-
1313
- return con_id == NULL;
1314
-}
1315
-
13161368 /* Run deferred acpi_gpiochip_request_irqs() */
1317
-static int acpi_gpio_handle_deferred_request_irqs(void)
1369
+static int __init acpi_gpio_handle_deferred_request_irqs(void)
13181370 {
13191371 struct acpi_gpio_chip *acpi_gpio, *tmp;
13201372
....@@ -1332,7 +1384,7 @@
13321384 /* We must use _sync so that this runs after the first deferred_probe run */
13331385 late_initcall_sync(acpi_gpio_handle_deferred_request_irqs);
13341386
1335
-static const struct dmi_system_id gpiolib_acpi_quirks[] = {
1387
+static const struct dmi_system_id gpiolib_acpi_quirks[] __initconst = {
13361388 {
13371389 /*
13381390 * The Minix Neo Z83-4 has a micro-USB-B id-pin handler for
....@@ -1430,7 +1482,7 @@
14301482 {} /* Terminating entry */
14311483 };
14321484
1433
-static int acpi_gpio_setup_params(void)
1485
+static int __init acpi_gpio_setup_params(void)
14341486 {
14351487 const struct acpi_gpiolib_dmi_quirk *quirk = NULL;
14361488 const struct dmi_system_id *id;