property.c 42 KB

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