From patchwork Wed Oct 4 22:22: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: 148607 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:254a:b0:403:3b70:6f57 with SMTP id hf10csp437078vqb; Wed, 4 Oct 2023 15:22:55 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFKDCJU5jXma7UwXN3oiJhwNB0khBNDrrd+V3T4s2jr5aU033feAauq1voBSP6pCNgiNF6n X-Received: by 2002:a05:6a21:71c1:b0:14c:d5d8:9fed with SMTP id ay1-20020a056a2171c100b0014cd5d89fedmr3525984pzc.54.1696458175658; Wed, 04 Oct 2023 15:22:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696458175; cv=none; d=google.com; s=arc-20160816; b=p9l/+YMGYF5IPo1AqDJUsM9MxbGcymR20O7RG9h8KL8oevnNxmPvEqvgDTcTC6AXqR BLh+X3XHpF4eKnWZebXqnDlyPPyrV9EKF92kV4K7fFrYqiNJcYeP+YjoYXC17/YxFEJp asJqkEdYGRLvvxBbFx/Meqx8g5P1G3asBUG26xkkOD5dxkFfhIn/RwNzqdlr307+O5NU v5uhSatpPUK9pzmQiRiwEtrww1C6i6EaDDCItAD0auJWiACeN7enX3pQPS5b5EIxzaCV eb4RnyW8Azm2dsCGhLkZ1liuobKq309aZRhsKFKCRvAzlQAzFLiGruK2+mSOarVstzaT TYFA== 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=ek8z6qq1VdvF44JDUEP0S5+gwXqIdO59LstVSiFw/0A=; fh=XQzgxiPMj/Cqsom9sWl23DkvcXkGD6r/vx0NuuBn5R0=; b=ZsBSSRMuE1a9mINFXd2UHd1Uh3DM3NVEaDRKnw96FvVCUZn3kt8isncO1psc9UcLmZ yywuQj3EqfOQtoEqS910XjG4iPpnbShG+KLW4zdegke7oSoh8h8ftWWIrDLIc9OP/j5R Xunj792kn6veVqKjdJ+RurPxkTT4YFc0FMDajJKD5EO2bL5QLYUd1oqQ0ax25URZLFSx 4KZzZuCQebjesH/Lo2Q+ocIUUCGHW4YOtj6XCL53CHdxE7sJTE7PF5jctVmTSZQo2mZs 6zXajYAaTKAWIEwEamePut9ZZTr98neWYnK1rydgcm6/Gs/3x/Mr5zzoJrgzqUMqBmF6 NF8g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=BPiBIONn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 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 howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id ca24-20020a056a00419800b0068c0300a3f7si47235pfb.279.2023.10.04.15.22.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Oct 2023 15:22:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=BPiBIONn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 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 howler.vger.email (Postfix) with ESMTP id E6D8C83B00B8; Wed, 4 Oct 2023 15:22:54 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233476AbjJDWWu (ORCPT + 19 others); Wed, 4 Oct 2023 18:22:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54918 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231148AbjJDWWt (ORCPT ); Wed, 4 Oct 2023 18:22:49 -0400 Received: from relay5-d.mail.gandi.net (relay5-d.mail.gandi.net [IPv6:2001:4b98:dc4:8::225]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 218AAC6; Wed, 4 Oct 2023 15:22:44 -0700 (PDT) Received: by mail.gandi.net (Postfix) with ESMTPSA id 94E6E1C0003; Wed, 4 Oct 2023 22:22:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1696458163; 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=ek8z6qq1VdvF44JDUEP0S5+gwXqIdO59LstVSiFw/0A=; b=BPiBIONnJ4kwPMVYv2jbJi/LNkcG9Ev49Vo6odDIAP8o7aPJscIu3fIPYGXSpsGEWwrjoq CcwIfOHnbIlD99ahsGEEkbQkl32iulIEmpUtbkBxvWsNu1TGVhCjtAUeBDqEJv8tFE15ly 8GCIHBv+ywYfGugBT12FDspJMw0EQRw9caJR+lWkzBF6FuY3tWVKZJPlQJfFkscXGpXzm2 cfgPscTeeb92lsXlrzwaMIRtBozAdKnsWKjQXAFO0obsEvI6IlLqZLuDLSdNSxZirVC2Rb fpJ1AIkDin0AgBKstjX8TUQkz+3g1Xz2Yc/4Z/x85p8euS4DelholIiyMJ43Iw== From: Miquel Raynal To: Srinivas Kandagatla , Greg Kroah-Hartman Cc: Michael Walle , =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= , Rob Herring , Frank Rowand , devicetree@vger.kernel.org, , Robert Marko , Thomas Petazzoni , Luka Perkov , Randy Dunlap , Chen-Yu Tsai , Daniel Golle , Miquel Raynal Subject: [PATCH v11 1/7] of: device: Export of_device_make_bus_id() Date: Thu, 5 Oct 2023 00:22:30 +0200 Message-Id: <20231004222236.411248-2-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231004222236.411248-1-miquel.raynal@bootlin.com> References: <20231004222236.411248-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 X-GND-Sasl: miquel.raynal@bootlin.com X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,SPF_HELO_PASS, SPF_PASS,URIBL_BLOCKED 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 (howler.vger.email [0.0.0.0]); Wed, 04 Oct 2023 15:22:55 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778865328130511836 X-GMAIL-MSGID: 1778865328130511836 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 --- 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 f235ab55b91e..be32e28c6f55 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 Oct 4 22:22: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: 148609 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:254a:b0:403:3b70:6f57 with SMTP id hf10csp437169vqb; Wed, 4 Oct 2023 15:23:07 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEoMA502O13Ib3FKD40jUI+03+xdRimc2puxO1JmnouiB9OeJSHo9wLNKngcfefvuRHpxpy X-Received: by 2002:a05:6830:1349:b0:6bc:9799:a00c with SMTP id r9-20020a056830134900b006bc9799a00cmr3254101otq.26.1696458187383; Wed, 04 Oct 2023 15:23:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696458187; cv=none; d=google.com; s=arc-20160816; b=tmouqSmF0OS3LkkY5BtvvNryKC/kY7wgSXKlVeE1eD2WB7DJdkTI2PNIMD332s+sx0 sxy8pdByAtbXHLcY/jzRCUy2EswTVJ/EI2uyLyc5H1XpsPrT+0rvj+JRG8ptxe7qKBvR HSuVMZ95IRNBG3BdkUVPpt1LmnCqQV7b/10iS/VS1NYGUfkkejKmYRQDqxyTWzajb41x xisu0ltxSeOLS3hXCLGEBj8c9Rv1rdbdm6L5xU7E6WL+dyc32C2KJ3B3TsRXhMD2+t9e eMcqjUBGdUxHDhHY34+qub8AdQ3IiM4dzx+SdoHzartk/fgUhS41SSdF529nH1EblSGm pRnA== 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=ozTgRpH7t0hBveOF8yXGnBl2vT15HtovIkhQFtR9B74=; fh=XQzgxiPMj/Cqsom9sWl23DkvcXkGD6r/vx0NuuBn5R0=; b=SVnjZhF6KIeiZ/aQ+JeFgdfkubnDPfLfPiQEcHFSHxF4znMk2GAv3rBY26zbozYqQj mWkPnQBsq7nEfyLDP2SaTrtExEUXRU5chA+doCvd1fZwGV3rVjctLobUN/pDZbQ9R3Hm FfS3CaT+qGaKwCilFY1LdEMpj5kJ3cdNvT17VaZzTrFG/zxYPMNM98qBLi/1kbusVxwh TfcTa0M2vE4klpWETpe0IChEvzyU93nhFlq0Qh2IC2SL1Cc+vD/jhlvz6F2yGVKShbui rh0Rp1bTYWo86QaHV5RFO89a2W9Q4fmN9Akpl/Ffni6h/ngMYYN90rbJk4pP2ylmRiOJ 9BAA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=cQd6LjNl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id z16-20020a656650000000b00577f4d85fdbsi77694pgv.548.2023.10.04.15.23.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Oct 2023 15:23:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=cQd6LjNl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 howler.vger.email (Postfix) with ESMTP id 6C2DB83B6BC7; Wed, 4 Oct 2023 15:23:06 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235193AbjJDWXA (ORCPT + 19 others); Wed, 4 Oct 2023 18:23:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54938 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231148AbjJDWWv (ORCPT ); Wed, 4 Oct 2023 18:22:51 -0400 Received: from relay5-d.mail.gandi.net (relay5-d.mail.gandi.net [IPv6:2001:4b98:dc4:8::225]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 84554D8; Wed, 4 Oct 2023 15:22:47 -0700 (PDT) Received: by mail.gandi.net (Postfix) with ESMTPSA id EF33E1C0008; Wed, 4 Oct 2023 22:22:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1696458166; 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=ozTgRpH7t0hBveOF8yXGnBl2vT15HtovIkhQFtR9B74=; b=cQd6LjNlsF+BeoUk5ZXFpdCXxeLsrgtit+isxZblQAIm+u0fis071wiB28G/L1Zz1WDzQo V49sfOGDgm9EasASWJkh35SANIbV2rLjPSOpUw4VBSaRChVqqQ/gSSvEpHG3+dINiDwagi j7piMSDMXA0HUPkfDf+PfDzPRlvRL1sK5xwKkPEsgguRmwmSTZC3Zh9h1uShJLeybEF0ai qnt8HdDjLAdjPDAVx5hEhLDZZlpnPa1dYTiifRAJZFVkBJCx1j/5meSksA/IwNIHuNjdcT sz5FSx5NHXaDOOYUs/wLZq7qF2lEn6nVtAA/sZT6WqkdchcGnftsEi/rOYMYxg== From: Miquel Raynal To: Srinivas Kandagatla , Greg Kroah-Hartman Cc: Michael Walle , =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= , Rob Herring , Frank Rowand , devicetree@vger.kernel.org, , Robert Marko , Thomas Petazzoni , Luka Perkov , Randy Dunlap , Chen-Yu Tsai , Daniel Golle , Miquel Raynal Subject: [PATCH v11 2/7] nvmem: Clarify the situation when there is no DT node available Date: Thu, 5 Oct 2023 00:22:31 +0200 Message-Id: <20231004222236.411248-3-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231004222236.411248-1-miquel.raynal@bootlin.com> References: <20231004222236.411248-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 X-GND-Sasl: miquel.raynal@bootlin.com X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,SPF_HELO_PASS, SPF_PASS,URIBL_BLOCKED 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 (howler.vger.email [0.0.0.0]); Wed, 04 Oct 2023 15:23:06 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778865340484549317 X-GMAIL-MSGID: 1778865340484549317 At a first look it might seem that the presence of the of_node pointer in the nvmem device does not matter much, but in practice, after looking deep into the DT core, nvmem_add_cells_from_dt() will simply and always return NULL if this field is not provided. As most mtd devices don't populate this field (this could evolve later), it means none of their children cells will be populated unless no_of_node is explicitly set to false. In order to clarify the logic, let's add clear check at the beginning of this helper. Signed-off-by: Miquel Raynal --- drivers/nvmem/core.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index eaf6a3fe8ca6..286efd3f5a31 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -743,6 +743,9 @@ static int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_nod static int nvmem_add_cells_from_legacy_of(struct nvmem_device *nvmem) { + if (!nvmem->dev.of_node) + return 0; + return nvmem_add_cells_from_dt(nvmem, nvmem->dev.of_node); } From patchwork Wed Oct 4 22:22: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: 148608 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:254a:b0:403:3b70:6f57 with SMTP id hf10csp437148vqb; Wed, 4 Oct 2023 15:23:04 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHgUFF2KIQVKFka0wquFidy+HWhzLGmuisaUb4FN//3XgY1mrTh+EHhKFUnxLX7gx4l5C2j X-Received: by 2002:a05:6808:1a26:b0:3a9:ba39:6d6e with SMTP id bk38-20020a0568081a2600b003a9ba396d6emr4773298oib.0.1696458184022; Wed, 04 Oct 2023 15:23:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696458183; cv=none; d=google.com; s=arc-20160816; b=IIS/8qrobLymO3cnjstP6TbApdphIZAc2X7GByUnfCmjynmjYQ0JM4J7/+nAc4DA17 A1aGaXELf+/Z3uToFDpMwwEWXGspJxKG1rW4gOGp0PFXxLoKHefh8I4izCehR0wiaSqG qm5aAFncI80qJBtgVROB4s2D1Xh1gNuKfItbIYgKO40m7aGvj60kG5YM6W1YspjWgfoF jqQ7PzLCend5/JuvuhVYMB62MJ1LFy+h9oB611G3RlswOg7DKaeQ4on8KrwiJSd95s8L nqqIwXLi+GA3E+6u5fBgzKzFb6fcQ2STXYIrINVLrarIMZNLQCEzmA1PZBt7SNkRM0K+ DZzw== 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=Gsso1VG1TaOtkSVVCJF5rMb/bS6AWoE0lsnGm4HjbN8=; fh=XQzgxiPMj/Cqsom9sWl23DkvcXkGD6r/vx0NuuBn5R0=; b=pGSR5uHJLb4lcZLjlKIeI0mOHQtJ+Xw/wnI5dBDeeiMpTgNUZ1mDEe1J7E5CSsAMUx IleHuyEIKXsajeQrB8Ukv6mTDRCYBC8f11/KFKB3dZgJztsJT+K6U+zkSalUMDV34W65 nL8ZtS6HO9peGtcOSXGEv+6AlHJ0n3OCQfqDQR+LB+OuAQzaiD7Y8KZFhEqVwj9qxNL7 h9NkTwiTud3lohoiu54dziGcLnoucP0b3qBjrBW9F0DHR725TdZZ89TGZuO0WcaRjNyH 2ugiRHiXtS9FUGF1BBecPCcNiCk65dw6WpWN/9G9XQTNLuQdfL8/dPiQhyW6dVjkLArB cyEQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b="H/x9tHbL"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id d185-20020a6336c2000000b0057759a5b7c6si115580pga.62.2023.10.04.15.23.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Oct 2023 15:23:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b="H/x9tHbL"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 howler.vger.email (Postfix) with ESMTP id C578283B6BC7; Wed, 4 Oct 2023 15:23:02 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234784AbjJDWW6 (ORCPT + 19 others); Wed, 4 Oct 2023 18:22:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54968 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233709AbjJDWWx (ORCPT ); Wed, 4 Oct 2023 18:22:53 -0400 Received: from relay5-d.mail.gandi.net (relay5-d.mail.gandi.net [IPv6:2001:4b98:dc4:8::225]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A9E2EC6; Wed, 4 Oct 2023 15:22:49 -0700 (PDT) Received: by mail.gandi.net (Postfix) with ESMTPSA id 5E7391C0009; Wed, 4 Oct 2023 22:22:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1696458168; 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=Gsso1VG1TaOtkSVVCJF5rMb/bS6AWoE0lsnGm4HjbN8=; b=H/x9tHbLI+7zWMC7ihWzo6QZnyW+b3IX0j/v+z5ek+ReixQc0nq9OPnhURCokEEYyt5bbl MojFzE3gnUlGqjBUXSIeriTcGfg9pwJGolU1twWcNSc+RvM3/O7sBDLctQB/J5iPn/4RDB kGKGoXNVkilXzE8kfB7MMgeN3r4FD+FqFI+OtHu0EqoupxezVybFk2zt7LFYktNTC4WpUa I0/f/ijb1bGu/MQtv2st3yvokJUjzASjIkoKYXaxlMIbhi9u7tDFDejUNjzWZPQfwhjOh9 eiSfq3huHDgCWLxYdA2DlUFl7gjNk8fxI36VXGTCgltZLIq9qZBXvt7szPGMRg== From: Miquel Raynal To: Srinivas Kandagatla , Greg Kroah-Hartman Cc: Michael Walle , =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= , Rob Herring , Frank Rowand , devicetree@vger.kernel.org, , Robert Marko , Thomas Petazzoni , Luka Perkov , Randy Dunlap , Chen-Yu Tsai , Daniel Golle , Miquel Raynal Subject: [PATCH v11 3/7] nvmem: Move of_nvmem_layout_get_container() in another header Date: Thu, 5 Oct 2023 00:22:32 +0200 Message-Id: <20231004222236.411248-4-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231004222236.411248-1-miquel.raynal@bootlin.com> References: <20231004222236.411248-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 X-GND-Sasl: miquel.raynal@bootlin.com X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,SPF_HELO_PASS, SPF_PASS,URIBL_BLOCKED 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 (howler.vger.email [0.0.0.0]); Wed, 04 Oct 2023 15:23:02 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778865336690950630 X-GMAIL-MSGID: 1778865336690950630 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 | 14 ++++++++++++++ 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index 286efd3f5a31..c63057a7a3b8 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -844,14 +844,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 4523e4e83319..960728b10a11 100644 --- a/include/linux/nvmem-consumer.h +++ b/include/linux/nvmem-consumer.h @@ -241,7 +241,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) @@ -254,12 +253,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 dae26295e6be..d260738ad03c 100644 --- a/include/linux/nvmem-provider.h +++ b/include/linux/nvmem-provider.h @@ -205,6 +205,16 @@ void nvmem_layout_unregister(struct nvmem_layout *layout); const void *nvmem_layout_get_match_data(struct nvmem_device *nvmem, struct nvmem_layout *layout); +/** + * 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 static inline struct nvmem_device *nvmem_register(const struct nvmem_config *c) @@ -242,6 +252,10 @@ nvmem_layout_get_match_data(struct nvmem_device *nvmem, return NULL; } +static inline struct device_node *of_nvmem_layout_get_container(struct nvmem_device *nvmem); +{ + return NULL; +} #endif /* CONFIG_NVMEM */ #define module_nvmem_layout_driver(__layout_driver) \ From patchwork Wed Oct 4 22:22: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: 148615 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:254a:b0:403:3b70:6f57 with SMTP id hf10csp440003vqb; Wed, 4 Oct 2023 15:29:59 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH7FNxKlNlIm0/fFQdGRtQ81TGPtdNRLaYxPXmRtdD9X/iYTDquWLqCPNRctljGM/O7Dlcy X-Received: by 2002:a05:6a20:7f83:b0:15e:b8a1:57b9 with SMTP id d3-20020a056a207f8300b0015eb8a157b9mr4519996pzj.24.1696458599329; Wed, 04 Oct 2023 15:29:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696458599; cv=none; d=google.com; s=arc-20160816; b=OkfLoRzFZ1IhzPFi9aaLY4J9Vo+ZXQuxhKfoN177K6AcAsK07IXmKmAiepH0aSXwID CtHExAkHkyqYAtM4uJI5pHiEHUM8LAiQGlB05wsYRdB86Is6wY3NFx3uUUCkI9yCucTW LAE+ZTt2/J5uT27dzs43v5SUFZ4I0GxuyZpug5fhZ3F07yDW5iNIvHTd49/sQeixEhnn lMi5NNnWX9E0yhHK+zC/dkHCYR23lxoQTNkgA6IKLxM9EJRZx7bj234yL03XhWtr7xDe xIU3SL4sHaV5We89M8cnWj5IE/ngsyIcXyFdFrZiauzWSUpSzWn5623cP+eTGHPq0WW4 Ufog== 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=/WtKn/Tb4sVAlce85oVAG5zntlgbWadZVDUoXswIydw=; fh=XQzgxiPMj/Cqsom9sWl23DkvcXkGD6r/vx0NuuBn5R0=; b=vgxgUa3aBv2eUwzmJNl7Y0tnEt4HTWeLz15861fFOYnbLesV2KLQM2NFTlKAXb4t9u /iGvg0DVwje5qTqgCM8Uk/MF1jW/+qoJfLbzb4b5P11+XtS2jDXmYId7WXNtb+k+//qD ux/sieKx/dU2ma9L2Gq/NHlxQ0u1iwV/KrSrFxzsMLX6cvtbOwQwUQJFQC9oe5hMBKEv e7qUTPXi2tEeLoXFjdE5Sn0HBNUInVeV3z4eqfWcwYvgzWPx8t3qI7wytH7eKGISafju Iy3ZNcx3Rv1BgjeDLK/pb3nZqL0JnwzOt3LNU6Mez9CoKgTJhJkvvKw39dC2xKpbpIlH nZsw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=WEsIoZC5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id j73-20020a638b4c000000b0058934e47c94si83406pge.740.2023.10.04.15.29.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Oct 2023 15:29:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=WEsIoZC5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 howler.vger.email (Postfix) with ESMTP id 86AF184CE640; Wed, 4 Oct 2023 15:29:58 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235666AbjJDWXD (ORCPT + 18 others); Wed, 4 Oct 2023 18:23:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54984 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235036AbjJDWW4 (ORCPT ); Wed, 4 Oct 2023 18:22:56 -0400 Received: from relay5-d.mail.gandi.net (relay5-d.mail.gandi.net [IPv6:2001:4b98:dc4:8::225]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1F79BEE; Wed, 4 Oct 2023 15:22:51 -0700 (PDT) Received: by mail.gandi.net (Postfix) with ESMTPSA id 987BC1C0005; Wed, 4 Oct 2023 22:22:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1696458170; 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=/WtKn/Tb4sVAlce85oVAG5zntlgbWadZVDUoXswIydw=; b=WEsIoZC5O98yyVASIaiclGnH86JWzemScB3JQObFyalXk4oQUwgzIUIYVfh9rQsRjKKxFd xE0Lr7SFgWNQrsGjk5GjCegG90wryvMJzveeaLdlWKr5ZqMElhRf5NjsvuECMoin5NOd/7 +x+DWFM2gDmUYsvR7G0Yee4ZxD7FyBU31fm/gjG8Jnjc/aaW1KBlqlE9Zi4csATt+NfQUR hgH9tIGd76FzE/V2w/JqyvFMjXqG9vGAa17EgM8W35sRvynO7bLyfGv0G/t0plheT9e3ky Tsw2CqWziksEO3iBDgFefEXuf2jyHeHGVkMJ6HodimQ5fnO5k9xRMMtBLrkuIA== From: Miquel Raynal To: Srinivas Kandagatla , Greg Kroah-Hartman Cc: Michael Walle , =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= , Rob Herring , Frank Rowand , devicetree@vger.kernel.org, , Robert Marko , Thomas Petazzoni , Luka Perkov , Randy Dunlap , Chen-Yu Tsai , Daniel Golle , Miquel Raynal Subject: [PATCH v11 4/7] nvmem: Create a header for internal sharing Date: Thu, 5 Oct 2023 00:22:33 +0200 Message-Id: <20231004222236.411248-5-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231004222236.411248-1-miquel.raynal@bootlin.com> References: <20231004222236.411248-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 X-GND-Sasl: miquel.raynal@bootlin.com X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,SPF_HELO_PASS, SPF_PASS,URIBL_BLOCKED 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 (howler.vger.email [0.0.0.0]); Wed, 04 Oct 2023 15:29:58 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778865772109702508 X-GMAIL-MSGID: 1778865772109702508 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 c63057a7a3b8..073fe4a73e37 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 Oct 4 22:22:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 148610 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:254a:b0:403:3b70:6f57 with SMTP id hf10csp437205vqb; Wed, 4 Oct 2023 15:23:12 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG088++l6UZvmON/5Q6bKQKiHWslgW0zJtdjfPUQLnFJfIGnmMvrlKVhXRnfEeO7/iPp8Kz X-Received: by 2002:a17:90a:4ec3:b0:274:84a2:f0d8 with SMTP id v3-20020a17090a4ec300b0027484a2f0d8mr3665760pjl.25.1696458192407; Wed, 04 Oct 2023 15:23:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696458192; cv=none; d=google.com; s=arc-20160816; b=xcofFfeCnW0dt+tMFfyb+la4mrFrIRT7MTxGCD2FHZRIVgAwSjW23UbwCrVV0y5Xv2 JD+l8sA3O9M+iI3/NQe/6y93R6f+oMo/tHJeHL7ovk69MKJB3EiZBPRpc4XHE9Gg3+YB 9J0gH/9e4c862xGIrPtaUHV0/a8R/ek2gPg25TwOvLE2u9NKrau+J+61P9YhNdEDeVlD 3lSbbKP6SChV7h3K2F0Pt5T1JaLqJFjaU4HgVKQYLfbf3rmVCDdFJrDMoosWAMh4xp15 Hvl0j9ty++Bzq16+hKBGzlfXuVDVOtTHeiqk7MunTi+EjtNGmn+72HzcW/g4o/KJrdI2 zN6A== 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+IyQ6AFHVL8vkHiWDUBkK8L51Tp6FEdFJJZ33XmkB0=; fh=XQzgxiPMj/Cqsom9sWl23DkvcXkGD6r/vx0NuuBn5R0=; b=tgxZ42wrG0gZ0vX7AF2ACdVItGZzNHxF9RdsGnHNLX/TkD0J3ih9yiU51hVPrZNIKX qa3Z46NzVi8QHPNabbXCN9DG0WNzsNbiNJsSfdPATatR2fb5PgxI31v0HETgJMKfC9S8 fx1sBwbYUv9yfp8dz8S7f3dppP8TnoBf8LlrbCEoZWFt4SzHhE9TBhbD8oZe7rvcxNpY vRLbE5S21aXQY8sxUfP3HyRMyeeL2Q5IUzbJNLSUK7gFXoMhtVzg8xHdxWToBcSKSM3J r6GRULo658xJApWGu8/OkQ1DEt/KVzMGRsuKPRIz5Ple0+xeT0F61NhT+YF1pU7E++/U UbfA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=G2NP9LLV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 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 howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id w19-20020a17090aea1300b00263860e1f4csi2291573pjy.16.2023.10.04.15.23.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Oct 2023 15:23:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=G2NP9LLV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 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 howler.vger.email (Postfix) with ESMTP id 135AE83B0083; Wed, 4 Oct 2023 15:23:11 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236558AbjJDWXG (ORCPT + 19 others); Wed, 4 Oct 2023 18:23:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37856 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232592AbjJDWXA (ORCPT ); Wed, 4 Oct 2023 18:23:00 -0400 Received: from relay5-d.mail.gandi.net (relay5-d.mail.gandi.net [217.70.183.197]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D98F8E5; Wed, 4 Oct 2023 15:22:54 -0700 (PDT) Received: by mail.gandi.net (Postfix) with ESMTPSA id 182031C0003; Wed, 4 Oct 2023 22:22:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1696458173; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=C+IyQ6AFHVL8vkHiWDUBkK8L51Tp6FEdFJJZ33XmkB0=; b=G2NP9LLV2baSTs+QesOicML/NnkVmhD2glBvDYzhompDMzeD+6XWF1RrYZVldV1gFlvV+e d6K7yEs0vPfFH3zJnFXaQUng0SpSTwvRV8pDptWN6vOykjbP6Czd6wQEji0k76KIlDsvW3 1GGqsjwEE01PMCwS4sC9V/iGNZiKOg3kyeKJzm/p2/w6v/741cNUzJl9B7GIzcraaav/mQ NVST/Lc7pc7e/zIOuOYOtDK/XQBBtWdNKV1WlT5kCnkL15cUShTrl7rnsdwaRO7rOWl2BV JRjR5i+yBeWWtttbblTYpIsWw0X8nCOZ1Kw718fEbj2AMwJRdBeU7mXFOxeVpw== From: Miquel Raynal To: Srinivas Kandagatla , Greg Kroah-Hartman Cc: Michael Walle , =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= , Rob Herring , Frank Rowand , devicetree@vger.kernel.org, , Robert Marko , Thomas Petazzoni , Luka Perkov , Randy Dunlap , Chen-Yu Tsai , Daniel Golle , Miquel Raynal Subject: [PATCH v11 5/7] nvmem: core: Rework layouts to become regular devices Date: Thu, 5 Oct 2023 00:22:34 +0200 Message-Id: <20231004222236.411248-6-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231004222236.411248-1-miquel.raynal@bootlin.com> References: <20231004222236.411248-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 X-GND-Sasl: miquel.raynal@bootlin.com X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS, URIBL_BLOCKED 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 (howler.vger.email [0.0.0.0]); Wed, 04 Oct 2023 15:23:11 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778865346040562459 X-GMAIL-MSGID: 1778865346040562459 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. Technically, the layouts are more like a "plus" and, even 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 existing notifier mechanism. When a new NVMEM device is registered, we can: - populate its nvmem-layout child, if any - try to modprobe the relevant driver, if relevant - try to hook the NVMEM device with a layout in the notifier And when a new layout is registered: - try to hook all the existing NVMEM devices which are not yet hooked to a layout with the new layout 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 need: * To keep track of all nvmem devices * To 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/Makefile | 2 +- drivers/nvmem/core.c | 157 +++++++++++++++++++++------- drivers/nvmem/internals.h | 4 + drivers/nvmem/layouts.c | 171 +++++++++++++++++++++++++++++++ drivers/nvmem/layouts/onie-tlv.c | 37 ++++++- drivers/nvmem/layouts/sl28vpd.c | 37 ++++++- include/linux/nvmem-provider.h | 24 +++-- 7 files changed, 376 insertions(+), 56 deletions(-) create mode 100644 drivers/nvmem/layouts.c diff --git a/drivers/nvmem/Makefile b/drivers/nvmem/Makefile index 423baf089515..77be96076ea6 100644 --- a/drivers/nvmem/Makefile +++ b/drivers/nvmem/Makefile @@ -4,7 +4,7 @@ # obj-$(CONFIG_NVMEM) += nvmem_core.o -nvmem_core-y := core.o +nvmem_core-y := core.o layouts.o obj-y += layouts/ # Devices diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index 073fe4a73e37..1f311c899ae1 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -53,6 +53,7 @@ static LIST_HEAD(nvmem_cell_tables); static DEFINE_MUTEX(nvmem_lookup_mutex); static LIST_HEAD(nvmem_lookup_list); +struct notifier_block nvmem_nb; static BLOCKING_NOTIFIER_HEAD(nvmem_notifier); static DEFINE_SPINLOCK(nvmem_layout_lock); @@ -771,23 +772,16 @@ 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); + struct nvmem_layout *l, *layout = NULL; layout_np = of_nvmem_layout_get_container(nvmem); if (!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 (of_match_node(l->dev->driver->of_match_table, layout_np)) { if (try_module_get(l->owner)) layout = l; @@ -821,14 +815,6 @@ static int nvmem_add_cells_from_layout(struct nvmem_device *nvmem) 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) { @@ -836,7 +822,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; } @@ -947,19 +933,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) @@ -978,24 +951,23 @@ 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; + /* Populate the layout bus */ + rval = nvmem_populate_layout(nvmem); + if (rval) + goto err_remove_cells; + blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem); return nvmem; 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: @@ -2097,13 +2069,122 @@ const char *nvmem_dev_name(struct nvmem_device *nvmem) } EXPORT_SYMBOL_GPL(nvmem_dev_name); +static void nvmem_try_loading_layout_driver(struct nvmem_device *nvmem) +{ + struct device_node *layout_np; + + layout_np = of_nvmem_layout_get_container(nvmem); + if (layout_np) { + of_request_module(layout_np); + of_node_put(layout_np); + } +} + +static int nvmem_match_available_layout(struct nvmem_device *nvmem) +{ + int ret; + + if (nvmem->layout) + return 0; + + nvmem->layout = nvmem_layout_get(nvmem); + if (!nvmem->layout) + return 0; + + ret = nvmem_add_cells_from_layout(nvmem); + if (ret) { + nvmem_layout_put(nvmem->layout); + nvmem->layout = NULL; + return ret; + } + + return 0; +} + +static int nvmem_dev_match_available_layout(struct device *dev, void *data) +{ + struct nvmem_device *nvmem = to_nvmem_device(dev); + + return nvmem_match_available_layout(nvmem); +} + +static int nvmem_for_each_dev(int (*fn)(struct device *dev, void *data)) +{ + return bus_for_each_dev(&nvmem_bus_type, NULL, NULL, fn); +} + +/* + * When an NVMEM device is registered, try to match against a layout and + * populate the cells. When an NVMEM layout is probed, ensure all NVMEM devices + * which could use it properly expose their cells. + */ +static int nvmem_notifier_call(struct notifier_block *notifier, + unsigned long event_flags, void *context) +{ + struct nvmem_device *nvmem = NULL; + int ret; + + switch (event_flags) { + case NVMEM_ADD: + nvmem = context; + break; + case NVMEM_LAYOUT_ADD: + break; + default: + return NOTIFY_DONE; + } + + if (nvmem) { + /* + * In case the nvmem device was built-in while the layout was + * built as a module, manually request loading the layout driver. + */ + nvmem_try_loading_layout_driver(nvmem); + + /* Populate the cells of the new nvmem device from its layout, if any */ + ret = nvmem_match_available_layout(nvmem); + } else { + /* NVMEM devices might be "waiting" for this layout */ + ret = nvmem_for_each_dev(nvmem_dev_match_available_layout); + } + + if (ret) + return notifier_from_errno(ret); + + return NOTIFY_OK; +} + 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) + goto unregister_nvmem_bus; + + nvmem_nb.notifier_call = &nvmem_notifier_call; + ret = nvmem_register_notifier(&nvmem_nb); + if (ret) + goto unregister_nvmem_layout_bus; + + return 0; + +unregister_nvmem_layout_bus: + nvmem_layout_bus_unregister(); +unregister_nvmem_bus: + bus_unregister(&nvmem_bus_type); + + return ret; } static void __exit nvmem_exit(void) { + nvmem_unregister_notifier(&nvmem_nb); + nvmem_layout_bus_unregister(); bus_unregister(&nvmem_bus_type); } diff --git a/drivers/nvmem/internals.h b/drivers/nvmem/internals.h index ce353831cd65..eb73b59d1fd9 100644 --- a/drivers/nvmem/internals.h +++ b/drivers/nvmem/internals.h @@ -32,4 +32,8 @@ struct nvmem_device { void *priv; }; +int nvmem_layout_bus_register(void); +void nvmem_layout_bus_unregister(void); +int nvmem_populate_layout(struct nvmem_device *nvmem); + #endif /* ifndef _LINUX_NVMEM_INTERNALS_H */ diff --git a/drivers/nvmem/layouts.c b/drivers/nvmem/layouts.c new file mode 100644 index 000000000000..3b11ec70edec --- /dev/null +++ b/drivers/nvmem/layouts.c @@ -0,0 +1,171 @@ +// 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" + +#if CONFIG_OF +static int nvmem_layout_bus_match(struct device *dev, struct device_driver *drv) +{ + return of_driver_match_device(dev, drv); +} + +static struct bus_type nvmem_layout_bus_type = { + .name = "nvmem-layouts", + .match = nvmem_layout_bus_match, +}; + +static struct device nvmem_layout_bus = { + .init_name = "nvmem-layouts", +}; + +int __nvmem_layout_driver_register(struct nvmem_layout_driver *drv, + struct module *owner) +{ + drv->driver.owner = owner; + 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_device_release(struct device *dev) +{ + of_node_put(dev->of_node); + kfree(dev); +} + +static struct device *of_nvmem_layout_create_device(struct device_node *np) +{ + struct device *dev; + + dev = kzalloc(sizeof(*dev), GFP_KERNEL); + if (!dev) + return NULL; + + device_initialize(dev); + dev->parent = &nvmem_layout_bus; + dev->bus = &nvmem_layout_bus_type; + dev->release = nvmem_layout_device_release; + 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); + + if (device_add(dev)) { + put_device(dev); + return NULL; + } + + return dev; +} + +static const struct of_device_id of_nvmem_layout_skip_table[] = { + { .compatible = "fixed-layout", }, + {} +}; + +static int of_nvmem_layout_bus_populate(struct device_node *layout_dn) +{ + /* 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 */ + of_nvmem_layout_create_device(layout_dn); + 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); + +int nvmem_populate_layout(struct nvmem_device *nvmem) +{ + struct device_node *nvmem_dn, *layout_dn; + int ret; + + nvmem_dn = of_node_get(nvmem->dev.of_node); + if (!nvmem_dn) + return 0; + + layout_dn = of_nvmem_layout_get_container(nvmem); + if (!layout_dn) { + of_node_put(nvmem_dn); + return 0; + } + + device_links_supplier_sync_state_pause(); + ret = of_nvmem_layout_bus_populate(layout_dn); + device_links_supplier_sync_state_resume(); + + of_node_set_flag(nvmem_dn, OF_POPULATED_BUS); + + of_node_put(layout_dn); + of_node_put(nvmem_dn); + return ret; +} + +int nvmem_layout_bus_register(void) +{ + int ret; + + ret = device_register(&nvmem_layout_bus); + if (ret) { + put_device(&nvmem_layout_bus); + return ret; + } + + ret = bus_register(&nvmem_layout_bus_type); + if (ret) { + device_unregister(&nvmem_layout_bus); + return ret; + } + + return 0; +} + +void nvmem_layout_bus_unregister(void) +{ + bus_unregister(&nvmem_layout_bus_type); + device_unregister(&nvmem_layout_bus); +} +#endif diff --git a/drivers/nvmem/layouts/onie-tlv.c b/drivers/nvmem/layouts/onie-tlv.c index 59fc87ccfcff..9c269e389b28 100644 --- a/drivers/nvmem/layouts/onie-tlv.c +++ b/drivers/nvmem/layouts/onie-tlv.c @@ -13,6 +13,7 @@ #include #include #include +#include #define ONIE_TLV_MAX_LEN 2048 #define ONIE_TLV_CRC_FIELD_SZ 6 @@ -226,16 +227,44 @@ static int onie_tlv_parse_table(struct device *dev, struct nvmem_device *nvmem, return 0; } +static int onie_tlv_probe(struct device *dev) +{ + struct nvmem_layout *layout; + + layout = devm_kzalloc(dev, sizeof(*layout), GFP_KERNEL); + if (!layout) + return -ENOMEM; + + layout->add_cells = onie_tlv_parse_table; + layout->dev = dev; + + dev_set_drvdata(dev, layout); + + return nvmem_layout_register(layout); +} + +static int onie_tlv_remove(struct device *dev) +{ + struct nvmem_layout *layout = dev_get_drvdata(dev); + + nvmem_layout_unregister(layout); + + return 0; +} + 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 = { + .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 05671371f631..6857b1472288 100644 --- a/drivers/nvmem/layouts/sl28vpd.c +++ b/drivers/nvmem/layouts/sl28vpd.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #define SL28VPD_MAGIC 'V' @@ -135,16 +136,44 @@ static int sl28vpd_add_cells(struct device *dev, struct nvmem_device *nvmem, return 0; } +static int sl28vpd_probe(struct device *dev) +{ + struct nvmem_layout *layout; + + layout = devm_kzalloc(dev, sizeof(*layout), GFP_KERNEL); + if (!layout) + return -ENOMEM; + + layout->add_cells = sl28vpd_add_cells; + layout->dev = dev; + + dev_set_drvdata(dev, layout); + + return nvmem_layout_register(layout); +} + +static int sl28vpd_remove(struct device *dev) +{ + struct nvmem_layout *layout = dev_get_drvdata(dev); + + nvmem_layout_unregister(layout); + + return 0; +} + 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 = { + .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 d260738ad03c..a1b982f4092e 100644 --- a/include/linux/nvmem-provider.h +++ b/include/linux/nvmem-provider.h @@ -154,8 +154,7 @@ struct nvmem_cell_table { /** * struct nvmem_layout - NVMEM layout definitions * - * @name: Layout name. - * @of_match_table: Open firmware match table. + * @dev: Device-model layout 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(). @@ -170,8 +169,7 @@ struct nvmem_cell_table { * cells. */ struct nvmem_layout { - const char *name; - const struct of_device_id *of_match_table; + struct device *dev; int (*add_cells)(struct device *dev, struct nvmem_device *nvmem, struct nvmem_layout *layout); void (*fixup_cell_info)(struct nvmem_device *nvmem, @@ -183,6 +181,10 @@ struct nvmem_layout { struct list_head node; }; +struct nvmem_layout_driver { + struct device_driver driver; +}; + #if IS_ENABLED(CONFIG_NVMEM) struct nvmem_device *nvmem_register(const struct nvmem_config *cfg); @@ -202,6 +204,15 @@ int __nvmem_layout_register(struct nvmem_layout *layout, struct module *owner); __nvmem_layout_register(layout, THIS_MODULE) void nvmem_layout_unregister(struct nvmem_layout *layout); +#define nvmem_layout_driver_register(drv) \ + __nvmem_layout_driver_register(drv, THIS_MODULE) +int __nvmem_layout_driver_register(struct nvmem_layout_driver *drv, + struct module *owner); +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); @@ -257,9 +268,4 @@ static inline struct device_node *of_nvmem_layout_get_container(struct nvmem_dev return NULL; } #endif /* CONFIG_NVMEM */ - -#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 Oct 4 22:22:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 148611 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:254a:b0:403:3b70:6f57 with SMTP id hf10csp437219vqb; Wed, 4 Oct 2023 15:23:14 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEdSqbgZ/dfQnNIjqZWaNYlUmUP1emknGkRhAqrRbm3cfclBIQ/5urNwbcC1WLjv6mNW1gk X-Received: by 2002:a05:6a00:99c:b0:68f:cc67:e723 with SMTP id u28-20020a056a00099c00b0068fcc67e723mr4243929pfg.17.1696458194320; Wed, 04 Oct 2023 15:23:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696458194; cv=none; d=google.com; s=arc-20160816; b=0iRFAfYnYkX0bMJ+enUu8EOwsumk+lhFeECYFfwcahvyOvBJ1PrzKsw0hRTFesLCHq bBIkCSH+icUgEPuea4P6YUZWvp+kdnCmjx3/WLKwgkxtsmLJA5NCO1y1y+VH7P7Zdk0I sFjHxebJn6XIVm66SX2K8WLiNMEvV9s1/rGL9eAbE9zRvbKcIDWYLq4LZiv34pv3A2TK YqFow08wm2a1+DnnshikzWLjYmGwHn2JEfd/2ODFIm4HdKbBtTtpWvTZqx4M1lka5T+v 4bK3Q22jbWSWCNXpqDK5+sRBZsyNOMMzosbC+0cpPZSZTUvFGNeSFDlww0r05XiEnbOe zgJw== 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=XQzgxiPMj/Cqsom9sWl23DkvcXkGD6r/vx0NuuBn5R0=; b=f1N6lVitLKiU0A8LyrcPaXhTfL1zaK807CRr+jJRbY8hLgTuNmG0cQRlo2sTXQqTjP gfhdxjAfOZqR5yinQLwpVMjMhqKrEWJlY9xr9uWDPn9v108w6l10rWJNrgG9bJQ8SjL0 Cf085PwapwKlLfVpApQjEN9Ou9DdheZzUm21Qr4U2OApFYUZc66IzBkLVksSU1ZHPJvL w9LPJlrAClLr+jDlSilLvYbYYD/PEVf6ruhQ0QTL8UCicmjYjMd0aj/O5+RT1c/plfa+ GafnBMtWzsYom4fucVhFxwTrCo+hVV4KjppfwAQH3U771wu36A6Ru3zvlTsNlJ6DpBsi Rc8Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=clzmPzVF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 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 howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id p1-20020a056a000a0100b00690bdd81cadsi45064pfh.155.2023.10.04.15.23.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Oct 2023 15:23:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=clzmPzVF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 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 howler.vger.email (Postfix) with ESMTP id 763E984D572F; Wed, 4 Oct 2023 15:23:13 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235737AbjJDWXK (ORCPT + 19 others); Wed, 4 Oct 2023 18:23:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37866 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236114AbjJDWXB (ORCPT ); Wed, 4 Oct 2023 18:23:01 -0400 Received: from relay5-d.mail.gandi.net (relay5-d.mail.gandi.net [217.70.183.197]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B0BD610F; Wed, 4 Oct 2023 15:22:56 -0700 (PDT) Received: by mail.gandi.net (Postfix) with ESMTPSA id 664391C0006; Wed, 4 Oct 2023 22:22:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1696458175; 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=clzmPzVFVf89AykIKBa1Jei5WFbi6RaZdA04K8dmqtHHD9JYr1WRT+l6ekSwU4VadGsfHV xw8Q6S9IB8kfK5yLAo9UsXwIr0159hMbyBU9VPO5iI2WxsQ6D/PVqGjxKWpy6PeRMnOdXe yA4oxOO8L1qdRPIJVXmLWxokyQvCF3y1+obtnEYkB9n/jK1hwGeLhvpi6V2J6D60v/ZZG/ h+jYZQsgi7jSPUo7kZ5fQ4SSoNND2+dkBkZ5IeYSmkbxJAxvKhOEnPiX9yH4E7pw7MiRrq 83fmb4fxXptRJ7wGOxGvAe1DG5XfJQt7psHim2krZBao1pkp3KBXoUfoMbongw== From: Miquel Raynal To: Srinivas Kandagatla , Greg Kroah-Hartman Cc: Michael Walle , =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= , Rob Herring , Frank Rowand , devicetree@vger.kernel.org, , Robert Marko , Thomas Petazzoni , Luka Perkov , Randy Dunlap , Chen-Yu Tsai , Daniel Golle , Miquel Raynal Subject: [PATCH v11 6/7] ABI: sysfs-nvmem-cells: Expose cells through sysfs Date: Thu, 5 Oct 2023 00:22:35 +0200 Message-Id: <20231004222236.411248-7-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231004222236.411248-1-miquel.raynal@bootlin.com> References: <20231004222236.411248-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 X-GND-Sasl: miquel.raynal@bootlin.com X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS, URIBL_BLOCKED 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 (howler.vger.email [0.0.0.0]); Wed, 04 Oct 2023 15:23:13 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778865347384308341 X-GMAIL-MSGID: 1778865347384308341 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 Oct 4 22:22:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 148612 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:254a:b0:403:3b70:6f57 with SMTP id hf10csp437263vqb; Wed, 4 Oct 2023 15:23:21 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFogWXrm2gilEBz93JOefZLBxqSBTc0KYjr5M9dK3xhdI/pHFwBKvyLIVlh5FAj+fIMNr2t X-Received: by 2002:a17:903:48f:b0:1c5:ad14:908f with SMTP id jj15-20020a170903048f00b001c5ad14908fmr3257362plb.39.1696458200851; Wed, 04 Oct 2023 15:23:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696458200; cv=none; d=google.com; s=arc-20160816; b=Qr42Mvd913cFnE0gevYKMtMUdtgjNqxJ2uaPF+FFuJapNJ2JPkJBsUVEpJllUwh/qV d1v/Ru5+T2WE9MnEtEmhPI5OQoLh7dkvhoqGcCPhUDvKvxgRouB7mo3+EbX0b1LFKyrX MMum/WB65PzPIQ+6IEEy0LUfRYe/xkyN+v1qeSMm2lzZEhxFJTfiikueIofjUcjcda4q n1TVvWAwy9FKDrYzoKPnBCZaEG8lHamBrPGNlk3jMvTicWRqFIAQ/XjOVpQtIQmMIio3 VwCpPoHdH0q+6hgX7QVHNhsicef50jzgHWCJJq6VqNllQeuQor1p3dGBs3LKfVn1A2Qy x4RQ== 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=MJSnkDzDxHMCQetZ/+skPNzc1TJhICAJggt8E2GoXhw=; fh=XQzgxiPMj/Cqsom9sWl23DkvcXkGD6r/vx0NuuBn5R0=; b=lVdLpt7zhI8pKzyppDlymKs2sEB/rWkoBik8Y74RS/V9VSNX8v7B13J4oynLWZaWnf vFB3M+3c82uB5xTzyOcDqzd+bO8XCdQH/aIk8gVNcIaM1RQd/9RA9Dm1G5hLeURZAh1t oj6gr3trZpPMwArCXaTQdD+jgzkdpWHooSiEPktxkVYqcW4UUywfQkp342bRcj12BQad c1soRiHGNS0YalzlLK52ucFeNGr3X8EC0OfXfPX8CQBH0wxY5qtZDiEP7oXWtG4p/wqR kt9jepRyvOWD5U5Pqh5hI6tbioPOS1iUDj7E6ewMS3ZCqaD9V0wo3sC5/RHPZSB5i/jT 5rCQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=CPLsLqOe; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id l8-20020a170903244800b001c62502d9fasi118510pls.343.2023.10.04.15.23.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Oct 2023 15:23:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=CPLsLqOe; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 howler.vger.email (Postfix) with ESMTP id 236ED84DB7AE; Wed, 4 Oct 2023 15:23:20 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233464AbjJDWXO (ORCPT + 19 others); Wed, 4 Oct 2023 18:23:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37942 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232667AbjJDWXE (ORCPT ); Wed, 4 Oct 2023 18:23:04 -0400 Received: from relay5-d.mail.gandi.net (relay5-d.mail.gandi.net [IPv6:2001:4b98:dc4:8::225]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 36D93DC; Wed, 4 Oct 2023 15:22:59 -0700 (PDT) Received: by mail.gandi.net (Postfix) with ESMTPSA id A07521C0009; Wed, 4 Oct 2023 22:22:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1696458177; 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=MJSnkDzDxHMCQetZ/+skPNzc1TJhICAJggt8E2GoXhw=; b=CPLsLqOebwAUKFkOPiza6wA07lk29WukSFnS52exZjJL4ZU7H3NOxXhGZ8B+dOhM2iAT0V wHiWFeTKF/DYA1w7OJWouhLys9jF7I3roD/dXL9rIp1TXlyaecPGfi2opEgyYqL3wv8e9Y pZG+qU/5Y9Dv32w+TpUSkRhyv6HCZnj2lGBRQ+ZxmD7c8XnmsKKx3UfJmZ/ZMJFLsYe88K XnP1uSLb59r5cRFbFdp75vhL9vwJDhUNWsJzxO8RJ9Y90bx4qy++VvTPgWsi6Ihf98bS4U slEAIz97JW2UyXSwfc47Lpju1WPB2PzBVlPmqWpX57VKMgl5jWprM2IVuzZaJQ== From: Miquel Raynal To: Srinivas Kandagatla , Greg Kroah-Hartman Cc: Michael Walle , =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= , Rob Herring , Frank Rowand , devicetree@vger.kernel.org, , Robert Marko , Thomas Petazzoni , Luka Perkov , Randy Dunlap , Chen-Yu Tsai , Daniel Golle , Miquel Raynal Subject: [PATCH v11 7/7] nvmem: core: Expose cells through sysfs Date: Thu, 5 Oct 2023 00:22:36 +0200 Message-Id: <20231004222236.411248-8-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231004222236.411248-1-miquel.raynal@bootlin.com> References: <20231004222236.411248-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 X-GND-Sasl: miquel.raynal@bootlin.com X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,SPF_HELO_PASS, SPF_PASS,URIBL_BLOCKED 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 (howler.vger.email [0.0.0.0]); Wed, 04 Oct 2023 15:23:20 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778865354472416076 X-GMAIL-MSGID: 1778865354472416076 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 --- drivers/nvmem/core.c | 116 ++++++++++++++++++++++++++++++++++++++ drivers/nvmem/internals.h | 1 + 2 files changed, 117 insertions(+) diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index 1f311c899ae1..bb29cfe11334 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -303,6 +303,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 = { @@ -324,11 +361,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", @@ -384,6 +431,69 @@ static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem, device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom); } +static int nvmem_dev_populate_sysfs_cells(struct device *dev, void *data) +{ + struct nvmem_device *nvmem = to_nvmem_device(dev); + 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, @@ -2151,6 +2261,12 @@ static int nvmem_notifier_call(struct notifier_block *notifier, if (ret) return notifier_from_errno(ret); +#ifdef CONFIG_NVMEM_SYSFS + ret = nvmem_for_each_dev(nvmem_dev_populate_sysfs_cells); + if (ret) + return notifier_from_errno(ret); +#endif + return NOTIFY_OK; } diff --git a/drivers/nvmem/internals.h b/drivers/nvmem/internals.h index eb73b59d1fd9..baa1c173be1c 100644 --- a/drivers/nvmem/internals.h +++ b/drivers/nvmem/internals.h @@ -30,6 +30,7 @@ struct nvmem_device { struct gpio_desc *wp_gpio; struct nvmem_layout *layout; void *priv; + bool sysfs_cells_populated; }; int nvmem_layout_bus_register(void);