property.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * property.h - Unified device property interface.
  4. *
  5. * Copyright (C) 2014, Intel Corporation
  6. * Authors: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
  7. * Mika Westerberg <mika.westerberg@linux.intel.com>
  8. */
  9. #ifndef _LINUX_PROPERTY_H_
  10. #define _LINUX_PROPERTY_H_
  11. #include <linux/bits.h>
  12. #include <linux/fwnode.h>
  13. #include <linux/types.h>
  14. struct device;
  15. enum dev_prop_type {
  16. DEV_PROP_U8,
  17. DEV_PROP_U16,
  18. DEV_PROP_U32,
  19. DEV_PROP_U64,
  20. DEV_PROP_STRING,
  21. DEV_PROP_REF,
  22. };
  23. enum dev_dma_attr {
  24. DEV_DMA_NOT_SUPPORTED,
  25. DEV_DMA_NON_COHERENT,
  26. DEV_DMA_COHERENT,
  27. };
  28. struct fwnode_handle *dev_fwnode(struct device *dev);
  29. bool device_property_present(struct device *dev, const char *propname);
  30. int device_property_read_u8_array(struct device *dev, const char *propname,
  31. u8 *val, size_t nval);
  32. int device_property_read_u16_array(struct device *dev, const char *propname,
  33. u16 *val, size_t nval);
  34. int device_property_read_u32_array(struct device *dev, const char *propname,
  35. u32 *val, size_t nval);
  36. int device_property_read_u64_array(struct device *dev, const char *propname,
  37. u64 *val, size_t nval);
  38. int device_property_read_string_array(struct device *dev, const char *propname,
  39. const char **val, size_t nval);
  40. int device_property_read_string(struct device *dev, const char *propname,
  41. const char **val);
  42. int device_property_match_string(struct device *dev,
  43. const char *propname, const char *string);
  44. bool fwnode_device_is_available(const struct fwnode_handle *fwnode);
  45. bool fwnode_property_present(const struct fwnode_handle *fwnode,
  46. const char *propname);
  47. int fwnode_property_read_u8_array(const struct fwnode_handle *fwnode,
  48. const char *propname, u8 *val,
  49. size_t nval);
  50. int fwnode_property_read_u16_array(const struct fwnode_handle *fwnode,
  51. const char *propname, u16 *val,
  52. size_t nval);
  53. int fwnode_property_read_u32_array(const struct fwnode_handle *fwnode,
  54. const char *propname, u32 *val,
  55. size_t nval);
  56. int fwnode_property_read_u64_array(const struct fwnode_handle *fwnode,
  57. const char *propname, u64 *val,
  58. size_t nval);
  59. int fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
  60. const char *propname, const char **val,
  61. size_t nval);
  62. int fwnode_property_read_string(const struct fwnode_handle *fwnode,
  63. const char *propname, const char **val);
  64. int fwnode_property_match_string(const struct fwnode_handle *fwnode,
  65. const char *propname, const char *string);
  66. int fwnode_property_get_reference_args(const struct fwnode_handle *fwnode,
  67. const char *prop, const char *nargs_prop,
  68. unsigned int nargs, unsigned int index,
  69. struct fwnode_reference_args *args);
  70. struct fwnode_handle *fwnode_find_reference(const struct fwnode_handle *fwnode,
  71. const char *name,
  72. unsigned int index);
  73. const char *fwnode_get_name(const struct fwnode_handle *fwnode);
  74. const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode);
  75. struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode);
  76. struct fwnode_handle *fwnode_get_next_parent(
  77. struct fwnode_handle *fwnode);
  78. struct device *fwnode_get_next_parent_dev(struct fwnode_handle *fwnode);
  79. unsigned int fwnode_count_parents(const struct fwnode_handle *fwn);
  80. struct fwnode_handle *fwnode_get_nth_parent(struct fwnode_handle *fwn,
  81. unsigned int depth);
  82. bool fwnode_is_ancestor_of(struct fwnode_handle *test_ancestor,
  83. struct fwnode_handle *test_child);
  84. struct fwnode_handle *fwnode_get_next_child_node(
  85. const struct fwnode_handle *fwnode, struct fwnode_handle *child);
  86. struct fwnode_handle *fwnode_get_next_available_child_node(
  87. const struct fwnode_handle *fwnode, struct fwnode_handle *child);
  88. #define fwnode_for_each_child_node(fwnode, child) \
  89. for (child = fwnode_get_next_child_node(fwnode, NULL); child; \
  90. child = fwnode_get_next_child_node(fwnode, child))
  91. #define fwnode_for_each_available_child_node(fwnode, child) \
  92. for (child = fwnode_get_next_available_child_node(fwnode, NULL); child;\
  93. child = fwnode_get_next_available_child_node(fwnode, child))
  94. struct fwnode_handle *device_get_next_child_node(
  95. struct device *dev, struct fwnode_handle *child);
  96. #define device_for_each_child_node(dev, child) \
  97. for (child = device_get_next_child_node(dev, NULL); child; \
  98. child = device_get_next_child_node(dev, child))
  99. struct fwnode_handle *fwnode_get_named_child_node(
  100. const struct fwnode_handle *fwnode, const char *childname);
  101. struct fwnode_handle *device_get_named_child_node(struct device *dev,
  102. const char *childname);
  103. struct fwnode_handle *fwnode_handle_get(struct fwnode_handle *fwnode);
  104. void fwnode_handle_put(struct fwnode_handle *fwnode);
  105. int fwnode_irq_get(struct fwnode_handle *fwnode, unsigned int index);
  106. unsigned int device_get_child_node_count(struct device *dev);
  107. static inline bool device_property_read_bool(struct device *dev,
  108. const char *propname)
  109. {
  110. return device_property_present(dev, propname);
  111. }
  112. static inline int device_property_read_u8(struct device *dev,
  113. const char *propname, u8 *val)
  114. {
  115. return device_property_read_u8_array(dev, propname, val, 1);
  116. }
  117. static inline int device_property_read_u16(struct device *dev,
  118. const char *propname, u16 *val)
  119. {
  120. return device_property_read_u16_array(dev, propname, val, 1);
  121. }
  122. static inline int device_property_read_u32(struct device *dev,
  123. const char *propname, u32 *val)
  124. {
  125. return device_property_read_u32_array(dev, propname, val, 1);
  126. }
  127. static inline int device_property_read_u64(struct device *dev,
  128. const char *propname, u64 *val)
  129. {
  130. return device_property_read_u64_array(dev, propname, val, 1);
  131. }
  132. static inline int device_property_count_u8(struct device *dev, const char *propname)
  133. {
  134. return device_property_read_u8_array(dev, propname, NULL, 0);
  135. }
  136. static inline int device_property_count_u16(struct device *dev, const char *propname)
  137. {
  138. return device_property_read_u16_array(dev, propname, NULL, 0);
  139. }
  140. static inline int device_property_count_u32(struct device *dev, const char *propname)
  141. {
  142. return device_property_read_u32_array(dev, propname, NULL, 0);
  143. }
  144. static inline int device_property_count_u64(struct device *dev, const char *propname)
  145. {
  146. return device_property_read_u64_array(dev, propname, NULL, 0);
  147. }
  148. static inline int device_property_string_array_count(struct device *dev,
  149. const char *propname)
  150. {
  151. return device_property_read_string_array(dev, propname, NULL, 0);
  152. }
  153. static inline bool fwnode_property_read_bool(const struct fwnode_handle *fwnode,
  154. const char *propname)
  155. {
  156. return fwnode_property_present(fwnode, propname);
  157. }
  158. static inline int fwnode_property_read_u8(const struct fwnode_handle *fwnode,
  159. const char *propname, u8 *val)
  160. {
  161. return fwnode_property_read_u8_array(fwnode, propname, val, 1);
  162. }
  163. static inline int fwnode_property_read_u16(const struct fwnode_handle *fwnode,
  164. const char *propname, u16 *val)
  165. {
  166. return fwnode_property_read_u16_array(fwnode, propname, val, 1);
  167. }
  168. static inline int fwnode_property_read_u32(const struct fwnode_handle *fwnode,
  169. const char *propname, u32 *val)
  170. {
  171. return fwnode_property_read_u32_array(fwnode, propname, val, 1);
  172. }
  173. static inline int fwnode_property_read_u64(const struct fwnode_handle *fwnode,
  174. const char *propname, u64 *val)
  175. {
  176. return fwnode_property_read_u64_array(fwnode, propname, val, 1);
  177. }
  178. static inline int fwnode_property_count_u8(const struct fwnode_handle *fwnode,
  179. const char *propname)
  180. {
  181. return fwnode_property_read_u8_array(fwnode, propname, NULL, 0);
  182. }
  183. static inline int fwnode_property_count_u16(const struct fwnode_handle *fwnode,
  184. const char *propname)
  185. {
  186. return fwnode_property_read_u16_array(fwnode, propname, NULL, 0);
  187. }
  188. static inline int fwnode_property_count_u32(const struct fwnode_handle *fwnode,
  189. const char *propname)
  190. {
  191. return fwnode_property_read_u32_array(fwnode, propname, NULL, 0);
  192. }
  193. static inline int fwnode_property_count_u64(const struct fwnode_handle *fwnode,
  194. const char *propname)
  195. {
  196. return fwnode_property_read_u64_array(fwnode, propname, NULL, 0);
  197. }
  198. static inline int
  199. fwnode_property_string_array_count(const struct fwnode_handle *fwnode,
  200. const char *propname)
  201. {
  202. return fwnode_property_read_string_array(fwnode, propname, NULL, 0);
  203. }
  204. struct software_node;
  205. /**
  206. * struct software_node_ref_args - Reference property with additional arguments
  207. * @node: Reference to a software node
  208. * @nargs: Number of elements in @args array
  209. * @args: Integer arguments
  210. */
  211. struct software_node_ref_args {
  212. const struct software_node *node;
  213. unsigned int nargs;
  214. u64 args[NR_FWNODE_REFERENCE_ARGS];
  215. };
  216. /**
  217. * struct property_entry - "Built-in" device property representation.
  218. * @name: Name of the property.
  219. * @length: Length of data making up the value.
  220. * @is_inline: True when the property value is stored inline.
  221. * @type: Type of the data in unions.
  222. * @pointer: Pointer to the property when it is not stored inline.
  223. * @value: Value of the property when it is stored inline.
  224. */
  225. struct property_entry {
  226. const char *name;
  227. size_t length;
  228. bool is_inline;
  229. enum dev_prop_type type;
  230. union {
  231. const void *pointer;
  232. union {
  233. u8 u8_data[sizeof(u64) / sizeof(u8)];
  234. u16 u16_data[sizeof(u64) / sizeof(u16)];
  235. u32 u32_data[sizeof(u64) / sizeof(u32)];
  236. u64 u64_data[sizeof(u64) / sizeof(u64)];
  237. const char *str[sizeof(u64) / sizeof(char *)];
  238. } value;
  239. };
  240. };
  241. /*
  242. * Note: the below initializers for the anonymous union are carefully
  243. * crafted to avoid gcc-4.4.4's problems with initialization of anon unions
  244. * and structs.
  245. */
  246. #define __PROPERTY_ENTRY_ELEMENT_SIZE(_elem_) \
  247. sizeof(((struct property_entry *)NULL)->value._elem_[0])
  248. #define __PROPERTY_ENTRY_ARRAY_ELSIZE_LEN(_name_, _elsize_, _Type_, \
  249. _val_, _len_) \
  250. (struct property_entry) { \
  251. .name = _name_, \
  252. .length = (_len_) * (_elsize_), \
  253. .type = DEV_PROP_##_Type_, \
  254. { .pointer = _val_ }, \
  255. }
  256. #define __PROPERTY_ENTRY_ARRAY_LEN(_name_, _elem_, _Type_, _val_, _len_)\
  257. __PROPERTY_ENTRY_ARRAY_ELSIZE_LEN(_name_, \
  258. __PROPERTY_ENTRY_ELEMENT_SIZE(_elem_), \
  259. _Type_, _val_, _len_)
  260. #define PROPERTY_ENTRY_U8_ARRAY_LEN(_name_, _val_, _len_) \
  261. __PROPERTY_ENTRY_ARRAY_LEN(_name_, u8_data, U8, _val_, _len_)
  262. #define PROPERTY_ENTRY_U16_ARRAY_LEN(_name_, _val_, _len_) \
  263. __PROPERTY_ENTRY_ARRAY_LEN(_name_, u16_data, U16, _val_, _len_)
  264. #define PROPERTY_ENTRY_U32_ARRAY_LEN(_name_, _val_, _len_) \
  265. __PROPERTY_ENTRY_ARRAY_LEN(_name_, u32_data, U32, _val_, _len_)
  266. #define PROPERTY_ENTRY_U64_ARRAY_LEN(_name_, _val_, _len_) \
  267. __PROPERTY_ENTRY_ARRAY_LEN(_name_, u64_data, U64, _val_, _len_)
  268. #define PROPERTY_ENTRY_STRING_ARRAY_LEN(_name_, _val_, _len_) \
  269. __PROPERTY_ENTRY_ARRAY_LEN(_name_, str, STRING, _val_, _len_)
  270. #define PROPERTY_ENTRY_REF_ARRAY_LEN(_name_, _val_, _len_) \
  271. __PROPERTY_ENTRY_ARRAY_ELSIZE_LEN(_name_, \
  272. sizeof(struct software_node_ref_args), \
  273. REF, _val_, _len_)
  274. #define PROPERTY_ENTRY_U8_ARRAY(_name_, _val_) \
  275. PROPERTY_ENTRY_U8_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
  276. #define PROPERTY_ENTRY_U16_ARRAY(_name_, _val_) \
  277. PROPERTY_ENTRY_U16_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
  278. #define PROPERTY_ENTRY_U32_ARRAY(_name_, _val_) \
  279. PROPERTY_ENTRY_U32_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
  280. #define PROPERTY_ENTRY_U64_ARRAY(_name_, _val_) \
  281. PROPERTY_ENTRY_U64_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
  282. #define PROPERTY_ENTRY_STRING_ARRAY(_name_, _val_) \
  283. PROPERTY_ENTRY_STRING_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
  284. #define PROPERTY_ENTRY_REF_ARRAY(_name_, _val_) \
  285. PROPERTY_ENTRY_REF_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
  286. #define __PROPERTY_ENTRY_ELEMENT(_name_, _elem_, _Type_, _val_) \
  287. (struct property_entry) { \
  288. .name = _name_, \
  289. .length = __PROPERTY_ENTRY_ELEMENT_SIZE(_elem_), \
  290. .is_inline = true, \
  291. .type = DEV_PROP_##_Type_, \
  292. { .value = { ._elem_[0] = _val_ } }, \
  293. }
  294. #define PROPERTY_ENTRY_U8(_name_, _val_) \
  295. __PROPERTY_ENTRY_ELEMENT(_name_, u8_data, U8, _val_)
  296. #define PROPERTY_ENTRY_U16(_name_, _val_) \
  297. __PROPERTY_ENTRY_ELEMENT(_name_, u16_data, U16, _val_)
  298. #define PROPERTY_ENTRY_U32(_name_, _val_) \
  299. __PROPERTY_ENTRY_ELEMENT(_name_, u32_data, U32, _val_)
  300. #define PROPERTY_ENTRY_U64(_name_, _val_) \
  301. __PROPERTY_ENTRY_ELEMENT(_name_, u64_data, U64, _val_)
  302. #define PROPERTY_ENTRY_STRING(_name_, _val_) \
  303. __PROPERTY_ENTRY_ELEMENT(_name_, str, STRING, _val_)
  304. #define PROPERTY_ENTRY_BOOL(_name_) \
  305. (struct property_entry) { \
  306. .name = _name_, \
  307. .is_inline = true, \
  308. }
  309. #define PROPERTY_ENTRY_REF(_name_, _ref_, ...) \
  310. (struct property_entry) { \
  311. .name = _name_, \
  312. .length = sizeof(struct software_node_ref_args), \
  313. .type = DEV_PROP_REF, \
  314. { .pointer = &(const struct software_node_ref_args) { \
  315. .node = _ref_, \
  316. .nargs = ARRAY_SIZE(((u64[]){ 0, ##__VA_ARGS__ })) - 1, \
  317. .args = { __VA_ARGS__ }, \
  318. } }, \
  319. }
  320. struct property_entry *
  321. property_entries_dup(const struct property_entry *properties);
  322. void property_entries_free(const struct property_entry *properties);
  323. int device_add_properties(struct device *dev,
  324. const struct property_entry *properties);
  325. void device_remove_properties(struct device *dev);
  326. bool device_dma_supported(struct device *dev);
  327. enum dev_dma_attr device_get_dma_attr(struct device *dev);
  328. const void *device_get_match_data(struct device *dev);
  329. int device_get_phy_mode(struct device *dev);
  330. void *device_get_mac_address(struct device *dev, char *addr, int alen);
  331. int fwnode_get_phy_mode(struct fwnode_handle *fwnode);
  332. void *fwnode_get_mac_address(struct fwnode_handle *fwnode,
  333. char *addr, int alen);
  334. struct fwnode_handle *fwnode_graph_get_next_endpoint(
  335. const struct fwnode_handle *fwnode, struct fwnode_handle *prev);
  336. struct fwnode_handle *
  337. fwnode_graph_get_port_parent(const struct fwnode_handle *fwnode);
  338. struct fwnode_handle *fwnode_graph_get_remote_port_parent(
  339. const struct fwnode_handle *fwnode);
  340. struct fwnode_handle *fwnode_graph_get_remote_port(
  341. const struct fwnode_handle *fwnode);
  342. struct fwnode_handle *fwnode_graph_get_remote_endpoint(
  343. const struct fwnode_handle *fwnode);
  344. struct fwnode_handle *
  345. fwnode_graph_get_remote_node(const struct fwnode_handle *fwnode, u32 port,
  346. u32 endpoint);
  347. static inline bool fwnode_graph_is_endpoint(struct fwnode_handle *fwnode)
  348. {
  349. return fwnode_property_present(fwnode, "remote-endpoint");
  350. }
  351. /*
  352. * Fwnode lookup flags
  353. *
  354. * @FWNODE_GRAPH_ENDPOINT_NEXT: In the case of no exact match, look for the
  355. * closest endpoint ID greater than the specified
  356. * one.
  357. * @FWNODE_GRAPH_DEVICE_DISABLED: That the device to which the remote
  358. * endpoint of the given endpoint belongs to,
  359. * may be disabled.
  360. */
  361. #define FWNODE_GRAPH_ENDPOINT_NEXT BIT(0)
  362. #define FWNODE_GRAPH_DEVICE_DISABLED BIT(1)
  363. struct fwnode_handle *
  364. fwnode_graph_get_endpoint_by_id(const struct fwnode_handle *fwnode,
  365. u32 port, u32 endpoint, unsigned long flags);
  366. #define fwnode_graph_for_each_endpoint(fwnode, child) \
  367. for (child = NULL; \
  368. (child = fwnode_graph_get_next_endpoint(fwnode, child)); )
  369. int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
  370. struct fwnode_endpoint *endpoint);
  371. typedef void *(*devcon_match_fn_t)(struct fwnode_handle *fwnode, const char *id,
  372. void *data);
  373. void *fwnode_connection_find_match(struct fwnode_handle *fwnode,
  374. const char *con_id, void *data,
  375. devcon_match_fn_t match);
  376. static inline void *device_connection_find_match(struct device *dev,
  377. const char *con_id, void *data,
  378. devcon_match_fn_t match)
  379. {
  380. return fwnode_connection_find_match(dev_fwnode(dev), con_id, data, match);
  381. }
  382. /* -------------------------------------------------------------------------- */
  383. /* Software fwnode support - when HW description is incomplete or missing */
  384. /**
  385. * struct software_node - Software node description
  386. * @name: Name of the software node
  387. * @parent: Parent of the software node
  388. * @properties: Array of device properties
  389. */
  390. struct software_node {
  391. const char *name;
  392. const struct software_node *parent;
  393. const struct property_entry *properties;
  394. };
  395. bool is_software_node(const struct fwnode_handle *fwnode);
  396. const struct software_node *
  397. to_software_node(const struct fwnode_handle *fwnode);
  398. struct fwnode_handle *software_node_fwnode(const struct software_node *node);
  399. const struct software_node *
  400. software_node_find_by_name(const struct software_node *parent,
  401. const char *name);
  402. int software_node_register_nodes(const struct software_node *nodes);
  403. void software_node_unregister_nodes(const struct software_node *nodes);
  404. int software_node_register_node_group(const struct software_node **node_group);
  405. void software_node_unregister_node_group(const struct software_node **node_group);
  406. int software_node_register(const struct software_node *node);
  407. void software_node_unregister(const struct software_node *node);
  408. int software_node_notify(struct device *dev, unsigned long action);
  409. struct fwnode_handle *
  410. fwnode_create_software_node(const struct property_entry *properties,
  411. const struct fwnode_handle *parent);
  412. void fwnode_remove_software_node(struct fwnode_handle *fwnode);
  413. int device_add_software_node(struct device *dev, const struct software_node *node);
  414. void device_remove_software_node(struct device *dev);
  415. #endif /* _LINUX_PROPERTY_H_ */