From patchwork Fri Mar 18 16:00:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= X-Patchwork-Id: 12785559 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B8E4BC433EF for ; Fri, 18 Mar 2022 16:04:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238478AbiCRQFR (ORCPT ); Fri, 18 Mar 2022 12:05:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60406 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238428AbiCRQFP (ORCPT ); Fri, 18 Mar 2022 12:05:15 -0400 Received: from relay2-d.mail.gandi.net (relay2-d.mail.gandi.net [IPv6:2001:4b98:dc4:8::222]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E4C1A58E73; Fri, 18 Mar 2022 09:03:26 -0700 (PDT) Received: (Authenticated sender: clement.leger@bootlin.com) by mail.gandi.net (Postfix) with ESMTPSA id 7956D40028; Fri, 18 Mar 2022 16:03:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1647619405; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=c/SCFKpq/JCyZCtCb1owNCXOwLEWKTbQIfZVT6MqRuA=; b=bMFyM5jlOA5ipjBlaEWX+IcDaNXz7a5I2Dc0Y8/7iMf4gNQ1zSRl/0hCzS1sv++eg697Eo uIWbT5gN5h7H5ymSEjwcJzsHjEYJIJGSkX3O3lka0M0InDGUC0I/6mhb28g2ZZ71oDy7Bs zYAXSiT6potwN82eTj3yOORVyOxgExVE//RrLE0zBecddPoAu9E9MfZVrNLkBDscylMMTU FYrpjHhtINvi5O+4szwfACgI9s95sVjaPA1uKUEkPktsp6p1jKmPLUt4iwHj5kPDapPrec tKKaoA129MyhAqzlyr6eX89tGzThx+b5+HbBYQBaxoGKijpU1EnkDRbowjSlyg== From: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= To: Andy Shevchenko , Daniel Scally , Heikki Krogerus , Sakari Ailus , Greg Kroah-Hartman , "'Rafael J . Wysocki '" , Wolfram Sang , Peter Rosin , Russell King , Andrew Lunn , Heiner Kallweit , "David S . Miller" , Jakub Kicinski , Paolo Abeni Cc: Hans de Goede , Thomas Petazzoni , Alexandre Belloni , Allan Nielsen , linux-kernel@vger.kernel.org, linux-acpi@vger.kernel.org, linux-i2c@vger.kernel.org, netdev@vger.kernel.org, =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= Subject: [PATCH 2/6] i2c: fwnode: add fwnode_find_i2c_adapter_by_node() Date: Fri, 18 Mar 2022 17:00:55 +0100 Message-Id: <20220318160059.328208-10-clement.leger@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220318160059.328208-1-clement.leger@bootlin.com> References: <20220318160059.328208-1-clement.leger@bootlin.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-acpi@vger.kernel.org Add fwnode_find_i2c_adapter_by_node() which allows to retrieve a i2c adapter using a fwnode. Since dev_fwnode() uses the fwnode provided by the of_node member of the device, this will also work for devices were the of_node has been set and not the fwnode field. Signed-off-by: Clément Léger --- drivers/i2c/Makefile | 1 + drivers/i2c/i2c-core-fwnode.c | 41 +++++++++++++++++++++++++++++++++++ include/linux/i2c.h | 2 ++ 3 files changed, 44 insertions(+) create mode 100644 drivers/i2c/i2c-core-fwnode.c diff --git a/drivers/i2c/Makefile b/drivers/i2c/Makefile index c1d493dc9bac..c9c97675163e 100644 --- a/drivers/i2c/Makefile +++ b/drivers/i2c/Makefile @@ -6,6 +6,7 @@ obj-$(CONFIG_I2C_BOARDINFO) += i2c-boardinfo.o obj-$(CONFIG_I2C) += i2c-core.o i2c-core-objs := i2c-core-base.o i2c-core-smbus.o +i2c-core-objs += i2c-core-fwnode.o i2c-core-$(CONFIG_ACPI) += i2c-core-acpi.o i2c-core-$(CONFIG_I2C_SLAVE) += i2c-core-slave.o i2c-core-$(CONFIG_OF) += i2c-core-of.o diff --git a/drivers/i2c/i2c-core-fwnode.c b/drivers/i2c/i2c-core-fwnode.c new file mode 100644 index 000000000000..2404c2477a80 --- /dev/null +++ b/drivers/i2c/i2c-core-fwnode.c @@ -0,0 +1,41 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Linux I2C core fwnode support code + * + * Copyright (C) 2022 Microchip + */ + +#include +#include + +#include "i2c-core.h" + +static int fwnode_dev_or_parent_node_match(struct device *dev, const void *data) +{ + if (device_match_fwnode(dev, data)) + return 1; + + /* For ACPI device node, we do not want to match the parent */ + if (!is_acpi_device_node(dev_fwnode(dev)) && dev->parent) + return device_match_fwnode(dev->parent, data); + + return 0; +} + +struct i2c_adapter *fwnode_find_i2c_adapter_by_node(struct fwnode_handle *node) +{ + struct device *dev; + struct i2c_adapter *adapter; + + dev = bus_find_device(&i2c_bus_type, NULL, node, + fwnode_dev_or_parent_node_match); + if (!dev) + return NULL; + + adapter = i2c_verify_adapter(dev); + if (!adapter) + put_device(dev); + + return adapter; +} +EXPORT_SYMBOL(fwnode_find_i2c_adapter_by_node); diff --git a/include/linux/i2c.h b/include/linux/i2c.h index 7d4f52ceb7b5..9b480a8b0a76 100644 --- a/include/linux/i2c.h +++ b/include/linux/i2c.h @@ -967,6 +967,8 @@ int i2c_handle_smbus_host_notify(struct i2c_adapter *adap, unsigned short addr); #endif /* I2C */ +struct i2c_adapter *fwnode_find_i2c_adapter_by_node(struct fwnode_handle *node); + #if IS_ENABLED(CONFIG_OF) /* must call put_device() when done with returned i2c_client device */ struct i2c_client *of_find_i2c_device_by_node(struct device_node *node);