property.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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/acpi.h>
  13. #include <linux/export.h>
  14. #include <linux/kernel.h>
  15. #include <linux/of.h>
  16. #include <linux/property.h>
  17. /**
  18. * device_add_property_set - Add a collection of properties to a device object.
  19. * @dev: Device to add properties to.
  20. * @pset: Collection of properties to add.
  21. *
  22. * Associate a collection of device properties represented by @pset with @dev
  23. * as its secondary firmware node.
  24. */
  25. void device_add_property_set(struct device *dev, struct property_set *pset)
  26. {
  27. if (pset)
  28. pset->fwnode.type = FWNODE_PDATA;
  29. set_secondary_fwnode(dev, &pset->fwnode);
  30. }
  31. EXPORT_SYMBOL_GPL(device_add_property_set);
  32. static inline bool is_pset(struct fwnode_handle *fwnode)
  33. {
  34. return fwnode && fwnode->type == FWNODE_PDATA;
  35. }
  36. static inline struct property_set *to_pset(struct fwnode_handle *fwnode)
  37. {
  38. return is_pset(fwnode) ?
  39. container_of(fwnode, struct property_set, fwnode) : NULL;
  40. }
  41. static struct property_entry *pset_prop_get(struct property_set *pset,
  42. const char *name)
  43. {
  44. struct property_entry *prop;
  45. if (!pset || !pset->properties)
  46. return NULL;
  47. for (prop = pset->properties; prop->name; prop++)
  48. if (!strcmp(name, prop->name))
  49. return prop;
  50. return NULL;
  51. }
  52. static int pset_prop_read_array(struct property_set *pset, const char *name,
  53. enum dev_prop_type type, void *val, size_t nval)
  54. {
  55. struct property_entry *prop;
  56. unsigned int item_size;
  57. prop = pset_prop_get(pset, name);
  58. if (!prop)
  59. return -ENODATA;
  60. if (prop->type != type)
  61. return -EPROTO;
  62. if (!val)
  63. return prop->nval;
  64. if (prop->nval < nval)
  65. return -EOVERFLOW;
  66. switch (type) {
  67. case DEV_PROP_U8:
  68. item_size = sizeof(u8);
  69. break;
  70. case DEV_PROP_U16:
  71. item_size = sizeof(u16);
  72. break;
  73. case DEV_PROP_U32:
  74. item_size = sizeof(u32);
  75. break;
  76. case DEV_PROP_U64:
  77. item_size = sizeof(u64);
  78. break;
  79. case DEV_PROP_STRING:
  80. item_size = sizeof(const char *);
  81. break;
  82. default:
  83. return -EINVAL;
  84. }
  85. memcpy(val, prop->value.raw_data, nval * item_size);
  86. return 0;
  87. }
  88. static inline struct fwnode_handle *dev_fwnode(struct device *dev)
  89. {
  90. return IS_ENABLED(CONFIG_OF) && dev->of_node ?
  91. &dev->of_node->fwnode : dev->fwnode;
  92. }
  93. /**
  94. * device_property_present - check if a property of a device is present
  95. * @dev: Device whose property is being checked
  96. * @propname: Name of the property
  97. *
  98. * Check if property @propname is present in the device firmware description.
  99. */
  100. bool device_property_present(struct device *dev, const char *propname)
  101. {
  102. return fwnode_property_present(dev_fwnode(dev), propname);
  103. }
  104. EXPORT_SYMBOL_GPL(device_property_present);
  105. /**
  106. * fwnode_property_present - check if a property of a firmware node is present
  107. * @fwnode: Firmware node whose property to check
  108. * @propname: Name of the property
  109. */
  110. bool fwnode_property_present(struct fwnode_handle *fwnode, const char *propname)
  111. {
  112. if (is_of_node(fwnode))
  113. return of_property_read_bool(of_node(fwnode), propname);
  114. else if (is_acpi_node(fwnode))
  115. return !acpi_dev_prop_get(acpi_node(fwnode), propname, NULL);
  116. return !!pset_prop_get(to_pset(fwnode), propname);
  117. }
  118. EXPORT_SYMBOL_GPL(fwnode_property_present);
  119. /**
  120. * device_property_read_u8_array - return a u8 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 or %NULL to return the number of values
  124. * @nval: Size of the @val array
  125. *
  126. * Function reads an array of u8 properties with @propname from the device
  127. * firmware description and stores them to @val if found.
  128. *
  129. * Return: number of values if @val was %NULL,
  130. * %0 if the property was found (success),
  131. * %-EINVAL if given arguments are not valid,
  132. * %-ENODATA if the property does not have a value,
  133. * %-EPROTO if the property is not an array of numbers,
  134. * %-EOVERFLOW if the size of the property is not as expected.
  135. */
  136. int device_property_read_u8_array(struct device *dev, const char *propname,
  137. u8 *val, size_t nval)
  138. {
  139. return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
  140. }
  141. EXPORT_SYMBOL_GPL(device_property_read_u8_array);
  142. /**
  143. * device_property_read_u16_array - return a u16 array property of a device
  144. * @dev: Device to get the property of
  145. * @propname: Name of the property
  146. * @val: The values are stored here or %NULL to return the number of values
  147. * @nval: Size of the @val array
  148. *
  149. * Function reads an array of u16 properties with @propname from the device
  150. * firmware description and stores them to @val if found.
  151. *
  152. * Return: number of values if @val was %NULL,
  153. * %0 if the property was found (success),
  154. * %-EINVAL if given arguments are not valid,
  155. * %-ENODATA if the property does not have a value,
  156. * %-EPROTO if the property is not an array of numbers,
  157. * %-EOVERFLOW if the size of the property is not as expected.
  158. */
  159. int device_property_read_u16_array(struct device *dev, const char *propname,
  160. u16 *val, size_t nval)
  161. {
  162. return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
  163. }
  164. EXPORT_SYMBOL_GPL(device_property_read_u16_array);
  165. /**
  166. * device_property_read_u32_array - return a u32 array property of a device
  167. * @dev: Device to get the property of
  168. * @propname: Name of the property
  169. * @val: The values are stored here or %NULL to return the number of values
  170. * @nval: Size of the @val array
  171. *
  172. * Function reads an array of u32 properties with @propname from the device
  173. * firmware description and stores them to @val if found.
  174. *
  175. * Return: number of values if @val was %NULL,
  176. * %0 if the property was found (success),
  177. * %-EINVAL if given arguments are not valid,
  178. * %-ENODATA if the property does not have a value,
  179. * %-EPROTO if the property is not an array of numbers,
  180. * %-EOVERFLOW if the size of the property is not as expected.
  181. */
  182. int device_property_read_u32_array(struct device *dev, const char *propname,
  183. u32 *val, size_t nval)
  184. {
  185. return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
  186. }
  187. EXPORT_SYMBOL_GPL(device_property_read_u32_array);
  188. /**
  189. * device_property_read_u64_array - return a u64 array property of a device
  190. * @dev: Device to get the property of
  191. * @propname: Name of the property
  192. * @val: The values are stored here or %NULL to return the number of values
  193. * @nval: Size of the @val array
  194. *
  195. * Function reads an array of u64 properties with @propname from the device
  196. * firmware description and stores them to @val if found.
  197. *
  198. * Return: number of values if @val was %NULL,
  199. * %0 if the property was found (success),
  200. * %-EINVAL if given arguments are not valid,
  201. * %-ENODATA if the property does not have a value,
  202. * %-EPROTO if the property is not an array of numbers,
  203. * %-EOVERFLOW if the size of the property is not as expected.
  204. */
  205. int device_property_read_u64_array(struct device *dev, const char *propname,
  206. u64 *val, size_t nval)
  207. {
  208. return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
  209. }
  210. EXPORT_SYMBOL_GPL(device_property_read_u64_array);
  211. /**
  212. * device_property_read_string_array - return a string array property of device
  213. * @dev: Device to get the property of
  214. * @propname: Name of the property
  215. * @val: The values are stored here or %NULL to return the number of values
  216. * @nval: Size of the @val array
  217. *
  218. * Function reads an array of string properties with @propname from the device
  219. * firmware description and stores them to @val if found.
  220. *
  221. * Return: number of values if @val was %NULL,
  222. * %0 if the property was found (success),
  223. * %-EINVAL if given arguments are not valid,
  224. * %-ENODATA if the property does not have a value,
  225. * %-EPROTO or %-EILSEQ if the property is not an array of strings,
  226. * %-EOVERFLOW if the size of the property is not as expected.
  227. */
  228. int device_property_read_string_array(struct device *dev, const char *propname,
  229. const char **val, size_t nval)
  230. {
  231. return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
  232. }
  233. EXPORT_SYMBOL_GPL(device_property_read_string_array);
  234. /**
  235. * device_property_read_string - return a string property of a device
  236. * @dev: Device to get the property of
  237. * @propname: Name of the property
  238. * @val: The value is stored here
  239. *
  240. * Function reads property @propname from the device firmware description and
  241. * stores the value into @val if found. The value is checked to be a string.
  242. *
  243. * Return: %0 if the property was found (success),
  244. * %-EINVAL if given arguments are not valid,
  245. * %-ENODATA if the property does not have a value,
  246. * %-EPROTO or %-EILSEQ if the property type is not a string.
  247. */
  248. int device_property_read_string(struct device *dev, const char *propname,
  249. const char **val)
  250. {
  251. return fwnode_property_read_string(dev_fwnode(dev), propname, val);
  252. }
  253. EXPORT_SYMBOL_GPL(device_property_read_string);
  254. #define OF_DEV_PROP_READ_ARRAY(node, propname, type, val, nval) \
  255. (val) ? of_property_read_##type##_array((node), (propname), (val), (nval)) \
  256. : of_property_count_elems_of_size((node), (propname), sizeof(type))
  257. #define FWNODE_PROP_READ_ARRAY(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
  258. ({ \
  259. int _ret_; \
  260. if (is_of_node(_fwnode_)) \
  261. _ret_ = OF_DEV_PROP_READ_ARRAY(of_node(_fwnode_), _propname_, \
  262. _type_, _val_, _nval_); \
  263. else if (is_acpi_node(_fwnode_)) \
  264. _ret_ = acpi_dev_prop_read(acpi_node(_fwnode_), _propname_, \
  265. _proptype_, _val_, _nval_); \
  266. else \
  267. _ret_ = pset_prop_read_array(to_pset(_fwnode_), _propname_, \
  268. _proptype_, _val_, _nval_); \
  269. _ret_; \
  270. })
  271. /**
  272. * fwnode_property_read_u8_array - return a u8 array property of firmware node
  273. * @fwnode: Firmware node to get the property of
  274. * @propname: Name of the property
  275. * @val: The values are stored here or %NULL to return the number of values
  276. * @nval: Size of the @val array
  277. *
  278. * Read an array of u8 properties with @propname from @fwnode and stores them to
  279. * @val if found.
  280. *
  281. * Return: number of values if @val was %NULL,
  282. * %0 if the property was found (success),
  283. * %-EINVAL if given arguments are not valid,
  284. * %-ENODATA if the property does not have a value,
  285. * %-EPROTO if the property is not an array of numbers,
  286. * %-EOVERFLOW if the size of the property is not as expected,
  287. * %-ENXIO if no suitable firmware interface is present.
  288. */
  289. int fwnode_property_read_u8_array(struct fwnode_handle *fwnode,
  290. const char *propname, u8 *val, size_t nval)
  291. {
  292. return FWNODE_PROP_READ_ARRAY(fwnode, propname, u8, DEV_PROP_U8,
  293. val, nval);
  294. }
  295. EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
  296. /**
  297. * fwnode_property_read_u16_array - return a u16 array property of firmware node
  298. * @fwnode: Firmware node to get the property of
  299. * @propname: Name of the property
  300. * @val: The values are stored here or %NULL to return the number of values
  301. * @nval: Size of the @val array
  302. *
  303. * Read an array of u16 properties with @propname from @fwnode and store them to
  304. * @val if found.
  305. *
  306. * Return: number of values if @val was %NULL,
  307. * %0 if the property was found (success),
  308. * %-EINVAL if given arguments are not valid,
  309. * %-ENODATA if the property does not have a value,
  310. * %-EPROTO if the property is not an array of numbers,
  311. * %-EOVERFLOW if the size of the property is not as expected,
  312. * %-ENXIO if no suitable firmware interface is present.
  313. */
  314. int fwnode_property_read_u16_array(struct fwnode_handle *fwnode,
  315. const char *propname, u16 *val, size_t nval)
  316. {
  317. return FWNODE_PROP_READ_ARRAY(fwnode, propname, u16, DEV_PROP_U16,
  318. val, nval);
  319. }
  320. EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
  321. /**
  322. * fwnode_property_read_u32_array - return a u32 array property of firmware node
  323. * @fwnode: Firmware node to get the property of
  324. * @propname: Name of the property
  325. * @val: The values are stored here or %NULL to return the number of values
  326. * @nval: Size of the @val array
  327. *
  328. * Read an array of u32 properties with @propname from @fwnode store them to
  329. * @val if found.
  330. *
  331. * Return: number of values if @val was %NULL,
  332. * %0 if the property was found (success),
  333. * %-EINVAL if given arguments are not valid,
  334. * %-ENODATA if the property does not have a value,
  335. * %-EPROTO if the property is not an array of numbers,
  336. * %-EOVERFLOW if the size of the property is not as expected,
  337. * %-ENXIO if no suitable firmware interface is present.
  338. */
  339. int fwnode_property_read_u32_array(struct fwnode_handle *fwnode,
  340. const char *propname, u32 *val, size_t nval)
  341. {
  342. return FWNODE_PROP_READ_ARRAY(fwnode, propname, u32, DEV_PROP_U32,
  343. val, nval);
  344. }
  345. EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
  346. /**
  347. * fwnode_property_read_u64_array - return a u64 array property firmware node
  348. * @fwnode: Firmware node to get the property of
  349. * @propname: Name of the property
  350. * @val: The values are stored here or %NULL to return the number of values
  351. * @nval: Size of the @val array
  352. *
  353. * Read an array of u64 properties with @propname from @fwnode and store them to
  354. * @val if found.
  355. *
  356. * Return: number of values if @val was %NULL,
  357. * %0 if the property was found (success),
  358. * %-EINVAL if given arguments are not valid,
  359. * %-ENODATA if the property does not have a value,
  360. * %-EPROTO if the property is not an array of numbers,
  361. * %-EOVERFLOW if the size of the property is not as expected,
  362. * %-ENXIO if no suitable firmware interface is present.
  363. */
  364. int fwnode_property_read_u64_array(struct fwnode_handle *fwnode,
  365. const char *propname, u64 *val, size_t nval)
  366. {
  367. return FWNODE_PROP_READ_ARRAY(fwnode, propname, u64, DEV_PROP_U64,
  368. val, nval);
  369. }
  370. EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
  371. /**
  372. * fwnode_property_read_string_array - return string array property of a node
  373. * @fwnode: Firmware node to get the property of
  374. * @propname: Name of the property
  375. * @val: The values are stored here or %NULL to return the number of values
  376. * @nval: Size of the @val array
  377. *
  378. * Read an string list property @propname from the given firmware node and store
  379. * them to @val if found.
  380. *
  381. * Return: number of values if @val was %NULL,
  382. * %0 if the property was found (success),
  383. * %-EINVAL if given arguments are not valid,
  384. * %-ENODATA if the property does not have a value,
  385. * %-EPROTO if the property is not an array of strings,
  386. * %-EOVERFLOW if the size of the property is not as expected,
  387. * %-ENXIO if no suitable firmware interface is present.
  388. */
  389. int fwnode_property_read_string_array(struct fwnode_handle *fwnode,
  390. const char *propname, const char **val,
  391. size_t nval)
  392. {
  393. if (is_of_node(fwnode))
  394. return val ?
  395. of_property_read_string_array(of_node(fwnode), propname,
  396. val, nval) :
  397. of_property_count_strings(of_node(fwnode), propname);
  398. else if (is_acpi_node(fwnode))
  399. return acpi_dev_prop_read(acpi_node(fwnode), propname,
  400. DEV_PROP_STRING, val, nval);
  401. return pset_prop_read_array(to_pset(fwnode), propname,
  402. DEV_PROP_STRING, val, nval);
  403. }
  404. EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
  405. /**
  406. * fwnode_property_read_string - return a string property of a firmware node
  407. * @fwnode: Firmware node to get the property of
  408. * @propname: Name of the property
  409. * @val: The value is stored here
  410. *
  411. * Read property @propname from the given firmware node and store the value into
  412. * @val if found. The value is checked to be a string.
  413. *
  414. * Return: %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 or %-EILSEQ if the property is not a string,
  418. * %-ENXIO if no suitable firmware interface is present.
  419. */
  420. int fwnode_property_read_string(struct fwnode_handle *fwnode,
  421. const char *propname, const char **val)
  422. {
  423. if (is_of_node(fwnode))
  424. return of_property_read_string(of_node(fwnode), propname, val);
  425. else if (is_acpi_node(fwnode))
  426. return acpi_dev_prop_read(acpi_node(fwnode), propname,
  427. DEV_PROP_STRING, val, 1);
  428. return -ENXIO;
  429. }
  430. EXPORT_SYMBOL_GPL(fwnode_property_read_string);
  431. /**
  432. * device_get_next_child_node - Return the next child node handle for a device
  433. * @dev: Device to find the next child node for.
  434. * @child: Handle to one of the device's child nodes or a null handle.
  435. */
  436. struct fwnode_handle *device_get_next_child_node(struct device *dev,
  437. struct fwnode_handle *child)
  438. {
  439. if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
  440. struct device_node *node;
  441. node = of_get_next_available_child(dev->of_node, of_node(child));
  442. if (node)
  443. return &node->fwnode;
  444. } else if (IS_ENABLED(CONFIG_ACPI)) {
  445. struct acpi_device *node;
  446. node = acpi_get_next_child(dev, acpi_node(child));
  447. if (node)
  448. return acpi_fwnode_handle(node);
  449. }
  450. return NULL;
  451. }
  452. EXPORT_SYMBOL_GPL(device_get_next_child_node);
  453. /**
  454. * fwnode_handle_put - Drop reference to a device node
  455. * @fwnode: Pointer to the device node to drop the reference to.
  456. *
  457. * This has to be used when terminating device_for_each_child_node() iteration
  458. * with break or return to prevent stale device node references from being left
  459. * behind.
  460. */
  461. void fwnode_handle_put(struct fwnode_handle *fwnode)
  462. {
  463. if (is_of_node(fwnode))
  464. of_node_put(of_node(fwnode));
  465. }
  466. EXPORT_SYMBOL_GPL(fwnode_handle_put);
  467. /**
  468. * device_get_child_node_count - return the number of child nodes for device
  469. * @dev: Device to cound the child nodes for
  470. */
  471. unsigned int device_get_child_node_count(struct device *dev)
  472. {
  473. struct fwnode_handle *child;
  474. unsigned int count = 0;
  475. device_for_each_child_node(dev, child)
  476. count++;
  477. return count;
  478. }
  479. EXPORT_SYMBOL_GPL(device_get_child_node_count);