|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 TOKITA Hiroshi |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#ifndef ZEPHYR_INCLUDE_DEVICETREE_MAP_H_ |
| 8 | +#define ZEPHYR_INCLUDE_DEVICETREE_MAP_H_ |
| 9 | + |
| 10 | +#ifdef __cplusplus |
| 11 | +extern "C" { |
| 12 | +#endif |
| 13 | + |
| 14 | +/** |
| 15 | + * @defgroup devicetree-map Devicetree Map API |
| 16 | + * |
| 17 | + * @brief Helper macros for handling map properties. |
| 18 | + * |
| 19 | + * This module provides helper macros that facilitate interrupt mapping and |
| 20 | + * specifier mapping based on DeviceTree specifications. It enables the extraction |
| 21 | + * and interpretation of mapping data represented as phandle-arrays. |
| 22 | + * |
| 23 | + * In a typical DeviceTree fragment, properties ending with "-map" specify: |
| 24 | + * - The child specifier to be mapped. |
| 25 | + * - The parent node (phandle) to which the mapping applies. |
| 26 | + * - The parent specifier associated with the mapping. |
| 27 | + * |
| 28 | + * For example, when the following DeviceTree snippet are defined: |
| 29 | + * |
| 30 | + * @code{.dts} |
| 31 | + * n: node { |
| 32 | + * gpio-map = <0 1 &gpio0 2 3>, <4 5 &gpio0 6 7>; |
| 33 | + * }; |
| 34 | + * @endcode |
| 35 | + * |
| 36 | + * In the first mapping entry: |
| 37 | + * - `0 1` are the child specifiers. |
| 38 | + * - &gpio0 is the parent node. |
| 39 | + * - `2 3` are the parent specifiers. |
| 40 | + * |
| 41 | + * Since map properties are implemented as phandle-arrays, macros such as |
| 42 | + * DT_PHANDLE_BY_IDX() and DT_PHA_BY_IDX() can be used to access individual elements. |
| 43 | + * |
| 44 | + * Both child and parent specifiers are treated as cells in a phandle-array. |
| 45 | + * By default, each group of specifiers is given a sequential cell name |
| 46 | + * (child_specifier_0, child_specifier_1, ..., parent_specifier_0, ...). |
| 47 | + * |
| 48 | + * If cell names are specified in dt-bindings, they will be used for the child specifier cell names. |
| 49 | + * Parent specifiers always use the default naming convention. |
| 50 | + * |
| 51 | + * Example usage: |
| 52 | + * |
| 53 | + * A mapping entry is a phandle-array whose elements can be referenced as follows: |
| 54 | + * - Child specifiers can be accessed via names such as `child_specifier_0`, |
| 55 | + * `child_specifier_1`, ... |
| 56 | + * - The parent node is accessed via DT_PHANDLE_BY_IDX(). |
| 57 | + * - Parent specifiers are accessed via names such as `parent_specifier_0`, |
| 58 | + * `parent_specifier_1`, ... |
| 59 | + * |
| 60 | + * @code{.c} |
| 61 | + * int cspec_0 = DT_PHA_BY_IDX(DT_NODELABEL(n), gpio_map, 0, child_specifier_0); // 0 |
| 62 | + * int cspec_1 = DT_PHA_BY_IDX(DT_NODELABEL(n), gpio_map, 0, child_specifier_1); // 1 |
| 63 | + * const struct device *parent = |
| 64 | + * device_get_binding(DT_PHANDLE_BY_IDX(DT_NODELABEL(n), gpio_map, 0)); // &gpio0 |
| 65 | + * int pspec_0 = DT_PHA_BY_IDX(DT_NODELABEL(n), gpio_map, 0, parent_specifier_0); // 2 |
| 66 | + * int pspec_1 = DT_PHA_BY_IDX(DT_NODELABEL(n), gpio_map, 0, parent_specifier_1); // 3 |
| 67 | + * @endcode |
| 68 | + * |
| 69 | + * The map helper API also provides the following macros for convenient access to |
| 70 | + * specific parts of a mapping entry: |
| 71 | + * - DT_MAP_CHILD_SPECIFIER_ARGS() |
| 72 | + * - DT_MAP_PARENT_SPECIFIER_ARGS() |
| 73 | + * - DT_MAP_PARENT_ARG() |
| 74 | + * |
| 75 | + * These macros extract, respectively, the child specifier arguments, the parent specifier |
| 76 | + * arguments, and the parent node argument from a mapping element identified by its node ID, |
| 77 | + * property name, and index. |
| 78 | + * |
| 79 | + * For instance: |
| 80 | + * |
| 81 | + * @code{.c} |
| 82 | + * #define SRC_AND_DST(node_id, prop, idx) \ |
| 83 | + * { GET_ARG_N(1, DT_MAP_CHILD_SPECIFIER_ARGS(node_id, prop, idx)), \ |
| 84 | + * GET_ARG_N(1, DT_MAP_PARENT_SPECIFIER_ARGS(node_id, prop, idx)) } |
| 85 | + * |
| 86 | + * int src_and_dst[2][] = { |
| 87 | + * DT_FOREACH_PROP_ELEM_SEP(DT_NODELABEL(n), gpio_map, SRC_AND_DST, (,)) |
| 88 | + * }; |
| 89 | + * @endcode |
| 90 | + * |
| 91 | + * The above expansion yields: |
| 92 | + * |
| 93 | + * @code{.c} |
| 94 | + * int src_and_dst[2][] = {{0, 2}, {4, 6}}; |
| 95 | + * @endcode |
| 96 | + * |
| 97 | + * @ingroup devicetree |
| 98 | + * @{ |
| 99 | + */ |
| 100 | + |
| 101 | +/** |
| 102 | + * @brief Extracts a specified range of arguments. |
| 103 | + * |
| 104 | + * This helper macro first skips a given number of arguments and then selects |
| 105 | + * the first @p len arguments from the remaining list. |
| 106 | + * |
| 107 | + * @param start The number of arguments to skip. |
| 108 | + * @param len The number of arguments to extract after skipping. |
| 109 | + * @param ... The list of input arguments. |
| 110 | + */ |
| 111 | +#define DT_MAP_HELPER_DO_ARGS_RANGE(start, len, ...) \ |
| 112 | + GET_ARGS_FIRST_N(len, GET_ARGS_LESS_N(start, __VA_ARGS__)) |
| 113 | + |
| 114 | +/** |
| 115 | + * @brief Extracts a range of mapping arguments for a specific field. |
| 116 | + * |
| 117 | + * This macro concatenates the field name with the appropriate suffixes to determine |
| 118 | + * the starting index and length of the arguments for a map entry, and then extracts |
| 119 | + * those arguments. |
| 120 | + * |
| 121 | + * @param name The mapping field name (e.g., CHILD_SPECIFIER, PARENT). |
| 122 | + * @param node_id The node identifier. |
| 123 | + * @param prop The property name in lowercase and underscores. |
| 124 | + * @param idx The index of the mapping entry. |
| 125 | + * @param ... Additional arguments corresponding to the mapping entry. |
| 126 | + */ |
| 127 | +#define DT_MAP_HELPER_ARGS_RANGE(name, node_id, prop, idx, ...) \ |
| 128 | + DT_MAP_HELPER_DO_ARGS_RANGE(DT_CAT3(DT_MAP_, name, _IDX)(node_id, prop, idx), \ |
| 129 | + DT_CAT3(DT_MAP_, name, _LEN)(node_id, prop, idx), __VA_ARGS__) |
| 130 | + |
| 131 | +/** |
| 132 | + * @brief Retrieves the mapping entry at the specified index. |
| 133 | + * |
| 134 | + * @param node_id The node identifier. |
| 135 | + * @param prop The property name in lowercase with underscores. |
| 136 | + * @param idx The mapping entry index. |
| 137 | + * @return The mapping entry as a list of comma-separated values. |
| 138 | + */ |
| 139 | +#define DT_MAP_IDX(node_id, prop, idx) DT_CAT5(node_id, _P_, prop, _MAP_IDX_, idx) |
| 140 | + |
| 141 | +/** |
| 142 | + * @brief Returns the number of mapping entries for the given property. |
| 143 | + * |
| 144 | + * @param node_id The node identifier. |
| 145 | + * @param prop The property name in lowercase with underscores. |
| 146 | + * @return The total count of mapping entries. |
| 147 | + */ |
| 148 | +#define DT_MAP_LEN(node_id, prop) DT_CAT4(node_id, _P_, prop, _MAP_LEN) |
| 149 | + |
| 150 | +/** |
| 151 | + * @brief Retrieves the starting index of the child specifier cell within a mapping entry. |
| 152 | + * |
| 153 | + * @param node_id The node identifier. |
| 154 | + * @param prop The property name. |
| 155 | + * @param idx The mapping entry index. |
| 156 | + * @return The starting index of the child specifier cell. |
| 157 | + */ |
| 158 | +#define DT_MAP_CHILD_SPECIFIER_IDX(node_id, prop, idx) \ |
| 159 | + DT_CAT7(node_id, _P_, prop, _MAP_IDX_, idx, _, CHILD_SPECIFIER_IDX) |
| 160 | + |
| 161 | +/** |
| 162 | + * @brief Returns the length (number of cells) of the child specifier within a mapping entry. |
| 163 | + * |
| 164 | + * @param node_id The node identifier. |
| 165 | + * @param prop The property name. |
| 166 | + * @param idx The mapping entry index. |
| 167 | + * @return The length (in cells) of the child specifier. |
| 168 | + */ |
| 169 | +#define DT_MAP_CHILD_SPECIFIER_LEN(node_id, prop, idx) \ |
| 170 | + DT_CAT7(node_id, _P_, prop, _MAP_IDX_, idx, _, CHILD_SPECIFIER_LEN) |
| 171 | + |
| 172 | +/** |
| 173 | + * @brief Retrieves the starting index of the parent cell in a mapping entry. |
| 174 | + * |
| 175 | + * @param node_id The node identifier. |
| 176 | + * @param prop The property name. |
| 177 | + * @param idx The mapping entry index. |
| 178 | + * @return The starting index of the parent cell. |
| 179 | + */ |
| 180 | +#define DT_MAP_PARENT_IDX(node_id, prop, idx) \ |
| 181 | + DT_CAT7(node_id, _P_, prop, _MAP_IDX_, idx, _, PARENT_IDX) |
| 182 | + |
| 183 | +/** |
| 184 | + * @brief Returns the length (number of cells) of the parent cell in a mapping entry. |
| 185 | + * |
| 186 | + * @param node_id The node identifier. |
| 187 | + * @param prop The property name. |
| 188 | + * @param idx The mapping entry index. |
| 189 | + * @return The length (in cells) of the parent cell. |
| 190 | + */ |
| 191 | +#define DT_MAP_PARENT_LEN(node_id, prop, idx) \ |
| 192 | + DT_CAT7(node_id, _P_, prop, _MAP_IDX_, idx, _, PARENT_LEN) |
| 193 | + |
| 194 | +/** |
| 195 | + * @brief Retrieves the starting index of the parent specifier cell within a mapping entry. |
| 196 | + * |
| 197 | + * @param node_id The node identifier. |
| 198 | + * @param prop The property name. |
| 199 | + * @param idx The mapping entry index. |
| 200 | + * @return The starting index of the parent specifier cell. |
| 201 | + */ |
| 202 | +#define DT_MAP_PARENT_SPECIFIER_IDX(node_id, prop, idx) \ |
| 203 | + DT_CAT7(node_id, _P_, prop, _MAP_IDX_, idx, _, PARENT_SPECIFIER_IDX) |
| 204 | + |
| 205 | +/** |
| 206 | + * @brief Returns the length (number of cells) of the parent specifier in a mapping entry. |
| 207 | + * |
| 208 | + * @param node_id The node identifier. |
| 209 | + * @param prop The property name. |
| 210 | + * @param idx The mapping entry index. |
| 211 | + * @return The length (in cells) of the parent specifier. |
| 212 | + */ |
| 213 | +#define DT_MAP_PARENT_SPECIFIER_LEN(node_id, prop, idx) \ |
| 214 | + DT_CAT7(node_id, _P_, prop, _MAP_IDX_, idx, _, PARENT_SPECIFIER_LEN) |
| 215 | + |
| 216 | +/** |
| 217 | + * @brief Extracts the child specifier arguments from a mapping entry. |
| 218 | + * |
| 219 | + * This macro returns the comma-separated list of arguments for the child specifier. |
| 220 | + * |
| 221 | + * @param node_id The node identifier. |
| 222 | + * @param prop The property name in lowercase with underscores. |
| 223 | + * @param idx The mapping entry index. |
| 224 | + * @return The child specifier arguments. |
| 225 | + */ |
| 226 | +#define DT_MAP_CHILD_SPECIFIER_ARGS(node_id, prop, idx) \ |
| 227 | + DT_MAP_HELPER_ARGS_RANGE(CHILD_SPECIFIER, node_id, prop, idx, \ |
| 228 | + DT_MAP_IDX(node_id, prop, idx)) |
| 229 | + |
| 230 | +/** |
| 231 | + * @brief Extracts the parent node argument from a mapping entry. |
| 232 | + * |
| 233 | + * @param node_id The node identifier. |
| 234 | + * @param prop The property name in lowercase with underscores. |
| 235 | + * @param idx The mapping entry index. |
| 236 | + * @return The parent node argument. |
| 237 | + */ |
| 238 | +#define DT_MAP_PARENT_ARG(node_id, prop, idx) \ |
| 239 | + DT_MAP_HELPER_ARGS_RANGE(PARENT, node_id, prop, idx, DT_MAP_IDX(node_id, prop, idx)) |
| 240 | + |
| 241 | +/** |
| 242 | + * @brief Extracts the parent specifier arguments from a mapping entry. |
| 243 | + * |
| 244 | + * This macro returns the comma-separated list of arguments for the parent specifier. |
| 245 | + * |
| 246 | + * @param node_id The node identifier. |
| 247 | + * @param prop The property name in lowercase with underscores. |
| 248 | + * @param idx The mapping entry index. |
| 249 | + * @return The parent specifier arguments. |
| 250 | + */ |
| 251 | +#define DT_MAP_PARENT_SPECIFIER_ARGS(node_id, prop, idx) \ |
| 252 | + DT_MAP_HELPER_ARGS_RANGE(PARENT_SPECIFIER, node_id, prop, idx, \ |
| 253 | + DT_MAP_IDX(node_id, prop, idx)) |
| 254 | + |
| 255 | +/** |
| 256 | + * @} |
| 257 | + */ |
| 258 | + |
| 259 | +#ifdef __cplusplus |
| 260 | +} |
| 261 | +#endif |
| 262 | + |
| 263 | +#endif /* ZEPHYR_INCLUDE_DEVICETREE_MAP_H_ */ |
0 commit comments