Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp3923574pxf; Mon, 29 Mar 2021 15:40:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzFhifiPFHTqQ9bbg1zfFNOfnMrpcGxWKzldmQylGRZ1f+7T9BY/RTpQgNCdxRXHTJlo/WE X-Received: by 2002:a17:906:4cd6:: with SMTP id q22mr30585695ejt.469.1617057655435; Mon, 29 Mar 2021 15:40:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617057655; cv=none; d=google.com; s=arc-20160816; b=sHF6+7yaMM2pH9v1/hpjlD/W39xgjtF4T7si/6td9ibNvGc6bHHSJvfi7g7mDwY8CE Gcd/vNQ0nFSFlxhWY598ucvnNILM5/dw7kLh7XZW4dv19ZBNE6S3O3DHTKfLxBD2DztE +57tut4Ze6PUdMR3GKu/lQ2gEdYbNEY1CfcXlm91iTwV5NUB5N8UqB40uPdkr5VlXP/z tKmuq3q1F0vImlsh5X4KdH2cUG1LrKm0M2hhMjoV2g12r9k0QpOz9qaPcAtnJ/JzL46I dXGxRKTuWNwSra1rdSdTahYL/7vmR1XgaHTTx2f5zdeuv19B+tUm9TbiLQIfCxV706Gh Srlg== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=kt+/KTchiEi7SRqxK7aMCZQW49hw+aThg0jidlExq98=; b=CxtI3AcJeqfVW8La/gay2gPzMmkn2i2IIt7H/h4w2TqOahUTmT9SXxmIbSjHZ1SJ9e g7bivL54uiLcY13316KlIMs3plDov5AjWViitdIDnz6WfUyEbpQgO+w9XbzVl+Njxm2H LLf2cyTyw31tLF4iBTXOtyn1vp8Acvrgj6Fq4lTlyO8GbuGvV91bgTE19WKIwMlUjGer UJhgGDw7X65Ugk3Qp0cr1v5Hf8/q4XbsIUw3X3nqdBSe06E5IXmzQq/5nHdyF13HrVAy GiudFtghuzb/quiaFzWqJ7fKNFhIF70FZXn9RIq5MbEj82wq9emuLscnpme6FW0keVV1 qUyw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@platinasystems-com.20150623.gappssmtp.com header.s=20150623 header.b=oR3NzwyZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y6si13816068edt.1.2021.03.29.15.40.33; Mon, 29 Mar 2021 15:40:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@platinasystems-com.20150623.gappssmtp.com header.s=20150623 header.b=oR3NzwyZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231631AbhC2Wjh (ORCPT + 99 others); Mon, 29 Mar 2021 18:39:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38228 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231652AbhC2WjD (ORCPT ); Mon, 29 Mar 2021 18:39:03 -0400 Received: from mail-pj1-x1034.google.com (mail-pj1-x1034.google.com [IPv6:2607:f8b0:4864:20::1034]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 45EDDC061765 for ; Mon, 29 Mar 2021 15:39:03 -0700 (PDT) Received: by mail-pj1-x1034.google.com with SMTP id cl21-20020a17090af695b02900c61ac0f0e9so280614pjb.1 for ; Mon, 29 Mar 2021 15:39:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=platinasystems-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=kt+/KTchiEi7SRqxK7aMCZQW49hw+aThg0jidlExq98=; b=oR3NzwyZzBfo9kEnzyV5vBSb2VSgssDHyQsvopt2CAWVjo4x0SC37sfkune2S2xnwE L3Jd51xuuJetASuuHItNskLXdViv4w/XUt33e34X8PniD6yzh2FcN6qICSAZXlu3HEAi vz3GiIVy8yWp0VzE4uuioxX6VR9jpJLVtXaj2lbvPVnSx7gn0ML3EXwiOiiyh8VarrHG C7Oc4BbaOoJ8l4tSxuQf7wLKv/XHnofQWNZOD0ubZ8Jy16q5fCcTFWkh84NdLUYoWWi4 91IMVzaganWxJWZySiRxdHUpa9d5rmlPD7/QrtdVKlP/JOaNPaoL3kX5bYCieuqUz9K3 /vXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=kt+/KTchiEi7SRqxK7aMCZQW49hw+aThg0jidlExq98=; b=UnEi2yJg/MRNjyF3XrXFAL+NiDWFisc1J3WGgSgcswIpA9i0cb5eWgmCLhuzKlWoq6 xVvdz0wELCf3oc/0pTFR74klDkgm0uIId27cAg4DTUJ61Tx3ICmJIFk4oCQjD0Bld7ix KmQz/YpjgPRUdz4YOT2EffClALqgkC9THN8kTCeIYMkLOHyoat3MS8Ah5y8S01Cz27X7 zvFmlWYufDoYF99CaRvoxJnD5jiQnuj4QJbM6QztQJgsWRJezfBwnXnHbPJF7c8nNSQE Winz7B1jiYWDpDhMZGBEmL677PmRshp7WB0Aesj0jWZYBgRRyKZLo+AgJr3yvTWDVIsu oVFQ== X-Gm-Message-State: AOAM530tS4ql1P/lWP4fpD3AQV3WPDtGOEXTgImwpie+wG+t1eZVPti6 YQBfGgBdMRPRzyYTvKNw2oyPBA== X-Received: by 2002:a17:90a:5d8f:: with SMTP id t15mr1293430pji.28.1617057542658; Mon, 29 Mar 2021 15:39:02 -0700 (PDT) Received: from localhost.localdomain ([207.53.255.56]) by smtp.gmail.com with ESMTPSA id y23sm18229857pfo.50.2021.03.29.15.39.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Mar 2021 15:39:02 -0700 (PDT) From: Kevin Paul Herbert To: Srinivas Kandagatla Cc: "Rafael J . Wysocki" , Len Brown , linux-acpi@vger.kernel.org, Rob Herring , Frank Rowand , devicetree@vger.kernel.org, Tom Grennan , linux-kernel@vger.kernel.org, Kevin Paul Herbert Subject: [PATCH] nvmem: Change to unified property interface Date: Mon, 29 Mar 2021 15:38:27 -0700 Message-Id: <20210329223827.2851916-1-kph@platinasystems.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Change from using device tree (Open Firmware) APIs to the unified 'fwnode' interface. Change of_nvmem_cell_get() to fwnode_nvmem_cell_get(), and add a wrapper for of_nvmem_cell_get(). Change of_nvmem_device_get() to fwnode_nvmem_device_get(). There are no known accessors to the OF interface, so no need for a wrapper. Signed-off-by: Kevin Paul Herbert --- drivers/nvmem/core.c | 176 ++++++++++++++++++++------------- include/linux/nvmem-consumer.h | 27 ++--- 2 files changed, 123 insertions(+), 80 deletions(-) diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index a5ab1e0c74cf..2e49304cd9a8 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -6,6 +6,7 @@ * Copyright (C) 2013 Maxime Ripard */ +#include #include #include #include @@ -17,6 +18,7 @@ #include #include #include +#include #include struct nvmem_device { @@ -52,7 +54,7 @@ struct nvmem_cell { int bytes; int bit_offset; int nbits; - struct device_node *np; + struct fwnode_handle *fwnode; struct nvmem_device *nvmem; struct list_head node; }; @@ -424,7 +426,7 @@ static void nvmem_cell_drop(struct nvmem_cell *cell) mutex_lock(&nvmem_mutex); list_del(&cell->node); mutex_unlock(&nvmem_mutex); - of_node_put(cell->np); + fwnode_handle_put(cell->fwnode); kfree_const(cell->name); kfree(cell); } @@ -670,39 +672,40 @@ static int nvmem_validate_keepouts(struct nvmem_device *nvmem) return 0; } -static int nvmem_add_cells_from_of(struct nvmem_device *nvmem) +static int nvmem_add_cells_from_fw(struct nvmem_device *nvmem) { - struct device_node *parent, *child; + struct fwnode_handle *parent, *child; struct device *dev = &nvmem->dev; struct nvmem_cell *cell; - const __be32 *addr; - int len; + int rval; + u32 vals[2]; - parent = dev->of_node; + parent = dev_fwnode(dev); - for_each_child_of_node(parent, child) { - addr = of_get_property(child, "reg", &len); - if (!addr) + fwnode_for_each_child_node(parent, child) { + rval = fwnode_property_read_u32_array(child, "reg", NULL, 2); + if (rval < 0) continue; - if (len < 2 * sizeof(u32)) { - dev_err(dev, "nvmem: invalid reg on %pOF\n", child); + if (rval < 2) { + dev_err(dev, "nvmem: invalid reg %d on %pfw\n", + rval, child); return -EINVAL; } - + rval = fwnode_property_read_u32_array(child, "reg", vals, 2); cell = kzalloc(sizeof(*cell), GFP_KERNEL); if (!cell) return -ENOMEM; cell->nvmem = nvmem; - cell->np = of_node_get(child); - cell->offset = be32_to_cpup(addr++); - cell->bytes = be32_to_cpup(addr); - cell->name = kasprintf(GFP_KERNEL, "%pOFn", child); - - addr = of_get_property(child, "bits", &len); - if (addr && len == (2 * sizeof(u32))) { - cell->bit_offset = be32_to_cpup(addr++); - cell->nbits = be32_to_cpup(addr); + cell->fwnode = child; + cell->offset = vals[0]; + cell->bytes = vals[1]; + cell->name = kasprintf(GFP_KERNEL, "%pfwn", child); + + rval = fwnode_property_read_u32_array(child, "bits", vals, 2); + if (rval >= 0) { + cell->bit_offset = vals[0]; + cell->nbits = vals[1]; } if (cell->nbits) @@ -715,7 +718,7 @@ static int nvmem_add_cells_from_of(struct nvmem_device *nvmem) cell->name, nvmem->stride); /* Cells already added will be freed later. */ kfree_const(cell->name); - of_node_put(cell->np); + fwnode_handle_put(cell->fwnode); kfree(cell); return -EINVAL; } @@ -789,8 +792,10 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) nvmem->reg_write = config->reg_write; nvmem->keepout = config->keepout; nvmem->nkeepout = config->nkeepout; - if (!config->no_of_node) + if (!config->no_of_node) { nvmem->dev.of_node = config->dev->of_node; + nvmem->dev.fwnode = config->dev->fwnode; + } switch (config->id) { case NVMEM_DEVID_NONE: @@ -841,7 +846,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) if (rval) goto err_remove_cells; - rval = nvmem_add_cells_from_of(nvmem); + rval = nvmem_add_cells_from_fw(nvmem); if (rval) goto err_remove_cells; @@ -984,36 +989,46 @@ static void __nvmem_device_put(struct nvmem_device *nvmem) kref_put(&nvmem->refcnt, nvmem_device_release); } -#if IS_ENABLED(CONFIG_OF) /** - * of_nvmem_device_get() - Get nvmem device from a given id + * fwnode_nvmem_device_get() - Get nvmem device from a given id * - * @np: Device tree node that uses the nvmem device. + * @fwnode: Firmware node that uses the nvmem device. * @id: nvmem name from nvmem-names property. * * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device * on success. */ -struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id) +struct nvmem_device *fwnode_nvmem_device_get(struct fwnode_handle *fwnode, const char *id) { - - struct device_node *nvmem_np; + struct fwnode_handle *nvmem_fwnode; struct nvmem_device *nvmem; int index = 0; if (id) - index = of_property_match_string(np, "nvmem-names", id); - - nvmem_np = of_parse_phandle(np, "nvmem", index); - if (!nvmem_np) - return ERR_PTR(-ENOENT); + index = fwnode_property_match_string(fwnode, "nvmem-names", id); + + if (is_of_node(fwnode)) { + struct device_node *nvmem_np = of_parse_phandle(to_of_node(fwnode), + "nvmem", index); + if (!nvmem_np) + return ERR_PTR(-ENOENT); + nvmem_fwnode = &nvmem_np->fwnode; + } else if (is_acpi_device_node(fwnode)) { + struct fwnode_reference_args args; + int rval = acpi_node_get_property_reference(fwnode, + "nvmem", index, &args); + if (rval) + return ERR_PTR(rval); + nvmem_fwnode = args.fwnode; + } else { + return ERR_PTR(-ENXIO); + } - nvmem = __nvmem_device_get(nvmem_np, device_match_of_node); - of_node_put(nvmem_np); + nvmem = __nvmem_device_get(nvmem_fwnode, device_match_fwnode); + fwnode_handle_put(nvmem_fwnode); return nvmem; } -EXPORT_SYMBOL_GPL(of_nvmem_device_get); -#endif +EXPORT_SYMBOL_GPL(fwnode_nvmem_device_get); /** * nvmem_device_get() - Get nvmem device from a given id @@ -1026,16 +1041,15 @@ EXPORT_SYMBOL_GPL(of_nvmem_device_get); */ struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name) { - if (dev->of_node) { /* try dt first */ - struct nvmem_device *nvmem; + struct fwnode_handle *fwnode = dev_fwnode(dev); - nvmem = of_nvmem_device_get(dev->of_node, dev_name); + if (fwnode) { /* try firmware tree first */ + struct nvmem_device *nvmem; + nvmem = fwnode_nvmem_device_get(fwnode, dev_name); if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER) return nvmem; - } - return __nvmem_device_get((void *)dev_name, device_match_name); } EXPORT_SYMBOL_GPL(nvmem_device_get); @@ -1171,15 +1185,14 @@ nvmem_cell_get_from_lookup(struct device *dev, const char *con_id) return cell; } -#if IS_ENABLED(CONFIG_OF) static struct nvmem_cell * -nvmem_find_cell_by_node(struct nvmem_device *nvmem, struct device_node *np) +nvmem_find_cell_by_fwnode(struct nvmem_device *nvmem, struct fwnode_handle *fwnode) { struct nvmem_cell *iter, *cell = NULL; mutex_lock(&nvmem_mutex); list_for_each_entry(iter, &nvmem->cells, node) { - if (np == iter->np) { + if (fwnode == iter->fwnode) { cell = iter; break; } @@ -1190,42 +1203,67 @@ nvmem_find_cell_by_node(struct nvmem_device *nvmem, struct device_node *np) } /** - * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id + * fwnode_nvmem_cell_get() - Get a nvmem cell from given firmwar node and cell id * - * @np: Device tree node that uses the nvmem cell. + * @fwnode: Firmware node that uses the nvmem cell. * @id: nvmem cell name from nvmem-cell-names property, or NULL - * for the cell at index 0 (the lone cell with no accompanying - * nvmem-cell-names property). + * for the cell at index 0 (the lone cell with no accompanying + * nvmem-cell-names property). * * Return: Will be an ERR_PTR() on error or a valid pointer * to a struct nvmem_cell. The nvmem_cell will be freed by the * nvmem_cell_put(). */ -struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id) +struct nvmem_cell *fwnode_nvmem_cell_get(struct fwnode_handle *fwnode, + const char *id) { - struct device_node *cell_np, *nvmem_np; + struct fwnode_handle *nvmem_fwnode, *cell_fwnode; struct nvmem_device *nvmem; struct nvmem_cell *cell; int index = 0; /* if cell name exists, find index to the name */ - if (id) - index = of_property_match_string(np, "nvmem-cell-names", id); + if (id) { + index = fwnode_property_match_string(fwnode, "nvmem-cell-names", id); + if (index < 0) + return ERR_PTR(index); + } - cell_np = of_parse_phandle(np, "nvmem-cells", index); - if (!cell_np) - return ERR_PTR(-ENOENT); + if (is_of_node(fwnode)) { + struct device_node *np = to_of_node(fwnode); + struct device_node *cell_np = of_parse_phandle(np, "nvmem-cells", index); + + if (!cell_np) + return ERR_PTR(-EINVAL); + cell_fwnode = &cell_np->fwnode; + } else if (is_acpi_device_node(fwnode)) { + struct fwnode_reference_args args; + struct fwnode_handle *dev_fwnode; + int rval; - nvmem_np = of_get_next_parent(cell_np); - if (!nvmem_np) + rval = acpi_node_get_property_reference(fwnode, + "nvmem-cells", index, &args); + if (rval) + return ERR_PTR(rval); + dev_fwnode = args.fwnode; + cell_fwnode = fwnode_get_named_child_node(dev_fwnode, + id ? id : "nvmem"); + if (!cell_fwnode) + return ERR_PTR(-EINVAL); + } else { + return ERR_PTR(-ENXIO); + } + + nvmem_fwnode = fwnode_get_next_parent(cell_fwnode); + if (!nvmem_fwnode) return ERR_PTR(-EINVAL); - nvmem = __nvmem_device_get(nvmem_np, device_match_of_node); - of_node_put(nvmem_np); + nvmem = __nvmem_device_get(nvmem_fwnode, device_match_fwnode); + fwnode_handle_put(nvmem_fwnode); if (IS_ERR(nvmem)) return ERR_CAST(nvmem); - cell = nvmem_find_cell_by_node(nvmem, cell_np); + cell = nvmem_find_cell_by_fwnode(nvmem, cell_fwnode); if (!cell) { __nvmem_device_put(nvmem); return ERR_PTR(-ENOENT); @@ -1233,8 +1271,7 @@ struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id) return cell; } -EXPORT_SYMBOL_GPL(of_nvmem_cell_get); -#endif +EXPORT_SYMBOL_GPL(fwnode_nvmem_cell_get); /** * nvmem_cell_get() - Get nvmem cell of device form a given cell name @@ -1251,14 +1288,15 @@ EXPORT_SYMBOL_GPL(of_nvmem_cell_get); struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *id) { struct nvmem_cell *cell; + struct fwnode_handle *fwnode = dev_fwnode(dev); - if (dev->of_node) { /* try dt first */ - cell = of_nvmem_cell_get(dev->of_node, id); + if (fwnode) { /* try firmware tree first */ + cell = fwnode_nvmem_cell_get(fwnode, id); if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER) return cell; } - /* NULL cell id only allowed for device tree; invalid otherwise */ + /* NULL cell_id only allowed for firmware tree; invalid otherwise */ if (!id) return ERR_PTR(-EINVAL); diff --git a/include/linux/nvmem-consumer.h b/include/linux/nvmem-consumer.h index 052293f4cbdb..e52c587d0a23 100644 --- a/include/linux/nvmem-consumer.h +++ b/include/linux/nvmem-consumer.h @@ -12,12 +12,14 @@ #include #include #include +#include struct device; struct device_node; /* consumer cookie */ struct nvmem_cell; struct nvmem_device; +struct fwnode_handle; struct nvmem_cell_info { const char *name; @@ -94,6 +96,10 @@ int nvmem_unregister_notifier(struct notifier_block *nb); struct nvmem_device *nvmem_device_find(void *data, int (*match)(struct device *dev, const void *data)); +struct nvmem_cell *fwnode_nvmem_cell_get(struct fwnode_handle *np, + const char *name); +struct nvmem_device *fwnode_nvmem_device_get(struct fwnode_handle *np, + const char *name); #else static inline struct nvmem_cell *nvmem_cell_get(struct device *dev, @@ -221,25 +227,24 @@ static inline struct nvmem_device *nvmem_device_find(void *data, return NULL; } -#endif /* CONFIG_NVMEM */ +static inline struct nvmem_cell *fwnode_nvmem_cell_get(struct fwnode_handle *np, + const char *name) +{ + return ERR_PTR(-EOPNOTSUPP); +} -#if IS_ENABLED(CONFIG_NVMEM) && IS_ENABLED(CONFIG_OF) -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); -#else -static inline struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, - const char *id) +static inline struct nvmem_device *fwnode_nvmem_device_get(struct fwnode_handle *np, + const char *name) { return ERR_PTR(-EOPNOTSUPP); } +#endif /* CONFIG_NVMEM */ + static inline struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *name) { - return ERR_PTR(-EOPNOTSUPP); + return np ? fwnode_nvmem_device_get(&np->fwnode, name) : NULL; } -#endif /* CONFIG_NVMEM && CONFIG_OF */ #endif /* ifndef _LINUX_NVMEM_CONSUMER_H */ -- 2.25.1