Skip to contentSkip to conversation

i2c: mux: gpio: Replace custom acpi_get_local_address()

PATCH Applied Andy Shevchenko 6 participants 15 messages Nov 15, 2021 – Nov 23, 2021

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

PATCH 1/3 i2c: mux: gpio: Replace custom acpi_get_local_address()

Recently ACPI gained the acpi_get_local_address() API which may be used instead of home grown i2c_mux_gpio_get_acpi_adr().

---
 drivers/i2c/muxes/i2c-mux-gpio.c | 43 ++------------------------------
 1 file changed, 2 insertions(+), 41 deletions(-)
drivers/i2c/muxes/i2c-mux-gpio.c +2 -41
index bac415a52b78..31e6eb1591bb 100644
@@ -49,45 +49,6 @@ static int i2c_mux_gpio_deselect(struct i2c_mux_core *muxc, u32 chan)
 	return 0;
 }
 
-#ifdef CONFIG_ACPI
-
-static int i2c_mux_gpio_get_acpi_adr(struct device *dev,
-				     struct fwnode_handle *fwdev,
-				     unsigned int *adr)
-
-{
-	unsigned long long adr64;
-	acpi_status status;
-
-	status = acpi_evaluate_integer(ACPI_HANDLE_FWNODE(fwdev),
-				       METHOD_NAME__ADR,
-				       NULL, &adr64);
-
-	if (!ACPI_SUCCESS(status)) {
-		dev_err(dev, "Cannot get address\n");
-		return -EINVAL;
-	}
-
-	*adr = adr64;
-	if (*adr != adr64) {
-		dev_err(dev, "Address out of range\n");
-		return -ERANGE;
-	}
-
-	return 0;
-}
-
-#else
-
-static int i2c_mux_gpio_get_acpi_adr(struct device *dev,
-				     struct fwnode_handle *fwdev,
-				     unsigned int *adr)
-{
-	return -EINVAL;
-}
-
-#endif
-
 static int i2c_mux_gpio_probe_fw(struct gpiomux *mux,
 				 struct platform_device *pdev)
 {
@@ -141,9 +102,9 @@ static int i2c_mux_gpio_probe_fw(struct gpiomux *mux,
 			fwnode_property_read_u32(child, "reg", values + i);
 
 		} else if (is_acpi_node(child)) {
-			rc = i2c_mux_gpio_get_acpi_adr(dev, child, values + i);
+			rc = acpi_get_local_address(ACPI_HANDLE_FWNODE(child), values + i);
 			if (rc)
-				return rc;
+				return dev_err_probe(dev, rc, "Cannot get address\n");
 		}
 
 		i++;
Signature · 1 line
2.33.0
#5 lore raw

PATCH 3/3 i2c: mux: gpio: Use array_size() helper

Use array_size() helper to aid in 2-factor allocation instances.

---
 drivers/i2c/muxes/i2c-mux-gpio.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/i2c/muxes/i2c-mux-gpio.c +2 -1
index b09c10f36ddb..73a23e117ebe 100644
@@ -7,6 +7,7 @@
 
 #include <linux/i2c.h>
 #include <linux/i2c-mux.h>
+#include <linux/overflow.h>
 #include <linux/platform_data/i2c-mux-gpio.h>
 #include <linux/platform_device.h>
 #include <linux/module.h>
@@ -152,7 +153,7 @@ static int i2c_mux_gpio_probe(struct platform_device *pdev)
 		return -EPROBE_DEFER;
 
 	muxc = i2c_mux_alloc(parent, &pdev->dev, mux->data.n_values,
-			     ngpios * sizeof(*mux->gpios), 0,
+			     array_size(ngpios, sizeof(*mux->gpios)), 0,
 			     i2c_mux_gpio_select, NULL);
 	if (!muxc) {
 		ret = -ENOMEM;
Signature · 1 line
2.33.0
#1 lore raw

PATCH 2/3 i2c: mux: gpio: Don't dereference fwnode from struct device

In reply to Andy Shevchenko

We have a special helper to get fwnode out of struct device. Moreover, dereferencing it directly prevents the fwnode modifications in the future.

---
 drivers/i2c/muxes/i2c-mux-gpio.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/i2c/muxes/i2c-mux-gpio.c +4 -3
index 31e6eb1591bb..b09c10f36ddb 100644
@@ -53,6 +53,7 @@ static int i2c_mux_gpio_probe_fw(struct gpiomux *mux,
 				 struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
+	struct fwnode_handle *fwnode = dev_fwnode(dev);
 	struct device_node *np = dev->of_node;
 	struct device_node *adapter_np;
 	struct i2c_adapter *adapter = NULL;
@@ -60,7 +61,7 @@ static int i2c_mux_gpio_probe_fw(struct gpiomux *mux,
 	unsigned *values;
 	int rc, i = 0;
 
-	if (is_of_node(dev->fwnode)) {
+	if (is_of_node(fwnode)) {
 		if (!np)
 			return -ENODEV;
 
@@ -72,7 +73,7 @@ static int i2c_mux_gpio_probe_fw(struct gpiomux *mux,
 		adapter = of_find_i2c_adapter_by_node(adapter_np);
 		of_node_put(adapter_np);
 
-	} else if (is_acpi_node(dev->fwnode)) {
+	} else if (is_acpi_node(fwnode)) {
 		/*
 		 * In ACPI land the mux should be a direct child of the i2c
 		 * bus it muxes.
@@ -111,7 +112,7 @@ static int i2c_mux_gpio_probe_fw(struct gpiomux *mux,
 	}
 	mux->data.values = values;
 
-	if (fwnode_property_read_u32(dev->fwnode, "idle-state", &mux->data.idle))
+	if (device_property_read_u32(dev, "idle-state", &mux->data.idle))
 		mux->data.idle = I2C_MUX_GPIO_NO_IDLE;
 
 	return 0;
Signature · 1 line
2.33.0
#10 lore raw
In reply to Andy Shevchenko

Hi!

Quoted 30 lines · Andy Shevchenko

Recently ACPI gained the acpi_get_local_address() API which may be used instead of home grown i2c_mux_gpio_get_acpi_adr().

---
 drivers/i2c/muxes/i2c-mux-gpio.c | 43 ++------------------------------
 1 file changed, 2 insertions(+), 41 deletions(-)
drivers/i2c/muxes/i2c-mux-gpio.c +0 -14
index bac415a52b78..31e6eb1591bb 100644
@@ -49,45 +49,6 @@ static int i2c_mux_gpio_deselect(struct i2c_mux_core *muxc, u32 chan)
 	return 0;
 }
 
-#ifdef CONFIG_ACPI
-
-static int i2c_mux_gpio_get_acpi_adr(struct device *dev,
-				     struct fwnode_handle *fwdev,
-				     unsigned int *adr)
-
-{
-	unsigned long long adr64;
-	acpi_status status;
-
-	status = acpi_evaluate_integer(ACPI_HANDLE_FWNODE(fwdev),
-				       METHOD_NAME__ADR,
-				       NULL, &adr64);
-

drivers/i2c/muxes/i2c-mux-gpio.c static int i2c_mux_gpio_deselect(struct i2c_mux_core *muxc, u32 chan) · line 75

Quoted 10 lines
drivers/i2c/muxes/i2c-mux-gpio.c +0 -10
-	if (!ACPI_SUCCESS(status)) {
-		dev_err(dev, "Cannot get address\n");
-		return -EINVAL;
-	}
-
-	*adr = adr64;
-	if (*adr != adr64) {
-		dev_err(dev, "Address out of range\n");
-		return -ERANGE;
-	}

In the conversion, I read it as if we lose this overflow check. Why is that not a problem?

Cheers,
Peter

Quoted 31 lines
drivers/i2c/muxes/i2c-mux-gpio.c +2 -17
-
-	return 0;
-}
-
-#else
-
-static int i2c_mux_gpio_get_acpi_adr(struct device *dev,
-				     struct fwnode_handle *fwdev,
-				     unsigned int *adr)
-{
-	return -EINVAL;
-}
-
-#endif
-
 static int i2c_mux_gpio_probe_fw(struct gpiomux *mux,
 				 struct platform_device *pdev)
 {
@@ -141,9 +102,9 @@ static int i2c_mux_gpio_probe_fw(struct gpiomux *mux,
 			fwnode_property_read_u32(child, "reg", values + i);
 
 		} else if (is_acpi_node(child)) {
-			rc = i2c_mux_gpio_get_acpi_adr(dev, child, values + i);
+			rc = acpi_get_local_address(ACPI_HANDLE_FWNODE(child), values + i);
 			if (rc)
-				return rc;
+				return dev_err_probe(dev, rc, "Cannot get address\n");
 		}
 
 		i++;
Acked-by on v1 2/3
#3 lore raw

Re: [PATCH v1 2/3] i2c: mux: gpio: Don't dereference fwnode from struct device

In reply to Andy Shevchenko

Hi!

Quoted 14 lines · Andy Shevchenko

We have a special helper to get fwnode out of struct device. Moreover, dereferencing it directly prevents the fwnode modifications in the future.

---
 drivers/i2c/muxes/i2c-mux-gpio.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/i2c/muxes/i2c-mux-gpio.c +0 -0
index 31e6eb1591bb..b09c10f36ddb 100644

drivers/i2c/muxes/i2c-mux-gpio.c static int i2c_mux_gpio_probe_fw(struct gpiomux *mux, · line 57

Quoted 5 lines
drivers/i2c/muxes/i2c-mux-gpio.c +1 -0
@@ -53,6 +53,7 @@ static int i2c_mux_gpio_probe_fw(struct gpiomux *mux,
 				 struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
+	struct fwnode_handle *fwnode = dev_fwnode(dev);
 	struct device_node *np = dev->of_node;

It feels like there is opportunity to get rid of np, but I suppose that can be done later...

Cheers,
Peter

Quoted 30 lines
drivers/i2c/muxes/i2c-mux-gpio.c +3 -3
 	struct device_node *adapter_np;
 	struct i2c_adapter *adapter = NULL;
@@ -60,7 +61,7 @@ static int i2c_mux_gpio_probe_fw(struct gpiomux *mux,
 	unsigned *values;
 	int rc, i = 0;
 
-	if (is_of_node(dev->fwnode)) {
+	if (is_of_node(fwnode)) {
 		if (!np)
 			return -ENODEV;
 
@@ -72,7 +73,7 @@ static int i2c_mux_gpio_probe_fw(struct gpiomux *mux,
 		adapter = of_find_i2c_adapter_by_node(adapter_np);
 		of_node_put(adapter_np);
 
-	} else if (is_acpi_node(dev->fwnode)) {
+	} else if (is_acpi_node(fwnode)) {
 		/*
 		 * In ACPI land the mux should be a direct child of the i2c
 		 * bus it muxes.
@@ -111,7 +112,7 @@ static int i2c_mux_gpio_probe_fw(struct gpiomux *mux,
 	}
 	mux->data.values = values;
 
-	if (fwnode_property_read_u32(dev->fwnode, "idle-state", &mux->data.idle))
+	if (device_property_read_u32(dev, "idle-state", &mux->data.idle))
 		mux->data.idle = I2C_MUX_GPIO_NO_IDLE;
 
 	return 0;
#11 lore raw
In reply to Peter Rosin

+Cc: Rafael

Quoted 1 line · Peter Rosin

On 2021-11-15 16:41, Andy Shevchenko wrote:

...

Quoted 7 lines

drivers/i2c/muxes/i2c-mux-gpio.c static int i2c_mux_gpio_deselect(struct i2c_mux_core *muxc, u32 chan) · line 75

Quoted 5 lines
drivers/i2c/muxes/i2c-mux-gpio.c +0 -5
-     *adr = adr64;
-     if (*adr != adr64) {
-             dev_err(dev, "Address out of range\n");
-             return -ERANGE;
-     }

In the conversion, I read it as if we lose this overflow check.

It depends from which angle you look at this. We relaxed requirements.

Quoted 2 lines

Why is that
not a problem?

The idea behind the acpi_get_local_address() is to provide a unified way between DT and ACPI for the same value. In either case we take only a 32-bit value. We might nevertheless add that check to the API. Rafael, what do you think?

P.S. Just realized that in ACPI the higher part of the address may be used as flags by some interfaces (SoundWire is one of them), this is not applicable to I²C muxes right now, but who knows... So I prefer a relaxed version and, if necessary, documentation should be amended/updated.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
Acked-by
#12 lore raw
Quoted 25 lines · Andy Shevchenko

+Cc: Rafael

Quoted 1 line · Peter Rosin

On 2021-11-15 16:41, Andy Shevchenko wrote:

...

Quoted 7 lines

drivers/i2c/muxes/i2c-mux-gpio.c static int i2c_mux_gpio_deselect(struct i2c_mux_core *muxc, u32 chan) · line 75

Quoted 5 lines
drivers/i2c/muxes/i2c-mux-gpio.c +0 -5
-     *adr = adr64;
-     if (*adr != adr64) {
-             dev_err(dev, "Address out of range\n");
-             return -ERANGE;
-     }

In the conversion, I read it as if we lose this overflow check.

It depends from which angle you look at this. We relaxed requirements.

Quoted 2 lines

Why is that
not a problem?

The idea behind the acpi_get_local_address() is to provide a unified way between DT and ACPI for the same value. In either case we take only a 32-bit value. We might nevertheless add that check to the API. Rafael, what do you think?

Quoted 5 lines

P.S. Just realized that in ACPI the higher part of the address may be used as flags by some interfaces (SoundWire is one of them), this is not applicable to I²C muxes right now, but who knows... So I prefer a relaxed version and, if necessary, documentation should be amended/updated.

Splendid, just checking that you're on top of things. I don't think any doc update is needed on the i2c-mux end, until flags in the upper bits are introduced? So, looks good to me, thanks!

@Wolfram: You're finding this series in patchwork and will be picking it up as usual, right? Thanks!

Cheers,
Peter

#13 lore raw
Quoted 2 lines

@Wolfram: You're finding this series in patchwork and will be picking it up as usual, right? Thanks!

Right, will do so now.