[PATCH] i2c: add dm_i2c_probe_chip() to detect chip presence

Add function to determine whether a chip is present. This check is typically implemented by writing the chip address to the bus and checking that the chip replies with an ACK.
The already existing dm_i2c_probe() will attempt to bind/probe the relevant driver if the chip is present. This makes it unsuitable for situations where one only wants to know the presence of a chip.
Signed-off-by: Johan Korsnes johan.korsnes@remarkable.no Cc: Eirik Schultz schultzern@gmail.com Cc: Heiko Schocher hs@denx.de Cc: Simon Glass sjg@chromium.org
--- Previously, I've used i2c_probe() to determine whether or not an i2c chip is present on an i2c bus. With the introduction of the driver model this function is deprecated. Fortunately, I found dm_i2c_probe(), which I expected to perform the same check, it was even documented to be suitable for this purpose:
``` dm_i2c_probe() - probe a particular chip address
This can be useful to check for the existence of a chip on the bus. It is typically implemented by writing the chip address to the bus and checking that the chip replies with an ACK. ```
Unfortunately, it does not seem to be a replacement. It seems dm_i2c_probe() will attempt to bind/probe a driver if the chip is present, and will in turn return the return value from the driver probe/bind attempt. --- drivers/i2c/i2c-uclass.c | 11 +++++++++++ include/i2c.h | 16 +++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-)
diff --git a/drivers/i2c/i2c-uclass.c b/drivers/i2c/i2c-uclass.c index 8d9a89ed89..16a2be7531 100644 --- a/drivers/i2c/i2c-uclass.c +++ b/drivers/i2c/i2c-uclass.c @@ -409,6 +409,17 @@ int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags, return ret; }
+int dm_i2c_probe_chip(struct udevice *bus, uint chip_addr, uint chip_flags) +{ + int ret; + + ret = i2c_probe_chip(bus, chip_addr, chip_flags); + debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name, + chip_addr, ret); + + return ret; +} + int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed) { struct dm_i2c_ops *ops = i2c_get_ops(bus); diff --git a/include/i2c.h b/include/i2c.h index e0ee94e550..4c22d8c887 100644 --- a/include/i2c.h +++ b/include/i2c.h @@ -206,6 +206,18 @@ int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer, /** * dm_i2c_probe() - probe a particular chip address * + * @bus: Bus to probe + * @chip_addr: 7-bit address to probe (10-bit and others are not supported) + * @chip_flags: Flags for the probe (see enum dm_i2c_chip_flags) + * @devp: Returns the device found, or NULL if none + * Return: 0 if a chip was found at that address, -ve if not + */ +int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags, + struct udevice **devp); + +/** + * dm_i2c_probe_chip() - probe a particular chip address + * * This can be useful to check for the existence of a chip on the bus. * It is typically implemented by writing the chip address to the bus * and checking that the chip replies with an ACK. @@ -213,11 +225,9 @@ int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer, * @bus: Bus to probe * @chip_addr: 7-bit address to probe (10-bit and others are not supported) * @chip_flags: Flags for the probe (see enum dm_i2c_chip_flags) - * @devp: Returns the device found, or NULL if none * Return: 0 if a chip was found at that address, -ve if not */ -int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags, - struct udevice **devp); +int dm_i2c_probe_chip(struct udevice *bus, uint chip_addr, uint chip_flags);
/** * dm_i2c_reg_read() - Read a value from an I2C register

Hi Johan,
On Mon, 19 Dec 2022 at 12:57, Johan Korsnes johan.korsnes@remarkable.no wrote:
Add function to determine whether a chip is present. This check is typically implemented by writing the chip address to the bus and checking that the chip replies with an ACK.
The already existing dm_i2c_probe() will attempt to bind/probe the relevant driver if the chip is present. This makes it unsuitable for situations where one only wants to know the presence of a chip.
Signed-off-by: Johan Korsnes johan.korsnes@remarkable.no Cc: Eirik Schultz schultzern@gmail.com Cc: Heiko Schocher hs@denx.de Cc: Simon Glass sjg@chromium.org
Previously, I've used i2c_probe() to determine whether or not an i2c chip is present on an i2c bus. With the introduction of the driver model this function is deprecated. Fortunately, I found dm_i2c_probe(), which I expected to perform the same check, it was even documented to be suitable for this purpose:
dm_i2c_probe() - probe a particular chip address This can be useful to check for the existence of a chip on the bus. It is typically implemented by writing the chip address to the bus and checking that the chip replies with an ACK.
Unfortunately, it does not seem to be a replacement. It seems dm_i2c_probe() will attempt to bind/probe a driver if the chip is present, and will in turn return the return value from the driver probe/bind attempt.
drivers/i2c/i2c-uclass.c | 11 +++++++++++ include/i2c.h | 16 +++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-)
This looks OK but please add a test to dm/test/i2c.c for your new function.
https://u-boot.readthedocs.io/en/latest/develop/testing.html
Regards, Simon

Hello Johan,
On 19.12.22 20:57, Johan Korsnes wrote:
Add function to determine whether a chip is present. This check is typically implemented by writing the chip address to the bus and checking that the chip replies with an ACK.
The already existing dm_i2c_probe() will attempt to bind/probe the relevant driver if the chip is present. This makes it unsuitable for situations where one only wants to know the presence of a chip.
Signed-off-by: Johan Korsnes johan.korsnes@remarkable.no Cc: Eirik Schultz schultzern@gmail.com Cc: Heiko Schocher hs@denx.de Cc: Simon Glass sjg@chromium.org
Previously, I've used i2c_probe() to determine whether or not an i2c chip is present on an i2c bus. With the introduction of the driver model this function is deprecated. Fortunately, I found dm_i2c_probe(), which I expected to perform the same check, it was even documented to be suitable for this purpose:
dm_i2c_probe() - probe a particular chip address This can be useful to check for the existence of a chip on the bus. It is typically implemented by writing the chip address to the bus and checking that the chip replies with an ACK.
Unfortunately, it does not seem to be a replacement. It seems dm_i2c_probe() will attempt to bind/probe a driver if the chip is present, and will in turn return the return value from the driver probe/bind attempt.
drivers/i2c/i2c-uclass.c | 11 +++++++++++ include/i2c.h | 16 +++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-)
Reviewed-by: Heiko Schocher hs@denx.de
Please add a test, as Simon requested, thanks!
bye, Heiko
participants (3)
-
Heiko Schocher
-
Johan Korsnes
-
Simon Glass