Skip to contentSkip to conversation

gpiolib: acpi: Refactor to shrink the code by ~8%

PATCH Superseded Andy Shevchenko 3 participants 11 messages Apr 2, 2025 – Apr 3, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover gpiolib: acpi: Refactor to shrink the code by ~8%

A simple refactoring of the GPIO ACPI library parts to get an impressive ~8% code shrink on x86_64 and ~2% on x86_32. Also reduces a C code a bit.

Andy Shevchenko (5):
gpiolib: acpi: Improve struct acpi_gpio_info memory footprint
gpiolib: acpi: Remove index parameter from acpi_gpio_property_lookup()
gpiolib: acpi: Reduce memory footprint for struct acpi_gpio_params
gpiolib: acpi: Reuse struct acpi_gpio_params in struct
acpi_gpio_lookup
gpiolib: acpi: Deduplicate some code in __acpi_find_gpio()

 drivers/gpio/gpiolib-acpi.c   | 138 +++++++++++++++++-----------------
 include/linux/gpio/consumer.h |   2 +-
 2 files changed, 68 insertions(+), 72 deletions(-)
Signature · 1 line
2.47.2
#1 lore raw

PATCH 1/5 gpiolib: acpi: Improve struct acpi_gpio_info memory footprint

The struct acpi_gpio_info has two boolean members that are located not close to each other making two gaps due to alignment requirements. Group them to improve memory footprint.

pahole difference before and after (on 32-bit):

- /* size: 36, cachelines: 1, members: 9 */
- /* sum members: 30, holes: 2, sum holes: 6 */

+ /* size: 32, cachelines: 1, members: 9 */
+ /* sum members: 30, holes: 1, sum holes: 2 */

---
 drivers/gpio/gpiolib-acpi.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpio/gpiolib-acpi.c +2 -2
