property.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. /*
  2. * drivers/of/property.c - Procedures for accessing and interpreting
  3. * Devicetree properties and graphs.
  4. *
  5. * Initially created by copying procedures from drivers/of/base.c. This
  6. * file contains the OF property as well as the OF graph interface
  7. * functions.
  8. *
  9. * Paul Mackerras August 1996.
  10. * Copyright (C) 1996-2005 Paul Mackerras.
  11. *
  12. * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
  13. * {engebret|bergner}@us.ibm.com
  14. *
  15. * Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net
  16. *
  17. * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
  18. * Grant Likely.
  19. *
  20. * This program is free software; you can redistribute it and/or
  21. * modify it under the terms of the GNU General Public License
  22. * as published by the Free Software Foundation; either version
  23. * 2 of the License, or (at your option) any later version.
  24. */
  25. #define pr_fmt(fmt) "OF: " fmt
  26. #include <linux/of.h>
  27. #include <linux/of_device.h>
  28. #include <linux/of_graph.h>
  29. #include <linux/string.h>
  30. #include "of_private.h"
  31. /**
  32. * of_property_count_elems_of_size - Count the number of elements in a property
  33. *
  34. * @np: device node from which the property value is to be read.
  35. * @propname: name of the property to be searched.
  36. * @elem_size: size of the individual element
  37. *
  38. * Search for a property in a device node and count the number of elements of
  39. * size elem_size in it. Returns number of elements on sucess, -EINVAL if the
  40. * property does not exist or its length does not match a multiple of elem_size
  41. * and -ENODATA if the property does not have a value.
  42. */
  43. int of_property_count_elems_of_size(const struct device_node *np,
  44. const char *propname, int elem_size)
  45. {
  46. struct property *prop = of_find_property(np, propname, NULL);
  47. if (!prop)
  48. return -EINVAL;
  49. if (!prop->value)
  50. return -ENODATA;
  51. if (prop->length % elem_size != 0) {
  52. pr_err("size of %s in node %pOF is not a multiple of %d\n",
  53. propname, np, elem_size);
  54. return -EINVAL;
  55. }
  56. return prop->length / elem_size;
  57. }
  58. EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
  59. /**
  60. * of_find_property_value_of_size
  61. *
  62. * @np: device node from which the property value is to be read.
  63. * @propname: name of the property to be searched.
  64. * @min: minimum allowed length of property value
  65. * @max: maximum allowed length of property value (0 means unlimited)
  66. * @len: if !=NULL, actual length is written to here
  67. *
  68. * Search for a property in a device node and valid the requested size.
  69. * Returns the property value on success, -EINVAL if the property does not
  70. * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
  71. * property data is too small or too large.
  72. *
  73. */
  74. static void *of_find_property_value_of_size(const struct device_node *np,
  75. const char *propname, u32 min, u32 max, size_t *len)
  76. {
  77. struct property *prop = of_find_property(np, propname, NULL);
  78. if (!prop)
  79. return ERR_PTR(-EINVAL);
  80. if (!prop->value)
  81. return ERR_PTR(-ENODATA);
  82. if (prop->length < min)
  83. return ERR_PTR(-EOVERFLOW);
  84. if (max && prop->length > max)
  85. return ERR_PTR(-EOVERFLOW);
  86. if (len)
  87. *len = prop->length;
  88. return prop->value;
  89. }
  90. /**
  91. * of_property_read_u32_index - Find and read a u32 from a multi-value property.
  92. *
  93. * @np: device node from which the property value is to be read.
  94. * @propname: name of the property to be searched.
  95. * @index: index of the u32 in the list of values
  96. * @out_value: pointer to return value, modified only if no error.
  97. *
  98. * Search for a property in a device node and read nth 32-bit value from
  99. * it. Returns 0 on success, -EINVAL if the property does not exist,
  100. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  101. * property data isn't large enough.
  102. *
  103. * The out_value is modified only if a valid u32 value can be decoded.
  104. */
  105. int of_property_read_u32_index(const struct device_node *np,
  106. const char *propname,
  107. u32 index, u32 *out_value)
  108. {
  109. const u32 *val = of_find_property_value_of_size(np, propname,
  110. ((index + 1) * sizeof(*out_value)),
  111. 0,
  112. NULL);
  113. if (IS_ERR(val))
  114. return PTR_ERR(val);
  115. *out_value = be32_to_cpup(((__be32 *)val) + index);
  116. return 0;
  117. }
  118. EXPORT_SYMBOL_GPL(of_property_read_u32_index);
  119. /**
  120. * of_property_read_u64_index - Find and read a u64 from a multi-value property.
  121. *
  122. * @np: device node from which the property value is to be read.
  123. * @propname: name of the property to be searched.
  124. * @index: index of the u64 in the list of values
  125. * @out_value: pointer to return value, modified only if no error.
  126. *
  127. * Search for a property in a device node and read nth 64-bit value from
  128. * it. Returns 0 on success, -EINVAL if the property does not exist,
  129. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  130. * property data isn't large enough.
  131. *
  132. * The out_value is modified only if a valid u64 value can be decoded.
  133. */
  134. int of_property_read_u64_index(const struct device_node *np,
  135. const char *propname,
  136. u32 index, u64 *out_value)
  137. {
  138. const u64 *val = of_find_property_value_of_size(np, propname,
  139. ((index + 1) * sizeof(*out_value)),
  140. 0, NULL);
  141. if (IS_ERR(val))
  142. return PTR_ERR(val);
  143. *out_value = be64_to_cpup(((__be64 *)val) + index);
  144. return 0;
  145. }
  146. EXPORT_SYMBOL_GPL(of_property_read_u64_index);
  147. /**
  148. * of_property_read_variable_u8_array - Find and read an array of u8 from a
  149. * property, with bounds on the minimum and maximum array size.
  150. *
  151. * @np: device node from which the property value is to be read.
  152. * @propname: name of the property to be searched.
  153. * @out_values: pointer to return value, modified only if return value is 0.
  154. * @sz_min: minimum number of array elements to read
  155. * @sz_max: maximum number of array elements to read, if zero there is no
  156. * upper limit on the number of elements in the dts entry but only
  157. * sz_min will be read.
  158. *
  159. * Search for a property in a device node and read 8-bit value(s) from
  160. * it. Returns number of elements read on success, -EINVAL if the property
  161. * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
  162. * if the property data is smaller than sz_min or longer than sz_max.
  163. *
  164. * dts entry of array should be like:
  165. * property = /bits/ 8 <0x50 0x60 0x70>;
  166. *
  167. * The out_values is modified only if a valid u8 value can be decoded.
  168. */
  169. int of_property_read_variable_u8_array(const struct device_node *np,
  170. const char *propname, u8 *out_values,
  171. size_t sz_min, size_t sz_max)
  172. {
  173. size_t sz, count;
  174. const u8 *val = of_find_property_value_of_size(np, propname,
  175. (sz_min * sizeof(*out_values)),
  176. (sz_max * sizeof(*out_values)),
  177. &sz);
  178. if (IS_ERR(val))
  179. return PTR_ERR(val);
  180. if (!sz_max)
  181. sz = sz_min;
  182. else
  183. sz /= sizeof(*out_values);
  184. count = sz;
  185. while (count--)
  186. *out_values++ = *val++;
  187. return sz;
  188. }
  189. EXPORT_SYMBOL_GPL(of_property_read_variable_u8_array);
  190. /**
  191. * of_property_read_variable_u16_array - Find and read an array of u16 from a
  192. * property, with bounds on the minimum and maximum array size.
  193. *
  194. * @np: device node from which the property value is to be read.
  195. * @propname: name of the property to be searched.
  196. * @out_values: pointer to return value, modified only if return value is 0.
  197. * @sz_min: minimum number of array elements to read
  198. * @sz_max: maximum number of array elements to read, if zero there is no
  199. * upper limit on the number of elements in the dts entry but only
  200. * sz_min will be read.
  201. *
  202. * Search for a property in a device node and read 16-bit value(s) from
  203. * it. Returns number of elements read on success, -EINVAL if the property
  204. * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
  205. * if the property data is smaller than sz_min or longer than sz_max.
  206. *
  207. * dts entry of array should be like:
  208. * property = /bits/ 16 <0x5000 0x6000 0x7000>;
  209. *
  210. * The out_values is modified only if a valid u16 value can be decoded.
  211. */
  212. int of_property_read_variable_u16_array(const struct device_node *np,
  213. const char *propname, u16 *out_values,
  214. size_t sz_min, size_t sz_max)
  215. {
  216. size_t sz, count;
  217. const __be16 *val = of_find_property_value_of_size(np, propname,
  218. (sz_min * sizeof(*out_values)),
  219. (sz_max * sizeof(*out_values)),
  220. &sz);
  221. if (IS_ERR(val))
  222. return PTR_ERR(val);
  223. if (!sz_max)
  224. sz = sz_min;
  225. else
  226. sz /= sizeof(*out_values);
  227. count = sz;
  228. while (count--)
  229. *out_values++ = be16_to_cpup(val++);
  230. return sz;
  231. }
  232. EXPORT_SYMBOL_GPL(of_property_read_variable_u16_array);
  233. /**
  234. * of_property_read_variable_u32_array - Find and read an array of 32 bit
  235. * integers from a property, with bounds on the minimum and maximum array size.
  236. *
  237. * @np: device node from which the property value is to be read.
  238. * @propname: name of the property to be searched.
  239. * @out_values: pointer to return value, modified only if return value is 0.
  240. * @sz_min: minimum number of array elements to read
  241. * @sz_max: maximum number of array elements to read, if zero there is no
  242. * upper limit on the number of elements in the dts entry but only
  243. * sz_min will be read.
  244. *
  245. * Search for a property in a device node and read 32-bit value(s) from
  246. * it. Returns number of elements read on success, -EINVAL if the property
  247. * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
  248. * if the property data is smaller than sz_min or longer than sz_max.
  249. *
  250. * The out_values is modified only if a valid u32 value can be decoded.
  251. */
  252. int of_property_read_variable_u32_array(const struct device_node *np,
  253. const char *propname, u32 *out_values,
  254. size_t sz_min, size_t sz_max)
  255. {
  256. size_t sz, count;
  257. const __be32 *val = of_find_property_value_of_size(np, propname,
  258. (sz_min * sizeof(*out_values)),
  259. (sz_max * sizeof(*out_values)),
  260. &sz);
  261. if (IS_ERR(val))
  262. return PTR_ERR(val);
  263. if (!sz_max)
  264. sz = sz_min;
  265. else
  266. sz /= sizeof(*out_values);
  267. count = sz;
  268. while (count--)
  269. *out_values++ = be32_to_cpup(val++);
  270. return sz;
  271. }
  272. EXPORT_SYMBOL_GPL(of_property_read_variable_u32_array);
  273. /**
  274. * of_property_read_u64 - Find and read a 64 bit integer from a property
  275. * @np: device node from which the property value is to be read.
  276. * @propname: name of the property to be searched.
  277. * @out_value: pointer to return value, modified only if return value is 0.
  278. *
  279. * Search for a property in a device node and read a 64-bit value from
  280. * it. Returns 0 on success, -EINVAL if the property does not exist,
  281. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  282. * property data isn't large enough.
  283. *
  284. * The out_value is modified only if a valid u64 value can be decoded.
  285. */
  286. int of_property_read_u64(const struct device_node *np, const char *propname,
  287. u64 *out_value)
  288. {
  289. const __be32 *val = of_find_property_value_of_size(np, propname,
  290. sizeof(*out_value),
  291. 0,
  292. NULL);
  293. if (IS_ERR(val))
  294. return PTR_ERR(val);
  295. *out_value = of_read_number(val, 2);
  296. return 0;
  297. }
  298. EXPORT_SYMBOL_GPL(of_property_read_u64);
  299. /**
  300. * of_property_read_variable_u64_array - Find and read an array of 64 bit
  301. * integers from a property, with bounds on the minimum and maximum array size.
  302. *
  303. * @np: device node from which the property value is to be read.
  304. * @propname: name of the property to be searched.
  305. * @out_values: pointer to return value, modified only if return value is 0.
  306. * @sz_min: minimum number of array elements to read
  307. * @sz_max: maximum number of array elements to read, if zero there is no
  308. * upper limit on the number of elements in the dts entry but only
  309. * sz_min will be read.
  310. *
  311. * Search for a property in a device node and read 64-bit value(s) from
  312. * it. Returns number of elements read on success, -EINVAL if the property
  313. * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
  314. * if the property data is smaller than sz_min or longer than sz_max.
  315. *
  316. * The out_values is modified only if a valid u64 value can be decoded.
  317. */
  318. int of_property_read_variable_u64_array(const struct device_node *np,
  319. const char *propname, u64 *out_values,
  320. size_t sz_min, size_t sz_max)
  321. {
  322. size_t sz, count;
  323. const __be32 *val = of_find_property_value_of_size(np, propname,
  324. (sz_min * sizeof(*out_values)),
  325. (sz_max * sizeof(*out_values)),
  326. &sz);
  327. if (IS_ERR(val))
  328. return PTR_ERR(val);
  329. if (!sz_max)
  330. sz = sz_min;
  331. else
  332. sz /= sizeof(*out_values);
  333. count = sz;
  334. while (count--) {
  335. *out_values++ = of_read_number(val, 2);
  336. val += 2;
  337. }
  338. return sz;
  339. }
  340. EXPORT_SYMBOL_GPL(of_property_read_variable_u64_array);
  341. /**
  342. * of_property_read_string - Find and read a string from a property
  343. * @np: device node from which the property value is to be read.
  344. * @propname: name of the property to be searched.
  345. * @out_string: pointer to null terminated return string, modified only if
  346. * return value is 0.
  347. *
  348. * Search for a property in a device tree node and retrieve a null
  349. * terminated string value (pointer to data, not a copy). Returns 0 on
  350. * success, -EINVAL if the property does not exist, -ENODATA if property
  351. * does not have a value, and -EILSEQ if the string is not null-terminated
  352. * within the length of the property data.
  353. *
  354. * The out_string pointer is modified only if a valid string can be decoded.
  355. */
  356. int of_property_read_string(const struct device_node *np, const char *propname,
  357. const char **out_string)
  358. {
  359. const struct property *prop = of_find_property(np, propname, NULL);
  360. if (!prop)
  361. return -EINVAL;
  362. if (!prop->value)
  363. return -ENODATA;
  364. if (strnlen(prop->value, prop->length) >= prop->length)
  365. return -EILSEQ;
  366. *out_string = prop->value;
  367. return 0;
  368. }
  369. EXPORT_SYMBOL_GPL(of_property_read_string);
  370. /**
  371. * of_property_match_string() - Find string in a list and return index
  372. * @np: pointer to node containing string list property
  373. * @propname: string list property name
  374. * @string: pointer to string to search for in string list
  375. *
  376. * This function searches a string list property and returns the index
  377. * of a specific string value.
  378. */
  379. int of_property_match_string(const struct device_node *np, const char *propname,
  380. const char *string)
  381. {
  382. const struct property *prop = of_find_property(np, propname, NULL);
  383. size_t l;
  384. int i;
  385. const char *p, *end;
  386. if (!prop)
  387. return -EINVAL;
  388. if (!prop->value)
  389. return -ENODATA;
  390. p = prop->value;
  391. end = p + prop->length;
  392. for (i = 0; p < end; i++, p += l) {
  393. l = strnlen(p, end - p) + 1;
  394. if (p + l > end)
  395. return -EILSEQ;
  396. pr_debug("comparing %s with %s\n", string, p);
  397. if (strcmp(string, p) == 0)
  398. return i; /* Found it; return index */
  399. }
  400. return -ENODATA;
  401. }
  402. EXPORT_SYMBOL_GPL(of_property_match_string);
  403. /**
  404. * of_property_read_string_helper() - Utility helper for parsing string properties
  405. * @np: device node from which the property value is to be read.
  406. * @propname: name of the property to be searched.
  407. * @out_strs: output array of string pointers.
  408. * @sz: number of array elements to read.
  409. * @skip: Number of strings to skip over at beginning of list.
  410. *
  411. * Don't call this function directly. It is a utility helper for the
  412. * of_property_read_string*() family of functions.
  413. */
  414. int of_property_read_string_helper(const struct device_node *np,
  415. const char *propname, const char **out_strs,
  416. size_t sz, int skip)
  417. {
  418. const struct property *prop = of_find_property(np, propname, NULL);
  419. int l = 0, i = 0;
  420. const char *p, *end;
  421. if (!prop)
  422. return -EINVAL;
  423. if (!prop->value)
  424. return -ENODATA;
  425. p = prop->value;
  426. end = p + prop->length;
  427. for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
  428. l = strnlen(p, end - p) + 1;
  429. if (p + l > end)
  430. return -EILSEQ;
  431. if (out_strs && i >= skip)
  432. *out_strs++ = p;
  433. }
  434. i -= skip;
  435. return i <= 0 ? -ENODATA : i;
  436. }
  437. EXPORT_SYMBOL_GPL(of_property_read_string_helper);
  438. const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
  439. u32 *pu)
  440. {
  441. const void *curv = cur;
  442. if (!prop)
  443. return NULL;
  444. if (!cur) {
  445. curv = prop->value;
  446. goto out_val;
  447. }
  448. curv += sizeof(*cur);
  449. if (curv >= prop->value + prop->length)
  450. return NULL;
  451. out_val:
  452. *pu = be32_to_cpup(curv);
  453. return curv;
  454. }
  455. EXPORT_SYMBOL_GPL(of_prop_next_u32);
  456. const char *of_prop_next_string(struct property *prop, const char *cur)
  457. {
  458. const void *curv = cur;
  459. if (!prop)
  460. return NULL;
  461. if (!cur)
  462. return prop->value;
  463. curv += strlen(cur) + 1;
  464. if (curv >= prop->value + prop->length)
  465. return NULL;
  466. return curv;
  467. }
  468. EXPORT_SYMBOL_GPL(of_prop_next_string);
  469. /**
  470. * of_graph_parse_endpoint() - parse common endpoint node properties
  471. * @node: pointer to endpoint device_node
  472. * @endpoint: pointer to the OF endpoint data structure
  473. *
  474. * The caller should hold a reference to @node.
  475. */
  476. int of_graph_parse_endpoint(const struct device_node *node,
  477. struct of_endpoint *endpoint)
  478. {
  479. struct device_node *port_node = of_get_parent(node);
  480. WARN_ONCE(!port_node, "%s(): endpoint %pOF has no parent node\n",
  481. __func__, node);
  482. memset(endpoint, 0, sizeof(*endpoint));
  483. endpoint->local_node = node;
  484. /*
  485. * It doesn't matter whether the two calls below succeed.
  486. * If they don't then the default value 0 is used.
  487. */
  488. of_property_read_u32(port_node, "reg", &endpoint->port);
  489. of_property_read_u32(node, "reg", &endpoint->id);
  490. of_node_put(port_node);
  491. return 0;
  492. }
  493. EXPORT_SYMBOL(of_graph_parse_endpoint);
  494. /**
  495. * of_graph_get_port_by_id() - get the port matching a given id
  496. * @parent: pointer to the parent device node
  497. * @id: id of the port
  498. *
  499. * Return: A 'port' node pointer with refcount incremented. The caller
  500. * has to use of_node_put() on it when done.
  501. */
  502. struct device_node *of_graph_get_port_by_id(struct device_node *parent, u32 id)
  503. {
  504. struct device_node *node, *port;
  505. node = of_get_child_by_name(parent, "ports");
  506. if (node)
  507. parent = node;
  508. for_each_child_of_node(parent, port) {
  509. u32 port_id = 0;
  510. if (of_node_cmp(port->name, "port") != 0)
  511. continue;
  512. of_property_read_u32(port, "reg", &port_id);
  513. if (id == port_id)
  514. break;
  515. }
  516. of_node_put(node);
  517. return port;
  518. }
  519. EXPORT_SYMBOL(of_graph_get_port_by_id);
  520. /**
  521. * of_graph_get_next_endpoint() - get next endpoint node
  522. * @parent: pointer to the parent device node
  523. * @prev: previous endpoint node, or NULL to get first
  524. *
  525. * Return: An 'endpoint' node pointer with refcount incremented. Refcount
  526. * of the passed @prev node is decremented.
  527. */
  528. struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
  529. struct device_node *prev)
  530. {
  531. struct device_node *endpoint;
  532. struct device_node *port;
  533. if (!parent)
  534. return NULL;
  535. /*
  536. * Start by locating the port node. If no previous endpoint is specified
  537. * search for the first port node, otherwise get the previous endpoint
  538. * parent port node.
  539. */
  540. if (!prev) {
  541. struct device_node *node;
  542. node = of_get_child_by_name(parent, "ports");
  543. if (node)
  544. parent = node;
  545. port = of_get_child_by_name(parent, "port");
  546. of_node_put(node);
  547. if (!port) {
  548. pr_err("graph: no port node found in %pOF\n", parent);
  549. return NULL;
  550. }
  551. } else {
  552. port = of_get_parent(prev);
  553. if (WARN_ONCE(!port, "%s(): endpoint %pOF has no parent node\n",
  554. __func__, prev))
  555. return NULL;
  556. }
  557. while (1) {
  558. /*
  559. * Now that we have a port node, get the next endpoint by
  560. * getting the next child. If the previous endpoint is NULL this
  561. * will return the first child.
  562. */
  563. endpoint = of_get_next_child(port, prev);
  564. if (endpoint) {
  565. of_node_put(port);
  566. return endpoint;
  567. }
  568. /* No more endpoints under this port, try the next one. */
  569. prev = NULL;
  570. do {
  571. port = of_get_next_child(parent, port);
  572. if (!port)
  573. return NULL;
  574. } while (of_node_cmp(port->name, "port"));
  575. }
  576. }
  577. EXPORT_SYMBOL(of_graph_get_next_endpoint);
  578. /**
  579. * of_graph_get_endpoint_by_regs() - get endpoint node of specific identifiers
  580. * @parent: pointer to the parent device node
  581. * @port_reg: identifier (value of reg property) of the parent port node
  582. * @reg: identifier (value of reg property) of the endpoint node
  583. *
  584. * Return: An 'endpoint' node pointer which is identified by reg and at the same
  585. * is the child of a port node identified by port_reg. reg and port_reg are
  586. * ignored when they are -1.
  587. */
  588. struct device_node *of_graph_get_endpoint_by_regs(
  589. const struct device_node *parent, int port_reg, int reg)
  590. {
  591. struct of_endpoint endpoint;
  592. struct device_node *node = NULL;
  593. for_each_endpoint_of_node(parent, node) {
  594. of_graph_parse_endpoint(node, &endpoint);
  595. if (((port_reg == -1) || (endpoint.port == port_reg)) &&
  596. ((reg == -1) || (endpoint.id == reg)))
  597. return node;
  598. }
  599. return NULL;
  600. }
  601. EXPORT_SYMBOL(of_graph_get_endpoint_by_regs);
  602. /**
  603. * of_graph_get_remote_endpoint() - get remote endpoint node
  604. * @node: pointer to a local endpoint device_node
  605. *
  606. * Return: Remote endpoint node associated with remote endpoint node linked
  607. * to @node. Use of_node_put() on it when done.
  608. */
  609. struct device_node *of_graph_get_remote_endpoint(const struct device_node *node)
  610. {
  611. /* Get remote endpoint node. */
  612. return of_parse_phandle(node, "remote-endpoint", 0);
  613. }
  614. EXPORT_SYMBOL(of_graph_get_remote_endpoint);
  615. /**
  616. * of_graph_get_port_parent() - get port's parent node
  617. * @node: pointer to a local endpoint device_node
  618. *
  619. * Return: device node associated with endpoint node linked
  620. * to @node. Use of_node_put() on it when done.
  621. */
  622. struct device_node *of_graph_get_port_parent(struct device_node *node)
  623. {
  624. unsigned int depth;
  625. if (!node)
  626. return NULL;
  627. /*
  628. * Preserve usecount for passed in node as of_get_next_parent()
  629. * will do of_node_put() on it.
  630. */
  631. of_node_get(node);
  632. /* Walk 3 levels up only if there is 'ports' node. */
  633. for (depth = 3; depth && node; depth--) {
  634. node = of_get_next_parent(node);
  635. if (depth == 2 && of_node_cmp(node->name, "ports"))
  636. break;
  637. }
  638. return node;
  639. }
  640. EXPORT_SYMBOL(of_graph_get_port_parent);
  641. /**
  642. * of_graph_get_remote_port_parent() - get remote port's parent node
  643. * @node: pointer to a local endpoint device_node
  644. *
  645. * Return: Remote device node associated with remote endpoint node linked
  646. * to @node. Use of_node_put() on it when done.
  647. */
  648. struct device_node *of_graph_get_remote_port_parent(
  649. const struct device_node *node)
  650. {
  651. struct device_node *np, *pp;
  652. /* Get remote endpoint node. */
  653. np = of_graph_get_remote_endpoint(node);
  654. pp = of_graph_get_port_parent(np);
  655. of_node_put(np);
  656. return pp;
  657. }
  658. EXPORT_SYMBOL(of_graph_get_remote_port_parent);
  659. /**
  660. * of_graph_get_remote_port() - get remote port node
  661. * @node: pointer to a local endpoint device_node
  662. *
  663. * Return: Remote port node associated with remote endpoint node linked
  664. * to @node. Use of_node_put() on it when done.
  665. */
  666. struct device_node *of_graph_get_remote_port(const struct device_node *node)
  667. {
  668. struct device_node *np;
  669. /* Get remote endpoint node. */
  670. np = of_graph_get_remote_endpoint(node);
  671. if (!np)
  672. return NULL;
  673. return of_get_next_parent(np);
  674. }
  675. EXPORT_SYMBOL(of_graph_get_remote_port);
  676. int of_graph_get_endpoint_count(const struct device_node *np)
  677. {
  678. struct device_node *endpoint;
  679. int num = 0;
  680. for_each_endpoint_of_node(np, endpoint)
  681. num++;
  682. return num;
  683. }
  684. EXPORT_SYMBOL(of_graph_get_endpoint_count);
  685. /**
  686. * of_graph_get_remote_node() - get remote parent device_node for given port/endpoint
  687. * @node: pointer to parent device_node containing graph port/endpoint
  688. * @port: identifier (value of reg property) of the parent port node
  689. * @endpoint: identifier (value of reg property) of the endpoint node
  690. *
  691. * Return: Remote device node associated with remote endpoint node linked
  692. * to @node. Use of_node_put() on it when done.
  693. */
  694. struct device_node *of_graph_get_remote_node(const struct device_node *node,
  695. u32 port, u32 endpoint)
  696. {
  697. struct device_node *endpoint_node, *remote;
  698. endpoint_node = of_graph_get_endpoint_by_regs(node, port, endpoint);
  699. if (!endpoint_node) {
  700. pr_debug("no valid endpoint (%d, %d) for node %pOF\n",
  701. port, endpoint, node);
  702. return NULL;
  703. }
  704. remote = of_graph_get_remote_port_parent(endpoint_node);
  705. of_node_put(endpoint_node);
  706. if (!remote) {
  707. pr_debug("no valid remote node\n");
  708. return NULL;
  709. }
  710. if (!of_device_is_available(remote)) {
  711. pr_debug("not available for remote node\n");
  712. of_node_put(remote);
  713. return NULL;
  714. }
  715. return remote;
  716. }
  717. EXPORT_SYMBOL(of_graph_get_remote_node);
  718. static void of_fwnode_get(struct fwnode_handle *fwnode)
  719. {
  720. of_node_get(to_of_node(fwnode));
  721. }
  722. static void of_fwnode_put(struct fwnode_handle *fwnode)
  723. {
  724. of_node_put(to_of_node(fwnode));
  725. }
  726. static bool of_fwnode_device_is_available(const struct fwnode_handle *fwnode)
  727. {
  728. return of_device_is_available(to_of_node(fwnode));
  729. }
  730. static bool of_fwnode_property_present(const struct fwnode_handle *fwnode,
  731. const char *propname)
  732. {
  733. return of_property_read_bool(to_of_node(fwnode), propname);
  734. }
  735. static int of_fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
  736. const char *propname,
  737. unsigned int elem_size, void *val,
  738. size_t nval)
  739. {
  740. const struct device_node *node = to_of_node(fwnode);
  741. if (!val)
  742. return of_property_count_elems_of_size(node, propname,
  743. elem_size);
  744. switch (elem_size) {
  745. case sizeof(u8):
  746. return of_property_read_u8_array(node, propname, val, nval);
  747. case sizeof(u16):
  748. return of_property_read_u16_array(node, propname, val, nval);
  749. case sizeof(u32):
  750. return of_property_read_u32_array(node, propname, val, nval);
  751. case sizeof(u64):
  752. return of_property_read_u64_array(node, propname, val, nval);
  753. }
  754. return -ENXIO;
  755. }
  756. static int
  757. of_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
  758. const char *propname, const char **val,
  759. size_t nval)
  760. {
  761. const struct device_node *node = to_of_node(fwnode);
  762. return val ?
  763. of_property_read_string_array(node, propname, val, nval) :
  764. of_property_count_strings(node, propname);
  765. }
  766. static struct fwnode_handle *
  767. of_fwnode_get_parent(const struct fwnode_handle *fwnode)
  768. {
  769. return of_fwnode_handle(of_get_parent(to_of_node(fwnode)));
  770. }
  771. static struct fwnode_handle *
  772. of_fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
  773. struct fwnode_handle *child)
  774. {
  775. return of_fwnode_handle(of_get_next_available_child(to_of_node(fwnode),
  776. to_of_node(child)));
  777. }
  778. static struct fwnode_handle *
  779. of_fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
  780. const char *childname)
  781. {
  782. const struct device_node *node = to_of_node(fwnode);
  783. struct device_node *child;
  784. for_each_available_child_of_node(node, child)
  785. if (!of_node_cmp(child->name, childname))
  786. return of_fwnode_handle(child);
  787. return NULL;
  788. }
  789. static int
  790. of_fwnode_get_reference_args(const struct fwnode_handle *fwnode,
  791. const char *prop, const char *nargs_prop,
  792. unsigned int nargs, unsigned int index,
  793. struct fwnode_reference_args *args)
  794. {
  795. struct of_phandle_args of_args;
  796. unsigned int i;
  797. int ret;
  798. if (nargs_prop)
  799. ret = of_parse_phandle_with_args(to_of_node(fwnode), prop,
  800. nargs_prop, index, &of_args);
  801. else
  802. ret = of_parse_phandle_with_fixed_args(to_of_node(fwnode), prop,
  803. nargs, index, &of_args);
  804. if (ret < 0)
  805. return ret;
  806. if (!args)
  807. return 0;
  808. args->nargs = of_args.args_count;
  809. args->fwnode = of_fwnode_handle(of_args.np);
  810. for (i = 0; i < NR_FWNODE_REFERENCE_ARGS; i++)
  811. args->args[i] = i < of_args.args_count ? of_args.args[i] : 0;
  812. return 0;
  813. }
  814. static struct fwnode_handle *
  815. of_fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
  816. struct fwnode_handle *prev)
  817. {
  818. return of_fwnode_handle(of_graph_get_next_endpoint(to_of_node(fwnode),
  819. to_of_node(prev)));
  820. }
  821. static struct fwnode_handle *
  822. of_fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
  823. {
  824. return of_fwnode_handle(
  825. of_graph_get_remote_endpoint(to_of_node(fwnode)));
  826. }
  827. static struct fwnode_handle *
  828. of_fwnode_graph_get_port_parent(struct fwnode_handle *fwnode)
  829. {
  830. struct device_node *np;
  831. /* Get the parent of the port */
  832. np = of_get_parent(to_of_node(fwnode));
  833. if (!np)
  834. return NULL;
  835. /* Is this the "ports" node? If not, it's the port parent. */
  836. if (of_node_cmp(np->name, "ports"))
  837. return of_fwnode_handle(np);
  838. return of_fwnode_handle(of_get_next_parent(np));
  839. }
  840. static int of_fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
  841. struct fwnode_endpoint *endpoint)
  842. {
  843. const struct device_node *node = to_of_node(fwnode);
  844. struct device_node *port_node = of_get_parent(node);
  845. endpoint->local_fwnode = fwnode;
  846. of_property_read_u32(port_node, "reg", &endpoint->port);
  847. of_property_read_u32(node, "reg", &endpoint->id);
  848. of_node_put(port_node);
  849. return 0;
  850. }
  851. const struct fwnode_operations of_fwnode_ops = {
  852. .get = of_fwnode_get,
  853. .put = of_fwnode_put,
  854. .device_is_available = of_fwnode_device_is_available,
  855. .property_present = of_fwnode_property_present,
  856. .property_read_int_array = of_fwnode_property_read_int_array,
  857. .property_read_string_array = of_fwnode_property_read_string_array,
  858. .get_parent = of_fwnode_get_parent,
  859. .get_next_child_node = of_fwnode_get_next_child_node,
  860. .get_named_child_node = of_fwnode_get_named_child_node,
  861. .get_reference_args = of_fwnode_get_reference_args,
  862. .graph_get_next_endpoint = of_fwnode_graph_get_next_endpoint,
  863. .graph_get_remote_endpoint = of_fwnode_graph_get_remote_endpoint,
  864. .graph_get_port_parent = of_fwnode_graph_get_port_parent,
  865. .graph_parse_endpoint = of_fwnode_graph_parse_endpoint,
  866. };
  867. EXPORT_SYMBOL_GPL(of_fwnode_ops);