THIS PATCH IS ALREADY PART OF THE ADC128D818 SERIES
WILL BE REMOVED BEFORE FINAL REVIEW

Slaves created with i2c_slave_create_simple() were left unparented and
showed up under /machine/unattached with no stable QOM path. Add each
slave as a QOM child of its bus, named after its I2C address, so it has
a deterministic and addressable QOM path.

Signed-off-by: Emmanuel Blot <[email protected]>
(cherry picked from commit f55644fa78777e52fd90efaa59dd89b4ff0a7020)
Signed-off-by: Emmanuel Blot <[email protected]>
---
 hw/i2c/core.c        | 3 +++
 include/hw/i2c/i2c.h | 7 +++++--
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/hw/i2c/core.c b/hw/i2c/core.c
index 54f6bdca882..0bbce45d48d 100644
--- a/hw/i2c/core.c
+++ b/hw/i2c/core.c
@@ -382,6 +382,9 @@ I2CSlave *i2c_slave_create_simple(I2CBus *bus, const char 
*name, uint8_t addr)
 {
     I2CSlave *dev = i2c_slave_new(name, addr);
 
+    g_autofree char *childname = g_strdup_printf("0x%02x", addr);
+    object_property_add_child(OBJECT(bus), childname, OBJECT(dev));
+
     i2c_slave_realize_and_unref(dev, bus, &error_abort);
 
     return dev;
diff --git a/include/hw/i2c/i2c.h b/include/hw/i2c/i2c.h
index dd5930f4b5e..dc557bbf3f2 100644
--- a/include/hw/i2c/i2c.h
+++ b/include/hw/i2c/i2c.h
@@ -166,13 +166,16 @@ bool i2c_scan_bus(I2CBus *bus, uint8_t address, bool 
broadcast,
 I2CSlave *i2c_slave_new(const char *name, uint8_t addr);
 
 /**
- * Create and realize an I2C slave device on the heap.
+ * Create and realize an I2C slave device on the heap, add the device as a
+ * child of its parent bus.
+ *
  * @bus: I2C bus to put it on
  * @name: I2C slave device type name
  * @addr: I2C address of the slave when put on a bus
  *
  * Create the device state structure, initialize it, put it on the
- * specified @bus, and drop the reference to it (the device is realized).
+ * specified @bus, parent it, and drop the reference to it (the device is
+ * realized).
  */
 I2CSlave *i2c_slave_create_simple(I2CBus *bus, const char *name, uint8_t addr);
 

-- 
2.50.1


Reply via email to