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);
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_get_reference_args(const struct fwnode_handle *fwnode,
128 const char *prop, const char *nargs_prop,
129 unsigned int nargs, unsigned int index,
130 struct fwnode_reference_args *args);
132 struct fwnode_handle *fwnode_find_reference(const struct fwnode_handle *fwnode,
136 const char *fwnode_get_name(const struct fwnode_handle *fwnode);
137 const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode);
139 struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode);
140 struct fwnode_handle *fwnode_get_next_parent(struct fwnode_handle *fwnode);
142 #define fwnode_for_each_parent_node(fwnode, parent) \
143 for (parent = fwnode_get_parent(fwnode); parent; \
144 parent = fwnode_get_next_parent(parent))
146 struct device *fwnode_get_next_parent_dev(const struct fwnode_handle *fwnode);
147 unsigned int fwnode_count_parents(const struct fwnode_handle *fwn);
148 struct fwnode_handle *fwnode_get_nth_parent(struct fwnode_handle *fwn,
150 bool fwnode_is_ancestor_of(const struct fwnode_handle *ancestor, const struct fwnode_handle *child);
151 struct fwnode_handle *fwnode_get_next_child_node(
152 const struct fwnode_handle *fwnode, struct fwnode_handle *child);
153 struct fwnode_handle *fwnode_get_next_available_child_node(
154 const struct fwnode_handle *fwnode, struct fwnode_handle *child);
156 #define fwnode_for_each_child_node(fwnode, child) \
157 for (child = fwnode_get_next_child_node(fwnode, NULL); child; \
158 child = fwnode_get_next_child_node(fwnode, child))
160 #define fwnode_for_each_available_child_node(fwnode, child) \
161 for (child = fwnode_get_next_available_child_node(fwnode, NULL); child;\
162 child = fwnode_get_next_available_child_node(fwnode, child))
164 struct fwnode_handle *device_get_next_child_node(const struct device *dev,
165 struct fwnode_handle *child);
167 #define device_for_each_child_node(dev, child) \
168 for (child = device_get_next_child_node(dev, NULL); child; \
169 child = device_get_next_child_node(dev, child))
171 struct fwnode_handle *fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
172 const char *childname);
173 struct fwnode_handle *device_get_named_child_node(const struct device *dev,
174 const char *childname);
176 struct fwnode_handle *fwnode_handle_get(struct fwnode_handle *fwnode);
177 void fwnode_handle_put(struct fwnode_handle *fwnode);
179 int fwnode_irq_get(const struct fwnode_handle *fwnode, unsigned int index);
180 int fwnode_irq_get_byname(const struct fwnode_handle *fwnode, const char *name);
182 unsigned int device_get_child_node_count(const struct device *dev);
184 static inline bool device_property_read_bool(const struct device *dev,
185 const char *propname)
187 return device_property_present(dev, propname);
190 static inline int device_property_read_u8(const struct device *dev,
191 const char *propname, u8 *val)
193 return device_property_read_u8_array(dev, propname, val, 1);
196 static inline int device_property_read_u16(const struct device *dev,
197 const char *propname, u16 *val)
199 return device_property_read_u16_array(dev, propname, val, 1);
202 static inline int device_property_read_u32(const struct device *dev,
203 const char *propname, u32 *val)
205 return device_property_read_u32_array(dev, propname, val, 1);
208 static inline int device_property_read_u64(const struct device *dev,
209 const char *propname, u64 *val)
211 return device_property_read_u64_array(dev, propname, val, 1);
214 static inline int device_property_count_u8(const struct device *dev, const char *propname)
216 return device_property_read_u8_array(dev, propname, NULL, 0);
219 static inline int device_property_count_u16(const struct device *dev, const char *propname)
221 return device_property_read_u16_array(dev, propname, NULL, 0);
224 static inline int device_property_count_u32(const struct device *dev, const char *propname)
226 return device_property_read_u32_array(dev, propname, NULL, 0);
229 static inline int device_property_count_u64(const struct device *dev, const char *propname)
231 return device_property_read_u64_array(dev, propname, NULL, 0);
234 static inline int device_property_string_array_count(const struct device *dev,
235 const char *propname)
237 return device_property_read_string_array(dev, propname, NULL, 0);
240 static inline bool fwnode_property_read_bool(const struct fwnode_handle *fwnode,
241 const char *propname)
243 return fwnode_property_present(fwnode, propname);
246 static inline int fwnode_property_read_u8(const struct fwnode_handle *fwnode,
247 const char *propname, u8 *val)
249 return fwnode_property_read_u8_array(fwnode, propname, val, 1);
252 static inline int fwnode_property_read_u16(const struct fwnode_handle *fwnode,
253 const char *propname, u16 *val)
255 return fwnode_property_read_u16_array(fwnode, propname, val, 1);
258 static inline int fwnode_property_read_u32(const struct fwnode_handle *fwnode,
259 const char *propname, u32 *val)
261 return fwnode_property_read_u32_array(fwnode, propname, val, 1);
264 static inline int fwnode_property_read_u64(const struct fwnode_handle *fwnode,
265 const char *propname, u64 *val)
267 return fwnode_property_read_u64_array(fwnode, propname, val, 1);
270 static inline int fwnode_property_count_u8(const struct fwnode_handle *fwnode,
271 const char *propname)
273 return fwnode_property_read_u8_array(fwnode, propname, NULL, 0);
276 static inline int fwnode_property_count_u16(const struct fwnode_handle *fwnode,
277 const char *propname)
279 return fwnode_property_read_u16_array(fwnode, propname, NULL, 0);
282 static inline int fwnode_property_count_u32(const struct fwnode_handle *fwnode,
283 const char *propname)
285 return fwnode_property_read_u32_array(fwnode, propname, NULL, 0);
288 static inline int fwnode_property_count_u64(const struct fwnode_handle *fwnode,
289 const char *propname)
291 return fwnode_property_read_u64_array(fwnode, propname, NULL, 0);
295 fwnode_property_string_array_count(const struct fwnode_handle *fwnode,
296 const char *propname)
298 return fwnode_property_read_string_array(fwnode, propname, NULL, 0);
301 struct software_node;
304 * struct software_node_ref_args - Reference property with additional arguments
305 * @node: Reference to a software node
306 * @nargs: Number of elements in @args array
307 * @args: Integer arguments
309 struct software_node_ref_args {
310 const struct software_node *node;
312 u64 args[NR_FWNODE_REFERENCE_ARGS];
315 #define SOFTWARE_NODE_REFERENCE(_ref_, ...) \
316 (const struct software_node_ref_args) { \
318 .nargs = COUNT_ARGS(__VA_ARGS__), \
319 .args = { __VA_ARGS__ }, \
323 * struct property_entry - "Built-in" device property representation.
324 * @name: Name of the property.
325 * @length: Length of data making up the value.
326 * @is_inline: True when the property value is stored inline.
327 * @type: Type of the data in unions.
328 * @pointer: Pointer to the property when it is not stored inline.
329 * @value: Value of the property when it is stored inline.
331 struct property_entry {
335 enum dev_prop_type type;
339 u8 u8_data[sizeof(u64) / sizeof(u8)];
340 u16 u16_data[sizeof(u64) / sizeof(u16)];
341 u32 u32_data[sizeof(u64) / sizeof(u32)];
342 u64 u64_data[sizeof(u64) / sizeof(u64)];
343 const char *str[sizeof(u64) / sizeof(char *)];
349 * Note: the below initializers for the anonymous union are carefully
350 * crafted to avoid gcc-4.4.4's problems with initialization of anon unions
353 #define __PROPERTY_ENTRY_ARRAY_LEN(_name_, _elem_, _Type_, _val_, _len_) \
354 (struct property_entry) { \
356 .length = (_len_) * sizeof_field(struct property_entry, value._elem_[0]), \
357 .type = DEV_PROP_##_Type_, \
358 { .pointer = _val_ }, \
361 #define PROPERTY_ENTRY_U8_ARRAY_LEN(_name_, _val_, _len_) \
362 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u8_data, U8, _val_, _len_)
363 #define PROPERTY_ENTRY_U16_ARRAY_LEN(_name_, _val_, _len_) \
364 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u16_data, U16, _val_, _len_)
365 #define PROPERTY_ENTRY_U32_ARRAY_LEN(_name_, _val_, _len_) \
366 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u32_data, U32, _val_, _len_)
367 #define PROPERTY_ENTRY_U64_ARRAY_LEN(_name_, _val_, _len_) \
368 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u64_data, U64, _val_, _len_)
369 #define PROPERTY_ENTRY_STRING_ARRAY_LEN(_name_, _val_, _len_) \
370 __PROPERTY_ENTRY_ARRAY_LEN(_name_, str, STRING, _val_, _len_)
372 #define PROPERTY_ENTRY_REF_ARRAY_LEN(_name_, _val_, _len_) \
373 (struct property_entry) { \
375 .length = (_len_) * sizeof(struct software_node_ref_args), \
376 .type = DEV_PROP_REF, \
377 { .pointer = _val_ }, \
380 #define PROPERTY_ENTRY_U8_ARRAY(_name_, _val_) \
381 PROPERTY_ENTRY_U8_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
382 #define PROPERTY_ENTRY_U16_ARRAY(_name_, _val_) \
383 PROPERTY_ENTRY_U16_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
384 #define PROPERTY_ENTRY_U32_ARRAY(_name_, _val_) \
385 PROPERTY_ENTRY_U32_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
386 #define PROPERTY_ENTRY_U64_ARRAY(_name_, _val_) \
387 PROPERTY_ENTRY_U64_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
388 #define PROPERTY_ENTRY_STRING_ARRAY(_name_, _val_) \
389 PROPERTY_ENTRY_STRING_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
390 #define PROPERTY_ENTRY_REF_ARRAY(_name_, _val_) \
391 PROPERTY_ENTRY_REF_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
393 #define __PROPERTY_ENTRY_ELEMENT(_name_, _elem_, _Type_, _val_) \
394 (struct property_entry) { \
396 .length = sizeof_field(struct property_entry, value._elem_[0]), \
398 .type = DEV_PROP_##_Type_, \
399 { .value = { ._elem_[0] = _val_ } }, \
402 #define PROPERTY_ENTRY_U8(_name_, _val_) \
403 __PROPERTY_ENTRY_ELEMENT(_name_, u8_data, U8, _val_)
404 #define PROPERTY_ENTRY_U16(_name_, _val_) \
405 __PROPERTY_ENTRY_ELEMENT(_name_, u16_data, U16, _val_)
406 #define PROPERTY_ENTRY_U32(_name_, _val_) \
407 __PROPERTY_ENTRY_ELEMENT(_name_, u32_data, U32, _val_)
408 #define PROPERTY_ENTRY_U64(_name_, _val_) \
409 __PROPERTY_ENTRY_ELEMENT(_name_, u64_data, U64, _val_)
410 #define PROPERTY_ENTRY_STRING(_name_, _val_) \
411 __PROPERTY_ENTRY_ELEMENT(_name_, str, STRING, _val_)
413 #define PROPERTY_ENTRY_REF(_name_, _ref_, ...) \
414 (struct property_entry) { \
416 .length = sizeof(struct software_node_ref_args), \
417 .type = DEV_PROP_REF, \
418 { .pointer = &SOFTWARE_NODE_REFERENCE(_ref_, ##__VA_ARGS__), }, \
421 #define PROPERTY_ENTRY_BOOL(_name_) \
422 (struct property_entry) { \
427 struct property_entry *
428 property_entries_dup(const struct property_entry *properties);
429 void property_entries_free(const struct property_entry *properties);
431 bool device_dma_supported(const struct device *dev);
432 enum dev_dma_attr device_get_dma_attr(const struct device *dev);
434 const void *device_get_match_data(const struct device *dev);
436 int device_get_phy_mode(struct device *dev);
437 int fwnode_get_phy_mode(const struct fwnode_handle *fwnode);
439 void __iomem *fwnode_iomap(struct fwnode_handle *fwnode, int index);
441 struct fwnode_handle *fwnode_graph_get_next_endpoint(
442 const struct fwnode_handle *fwnode, struct fwnode_handle *prev);
443 struct fwnode_handle *
444 fwnode_graph_get_port_parent(const struct fwnode_handle *fwnode);
445 struct fwnode_handle *fwnode_graph_get_remote_port_parent(
446 const struct fwnode_handle *fwnode);
447 struct fwnode_handle *fwnode_graph_get_remote_port(
448 const struct fwnode_handle *fwnode);
449 struct fwnode_handle *fwnode_graph_get_remote_endpoint(
450 const struct fwnode_handle *fwnode);
452 static inline bool fwnode_graph_is_endpoint(const struct fwnode_handle *fwnode)
454 return fwnode_property_present(fwnode, "remote-endpoint");
458 * Fwnode lookup flags
460 * @FWNODE_GRAPH_ENDPOINT_NEXT: In the case of no exact match, look for the
461 * closest endpoint ID greater than the specified
463 * @FWNODE_GRAPH_DEVICE_DISABLED: That the device to which the remote
464 * endpoint of the given endpoint belongs to,
465 * may be disabled, or that the endpoint is not
468 #define FWNODE_GRAPH_ENDPOINT_NEXT BIT(0)
469 #define FWNODE_GRAPH_DEVICE_DISABLED BIT(1)
471 struct fwnode_handle *
472 fwnode_graph_get_endpoint_by_id(const struct fwnode_handle *fwnode,
473 u32 port, u32 endpoint, unsigned long flags);
474 unsigned int fwnode_graph_get_endpoint_count(const struct fwnode_handle *fwnode,
475 unsigned long flags);
477 #define fwnode_graph_for_each_endpoint(fwnode, child) \
478 for (child = fwnode_graph_get_next_endpoint(fwnode, NULL); child; \
479 child = fwnode_graph_get_next_endpoint(fwnode, child))
481 int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
482 struct fwnode_endpoint *endpoint);
484 typedef void *(*devcon_match_fn_t)(const struct fwnode_handle *fwnode, const char *id,
487 void *fwnode_connection_find_match(const struct fwnode_handle *fwnode,
488 const char *con_id, void *data,
489 devcon_match_fn_t match);
491 static inline void *device_connection_find_match(const struct device *dev,
492 const char *con_id, void *data,
493 devcon_match_fn_t match)
495 return fwnode_connection_find_match(dev_fwnode(dev), con_id, data, match);
498 int fwnode_connection_find_matches(const struct fwnode_handle *fwnode,
499 const char *con_id, void *data,
500 devcon_match_fn_t match,
501 void **matches, unsigned int matches_len);
503 /* -------------------------------------------------------------------------- */
504 /* Software fwnode support - when HW description is incomplete or missing */
507 * struct software_node - Software node description
508 * @name: Name of the software node
509 * @parent: Parent of the software node
510 * @properties: Array of device properties
512 struct software_node {
514 const struct software_node *parent;
515 const struct property_entry *properties;
518 #define SOFTWARE_NODE(_name_, _properties_, _parent_) \
519 (struct software_node) { \
521 .properties = _properties_, \
522 .parent = _parent_, \
525 bool is_software_node(const struct fwnode_handle *fwnode);
526 const struct software_node *
527 to_software_node(const struct fwnode_handle *fwnode);
528 struct fwnode_handle *software_node_fwnode(const struct software_node *node);
530 const struct software_node *
531 software_node_find_by_name(const struct software_node *parent,
534 int software_node_register_node_group(const struct software_node **node_group);
535 void software_node_unregister_node_group(const struct software_node **node_group);
537 int software_node_register(const struct software_node *node);
538 void software_node_unregister(const struct software_node *node);
540 struct fwnode_handle *
541 fwnode_create_software_node(const struct property_entry *properties,
542 const struct fwnode_handle *parent);
543 void fwnode_remove_software_node(struct fwnode_handle *fwnode);
545 int device_add_software_node(struct device *dev, const struct software_node *node);
546 void device_remove_software_node(struct device *dev);
548 int device_create_managed_software_node(struct device *dev,
549 const struct property_entry *properties,
550 const struct software_node *parent);
552 #endif /* _LINUX_PROPERTY_H_ */