|
| 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 | + * DT_NVMEM_CELLS_HAS_IDX(DT_NODELABEL(eth), 0) // 1 |
| 44 | + * DT_NVMEM_CELLS_HAS_IDX(DT_NODELABEL(eth), 1) // 0 |
| 45 | + * |
| 46 | + * @param node_id node identifier; may or may not have any nvmem-cells property |
| 47 | + * @param idx index of a nvmem-cells property phandle-array whose existence to check |
| 48 | + * @return 1 if the index exists, 0 otherwise |
| 49 | + */ |
| 50 | +#define DT_NVMEM_CELLS_HAS_IDX(node_id, idx) DT_PROP_HAS_IDX(node_id, nvmem_cells, idx) |
| 51 | + |
| 52 | +/** |
| 53 | + * @brief Test if a node has an nvmem-cell-names array property hold a given name. |
| 54 | + * |
| 55 | + * This expands to 1 if the name is available as nvmem-cells-name array property cell. |
| 56 | + * Otherwise, it expands to 0. |
| 57 | + * |
| 58 | + * Example devicetree fragment: |
| 59 | + * |
| 60 | + * @code{.dts} |
| 61 | + * eth: ethernet { |
| 62 | + * nvmem-cells = <&mac_address>; |
| 63 | + * nvmem-cell-names = "mac-address"; |
| 64 | + * }; |
| 65 | + * @endcode |
| 66 | + * |
| 67 | + * Example usage: |
| 68 | + * |
| 69 | + * DT_NVMEM_CELLS_HAS_NAME(DT_NODELABEL(eth), mac_address) // 1 |
| 70 | + * DT_NVMEM_CELLS_HAS_NAME(DT_NODELABEL(eth), bogus) // 0 |
| 71 | + * |
| 72 | + * @param node_id node identifier; may or may not have any nvmem-cell-names property |
| 73 | + * @param name lowercase-and-underscores nvmem-cell-names cell value name to check |
| 74 | + * @return 1 if the index exists, 0 otherwise |
| 75 | + */ |
| 76 | +#define DT_NVMEM_CELLS_HAS_NAME(node_id, name) DT_PROP_HAS_NAME(node_id, nvmem_cells, name) |
| 77 | + |
| 78 | +/** |
| 79 | + * @brief Get the number of elements in an nvmem-cells property |
| 80 | + * |
| 81 | + * Example devicetree fragment: |
| 82 | + * |
| 83 | + * @code{.dts} |
| 84 | + * eth: ethernet { |
| 85 | + * nvmem-cells = <&mac_address>; |
| 86 | + * nvmem-cell-names = "mac-address"; |
| 87 | + * }; |
| 88 | + * @endcode |
| 89 | + * |
| 90 | + * Example usage: |
| 91 | + * |
| 92 | + * DT_NUM_NVMEM_CELLS(DT_NODELABEL(eth)) // 1 |
| 93 | + * |
| 94 | + * @param node_id node identifier with an nvmem-cells property |
| 95 | + * @return number of elements in the property |
| 96 | + */ |
| 97 | +#define DT_NUM_NVMEM_CELLS(node_id) DT_PROP_LEN(node_id, nvmem_cells) |
| 98 | + |
| 99 | +/** |
| 100 | + * @brief Get the node identifier for the NVMEM cell from the nvmem-cells property by index. |
| 101 | + * |
| 102 | + * Example devicetree fragment: |
| 103 | + * |
| 104 | + * @code{.dts} |
| 105 | + * mac_eeprom: mac_eeprom@2 { |
| 106 | + * #address-cells = <1>; |
| 107 | + * #size-cells = <1>; |
| 108 | + * mac_address: mac_address@fa { |
| 109 | + * reg = <0xfa 0x06>; |
| 110 | + * }; |
| 111 | + * }; |
| 112 | + * |
| 113 | + * eth: ethernet { |
| 114 | + * nvmem-cells = <&mac_address>; |
| 115 | + * nvmem-cell-names = "mac-address"; |
| 116 | + * }; |
| 117 | + * @endcode |
| 118 | + * |
| 119 | + * Example usage: |
| 120 | + * |
| 121 | + * DT_NVMEM_CELL_BY_IDX(DT_NODELABEL(eth), 0) // DT_NODELABEL(mac_address) |
| 122 | + * |
| 123 | + * @param node_id node identifier for a node with a nvmem-cells property |
| 124 | + * @param idx index into the nvmem-cells property |
| 125 | + * |
| 126 | + * @return the node identifier for the NVMEM cell at index idx |
| 127 | + */ |
| 128 | +#define DT_NVMEM_CELL_BY_IDX(node_id, idx) DT_PHANDLE_BY_IDX(node_id, nvmem_cells, idx) |
| 129 | + |
| 130 | +/** |
| 131 | + * @brief Equivalent to DT_NVMEM_CELL_BY_IDX(node_id, 0) |
| 132 | + * @param node_id node identifier |
| 133 | + * @return a node identifier for the NVMEM cell at index 0 |
| 134 | + * in "nvmem-cells" |
| 135 | + * @see DT_NVMEM_CELL_BY_IDX() |
| 136 | + */ |
| 137 | +#define DT_NVMEM_CELL(node_id) DT_NVMEM_CELL_BY_IDX(node_id, 0) |
| 138 | + |
| 139 | +/** |
| 140 | + * @brief Get the node identifier for the NVMEM cell from the nvmem-cells property by name. |
| 141 | + * |
| 142 | + * Example devicetree fragment: |
| 143 | + * |
| 144 | + * @code{.dts} |
| 145 | + * mac_eeprom: mac_eeprom@2 { |
| 146 | + * #address-cells = <1>; |
| 147 | + * #size-cells = <1>; |
| 148 | + * mac_address: mac_address@fa { |
| 149 | + * reg = <0xfa 0x06>; |
| 150 | + * }; |
| 151 | + * }; |
| 152 | + * |
| 153 | + * eth: ethernet { |
| 154 | + * nvmem-cells = <&mac_address>; |
| 155 | + * nvmem-cell-names = "mac-address"; |
| 156 | + * }; |
| 157 | + * @endcode |
| 158 | + * |
| 159 | + * Example usage: |
| 160 | + * |
| 161 | + * DT_NVMEM_CELL_BY_NAME(DT_NODELABEL(eth), mac_address) // DT_NODELABEL(mac_address) |
| 162 | + * |
| 163 | + * @param node_id node identifier for a node with a nvmem-cells property |
| 164 | + * @param name lowercase-and-underscores name of an nvmem-cells element |
| 165 | + * as defined by the node's nvmem-cell-names property |
| 166 | + * |
| 167 | + * @return the node identifier for the NVMEM cell by name |
| 168 | + */ |
| 169 | +#define DT_NVMEM_CELL_BY_NAME(node_id, name) DT_PHANDLE_BY_NAME(node_id, nvmem_cells, name) |
| 170 | + |
| 171 | +/** |
| 172 | + * @brief Equivalent to DT_NVMEM_CELLS_HAS_IDX(DT_DRV_INST(inst), idx) |
| 173 | + * @param inst DT_DRV_COMPAT instance number; may or may not have any nvmem-cells property |
| 174 | + * @param idx index of an nvmem-cells property phandle-array whose existence to check |
| 175 | + * @return 1 if the index exists, 0 otherwise |
| 176 | + */ |
| 177 | +#define DT_INST_NVMEM_CELLS_HAS_IDX(inst, idx) DT_NVMEM_CELLS_HAS_IDX(DT_DRV_INST(inst), idx) |
| 178 | + |
| 179 | +/** |
| 180 | + * @brief Equivalent to DT_NVMEM_CELLS_HAS_NAME(DT_DRV_INST(inst), name) |
| 181 | + * @param inst DT_DRV_COMPAT instance number; may or may not have any nvmem-cell-names property. |
| 182 | + * @param name lowercase-and-underscores nvmem-cell-names cell value name to check |
| 183 | + * @return 1 if the nvmem cell name exists, 0 otherwise |
| 184 | + */ |
| 185 | +#define DT_INST_NVMEM_CELLS_HAS_NAME(inst, name) DT_NVMEM_CELLS_HAS_NAME(DT_DRV_INST(inst), name) |
| 186 | + |
| 187 | +/** |
| 188 | + * @brief Equivalent to DT_NUM_NVMEM_CELLS(DT_DRV_INST(inst)) |
| 189 | + * @param inst instance number |
| 190 | + * @return number of elements in the nvmem-cells property |
| 191 | + */ |
| 192 | +#define DT_INST_NUM_NVMEM_CELLS(inst) DT_NUM_NVMEM_CELLS(DT_DRV_INST(inst)) |
| 193 | + |
| 194 | +/** |
| 195 | + * @brief Get the node identifier for the controller phandle from an |
| 196 | + * nvmem-cells phandle-array property at an index |
| 197 | + * |
| 198 | + * @param inst instance number |
| 199 | + * @param idx logical index into nvmem-cells |
| 200 | + * @return the node identifier for the nvmem cell referenced at |
| 201 | + * index "idx" |
| 202 | + * @see DT_NVMEM_CELL_CTLR_BY_IDX() |
| 203 | + */ |
| 204 | +#define DT_INST_NVMEM_CELL_BY_IDX(inst, idx) DT_NVMEM_CELL_BY_IDX(DT_DRV_INST(inst), idx) |
| 205 | + |
| 206 | +/** |
| 207 | + * @brief Equivalent to DT_INST_NVMEM_CELL_BY_IDX(inst, 0) |
| 208 | + * @param inst instance number |
| 209 | + * @return a node identifier for the nvmem cell at index 0 |
| 210 | + * in nvmem-cells |
| 211 | + * @see DT_NVMEM_CELL() |
| 212 | + */ |
| 213 | +#define DT_INST_NVMEM_CELL(inst) DT_INST_NVMEM_CELL_BY_IDX(inst, 0) |
| 214 | + |
| 215 | +/** |
| 216 | + * @brief Get the node identifier for the controller phandle from an |
| 217 | + * nvmem-cells phandle-array property by name |
| 218 | + * |
| 219 | + * @param inst instance number |
| 220 | + * @param name lowercase-and-underscores name of an nvmem-cells element |
| 221 | + * as defined by the node's nvmem-cell-names property |
| 222 | + * @return the node identifier for the nvmem cell referenced by |
| 223 | + * the named element |
| 224 | + * @see DT_NVMEM_CELL_BY_NAME() |
| 225 | + */ |
| 226 | +#define DT_INST_NVMEM_CELL_BY_NAME(inst, name) DT_NVMEM_CELL_BY_NAME(DT_DRV_INST(inst), name) |
| 227 | + |
| 228 | +/** |
| 229 | + * @brief Get the node identifier of the memory controller for an nvmem cell. |
| 230 | + * |
| 231 | + * Example devicetree fragment: |
| 232 | + * |
| 233 | + * @code{.dts} |
| 234 | + * mac_eeprom: mac_eeprom@2 { |
| 235 | + * #address-cells = <1>; |
| 236 | + * #size-cells = <1>; |
| 237 | + * mac_address: mac_address@fa { |
| 238 | + * reg = <0xfa 0x06>; |
| 239 | + * }; |
| 240 | + * }; |
| 241 | + * |
| 242 | + * eth: ethernet { |
| 243 | + * nvmem-cells = <&mac_address>; |
| 244 | + * nvmem-cell-names = "mac-address"; |
| 245 | + * }; |
| 246 | + * @endcode |
| 247 | + * |
| 248 | + * Example usage: |
| 249 | + * |
| 250 | + * DT_MTD_FROM_NVMEM_CELL(DT_NVMEM_CELL(DT_NODELABEL(eth))) // DT_NODELABEL(mac_eeprom) |
| 251 | + * |
| 252 | + * @param node_id node identifier for an nvmem cell node |
| 253 | + * @return the node identifier of the Memory Technology Device (MTD) that |
| 254 | + * contains the nvmem cell node. |
| 255 | + */ |
| 256 | +#define DT_MTD_FROM_NVMEM_CELL(node_id) DT_PARENT(node_id) |
| 257 | + |
| 258 | +/** |
| 259 | + * @} |
| 260 | + */ |
| 261 | + |
| 262 | +#ifdef __cplusplus |
| 263 | +} |
| 264 | +#endif |
| 265 | + |
| 266 | +#endif /* INCLUDE_ZEPHYR_DEVICETREE_NVMEM_H_ */ |
0 commit comments