property.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. #define OF_DEV_PROP_READ_ARRAY(node, propname, type, val, nval) \
  31. (val) ? of_property_read_##type##_array((node), (propname), (val), (nval)) \
  32. : of_property_count_elems_of_size((node), (propname), sizeof(type))
  33. #define DEV_PROP_READ_ARRAY(_dev_, _propname_, _type_, _proptype_, _val_, _nval_) \
  34. IS_ENABLED(CONFIG_OF) && _dev_->of_node ? \
  35. (OF_DEV_PROP_READ_ARRAY(_dev_->of_node, _propname_, _type_, \
  36. _val_, _nval_)) : \
  37. acpi_dev_prop_read(ACPI_COMPANION(_dev_), _propname_, \
  38. _proptype_, _val_, _nval_)
  39. /**
  40. * device_property_read_u8_array - return a u8 array property of a device
  41. * @dev: Device to get the property of
  42. * @propname: Name of the property
  43. * @val: The values are stored here
  44. * @nval: Size of the @val array
  45. *
  46. * Function reads an array of u8 properties with @propname from the device
  47. * firmware description and stores them to @val if found.
  48. *
  49. * Return: %0 if the property was found (success),
  50. * %-EINVAL if given arguments are not valid,
  51. * %-ENODATA if the property does not have a value,
  52. * %-EPROTO if the property is not an array of numbers,
  53. * %-EOVERFLOW if the size of the property is not as expected.
  54. */
  55. int device_property_read_u8_array(struct device *dev, const char *propname,
  56. u8 *val, size_t nval)
  57. {
  58. return DEV_PROP_READ_ARRAY(dev, propname, u8, DEV_PROP_U8, val, nval);
  59. }
  60. EXPORT_SYMBOL_GPL(device_property_read_u8_array);
  61. /**
  62. * device_property_read_u16_array - return a u16 array property of a device
  63. * @dev: Device to get the property of
  64. * @propname: Name of the property
  65. * @val: The values are stored here
  66. * @nval: Size of the @val array
  67. *
  68. * Function reads an array of u16 properties with @propname from the device
  69. * firmware description and stores them to @val if found.
  70. *
  71. * Return: %0 if the property was found (success),
  72. * %-EINVAL if given arguments are not valid,
  73. * %-ENODATA if the property does not have a value,
  74. * %-EPROTO if the property is not an array of numbers,
  75. * %-EOVERFLOW if the size of the property is not as expected.
  76. */
  77. int device_property_read_u16_array(struct device *dev, const char *propname,
  78. u16 *val, size_t nval)
  79. {
  80. return DEV_PROP_READ_ARRAY(dev, propname, u16, DEV_PROP_U16, val, nval);
  81. }
  82. EXPORT_SYMBOL_GPL(device_property_read_u16_array);
  83. /**
  84. * device_property_read_u32_array - return a u32 array property of a device
  85. * @dev: Device to get the property of
  86. * @propname: Name of the property
  87. * @val: The values are stored here
  88. * @nval: Size of the @val array
  89. *
  90. * Function reads an array of u32 properties with @propname from the device
  91. * firmware description and stores them to @val if found.
  92. *
  93. * Return: %0 if the property was found (success),
  94. * %-EINVAL if given arguments are not valid,
  95. * %-ENODATA if the property does not have a value,
  96. * %-EPROTO if the property is not an array of numbers,
  97. * %-EOVERFLOW if the size of the property is not as expected.
  98. */
  99. int device_property_read_u32_array(struct device *dev, const char *propname,
  100. u32 *val, size_t nval)
  101. {
  102. return DEV_PROP_READ_ARRAY(dev, propname, u32, DEV_PROP_U32, val, nval);
  103. }
  104. EXPORT_SYMBOL_GPL(device_property_read_u32_array);
  105. /**
  106. * device_property_read_u64_array - return a u64 array property of a device
  107. * @dev: Device to get the property of
  108. * @propname: Name of the property
  109. * @val: The values are stored here
  110. * @nval: Size of the @val array
  111. *
  112. * Function reads an array of u64 properties with @propname from the device
  113. * firmware description and stores them to @val if found.
  114. *
  115. * Return: %0 if the property was found (success),
  116. * %-EINVAL if given arguments are not valid,
  117. * %-ENODATA if the property does not have a value,
  118. * %-EPROTO if the property is not an array of numbers,
  119. * %-EOVERFLOW if the size of the property is not as expected.
  120. */
  121. int device_property_read_u64_array(struct device *dev, const char *propname,
  122. u64 *val, size_t nval)
  123. {
  124. return DEV_PROP_READ_ARRAY(dev, propname, u64, DEV_PROP_U64, val, nval);
  125. }
  126. EXPORT_SYMBOL_GPL(device_property_read_u64_array);
  127. /**
  128. * device_property_read_string_array - return a string array property of device
  129. * @dev: Device to get the property of
  130. * @propname: Name of the property
  131. * @val: The values are stored here
  132. * @nval: Size of the @val array
  133. *
  134. * Function reads an array of string properties with @propname from the device
  135. * firmware description and stores them to @val if found.
  136. *
  137. * Return: %0 if the property was found (success),
  138. * %-EINVAL if given arguments are not valid,
  139. * %-ENODATA if the property does not have a value,
  140. * %-EPROTO or %-EILSEQ if the property is not an array of strings,
  141. * %-EOVERFLOW if the size of the property is not as expected.
  142. */
  143. int device_property_read_string_array(struct device *dev, const char *propname,
  144. const char **val, size_t nval)
  145. {
  146. return IS_ENABLED(CONFIG_OF) && dev->of_node ?
  147. of_property_read_string_array(dev->of_node, propname, val, nval) :
  148. acpi_dev_prop_read(ACPI_COMPANION(dev), propname,
  149. DEV_PROP_STRING, val, nval);
  150. }
  151. EXPORT_SYMBOL_GPL(device_property_read_string_array);
  152. /**
  153. * device_property_read_string - return a string property of a device
  154. * @dev: Device to get the property of
  155. * @propname: Name of the property
  156. * @val: The value is stored here
  157. *
  158. * Function reads property @propname from the device firmware description and
  159. * stores the value into @val if found. The value is checked to be a string.
  160. *
  161. * Return: %0 if the property was found (success),
  162. * %-EINVAL if given arguments are not valid,
  163. * %-ENODATA if the property does not have a value,
  164. * %-EPROTO or %-EILSEQ if the property type is not a string.
  165. */
  166. int device_property_read_string(struct device *dev, const char *propname,
  167. const char **val)
  168. {
  169. return IS_ENABLED(CONFIG_OF) && dev->of_node ?
  170. of_property_read_string(dev->of_node, propname, val) :
  171. acpi_dev_prop_read(ACPI_COMPANION(dev), propname,
  172. DEV_PROP_STRING, val, 1);
  173. }
  174. EXPORT_SYMBOL_GPL(device_property_read_string);