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