From patchwork Mon Dec 19 19:57:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johan Korsnes X-Patchwork-Id: 1717827 X-Patchwork-Delegate: hs@denx.de Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=remarkable.no header.i=@remarkable.no header.a=rsa-sha256 header.s=google header.b=n1zfXkaG; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4NbvwK0k8Zz1ydZ for ; Tue, 20 Dec 2022 22:48:17 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 06EA285301; Tue, 20 Dec 2022 12:47:58 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=remarkable.no Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; secure) header.d=remarkable.no header.i=@remarkable.no header.b="n1zfXkaG"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id B28A38523C; Mon, 19 Dec 2022 20:57:37 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.2 Received: from mail-lf1-x130.google.com (mail-lf1-x130.google.com [IPv6:2a00:1450:4864:20::130]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 68C5785196 for ; Mon, 19 Dec 2022 20:57:34 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=remarkable.no Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=johan.korsnes@remarkable.no Received: by mail-lf1-x130.google.com with SMTP id y25so15292221lfa.9 for ; Mon, 19 Dec 2022 11:57:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=remarkable.no; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=ZJUm5tgukAHLug2yBgsYYm5jmji5dA5jWDGhJ+kjdEA=; b=n1zfXkaGirHDYr6JCFdrcU5LrTNBxY6smVFTUPUsmYYp+KiItdhvclGRSQ+MvNjof1 0nqD1VuGOgwq09RSK0EYlOODtcuBGIdncevGJA14mQc+hmxh2c6q4LdCvs6nTz0sNamP cWZHio0TLNQMaVa5zfvugt0wm7m4VnQ3t+MxVKDPm0cXVatz36GwHuGXeTltmTOG45OY nMEzxsdpic/EHH3Kh6gHNNhkqEUMl9BZJMeAB5vpaDZ8nCeKCHss7/Xi95++Jx8EiYOs TTUc3rMMa/7unGTs7+XqD2jPrGqjoCdIwC3gLWBXZnKhR/UVJH8f85t5G2VRVXl7QE2t Lx0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ZJUm5tgukAHLug2yBgsYYm5jmji5dA5jWDGhJ+kjdEA=; b=gIe6MJvXNMkjbpdF3rNc2QQsmNG4q9O5/TEPD77+mjJThej7Z0LYtv6ni+NfjWGxw6 0/1vmQxVnsYn+3EQtvssfEQmnZTQD22UGG6K7hmrYL09wgREuEhUbdITlZ6HCr3hdFCh wWjywl+yN/JM72VwHWZzojlsQ1ZSglOFVe+9HHV/4DZwcXkCz+olC3nToVQPPPIKP6Br Q/kFDEfObYevGegA38bvLaaJFeej7Bow6uoLsP25QveCqT5PjXQPoj+TNraDvVlDyiIC kbP00jM4FMBXXlusXqomf2xLo+eOaIBymCFZE3c4MoBAkqpXClCUk0cc71ycj3Vim/4j QQsQ== X-Gm-Message-State: ANoB5pmYEFcjzx8e9AvneH868kaaOrbMuA3/Q9iH4q6EteI32c4CAmH9 x/MKNj+mdRYMouSUiBQYYO6yjRlOgAnq2ewX2peD X-Google-Smtp-Source: AA0mqf6RRIkkXElWjJkz0aDkNknZNuiJSlSwsWQKtHO9Q4q41o2QsDYggPOcPUsdlo5XMAzG89IORg== X-Received: by 2002:a05:6512:2293:b0:4b5:5dea:ec68 with SMTP id f19-20020a056512229300b004b55deaec68mr15026860lfu.44.1671479853509; Mon, 19 Dec 2022 11:57:33 -0800 (PST) Received: from t490.home ([2a01:799:a23:9b00:fece:64c2:e746:9eec]) by smtp.gmail.com with ESMTPSA id u24-20020ac258d8000000b004991437990esm1186282lfo.11.2022.12.19.11.57.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Dec 2022 11:57:33 -0800 (PST) From: Johan Korsnes To: u-boot@lists.denx.de Cc: Johan Korsnes , Eirik Schultz , Heiko Schocher , Simon Glass Subject: [PATCH] i2c: add dm_i2c_probe_chip() to detect chip presence Date: Mon, 19 Dec 2022 20:57:27 +0100 Message-Id: <20221219195727.494332-1-johan.korsnes@remarkable.no> X-Mailer: git-send-email 2.38.1 MIME-Version: 1.0 X-Mailman-Approved-At: Tue, 20 Dec 2022 12:47:53 +0100 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean 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 Cc: Eirik Schultz Cc: Heiko Schocher Cc: Simon Glass Reviewed-by: Heiko Schocher --- 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