property.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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_device_is_available(struct fwnode_handle *fwnode);
  47. bool fwnode_property_present(struct fwnode_handle *fwnode, const char *propname);
  48. int fwnode_property_read_u8_array(struct fwnode_handle *fwnode,
  49. const char *propname, u8 *val,
  50. size_t nval);
  51. int fwnode_property_read_u16_array(struct fwnode_handle *fwnode,
  52. const char *propname, u16 *val,
  53. size_t nval);
  54. int fwnode_property_read_u32_array(struct fwnode_handle *fwnode,
  55. const char *propname, u32 *val,
  56. size_t nval);
  57. int fwnode_property_read_u64_array(struct fwnode_handle *fwnode,
  58. const char *propname, u64 *val,
  59. size_t nval);
  60. int fwnode_property_read_string_array(struct fwnode_handle *fwnode,
  61. const char *propname, const char **val,
  62. size_t nval);
  63. int fwnode_property_read_string(struct fwnode_handle *fwnode,
  64. const char *propname, const char **val);
  65. int fwnode_property_match_string(struct fwnode_handle *fwnode,
  66. const char *propname, const char *string);
  67. struct fwnode_handle *fwnode_get_parent(struct fwnode_handle *fwnode);
  68. struct fwnode_handle *fwnode_get_next_parent(struct fwnode_handle *fwnode);
  69. struct fwnode_handle *fwnode_get_next_child_node(struct fwnode_handle *fwnode,
  70. struct fwnode_handle *child);
  71. #define fwnode_for_each_child_node(fwnode, child) \
  72. for (child = fwnode_get_next_child_node(fwnode, NULL); child; \
  73. child = fwnode_get_next_child_node(fwnode, child))
  74. struct fwnode_handle *device_get_next_child_node(struct device *dev,
  75. struct fwnode_handle *child);
  76. #define device_for_each_child_node(dev, child) \
  77. for (child = device_get_next_child_node(dev, NULL); child; \
  78. child = device_get_next_child_node(dev, child))
  79. struct fwnode_handle *fwnode_get_named_child_node(struct fwnode_handle *fwnode,
  80. const char *childname);
  81. struct fwnode_handle *device_get_named_child_node(struct device *dev,
  82. const char *childname);
  83. void fwnode_handle_get(struct fwnode_handle *fwnode);
  84. void fwnode_handle_put(struct fwnode_handle *fwnode);
  85. unsigned int device_get_child_node_count(struct device *dev);
  86. static inline bool device_property_read_bool(struct device *dev,
  87. const char *propname)
  88. {
  89. return device_property_present(dev, propname);
  90. }
  91. static inline int device_property_read_u8(struct device *dev,
  92. const char *propname, u8 *val)
  93. {
  94. return device_property_read_u8_array(dev, propname, val, 1);
  95. }
  96. static inline int device_property_read_u16(struct device *dev,
  97. const char *propname, u16 *val)
  98. {
  99. return device_property_read_u16_array(dev, propname, val, 1);
  100. }
  101. static inline int device_property_read_u32(struct device *dev,
  102. const char *propname, u32 *val)
  103. {
  104. return device_property_read_u32_array(dev, propname, val, 1);
  105. }
  106. static inline int device_property_read_u64(struct device *dev,
  107. const char *propname, u64 *val)
  108. {
  109. return device_property_read_u64_array(dev, propname, val, 1);
  110. }
  111. static inline bool fwnode_property_read_bool(struct fwnode_handle *fwnode,
  112. const char *propname)
  113. {
  114. return fwnode_property_present(fwnode, propname);
  115. }
  116. static inline int fwnode_property_read_u8(struct fwnode_handle *fwnode,
  117. const char *propname, u8 *val)
  118. {
  119. return fwnode_property_read_u8_array(fwnode, propname, val, 1);
  120. }
  121. static inline int fwnode_property_read_u16(struct fwnode_handle *fwnode,
  122. const char *propname, u16 *val)
  123. {
  124. return fwnode_property_read_u16_array(fwnode, propname, val, 1);
  125. }
  126. static inline int fwnode_property_read_u32(struct fwnode_handle *fwnode,
  127. const char *propname, u32 *val)
  128. {
  129. return fwnode_property_read_u32_array(fwnode, propname, val, 1);
  130. }
  131. static inline int fwnode_property_read_u64(struct fwnode_handle *fwnode,
  132. const char *propname, u64 *val)
  133. {
  134. return fwnode_property_read_u64_array(fwnode, propname, val, 1);
  135. }
  136. /**
  137. * struct property_entry - "Built-in" device property representation.
  138. * @name: Name of the property.
  139. * @length: Length of data making up the value.
  140. * @is_array: True when the property is an array.
  141. * @is_string: True when property is a string.
  142. * @pointer: Pointer to the property (an array of items of the given type).
  143. * @value: Value of the property (when it is a single item of the given type).
  144. */
  145. struct property_entry {
  146. const char *name;
  147. size_t length;
  148. bool is_array;
  149. bool is_string;
  150. union {
  151. union {
  152. const void *raw_data;
  153. const u8 *u8_data;
  154. const u16 *u16_data;
  155. const u32 *u32_data;
  156. const u64 *u64_data;
  157. const char * const *str;
  158. } pointer;
  159. union {
  160. unsigned long long raw_data;
  161. u8 u8_data;
  162. u16 u16_data;
  163. u32 u32_data;
  164. u64 u64_data;
  165. const char *str;
  166. } value;
  167. };
  168. };
  169. /*
  170. * Note: the below four initializers for the anonymous union are carefully
  171. * crafted to avoid gcc-4.4.4's problems with initialization of anon unions
  172. * and structs.
  173. */
  174. #define PROPERTY_ENTRY_INTEGER_ARRAY(_name_, _type_, _val_) \
  175. { \
  176. .name = _name_, \
  177. .length = ARRAY_SIZE(_val_) * sizeof(_type_), \
  178. .is_array = true, \
  179. .is_string = false, \
  180. { .pointer = { ._type_##_data = _val_ } }, \
  181. }
  182. #define PROPERTY_ENTRY_U8_ARRAY(_name_, _val_) \
  183. PROPERTY_ENTRY_INTEGER_ARRAY(_name_, u8, _val_)
  184. #define PROPERTY_ENTRY_U16_ARRAY(_name_, _val_) \
  185. PROPERTY_ENTRY_INTEGER_ARRAY(_name_, u16, _val_)
  186. #define PROPERTY_ENTRY_U32_ARRAY(_name_, _val_) \
  187. PROPERTY_ENTRY_INTEGER_ARRAY(_name_, u32, _val_)
  188. #define PROPERTY_ENTRY_U64_ARRAY(_name_, _val_) \
  189. PROPERTY_ENTRY_INTEGER_ARRAY(_name_, u64, _val_)
  190. #define PROPERTY_ENTRY_STRING_ARRAY(_name_, _val_) \
  191. { \
  192. .name = _name_, \
  193. .length = ARRAY_SIZE(_val_) * sizeof(const char *), \
  194. .is_array = true, \
  195. .is_string = true, \
  196. { .pointer = { .str = _val_ } }, \
  197. }
  198. #define PROPERTY_ENTRY_INTEGER(_name_, _type_, _val_) \
  199. { \
  200. .name = _name_, \
  201. .length = sizeof(_type_), \
  202. .is_string = false, \
  203. { .value = { ._type_##_data = _val_ } }, \
  204. }
  205. #define PROPERTY_ENTRY_U8(_name_, _val_) \
  206. PROPERTY_ENTRY_INTEGER(_name_, u8, _val_)
  207. #define PROPERTY_ENTRY_U16(_name_, _val_) \
  208. PROPERTY_ENTRY_INTEGER(_name_, u16, _val_)
  209. #define PROPERTY_ENTRY_U32(_name_, _val_) \
  210. PROPERTY_ENTRY_INTEGER(_name_, u32, _val_)
  211. #define PROPERTY_ENTRY_U64(_name_, _val_) \
  212. PROPERTY_ENTRY_INTEGER(_name_, u64, _val_)
  213. #define PROPERTY_ENTRY_STRING(_name_, _val_) \
  214. { \
  215. .name = _name_, \
  216. .length = sizeof(_val_), \
  217. .is_string = true, \
  218. { .value = { .str = _val_ } }, \
  219. }
  220. #define PROPERTY_ENTRY_BOOL(_name_) \
  221. { \
  222. .name = _name_, \
  223. }
  224. struct property_entry *
  225. property_entries_dup(const struct property_entry *properties);
  226. void property_entries_free(const struct property_entry *properties);
  227. int device_add_properties(struct device *dev,
  228. const struct property_entry *properties);
  229. void device_remove_properties(struct device *dev);
  230. bool device_dma_supported(struct device *dev);
  231. enum dev_dma_attr device_get_dma_attr(struct device *dev);
  232. int device_get_phy_mode(struct device *dev);
  233. void *device_get_mac_address(struct device *dev, char *addr, int alen);
  234. struct fwnode_handle *fwnode_graph_get_next_endpoint(
  235. struct fwnode_handle *fwnode, struct fwnode_handle *prev);
  236. struct fwnode_handle *
  237. fwnode_graph_get_port_parent(struct fwnode_handle *fwnode);
  238. struct fwnode_handle *fwnode_graph_get_remote_port_parent(
  239. struct fwnode_handle *fwnode);
  240. struct fwnode_handle *fwnode_graph_get_remote_port(
  241. struct fwnode_handle *fwnode);
  242. struct fwnode_handle *fwnode_graph_get_remote_endpoint(
  243. struct fwnode_handle *fwnode);
  244. struct fwnode_handle *fwnode_graph_get_remote_node(struct fwnode_handle *fwnode,
  245. u32 port, u32 endpoint);
  246. int fwnode_graph_parse_endpoint(struct fwnode_handle *fwnode,
  247. struct fwnode_endpoint *endpoint);
  248. #endif /* _LINUX_PROPERTY_H_ */