Introduce new API functions to allow looking up NVMEM devices and cells
by name, enhancing flexibility in cases where devicetree-based lookup
is not available.
Key changes:
- Added `nvmem_device_get_by_name()`: Enables retrieving an NVMEM device by its
name for systems where direct device reference is needed.
- Added `nvmem_cell_get_by_sysfs_name()`: Allows retrieving an NVMEM cell based
on its sysfs-style name (e.g., "cell@offset,bits"), making it possible to
identify cells dynamically.
- Introduced `nvmem_find_cell_entry_by_sysfs_name()`: A helper function that
constructs sysfs-like names and searches for matching cell entries.
Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
drivers/nvmem/core.c | 91 ++++++++++++++++++++++++++++++++++
include/linux/nvmem-consumer.h | 14 ++++++
2 files changed, 105 insertions(+)
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 928ac15f04a0..2ff0926dea23 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -1203,6 +1203,20 @@ struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id)
EXPORT_SYMBOL_GPL(of_nvmem_device_get);
#endif
+/**
+ * nvmem_device_get_by_name - Look up an NVMEM device by its device name
+ * @name: String matching device name in the provider
+ *
+ * Return: A valid pointer to struct nvmem_device on success,
+ * or ERR_PTR(...) on failure. The caller must later call nvmem_device_put() to
+ * release the reference.
+ */
+struct nvmem_device *nvmem_device_get_by_name(const char *name)
+{
+ return __nvmem_device_get((void *)name, device_match_name);
+}
+EXPORT_SYMBOL_GPL(nvmem_device_get_by_name);
+
/**
* nvmem_device_get() - Get nvmem device from a given id
*
@@ -1516,6 +1530,83 @@ struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
EXPORT_SYMBOL_GPL(of_nvmem_cell_get);
#endif
+/**
+ * nvmem_find_cell_entry_by_sysfs_name - Find an NVMEM cell entry by its sysfs
+ * name.
+ * @nvmem: The nvmem_device pointer where the cell is located.
+ * @sysfs_name: The full sysfs cell name, e.g. "mycell@0x100,8".
+ *
+ * This function constructs the sysfs-like name for each cell and compares it
+ * to @sysfs_name. If a match is found, the matching nvmem_cell_entry pointer
+ * is returned. This is analogous to nvmem_find_cell_entry_by_name(), except
+ * it matches on the sysfs naming convention used in the device's attributes.
+ *
+ * Return: Pointer to the matching nvmem_cell_entry on success, or NULL if no
+ * match is found.
+ */
+static struct nvmem_cell_entry *
+nvmem_find_cell_entry_by_sysfs_name(struct nvmem_device *nvmem,
+ const char *sysfs_name)
+{
+ struct nvmem_cell_entry *entry;
+ char *tmp;
+
+ mutex_lock(&nvmem_mutex);
+ list_for_each_entry(entry, &nvmem->cells, node) {
+ /* Reconstruct how the sysfs name is assigned */
+ tmp = kasprintf(GFP_KERNEL, "%s@%x,%u", entry->name,
+ entry->offset, entry->bit_offset);
+ if (!tmp)
+ continue;
+
+ if (!strcmp(tmp, sysfs_name)) {
+ kfree(tmp);
+ mutex_unlock(&nvmem_mutex);
+ return entry;
+ }
+ kfree(tmp);
+ }
+ mutex_unlock(&nvmem_mutex);
+
+ return NULL;
+}
+
+/**
+ * nvmem_cell_get_by_sysfs_name - Get a cell by its sysfs name from a given
+ * nvmem_device.
+ * @nvmem: The nvmem_device pointer where the cell is located.
+ * @sysfs_name: The sysfs-style name, e.g. "mycell@0x100,8".
+ *
+ * This function uses nvmem_find_cell_entry_by_sysfs_name() to locate the cell
+ * entry, increments the reference counts for the matching NVMEM device, and
+ * then creates a struct nvmem_cell for the caller.
+ *
+ * Return: On success, a valid pointer to an nvmem_cell. On failure, an
+ * ERR_PTR() encoded error (e.g., -ENOENT if the cell entry is not found).
+ */
+struct nvmem_cell *nvmem_cell_get_by_sysfs_name(struct nvmem_device *nvmem,
+ const char *sysfs_name)
+{
+ struct nvmem_cell_entry *entry;
+ struct nvmem_cell *cell;
+
+ entry = nvmem_find_cell_entry_by_sysfs_name(nvmem, sysfs_name);
+ if (!entry)
+ return ERR_PTR(-ENOENT);
+
+ if (!try_module_get(nvmem->owner))
+ return ERR_PTR(-EINVAL);
+
+ kref_get(&nvmem->refcnt);
+
+ cell = nvmem_create_cell(entry, entry->name, 0);
+ if (IS_ERR(cell))
+ __nvmem_device_put(nvmem);
+
+ return cell;
+}
+EXPORT_SYMBOL_GPL(nvmem_cell_get_by_sysfs_name);
+
/**
* nvmem_cell_get() - Get nvmem cell of device form a given cell name
*
diff --git a/include/linux/nvmem-consumer.h b/include/linux/nvmem-consumer.h
index bcb0e17e415d..6fc12cce8eb2 100644
--- a/include/linux/nvmem-consumer.h
+++ b/include/linux/nvmem-consumer.h
@@ -52,6 +52,8 @@ enum {
/* Cell based interface */
struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *id);
struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id);
+struct nvmem_cell *nvmem_cell_get_by_sysfs_name(struct nvmem_device *nvmem,
+ const char *cell_name);
void nvmem_cell_put(struct nvmem_cell *cell);
void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell);
void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len);
@@ -70,6 +72,7 @@ int nvmem_cell_read_variable_le_u64(struct device *dev, const char *cell_id,
struct nvmem_device *nvmem_device_get(struct device *dev, const char *name);
struct nvmem_device *devm_nvmem_device_get(struct device *dev,
const char *name);
+struct nvmem_device *nvmem_device_get_by_name(const char *name);
void nvmem_device_put(struct nvmem_device *nvmem);
void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem);
int nvmem_device_read(struct nvmem_device *nvmem, unsigned int offset,
@@ -109,6 +112,12 @@ static inline struct nvmem_cell *devm_nvmem_cell_get(struct device *dev,
return ERR_PTR(-EOPNOTSUPP);
}
+struct nvmem_cell *nvmem_cell_get_by_sysfs_name(struct nvmem_device *nvmem,
+ const char *cell_name)
+{
+ return ERR_PTR(-EOPNOTSUPP);
+}
+
static inline void devm_nvmem_cell_put(struct device *dev,
struct nvmem_cell *cell)
{
@@ -185,6 +194,11 @@ static inline struct nvmem_device *devm_nvmem_device_get(struct device *dev,
return ERR_PTR(-EOPNOTSUPP);
}
+struct nvmem_device *nvmem_device_get_by_name(const char *name)
+{
+ return ERR_PTR(-EOPNOTSUPP);
+}
+
static inline void nvmem_device_put(struct nvmem_device *nvmem)
{
}
--
2.39.5
Hi Oleksij,
kernel test robot noticed the following build warnings:
[auto build test WARNING on sre-power-supply/for-next]
[also build test WARNING on broonie-regulator/for-next rafael-pm/thermal linus/master v6.14-rc5 next-20250307]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Oleksij-Rempel/power-Extend-power_on_reason-h-for-upcoming-PSCRR-framework/20250306-174233
base: https://git.kernel.org/pub/scm/linux/kernel/git/sre/linux-power-supply.git for-next
patch link: https://lore.kernel.org/r/20250306093900.2199442-5-o.rempel%40pengutronix.de
patch subject: [PATCH v4 4/7] nvmem: add support for device and sysfs-based cell lookups
config: hexagon-randconfig-002-20250308 (https://download.01.org/0day-ci/archive/20250309/202503090029.GNYIypVB-lkp@intel.com/config)
compiler: clang version 21.0.0git (https://github.com/llvm/llvm-project e15545cad8297ec7555f26e5ae74a9f0511203e7)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250309/202503090029.GNYIypVB-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202503090029.GNYIypVB-lkp@intel.com/
All warnings (new ones prefixed by >>):
In file included from drivers/rtc/rtc-pm8xxx.c:10:
>> include/linux/nvmem-consumer.h:115:20: warning: no previous prototype for function 'nvmem_cell_get_by_sysfs_name' [-Wmissing-prototypes]
115 | struct nvmem_cell *nvmem_cell_get_by_sysfs_name(struct nvmem_device *nvmem,
| ^
include/linux/nvmem-consumer.h:115:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
115 | struct nvmem_cell *nvmem_cell_get_by_sysfs_name(struct nvmem_device *nvmem,
| ^
| static
>> include/linux/nvmem-consumer.h:197:22: warning: no previous prototype for function 'nvmem_device_get_by_name' [-Wmissing-prototypes]
197 | struct nvmem_device *nvmem_device_get_by_name(const char *name)
| ^
include/linux/nvmem-consumer.h:197:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
197 | struct nvmem_device *nvmem_device_get_by_name(const char *name)
| ^
| static
2 warnings generated.
vim +/nvmem_cell_get_by_sysfs_name +115 include/linux/nvmem-consumer.h
114
> 115 struct nvmem_cell *nvmem_cell_get_by_sysfs_name(struct nvmem_device *nvmem,
116 const char *cell_name)
117 {
118 return ERR_PTR(-EOPNOTSUPP);
119 }
120
121 static inline void devm_nvmem_cell_put(struct device *dev,
122 struct nvmem_cell *cell)
123 {
124
125 }
126 static inline void nvmem_cell_put(struct nvmem_cell *cell)
127 {
128 }
129
130 static inline void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
131 {
132 return ERR_PTR(-EOPNOTSUPP);
133 }
134
135 static inline int nvmem_cell_write(struct nvmem_cell *cell,
136 void *buf, size_t len)
137 {
138 return -EOPNOTSUPP;
139 }
140
141 static inline int nvmem_cell_get_size(struct nvmem_cell *cell, size_t *bytes,
142 size_t *bits)
143 {
144 return -EOPNOTSUPP;
145 }
146
147 static inline int nvmem_cell_read_u8(struct device *dev,
148 const char *cell_id, u8 *val)
149 {
150 return -EOPNOTSUPP;
151 }
152
153 static inline int nvmem_cell_read_u16(struct device *dev,
154 const char *cell_id, u16 *val)
155 {
156 return -EOPNOTSUPP;
157 }
158
159 static inline int nvmem_cell_read_u32(struct device *dev,
160 const char *cell_id, u32 *val)
161 {
162 return -EOPNOTSUPP;
163 }
164
165 static inline int nvmem_cell_read_u64(struct device *dev,
166 const char *cell_id, u64 *val)
167 {
168 return -EOPNOTSUPP;
169 }
170
171 static inline int nvmem_cell_read_variable_le_u32(struct device *dev,
172 const char *cell_id,
173 u32 *val)
174 {
175 return -EOPNOTSUPP;
176 }
177
178 static inline int nvmem_cell_read_variable_le_u64(struct device *dev,
179 const char *cell_id,
180 u64 *val)
181 {
182 return -EOPNOTSUPP;
183 }
184
185 static inline struct nvmem_device *nvmem_device_get(struct device *dev,
186 const char *name)
187 {
188 return ERR_PTR(-EOPNOTSUPP);
189 }
190
191 static inline struct nvmem_device *devm_nvmem_device_get(struct device *dev,
192 const char *name)
193 {
194 return ERR_PTR(-EOPNOTSUPP);
195 }
196
> 197 struct nvmem_device *nvmem_device_get_by_name(const char *name)
198 {
199 return ERR_PTR(-EOPNOTSUPP);
200 }
201
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Hi Oleksij, kernel test robot noticed the following build errors: [auto build test ERROR on sre-power-supply/for-next] [also build test ERROR on broonie-regulator/for-next rafael-pm/thermal linus/master v6.14-rc5 next-20250307] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch#_base_tree_information] url: https://github.com/intel-lab-lkp/linux/commits/Oleksij-Rempel/power-Extend-power_on_reason-h-for-upcoming-PSCRR-framework/20250306-174233 base: https://git.kernel.org/pub/scm/linux/kernel/git/sre/linux-power-supply.git for-next patch link: https://lore.kernel.org/r/20250306093900.2199442-5-o.rempel%40pengutronix.de patch subject: [PATCH v4 4/7] nvmem: add support for device and sysfs-based cell lookups config: sparc-randconfig-002-20250307 (https://download.01.org/0day-ci/archive/20250307/202503072216.f6lwkxwJ-lkp@intel.com/config) compiler: sparc-linux-gcc (GCC) 14.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250307/202503072216.f6lwkxwJ-lkp@intel.com/reproduce) If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags | Reported-by: kernel test robot <lkp@intel.com> | Closes: https://lore.kernel.org/oe-kbuild-all/202503072216.f6lwkxwJ-lkp@intel.com/ All errors (new ones prefixed by >>): sparc-linux-ld: drivers/phy/mediatek/phy-mtk-mipi-dsi-mt8173.o: in function `nvmem_cell_get_by_sysfs_name': >> phy-mtk-mipi-dsi-mt8173.c:(.text+0x434): multiple definition of `nvmem_cell_get_by_sysfs_name'; drivers/phy/mediatek/phy-mtk-mipi-dsi.o:phy-mtk-mipi-dsi.c:(.text+0x2a0): first defined here sparc-linux-ld: drivers/phy/mediatek/phy-mtk-mipi-dsi-mt8173.o: in function `nvmem_device_get_by_name': >> phy-mtk-mipi-dsi-mt8173.c:(.text+0x43c): multiple definition of `nvmem_device_get_by_name'; drivers/phy/mediatek/phy-mtk-mipi-dsi.o:phy-mtk-mipi-dsi.c:(.text+0x2a8): first defined here sparc-linux-ld: drivers/phy/mediatek/phy-mtk-mipi-dsi-mt8183.o: in function `nvmem_cell_get_by_sysfs_name': phy-mtk-mipi-dsi-mt8183.c:(.text+0x470): multiple definition of `nvmem_cell_get_by_sysfs_name'; drivers/phy/mediatek/phy-mtk-mipi-dsi.o:phy-mtk-mipi-dsi.c:(.text+0x2a0): first defined here sparc-linux-ld: drivers/phy/mediatek/phy-mtk-mipi-dsi-mt8183.o: in function `nvmem_device_get_by_name': phy-mtk-mipi-dsi-mt8183.c:(.text+0x478): multiple definition of `nvmem_device_get_by_name'; drivers/phy/mediatek/phy-mtk-mipi-dsi.o:phy-mtk-mipi-dsi.c:(.text+0x2a8): first defined here -- 0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Hi Oleksij,
kernel test robot noticed the following build errors:
[auto build test ERROR on sre-power-supply/for-next]
[also build test ERROR on broonie-regulator/for-next rafael-pm/thermal linus/master v6.14-rc5 next-20250306]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Oleksij-Rempel/power-Extend-power_on_reason-h-for-upcoming-PSCRR-framework/20250306-174233
base: https://git.kernel.org/pub/scm/linux/kernel/git/sre/linux-power-supply.git for-next
patch link: https://lore.kernel.org/r/20250306093900.2199442-5-o.rempel%40pengutronix.de
patch subject: [PATCH v4 4/7] nvmem: add support for device and sysfs-based cell lookups
config: s390-randconfig-001-20250307 (https://download.01.org/0day-ci/archive/20250307/202503072059.sFFL62LS-lkp@intel.com/config)
compiler: s390-linux-gcc (GCC) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250307/202503072059.sFFL62LS-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202503072059.sFFL62LS-lkp@intel.com/
All error/warnings (new ones prefixed by >>):
In file included from net/ethernet/eth.c:46:
>> include/linux/nvmem-consumer.h:115:20: warning: no previous prototype for 'nvmem_cell_get_by_sysfs_name' [-Wmissing-prototypes]
115 | struct nvmem_cell *nvmem_cell_get_by_sysfs_name(struct nvmem_device *nvmem,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> include/linux/nvmem-consumer.h:197:22: warning: no previous prototype for 'nvmem_device_get_by_name' [-Wmissing-prototypes]
197 | struct nvmem_device *nvmem_device_get_by_name(const char *name)
| ^~~~~~~~~~~~~~~~~~~~~~~~
--
s390-linux-ld: net/ethernet/eth.o: in function `nvmem_cell_get_by_sysfs_name':
>> eth.c:(.text+0x16a0): multiple definition of `nvmem_cell_get_by_sysfs_name'; drivers/net/phy/dp83867.o:dp83867.c:(.text+0x18d0): first defined here
s390-linux-ld: net/ethernet/eth.o: in function `nvmem_device_get_by_name':
>> eth.c:(.text+0x16a8): multiple definition of `nvmem_device_get_by_name'; drivers/net/phy/dp83867.o:dp83867.c:(.text+0x18d8): first defined here
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
© 2016 - 2026 Red Hat, Inc.