[PATCH 27/44] nvmem: export functions to query NVMEM size
Ahmad Fatoum
a.fatoum at barebox.org
Mon Aug 11 05:28:07 PDT 2025
Allow users to query how much bytes are in a NVMEM device given a struct
device that was retrieved by iterating the NVMEM class.
Signed-off-by: Ahmad Fatoum <a.fatoum at barebox.org>
---
drivers/nvmem/core.c | 12 ++++++++++++
include/linux/nvmem-consumer.h | 13 ++++++++++++-
2 files changed, 24 insertions(+), 1 deletion(-)
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 253c00575d88..1e701426c9ad 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -374,6 +374,12 @@ struct nvmem_device *nvmem_device_get(struct device *dev,
}
EXPORT_SYMBOL_GPL(nvmem_device_get);
+struct nvmem_device *nvmem_from_device(struct device *dev)
+{
+ return container_of_safe(dev, struct nvmem_device, dev);
+}
+EXPORT_SYMBOL_GPL(nvmem_from_device);
+
/**
* nvmem_device_put() - put alredy got nvmem device
*
@@ -829,6 +835,12 @@ int nvmem_device_write(struct nvmem_device *nvmem,
}
EXPORT_SYMBOL_GPL(nvmem_device_write);
+ssize_t nvmem_device_size(struct nvmem_device *nvmem)
+{
+ return nvmem->size;
+}
+EXPORT_SYMBOL_GPL(nvmem_device_size);
+
void *nvmem_cell_get_and_read(struct device_node *np, const char *cell_name,
size_t bytes)
{
diff --git a/include/linux/nvmem-consumer.h b/include/linux/nvmem-consumer.h
index c711b63aeeb0..25591d166a02 100644
--- a/include/linux/nvmem-consumer.h
+++ b/include/linux/nvmem-consumer.h
@@ -44,6 +44,7 @@ int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len);
/* direct nvmem device read/write interface */
struct nvmem_device *nvmem_device_get(struct device *dev, const char *name);
+struct nvmem_device *nvmem_from_device(struct device *dev);
void nvmem_device_put(struct nvmem_device *nvmem);
int nvmem_device_read(struct nvmem_device *nvmem, unsigned int offset,
size_t bytes, void *buf);
@@ -53,7 +54,7 @@ ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
struct nvmem_cell_info *info, void *buf);
int nvmem_device_cell_write(struct nvmem_device *nvmem,
struct nvmem_cell_info *info, void *buf);
-
+ssize_t nvmem_device_size(struct nvmem_device *nvmem);
void nvmem_devices_print(void);
#else
@@ -99,6 +100,11 @@ static inline struct nvmem_device *nvmem_device_get(struct device *dev,
return ERR_PTR(-EOPNOTSUPP);
}
+static inline struct nvmem_device *nvmem_from_device(struct device *dev)
+{
+ return IS_ERR(dev) ? ERR_CAST(dev) : ERR_PTR(-EOPNOTSUPP);
+}
+
static inline void nvmem_device_put(struct nvmem_device *nvmem)
{
}
@@ -131,6 +137,11 @@ static inline int nvmem_device_write(struct nvmem_device *nvmem,
return -EOPNOTSUPP;
}
+static inline ssize_t nvmem_device_size(struct nvmem_device *nvmem)
+{
+ return -EOPNOTSUPP;
+}
+
#endif /* CONFIG_NVMEM */
#if IS_ENABLED(CONFIG_NVMEM) && IS_ENABLED(CONFIG_OFTREE)
--
2.39.5
More information about the barebox
mailing list