From patchwork Wed Nov 29 16:37:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 171489 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a5a7:0:b0:403:3b70:6f57 with SMTP id d7csp463558vqn; Wed, 29 Nov 2023 08:37:49 -0800 (PST) X-Google-Smtp-Source: AGHT+IGjvz+2iDXUK5W9wZc9wAFK7mxKaQhqIUNhRlCIbZurcnougyxNIJi50i8VGYPz3nqTmsNH X-Received: by 2002:a17:90b:3883:b0:285:98dc:2d19 with SMTP id mu3-20020a17090b388300b0028598dc2d19mr19998663pjb.28.1701275868957; Wed, 29 Nov 2023 08:37:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701275868; cv=none; d=google.com; s=arc-20160816; b=Co4x+l/GH3mNRIwxXRHSRlc/wQQFtGx1MG1Ch532xnx8lLJ7V2laEkXHOBocNdhsEb oPlTMwqsZ64QJ2oJSfXRVAINStr141fnwQ3aJbQVByVYuaX3IMC+7LBatNktw1VCq945 OEGGG/o/pyrebKG5VlRXA9x12JSGXJLBON772Cn5ixWlE8Qt3Fw4R001Z/wJbfdakfD7 pfCSaC6afwdbN34kTgEcWqPEZJSlRECqkv2PSMKNHxOZCNnDlGPEgRfIMklPxBu5nayw Kr1CmXxNDa08eEZ1QenSOFW3s9/L5qk7QfKOu93FAjZ3FsngrflMke9P08JSD2i2dobB cXpA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=etuea1hYns3mh9gfNWVfF9L9O5bOXs1ej6kKA06ZjoU=; fh=qgA2FVbwaqNFxGsYB26kv0E2ZBI0ilnhCMBmPIeDQ3Q=; b=n2opyouH/V6YqFreCXovaMgq4cztpnyTSNq/H2D/EOFVpRImIFcKNw9+HIxSSfaK+4 539ziNmDIRkvFjOQ4VcpZ3gWDBTHrL3X+ks+8LNAcmio/GgNjVZK30ar6UfqnH3Rd2z1 B8MBmAf0IrwbZE9cfK1mIYKrQ5/QaC9mkouxAHn9minLqmh/9pbFYOKIVH1gf9DmCPYl MBAKKVCCOi4Wupb2QOq7Ai2EH8TfYNCvRI3NcXdWAS/yFGYF689YWxrq7VL3GGqkohGv LiVU2NjslyqZ1ZrzkeDHaVywL5YxWgG3tvCEZ6iN2OFWvFlIxjFoFV9BfSwTapQcOdmU YP/g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=JeAPBSJO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id x18-20020a17090aca1200b00278eb61c0ebsi1653017pjt.118.2023.11.29.08.37.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Nov 2023 08:37:48 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=JeAPBSJO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id F0F30802FA60; Wed, 29 Nov 2023 08:37:47 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230391AbjK2Qhh (ORCPT + 99 others); Wed, 29 Nov 2023 11:37:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34084 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229561AbjK2Qhf (ORCPT ); Wed, 29 Nov 2023 11:37:35 -0500 Received: from relay6-d.mail.gandi.net (relay6-d.mail.gandi.net [217.70.183.198]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7A6F4D5E for ; Wed, 29 Nov 2023 08:37:41 -0800 (PST) Received: by mail.gandi.net (Postfix) with ESMTPSA id 89061C0003; Wed, 29 Nov 2023 16:37:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1701275860; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=etuea1hYns3mh9gfNWVfF9L9O5bOXs1ej6kKA06ZjoU=; b=JeAPBSJOH3r6FB/6/MlthKOFO5psrg4lKC5DX3X3ek/xb8vIfqN1h4bgde7jcXSZDbSca2 CTYibRIos6A+lmEYVdER6vKP+EutUMLUo90WWakj3bgGelnfg6ERpZBJCVhQk9QSVt47MV kmHgX9zvB1n3T0kN88a5DMYSoOfrVwbW73akrDWAEhDecyaS87aQTkVkxqftqhqJGlFXP4 mPYRWJbjm3YJeJ/CvixSsgNEGhH9+CZsySaZxPR5FdyPYL/WIOQgxC7PWnGUYRpeZmynZX +3TLE8VCsiDVmL/eE/WyAzb7jvESDArX+PBiMyYBG/CHA+tam5AiRGpmo3+s+A== From: Miquel Raynal To: Srinivas Kandagatla , Greg Kroah-Hartman , Cc: Thomas Petazzoni , Robert Marko , Luca Ceresoli , Michael Walle , =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= , Marco Felsch , Randy Dunlap , Chen-Yu Tsai , Daniel Golle , Miquel Raynal , Rob Herring Subject: [PATCH v14 1/8] of: device: Export of_device_make_bus_id() Date: Wed, 29 Nov 2023 17:37:30 +0100 Message-Id: <20231129163737.698317-2-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231129163737.698317-1-miquel.raynal@bootlin.com> References: <20231129163737.698317-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 X-GND-Sasl: miquel.raynal@bootlin.com X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 29 Nov 2023 08:37:48 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783917045422219088 X-GMAIL-MSGID: 1783917045422219088 This helper is really handy to create unique device names based on their device tree path, we may need it outside of the OF core (in the NVMEM subsystem) so let's export it. As this helper has nothing patform specific, let's move it to of/device.c instead of of/platform.c so we can add its prototype to of_device.h. Signed-off-by: Miquel Raynal Acked-by: Rob Herring --- drivers/of/device.c | 41 +++++++++++++++++++++++++++++++++++++++ drivers/of/platform.c | 40 -------------------------------------- include/linux/of_device.h | 6 ++++++ 3 files changed, 47 insertions(+), 40 deletions(-) diff --git a/drivers/of/device.c b/drivers/of/device.c index 1ca42ad9dd15..6e9572c4af83 100644 --- a/drivers/of/device.c +++ b/drivers/of/device.c @@ -304,3 +304,44 @@ int of_device_uevent_modalias(const struct device *dev, struct kobj_uevent_env * return 0; } EXPORT_SYMBOL_GPL(of_device_uevent_modalias); + +/** + * of_device_make_bus_id - Use the device node data to assign a unique name + * @dev: pointer to device structure that is linked to a device tree node + * + * This routine will first try using the translated bus address to + * derive a unique name. If it cannot, then it will prepend names from + * parent nodes until a unique name can be derived. + */ +void of_device_make_bus_id(struct device *dev) +{ + struct device_node *node = dev->of_node; + const __be32 *reg; + u64 addr; + u32 mask; + + /* Construct the name, using parent nodes if necessary to ensure uniqueness */ + while (node->parent) { + /* + * If the address can be translated, then that is as much + * uniqueness as we need. Make it the first component and return + */ + reg = of_get_property(node, "reg", NULL); + if (reg && (addr = of_translate_address(node, reg)) != OF_BAD_ADDR) { + if (!of_property_read_u32(node, "mask", &mask)) + dev_set_name(dev, dev_name(dev) ? "%llx.%x.%pOFn:%s" : "%llx.%x.%pOFn", + addr, ffs(mask) - 1, node, dev_name(dev)); + + else + dev_set_name(dev, dev_name(dev) ? "%llx.%pOFn:%s" : "%llx.%pOFn", + addr, node, dev_name(dev)); + return; + } + + /* format arguments only used if dev_name() resolves to NULL */ + dev_set_name(dev, dev_name(dev) ? "%s:%s" : "%s", + kbasename(node->full_name), dev_name(dev)); + node = node->parent; + } +} +EXPORT_SYMBOL_GPL(of_device_make_bus_id); diff --git a/drivers/of/platform.c b/drivers/of/platform.c index 126d265aa7d8..c39e49a1eba0 100644 --- a/drivers/of/platform.c +++ b/drivers/of/platform.c @@ -97,46 +97,6 @@ static const struct of_device_id of_skipped_node_table[] = { * mechanism for creating devices from device tree nodes. */ -/** - * of_device_make_bus_id - Use the device node data to assign a unique name - * @dev: pointer to device structure that is linked to a device tree node - * - * This routine will first try using the translated bus address to - * derive a unique name. If it cannot, then it will prepend names from - * parent nodes until a unique name can be derived. - */ -static void of_device_make_bus_id(struct device *dev) -{ - struct device_node *node = dev->of_node; - const __be32 *reg; - u64 addr; - u32 mask; - - /* Construct the name, using parent nodes if necessary to ensure uniqueness */ - while (node->parent) { - /* - * If the address can be translated, then that is as much - * uniqueness as we need. Make it the first component and return - */ - reg = of_get_property(node, "reg", NULL); - if (reg && (addr = of_translate_address(node, reg)) != OF_BAD_ADDR) { - if (!of_property_read_u32(node, "mask", &mask)) - dev_set_name(dev, dev_name(dev) ? "%llx.%x.%pOFn:%s" : "%llx.%x.%pOFn", - addr, ffs(mask) - 1, node, dev_name(dev)); - - else - dev_set_name(dev, dev_name(dev) ? "%llx.%pOFn:%s" : "%llx.%pOFn", - addr, node, dev_name(dev)); - return; - } - - /* format arguments only used if dev_name() resolves to NULL */ - dev_set_name(dev, dev_name(dev) ? "%s:%s" : "%s", - kbasename(node->full_name), dev_name(dev)); - node = node->parent; - } -} - /** * of_device_alloc - Allocate and initialize an of_device * @np: device node to assign to device diff --git a/include/linux/of_device.h b/include/linux/of_device.h index 2c7a3d4bc775..a72661e47faa 100644 --- a/include/linux/of_device.h +++ b/include/linux/of_device.h @@ -40,6 +40,9 @@ static inline int of_dma_configure(struct device *dev, { return of_dma_configure_id(dev, np, force_dma, NULL); } + +void of_device_make_bus_id(struct device *dev); + #else /* CONFIG_OF */ static inline int of_driver_match_device(struct device *dev, @@ -82,6 +85,9 @@ static inline int of_dma_configure(struct device *dev, { return 0; } + +static inline void of_device_make_bus_id(struct device *dev) {} + #endif /* CONFIG_OF */ #endif /* _LINUX_OF_DEVICE_H */ From patchwork Wed Nov 29 16:37:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 171490 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a5a7:0:b0:403:3b70:6f57 with SMTP id d7csp463645vqn; Wed, 29 Nov 2023 08:37:56 -0800 (PST) X-Google-Smtp-Source: AGHT+IFr8FlLv/T8wf0XQbA72PVKgqigE89Xm0hnSRjmbsg4NPAMojUeVtpa9abYVm2VJBOwNRTJ X-Received: by 2002:a17:902:d2cf:b0:1cf:dce0:4d9f with SMTP id n15-20020a170902d2cf00b001cfdce04d9fmr10969359plc.20.1701275876065; Wed, 29 Nov 2023 08:37:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701275876; cv=none; d=google.com; s=arc-20160816; b=cgq2GBNygUkSyLieHW2SxCQR+ff/pIVH8UafYInQB1ZnAgubc4tQBip484QIYsgrUx zaHZuvPTj8opVwg3ZPd0PMIo0Pduwz1hsXQ2kfM413CDITWoqCq/OBi7+ZEIZ+VX6ytq vTA8pYVyOWBHv0m33a2uZAZKE2HxaVw5wDoy2NyeKLHmxus05/KePWjisD9qlgt6C3TD nZ+4QjnB54zgoEx5LjHAWhkCblS9h/SWKPZpbHtkW0Q0FPNIDMOtTGnpxu3sRBsN/kal ZwlIeu9X8XxdAhIGmcgoMpQZau+NaCwhtXt7NanD25iDMNIreDEVoa0a16tTNc3dpXws uP4w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=NEqUrySQxiqBK2Xf7e40ptVb8/7simBVDiwRFnhyJTI=; fh=LHVfHqkxtSfhDK3Auz+HtR9CGNiwZCQHjp1a868hmsY=; b=R44J8SmrpOQfr1FA/kqgRUXhs/UrW+gOhXGPWex5WsicfwelPXtPVdSepvAMO9J0vX CnXhs69ZuvQNHLyvePfhyVZ3g9k2hGcdzD7cHxPNKag3RPnXlS+7cLzbCFdxvEN22jVO n5mwJuKYcMHnnTx41rdczvjB7zjJJr0ZGVLq8PGTnvqrQWC/Fy6+a6sD4ykoM2+C//ke YfaheYGbCye+8ooXjtMi5/ZPc4pGpCEkle0QN8L1KcCM45JBjhf4YvtjBAAfc2jFKBoC iuDKD5vXA7nLzetWDJQcq12H4cjBQeRKhDMyMkz0Gd3AdrVTghEmMKMFx9OlWuzIUp+K swfA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b="SXq/ud+x"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id n18-20020a170902d2d200b001cfe100a9a6si5368997plc.405.2023.11.29.08.37.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Nov 2023 08:37:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b="SXq/ud+x"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 94711802FA6E; Wed, 29 Nov 2023 08:37:54 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230493AbjK2Qhl (ORCPT + 99 others); Wed, 29 Nov 2023 11:37:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34092 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229588AbjK2Qhg (ORCPT ); Wed, 29 Nov 2023 11:37:36 -0500 Received: from relay6-d.mail.gandi.net (relay6-d.mail.gandi.net [217.70.183.198]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 415E5D66 for ; Wed, 29 Nov 2023 08:37:42 -0800 (PST) Received: by mail.gandi.net (Postfix) with ESMTPSA id 5DF1FC0008; Wed, 29 Nov 2023 16:37:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1701275861; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=NEqUrySQxiqBK2Xf7e40ptVb8/7simBVDiwRFnhyJTI=; b=SXq/ud+xRr+okUa1SU0hm65e4dwhrGCGmtXAmtbdIfDDODRbwMHu3+au+k9EscP3WiD4/y DaHuw4AR1q2u/215u0O8ZyIXuwSqyM3zPVI2/KdlsoCqRkfa7iv+r3Jyuq35FT13eoB09r 5sjZDc+XF9L5Sa3ez7WnL7cwrzTe91yAZMspzJK2uzhNK/7tU32TWGeuMsPv7EUPikXRfZ SzzSj2wxX1cMr411KbPaO9f1tnxkdF8F61TGc9G1UTm2dFZyh5o5MdDevXpOsag48ienZC IL7ZI1DsVZLYB+TeKTyHSitzJjpUUOQTgT3BRYc0tmWEgQ45wIR+NU+qh7e5ig== From: Miquel Raynal To: Srinivas Kandagatla , Greg Kroah-Hartman , Cc: Thomas Petazzoni , Robert Marko , Luca Ceresoli , Michael Walle , =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= , Marco Felsch , Randy Dunlap , Chen-Yu Tsai , Daniel Golle , Miquel Raynal Subject: [PATCH v14 2/8] nvmem: Move of_nvmem_layout_get_container() in another header Date: Wed, 29 Nov 2023 17:37:31 +0100 Message-Id: <20231129163737.698317-3-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231129163737.698317-1-miquel.raynal@bootlin.com> References: <20231129163737.698317-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 X-GND-Sasl: miquel.raynal@bootlin.com X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 29 Nov 2023 08:37:54 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783917052879484199 X-GMAIL-MSGID: 1783917052879484199 nvmem-consumer.h is included by consumer devices, extracting data from NVMEM devices whereas nvmem-provider.h is included by devices providing NVMEM content. The only users of of_nvmem_layout_get_container() outside of the core are layout drivers, so better move its prototype to nvmem-provider.h. While we do so, we also move the kdoc associated with the function to the header rather than the .c file. Signed-off-by: Miquel Raynal --- drivers/nvmem/core.c | 8 -------- include/linux/nvmem-consumer.h | 7 ------- include/linux/nvmem-provider.h | 21 +++++++++++++++++++++ 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index 608b352a7d91..b5b6ec8e04bb 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -847,14 +847,6 @@ static int nvmem_add_cells_from_layout(struct nvmem_device *nvmem) } #if IS_ENABLED(CONFIG_OF) -/** - * of_nvmem_layout_get_container() - Get OF node to layout container. - * - * @nvmem: nvmem device. - * - * Return: a node pointer with refcount incremented or NULL if no - * container exists. Use of_node_put() on it when done. - */ struct device_node *of_nvmem_layout_get_container(struct nvmem_device *nvmem) { return of_get_child_by_name(nvmem->dev.of_node, "nvmem-layout"); diff --git a/include/linux/nvmem-consumer.h b/include/linux/nvmem-consumer.h index 6ec4b9743e25..2d306fa13b1a 100644 --- a/include/linux/nvmem-consumer.h +++ b/include/linux/nvmem-consumer.h @@ -247,7 +247,6 @@ struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id); struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *name); -struct device_node *of_nvmem_layout_get_container(struct nvmem_device *nvmem); #else static inline struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id) @@ -260,12 +259,6 @@ static inline struct nvmem_device *of_nvmem_device_get(struct device_node *np, { return ERR_PTR(-EOPNOTSUPP); } - -static inline struct device_node * -of_nvmem_layout_get_container(struct nvmem_device *nvmem) -{ - return NULL; -} #endif /* CONFIG_NVMEM && CONFIG_OF */ #endif /* ifndef _LINUX_NVMEM_CONSUMER_H */ diff --git a/include/linux/nvmem-provider.h b/include/linux/nvmem-provider.h index e3930835235b..e5de21516387 100644 --- a/include/linux/nvmem-provider.h +++ b/include/linux/nvmem-provider.h @@ -244,6 +244,27 @@ nvmem_layout_get_match_data(struct nvmem_device *nvmem, #endif /* CONFIG_NVMEM */ +#if IS_ENABLED(CONFIG_NVMEM) && IS_ENABLED(CONFIG_OF) + +/** + * of_nvmem_layout_get_container() - Get OF node of layout container + * + * @nvmem: nvmem device + * + * Return: a node pointer with refcount incremented or NULL if no + * container exists. Use of_node_put() on it when done. + */ +struct device_node *of_nvmem_layout_get_container(struct nvmem_device *nvmem); + +#else /* CONFIG_NVMEM && CONFIG_OF */ + +static inline struct device_node *of_nvmem_layout_get_container(struct nvmem_device *nvmem) +{ + return NULL; +} + +#endif /* CONFIG_NVMEM && CONFIG_OF */ + #define module_nvmem_layout_driver(__layout_driver) \ module_driver(__layout_driver, nvmem_layout_register, \ nvmem_layout_unregister) From patchwork Wed Nov 29 16:37:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 171492 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a5a7:0:b0:403:3b70:6f57 with SMTP id d7csp463747vqn; Wed, 29 Nov 2023 08:38:04 -0800 (PST) X-Google-Smtp-Source: AGHT+IEARmNLWRmrHBQVL4wE940eU1NPkHf+L4mlJfbbvhD4+3faoJp7RDnmA+oySGRnulZDpG/F X-Received: by 2002:a05:6a21:3283:b0:18b:246a:d43d with SMTP id yt3-20020a056a21328300b0018b246ad43dmr25088697pzb.15.1701275884039; Wed, 29 Nov 2023 08:38:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701275884; cv=none; d=google.com; s=arc-20160816; b=Stz8t4qXJuS+RD9BxYsiJci8SoaxLZ6VRxlkqnBpnijuux345+f/UvCi6Qyg6WTD6G XqTx5G9/umB0vdPnl9QgScKM7Z4aXtf/fCmN0Hneo1dvGtvYIB9t9fpoUn9yHr9R+nOI rFv3BF4CXkenWuEeq3SQkuOO27m2VIw/sS3zvowyIix6rzK1j2jHQiUZEDfC8TXsuJRc 2vPY8cpoJywdhgHCGR+x2UBtlnmoaLO3aedt98js4MSG4sSFCQimko6i2VVpjXP3dqys wOlLVvh7UmUL1kGY9+5KdJyHQ7LY4lLPZE5jaFrp7SEoIoSVM4VdOAFr+XmBVa+M1Fio YP+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=cFyU6O8ilcV7xp4q/of+2+aQkRrhD5AxyRveYuZqHQY=; fh=LHVfHqkxtSfhDK3Auz+HtR9CGNiwZCQHjp1a868hmsY=; b=uU1Moz+rzIpvFOV254z2YhQsUQ2TPeepTypgTSkITnIqc6Rlz/JlQ8337Cv+qcq2Tb /1Mot/xj/Mk4QkKlc/9MUa/amagwQP/ktGx2+TWkI7C2sXrXChvn+fWiMHSTQl3hc9oJ lq+4SSvbkuxCd66TEOU5+EjQK0CNB2QEjB0uK0syDVPL/2cf7Sp+XjtKjDYDQoaQIe+4 kMRXGGA/X+pLrG+omBu+xlAgQgu67ntkIyagWzgd9+9si64mPa3GJrLuhvxuD3UWH0Y0 g2JE1Gc1MDHwhHSEXdDZf66/3Gj5OrIL19WtyqYfW8TcXeZjk2073m5i67bfrIwYrR4N u0rg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=aWBx7yX3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id gd6-20020a17090b0fc600b002855e50a7d9si1580546pjb.26.2023.11.29.08.38.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Nov 2023 08:38:04 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=aWBx7yX3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id F09C8802F85A; Wed, 29 Nov 2023 08:38:01 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230511AbjK2Qho (ORCPT + 99 others); Wed, 29 Nov 2023 11:37:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34094 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229485AbjK2Qhh (ORCPT ); Wed, 29 Nov 2023 11:37:37 -0500 Received: from relay6-d.mail.gandi.net (relay6-d.mail.gandi.net [IPv6:2001:4b98:dc4:8::226]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3DE5F10A for ; Wed, 29 Nov 2023 08:37:43 -0800 (PST) Received: by mail.gandi.net (Postfix) with ESMTPSA id 2507FC0007; Wed, 29 Nov 2023 16:37:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1701275861; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=cFyU6O8ilcV7xp4q/of+2+aQkRrhD5AxyRveYuZqHQY=; b=aWBx7yX3Ta1jei4cV0eIEV73KUZHp7UHbwBlkWjCofvx+OaOLayuNcLBsvY6BKmRTVAnTo mz6LcYUSyxry5Skh1ZeD8Owq6u0qkwOSfOx6Hz/9IESUN7O2X/hEAq4lbwz9hH/xYem2AY XLtRu9d7Xgb6h/7ReBnF5MjjCD/d1wvn6PwRQrtZX7uYQHSUA15nmgaJ0HOn/8PiQWnoj6 Mwrj4jZkMnZfoBVQdYX1Cw8nZPg+PC4y3p5MlkCQdhBCzcC2weAUGt1O5FPTAfaHcKoFA0 /vA2jEvWlt2hHk/i6Rt/NpJQUutfdfWvKIfOJ9jgjJM2GKtU3oE6dYNq6Hb1MA== From: Miquel Raynal To: Srinivas Kandagatla , Greg Kroah-Hartman , Cc: Thomas Petazzoni , Robert Marko , Luca Ceresoli , Michael Walle , =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= , Marco Felsch , Randy Dunlap , Chen-Yu Tsai , Daniel Golle , Miquel Raynal Subject: [PATCH v14 3/8] nvmem: Create a header for internal sharing Date: Wed, 29 Nov 2023 17:37:32 +0100 Message-Id: <20231129163737.698317-4-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231129163737.698317-1-miquel.raynal@bootlin.com> References: <20231129163737.698317-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 X-GND-Sasl: miquel.raynal@bootlin.com X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_PASS,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 29 Nov 2023 08:38:02 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783917060891095729 X-GMAIL-MSGID: 1783917060891095729 Before adding all the NVMEM layout bus infrastructure to the core, let's move the main nvmem_device structure in an internal header, only available to the core. This way all the additional code can be added in a dedicated file in order to keep the current core file tidy. Signed-off-by: Miquel Raynal --- drivers/nvmem/core.c | 24 +----------------------- drivers/nvmem/internals.h | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 23 deletions(-) create mode 100644 drivers/nvmem/internals.h diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index b5b6ec8e04bb..b5e5ce67398f 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -19,29 +19,7 @@ #include #include -struct nvmem_device { - struct module *owner; - struct device dev; - int stride; - int word_size; - int id; - struct kref refcnt; - size_t size; - bool read_only; - bool root_only; - int flags; - enum nvmem_type type; - struct bin_attribute eeprom; - struct device *base_dev; - struct list_head cells; - const struct nvmem_keepout *keepout; - unsigned int nkeepout; - nvmem_reg_read_t reg_read; - nvmem_reg_write_t reg_write; - struct gpio_desc *wp_gpio; - struct nvmem_layout *layout; - void *priv; -}; +#include "internals.h" #define to_nvmem_device(d) container_of(d, struct nvmem_device, dev) diff --git a/drivers/nvmem/internals.h b/drivers/nvmem/internals.h new file mode 100644 index 000000000000..ce353831cd65 --- /dev/null +++ b/drivers/nvmem/internals.h @@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef _LINUX_NVMEM_INTERNALS_H +#define _LINUX_NVMEM_INTERNALS_H + +#include +#include +#include + +struct nvmem_device { + struct module *owner; + struct device dev; + struct list_head node; + int stride; + int word_size; + int id; + struct kref refcnt; + size_t size; + bool read_only; + bool root_only; + int flags; + enum nvmem_type type; + struct bin_attribute eeprom; + struct device *base_dev; + struct list_head cells; + const struct nvmem_keepout *keepout; + unsigned int nkeepout; + nvmem_reg_read_t reg_read; + nvmem_reg_write_t reg_write; + struct gpio_desc *wp_gpio; + struct nvmem_layout *layout; + void *priv; +}; + +#endif /* ifndef _LINUX_NVMEM_INTERNALS_H */ From patchwork Wed Nov 29 16:37:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 171491 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a5a7:0:b0:403:3b70:6f57 with SMTP id d7csp463720vqn; Wed, 29 Nov 2023 08:38:02 -0800 (PST) X-Google-Smtp-Source: AGHT+IEcLCOZP2TTG4Vw+QSGelwV/D8p3dM5Cr/UmSZFv83k7XEdZxFkstVJnTCzL4Gc03tfkFcL X-Received: by 2002:a05:6a20:8f13:b0:18c:af15:7e73 with SMTP id b19-20020a056a208f1300b0018caf157e73mr11047821pzk.48.1701275882180; Wed, 29 Nov 2023 08:38:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701275882; cv=none; d=google.com; s=arc-20160816; b=RktW3PC/r8N4JTukIX+LVMEMYuucBts1blaJU2xhrx+GFB3hjveiMIeJSW2GIiGx0S fMAatey2QD2/IP0TQz8dF3XUKex/ChUlY9rDQOFQ7QkH6w3cksgzOnF1FbUvrlJ7BaVE YH/6rL1nvc57jxvOH8IijNhwo6fW1KJXjGDe4tva6XbdOapXAHkKL7/2TNwbii9NYDKm 1pCoAlz2CYO7ELzUuw04zA51AwEEjAFh45krDlTekXTE8BoOmpurc2kzYyWADRnCCGB2 F4SUrOtGit9vd5PowsmJU7RwtAUqjJv90iyCLsy4b+3w2Ozrp0TqkGrxsrR4pwdO6EHk eOUw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=c/YNgCWPKxHY49wBO0+94oHV863lvdF8QP5nwMeh4Ks=; fh=LHVfHqkxtSfhDK3Auz+HtR9CGNiwZCQHjp1a868hmsY=; b=0U1FU6S+wlswLHQtKpXs+UV6bKXMvJHe625/+UnFCqwBH476ktcXW5kX6abxlMJTmT 30r5c0tQe/vjt/ErzLxGiKZBHYEOeRVOZnAKgWyC9R/zIb1lkT18cmoy85bANjGJ02dT EwTlA+FI4M/8KbMECVuRX+XkXYeakUEhB/GEwalvehwpUQzLtjbuNK/T2YMQZ2poQ0LU 4XkoI6RlzRjvkqvrbCB0i55aHrSpVcvA01p2916iZBqH1XV1lpi+QKaLczYZ4TlvMIW1 uESKt9vfmR7vJWc8C0s589+DOLBTcyd3sUi4HVEnUt61srjEYSD/FdISRP+iDKujGlLh oMZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=Swke5rHg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id cf9-20020a056a02084900b005a0788d4a50si15924969pgb.376.2023.11.29.08.38.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Nov 2023 08:38:02 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=Swke5rHg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 88DC4802F845; Wed, 29 Nov 2023 08:38:00 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230332AbjK2Qhr (ORCPT + 99 others); Wed, 29 Nov 2023 11:37:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34098 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230406AbjK2Qhh (ORCPT ); Wed, 29 Nov 2023 11:37:37 -0500 Received: from relay6-d.mail.gandi.net (relay6-d.mail.gandi.net [IPv6:2001:4b98:dc4:8::226]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D4154D5E for ; Wed, 29 Nov 2023 08:37:43 -0800 (PST) Received: by mail.gandi.net (Postfix) with ESMTPSA id D4108C000C; Wed, 29 Nov 2023 16:37:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1701275862; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=c/YNgCWPKxHY49wBO0+94oHV863lvdF8QP5nwMeh4Ks=; b=Swke5rHgsw9rKPOlVP+ptkMkixWITnj2HDGZCjEC5/PJ0IolndzKGQiWwkuHoEx4KDbNDL VlzAsgWoxZMvvGR/H7RmJBoJhq+jxS70svG1+axlRz3WXbNZEwwiUa4PsxoEVTVBTV3EUl P6laxC9jKEy8Uh4NKf4XsQWYeFOEuIR2zye/MBALTgQLiVN5L0fuhzal4gn1QL6mHtAith wXdA6VNibGkVgMcmUKIvlJebJamwBdHsp3wSACOh3CGjxthrv0wyDuiwje2/4Dx7y7uH3B N6BTK0yoqcFDYupaVHm/wF/AQqZeIf2U5PfA4lFPlMiM4S35fW5BFrpYJxtuIg== From: Miquel Raynal To: Srinivas Kandagatla , Greg Kroah-Hartman , Cc: Thomas Petazzoni , Robert Marko , Luca Ceresoli , Michael Walle , =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= , Marco Felsch , Randy Dunlap , Chen-Yu Tsai , Daniel Golle , Miquel Raynal Subject: [PATCH v14 4/8] nvmem: Simplify the ->add_cells() hook Date: Wed, 29 Nov 2023 17:37:33 +0100 Message-Id: <20231129163737.698317-5-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231129163737.698317-1-miquel.raynal@bootlin.com> References: <20231129163737.698317-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 X-GND-Sasl: miquel.raynal@bootlin.com X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_PASS,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 29 Nov 2023 08:38:00 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783917059462271264 X-GMAIL-MSGID: 1783917059462271264 The layout entry is not used and will anyway be made useless by the new layout bus infrastructure coming next, so drop it. While at it, clarify the kdoc entry. Signed-off-by: Miquel Raynal --- drivers/nvmem/core.c | 2 +- drivers/nvmem/layouts/onie-tlv.c | 3 +-- drivers/nvmem/layouts/sl28vpd.c | 3 +-- include/linux/nvmem-provider.h | 8 +++----- 4 files changed, 6 insertions(+), 10 deletions(-) diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index b5e5ce67398f..f63db5e01fca 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -816,7 +816,7 @@ static int nvmem_add_cells_from_layout(struct nvmem_device *nvmem) int ret; if (layout && layout->add_cells) { - ret = layout->add_cells(&nvmem->dev, nvmem, layout); + ret = layout->add_cells(&nvmem->dev, nvmem); if (ret) return ret; } diff --git a/drivers/nvmem/layouts/onie-tlv.c b/drivers/nvmem/layouts/onie-tlv.c index 59fc87ccfcff..defd42d4375c 100644 --- a/drivers/nvmem/layouts/onie-tlv.c +++ b/drivers/nvmem/layouts/onie-tlv.c @@ -182,8 +182,7 @@ static bool onie_tlv_crc_is_valid(struct device *dev, size_t table_len, u8 *tabl return true; } -static int onie_tlv_parse_table(struct device *dev, struct nvmem_device *nvmem, - struct nvmem_layout *layout) +static int onie_tlv_parse_table(struct device *dev, struct nvmem_device *nvmem) { struct onie_tlv_hdr hdr; size_t table_len, data_len, hdr_len; diff --git a/drivers/nvmem/layouts/sl28vpd.c b/drivers/nvmem/layouts/sl28vpd.c index 05671371f631..26c7cf21b523 100644 --- a/drivers/nvmem/layouts/sl28vpd.c +++ b/drivers/nvmem/layouts/sl28vpd.c @@ -80,8 +80,7 @@ static int sl28vpd_v1_check_crc(struct device *dev, struct nvmem_device *nvmem) return 0; } -static int sl28vpd_add_cells(struct device *dev, struct nvmem_device *nvmem, - struct nvmem_layout *layout) +static int sl28vpd_add_cells(struct device *dev, struct nvmem_device *nvmem) { const struct nvmem_cell_info *pinfo; struct nvmem_cell_info info = {0}; diff --git a/include/linux/nvmem-provider.h b/include/linux/nvmem-provider.h index e5de21516387..3939991b3c5f 100644 --- a/include/linux/nvmem-provider.h +++ b/include/linux/nvmem-provider.h @@ -156,9 +156,8 @@ struct nvmem_cell_table { * * @name: Layout name. * @of_match_table: Open firmware match table. - * @add_cells: Will be called if a nvmem device is found which - * has this layout. The function will add layout - * specific cells with nvmem_add_one_cell(). + * @add_cells: Called to populate the layout using + * nvmem_add_one_cell(). * @fixup_cell_info: Will be called before a cell is added. Can be * used to modify the nvmem_cell_info. * @owner: Pointer to struct module. @@ -172,8 +171,7 @@ struct nvmem_cell_table { struct nvmem_layout { const char *name; const struct of_device_id *of_match_table; - int (*add_cells)(struct device *dev, struct nvmem_device *nvmem, - struct nvmem_layout *layout); + int (*add_cells)(struct device *dev, struct nvmem_device *nvmem); void (*fixup_cell_info)(struct nvmem_device *nvmem, struct nvmem_layout *layout, struct nvmem_cell_info *cell); From patchwork Wed Nov 29 16:37:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 171493 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a5a7:0:b0:403:3b70:6f57 with SMTP id d7csp463831vqn; Wed, 29 Nov 2023 08:38:13 -0800 (PST) X-Google-Smtp-Source: AGHT+IGWlQyw9Cib9H8sc0Vk1G0TxRJCv4SoJQMD+40kn2rdR9RXz5n7gh2sB+1aBFTI+S7TUSWV X-Received: by 2002:a17:902:e888:b0:1cf:c2b8:4772 with SMTP id w8-20020a170902e88800b001cfc2b84772mr12913920plg.18.1701275893074; Wed, 29 Nov 2023 08:38:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701275893; cv=none; d=google.com; s=arc-20160816; b=UikUjcq9jiM4OL8y/cf0n9k3VkFz3QxAIgWWbSI4H+IVP0mUVJSgOViD6Kkp/RVx0W HelZ1Cv/ebQaoQzovueOGcZUQrEHjFl4kj9EMqiMQTs1M1gr31S51JS0pdp3XN3Q3VwJ tG8O92r1N54YIB4vWN5QZ7K3pFbQebCInlIoUQeWf7GUs92asH0DIFSYKS8L2G8LObDq Fl8BJgz5Ubo9BFgRGS+Ij3FApxCkxoNfBpYXS04r34wvvEe27j29iPTPwrOxn+zOHWPM 6Mv5Wdtsqv7tTW6QsjbzCKzuG+wpiWKnys5DgukkhFDm9DI/6unoIY9LBF2BRDHJS/KJ J7KQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=N0YsLzvbE9fekXIAP2xv8c6ISu9IRksqX2H+fVGPhYA=; fh=LHVfHqkxtSfhDK3Auz+HtR9CGNiwZCQHjp1a868hmsY=; b=r4m7cjNn/KqwOlN9N7UOFq5vDfzmAhSKopkzvYv5vILwQ5ffFeDf+jsi2Q70mPk5al X/vubwej+inX3YGkhcoTomgh4hetetcuN14Prcs7XKFii9ATwRLRXLSK078bfuUx6xLq vW+JxbFj6x5GPKL+gF23i/ubaLxRWwqDODFUZVvrBONkgJXQ19qYPtdTmW4NSdE1DKRi 1PQoy87GH3hNL83RWv9MnqGhMheU/62sa2fGQj1VxzzigHrZ3+sNVPxorAuB9jbxG3a6 G0FXHORlhHFetXvGeWAhAohL3MH6OZkG4BWeMEcfS7phNpb/G55accEvq/bwWzIZFfgO 71KA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=jg4AMwhu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id e21-20020a170902d39500b001cfaeeb91a3si10870369pld.474.2023.11.29.08.38.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Nov 2023 08:38:13 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=jg4AMwhu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id C1F4B80B87D8; Wed, 29 Nov 2023 08:38:00 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231191AbjK2Qhv (ORCPT + 99 others); Wed, 29 Nov 2023 11:37:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34110 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229561AbjK2Qhi (ORCPT ); Wed, 29 Nov 2023 11:37:38 -0500 Received: from relay6-d.mail.gandi.net (relay6-d.mail.gandi.net [217.70.183.198]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 75E21D66 for ; Wed, 29 Nov 2023 08:37:44 -0800 (PST) Received: by mail.gandi.net (Postfix) with ESMTPSA id 93395C000F; Wed, 29 Nov 2023 16:37:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1701275863; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=N0YsLzvbE9fekXIAP2xv8c6ISu9IRksqX2H+fVGPhYA=; b=jg4AMwhu8UsZFBS15P+yPubjAgX/mYadHelmx0N6dyMOqd1riEjD/xcgtJFDLhOmsw65Ly HMp4aiV6sucWhw1/5gcO7jEawTs6uKK2bNJanT2RhasmKlz43vwHC84BMn58l8VUHhUZxv dhb4Fl7/mRWVI7QZ4+ebozaEjeM8w32YDYdL6tcoZBgRHtWCNFfayPbaKs9VwhNUCMW3EO dEY+Tim2xf+gncNPMJHQAKt+KVkFaiVoSjxdk6UQVMwn9ieOyMhnVUuK+NfS6c8lTO5IuU hLc2L4mgx29aODMUFfvPdenmKIg7jz8Dq8dAlum1IW3SnTPAfmd6AY8/Mcgqkg== From: Miquel Raynal To: Srinivas Kandagatla , Greg Kroah-Hartman , Cc: Thomas Petazzoni , Robert Marko , Luca Ceresoli , Michael Walle , =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= , Marco Felsch , Randy Dunlap , Chen-Yu Tsai , Daniel Golle , Miquel Raynal Subject: [PATCH v14 5/8] nvmem: Move and rename ->fixup_cell_info() Date: Wed, 29 Nov 2023 17:37:34 +0100 Message-Id: <20231129163737.698317-6-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231129163737.698317-1-miquel.raynal@bootlin.com> References: <20231129163737.698317-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 X-GND-Sasl: miquel.raynal@bootlin.com X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Wed, 29 Nov 2023 08:38:01 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783917070726194291 X-GMAIL-MSGID: 1783917070726194291 This hook is meant to be used by any provider and instantiating a layout just for this is useless. Let's instead move this hook to the nvmem device and add it to the config structure to be easily shared by the providers. While at moving this hook, rename it ->fixup_dt_cell_info() to clarify its main intended purpose. Signed-off-by: Miquel Raynal --- drivers/nvmem/core.c | 6 +++--- drivers/nvmem/imx-ocotp.c | 11 +++-------- drivers/nvmem/internals.h | 2 ++ drivers/nvmem/mtk-efuse.c | 11 +++-------- include/linux/nvmem-provider.h | 9 ++++----- 5 files changed, 15 insertions(+), 24 deletions(-) diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index f63db5e01fca..a885713d6b4b 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -675,7 +675,6 @@ static int nvmem_validate_keepouts(struct nvmem_device *nvmem) static int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_node *np) { - struct nvmem_layout *layout = nvmem->layout; struct device *dev = &nvmem->dev; struct device_node *child; const __be32 *addr; @@ -705,8 +704,8 @@ static int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_nod info.np = of_node_get(child); - if (layout && layout->fixup_cell_info) - layout->fixup_cell_info(nvmem, layout, &info); + if (nvmem->fixup_dt_cell_info) + nvmem->fixup_dt_cell_info(nvmem, &info); ret = nvmem_add_one_cell(nvmem, &info); kfree(info.name); @@ -895,6 +894,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) kref_init(&nvmem->refcnt); INIT_LIST_HEAD(&nvmem->cells); + nvmem->fixup_dt_cell_info = config->fixup_dt_cell_info; nvmem->owner = config->owner; if (!nvmem->owner && config->dev->driver) diff --git a/drivers/nvmem/imx-ocotp.c b/drivers/nvmem/imx-ocotp.c index f1e202efaa49..79dd4fda0329 100644 --- a/drivers/nvmem/imx-ocotp.c +++ b/drivers/nvmem/imx-ocotp.c @@ -583,17 +583,12 @@ static const struct of_device_id imx_ocotp_dt_ids[] = { }; MODULE_DEVICE_TABLE(of, imx_ocotp_dt_ids); -static void imx_ocotp_fixup_cell_info(struct nvmem_device *nvmem, - struct nvmem_layout *layout, - struct nvmem_cell_info *cell) +static void imx_ocotp_fixup_dt_cell_info(struct nvmem_device *nvmem, + struct nvmem_cell_info *cell) { cell->read_post_process = imx_ocotp_cell_pp; } -static struct nvmem_layout imx_ocotp_layout = { - .fixup_cell_info = imx_ocotp_fixup_cell_info, -}; - static int imx_ocotp_probe(struct platform_device *pdev) { struct device *dev = &pdev->dev; @@ -619,7 +614,7 @@ static int imx_ocotp_probe(struct platform_device *pdev) imx_ocotp_nvmem_config.size = 4 * priv->params->nregs; imx_ocotp_nvmem_config.dev = dev; imx_ocotp_nvmem_config.priv = priv; - imx_ocotp_nvmem_config.layout = &imx_ocotp_layout; + imx_ocotp_nvmem_config.fixup_dt_cell_info = &imx_ocotp_fixup_dt_cell_info; priv->config = &imx_ocotp_nvmem_config; diff --git a/drivers/nvmem/internals.h b/drivers/nvmem/internals.h index ce353831cd65..893553fbdf51 100644 --- a/drivers/nvmem/internals.h +++ b/drivers/nvmem/internals.h @@ -23,6 +23,8 @@ struct nvmem_device { struct bin_attribute eeprom; struct device *base_dev; struct list_head cells; + void (*fixup_dt_cell_info)(struct nvmem_device *nvmem, + struct nvmem_cell_info *cell); const struct nvmem_keepout *keepout; unsigned int nkeepout; nvmem_reg_read_t reg_read; diff --git a/drivers/nvmem/mtk-efuse.c b/drivers/nvmem/mtk-efuse.c index 87c94686cfd2..84f05b40a411 100644 --- a/drivers/nvmem/mtk-efuse.c +++ b/drivers/nvmem/mtk-efuse.c @@ -45,9 +45,8 @@ static int mtk_efuse_gpu_speedbin_pp(void *context, const char *id, int index, return 0; } -static void mtk_efuse_fixup_cell_info(struct nvmem_device *nvmem, - struct nvmem_layout *layout, - struct nvmem_cell_info *cell) +static void mtk_efuse_fixup_dt_cell_info(struct nvmem_device *nvmem, + struct nvmem_cell_info *cell) { size_t sz = strlen(cell->name); @@ -61,10 +60,6 @@ static void mtk_efuse_fixup_cell_info(struct nvmem_device *nvmem, cell->read_post_process = mtk_efuse_gpu_speedbin_pp; } -static struct nvmem_layout mtk_efuse_layout = { - .fixup_cell_info = mtk_efuse_fixup_cell_info, -}; - static int mtk_efuse_probe(struct platform_device *pdev) { struct device *dev = &pdev->dev; @@ -91,7 +86,7 @@ static int mtk_efuse_probe(struct platform_device *pdev) econfig.priv = priv; econfig.dev = dev; if (pdata->uses_post_processing) - econfig.layout = &mtk_efuse_layout; + econfig.fixup_dt_cell_info = &mtk_efuse_fixup_dt_cell_info; nvmem = devm_nvmem_register(dev, &econfig); return PTR_ERR_OR_ZERO(nvmem); diff --git a/include/linux/nvmem-provider.h b/include/linux/nvmem-provider.h index 3939991b3c5f..36415a602d9e 100644 --- a/include/linux/nvmem-provider.h +++ b/include/linux/nvmem-provider.h @@ -83,6 +83,8 @@ struct nvmem_cell_info { * @cells: Optional array of pre-defined NVMEM cells. * @ncells: Number of elements in cells. * @add_legacy_fixed_of_cells: Read fixed NVMEM cells from old OF syntax. + * @fixup_dt_cell_info: Will be called before a cell is added. Can be + * used to modify the nvmem_cell_info. * @keepout: Optional array of keepout ranges (sorted ascending by start). * @nkeepout: Number of elements in the keepout array. * @type: Type of the nvmem storage @@ -113,6 +115,8 @@ struct nvmem_config { const struct nvmem_cell_info *cells; int ncells; bool add_legacy_fixed_of_cells; + void (*fixup_dt_cell_info)(struct nvmem_device *nvmem, + struct nvmem_cell_info *cell); const struct nvmem_keepout *keepout; unsigned int nkeepout; enum nvmem_type type; @@ -158,8 +162,6 @@ struct nvmem_cell_table { * @of_match_table: Open firmware match table. * @add_cells: Called to populate the layout using * nvmem_add_one_cell(). - * @fixup_cell_info: Will be called before a cell is added. Can be - * used to modify the nvmem_cell_info. * @owner: Pointer to struct module. * @node: List node. * @@ -172,9 +174,6 @@ struct nvmem_layout { const char *name; const struct of_device_id *of_match_table; int (*add_cells)(struct device *dev, struct nvmem_device *nvmem); - void (*fixup_cell_info)(struct nvmem_device *nvmem, - struct nvmem_layout *layout, - struct nvmem_cell_info *cell); /* private */ struct module *owner; From patchwork Wed Nov 29 16:37:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 171495 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a5a7:0:b0:403:3b70:6f57 with SMTP id d7csp464146vqn; Wed, 29 Nov 2023 08:38:43 -0800 (PST) X-Google-Smtp-Source: AGHT+IEot7pNaZu2pGbRbfcXiSERvY3p9bXnE6ZTT3FwTkqsyyCpi7hej8fG0gcoQkBD8UgJDJko X-Received: by 2002:a17:90b:1d0a:b0:27c:f1f8:261f with SMTP id on10-20020a17090b1d0a00b0027cf1f8261fmr17628410pjb.20.1701275923622; Wed, 29 Nov 2023 08:38:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701275923; cv=none; d=google.com; s=arc-20160816; b=bMRkA6GSXsNp72FWw1ukVZ+gW5tLxa2nmpREpwz0D5xFoSxjrCiXq008/k0dOdy1J3 tsSv3zSu4AIb3sTgVDOMrBeCB0sJeiQCcVEJIk1SxqOPYj8SVqP33oEv9IupPxk4ZFAl Dg2CEaiArhT2HWRooqyVCed/pdW/uw/2CP+ulN3pDYrGy+soZxmy8p/fPRa/QVKPt3v4 3e+Fe33+n4/EcUxNW8jmkkaZMQ5jtBnJ4RXND/tm71Nsn1YzTrKZkmv0iX4JB8Otj9iU jBbQjvJvL9MY4NXWW8tqzfQOJ0Jzb2m0EgY5nKRd275mAMLQZwoger0AtCTe4vsWSF3p 5Jqg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=D36Ytx8rGsXg+xW5pN1QzY4n3vAl7v+sVZy7xtsPpH8=; fh=LHVfHqkxtSfhDK3Auz+HtR9CGNiwZCQHjp1a868hmsY=; b=n1Q/16kbEhSC7/PMDH2JIZYc9Qt9AJIRi0ic6bbp3Rp5yYH1nwWmO8/EsGN2Azszs2 pNUsjWDxFHGzdxb3vvmheldrD/BPjMia5ujnr6TUMKv8gu6tbOMMkV/EBffnOhlSOoJN U2RNrkj+Avzu6p99+YUpmVJ9jc8QWytEZ4tDFUNseZUmLmbTlf6pFaFVGDTCILs/JDt1 a63hH54QhjTPAzB1z3LSLovRj+dHxXbeLykRag6d+y9jOfunJXwOaIlP33eAglXYsO/e qlGY6wGW6S9uZtUu6DIXad9Ly67z5ya3WvkhuPAMRjQqyX1s8U6mJlUdrK+TEqMVoiRk 6lMQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=gfCXLoYY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id kx3-20020a17090b228300b00280294ec225si1544520pjb.24.2023.11.29.08.38.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Nov 2023 08:38:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=gfCXLoYY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 41E258033192; Wed, 29 Nov 2023 08:38:39 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230444AbjK2Qhy (ORCPT + 99 others); Wed, 29 Nov 2023 11:37:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34130 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229705AbjK2Qhk (ORCPT ); Wed, 29 Nov 2023 11:37:40 -0500 Received: from relay6-d.mail.gandi.net (relay6-d.mail.gandi.net [IPv6:2001:4b98:dc4:8::226]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 444EB10A for ; Wed, 29 Nov 2023 08:37:45 -0800 (PST) Received: by mail.gandi.net (Postfix) with ESMTPSA id 49BBDC000E; Wed, 29 Nov 2023 16:37:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1701275863; 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=D36Ytx8rGsXg+xW5pN1QzY4n3vAl7v+sVZy7xtsPpH8=; b=gfCXLoYYIasH7bXixhYaysPmYtsB2QqGOPM5BxFQjdlvdId7z09vbs5RH1L1mhWKxRRO0C etDvomc0QrqYTfNu10adAEJWMo5qarnq8mQe/iE8eAfZ8TR/aR9FEw7hmInv9wqAMidx6h AmgknxGyiJeyOPkrH3f+uQpKD4/D/F0c/a9Qs/CoStIni0ZCuekPqRPUNilZszLly4QDi7 7ry/U92x2SdkXBnGRYYwMfSseo1aiA+5WkOooaoH/sCKPNV90Qx7YWFHWfExs7wueSXMbh UZdYMgpfufpY+rRQKw/6BzRqELQFcU2dfOIRPoWaEmMqOgEhNs7PsdmlmVrqkA== From: Miquel Raynal To: Srinivas Kandagatla , Greg Kroah-Hartman , Cc: Thomas Petazzoni , Robert Marko , Luca Ceresoli , Michael Walle , =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= , Marco Felsch , Randy Dunlap , Chen-Yu Tsai , Daniel Golle , Miquel Raynal Subject: [PATCH v14 6/8] nvmem: core: Rework layouts to become regular devices Date: Wed, 29 Nov 2023 17:37:35 +0100 Message-Id: <20231129163737.698317-7-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231129163737.698317-1-miquel.raynal@bootlin.com> References: <20231129163737.698317-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 X-GND-Sasl: miquel.raynal@bootlin.com X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Wed, 29 Nov 2023 08:38:39 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783917103135517677 X-GMAIL-MSGID: 1783917103135517677 Current layout support was initially written without modules support in mind. When the requirement for module support rose, the existing base was improved to adopt modularization support, but kind of a design flaw was introduced. With the existing implementation, when a storage device registers into NVMEM, the core tries to hook a layout (if any) and populates its cells immediately. This means, if the hardware description expects a layout to be hooked up, but no driver was provided for that, the storage medium will fail to probe and try later from scratch. Even if we consider that the hardware description shall be correct, we could still probe the storage device (especially if it contains the rootfs). One way to overcome this situation is to consider the layouts as devices, and leverage the native notifier mechanism. When a new NVMEM device is registered, we can populate its nvmem-layout child, if any, and wait for the matching to be done in order to get the cells (the waiting can be easily done with the NVMEM notifiers). If the layout driver is compiled as a module, it should automatically be loaded. This way, there is no strong order to enforce, any NVMEM device creation or NVMEM layout driver insertion will be observed as a new event which may lead to the creation of additional cells, without disturbing the probes with costly (and sometimes endless) deferrals. In order to achieve that goal we create a new bus for the nvmem-layouts with minimal logic to match nvmem-layout devices with nvmem-layout drivers. All this infrastructure code is created in the layouts.c file. Signed-off-by: Miquel Raynal Tested-by: Rafał Miłecki --- drivers/nvmem/Kconfig | 1 + drivers/nvmem/Makefile | 2 + drivers/nvmem/core.c | 170 ++++++++++---------------- drivers/nvmem/internals.h | 21 ++++ drivers/nvmem/layouts.c | 201 +++++++++++++++++++++++++++++++ drivers/nvmem/layouts/Kconfig | 8 ++ drivers/nvmem/layouts/onie-tlv.c | 24 +++- drivers/nvmem/layouts/sl28vpd.c | 24 +++- include/linux/nvmem-provider.h | 38 +++--- 9 files changed, 354 insertions(+), 135 deletions(-) create mode 100644 drivers/nvmem/layouts.c diff --git a/drivers/nvmem/Kconfig b/drivers/nvmem/Kconfig index 5bc9c4874fe3..283134498fbc 100644 --- a/drivers/nvmem/Kconfig +++ b/drivers/nvmem/Kconfig @@ -1,6 +1,7 @@ # SPDX-License-Identifier: GPL-2.0-only menuconfig NVMEM bool "NVMEM Support" + imply NVMEM_LAYOUTS help Support for NVMEM(Non Volatile Memory) devices like EEPROM, EFUSES... diff --git a/drivers/nvmem/Makefile b/drivers/nvmem/Makefile index 423baf089515..cdd01fbf1313 100644 --- a/drivers/nvmem/Makefile +++ b/drivers/nvmem/Makefile @@ -5,6 +5,8 @@ obj-$(CONFIG_NVMEM) += nvmem_core.o nvmem_core-y := core.o +obj-$(CONFIG_NVMEM_LAYOUTS) += nvmem_layouts.o +nvmem_layouts-y := layouts.o obj-y += layouts/ # Devices diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index a885713d6b4b..1e1651be707b 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -55,9 +55,6 @@ static LIST_HEAD(nvmem_lookup_list); static BLOCKING_NOTIFIER_HEAD(nvmem_notifier); -static DEFINE_SPINLOCK(nvmem_layout_lock); -static LIST_HEAD(nvmem_layouts); - static int __nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset, void *val, size_t bytes) { @@ -740,97 +737,22 @@ static int nvmem_add_cells_from_fixed_layout(struct nvmem_device *nvmem) return err; } -int __nvmem_layout_register(struct nvmem_layout *layout, struct module *owner) +int nvmem_layout_register(struct nvmem_layout *layout) { - layout->owner = owner; + if (!layout->add_cells) + return -EINVAL; - spin_lock(&nvmem_layout_lock); - list_add(&layout->node, &nvmem_layouts); - spin_unlock(&nvmem_layout_lock); - - blocking_notifier_call_chain(&nvmem_notifier, NVMEM_LAYOUT_ADD, layout); - - return 0; + /* Populate the cells */ + return layout->add_cells(&layout->nvmem->dev, layout->nvmem); } -EXPORT_SYMBOL_GPL(__nvmem_layout_register); +EXPORT_SYMBOL_GPL(nvmem_layout_register); void nvmem_layout_unregister(struct nvmem_layout *layout) { - blocking_notifier_call_chain(&nvmem_notifier, NVMEM_LAYOUT_REMOVE, layout); - - spin_lock(&nvmem_layout_lock); - list_del(&layout->node); - spin_unlock(&nvmem_layout_lock); + /* Keep the API even with an empty stub in case we need it later */ } EXPORT_SYMBOL_GPL(nvmem_layout_unregister); -static struct nvmem_layout *nvmem_layout_get(struct nvmem_device *nvmem) -{ - struct device_node *layout_np; - struct nvmem_layout *l, *layout = ERR_PTR(-EPROBE_DEFER); - - layout_np = of_nvmem_layout_get_container(nvmem); - if (!layout_np) - return NULL; - - /* Fixed layouts don't have a matching driver */ - if (of_device_is_compatible(layout_np, "fixed-layout")) { - of_node_put(layout_np); - return NULL; - } - - /* - * In case the nvmem device was built-in while the layout was built as a - * module, we shall manually request the layout driver loading otherwise - * we'll never have any match. - */ - of_request_module(layout_np); - - spin_lock(&nvmem_layout_lock); - - list_for_each_entry(l, &nvmem_layouts, node) { - if (of_match_node(l->of_match_table, layout_np)) { - if (try_module_get(l->owner)) - layout = l; - - break; - } - } - - spin_unlock(&nvmem_layout_lock); - of_node_put(layout_np); - - return layout; -} - -static void nvmem_layout_put(struct nvmem_layout *layout) -{ - if (layout) - module_put(layout->owner); -} - -static int nvmem_add_cells_from_layout(struct nvmem_device *nvmem) -{ - struct nvmem_layout *layout = nvmem->layout; - int ret; - - if (layout && layout->add_cells) { - ret = layout->add_cells(&nvmem->dev, nvmem); - if (ret) - return ret; - } - - return 0; -} - -#if IS_ENABLED(CONFIG_OF) -struct device_node *of_nvmem_layout_get_container(struct nvmem_device *nvmem) -{ - return of_get_child_by_name(nvmem->dev.of_node, "nvmem-layout"); -} -EXPORT_SYMBOL_GPL(of_nvmem_layout_get_container); -#endif - const void *nvmem_layout_get_match_data(struct nvmem_device *nvmem, struct nvmem_layout *layout) { @@ -838,7 +760,7 @@ const void *nvmem_layout_get_match_data(struct nvmem_device *nvmem, const struct of_device_id *match; layout_np = of_nvmem_layout_get_container(nvmem); - match = of_match_node(layout->of_match_table, layout_np); + match = of_match_node(layout->dev.driver->of_match_table, layout_np); return match ? match->data : NULL; } @@ -950,19 +872,6 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) goto err_put_device; } - /* - * If the driver supplied a layout by config->layout, the module - * pointer will be NULL and nvmem_layout_put() will be a noop. - */ - nvmem->layout = config->layout ?: nvmem_layout_get(nvmem); - if (IS_ERR(nvmem->layout)) { - rval = PTR_ERR(nvmem->layout); - nvmem->layout = NULL; - - if (rval == -EPROBE_DEFER) - goto err_teardown_compat; - } - if (config->cells) { rval = nvmem_add_cells(nvmem, config->cells, config->ncells); if (rval) @@ -983,24 +892,24 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) if (rval) goto err_remove_cells; - rval = nvmem_add_cells_from_layout(nvmem); - if (rval) - goto err_remove_cells; - dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name); rval = device_add(&nvmem->dev); if (rval) goto err_remove_cells; + rval = nvmem_populate_layout(nvmem); + if (rval) + goto err_remove_dev; + blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem); return nvmem; +err_remove_dev: + device_del(&nvmem->dev); err_remove_cells: nvmem_device_remove_all_cells(nvmem); - nvmem_layout_put(nvmem->layout); -err_teardown_compat: if (config->compat) nvmem_sysfs_remove_compat(nvmem, config); err_put_device: @@ -1022,7 +931,7 @@ static void nvmem_device_release(struct kref *kref) device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom); nvmem_device_remove_all_cells(nvmem); - nvmem_layout_put(nvmem->layout); + nvmem_destroy_layout(nvmem); device_unregister(&nvmem->dev); } @@ -1342,6 +1251,24 @@ nvmem_find_cell_entry_by_node(struct nvmem_device *nvmem, struct device_node *np return cell; } +static int nvmem_layout_module_get_optional(struct nvmem_device *nvmem) +{ + if (!nvmem->layout) + return 0; + + if (!nvmem->layout->dev.driver || + !try_module_get(nvmem->layout->dev.driver->owner)) + return -EPROBE_DEFER; + + return 0; +} + +static void nvmem_layout_module_put(struct nvmem_device *nvmem) +{ + if (nvmem->layout && nvmem->layout->dev.driver) + module_put(nvmem->layout->dev.driver->owner); +} + /** * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id * @@ -1404,16 +1331,29 @@ struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id) return ERR_CAST(nvmem); } + ret = nvmem_layout_module_get_optional(nvmem); + if (ret) { + of_node_put(cell_np); + __nvmem_device_put(nvmem); + return ERR_PTR(ret); + } + cell_entry = nvmem_find_cell_entry_by_node(nvmem, cell_np); of_node_put(cell_np); if (!cell_entry) { __nvmem_device_put(nvmem); - return ERR_PTR(-ENOENT); + nvmem_layout_module_put(nvmem); + if (nvmem->layout) + return ERR_PTR(-EPROBE_DEFER); + else + return ERR_PTR(-ENOENT); } cell = nvmem_create_cell(cell_entry, id, cell_index); - if (IS_ERR(cell)) + if (IS_ERR(cell)) { __nvmem_device_put(nvmem); + nvmem_layout_module_put(nvmem); + } return cell; } @@ -1527,6 +1467,7 @@ void nvmem_cell_put(struct nvmem_cell *cell) kfree(cell); __nvmem_device_put(nvmem); + nvmem_layout_module_put(nvmem); } EXPORT_SYMBOL_GPL(nvmem_cell_put); @@ -2104,11 +2045,22 @@ EXPORT_SYMBOL_GPL(nvmem_dev_name); static int __init nvmem_init(void) { - return bus_register(&nvmem_bus_type); + int ret; + + ret = bus_register(&nvmem_bus_type); + if (ret) + return ret; + + ret = nvmem_layout_bus_register(); + if (ret) + bus_unregister(&nvmem_bus_type); + + return ret; } static void __exit nvmem_exit(void) { + nvmem_layout_bus_unregister(); bus_unregister(&nvmem_bus_type); } diff --git a/drivers/nvmem/internals.h b/drivers/nvmem/internals.h index 893553fbdf51..4946456c76c7 100644 --- a/drivers/nvmem/internals.h +++ b/drivers/nvmem/internals.h @@ -34,4 +34,25 @@ struct nvmem_device { void *priv; }; +#if IS_ENABLED(CONFIG_OF) +int nvmem_layout_bus_register(void); +void nvmem_layout_bus_unregister(void); +int nvmem_populate_layout(struct nvmem_device *nvmem); +void nvmem_destroy_layout(struct nvmem_device *nvmem); +#else /* CONFIG_OF */ +static inline int nvmem_layout_bus_register(void) +{ + return 0; +} + +static inline void nvmem_layout_bus_unregister(void) {} + +static inline int nvmem_populate_layout(struct nvmem_device *nvmem) +{ + return 0; +} + +static inline void nvmem_destroy_layout(struct nvmem_device *nvmem) { } +#endif /* CONFIG_OF */ + #endif /* ifndef _LINUX_NVMEM_INTERNALS_H */ diff --git a/drivers/nvmem/layouts.c b/drivers/nvmem/layouts.c new file mode 100644 index 000000000000..6a6aa58369ff --- /dev/null +++ b/drivers/nvmem/layouts.c @@ -0,0 +1,201 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * NVMEM layout bus handling + * + * Copyright (C) 2023 Bootlin + * Author: Miquel Raynal +#include +#include +#include +#include +#include +#include + +#include "internals.h" + +#define to_nvmem_layout_driver(drv) \ + (container_of((drv), struct nvmem_layout_driver, driver)) +#define to_nvmem_layout_device(_dev) \ + container_of((_dev), struct nvmem_layout, dev) + +static int nvmem_layout_bus_match(struct device *dev, struct device_driver *drv) +{ + return of_driver_match_device(dev, drv); +} + +static int nvmem_layout_bus_probe(struct device *dev) +{ + struct nvmem_layout_driver *drv = to_nvmem_layout_driver(dev->driver); + struct nvmem_layout *layout = to_nvmem_layout_device(dev); + + if (!drv->probe || !drv->remove) + return -EINVAL; + + return drv->probe(layout); +} + +static void nvmem_layout_bus_remove(struct device *dev) +{ + struct nvmem_layout_driver *drv = to_nvmem_layout_driver(dev->driver); + struct nvmem_layout *layout = to_nvmem_layout_device(dev); + + return drv->remove(layout); +} + +static struct bus_type nvmem_layout_bus_type = { + .name = "nvmem-layout", + .match = nvmem_layout_bus_match, + .probe = nvmem_layout_bus_probe, + .remove = nvmem_layout_bus_remove, +}; + +int nvmem_layout_driver_register(struct nvmem_layout_driver *drv) +{ + drv->driver.bus = &nvmem_layout_bus_type; + + return driver_register(&drv->driver); +} +EXPORT_SYMBOL_GPL(nvmem_layout_driver_register); + +void nvmem_layout_driver_unregister(struct nvmem_layout_driver *drv) +{ + driver_unregister(&drv->driver); +} +EXPORT_SYMBOL_GPL(nvmem_layout_driver_unregister); + +static void nvmem_layout_release_device(struct device *dev) +{ + struct nvmem_layout *layout = to_nvmem_layout_device(dev); + + of_node_put(layout->dev.of_node); + kfree(layout); +} + +static int nvmem_layout_create_device(struct nvmem_device *nvmem, + struct device_node *np) +{ + struct nvmem_layout *layout; + struct device *dev; + int ret; + + layout = kzalloc(sizeof(*layout), GFP_KERNEL); + if (!layout) + return -ENOMEM; + + /* Create a bidirectional link */ + layout->nvmem = nvmem; + nvmem->layout = layout; + + /* Device model registration */ + dev = &layout->dev; + device_initialize(dev); + dev->parent = &nvmem->dev; + dev->bus = &nvmem_layout_bus_type; + dev->release = nvmem_layout_release_device; + dev->coherent_dma_mask = DMA_BIT_MASK(32); + dev->dma_mask = &dev->coherent_dma_mask; + device_set_node(dev, of_fwnode_handle(of_node_get(np))); + of_device_make_bus_id(dev); + of_msi_configure(dev, dev->of_node); + + ret = device_add(dev); + if (ret) { + put_device(dev); + return ret; + } + + return 0; +} + +static const struct of_device_id of_nvmem_layout_skip_table[] = { + { .compatible = "fixed-layout", }, + {} +}; + +static int nvmem_layout_bus_populate(struct nvmem_device *nvmem, + struct device_node *layout_dn) +{ + int ret; + + /* Make sure it has a compatible property */ + if (!of_get_property(layout_dn, "compatible", NULL)) { + pr_debug("%s() - skipping %pOF, no compatible prop\n", + __func__, layout_dn); + return 0; + } + + /* Fixed layouts are parsed manually somewhere else for now */ + if (of_match_node(of_nvmem_layout_skip_table, layout_dn)) { + pr_debug("%s() - skipping %pOF node\n", __func__, layout_dn); + return 0; + } + + if (of_node_check_flag(layout_dn, OF_POPULATED_BUS)) { + pr_debug("%s() - skipping %pOF, already populated\n", + __func__, layout_dn); + + return 0; + } + + /* NVMEM layout buses expect only a single device representing the layout */ + ret = nvmem_layout_create_device(nvmem, layout_dn); + if (ret) + return ret; + + of_node_set_flag(layout_dn, OF_POPULATED_BUS); + + return 0; +} + +struct device_node *of_nvmem_layout_get_container(struct nvmem_device *nvmem) +{ + return of_get_child_by_name(nvmem->dev.of_node, "nvmem-layout"); +} +EXPORT_SYMBOL_GPL(of_nvmem_layout_get_container); + +/* + * Returns the number of devices populated, 0 if the operation was not relevant + * for this nvmem device, an error code otherwise. + */ +int nvmem_populate_layout(struct nvmem_device *nvmem) +{ + struct device_node *layout_dn; + int ret; + + layout_dn = of_nvmem_layout_get_container(nvmem); + if (!layout_dn) + return 0; + + /* Populate the layout device */ + device_links_supplier_sync_state_pause(); + ret = nvmem_layout_bus_populate(nvmem, layout_dn); + device_links_supplier_sync_state_resume(); + + of_node_put(layout_dn); + return ret; +} + +void nvmem_destroy_layout(struct nvmem_device *nvmem) +{ + struct device *dev; + + if (!nvmem->layout) + return; + + dev = &nvmem->layout->dev; + of_node_clear_flag(dev->of_node, OF_POPULATED_BUS); + device_unregister(dev); +} + +int nvmem_layout_bus_register(void) +{ + return bus_register(&nvmem_layout_bus_type); +} + +void nvmem_layout_bus_unregister(void) +{ + bus_unregister(&nvmem_layout_bus_type); +} diff --git a/drivers/nvmem/layouts/Kconfig b/drivers/nvmem/layouts/Kconfig index 7ff1ee1c1f05..9c6e672fc350 100644 --- a/drivers/nvmem/layouts/Kconfig +++ b/drivers/nvmem/layouts/Kconfig @@ -1,5 +1,11 @@ # SPDX-License-Identifier: GPL-2.0 +config NVMEM_LAYOUTS + bool + depends on OF + +if NVMEM_LAYOUTS + menu "Layout Types" config NVMEM_LAYOUT_SL28_VPD @@ -21,3 +27,5 @@ config NVMEM_LAYOUT_ONIE_TLV If unsure, say N. endmenu + +endif diff --git a/drivers/nvmem/layouts/onie-tlv.c b/drivers/nvmem/layouts/onie-tlv.c index defd42d4375c..b24cc5dcc6ee 100644 --- a/drivers/nvmem/layouts/onie-tlv.c +++ b/drivers/nvmem/layouts/onie-tlv.c @@ -225,16 +225,32 @@ static int onie_tlv_parse_table(struct device *dev, struct nvmem_device *nvmem) return 0; } +static int onie_tlv_probe(struct nvmem_layout *layout) +{ + layout->add_cells = onie_tlv_parse_table; + + return nvmem_layout_register(layout); +} + +static void onie_tlv_remove(struct nvmem_layout *layout) +{ + nvmem_layout_unregister(layout); +} + static const struct of_device_id onie_tlv_of_match_table[] = { { .compatible = "onie,tlv-layout", }, {}, }; MODULE_DEVICE_TABLE(of, onie_tlv_of_match_table); -static struct nvmem_layout onie_tlv_layout = { - .name = "ONIE tlv layout", - .of_match_table = onie_tlv_of_match_table, - .add_cells = onie_tlv_parse_table, +static struct nvmem_layout_driver onie_tlv_layout = { + .driver = { + .owner = THIS_MODULE, + .name = "onie-tlv-layout", + .of_match_table = onie_tlv_of_match_table, + }, + .probe = onie_tlv_probe, + .remove = onie_tlv_remove, }; module_nvmem_layout_driver(onie_tlv_layout); diff --git a/drivers/nvmem/layouts/sl28vpd.c b/drivers/nvmem/layouts/sl28vpd.c index 26c7cf21b523..b8ffae646cc2 100644 --- a/drivers/nvmem/layouts/sl28vpd.c +++ b/drivers/nvmem/layouts/sl28vpd.c @@ -134,16 +134,32 @@ static int sl28vpd_add_cells(struct device *dev, struct nvmem_device *nvmem) return 0; } +static int sl28vpd_probe(struct nvmem_layout *layout) +{ + layout->add_cells = sl28vpd_add_cells; + + return nvmem_layout_register(layout); +} + +static void sl28vpd_remove(struct nvmem_layout *layout) +{ + nvmem_layout_unregister(layout); +} + static const struct of_device_id sl28vpd_of_match_table[] = { { .compatible = "kontron,sl28-vpd" }, {}, }; MODULE_DEVICE_TABLE(of, sl28vpd_of_match_table); -static struct nvmem_layout sl28vpd_layout = { - .name = "sl28-vpd", - .of_match_table = sl28vpd_of_match_table, - .add_cells = sl28vpd_add_cells, +static struct nvmem_layout_driver sl28vpd_layout = { + .driver = { + .owner = THIS_MODULE, + .name = "kontron-sl28vpd-layout", + .of_match_table = sl28vpd_of_match_table, + }, + .probe = sl28vpd_probe, + .remove = sl28vpd_remove, }; module_nvmem_layout_driver(sl28vpd_layout); diff --git a/include/linux/nvmem-provider.h b/include/linux/nvmem-provider.h index 36415a602d9e..6fe65b35ea97 100644 --- a/include/linux/nvmem-provider.h +++ b/include/linux/nvmem-provider.h @@ -9,6 +9,7 @@ #ifndef _LINUX_NVMEM_PROVIDER_H #define _LINUX_NVMEM_PROVIDER_H +#include #include #include #include @@ -158,12 +159,11 @@ struct nvmem_cell_table { /** * struct nvmem_layout - NVMEM layout definitions * - * @name: Layout name. - * @of_match_table: Open firmware match table. - * @add_cells: Called to populate the layout using - * nvmem_add_one_cell(). - * @owner: Pointer to struct module. - * @node: List node. + * @dev: Device-model layout device. + * @nvmem: The underlying NVMEM device + * @add_cells: Will be called if a nvmem device is found which + * has this layout. The function will add layout + * specific cells with nvmem_add_one_cell(). * * A nvmem device can hold a well defined structure which can just be * evaluated during runtime. For example a TLV list, or a list of "name=val" @@ -171,13 +171,15 @@ struct nvmem_cell_table { * cells. */ struct nvmem_layout { - const char *name; - const struct of_device_id *of_match_table; + struct device dev; + struct nvmem_device *nvmem; int (*add_cells)(struct device *dev, struct nvmem_device *nvmem); +}; - /* private */ - struct module *owner; - struct list_head node; +struct nvmem_layout_driver { + struct device_driver driver; + int (*probe)(struct nvmem_layout *layout); + void (*remove)(struct nvmem_layout *layout); }; #if IS_ENABLED(CONFIG_NVMEM) @@ -194,11 +196,15 @@ void nvmem_del_cell_table(struct nvmem_cell_table *table); int nvmem_add_one_cell(struct nvmem_device *nvmem, const struct nvmem_cell_info *info); -int __nvmem_layout_register(struct nvmem_layout *layout, struct module *owner); -#define nvmem_layout_register(layout) \ - __nvmem_layout_register(layout, THIS_MODULE) +int nvmem_layout_register(struct nvmem_layout *layout); void nvmem_layout_unregister(struct nvmem_layout *layout); +int nvmem_layout_driver_register(struct nvmem_layout_driver *drv); +void nvmem_layout_driver_unregister(struct nvmem_layout_driver *drv); +#define module_nvmem_layout_driver(__nvmem_layout_driver) \ + module_driver(__nvmem_layout_driver, nvmem_layout_driver_register, \ + nvmem_layout_driver_unregister) + const void *nvmem_layout_get_match_data(struct nvmem_device *nvmem, struct nvmem_layout *layout); @@ -262,8 +268,4 @@ static inline struct device_node *of_nvmem_layout_get_container(struct nvmem_dev #endif /* CONFIG_NVMEM && CONFIG_OF */ -#define module_nvmem_layout_driver(__layout_driver) \ - module_driver(__layout_driver, nvmem_layout_register, \ - nvmem_layout_unregister) - #endif /* ifndef _LINUX_NVMEM_PROVIDER_H */ From patchwork Wed Nov 29 16:37:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 171494 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a5a7:0:b0:403:3b70:6f57 with SMTP id d7csp463877vqn; Wed, 29 Nov 2023 08:38:18 -0800 (PST) X-Google-Smtp-Source: AGHT+IF2JhTrmx6Juk51izQeE4StgiQY6XbCNLywU3tvu0/tS2rFVNoYcPI+zFMRby93FzQg1qSI X-Received: by 2002:a05:6808:cc:b0:3b8:93c7:f7b0 with SMTP id t12-20020a05680800cc00b003b893c7f7b0mr3490101oic.52.1701275897931; Wed, 29 Nov 2023 08:38:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701275897; cv=none; d=google.com; s=arc-20160816; b=n0iU+2yeXtX4GPcG1MXjRijRhCrJ+XJScEDy40deML8I5Z/I/eZgxvpGxcDwo7Aq55 WJk3N9SI1jxe37OlV9uSMcvnQb9JDTmDANvNaXI/svHXseJ8/fnqMcmRs3sXnL+fDlZ/ pB9tH/dxnxMdFv3EifEdWlH2h6RL+cEdXddIEFo8fdtScFORpHESX+Br+xaLZpsMuI9C NdG8Qi2kzZ/Buz/w7hGx9TIQkS3qgL+W3pM6SKB4qrVPSZkvjcyDWiaHsB7NSmPo/nSO 8E4TC+A81bz1YdTGmwAbjbasysCImc+Z4fr4wwieCU4mNem5dGLsIhUNKGWiqMVpVmxq 5tUg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=dfOp8t/SWXoAMbVxsmdWr6p/Yay0CylcPPBrARczmbM=; fh=LHVfHqkxtSfhDK3Auz+HtR9CGNiwZCQHjp1a868hmsY=; b=F+lWu1bG1zyLXScTHg9epEovynBiiE99LT/zpJCCQIEOvl19Jy31PyL4z+e3Gi18lB sQ1tGDCuUt/2AgdTQO5Sy8wEZiUFNuPLWNmJXY608JF0FidmMEb8Vm7rZRupRq4YxnS6 rqt2I4VGUxQofxjyc1TJe++j953kBg4+Y7pKret3YUVp1lx6w+IqLv86k0O5yNMLaW+n CpP4GUg4ubZg4WTHrYhvXg3ILqrmau83syla17lsTHKoQx1VA08qo0IPqqMfRhVHTWOb wIEx1VNGLpdqHsxGg0/PAwIpctxnDC6Ld5BJszVB9MuSgKRnsXFL//zoJosQsTNfyZgg xIKw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=glZ3NGop; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id s6-20020a63e806000000b0058afac51c12si15108576pgh.545.2023.11.29.08.38.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Nov 2023 08:38:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=glZ3NGop; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 1FD8B802FA7C; Wed, 29 Nov 2023 08:38:16 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231483AbjK2QiA (ORCPT + 99 others); Wed, 29 Nov 2023 11:38:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34142 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230480AbjK2Qhk (ORCPT ); Wed, 29 Nov 2023 11:37:40 -0500 Received: from relay6-d.mail.gandi.net (relay6-d.mail.gandi.net [IPv6:2001:4b98:dc4:8::226]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0237CD68 for ; Wed, 29 Nov 2023 08:37:45 -0800 (PST) Received: by mail.gandi.net (Postfix) with ESMTPSA id 0A45CC000A; Wed, 29 Nov 2023 16:37:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1701275864; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=dfOp8t/SWXoAMbVxsmdWr6p/Yay0CylcPPBrARczmbM=; b=glZ3NGop0Vb3y9yDIWz+FYhMiWC3A2/Qs+sB8aCJs4shCfLbbrdzLgFlBZH7U4NCP2hH3M uiZ7W0l6QlITXpQFguGB5TyBLybUtGZbbUqAkCoT5J7rWMYJJl+a0gUENQh4FCa0r/9vUh FMUxpF7jYg3nbT3QDvPlKRiZZxV+sbAqZYhdqy++kyaDRsQW1K9/bQ6MLpQxiFrVz7k7ZV eNeI2ngePFYnurOdtxBmZZoVnMIYiLC+lmYD9SskT8jed12thZJHAgh+MIEQpu4FXroxUt aosXNCM4qOIuMlQWoDnacpGUaIYNzPd6X6GgTLTLUlMrJpoJr/ieB6dqryEiEw== From: Miquel Raynal To: Srinivas Kandagatla , Greg Kroah-Hartman , Cc: Thomas Petazzoni , Robert Marko , Luca Ceresoli , Michael Walle , =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= , Marco Felsch , Randy Dunlap , Chen-Yu Tsai , Daniel Golle , Miquel Raynal Subject: [PATCH v14 7/8] ABI: sysfs-nvmem-cells: Expose cells through sysfs Date: Wed, 29 Nov 2023 17:37:36 +0100 Message-Id: <20231129163737.698317-8-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231129163737.698317-1-miquel.raynal@bootlin.com> References: <20231129163737.698317-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 X-GND-Sasl: miquel.raynal@bootlin.com X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_PASS,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 29 Nov 2023 08:38:16 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783917075876635904 X-GMAIL-MSGID: 1783917075876635904 The binary content of nvmem devices is available to the user so in the easiest cases, finding the content of a cell is rather easy as it is just a matter of looking at a known and fixed offset. However, nvmem layouts have been recently introduced to cope with more advanced situations, where the offset and size of the cells is not known in advance or is dynamic. When using layouts, more advanced parsers are used by the kernel in order to give direct access to the content of each cell regardless of their position/size in the underlying device, but these information were not accessible to the user. By exposing the nvmem cells to the user through a dedicated cell/ folder containing one file per cell, we provide a straightforward access to useful user information without the need for re-writing a userland parser. Content of nvmem cells is usually: product names, manufacturing date, MAC addresses, etc, Signed-off-by: Miquel Raynal Reviewed-by: Greg Kroah-Hartman --- Documentation/ABI/testing/sysfs-nvmem-cells | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-nvmem-cells diff --git a/Documentation/ABI/testing/sysfs-nvmem-cells b/Documentation/ABI/testing/sysfs-nvmem-cells new file mode 100644 index 000000000000..7af70adf3690 --- /dev/null +++ b/Documentation/ABI/testing/sysfs-nvmem-cells @@ -0,0 +1,21 @@ +What: /sys/bus/nvmem/devices/.../cells/ +Date: May 2023 +KernelVersion: 6.5 +Contact: Miquel Raynal +Description: + The "cells" folder contains one file per cell exposed by the + NVMEM device. The name of the file is: @, with + being the cell name and its location in the NVMEM + device, in hexadecimal (without the '0x' prefix, to mimic device + tree node names). The length of the file is the size of the cell + (when known). The content of the file is the binary content of + the cell (may sometimes be ASCII, likely without trailing + character). + Note: This file is only present if CONFIG_NVMEM_SYSFS + is enabled. + + Example:: + + hexdump -C /sys/bus/nvmem/devices/1-00563/cells/product-name@d + 00000000 54 4e 34 38 4d 2d 50 2d 44 4e |TN48M-P-DN| + 0000000a From patchwork Wed Nov 29 16:37:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 171496 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a5a7:0:b0:403:3b70:6f57 with SMTP id d7csp464298vqn; Wed, 29 Nov 2023 08:39:00 -0800 (PST) X-Google-Smtp-Source: AGHT+IE9JquCRRVgWMfWSLjvi7DOZTc1trbO6oZ30NJkxSx3hHfx6DWOZm6+kobg6fadZ4VT4sCf X-Received: by 2002:a17:90b:1bc8:b0:285:d550:997e with SMTP id oa8-20020a17090b1bc800b00285d550997emr10856814pjb.11.1701275940365; Wed, 29 Nov 2023 08:39:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701275940; cv=none; d=google.com; s=arc-20160816; b=lIKsgyI61vTOxPE0jYQwMmLr1e8H0snYqYCjttPdTxBGc6EebSvutiIY/g9WJVF3op Dvgjz0XZzK3kG7QGG0PNyN3qaoUZP/X77kF4sV8CbzhkV7ex/xSe+at/mXUJtqzZJp6O EoXvQ/Kjxg3ArBk2CfVClOVcaEgDNoGpcCggOLBL8mG8Z4ZlQbsU3J4etOd+OPYjjgBp n71HG46qoQc91c4xHID5e+j7J8fA5J4eXZB3zQJ2z3hmprg8pItZU2mRoglrxt8Lcsbf YtUIjbFzhqw1atBcPvOM8paEPHC//eDmqygpMtkJcBGs0qzqRMPDFOvCKEIAeXXr1mjh lXTw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=0FsAS2J1GPD2iJAFPs2AK3nrQuUf0UM1rynbZnwsGZA=; fh=LHVfHqkxtSfhDK3Auz+HtR9CGNiwZCQHjp1a868hmsY=; b=WIV1L7GPT8vw4REJ2oO7UDT+u8gj+xIoG7icoKf8TpCSKpXLyGU7vdhfRDKAQZW6HK +YhTyHjaU0MCEifZbIlbauW1YGZ2HY4FDfE9rz3zuCaMLkKCJtmw4YIVPeoWgfiT0hNE Rlgsu0+mJti4ksIYBOzHrE9AjzGAwgkopvMc0wr2Y7SGGmtGZ6cEz7T1CUZ70B+xL/9o 08dmWt2WZGJMMNFuence0tHQaBsT9L5R91oPGxiQ28LRxWtbSlviP1r2NmgJMB51/GTv GSxagrR4wLNcd9eCLCdzNcjuX8Wd87Zz1Fzn8fU5yG7Y7hM2hZO4koFxP9m5px3eITTM tZpQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=XHwiSS8n; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id jo6-20020a170903054600b001cfa7f91403si11556166plb.183.2023.11.29.08.38.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Nov 2023 08:39:00 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=XHwiSS8n; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id EAFD380BE09D; Wed, 29 Nov 2023 08:38:56 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231490AbjK2QiE (ORCPT + 99 others); Wed, 29 Nov 2023 11:38:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48258 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230501AbjK2Qhn (ORCPT ); Wed, 29 Nov 2023 11:37:43 -0500 Received: from relay6-d.mail.gandi.net (relay6-d.mail.gandi.net [IPv6:2001:4b98:dc4:8::226]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A92AED6E for ; Wed, 29 Nov 2023 08:37:46 -0800 (PST) Received: by mail.gandi.net (Postfix) with ESMTPSA id B8B16C0010; Wed, 29 Nov 2023 16:37:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1701275865; 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=0FsAS2J1GPD2iJAFPs2AK3nrQuUf0UM1rynbZnwsGZA=; b=XHwiSS8nW0BZ+swzdOcjpmXtNgdE+zOy2QRTYOvUc8dVX7KaE7q1vmy0L7iBZfwSYw+l07 xQFMUPPmTL/SsBiKXVgiL7OlqUUR/bB9TqqZqKIB5LGX+uY606XUFvmaKgypPVdJPZm8gD czsLWWucRIC+ww1pl3Bs7WFMl4ZVpwheD6BQMrFmbM2O+ALWVDJ0D9Jeq5BUWg1K6Jc4/g 6EPoBcms1zoGKAQjuO7rr95t7JVnVbH1a8E/4QkwVT3EX0vc8eHOqnm9jaJX11OUgB9jh+ M6blGO/w/TNEF82pvSBJYQ8At9+xWqs6LrfOis59n/a7Q8fLHvAato9muz3p3Q== From: Miquel Raynal To: Srinivas Kandagatla , Greg Kroah-Hartman , Cc: Thomas Petazzoni , Robert Marko , Luca Ceresoli , Michael Walle , =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= , Marco Felsch , Randy Dunlap , Chen-Yu Tsai , Daniel Golle , Miquel Raynal Subject: [PATCH v14 8/8] nvmem: core: Expose cells through sysfs Date: Wed, 29 Nov 2023 17:37:37 +0100 Message-Id: <20231129163737.698317-9-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231129163737.698317-1-miquel.raynal@bootlin.com> References: <20231129163737.698317-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 X-GND-Sasl: miquel.raynal@bootlin.com X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Wed, 29 Nov 2023 08:38:57 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783917120076386602 X-GMAIL-MSGID: 1783917120076386602 The binary content of nvmem devices is available to the user so in the easiest cases, finding the content of a cell is rather easy as it is just a matter of looking at a known and fixed offset. However, nvmem layouts have been recently introduced to cope with more advanced situations, where the offset and size of the cells is not known in advance or is dynamic. When using layouts, more advanced parsers are used by the kernel in order to give direct access to the content of each cell, regardless of its position/size in the underlying device. Unfortunately, these information are not accessible by users, unless by fully re-implementing the parser logic in userland. Let's expose the cells and their content through sysfs to avoid these situations. Of course the relevant NVMEM sysfs Kconfig option must be enabled for this support to be available. Not all nvmem devices expose cells. Indeed, the .bin_attrs attribute group member will be filled at runtime only when relevant and will remain empty otherwise. In this case, as the cells attribute group will be empty, it will not lead to any additional folder/file creation. Exposed cells are read-only. There is, in practice, everything in the core to support a write path, but as I don't see any need for that, I prefer to keep the interface simple (and probably safer). The interface is documented as being in the "testing" state which means we can later add a write attribute if though relevant. Signed-off-by: Miquel Raynal Tested-by: Rafał Miłecki Tested-by: Chen-Yu Tsai --- drivers/nvmem/core.c | 135 +++++++++++++++++++++++++++++++++++++- drivers/nvmem/internals.h | 1 + 2 files changed, 135 insertions(+), 1 deletion(-) diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index 1e1651be707b..9fc452e8ada8 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -299,6 +299,43 @@ static umode_t nvmem_bin_attr_is_visible(struct kobject *kobj, return nvmem_bin_attr_get_umode(nvmem); } +static struct nvmem_cell *nvmem_create_cell(struct nvmem_cell_entry *entry, + const char *id, int index); + +static ssize_t nvmem_cell_attr_read(struct file *filp, struct kobject *kobj, + struct bin_attribute *attr, char *buf, + loff_t pos, size_t count) +{ + struct nvmem_cell_entry *entry; + struct nvmem_cell *cell = NULL; + size_t cell_sz, read_len; + void *content; + + entry = attr->private; + cell = nvmem_create_cell(entry, entry->name, 0); + if (IS_ERR(cell)) + return PTR_ERR(cell); + + if (!cell) + return -EINVAL; + + content = nvmem_cell_read(cell, &cell_sz); + if (IS_ERR(content)) { + read_len = PTR_ERR(content); + goto destroy_cell; + } + + read_len = min_t(unsigned int, cell_sz - pos, count); + memcpy(buf, content + pos, read_len); + kfree(content); + +destroy_cell: + kfree_const(cell->id); + kfree(cell); + + return read_len; +} + /* default read/write permissions */ static struct bin_attribute bin_attr_rw_nvmem = { .attr = { @@ -320,11 +357,21 @@ static const struct attribute_group nvmem_bin_group = { .is_bin_visible = nvmem_bin_attr_is_visible, }; +/* Cell attributes will be dynamically allocated */ +static struct attribute_group nvmem_cells_group = { + .name = "cells", +}; + static const struct attribute_group *nvmem_dev_groups[] = { &nvmem_bin_group, NULL, }; +static const struct attribute_group *nvmem_cells_groups[] = { + &nvmem_cells_group, + NULL, +}; + static struct bin_attribute bin_attr_nvmem_eeprom_compat = { .attr = { .name = "eeprom", @@ -380,6 +427,68 @@ static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem, device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom); } +static int nvmem_populate_sysfs_cells(struct nvmem_device *nvmem) +{ + struct bin_attribute **cells_attrs, *attrs; + struct nvmem_cell_entry *entry; + unsigned int ncells = 0, i = 0; + int ret = 0; + + mutex_lock(&nvmem_mutex); + + if (list_empty(&nvmem->cells) || nvmem->sysfs_cells_populated) { + nvmem_cells_group.bin_attrs = NULL; + goto unlock_mutex; + } + + /* Allocate an array of attributes with a sentinel */ + ncells = list_count_nodes(&nvmem->cells); + cells_attrs = devm_kcalloc(&nvmem->dev, ncells + 1, + sizeof(struct bin_attribute *), GFP_KERNEL); + if (!cells_attrs) { + ret = -ENOMEM; + goto unlock_mutex; + } + + attrs = devm_kcalloc(&nvmem->dev, ncells, sizeof(struct bin_attribute), GFP_KERNEL); + if (!attrs) { + ret = -ENOMEM; + goto unlock_mutex; + } + + /* Initialize each attribute to take the name and size of the cell */ + list_for_each_entry(entry, &nvmem->cells, node) { + sysfs_bin_attr_init(&attrs[i]); + attrs[i].attr.name = devm_kasprintf(&nvmem->dev, GFP_KERNEL, + "%s@%x", entry->name, + entry->offset); + attrs[i].attr.mode = 0444; + attrs[i].size = entry->bytes; + attrs[i].read = &nvmem_cell_attr_read; + attrs[i].private = entry; + if (!attrs[i].attr.name) { + ret = -ENOMEM; + goto unlock_mutex; + } + + cells_attrs[i] = &attrs[i]; + i++; + } + + nvmem_cells_group.bin_attrs = cells_attrs; + + ret = devm_device_add_groups(&nvmem->dev, nvmem_cells_groups); + if (ret) + goto unlock_mutex; + + nvmem->sysfs_cells_populated = true; + +unlock_mutex: + mutex_unlock(&nvmem_mutex); + + return ret; +} + #else /* CONFIG_NVMEM_SYSFS */ static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem, @@ -739,11 +848,25 @@ static int nvmem_add_cells_from_fixed_layout(struct nvmem_device *nvmem) int nvmem_layout_register(struct nvmem_layout *layout) { + int ret; + if (!layout->add_cells) return -EINVAL; /* Populate the cells */ - return layout->add_cells(&layout->nvmem->dev, layout->nvmem); + ret = layout->add_cells(&layout->nvmem->dev, layout->nvmem); + if (ret) + return ret; + +#ifdef CONFIG_NVMEM_SYSFS + ret = nvmem_populate_sysfs_cells(layout->nvmem); + if (ret) { + nvmem_device_remove_all_cells(layout->nvmem); + return ret; + } +#endif + + return 0; } EXPORT_SYMBOL_GPL(nvmem_layout_register); @@ -902,10 +1025,20 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) if (rval) goto err_remove_dev; +#ifdef CONFIG_NVMEM_SYSFS + rval = nvmem_populate_sysfs_cells(nvmem); + if (rval) + goto err_destroy_layout; +#endif + blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem); return nvmem; +#ifdef CONFIG_NVMEM_SYSFS +err_destroy_layout: + nvmem_destroy_layout(nvmem); +#endif err_remove_dev: device_del(&nvmem->dev); err_remove_cells: diff --git a/drivers/nvmem/internals.h b/drivers/nvmem/internals.h index 4946456c76c7..18fed57270e5 100644 --- a/drivers/nvmem/internals.h +++ b/drivers/nvmem/internals.h @@ -32,6 +32,7 @@ struct nvmem_device { struct gpio_desc *wp_gpio; struct nvmem_layout *layout; void *priv; + bool sysfs_cells_populated; }; #if IS_ENABLED(CONFIG_OF)