property.h 10 KB

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