property.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * property.c - Unified device property interface.
  4. *
  5. * Copyright (C) 2014, Intel Corporation
  6. * Authors: Rafael J. Wysocki <[email protected]>
  7. * Mika Westerberg <[email protected]>
  8. */
  9. #include <linux/acpi.h>
  10. #include <linux/export.h>
  11. #include <linux/kernel.h>
  12. #include <linux/of.h>
  13. #include <linux/of_address.h>
  14. #include <linux/of_graph.h>
  15. #include <linux/of_irq.h>
  16. #include <linux/property.h>
  17. #include <linux/phy.h>
  18. struct fwnode_handle *dev_fwnode(const struct device *dev)
  19. {
  20. return IS_ENABLED(CONFIG_OF) && dev->of_node ?
  21. of_fwnode_handle(dev->of_node) : dev->fwnode;
  22. }
  23. EXPORT_SYMBOL_GPL(dev_fwnode);
  24. /**
  25. * device_property_present - check if a property of a device is present
  26. * @dev: Device whose property is being checked
  27. * @propname: Name of the property
  28. *
  29. * Check if property @propname is present in the device firmware description.
  30. *
  31. * Return: true if property @propname is present. Otherwise, returns false.
  32. */
  33. bool device_property_present(struct device *dev, const char *propname)
  34. {
  35. return fwnode_property_present(dev_fwnode(dev), propname);
  36. }
  37. EXPORT_SYMBOL_GPL(device_property_present);
  38. /**
  39. * fwnode_property_present - check if a property of a firmware node is present
  40. * @fwnode: Firmware node whose property to check
  41. * @propname: Name of the property
  42. *
  43. * Return: true if property @propname is present. Otherwise, returns false.
  44. */
  45. bool fwnode_property_present(const struct fwnode_handle *fwnode,
  46. const char *propname)
  47. {
  48. bool ret;
  49. if (IS_ERR_OR_NULL(fwnode))
  50. return false;
  51. ret = fwnode_call_bool_op(fwnode, property_present, propname);
  52. if (ret)
  53. return ret;
  54. return fwnode_call_bool_op(fwnode->secondary, property_present, propname);
  55. }
  56. EXPORT_SYMBOL_GPL(fwnode_property_present);
  57. /**
  58. * device_property_read_u8_array - return a u8 array property of a device
  59. * @dev: Device to get the property of
  60. * @propname: Name of the property
  61. * @val: The values are stored here or %NULL to return the number of values
  62. * @nval: Size of the @val array
  63. *
  64. * Function reads an array of u8 properties with @propname from the device
  65. * firmware description and stores them to @val if found.
  66. *
  67. * It's recommended to call device_property_count_u8() instead of calling
  68. * this function with @val equals %NULL and @nval equals 0.
  69. *
  70. * Return: number of values if @val was %NULL,
  71. * %0 if the property was found (success),
  72. * %-EINVAL if given arguments are not valid,
  73. * %-ENODATA if the property does not have a value,
  74. * %-EPROTO if the property is not an array of numbers,
  75. * %-EOVERFLOW if the size of the property is not as expected.
  76. * %-ENXIO if no suitable firmware interface is present.
  77. */
  78. int device_property_read_u8_array(struct device *dev, const char *propname,
  79. u8 *val, size_t nval)
  80. {
  81. return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
  82. }
  83. EXPORT_SYMBOL_GPL(device_property_read_u8_array);
  84. /**
  85. * device_property_read_u16_array - return a u16 array property of a device
  86. * @dev: Device to get the property of
  87. * @propname: Name of the property
  88. * @val: The values are stored here or %NULL to return the number of values
  89. * @nval: Size of the @val array
  90. *
  91. * Function reads an array of u16 properties with @propname from the device
  92. * firmware description and stores them to @val if found.
  93. *
  94. * It's recommended to call device_property_count_u16() instead of calling
  95. * this function with @val equals %NULL and @nval equals 0.
  96. *
  97. * Return: number of values if @val was %NULL,
  98. * %0 if the property was found (success),
  99. * %-EINVAL if given arguments are not valid,
  100. * %-ENODATA if the property does not have a value,
  101. * %-EPROTO if the property is not an array of numbers,
  102. * %-EOVERFLOW if the size of the property is not as expected.
  103. * %-ENXIO if no suitable firmware interface is present.
  104. */
  105. int device_property_read_u16_array(struct device *dev, const char *propname,
  106. u16 *val, size_t nval)
  107. {
  108. return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
  109. }
  110. EXPORT_SYMBOL_GPL(device_property_read_u16_array);
  111. /**
  112. * device_property_read_u32_array - return a u32 array property of a device
  113. * @dev: Device to get the property of
  114. * @propname: Name of the property
  115. * @val: The values are stored here or %NULL to return the number of values
  116. * @nval: Size of the @val array
  117. *
  118. * Function reads an array of u32 properties with @propname from the device
  119. * firmware description and stores them to @val if found.
  120. *
  121. * It's recommended to call device_property_count_u32() instead of calling
  122. * this function with @val equals %NULL and @nval equals 0.
  123. *
  124. * Return: number of values if @val was %NULL,
  125. * %0 if the property was found (success),
  126. * %-EINVAL if given arguments are not valid,
  127. * %-ENODATA if the property does not have a value,
  128. * %-EPROTO if the property is not an array of numbers,
  129. * %-EOVERFLOW if the size of the property is not as expected.
  130. * %-ENXIO if no suitable firmware interface is present.
  131. */
  132. int device_property_read_u32_array(struct device *dev, const char *propname,
  133. u32 *val, size_t nval)
  134. {
  135. return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
  136. }
  137. EXPORT_SYMBOL_GPL(device_property_read_u32_array);
  138. /**
  139. * device_property_read_u64_array - return a u64 array property of a device
  140. * @dev: Device to get the property of
  141. * @propname: Name of the property
  142. * @val: The values are stored here or %NULL to return the number of values
  143. * @nval: Size of the @val array
  144. *
  145. * Function reads an array of u64 properties with @propname from the device
  146. * firmware description and stores them to @val if found.
  147. *
  148. * It's recommended to call device_property_count_u64() instead of calling
  149. * this function with @val equals %NULL and @nval equals 0.
  150. *
  151. * Return: number of values if @val was %NULL,
  152. * %0 if the property was found (success),
  153. * %-EINVAL if given arguments are not valid,
  154. * %-ENODATA if the property does not have a value,
  155. * %-EPROTO if the property is not an array of numbers,
  156. * %-EOVERFLOW if the size of the property is not as expected.
  157. * %-ENXIO if no suitable firmware interface is present.
  158. */
  159. int device_property_read_u64_array(struct device *dev, const char *propname,
  160. u64 *val, size_t nval)
  161. {
  162. return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
  163. }
  164. EXPORT_SYMBOL_GPL(device_property_read_u64_array);
  165. /**
  166. * device_property_read_string_array - return a string array property of device
  167. * @dev: Device to get the property of
  168. * @propname: Name of the property
  169. * @val: The values are stored here or %NULL to return the number of values
  170. * @nval: Size of the @val array
  171. *
  172. * Function reads an array of string properties with @propname from the device
  173. * firmware description and stores them to @val if found.
  174. *
  175. * It's recommended to call device_property_string_array_count() instead of calling
  176. * this function with @val equals %NULL and @nval equals 0.
  177. *
  178. * Return: number of values read on success if @val is non-NULL,
  179. * number of values available on success if @val is NULL,
  180. * %-EINVAL if given arguments are not valid,
  181. * %-ENODATA if the property does not have a value,
  182. * %-EPROTO or %-EILSEQ if the property is not an array of strings,
  183. * %-EOVERFLOW if the size of the property is not as expected.
  184. * %-ENXIO if no suitable firmware interface is present.
  185. */
  186. int device_property_read_string_array(struct device *dev, const char *propname,
  187. const char **val, size_t nval)
  188. {
  189. return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
  190. }
  191. EXPORT_SYMBOL_GPL(device_property_read_string_array);
  192. /**
  193. * device_property_read_string - return a string property of a device
  194. * @dev: Device to get the property of
  195. * @propname: Name of the property
  196. * @val: The value is stored here
  197. *
  198. * Function reads property @propname from the device firmware description and
  199. * stores the value into @val if found. The value is checked to be a string.
  200. *
  201. * Return: %0 if the property was found (success),
  202. * %-EINVAL if given arguments are not valid,
  203. * %-ENODATA if the property does not have a value,
  204. * %-EPROTO or %-EILSEQ if the property type is not a string.
  205. * %-ENXIO if no suitable firmware interface is present.
  206. */
  207. int device_property_read_string(struct device *dev, const char *propname,
  208. const char **val)
  209. {
  210. return fwnode_property_read_string(dev_fwnode(dev), propname, val);
  211. }
  212. EXPORT_SYMBOL_GPL(device_property_read_string);
  213. /**
  214. * device_property_match_string - find a string in an array and return index
  215. * @dev: Device to get the property of
  216. * @propname: Name of the property holding the array
  217. * @string: String to look for
  218. *
  219. * Find a given string in a string array and if it is found return the
  220. * index back.
  221. *
  222. * Return: index, starting from %0, if the property was found (success),
  223. * %-EINVAL if given arguments are not valid,
  224. * %-ENODATA if the property does not have a value,
  225. * %-EPROTO if the property is not an array of strings,
  226. * %-ENXIO if no suitable firmware interface is present.
  227. */
  228. int device_property_match_string(struct device *dev, const char *propname,
  229. const char *string)
  230. {
  231. return fwnode_property_match_string(dev_fwnode(dev), propname, string);
  232. }
  233. EXPORT_SYMBOL_GPL(device_property_match_string);
  234. static int fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
  235. const char *propname,
  236. unsigned int elem_size, void *val,
  237. size_t nval)
  238. {
  239. int ret;
  240. if (IS_ERR_OR_NULL(fwnode))
  241. return -EINVAL;
  242. ret = fwnode_call_int_op(fwnode, property_read_int_array, propname,
  243. elem_size, val, nval);
  244. if (ret != -EINVAL)
  245. return ret;
  246. return fwnode_call_int_op(fwnode->secondary, property_read_int_array, propname,
  247. elem_size, val, nval);
  248. }
  249. /**
  250. * fwnode_property_read_u8_array - return a u8 array property of firmware node
  251. * @fwnode: Firmware node to get the property of
  252. * @propname: Name of the property
  253. * @val: The values are stored here or %NULL to return the number of values
  254. * @nval: Size of the @val array
  255. *
  256. * Read an array of u8 properties with @propname from @fwnode and stores them to
  257. * @val if found.
  258. *
  259. * It's recommended to call fwnode_property_count_u8() instead of calling
  260. * this function with @val equals %NULL and @nval equals 0.
  261. *
  262. * Return: number of values if @val was %NULL,
  263. * %0 if the property was found (success),
  264. * %-EINVAL if given arguments are not valid,
  265. * %-ENODATA if the property does not have a value,
  266. * %-EPROTO if the property is not an array of numbers,
  267. * %-EOVERFLOW if the size of the property is not as expected,
  268. * %-ENXIO if no suitable firmware interface is present.
  269. */
  270. int fwnode_property_read_u8_array(const struct fwnode_handle *fwnode,
  271. const char *propname, u8 *val, size_t nval)
  272. {
  273. return fwnode_property_read_int_array(fwnode, propname, sizeof(u8),
  274. val, nval);
  275. }
  276. EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
  277. /**
  278. * fwnode_property_read_u16_array - return a u16 array property of firmware node
  279. * @fwnode: Firmware node to get the property of
  280. * @propname: Name of the property
  281. * @val: The values are stored here or %NULL to return the number of values
  282. * @nval: Size of the @val array
  283. *
  284. * Read an array of u16 properties with @propname from @fwnode and store them to
  285. * @val if found.
  286. *
  287. * It's recommended to call fwnode_property_count_u16() instead of calling
  288. * this function with @val equals %NULL and @nval equals 0.
  289. *
  290. * Return: number of values if @val was %NULL,
  291. * %0 if the property was found (success),
  292. * %-EINVAL if given arguments are not valid,
  293. * %-ENODATA if the property does not have a value,
  294. * %-EPROTO if the property is not an array of numbers,
  295. * %-EOVERFLOW if the size of the property is not as expected,
  296. * %-ENXIO if no suitable firmware interface is present.
  297. */
  298. int fwnode_property_read_u16_array(const struct fwnode_handle *fwnode,
  299. const char *propname, u16 *val, size_t nval)
  300. {
  301. return fwnode_property_read_int_array(fwnode, propname, sizeof(u16),
  302. val, nval);
  303. }
  304. EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
  305. /**
  306. * fwnode_property_read_u32_array - return a u32 array property of firmware node
  307. * @fwnode: Firmware node to get the property of
  308. * @propname: Name of the property
  309. * @val: The values are stored here or %NULL to return the number of values
  310. * @nval: Size of the @val array
  311. *
  312. * Read an array of u32 properties with @propname from @fwnode store them to
  313. * @val if found.
  314. *
  315. * It's recommended to call fwnode_property_count_u32() instead of calling
  316. * this function with @val equals %NULL and @nval equals 0.
  317. *
  318. * Return: number of values if @val was %NULL,
  319. * %0 if the property was found (success),
  320. * %-EINVAL if given arguments are not valid,
  321. * %-ENODATA if the property does not have a value,
  322. * %-EPROTO if the property is not an array of numbers,
  323. * %-EOVERFLOW if the size of the property is not as expected,
  324. * %-ENXIO if no suitable firmware interface is present.
  325. */
  326. int fwnode_property_read_u32_array(const struct fwnode_handle *fwnode,
  327. const char *propname, u32 *val, size_t nval)
  328. {
  329. return fwnode_property_read_int_array(fwnode, propname, sizeof(u32),
  330. val, nval);
  331. }
  332. EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
  333. /**
  334. * fwnode_property_read_u64_array - return a u64 array property firmware node
  335. * @fwnode: Firmware node to get the property of
  336. * @propname: Name of the property
  337. * @val: The values are stored here or %NULL to return the number of values
  338. * @nval: Size of the @val array
  339. *
  340. * Read an array of u64 properties with @propname from @fwnode and store them to
  341. * @val if found.
  342. *
  343. * It's recommended to call fwnode_property_count_u64() instead of calling
  344. * this function with @val equals %NULL and @nval equals 0.
  345. *
  346. * Return: number of values if @val was %NULL,
  347. * %0 if the property was found (success),
  348. * %-EINVAL if given arguments are not valid,
  349. * %-ENODATA if the property does not have a value,
  350. * %-EPROTO if the property is not an array of numbers,
  351. * %-EOVERFLOW if the size of the property is not as expected,
  352. * %-ENXIO if no suitable firmware interface is present.
  353. */
  354. int fwnode_property_read_u64_array(const struct fwnode_handle *fwnode,
  355. const char *propname, u64 *val, size_t nval)
  356. {
  357. return fwnode_property_read_int_array(fwnode, propname, sizeof(u64),
  358. val, nval);
  359. }
  360. EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
  361. /**
  362. * fwnode_property_read_string_array - return string array property of a node
  363. * @fwnode: Firmware node to get the property of
  364. * @propname: Name of the property
  365. * @val: The values are stored here or %NULL to return the number of values
  366. * @nval: Size of the @val array
  367. *
  368. * Read an string list property @propname from the given firmware node and store
  369. * them to @val if found.
  370. *
  371. * It's recommended to call fwnode_property_string_array_count() instead of calling
  372. * this function with @val equals %NULL and @nval equals 0.
  373. *
  374. * Return: number of values read on success if @val is non-NULL,
  375. * number of values available on success if @val is NULL,
  376. * %-EINVAL if given arguments are not valid,
  377. * %-ENODATA if the property does not have a value,
  378. * %-EPROTO or %-EILSEQ if the property is not an array of strings,
  379. * %-EOVERFLOW if the size of the property is not as expected,
  380. * %-ENXIO if no suitable firmware interface is present.
  381. */
  382. int fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
  383. const char *propname, const char **val,
  384. size_t nval)
  385. {
  386. int ret;
  387. if (IS_ERR_OR_NULL(fwnode))
  388. return -EINVAL;
  389. ret = fwnode_call_int_op(fwnode, property_read_string_array, propname,
  390. val, nval);
  391. if (ret != -EINVAL)
  392. return ret;
  393. return fwnode_call_int_op(fwnode->secondary, property_read_string_array, propname,
  394. val, nval);
  395. }
  396. EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
  397. /**
  398. * fwnode_property_read_string - return a string property of a firmware node
  399. * @fwnode: Firmware node to get the property of
  400. * @propname: Name of the property
  401. * @val: The value is stored here
  402. *
  403. * Read property @propname from the given firmware node and store the value into
  404. * @val if found. The value is checked to be a string.
  405. *
  406. * Return: %0 if the property was found (success),
  407. * %-EINVAL if given arguments are not valid,
  408. * %-ENODATA if the property does not have a value,
  409. * %-EPROTO or %-EILSEQ if the property is not a string,
  410. * %-ENXIO if no suitable firmware interface is present.
  411. */
  412. int fwnode_property_read_string(const struct fwnode_handle *fwnode,
  413. const char *propname, const char **val)
  414. {
  415. int ret = fwnode_property_read_string_array(fwnode, propname, val, 1);
  416. return ret < 0 ? ret : 0;
  417. }
  418. EXPORT_SYMBOL_GPL(fwnode_property_read_string);
  419. /**
  420. * fwnode_property_match_string - find a string in an array and return index
  421. * @fwnode: Firmware node to get the property of
  422. * @propname: Name of the property holding the array
  423. * @string: String to look for
  424. *
  425. * Find a given string in a string array and if it is found return the
  426. * index back.
  427. *
  428. * Return: index, starting from %0, if the property was found (success),
  429. * %-EINVAL if given arguments are not valid,
  430. * %-ENODATA if the property does not have a value,
  431. * %-EPROTO if the property is not an array of strings,
  432. * %-ENXIO if no suitable firmware interface is present.
  433. */
  434. int fwnode_property_match_string(const struct fwnode_handle *fwnode,
  435. const char *propname, const char *string)
  436. {
  437. const char **values;
  438. int nval, ret;
  439. nval = fwnode_property_read_string_array(fwnode, propname, NULL, 0);
  440. if (nval < 0)
  441. return nval;
  442. if (nval == 0)
  443. return -ENODATA;
  444. values = kcalloc(nval, sizeof(*values), GFP_KERNEL);
  445. if (!values)
  446. return -ENOMEM;
  447. ret = fwnode_property_read_string_array(fwnode, propname, values, nval);
  448. if (ret < 0)
  449. goto out;
  450. ret = match_string(values, nval, string);
  451. if (ret < 0)
  452. ret = -ENODATA;
  453. out:
  454. kfree(values);
  455. return ret;
  456. }
  457. EXPORT_SYMBOL_GPL(fwnode_property_match_string);
  458. /**
  459. * fwnode_property_get_reference_args() - Find a reference with arguments
  460. * @fwnode: Firmware node where to look for the reference
  461. * @prop: The name of the property
  462. * @nargs_prop: The name of the property telling the number of
  463. * arguments in the referred node. NULL if @nargs is known,
  464. * otherwise @nargs is ignored. Only relevant on OF.
  465. * @nargs: Number of arguments. Ignored if @nargs_prop is non-NULL.
  466. * @index: Index of the reference, from zero onwards.
  467. * @args: Result structure with reference and integer arguments.
  468. *
  469. * Obtain a reference based on a named property in an fwnode, with
  470. * integer arguments.
  471. *
  472. * The caller is responsible for calling fwnode_handle_put() on the returned
  473. * @args->fwnode pointer.
  474. *
  475. * Return: %0 on success
  476. * %-ENOENT when the index is out of bounds, the index has an empty
  477. * reference or the property was not found
  478. * %-EINVAL on parse error
  479. */
  480. int fwnode_property_get_reference_args(const struct fwnode_handle *fwnode,
  481. const char *prop, const char *nargs_prop,
  482. unsigned int nargs, unsigned int index,
  483. struct fwnode_reference_args *args)
  484. {
  485. int ret;
  486. if (IS_ERR_OR_NULL(fwnode))
  487. return -ENOENT;
  488. ret = fwnode_call_int_op(fwnode, get_reference_args, prop, nargs_prop,
  489. nargs, index, args);
  490. if (ret == 0)
  491. return ret;
  492. if (IS_ERR_OR_NULL(fwnode->secondary))
  493. return ret;
  494. return fwnode_call_int_op(fwnode->secondary, get_reference_args, prop, nargs_prop,
  495. nargs, index, args);
  496. }
  497. EXPORT_SYMBOL_GPL(fwnode_property_get_reference_args);
  498. /**
  499. * fwnode_find_reference - Find named reference to a fwnode_handle
  500. * @fwnode: Firmware node where to look for the reference
  501. * @name: The name of the reference
  502. * @index: Index of the reference
  503. *
  504. * @index can be used when the named reference holds a table of references.
  505. *
  506. * The caller is responsible for calling fwnode_handle_put() on the returned
  507. * fwnode pointer.
  508. *
  509. * Return: a pointer to the reference fwnode, when found. Otherwise,
  510. * returns an error pointer.
  511. */
  512. struct fwnode_handle *fwnode_find_reference(const struct fwnode_handle *fwnode,
  513. const char *name,
  514. unsigned int index)
  515. {
  516. struct fwnode_reference_args args;
  517. int ret;
  518. ret = fwnode_property_get_reference_args(fwnode, name, NULL, 0, index,
  519. &args);
  520. return ret ? ERR_PTR(ret) : args.fwnode;
  521. }
  522. EXPORT_SYMBOL_GPL(fwnode_find_reference);
  523. /**
  524. * fwnode_get_name - Return the name of a node
  525. * @fwnode: The firmware node
  526. *
  527. * Return: a pointer to the node name, or %NULL.
  528. */
  529. const char *fwnode_get_name(const struct fwnode_handle *fwnode)
  530. {
  531. return fwnode_call_ptr_op(fwnode, get_name);
  532. }
  533. EXPORT_SYMBOL_GPL(fwnode_get_name);
  534. /**
  535. * fwnode_get_name_prefix - Return the prefix of node for printing purposes
  536. * @fwnode: The firmware node
  537. *
  538. * Return: the prefix of a node, intended to be printed right before the node.
  539. * The prefix works also as a separator between the nodes.
  540. */
  541. const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
  542. {
  543. return fwnode_call_ptr_op(fwnode, get_name_prefix);
  544. }
  545. /**
  546. * fwnode_get_parent - Return parent firwmare node
  547. * @fwnode: Firmware whose parent is retrieved
  548. *
  549. * The caller is responsible for calling fwnode_handle_put() on the returned
  550. * fwnode pointer.
  551. *
  552. * Return: parent firmware node of the given node if possible or %NULL if no
  553. * parent was available.
  554. */
  555. struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode)
  556. {
  557. return fwnode_call_ptr_op(fwnode, get_parent);
  558. }
  559. EXPORT_SYMBOL_GPL(fwnode_get_parent);
  560. /**
  561. * fwnode_get_next_parent - Iterate to the node's parent
  562. * @fwnode: Firmware whose parent is retrieved
  563. *
  564. * This is like fwnode_get_parent() except that it drops the refcount
  565. * on the passed node, making it suitable for iterating through a
  566. * node's parents.
  567. *
  568. * The caller is responsible for calling fwnode_handle_put() on the returned
  569. * fwnode pointer. Note that this function also puts a reference to @fwnode
  570. * unconditionally.
  571. *
  572. * Return: parent firmware node of the given node if possible or %NULL if no
  573. * parent was available.
  574. */
  575. struct fwnode_handle *fwnode_get_next_parent(struct fwnode_handle *fwnode)
  576. {
  577. struct fwnode_handle *parent = fwnode_get_parent(fwnode);
  578. fwnode_handle_put(fwnode);
  579. return parent;
  580. }
  581. EXPORT_SYMBOL_GPL(fwnode_get_next_parent);
  582. /**
  583. * fwnode_get_next_parent_dev - Find device of closest ancestor fwnode
  584. * @fwnode: firmware node
  585. *
  586. * Given a firmware node (@fwnode), this function finds its closest ancestor
  587. * firmware node that has a corresponding struct device and returns that struct
  588. * device.
  589. *
  590. * The caller is responsible for calling put_device() on the returned device
  591. * pointer.
  592. *
  593. * Return: a pointer to the device of the @fwnode's closest ancestor.
  594. */
  595. struct device *fwnode_get_next_parent_dev(struct fwnode_handle *fwnode)
  596. {
  597. struct fwnode_handle *parent;
  598. struct device *dev;
  599. fwnode_for_each_parent_node(fwnode, parent) {
  600. dev = get_dev_from_fwnode(parent);
  601. if (dev) {
  602. fwnode_handle_put(parent);
  603. return dev;
  604. }
  605. }
  606. return NULL;
  607. }
  608. /**
  609. * fwnode_count_parents - Return the number of parents a node has
  610. * @fwnode: The node the parents of which are to be counted
  611. *
  612. * Return: the number of parents a node has.
  613. */
  614. unsigned int fwnode_count_parents(const struct fwnode_handle *fwnode)
  615. {
  616. struct fwnode_handle *parent;
  617. unsigned int count = 0;
  618. fwnode_for_each_parent_node(fwnode, parent)
  619. count++;
  620. return count;
  621. }
  622. EXPORT_SYMBOL_GPL(fwnode_count_parents);
  623. /**
  624. * fwnode_get_nth_parent - Return an nth parent of a node
  625. * @fwnode: The node the parent of which is requested
  626. * @depth: Distance of the parent from the node
  627. *
  628. * The caller is responsible for calling fwnode_handle_put() on the returned
  629. * fwnode pointer.
  630. *
  631. * Return: the nth parent of a node. If there is no parent at the requested
  632. * @depth, %NULL is returned. If @depth is 0, the functionality is equivalent to
  633. * fwnode_handle_get(). For @depth == 1, it is fwnode_get_parent() and so on.
  634. */
  635. struct fwnode_handle *fwnode_get_nth_parent(struct fwnode_handle *fwnode,
  636. unsigned int depth)
  637. {
  638. struct fwnode_handle *parent;
  639. if (depth == 0)
  640. return fwnode_handle_get(fwnode);
  641. fwnode_for_each_parent_node(fwnode, parent) {
  642. if (--depth == 0)
  643. return parent;
  644. }
  645. return NULL;
  646. }
  647. EXPORT_SYMBOL_GPL(fwnode_get_nth_parent);
  648. /**
  649. * fwnode_is_ancestor_of - Test if @ancestor is ancestor of @child
  650. * @ancestor: Firmware which is tested for being an ancestor
  651. * @child: Firmware which is tested for being the child
  652. *
  653. * A node is considered an ancestor of itself too.
  654. *
  655. * Return: true if @ancestor is an ancestor of @child. Otherwise, returns false.
  656. */
  657. bool fwnode_is_ancestor_of(struct fwnode_handle *ancestor, struct fwnode_handle *child)
  658. {
  659. struct fwnode_handle *parent;
  660. if (IS_ERR_OR_NULL(ancestor))
  661. return false;
  662. if (child == ancestor)
  663. return true;
  664. fwnode_for_each_parent_node(child, parent) {
  665. if (parent == ancestor) {
  666. fwnode_handle_put(parent);
  667. return true;
  668. }
  669. }
  670. return false;
  671. }
  672. /**
  673. * fwnode_get_next_child_node - Return the next child node handle for a node
  674. * @fwnode: Firmware node to find the next child node for.
  675. * @child: Handle to one of the node's child nodes or a %NULL handle.
  676. *
  677. * The caller is responsible for calling fwnode_handle_put() on the returned
  678. * fwnode pointer. Note that this function also puts a reference to @child
  679. * unconditionally.
  680. */
  681. struct fwnode_handle *
  682. fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
  683. struct fwnode_handle *child)
  684. {
  685. return fwnode_call_ptr_op(fwnode, get_next_child_node, child);
  686. }
  687. EXPORT_SYMBOL_GPL(fwnode_get_next_child_node);
  688. /**
  689. * fwnode_get_next_available_child_node - Return the next available child node handle for a node
  690. * @fwnode: Firmware node to find the next child node for.
  691. * @child: Handle to one of the node's child nodes or a %NULL handle.
  692. *
  693. * The caller is responsible for calling fwnode_handle_put() on the returned
  694. * fwnode pointer. Note that this function also puts a reference to @child
  695. * unconditionally.
  696. */
  697. struct fwnode_handle *
  698. fwnode_get_next_available_child_node(const struct fwnode_handle *fwnode,
  699. struct fwnode_handle *child)
  700. {
  701. struct fwnode_handle *next_child = child;
  702. if (IS_ERR_OR_NULL(fwnode))
  703. return NULL;
  704. do {
  705. next_child = fwnode_get_next_child_node(fwnode, next_child);
  706. if (!next_child)
  707. return NULL;
  708. } while (!fwnode_device_is_available(next_child));
  709. return next_child;
  710. }
  711. EXPORT_SYMBOL_GPL(fwnode_get_next_available_child_node);
  712. /**
  713. * device_get_next_child_node - Return the next child node handle for a device
  714. * @dev: Device to find the next child node for.
  715. * @child: Handle to one of the device's child nodes or a %NULL handle.
  716. *
  717. * The caller is responsible for calling fwnode_handle_put() on the returned
  718. * fwnode pointer. Note that this function also puts a reference to @child
  719. * unconditionally.
  720. */
  721. struct fwnode_handle *device_get_next_child_node(struct device *dev,
  722. struct fwnode_handle *child)
  723. {
  724. const struct fwnode_handle *fwnode = dev_fwnode(dev);
  725. struct fwnode_handle *next;
  726. if (IS_ERR_OR_NULL(fwnode))
  727. return NULL;
  728. /* Try to find a child in primary fwnode */
  729. next = fwnode_get_next_child_node(fwnode, child);
  730. if (next)
  731. return next;
  732. /* When no more children in primary, continue with secondary */
  733. return fwnode_get_next_child_node(fwnode->secondary, child);
  734. }
  735. EXPORT_SYMBOL_GPL(device_get_next_child_node);
  736. /**
  737. * fwnode_get_named_child_node - Return first matching named child node handle
  738. * @fwnode: Firmware node to find the named child node for.
  739. * @childname: String to match child node name against.
  740. *
  741. * The caller is responsible for calling fwnode_handle_put() on the returned
  742. * fwnode pointer.
  743. */
  744. struct fwnode_handle *
  745. fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
  746. const char *childname)
  747. {
  748. return fwnode_call_ptr_op(fwnode, get_named_child_node, childname);
  749. }
  750. EXPORT_SYMBOL_GPL(fwnode_get_named_child_node);
  751. /**
  752. * device_get_named_child_node - Return first matching named child node handle
  753. * @dev: Device to find the named child node for.
  754. * @childname: String to match child node name against.
  755. *
  756. * The caller is responsible for calling fwnode_handle_put() on the returned
  757. * fwnode pointer.
  758. */
  759. struct fwnode_handle *device_get_named_child_node(struct device *dev,
  760. const char *childname)
  761. {
  762. return fwnode_get_named_child_node(dev_fwnode(dev), childname);
  763. }
  764. EXPORT_SYMBOL_GPL(device_get_named_child_node);
  765. /**
  766. * fwnode_handle_get - Obtain a reference to a device node
  767. * @fwnode: Pointer to the device node to obtain the reference to.
  768. *
  769. * The caller is responsible for calling fwnode_handle_put() on the returned
  770. * fwnode pointer.
  771. *
  772. * Return: the fwnode handle.
  773. */
  774. struct fwnode_handle *fwnode_handle_get(struct fwnode_handle *fwnode)
  775. {
  776. if (!fwnode_has_op(fwnode, get))
  777. return fwnode;
  778. return fwnode_call_ptr_op(fwnode, get);
  779. }
  780. EXPORT_SYMBOL_GPL(fwnode_handle_get);
  781. /**
  782. * fwnode_handle_put - Drop reference to a device node
  783. * @fwnode: Pointer to the device node to drop the reference to.
  784. *
  785. * This has to be used when terminating device_for_each_child_node() iteration
  786. * with break or return to prevent stale device node references from being left
  787. * behind.
  788. */
  789. void fwnode_handle_put(struct fwnode_handle *fwnode)
  790. {
  791. fwnode_call_void_op(fwnode, put);
  792. }
  793. EXPORT_SYMBOL_GPL(fwnode_handle_put);
  794. /**
  795. * fwnode_device_is_available - check if a device is available for use
  796. * @fwnode: Pointer to the fwnode of the device.
  797. *
  798. * Return: true if device is available for use. Otherwise, returns false.
  799. *
  800. * For fwnode node types that don't implement the .device_is_available()
  801. * operation, this function returns true.
  802. */
  803. bool fwnode_device_is_available(const struct fwnode_handle *fwnode)
  804. {
  805. if (IS_ERR_OR_NULL(fwnode))
  806. return false;
  807. if (!fwnode_has_op(fwnode, device_is_available))
  808. return true;
  809. return fwnode_call_bool_op(fwnode, device_is_available);
  810. }
  811. EXPORT_SYMBOL_GPL(fwnode_device_is_available);
  812. /**
  813. * device_get_child_node_count - return the number of child nodes for device
  814. * @dev: Device to cound the child nodes for
  815. *
  816. * Return: the number of child nodes for a given device.
  817. */
  818. unsigned int device_get_child_node_count(struct device *dev)
  819. {
  820. struct fwnode_handle *child;
  821. unsigned int count = 0;
  822. device_for_each_child_node(dev, child)
  823. count++;
  824. return count;
  825. }
  826. EXPORT_SYMBOL_GPL(device_get_child_node_count);
  827. bool device_dma_supported(struct device *dev)
  828. {
  829. return fwnode_call_bool_op(dev_fwnode(dev), device_dma_supported);
  830. }
  831. EXPORT_SYMBOL_GPL(device_dma_supported);
  832. enum dev_dma_attr device_get_dma_attr(struct device *dev)
  833. {
  834. if (!fwnode_has_op(dev_fwnode(dev), device_get_dma_attr))
  835. return DEV_DMA_NOT_SUPPORTED;
  836. return fwnode_call_int_op(dev_fwnode(dev), device_get_dma_attr);
  837. }
  838. EXPORT_SYMBOL_GPL(device_get_dma_attr);
  839. /**
  840. * fwnode_get_phy_mode - Get phy mode for given firmware node
  841. * @fwnode: Pointer to the given node
  842. *
  843. * The function gets phy interface string from property 'phy-mode' or
  844. * 'phy-connection-type', and return its index in phy_modes table, or errno in
  845. * error case.
  846. */
  847. int fwnode_get_phy_mode(struct fwnode_handle *fwnode)
  848. {
  849. const char *pm;
  850. int err, i;
  851. err = fwnode_property_read_string(fwnode, "phy-mode", &pm);
  852. if (err < 0)
  853. err = fwnode_property_read_string(fwnode,
  854. "phy-connection-type", &pm);
  855. if (err < 0)
  856. return err;
  857. for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
  858. if (!strcasecmp(pm, phy_modes(i)))
  859. return i;
  860. return -ENODEV;
  861. }
  862. EXPORT_SYMBOL_GPL(fwnode_get_phy_mode);
  863. /**
  864. * device_get_phy_mode - Get phy mode for given device
  865. * @dev: Pointer to the given device
  866. *
  867. * The function gets phy interface string from property 'phy-mode' or
  868. * 'phy-connection-type', and return its index in phy_modes table, or errno in
  869. * error case.
  870. */
  871. int device_get_phy_mode(struct device *dev)
  872. {
  873. return fwnode_get_phy_mode(dev_fwnode(dev));
  874. }
  875. EXPORT_SYMBOL_GPL(device_get_phy_mode);
  876. /**
  877. * fwnode_iomap - Maps the memory mapped IO for a given fwnode
  878. * @fwnode: Pointer to the firmware node
  879. * @index: Index of the IO range
  880. *
  881. * Return: a pointer to the mapped memory.
  882. */
  883. void __iomem *fwnode_iomap(struct fwnode_handle *fwnode, int index)
  884. {
  885. return fwnode_call_ptr_op(fwnode, iomap, index);
  886. }
  887. EXPORT_SYMBOL(fwnode_iomap);
  888. /**
  889. * fwnode_irq_get - Get IRQ directly from a fwnode
  890. * @fwnode: Pointer to the firmware node
  891. * @index: Zero-based index of the IRQ
  892. *
  893. * Return: Linux IRQ number on success. Negative errno on failure.
  894. */
  895. int fwnode_irq_get(const struct fwnode_handle *fwnode, unsigned int index)
  896. {
  897. int ret;
  898. ret = fwnode_call_int_op(fwnode, irq_get, index);
  899. /* We treat mapping errors as invalid case */
  900. if (ret == 0)
  901. return -EINVAL;
  902. return ret;
  903. }
  904. EXPORT_SYMBOL(fwnode_irq_get);
  905. /**
  906. * fwnode_irq_get_byname - Get IRQ from a fwnode using its name
  907. * @fwnode: Pointer to the firmware node
  908. * @name: IRQ name
  909. *
  910. * Description:
  911. * Find a match to the string @name in the 'interrupt-names' string array
  912. * in _DSD for ACPI, or of_node for Device Tree. Then get the Linux IRQ
  913. * number of the IRQ resource corresponding to the index of the matched
  914. * string.
  915. *
  916. * Return: Linux IRQ number on success, or negative errno otherwise.
  917. */
  918. int fwnode_irq_get_byname(const struct fwnode_handle *fwnode, const char *name)
  919. {
  920. int index;
  921. if (!name)
  922. return -EINVAL;
  923. index = fwnode_property_match_string(fwnode, "interrupt-names", name);
  924. if (index < 0)
  925. return index;
  926. return fwnode_irq_get(fwnode, index);
  927. }
  928. EXPORT_SYMBOL(fwnode_irq_get_byname);
  929. /**
  930. * fwnode_graph_get_next_endpoint - Get next endpoint firmware node
  931. * @fwnode: Pointer to the parent firmware node
  932. * @prev: Previous endpoint node or %NULL to get the first
  933. *
  934. * The caller is responsible for calling fwnode_handle_put() on the returned
  935. * fwnode pointer. Note that this function also puts a reference to @prev
  936. * unconditionally.
  937. *
  938. * Return: an endpoint firmware node pointer or %NULL if no more endpoints
  939. * are available.
  940. */
  941. struct fwnode_handle *
  942. fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
  943. struct fwnode_handle *prev)
  944. {
  945. struct fwnode_handle *ep, *port_parent = NULL;
  946. const struct fwnode_handle *parent;
  947. /*
  948. * If this function is in a loop and the previous iteration returned
  949. * an endpoint from fwnode->secondary, then we need to use the secondary
  950. * as parent rather than @fwnode.
  951. */
  952. if (prev) {
  953. port_parent = fwnode_graph_get_port_parent(prev);
  954. parent = port_parent;
  955. } else {
  956. parent = fwnode;
  957. }
  958. if (IS_ERR_OR_NULL(parent))
  959. return NULL;
  960. ep = fwnode_call_ptr_op(parent, graph_get_next_endpoint, prev);
  961. if (ep)
  962. goto out_put_port_parent;
  963. ep = fwnode_graph_get_next_endpoint(parent->secondary, NULL);
  964. out_put_port_parent:
  965. fwnode_handle_put(port_parent);
  966. return ep;
  967. }
  968. EXPORT_SYMBOL_GPL(fwnode_graph_get_next_endpoint);
  969. /**
  970. * fwnode_graph_get_port_parent - Return the device fwnode of a port endpoint
  971. * @endpoint: Endpoint firmware node of the port
  972. *
  973. * The caller is responsible for calling fwnode_handle_put() on the returned
  974. * fwnode pointer.
  975. *
  976. * Return: the firmware node of the device the @endpoint belongs to.
  977. */
  978. struct fwnode_handle *
  979. fwnode_graph_get_port_parent(const struct fwnode_handle *endpoint)
  980. {
  981. struct fwnode_handle *port, *parent;
  982. port = fwnode_get_parent(endpoint);
  983. parent = fwnode_call_ptr_op(port, graph_get_port_parent);
  984. fwnode_handle_put(port);
  985. return parent;
  986. }
  987. EXPORT_SYMBOL_GPL(fwnode_graph_get_port_parent);
  988. /**
  989. * fwnode_graph_get_remote_port_parent - Return fwnode of a remote device
  990. * @fwnode: Endpoint firmware node pointing to the remote endpoint
  991. *
  992. * Extracts firmware node of a remote device the @fwnode points to.
  993. *
  994. * The caller is responsible for calling fwnode_handle_put() on the returned
  995. * fwnode pointer.
  996. */
  997. struct fwnode_handle *
  998. fwnode_graph_get_remote_port_parent(const struct fwnode_handle *fwnode)
  999. {
  1000. struct fwnode_handle *endpoint, *parent;
  1001. endpoint = fwnode_graph_get_remote_endpoint(fwnode);
  1002. parent = fwnode_graph_get_port_parent(endpoint);
  1003. fwnode_handle_put(endpoint);
  1004. return parent;
  1005. }
  1006. EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port_parent);
  1007. /**
  1008. * fwnode_graph_get_remote_port - Return fwnode of a remote port
  1009. * @fwnode: Endpoint firmware node pointing to the remote endpoint
  1010. *
  1011. * Extracts firmware node of a remote port the @fwnode points to.
  1012. *
  1013. * The caller is responsible for calling fwnode_handle_put() on the returned
  1014. * fwnode pointer.
  1015. */
  1016. struct fwnode_handle *
  1017. fwnode_graph_get_remote_port(const struct fwnode_handle *fwnode)
  1018. {
  1019. return fwnode_get_next_parent(fwnode_graph_get_remote_endpoint(fwnode));
  1020. }
  1021. EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port);
  1022. /**
  1023. * fwnode_graph_get_remote_endpoint - Return fwnode of a remote endpoint
  1024. * @fwnode: Endpoint firmware node pointing to the remote endpoint
  1025. *
  1026. * Extracts firmware node of a remote endpoint the @fwnode points to.
  1027. *
  1028. * The caller is responsible for calling fwnode_handle_put() on the returned
  1029. * fwnode pointer.
  1030. */
  1031. struct fwnode_handle *
  1032. fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
  1033. {
  1034. return fwnode_call_ptr_op(fwnode, graph_get_remote_endpoint);
  1035. }
  1036. EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_endpoint);
  1037. static bool fwnode_graph_remote_available(struct fwnode_handle *ep)
  1038. {
  1039. struct fwnode_handle *dev_node;
  1040. bool available;
  1041. dev_node = fwnode_graph_get_remote_port_parent(ep);
  1042. available = fwnode_device_is_available(dev_node);
  1043. fwnode_handle_put(dev_node);
  1044. return available;
  1045. }
  1046. /**
  1047. * fwnode_graph_get_endpoint_by_id - get endpoint by port and endpoint numbers
  1048. * @fwnode: parent fwnode_handle containing the graph
  1049. * @port: identifier of the port node
  1050. * @endpoint: identifier of the endpoint node under the port node
  1051. * @flags: fwnode lookup flags
  1052. *
  1053. * The caller is responsible for calling fwnode_handle_put() on the returned
  1054. * fwnode pointer.
  1055. *
  1056. * Return: the fwnode handle of the local endpoint corresponding the port and
  1057. * endpoint IDs or %NULL if not found.
  1058. *
  1059. * If FWNODE_GRAPH_ENDPOINT_NEXT is passed in @flags and the specified endpoint
  1060. * has not been found, look for the closest endpoint ID greater than the
  1061. * specified one and return the endpoint that corresponds to it, if present.
  1062. *
  1063. * Does not return endpoints that belong to disabled devices or endpoints that
  1064. * are unconnected, unless FWNODE_GRAPH_DEVICE_DISABLED is passed in @flags.
  1065. */
  1066. struct fwnode_handle *
  1067. fwnode_graph_get_endpoint_by_id(const struct fwnode_handle *fwnode,
  1068. u32 port, u32 endpoint, unsigned long flags)
  1069. {
  1070. struct fwnode_handle *ep, *best_ep = NULL;
  1071. unsigned int best_ep_id = 0;
  1072. bool endpoint_next = flags & FWNODE_GRAPH_ENDPOINT_NEXT;
  1073. bool enabled_only = !(flags & FWNODE_GRAPH_DEVICE_DISABLED);
  1074. fwnode_graph_for_each_endpoint(fwnode, ep) {
  1075. struct fwnode_endpoint fwnode_ep = { 0 };
  1076. int ret;
  1077. if (enabled_only && !fwnode_graph_remote_available(ep))
  1078. continue;
  1079. ret = fwnode_graph_parse_endpoint(ep, &fwnode_ep);
  1080. if (ret < 0)
  1081. continue;
  1082. if (fwnode_ep.port != port)
  1083. continue;
  1084. if (fwnode_ep.id == endpoint)
  1085. return ep;
  1086. if (!endpoint_next)
  1087. continue;
  1088. /*
  1089. * If the endpoint that has just been found is not the first
  1090. * matching one and the ID of the one found previously is closer
  1091. * to the requested endpoint ID, skip it.
  1092. */
  1093. if (fwnode_ep.id < endpoint ||
  1094. (best_ep && best_ep_id < fwnode_ep.id))
  1095. continue;
  1096. fwnode_handle_put(best_ep);
  1097. best_ep = fwnode_handle_get(ep);
  1098. best_ep_id = fwnode_ep.id;
  1099. }
  1100. return best_ep;
  1101. }
  1102. EXPORT_SYMBOL_GPL(fwnode_graph_get_endpoint_by_id);
  1103. /**
  1104. * fwnode_graph_get_endpoint_count - Count endpoints on a device node
  1105. * @fwnode: The node related to a device
  1106. * @flags: fwnode lookup flags
  1107. * Count endpoints in a device node.
  1108. *
  1109. * If FWNODE_GRAPH_DEVICE_DISABLED flag is specified, also unconnected endpoints
  1110. * and endpoints connected to disabled devices are counted.
  1111. */
  1112. unsigned int fwnode_graph_get_endpoint_count(struct fwnode_handle *fwnode,
  1113. unsigned long flags)
  1114. {
  1115. struct fwnode_handle *ep;
  1116. unsigned int count = 0;
  1117. fwnode_graph_for_each_endpoint(fwnode, ep) {
  1118. if (flags & FWNODE_GRAPH_DEVICE_DISABLED ||
  1119. fwnode_graph_remote_available(ep))
  1120. count++;
  1121. }
  1122. return count;
  1123. }
  1124. EXPORT_SYMBOL_GPL(fwnode_graph_get_endpoint_count);
  1125. /**
  1126. * fwnode_graph_parse_endpoint - parse common endpoint node properties
  1127. * @fwnode: pointer to endpoint fwnode_handle
  1128. * @endpoint: pointer to the fwnode endpoint data structure
  1129. *
  1130. * Parse @fwnode representing a graph endpoint node and store the
  1131. * information in @endpoint. The caller must hold a reference to
  1132. * @fwnode.
  1133. */
  1134. int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
  1135. struct fwnode_endpoint *endpoint)
  1136. {
  1137. memset(endpoint, 0, sizeof(*endpoint));
  1138. return fwnode_call_int_op(fwnode, graph_parse_endpoint, endpoint);
  1139. }
  1140. EXPORT_SYMBOL(fwnode_graph_parse_endpoint);
  1141. const void *device_get_match_data(const struct device *dev)
  1142. {
  1143. return fwnode_call_ptr_op(dev_fwnode(dev), device_get_match_data, dev);
  1144. }
  1145. EXPORT_SYMBOL_GPL(device_get_match_data);
  1146. static unsigned int fwnode_graph_devcon_matches(struct fwnode_handle *fwnode,
  1147. const char *con_id, void *data,
  1148. devcon_match_fn_t match,
  1149. void **matches,
  1150. unsigned int matches_len)
  1151. {
  1152. struct fwnode_handle *node;
  1153. struct fwnode_handle *ep;
  1154. unsigned int count = 0;
  1155. void *ret;
  1156. fwnode_graph_for_each_endpoint(fwnode, ep) {
  1157. if (matches && count >= matches_len) {
  1158. fwnode_handle_put(ep);
  1159. break;
  1160. }
  1161. node = fwnode_graph_get_remote_port_parent(ep);
  1162. if (!fwnode_device_is_available(node)) {
  1163. fwnode_handle_put(node);
  1164. continue;
  1165. }
  1166. ret = match(node, con_id, data);
  1167. fwnode_handle_put(node);
  1168. if (ret) {
  1169. if (matches)
  1170. matches[count] = ret;
  1171. count++;
  1172. }
  1173. }
  1174. return count;
  1175. }
  1176. static unsigned int fwnode_devcon_matches(struct fwnode_handle *fwnode,
  1177. const char *con_id, void *data,
  1178. devcon_match_fn_t match,
  1179. void **matches,
  1180. unsigned int matches_len)
  1181. {
  1182. struct fwnode_handle *node;
  1183. unsigned int count = 0;
  1184. unsigned int i;
  1185. void *ret;
  1186. for (i = 0; ; i++) {
  1187. if (matches && count >= matches_len)
  1188. break;
  1189. node = fwnode_find_reference(fwnode, con_id, i);
  1190. if (IS_ERR(node))
  1191. break;
  1192. ret = match(node, NULL, data);
  1193. fwnode_handle_put(node);
  1194. if (ret) {
  1195. if (matches)
  1196. matches[count] = ret;
  1197. count++;
  1198. }
  1199. }
  1200. return count;
  1201. }
  1202. /**
  1203. * fwnode_connection_find_match - Find connection from a device node
  1204. * @fwnode: Device node with the connection
  1205. * @con_id: Identifier for the connection
  1206. * @data: Data for the match function
  1207. * @match: Function to check and convert the connection description
  1208. *
  1209. * Find a connection with unique identifier @con_id between @fwnode and another
  1210. * device node. @match will be used to convert the connection description to
  1211. * data the caller is expecting to be returned.
  1212. */
  1213. void *fwnode_connection_find_match(struct fwnode_handle *fwnode,
  1214. const char *con_id, void *data,
  1215. devcon_match_fn_t match)
  1216. {
  1217. unsigned int count;
  1218. void *ret;
  1219. if (!fwnode || !match)
  1220. return NULL;
  1221. count = fwnode_graph_devcon_matches(fwnode, con_id, data, match, &ret, 1);
  1222. if (count)
  1223. return ret;
  1224. count = fwnode_devcon_matches(fwnode, con_id, data, match, &ret, 1);
  1225. return count ? ret : NULL;
  1226. }
  1227. EXPORT_SYMBOL_GPL(fwnode_connection_find_match);
  1228. /**
  1229. * fwnode_connection_find_matches - Find connections from a device node
  1230. * @fwnode: Device node with the connection
  1231. * @con_id: Identifier for the connection
  1232. * @data: Data for the match function
  1233. * @match: Function to check and convert the connection description
  1234. * @matches: (Optional) array of pointers to fill with matches
  1235. * @matches_len: Length of @matches
  1236. *
  1237. * Find up to @matches_len connections with unique identifier @con_id between
  1238. * @fwnode and other device nodes. @match will be used to convert the
  1239. * connection description to data the caller is expecting to be returned
  1240. * through the @matches array.
  1241. *
  1242. * If @matches is %NULL @matches_len is ignored and the total number of resolved
  1243. * matches is returned.
  1244. *
  1245. * Return: Number of matches resolved, or negative errno.
  1246. */
  1247. int fwnode_connection_find_matches(struct fwnode_handle *fwnode,
  1248. const char *con_id, void *data,
  1249. devcon_match_fn_t match,
  1250. void **matches, unsigned int matches_len)
  1251. {
  1252. unsigned int count_graph;
  1253. unsigned int count_ref;
  1254. if (!fwnode || !match)
  1255. return -EINVAL;
  1256. count_graph = fwnode_graph_devcon_matches(fwnode, con_id, data, match,
  1257. matches, matches_len);
  1258. if (matches) {
  1259. matches += count_graph;
  1260. matches_len -= count_graph;
  1261. }
  1262. count_ref = fwnode_devcon_matches(fwnode, con_id, data, match,
  1263. matches, matches_len);
  1264. return count_graph + count_ref;
  1265. }
  1266. EXPORT_SYMBOL_GPL(fwnode_connection_find_matches);