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/array_size.h>
15 #include <linux/bits.h>
16 #include <linux/cleanup.h>
17 #include <linux/fwnode.h>
18 #include <linux/stddef.h>
19 #include <linux/types.h>
32 const struct fwnode_handle
*__dev_fwnode_const(const struct device
*dev
);
33 struct fwnode_handle
*__dev_fwnode(struct device
*dev
);
34 #define dev_fwnode(dev) \
36 const struct device *: __dev_fwnode_const, \
37 struct device *: __dev_fwnode)(dev)
39 bool device_property_present(const struct device
*dev
, const char *propname
);
40 int device_property_read_u8_array(const struct device
*dev
, const char *propname
,
41 u8
*val
, size_t nval
);
42 int device_property_read_u16_array(const struct device
*dev
, const char *propname
,
43 u16
*val
, size_t nval
);
44 int device_property_read_u32_array(const struct device
*dev
, const char *propname
,
45 u32
*val
, size_t nval
);
46 int device_property_read_u64_array(const struct device
*dev
, const char *propname
,
47 u64
*val
, size_t nval
);
48 int device_property_read_string_array(const struct device
*dev
, const char *propname
,
49 const char **val
, size_t nval
);
50 int device_property_read_string(const struct device
*dev
, const char *propname
,
52 int device_property_match_string(const struct device
*dev
,
53 const char *propname
, const char *string
);
55 bool fwnode_property_present(const struct fwnode_handle
*fwnode
,
56 const char *propname
);
57 int fwnode_property_read_u8_array(const struct fwnode_handle
*fwnode
,
58 const char *propname
, u8
*val
,
60 int fwnode_property_read_u16_array(const struct fwnode_handle
*fwnode
,
61 const char *propname
, u16
*val
,
63 int fwnode_property_read_u32_array(const struct fwnode_handle
*fwnode
,
64 const char *propname
, u32
*val
,
66 int fwnode_property_read_u64_array(const struct fwnode_handle
*fwnode
,
67 const char *propname
, u64
*val
,
69 int fwnode_property_read_string_array(const struct fwnode_handle
*fwnode
,
70 const char *propname
, const char **val
,
72 int fwnode_property_read_string(const struct fwnode_handle
*fwnode
,
73 const char *propname
, const char **val
);
74 int fwnode_property_match_string(const struct fwnode_handle
*fwnode
,
75 const char *propname
, const char *string
);
77 bool fwnode_device_is_available(const struct fwnode_handle
*fwnode
);
79 static inline bool fwnode_device_is_big_endian(const struct fwnode_handle
*fwnode
)
81 if (fwnode_property_present(fwnode
, "big-endian"))
83 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN
) &&
84 fwnode_property_present(fwnode
, "native-endian"))
90 bool fwnode_device_is_compatible(const struct fwnode_handle
*fwnode
, const char *compat
)
92 return fwnode_property_match_string(fwnode
, "compatible", compat
) >= 0;
96 * device_is_big_endian - check if a device has BE registers
97 * @dev: Pointer to the struct device
99 * Returns: true if the device has a "big-endian" property, or if the kernel
100 * was compiled for BE *and* the device has a "native-endian" property.
101 * Returns false otherwise.
103 * Callers would nominally use ioread32be/iowrite32be if
104 * device_is_big_endian() == true, or readl/writel otherwise.
106 static inline bool device_is_big_endian(const struct device
*dev
)
108 return fwnode_device_is_big_endian(dev_fwnode(dev
));
112 * device_is_compatible - match 'compatible' property of the device with a given string
113 * @dev: Pointer to the struct device
114 * @compat: The string to match 'compatible' property with
116 * Returns: true if matches, otherwise false.
118 static inline bool device_is_compatible(const struct device
*dev
, const char *compat
)
120 return fwnode_device_is_compatible(dev_fwnode(dev
), compat
);
123 int fwnode_property_match_property_string(const struct fwnode_handle
*fwnode
,
124 const char *propname
,
125 const char * const *array
, size_t n
);
128 int device_property_match_property_string(const struct device
*dev
,
129 const char *propname
,
130 const char * const *array
, size_t n
)
132 return fwnode_property_match_property_string(dev_fwnode(dev
), propname
, array
, n
);
135 int fwnode_property_get_reference_args(const struct fwnode_handle
*fwnode
,
136 const char *prop
, const char *nargs_prop
,
137 unsigned int nargs
, unsigned int index
,
138 struct fwnode_reference_args
*args
);
140 struct fwnode_handle
*fwnode_find_reference(const struct fwnode_handle
*fwnode
,
144 const char *fwnode_get_name(const struct fwnode_handle
*fwnode
);
145 const char *fwnode_get_name_prefix(const struct fwnode_handle
*fwnode
);
146 bool fwnode_name_eq(const struct fwnode_handle
*fwnode
, const char *name
);
148 struct fwnode_handle
*fwnode_get_parent(const struct fwnode_handle
*fwnode
);
149 struct fwnode_handle
*fwnode_get_next_parent(struct fwnode_handle
*fwnode
);
151 #define fwnode_for_each_parent_node(fwnode, parent) \
152 for (parent = fwnode_get_parent(fwnode); parent; \
153 parent = fwnode_get_next_parent(parent))
155 unsigned int fwnode_count_parents(const struct fwnode_handle
*fwn
);
156 struct fwnode_handle
*fwnode_get_nth_parent(struct fwnode_handle
*fwn
,
158 struct fwnode_handle
*fwnode_get_next_child_node(
159 const struct fwnode_handle
*fwnode
, struct fwnode_handle
*child
);
160 struct fwnode_handle
*fwnode_get_next_available_child_node(
161 const struct fwnode_handle
*fwnode
, struct fwnode_handle
*child
);
163 #define fwnode_for_each_child_node(fwnode, child) \
164 for (child = fwnode_get_next_child_node(fwnode, NULL); child; \
165 child = fwnode_get_next_child_node(fwnode, child))
167 #define fwnode_for_each_available_child_node(fwnode, child) \
168 for (child = fwnode_get_next_available_child_node(fwnode, NULL); child;\
169 child = fwnode_get_next_available_child_node(fwnode, child))
171 struct fwnode_handle
*device_get_next_child_node(const struct device
*dev
,
172 struct fwnode_handle
*child
);
174 #define device_for_each_child_node(dev, child) \
175 for (child = device_get_next_child_node(dev, NULL); child; \
176 child = device_get_next_child_node(dev, child))
178 #define device_for_each_child_node_scoped(dev, child) \
179 for (struct fwnode_handle *child __free(fwnode_handle) = \
180 device_get_next_child_node(dev, NULL); \
181 child; child = device_get_next_child_node(dev, child))
183 struct fwnode_handle
*fwnode_get_named_child_node(const struct fwnode_handle
*fwnode
,
184 const char *childname
);
185 struct fwnode_handle
*device_get_named_child_node(const struct device
*dev
,
186 const char *childname
);
188 struct fwnode_handle
*fwnode_handle_get(struct fwnode_handle
*fwnode
);
191 * fwnode_handle_put - Drop reference to a device node
192 * @fwnode: Pointer to the device node to drop the reference to.
194 * This has to be used when terminating device_for_each_child_node() iteration
195 * with break or return to prevent stale device node references from being left
198 static inline void fwnode_handle_put(struct fwnode_handle
*fwnode
)
200 fwnode_call_void_op(fwnode
, put
);
203 DEFINE_FREE(fwnode_handle
, struct fwnode_handle
*, fwnode_handle_put(_T
))
205 int fwnode_irq_get(const struct fwnode_handle
*fwnode
, unsigned int index
);
206 int fwnode_irq_get_byname(const struct fwnode_handle
*fwnode
, const char *name
);
208 unsigned int device_get_child_node_count(const struct device
*dev
);
210 static inline bool device_property_read_bool(const struct device
*dev
,
211 const char *propname
)
213 return device_property_present(dev
, propname
);
216 static inline int device_property_read_u8(const struct device
*dev
,
217 const char *propname
, u8
*val
)
219 return device_property_read_u8_array(dev
, propname
, val
, 1);
222 static inline int device_property_read_u16(const struct device
*dev
,
223 const char *propname
, u16
*val
)
225 return device_property_read_u16_array(dev
, propname
, val
, 1);
228 static inline int device_property_read_u32(const struct device
*dev
,
229 const char *propname
, u32
*val
)
231 return device_property_read_u32_array(dev
, propname
, val
, 1);
234 static inline int device_property_read_u64(const struct device
*dev
,
235 const char *propname
, u64
*val
)
237 return device_property_read_u64_array(dev
, propname
, val
, 1);
240 static inline int device_property_count_u8(const struct device
*dev
, const char *propname
)
242 return device_property_read_u8_array(dev
, propname
, NULL
, 0);
245 static inline int device_property_count_u16(const struct device
*dev
, const char *propname
)
247 return device_property_read_u16_array(dev
, propname
, NULL
, 0);
250 static inline int device_property_count_u32(const struct device
*dev
, const char *propname
)
252 return device_property_read_u32_array(dev
, propname
, NULL
, 0);
255 static inline int device_property_count_u64(const struct device
*dev
, const char *propname
)
257 return device_property_read_u64_array(dev
, propname
, NULL
, 0);
260 static inline int device_property_string_array_count(const struct device
*dev
,
261 const char *propname
)
263 return device_property_read_string_array(dev
, propname
, NULL
, 0);
266 static inline bool fwnode_property_read_bool(const struct fwnode_handle
*fwnode
,
267 const char *propname
)
269 return fwnode_property_present(fwnode
, propname
);
272 static inline int fwnode_property_read_u8(const struct fwnode_handle
*fwnode
,
273 const char *propname
, u8
*val
)
275 return fwnode_property_read_u8_array(fwnode
, propname
, val
, 1);
278 static inline int fwnode_property_read_u16(const struct fwnode_handle
*fwnode
,
279 const char *propname
, u16
*val
)
281 return fwnode_property_read_u16_array(fwnode
, propname
, val
, 1);
284 static inline int fwnode_property_read_u32(const struct fwnode_handle
*fwnode
,
285 const char *propname
, u32
*val
)
287 return fwnode_property_read_u32_array(fwnode
, propname
, val
, 1);
290 static inline int fwnode_property_read_u64(const struct fwnode_handle
*fwnode
,
291 const char *propname
, u64
*val
)
293 return fwnode_property_read_u64_array(fwnode
, propname
, val
, 1);
296 static inline int fwnode_property_count_u8(const struct fwnode_handle
*fwnode
,
297 const char *propname
)
299 return fwnode_property_read_u8_array(fwnode
, propname
, NULL
, 0);
302 static inline int fwnode_property_count_u16(const struct fwnode_handle
*fwnode
,
303 const char *propname
)
305 return fwnode_property_read_u16_array(fwnode
, propname
, NULL
, 0);
308 static inline int fwnode_property_count_u32(const struct fwnode_handle
*fwnode
,
309 const char *propname
)
311 return fwnode_property_read_u32_array(fwnode
, propname
, NULL
, 0);
314 static inline int fwnode_property_count_u64(const struct fwnode_handle
*fwnode
,
315 const char *propname
)
317 return fwnode_property_read_u64_array(fwnode
, propname
, NULL
, 0);
321 fwnode_property_string_array_count(const struct fwnode_handle
*fwnode
,
322 const char *propname
)
324 return fwnode_property_read_string_array(fwnode
, propname
, NULL
, 0);
327 struct software_node
;
330 * struct software_node_ref_args - Reference property with additional arguments
331 * @node: Reference to a software node
332 * @nargs: Number of elements in @args array
333 * @args: Integer arguments
335 struct software_node_ref_args
{
336 const struct software_node
*node
;
338 u64 args
[NR_FWNODE_REFERENCE_ARGS
];
341 #define SOFTWARE_NODE_REFERENCE(_ref_, ...) \
342 (const struct software_node_ref_args) { \
344 .nargs = COUNT_ARGS(__VA_ARGS__), \
345 .args = { __VA_ARGS__ }, \
349 * struct property_entry - "Built-in" device property representation.
350 * @name: Name of the property.
351 * @length: Length of data making up the value.
352 * @is_inline: True when the property value is stored inline.
353 * @type: Type of the data in unions.
354 * @pointer: Pointer to the property when it is not stored inline.
355 * @value: Value of the property when it is stored inline.
357 struct property_entry
{
361 enum dev_prop_type type
;
365 u8 u8_data
[sizeof(u64
) / sizeof(u8
)];
366 u16 u16_data
[sizeof(u64
) / sizeof(u16
)];
367 u32 u32_data
[sizeof(u64
) / sizeof(u32
)];
368 u64 u64_data
[sizeof(u64
) / sizeof(u64
)];
369 const char *str
[sizeof(u64
) / sizeof(char *)];
375 * Note: the below initializers for the anonymous union are carefully
376 * crafted to avoid gcc-4.4.4's problems with initialization of anon unions
379 #define __PROPERTY_ENTRY_ARRAY_LEN(_name_, _elem_, _Type_, _val_, _len_) \
380 (struct property_entry) { \
382 .length = (_len_) * sizeof_field(struct property_entry, value._elem_[0]), \
383 .type = DEV_PROP_##_Type_, \
384 { .pointer = _val_ }, \
387 #define PROPERTY_ENTRY_U8_ARRAY_LEN(_name_, _val_, _len_) \
388 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u8_data, U8, _val_, _len_)
389 #define PROPERTY_ENTRY_U16_ARRAY_LEN(_name_, _val_, _len_) \
390 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u16_data, U16, _val_, _len_)
391 #define PROPERTY_ENTRY_U32_ARRAY_LEN(_name_, _val_, _len_) \
392 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u32_data, U32, _val_, _len_)
393 #define PROPERTY_ENTRY_U64_ARRAY_LEN(_name_, _val_, _len_) \
394 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u64_data, U64, _val_, _len_)
395 #define PROPERTY_ENTRY_STRING_ARRAY_LEN(_name_, _val_, _len_) \
396 __PROPERTY_ENTRY_ARRAY_LEN(_name_, str, STRING, _val_, _len_)
398 #define PROPERTY_ENTRY_REF_ARRAY_LEN(_name_, _val_, _len_) \
399 (struct property_entry) { \
401 .length = (_len_) * sizeof(struct software_node_ref_args), \
402 .type = DEV_PROP_REF, \
403 { .pointer = _val_ }, \
406 #define PROPERTY_ENTRY_U8_ARRAY(_name_, _val_) \
407 PROPERTY_ENTRY_U8_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
408 #define PROPERTY_ENTRY_U16_ARRAY(_name_, _val_) \
409 PROPERTY_ENTRY_U16_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
410 #define PROPERTY_ENTRY_U32_ARRAY(_name_, _val_) \
411 PROPERTY_ENTRY_U32_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
412 #define PROPERTY_ENTRY_U64_ARRAY(_name_, _val_) \
413 PROPERTY_ENTRY_U64_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
414 #define PROPERTY_ENTRY_STRING_ARRAY(_name_, _val_) \
415 PROPERTY_ENTRY_STRING_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
416 #define PROPERTY_ENTRY_REF_ARRAY(_name_, _val_) \
417 PROPERTY_ENTRY_REF_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
419 #define __PROPERTY_ENTRY_ELEMENT(_name_, _elem_, _Type_, _val_) \
420 (struct property_entry) { \
422 .length = sizeof_field(struct property_entry, value._elem_[0]), \
424 .type = DEV_PROP_##_Type_, \
425 { .value = { ._elem_[0] = _val_ } }, \
428 #define PROPERTY_ENTRY_U8(_name_, _val_) \
429 __PROPERTY_ENTRY_ELEMENT(_name_, u8_data, U8, _val_)
430 #define PROPERTY_ENTRY_U16(_name_, _val_) \
431 __PROPERTY_ENTRY_ELEMENT(_name_, u16_data, U16, _val_)
432 #define PROPERTY_ENTRY_U32(_name_, _val_) \
433 __PROPERTY_ENTRY_ELEMENT(_name_, u32_data, U32, _val_)
434 #define PROPERTY_ENTRY_U64(_name_, _val_) \
435 __PROPERTY_ENTRY_ELEMENT(_name_, u64_data, U64, _val_)
436 #define PROPERTY_ENTRY_STRING(_name_, _val_) \
437 __PROPERTY_ENTRY_ELEMENT(_name_, str, STRING, _val_)
439 #define PROPERTY_ENTRY_REF(_name_, _ref_, ...) \
440 (struct property_entry) { \
442 .length = sizeof(struct software_node_ref_args), \
443 .type = DEV_PROP_REF, \
444 { .pointer = &SOFTWARE_NODE_REFERENCE(_ref_, ##__VA_ARGS__), }, \
447 #define PROPERTY_ENTRY_BOOL(_name_) \
448 (struct property_entry) { \
453 struct property_entry
*
454 property_entries_dup(const struct property_entry
*properties
);
455 void property_entries_free(const struct property_entry
*properties
);
457 bool device_dma_supported(const struct device
*dev
);
458 enum dev_dma_attr
device_get_dma_attr(const struct device
*dev
);
460 const void *device_get_match_data(const struct device
*dev
);
462 int device_get_phy_mode(struct device
*dev
);
463 int fwnode_get_phy_mode(const struct fwnode_handle
*fwnode
);
465 void __iomem
*fwnode_iomap(struct fwnode_handle
*fwnode
, int index
);
467 struct fwnode_handle
*fwnode_graph_get_next_endpoint(
468 const struct fwnode_handle
*fwnode
, struct fwnode_handle
*prev
);
469 struct fwnode_handle
*
470 fwnode_graph_get_port_parent(const struct fwnode_handle
*fwnode
);
471 struct fwnode_handle
*fwnode_graph_get_remote_port_parent(
472 const struct fwnode_handle
*fwnode
);
473 struct fwnode_handle
*fwnode_graph_get_remote_port(
474 const struct fwnode_handle
*fwnode
);
475 struct fwnode_handle
*fwnode_graph_get_remote_endpoint(
476 const struct fwnode_handle
*fwnode
);
478 static inline bool fwnode_graph_is_endpoint(const struct fwnode_handle
*fwnode
)
480 return fwnode_property_present(fwnode
, "remote-endpoint");
484 * Fwnode lookup flags
486 * @FWNODE_GRAPH_ENDPOINT_NEXT: In the case of no exact match, look for the
487 * closest endpoint ID greater than the specified
489 * @FWNODE_GRAPH_DEVICE_DISABLED: That the device to which the remote
490 * endpoint of the given endpoint belongs to,
491 * may be disabled, or that the endpoint is not
494 #define FWNODE_GRAPH_ENDPOINT_NEXT BIT(0)
495 #define FWNODE_GRAPH_DEVICE_DISABLED BIT(1)
497 struct fwnode_handle
*
498 fwnode_graph_get_endpoint_by_id(const struct fwnode_handle
*fwnode
,
499 u32 port
, u32 endpoint
, unsigned long flags
);
500 unsigned int fwnode_graph_get_endpoint_count(const struct fwnode_handle
*fwnode
,
501 unsigned long flags
);
503 #define fwnode_graph_for_each_endpoint(fwnode, child) \
504 for (child = fwnode_graph_get_next_endpoint(fwnode, NULL); child; \
505 child = fwnode_graph_get_next_endpoint(fwnode, child))
507 int fwnode_graph_parse_endpoint(const struct fwnode_handle
*fwnode
,
508 struct fwnode_endpoint
*endpoint
);
510 typedef void *(*devcon_match_fn_t
)(const struct fwnode_handle
*fwnode
, const char *id
,
513 void *fwnode_connection_find_match(const struct fwnode_handle
*fwnode
,
514 const char *con_id
, void *data
,
515 devcon_match_fn_t match
);
517 static inline void *device_connection_find_match(const struct device
*dev
,
518 const char *con_id
, void *data
,
519 devcon_match_fn_t match
)
521 return fwnode_connection_find_match(dev_fwnode(dev
), con_id
, data
, match
);
524 int fwnode_connection_find_matches(const struct fwnode_handle
*fwnode
,
525 const char *con_id
, void *data
,
526 devcon_match_fn_t match
,
527 void **matches
, unsigned int matches_len
);
529 /* -------------------------------------------------------------------------- */
530 /* Software fwnode support - when HW description is incomplete or missing */
533 * struct software_node - Software node description
534 * @name: Name of the software node
535 * @parent: Parent of the software node
536 * @properties: Array of device properties
538 struct software_node
{
540 const struct software_node
*parent
;
541 const struct property_entry
*properties
;
544 #define SOFTWARE_NODE(_name_, _properties_, _parent_) \
545 (struct software_node) { \
547 .properties = _properties_, \
548 .parent = _parent_, \
551 bool is_software_node(const struct fwnode_handle
*fwnode
);
552 const struct software_node
*
553 to_software_node(const struct fwnode_handle
*fwnode
);
554 struct fwnode_handle
*software_node_fwnode(const struct software_node
*node
);
556 const struct software_node
*
557 software_node_find_by_name(const struct software_node
*parent
,
560 int software_node_register_node_group(const struct software_node
**node_group
);
561 void software_node_unregister_node_group(const struct software_node
**node_group
);
563 int software_node_register(const struct software_node
*node
);
564 void software_node_unregister(const struct software_node
*node
);
566 struct fwnode_handle
*
567 fwnode_create_software_node(const struct property_entry
*properties
,
568 const struct fwnode_handle
*parent
);
569 void fwnode_remove_software_node(struct fwnode_handle
*fwnode
);
571 int device_add_software_node(struct device
*dev
, const struct software_node
*node
);
572 void device_remove_software_node(struct device
*dev
);
574 int device_create_managed_software_node(struct device
*dev
,
575 const struct property_entry
*properties
,
576 const struct software_node
*parent
);
578 #endif /* _LINUX_PROPERTY_H_ */