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/bits.h>
14 #include <linux/fwnode.h>
15 #include <linux/types.h>
28 DEV_DMA_NOT_SUPPORTED,
33 struct fwnode_handle *dev_fwnode(struct device *dev);
35 bool device_property_present(struct device *dev, const char *propname);
36 int device_property_read_u8_array(struct device *dev, const char *propname,
37 u8 *val, size_t nval);
38 int device_property_read_u16_array(struct device *dev, const char *propname,
39 u16 *val, size_t nval);
40 int device_property_read_u32_array(struct device *dev, const char *propname,
41 u32 *val, size_t nval);
42 int device_property_read_u64_array(struct device *dev, const char *propname,
43 u64 *val, size_t nval);
44 int device_property_read_string_array(struct device *dev, const char *propname,
45 const char **val, size_t nval);
46 int device_property_read_string(struct device *dev, const char *propname,
48 int device_property_match_string(struct device *dev,
49 const char *propname, const char *string);
51 bool fwnode_device_is_available(const struct fwnode_handle *fwnode);
52 bool fwnode_property_present(const struct fwnode_handle *fwnode,
53 const char *propname);
54 int fwnode_property_read_u8_array(const struct fwnode_handle *fwnode,
55 const char *propname, u8 *val,
57 int fwnode_property_read_u16_array(const struct fwnode_handle *fwnode,
58 const char *propname, u16 *val,
60 int fwnode_property_read_u32_array(const struct fwnode_handle *fwnode,
61 const char *propname, u32 *val,
63 int fwnode_property_read_u64_array(const struct fwnode_handle *fwnode,
64 const char *propname, u64 *val,
66 int fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
67 const char *propname, const char **val,
69 int fwnode_property_read_string(const struct fwnode_handle *fwnode,
70 const char *propname, const char **val);
71 int fwnode_property_match_string(const struct fwnode_handle *fwnode,
72 const char *propname, const char *string);
73 int fwnode_property_get_reference_args(const struct fwnode_handle *fwnode,
74 const char *prop, const char *nargs_prop,
75 unsigned int nargs, unsigned int index,
76 struct fwnode_reference_args *args);
78 struct fwnode_handle *fwnode_find_reference(const struct fwnode_handle *fwnode,
82 const char *fwnode_get_name(const struct fwnode_handle *fwnode);
83 const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode);
84 struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode);
85 struct fwnode_handle *fwnode_get_next_parent(
86 struct fwnode_handle *fwnode);
87 unsigned int fwnode_count_parents(const struct fwnode_handle *fwn);
88 struct fwnode_handle *fwnode_get_nth_parent(struct fwnode_handle *fwn,
90 struct fwnode_handle *fwnode_get_next_child_node(
91 const struct fwnode_handle *fwnode, struct fwnode_handle *child);
92 struct fwnode_handle *fwnode_get_next_available_child_node(
93 const struct fwnode_handle *fwnode, struct fwnode_handle *child);
95 #define fwnode_for_each_child_node(fwnode, child) \
96 for (child = fwnode_get_next_child_node(fwnode, NULL); child; \
97 child = fwnode_get_next_child_node(fwnode, child))
99 #define fwnode_for_each_available_child_node(fwnode, child) \
100 for (child = fwnode_get_next_available_child_node(fwnode, NULL); child;\
101 child = fwnode_get_next_available_child_node(fwnode, child))
103 struct fwnode_handle *device_get_next_child_node(
104 struct device *dev, struct fwnode_handle *child);
106 #define device_for_each_child_node(dev, child) \
107 for (child = device_get_next_child_node(dev, NULL); child; \
108 child = device_get_next_child_node(dev, child))
110 struct fwnode_handle *fwnode_get_named_child_node(
111 const struct fwnode_handle *fwnode, const char *childname);
112 struct fwnode_handle *device_get_named_child_node(struct device *dev,
113 const char *childname);
115 struct fwnode_handle *fwnode_handle_get(struct fwnode_handle *fwnode);
116 void fwnode_handle_put(struct fwnode_handle *fwnode);
118 int fwnode_irq_get(struct fwnode_handle *fwnode, unsigned int index);
120 unsigned int device_get_child_node_count(struct device *dev);
122 static inline bool device_property_read_bool(struct device *dev,
123 const char *propname)
125 return device_property_present(dev, propname);
128 static inline int device_property_read_u8(struct device *dev,
129 const char *propname, u8 *val)
131 return device_property_read_u8_array(dev, propname, val, 1);
134 static inline int device_property_read_u16(struct device *dev,
135 const char *propname, u16 *val)
137 return device_property_read_u16_array(dev, propname, val, 1);
140 static inline int device_property_read_u32(struct device *dev,
141 const char *propname, u32 *val)
143 return device_property_read_u32_array(dev, propname, val, 1);
146 static inline int device_property_read_u64(struct device *dev,
147 const char *propname, u64 *val)
149 return device_property_read_u64_array(dev, propname, val, 1);
152 static inline int device_property_count_u8(struct device *dev, const char *propname)
154 return device_property_read_u8_array(dev, propname, NULL, 0);
157 static inline int device_property_count_u16(struct device *dev, const char *propname)
159 return device_property_read_u16_array(dev, propname, NULL, 0);
162 static inline int device_property_count_u32(struct device *dev, const char *propname)
164 return device_property_read_u32_array(dev, propname, NULL, 0);
167 static inline int device_property_count_u64(struct device *dev, const char *propname)
169 return device_property_read_u64_array(dev, propname, NULL, 0);
172 static inline bool fwnode_property_read_bool(const struct fwnode_handle *fwnode,
173 const char *propname)
175 return fwnode_property_present(fwnode, propname);
178 static inline int fwnode_property_read_u8(const struct fwnode_handle *fwnode,
179 const char *propname, u8 *val)
181 return fwnode_property_read_u8_array(fwnode, propname, val, 1);
184 static inline int fwnode_property_read_u16(const struct fwnode_handle *fwnode,
185 const char *propname, u16 *val)
187 return fwnode_property_read_u16_array(fwnode, propname, val, 1);
190 static inline int fwnode_property_read_u32(const struct fwnode_handle *fwnode,
191 const char *propname, u32 *val)
193 return fwnode_property_read_u32_array(fwnode, propname, val, 1);
196 static inline int fwnode_property_read_u64(const struct fwnode_handle *fwnode,
197 const char *propname, u64 *val)
199 return fwnode_property_read_u64_array(fwnode, propname, val, 1);
202 static inline int fwnode_property_count_u8(const struct fwnode_handle *fwnode,
203 const char *propname)
205 return fwnode_property_read_u8_array(fwnode, propname, NULL, 0);
208 static inline int fwnode_property_count_u16(const struct fwnode_handle *fwnode,
209 const char *propname)
211 return fwnode_property_read_u16_array(fwnode, propname, NULL, 0);
214 static inline int fwnode_property_count_u32(const struct fwnode_handle *fwnode,
215 const char *propname)
217 return fwnode_property_read_u32_array(fwnode, propname, NULL, 0);
220 static inline int fwnode_property_count_u64(const struct fwnode_handle *fwnode,
221 const char *propname)
223 return fwnode_property_read_u64_array(fwnode, propname, NULL, 0);
227 * struct property_entry - "Built-in" device property representation.
228 * @name: Name of the property.
229 * @length: Length of data making up the value.
230 * @is_array: True when the property is an array.
231 * @type: Type of the data in unions.
232 * @pointer: Pointer to the property (an array of items of the given type).
233 * @value: Value of the property (when it is a single item of the given type).
235 struct property_entry {
239 enum dev_prop_type type;
246 const char * const *str;
259 * Note: the below four initializers for the anonymous union are carefully
260 * crafted to avoid gcc-4.4.4's problems with initialization of anon unions
264 #define __PROPERTY_ENTRY_ARRAY_LEN(_name_, _type_, _Type_, _val_, _len_)\
265 (struct property_entry) { \
267 .length = (_len_) * sizeof(_type_), \
269 .type = DEV_PROP_##_Type_, \
270 { .pointer = { ._type_##_data = _val_ } }, \
273 #define PROPERTY_ENTRY_U8_ARRAY_LEN(_name_, _val_, _len_) \
274 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u8, U8, _val_, _len_)
275 #define PROPERTY_ENTRY_U16_ARRAY_LEN(_name_, _val_, _len_) \
276 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u16, U16, _val_, _len_)
277 #define PROPERTY_ENTRY_U32_ARRAY_LEN(_name_, _val_, _len_) \
278 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u32, U32, _val_, _len_)
279 #define PROPERTY_ENTRY_U64_ARRAY_LEN(_name_, _val_, _len_) \
280 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u64, U64, _val_, _len_)
282 #define PROPERTY_ENTRY_STRING_ARRAY_LEN(_name_, _val_, _len_) \
283 (struct property_entry) { \
285 .length = (_len_) * sizeof(const char *), \
287 .type = DEV_PROP_STRING, \
288 { .pointer = { .str = _val_ } }, \
291 #define PROPERTY_ENTRY_U8_ARRAY(_name_, _val_) \
292 PROPERTY_ENTRY_U8_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
293 #define PROPERTY_ENTRY_U16_ARRAY(_name_, _val_) \
294 PROPERTY_ENTRY_U16_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
295 #define PROPERTY_ENTRY_U32_ARRAY(_name_, _val_) \
296 PROPERTY_ENTRY_U32_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
297 #define PROPERTY_ENTRY_U64_ARRAY(_name_, _val_) \
298 PROPERTY_ENTRY_U64_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
299 #define PROPERTY_ENTRY_STRING_ARRAY(_name_, _val_) \
300 PROPERTY_ENTRY_STRING_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
302 #define __PROPERTY_ENTRY_INTEGER(_name_, _type_, _Type_, _val_) \
303 (struct property_entry) { \
305 .length = sizeof(_type_), \
306 .type = DEV_PROP_##_Type_, \
307 { .value = { ._type_##_data = _val_ } }, \
310 #define PROPERTY_ENTRY_U8(_name_, _val_) \
311 __PROPERTY_ENTRY_INTEGER(_name_, u8, U8, _val_)
312 #define PROPERTY_ENTRY_U16(_name_, _val_) \
313 __PROPERTY_ENTRY_INTEGER(_name_, u16, U16, _val_)
314 #define PROPERTY_ENTRY_U32(_name_, _val_) \
315 __PROPERTY_ENTRY_INTEGER(_name_, u32, U32, _val_)
316 #define PROPERTY_ENTRY_U64(_name_, _val_) \
317 __PROPERTY_ENTRY_INTEGER(_name_, u64, U64, _val_)
319 #define PROPERTY_ENTRY_STRING(_name_, _val_) \
320 (struct property_entry) { \
322 .length = sizeof(const char *), \
323 .type = DEV_PROP_STRING, \
324 { .value = { .str = _val_ } }, \
327 #define PROPERTY_ENTRY_BOOL(_name_) \
328 (struct property_entry) { \
332 struct property_entry *
333 property_entries_dup(const struct property_entry *properties);
335 void property_entries_free(const struct property_entry *properties);
337 int device_add_properties(struct device *dev,
338 const struct property_entry *properties);
339 void device_remove_properties(struct device *dev);
341 bool device_dma_supported(struct device *dev);
343 enum dev_dma_attr device_get_dma_attr(struct device *dev);
345 const void *device_get_match_data(struct device *dev);
347 int device_get_phy_mode(struct device *dev);
349 void *device_get_mac_address(struct device *dev, char *addr, int alen);
351 int fwnode_get_phy_mode(struct fwnode_handle *fwnode);
352 void *fwnode_get_mac_address(struct fwnode_handle *fwnode,
353 char *addr, int alen);
354 struct fwnode_handle *fwnode_graph_get_next_endpoint(
355 const struct fwnode_handle *fwnode, struct fwnode_handle *prev);
356 struct fwnode_handle *
357 fwnode_graph_get_port_parent(const struct fwnode_handle *fwnode);
358 struct fwnode_handle *fwnode_graph_get_remote_port_parent(
359 const struct fwnode_handle *fwnode);
360 struct fwnode_handle *fwnode_graph_get_remote_port(
361 const struct fwnode_handle *fwnode);
362 struct fwnode_handle *fwnode_graph_get_remote_endpoint(
363 const struct fwnode_handle *fwnode);
364 struct fwnode_handle *
365 fwnode_graph_get_remote_node(const struct fwnode_handle *fwnode, u32 port,
369 * Fwnode lookup flags
371 * @FWNODE_GRAPH_ENDPOINT_NEXT: In the case of no exact match, look for the
372 * closest endpoint ID greater than the specified
374 * @FWNODE_GRAPH_DEVICE_DISABLED: That the device to which the remote
375 * endpoint of the given endpoint belongs to,
378 #define FWNODE_GRAPH_ENDPOINT_NEXT BIT(0)
379 #define FWNODE_GRAPH_DEVICE_DISABLED BIT(1)
381 struct fwnode_handle *
382 fwnode_graph_get_endpoint_by_id(const struct fwnode_handle *fwnode,
383 u32 port, u32 endpoint, unsigned long flags);
385 #define fwnode_graph_for_each_endpoint(fwnode, child) \
387 (child = fwnode_graph_get_next_endpoint(fwnode, child)); )
389 int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
390 struct fwnode_endpoint *endpoint);
392 /* -------------------------------------------------------------------------- */
393 /* Software fwnode support - when HW description is incomplete or missing */
395 struct software_node;
398 * struct software_node_ref_args - Reference with additional arguments
399 * @node: Reference to a software node
400 * @nargs: Number of elements in @args array
401 * @args: Integer arguments
403 struct software_node_ref_args {
404 const struct software_node *node;
406 u64 args[NR_FWNODE_REFERENCE_ARGS];
410 * struct software_node_reference - Named software node reference property
411 * @name: Name of the property
412 * @nrefs: Number of elements in @refs array
413 * @refs: Array of references with optional arguments
415 struct software_node_reference {
418 const struct software_node_ref_args *refs;
422 * struct software_node - Software node description
423 * @name: Name of the software node
424 * @parent: Parent of the software node
425 * @properties: Array of device properties
426 * @references: Array of software node reference properties
428 struct software_node {
430 const struct software_node *parent;
431 const struct property_entry *properties;
432 const struct software_node_reference *references;
435 bool is_software_node(const struct fwnode_handle *fwnode);
436 const struct software_node *
437 to_software_node(const struct fwnode_handle *fwnode);
438 struct fwnode_handle *software_node_fwnode(const struct software_node *node);
440 const struct software_node *
441 software_node_find_by_name(const struct software_node *parent,
444 int software_node_register_nodes(const struct software_node *nodes);
445 void software_node_unregister_nodes(const struct software_node *nodes);
447 int software_node_register(const struct software_node *node);
449 int software_node_notify(struct device *dev, unsigned long action);
451 struct fwnode_handle *
452 fwnode_create_software_node(const struct property_entry *properties,
453 const struct fwnode_handle *parent);
454 void fwnode_remove_software_node(struct fwnode_handle *fwnode);
456 #endif /* _LINUX_PROPERTY_H_ */