Skip to contentSkip to conversation

sandbox: i2c: Make sure phandle dependency is probed

PATCH In review Marek Vasut 2 participants 4 messages Oct 3, 2024 – Oct 5, 2024 I2C

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sandbox: i2c: Make sure phandle dependency is probed

This driver depends on i2c emulator via DT phandle. Make sure the dependency is probed before this driver is probed, not when it is first used during transfer. This fixes the following i2c_emul_find() error:

"

$ ./u-boot -Dc ""
...
       i2c_emul_find() No emulators for device 'sandbox_pmic'
  sandbox_pmic_write() write error to device: 0000000018c568d0 register: 0x0!
       out_set_value() PMIC write failed: -5
       i2c_emul_find() No emulators for device 'sandbox_pmic'
  sandbox_pmic_write() write error to device: 0000000018c568d0 register: 0x0!
       out_set_value() PMIC write failed: -5
...
"

---
Cc: Heiko Schocher <[email protected]>
Cc: Simon Glass <[email protected]>
Cc: Tom Rini <[email protected]>
Cc: [email protected]

---
 drivers/i2c/sandbox_i2c.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/i2c/sandbox_i2c.c +9 -0
index 74bb5e93397..9a39ac14fb4 100644
@@ -80,6 +80,14 @@ static int sandbox_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
 	return ops->xfer(emul, msg, nmsgs);
 }
 
+static int sandbox_i2c_probe(struct udevice *dev)
+{
+	struct udevice *emul;
+
+	return uclass_get_device_by_phandle(UCLASS_I2C_EMUL, dev,
+					    "sandbox,emul", &emul);
+}
+
 static const struct dm_i2c_ops sandbox_i2c_ops = {
 	.xfer		= sandbox_i2c_xfer,
 };
@@ -95,4 +103,5 @@ U_BOOT_DRIVER(sandbox_i2c) = {
 	.of_match = sandbox_i2c_ids,
 	.ops	= &sandbox_i2c_ops,
 	.priv_auto	= sizeof(struct sandbox_i2c_priv),
+	.probe	= sandbox_i2c_probe,
 };
Signature · 1 line
2.45.2
#1 lore raw
Quoted 32 lines · Marek Vasut

This driver depends on i2c emulator via DT phandle. Make sure the dependency is probed before this driver is probed, not when it is first used during transfer. This fixes the following i2c_emul_find() error:

"

$ ./u-boot -Dc ""
...
        i2c_emul_find() No emulators for device 'sandbox_pmic'
   sandbox_pmic_write() write error to device: 0000000018c568d0 register: 0x0!
        out_set_value() PMIC write failed: -5
        i2c_emul_find() No emulators for device 'sandbox_pmic'
   sandbox_pmic_write() write error to device: 0000000018c568d0 register: 0x0!
        out_set_value() PMIC write failed: -5
...
"

---
Cc: Heiko Schocher <[email protected]>
Cc: Simon Glass <[email protected]>
Cc: Tom Rini <[email protected]>
Cc: [email protected]

---
  drivers/i2c/sandbox_i2c.c | 9 +++++++++
  1 file changed, 9 insertions(+)
drivers/i2c/sandbox_i2c.c +0 -0
index 74bb5e93397..9a39ac14fb4 100644

drivers/i2c/sandbox_i2c.c static int sandbox_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, · line 89

Quoted 10 lines
drivers/i2c/sandbox_i2c.c +7 -0
@@ -80,6 +80,14 @@ static int sandbox_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
  	return ops->xfer(emul, msg, nmsgs);
  }
 
+static int sandbox_i2c_probe(struct udevice *dev)
+{
+	struct udevice *emul;
+
+	return uclass_get_device_by_phandle(UCLASS_I2C_EMUL, dev,
+					    "sandbox,emul", &emul);
+}

Hmmm, no, this does not do it, but it does show what the issue is -- sandbox_i2c_xfer is misused to defer access to this emulator until the first I2C transfer, so the emulator can hopefully probe in the meantime. This is fragile and now it broke. What I am not quite sure about is why this uclass_get_device_by_phandle() returns -2 here instead of probing the emulator ?

#2 lore raw

Hi Marek!

Quoted 28 lines · Marek Vasut
Quoted 27 lines · Marek Vasut

This driver depends on i2c emulator via DT phandle. Make sure the dependency is probed before this driver is probed, not when it is first used during transfer. This fixes the following i2c_emul_find() error:

"

