property.c 29 KB

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