1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * property.h - Unified device property interface.
5 * Copyright (C) 2014, Intel Corporation
6 * Authors: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
7 * Mika Westerberg <mika.westerberg@linux.intel.com>
10 #ifndef _LINUX_PROPERTY_H_
11 #define _LINUX_PROPERTY_H_
13 #include <linux/args.h>
14 #include <linux/bits.h>
15 #include <linux/fwnode.h>
16 #include <linux/stddef.h>
17 #include <linux/types.h>
31 DEV_DMA_NOT_SUPPORTED,
36 const struct fwnode_handle *__dev_fwnode_const(const struct device *dev);
37 struct fwnode_handle *__dev_fwnode(struct device *dev);
38 #define dev_fwnode(dev) \
40 const struct device *: __dev_fwnode_const, \
41 struct device *: __dev_fwnode)(dev)
43 bool device_property_present(const struct device *dev, const char *propname);
44 int device_property_read_u8_array(const struct device *dev, const char *propname,
45 u8 *val, size_t nval);
46 int device_property_read_u16_array(const struct device *dev, const char *propname,
47 u16 *val, size_t nval);
48 int device_property_read_u32_array(const struct device *dev, const char *propname,
49 u32 *val, size_t nval);
50 int device_property_read_u64_array(const struct device *dev, const char *propname,
51 u64 *val, size_t nval);
52 int device_property_read_string_array(const struct device *dev, const char *propname,
53 const char **val, size_t nval);
54 int device_property_read_string(const struct device *dev, const char *propname,
56 int device_property_match_string(const struct device *dev,
57 const char *propname, const char *string);
59 bool fwnode_property_present(const struct fwnode_handle *fwnode,
60 const char *propname);
61 int fwnode_property_read_u8_array(const struct fwnode_handle *fwnode,
62 const char *propname, u8 *val,
64 int fwnode_property_read_u16_array(const struct fwnode_handle *fwnode,
65 const char *propname, u16 *val,
67 int fwnode_property_read_u32_array(const struct fwnode_handle *fwnode,
68 const char *propname, u32 *val,
70 int fwnode_property_read_u64_array(const struct fwnode_handle *fwnode,
71 const char *propname, u64 *val,
73 int fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
74 const char *propname, const char **val,
76 int fwnode_property_read_string(const struct fwnode_handle *fwnode,
77 const char *propname, const char **val);
78 int fwnode_property_match_string(const struct fwnode_handle *fwnode,
79 const char *propname, const char *string);
81 bool fwnode_device_is_available(const struct fwnode_handle *fwnode);
84 bool fwnode_device_is_compatible(const struct fwnode_handle *fwnode, const char *compat)
86 return fwnode_property_match_string(fwnode, "compatible", compat) >= 0;
90 * device_is_compatible - match 'compatible' property of the device with a given string
91 * @dev: Pointer to the struct device
92 * @compat: The string to match 'compatible' property with
94 * Returns: true if matches, otherwise false.
96 static inline bool device_is_compatible(const struct device *dev, const char *compat)
98 return fwnode_device_is_compatible(dev_fwnode(dev), compat);
101 int fwnode_property_get_reference_args(const struct fwnode_handle *fwnode,
102 const char *prop, const char *nargs_prop,
103 unsigned int nargs, unsigned int index,
104 struct fwnode_reference_args *args);
106 struct fwnode_handle *fwnode_find_reference(const struct fwnode_handle *fwnode,
110 const char *fwnode_get_name(const struct fwnode_handle *fwnode);
111 const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode);
113 struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode);
114 struct fwnode_handle *fwnode_get_next_parent(struct fwnode_handle *fwnode);
116 #define fwnode_for_each_parent_node(fwnode, parent) \
117 for (parent = fwnode_get_parent(fwnode); parent; \
118 parent = fwnode_get_next_parent(parent))
120 struct device *fwnode_get_next_parent_dev(const struct fwnode_handle *fwnode);
121 unsigned int fwnode_count_parents(const struct fwnode_handle *fwn);
122 struct fwnode_handle *fwnode_get_nth_parent(struct fwnode_handle *fwn,
124 bool fwnode_is_ancestor_of(const struct fwnode_handle *ancestor, const struct fwnode_handle *child);
125 struct fwnode_handle *fwnode_get_next_child_node(
126 const struct fwnode_handle *fwnode, struct fwnode_handle *child);
127 struct fwnode_handle *fwnode_get_next_available_child_node(
128 const struct fwnode_handle *fwnode, struct fwnode_handle *child);
130 #define fwnode_for_each_child_node(fwnode, child) \
131 for (child = fwnode_get_next_child_node(fwnode, NULL); child; \
132 child = fwnode_get_next_child_node(fwnode, child))
134 #define fwnode_for_each_available_child_node(fwnode, child) \
135 for (child = fwnode_get_next_available_child_node(fwnode, NULL); child;\
136 child = fwnode_get_next_available_child_node(fwnode, child))
138 struct fwnode_handle *device_get_next_child_node(const struct device *dev,
139 struct fwnode_handle *child);
141 #define device_for_each_child_node(dev, child) \
142 for (child = device_get_next_child_node(dev, NULL); child; \
143 child = device_get_next_child_node(dev, child))
145 struct fwnode_handle *fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
146 const char *childname);
147 struct fwnode_handle *device_get_named_child_node(const struct device *dev,
148 const char *childname);
150 struct fwnode_handle *fwnode_handle_get(struct fwnode_handle *fwnode);
151 void fwnode_handle_put(struct fwnode_handle *fwnode);
153 int fwnode_irq_get(const struct fwnode_handle *fwnode, unsigned int index);
154 int fwnode_irq_get_byname(const struct fwnode_handle *fwnode, const char *name);
156 unsigned int device_get_child_node_count(const struct device *dev);
158 static inline bool device_property_read_bool(const struct device *dev,
159 const char *propname)
161 return device_property_present(dev, propname);
164 static inline int device_property_read_u8(const struct device *dev,
165 const char *propname, u8 *val)
167 return device_property_read_u8_array(dev, propname, val, 1);
170 static inline int device_property_read_u16(const struct device *dev,
171 const char *propname, u16 *val)
173 return device_property_read_u16_array(dev, propname, val, 1);
176 static inline int device_property_read_u32(const struct device *dev,
177 const char *propname, u32 *val)
179 return device_property_read_u32_array(dev, propname, val, 1);
182 static inline int device_property_read_u64(const struct device *dev,
183 const char *propname, u64 *val)
185 return device_property_read_u64_array(dev, propname, val, 1);
188 static inline int device_property_count_u8(const struct device *dev, const char *propname)
190 return device_property_read_u8_array(dev, propname, NULL, 0);
193 static inline int device_property_count_u16(const struct device *dev, const char *propname)
195 return device_property_read_u16_array(dev, propname, NULL, 0);
198 static inline int device_property_count_u32(const struct device *dev, const char *propname)
200 return device_property_read_u32_array(dev, propname, NULL, 0);
203 static inline int device_property_count_u64(const struct device *dev, const char *propname)
205 return device_property_read_u64_array(dev, propname, NULL, 0);
208 static inline int device_property_string_array_count(const struct device *dev,
209 const char *propname)
211 return device_property_read_string_array(dev, propname, NULL, 0);
214 static inline bool fwnode_property_read_bool(const struct fwnode_handle *fwnode,
215 const char *propname)
217 return fwnode_property_present(fwnode, propname);
220 static inline int fwnode_property_read_u8(const struct fwnode_handle *fwnode,
221 const char *propname, u8 *val)
223 return fwnode_property_read_u8_array(fwnode, propname, val, 1);
226 static inline int fwnode_property_read_u16(const struct fwnode_handle *fwnode,
227 const char *propname, u16 *val)
229 return fwnode_property_read_u16_array(fwnode, propname, val, 1);
232 static inline int fwnode_property_read_u32(const struct fwnode_handle *fwnode,
233 const char *propname, u32 *val)
235 return fwnode_property_read_u32_array(fwnode, propname, val, 1);
238 static inline int fwnode_property_read_u64(const struct fwnode_handle *fwnode,
239 const char *propname, u64 *val)
241 return fwnode_property_read_u64_array(fwnode, propname, val, 1);
244 static inline int fwnode_property_count_u8(const struct fwnode_handle *fwnode,
245 const char *propname)
247 return fwnode_property_read_u8_array(fwnode, propname, NULL, 0);
250 static inline int fwnode_property_count_u16(const struct fwnode_handle *fwnode,
251 const char *propname)
253 return fwnode_property_read_u16_array(fwnode, propname, NULL, 0);
256 static inline int fwnode_property_count_u32(const struct fwnode_handle *fwnode,
257 const char *propname)
259 return fwnode_property_read_u32_array(fwnode, propname, NULL, 0);
262 static inline int fwnode_property_count_u64(const struct fwnode_handle *fwnode,
263 const char *propname)
265 return fwnode_property_read_u64_array(fwnode, propname, NULL, 0);
269 fwnode_property_string_array_count(const struct fwnode_handle *fwnode,
270 const char *propname)
272 return fwnode_property_read_string_array(fwnode, propname, NULL, 0);
275 struct software_node;
278 * struct software_node_ref_args - Reference property with additional arguments
279 * @node: Reference to a software node
280 * @nargs: Number of elements in @args array
281 * @args: Integer arguments
283 struct software_node_ref_args {
284 const struct software_node *node;
286 u64 args[NR_FWNODE_REFERENCE_ARGS];
289 #define SOFTWARE_NODE_REFERENCE(_ref_, ...) \
290 (const struct software_node_ref_args) { \
292 .nargs = COUNT_ARGS(__VA_ARGS__), \
293 .args = { __VA_ARGS__ }, \
297 * struct property_entry - "Built-in" device property representation.
298 * @name: Name of the property.
299 * @length: Length of data making up the value.
300 * @is_inline: True when the property value is stored inline.
301 * @type: Type of the data in unions.
302 * @pointer: Pointer to the property when it is not stored inline.
303 * @value: Value of the property when it is stored inline.
305 struct property_entry {
309 enum dev_prop_type type;
313 u8 u8_data[sizeof(u64) / sizeof(u8)];
314 u16 u16_data[sizeof(u64) / sizeof(u16)];
315 u32 u32_data[sizeof(u64) / sizeof(u32)];
316 u64 u64_data[sizeof(u64) / sizeof(u64)];
317 const char *str[sizeof(u64) / sizeof(char *)];
323 * Note: the below initializers for the anonymous union are carefully
324 * crafted to avoid gcc-4.4.4's problems with initialization of anon unions
327 #define __PROPERTY_ENTRY_ARRAY_LEN(_name_, _elem_, _Type_, _val_, _len_) \
328 (struct property_entry) { \
330 .length = (_len_) * sizeof_field(struct property_entry, value._elem_[0]), \
331 .type = DEV_PROP_##_Type_, \
332 { .pointer = _val_ }, \
335 #define PROPERTY_ENTRY_U8_ARRAY_LEN(_name_, _val_, _len_) \
336 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u8_data, U8, _val_, _len_)
337 #define PROPERTY_ENTRY_U16_ARRAY_LEN(_name_, _val_, _len_) \
338 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u16_data, U16, _val_, _len_)
339 #define PROPERTY_ENTRY_U32_ARRAY_LEN(_name_, _val_, _len_) \
340 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u32_data, U32, _val_, _len_)
341 #define PROPERTY_ENTRY_U64_ARRAY_LEN(_name_, _val_, _len_) \
342 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u64_data, U64, _val_, _len_)
343 #define PROPERTY_ENTRY_STRING_ARRAY_LEN(_name_, _val_, _len_) \
344 __PROPERTY_ENTRY_ARRAY_LEN(_name_, str, STRING, _val_, _len_)
346 #define PROPERTY_ENTRY_REF_ARRAY_LEN(_name_, _val_, _len_) \
347 (struct property_entry) { \
349 .length = (_len_) * sizeof(struct software_node_ref_args), \
350 .type = DEV_PROP_REF, \
351 { .pointer = _val_ }, \
354 #define PROPERTY_ENTRY_U8_ARRAY(_name_, _val_) \
355 PROPERTY_ENTRY_U8_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
356 #define PROPERTY_ENTRY_U16_ARRAY(_name_, _val_) \
357 PROPERTY_ENTRY_U16_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
358 #define PROPERTY_ENTRY_U32_ARRAY(_name_, _val_) \
359 PROPERTY_ENTRY_U32_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
360 #define PROPERTY_ENTRY_U64_ARRAY(_name_, _val_) \
361 PROPERTY_ENTRY_U64_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
362 #define PROPERTY_ENTRY_STRING_ARRAY(_name_, _val_) \
363 PROPERTY_ENTRY_STRING_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
364 #define PROPERTY_ENTRY_REF_ARRAY(_name_, _val_) \
365 PROPERTY_ENTRY_REF_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
367 #define __PROPERTY_ENTRY_ELEMENT(_name_, _elem_, _Type_, _val_) \
368 (struct property_entry) { \
370 .length = sizeof_field(struct property_entry, value._elem_[0]), \
372 .type = DEV_PROP_##_Type_, \
373 { .value = { ._elem_[0] = _val_ } }, \
376 #define PROPERTY_ENTRY_U8(_name_, _val_) \
377 __PROPERTY_ENTRY_ELEMENT(_name_, u8_data, U8, _val_)
378 #define PROPERTY_ENTRY_U16(_name_, _val_) \
379 __PROPERTY_ENTRY_ELEMENT(_name_, u16_data, U16, _val_)
380 #define PROPERTY_ENTRY_U32(_name_, _val_) \
381 __PROPERTY_ENTRY_ELEMENT(_name_, u32_data, U32, _val_)
382 #define PROPERTY_ENTRY_U64(_name_, _val_) \
383 __PROPERTY_ENTRY_ELEMENT(_name_, u64_data, U64, _val_)
384 #define PROPERTY_ENTRY_STRING(_name_, _val_) \
385 __PROPERTY_ENTRY_ELEMENT(_name_, str, STRING, _val_)
387 #define PROPERTY_ENTRY_REF(_name_, _ref_, ...) \
388 (struct property_entry) { \
390 .length = sizeof(struct software_node_ref_args), \
391 .type = DEV_PROP_REF, \
392 { .pointer = &SOFTWARE_NODE_REFERENCE(_ref_, ##__VA_ARGS__), }, \
395 #define PROPERTY_ENTRY_BOOL(_name_) \
396 (struct property_entry) { \
401 struct property_entry *
402 property_entries_dup(const struct property_entry *properties);
403 void property_entries_free(const struct property_entry *properties);
405 bool device_dma_supported(const struct device *dev);
406 enum dev_dma_attr device_get_dma_attr(const struct device *dev);
408 const void *device_get_match_data(const struct device *dev);
410 int device_get_phy_mode(struct device *dev);
411 int fwnode_get_phy_mode(const struct fwnode_handle *fwnode);
413 void __iomem *fwnode_iomap(struct fwnode_handle *fwnode, int index);
415 struct fwnode_handle *fwnode_graph_get_next_endpoint(
416 const struct fwnode_handle *fwnode, struct fwnode_handle *prev);
417 struct fwnode_handle *
418 fwnode_graph_get_port_parent(const struct fwnode_handle *fwnode);
419 struct fwnode_handle *fwnode_graph_get_remote_port_parent(
420 const struct fwnode_handle *fwnode);
421 struct fwnode_handle *fwnode_graph_get_remote_port(
422 const struct fwnode_handle *fwnode);
423 struct fwnode_handle *fwnode_graph_get_remote_endpoint(
424 const struct fwnode_handle *fwnode);
426 static inline bool fwnode_graph_is_endpoint(const struct fwnode_handle *fwnode)
428 return fwnode_property_present(fwnode, "remote-endpoint");
432 * Fwnode lookup flags
434 * @FWNODE_GRAPH_ENDPOINT_NEXT: In the case of no exact match, look for the
435 * closest endpoint ID greater than the specified
437 * @FWNODE_GRAPH_DEVICE_DISABLED: That the device to which the remote
438 * endpoint of the given endpoint belongs to,
439 * may be disabled, or that the endpoint is not
442 #define FWNODE_GRAPH_ENDPOINT_NEXT BIT(0)
443 #define FWNODE_GRAPH_DEVICE_DISABLED BIT(1)
445 struct fwnode_handle *
446 fwnode_graph_get_endpoint_by_id(const struct fwnode_handle *fwnode,
447 u32 port, u32 endpoint, unsigned long flags);
448 unsigned int fwnode_graph_get_endpoint_count(const struct fwnode_handle *fwnode,
449 unsigned long flags);
451 #define fwnode_graph_for_each_endpoint(fwnode, child) \
452 for (child = fwnode_graph_get_next_endpoint(fwnode, NULL); child; \
453 child = fwnode_graph_get_next_endpoint(fwnode, child))
455 int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
456 struct fwnode_endpoint *endpoint);
458 typedef void *(*devcon_match_fn_t)(const struct fwnode_handle *fwnode, const char *id,
461 void *fwnode_connection_find_match(const struct fwnode_handle *fwnode,
462 const char *con_id, void *data,
463 devcon_match_fn_t match);
465 static inline void *device_connection_find_match(const struct device *dev,
466 const char *con_id, void *data,
467 devcon_match_fn_t match)
469 return fwnode_connection_find_match(dev_fwnode(dev), con_id, data, match);
472 int fwnode_connection_find_matches(const struct fwnode_handle *fwnode,
473 const char *con_id, void *data,
474 devcon_match_fn_t match,
475 void **matches, unsigned int matches_len);
477 /* -------------------------------------------------------------------------- */
478 /* Software fwnode support - when HW description is incomplete or missing */
481 * struct software_node - Software node description
482 * @name: Name of the software node
483 * @parent: Parent of the software node
484 * @properties: Array of device properties
486 struct software_node {
488 const struct software_node *parent;
489 const struct property_entry *properties;
492 bool is_software_node(const struct fwnode_handle *fwnode);
493 const struct software_node *
494 to_software_node(const struct fwnode_handle *fwnode);
495 struct fwnode_handle *software_node_fwnode(const struct software_node *node);
497 const struct software_node *
498 software_node_find_by_name(const struct software_node *parent,
501 int software_node_register_node_group(const struct software_node **node_group);
502 void software_node_unregister_node_group(const struct software_node **node_group);
504 int software_node_register(const struct software_node *node);
505 void software_node_unregister(const struct software_node *node);
507 struct fwnode_handle *
508 fwnode_create_software_node(const struct property_entry *properties,
509 const struct fwnode_handle *parent);
510 void fwnode_remove_software_node(struct fwnode_handle *fwnode);
512 int device_add_software_node(struct device *dev, const struct software_node *node);
513 void device_remove_software_node(struct device *dev);
515 int device_create_managed_software_node(struct device *dev,
516 const struct property_entry *properties,
517 const struct software_node *parent);
519 #endif /* _LINUX_PROPERTY_H_ */