property.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * property.c - Unified device property interface.
  3. *
  4. * Copyright (C) 2014, Intel Corporation
  5. * Authors: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
  6. * Mika Westerberg <mika.westerberg@linux.intel.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/property.h>
  13. #include <linux/export.h>
  14. #include <linux/acpi.h>
  15. #include <linux/of.h>
  16. /**
  17. * device_property_present - check if a property of a device is present
  18. * @dev: Device whose property is being checked
  19. * @propname: Name of the property
  20. *
  21. * Check if property @propname is present in the device firmware description.
  22. */
  23. bool device_property_present(struct device *dev, const char *propname)
  24. {
  25. if (IS_ENABLED(CONFIG_OF) && dev->of_node)
  26. return of_property_read_bool(dev->of_node, propname);
  27. return !acpi_dev_prop_get(ACPI_COMPANION(dev), propname, NULL);
  28. }
  29. EXPORT_SYMBOL_GPL(device_property_present);
  30. /**
  31. * fwnode_property_present - check if a property of a firmware node is present
  32. * @fwnode: Firmware node whose property to check
  33. * @propname: Name of the property
  34. */
  35. bool fwnode_property_present(struct fwnode_handle *fwnode, const char *propname)
  36. {
  37. if (is_of_node(fwnode))
  38. return of_property_read_bool(of_node(fwnode), propname);
  39. else if (is_acpi_node(fwnode))
  40. return !acpi_dev_prop_get(acpi_node(fwnode), propname, NULL);
  41. return false;
  42. }
  43. EXPORT_SYMBOL_GPL(fwnode_property_present);
  44. #define OF_DEV_PROP_READ_ARRAY(node, propname, type, val, nval) \
  45. (val) ? of_property_read_##type##_array((node), (propname), (val), (nval)) \
  46. : of_property_count_elems_of_size((node), (propname), sizeof(type))
  47. #define DEV_PROP_READ_ARRAY(_dev_, _propname_, _type_, _proptype_, _val_, _nval_) \
  48. IS_ENABLED(CONFIG_OF) && _dev_->of_node ? \
  49. (OF_DEV_PROP_READ_ARRAY(_dev_->of_node, _propname_, _type_, \
  50. _val_, _nval_)) : \
  51. acpi_dev_prop_read(ACPI_COMPANION(_dev_), _propname_, \
  52. _proptype_, _val_, _nval_)
  53. /**
  54. * device_property_read_u8_array - return a u8 array property of a device
  55. * @dev: Device to get the property of
  56. * @propname: Name of the property
  57. * @val: The values are stored here
  58. * @nval: Size of the @val array
  59. *
  60. * Function reads an array of u8 properties with @propname from the device
  61. * firmware description and stores them to @val if found.
  62. *
  63. * Return: %0 if the property was found (success),
  64. * %-EINVAL if given arguments are not valid,
  65. * %-ENODATA if the property does not have a value,
  66. * %-EPROTO if the property is not an array of numbers,
  67. * %-EOVERFLOW if the size of the property is not as expected.
  68. */
  69. int device_property_read_u8_array(struct device *dev, const char *propname,
  70. u8 *val, size_t nval)
  71. {
  72. return DEV_PROP_READ_ARRAY(dev, propname, u8, DEV_PROP_U8, val, nval);
  73. }
  74. EXPORT_SYMBOL_GPL(device_property_read_u8_array);
  75. /**
  76. * device_property_read_u16_array - return a u16 array property of a device
  77. * @dev: Device to get the property of
  78. * @propname: Name of the property
  79. * @val: The values are stored here
  80. * @nval: Size of the @val array
  81. *
  82. * Function reads an array of u16 properties with @propname from the device
  83. * firmware description and stores them to @val if found.
  84. *
  85. * Return: %0 if the property was found (success),
  86. * %-EINVAL if given arguments are not valid,
  87. * %-ENODATA if the property does not have a value,
  88. * %-EPROTO if the property is not an array of numbers,
  89. * %-EOVERFLOW if the size of the property is not as expected.
  90. */
  91. int device_property_read_u16_array(struct device *dev, const char *propname,
  92. u16 *val, size_t nval)
  93. {
  94. return DEV_PROP_READ_ARRAY(dev, propname, u16, DEV_PROP_U16, val, nval);
  95. }
  96. EXPORT_SYMBOL_GPL(device_property_read_u16_array);
  97. /**
  98. * device_property_read_u32_array - return a u32 array property of a device
  99. * @dev: Device to get the property of
  100. * @propname: Name of the property
  101. * @val: The values are stored here
  102. * @nval: Size of the @val array
  103. *
  104. * Function reads an array of u32 properties with @propname from the device
  105. * firmware description and stores them to @val if found.
  106. *
  107. * Return: %0 if the property was found (success),
  108. * %-EINVAL if given arguments are not valid,
  109. * %-ENODATA if the property does not have a value,
  110. * %-EPROTO if the property is not an array of numbers,
  111. * %-EOVERFLOW if the size of the property is not as expected.
  112. */
  113. int device_property_read_u32_array(struct device *dev, const char *propname,
  114. u32 *val, size_t nval)
  115. {
  116. return DEV_PROP_READ_ARRAY(dev, propname, u32, DEV_PROP_U32, val, nval);
  117. }
  118. EXPORT_SYMBOL_GPL(device_property_read_u32_array);
  119. /**
  120. * device_property_read_u64_array - return a u64 array property of a device
  121. * @dev: Device to get the property of
  122. * @propname: Name of the property
  123. * @val: The values are stored here
  124. * @nval: Size of the @val array
  125. *
  126. * Function reads an array of u64 properties with @propname from the device
  127. * firmware description and stores them to @val if found.
  128. *
  129. * Return: %0 if the property was found (success),
  130. * %-EINVAL if given arguments are not valid,
  131. * %-ENODATA if the property does not have a value,
  132. * %-EPROTO if the property is not an array of numbers,
  133. * %-EOVERFLOW if the size of the property is not as expected.
  134. */
  135. int device_property_read_u64_array(struct device *dev, const char *propname,
  136. u64 *val, size_t nval)
  137. {
  138. return DEV_PROP_READ_ARRAY(dev, propname, u64, DEV_PROP_U64, val, nval);
  139. }
  140. EXPORT_SYMBOL_GPL(device_property_read_u64_array);
  141. /**
  142. * device_property_read_string_array - return a string array property of device
  143. * @dev: Device to get the property of
  144. * @propname: Name of the property
  145. * @val: The values are stored here
  146. * @nval: Size of the @val array
  147. *
  148. * Function reads an array of string properties with @propname from the device
  149. * firmware description and stores them to @val if found.
  150. *
  151. * Return: %0 if the property was found (success),
  152. * %-EINVAL if given arguments are not valid,
  153. * %-ENODATA if the property does not have a value,
  154. * %-EPROTO or %-EILSEQ if the property is not an array of strings,
  155. * %-EOVERFLOW if the size of the property is not as expected.
  156. */
  157. int device_property_read_string_array(struct device *dev, const char *propname,
  158. const char **val, size_t nval)
  159. {
  160. return IS_ENABLED(CONFIG_OF) && dev->of_node ?
  161. of_property_read_string_array(dev->of_node, propname, val, nval) :
  162. acpi_dev_prop_read(ACPI_COMPANION(dev), propname,
  163. DEV_PROP_STRING, val, nval);
  164. }
  165. EXPORT_SYMBOL_GPL(device_property_read_string_array);
  166. /**
  167. * device_property_read_string - return a string property of a device
  168. * @dev: Device to get the property of
  169. * @propname: Name of the property
  170. * @val: The value is stored here
  171. *
  172. * Function reads property @propname from the device firmware description and
  173. * stores the value into @val if found. The value is checked to be a string.
  174. *
  175. * Return: %0 if the property was found (success),
  176. * %-EINVAL if given arguments are not valid,
  177. * %-ENODATA if the property does not have a value,
  178. * %-EPROTO or %-EILSEQ if the property type is not a string.
  179. */
  180. int device_property_read_string(struct device *dev, const char *propname,
  181. const char **val)
  182. {
  183. return IS_ENABLED(CONFIG_OF) && dev->of_node ?
  184. of_property_read_string(dev->of_node, propname, val) :
  185. acpi_dev_prop_read(ACPI_COMPANION(dev), propname,
  186. DEV_PROP_STRING, val, 1);
  187. }
  188. EXPORT_SYMBOL_GPL(device_property_read_string);
  189. #define FWNODE_PROP_READ_ARRAY(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
  190. ({ \
  191. int _ret_; \
  192. if (is_of_node(_fwnode_)) \
  193. _ret_ = OF_DEV_PROP_READ_ARRAY(of_node(_fwnode_), _propname_, \
  194. _type_, _val_, _nval_); \
  195. else if (is_acpi_node(_fwnode_)) \
  196. _ret_ = acpi_dev_prop_read(acpi_node(_fwnode_), _propname_, \
  197. _proptype_, _val_, _nval_); \
  198. else \
  199. _ret_ = -ENXIO; \
  200. _ret_; \
  201. })
  202. /**
  203. * fwnode_property_read_u8_array - return a u8 array property of firmware node
  204. * @fwnode: Firmware node to get the property of
  205. * @propname: Name of the property
  206. * @val: The values are stored here
  207. * @nval: Size of the @val array
  208. *
  209. * Read an array of u8 properties with @propname from @fwnode and stores them to
  210. * @val if found.
  211. *
  212. * Return: %0 if the property was found (success),
  213. * %-EINVAL if given arguments are not valid,
  214. * %-ENODATA if the property does not have a value,
  215. * %-EPROTO if the property is not an array of numbers,
  216. * %-EOVERFLOW if the size of the property is not as expected,
  217. * %-ENXIO if no suitable firmware interface is present.
  218. */
  219. int fwnode_property_read_u8_array(struct fwnode_handle *fwnode,
  220. const char *propname, u8 *val, size_t nval)
  221. {
  222. return FWNODE_PROP_READ_ARRAY(fwnode, propname, u8, DEV_PROP_U8,
  223. val, nval);
  224. }
  225. EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
  226. /**
  227. * fwnode_property_read_u16_array - return a u16 array property of firmware node
  228. * @fwnode: Firmware node to get the property of
  229. * @propname: Name of the property
  230. * @val: The values are stored here
  231. * @nval: Size of the @val array
  232. *
  233. * Read an array of u16 properties with @propname from @fwnode and store them to
  234. * @val if found.
  235. *
  236. * Return: %0 if the property was found (success),
  237. * %-EINVAL if given arguments are not valid,
  238. * %-ENODATA if the property does not have a value,
  239. * %-EPROTO if the property is not an array of numbers,
  240. * %-EOVERFLOW if the size of the property is not as expected,
  241. * %-ENXIO if no suitable firmware interface is present.
  242. */
  243. int fwnode_property_read_u16_array(struct fwnode_handle *fwnode,
  244. const char *propname, u16 *val, size_t nval)
  245. {
  246. return FWNODE_PROP_READ_ARRAY(fwnode, propname, u16, DEV_PROP_U16,
  247. val, nval);
  248. }
  249. EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
  250. /**
  251. * fwnode_property_read_u32_array - return a u32 array property of firmware node
  252. * @fwnode: Firmware node to get the property of
  253. * @propname: Name of the property
  254. * @val: The values are stored here
  255. * @nval: Size of the @val array
  256. *
  257. * Read an array of u32 properties with @propname from @fwnode store them to
  258. * @val if found.
  259. *
  260. * Return: %0 if the property was found (success),
  261. * %-EINVAL if given arguments are not valid,
  262. * %-ENODATA if the property does not have a value,
  263. * %-EPROTO if the property is not an array of numbers,
  264. * %-EOVERFLOW if the size of the property is not as expected,
  265. * %-ENXIO if no suitable firmware interface is present.
  266. */
  267. int fwnode_property_read_u32_array(struct fwnode_handle *fwnode,
  268. const char *propname, u32 *val, size_t nval)
  269. {
  270. return FWNODE_PROP_READ_ARRAY(fwnode, propname, u32, DEV_PROP_U32,
  271. val, nval);
  272. }
  273. EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
  274. /**
  275. * fwnode_property_read_u64_array - return a u64 array property firmware node
  276. * @fwnode: Firmware node to get the property of
  277. * @propname: Name of the property
  278. * @val: The values are stored here
  279. * @nval: Size of the @val array
  280. *
  281. * Read an array of u64 properties with @propname from @fwnode and store them to
  282. * @val if found.
  283. *
  284. * Return: %0 if the property was found (success),
  285. * %-EINVAL if given arguments are not valid,
  286. * %-ENODATA if the property does not have a value,
  287. * %-EPROTO if the property is not an array of numbers,
  288. * %-EOVERFLOW if the size of the property is not as expected,
  289. * %-ENXIO if no suitable firmware interface is present.
  290. */
  291. int fwnode_property_read_u64_array(struct fwnode_handle *fwnode,
  292. const char *propname, u64 *val, size_t nval)
  293. {
  294. return FWNODE_PROP_READ_ARRAY(fwnode, propname, u64, DEV_PROP_U64,
  295. val, nval);
  296. }
  297. EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
  298. /**
  299. * fwnode_property_read_string_array - return string array property of a node
  300. * @fwnode: Firmware node to get the property of
  301. * @propname: Name of the property
  302. * @val: The values are stored here
  303. * @nval: Size of the @val array
  304. *
  305. * Read an string list property @propname from the given firmware node and store
  306. * them to @val if found.
  307. *
  308. * Return: %0 if the property was found (success),
  309. * %-EINVAL if given arguments are not valid,
  310. * %-ENODATA if the property does not have a value,
  311. * %-EPROTO if the property is not an array of strings,
  312. * %-EOVERFLOW if the size of the property is not as expected,
  313. * %-ENXIO if no suitable firmware interface is present.
  314. */
  315. int fwnode_property_read_string_array(struct fwnode_handle *fwnode,
  316. const char *propname, const char **val,
  317. size_t nval)
  318. {
  319. if (is_of_node(fwnode))
  320. return of_property_read_string_array(of_node(fwnode), propname,
  321. val, nval);
  322. else if (is_acpi_node(fwnode))
  323. return acpi_dev_prop_read(acpi_node(fwnode), propname,
  324. DEV_PROP_STRING, val, nval);
  325. return -ENXIO;
  326. }
  327. EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
  328. /**
  329. * fwnode_property_read_string - return a string property of a firmware node
  330. * @fwnode: Firmware node to get the property of
  331. * @propname: Name of the property
  332. * @val: The value is stored here
  333. *
  334. * Read property @propname from the given firmware node and store the value into
  335. * @val if found. The value is checked to be a string.
  336. *
  337. * Return: %0 if the property was found (success),
  338. * %-EINVAL if given arguments are not valid,
  339. * %-ENODATA if the property does not have a value,
  340. * %-EPROTO or %-EILSEQ if the property is not a string,
  341. * %-ENXIO if no suitable firmware interface is present.
  342. */
  343. int fwnode_property_read_string(struct fwnode_handle *fwnode,
  344. const char *propname, const char **val)
  345. {
  346. if (is_of_node(fwnode))
  347. return of_property_read_string(of_node(fwnode),propname, val);
  348. else if (is_acpi_node(fwnode))
  349. return acpi_dev_prop_read(acpi_node(fwnode), propname,
  350. DEV_PROP_STRING, val, 1);
  351. return -ENXIO;
  352. }
  353. EXPORT_SYMBOL_GPL(fwnode_property_read_string);
  354. /**
  355. * device_get_next_child_node - Return the next child node handle for a device
  356. * @dev: Device to find the next child node for.
  357. * @child: Handle to one of the device's child nodes or a null handle.
  358. */
  359. struct fwnode_handle *device_get_next_child_node(struct device *dev,
  360. struct fwnode_handle *child)
  361. {
  362. if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
  363. struct device_node *node;
  364. node = of_get_next_available_child(dev->of_node, of_node(child));
  365. if (node)
  366. return &node->fwnode;
  367. } else if (IS_ENABLED(CONFIG_ACPI)) {
  368. struct acpi_device *node;
  369. node = acpi_get_next_child(dev, acpi_node(child));
  370. if (node)
  371. return acpi_fwnode_handle(node);
  372. }
  373. return NULL;
  374. }
  375. EXPORT_SYMBOL_GPL(device_get_next_child_node);
  376. /**
  377. * fwnode_handle_put - Drop reference to a device node
  378. * @fwnode: Pointer to the device node to drop the reference to.
  379. *
  380. * This has to be used when terminating device_for_each_child_node() iteration
  381. * with break or return to prevent stale device node references from being left
  382. * behind.
  383. */
  384. void fwnode_handle_put(struct fwnode_handle *fwnode)
  385. {
  386. if (is_of_node(fwnode))
  387. of_node_put(of_node(fwnode));
  388. }
  389. EXPORT_SYMBOL_GPL(fwnode_handle_put);
  390. /**
  391. * device_get_child_node_count - return the number of child nodes for device
  392. * @dev: Device to cound the child nodes for
  393. */
  394. unsigned int device_get_child_node_count(struct device *dev)
  395. {
  396. struct fwnode_handle *child;
  397. unsigned int count = 0;
  398. device_for_each_child_node(dev, child)
  399. count++;
  400. return count;
  401. }
  402. EXPORT_SYMBOL_GPL(device_get_child_node_count);