Skip to content

Commit 0cc454b

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 9354e06 commit 0cc454b

File tree

2 files changed

+311
-0
lines changed

2 files changed

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

include/zephyr/devicetree/nvmem.h

Lines changed: 298 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,298 @@
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+
* #address-cells = <1>;
116+
* #size-cells = <1>;
117+
* mac_address: mac_address@fa {
118+
* reg = <0xfa 0x06>;
119+
* };
120+
* };
121+
*
122+
* eth: ethernet {
123+
* nvmem-cells = <&mac_address>;
124+
* nvmem-cell-names = "mac-address";
125+
* };
126+
* @endcode
127+
*
128+
* Example usage:
129+
*
130+
* @code{.c}
131+
* DT_NVMEM_CELL_BY_IDX(DT_NODELABEL(eth), 0) // DT_NODELABEL(mac_address)
132+
* @endcode
133+
*
134+
* @param node_id node identifier for a node with a nvmem-cells property
135+
* @param idx index into the nvmem-cells property
136+
*
137+
* @return the node identifier for the NVMEM cell at index idx
138+
*/
139+
#define DT_NVMEM_CELL_BY_IDX(node_id, idx) DT_PHANDLE_BY_IDX(node_id, nvmem_cells, idx)
140+
141+
/**
142+
* @brief Equivalent to DT_NVMEM_CELL_BY_IDX(node_id, 0)
143+
*
144+
* @param node_id node identifier
145+
*
146+
* @return a node identifier for the NVMEM cell at index 0
147+
* in "nvmem-cells"
148+
*
149+
* @see DT_NVMEM_CELL_BY_IDX()
150+
*/
151+
#define DT_NVMEM_CELL(node_id) DT_NVMEM_CELL_BY_IDX(node_id, 0)
152+
153+
/**
154+
* @brief Get the node identifier for the NVMEM cell from the nvmem-cells property by name.
155+
*
156+
* Example devicetree fragment:
157+
*
158+
* @code{.dts}
159+
* mac_eeprom: mac_eeprom@2 {
160+
* #address-cells = <1>;
161+
* #size-cells = <1>;
162+
* mac_address: mac_address@fa {
163+
* reg = <0xfa 0x06>;
164+
* };
165+
* };
166+
*
167+
* eth: ethernet {
168+
* nvmem-cells = <&mac_address>;
169+
* nvmem-cell-names = "mac-address";
170+
* };
171+
* @endcode
172+
*
173+
* Example usage:
174+
*
175+
* @code{.c}
176+
* DT_NVMEM_CELL_BY_NAME(DT_NODELABEL(eth), mac_address) // DT_NODELABEL(mac_address)
177+
* @endcode
178+
*
179+
* @param node_id node identifier for a node with a nvmem-cells property
180+
* @param name lowercase-and-underscores name of an nvmem-cells element
181+
* as defined by the node's nvmem-cell-names property
182+
*
183+
* @return the node identifier for the NVMEM cell by name
184+
*/
185+
#define DT_NVMEM_CELL_BY_NAME(node_id, name) DT_PHANDLE_BY_NAME(node_id, nvmem_cells, name)
186+
187+
/**
188+
* @brief Equivalent to DT_NVMEM_CELLS_HAS_IDX(DT_DRV_INST(inst), idx)
189+
*
190+
* @param inst DT_DRV_COMPAT instance number; may or may not have any nvmem-cells property
191+
* @param idx index of an nvmem-cells property phandle-array whose existence to check
192+
*
193+
* @return 1 if the index exists, 0 otherwise
194+
*/
195+
#define DT_INST_NVMEM_CELLS_HAS_IDX(inst, idx) DT_NVMEM_CELLS_HAS_IDX(DT_DRV_INST(inst), idx)
196+
197+
/**
198+
* @brief Equivalent to DT_NVMEM_CELLS_HAS_NAME(DT_DRV_INST(inst), name)
199+
*
200+
* @param inst DT_DRV_COMPAT instance number; may or may not have any nvmem-cell-names property.
201+
* @param name lowercase-and-underscores nvmem-cell-names cell value name to check
202+
*
203+
* @return 1 if the nvmem cell name exists, 0 otherwise
204+
*/
205+
#define DT_INST_NVMEM_CELLS_HAS_NAME(inst, name) DT_NVMEM_CELLS_HAS_NAME(DT_DRV_INST(inst), name)
206+
207+
/**
208+
* @brief Equivalent to DT_NUM_NVMEM_CELLS(DT_DRV_INST(inst))
209+
*
210+
* @param inst instance number
211+
*
212+
* @return number of elements in the nvmem-cells property
213+
*/
214+
#define DT_INST_NUM_NVMEM_CELLS(inst) DT_NUM_NVMEM_CELLS(DT_DRV_INST(inst))
215+
216+
/**
217+
* @brief Get the node identifier for the controller phandle from an
218+
* nvmem-cells phandle-array property at an index
219+
*
220+
* @param inst instance number
221+
* @param idx logical index into nvmem-cells
222+
*
223+
* @return the node identifier for the nvmem cell referenced at
224+
* index "idx"
225+
*
226+
* @see DT_NVMEM_CELL_CTLR_BY_IDX()
227+
*/
228+
#define DT_INST_NVMEM_CELL_BY_IDX(inst, idx) DT_NVMEM_CELL_BY_IDX(DT_DRV_INST(inst), idx)
229+
230+
/**
231+
* @brief Equivalent to DT_INST_NVMEM_CELL_BY_IDX(inst, 0)
232+
*
233+
* @param inst instance number
234+
*
235+
* @return a node identifier for the nvmem cell at index 0
236+
* in nvmem-cells
237+
*
238+
* @see DT_NVMEM_CELL()
239+
*/
240+
#define DT_INST_NVMEM_CELL(inst) DT_INST_NVMEM_CELL_BY_IDX(inst, 0)
241+
242+
/**
243+
* @brief Get the node identifier for the controller phandle from an
244+
* nvmem-cells phandle-array property by name
245+
*
246+
* @param inst instance number
247+
* @param name lowercase-and-underscores name of an nvmem-cells element
248+
* as defined by the node's nvmem-cell-names property
249+
*
250+
* @return the node identifier for the nvmem cell referenced by
251+
* the named element
252+
*
253+
* @see DT_NVMEM_CELL_BY_NAME()
254+
*/
255+
#define DT_INST_NVMEM_CELL_BY_NAME(inst, name) DT_NVMEM_CELL_BY_NAME(DT_DRV_INST(inst), name)
256+
257+
/**
258+
* @brief Get the node identifier of the memory controller for an nvmem cell.
259+
*
260+
* Example devicetree fragment:
261+
*
262+
* @code{.dts}
263+
* mac_eeprom: mac_eeprom@2 {
264+
* #address-cells = <1>;
265+
* #size-cells = <1>;
266+
* mac_address: mac_address@fa {
267+
* reg = <0xfa 0x06>;
268+
* };
269+
* };
270+
*
271+
* eth: ethernet {
272+
* nvmem-cells = <&mac_address>;
273+
* nvmem-cell-names = "mac-address";
274+
* };
275+
* @endcode
276+
*
277+
* Example usage:
278+
*
279+
* @code{.c}
280+
* DT_MTD_FROM_NVMEM_CELL(DT_NVMEM_CELL(DT_NODELABEL(eth))) // DT_NODELABEL(mac_eeprom)
281+
* @endcode
282+
*
283+
* @param node_id node identifier for an nvmem cell node
284+
*
285+
* @return the node identifier of the Memory Technology Device (MTD) that
286+
* contains the nvmem cell node.
287+
*/
288+
#define DT_MTD_FROM_NVMEM_CELL(node_id) DT_PARENT(node_id)
289+
290+
/**
291+
* @}
292+
*/
293+
294+
#ifdef __cplusplus
295+
}
296+
#endif
297+
298+
#endif /* INCLUDE_ZEPHYR_DEVICETREE_NVMEM_H_ */

0 commit comments

Comments
 (0)