property.c 18 KB

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