property.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417
  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 <rafael.j.wysocki@intel.com>
  7. * Mika Westerberg <mika.westerberg@linux.intel.com>
  8. */
  9. #include <linux/device.h>
  10. #include <linux/err.h>
  11. #include <linux/export.h>
  12. #include <linux/kconfig.h>
  13. #include <linux/of.h>
  14. #include <linux/property.h>
  15. #include <linux/phy.h>
  16. #include <linux/slab.h>
  17. #include <linux/string.h>
  18. #include <linux/types.h>
  19. struct fwnode_handle *__dev_fwnode(struct device *dev)
  20. {
  21. return IS_ENABLED(CONFIG_OF) && dev->of_node ?
  22. of_fwnode_handle(dev->of_node) : dev->fwnode;
  23. }
  24. EXPORT_SYMBOL_GPL(__dev_fwnode);
  25. const struct fwnode_handle *__dev_fwnode_const(const struct device *dev)
  26. {
  27. return IS_ENABLED(CONFIG_OF) && dev->of_node ?
  28. of_fwnode_handle(dev->of_node) : dev->fwnode;
  29. }
  30. EXPORT_SYMBOL_GPL(__dev_fwnode_const);
  31. /**
  32. * device_property_present - check if a property of a device is present
  33. * @dev: Device whose property is being checked
  34. * @propname: Name of the property
  35. *
  36. * Check if property @propname is present in the device firmware description.
  37. *
  38. * Return: true if property @propname is present. Otherwise, returns false.
  39. */
  40. bool device_property_present(const struct device *dev, const char *propname)
  41. {
  42. return fwnode_property_present(dev_fwnode(dev), propname);
  43. }
  44. EXPORT_SYMBOL_GPL(device_property_present);
  45. /**
  46. * fwnode_property_present - check if a property of a firmware node is present
  47. * @fwnode: Firmware node whose property to check
  48. * @propname: Name of the property
  49. *
  50. * Return: true if property @propname is present. Otherwise, returns false.
  51. */
  52. bool fwnode_property_present(const struct fwnode_handle *fwnode,
  53. const char *propname)
  54. {
  55. bool ret;
  56. if (IS_ERR_OR_NULL(fwnode))
  57. return false;
  58. ret = fwnode_call_bool_op(fwnode, property_present, propname);
  59. if (ret)
  60. return ret;
  61. return fwnode_call_bool_op(fwnode->secondary, property_present, propname);
  62. }
  63. EXPORT_SYMBOL_GPL(fwnode_property_present);
  64. /**
  65. * device_property_read_u8_array - return a u8 array property of a device
  66. * @dev: Device to get the property of
  67. * @propname: Name of the property
  68. * @val: The values are stored here or %NULL to return the number of values
  69. * @nval: Size of the @val array
  70. *
  71. * Function reads an array of u8 properties with @propname from the device
  72. * firmware description and stores them to @val if found.
  73. *
  74. * It's recommended to call device_property_count_u8() instead of calling
  75. * this function with @val equals %NULL and @nval equals 0.
  76. *
  77. * Return: number of values if @val was %NULL,
  78. * %0 if the property was found (success),
  79. * %-EINVAL if given arguments are not valid,
  80. * %-ENODATA if the property does not have a value,
  81. * %-EPROTO if the property is not an array of numbers,
  82. * %-EOVERFLOW if the size of the property is not as expected.
  83. * %-ENXIO if no suitable firmware interface is present.
  84. */
  85. int device_property_read_u8_array(const struct device *dev, const char *propname,
  86. u8 *val, size_t nval)
  87. {
  88. return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
  89. }
  90. EXPORT_SYMBOL_GPL(device_property_read_u8_array);
  91. /**
  92. * device_property_read_u16_array - return a u16 array property of a device
  93. * @dev: Device to get the property of
  94. * @propname: Name of the property
  95. * @val: The values are stored here or %NULL to return the number of values
  96. * @nval: Size of the @val array
  97. *
  98. * Function reads an array of u16 properties with @propname from the device
  99. * firmware description and stores them to @val if found.
  100. *
  101. * It's recommended to call device_property_count_u16() instead of calling
  102. * this function with @val equals %NULL and @nval equals 0.
  103. *
  104. * Return: number of values if @val was %NULL,
  105. * %0 if the property was found (success),
  106. * %-EINVAL if given arguments are not valid,
  107. * %-ENODATA if the property does not have a value,
  108. * %-EPROTO if the property is not an array of numbers,
  109. * %-EOVERFLOW if the size of the property is not as expected.
  110. * %-ENXIO if no suitable firmware interface is present.
  111. */
  112. int device_property_read_u16_array(const struct device *dev, const char *propname,
  113. u16 *val, size_t nval)
  114. {
  115. return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
  116. }
  117. EXPORT_SYMBOL_GPL(device_property_read_u16_array);
  118. /**
  119. * device_property_read_u32_array - return a u32 array property of a device
  120. * @dev: Device to get the property of
  121. * @propname: Name of the property
  122. * @val: The values are stored here or %NULL to return the number of values
  123. * @nval: Size of the @val array
  124. *
  125. * Function reads an array of u32 properties with @propname from the device
  126. * firmware description and stores them to @val if found.
  127. *
  128. * It's recommended to call device_property_count_u32() instead of calling
  129. * this function with @val equals %NULL and @nval equals 0.
  130. *
  131. * Return: number of values if @val was %NULL,
  132. * %0 if the property was found (success),
  133. * %-EINVAL if given arguments are not valid,
  134. * %-ENODATA if the property does not have a value,
  135. * %-EPROTO if the property is not an array of numbers,
  136. * %-EOVERFLOW if the size of the property is not as expected.
  137. * %-ENXIO if no suitable firmware interface is present.
  138. */
  139. int device_property_read_u32_array(const struct device *dev, const char *propname,
  140. u32 *val, size_t nval)
  141. {
  142. return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
  143. }
  144. EXPORT_SYMBOL_GPL(device_property_read_u32_array);
  145. /**
  146. * device_property_read_u64_array - return a u64 array property of a device
  147. * @dev: Device to get the property of
  148. * @propname: Name of the property
  149. * @val: The values are stored here or %NULL to return the number of values
  150. * @nval: Size of the @val array
  151. *
  152. * Function reads an array of u64 properties with @propname from the device
  153. * firmware description and stores them to @val if found.
  154. *
  155. * It's recommended to call device_property_count_u64() instead of calling
  156. * this function with @val equals %NULL and @nval equals 0.
  157. *
  158. * Return: number of values if @val was %NULL,
  159. * %0 if the property was found (success),
  160. * %-EINVAL if given arguments are not valid,
  161. * %-ENODATA if the property does not have a value,
  162. * %-EPROTO if the property is not an array of numbers,
  163. * %-EOVERFLOW if the size of the property is not as expected.
  164. * %-ENXIO if no suitable firmware interface is present.
  165. */
  166. int device_property_read_u64_array(const struct device *dev, const char *propname,
  167. u64 *val, size_t nval)
  168. {
  169. return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
  170. }
  171. EXPORT_SYMBOL_GPL(device_property_read_u64_array);
  172. /**
  173. * device_property_read_string_array - return a string array property of device
  174. * @dev: Device to get the property of
  175. * @propname: Name of the property
  176. * @val: The values are stored here or %NULL to return the number of values
  177. * @nval: Size of the @val array
  178. *
  179. * Function reads an array of string properties with @propname from the device
  180. * firmware description and stores them to @val if found.
  181. *
  182. * It's recommended to call device_property_string_array_count() instead of calling
  183. * this function with @val equals %NULL and @nval equals 0.
  184. *
  185. * Return: number of values read on success if @val is non-NULL,
  186. * number of values available on success if @val is NULL,
  187. * %-EINVAL if given arguments are not valid,
  188. * %-ENODATA if the property does not have a value,
  189. * %-EPROTO or %-EILSEQ if the property is not an array of strings,
  190. * %-EOVERFLOW if the size of the property is not as expected.
  191. * %-ENXIO if no suitable firmware interface is present.
  192. */
  193. int device_property_read_string_array(const struct device *dev, const char *propname,
  194. const char **val, size_t nval)
  195. {
  196. return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
  197. }
  198. EXPORT_SYMBOL_GPL(device_property_read_string_array);
  199. /**
  200. * device_property_read_string - return a string property of a device
  201. * @dev: Device to get the property of
  202. * @propname: Name of the property
  203. * @val: The value is stored here
  204. *
  205. * Function reads property @propname from the device firmware description and
  206. * stores the value into @val if found. The value is checked to be a string.
  207. *
  208. * Return: %0 if the property was found (success),
  209. * %-EINVAL if given arguments are not valid,
  210. * %-ENODATA if the property does not have a value,
  211. * %-EPROTO or %-EILSEQ if the property type is not a string.
  212. * %-ENXIO if no suitable firmware interface is present.
  213. */
  214. int device_property_read_string(const struct device *dev, const char *propname,
  215. const char **val)
  216. {
  217. return fwnode_property_read_string(dev_fwnode(dev), propname, val);
  218. }
  219. EXPORT_SYMBOL_GPL(device_property_read_string);
  220. /**
  221. * device_property_match_string - find a string in an array and return index
  222. * @dev: Device to get the property of
  223. * @propname: Name of the property holding the array
  224. * @string: String to look for
  225. *
  226. * Find a given string in a string array and if it is found return the
  227. * index back.
  228. *
  229. * Return: index, starting from %0, if the property was found (success),
  230. * %-EINVAL if given arguments are not valid,
  231. * %-ENODATA if the property does not have a value,
  232. * %-EPROTO if the property is not an array of strings,
  233. * %-ENXIO if no suitable firmware interface is present.
  234. */
  235. int device_property_match_string(const struct device *dev, const char *propname,
  236. const char *string)
  237. {
  238. return fwnode_property_match_string(dev_fwnode(dev), propname, string);
  239. }
  240. EXPORT_SYMBOL_GPL(device_property_match_string);
  241. static int fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
  242. const char *propname,
  243. unsigned int elem_size, void *val,
  244. size_t nval)
  245. {
  246. int ret;
  247. if (IS_ERR_OR_NULL(fwnode))
  248. return -EINVAL;
  249. ret = fwnode_call_int_op(fwnode, property_read_int_array, propname,
  250. elem_size, val, nval);
  251. if (ret != -EINVAL)
  252. return ret;
  253. return fwnode_call_int_op(fwnode->secondary, property_read_int_array, propname,
  254. elem_size, val, nval);
  255. }
  256. /**
  257. * fwnode_property_read_u8_array - return a u8 array property of firmware node
  258. * @fwnode: Firmware node to get the property of
  259. * @propname: Name of the property
  260. * @val: The values are stored here or %NULL to return the number of values
  261. * @nval: Size of the @val array
  262. *
  263. * Read an array of u8 properties with @propname from @fwnode and stores them to
  264. * @val if found.
  265. *
  266. * It's recommended to call fwnode_property_count_u8() instead of calling
  267. * this function with @val equals %NULL and @nval equals 0.
  268. *
  269. * Return: number of values if @val was %NULL,
  270. * %0 if the property was found (success),
  271. * %-EINVAL if given arguments are not valid,
  272. * %-ENODATA if the property does not have a value,
  273. * %-EPROTO if the property is not an array of numbers,
  274. * %-EOVERFLOW if the size of the property is not as expected,
  275. * %-ENXIO if no suitable firmware interface is present.
  276. */
  277. int fwnode_property_read_u8_array(const struct fwnode_handle *fwnode,
  278. const char *propname, u8 *val, size_t nval)
  279. {
  280. return fwnode_property_read_int_array(fwnode, propname, sizeof(u8),
  281. val, nval);
  282. }
  283. EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
  284. /**
  285. * fwnode_property_read_u16_array - return a u16 array property of firmware node
  286. * @fwnode: Firmware node to get the property of
  287. * @propname: Name of the property
  288. * @val: The values are stored here or %NULL to return the number of values
  289. * @nval: Size of the @val array
  290. *
  291. * Read an array of u16 properties with @propname from @fwnode and store them to
  292. * @val if found.
  293. *
  294. * It's recommended to call fwnode_property_count_u16() instead of calling
  295. * this function with @val equals %NULL and @nval equals 0.
  296. *
  297. * Return: number of values if @val was %NULL,
  298. * %0 if the property was found (success),
  299. * %-EINVAL if given arguments are not valid,
  300. * %-ENODATA if the property does not have a value,
  301. * %-EPROTO if the property is not an array of numbers,
  302. * %-EOVERFLOW if the size of the property is not as expected,
  303. * %-ENXIO if no suitable firmware interface is present.
  304. */
  305. int fwnode_property_read_u16_array(const struct fwnode_handle *fwnode,
  306. const char *propname, u16 *val, size_t nval)
  307. {
  308. return fwnode_property_read_int_array(fwnode, propname, sizeof(u16),
  309. val, nval);
  310. }
  311. EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
  312. /**
  313. * fwnode_property_read_u32_array - return a u32 array property of firmware node
  314. * @fwnode: Firmware node to get the property of
  315. * @propname: Name of the property
  316. * @val: The values are stored here or %NULL to return the number of values
  317. * @nval: Size of the @val array
  318. *
  319. * Read an array of u32 properties with @propname from @fwnode store them to
  320. * @val if found.
  321. *
  322. * It's recommended to call fwnode_property_count_u32() instead of calling
  323. * this function with @val equals %NULL and @nval equals 0.
  324. *
  325. * Return: number of values if @val was %NULL,
  326. * %0 if the property was found (success),
  327. * %-EINVAL if given arguments are not valid,
  328. * %-ENODATA if the property does not have a value,
  329. * %-EPROTO if the property is not an array of numbers,
  330. * %-EOVERFLOW if the size of the property is not as expected,
  331. * %-ENXIO if no suitable firmware interface is present.
  332. */
  333. int fwnode_property_read_u32_array(const struct fwnode_handle *fwnode,
  334. const char *propname, u32 *val, size_t nval)
  335. {
  336. return fwnode_property_read_int_array(fwnode, propname, sizeof(u32),
  337. val, nval);
  338. }
  339. EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
  340. /**
  341. * fwnode_property_read_u64_array - return a u64 array property firmware node
  342. * @fwnode: Firmware node to get the property of
  343. * @propname: Name of the property
  344. * @val: The values are stored here or %NULL to return the number of values
  345. * @nval: Size of the @val array
  346. *
  347. * Read an array of u64 properties with @propname from @fwnode and store them to
  348. * @val if found.
  349. *
  350. * It's recommended to call fwnode_property_count_u64() instead of calling
  351. * this function with @val equals %NULL and @nval equals 0.
  352. *
  353. * Return: number of values if @val was %NULL,
  354. * %0 if the property was found (success),
  355. * %-EINVAL if given arguments are not valid,
  356. * %-ENODATA if the property does not have a value,
  357. * %-EPROTO if the property is not an array of numbers,
  358. * %-EOVERFLOW if the size of the property is not as expected,
  359. * %-ENXIO if no suitable firmware interface is present.
  360. */
  361. int fwnode_property_read_u64_array(const struct fwnode_handle *fwnode,
  362. const char *propname, u64 *val, size_t nval)
  363. {
  364. return fwnode_property_read_int_array(fwnode, propname, sizeof(u64),
  365. val, nval);
  366. }
  367. EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
  368. /**
  369. * fwnode_property_read_string_array - return string array property of a node
  370. * @fwnode: Firmware node to get the property of
  371. * @propname: Name of the property
  372. * @val: The values are stored here or %NULL to return the number of values
  373. * @nval: Size of the @val array
  374. *
  375. * Read an string list property @propname from the given firmware node and store
  376. * them to @val if found.
  377. *
  378. * It's recommended to call fwnode_property_string_array_count() instead of calling
  379. * this function with @val equals %NULL and @nval equals 0.
  380. *
  381. * Return: number of values read on success if @val is non-NULL,
  382. * number of values available on success if @val is NULL,
  383. * %-EINVAL if given arguments are not valid,
  384. * %-ENODATA if the property does not have a value,
  385. * %-EPROTO or %-EILSEQ if the property is not an array of strings,
  386. * %-EOVERFLOW if the size of the property is not as expected,
  387. * %-ENXIO if no suitable firmware interface is present.
  388. */
  389. int fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
  390. const char *propname, const char **val,
  391. size_t nval)
  392. {
  393. int ret;
  394. if (IS_ERR_OR_NULL(fwnode))
  395. return -EINVAL;
  396. ret = fwnode_call_int_op(fwnode, property_read_string_array, propname,
  397. val, nval);
  398. if (ret != -EINVAL)
  399. return ret;
  400. return fwnode_call_int_op(fwnode->secondary, property_read_string_array, propname,
  401. val, nval);
  402. }
  403. EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
  404. /**
  405. * fwnode_property_read_string - return a string property of a firmware node
  406. * @fwnode: Firmware node to get the property of
  407. * @propname: Name of the property
  408. * @val: The value is stored here
  409. *
  410. * Read property @propname from the given firmware node and store the value into
  411. * @val if found. The value is checked to be a string.
  412. *
  413. * Return: %0 if the property was found (success),
  414. * %-EINVAL if given arguments are not valid,
  415. * %-ENODATA if the property does not have a value,
  416. * %-EPROTO or %-EILSEQ if the property is not a string,
  417. * %-ENXIO if no suitable firmware interface is present.
  418. */
  419. int fwnode_property_read_string(const struct fwnode_handle *fwnode,
  420. const char *propname, const char **val)
  421. {
  422. int ret = fwnode_property_read_string_array(fwnode, propname, val, 1);
  423. return ret < 0 ? ret : 0;
  424. }
  425. EXPORT_SYMBOL_GPL(fwnode_property_read_string);
  426. /**
  427. * fwnode_property_match_string - find a string in an array and return index
  428. * @fwnode: Firmware node to get the property of
  429. * @propname: Name of the property holding the array
  430. * @string: String to look for
  431. *
  432. * Find a given string in a string array and if it is found return the
  433. * index back.
  434. *
  435. * Return: index, starting from %0, if the property was found (success),
  436. * %-EINVAL if given arguments are not valid,
  437. * %-ENODATA if the property does not have a value,
  438. * %-EPROTO if the property is not an array of strings,
  439. * %-ENXIO if no suitable firmware interface is present.
  440. */
  441. int fwnode_property_match_string(const struct fwnode_handle *fwnode,
  442. const char *propname, const char *string)
  443. {
  444. const char **values;
  445. int nval, ret;
  446. nval = fwnode_property_string_array_count(fwnode, propname);
  447. if (nval < 0)
  448. return nval;
  449. if (nval == 0)
  450. return -ENODATA;
  451. values = kcalloc(nval, sizeof(*values), GFP_KERNEL);
  452. if (!values)
  453. return -ENOMEM;
  454. ret = fwnode_property_read_string_array(fwnode, propname, values, nval);
  455. if (ret < 0)
  456. goto out_free;
  457. ret = match_string(values, nval, string);
  458. if (ret < 0)
  459. ret = -ENODATA;
  460. out_free:
  461. kfree(values);
  462. return ret;
  463. }
  464. EXPORT_SYMBOL_GPL(fwnode_property_match_string);
  465. /**
  466. * fwnode_property_match_property_string - find a property string value in an array and return index
  467. * @fwnode: Firmware node to get the property of
  468. * @propname: Name of the property holding the string value
  469. * @array: String array to search in
  470. * @n: Size of the @array
  471. *
  472. * Find a property string value in a given @array and if it is found return
  473. * the index back.
  474. *
  475. * Return: index, starting from %0, if the string value was found in the @array (success),
  476. * %-ENOENT when the string value was not found in the @array,
  477. * %-EINVAL if given arguments are not valid,
  478. * %-ENODATA if the property does not have a value,
  479. * %-EPROTO or %-EILSEQ if the property is not a string,
  480. * %-ENXIO if no suitable firmware interface is present.
  481. */
  482. int fwnode_property_match_property_string(const struct fwnode_handle *fwnode,
  483. const char *propname, const char * const *array, size_t n)
  484. {
  485. const char *string;
  486. int ret;
  487. ret = fwnode_property_read_string(fwnode, propname, &string);
  488. if (ret)
  489. return ret;
  490. ret = match_string(array, n, string);
  491. if (ret < 0)
  492. ret = -ENOENT;
  493. return ret;
  494. }
  495. EXPORT_SYMBOL_GPL(fwnode_property_match_property_string);
  496. /**
  497. * fwnode_property_get_reference_args() - Find a reference with arguments
  498. * @fwnode: Firmware node where to look for the reference
  499. * @prop: The name of the property
  500. * @nargs_prop: The name of the property telling the number of
  501. * arguments in the referred node. NULL if @nargs is known,
  502. * otherwise @nargs is ignored. Only relevant on OF.
  503. * @nargs: Number of arguments. Ignored if @nargs_prop is non-NULL.
  504. * @index: Index of the reference, from zero onwards.
  505. * @args: Result structure with reference and integer arguments.
  506. * May be NULL.
  507. *
  508. * Obtain a reference based on a named property in an fwnode, with
  509. * integer arguments.
  510. *
  511. * The caller is responsible for calling fwnode_handle_put() on the returned
  512. * @args->fwnode pointer.
  513. *
  514. * Return: %0 on success
  515. * %-ENOENT when the index is out of bounds, the index has an empty
  516. * reference or the property was not found
  517. * %-EINVAL on parse error
  518. */
  519. int fwnode_property_get_reference_args(const struct fwnode_handle *fwnode,
  520. const char *prop, const char *nargs_prop,
  521. unsigned int nargs, unsigned int index,
  522. struct fwnode_reference_args *args)
  523. {
  524. int ret;
  525. if (IS_ERR_OR_NULL(fwnode))
  526. return -ENOENT;
  527. ret = fwnode_call_int_op(fwnode, get_reference_args, prop, nargs_prop,
  528. nargs, index, args);
  529. if (ret == 0)
  530. return ret;
  531. if (IS_ERR_OR_NULL(fwnode->secondary))
  532. return ret;
  533. return fwnode_call_int_op(fwnode->secondary, get_reference_args, prop, nargs_prop,
  534. nargs, index, args);
  535. }
  536. EXPORT_SYMBOL_GPL(fwnode_property_get_reference_args);
  537. /**
  538. * fwnode_find_reference - Find named reference to a fwnode_handle
  539. * @fwnode: Firmware node where to look for the reference
  540. * @name: The name of the reference
  541. * @index: Index of the reference
  542. *
  543. * @index can be used when the named reference holds a table of references.
  544. *
  545. * The caller is responsible for calling fwnode_handle_put() on the returned
  546. * fwnode pointer.
  547. *
  548. * Return: a pointer to the reference fwnode, when found. Otherwise,
  549. * returns an error pointer.
  550. */
  551. struct fwnode_handle *fwnode_find_reference(const struct fwnode_handle *fwnode,
  552. const char *name,
  553. unsigned int index)
  554. {
  555. struct fwnode_reference_args args;
  556. int ret;
  557. ret = fwnode_property_get_reference_args(fwnode, name, NULL, 0, index,
  558. &args);
  559. return ret ? ERR_PTR(ret) : args.fwnode;
  560. }
  561. EXPORT_SYMBOL_GPL(fwnode_find_reference);
  562. /**
  563. * fwnode_get_name - Return the name of a node
  564. * @fwnode: The firmware node
  565. *
  566. * Return: a pointer to the node name, or %NULL.
  567. */
  568. const char *fwnode_get_name(const struct fwnode_handle *fwnode)
  569. {
  570. return fwnode_call_ptr_op(fwnode, get_name);
  571. }
  572. EXPORT_SYMBOL_GPL(fwnode_get_name);
  573. /**
  574. * fwnode_get_name_prefix - Return the prefix of node for printing purposes
  575. * @fwnode: The firmware node
  576. *
  577. * Return: the prefix of a node, intended to be printed right before the node.
  578. * The prefix works also as a separator between the nodes.
  579. */
  580. const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
  581. {
  582. return fwnode_call_ptr_op(fwnode, get_name_prefix);
  583. }
  584. /**
  585. * fwnode_name_eq - Return true if node name is equal
  586. * @fwnode: The firmware node
  587. * @name: The name to which to compare the node name
  588. *
  589. * Compare the name provided as an argument to the name of the node, stopping
  590. * the comparison at either NUL or '@' character, whichever comes first. This
  591. * function is generally used for comparing node names while ignoring the
  592. * possible unit address of the node.
  593. *
  594. * Return: true if the node name matches with the name provided in the @name
  595. * argument, false otherwise.
  596. */
  597. bool fwnode_name_eq(const struct fwnode_handle *fwnode, const char *name)
  598. {
  599. const char *node_name;
  600. ptrdiff_t len;
  601. node_name = fwnode_get_name(fwnode);
  602. if (!node_name)
  603. return false;
  604. len = strchrnul(node_name, '@') - node_name;
  605. return str_has_prefix(node_name, name) == len;
  606. }
  607. EXPORT_SYMBOL_GPL(fwnode_name_eq);
  608. /**
  609. * fwnode_get_parent - Return parent firwmare node
  610. * @fwnode: Firmware whose parent is retrieved
  611. *
  612. * The caller is responsible for calling fwnode_handle_put() on the returned
  613. * fwnode pointer.
  614. *
  615. * Return: parent firmware node of the given node if possible or %NULL if no
  616. * parent was available.
  617. */
  618. struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode)
  619. {
  620. return fwnode_call_ptr_op(fwnode, get_parent);
  621. }
  622. EXPORT_SYMBOL_GPL(fwnode_get_parent);
  623. /**
  624. * fwnode_get_next_parent - Iterate to the node's parent
  625. * @fwnode: Firmware whose parent is retrieved
  626. *
  627. * This is like fwnode_get_parent() except that it drops the refcount
  628. * on the passed node, making it suitable for iterating through a
  629. * node's parents.
  630. *
  631. * The caller is responsible for calling fwnode_handle_put() on the returned
  632. * fwnode pointer. Note that this function also puts a reference to @fwnode
  633. * unconditionally.
  634. *
  635. * Return: parent firmware node of the given node if possible or %NULL if no
  636. * parent was available.
  637. */
  638. struct fwnode_handle *fwnode_get_next_parent(struct fwnode_handle *fwnode)
  639. {
  640. struct fwnode_handle *parent = fwnode_get_parent(fwnode);
  641. fwnode_handle_put(fwnode);
  642. return parent;
  643. }
  644. EXPORT_SYMBOL_GPL(fwnode_get_next_parent);
  645. /**
  646. * fwnode_count_parents - Return the number of parents a node has
  647. * @fwnode: The node the parents of which are to be counted
  648. *
  649. * Return: the number of parents a node has.
  650. */
  651. unsigned int fwnode_count_parents(const struct fwnode_handle *fwnode)
  652. {
  653. struct fwnode_handle *parent;
  654. unsigned int count = 0;
  655. fwnode_for_each_parent_node(fwnode, parent)
  656. count++;
  657. return count;
  658. }
  659. EXPORT_SYMBOL_GPL(fwnode_count_parents);
  660. /**
  661. * fwnode_get_nth_parent - Return an nth parent of a node
  662. * @fwnode: The node the parent of which is requested
  663. * @depth: Distance of the parent from the node
  664. *
  665. * The caller is responsible for calling fwnode_handle_put() on the returned
  666. * fwnode pointer.
  667. *
  668. * Return: the nth parent of a node. If there is no parent at the requested
  669. * @depth, %NULL is returned. If @depth is 0, the functionality is equivalent to
  670. * fwnode_handle_get(). For @depth == 1, it is fwnode_get_parent() and so on.
  671. */
  672. struct fwnode_handle *fwnode_get_nth_parent(struct fwnode_handle *fwnode,
  673. unsigned int depth)
  674. {
  675. struct fwnode_handle *parent;
  676. if (depth == 0)
  677. return fwnode_handle_get(fwnode);
  678. fwnode_for_each_parent_node(fwnode, parent) {
  679. if (--depth == 0)
  680. return parent;
  681. }
  682. return NULL;
  683. }
  684. EXPORT_SYMBOL_GPL(fwnode_get_nth_parent);
  685. /**
  686. * fwnode_get_next_child_node - Return the next child node handle for a node
  687. * @fwnode: Firmware node to find the next child node for.
  688. * @child: Handle to one of the node's child nodes or a %NULL handle.
  689. *
  690. * The caller is responsible for calling fwnode_handle_put() on the returned
  691. * fwnode pointer. Note that this function also puts a reference to @child
  692. * unconditionally.
  693. */
  694. struct fwnode_handle *
  695. fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
  696. struct fwnode_handle *child)
  697. {
  698. return fwnode_call_ptr_op(fwnode, get_next_child_node, child);
  699. }
  700. EXPORT_SYMBOL_GPL(fwnode_get_next_child_node);
  701. /**
  702. * fwnode_get_next_available_child_node - Return the next available child node handle for a node
  703. * @fwnode: Firmware node to find the next child node for.
  704. * @child: Handle to one of the node's child nodes or a %NULL handle.
  705. *
  706. * The caller is responsible for calling fwnode_handle_put() on the returned
  707. * fwnode pointer. Note that this function also puts a reference to @child
  708. * unconditionally.
  709. */
  710. struct fwnode_handle *
  711. fwnode_get_next_available_child_node(const struct fwnode_handle *fwnode,
  712. struct fwnode_handle *child)
  713. {
  714. struct fwnode_handle *next_child = child;
  715. if (IS_ERR_OR_NULL(fwnode))
  716. return NULL;
  717. do {
  718. next_child = fwnode_get_next_child_node(fwnode, next_child);
  719. if (!next_child)
  720. return NULL;
  721. } while (!fwnode_device_is_available(next_child));
  722. return next_child;
  723. }
  724. EXPORT_SYMBOL_GPL(fwnode_get_next_available_child_node);
  725. /**
  726. * device_get_next_child_node - Return the next child node handle for a device
  727. * @dev: Device to find the next child node for.
  728. * @child: Handle to one of the device's child nodes or a %NULL handle.
  729. *
  730. * The caller is responsible for calling fwnode_handle_put() on the returned
  731. * fwnode pointer. Note that this function also puts a reference to @child
  732. * unconditionally.
  733. */
  734. struct fwnode_handle *device_get_next_child_node(const struct device *dev,
  735. struct fwnode_handle *child)
  736. {
  737. const struct fwnode_handle *fwnode = dev_fwnode(dev);
  738. struct fwnode_handle *next;
  739. if (IS_ERR_OR_NULL(fwnode))
  740. return NULL;
  741. /* Try to find a child in primary fwnode */
  742. next = fwnode_get_next_child_node(fwnode, child);
  743. if (next)
  744. return next;
  745. /* When no more children in primary, continue with secondary */
  746. return fwnode_get_next_child_node(fwnode->secondary, child);
  747. }
  748. EXPORT_SYMBOL_GPL(device_get_next_child_node);
  749. /**
  750. * fwnode_get_named_child_node - Return first matching named child node handle
  751. * @fwnode: Firmware node to find the named child node for.
  752. * @childname: String to match child node name against.
  753. *
  754. * The caller is responsible for calling fwnode_handle_put() on the returned
  755. * fwnode pointer.
  756. */
  757. struct fwnode_handle *
  758. fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
  759. const char *childname)
  760. {
  761. return fwnode_call_ptr_op(fwnode, get_named_child_node, childname);
  762. }
  763. EXPORT_SYMBOL_GPL(fwnode_get_named_child_node);
  764. /**
  765. * device_get_named_child_node - Return first matching named child node handle
  766. * @dev: Device to find the named child node for.
  767. * @childname: String to match child node name against.
  768. *
  769. * The caller is responsible for calling fwnode_handle_put() on the returned
  770. * fwnode pointer.
  771. */
  772. struct fwnode_handle *device_get_named_child_node(const struct device *dev,
  773. const char *childname)
  774. {
  775. return fwnode_get_named_child_node(dev_fwnode(dev), childname);
  776. }
  777. EXPORT_SYMBOL_GPL(device_get_named_child_node);
  778. /**
  779. * fwnode_handle_get - Obtain a reference to a device node
  780. * @fwnode: Pointer to the device node to obtain the reference to.
  781. *
  782. * The caller is responsible for calling fwnode_handle_put() on the returned
  783. * fwnode pointer.
  784. *
  785. * Return: the fwnode handle.
  786. */
  787. struct fwnode_handle *fwnode_handle_get(struct fwnode_handle *fwnode)
  788. {
  789. if (!fwnode_has_op(fwnode, get))
  790. return fwnode;
  791. return fwnode_call_ptr_op(fwnode, get);
  792. }
  793. EXPORT_SYMBOL_GPL(fwnode_handle_get);
  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 count 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(const 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(const 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(const 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(const 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(const 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(const 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(const 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(const 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(const 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);