Skip to contentSkip to conversation

mfd: lpc_ich: Simplify GPIO resource handling

PATCH In review Andy Shevchenko 2 participants 8 messages Sep 3, 2025 – Sep 4, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover mfd: lpc_ich: Simplify GPIO resource handling

Introduce a helper to iopoll.h which allows to simplify GPIO resource handling in lpc_ich driver for the starter. The helper can be used in many other cases.

Andy Shevchenko (2):
resource: Introduce resource_rebase() helper
mfd: lpc_ich: Convert to use resource_rebase()

 drivers/mfd/lpc_ich.c  | 38 ++++++++------------------------------
 include/linux/ioport.h |  6 ++++++
 2 files changed, 14 insertions(+), 30 deletions(-)
Signature · 1 line
2.50.1
#1 lore raw

PATCH 1/2 resource: Introduce resource_rebase() helper

Introduce a helper to add an offset to the resource. This is helpful in the cases when, for example) the resource has statically defined the start and end fields, but the base of it is yet to be defined, usually dynamically at run-time.

---
 include/linux/ioport.h | 6 ++++++
 1 file changed, 6 insertions(+)
include/linux/ioport.h +6 -0
index e8b2d6aa4013..159e74284d0b 100644
@@ -290,6 +290,12 @@ static inline resource_size_t resource_size(const struct resource *res)
 {
 	return res->end - res->start + 1;
 }
+
+static inline void resource_rebase(struct resource *res, resource_size_t start)
+{
+	resource_set_range(res, start + res->start, resource_size(res));
+}
+
 static inline unsigned long resource_type(const struct resource *res)
 {
 	return res->flags & IORESOURCE_TYPE_BITS;
Signature · 1 line
2.50.1
#2 lore raw

Re: [PATCH v1 1/2] resource: Introduce resource_rebase() helper

Quoted 15 lines · Andy Shevchenko

Introduce a helper to add an offset to the resource. This is helpful in the cases when, for example) the resource has statically defined the start and end fields, but the base of it is yet to be defined, usually dynamically at run-time.

---
 include/linux/ioport.h | 6 ++++++
 1 file changed, 6 insertions(+)
include/linux/ioport.h +0 -0
index e8b2d6aa4013..159e74284d0b 100644

include/linux/ioport.h static inline resource_size_t resource_size(const struct resource *res) · line 297

Quoted 8 lines
include/linux/ioport.h +5 -0
@@ -290,6 +290,12 @@ static inline resource_size_t resource_size(const struct resource *res)
 {
 	return res->end - res->start + 1;
 }
+
+static inline void resource_rebase(struct resource *res, resource_size_t start)
+{
+	resource_set_range(res, start + res->start, resource_size(res));
+}

Hi Andy,

This seems fine, it's nice to get rid of complex ->end calculations. But I wanted to mention another common case which is resetting the base to zero. Are we expected to use resource_rebase() for those cases too? I've been thinking of adding something like resource_reset().

resource_rebase(res, 0) would work for those cases but it doesn't then carry the intent of "removing" the base in its name. Opinions?

Signature · 1 line
 i.
#3 lore raw

Re: [PATCH v1 1/2] resource: Introduce resource_rebase() helper

Quoted 1 line · Ilpo Järvinen

On Wed, 3 Sep 2025, Andy Shevchenko wrote:

...

Quoted 10 lines

include/linux/ioport.h static inline resource_size_t resource_size(const struct resource *res) · line 297

Quoted 4 lines
include/linux/ioport.h +4 -0
+static inline void resource_rebase(struct resource *res, resource_size_t start)
+{
+	resource_set_range(res, start + res->start, resource_size(res));
+}

This seems fine, it's nice to get rid of complex ->end calculations. But I wanted to mention another common case which is resetting the base to zero. Are we expected to use resource_rebase() for those cases too? I've been thinking of adding something like resource_reset().

Quoted 2 lines

resource_rebase(res, 0) would work for those cases but it doesn't then carry the intent of "removing" the base in its name. Opinions?

Another case I have just realised is repeated "rebase" over the statically global resource (when driver is in but device is bind-unbind-and-repeat).

Perhaps rebase has to be idempotent. That will do exactly, if I'm not mistaken, what you are telling as a _reset() by default.

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

