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