$ ./u-boot -Dc ""
...
        i2c_emul_find() No emulators for device 'sandbox_pmic'
   sandbox_pmic_write() write error to device: 0000000018c568d0 register: 0x0!
        out_set_value() PMIC write failed: -5
        i2c_emul_find() No emulators for device 'sandbox_pmic'
   sandbox_pmic_write() write error to device: 0000000018c568d0 register: 0x0!
        out_set_value() PMIC write failed: -5
...
"

---
Cc: Heiko Schocher <[email protected]>
Cc: Simon Glass <[email protected]>
Cc: Tom Rini <[email protected]>
Cc: [email protected]
---
  drivers/i2c/sandbox_i2c.c | 9 +++++++++

  1 file changed, 9 insertions(+)
Quoted 15 lines
Quoted 5 lines
drivers/i2c/sandbox_i2c.c +0 -0
index 74bb5e93397..9a39ac14fb4 100644

drivers/i2c/sandbox_i2c.c static int sandbox_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, · line 88

Quoted 9 lines
drivers/i2c/sandbox_i2c.c +7 -0
@@ -80,6 +80,14 @@ static int sandbox_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
       return ops->xfer(emul, msg, nmsgs);
   }
+static int sandbox_i2c_probe(struct udevice *dev)
+{
+    struct udevice *emul;
+
+    return uclass_get_device_by_phandle(UCLASS_I2C_EMUL, dev,
+                        "sandbox,emul", &emul);
+}

Hmmm, no, this does not do it, but it does show what the issue is -- sandbox_i2c_xfer is misused to defer access to this emulator until the first I2C transfer, so the emulator can hopefully probe in the meantime. This is fragile and now it broke. What I am not quite sure about is why this uclass_get_device_by_phandle() returns -2 here instead of probing the emulator ?

Hmm... -2 means it does not find such a device ... why, I don;t know ... do you have a chance to debug?

Just a fast grep (I must admit, never looked into this parts...)) may it helps if you use

int i2c_emul_find(struct udevice *dev, struct udevice **emulp)

found in file:

./drivers/i2c/i2c-emul-uclass.c

instead?

bye,
Heiko

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Erika Unter
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-52   Fax: +49-8142-66989-80   Email: [email protected]
#3 lore raw
Quoted 48 lines · Heiko Schocher

Hi Marek!

Quoted 28 lines · Marek Vasut
Quoted 27 lines · Marek Vasut

This driver depends on i2c emulator via DT phandle. Make sure the dependency is probed before this driver is probed, not when it is first used during transfer. This fixes the following i2c_emul_find() error:

"

$ ./u-boot -Dc ""
...
        i2c_emul_find() No emulators for device 'sandbox_pmic'
   sandbox_pmic_write() write error to device: 0000000018c568d0 register: 0x0!
        out_set_value() PMIC write failed: -5
        i2c_emul_find() No emulators for device 'sandbox_pmic'
   sandbox_pmic_write() write error to device: 0000000018c568d0 register: 0x0!
        out_set_value() PMIC write failed: -5
...
"

---
Cc: Heiko Schocher <[email protected]>
Cc: Simon Glass <[email protected]>
Cc: Tom Rini <[email protected]>
Cc: [email protected]
---
  drivers/i2c/sandbox_i2c.c | 9 +++++++++

  1 file changed, 9 insertions(+)
Quoted 16 lines
Quoted 5 lines
drivers/i2c/sandbox_i2c.c +0 -0
index 74bb5e93397..9a39ac14fb4 100644

drivers/i2c/sandbox_i2c.c static int sandbox_i2c_xfer(struct udevice *bus, · line 89

Quoted 10 lines
drivers/i2c/sandbox_i2c.c +7 -0
@@ -80,6 +80,14 @@ static int sandbox_i2c_xfer(struct udevice *bus, 
 struct i2c_msg *msg,
       return ops->xfer(emul, msg, nmsgs);
   }
+static int sandbox_i2c_probe(struct udevice *dev)
+{
+    struct udevice *emul;
+
+    return uclass_get_device_by_phandle(UCLASS_I2C_EMUL, dev,
+                        "sandbox,emul", &emul);
+}

Hmmm, no, this does not do it, but it does show what the issue is -- sandbox_i2c_xfer is misused to defer access to this emulator until the first I2C transfer, so the emulator can hopefully probe in the meantime. This is fragile and now it broke. What I am not quite sure about is why this uclass_get_device_by_phandle() returns -2 here instead of probing the emulator ?

Quoted 2 lines

Hmm... -2 means it does not find such a device ... why, I don;t know ... do you have a chance to debug?

It seems I was too hasty with this patch, now superseded by:

[PATCH] sandbox: dts: Add missing sandbox,emul phandle to sandbox-pmic