From patchwork Tue Oct 31 07:38:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philip Oberfichtner X-Patchwork-Id: 1857380 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; dkim=pass (2048-bit key; unprotected) header.d=denx.de header.i=@denx.de header.a=rsa-sha256 header.s=phobos-20191101 header.b=jaai/eAR; dkim=pass (2048-bit key) header.d=denx.de header.i=@denx.de header.a=rsa-sha256 header.s=phobos-20191101 header.b=kdFfC3g7; dkim-atps=neutral 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=patchwork.ozlabs.org) 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 (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4SKMX53Wm1z1yQ5 for ; Tue, 31 Oct 2023 18:41:25 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 625EC87C66; Tue, 31 Oct 2023 08:41:23 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=denx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=denx.de; s=phobos-20191101; t=1698738083; bh=aMZJhUXVGXnRgek9MX/fSpNLDfeju/VEHlpNYTqH/9I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=jaai/eARKU9EiEkOrcjFiZs5pjz90S/8uxvTCSVnv8w57cBTVMn1nH4tLYD2pUoFx Dn75mQpT5viHyUo/PQmphnES+UWH7b9BHhJV3yAUcNlIOvOeSZtplP39iFtm/nz+E/ OxGq7HGVc91VO2qOe1QF7ozt5grfwMIPerSoL/z8A+w5MJFVs4tfQnZ0DFlk8cFHUJ dNrXy2y2yjpT74Urb50F7+yCF6i+a2WEn9thvYfXNLytrhfG9ISddkWTHQFIW68W17 Bt+pEWZtsUHCP+OteCKywe3r/kP1sk6qFbkghtSkXWoqZzSLRNyks2oPIlxnrq0rWe Q1jki96aOOEIw== Received: from localhost (unknown [IPv6:2a01:cb18:989:a700:4b67:3ae7:15fa:40a0]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: pro@denx.de) by phobos.denx.de (Postfix) with ESMTPSA id 426CE87C6E; Tue, 31 Oct 2023 08:41:22 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=denx.de; s=phobos-20191101; t=1698738082; bh=aMZJhUXVGXnRgek9MX/fSpNLDfeju/VEHlpNYTqH/9I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kdFfC3g7SjHUHOQAbyIDuAfJCGl1he57q4xiZ4/tK9au8fBbOEdejsmUK6OMFUTVW y6ookJTsAEff4fRPhw+VRiL6uOwdbdNIs16CdHkdYQy8J2AcIXn+nizcdjaKOBmbdM x1JHj8s9c8Rzmm3IRQcZZGDNSkxZgyTQh36eU3cfJoFk2jk9IFffqnUfzDTJuCm0dH 1AN7muJO0svA+Syk+vNlM9t0imULfiDwAhXGk+u06rDGLJLOhXwWPt6a9zV9v/yxgm 0yE7Xv+x0KhguDf032Y9sSoVMbYb5z3cjmCl+nYGrqEsviibZjyKxmD4U4koWZvpW4 EQd4oTZwE1tFw== From: Philip Richard Oberfichtner To: u-boot@lists.denx.de Cc: hs@denx.de, sjg@chromium.org, trini@konsulko.com, Philip Richard Oberfichtner Subject: [PATCH v3 2/3] i2c: Implement i2c_get_chip_by_phandle() Date: Tue, 31 Oct 2023 08:38:46 +0100 Message-ID: <20231031073844.15683-6-pro@denx.de> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231031073844.15683-2-pro@denx.de> References: <20231031073844.15683-2-pro@denx.de> MIME-Version: 1.0 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.8 at phobos.denx.de X-Virus-Status: Clean 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 Reviewed-by: Heiko Schocher --- Notes: Changes in v3: none Changes in v2: new drivers/i2c/i2c-uclass.c | 75 ++++++++++++++++++++++++++++++++++++++++ include/i2c.h | 12 +++++++ 2 files changed, 87 insertions(+) diff --git a/drivers/i2c/i2c-uclass.c b/drivers/i2c/i2c-uclass.c index 8867a560bd..5405067861 100644 --- a/drivers/i2c/i2c-uclass.c +++ b/drivers/i2c/i2c-uclass.c @@ -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) { diff --git a/include/i2c.h b/include/i2c.h index ef3820eaba..4e59009cd9 100644 --- a/include/i2c.h +++ b/include/i2c.h @@ -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 *