Skip to content

Commit 23317ef

Browse files
talih0pdgendt
authored andcommitted
devicetree: Add nvmem-consumer
Adds property nvmem-cells for referencing a node that stores some configuration information. A typical use case is the reading of MAC address from an EEPROM device. Signed-off-by: Andriy Gelman <[email protected]> Signed-off-by: Pieter De Gendt <[email protected]>
1 parent ab2622f commit 23317ef

File tree

2 files changed

+324
-0
lines changed

2 files changed

+324
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Copyright (c) 2024, Andriy Gelman <[email protected]>
2+
# Copyright (c) 2025 Basalte bv
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
properties:
6+
nvmem-cell-names:
7+
type: string-array
8+
description:
9+
Names for each nvmem-cells specified.
10+
11+
nvmem-cells:
12+
type: phandle-array
13+
description:
14+
List of phandles to the nvmem data cells.

include/zephyr/devicetree/nvmem.h

Lines changed: 310 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,310 @@
1+
/**
2+
* @file
3+
* @brief NVMEM Devicetree public API header file.
4+
*/
5+
6+
/*
7+
* Copyright (c) 2024, Andriy Gelman
8+
* Copyright (c) 2025, Basalte bv
9+
*
10+
* SPDX-License-Identifier: Apache-2.0
11+
*/
12+
13+
#ifndef INCLUDE_ZEPHYR_DEVICETREE_NVMEM_H_
14+
#define INCLUDE_ZEPHYR_DEVICETREE_NVMEM_H_
15+
16+
#ifdef __cplusplus
17+
extern "C" {
18+
#endif
19+
20+
/**
21+
* @defgroup devicetree-nvmem Devicetree NVMEM API
22+
* @ingroup devicetree
23+
* @{
24+
*/
25+
26+
/**
27+
* @brief Test if a node has an nvmem-cells phandle-array property at a given index
28+
*
29+
* This expands to 1 if the given index is a valid nvmem-cells property phandle-array index.
30+
* Otherwise, it expands to 0.
31+
*
32+
* Example devicetree fragment:
33+
*
34+
* @code{.dts}
35+
* eth: ethernet {
36+
* nvmem-cells = <&mac_address>;
37+
* nvmem-cell-names = "mac-address";
38+
* };
39+
* @endcode
40+
*
41+
* Example usage:
42+
*
43+
* @code{.c}
44+
* DT_NVMEM_CELLS_HAS_IDX(DT_NODELABEL(eth), 0) // 1
45+
* DT_NVMEM_CELLS_HAS_IDX(DT_NODELABEL(eth), 1) // 0
46+
* @endcode
47+
*
48+
* @param node_id node identifier; may or may not have any nvmem-cells property
49+
* @param idx index of a nvmem-cells property phandle-array whose existence to check
50+
*
51+
* @return 1 if the index exists, 0 otherwise
52+
*/
53+
#define DT_NVMEM_CELLS_HAS_IDX(node_id, idx) DT_PROP_HAS_IDX(node_id, nvmem_cells, idx)
54+
55+
/**
56+
* @brief Test if a node has an nvmem-cell-names array property hold a given name.
57+
*
58+
* This expands to 1 if the name is available as nvmem-cells-name array property cell.
59+
* Otherwise, it expands to 0.
60+
*
61+
* Example devicetree fragment:
62+
*
63+
* @code{.dts}
64+
* eth: ethernet {
65+
* nvmem-cells = <&mac_address>;
66+
* nvmem-cell-names = "mac-address";
67+
* };
68+
* @endcode
69+
*
70+
* Example usage:
71+
*
72+
* @code{.c}
73+
* DT_NVMEM_CELLS_HAS_NAME(DT_NODELABEL(eth), mac_address) // 1
74+
* DT_NVMEM_CELLS_HAS_NAME(DT_NODELABEL(eth), bogus) // 0
75+
* @endcode
76+
*
77+
* @param node_id node identifier; may or may not have any nvmem-cell-names property
78+
* @param name lowercase-and-underscores nvmem-cell-names cell value name to check
79+
*
80+
* @return 1 if the index exists, 0 otherwise
81+
*/
82+
#define DT_NVMEM_CELLS_HAS_NAME(node_id, name) DT_PROP_HAS_NAME(node_id, nvmem_cells, name)
83+
84+
/**
85+
* @brief Get the number of elements in an nvmem-cells property
86+
*
87+
* Example devicetree fragment:
88+
*
89+
* @code{.dts}
90+
* eth: ethernet {
91+
* nvmem-cells = <&mac_address>;
92+
* nvmem-cell-names = "mac-address";
93+
* };
94+
* @endcode
95+
*
96+
* Example usage:
97+
*
98+
* @code{.c}
99+
* DT_NUM_NVMEM_CELLS(DT_NODELABEL(eth)) // 1
100+
* @endcode
101+
*
102+
* @param node_id node identifier with an nvmem-cells property
103+
*
104+
* @return number of elements in the property
105+
*/
106+
#define DT_NUM_NVMEM_CELLS(node_id) DT_PROP_LEN(node_id, nvmem_cells)
107+
108+
/**
109+
* @brief Get the node identifier for the NVMEM cell from the nvmem-cells property by index.
110+
*
111+
* Example devicetree fragment:
112+
*
113+
* @code{.dts}
114+
* mac_eeprom: mac_eeprom@2 {
115+
* nvmem-layout {
116+
* compatible = "fixed-layout";
117+
* #address-cells = <1>;
118+
* #size-cells = <1>;
119+
* mac_address: mac_address@fa {
120+
* reg = <0xfa 0x06>;
121+
* #nvmem-cell-cells = <0>;
122+
* };
123+
* };
124+
* };
125+
*
126+
* eth: ethernet {
127+
* nvmem-cells = <&mac_address>;
128+
* nvmem-cell-names = "mac-address";
129+
* };
130+
* @endcode
131+
*
132+
* Example usage:
133+
*
134+
* @code{.c}
135+
* DT_NVMEM_CELL_BY_IDX(DT_NODELABEL(eth), 0) // DT_NODELABEL(mac_address)
136+
* @endcode
137+
*
138+
* @param node_id node identifier for a node with a nvmem-cells property
139+
* @param idx index into the nvmem-cells property
140+
*
141+
* @return the node identifier for the NVMEM cell at index idx
142+
*/
143+
#define DT_NVMEM_CELL_BY_IDX(node_id, idx) DT_PHANDLE_BY_IDX(node_id, nvmem_cells, idx)
144+
145+
/**
146+
* @brief Equivalent to DT_NVMEM_CELL_BY_IDX(node_id, 0)
147+
*
148+
* @param node_id node identifier
149+
*
150+
* @return a node identifier for the NVMEM cell at index 0
151+
* in "nvmem-cells"
152+
*
153+
* @see DT_NVMEM_CELL_BY_IDX()
154+
*/
155+
#define DT_NVMEM_CELL(node_id) DT_NVMEM_CELL_BY_IDX(node_id, 0)
156+
157+
/**
158+
* @brief Get the node identifier for the NVMEM cell from the nvmem-cells property by name.
159+
*
160+
* Example devicetree fragment:
161+
*
162+
* @code{.dts}
163+
* mac_eeprom: mac_eeprom@2 {
164+
* nvmem-layout {
165+
* compatible = "fixed-layout";
166+
* #address-cells = <1>;
167+
* #size-cells = <1>;
168+
* mac_address: mac_address@fa {
169+
* reg = <0xfa 0x06>;
170+
* #nvmem-cell-cells = <0>;
171+
* };
172+
* };
173+
* };
174+
*
175+
* eth: ethernet {
176+
* nvmem-cells = <&mac_address>;
177+
* nvmem-cell-names = "mac-address";
178+
* };
179+
* @endcode
180+
*
181+
* Example usage:
182+
*
183+
* @code{.c}
184+
* DT_NVMEM_CELL_BY_NAME(DT_NODELABEL(eth), mac_address) // DT_NODELABEL(mac_address)
185+
* @endcode
186+
*
187+
* @param node_id node identifier for a node with a nvmem-cells property
188+
* @param name lowercase-and-underscores name of an nvmem-cells element
189+
* as defined by the node's nvmem-cell-names property
190+
*
191+
* @return the node identifier for the NVMEM cell by name
192+
*/
193+
#define DT_NVMEM_CELL_BY_NAME(node_id, name) DT_PHANDLE_BY_NAME(node_id, nvmem_cells, name)
194+
195+
/**
196+
* @brief Equivalent to DT_NVMEM_CELLS_HAS_IDX(DT_DRV_INST(inst), idx)
197+
*
198+
* @param inst DT_DRV_COMPAT instance number; may or may not have any nvmem-cells property
199+
* @param idx index of an nvmem-cells property phandle-array whose existence to check
200+
*
201+
* @return 1 if the index exists, 0 otherwise
202+
*/
203+
#define DT_INST_NVMEM_CELLS_HAS_IDX(inst, idx) DT_NVMEM_CELLS_HAS_IDX(DT_DRV_INST(inst), idx)
204+
205+
/**
206+
* @brief Equivalent to DT_NVMEM_CELLS_HAS_NAME(DT_DRV_INST(inst), name)
207+
*
208+
* @param inst DT_DRV_COMPAT instance number; may or may not have any nvmem-cell-names property.
209+
* @param name lowercase-and-underscores nvmem-cell-names cell value name to check
210+
*
211+
* @return 1 if the nvmem cell name exists, 0 otherwise
212+
*/
213+
#define DT_INST_NVMEM_CELLS_HAS_NAME(inst, name) DT_NVMEM_CELLS_HAS_NAME(DT_DRV_INST(inst), name)
214+
215+
/**
216+
* @brief Equivalent to DT_NUM_NVMEM_CELLS(DT_DRV_INST(inst))
217+
*
218+
* @param inst instance number
219+
*
220+
* @return number of elements in the nvmem-cells property
221+
*/
222+
#define DT_INST_NUM_NVMEM_CELLS(inst) DT_NUM_NVMEM_CELLS(DT_DRV_INST(inst))
223+
224+
/**
225+
* @brief Get the node identifier for the controller phandle from an
226+
* nvmem-cells phandle-array property at an index
227+
*
228+
* @param inst instance number
229+
* @param idx logical index into nvmem-cells
230+
*
231+
* @return the node identifier for the nvmem cell referenced at
232+
* index "idx"
233+
*
234+
* @see DT_NVMEM_CELL_CTLR_BY_IDX()
235+
*/
236+
#define DT_INST_NVMEM_CELL_BY_IDX(inst, idx) DT_NVMEM_CELL_BY_IDX(DT_DRV_INST(inst), idx)
237+
238+
/**
239+
* @brief Equivalent to DT_INST_NVMEM_CELL_BY_IDX(inst, 0)
240+
*
241+
* @param inst instance number
242+
*
243+
* @return a node identifier for the nvmem cell at index 0
244+
* in nvmem-cells
245+
*
246+
* @see DT_NVMEM_CELL()
247+
*/
248+
#define DT_INST_NVMEM_CELL(inst) DT_INST_NVMEM_CELL_BY_IDX(inst, 0)
249+
250+
/**
251+
* @brief Get the node identifier for the controller phandle from an
252+
* nvmem-cells phandle-array property by name
253+
*
254+
* @param inst instance number
255+
* @param name lowercase-and-underscores name of an nvmem-cells element
256+
* as defined by the node's nvmem-cell-names property
257+
*
258+
* @return the node identifier for the nvmem cell referenced by
259+
* the named element
260+
*
261+
* @see DT_NVMEM_CELL_BY_NAME()
262+
*/
263+
#define DT_INST_NVMEM_CELL_BY_NAME(inst, name) DT_NVMEM_CELL_BY_NAME(DT_DRV_INST(inst), name)
264+
265+
/**
266+
* @brief Get the node identifier of the memory controller for an nvmem cell.
267+
*
268+
* Example devicetree fragment:
269+
*
270+
* @code{.dts}
271+
* mac_eeprom: mac_eeprom@2 {
272+
* nvmem-layout {
273+
* compatible = "fixed-layout";
274+
* #address-cells = <1>;
275+
* #size-cells = <1>;
276+
* mac_address: mac_address@fa {
277+
* reg = <0xfa 0x06>;
278+
* #nvmem-cell-cells = <0>;
279+
* };
280+
* };
281+
* };
282+
*
283+
* eth: ethernet {
284+
* nvmem-cells = <&mac_address>;
285+
* nvmem-cell-names = "mac-address";
286+
* };
287+
* @endcode
288+
*
289+
* Example usage:
290+
*
291+
* @code{.c}
292+
* DT_MTD_FROM_NVMEM_CELL(DT_NVMEM_CELL(DT_NODELABEL(eth))) // DT_NODELABEL(mac_eeprom)
293+
* @endcode
294+
*
295+
* @param node_id node identifier for an nvmem cell node
296+
*
297+
* @return the node identifier of the Memory Technology Device (MTD) that
298+
* contains the nvmem cell node.
299+
*/
300+
#define DT_MTD_FROM_NVMEM_CELL(node_id) DT_GPARENT(node_id)
301+
302+
/**
303+
* @}
304+
*/
305+
306+
#ifdef __cplusplus
307+
}
308+
#endif
309+
310+
#endif /* INCLUDE_ZEPHYR_DEVICETREE_NVMEM_H_ */

0 commit comments

Comments
 (0)