property.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * property.h - 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. #ifndef _LINUX_PROPERTY_H_
  13. #define _LINUX_PROPERTY_H_
  14. #include <linux/fwnode.h>
  15. #include <linux/types.h>
  16. struct device;
  17. enum dev_prop_type {
  18. DEV_PROP_U8,
  19. DEV_PROP_U16,
  20. DEV_PROP_U32,
  21. DEV_PROP_U64,
  22. DEV_PROP_STRING,
  23. DEV_PROP_MAX,
  24. };
  25. enum dev_dma_attr {
  26. DEV_DMA_NOT_SUPPORTED,
  27. DEV_DMA_NON_COHERENT,
  28. DEV_DMA_COHERENT,
  29. };
  30. struct fwnode_handle *dev_fwnode(struct device *dev);
  31. bool device_property_present(struct device *dev, const char *propname);
  32. int device_property_read_u8_array(struct device *dev, const char *propname,
  33. u8 *val, size_t nval);
  34. int device_property_read_u16_array(struct device *dev, const char *propname,
  35. u16 *val, size_t nval);
  36. int device_property_read_u32_array(struct device *dev, const char *propname,
  37. u32 *val, size_t nval);
  38. int device_property_read_u64_array(struct device *dev, const char *propname,
  39. u64 *val, size_t nval);
  40. int device_property_read_string_array(struct device *dev, const char *propname,
  41. const char **val, size_t nval);
  42. int device_property_read_string(struct device *dev, const char *propname,
  43. const char **val);
  44. int device_property_match_string(struct device *dev,
  45. const char *propname, const char *string);
  46. bool fwnode_property_present(struct fwnode_handle *fwnode, const char *propname);
  47. int fwnode_property_read_u8_array(struct fwnode_handle *fwnode,
  48. const char *propname, u8 *val,
  49. size_t nval);
  50. int fwnode_property_read_u16_array(struct fwnode_handle *fwnode,
  51. const char *propname, u16 *val,
  52. size_t nval);
  53. int fwnode_property_read_u32_array(struct fwnode_handle *fwnode,
  54. const char *propname, u32 *val,
  55. size_t nval);
  56. int fwnode_property_read_u64_array(struct fwnode_handle *fwnode,
  57. const char *propname, u64 *val,
  58. size_t nval);
  59. int fwnode_property_read_string_array(struct fwnode_handle *fwnode,
  60. const char *propname, const char **val,
  61. size_t nval);
  62. int fwnode_property_read_string(struct fwnode_handle *fwnode,
  63. const char *propname, const char **val);
  64. int fwnode_property_match_string(struct fwnode_handle *fwnode,
  65. const char *propname, const char *string);
  66. struct fwnode_handle *device_get_next_child_node(struct device *dev,
  67. struct fwnode_handle *child);
  68. #define device_for_each_child_node(dev, child) \
  69. for (child = device_get_next_child_node(dev, NULL); child; \
  70. child = device_get_next_child_node(dev, child))
  71. struct fwnode_handle *device_get_named_child_node(struct device *dev,
  72. const char *childname);
  73. void fwnode_handle_put(struct fwnode_handle *fwnode);
  74. unsigned int device_get_child_node_count(struct device *dev);
  75. static inline bool device_property_read_bool(struct device *dev,
  76. const char *propname)
  77. {
  78. return device_property_present(dev, propname);
  79. }
  80. static inline int device_property_read_u8(struct device *dev,
  81. const char *propname, u8 *val)
  82. {
  83. return device_property_read_u8_array(dev, propname, val, 1);
  84. }
  85. static inline int device_property_read_u16(struct device *dev,
  86. const char *propname, u16 *val)
  87. {
  88. return device_property_read_u16_array(dev, propname, val, 1);
  89. }
  90. static inline int device_property_read_u32(struct device *dev,
  91. const char *propname, u32 *val)
  92. {
  93. return device_property_read_u32_array(dev, propname, val, 1);
  94. }
  95. static inline int device_property_read_u64(struct device *dev,
  96. const char *propname, u64 *val)
  97. {
  98. return device_property_read_u64_array(dev, propname, val, 1);
  99. }
  100. static inline bool fwnode_property_read_bool(struct fwnode_handle *fwnode,
  101. const char *propname)
  102. {
  103. return fwnode_property_present(fwnode, propname);
  104. }
  105. static inline int fwnode_property_read_u8(struct fwnode_handle *fwnode,
  106. const char *propname, u8 *val)
  107. {
  108. return fwnode_property_read_u8_array(fwnode, propname, val, 1);
  109. }
  110. static inline int fwnode_property_read_u16(struct fwnode_handle *fwnode,
  111. const char *propname, u16 *val)
  112. {
  113. return fwnode_property_read_u16_array(fwnode, propname, val, 1);
  114. }
  115. static inline int fwnode_property_read_u32(struct fwnode_handle *fwnode,
  116. const char *propname, u32 *val)
  117. {
  118. return fwnode_property_read_u32_array(fwnode, propname, val, 1);
  119. }
  120. static inline int fwnode_property_read_u64(struct fwnode_handle *fwnode,
  121. const char *propname, u64 *val)
  122. {
  123. return fwnode_property_read_u64_array(fwnode, propname, val, 1);
  124. }
  125. /**
  126. * struct property_entry - "Built-in" device property representation.
  127. * @name: Name of the property.
  128. * @length: Length of data making up the value.
  129. * @is_array: True when the property is an array.
  130. * @is_string: True when property is a string.
  131. * @pointer: Pointer to the property (an array of items of the given type).
  132. * @value: Value of the property (when it is a single item of the given type).
  133. */
  134. struct property_entry {
  135. const char *name;
  136. size_t length;
  137. bool is_array;
  138. bool is_string;
  139. union {
  140. union {
  141. void *raw_data;
  142. u8 *u8_data;
  143. u16 *u16_data;
  144. u32 *u32_data;
  145. u64 *u64_data;
  146. const char **str;
  147. } pointer;
  148. union {
  149. unsigned long long raw_data;
  150. u8 u8_data;
  151. u16 u16_data;
  152. u32 u32_data;
  153. u64 u64_data;
  154. const char *str;
  155. } value;
  156. };
  157. };
  158. /*
  159. * Note: the below four initializers for the anonymous union are carefully
  160. * crafted to avoid gcc-4.4.4's problems with initialization of anon unions
  161. * and structs.
  162. */
  163. #define PROPERTY_ENTRY_INTEGER_ARRAY(_name_, _type_, _val_) \
  164. (struct property_entry) { \
  165. .name = _name_, \
  166. .length = ARRAY_SIZE(_val_) * sizeof(_type_), \
  167. .is_array = true, \
  168. .is_string = false, \
  169. { .pointer = { ._type_##_data = _val_ } }, \
  170. }
  171. #define PROPERTY_ENTRY_U8_ARRAY(_name_, _val_) \
  172. PROPERTY_ENTRY_INTEGER_ARRAY(_name_, u8, _val_)
  173. #define PROPERTY_ENTRY_U16_ARRAY(_name_, _val_) \
  174. PROPERTY_ENTRY_INTEGER_ARRAY(_name_, u16, _val_)
  175. #define PROPERTY_ENTRY_U32_ARRAY(_name_, _val_) \
  176. PROPERTY_ENTRY_INTEGER_ARRAY(_name_, u32, _val_)
  177. #define PROPERTY_ENTRY_U64_ARRAY(_name_, _val_) \
  178. PROPERTY_ENTRY_INTEGER_ARRAY(_name_, u64, _val_)
  179. #define PROPERTY_ENTRY_STRING_ARRAY(_name_, _val_) \
  180. (struct property_entry) { \
  181. .name = _name_, \
  182. .length = ARRAY_SIZE(_val_) * sizeof(const char *), \
  183. .is_array = true, \
  184. .is_string = true, \
  185. { .pointer = { .str = _val_ } }, \
  186. }
  187. #define PROPERTY_ENTRY_INTEGER(_name_, _type_, _val_) \
  188. (struct property_entry) { \
  189. .name = _name_, \
  190. .length = sizeof(_type_), \
  191. .is_string = false, \
  192. { .value = { ._type_##_data = _val_ } }, \
  193. }
  194. #define PROPERTY_ENTRY_U8(_name_, _val_) \
  195. PROPERTY_ENTRY_INTEGER(_name_, u8, _val_)
  196. #define PROPERTY_ENTRY_U16(_name_, _val_) \
  197. PROPERTY_ENTRY_INTEGER(_name_, u16, _val_)
  198. #define PROPERTY_ENTRY_U32(_name_, _val_) \
  199. PROPERTY_ENTRY_INTEGER(_name_, u32, _val_)
  200. #define PROPERTY_ENTRY_U64(_name_, _val_) \
  201. PROPERTY_ENTRY_INTEGER(_name_, u64, _val_)
  202. #define PROPERTY_ENTRY_STRING(_name_, _val_) \
  203. (struct property_entry) { \
  204. .name = _name_, \
  205. .length = sizeof(_val_), \
  206. .is_string = true, \
  207. { .value = { .str = _val_ } }, \
  208. }
  209. #define PROPERTY_ENTRY_BOOL(_name_) \
  210. (struct property_entry) { \
  211. .name = _name_, \
  212. }
  213. int device_add_properties(struct device *dev,
  214. struct property_entry *properties);
  215. void device_remove_properties(struct device *dev);
  216. bool device_dma_supported(struct device *dev);
  217. enum dev_dma_attr device_get_dma_attr(struct device *dev);
  218. int device_get_phy_mode(struct device *dev);
  219. void *device_get_mac_address(struct device *dev, char *addr, int alen);
  220. #endif /* _LINUX_PROPERTY_H_ */