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;
253 * Note: the below initializers for the anonymous union are carefully
254 * crafted to avoid gcc-4.4.4's problems with initialization of anon unions
258 #define __PROPERTY_ENTRY_ELEMENT_SIZE(_elem_) \
259 sizeof(((struct property_entry *)NULL)->value._elem_)
261 #define __PROPERTY_ENTRY_ARRAY_LEN(_name_, _elem_, _Type_, _val_, _len_)\
262 (struct property_entry) { \
264 .length = (_len_) * __PROPERTY_ENTRY_ELEMENT_SIZE(_elem_), \
266 .type = DEV_PROP_##_Type_, \
267 { .pointer = _val_ }, \
270 #define PROPERTY_ENTRY_U8_ARRAY_LEN(_name_, _val_, _len_) \
271 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u8_data, U8, _val_, _len_)
272 #define PROPERTY_ENTRY_U16_ARRAY_LEN(_name_, _val_, _len_) \
273 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u16_data, U16, _val_, _len_)
274 #define PROPERTY_ENTRY_U32_ARRAY_LEN(_name_, _val_, _len_) \
275 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u32_data, U32, _val_, _len_)
276 #define PROPERTY_ENTRY_U64_ARRAY_LEN(_name_, _val_, _len_) \
277 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u64_data, U64, _val_, _len_)
278 #define PROPERTY_ENTRY_STRING_ARRAY_LEN(_name_, _val_, _len_) \
279 __PROPERTY_ENTRY_ARRAY_LEN(_name_, str, STRING, _val_, _len_)
281 #define PROPERTY_ENTRY_U8_ARRAY(_name_, _val_) \
282 PROPERTY_ENTRY_U8_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
283 #define PROPERTY_ENTRY_U16_ARRAY(_name_, _val_) \
284 PROPERTY_ENTRY_U16_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
285 #define PROPERTY_ENTRY_U32_ARRAY(_name_, _val_) \
286 PROPERTY_ENTRY_U32_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
287 #define PROPERTY_ENTRY_U64_ARRAY(_name_, _val_) \
288 PROPERTY_ENTRY_U64_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
289 #define PROPERTY_ENTRY_STRING_ARRAY(_name_, _val_) \
290 PROPERTY_ENTRY_STRING_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
292 #define __PROPERTY_ENTRY_ELEMENT(_name_, _elem_, _Type_, _val_) \
293 (struct property_entry) { \
295 .length = __PROPERTY_ENTRY_ELEMENT_SIZE(_elem_), \
296 .type = DEV_PROP_##_Type_, \
297 { .value = { ._elem_ = _val_ } }, \
300 #define PROPERTY_ENTRY_U8(_name_, _val_) \
301 __PROPERTY_ENTRY_ELEMENT(_name_, u8_data, U8, _val_)
302 #define PROPERTY_ENTRY_U16(_name_, _val_) \
303 __PROPERTY_ENTRY_ELEMENT(_name_, u16_data, U16, _val_)
304 #define PROPERTY_ENTRY_U32(_name_, _val_) \
305 __PROPERTY_ENTRY_ELEMENT(_name_, u32_data, U32, _val_)
306 #define PROPERTY_ENTRY_U64(_name_, _val_) \
307 __PROPERTY_ENTRY_ELEMENT(_name_, u64_data, U64, _val_)
308 #define PROPERTY_ENTRY_STRING(_name_, _val_) \
309 __PROPERTY_ENTRY_ELEMENT(_name_, str, STRING, _val_)
311 #define PROPERTY_ENTRY_BOOL(_name_) \
312 (struct property_entry) { \
316 struct property_entry *
317 property_entries_dup(const struct property_entry *properties);
319 void property_entries_free(const struct property_entry *properties);
321 int device_add_properties(struct device *dev,
322 const struct property_entry *properties);
323 void device_remove_properties(struct device *dev);
325 bool device_dma_supported(struct device *dev);
327 enum dev_dma_attr device_get_dma_attr(struct device *dev);
329 const void *device_get_match_data(struct device *dev);
331 int device_get_phy_mode(struct device *dev);
333 void *device_get_mac_address(struct device *dev, char *addr, int alen);
335 int fwnode_get_phy_mode(struct fwnode_handle *fwnode);
336 void *fwnode_get_mac_address(struct fwnode_handle *fwnode,
337 char *addr, int alen);
338 struct fwnode_handle *fwnode_graph_get_next_endpoint(
339 const struct fwnode_handle *fwnode, struct fwnode_handle *prev);
340 struct fwnode_handle *
341 fwnode_graph_get_port_parent(const struct fwnode_handle *fwnode);
342 struct fwnode_handle *fwnode_graph_get_remote_port_parent(
343 const struct fwnode_handle *fwnode);
344 struct fwnode_handle *fwnode_graph_get_remote_port(
345 const struct fwnode_handle *fwnode);
346 struct fwnode_handle *fwnode_graph_get_remote_endpoint(
347 const struct fwnode_handle *fwnode);
348 struct fwnode_handle *
349 fwnode_graph_get_remote_node(const struct fwnode_handle *fwnode, u32 port,
353 * Fwnode lookup flags
355 * @FWNODE_GRAPH_ENDPOINT_NEXT: In the case of no exact match, look for the
356 * closest endpoint ID greater than the specified
358 * @FWNODE_GRAPH_DEVICE_DISABLED: That the device to which the remote
359 * endpoint of the given endpoint belongs to,
362 #define FWNODE_GRAPH_ENDPOINT_NEXT BIT(0)
363 #define FWNODE_GRAPH_DEVICE_DISABLED BIT(1)
365 struct fwnode_handle *
366 fwnode_graph_get_endpoint_by_id(const struct fwnode_handle *fwnode,
367 u32 port, u32 endpoint, unsigned long flags);
369 #define fwnode_graph_for_each_endpoint(fwnode, child) \
371 (child = fwnode_graph_get_next_endpoint(fwnode, child)); )
373 int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
374 struct fwnode_endpoint *endpoint);
376 /* -------------------------------------------------------------------------- */
377 /* Software fwnode support - when HW description is incomplete or missing */
379 struct software_node;
382 * struct software_node_ref_args - Reference with additional arguments
383 * @node: Reference to a software node
384 * @nargs: Number of elements in @args array
385 * @args: Integer arguments
387 struct software_node_ref_args {
388 const struct software_node *node;
390 u64 args[NR_FWNODE_REFERENCE_ARGS];
394 * struct software_node_reference - Named software node reference property
395 * @name: Name of the property
396 * @nrefs: Number of elements in @refs array
397 * @refs: Array of references with optional arguments
399 struct software_node_reference {
402 const struct software_node_ref_args *refs;
406 * struct software_node - Software node description
407 * @name: Name of the software node
408 * @parent: Parent of the software node
409 * @properties: Array of device properties
410 * @references: Array of software node reference properties
412 struct software_node {
414 const struct software_node *parent;
415 const struct property_entry *properties;
416 const struct software_node_reference *references;
419 bool is_software_node(const struct fwnode_handle *fwnode);
420 const struct software_node *
421 to_software_node(const struct fwnode_handle *fwnode);
422 struct fwnode_handle *software_node_fwnode(const struct software_node *node);
424 const struct software_node *
425 software_node_find_by_name(const struct software_node *parent,
428 int software_node_register_nodes(const struct software_node *nodes);
429 void software_node_unregister_nodes(const struct software_node *nodes);
431 int software_node_register(const struct software_node *node);
433 int software_node_notify(struct device *dev, unsigned long action);
435 struct fwnode_handle *
436 fwnode_create_software_node(const struct property_entry *properties,
437 const struct fwnode_handle *parent);
438 void fwnode_remove_software_node(struct fwnode_handle *fwnode);
440 #endif /* _LINUX_PROPERTY_H_ */