Skip to contentSkip to conversation

i2c: mpc: Use of_property_read_reg() to parse "reg"

PATCH Applied Rob Herring 4 participants 7 messages Jun 9, 2023 – Jun 21, 2023

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

PATCH 1/1 i2c: mpc: Use of_property_read_reg() to parse "reg"

Use the recently added of_property_read_reg() helper to get the untranslated "reg" address value.

---
 drivers/i2c/busses/i2c-mpc.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/i2c/busses/i2c-mpc.c +3 -2
index cfd074ee6d54..595dce9218ad 100644
@@ -316,9 +316,10 @@ static void mpc_i2c_setup_512x(struct device_node *node,
 	if (node_ctrl) {
 		ctrl = of_iomap(node_ctrl, 0);
 		if (ctrl) {
+			u64 addr;
 			/* Interrupt enable bits for i2c-0/1/2: bit 24/26/28 */
-			pval = of_get_property(node, "reg", NULL);
-			idx = (*pval & 0xff) / 0x20;
+			of_property_read_reg(node, 0, &addr, NULL);
+			idx = (addr & 0xff) / 0x20;
 			setbits32(ctrl, 1 << (24 + idx * 2));
 			iounmap(ctrl);
 		}
Signature · 1 line
2.39.2
maintainer
#1 lore raw

Hi Rob,

Quoted 13 lines · Rob Herring

Use the recently added of_property_read_reg() helper to get the untranslated "reg" address value.

---
 drivers/i2c/busses/i2c-mpc.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/i2c/busses/i2c-mpc.c +0 -0
index cfd074ee6d54..595dce9218ad 100644

drivers/i2c/busses/i2c-mpc.c static void mpc_i2c_setup_512x(struct device_node *node, · line 321

Quoted 8 lines
drivers/i2c/busses/i2c-mpc.c +2 -2
@@ -316,9 +316,10 @@ static void mpc_i2c_setup_512x(struct device_node *node,
 	if (node_ctrl) {
 		ctrl = of_iomap(node_ctrl, 0);
 		if (ctrl) {
+			u64 addr;
 			/* Interrupt enable bits for i2c-0/1/2: bit 24/26/28 */
-			pval = of_get_property(node, "reg", NULL);
-			idx = (*pval & 0xff) / 0x20;
+			of_property_read_reg(node, 0, &addr, NULL);

because of_property_read_reg() can return error, can we check also the error value here?

Thanks,
Andi

Quoted 7 lines
drivers/i2c/busses/i2c-mpc.c +1 -0
+			idx = (addr & 0xff) / 0x20;
 			setbits32(ctrl, 1 << (24 + idx * 2));
 			iounmap(ctrl);
 		}
Signature · 1 line
2.39.2
#2 lore raw
Quoted 12 lines · Andi Shyti

Hi Rob,

Quoted 8 lines · Rob Herring

Use the recently added of_property_read_reg() helper to get the untranslated "reg" address value.

---
 drivers/i2c/busses/i2c-mpc.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
Quoted 16 lines

drivers/i2c/busses/i2c-mpc.c static void mpc_i2c_setup_512x(struct device_node *node, · line 321

Quoted 13 lines
drivers/i2c/busses/i2c-mpc.c +2 -2
index cfd074ee6d54..595dce9218ad 100644
@@ -316,9 +316,10 @@ static void mpc_i2c_setup_512x(struct device_node *node,
      if (node_ctrl) {
              ctrl = of_iomap(node_ctrl, 0);
              if (ctrl) {
+                     u64 addr;
                      /* Interrupt enable bits for i2c-0/1/2: bit 24/26/28 */
-                     pval = of_get_property(node, "reg", NULL);
-                     idx = (*pval & 0xff) / 0x20;
+                     of_property_read_reg(node, 0, &addr, NULL);

because of_property_read_reg() can return error, can we check also the error value here?

Why? The old code wasn't worried about of_get_property() returning NULL on the same possible errors. If anyone is still actually using mpc512x, I don't think their DTB will have an error at this point. IOW, is improving the error handling on this really worth it?

Rob

maintainer Acked-by
#3 lore raw

Hi Rob,

Quoted 30 lines · Rob Herring
Quoted 12 lines · Andi Shyti

Hi Rob,

Quoted 8 lines · Rob Herring

Use the recently added of_property_read_reg() helper to get the untranslated "reg" address value.

---
 drivers/i2c/busses/i2c-mpc.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
Quoted 16 lines

drivers/i2c/busses/i2c-mpc.c static void mpc_i2c_setup_512x(struct device_node *node, · line 321

Quoted 13 lines
drivers/i2c/busses/i2c-mpc.c +2 -2
index cfd074ee6d54..595dce9218ad 100644
@@ -316,9 +316,10 @@ static void mpc_i2c_setup_512x(struct device_node *node,
      if (node_ctrl) {
              ctrl = of_iomap(node_ctrl, 0);
              if (ctrl) {
+                     u64 addr;
                      /* Interrupt enable bits for i2c-0/1/2: bit 24/26/28 */
-                     pval = of_get_property(node, "reg", NULL);
-                     idx = (*pval & 0xff) / 0x20;
+                     of_property_read_reg(node, 0, &addr, NULL);

because of_property_read_reg() can return error, can we check also the error value here?

Quoted 1 line

Why?

Because if a function can return an error, the error must be checked. Even if the property is "reg" and the binding says that it's required. Otherwise let's make those functions void.

Quoted 2 lines

The old code wasn't worried about of_get_property() returning NULL on the same possible errors.

Sure! Checking the error comes for free. The patch is fine as it is, mine was a little improvement I asked for. I can still ack it and add the error handling later myself :)

Quoted 3 lines

If anyone is still actually using
mpc512x, I don't think their DTB will have an error at this point. IOW, is improving the error handling on this really worth it?

In my view, every error needs to be checked as every error is unlikely to happen: it makes the code future proof and makes sure other components failure don't impact the normal functioning of this driver.

Anyway, the patch is doing what you described and for the sole economy of this change:

Thank you,
Andi

#4 lore raw
Quoted 34 lines · Andi Shyti

Hi Rob,

Quoted 30 lines · Rob Herring
Quoted 28 lines · Andi Shyti

Hi Rob,

Quoted 13 lines · Rob Herring

Use the recently added of_property_read_reg() helper to get the untranslated "reg" address value.

---
 drivers/i2c/busses/i2c-mpc.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/i2c/busses/i2c-mpc.c +0 -0
index cfd074ee6d54..595dce9218ad 100644

drivers/i2c/busses/i2c-mpc.c static void mpc_i2c_setup_512x(struct device_node *node, · line 321

Quoted 8 lines
drivers/i2c/busses/i2c-mpc.c +2 -2
@@ -316,9 +316,10 @@ static void mpc_i2c_setup_512x(struct device_node *node,
      if (node_ctrl) {
              ctrl = of_iomap(node_ctrl, 0);
              if (ctrl) {
+                     u64 addr;
                      /* Interrupt enable bits for i2c-0/1/2: bit 24/26/28 */
-                     pval = of_get_property(node, "reg", NULL);
-                     idx = (*pval & 0xff) / 0x20;
+                     of_property_read_reg(node, 0, &addr, NULL);

because of_property_read_reg() can return error, can we check also the error value here?

Quoted 5 lines
Quoted 1 line

Why?

Because if a function can return an error, the error must be checked. Even if the property is "reg" and the binding says that it's required. Otherwise let's make those functions void.

Then every function should have a must_check annotation, but they don't as the function is designed to work with optional properties where we want to ignore errors.

Quoted 7 lines
Quoted 2 lines

The old code wasn't worried about of_get_property() returning NULL on the same possible errors.

Sure! Checking the error comes for free. The patch is fine as it is, mine was a little improvement I asked for. I can still ack it and add the error handling later myself :)

Quoted 8 lines
Quoted 3 lines

If anyone is still actually using
mpc512x, I don't think their DTB will have an error at this point. IOW, is improving the error handling on this really worth it?

In my view, every error needs to be checked as every error is unlikely to happen: it makes the code future proof and makes sure other components failure don't impact the normal functioning of this driver.

An error in this case is a bad DT. It's not the kernel's job to ensure DT is correct. If it is, then it is doing a terrible job. The reason we have dtschema is to ensure correctness.

Rob

#6 lore raw
In reply to Rob Herring
Quoted 4 lines · Rob Herring

Use the recently added of_property_read_reg() helper to get the untranslated "reg" address value.

This patch results in:

Building powerpc:ppc32_allmodconfig ... failed
--------------
Error log:
drivers/i2c/busses/i2c-mpc.c: In function 'mpc_i2c_setup_512x':
drivers/i2c/busses/i2c-mpc.c:310:20: error: unused variable 'pval' [-Werror=unused-variable]
310 | const u32 *pval;

because pval is no longer used.

Guenter

Quoted 24 lines
---
 drivers/i2c/busses/i2c-mpc.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/i2c/busses/i2c-mpc.c +3 -2
index cfd074ee6d54..595dce9218ad 100644
@@ -316,9 +316,10 @@ static void mpc_i2c_setup_512x(struct device_node *node,
 	if (node_ctrl) {
 		ctrl = of_iomap(node_ctrl, 0);
 		if (ctrl) {
+			u64 addr;
 			/* Interrupt enable bits for i2c-0/1/2: bit 24/26/28 */
-			pval = of_get_property(node, "reg", NULL);
-			idx = (*pval & 0xff) / 0x20;
+			of_property_read_reg(node, 0, &addr, NULL);
+			idx = (addr & 0xff) / 0x20;
 			setbits32(ctrl, 1 << (24 + idx * 2));
 			iounmap(ctrl);
 		}
Signature · 1 line
2.39.2