property.h 7.8 KB

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