1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * property.h - Unified device property interface.
5 * Copyright (C) 2014, Intel Corporation
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);
83 static inline bool fwnode_device_is_big_endian(const struct fwnode_handle *fwnode)
85 if (fwnode_property_present(fwnode, "big-endian"))
87 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
88 fwnode_property_present(fwnode, "native-endian"))
94 bool fwnode_device_is_compatible(const struct fwnode_handle *fwnode, const char *compat)
96 return fwnode_property_match_string(fwnode, "compatible", compat) >= 0;
100 * device_is_big_endian - check if a device has BE registers
101 * @dev: Pointer to the struct device
103 * Returns: true if the device has a "big-endian" property, or if the kernel
104 * was compiled for BE *and* the device has a "native-endian" property.
105 * Returns false otherwise.
107 * Callers would nominally use ioread32be/iowrite32be if
108 * device_is_big_endian() == true, or readl/writel otherwise.
110 static inline bool device_is_big_endian(const struct device *dev)
112 return fwnode_device_is_big_endian(dev_fwnode(dev));
116 * device_is_compatible - match 'compatible' property of the device with a given string
117 * @dev: Pointer to the struct device
118 * @compat: The string to match 'compatible' property with
120 * Returns: true if matches, otherwise false.
122 static inline bool device_is_compatible(const struct device *dev, const char *compat)
124 return fwnode_device_is_compatible(dev_fwnode(dev), compat);
127 int fwnode_property_match_property_string(const struct fwnode_handle *fwnode,
128 const char *propname,
129 const char * const *array, size_t n);
132 int device_property_match_property_string(const struct device *dev,
133 const char *propname,
134 const char * const *array, size_t n)
136 return fwnode_property_match_property_string(dev_fwnode(dev), propname, array, n);
139 int fwnode_property_get_reference_args(const struct fwnode_handle *fwnode,
140 const char *prop, const char *nargs_prop,
141 unsigned int nargs, unsigned int index,
142 struct fwnode_reference_args *args);
144 struct fwnode_handle *fwnode_find_reference(const struct fwnode_handle *fwnode,
148 const char *fwnode_get_name(const struct fwnode_handle *fwnode);
149 const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode);
150 bool fwnode_name_eq(const struct fwnode_handle *fwnode, const char *name);
152 struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode);
153 struct fwnode_handle *fwnode_get_next_parent(struct fwnode_handle *fwnode);
155 #define fwnode_for_each_parent_node(fwnode, parent) \
156 for (parent = fwnode_get_parent(fwnode); parent; \
157 parent = fwnode_get_next_parent(parent))
159 struct device *fwnode_get_next_parent_dev(const struct fwnode_handle *fwnode);
160 unsigned int fwnode_count_parents(const struct fwnode_handle *fwn);
161 struct fwnode_handle *fwnode_get_nth_parent(struct fwnode_handle *fwn,
163 bool fwnode_is_ancestor_of(const struct fwnode_handle *ancestor, const struct fwnode_handle *child);
164 struct fwnode_handle *fwnode_get_next_child_node(
165 const struct fwnode_handle *fwnode, struct fwnode_handle *child);
166 struct fwnode_handle *fwnode_get_next_available_child_node(
167 const struct fwnode_handle *fwnode, struct fwnode_handle *child);
169 #define fwnode_for_each_child_node(fwnode, child) \
170 for (child = fwnode_get_next_child_node(fwnode, NULL); child; \
171 child = fwnode_get_next_child_node(fwnode, child))
173 #define fwnode_for_each_available_child_node(fwnode, child) \
174 for (child = fwnode_get_next_available_child_node(fwnode, NULL); child;\
175 child = fwnode_get_next_available_child_node(fwnode, child))
177 struct fwnode_handle *device_get_next_child_node(const struct device *dev,
178 struct fwnode_handle *child);
180 #define device_for_each_child_node(dev, child) \
181 for (child = device_get_next_child_node(dev, NULL); child; \
182 child = device_get_next_child_node(dev, child))
184 struct fwnode_handle *fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
185 const char *childname);
186 struct fwnode_handle *device_get_named_child_node(const struct device *dev,
187 const char *childname);
189 struct fwnode_handle *fwnode_handle_get(struct fwnode_handle *fwnode);
190 void fwnode_handle_put(struct fwnode_handle *fwnode);
192 int fwnode_irq_get(const struct fwnode_handle *fwnode, unsigned int index);
193 int fwnode_irq_get_byname(const struct fwnode_handle *fwnode, const char *name);
195 unsigned int device_get_child_node_count(const struct device *dev);
197 static inline bool device_property_read_bool(const struct device *dev,
198 const char *propname)
200 return device_property_present(dev, propname);
203 static inline int device_property_read_u8(const struct device *dev,
204 const char *propname, u8 *val)
206 return device_property_read_u8_array(dev, propname, val, 1);
209 static inline int device_property_read_u16(const struct device *dev,
210 const char *propname, u16 *val)
212 return device_property_read_u16_array(dev, propname, val, 1);
215 static inline int device_property_read_u32(const struct device *dev,
216 const char *propname, u32 *val)
218 return device_property_read_u32_array(dev, propname, val, 1);
221 static inline int device_property_read_u64(const struct device *dev,
222 const char *propname, u64 *val)
224 return device_property_read_u64_array(dev, propname, val, 1);
227 static inline int device_property_count_u8(const struct device *dev, const char *propname)
229 return device_property_read_u8_array(dev, propname, NULL, 0);
232 static inline int device_property_count_u16(const struct device *dev, const char *propname)
234 return device_property_read_u16_array(dev, propname, NULL, 0);
237 static inline int device_property_count_u32(const struct device *dev, const char *propname)
239 return device_property_read_u32_array(dev, propname, NULL, 0);
242 static inline int device_property_count_u64(const struct device *dev, const char *propname)
244 return device_property_read_u64_array(dev, propname, NULL, 0);
247 static inline int device_property_string_array_count(const struct device *dev,
248 const char *propname)
250 return device_property_read_string_array(dev, propname, NULL, 0);
253 static inline bool fwnode_property_read_bool(const struct fwnode_handle *fwnode,
254 const char *propname)
256 return fwnode_property_present(fwnode, propname);
259 static inline int fwnode_property_read_u8(const struct fwnode_handle *fwnode,
260 const char *propname, u8 *val)
262 return fwnode_property_read_u8_array(fwnode, propname, val, 1);
265 static inline int fwnode_property_read_u16(const struct fwnode_handle *fwnode,
266 const char *propname, u16 *val)
268 return fwnode_property_read_u16_array(fwnode, propname, val, 1);
271 static inline int fwnode_property_read_u32(const struct fwnode_handle *fwnode,
272 const char *propname, u32 *val)
274 return fwnode_property_read_u32_array(fwnode, propname, val, 1);
277 static inline int fwnode_property_read_u64(const struct fwnode_handle *fwnode,
278 const char *propname, u64 *val)
280 return fwnode_property_read_u64_array(fwnode, propname, val, 1);
283 static inline int fwnode_property_count_u8(const struct fwnode_handle *fwnode,
284 const char *propname)
286 return fwnode_property_read_u8_array(fwnode, propname, NULL, 0);
289 static inline int fwnode_property_count_u16(const struct fwnode_handle *fwnode,
290 const char *propname)
292 return fwnode_property_read_u16_array(fwnode, propname, NULL, 0);
295 static inline int fwnode_property_count_u32(const struct fwnode_handle *fwnode,
296 const char *propname)
298 return fwnode_property_read_u32_array(fwnode, propname, NULL, 0);
301 static inline int fwnode_property_count_u64(const struct fwnode_handle *fwnode,
302 const char *propname)
304 return fwnode_property_read_u64_array(fwnode, propname, NULL, 0);
308 fwnode_property_string_array_count(const struct fwnode_handle *fwnode,
309 const char *propname)
311 return fwnode_property_read_string_array(fwnode, propname, NULL, 0);
314 struct software_node;
317 * struct software_node_ref_args - Reference property with additional arguments
318 * @node: Reference to a software node
319 * @nargs: Number of elements in @args array
320 * @args: Integer arguments
322 struct software_node_ref_args {
323 const struct software_node *node;
325 u64 args[NR_FWNODE_REFERENCE_ARGS];
328 #define SOFTWARE_NODE_REFERENCE(_ref_, ...) \
329 (const struct software_node_ref_args) { \
331 .nargs = COUNT_ARGS(__VA_ARGS__), \
332 .args = { __VA_ARGS__ }, \
336 * struct property_entry - "Built-in" device property representation.
337 * @name: Name of the property.
338 * @length: Length of data making up the value.
339 * @is_inline: True when the property value is stored inline.
340 * @type: Type of the data in unions.
341 * @pointer: Pointer to the property when it is not stored inline.
342 * @value: Value of the property when it is stored inline.
344 struct property_entry {
348 enum dev_prop_type type;
352 u8 u8_data[sizeof(u64) / sizeof(u8)];
353 u16 u16_data[sizeof(u64) / sizeof(u16)];
354 u32 u32_data[sizeof(u64) / sizeof(u32)];
355 u64 u64_data[sizeof(u64) / sizeof(u64)];
356 const char *str[sizeof(u64) / sizeof(char *)];
362 * Note: the below initializers for the anonymous union are carefully
363 * crafted to avoid gcc-4.4.4's problems with initialization of anon unions
366 #define __PROPERTY_ENTRY_ARRAY_LEN(_name_, _elem_, _Type_, _val_, _len_) \
367 (struct property_entry) { \
369 .length = (_len_) * sizeof_field(struct property_entry, value._elem_[0]), \
370 .type = DEV_PROP_##_Type_, \
371 { .pointer = _val_ }, \
374 #define PROPERTY_ENTRY_U8_ARRAY_LEN(_name_, _val_, _len_) \
375 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u8_data, U8, _val_, _len_)
376 #define PROPERTY_ENTRY_U16_ARRAY_LEN(_name_, _val_, _len_) \
377 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u16_data, U16, _val_, _len_)
378 #define PROPERTY_ENTRY_U32_ARRAY_LEN(_name_, _val_, _len_) \
379 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u32_data, U32, _val_, _len_)
380 #define PROPERTY_ENTRY_U64_ARRAY_LEN(_name_, _val_, _len_) \
381 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u64_data, U64, _val_, _len_)
382 #define PROPERTY_ENTRY_STRING_ARRAY_LEN(_name_, _val_, _len_) \
383 __PROPERTY_ENTRY_ARRAY_LEN(_name_, str, STRING, _val_, _len_)
385 #define PROPERTY_ENTRY_REF_ARRAY_LEN(_name_, _val_, _len_) \
386 (struct property_entry) { \
388 .length = (_len_) * sizeof(struct software_node_ref_args), \
389 .type = DEV_PROP_REF, \
390 { .pointer = _val_ }, \
393 #define PROPERTY_ENTRY_U8_ARRAY(_name_, _val_) \
394 PROPERTY_ENTRY_U8_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
395 #define PROPERTY_ENTRY_U16_ARRAY(_name_, _val_) \
396 PROPERTY_ENTRY_U16_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
397 #define PROPERTY_ENTRY_U32_ARRAY(_name_, _val_) \
398 PROPERTY_ENTRY_U32_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
399 #define PROPERTY_ENTRY_U64_ARRAY(_name_, _val_) \
400 PROPERTY_ENTRY_U64_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
401 #define PROPERTY_ENTRY_STRING_ARRAY(_name_, _val_) \
402 PROPERTY_ENTRY_STRING_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
403 #define PROPERTY_ENTRY_REF_ARRAY(_name_, _val_) \
404 PROPERTY_ENTRY_REF_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
406 #define __PROPERTY_ENTRY_ELEMENT(_name_, _elem_, _Type_, _val_) \
407 (struct property_entry) { \
409 .length = sizeof_field(struct property_entry, value._elem_[0]), \
411 .type = DEV_PROP_##_Type_, \
412 { .value = { ._elem_[0] = _val_ } }, \
415 #define PROPERTY_ENTRY_U8(_name_, _val_) \
416 __PROPERTY_ENTRY_ELEMENT(_name_, u8_data, U8, _val_)
417 #define PROPERTY_ENTRY_U16(_name_, _val_) \
418 __PROPERTY_ENTRY_ELEMENT(_name_, u16_data, U16, _val_)
419 #define PROPERTY_ENTRY_U32(_name_, _val_) \
420 __PROPERTY_ENTRY_ELEMENT(_name_, u32_data, U32, _val_)
421 #define PROPERTY_ENTRY_U64(_name_, _val_) \
422 __PROPERTY_ENTRY_ELEMENT(_name_, u64_data, U64, _val_)
423 #define PROPERTY_ENTRY_STRING(_name_, _val_) \
424 __PROPERTY_ENTRY_ELEMENT(_name_, str, STRING, _val_)
426 #define PROPERTY_ENTRY_REF(_name_, _ref_, ...) \
427 (struct property_entry) { \
429 .length = sizeof(struct software_node_ref_args), \
430 .type = DEV_PROP_REF, \
431 { .pointer = &SOFTWARE_NODE_REFERENCE(_ref_, ##__VA_ARGS__), }, \
434 #define PROPERTY_ENTRY_BOOL(_name_) \
435 (struct property_entry) { \
440 struct property_entry *
441 property_entries_dup(const struct property_entry *properties);
442 void property_entries_free(const struct property_entry *properties);
444 bool device_dma_supported(const struct device *dev);
445 enum dev_dma_attr device_get_dma_attr(const struct device *dev);
447 const void *device_get_match_data(const struct device *dev);
449 int device_get_phy_mode(struct device *dev);
450 int fwnode_get_phy_mode(const struct fwnode_handle *fwnode);
452 void __iomem *fwnode_iomap(struct fwnode_handle *fwnode, int index);
454 struct fwnode_handle *fwnode_graph_get_next_endpoint(
455 const struct fwnode_handle *fwnode, struct fwnode_handle *prev);
456 struct fwnode_handle *
457 fwnode_graph_get_port_parent(const struct fwnode_handle *fwnode);
458 struct fwnode_handle *fwnode_graph_get_remote_port_parent(
459 const struct fwnode_handle *fwnode);
460 struct fwnode_handle *fwnode_graph_get_remote_port(
461 const struct fwnode_handle *fwnode);
462 struct fwnode_handle *fwnode_graph_get_remote_endpoint(
463 const struct fwnode_handle *fwnode);
465 static inline bool fwnode_graph_is_endpoint(const struct fwnode_handle *fwnode)
467 return fwnode_property_present(fwnode, "remote-endpoint");
471 * Fwnode lookup flags
473 * @FWNODE_GRAPH_ENDPOINT_NEXT: In the case of no exact match, look for the
474 * closest endpoint ID greater than the specified
476 * @FWNODE_GRAPH_DEVICE_DISABLED: That the device to which the remote
477 * endpoint of the given endpoint belongs to,
478 * may be disabled, or that the endpoint is not
481 #define FWNODE_GRAPH_ENDPOINT_NEXT BIT(0)
482 #define FWNODE_GRAPH_DEVICE_DISABLED BIT(1)
484 struct fwnode_handle *
485 fwnode_graph_get_endpoint_by_id(const struct fwnode_handle *fwnode,
486 u32 port, u32 endpoint, unsigned long flags);
487 unsigned int fwnode_graph_get_endpoint_count(const struct fwnode_handle *fwnode,
488 unsigned long flags);
490 #define fwnode_graph_for_each_endpoint(fwnode, child) \
491 for (child = fwnode_graph_get_next_endpoint(fwnode, NULL); child; \
492 child = fwnode_graph_get_next_endpoint(fwnode, child))
494 int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
495 struct fwnode_endpoint *endpoint);
497 typedef void *(*devcon_match_fn_t)(const struct fwnode_handle *fwnode, const char *id,
500 void *fwnode_connection_find_match(const struct fwnode_handle *fwnode,
501 const char *con_id, void *data,
502 devcon_match_fn_t match);
504 static inline void *device_connection_find_match(const struct device *dev,
505 const char *con_id, void *data,
506 devcon_match_fn_t match)
508 return fwnode_connection_find_match(dev_fwnode(dev), con_id, data, match);
511 int fwnode_connection_find_matches(const struct fwnode_handle *fwnode,
512 const char *con_id, void *data,
513 devcon_match_fn_t match,
514 void **matches, unsigned int matches_len);
516 /* -------------------------------------------------------------------------- */
517 /* Software fwnode support - when HW description is incomplete or missing */
520 * struct software_node - Software node description
521 * @name: Name of the software node
522 * @parent: Parent of the software node
523 * @properties: Array of device properties
525 struct software_node {
527 const struct software_node *parent;
528 const struct property_entry *properties;
531 #define SOFTWARE_NODE(_name_, _properties_, _parent_) \
532 (struct software_node) { \
534 .properties = _properties_, \
535 .parent = _parent_, \
538 bool is_software_node(const struct fwnode_handle *fwnode);
539 const struct software_node *
540 to_software_node(const struct fwnode_handle *fwnode);
541 struct fwnode_handle *software_node_fwnode(const struct software_node *node);
543 const struct software_node *
544 software_node_find_by_name(const struct software_node *parent,
547 int software_node_register_node_group(const struct software_node **node_group);
548 void software_node_unregister_node_group(const struct software_node **node_group);
550 int software_node_register(const struct software_node *node);
551 void software_node_unregister(const struct software_node *node);
553 struct fwnode_handle *
554 fwnode_create_software_node(const struct property_entry *properties,
555 const struct fwnode_handle *parent);
556 void fwnode_remove_software_node(struct fwnode_handle *fwnode);
558 int device_add_software_node(struct device *dev, const struct software_node *node);
559 void device_remove_software_node(struct device *dev);
561 int device_create_managed_software_node(struct device *dev,
562 const struct property_entry *properties,
563 const struct software_node *parent);
565 #endif /* _LINUX_PROPERTY_H_ */