Rob Herring | af6074f | 2017-12-27 12:55:14 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Sakari Ailus | 1df09bc | 2017-05-24 17:53:53 +0300 | [diff] [blame] | 2 | /* |
| 3 | * drivers/of/property.c - Procedures for accessing and interpreting |
| 4 | * Devicetree properties and graphs. |
| 5 | * |
| 6 | * Initially created by copying procedures from drivers/of/base.c. This |
| 7 | * file contains the OF property as well as the OF graph interface |
| 8 | * functions. |
| 9 | * |
| 10 | * Paul Mackerras August 1996. |
| 11 | * Copyright (C) 1996-2005 Paul Mackerras. |
| 12 | * |
| 13 | * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner. |
| 14 | * {engebret|bergner}@us.ibm.com |
| 15 | * |
| 16 | * Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net |
| 17 | * |
| 18 | * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and |
| 19 | * Grant Likely. |
Sakari Ailus | 1df09bc | 2017-05-24 17:53:53 +0300 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #define pr_fmt(fmt) "OF: " fmt |
| 23 | |
| 24 | #include <linux/of.h> |
| 25 | #include <linux/of_device.h> |
| 26 | #include <linux/of_graph.h> |
Saravana Kannan | 4104ca7 | 2021-01-21 14:57:12 -0800 | [diff] [blame] | 27 | #include <linux/of_irq.h> |
Sakari Ailus | 1df09bc | 2017-05-24 17:53:53 +0300 | [diff] [blame] | 28 | #include <linux/string.h> |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 29 | #include <linux/moduleparam.h> |
Sakari Ailus | 1df09bc | 2017-05-24 17:53:53 +0300 | [diff] [blame] | 30 | |
| 31 | #include "of_private.h" |
| 32 | |
| 33 | /** |
Dmitry Osipenko | 4ec0a44 | 2020-07-01 10:42:31 +0300 | [diff] [blame] | 34 | * of_graph_is_present() - check graph's presence |
| 35 | * @node: pointer to device_node containing graph port |
| 36 | * |
| 37 | * Return: True if @node has a port or ports (with a port) sub-node, |
| 38 | * false otherwise. |
| 39 | */ |
| 40 | bool of_graph_is_present(const struct device_node *node) |
| 41 | { |
| 42 | struct device_node *ports, *port; |
| 43 | |
| 44 | ports = of_get_child_by_name(node, "ports"); |
| 45 | if (ports) |
| 46 | node = ports; |
| 47 | |
| 48 | port = of_get_child_by_name(node, "port"); |
| 49 | of_node_put(ports); |
| 50 | of_node_put(port); |
| 51 | |
| 52 | return !!port; |
| 53 | } |
| 54 | EXPORT_SYMBOL(of_graph_is_present); |
| 55 | |
| 56 | /** |
Sakari Ailus | 1df09bc | 2017-05-24 17:53:53 +0300 | [diff] [blame] | 57 | * of_property_count_elems_of_size - Count the number of elements in a property |
| 58 | * |
| 59 | * @np: device node from which the property value is to be read. |
| 60 | * @propname: name of the property to be searched. |
| 61 | * @elem_size: size of the individual element |
| 62 | * |
| 63 | * Search for a property in a device node and count the number of elements of |
Rob Herring | 8c8239c | 2021-03-25 10:47:12 -0600 | [diff] [blame] | 64 | * size elem_size in it. |
| 65 | * |
| 66 | * Return: The number of elements on sucess, -EINVAL if the property does not |
| 67 | * exist or its length does not match a multiple of elem_size and -ENODATA if |
| 68 | * the property does not have a value. |
Sakari Ailus | 1df09bc | 2017-05-24 17:53:53 +0300 | [diff] [blame] | 69 | */ |
| 70 | int of_property_count_elems_of_size(const struct device_node *np, |
| 71 | const char *propname, int elem_size) |
| 72 | { |
| 73 | struct property *prop = of_find_property(np, propname, NULL); |
| 74 | |
| 75 | if (!prop) |
| 76 | return -EINVAL; |
| 77 | if (!prop->value) |
| 78 | return -ENODATA; |
| 79 | |
| 80 | if (prop->length % elem_size != 0) { |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 81 | pr_err("size of %s in node %pOF is not a multiple of %d\n", |
| 82 | propname, np, elem_size); |
Sakari Ailus | 1df09bc | 2017-05-24 17:53:53 +0300 | [diff] [blame] | 83 | return -EINVAL; |
| 84 | } |
| 85 | |
| 86 | return prop->length / elem_size; |
| 87 | } |
| 88 | EXPORT_SYMBOL_GPL(of_property_count_elems_of_size); |
| 89 | |
| 90 | /** |
| 91 | * of_find_property_value_of_size |
| 92 | * |
| 93 | * @np: device node from which the property value is to be read. |
| 94 | * @propname: name of the property to be searched. |
| 95 | * @min: minimum allowed length of property value |
| 96 | * @max: maximum allowed length of property value (0 means unlimited) |
| 97 | * @len: if !=NULL, actual length is written to here |
| 98 | * |
| 99 | * Search for a property in a device node and valid the requested size. |
Rob Herring | 8c8239c | 2021-03-25 10:47:12 -0600 | [diff] [blame] | 100 | * |
| 101 | * Return: The property value on success, -EINVAL if the property does not |
| 102 | * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the |
Sakari Ailus | 1df09bc | 2017-05-24 17:53:53 +0300 | [diff] [blame] | 103 | * property data is too small or too large. |
| 104 | * |
| 105 | */ |
| 106 | static void *of_find_property_value_of_size(const struct device_node *np, |
| 107 | const char *propname, u32 min, u32 max, size_t *len) |
| 108 | { |
| 109 | struct property *prop = of_find_property(np, propname, NULL); |
| 110 | |
| 111 | if (!prop) |
| 112 | return ERR_PTR(-EINVAL); |
| 113 | if (!prop->value) |
| 114 | return ERR_PTR(-ENODATA); |
| 115 | if (prop->length < min) |
| 116 | return ERR_PTR(-EOVERFLOW); |
| 117 | if (max && prop->length > max) |
| 118 | return ERR_PTR(-EOVERFLOW); |
| 119 | |
| 120 | if (len) |
| 121 | *len = prop->length; |
| 122 | |
| 123 | return prop->value; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * of_property_read_u32_index - Find and read a u32 from a multi-value property. |
| 128 | * |
| 129 | * @np: device node from which the property value is to be read. |
| 130 | * @propname: name of the property to be searched. |
| 131 | * @index: index of the u32 in the list of values |
| 132 | * @out_value: pointer to return value, modified only if no error. |
| 133 | * |
| 134 | * Search for a property in a device node and read nth 32-bit value from |
Rob Herring | 8c8239c | 2021-03-25 10:47:12 -0600 | [diff] [blame] | 135 | * it. |
| 136 | * |
| 137 | * Return: 0 on success, -EINVAL if the property does not exist, |
Sakari Ailus | 1df09bc | 2017-05-24 17:53:53 +0300 | [diff] [blame] | 138 | * -ENODATA if property does not have a value, and -EOVERFLOW if the |
| 139 | * property data isn't large enough. |
| 140 | * |
| 141 | * The out_value is modified only if a valid u32 value can be decoded. |
| 142 | */ |
| 143 | int of_property_read_u32_index(const struct device_node *np, |
| 144 | const char *propname, |
| 145 | u32 index, u32 *out_value) |
| 146 | { |
| 147 | const u32 *val = of_find_property_value_of_size(np, propname, |
| 148 | ((index + 1) * sizeof(*out_value)), |
| 149 | 0, |
| 150 | NULL); |
| 151 | |
| 152 | if (IS_ERR(val)) |
| 153 | return PTR_ERR(val); |
| 154 | |
| 155 | *out_value = be32_to_cpup(((__be32 *)val) + index); |
| 156 | return 0; |
| 157 | } |
| 158 | EXPORT_SYMBOL_GPL(of_property_read_u32_index); |
| 159 | |
| 160 | /** |
| 161 | * of_property_read_u64_index - Find and read a u64 from a multi-value property. |
| 162 | * |
| 163 | * @np: device node from which the property value is to be read. |
| 164 | * @propname: name of the property to be searched. |
| 165 | * @index: index of the u64 in the list of values |
| 166 | * @out_value: pointer to return value, modified only if no error. |
| 167 | * |
| 168 | * Search for a property in a device node and read nth 64-bit value from |
Rob Herring | 8c8239c | 2021-03-25 10:47:12 -0600 | [diff] [blame] | 169 | * it. |
| 170 | * |
| 171 | * Return: 0 on success, -EINVAL if the property does not exist, |
Sakari Ailus | 1df09bc | 2017-05-24 17:53:53 +0300 | [diff] [blame] | 172 | * -ENODATA if property does not have a value, and -EOVERFLOW if the |
| 173 | * property data isn't large enough. |
| 174 | * |
| 175 | * The out_value is modified only if a valid u64 value can be decoded. |
| 176 | */ |
| 177 | int of_property_read_u64_index(const struct device_node *np, |
| 178 | const char *propname, |
| 179 | u32 index, u64 *out_value) |
| 180 | { |
| 181 | const u64 *val = of_find_property_value_of_size(np, propname, |
| 182 | ((index + 1) * sizeof(*out_value)), |
| 183 | 0, NULL); |
| 184 | |
| 185 | if (IS_ERR(val)) |
| 186 | return PTR_ERR(val); |
| 187 | |
| 188 | *out_value = be64_to_cpup(((__be64 *)val) + index); |
| 189 | return 0; |
| 190 | } |
| 191 | EXPORT_SYMBOL_GPL(of_property_read_u64_index); |
| 192 | |
| 193 | /** |
| 194 | * of_property_read_variable_u8_array - Find and read an array of u8 from a |
| 195 | * property, with bounds on the minimum and maximum array size. |
| 196 | * |
| 197 | * @np: device node from which the property value is to be read. |
| 198 | * @propname: name of the property to be searched. |
Matti Vaittinen | 7f3fefe | 2019-11-13 08:43:38 +0200 | [diff] [blame] | 199 | * @out_values: pointer to found values. |
Sakari Ailus | 1df09bc | 2017-05-24 17:53:53 +0300 | [diff] [blame] | 200 | * @sz_min: minimum number of array elements to read |
| 201 | * @sz_max: maximum number of array elements to read, if zero there is no |
| 202 | * upper limit on the number of elements in the dts entry but only |
| 203 | * sz_min will be read. |
| 204 | * |
| 205 | * Search for a property in a device node and read 8-bit value(s) from |
Rob Herring | 8c8239c | 2021-03-25 10:47:12 -0600 | [diff] [blame] | 206 | * it. |
Sakari Ailus | 1df09bc | 2017-05-24 17:53:53 +0300 | [diff] [blame] | 207 | * |
| 208 | * dts entry of array should be like: |
Rob Herring | 8c8239c | 2021-03-25 10:47:12 -0600 | [diff] [blame] | 209 | * ``property = /bits/ 8 <0x50 0x60 0x70>;`` |
| 210 | * |
| 211 | * Return: The number of elements read on success, -EINVAL if the property |
| 212 | * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW |
| 213 | * if the property data is smaller than sz_min or longer than sz_max. |
Sakari Ailus | 1df09bc | 2017-05-24 17:53:53 +0300 | [diff] [blame] | 214 | * |
| 215 | * The out_values is modified only if a valid u8 value can be decoded. |
| 216 | */ |
| 217 | int of_property_read_variable_u8_array(const struct device_node *np, |
| 218 | const char *propname, u8 *out_values, |
| 219 | size_t sz_min, size_t sz_max) |
| 220 | { |
| 221 | size_t sz, count; |
| 222 | const u8 *val = of_find_property_value_of_size(np, propname, |
| 223 | (sz_min * sizeof(*out_values)), |
| 224 | (sz_max * sizeof(*out_values)), |
| 225 | &sz); |
| 226 | |
| 227 | if (IS_ERR(val)) |
| 228 | return PTR_ERR(val); |
| 229 | |
| 230 | if (!sz_max) |
| 231 | sz = sz_min; |
| 232 | else |
| 233 | sz /= sizeof(*out_values); |
| 234 | |
| 235 | count = sz; |
| 236 | while (count--) |
| 237 | *out_values++ = *val++; |
| 238 | |
| 239 | return sz; |
| 240 | } |
| 241 | EXPORT_SYMBOL_GPL(of_property_read_variable_u8_array); |
| 242 | |
| 243 | /** |
| 244 | * of_property_read_variable_u16_array - Find and read an array of u16 from a |
| 245 | * property, with bounds on the minimum and maximum array size. |
| 246 | * |
| 247 | * @np: device node from which the property value is to be read. |
| 248 | * @propname: name of the property to be searched. |
Matti Vaittinen | 7f3fefe | 2019-11-13 08:43:38 +0200 | [diff] [blame] | 249 | * @out_values: pointer to found values. |
Sakari Ailus | 1df09bc | 2017-05-24 17:53:53 +0300 | [diff] [blame] | 250 | * @sz_min: minimum number of array elements to read |
| 251 | * @sz_max: maximum number of array elements to read, if zero there is no |
| 252 | * upper limit on the number of elements in the dts entry but only |
| 253 | * sz_min will be read. |
| 254 | * |
| 255 | * Search for a property in a device node and read 16-bit value(s) from |
Rob Herring | 8c8239c | 2021-03-25 10:47:12 -0600 | [diff] [blame] | 256 | * it. |
Sakari Ailus | 1df09bc | 2017-05-24 17:53:53 +0300 | [diff] [blame] | 257 | * |
| 258 | * dts entry of array should be like: |
Rob Herring | 8c8239c | 2021-03-25 10:47:12 -0600 | [diff] [blame] | 259 | * ``property = /bits/ 16 <0x5000 0x6000 0x7000>;`` |
| 260 | * |
| 261 | * Return: The number of elements read on success, -EINVAL if the property |
| 262 | * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW |
| 263 | * if the property data is smaller than sz_min or longer than sz_max. |
Sakari Ailus | 1df09bc | 2017-05-24 17:53:53 +0300 | [diff] [blame] | 264 | * |
| 265 | * The out_values is modified only if a valid u16 value can be decoded. |
| 266 | */ |
| 267 | int of_property_read_variable_u16_array(const struct device_node *np, |
| 268 | const char *propname, u16 *out_values, |
| 269 | size_t sz_min, size_t sz_max) |
| 270 | { |
| 271 | size_t sz, count; |
| 272 | const __be16 *val = of_find_property_value_of_size(np, propname, |
| 273 | (sz_min * sizeof(*out_values)), |
| 274 | (sz_max * sizeof(*out_values)), |
| 275 | &sz); |
| 276 | |
| 277 | if (IS_ERR(val)) |
| 278 | return PTR_ERR(val); |
| 279 | |
| 280 | if (!sz_max) |
| 281 | sz = sz_min; |
| 282 | else |
| 283 | sz /= sizeof(*out_values); |
| 284 | |
| 285 | count = sz; |
| 286 | while (count--) |
| 287 | *out_values++ = be16_to_cpup(val++); |
| 288 | |
| 289 | return sz; |
| 290 | } |
| 291 | EXPORT_SYMBOL_GPL(of_property_read_variable_u16_array); |
| 292 | |
| 293 | /** |
| 294 | * of_property_read_variable_u32_array - Find and read an array of 32 bit |
| 295 | * integers from a property, with bounds on the minimum and maximum array size. |
| 296 | * |
| 297 | * @np: device node from which the property value is to be read. |
| 298 | * @propname: name of the property to be searched. |
Matti Vaittinen | 7f3fefe | 2019-11-13 08:43:38 +0200 | [diff] [blame] | 299 | * @out_values: pointer to return found values. |
Sakari Ailus | 1df09bc | 2017-05-24 17:53:53 +0300 | [diff] [blame] | 300 | * @sz_min: minimum number of array elements to read |
| 301 | * @sz_max: maximum number of array elements to read, if zero there is no |
| 302 | * upper limit on the number of elements in the dts entry but only |
| 303 | * sz_min will be read. |
| 304 | * |
| 305 | * Search for a property in a device node and read 32-bit value(s) from |
Rob Herring | 8c8239c | 2021-03-25 10:47:12 -0600 | [diff] [blame] | 306 | * it. |
| 307 | * |
| 308 | * Return: The number of elements read on success, -EINVAL if the property |
Sakari Ailus | 1df09bc | 2017-05-24 17:53:53 +0300 | [diff] [blame] | 309 | * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW |
| 310 | * if the property data is smaller than sz_min or longer than sz_max. |
| 311 | * |
| 312 | * The out_values is modified only if a valid u32 value can be decoded. |
| 313 | */ |
| 314 | int of_property_read_variable_u32_array(const struct device_node *np, |
| 315 | const char *propname, u32 *out_values, |
| 316 | size_t sz_min, size_t sz_max) |
| 317 | { |
| 318 | size_t sz, count; |
| 319 | const __be32 *val = of_find_property_value_of_size(np, propname, |
| 320 | (sz_min * sizeof(*out_values)), |
| 321 | (sz_max * sizeof(*out_values)), |
| 322 | &sz); |
| 323 | |
| 324 | if (IS_ERR(val)) |
| 325 | return PTR_ERR(val); |
| 326 | |
| 327 | if (!sz_max) |
| 328 | sz = sz_min; |
| 329 | else |
| 330 | sz /= sizeof(*out_values); |
| 331 | |
| 332 | count = sz; |
| 333 | while (count--) |
| 334 | *out_values++ = be32_to_cpup(val++); |
| 335 | |
| 336 | return sz; |
| 337 | } |
| 338 | EXPORT_SYMBOL_GPL(of_property_read_variable_u32_array); |
| 339 | |
| 340 | /** |
| 341 | * of_property_read_u64 - Find and read a 64 bit integer from a property |
| 342 | * @np: device node from which the property value is to be read. |
| 343 | * @propname: name of the property to be searched. |
| 344 | * @out_value: pointer to return value, modified only if return value is 0. |
| 345 | * |
| 346 | * Search for a property in a device node and read a 64-bit value from |
Rob Herring | 8c8239c | 2021-03-25 10:47:12 -0600 | [diff] [blame] | 347 | * it. |
| 348 | * |
| 349 | * Return: 0 on success, -EINVAL if the property does not exist, |
Sakari Ailus | 1df09bc | 2017-05-24 17:53:53 +0300 | [diff] [blame] | 350 | * -ENODATA if property does not have a value, and -EOVERFLOW if the |
| 351 | * property data isn't large enough. |
| 352 | * |
| 353 | * The out_value is modified only if a valid u64 value can be decoded. |
| 354 | */ |
| 355 | int of_property_read_u64(const struct device_node *np, const char *propname, |
| 356 | u64 *out_value) |
| 357 | { |
| 358 | const __be32 *val = of_find_property_value_of_size(np, propname, |
| 359 | sizeof(*out_value), |
| 360 | 0, |
| 361 | NULL); |
| 362 | |
| 363 | if (IS_ERR(val)) |
| 364 | return PTR_ERR(val); |
| 365 | |
| 366 | *out_value = of_read_number(val, 2); |
| 367 | return 0; |
| 368 | } |
| 369 | EXPORT_SYMBOL_GPL(of_property_read_u64); |
| 370 | |
| 371 | /** |
| 372 | * of_property_read_variable_u64_array - Find and read an array of 64 bit |
| 373 | * integers from a property, with bounds on the minimum and maximum array size. |
| 374 | * |
| 375 | * @np: device node from which the property value is to be read. |
| 376 | * @propname: name of the property to be searched. |
Matti Vaittinen | 7f3fefe | 2019-11-13 08:43:38 +0200 | [diff] [blame] | 377 | * @out_values: pointer to found values. |
Sakari Ailus | 1df09bc | 2017-05-24 17:53:53 +0300 | [diff] [blame] | 378 | * @sz_min: minimum number of array elements to read |
| 379 | * @sz_max: maximum number of array elements to read, if zero there is no |
| 380 | * upper limit on the number of elements in the dts entry but only |
| 381 | * sz_min will be read. |
| 382 | * |
| 383 | * Search for a property in a device node and read 64-bit value(s) from |
Rob Herring | 8c8239c | 2021-03-25 10:47:12 -0600 | [diff] [blame] | 384 | * it. |
| 385 | * |
| 386 | * Return: The number of elements read on success, -EINVAL if the property |
Sakari Ailus | 1df09bc | 2017-05-24 17:53:53 +0300 | [diff] [blame] | 387 | * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW |
| 388 | * if the property data is smaller than sz_min or longer than sz_max. |
| 389 | * |
| 390 | * The out_values is modified only if a valid u64 value can be decoded. |
| 391 | */ |
| 392 | int of_property_read_variable_u64_array(const struct device_node *np, |
| 393 | const char *propname, u64 *out_values, |
| 394 | size_t sz_min, size_t sz_max) |
| 395 | { |
| 396 | size_t sz, count; |
| 397 | const __be32 *val = of_find_property_value_of_size(np, propname, |
| 398 | (sz_min * sizeof(*out_values)), |
| 399 | (sz_max * sizeof(*out_values)), |
| 400 | &sz); |
| 401 | |
| 402 | if (IS_ERR(val)) |
| 403 | return PTR_ERR(val); |
| 404 | |
| 405 | if (!sz_max) |
| 406 | sz = sz_min; |
| 407 | else |
| 408 | sz /= sizeof(*out_values); |
| 409 | |
| 410 | count = sz; |
| 411 | while (count--) { |
| 412 | *out_values++ = of_read_number(val, 2); |
| 413 | val += 2; |
| 414 | } |
| 415 | |
| 416 | return sz; |
| 417 | } |
| 418 | EXPORT_SYMBOL_GPL(of_property_read_variable_u64_array); |
| 419 | |
| 420 | /** |
| 421 | * of_property_read_string - Find and read a string from a property |
| 422 | * @np: device node from which the property value is to be read. |
| 423 | * @propname: name of the property to be searched. |
| 424 | * @out_string: pointer to null terminated return string, modified only if |
| 425 | * return value is 0. |
| 426 | * |
| 427 | * Search for a property in a device tree node and retrieve a null |
Rob Herring | 8c8239c | 2021-03-25 10:47:12 -0600 | [diff] [blame] | 428 | * terminated string value (pointer to data, not a copy). |
| 429 | * |
| 430 | * Return: 0 on success, -EINVAL if the property does not exist, -ENODATA if |
| 431 | * property does not have a value, and -EILSEQ if the string is not |
| 432 | * null-terminated within the length of the property data. |
Sakari Ailus | 1df09bc | 2017-05-24 17:53:53 +0300 | [diff] [blame] | 433 | * |
| 434 | * The out_string pointer is modified only if a valid string can be decoded. |
| 435 | */ |
| 436 | int of_property_read_string(const struct device_node *np, const char *propname, |
| 437 | const char **out_string) |
| 438 | { |
| 439 | const struct property *prop = of_find_property(np, propname, NULL); |
| 440 | if (!prop) |
| 441 | return -EINVAL; |
| 442 | if (!prop->value) |
| 443 | return -ENODATA; |
| 444 | if (strnlen(prop->value, prop->length) >= prop->length) |
| 445 | return -EILSEQ; |
| 446 | *out_string = prop->value; |
| 447 | return 0; |
| 448 | } |
| 449 | EXPORT_SYMBOL_GPL(of_property_read_string); |
| 450 | |
| 451 | /** |
| 452 | * of_property_match_string() - Find string in a list and return index |
| 453 | * @np: pointer to node containing string list property |
| 454 | * @propname: string list property name |
| 455 | * @string: pointer to string to search for in string list |
| 456 | * |
| 457 | * This function searches a string list property and returns the index |
| 458 | * of a specific string value. |
| 459 | */ |
| 460 | int of_property_match_string(const struct device_node *np, const char *propname, |
| 461 | const char *string) |
| 462 | { |
| 463 | const struct property *prop = of_find_property(np, propname, NULL); |
| 464 | size_t l; |
| 465 | int i; |
| 466 | const char *p, *end; |
| 467 | |
| 468 | if (!prop) |
| 469 | return -EINVAL; |
| 470 | if (!prop->value) |
| 471 | return -ENODATA; |
| 472 | |
| 473 | p = prop->value; |
| 474 | end = p + prop->length; |
| 475 | |
| 476 | for (i = 0; p < end; i++, p += l) { |
| 477 | l = strnlen(p, end - p) + 1; |
| 478 | if (p + l > end) |
| 479 | return -EILSEQ; |
| 480 | pr_debug("comparing %s with %s\n", string, p); |
| 481 | if (strcmp(string, p) == 0) |
| 482 | return i; /* Found it; return index */ |
| 483 | } |
| 484 | return -ENODATA; |
| 485 | } |
| 486 | EXPORT_SYMBOL_GPL(of_property_match_string); |
| 487 | |
| 488 | /** |
| 489 | * of_property_read_string_helper() - Utility helper for parsing string properties |
| 490 | * @np: device node from which the property value is to be read. |
| 491 | * @propname: name of the property to be searched. |
| 492 | * @out_strs: output array of string pointers. |
| 493 | * @sz: number of array elements to read. |
| 494 | * @skip: Number of strings to skip over at beginning of list. |
| 495 | * |
| 496 | * Don't call this function directly. It is a utility helper for the |
| 497 | * of_property_read_string*() family of functions. |
| 498 | */ |
| 499 | int of_property_read_string_helper(const struct device_node *np, |
| 500 | const char *propname, const char **out_strs, |
| 501 | size_t sz, int skip) |
| 502 | { |
| 503 | const struct property *prop = of_find_property(np, propname, NULL); |
| 504 | int l = 0, i = 0; |
| 505 | const char *p, *end; |
| 506 | |
| 507 | if (!prop) |
| 508 | return -EINVAL; |
| 509 | if (!prop->value) |
| 510 | return -ENODATA; |
| 511 | p = prop->value; |
| 512 | end = p + prop->length; |
| 513 | |
| 514 | for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) { |
| 515 | l = strnlen(p, end - p) + 1; |
| 516 | if (p + l > end) |
| 517 | return -EILSEQ; |
| 518 | if (out_strs && i >= skip) |
| 519 | *out_strs++ = p; |
| 520 | } |
| 521 | i -= skip; |
| 522 | return i <= 0 ? -ENODATA : i; |
| 523 | } |
| 524 | EXPORT_SYMBOL_GPL(of_property_read_string_helper); |
| 525 | |
| 526 | const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur, |
| 527 | u32 *pu) |
| 528 | { |
| 529 | const void *curv = cur; |
| 530 | |
| 531 | if (!prop) |
| 532 | return NULL; |
| 533 | |
| 534 | if (!cur) { |
| 535 | curv = prop->value; |
| 536 | goto out_val; |
| 537 | } |
| 538 | |
| 539 | curv += sizeof(*cur); |
| 540 | if (curv >= prop->value + prop->length) |
| 541 | return NULL; |
| 542 | |
| 543 | out_val: |
| 544 | *pu = be32_to_cpup(curv); |
| 545 | return curv; |
| 546 | } |
| 547 | EXPORT_SYMBOL_GPL(of_prop_next_u32); |
| 548 | |
| 549 | const char *of_prop_next_string(struct property *prop, const char *cur) |
| 550 | { |
| 551 | const void *curv = cur; |
| 552 | |
| 553 | if (!prop) |
| 554 | return NULL; |
| 555 | |
| 556 | if (!cur) |
| 557 | return prop->value; |
| 558 | |
| 559 | curv += strlen(cur) + 1; |
| 560 | if (curv >= prop->value + prop->length) |
| 561 | return NULL; |
| 562 | |
| 563 | return curv; |
| 564 | } |
| 565 | EXPORT_SYMBOL_GPL(of_prop_next_string); |
| 566 | |
| 567 | /** |
| 568 | * of_graph_parse_endpoint() - parse common endpoint node properties |
| 569 | * @node: pointer to endpoint device_node |
| 570 | * @endpoint: pointer to the OF endpoint data structure |
| 571 | * |
| 572 | * The caller should hold a reference to @node. |
| 573 | */ |
| 574 | int of_graph_parse_endpoint(const struct device_node *node, |
| 575 | struct of_endpoint *endpoint) |
| 576 | { |
| 577 | struct device_node *port_node = of_get_parent(node); |
| 578 | |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 579 | WARN_ONCE(!port_node, "%s(): endpoint %pOF has no parent node\n", |
| 580 | __func__, node); |
Sakari Ailus | 1df09bc | 2017-05-24 17:53:53 +0300 | [diff] [blame] | 581 | |
| 582 | memset(endpoint, 0, sizeof(*endpoint)); |
| 583 | |
| 584 | endpoint->local_node = node; |
| 585 | /* |
| 586 | * It doesn't matter whether the two calls below succeed. |
| 587 | * If they don't then the default value 0 is used. |
| 588 | */ |
| 589 | of_property_read_u32(port_node, "reg", &endpoint->port); |
| 590 | of_property_read_u32(node, "reg", &endpoint->id); |
| 591 | |
| 592 | of_node_put(port_node); |
| 593 | |
| 594 | return 0; |
| 595 | } |
| 596 | EXPORT_SYMBOL(of_graph_parse_endpoint); |
| 597 | |
| 598 | /** |
| 599 | * of_graph_get_port_by_id() - get the port matching a given id |
| 600 | * @parent: pointer to the parent device node |
| 601 | * @id: id of the port |
| 602 | * |
| 603 | * Return: A 'port' node pointer with refcount incremented. The caller |
| 604 | * has to use of_node_put() on it when done. |
| 605 | */ |
| 606 | struct device_node *of_graph_get_port_by_id(struct device_node *parent, u32 id) |
| 607 | { |
| 608 | struct device_node *node, *port; |
| 609 | |
| 610 | node = of_get_child_by_name(parent, "ports"); |
| 611 | if (node) |
| 612 | parent = node; |
| 613 | |
| 614 | for_each_child_of_node(parent, port) { |
| 615 | u32 port_id = 0; |
| 616 | |
Rob Herring | b3e46d1 | 2018-08-27 08:37:06 -0500 | [diff] [blame] | 617 | if (!of_node_name_eq(port, "port")) |
Sakari Ailus | 1df09bc | 2017-05-24 17:53:53 +0300 | [diff] [blame] | 618 | continue; |
| 619 | of_property_read_u32(port, "reg", &port_id); |
| 620 | if (id == port_id) |
| 621 | break; |
| 622 | } |
| 623 | |
| 624 | of_node_put(node); |
| 625 | |
| 626 | return port; |
| 627 | } |
| 628 | EXPORT_SYMBOL(of_graph_get_port_by_id); |
| 629 | |
| 630 | /** |
| 631 | * of_graph_get_next_endpoint() - get next endpoint node |
| 632 | * @parent: pointer to the parent device node |
| 633 | * @prev: previous endpoint node, or NULL to get first |
| 634 | * |
| 635 | * Return: An 'endpoint' node pointer with refcount incremented. Refcount |
| 636 | * of the passed @prev node is decremented. |
| 637 | */ |
| 638 | struct device_node *of_graph_get_next_endpoint(const struct device_node *parent, |
| 639 | struct device_node *prev) |
| 640 | { |
| 641 | struct device_node *endpoint; |
| 642 | struct device_node *port; |
| 643 | |
| 644 | if (!parent) |
| 645 | return NULL; |
| 646 | |
| 647 | /* |
| 648 | * Start by locating the port node. If no previous endpoint is specified |
| 649 | * search for the first port node, otherwise get the previous endpoint |
| 650 | * parent port node. |
| 651 | */ |
| 652 | if (!prev) { |
| 653 | struct device_node *node; |
| 654 | |
| 655 | node = of_get_child_by_name(parent, "ports"); |
| 656 | if (node) |
| 657 | parent = node; |
| 658 | |
| 659 | port = of_get_child_by_name(parent, "port"); |
| 660 | of_node_put(node); |
| 661 | |
| 662 | if (!port) { |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 663 | pr_err("graph: no port node found in %pOF\n", parent); |
Sakari Ailus | 1df09bc | 2017-05-24 17:53:53 +0300 | [diff] [blame] | 664 | return NULL; |
| 665 | } |
| 666 | } else { |
| 667 | port = of_get_parent(prev); |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 668 | if (WARN_ONCE(!port, "%s(): endpoint %pOF has no parent node\n", |
| 669 | __func__, prev)) |
Sakari Ailus | 1df09bc | 2017-05-24 17:53:53 +0300 | [diff] [blame] | 670 | return NULL; |
| 671 | } |
| 672 | |
| 673 | while (1) { |
| 674 | /* |
| 675 | * Now that we have a port node, get the next endpoint by |
| 676 | * getting the next child. If the previous endpoint is NULL this |
| 677 | * will return the first child. |
| 678 | */ |
| 679 | endpoint = of_get_next_child(port, prev); |
| 680 | if (endpoint) { |
| 681 | of_node_put(port); |
| 682 | return endpoint; |
| 683 | } |
| 684 | |
| 685 | /* No more endpoints under this port, try the next one. */ |
| 686 | prev = NULL; |
| 687 | |
| 688 | do { |
| 689 | port = of_get_next_child(parent, port); |
| 690 | if (!port) |
| 691 | return NULL; |
Rob Herring | b3e46d1 | 2018-08-27 08:37:06 -0500 | [diff] [blame] | 692 | } while (!of_node_name_eq(port, "port")); |
Sakari Ailus | 1df09bc | 2017-05-24 17:53:53 +0300 | [diff] [blame] | 693 | } |
| 694 | } |
| 695 | EXPORT_SYMBOL(of_graph_get_next_endpoint); |
| 696 | |
| 697 | /** |
| 698 | * of_graph_get_endpoint_by_regs() - get endpoint node of specific identifiers |
| 699 | * @parent: pointer to the parent device node |
| 700 | * @port_reg: identifier (value of reg property) of the parent port node |
| 701 | * @reg: identifier (value of reg property) of the endpoint node |
| 702 | * |
| 703 | * Return: An 'endpoint' node pointer which is identified by reg and at the same |
| 704 | * is the child of a port node identified by port_reg. reg and port_reg are |
Maxime Ripard | deb387d | 2019-03-15 10:22:47 +0100 | [diff] [blame] | 705 | * ignored when they are -1. Use of_node_put() on the pointer when done. |
Sakari Ailus | 1df09bc | 2017-05-24 17:53:53 +0300 | [diff] [blame] | 706 | */ |
| 707 | struct device_node *of_graph_get_endpoint_by_regs( |
| 708 | const struct device_node *parent, int port_reg, int reg) |
| 709 | { |
| 710 | struct of_endpoint endpoint; |
| 711 | struct device_node *node = NULL; |
| 712 | |
| 713 | for_each_endpoint_of_node(parent, node) { |
| 714 | of_graph_parse_endpoint(node, &endpoint); |
| 715 | if (((port_reg == -1) || (endpoint.port == port_reg)) && |
| 716 | ((reg == -1) || (endpoint.id == reg))) |
| 717 | return node; |
| 718 | } |
| 719 | |
| 720 | return NULL; |
| 721 | } |
| 722 | EXPORT_SYMBOL(of_graph_get_endpoint_by_regs); |
| 723 | |
| 724 | /** |
Rob Herring | b8ba92b | 2017-07-05 08:24:05 -0500 | [diff] [blame] | 725 | * of_graph_get_remote_endpoint() - get remote endpoint node |
| 726 | * @node: pointer to a local endpoint device_node |
| 727 | * |
| 728 | * Return: Remote endpoint node associated with remote endpoint node linked |
| 729 | * to @node. Use of_node_put() on it when done. |
| 730 | */ |
| 731 | struct device_node *of_graph_get_remote_endpoint(const struct device_node *node) |
| 732 | { |
| 733 | /* Get remote endpoint node. */ |
| 734 | return of_parse_phandle(node, "remote-endpoint", 0); |
| 735 | } |
| 736 | EXPORT_SYMBOL(of_graph_get_remote_endpoint); |
| 737 | |
| 738 | /** |
| 739 | * of_graph_get_port_parent() - get port's parent node |
| 740 | * @node: pointer to a local endpoint device_node |
| 741 | * |
| 742 | * Return: device node associated with endpoint node linked |
| 743 | * to @node. Use of_node_put() on it when done. |
| 744 | */ |
| 745 | struct device_node *of_graph_get_port_parent(struct device_node *node) |
| 746 | { |
| 747 | unsigned int depth; |
| 748 | |
Tony Lindgren | c0a480d | 2017-07-28 01:23:15 -0700 | [diff] [blame] | 749 | if (!node) |
| 750 | return NULL; |
| 751 | |
| 752 | /* |
| 753 | * Preserve usecount for passed in node as of_get_next_parent() |
| 754 | * will do of_node_put() on it. |
| 755 | */ |
| 756 | of_node_get(node); |
| 757 | |
Rob Herring | b8ba92b | 2017-07-05 08:24:05 -0500 | [diff] [blame] | 758 | /* Walk 3 levels up only if there is 'ports' node. */ |
| 759 | for (depth = 3; depth && node; depth--) { |
| 760 | node = of_get_next_parent(node); |
Rob Herring | b3e46d1 | 2018-08-27 08:37:06 -0500 | [diff] [blame] | 761 | if (depth == 2 && !of_node_name_eq(node, "ports")) |
Rob Herring | b8ba92b | 2017-07-05 08:24:05 -0500 | [diff] [blame] | 762 | break; |
| 763 | } |
| 764 | return node; |
| 765 | } |
| 766 | EXPORT_SYMBOL(of_graph_get_port_parent); |
| 767 | |
| 768 | /** |
Sakari Ailus | 1df09bc | 2017-05-24 17:53:53 +0300 | [diff] [blame] | 769 | * of_graph_get_remote_port_parent() - get remote port's parent node |
| 770 | * @node: pointer to a local endpoint device_node |
| 771 | * |
| 772 | * Return: Remote device node associated with remote endpoint node linked |
| 773 | * to @node. Use of_node_put() on it when done. |
| 774 | */ |
| 775 | struct device_node *of_graph_get_remote_port_parent( |
| 776 | const struct device_node *node) |
| 777 | { |
Tony Lindgren | c0a480d | 2017-07-28 01:23:15 -0700 | [diff] [blame] | 778 | struct device_node *np, *pp; |
Sakari Ailus | 1df09bc | 2017-05-24 17:53:53 +0300 | [diff] [blame] | 779 | |
| 780 | /* Get remote endpoint node. */ |
Rob Herring | b8ba92b | 2017-07-05 08:24:05 -0500 | [diff] [blame] | 781 | np = of_graph_get_remote_endpoint(node); |
Sakari Ailus | 1df09bc | 2017-05-24 17:53:53 +0300 | [diff] [blame] | 782 | |
Tony Lindgren | c0a480d | 2017-07-28 01:23:15 -0700 | [diff] [blame] | 783 | pp = of_graph_get_port_parent(np); |
| 784 | |
| 785 | of_node_put(np); |
| 786 | |
| 787 | return pp; |
Sakari Ailus | 1df09bc | 2017-05-24 17:53:53 +0300 | [diff] [blame] | 788 | } |
| 789 | EXPORT_SYMBOL(of_graph_get_remote_port_parent); |
| 790 | |
| 791 | /** |
| 792 | * of_graph_get_remote_port() - get remote port node |
| 793 | * @node: pointer to a local endpoint device_node |
| 794 | * |
| 795 | * Return: Remote port node associated with remote endpoint node linked |
Rob Herring | 8c8239c | 2021-03-25 10:47:12 -0600 | [diff] [blame] | 796 | * to @node. Use of_node_put() on it when done. |
Sakari Ailus | 1df09bc | 2017-05-24 17:53:53 +0300 | [diff] [blame] | 797 | */ |
| 798 | struct device_node *of_graph_get_remote_port(const struct device_node *node) |
| 799 | { |
| 800 | struct device_node *np; |
| 801 | |
| 802 | /* Get remote endpoint node. */ |
Rob Herring | b8ba92b | 2017-07-05 08:24:05 -0500 | [diff] [blame] | 803 | np = of_graph_get_remote_endpoint(node); |
Sakari Ailus | 1df09bc | 2017-05-24 17:53:53 +0300 | [diff] [blame] | 804 | if (!np) |
| 805 | return NULL; |
| 806 | return of_get_next_parent(np); |
| 807 | } |
| 808 | EXPORT_SYMBOL(of_graph_get_remote_port); |
| 809 | |
Rob Herring | b8ba92b | 2017-07-05 08:24:05 -0500 | [diff] [blame] | 810 | int of_graph_get_endpoint_count(const struct device_node *np) |
| 811 | { |
| 812 | struct device_node *endpoint; |
| 813 | int num = 0; |
| 814 | |
| 815 | for_each_endpoint_of_node(np, endpoint) |
| 816 | num++; |
| 817 | |
| 818 | return num; |
| 819 | } |
| 820 | EXPORT_SYMBOL(of_graph_get_endpoint_count); |
| 821 | |
Sakari Ailus | 1df09bc | 2017-05-24 17:53:53 +0300 | [diff] [blame] | 822 | /** |
| 823 | * of_graph_get_remote_node() - get remote parent device_node for given port/endpoint |
| 824 | * @node: pointer to parent device_node containing graph port/endpoint |
| 825 | * @port: identifier (value of reg property) of the parent port node |
| 826 | * @endpoint: identifier (value of reg property) of the endpoint node |
| 827 | * |
| 828 | * Return: Remote device node associated with remote endpoint node linked |
Rob Herring | 8c8239c | 2021-03-25 10:47:12 -0600 | [diff] [blame] | 829 | * to @node. Use of_node_put() on it when done. |
Sakari Ailus | 1df09bc | 2017-05-24 17:53:53 +0300 | [diff] [blame] | 830 | */ |
| 831 | struct device_node *of_graph_get_remote_node(const struct device_node *node, |
| 832 | u32 port, u32 endpoint) |
| 833 | { |
| 834 | struct device_node *endpoint_node, *remote; |
| 835 | |
| 836 | endpoint_node = of_graph_get_endpoint_by_regs(node, port, endpoint); |
| 837 | if (!endpoint_node) { |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 838 | pr_debug("no valid endpoint (%d, %d) for node %pOF\n", |
| 839 | port, endpoint, node); |
Sakari Ailus | 1df09bc | 2017-05-24 17:53:53 +0300 | [diff] [blame] | 840 | return NULL; |
| 841 | } |
| 842 | |
| 843 | remote = of_graph_get_remote_port_parent(endpoint_node); |
| 844 | of_node_put(endpoint_node); |
| 845 | if (!remote) { |
| 846 | pr_debug("no valid remote node\n"); |
| 847 | return NULL; |
| 848 | } |
| 849 | |
| 850 | if (!of_device_is_available(remote)) { |
| 851 | pr_debug("not available for remote node\n"); |
Julia Lawall | 28b170e | 2019-01-13 10:44:50 +0100 | [diff] [blame] | 852 | of_node_put(remote); |
Sakari Ailus | 1df09bc | 2017-05-24 17:53:53 +0300 | [diff] [blame] | 853 | return NULL; |
| 854 | } |
| 855 | |
| 856 | return remote; |
| 857 | } |
| 858 | EXPORT_SYMBOL(of_graph_get_remote_node); |
Sakari Ailus | 3708184 | 2017-06-06 12:37:37 +0300 | [diff] [blame] | 859 | |
Sakari Ailus | cf89a31 | 2017-09-19 12:39:11 +0300 | [diff] [blame] | 860 | static struct fwnode_handle *of_fwnode_get(struct fwnode_handle *fwnode) |
Sakari Ailus | 3708184 | 2017-06-06 12:37:37 +0300 | [diff] [blame] | 861 | { |
Sakari Ailus | cf89a31 | 2017-09-19 12:39:11 +0300 | [diff] [blame] | 862 | return of_fwnode_handle(of_node_get(to_of_node(fwnode))); |
Sakari Ailus | 3708184 | 2017-06-06 12:37:37 +0300 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | static void of_fwnode_put(struct fwnode_handle *fwnode) |
| 866 | { |
| 867 | of_node_put(to_of_node(fwnode)); |
| 868 | } |
| 869 | |
Sakari Ailus | 37ba983 | 2017-07-21 14:39:36 +0300 | [diff] [blame] | 870 | static bool of_fwnode_device_is_available(const struct fwnode_handle *fwnode) |
Sakari Ailus | 2294b3a | 2017-06-06 12:37:39 +0300 | [diff] [blame] | 871 | { |
| 872 | return of_device_is_available(to_of_node(fwnode)); |
| 873 | } |
| 874 | |
Sakari Ailus | 37ba983 | 2017-07-21 14:39:36 +0300 | [diff] [blame] | 875 | static bool of_fwnode_property_present(const struct fwnode_handle *fwnode, |
Sakari Ailus | 3708184 | 2017-06-06 12:37:37 +0300 | [diff] [blame] | 876 | const char *propname) |
| 877 | { |
| 878 | return of_property_read_bool(to_of_node(fwnode), propname); |
| 879 | } |
| 880 | |
Sakari Ailus | 37ba983 | 2017-07-21 14:39:36 +0300 | [diff] [blame] | 881 | static int of_fwnode_property_read_int_array(const struct fwnode_handle *fwnode, |
Sakari Ailus | 3708184 | 2017-06-06 12:37:37 +0300 | [diff] [blame] | 882 | const char *propname, |
| 883 | unsigned int elem_size, void *val, |
| 884 | size_t nval) |
| 885 | { |
Sakari Ailus | 37ba983 | 2017-07-21 14:39:36 +0300 | [diff] [blame] | 886 | const struct device_node *node = to_of_node(fwnode); |
Sakari Ailus | 3708184 | 2017-06-06 12:37:37 +0300 | [diff] [blame] | 887 | |
| 888 | if (!val) |
| 889 | return of_property_count_elems_of_size(node, propname, |
| 890 | elem_size); |
| 891 | |
| 892 | switch (elem_size) { |
| 893 | case sizeof(u8): |
| 894 | return of_property_read_u8_array(node, propname, val, nval); |
| 895 | case sizeof(u16): |
| 896 | return of_property_read_u16_array(node, propname, val, nval); |
| 897 | case sizeof(u32): |
| 898 | return of_property_read_u32_array(node, propname, val, nval); |
| 899 | case sizeof(u64): |
| 900 | return of_property_read_u64_array(node, propname, val, nval); |
| 901 | } |
| 902 | |
| 903 | return -ENXIO; |
| 904 | } |
| 905 | |
Sakari Ailus | 37ba983 | 2017-07-21 14:39:36 +0300 | [diff] [blame] | 906 | static int |
| 907 | of_fwnode_property_read_string_array(const struct fwnode_handle *fwnode, |
| 908 | const char *propname, const char **val, |
| 909 | size_t nval) |
Sakari Ailus | 3708184 | 2017-06-06 12:37:37 +0300 | [diff] [blame] | 910 | { |
Sakari Ailus | 37ba983 | 2017-07-21 14:39:36 +0300 | [diff] [blame] | 911 | const struct device_node *node = to_of_node(fwnode); |
Sakari Ailus | 3708184 | 2017-06-06 12:37:37 +0300 | [diff] [blame] | 912 | |
| 913 | return val ? |
| 914 | of_property_read_string_array(node, propname, val, nval) : |
| 915 | of_property_count_strings(node, propname); |
| 916 | } |
| 917 | |
Sakari Ailus | bc0500c | 2019-10-03 15:32:12 +0300 | [diff] [blame] | 918 | static const char *of_fwnode_get_name(const struct fwnode_handle *fwnode) |
| 919 | { |
| 920 | return kbasename(to_of_node(fwnode)->full_name); |
| 921 | } |
| 922 | |
Sakari Ailus | e7e242b | 2019-10-03 15:32:13 +0300 | [diff] [blame] | 923 | static const char *of_fwnode_get_name_prefix(const struct fwnode_handle *fwnode) |
| 924 | { |
| 925 | /* Root needs no prefix here (its name is "/"). */ |
| 926 | if (!to_of_node(fwnode)->parent) |
| 927 | return ""; |
| 928 | |
| 929 | return "/"; |
| 930 | } |
| 931 | |
Sakari Ailus | 37ba983 | 2017-07-21 14:39:36 +0300 | [diff] [blame] | 932 | static struct fwnode_handle * |
| 933 | of_fwnode_get_parent(const struct fwnode_handle *fwnode) |
Sakari Ailus | 3708184 | 2017-06-06 12:37:37 +0300 | [diff] [blame] | 934 | { |
| 935 | return of_fwnode_handle(of_get_parent(to_of_node(fwnode))); |
| 936 | } |
| 937 | |
| 938 | static struct fwnode_handle * |
Sakari Ailus | 37ba983 | 2017-07-21 14:39:36 +0300 | [diff] [blame] | 939 | of_fwnode_get_next_child_node(const struct fwnode_handle *fwnode, |
Sakari Ailus | 3708184 | 2017-06-06 12:37:37 +0300 | [diff] [blame] | 940 | struct fwnode_handle *child) |
| 941 | { |
| 942 | return of_fwnode_handle(of_get_next_available_child(to_of_node(fwnode), |
| 943 | to_of_node(child))); |
| 944 | } |
| 945 | |
| 946 | static struct fwnode_handle * |
Sakari Ailus | 37ba983 | 2017-07-21 14:39:36 +0300 | [diff] [blame] | 947 | of_fwnode_get_named_child_node(const struct fwnode_handle *fwnode, |
Sakari Ailus | 3708184 | 2017-06-06 12:37:37 +0300 | [diff] [blame] | 948 | const char *childname) |
| 949 | { |
Sakari Ailus | 37ba983 | 2017-07-21 14:39:36 +0300 | [diff] [blame] | 950 | const struct device_node *node = to_of_node(fwnode); |
Sakari Ailus | 3708184 | 2017-06-06 12:37:37 +0300 | [diff] [blame] | 951 | struct device_node *child; |
| 952 | |
| 953 | for_each_available_child_of_node(node, child) |
Rob Herring | b3e46d1 | 2018-08-27 08:37:06 -0500 | [diff] [blame] | 954 | if (of_node_name_eq(child, childname)) |
Sakari Ailus | 3708184 | 2017-06-06 12:37:37 +0300 | [diff] [blame] | 955 | return of_fwnode_handle(child); |
| 956 | |
| 957 | return NULL; |
| 958 | } |
| 959 | |
Sakari Ailus | 3e3119d | 2017-07-21 15:11:49 +0300 | [diff] [blame] | 960 | static int |
| 961 | of_fwnode_get_reference_args(const struct fwnode_handle *fwnode, |
| 962 | const char *prop, const char *nargs_prop, |
| 963 | unsigned int nargs, unsigned int index, |
| 964 | struct fwnode_reference_args *args) |
| 965 | { |
| 966 | struct of_phandle_args of_args; |
| 967 | unsigned int i; |
| 968 | int ret; |
| 969 | |
| 970 | if (nargs_prop) |
| 971 | ret = of_parse_phandle_with_args(to_of_node(fwnode), prop, |
| 972 | nargs_prop, index, &of_args); |
| 973 | else |
| 974 | ret = of_parse_phandle_with_fixed_args(to_of_node(fwnode), prop, |
| 975 | nargs, index, &of_args); |
| 976 | if (ret < 0) |
| 977 | return ret; |
| 978 | if (!args) |
| 979 | return 0; |
| 980 | |
| 981 | args->nargs = of_args.args_count; |
| 982 | args->fwnode = of_fwnode_handle(of_args.np); |
| 983 | |
| 984 | for (i = 0; i < NR_FWNODE_REFERENCE_ARGS; i++) |
| 985 | args->args[i] = i < of_args.args_count ? of_args.args[i] : 0; |
| 986 | |
| 987 | return 0; |
| 988 | } |
| 989 | |
Sakari Ailus | 3b27d00 | 2017-06-06 12:37:38 +0300 | [diff] [blame] | 990 | static struct fwnode_handle * |
Sakari Ailus | 37ba983 | 2017-07-21 14:39:36 +0300 | [diff] [blame] | 991 | of_fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode, |
Sakari Ailus | 3b27d00 | 2017-06-06 12:37:38 +0300 | [diff] [blame] | 992 | struct fwnode_handle *prev) |
| 993 | { |
| 994 | return of_fwnode_handle(of_graph_get_next_endpoint(to_of_node(fwnode), |
| 995 | to_of_node(prev))); |
| 996 | } |
| 997 | |
| 998 | static struct fwnode_handle * |
Sakari Ailus | 37ba983 | 2017-07-21 14:39:36 +0300 | [diff] [blame] | 999 | of_fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode) |
Sakari Ailus | 3b27d00 | 2017-06-06 12:37:38 +0300 | [diff] [blame] | 1000 | { |
Kuninori Morimoto | 358155e | 2017-08-10 04:38:16 +0000 | [diff] [blame] | 1001 | return of_fwnode_handle( |
| 1002 | of_graph_get_remote_endpoint(to_of_node(fwnode))); |
Sakari Ailus | 3b27d00 | 2017-06-06 12:37:38 +0300 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | static struct fwnode_handle * |
| 1006 | of_fwnode_graph_get_port_parent(struct fwnode_handle *fwnode) |
| 1007 | { |
| 1008 | struct device_node *np; |
| 1009 | |
| 1010 | /* Get the parent of the port */ |
Niklas Söderlund | 3314c6b | 2017-08-22 02:19:12 +0200 | [diff] [blame] | 1011 | np = of_get_parent(to_of_node(fwnode)); |
Sakari Ailus | 3b27d00 | 2017-06-06 12:37:38 +0300 | [diff] [blame] | 1012 | if (!np) |
| 1013 | return NULL; |
| 1014 | |
| 1015 | /* Is this the "ports" node? If not, it's the port parent. */ |
Rob Herring | b3e46d1 | 2018-08-27 08:37:06 -0500 | [diff] [blame] | 1016 | if (!of_node_name_eq(np, "ports")) |
Sakari Ailus | 3b27d00 | 2017-06-06 12:37:38 +0300 | [diff] [blame] | 1017 | return of_fwnode_handle(np); |
| 1018 | |
| 1019 | return of_fwnode_handle(of_get_next_parent(np)); |
| 1020 | } |
| 1021 | |
Sakari Ailus | 37ba983 | 2017-07-21 14:39:36 +0300 | [diff] [blame] | 1022 | static int of_fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode, |
Sakari Ailus | 3b27d00 | 2017-06-06 12:37:38 +0300 | [diff] [blame] | 1023 | struct fwnode_endpoint *endpoint) |
| 1024 | { |
Sakari Ailus | 37ba983 | 2017-07-21 14:39:36 +0300 | [diff] [blame] | 1025 | const struct device_node *node = to_of_node(fwnode); |
Sakari Ailus | 3b27d00 | 2017-06-06 12:37:38 +0300 | [diff] [blame] | 1026 | struct device_node *port_node = of_get_parent(node); |
| 1027 | |
| 1028 | endpoint->local_fwnode = fwnode; |
| 1029 | |
| 1030 | of_property_read_u32(port_node, "reg", &endpoint->port); |
| 1031 | of_property_read_u32(node, "reg", &endpoint->id); |
| 1032 | |
| 1033 | of_node_put(port_node); |
| 1034 | |
| 1035 | return 0; |
| 1036 | } |
| 1037 | |
Andy Shevchenko | 67dcc26 | 2018-02-09 17:38:36 +0200 | [diff] [blame] | 1038 | static const void * |
Sinan Kaya | 1c2c82e | 2017-12-13 02:20:50 -0500 | [diff] [blame] | 1039 | of_fwnode_device_get_match_data(const struct fwnode_handle *fwnode, |
| 1040 | const struct device *dev) |
| 1041 | { |
Andy Shevchenko | 67dcc26 | 2018-02-09 17:38:36 +0200 | [diff] [blame] | 1042 | return of_device_get_match_data(dev); |
Sinan Kaya | 1c2c82e | 2017-12-13 02:20:50 -0500 | [diff] [blame] | 1043 | } |
| 1044 | |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 1045 | static bool of_is_ancestor_of(struct device_node *test_ancestor, |
| 1046 | struct device_node *child) |
| 1047 | { |
| 1048 | of_node_get(child); |
| 1049 | while (child) { |
| 1050 | if (child == test_ancestor) { |
| 1051 | of_node_put(child); |
Saravana Kannan | 3883539 | 2019-11-20 00:02:29 -0800 | [diff] [blame] | 1052 | return true; |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 1053 | } |
| 1054 | child = of_get_next_parent(child); |
| 1055 | } |
Saravana Kannan | 3883539 | 2019-11-20 00:02:29 -0800 | [diff] [blame] | 1056 | return false; |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 1057 | } |
| 1058 | |
Saravana Kannan | f7514a6 | 2021-03-30 11:50:55 -0700 | [diff] [blame] | 1059 | static struct device_node *of_get_compat_node(struct device_node *np) |
| 1060 | { |
| 1061 | of_node_get(np); |
| 1062 | |
| 1063 | while (np) { |
| 1064 | if (!of_device_is_available(np)) { |
| 1065 | of_node_put(np); |
| 1066 | np = NULL; |
| 1067 | } |
| 1068 | |
| 1069 | if (of_find_property(np, "compatible", NULL)) |
| 1070 | break; |
| 1071 | |
| 1072 | np = of_get_next_parent(np); |
| 1073 | } |
| 1074 | |
| 1075 | return np; |
| 1076 | } |
| 1077 | |
Ulf Hansson | 3cd6bab | 2021-09-02 11:02:21 +0200 | [diff] [blame] | 1078 | static struct device_node *of_get_compat_node_parent(struct device_node *np) |
| 1079 | { |
| 1080 | struct device_node *parent, *node; |
| 1081 | |
| 1082 | parent = of_get_parent(np); |
| 1083 | node = of_get_compat_node(parent); |
| 1084 | of_node_put(parent); |
| 1085 | |
| 1086 | return node; |
| 1087 | } |
| 1088 | |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 1089 | /** |
Saravana Kannan | 8a06d1e | 2020-11-20 18:02:29 -0800 | [diff] [blame] | 1090 | * of_link_to_phandle - Add fwnode link to supplier from supplier phandle |
| 1091 | * @con_np: consumer device tree node |
| 1092 | * @sup_np: supplier device tree node |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 1093 | * |
| 1094 | * Given a phandle to a supplier device tree node (@sup_np), this function |
| 1095 | * finds the device that owns the supplier device tree node and creates a |
| 1096 | * device link from @dev consumer device to the supplier device. This function |
| 1097 | * doesn't create device links for invalid scenarios such as trying to create a |
| 1098 | * link with a parent device as the consumer of its child device. In such |
| 1099 | * cases, it returns an error. |
| 1100 | * |
| 1101 | * Returns: |
Saravana Kannan | 8a06d1e | 2020-11-20 18:02:29 -0800 | [diff] [blame] | 1102 | * - 0 if fwnode link successfully created to supplier |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 1103 | * - -EINVAL if the supplier link is invalid and should not be created |
Saravana Kannan | 8a06d1e | 2020-11-20 18:02:29 -0800 | [diff] [blame] | 1104 | * - -ENODEV if struct device will never be create for supplier |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 1105 | */ |
Saravana Kannan | 8a06d1e | 2020-11-20 18:02:29 -0800 | [diff] [blame] | 1106 | static int of_link_to_phandle(struct device_node *con_np, |
| 1107 | struct device_node *sup_np) |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 1108 | { |
Saravana Kannan | 8a06d1e | 2020-11-20 18:02:29 -0800 | [diff] [blame] | 1109 | struct device *sup_dev; |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 1110 | struct device_node *tmp_np = sup_np; |
| 1111 | |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 1112 | /* |
| 1113 | * Find the device node that contains the supplier phandle. It may be |
| 1114 | * @sup_np or it may be an ancestor of @sup_np. |
| 1115 | */ |
Saravana Kannan | f7514a6 | 2021-03-30 11:50:55 -0700 | [diff] [blame] | 1116 | sup_np = of_get_compat_node(sup_np); |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 1117 | if (!sup_np) { |
Saravana Kannan | 8a06d1e | 2020-11-20 18:02:29 -0800 | [diff] [blame] | 1118 | pr_debug("Not linking %pOFP to %pOFP - No device\n", |
| 1119 | con_np, tmp_np); |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 1120 | return -ENODEV; |
| 1121 | } |
| 1122 | |
| 1123 | /* |
| 1124 | * Don't allow linking a device node as a consumer of one of its |
| 1125 | * descendant nodes. By definition, a child node can't be a functional |
| 1126 | * dependency for the parent node. |
| 1127 | */ |
Saravana Kannan | 8a06d1e | 2020-11-20 18:02:29 -0800 | [diff] [blame] | 1128 | if (of_is_ancestor_of(con_np, sup_np)) { |
| 1129 | pr_debug("Not linking %pOFP to %pOFP - is descendant\n", |
| 1130 | con_np, sup_np); |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 1131 | of_node_put(sup_np); |
| 1132 | return -EINVAL; |
| 1133 | } |
Saravana Kannan | 8a06d1e | 2020-11-20 18:02:29 -0800 | [diff] [blame] | 1134 | |
| 1135 | /* |
| 1136 | * Don't create links to "early devices" that won't have struct devices |
| 1137 | * created for them. |
| 1138 | */ |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 1139 | sup_dev = get_dev_from_fwnode(&sup_np->fwnode); |
Saravana Kannan | 1753c4d | 2021-02-05 14:26:38 -0800 | [diff] [blame] | 1140 | if (!sup_dev && |
| 1141 | (of_node_check_flag(sup_np, OF_POPULATED) || |
| 1142 | sup_np->fwnode.flags & FWNODE_FLAG_NOT_DEVICE)) { |
Saravana Kannan | 8a06d1e | 2020-11-20 18:02:29 -0800 | [diff] [blame] | 1143 | pr_debug("Not linking %pOFP to %pOFP - No struct device\n", |
| 1144 | con_np, sup_np); |
Saravana Kannan | bb278b1 | 2020-06-09 18:19:34 -0700 | [diff] [blame] | 1145 | of_node_put(sup_np); |
Saravana Kannan | ba861f8 | 2019-11-04 22:49:58 -0800 | [diff] [blame] | 1146 | return -ENODEV; |
Saravana Kannan | ba861f8 | 2019-11-04 22:49:58 -0800 | [diff] [blame] | 1147 | } |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 1148 | put_device(sup_dev); |
Saravana Kannan | 8a06d1e | 2020-11-20 18:02:29 -0800 | [diff] [blame] | 1149 | |
| 1150 | fwnode_link_add(of_fwnode_handle(con_np), of_fwnode_handle(sup_np)); |
| 1151 | of_node_put(sup_np); |
| 1152 | |
| 1153 | return 0; |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | /** |
| 1157 | * parse_prop_cells - Property parsing function for suppliers |
| 1158 | * |
| 1159 | * @np: Pointer to device tree node containing a list |
| 1160 | * @prop_name: Name of property to be parsed. Expected to hold phandle values |
| 1161 | * @index: For properties holding a list of phandles, this is the index |
| 1162 | * into the list. |
| 1163 | * @list_name: Property name that is known to contain list of phandle(s) to |
| 1164 | * supplier(s) |
| 1165 | * @cells_name: property name that specifies phandles' arguments count |
| 1166 | * |
| 1167 | * This is a helper function to parse properties that have a known fixed name |
| 1168 | * and are a list of phandles and phandle arguments. |
| 1169 | * |
| 1170 | * Returns: |
| 1171 | * - phandle node pointer with refcount incremented. Caller must of_node_put() |
| 1172 | * on it when done. |
| 1173 | * - NULL if no phandle found at index |
| 1174 | */ |
| 1175 | static struct device_node *parse_prop_cells(struct device_node *np, |
| 1176 | const char *prop_name, int index, |
| 1177 | const char *list_name, |
| 1178 | const char *cells_name) |
| 1179 | { |
| 1180 | struct of_phandle_args sup_args; |
| 1181 | |
| 1182 | if (strcmp(prop_name, list_name)) |
| 1183 | return NULL; |
| 1184 | |
| 1185 | if (of_parse_phandle_with_args(np, list_name, cells_name, index, |
| 1186 | &sup_args)) |
| 1187 | return NULL; |
| 1188 | |
| 1189 | return sup_args.np; |
| 1190 | } |
| 1191 | |
Saravana Kannan | a436ef4 | 2019-11-04 22:49:59 -0800 | [diff] [blame] | 1192 | #define DEFINE_SIMPLE_PROP(fname, name, cells) \ |
| 1193 | static struct device_node *parse_##fname(struct device_node *np, \ |
| 1194 | const char *prop_name, int index) \ |
| 1195 | { \ |
| 1196 | return parse_prop_cells(np, prop_name, index, name, cells); \ |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | static int strcmp_suffix(const char *str, const char *suffix) |
| 1200 | { |
| 1201 | unsigned int len, suffix_len; |
| 1202 | |
| 1203 | len = strlen(str); |
| 1204 | suffix_len = strlen(suffix); |
| 1205 | if (len <= suffix_len) |
| 1206 | return -1; |
| 1207 | return strcmp(str + len - suffix_len, suffix); |
| 1208 | } |
| 1209 | |
Saravana Kannan | a436ef4 | 2019-11-04 22:49:59 -0800 | [diff] [blame] | 1210 | /** |
| 1211 | * parse_suffix_prop_cells - Suffix property parsing function for suppliers |
| 1212 | * |
| 1213 | * @np: Pointer to device tree node containing a list |
| 1214 | * @prop_name: Name of property to be parsed. Expected to hold phandle values |
| 1215 | * @index: For properties holding a list of phandles, this is the index |
| 1216 | * into the list. |
| 1217 | * @suffix: Property suffix that is known to contain list of phandle(s) to |
| 1218 | * supplier(s) |
| 1219 | * @cells_name: property name that specifies phandles' arguments count |
| 1220 | * |
| 1221 | * This is a helper function to parse properties that have a known fixed suffix |
| 1222 | * and are a list of phandles and phandle arguments. |
| 1223 | * |
| 1224 | * Returns: |
| 1225 | * - phandle node pointer with refcount incremented. Caller must of_node_put() |
| 1226 | * on it when done. |
| 1227 | * - NULL if no phandle found at index |
| 1228 | */ |
| 1229 | static struct device_node *parse_suffix_prop_cells(struct device_node *np, |
| 1230 | const char *prop_name, int index, |
| 1231 | const char *suffix, |
| 1232 | const char *cells_name) |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 1233 | { |
Saravana Kannan | a436ef4 | 2019-11-04 22:49:59 -0800 | [diff] [blame] | 1234 | struct of_phandle_args sup_args; |
| 1235 | |
| 1236 | if (strcmp_suffix(prop_name, suffix)) |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 1237 | return NULL; |
| 1238 | |
Saravana Kannan | a436ef4 | 2019-11-04 22:49:59 -0800 | [diff] [blame] | 1239 | if (of_parse_phandle_with_args(np, prop_name, cells_name, index, |
| 1240 | &sup_args)) |
| 1241 | return NULL; |
| 1242 | |
| 1243 | return sup_args.np; |
| 1244 | } |
| 1245 | |
| 1246 | #define DEFINE_SUFFIX_PROP(fname, suffix, cells) \ |
| 1247 | static struct device_node *parse_##fname(struct device_node *np, \ |
| 1248 | const char *prop_name, int index) \ |
| 1249 | { \ |
| 1250 | return parse_suffix_prop_cells(np, prop_name, index, suffix, cells); \ |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | /** |
| 1254 | * struct supplier_bindings - Property parsing functions for suppliers |
| 1255 | * |
| 1256 | * @parse_prop: function name |
| 1257 | * parse_prop() finds the node corresponding to a supplier phandle |
| 1258 | * @parse_prop.np: Pointer to device node holding supplier phandle property |
| 1259 | * @parse_prop.prop_name: Name of property holding a phandle value |
| 1260 | * @parse_prop.index: For properties holding a list of phandles, this is the |
| 1261 | * index into the list |
Lee Jones | 3915fed | 2021-03-18 10:40:31 +0000 | [diff] [blame] | 1262 | * @optional: Describes whether a supplier is mandatory or not |
Ulf Hansson | 3cd6bab | 2021-09-02 11:02:21 +0200 | [diff] [blame] | 1263 | * @node_not_dev: The consumer node containing the property is never converted |
| 1264 | * to a struct device. Instead, parse ancestor nodes for the |
| 1265 | * compatible property to find a node corresponding to a device. |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 1266 | * |
| 1267 | * Returns: |
| 1268 | * parse_prop() return values are |
| 1269 | * - phandle node pointer with refcount incremented. Caller must of_node_put() |
| 1270 | * on it when done. |
| 1271 | * - NULL if no phandle found at index |
| 1272 | */ |
| 1273 | struct supplier_bindings { |
| 1274 | struct device_node *(*parse_prop)(struct device_node *np, |
| 1275 | const char *prop_name, int index); |
Saravana Kannan | a9dd8f3 | 2021-02-05 14:26:40 -0800 | [diff] [blame] | 1276 | bool optional; |
Saravana Kannan | f7514a6 | 2021-03-30 11:50:55 -0700 | [diff] [blame] | 1277 | bool node_not_dev; |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 1278 | }; |
| 1279 | |
Saravana Kannan | a436ef4 | 2019-11-04 22:49:59 -0800 | [diff] [blame] | 1280 | DEFINE_SIMPLE_PROP(clocks, "clocks", "#clock-cells") |
| 1281 | DEFINE_SIMPLE_PROP(interconnects, "interconnects", "#interconnect-cells") |
Saravana Kannan | 8e12257 | 2019-11-04 22:50:00 -0800 | [diff] [blame] | 1282 | DEFINE_SIMPLE_PROP(iommus, "iommus", "#iommu-cells") |
| 1283 | DEFINE_SIMPLE_PROP(mboxes, "mboxes", "#mbox-cells") |
| 1284 | DEFINE_SIMPLE_PROP(io_channels, "io-channel", "#io-channel-cells") |
Saravana Kannan | 7f00be9 | 2019-11-19 23:13:01 -0800 | [diff] [blame] | 1285 | DEFINE_SIMPLE_PROP(interrupt_parent, "interrupt-parent", NULL) |
| 1286 | DEFINE_SIMPLE_PROP(dmas, "dmas", "#dma-cells") |
Saravana Kannan | 2f7afc3 | 2020-02-19 21:52:50 -0800 | [diff] [blame] | 1287 | DEFINE_SIMPLE_PROP(power_domains, "power-domains", "#power-domain-cells") |
| 1288 | DEFINE_SIMPLE_PROP(hwlocks, "hwlocks", "#hwlock-cells") |
Saravana Kannan | 78056e7 | 2020-04-01 15:52:03 -0700 | [diff] [blame] | 1289 | DEFINE_SIMPLE_PROP(extcon, "extcon", NULL) |
Saravana Kannan | 53e6a67 | 2020-07-24 16:44:14 -0700 | [diff] [blame] | 1290 | DEFINE_SIMPLE_PROP(nvmem_cells, "nvmem-cells", NULL) |
| 1291 | DEFINE_SIMPLE_PROP(phys, "phys", "#phy-cells") |
| 1292 | DEFINE_SIMPLE_PROP(wakeup_parent, "wakeup-parent", NULL) |
Saravana Kannan | fb820b4 | 2020-07-24 16:44:15 -0700 | [diff] [blame] | 1293 | DEFINE_SIMPLE_PROP(pinctrl0, "pinctrl-0", NULL) |
| 1294 | DEFINE_SIMPLE_PROP(pinctrl1, "pinctrl-1", NULL) |
| 1295 | DEFINE_SIMPLE_PROP(pinctrl2, "pinctrl-2", NULL) |
| 1296 | DEFINE_SIMPLE_PROP(pinctrl3, "pinctrl-3", NULL) |
| 1297 | DEFINE_SIMPLE_PROP(pinctrl4, "pinctrl-4", NULL) |
| 1298 | DEFINE_SIMPLE_PROP(pinctrl5, "pinctrl-5", NULL) |
| 1299 | DEFINE_SIMPLE_PROP(pinctrl6, "pinctrl-6", NULL) |
| 1300 | DEFINE_SIMPLE_PROP(pinctrl7, "pinctrl-7", NULL) |
| 1301 | DEFINE_SIMPLE_PROP(pinctrl8, "pinctrl-8", NULL) |
Saravana Kannan | f7514a6 | 2021-03-30 11:50:55 -0700 | [diff] [blame] | 1302 | DEFINE_SIMPLE_PROP(remote_endpoint, "remote-endpoint", NULL) |
Saravana Kannan | 6b2117a | 2021-08-05 15:37:29 -0700 | [diff] [blame] | 1303 | DEFINE_SIMPLE_PROP(pwms, "pwms", "#pwm-cells") |
| 1304 | DEFINE_SIMPLE_PROP(resets, "resets", "#reset-cells") |
Saravana Kannan | 18c585c | 2021-08-13 19:31:30 -0700 | [diff] [blame] | 1305 | DEFINE_SIMPLE_PROP(leds, "leds", NULL) |
| 1306 | DEFINE_SIMPLE_PROP(backlight, "backlight", NULL) |
Saravana Kannan | a436ef4 | 2019-11-04 22:49:59 -0800 | [diff] [blame] | 1307 | DEFINE_SUFFIX_PROP(regulators, "-supply", NULL) |
Saravana Kannan | 7f00be9 | 2019-11-19 23:13:01 -0800 | [diff] [blame] | 1308 | DEFINE_SUFFIX_PROP(gpio, "-gpio", "#gpio-cells") |
Ilya Lipnitskiy | d473d32 | 2021-04-05 15:25:40 -0700 | [diff] [blame] | 1309 | |
| 1310 | static struct device_node *parse_gpios(struct device_node *np, |
| 1311 | const char *prop_name, int index) |
| 1312 | { |
| 1313 | if (!strcmp_suffix(prop_name, ",nr-gpios")) |
| 1314 | return NULL; |
| 1315 | |
| 1316 | return parse_suffix_prop_cells(np, prop_name, index, "-gpios", |
| 1317 | "#gpio-cells"); |
| 1318 | } |
Saravana Kannan | a436ef4 | 2019-11-04 22:49:59 -0800 | [diff] [blame] | 1319 | |
Will Deacon | e149573 | 2019-11-20 19:00:28 +0000 | [diff] [blame] | 1320 | static struct device_node *parse_iommu_maps(struct device_node *np, |
| 1321 | const char *prop_name, int index) |
| 1322 | { |
| 1323 | if (strcmp(prop_name, "iommu-map")) |
| 1324 | return NULL; |
| 1325 | |
| 1326 | return of_parse_phandle(np, prop_name, (index * 4) + 1); |
| 1327 | } |
| 1328 | |
Saravana Kannan | e13f5b7 | 2021-01-21 14:57:11 -0800 | [diff] [blame] | 1329 | static struct device_node *parse_gpio_compat(struct device_node *np, |
| 1330 | const char *prop_name, int index) |
| 1331 | { |
| 1332 | struct of_phandle_args sup_args; |
| 1333 | |
| 1334 | if (strcmp(prop_name, "gpio") && strcmp(prop_name, "gpios")) |
| 1335 | return NULL; |
| 1336 | |
| 1337 | /* |
| 1338 | * Ignore node with gpio-hog property since its gpios are all provided |
| 1339 | * by its parent. |
| 1340 | */ |
| 1341 | if (of_find_property(np, "gpio-hog", NULL)) |
| 1342 | return NULL; |
| 1343 | |
| 1344 | if (of_parse_phandle_with_args(np, prop_name, "#gpio-cells", index, |
| 1345 | &sup_args)) |
| 1346 | return NULL; |
| 1347 | |
| 1348 | return sup_args.np; |
| 1349 | } |
| 1350 | |
Saravana Kannan | 4104ca7 | 2021-01-21 14:57:12 -0800 | [diff] [blame] | 1351 | static struct device_node *parse_interrupts(struct device_node *np, |
| 1352 | const char *prop_name, int index) |
| 1353 | { |
Saravana Kannan | f265f06 | 2021-02-08 17:04:38 -0800 | [diff] [blame] | 1354 | struct of_phandle_args sup_args; |
| 1355 | |
Saravana Kannan | bd6d617 | 2021-02-15 14:42:58 -0800 | [diff] [blame] | 1356 | if (!IS_ENABLED(CONFIG_OF_IRQ) || IS_ENABLED(CONFIG_PPC)) |
| 1357 | return NULL; |
| 1358 | |
Saravana Kannan | f265f06 | 2021-02-08 17:04:38 -0800 | [diff] [blame] | 1359 | if (strcmp(prop_name, "interrupts") && |
| 1360 | strcmp(prop_name, "interrupts-extended")) |
Saravana Kannan | 4104ca7 | 2021-01-21 14:57:12 -0800 | [diff] [blame] | 1361 | return NULL; |
| 1362 | |
Saravana Kannan | f265f06 | 2021-02-08 17:04:38 -0800 | [diff] [blame] | 1363 | return of_irq_parse_one(np, index, &sup_args) ? NULL : sup_args.np; |
Saravana Kannan | 4104ca7 | 2021-01-21 14:57:12 -0800 | [diff] [blame] | 1364 | } |
| 1365 | |
Saravana Kannan | af1b967 | 2019-10-11 12:15:19 -0700 | [diff] [blame] | 1366 | static const struct supplier_bindings of_supplier_bindings[] = { |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 1367 | { .parse_prop = parse_clocks, }, |
| 1368 | { .parse_prop = parse_interconnects, }, |
Saravana Kannan | a9dd8f3 | 2021-02-05 14:26:40 -0800 | [diff] [blame] | 1369 | { .parse_prop = parse_iommus, .optional = true, }, |
| 1370 | { .parse_prop = parse_iommu_maps, .optional = true, }, |
Saravana Kannan | 8e12257 | 2019-11-04 22:50:00 -0800 | [diff] [blame] | 1371 | { .parse_prop = parse_mboxes, }, |
| 1372 | { .parse_prop = parse_io_channels, }, |
Saravana Kannan | 7f00be9 | 2019-11-19 23:13:01 -0800 | [diff] [blame] | 1373 | { .parse_prop = parse_interrupt_parent, }, |
Saravana Kannan | a9dd8f3 | 2021-02-05 14:26:40 -0800 | [diff] [blame] | 1374 | { .parse_prop = parse_dmas, .optional = true, }, |
Saravana Kannan | 2f7afc3 | 2020-02-19 21:52:50 -0800 | [diff] [blame] | 1375 | { .parse_prop = parse_power_domains, }, |
| 1376 | { .parse_prop = parse_hwlocks, }, |
Saravana Kannan | 78056e7 | 2020-04-01 15:52:03 -0700 | [diff] [blame] | 1377 | { .parse_prop = parse_extcon, }, |
Saravana Kannan | 53e6a67 | 2020-07-24 16:44:14 -0700 | [diff] [blame] | 1378 | { .parse_prop = parse_nvmem_cells, }, |
| 1379 | { .parse_prop = parse_phys, }, |
| 1380 | { .parse_prop = parse_wakeup_parent, }, |
Saravana Kannan | fb820b4 | 2020-07-24 16:44:15 -0700 | [diff] [blame] | 1381 | { .parse_prop = parse_pinctrl0, }, |
| 1382 | { .parse_prop = parse_pinctrl1, }, |
| 1383 | { .parse_prop = parse_pinctrl2, }, |
| 1384 | { .parse_prop = parse_pinctrl3, }, |
| 1385 | { .parse_prop = parse_pinctrl4, }, |
| 1386 | { .parse_prop = parse_pinctrl5, }, |
| 1387 | { .parse_prop = parse_pinctrl6, }, |
| 1388 | { .parse_prop = parse_pinctrl7, }, |
| 1389 | { .parse_prop = parse_pinctrl8, }, |
Saravana Kannan | f7514a6 | 2021-03-30 11:50:55 -0700 | [diff] [blame] | 1390 | { .parse_prop = parse_remote_endpoint, .node_not_dev = true, }, |
Saravana Kannan | 6b2117a | 2021-08-05 15:37:29 -0700 | [diff] [blame] | 1391 | { .parse_prop = parse_pwms, }, |
| 1392 | { .parse_prop = parse_resets, }, |
Saravana Kannan | 18c585c | 2021-08-13 19:31:30 -0700 | [diff] [blame] | 1393 | { .parse_prop = parse_leds, }, |
| 1394 | { .parse_prop = parse_backlight, }, |
Saravana Kannan | e13f5b7 | 2021-01-21 14:57:11 -0800 | [diff] [blame] | 1395 | { .parse_prop = parse_gpio_compat, }, |
Saravana Kannan | 4104ca7 | 2021-01-21 14:57:12 -0800 | [diff] [blame] | 1396 | { .parse_prop = parse_interrupts, }, |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 1397 | { .parse_prop = parse_regulators, }, |
Saravana Kannan | 7f00be9 | 2019-11-19 23:13:01 -0800 | [diff] [blame] | 1398 | { .parse_prop = parse_gpio, }, |
| 1399 | { .parse_prop = parse_gpios, }, |
Saravana Kannan | af1b967 | 2019-10-11 12:15:19 -0700 | [diff] [blame] | 1400 | {} |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 1401 | }; |
| 1402 | |
| 1403 | /** |
| 1404 | * of_link_property - Create device links to suppliers listed in a property |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 1405 | * @con_np: The consumer device tree node which contains the property |
| 1406 | * @prop_name: Name of property to be parsed |
| 1407 | * |
| 1408 | * This function checks if the property @prop_name that is present in the |
| 1409 | * @con_np device tree node is one of the known common device tree bindings |
| 1410 | * that list phandles to suppliers. If @prop_name isn't one, this function |
| 1411 | * doesn't do anything. |
| 1412 | * |
Saravana Kannan | 8a06d1e | 2020-11-20 18:02:29 -0800 | [diff] [blame] | 1413 | * If @prop_name is one, this function attempts to create fwnode links from the |
| 1414 | * consumer device tree node @con_np to all the suppliers device tree nodes |
| 1415 | * listed in @prop_name. |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 1416 | * |
Saravana Kannan | 8a06d1e | 2020-11-20 18:02:29 -0800 | [diff] [blame] | 1417 | * Any failed attempt to create a fwnode link will NOT result in an immediate |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 1418 | * return. of_link_property() must create links to all the available supplier |
Saravana Kannan | 8a06d1e | 2020-11-20 18:02:29 -0800 | [diff] [blame] | 1419 | * device tree nodes even when attempts to create a link to one or more |
| 1420 | * suppliers fail. |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 1421 | */ |
Saravana Kannan | 8a06d1e | 2020-11-20 18:02:29 -0800 | [diff] [blame] | 1422 | static int of_link_property(struct device_node *con_np, const char *prop_name) |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 1423 | { |
| 1424 | struct device_node *phandle; |
Saravana Kannan | af1b967 | 2019-10-11 12:15:19 -0700 | [diff] [blame] | 1425 | const struct supplier_bindings *s = of_supplier_bindings; |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 1426 | unsigned int i = 0; |
| 1427 | bool matched = false; |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 1428 | |
| 1429 | /* Do not stop at first failed link, link all available suppliers. */ |
| 1430 | while (!matched && s->parse_prop) { |
Saravana Kannan | a9dd8f3 | 2021-02-05 14:26:40 -0800 | [diff] [blame] | 1431 | if (s->optional && !fw_devlink_is_strict()) { |
| 1432 | s++; |
| 1433 | continue; |
| 1434 | } |
| 1435 | |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 1436 | while ((phandle = s->parse_prop(con_np, prop_name, i))) { |
Saravana Kannan | f7514a6 | 2021-03-30 11:50:55 -0700 | [diff] [blame] | 1437 | struct device_node *con_dev_np; |
| 1438 | |
| 1439 | con_dev_np = s->node_not_dev |
Ulf Hansson | 3cd6bab | 2021-09-02 11:02:21 +0200 | [diff] [blame] | 1440 | ? of_get_compat_node_parent(con_np) |
Saravana Kannan | f7514a6 | 2021-03-30 11:50:55 -0700 | [diff] [blame] | 1441 | : of_node_get(con_np); |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 1442 | matched = true; |
| 1443 | i++; |
Saravana Kannan | f7514a6 | 2021-03-30 11:50:55 -0700 | [diff] [blame] | 1444 | of_link_to_phandle(con_dev_np, phandle); |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 1445 | of_node_put(phandle); |
Saravana Kannan | f7514a6 | 2021-03-30 11:50:55 -0700 | [diff] [blame] | 1446 | of_node_put(con_dev_np); |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 1447 | } |
| 1448 | s++; |
| 1449 | } |
Yang Li | 065cac6 | 2021-03-02 17:16:38 +0800 | [diff] [blame] | 1450 | return 0; |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 1451 | } |
| 1452 | |
Saravana Kannan | 2d09e6e | 2020-11-20 18:02:32 -0800 | [diff] [blame] | 1453 | static int of_fwnode_add_links(struct fwnode_handle *fwnode) |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 1454 | { |
Saravana Kannan | 8a06d1e | 2020-11-20 18:02:29 -0800 | [diff] [blame] | 1455 | struct property *p; |
| 1456 | struct device_node *con_np = to_of_node(fwnode); |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 1457 | |
Saravana Kannan | 4a48b66 | 2021-09-09 18:14:45 -0700 | [diff] [blame] | 1458 | if (IS_ENABLED(CONFIG_X86)) |
| 1459 | return 0; |
| 1460 | |
Saravana Kannan | 8a06d1e | 2020-11-20 18:02:29 -0800 | [diff] [blame] | 1461 | if (!con_np) |
| 1462 | return -EINVAL; |
| 1463 | |
| 1464 | for_each_property_of_node(con_np, p) |
| 1465 | of_link_property(con_np, p->name); |
| 1466 | |
| 1467 | return 0; |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 1468 | } |
| 1469 | |
Sakari Ailus | 3708184 | 2017-06-06 12:37:37 +0300 | [diff] [blame] | 1470 | const struct fwnode_operations of_fwnode_ops = { |
| 1471 | .get = of_fwnode_get, |
| 1472 | .put = of_fwnode_put, |
Sakari Ailus | 2294b3a | 2017-06-06 12:37:39 +0300 | [diff] [blame] | 1473 | .device_is_available = of_fwnode_device_is_available, |
Sinan Kaya | 1c2c82e | 2017-12-13 02:20:50 -0500 | [diff] [blame] | 1474 | .device_get_match_data = of_fwnode_device_get_match_data, |
Sakari Ailus | 3708184 | 2017-06-06 12:37:37 +0300 | [diff] [blame] | 1475 | .property_present = of_fwnode_property_present, |
| 1476 | .property_read_int_array = of_fwnode_property_read_int_array, |
| 1477 | .property_read_string_array = of_fwnode_property_read_string_array, |
Sakari Ailus | bc0500c | 2019-10-03 15:32:12 +0300 | [diff] [blame] | 1478 | .get_name = of_fwnode_get_name, |
Sakari Ailus | e7e242b | 2019-10-03 15:32:13 +0300 | [diff] [blame] | 1479 | .get_name_prefix = of_fwnode_get_name_prefix, |
Sakari Ailus | 3708184 | 2017-06-06 12:37:37 +0300 | [diff] [blame] | 1480 | .get_parent = of_fwnode_get_parent, |
| 1481 | .get_next_child_node = of_fwnode_get_next_child_node, |
| 1482 | .get_named_child_node = of_fwnode_get_named_child_node, |
Sakari Ailus | 3e3119d | 2017-07-21 15:11:49 +0300 | [diff] [blame] | 1483 | .get_reference_args = of_fwnode_get_reference_args, |
Sakari Ailus | 3b27d00 | 2017-06-06 12:37:38 +0300 | [diff] [blame] | 1484 | .graph_get_next_endpoint = of_fwnode_graph_get_next_endpoint, |
| 1485 | .graph_get_remote_endpoint = of_fwnode_graph_get_remote_endpoint, |
| 1486 | .graph_get_port_parent = of_fwnode_graph_get_port_parent, |
| 1487 | .graph_parse_endpoint = of_fwnode_graph_parse_endpoint, |
Saravana Kannan | a3e1d1a | 2019-09-04 14:11:22 -0700 | [diff] [blame] | 1488 | .add_links = of_fwnode_add_links, |
Sakari Ailus | 3708184 | 2017-06-06 12:37:37 +0300 | [diff] [blame] | 1489 | }; |
Sakari Ailus | db3e50f | 2017-07-21 14:39:31 +0300 | [diff] [blame] | 1490 | EXPORT_SYMBOL_GPL(of_fwnode_ops); |