From patchwork Thu Oct 5 15:59:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 148885 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2016:b0:403:3b70:6f57 with SMTP id fe22csp414165vqb; Thu, 5 Oct 2023 09:22:37 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEHamdrFRnBY+WWXM+8icF3188B8pNv9ie0NPxjeZXjcxcRZLQBOTgdus7HSTCVzH3l5Z3U X-Received: by 2002:a05:6e02:12b3:b0:34f:f0d9:5a1c with SMTP id f19-20020a056e0212b300b0034ff0d95a1cmr5351145ilr.15.1696522957061; Thu, 05 Oct 2023 09:22:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696522957; cv=none; d=google.com; s=arc-20160816; b=MVE2cmd2Sc9sdGxcwNNxob+ZMXjk6QTxGSRuIxuUQpgGM18ugfioTUg/LkJ8HTZz7i 2TFRW7qhguV1if9weCAqQN6x+VMfrz86wR7v+t3u8Zq7O7cH5vKLob1TNPIkokop0ykv k2IIIXBPDgWDfcf/8dg5xv05Z1rBqAChlwU5/0SN3/ue1KLgH3oQ7QvL27hqG+eWLOTo 78EJcXeml9jmri5pVRMCkpMg0eruflhWK52xEnrGjzVhgu5Y89WZeMhAE+ZkemOQEQT/ 27dliAD8L5XENaSXjYr0jZKQbQa6rqNU3qYtw+SwLFCATu/b15xG8iyqPXK3jCqyXMMM HPvw== 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=xVORv4M36pBe5MnX1Q1OB+/3pJK6y94SLiGpnRPqj/AVclqguwsNgPRa7buKzB2Pzw MJB4ufM19XHJiet+L76kh3xnQhFxMtrrPyIYnR318Aq9npNToJ03uyfnxSF12e8ufnYc qEc5uM2aCiNw5OKFm87qCyJNkci+kZ5Ik2Rk/ORh9Irvd4yErI48tsLj382YF6GXHYEy 2stu/G9oU9cxLH31W9GtmFOPg6bmzZnpcbiXUr0a9uudGlGD03XqAgobCulY2ZotVnlY Zg6JNuEu4t6F1mgvTWevPNJrsJP289hF5YwyunzIGo5WOwDXf97ZpWSl+0QgCGUaySFp rwKQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=OycCdYD6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 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. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id k189-20020a6384c6000000b0057b55b52870si1794657pgd.170.2023.10.05.09.22.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Oct 2023 09:22:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=OycCdYD6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 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 1E30983DD685; Thu, 5 Oct 2023 09:22:05 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242877AbjJEQTZ (ORCPT + 19 others); Thu, 5 Oct 2023 12:19:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34418 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240999AbjJEQPl (ORCPT ); Thu, 5 Oct 2023 12:15:41 -0400 Received: from relay8-d.mail.gandi.net (relay8-d.mail.gandi.net [IPv6:2001:4b98:dc4:8::228]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 019A36A8E; Thu, 5 Oct 2023 08:59:14 -0700 (PDT) Received: by mail.gandi.net (Postfix) with ESMTPSA id 0D4FE1BF209; Thu, 5 Oct 2023 15:59:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1696521553; 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=OycCdYD6veGEf3F9gxTneo0BiWUY4LInvlhaUBmPO8OzBRolI8XsQlIZ2JG9ME5gTiJjJn ugGB6Ji8y57zW48gHYyadYZXUALw7tJaYRy14SaspD5okplxaPhJfr69nreGYP7Ep2DxXE jXpSsYW9OyvPbqw26L0wYWB6tZD3ijN/vaieQBRZPtx+U784zlEZi6buDkZtL5rhZNYHlR xKIxUhhr6Oinqi2pB9U5UfdUHfUh9/IW9W+G3rwSoEtm1E1BusQXfxVLOhnUlg3WTFjama 7sCEQbvLLFRWiIFhOM6fzlyr6TsWcj+ta3OWJFKq+DZF+LdhC/z5OiZB9wyB9A== 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 v12 1/7] of: device: Export of_device_make_bus_id() Date: Thu, 5 Oct 2023 17:59:01 +0200 Message-Id: <20231005155907.2701706-2-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231005155907.2701706-1-miquel.raynal@bootlin.com> References: <20231005155907.2701706-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 X-GND-Sasl: miquel.raynal@bootlin.com X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED 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]); Thu, 05 Oct 2023 09:22:05 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778933256453147997 X-GMAIL-MSGID: 1778933256453147997 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 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 Thu Oct 5 15:59:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 148876 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2016:b0:403:3b70:6f57 with SMTP id fe22csp413245vqb; Thu, 5 Oct 2023 09:21:00 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHH0aRHikMdi6F74m4eHI/vHJixEoTXIhSxpkovlwXqHnVTqwZtVzaqK0b8vP7I7eSiP8Wc X-Received: by 2002:a05:6a00:1790:b0:68f:c057:b567 with SMTP id s16-20020a056a00179000b0068fc057b567mr6490827pfg.26.1696522859923; Thu, 05 Oct 2023 09:20:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696522859; cv=none; d=google.com; s=arc-20160816; b=mZ19h8LCChzEI49fQAfXF7LWfOCGh9FTeAW7yXCUXkUA/BF1MQ71XWFDeC/7dxj0qR d/Wsiu48OmAKd8IPLF7+3M7CTvflTAIQTKHxa0UfAZnV/E5DHIJPsqVlGNu6sOL4Pqgk c94iyiPf3AvhxJwUJ03MRM1noBIt5IhLou+jIFWWi5odHeIPNgP6okWUn+QRcZRoRhpF zdlK9QdevIAmMe8t1gzN9qxhqEwnMLFFCgTLWGGSZT7UonYvDCNz12OmNscyUScyphFM X58bGTtiRdiQCDqSu1Bz8Z9C4b1d8htMjX46ruRh4ZgmgpEgdRKueS0xuWm3TxCHgptv iIQA== 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=RNTdnfVQ4H7LtA9NOJwZVY/ap3UKde8fYrt3+jXaqS+poLVXHPwx1Loi4xZzF04RYW WqCCGxlRh3vgjG7pPp7X0EAIfp3RPGyCdlVxLviadBxfrHs22FgBbOV03dTUNBY1yazf vP/7yBmloOcI1SD5gPa07vGTu2jvRm4V6PLMabUC0U7eIvbOKGKncd2YwKRtjfkdsJM9 /XKi5oCdbiayGLAFuTZy12498fbc8f/1FFYUjBwpLCP6bzkmkf5boCqzli/9R1R3cO66 ANwfu9dNZ1ckBC5yMCehi2rjhrbwTgJ+d9oEAtZnuirHaKNHfmkttYpqgD7zsvhc6Ysl vAPw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=Ck3ZN1IJ; 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 cq8-20020a056a00330800b00690bf904bb6si1646837pfb.307.2023.10.05.09.20.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Oct 2023 09:20: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=Ck3ZN1IJ; 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 570DB80A9AA9; Thu, 5 Oct 2023 09:20:34 -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 S242678AbjJEQSp (ORCPT + 19 others); Thu, 5 Oct 2023 12:18:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36402 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242025AbjJEQPD (ORCPT ); Thu, 5 Oct 2023 12:15:03 -0400 Received: from relay8-d.mail.gandi.net (relay8-d.mail.gandi.net [217.70.183.201]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 99AEE7285; Thu, 5 Oct 2023 08:59:16 -0700 (PDT) Received: by mail.gandi.net (Postfix) with ESMTPSA id 838311BF213; Thu, 5 Oct 2023 15:59:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1696521554; 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=Ck3ZN1IJji2OY+dMnjA+DdC2x6VAfE4ZZp7r6kuL50L/U58uuF4Frsm3crOYTr2Xr6QRD0 +2km3T173WAQtA58ghw76qM5qNQ+nteaRCDHz1tugcKisNuWXIpTq8+UDJ1G8xw6o/VNT6 CzzExtKtwXEV5OrA0fYuK9Hjz/MY2F9mGltJW1omwlUgWWk3MUpjiZdlF3IOY4yGTFdMik 9M1Yp7SfHZwOfk4rh9oJaJdvXJUj6rEg6HvBo6kKScUGUobTXOrdG3JH3y3h2LsBM5yL0Z 4+V8yieOVFLwOKkYmxKjHHkXDeL7yw0rt57oMPF6CufiaU2NdAxEhjAINHjiKQ== 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 v12 2/7] nvmem: Clarify the situation when there is no DT node available Date: Thu, 5 Oct 2023 17:59:02 +0200 Message-Id: <20231005155907.2701706-3-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231005155907.2701706-1-miquel.raynal@bootlin.com> References: <20231005155907.2701706-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_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]); Thu, 05 Oct 2023 09:20:34 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778933153970393477 X-GMAIL-MSGID: 1778933153970393477 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 Thu Oct 5 15:59:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 148911 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2016:b0:403:3b70:6f57 with SMTP id fe22csp426962vqb; Thu, 5 Oct 2023 09:43:57 -0700 (PDT) X-Google-Smtp-Source: AGHT+IERnWx6cbkdbxpvRxcp/u4BufFOs1cAvJPdjtBCj4IMXJGLfYw69x7EjggmKuj0813LkwiQ X-Received: by 2002:a17:902:ecc8:b0:1c4:4a4d:cda with SMTP id a8-20020a170902ecc800b001c44a4d0cdamr6623943plh.15.1696524237250; Thu, 05 Oct 2023 09:43:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696524237; cv=none; d=google.com; s=arc-20160816; b=nyRfVXuhW4rYF/bDj38tfWSYXRsYNAkzA/MYYWvk+fvZVRy6Uj0qicyavoNTMB9wUv uHaN4klOMVWuzV2NzUgJytrrWajRIzciFGBauiQYFKk9FqFbNdX1DBZEpg4oNtOJOJOb sMTlAhVud2+o0QTMsSws2SbOMPjmznqlXP/Ks/h0mSyzZpn92URUQwhUPeTGcpxjE8ST BT39vpL/LmSxtboG9dtOqlD+sgSYJT0mUHYZniwY31vXsMj3P2oFceDZG7puBf/F6Yn8 jcVtkqX5BmtwSRQOR9wJvXQ1owfbBy4tWvSWVeQVlM8v0jhp+imhW1vYfELZI9Gjp+gb JHkw== 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=aGKTwONKSfoZzpn2n3JSkVgAv7XwGFmEAvr55ZBB+Go=; fh=XQzgxiPMj/Cqsom9sWl23DkvcXkGD6r/vx0NuuBn5R0=; b=yhurpvputVY463CaL4siHjyT1HW4ydBF+jtnuv5K9qlahzuYyDDu3DbwNjkCWhqjYq 5kvZeSksoj/+qdBZbOKKoFcfuj8oXHymUT+u7aKtxxCz74b6gur1Gal304Uvb1UTD58u k45Jky5wWSfDhelA7wbwtmXGq0Hc5RPJyQXmZuFqV6wG0jl3neB3ItOUkMS5TYGm9iJG 3p50IykRdWaXPMulDWoiWW7sA5KoHTIbE+rAXgSMxXIDD6T3Ezo8Q5aFqK0hpo6HLSKy 2ztLX9uPYPeeBSJdTeVQmRyLNUHUC9mDEHs9OqoKbHAsqlQUxUGArD+Bb+u05/smzcIF wQJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=kThq2dA9; 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 la16-20020a170902fa1000b001bbcddc33dasi1714160plb.180.2023.10.05.09.43.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Oct 2023 09:43:57 -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=kThq2dA9; 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 7335182A2C32; Thu, 5 Oct 2023 09:43: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 S232542AbjJEQnZ (ORCPT + 18 others); Thu, 5 Oct 2023 12:43:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33450 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234489AbjJEQld (ORCPT ); Thu, 5 Oct 2023 12:41:33 -0400 Received: from relay8-d.mail.gandi.net (relay8-d.mail.gandi.net [217.70.183.201]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 01D251FC2; Thu, 5 Oct 2023 08:59:17 -0700 (PDT) Received: by mail.gandi.net (Postfix) with ESMTPSA id F08F71BF212; Thu, 5 Oct 2023 15:59:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1696521556; 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=aGKTwONKSfoZzpn2n3JSkVgAv7XwGFmEAvr55ZBB+Go=; b=kThq2dA9mg6lXKkMVJK+SStPNlI+PSaAURbLkfhnE7QiJGX6Fdtn3qEPUp4K48j0lUvhAd w6Zq4TiANeUnV1YI3jWKyP03QGAuAWir3N9sIKN5eJV1Mwn2QgJ7Pof75WvzSh5XJKJOGk LWf0op/24SDiJ7wRbwEgs9FYHJNTTSnWZT0ehK23okOMaUma+2KOKZoRoMjh9S3hFHgJqr QqA4y0Gyht9/MY88Rb6JCJDjj9ZtFyGyjrzXpVH3UR+byy86F6//LuWJ4b95pqW0sWfqhj N2szJGoGyy0ga7cl+9O7nC2nLQaYIR26c++KrDRmH5fw3CzlMpzCm3aYCkZ0Aw== 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 v12 3/7] nvmem: Move of_nvmem_layout_get_container() in another header Date: Thu, 5 Oct 2023 17:59:03 +0200 Message-Id: <20231005155907.2701706-4-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231005155907.2701706-1-miquel.raynal@bootlin.com> References: <20231005155907.2701706-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_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]); Thu, 05 Oct 2023 09:43:54 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778934598609142132 X-GMAIL-MSGID: 1778934598609142132 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..2905f9e6fc2a 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 Thu Oct 5 15:59:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 148889 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2016:b0:403:3b70:6f57 with SMTP id fe22csp414342vqb; Thu, 5 Oct 2023 09:22:54 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFXUxNqiECf4nFPrVAJujVpL9vFGr0ASIqvINUOfWlm3sdVsXLIYUq473YgPe4MPKOJyLc6 X-Received: by 2002:a17:902:c212:b0:1c6:1611:e540 with SMTP id 18-20020a170902c21200b001c61611e540mr4916622pll.25.1696522974137; Thu, 05 Oct 2023 09:22:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696522974; cv=none; d=google.com; s=arc-20160816; b=Uj3fwTMkNIkkhjgRATo0wWOsu1iD40YX6E+Pk94uNUXQR9yrUZ2eDH4TxMZxrmIlLs gyNi9+8kZp288QnsSVhr+qBUQBzXx9Ozt4iIN4QErBl8ndIW+9Q+Dwm/3pJVRtQ3sLUJ shYX8LrLWOhHVDiVhQcq3PmkQFzEhybRFsfYyUQnRh41Y/5O+S5b5MNjFRTkUP9MseTr sV51xs6LzCgTNBVE7nUAx4bjdjr2GaVCtNURvdya14xq+LLUxb/2O0/fITE1DE1D38d1 7tPRI5Ei3+3lGJkbjZWwzMp1NFwWDfmWmFiCuVb9sr9fcviT7kRAEwf5Ada8cwVARfyN kDSg== 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=J0+UgdIzyNDpPu4jd0sjt9gAMGluuPWZ92FJdaayG8tYTSnVjSHzbZJsj7w9JcfN0U pflsqNduucNpk9eEcDjxUgxKtNUGhA1o/IILBVjlEjHFLTHOT3io0XNe8f1umiZaJGjc M9ffY7ECPX19L3A3QEvLeopjzXWiBtguao0yyYxjR2Bf7yN+PCjDESdX1dWW9PX7DLtK dQ/rKwlsIOtnu6MdP2cvWtypnwXFqlCxZr2+UArZoy0wBD6qISCpBqg00FL4ZtiJpIev mYxWntAmLpv2/s4zJxaXbq+S/tSHMr7qdto62ie55sTDfIUuEBnKmXIoH8Qvi24RcHlQ MkeA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=LM5De+Ri; 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 jz11-20020a170903430b00b001c724f99800si1656690plb.44.2023.10.05.09.22.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Oct 2023 09:22:54 -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=LM5De+Ri; 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 7B2FD8054BEC; Thu, 5 Oct 2023 09:22:53 -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 S243070AbjJEQWW (ORCPT + 19 others); Thu, 5 Oct 2023 12:22:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36418 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241468AbjJEQQJ (ORCPT ); Thu, 5 Oct 2023 12:16:09 -0400 Received: from relay8-d.mail.gandi.net (relay8-d.mail.gandi.net [217.70.183.201]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 543D23C3A; Thu, 5 Oct 2023 08:59:19 -0700 (PDT) Received: by mail.gandi.net (Postfix) with ESMTPSA id 8FAAE1BF207; Thu, 5 Oct 2023 15:59:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1696521558; 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=LM5De+RiQX+nTg+ib49/YUIVzd9DAeclp5XZnovNXjakXB0AegN3UHBdGFH7ho5anJUPoB gsQpXZxIrsM8EcyAe35ohIMbcSAKMw2R94fP4XR2qaJTWMnUrAp7ISJ7/qVEU5PeNXZUWB NeVDRmajCs1Tw65WQu5jQgNY55M0anL3RMFDiY1PbKfDuHcNNGjSiyQg77NbPl8SdxkrWS HrjmIzpZYJo3TsaF2hQp2BlgsQL4Q03RAYiU92fOhWX4J8F9CxPvi6sviNfjoBBmO6qwyW veHxHLj93pJaKo/wFpXGhonCy2Z+sJ7+WmtU+gVdAiMbVsNZqoLcPz4MBV981w== 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 v12 4/7] nvmem: Create a header for internal sharing Date: Thu, 5 Oct 2023 17:59:04 +0200 Message-Id: <20231005155907.2701706-5-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231005155907.2701706-1-miquel.raynal@bootlin.com> References: <20231005155907.2701706-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_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]); Thu, 05 Oct 2023 09:22:53 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778933273930763328 X-GMAIL-MSGID: 1778933273930763328 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 Thu Oct 5 15:59:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 148886 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2016:b0:403:3b70:6f57 with SMTP id fe22csp414244vqb; Thu, 5 Oct 2023 09:22:44 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF5UhlxkwaZiRf2iUeaEgoFJCGqYv8QSsXW25IqKn6gCbvUHxzomhHnaqy2cHPy5FE2syU3 X-Received: by 2002:a05:6a20:4292:b0:13f:c40c:379 with SMTP id o18-20020a056a20429200b0013fc40c0379mr6893280pzj.13.1696522964512; Thu, 05 Oct 2023 09:22:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696522964; cv=none; d=google.com; s=arc-20160816; b=g4Q1Zoc+7Firy9WYYpHRYv/BjU6waq9GEg4LlkiDXbd8C7NOKp0khUXegfgBsH8YSv VGe29SsljV1i0yYupPv/vdyg2qbT+gKoC612QHcfg/LzgOCZghO1BvFgRsynLFuPSgoS 7n3LSzBz4AufnXpB+NHqhyzLbmgl6m2l7F2bHldgmivgXCncNA91OMMn6n7yA1j39N1n 7ycmUELmX0KlQKxkEo3MwwitgE4pi5Z5A/R6OPNWp/HXb5emg5wLJeQHJvx14vv8c2EV wmELIKsq4k6igkIGzQADigQ9ad4iCxMHvRv6jEk0U0ne9BVTpvANuaZi/zxokaWyx4Cu Uqbg== 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=Mkn/+vhJW3sVKv08jw//0zzOWdX1AuMFFQwZrqYl60U=; fh=XQzgxiPMj/Cqsom9sWl23DkvcXkGD6r/vx0NuuBn5R0=; b=J3GgU8/VjZJhkFAh19c1pOnK8+M2XgN/Gwz40pDytsNrDhGK43Q0z3q2/QhKOlQQa7 aFRNcGXLX0TJNTpy5xWLIcuFjL0aEd511aWrJmel3ODFrQeyzh8nhd3QPY4m3Jv+Qrb/ zPUuY6zNWO7yHuY9R8Vmhi+qjN4NRdaF4+A033Y90sWH4k0JC1kccRUasMw/NcSt8UDO v3CeRuB7/PdxI3jW7/h3I0UZfO7VbrZNWlMPM67mOoG2DBPPDgBdpKyvW3hmDb/Xu2Px GJGJx8Ixiw3D++c4sUz06+NeQsC9UQJAAOY0loyLaFFVlLg8vpCPHGOnO0Xn0btk8158 rUcg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=BuAUlGFW; 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 dw8-20020a056a00368800b0069024c6a9a8si1692506pfb.389.2023.10.05.09.22.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Oct 2023 09:22:44 -0700 (PDT) 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=BuAUlGFW; 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 B0365830C339; Thu, 5 Oct 2023 09:22:43 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242916AbjJEQWI (ORCPT + 19 others); Thu, 5 Oct 2023 12:22:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36140 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241283AbjJEQQH (ORCPT ); Thu, 5 Oct 2023 12:16:07 -0400 Received: from relay8-d.mail.gandi.net (relay8-d.mail.gandi.net [IPv6:2001:4b98:dc4:8::228]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F29792695; Thu, 5 Oct 2023 08:59:20 -0700 (PDT) Received: by mail.gandi.net (Postfix) with ESMTPSA id 235561BF208; Thu, 5 Oct 2023 15:59:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1696521559; 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=Mkn/+vhJW3sVKv08jw//0zzOWdX1AuMFFQwZrqYl60U=; b=BuAUlGFWrQtv2lq9V+KnngOUldENbnUX19nOBaGp2e/UY3Of/BRGKkuOa7AHEARz1a36P8 4+PpCxMLXEQs0SFUi179tMo9IfmbrdIBsF1rrZQAxzZu3HD/kzZpQmg34BQE9l2GOzGlZn 6P3+GtBKsrEelOZ2gdErzNcXI5iEGY1LOzhAnipqSl5HXVRn3pJjoDHlQr5o625D1t7q3+ wrVGaywJUAahOGcn5DnsH4E8juqZ1T7T7yzYHAL2y9Qx0L7q343P8m8gxGVyNTLAC9l82L oUQz+gz3K9yhWNTaWl2hjbXcdB/AbgnHSecOEcO18QNwUU+86jy8zPm6GJa0Pg== 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 v12 5/7] nvmem: core: Rework layouts to become regular devices Date: Thu, 5 Oct 2023 17:59:05 +0200 Message-Id: <20231005155907.2701706-6-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231005155907.2701706-1-miquel.raynal@bootlin.com> References: <20231005155907.2701706-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,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 (snail.vger.email [0.0.0.0]); Thu, 05 Oct 2023 09:22:43 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778933264185514583 X-GMAIL-MSGID: 1778933264185514583 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 | 126 +++++-------------- drivers/nvmem/internals.h | 22 ++++ drivers/nvmem/layouts.c | 201 +++++++++++++++++++++++++++++++ drivers/nvmem/layouts/onie-tlv.c | 36 +++++- drivers/nvmem/layouts/sl28vpd.c | 36 +++++- include/linux/nvmem-provider.h | 25 ++-- 7 files changed, 331 insertions(+), 117 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..6c6b0bac24f5 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) { @@ -744,91 +741,29 @@ 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; + struct nvmem_device *nvmem = dev_get_platdata(layout->dev); - spin_lock(&nvmem_layout_lock); - list_add(&layout->node, &nvmem_layouts); - spin_unlock(&nvmem_layout_lock); + if (!layout->add_cells) + return -EINVAL; - blocking_notifier_call_chain(&nvmem_notifier, NVMEM_LAYOUT_ADD, layout); + /* Link internally the nvmem device to its layout */ + nvmem->layout = layout; - return 0; + /* Populate the cells */ + return nvmem->layout->add_cells(&nvmem->dev, nvmem, nvmem->layout); } -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); + struct nvmem_device *nvmem = dev_get_platdata(layout->dev); - spin_lock(&nvmem_layout_lock); - list_del(&layout->node); - spin_unlock(&nvmem_layout_lock); + nvmem->layout = NULL; } 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; - - /* - * 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, layout); - 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) { @@ -836,7 +771,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 +882,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,7 +900,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) if (rval) goto err_remove_cells; - rval = nvmem_add_cells_from_layout(nvmem); + rval = nvmem_populate_layout(nvmem); if (rval) goto err_remove_cells; @@ -986,16 +908,17 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) rval = device_add(&nvmem->dev); if (rval) - goto err_remove_cells; + goto err_destroy_layout; + blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem); return nvmem; +err_destroy_layout: + nvmem_destroy_layout(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: @@ -1017,7 +940,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); } @@ -2099,11 +2022,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 ce353831cd65..10a317d46fb6 100644 --- a/drivers/nvmem/internals.h +++ b/drivers/nvmem/internals.h @@ -28,8 +28,30 @@ struct nvmem_device { nvmem_reg_read_t reg_read; nvmem_reg_write_t reg_write; struct gpio_desc *wp_gpio; + struct device *layout_dev; struct nvmem_layout *layout; 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 int 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..5f2ec4213469 --- /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" + +#if IS_ENABLED(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) +{ + 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 int nvmem_layout_create_device(struct nvmem_device *nvmem, + struct device_node *np) +{ + struct device *dev; + int ret; + + dev = kzalloc(sizeof(*dev), GFP_KERNEL); + if (!dev) + return -ENOMEM; + + 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; + dev->platform_data = nvmem; + 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; + } + + nvmem->layout_dev = dev; + + 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 *nvmem_dn, *layout_dn; + int ret; + + layout_dn = of_nvmem_layout_get_container(nvmem); + if (!layout_dn) + return 0; + + nvmem_dn = of_node_get(nvmem->dev.of_node); + if (!nvmem_dn) { + of_node_put(layout_dn); + return 0; + } + + /* Ensure the layout driver is loaded */ + of_request_module(layout_dn); + + /* 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(nvmem_dn); + of_node_put(layout_dn); + return ret; +} + +void nvmem_destroy_layout(struct nvmem_device *nvmem) +{ + struct device_node *layout_dn; + + layout_dn = of_nvmem_layout_get_container(nvmem); + if (!layout_dn) + return; + + of_node_clear_flag(layout_dn, OF_POPULATED_BUS); + put_device(nvmem->layout_dev); + + of_node_put(layout_dn); +} + +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..191b2540d347 100644 --- a/drivers/nvmem/layouts/onie-tlv.c +++ b/drivers/nvmem/layouts/onie-tlv.c @@ -226,16 +226,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..330badebfcf6 100644 --- a/drivers/nvmem/layouts/sl28vpd.c +++ b/drivers/nvmem/layouts/sl28vpd.c @@ -135,16 +135,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 2905f9e6fc2a..10537abea008 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); @@ -197,11 +199,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); @@ -257,9 +263,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 Thu Oct 5 15:59:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 148887 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2016:b0:403:3b70:6f57 with SMTP id fe22csp414262vqb; Thu, 5 Oct 2023 09:22:46 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE5N+V9rx2KX89PtUGKuw+xv+7DmGelnSL+9q3HROuhk/0yqO1Qvqi2wE0Gi5N8mjaVNWip X-Received: by 2002:a17:90b:4ad0:b0:274:a021:9383 with SMTP id mh16-20020a17090b4ad000b00274a0219383mr5696439pjb.17.1696522966349; Thu, 05 Oct 2023 09:22:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696522966; cv=none; d=google.com; s=arc-20160816; b=p9e2DGgHitD4NI8qs7UgVYwxCrCXYx0sqFpIg2hhMPzfQmcuBt/eWXsCt07fDWjOyY L8dK6otIXUerpjMfBcR7kPxYSDzPh9OPcysdnxF8WQS6lLgNitKSXRr6wEkwe9h1TEDU VkLoiV4bXle9MAXt483A61Q/Tgqi7FlHQnCsR/XX6qioDMpdBsv2yefyNvSJcdNmky1b XGmosxcqjyIxkhNbE6vyz7EwI1EFbsQC+7J/kIdPr0MqmlFqIgJMCpPr4DoF95hmvwL2 48NbKsQVQuwPf+3dqjrJ6HoMKdfML3udFaewrjwxWI2JfcjHo33Rpt73/uk7KdBLwt4R x2tg== 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=no0In6QdzUQBzeapPVrLyKWz1Sh/3etsueSfl/EVMMJ8ixXRh/NXtUBaijFiM0UV83 S33BdhwFIIVAWrcUhVMvHb2CRNcHmF63gpEJ31X2U/umfYeOU3B88uEWU7JL6faPvmCQ fFRddLwfQi/1m1V2juK01LLamgZVt/JmB+XXdddvmpmqWMzlMOYHmPtuUnYgHAKrvon7 ocVw44GcSdp56g/N07ubRBLbtVdJCvZvZLDiaKN710+LNpf2+EBj+iXh5EkDqEpjg/NS G/UDA9IXOXXXZ9GbCcY0MsUBcl6JYc9wfIlHFGoi35SQtSyHpGGDfNKZoGI73PjeFbjs 73nQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=oSba1V3M; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 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 agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id f18-20020a17090ace1200b00278ea235413si4026937pju.31.2023.10.05.09.22.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Oct 2023 09:22:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=oSba1V3M; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 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 agentk.vger.email (Postfix) with ESMTP id 87D0C82F09B6; Thu, 5 Oct 2023 09:22:25 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242509AbjJEQUy (ORCPT + 19 others); Thu, 5 Oct 2023 12:20:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38620 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240385AbjJEQQC (ORCPT ); Thu, 5 Oct 2023 12:16:02 -0400 Received: from relay8-d.mail.gandi.net (relay8-d.mail.gandi.net [IPv6:2001:4b98:dc4:8::228]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1D98E47B8; Thu, 5 Oct 2023 08:59:22 -0700 (PDT) Received: by mail.gandi.net (Postfix) with ESMTPSA id BBCE81BF20B; Thu, 5 Oct 2023 15:59:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1696521561; 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=oSba1V3MvsJggcGvucw1bE9xLdxB38sDD+5KUIvRUW3fdC0PY9DF7Kvz+B9RBagJFc5jVD EnOZYK6T6jcctQnr/xAEVDTh9UzCnn62SkQ+xwjVxTLBZv/kNmpNPwqCuf1NcwWC8LLFdK Vka7RG7le3hr5HHMo/NVPbvkCgKIXnJB5UpeysNgBbRMjj+Dgy9mBBKWZ+HvIkoetD8RKU LYiSQoaNYdi00Qs0qWeIWR8dveGfxYt37+OHlZobWaG5Vxd6KFkU12Vgl5sSZOqamBN3BK 1OjZEyeulYFJMi/5xRc3cP4FrhCbys+uA40qQHTt9q8S/5ULUcZerM+sS2UMcw== 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 v12 6/7] ABI: sysfs-nvmem-cells: Expose cells through sysfs Date: Thu, 5 Oct 2023 17:59:06 +0200 Message-Id: <20231005155907.2701706-7-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231005155907.2701706-1-miquel.raynal@bootlin.com> References: <20231005155907.2701706-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 X-GND-Sasl: miquel.raynal@bootlin.com X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.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 (agentk.vger.email [0.0.0.0]); Thu, 05 Oct 2023 09:22:25 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778933265660006297 X-GMAIL-MSGID: 1778933265660006297 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 Thu Oct 5 15:59:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 148877 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2016:b0:403:3b70:6f57 with SMTP id fe22csp413322vqb; Thu, 5 Oct 2023 09:21:07 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFY3vu6guJexUNb4YKMMQ2ZMmdRQQFIJFNPcZx/qqkKEaV/qcdlBefKj7mIU5h/iLPJUyzn X-Received: by 2002:a17:902:6f02:b0:1c5:de4c:f079 with SMTP id w2-20020a1709026f0200b001c5de4cf079mr4070956plk.33.1696522867498; Thu, 05 Oct 2023 09:21:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696522867; cv=none; d=google.com; s=arc-20160816; b=mRsVLk5Fvun90P2tFg+4e8cXygctd5nnVzwO1iJPv+TrQ5ukeVxCq1YteWO5SWxad+ QbymRsY1/A+BmznURUOdPbqyWZ3Nodbg2IwtO/CjmomcOS6D/4LQ/FW+VAx7gSSDjmyJ NQb8QbEnHDff3lQxQRfxV51hklTQ5m7csQLQYEvhd/GBURs41SNWBh/TFAt7d75DIEEY D7/nEFe6uRatQfbQNG22az7oht5tSVKJoehRg+S31UkO/blFmPwhYXZtsgAclIzzreoT IvIE1pMidCIZ/xDFQZufS0YJkUI5MnT5J4c/eMrBXtLqzxxHL5uoDAHABih2ImtVK48m SL/w== 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=d9qv6wW6nLIBV472azKIbA9MexkFiNMfPwaC3rv72TU=; fh=XQzgxiPMj/Cqsom9sWl23DkvcXkGD6r/vx0NuuBn5R0=; b=ZasUY+SWClvmdTG/wvgTvY1s0pMxBvvk/fpnqr3FQpU5yRUZuRWpnKTa7zqVPaVUVV P4YnIHcuDyR5biKrhQ2jWDUi1KO5MdiOklVaJ3kJVg+ovIv0ZzIfK4igr4hOc5k/5SZD jEdjQY36AUzJyUcb9jSzxyY/b6KznbMLCPxMYmcGuswwM+tAScBAIajet8LNmvD05vKA gTAc5AQV/+UpmcEVnHGE6zKqpHVZvfqaPDf1QO/jDAVi27D8iCzeAzMdghXLT1h0DN3f o1JK3gX8E8BcabCr2sYMBGM8EvsmjOEQwNhFD2Uuf3EYsar4+R+6NCFJ03tatSoZHz+G IAjw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=S3TLUt4d; 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 k4-20020a170902d58400b001c7845637eesi1735048plh.168.2023.10.05.09.21.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Oct 2023 09:21: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=S3TLUt4d; 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 524FD8254AAF; Thu, 5 Oct 2023 09:20:46 -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 S242944AbjJEQTd (ORCPT + 19 others); Thu, 5 Oct 2023 12:19:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34360 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232106AbjJEQP6 (ORCPT ); Thu, 5 Oct 2023 12:15:58 -0400 Received: from relay8-d.mail.gandi.net (relay8-d.mail.gandi.net [IPv6:2001:4b98:dc4:8::228]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F0E9C658D; Thu, 5 Oct 2023 08:59:23 -0700 (PDT) Received: by mail.gandi.net (Postfix) with ESMTPSA id 391541BF204; Thu, 5 Oct 2023 15:59:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1696521562; 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=d9qv6wW6nLIBV472azKIbA9MexkFiNMfPwaC3rv72TU=; b=S3TLUt4dJ2IVoFIkrPLeOOKXiT/pxodrgelbBcY++SpVPkVHMki9qhcKBpWnTZOfrBBTIe Qgtv6UUeBIrOqODaYwTB2CacZwEf/ZLKuFz97HxlZEuGOwANdTuiTVxDn4jP738sD+33uy 8UHg53YrUeCIvuos7J3f7V0QFA0Q/jSWclutctO1Eg2dCjbhM5QMuq6K122G7AEU8Fn+P8 KjW32EfQ/NP2YArvC0MX9K1Xqch6/VmkSKZ4ZEWLXwl0ntTnozZm/zCvvuCGBVKJ/fqaPa wBz6U2dGpQdjSh2pMvoemo4gXfVzy1xLPs82vQSH2PM0bz1TXJaF6BriS7z3xg== 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 v12 7/7] nvmem: core: Expose cells through sysfs Date: Thu, 5 Oct 2023 17:59:07 +0200 Message-Id: <20231005155907.2701706-8-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231005155907.2701706-1-miquel.raynal@bootlin.com> References: <20231005155907.2701706-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,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]); Thu, 05 Oct 2023 09:20:46 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778933162187966172 X-GMAIL-MSGID: 1778933162187966172 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 | 131 +++++++++++++++++++++++++++++++++++++- drivers/nvmem/internals.h | 1 + 2 files changed, 131 insertions(+), 1 deletion(-) diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index 6c6b0bac24f5..9ac74a6e38a0 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, @@ -744,6 +853,7 @@ static int nvmem_add_cells_from_fixed_layout(struct nvmem_device *nvmem) int nvmem_layout_register(struct nvmem_layout *layout) { struct nvmem_device *nvmem = dev_get_platdata(layout->dev); + int ret; if (!layout->add_cells) return -EINVAL; @@ -752,7 +862,19 @@ int nvmem_layout_register(struct nvmem_layout *layout) nvmem->layout = layout; /* Populate the cells */ - return nvmem->layout->add_cells(&nvmem->dev, nvmem, nvmem->layout); + ret = nvmem->layout->add_cells(&nvmem->dev, nvmem, nvmem->layout); + if (ret) + return ret; + +#ifdef CONFIG_NVMEM_SYSFS + ret = nvmem_populate_sysfs_cells(nvmem); + if (ret) { + nvmem_device_remove_all_cells(nvmem); + return ret; + } +#endif + + return 0; } EXPORT_SYMBOL_GPL(nvmem_layout_register); @@ -910,11 +1032,18 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) if (rval) goto err_destroy_layout; +#ifdef CONFIG_NVMEM_SYSFS + rval = nvmem_populate_sysfs_cells(nvmem); + if (rval) + goto err_remove_dev; +#endif blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem); return nvmem; +err_remove_dev: + device_del(&nvmem->dev); err_destroy_layout: nvmem_destroy_layout(nvmem); err_remove_cells: diff --git a/drivers/nvmem/internals.h b/drivers/nvmem/internals.h index 10a317d46fb6..bd24fc915298 100644 --- a/drivers/nvmem/internals.h +++ b/drivers/nvmem/internals.h @@ -31,6 +31,7 @@ struct nvmem_device { struct device *layout_dev; struct nvmem_layout *layout; void *priv; + bool sysfs_cells_populated; }; #if IS_ENABLED(CONFIG_OF)