From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH] nvmem: core: add nvmem_cell_nbits()
Date: Thu, 15 Oct 2026 16:07:42 +0200 [thread overview]
Message-ID: <20261015140743.1214225-1-a.fatoum@pengutronix.de> (raw)
A cell with a bits property is narrower than the bytes nvmem_cell_read()
returns and nvmem_cell_write() doesn't mask the value written to it, so
anything that doesn't fit spills into the neighbouring bits.
A consumer that computes the value it writes back, like a counter, thus
needs to know the width of the cell. Let's add a helper that returns it.
Assisted-by: Claude:opus-5.5
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
drivers/nvmem/core.c | 16 ++++++++++++++++
include/linux/nvmem-consumer.h | 6 ++++++
2 files changed, 22 insertions(+)
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 79a36bb9b996..9fb7049c0116 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -928,6 +928,22 @@ void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
}
EXPORT_SYMBOL_GPL(nvmem_cell_read);
+/**
+ * nvmem_cell_nbits() - Get the width of a nvmem cell in bits
+ *
+ * @cell: nvmem cell to be queried.
+ *
+ * Return: the number of bits in the cell, which can be less than its size in
+ * bytes times 8 if the cell has a bits property.
+ */
+unsigned int nvmem_cell_nbits(struct nvmem_cell *cell)
+{
+ struct nvmem_cell_entry *entry = cell->entry;
+
+ return entry->nbits ?: entry->bytes * BITS_PER_BYTE;
+}
+EXPORT_SYMBOL_GPL(nvmem_cell_nbits);
+
static inline const void *nvmem_cell_prepare_write_buffer(struct nvmem_cell_entry *cell,
const u8 *_buf, int len)
{
diff --git a/include/linux/nvmem-consumer.h b/include/linux/nvmem-consumer.h
index ff9b87442852..efce9bb0e593 100644
--- a/include/linux/nvmem-consumer.h
+++ b/include/linux/nvmem-consumer.h
@@ -28,6 +28,7 @@ extern struct class nvmem_class;
struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *name);
void nvmem_cell_put(struct nvmem_cell *cell);
void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len);
+unsigned int nvmem_cell_nbits(struct nvmem_cell *cell);
void *nvmem_cell_get_and_read(struct device_node *np, const char *cell_name,
size_t bytes);
int nvmem_cell_read_variable_le_u32(struct device *dev, const char *cell_id,
@@ -68,6 +69,11 @@ static inline char *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
return ERR_PTR(-EOPNOTSUPP);
}
+static inline unsigned int nvmem_cell_nbits(struct nvmem_cell *cell)
+{
+ return 0;
+}
+
static inline void *nvmem_cell_get_and_read(struct device_node *np,
const char *cell_name,
size_t bytes)
--
2.47.3
reply other threads:[~2026-10-15 14:09 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261015140743.1214225-1-a.fatoum@pengutronix.de \
--to=a.fatoum@pengutronix.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox