property.h 18 KB

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