1 From a28e824fb8270eda43fd0f65c2a5fdf33f55c5eb Mon Sep 17 00:00:00 2001
2 From: Douglas Anderson <dianders@chromium.org>
3 Date: Tue, 30 Mar 2021 12:12:37 +0100
4 Subject: [PATCH] nvmem: core: Add functions to make number reading easy
6 Sometimes the clients of nvmem just want to get a number out of
7 nvmem. They don't want to think about exactly how many bytes the nvmem
8 cell took up. They just want the number. Let's make it easy.
10 In general this concept is useful because nvmem space is precious and
11 usually the fewest bits are allocated that will hold a given value on
12 a given system. However, even though small numbers might be fine on
13 one system that doesn't mean that logically the number couldn't be
14 bigger. Imagine nvmem containing a max frequency for a component. On
15 one system perhaps that fits in 16 bits. On another system it might
16 fit in 32 bits. The code reading this number doesn't care--it just
19 We'll provide two functions: nvmem_cell_read_variable_le_u32() and
20 nvmem_cell_read_variable_le_u64().
22 Comparing these to the existing functions like nvmem_cell_read_u32():
23 * These new functions have no problems if the value was stored in
24 nvmem in fewer bytes. It's OK to use these function as long as the
25 value stored will fit in 32-bits (or 64-bits).
26 * These functions avoid problems that the earlier APIs had with bit
27 offsets. For instance, you can't use nvmem_cell_read_u32() to read a
28 value has nbits=32 and bit_offset=4 because the nvmem cell must be
29 at least 5 bytes big to hold this value. The new API accounts for
31 * These functions make it very explicit that they assume that the
32 number was stored in little endian format. The old functions made
33 this assumption whenever bit_offset was non-zero (see
34 nvmem_shift_read_buffer_in_place()) but didn't whenever the
37 NOTE: it's assumed that we don't need an 8-bit or 16-bit version of
38 this function. The 32-bit version of the function can be used to read
41 At the moment, I'm only adding the "unsigned" versions of these
42 functions, but if it ends up being useful someone could add a "signed"
43 version that did 2's complement sign extension.
45 At the moment, I'm only adding the "little endian" versions of these
46 functions. Adding the "big endian" version would require adding "big
47 endian" support to nvmem_shift_read_buffer_in_place().
49 Signed-off-by: Douglas Anderson <dianders@chromium.org>
50 Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
51 Link: https://lore.kernel.org/r/20210330111241.19401-7-srinivas.kandagatla@linaro.org
52 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
54 drivers/nvmem/core.c | 95 ++++++++++++++++++++++++++++++++++
55 include/linux/nvmem-consumer.h | 4 ++
56 2 files changed, 99 insertions(+)
58 --- a/drivers/nvmem/core.c
59 +++ b/drivers/nvmem/core.c
60 @@ -1612,6 +1612,101 @@ int nvmem_cell_read_u64(struct device *d
62 EXPORT_SYMBOL_GPL(nvmem_cell_read_u64);
64 +static void *nvmem_cell_read_variable_common(struct device *dev,
65 + const char *cell_id,
66 + size_t max_len, size_t *len)
68 + struct nvmem_cell *cell;
72 + cell = nvmem_cell_get(dev, cell_id);
76 + nbits = cell->nbits;
77 + buf = nvmem_cell_read(cell, len);
78 + nvmem_cell_put(cell);
83 + * If nbits is set then nvmem_cell_read() can significantly exaggerate
84 + * the length of the real data. Throw away the extra junk.
87 + *len = DIV_ROUND_UP(nbits, 8);
89 + if (*len > max_len) {
91 + return ERR_PTR(-ERANGE);
98 + * nvmem_cell_read_variable_le_u32() - Read up to 32-bits of data as a little endian number.
100 + * @dev: Device that requests the nvmem cell.
101 + * @cell_id: Name of nvmem cell to read.
102 + * @val: pointer to output value.
104 + * Return: 0 on success or negative errno.
106 +int nvmem_cell_read_variable_le_u32(struct device *dev, const char *cell_id,
113 + buf = nvmem_cell_read_variable_common(dev, cell_id, sizeof(*val), &len);
115 + return PTR_ERR(buf);
117 + /* Copy w/ implicit endian conversion */
119 + for (i = 0; i < len; i++)
120 + *val |= buf[i] << (8 * i);
126 +EXPORT_SYMBOL_GPL(nvmem_cell_read_variable_le_u32);
129 + * nvmem_cell_read_variable_le_u64() - Read up to 64-bits of data as a little endian number.
131 + * @dev: Device that requests the nvmem cell.
132 + * @cell_id: Name of nvmem cell to read.
133 + * @val: pointer to output value.
135 + * Return: 0 on success or negative errno.
137 +int nvmem_cell_read_variable_le_u64(struct device *dev, const char *cell_id,
144 + buf = nvmem_cell_read_variable_common(dev, cell_id, sizeof(*val), &len);
146 + return PTR_ERR(buf);
148 + /* Copy w/ implicit endian conversion */
150 + for (i = 0; i < len; i++)
151 + *val |= buf[i] << (8 * i);
157 +EXPORT_SYMBOL_GPL(nvmem_cell_read_variable_le_u64);
160 * nvmem_device_cell_read() - Read a given nvmem device and cell
162 --- a/include/linux/nvmem-consumer.h
163 +++ b/include/linux/nvmem-consumer.h
164 @@ -65,6 +65,10 @@ int nvmem_cell_read_u8(struct device *de
165 int nvmem_cell_read_u16(struct device *dev, const char *cell_id, u16 *val);
166 int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val);
167 int nvmem_cell_read_u64(struct device *dev, const char *cell_id, u64 *val);
168 +int nvmem_cell_read_variable_le_u32(struct device *dev, const char *cell_id,
170 +int nvmem_cell_read_variable_le_u64(struct device *dev, const char *cell_id,
173 /* direct nvmem device read/write interface */
174 struct nvmem_device *nvmem_device_get(struct device *dev, const char *name);