PATCH 2/2 mfd: lpc_ich: Convert to use resource_rebase()

In reply to Andy Shevchenko

Simplify the resource handling by converting to use resource_rebase(). No functional change intended.

---
 drivers/mfd/lpc_ich.c | 38 ++++++++------------------------------
 1 file changed, 8 insertions(+), 30 deletions(-)
drivers/mfd/lpc_ich.c +8 -30
index 4b7d0cb9340f..6942a744e87b 100644
@@ -132,15 +132,12 @@ static struct mfd_cell lpc_ich_gpio_cell = {
 	.ignore_resource_conflicts = true,
 };
 
-#define INTEL_GPIO_RESOURCE_SIZE	0x1000
-
 struct lpc_ich_gpio_info {
 	const char *hid;
 	const struct mfd_cell *devices;
 	size_t nr_devices;
 	struct resource **resources;
 	size_t nr_resources;
-	const resource_size_t *offsets;
 };
 
 #define APL_GPIO_NORTH		0
@@ -151,31 +148,23 @@ struct lpc_ich_gpio_info {
 #define APL_GPIO_NR_DEVICES	4
 #define APL_GPIO_NR_RESOURCES	4
 
-/* Offset data for Apollo Lake GPIO controllers */
-static const resource_size_t apl_gpio_offsets[APL_GPIO_NR_RESOURCES] = {
-	[APL_GPIO_NORTH]	= 0xc50000,
-	[APL_GPIO_NORTHWEST]	= 0xc40000,
-	[APL_GPIO_WEST]		= 0xc70000,
-	[APL_GPIO_SOUTHWEST]	= 0xc00000,
-};
-
 #define APL_GPIO_IRQ			14
 
 static struct resource apl_gpio_resources[APL_GPIO_NR_DEVICES][2] = {
 	[APL_GPIO_NORTH] = {
-		DEFINE_RES_MEM(0, 0),
+		DEFINE_RES_MEM(0xc50000, 0x1000),
 		DEFINE_RES_IRQ(APL_GPIO_IRQ),
 	},
 	[APL_GPIO_NORTHWEST] = {
-		DEFINE_RES_MEM(0, 0),
+		DEFINE_RES_MEM(0xc40000, 0x1000),
 		DEFINE_RES_IRQ(APL_GPIO_IRQ),
 	},
 	[APL_GPIO_WEST] = {
-		DEFINE_RES_MEM(0, 0),
+		DEFINE_RES_MEM(0xc70000, 0x1000),
 		DEFINE_RES_IRQ(APL_GPIO_IRQ),
 	},
 	[APL_GPIO_SOUTHWEST] = {
-		DEFINE_RES_MEM(0, 0),
+		DEFINE_RES_MEM(0xc00000, 0x1000),
 		DEFINE_RES_IRQ(APL_GPIO_IRQ),
 	},
 };
@@ -224,7 +213,6 @@ static const struct lpc_ich_gpio_info apl_gpio_info = {
 	.nr_devices = ARRAY_SIZE(apl_gpio_devices),
 	.resources = apl_gpio_mem_resources,
 	.nr_resources = ARRAY_SIZE(apl_gpio_mem_resources),
-	.offsets = apl_gpio_offsets,
 };
 
 #define DNV_GPIO_NORTH		0
@@ -233,17 +221,11 @@ static const struct lpc_ich_gpio_info apl_gpio_info = {
 #define DNV_GPIO_NR_DEVICES	1
 #define DNV_GPIO_NR_RESOURCES	2
 
-/* Offset data for Denverton GPIO controllers */
-static const resource_size_t dnv_gpio_offsets[DNV_GPIO_NR_RESOURCES] = {
-	[DNV_GPIO_NORTH]	= 0xc20000,
-	[DNV_GPIO_SOUTH]	= 0xc50000,
-};
-
 #define DNV_GPIO_IRQ			14
 
 static struct resource dnv_gpio_resources[DNV_GPIO_NR_RESOURCES + 1] = {
-	[DNV_GPIO_NORTH] = DEFINE_RES_MEM(0, 0),
-	[DNV_GPIO_SOUTH] = DEFINE_RES_MEM(0, 0),
+	[DNV_GPIO_NORTH] = DEFINE_RES_MEM(0xc20000, 0x1000),
+	[DNV_GPIO_SOUTH] = DEFINE_RES_MEM(0xc50000, 0x1000),
 	DEFINE_RES_IRQ(DNV_GPIO_IRQ),
 };
 
@@ -267,7 +249,6 @@ static const struct lpc_ich_gpio_info dnv_gpio_info = {
 	.nr_devices = ARRAY_SIZE(dnv_gpio_devices),
 	.resources = dnv_gpio_mem_resources,
 	.nr_resources = ARRAY_SIZE(dnv_gpio_mem_resources),
-	.offsets = dnv_gpio_offsets,
 };
 
 static struct mfd_cell lpc_ich_spi_cell = {
@@ -1251,12 +1232,9 @@ static int lpc_ich_init_pinctrl(struct pci_dev *dev)
 
 	for (i = 0; i < info->nr_resources; i++) {
 		struct resource *mem = info->resources[i];
-		resource_size_t offset = info->offsets[i];
 
-		/* Fill MEM resource */
-		mem->start = base.start + offset;
-		mem->end = base.start + offset + INTEL_GPIO_RESOURCE_SIZE - 1;
-		mem->flags = base.flags;
+		/* Rebase MEM resource */
+		resource_rebase(mem, base.start);
 	}
 
 	return mfd_add_devices(&dev->dev, 0, info->devices, info->nr_devices,
Signature · 1 line
2.50.1
#5 lore raw

Re: [PATCH v1 2/2] mfd: lpc_ich: Convert to use resource_rebase()

Quoted 2 lines · Andy Shevchenko

Simplify the resource handling by converting to use resource_rebase(). No functional change intended.

...

drivers/mfd/lpc_ich.c static int lpc_ich_init_pinctrl(struct pci_dev *dev) · line 1238

Quoted 11 lines
drivers/mfd/lpc_ich.c +2 -5
 	for (i = 0; i < info->nr_resources; i++) {
 		struct resource *mem = info->resources[i];
-		resource_size_t offset = info->offsets[i];
 
-		/* Fill MEM resource */
-		mem->start = base.start + offset;
-		mem->end = base.start + offset + INTEL_GPIO_RESOURCE_SIZE - 1;
-		mem->flags = base.flags;
+		/* Rebase MEM resource */
+		resource_rebase(mem, base.start);
 	}

We may gain 3 LoC more by dropping temporary variable.

But I will wait for the reviews in general before mocking up v2 of this.

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

Re: [PATCH v1 2/2] mfd: lpc_ich: Convert to use resource_rebase()

Quoted 10 lines · Andy Shevchenko
Quoted 2 lines · Andy Shevchenko

Simplify the resource handling by converting to use resource_rebase(). No functional change intended.

...

Quoted 4 lines
drivers/mfd/lpc_ich.c +0 -1
 	for (i = 0; i < info->nr_resources; i++) {
 		struct resource *mem = info->resources[i];
-		resource_size_t offset = info->offsets[i];
Quoted 9 lines

drivers/mfd/lpc_ich.c static int lpc_ich_init_pinctrl(struct pci_dev *dev) · line 1238

Quoted 7 lines
drivers/mfd/lpc_ich.c +2 -4
-		/* Fill MEM resource */
-		mem->start = base.start + offset;
-		mem->end = base.start + offset + INTEL_GPIO_RESOURCE_SIZE - 1;
-		mem->flags = base.flags;
+		/* Rebase MEM resource */
+		resource_rebase(mem, base.start);
 	}

We may gain 3 LoC more by dropping temporary variable.

Yeah, drop that super-obvious comment as well. :-)

Quoted 3 lines

But I will wait for the reviews in general before mocking up v2 of this.

Signature · 1 line
 i.
#7 lore raw
In reply to Andy Shevchenko
Quoted 2 lines · Andy Shevchenko

Introduce a helper to iopoll.h which allows to simplify GPIO resource handling in lpc_ich driver for the starter. The helper can be used in many other cases.

After thinking a bit, this series is no go as it will regress on module unbind-bind and I have no clear view right now how to avoid that. So self-NAK.

Signature · 2 lines
With Best Regards,
Andy Shevchenko