index 69caa35c58df..878b11c81c7b 100644
@@ -96,10 +96,10 @@ struct acpi_gpio_chip {
  * @adev: reference to ACPI device which consumes GPIO resource
  * @flags: GPIO initialization flags
  * @gpioint: if %true this GPIO is of type GpioInt otherwise type is GpioIo
+ * @wake_capable: wake capability as provided by ACPI
  * @pin_config: pin bias as provided by ACPI
  * @polarity: interrupt polarity as provided by ACPI
  * @triggering: triggering type as provided by ACPI
- * @wake_capable: wake capability as provided by ACPI
  * @debounce: debounce timeout as provided by ACPI
  * @quirks: Linux specific quirks as provided by struct acpi_gpio_mapping
  */
@@ -107,10 +107,10 @@ struct acpi_gpio_info {
 	struct acpi_device *adev;
 	enum gpiod_flags flags;
 	bool gpioint;
+	bool wake_capable;
 	int pin_config;
 	int polarity;
 	int triggering;
-	bool wake_capable;
 	unsigned int debounce;
 	unsigned int quirks;
 };
Signature · 1 line
2.47.2
#2 lore raw

PATCH 2/5 gpiolib: acpi: Remove index parameter from acpi_gpio_property_lookup()

In reply to Andy Shevchenko

In all cases the supplied index is the same as the passed one in the struct acpi_gpio_lookup. Remove index parameter and reuse one from the struct acpi_gpio_lookup instead.

---
 drivers/gpio/gpiolib-acpi.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
drivers/gpio/gpiolib-acpi.c +6 -7
index 878b11c81c7b..afeb8d1c7102 100644
@@ -830,17 +830,17 @@ static int acpi_gpio_resource_lookup(struct acpi_gpio_lookup *lookup,
 	return 0;
 }
 
-static int acpi_gpio_property_lookup(struct fwnode_handle *fwnode,
-				     const char *propname, int index,
+static int acpi_gpio_property_lookup(struct fwnode_handle *fwnode, const char *propname,
 				     struct acpi_gpio_lookup *lookup)
 {
 	struct fwnode_reference_args args;
+	unsigned int index = lookup->index;
 	unsigned int quirks = 0;
 	int ret;
 
 	memset(&args, 0, sizeof(args));
-	ret = __acpi_node_get_property_reference(fwnode, propname, index, 3,
-						 &args);
+
+	ret = __acpi_node_get_property_reference(fwnode, propname, index, 3, &args);
 	if (ret) {
 		struct acpi_device *adev;
 
@@ -905,8 +905,7 @@ static struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev,
 	if (propname) {
 		dev_dbg(&adev->dev, "GPIO: looking up %s\n", propname);
 
-		ret = acpi_gpio_property_lookup(acpi_fwnode_handle(adev),
-						propname, index, &lookup);
+		ret = acpi_gpio_property_lookup(acpi_fwnode_handle(adev), propname, &lookup);
 		if (ret)
 			return ERR_PTR(ret);
 
@@ -955,7 +954,7 @@ static struct gpio_desc *acpi_get_gpiod_from_data(struct fwnode_handle *fwnode,
 	memset(&lookup, 0, sizeof(lookup));
 	lookup.index = index;
 
-	ret = acpi_gpio_property_lookup(fwnode, propname, index, &lookup);
+	ret = acpi_gpio_property_lookup(fwnode, propname, &lookup);
 	if (ret)
 		return ERR_PTR(ret);
 
Signature · 1 line
2.47.2
#3 lore raw

PATCH 3/5 gpiolib: acpi: Reduce memory footprint for struct acpi_gpio_params

In reply to Andy Shevchenko

The line_index member in the struct acpi_gpio_params replicates what is covered in the ACPI GpioIo() or GpioInt() resource. The value there is limited to 16-bit one, so we don't really need to have a full 32-bit storage for it. Together with followed boolean the structure will be smaller.

pahole difference before and after:

- /* size: 12, cachelines: 1, members: 3 */
- /* padding: 3 */

+ /* size: 8, cachelines: 1, members: 3 */
+ /* padding: 1 */

---
 include/linux/gpio/consumer.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/gpio/consumer.h +1 -1
index 45b651c05b9c..899179972bec 100644
@@ -587,7 +587,7 @@ struct gpio_desc *devm_fwnode_gpiod_get(struct device *dev,
 
 struct acpi_gpio_params {
 	unsigned int crs_entry_index;
-	unsigned int line_index;
+	unsigned short line_index;
 	bool active_low;
 };
 
Signature · 1 line
2.47.2
#4 lore raw

PATCH 4/5 gpiolib: acpi: Reuse struct acpi_gpio_params in struct acpi_gpio_lookup

In reply to Andy Shevchenko

Some of the contents of struct acpi_gpio_lookup repeats what we have in the struct acpi_gpio_params. Reuse the latter in the former.

---
 drivers/gpio/gpiolib-acpi.c | 34 ++++++++++++++++++----------------
 1 file changed, 18 insertions(+), 16 deletions(-)
drivers/gpio/gpiolib-acpi.c +18 -16
index afeb8d1c7102..750724601106 100644
@@ -744,9 +744,7 @@ static int acpi_gpio_update_gpiod_lookup_flags(unsigned long *lookupflags,
 
 struct acpi_gpio_lookup {
 	struct acpi_gpio_info info;
-	int index;
-	u16 pin_index;
-	bool active_low;
+	struct acpi_gpio_params par;
 	struct gpio_desc *desc;
 	int n;
 };
@@ -754,6 +752,7 @@ struct acpi_gpio_lookup {
 static int acpi_populate_gpio_lookup(struct acpi_resource *ares, void *data)
 {
 	struct acpi_gpio_lookup *lookup = data;
+	struct acpi_gpio_params *par = &lookup->par;
 
 	if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
 		return 1;
@@ -765,12 +764,12 @@ static int acpi_populate_gpio_lookup(struct acpi_resource *ares, void *data)
 		u16 pin_index;
 
 		if (lookup->info.quirks & ACPI_GPIO_QUIRK_ONLY_GPIOIO && gpioint)
-			lookup->index++;
+			par->crs_entry_index++;
 
-		if (lookup->n++ != lookup->index)
+		if (lookup->n++ != par->crs_entry_index)
 			return 1;
 
-		pin_index = lookup->pin_index;
+		pin_index = par->line_index;
 		if (pin_index >= agpio->pin_table_length)
 			return 1;
 
@@ -796,7 +795,7 @@ static int acpi_populate_gpio_lookup(struct acpi_resource *ares, void *data)
 			lookup->info.polarity = agpio->polarity;
 			lookup->info.triggering = agpio->triggering;
 		} else {
-			lookup->info.polarity = lookup->active_low;
+			lookup->info.polarity = par->active_low;
 		}
 
 		lookup->info.flags = acpi_gpio_to_gpiod_flags(agpio, lookup->info.polarity);
@@ -834,7 +833,8 @@ static int acpi_gpio_property_lookup(struct fwnode_handle *fwnode, const char *p
 				     struct acpi_gpio_lookup *lookup)
 {
 	struct fwnode_reference_args args;
-	unsigned int index = lookup->index;
+	struct acpi_gpio_params *par = &lookup->par;
+	unsigned int index = par->crs_entry_index;
 	unsigned int quirks = 0;
 	int ret;
 
@@ -857,9 +857,9 @@ static int acpi_gpio_property_lookup(struct fwnode_handle *fwnode, const char *p
 	if (args.nargs != 3)
 		return -EPROTO;
 
-	lookup->index = args.args[0];
-	lookup->pin_index = args.args[1];
-	lookup->active_low = !!args.args[2];
+	par->crs_entry_index = args.args[0];
+	par->line_index = args.args[1];
+	par->active_low = !!args.args[2];
 
 	lookup->info.adev = to_acpi_device_node(args.fwnode);
 	lookup->info.quirks = quirks;
@@ -897,10 +897,11 @@ static struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev,
 						 struct acpi_gpio_info *info)
 {
 	struct acpi_gpio_lookup lookup;
+	struct acpi_gpio_params *par = &lookup.par;
 	int ret;
 
 	memset(&lookup, 0, sizeof(lookup));
-	lookup.index = index;
+	par->crs_entry_index = index;
 
 	if (propname) {
 		dev_dbg(&adev->dev, "GPIO: looking up %s\n", propname);
@@ -909,9 +910,9 @@ static struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev,
 		if (ret)
 			return ERR_PTR(ret);
 
-		dev_dbg(&adev->dev, "GPIO: _DSD returned %s %d %u %u\n",
-			dev_name(&lookup.info.adev->dev), lookup.index,
-			lookup.pin_index, lookup.active_low);
+		dev_dbg(&adev->dev, "GPIO: _DSD returned %s %u %u %u\n",
+			dev_name(&lookup.info.adev->dev),
+			par->crs_entry_index, par->line_index, par->active_low);
 	} else {
 		dev_dbg(&adev->dev, "GPIO: looking up %d in _CRS\n", index);
 		lookup.info.adev = adev;
@@ -943,6 +944,7 @@ static struct gpio_desc *acpi_get_gpiod_from_data(struct fwnode_handle *fwnode,
 						  struct acpi_gpio_info *info)
 {
 	struct acpi_gpio_lookup lookup;
+	struct acpi_gpio_params *par = &lookup.par;
 	int ret;
 
 	if (!is_acpi_data_node(fwnode))
@@ -952,7 +954,7 @@ static struct gpio_desc *acpi_get_gpiod_from_data(struct fwnode_handle *fwnode,
 		return ERR_PTR(-EINVAL);
 
 	memset(&lookup, 0, sizeof(lookup));
-	lookup.index = index;
+	par->crs_entry_index = index;
 
 	ret = acpi_gpio_property_lookup(fwnode, propname, &lookup);
 	if (ret)
Signature · 1 line
2.47.2
#5 lore raw

Re: [PATCH v1 4/5] gpiolib: acpi: Reuse struct acpi_gpio_params in struct acpi_gpio_lookup

Quoted 13 lines · Andy Shevchenko

Some of the contents of struct acpi_gpio_lookup repeats what we have in the struct acpi_gpio_params. Reuse the latter in the former.

---
 drivers/gpio/gpiolib-acpi.c | 34 ++++++++++++++++++----------------
 1 file changed, 18 insertions(+), 16 deletions(-)
drivers/gpio/gpiolib-acpi.c +0 -0
index afeb8d1c7102..750724601106 100644

drivers/gpio/gpiolib-acpi.c static int acpi_gpio_update_gpiod_lookup_flags(unsigned long *lookupflags, · line 747

Quoted 7 lines
drivers/gpio/gpiolib-acpi.c +1 -3
@@ -744,9 +744,7 @@ static int acpi_gpio_update_gpiod_lookup_flags(unsigned long *lookupflags,
 
 struct acpi_gpio_lookup {
 	struct acpi_gpio_info info;
-	int index;
-	u16 pin_index;
-	bool active_low;
+	struct acpi_gpio_params par;

params is better name

drivers/gpio/gpiolib-acpi.c struct acpi_gpio_lookup { · line 755

Quoted 8 lines
drivers/gpio/gpiolib-acpi.c +1 -0
 	struct gpio_desc *desc;
 	int n;
 };
@@ -754,6 +752,7 @@ struct acpi_gpio_lookup {
 static int acpi_populate_gpio_lookup(struct acpi_resource *ares, void *data)
 {
 	struct acpi_gpio_lookup *lookup = data;
+	struct acpi_gpio_params *par = &lookup->par;

These are not changed I guess so can this be const?

Ditto everywhere.

Quoted 96 lines
drivers/gpio/gpiolib-acpi.c +16 -13
 	if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
 		return 1;
@@ -765,12 +764,12 @@ static int acpi_populate_gpio_lookup(struct acpi_resource *ares, void *data)
 		u16 pin_index;
 
 		if (lookup->info.quirks & ACPI_GPIO_QUIRK_ONLY_GPIOIO && gpioint)
-			lookup->index++;
+			par->crs_entry_index++;
 
-		if (lookup->n++ != lookup->index)
+		if (lookup->n++ != par->crs_entry_index)
 			return 1;
 
-		pin_index = lookup->pin_index;
+		pin_index = par->line_index;
 		if (pin_index >= agpio->pin_table_length)
 			return 1;
 
@@ -796,7 +795,7 @@ static int acpi_populate_gpio_lookup(struct acpi_resource *ares, void *data)
 			lookup->info.polarity = agpio->polarity;
 			lookup->info.triggering = agpio->triggering;
 		} else {
-			lookup->info.polarity = lookup->active_low;
+			lookup->info.polarity = par->active_low;
 		}
 
 		lookup->info.flags = acpi_gpio_to_gpiod_flags(agpio, lookup->info.polarity);
@@ -834,7 +833,8 @@ static int acpi_gpio_property_lookup(struct fwnode_handle *fwnode, const char *p
 				     struct acpi_gpio_lookup *lookup)
 {
 	struct fwnode_reference_args args;
-	unsigned int index = lookup->index;
+	struct acpi_gpio_params *par = &lookup->par;
+	unsigned int index = par->crs_entry_index;
 	unsigned int quirks = 0;
 	int ret;
 
@@ -857,9 +857,9 @@ static int acpi_gpio_property_lookup(struct fwnode_handle *fwnode, const char *p
 	if (args.nargs != 3)
 		return -EPROTO;
 
-	lookup->index = args.args[0];
-	lookup->pin_index = args.args[1];
-	lookup->active_low = !!args.args[2];
+	par->crs_entry_index = args.args[0];
+	par->line_index = args.args[1];
+	par->active_low = !!args.args[2];
 
 	lookup->info.adev = to_acpi_device_node(args.fwnode);
 	lookup->info.quirks = quirks;
@@ -897,10 +897,11 @@ static struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev,
 						 struct acpi_gpio_info *info)
 {
 	struct acpi_gpio_lookup lookup;
+	struct acpi_gpio_params *par = &lookup.par;
 	int ret;
 
 	memset(&lookup, 0, sizeof(lookup));
-	lookup.index = index;
+	par->crs_entry_index = index;
 
 	if (propname) {
 		dev_dbg(&adev->dev, "GPIO: looking up %s\n", propname);
@@ -909,9 +910,9 @@ static struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev,
 		if (ret)
 			return ERR_PTR(ret);
 
-		dev_dbg(&adev->dev, "GPIO: _DSD returned %s %d %u %u\n",
-			dev_name(&lookup.info.adev->dev), lookup.index,
-			lookup.pin_index, lookup.active_low);
+		dev_dbg(&adev->dev, "GPIO: _DSD returned %s %u %u %u\n",
+			dev_name(&lookup.info.adev->dev),
+			par->crs_entry_index, par->line_index, par->active_low);
 	} else {
 		dev_dbg(&adev->dev, "GPIO: looking up %d in _CRS\n", index);
 		lookup.info.adev = adev;
@@ -943,6 +944,7 @@ static struct gpio_desc *acpi_get_gpiod_from_data(struct fwnode_handle *fwnode,
 						  struct acpi_gpio_info *info)
 {
 	struct acpi_gpio_lookup lookup;
+	struct acpi_gpio_params *par = &lookup.par;
 	int ret;
 
 	if (!is_acpi_data_node(fwnode))
@@ -952,7 +954,7 @@ static struct gpio_desc *acpi_get_gpiod_from_data(struct fwnode_handle *fwnode,
 		return ERR_PTR(-EINVAL);
 
 	memset(&lookup, 0, sizeof(lookup));
-	lookup.index = index;
+	par->crs_entry_index = index;
 
 	ret = acpi_gpio_property_lookup(fwnode, propname, &lookup);
 	if (ret)
Signature · 1 line
2.47.2
#6 lore raw

Re: [PATCH v1 4/5] gpiolib: acpi: Reuse struct acpi_gpio_params in struct acpi_gpio_lookup

Quoted 7 lines · Mika Westerberg

drivers/gpio/gpiolib-acpi.c static int acpi_gpio_update_gpiod_lookup_flags(unsigned long *lookupflags, · line 747

Quoted 4 lines · Andy Shevchenko

Some of the contents of struct acpi_gpio_lookup repeats what we have in the struct acpi_gpio_params. Reuse the latter in the former.

drivers/gpio/gpiolib-acpi.c +1 -0
+	struct acpi_gpio_params par;

params is better name

It's been already used elsewhere in the code. Do you want renaming there as well for consistency's sake?

...

Quoted 4 lines

drivers/gpio/gpiolib-acpi.c struct acpi_gpio_lookup { · line 755

Quoted 2 lines
drivers/gpio/gpiolib-acpi.c +1 -0
 	struct acpi_gpio_lookup *lookup = data;
+	struct acpi_gpio_params *par = &lookup->par;

These are not changed I guess so can this be const?

They are, see below. So the answer, it can't.

But I will double check and add const where it makes sense.

...

Quoted 3 lines

drivers/gpio/gpiolib-acpi.c static int acpi_populate_gpio_lookup(struct acpi_resource *ares, void *data) · line 767

Quoted 3 lines
drivers/gpio/gpiolib-acpi.c +1 -1
 		if (lookup->info.quirks & ACPI_GPIO_QUIRK_ONLY_GPIOIO && gpioint)
-			lookup->index++;
+			par->crs_entry_index++;

E.g., ^^^

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw

Re: [PATCH v1 4/5] gpiolib: acpi: Reuse struct acpi_gpio_params in struct acpi_gpio_lookup

Quoted 6 lines · Andy Shevchenko
Quoted 3 lines · Mika Westerberg
Quoted 2 lines · Andy Shevchenko

Some of the contents of struct acpi_gpio_lookup repeats what we have in the struct acpi_gpio_params. Reuse the latter in the former.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

+ struct acpi_gpio_params par;

params is better name

It's been already used elsewhere in the code. Do you want renaming there as well for consistency's sake?

+1 for using param or params here and elsewhere. It's much better than par.

Bart

#8 lore raw

Re: [PATCH v1 4/5] gpiolib: acpi: Reuse struct acpi_gpio_params in struct acpi_gpio_lookup

Quoted 7 lines · Bartosz Golaszewski
Quoted 5 lines · Andy Shevchenko
Quoted 3 lines · Mika Westerberg
Quoted 2 lines · Andy Shevchenko

Some of the contents of struct acpi_gpio_lookup repeats what we have in the struct acpi_gpio_params. Reuse the latter in the former.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

+ struct acpi_gpio_params par;

params is better name

It's been already used elsewhere in the code. Do you want renaming there as well for consistency's sake?

+1 for using param or params here and elsewhere. It's much better than par.

Okay, will be an add-on in the next version. But I will wait for Mika and others to review the rest and give tags and/or comments where it applies.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#9 lore raw

Re: [PATCH v1 4/5] gpiolib: acpi: Reuse struct acpi_gpio_params in struct acpi_gpio_lookup

In reply to Andy Shevchenko
Quoted 11 lines · Andy Shevchenko
Quoted 7 lines · Mika Westerberg

drivers/gpio/gpiolib-acpi.c static int acpi_gpio_update_gpiod_lookup_flags(unsigned long *lookupflags, · line 747

Quoted 4 lines · Andy Shevchenko

Some of the contents of struct acpi_gpio_lookup repeats what we have in the struct acpi_gpio_params. Reuse the latter in the former.

drivers/gpio/gpiolib-acpi.c +1 -0
+	struct acpi_gpio_params par;

params is better name

It's been already used elsewhere in the code. Do you want renaming there as well for consistency's sake?

If there is no problem confusing these then no need to rename anything else.

Quoted 9 lines

...

Quoted 4 lines

drivers/gpio/gpiolib-acpi.c struct acpi_gpio_lookup { · line 755

Quoted 2 lines
drivers/gpio/gpiolib-acpi.c +1 -0
 	struct acpi_gpio_lookup *lookup = data;
+	struct acpi_gpio_params *par = &lookup->par;

These are not changed I guess so can this be const?

They are, see below. So the answer, it can't.

Okay then nevermind.

#10 lore raw

PATCH 5/5 gpiolib: acpi: Deduplicate some code in __acpi_find_gpio()

In reply to Andy Shevchenko

__acpi_find_gpio() calls two functions depending on the supplied con_id and possibility to fallback to _CRS lookup. Those functions have the same pieces of code that can be done only in one place. Do it so.

This gives an impressive shrink of the generated code for x86_64:

add/remove: 0/2 grow/shrink: 0/4 up/down: 0/-1204 (-1204)
Function old new delta
acpi_find_gpio.__UNIQUE_ID_ddebug478 56 - -56
acpi_dev_gpio_irq_wake_get_by.__UNIQUE_ID_ddebug480 56 - -56

acpi_find_gpio                               354     216    -138
acpi_get_gpiod_by_index                      456     307    -149
__acpi_find_gpio                             877     638    -239
acpi_dev_gpio_irq_wake_get_by                695     129    -566

Total: Before=15358, After=14154, chg -7.84%

---
 drivers/gpio/gpiolib-acpi.c | 103 +++++++++++++++++-------------------
 1 file changed, 49 insertions(+), 54 deletions(-)
drivers/gpio/gpiolib-acpi.c +49 -54
index 750724601106..5c6ef419f875 100644
@@ -804,8 +804,7 @@ static int acpi_populate_gpio_lookup(struct acpi_resource *ares, void *data)
 	return 1;
 }
 
-static int acpi_gpio_resource_lookup(struct acpi_gpio_lookup *lookup,
-				     struct acpi_gpio_info *info)
+static int acpi_gpio_resource_lookup(struct acpi_gpio_lookup *lookup)
 {
 	struct acpi_device *adev = lookup->info.adev;
 	struct list_head res_list;
@@ -824,8 +823,6 @@ static int acpi_gpio_resource_lookup(struct acpi_gpio_lookup *lookup,
 	if (!lookup->desc)
 		return -ENOENT;
 
-	if (info)
-		*info = lookup->info;
 	return 0;
 }
 
@@ -871,97 +868,83 @@ static int acpi_gpio_property_lookup(struct fwnode_handle *fwnode, const char *p
  * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources
  * @adev: pointer to a ACPI device to get GPIO from
  * @propname: Property name of the GPIO (optional)
- * @index: index of GpioIo/GpioInt resource (starting from %0)
- * @info: info pointer to fill in (optional)
+ * @lookup: pointer to struct acpi_gpio_lookup to fill in
  *
- * Function goes through ACPI resources for @adev and based on @index looks
+ * Function goes through ACPI resources for @adev and based on @lookup.index looks
  * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor,
- * and returns it. @index matches GpioIo/GpioInt resources only so if there
- * are total %3 GPIO resources, the index goes from %0 to %2.
+ * and returns it. @lookup.index matches GpioIo/GpioInt resources only so if there
+ * are total 3 GPIO resources, the index goes from 0 to 2.
  *
  * If @propname is specified the GPIO is looked using device property. In
  * that case @index is used to select the GPIO entry in the property value
  * (in case of multiple).
  *
  * Returns:
- * GPIO descriptor to use with Linux generic GPIO API.
- * If the GPIO cannot be translated or there is an error an ERR_PTR is
- * returned.
+ * 0 on success, negative errno on failure.
+ *
+ * The @lookup is filled with GPIO descriptor to use with Linux generic GPIO API.
+ * If the GPIO cannot be translated an error will be returned.
  *
  * Note: if the GPIO resource has multiple entries in the pin list, this
  * function only returns the first.
  */
-static struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev,
-						 const char *propname,
-						 int index,
-						 struct acpi_gpio_info *info)
+static int acpi_get_gpiod_by_index(struct acpi_device *adev, const char *propname,
+				   struct acpi_gpio_lookup *lookup)
 {
-	struct acpi_gpio_lookup lookup;
-	struct acpi_gpio_params *par = &lookup.par;
+	struct acpi_gpio_info *info = &lookup->info;
+	struct acpi_gpio_params *par = &lookup->par;
 	int ret;
 
-	memset(&lookup, 0, sizeof(lookup));
-	par->crs_entry_index = index;
-
 	if (propname) {
 		dev_dbg(&adev->dev, "GPIO: looking up %s\n", propname);
 
-		ret = acpi_gpio_property_lookup(acpi_fwnode_handle(adev), propname, &lookup);
+		ret = acpi_gpio_property_lookup(acpi_fwnode_handle(adev), propname, lookup);
 		if (ret)
-			return ERR_PTR(ret);
+			return ret;
 
 		dev_dbg(&adev->dev, "GPIO: _DSD returned %s %u %u %u\n",
-			dev_name(&lookup.info.adev->dev),
+			dev_name(&info->adev->dev),
 			par->crs_entry_index, par->line_index, par->active_low);
 	} else {
-		dev_dbg(&adev->dev, "GPIO: looking up %d in _CRS\n", index);
-		lookup.info.adev = adev;
+		dev_dbg(&adev->dev, "GPIO: looking up %u in _CRS\n", par->crs_entry_index);
+		info->adev = adev;
 	}
 
-	ret = acpi_gpio_resource_lookup(&lookup, info);
-	return ret ? ERR_PTR(ret) : lookup.desc;
+	return acpi_gpio_resource_lookup(lookup);
 }
 
 /**
  * acpi_get_gpiod_from_data() - get a GPIO descriptor from ACPI data node
  * @fwnode: pointer to an ACPI firmware node to get the GPIO information from
  * @propname: Property name of the GPIO
- * @index: index of GpioIo/GpioInt resource (starting from %0)
- * @info: info pointer to fill in (optional)
+ * @lookup: pointer to struct acpi_gpio_lookup to fill in
  *
  * This function uses the property-based GPIO lookup to get to the GPIO
  * resource with the relevant information from a data-only ACPI firmware node
  * and uses that to obtain the GPIO descriptor to return.
  *
  * Returns:
- * GPIO descriptor to use with Linux generic GPIO API.
- * If the GPIO cannot be translated or there is an error an ERR_PTR is
- * returned.
+ * 0 on success, negative errno on failure.
+ *
+ * The @lookup is filled with GPIO descriptor to use with Linux generic GPIO API.
+ * If the GPIO cannot be translated an error will be returned.
  */
-static struct gpio_desc *acpi_get_gpiod_from_data(struct fwnode_handle *fwnode,
-						  const char *propname,
-						  int index,
-						  struct acpi_gpio_info *info)
+static int acpi_get_gpiod_from_data(struct fwnode_handle *fwnode, const char *propname,
+				    struct acpi_gpio_lookup *lookup)
 {
-	struct acpi_gpio_lookup lookup;
-	struct acpi_gpio_params *par = &lookup.par;
 	int ret;
 
 	if (!is_acpi_data_node(fwnode))
-		return ERR_PTR(-ENODEV);
+		return -ENODEV;
 
 	if (!propname)
-		return ERR_PTR(-EINVAL);
+		return -EINVAL;
 
-	memset(&lookup, 0, sizeof(lookup));
-	par->crs_entry_index = index;
-
-	ret = acpi_gpio_property_lookup(fwnode, propname, &lookup);
+	ret = acpi_gpio_property_lookup(fwnode, propname, lookup);
 	if (ret)
-		return ERR_PTR(ret);
+		return ret;
 
-	ret = acpi_gpio_resource_lookup(&lookup, info);
-	return ret ? ERR_PTR(ret) : lookup.desc;
+	return acpi_gpio_resource_lookup(lookup);
 }
 
 static bool acpi_can_fallback_to_crs(struct acpi_device *adev,
@@ -983,17 +966,24 @@ __acpi_find_gpio(struct fwnode_handle *fwnode, const char *con_id, unsigned int
 		 bool can_fallback, struct acpi_gpio_info *info)
 {
 	struct acpi_device *adev = to_acpi_device_node(fwnode);
+	struct acpi_gpio_lookup lookup;
 	struct gpio_desc *desc;
 	char propname[32];
+	int ret;
+
+	memset(&lookup, 0, sizeof(lookup));
+	lookup.par.crs_entry_index = idx;
 
 	/* Try first from _DSD */
 	for_each_gpio_property_name(propname, con_id) {
 		if (adev)
-			desc = acpi_get_gpiod_by_index(adev,
-						       propname, idx, info);
+			ret = acpi_get_gpiod_by_index(adev, propname, &lookup);
 		else
-			desc = acpi_get_gpiod_from_data(fwnode,
-							propname, idx, info);
+			ret = acpi_get_gpiod_from_data(fwnode, propname, &lookup);
+		if (ret)
+			continue;
+
+		desc = lookup.desc;
 		if (PTR_ERR(desc) == -EPROBE_DEFER)
 			return desc;
 
@@ -1002,8 +992,13 @@ __acpi_find_gpio(struct fwnode_handle *fwnode, const char *con_id, unsigned int
 	}
 
 	/* Then from plain _CRS GPIOs */
-	if (can_fallback)
-		return acpi_get_gpiod_by_index(adev, NULL, idx, info);
+	if (can_fallback) {
+		ret = acpi_get_gpiod_by_index(adev, NULL, &lookup);
+		if (ret)
+			return ERR_PTR(ret);
+
+		return lookup.desc;
+	}
 
 	return ERR_PTR(-ENOENT);
 }
Signature · 1 line
2.47.2