f791aea8ae8349f54a5d4d008841fe547c759644
[openwrt/staging/neocturne.git] /
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
5
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.
9
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
17 wants the number.
18
19 We'll provide two functions: nvmem_cell_read_variable_le_u32() and
20 nvmem_cell_read_variable_le_u64().
21
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
30 this and works fine.
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
35 bit_offset was zero.
36
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
39 8-bit or 16-bit data.
40
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.
44
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().
48
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>
53 ---
54 drivers/nvmem/core.c | 95 ++++++++++++++++++++++++++++++++++
55 include/linux/nvmem-consumer.h | 4 ++
56 2 files changed, 99 insertions(+)
57
58 --- a/drivers/nvmem/core.c
59 +++ b/drivers/nvmem/core.c
60 @@ -1613,6 +1613,101 @@ int nvmem_cell_read_u64(struct device *d
61 }
62 EXPORT_SYMBOL_GPL(nvmem_cell_read_u64);
63
64 +static void *nvmem_cell_read_variable_common(struct device *dev,
65 + const char *cell_id,
66 + size_t max_len, size_t *len)
67 +{
68 + struct nvmem_cell *cell;
69 + int nbits;
70 + void *buf;
71 +
72 + cell = nvmem_cell_get(dev, cell_id);
73 + if (IS_ERR(cell))
74 + return cell;
75 +
76 + nbits = cell->nbits;
77 + buf = nvmem_cell_read(cell, len);
78 + nvmem_cell_put(cell);
79 + if (IS_ERR(buf))
80 + return buf;
81 +
82 + /*
83 + * If nbits is set then nvmem_cell_read() can significantly exaggerate
84 + * the length of the real data. Throw away the extra junk.
85 + */
86 + if (nbits)
87 + *len = DIV_ROUND_UP(nbits, 8);
88 +
89 + if (*len > max_len) {
90 + kfree(buf);
91 + return ERR_PTR(-ERANGE);
92 + }
93 +
94 + return buf;
95 +}
96 +
97 +/**
98 + * nvmem_cell_read_variable_le_u32() - Read up to 32-bits of data as a little endian number.
99 + *
100 + * @dev: Device that requests the nvmem cell.
101 + * @cell_id: Name of nvmem cell to read.
102 + * @val: pointer to output value.
103 + *
104 + * Return: 0 on success or negative errno.
105 + */
106 +int nvmem_cell_read_variable_le_u32(struct device *dev, const char *cell_id,
107 + u32 *val)
108 +{
109 + size_t len;
110 + u8 *buf;
111 + int i;
112 +
113 + buf = nvmem_cell_read_variable_common(dev, cell_id, sizeof(*val), &len);
114 + if (IS_ERR(buf))
115 + return PTR_ERR(buf);
116 +
117 + /* Copy w/ implicit endian conversion */
118 + *val = 0;
119 + for (i = 0; i < len; i++)
120 + *val |= buf[i] << (8 * i);
121 +
122 + kfree(buf);
123 +
124 + return 0;
125 +}
126 +EXPORT_SYMBOL_GPL(nvmem_cell_read_variable_le_u32);
127 +
128 +/**
129 + * nvmem_cell_read_variable_le_u64() - Read up to 64-bits of data as a little endian number.
130 + *
131 + * @dev: Device that requests the nvmem cell.
132 + * @cell_id: Name of nvmem cell to read.
133 + * @val: pointer to output value.
134 + *
135 + * Return: 0 on success or negative errno.
136 + */
137 +int nvmem_cell_read_variable_le_u64(struct device *dev, const char *cell_id,
138 + u64 *val)
139 +{
140 + size_t len;
141 + u8 *buf;
142 + int i;
143 +
144 + buf = nvmem_cell_read_variable_common(dev, cell_id, sizeof(*val), &len);
145 + if (IS_ERR(buf))
146 + return PTR_ERR(buf);
147 +
148 + /* Copy w/ implicit endian conversion */
149 + *val = 0;
150 + for (i = 0; i < len; i++)
151 + *val |= buf[i] << (8 * i);
152 +
153 + kfree(buf);
154 +
155 + return 0;
156 +}
157 +EXPORT_SYMBOL_GPL(nvmem_cell_read_variable_le_u64);
158 +
159 /**
160 * nvmem_device_cell_read() - Read a given nvmem device and cell
161 *
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,
169 + u32 *val);
170 +int nvmem_cell_read_variable_le_u64(struct device *dev, const char *cell_id,
171 + u64 *val);
172
173 /* direct nvmem device read/write interface */
174 struct nvmem_device *nvmem_device_get(struct device *dev, const char *name);