]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
i2c: Implement i2c_get_chip_by_phandle()
authorPhilip Richard Oberfichtner <pro@denx.de>
Tue, 31 Oct 2023 07:38:46 +0000 (08:38 +0100)
committerHeiko Schocher <hs@denx.de>
Tue, 31 Oct 2023 08:08:51 +0000 (09:08 +0100)
This new function enhances the i2c_get_chip*() toolbox by implementing a
variant that does not require a chip_addr. Instead, the desired device
is pointed to by a phandle.

Signed-off-by: Philip Richard Oberfichtner <pro@denx.de>
Reviewed-by: Heiko Schocher <hs@denx.de>
drivers/i2c/i2c-uclass.c
include/i2c.h

index 8867a560bd8e8264db24310dbd324e31cdb36c16..5405067861eb9c66d20854e0a989ff04727ba8fe 100644 (file)
@@ -388,6 +388,81 @@ int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
        return 0;
 }
 
+/* Find and probe I2C bus based on a chip attached to it */
+static int i2c_get_parent_bus(ofnode chip, struct udevice **devp)
+{
+       ofnode node;
+       struct udevice *dev;
+       int ret;
+
+       node = ofnode_get_parent(chip);
+       if (!ofnode_valid(node))
+               return -ENODEV;
+
+       ret = uclass_get_device_by_ofnode(UCLASS_I2C, node, &dev);
+       if (ret) {
+               *devp = NULL;
+               return ret;
+       }
+
+       *devp = dev;
+       return 0;
+}
+
+int i2c_get_chip_by_phandle(const struct udevice *parent, const char *prop_name,
+                           struct udevice **devp)
+{
+       ofnode node;
+       uint phandle;
+       struct udevice *bus, *chip;
+       char *dev_name;
+       int ret;
+
+       debug("%s: Searching I2C chip for phandle \"%s\"\n",
+             __func__, prop_name);
+
+       dev_name = strdup(prop_name);
+       if (!dev_name) {
+               ret = -ENOMEM;
+               goto err_exit;
+       }
+
+       ret = dev_read_u32(parent, "i2cbcdev", &phandle);
+       if (ret)
+               goto err_exit;
+
+       node = ofnode_get_by_phandle(phandle);
+       if (!ofnode_valid(node)) {
+               ret = -ENODEV;
+               goto err_exit;
+       }
+
+       ret = i2c_get_parent_bus(node, &bus);
+       if (ret)
+               goto err_exit;
+
+       ret = device_bind_driver_to_node(bus, "i2c_generic_chip_drv",
+                                        dev_name, node, &chip);
+       if (ret)
+               goto err_exit;
+
+       ret = device_probe(chip);
+       if (ret) {
+               device_unbind(chip);
+               goto err_exit;
+       }
+
+       debug("%s succeeded\n", __func__);
+       *devp = chip;
+       return 0;
+
+err_exit:
+       free(dev_name);
+       debug("%s failed, ret = %d\n", __func__, ret);
+       *devp = NULL;
+       return ret;
+}
+
 int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
                 struct udevice **devp)
 {
index ef3820eaba7c05b2103bc5912299416c2a4b492d..4e59009cd9310ede6997e32a499738e32fec1430 100644 (file)
@@ -537,6 +537,18 @@ int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
 int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
                            struct udevice **devp);
 
+/**
+ * i2c_get_chip_by_phandle() - get a device to use to access a chip
+ *                            based on a phandle property pointing to it
+ *
+ * @parent: Parent device containing the phandle pointer
+ * @name:   Name of phandle property in the parent device node
+ * @devp:   Returns pointer to new device or NULL if not found
+ * Return:  0 on success, -ve on failure
+ */
+int i2c_get_chip_by_phandle(const struct udevice *parent, const char *prop_name,
+                           struct udevice **devp);
+
 /**
  * i2c_chip_of_to_plat() - Decode standard I2C platform data
  *