property.c 46 KB

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