property.c 41 KB

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