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>
29 DEV_DMA_NOT_SUPPORTED,
34 struct fwnode_handle *dev_fwnode(struct device *dev);
36 bool device_property_present(struct device *dev, const char *propname);
37 int device_property_read_u8_array(struct device *dev, const char *propname,
38 u8 *val, size_t nval);
39 int device_property_read_u16_array(struct device *dev, const char *propname,
40 u16 *val, size_t nval);
41 int device_property_read_u32_array(struct device *dev, const char *propname,
42 u32 *val, size_t nval);
43 int device_property_read_u64_array(struct device *dev, const char *propname,
44 u64 *val, size_t nval);
45 int device_property_read_string_array(struct device *dev, const char *propname,
46 const char **val, size_t nval);
47 int device_property_read_string(struct device *dev, const char *propname,
49 int device_property_match_string(struct device *dev,
50 const char *propname, const char *string);
52 bool fwnode_device_is_available(const struct fwnode_handle *fwnode);
53 bool fwnode_property_present(const struct fwnode_handle *fwnode,
54 const char *propname);
55 int fwnode_property_read_u8_array(const struct fwnode_handle *fwnode,
56 const char *propname, u8 *val,
58 int fwnode_property_read_u16_array(const struct fwnode_handle *fwnode,
59 const char *propname, u16 *val,
61 int fwnode_property_read_u32_array(const struct fwnode_handle *fwnode,
62 const char *propname, u32 *val,
64 int fwnode_property_read_u64_array(const struct fwnode_handle *fwnode,
65 const char *propname, u64 *val,
67 int fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
68 const char *propname, const char **val,
70 int fwnode_property_read_string(const struct fwnode_handle *fwnode,
71 const char *propname, const char **val);
72 int fwnode_property_match_string(const struct fwnode_handle *fwnode,
73 const char *propname, const char *string);
74 int fwnode_property_get_reference_args(const struct fwnode_handle *fwnode,
75 const char *prop, const char *nargs_prop,
76 unsigned int nargs, unsigned int index,
77 struct fwnode_reference_args *args);
79 struct fwnode_handle *fwnode_find_reference(const struct fwnode_handle *fwnode,
83 struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode);
84 struct fwnode_handle *fwnode_get_next_parent(
85 struct fwnode_handle *fwnode);
86 struct fwnode_handle *fwnode_get_next_child_node(
87 const struct fwnode_handle *fwnode, struct fwnode_handle *child);
88 struct fwnode_handle *fwnode_get_next_available_child_node(
89 const struct fwnode_handle *fwnode, struct fwnode_handle *child);
91 #define fwnode_for_each_child_node(fwnode, child) \
92 for (child = fwnode_get_next_child_node(fwnode, NULL); child; \
93 child = fwnode_get_next_child_node(fwnode, child))
95 #define fwnode_for_each_available_child_node(fwnode, child) \
96 for (child = fwnode_get_next_available_child_node(fwnode, NULL); child;\
97 child = fwnode_get_next_available_child_node(fwnode, child))
99 struct fwnode_handle *device_get_next_child_node(
100 struct device *dev, struct fwnode_handle *child);
102 #define device_for_each_child_node(dev, child) \
103 for (child = device_get_next_child_node(dev, NULL); child; \
104 child = device_get_next_child_node(dev, child))
106 struct fwnode_handle *fwnode_get_named_child_node(
107 const struct fwnode_handle *fwnode, const char *childname);
108 struct fwnode_handle *device_get_named_child_node(struct device *dev,
109 const char *childname);
111 struct fwnode_handle *fwnode_handle_get(struct fwnode_handle *fwnode);
112 void fwnode_handle_put(struct fwnode_handle *fwnode);
114 int fwnode_irq_get(struct fwnode_handle *fwnode, unsigned int index);
116 unsigned int device_get_child_node_count(struct device *dev);
118 static inline bool device_property_read_bool(struct device *dev,
119 const char *propname)
121 return device_property_present(dev, propname);
124 static inline int device_property_read_u8(struct device *dev,
125 const char *propname, u8 *val)
127 return device_property_read_u8_array(dev, propname, val, 1);
130 static inline int device_property_read_u16(struct device *dev,
131 const char *propname, u16 *val)
133 return device_property_read_u16_array(dev, propname, val, 1);
136 static inline int device_property_read_u32(struct device *dev,
137 const char *propname, u32 *val)
139 return device_property_read_u32_array(dev, propname, val, 1);
142 static inline int device_property_read_u64(struct device *dev,
143 const char *propname, u64 *val)
145 return device_property_read_u64_array(dev, propname, val, 1);
148 static inline int device_property_count_u8(struct device *dev, const char *propname)
150 return device_property_read_u8_array(dev, propname, NULL, 0);
153 static inline int device_property_count_u16(struct device *dev, const char *propname)
155 return device_property_read_u16_array(dev, propname, NULL, 0);
158 static inline int device_property_count_u32(struct device *dev, const char *propname)
160 return device_property_read_u32_array(dev, propname, NULL, 0);
163 static inline int device_property_count_u64(struct device *dev, const char *propname)
165 return device_property_read_u64_array(dev, propname, NULL, 0);
168 static inline bool fwnode_property_read_bool(const struct fwnode_handle *fwnode,
169 const char *propname)
171 return fwnode_property_present(fwnode, propname);
174 static inline int fwnode_property_read_u8(const struct fwnode_handle *fwnode,
175 const char *propname, u8 *val)
177 return fwnode_property_read_u8_array(fwnode, propname, val, 1);
180 static inline int fwnode_property_read_u16(const struct fwnode_handle *fwnode,
181 const char *propname, u16 *val)
183 return fwnode_property_read_u16_array(fwnode, propname, val, 1);
186 static inline int fwnode_property_read_u32(const struct fwnode_handle *fwnode,
187 const char *propname, u32 *val)
189 return fwnode_property_read_u32_array(fwnode, propname, val, 1);
192 static inline int fwnode_property_read_u64(const struct fwnode_handle *fwnode,
193 const char *propname, u64 *val)
195 return fwnode_property_read_u64_array(fwnode, propname, val, 1);
198 static inline int fwnode_property_count_u8(const struct fwnode_handle *fwnode,
199 const char *propname)
201 return fwnode_property_read_u8_array(fwnode, propname, NULL, 0);
204 static inline int fwnode_property_count_u16(const struct fwnode_handle *fwnode,
205 const char *propname)
207 return fwnode_property_read_u16_array(fwnode, propname, NULL, 0);
210 static inline int fwnode_property_count_u32(const struct fwnode_handle *fwnode,
211 const char *propname)
213 return fwnode_property_read_u32_array(fwnode, propname, NULL, 0);
216 static inline int fwnode_property_count_u64(const struct fwnode_handle *fwnode,
217 const char *propname)
219 return fwnode_property_read_u64_array(fwnode, propname, NULL, 0);
223 * struct property_entry - "Built-in" device property representation.
224 * @name: Name of the property.
225 * @length: Length of data making up the value.
226 * @is_array: True when the property is an array.
227 * @type: Type of the data in unions.
228 * @pointer: Pointer to the property (an array of items of the given type).
229 * @value: Value of the property (when it is a single item of the given type).
231 struct property_entry {
235 enum dev_prop_type type;
242 const char * const *str;
255 * Note: the below four initializers for the anonymous union are carefully
256 * crafted to avoid gcc-4.4.4's problems with initialization of anon unions
260 #define PROPERTY_ENTRY_INTEGER_ARRAY(_name_, _type_, _Type_, _val_) \
261 (struct property_entry) { \
263 .length = ARRAY_SIZE(_val_) * sizeof(_type_), \
265 .type = DEV_PROP_##_Type_, \
266 { .pointer = { ._type_##_data = _val_ } }, \
269 #define PROPERTY_ENTRY_U8_ARRAY(_name_, _val_) \
270 PROPERTY_ENTRY_INTEGER_ARRAY(_name_, u8, U8, _val_)
271 #define PROPERTY_ENTRY_U16_ARRAY(_name_, _val_) \
272 PROPERTY_ENTRY_INTEGER_ARRAY(_name_, u16, U16, _val_)
273 #define PROPERTY_ENTRY_U32_ARRAY(_name_, _val_) \
274 PROPERTY_ENTRY_INTEGER_ARRAY(_name_, u32, U32, _val_)
275 #define PROPERTY_ENTRY_U64_ARRAY(_name_, _val_) \
276 PROPERTY_ENTRY_INTEGER_ARRAY(_name_, u64, U64, _val_)
278 #define PROPERTY_ENTRY_STRING_ARRAY(_name_, _val_) \
279 (struct property_entry) { \
281 .length = ARRAY_SIZE(_val_) * sizeof(const char *), \
283 .type = DEV_PROP_STRING, \
284 { .pointer = { .str = _val_ } }, \
287 #define PROPERTY_ENTRY_INTEGER(_name_, _type_, _Type_, _val_) \
288 (struct property_entry) { \
290 .length = sizeof(_type_), \
291 .type = DEV_PROP_##_Type_, \
292 { .value = { ._type_##_data = _val_ } }, \
295 #define PROPERTY_ENTRY_U8(_name_, _val_) \
296 PROPERTY_ENTRY_INTEGER(_name_, u8, U8, _val_)
297 #define PROPERTY_ENTRY_U16(_name_, _val_) \
298 PROPERTY_ENTRY_INTEGER(_name_, u16, U16, _val_)
299 #define PROPERTY_ENTRY_U32(_name_, _val_) \
300 PROPERTY_ENTRY_INTEGER(_name_, u32, U32, _val_)
301 #define PROPERTY_ENTRY_U64(_name_, _val_) \
302 PROPERTY_ENTRY_INTEGER(_name_, u64, U64, _val_)
304 #define PROPERTY_ENTRY_STRING(_name_, _val_) \
305 (struct property_entry) { \
307 .length = sizeof(const char *), \
308 .type = DEV_PROP_STRING, \
309 { .value = { .str = _val_ } }, \
312 #define PROPERTY_ENTRY_BOOL(_name_) \
313 (struct property_entry) { \
317 struct property_entry *
318 property_entries_dup(const struct property_entry *properties);
320 void property_entries_free(const struct property_entry *properties);
322 int device_add_properties(struct device *dev,
323 const struct property_entry *properties);
324 void device_remove_properties(struct device *dev);
326 bool device_dma_supported(struct device *dev);
328 enum dev_dma_attr device_get_dma_attr(struct device *dev);
330 const void *device_get_match_data(struct device *dev);
332 int device_get_phy_mode(struct device *dev);
334 void *device_get_mac_address(struct device *dev, char *addr, int alen);
336 int fwnode_get_phy_mode(struct fwnode_handle *fwnode);
337 void *fwnode_get_mac_address(struct fwnode_handle *fwnode,
338 char *addr, int alen);
339 struct fwnode_handle *fwnode_graph_get_next_endpoint(
340 const struct fwnode_handle *fwnode, struct fwnode_handle *prev);
341 struct fwnode_handle *
342 fwnode_graph_get_port_parent(const struct fwnode_handle *fwnode);
343 struct fwnode_handle *fwnode_graph_get_remote_port_parent(
344 const struct fwnode_handle *fwnode);
345 struct fwnode_handle *fwnode_graph_get_remote_port(
346 const struct fwnode_handle *fwnode);
347 struct fwnode_handle *fwnode_graph_get_remote_endpoint(
348 const struct fwnode_handle *fwnode);
349 struct fwnode_handle *
350 fwnode_graph_get_remote_node(const struct fwnode_handle *fwnode, u32 port,
354 * Fwnode lookup flags
356 * @FWNODE_GRAPH_ENDPOINT_NEXT: In the case of no exact match, look for the
357 * closest endpoint ID greater than the specified
359 * @FWNODE_GRAPH_DEVICE_DISABLED: That the device to which the remote
360 * endpoint of the given endpoint belongs to,
363 #define FWNODE_GRAPH_ENDPOINT_NEXT BIT(0)
364 #define FWNODE_GRAPH_DEVICE_DISABLED BIT(1)
366 struct fwnode_handle *
367 fwnode_graph_get_endpoint_by_id(const struct fwnode_handle *fwnode,
368 u32 port, u32 endpoint, unsigned long flags);
370 #define fwnode_graph_for_each_endpoint(fwnode, child) \
372 (child = fwnode_graph_get_next_endpoint(fwnode, child)); )
374 int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
375 struct fwnode_endpoint *endpoint);
377 /* -------------------------------------------------------------------------- */
378 /* Software fwnode support - when HW description is incomplete or missing */
380 struct software_node;
383 * struct software_node_ref_args - Reference with additional arguments
384 * @node: Reference to a software node
385 * @nargs: Number of elements in @args array
386 * @args: Integer arguments
388 struct software_node_ref_args {
389 const struct software_node *node;
391 u64 args[NR_FWNODE_REFERENCE_ARGS];
395 * struct software_node_reference - Named software node reference property
396 * @name: Name of the property
397 * @nrefs: Number of elements in @refs array
398 * @refs: Array of references with optional arguments
400 struct software_node_reference {
403 const struct software_node_ref_args *refs;
407 * struct software_node - Software node description
408 * @name: Name of the software node
409 * @parent: Parent of the software node
410 * @properties: Array of device properties
411 * @references: Array of software node reference properties
413 struct software_node {
415 const struct software_node *parent;
416 const struct property_entry *properties;
417 const struct software_node_reference *references;
420 bool is_software_node(const struct fwnode_handle *fwnode);
421 const struct software_node *to_software_node(struct fwnode_handle *fwnode);
422 struct fwnode_handle *software_node_fwnode(const struct software_node *node);
424 int software_node_register_nodes(const struct software_node *nodes);
425 void software_node_unregister_nodes(const struct software_node *nodes);
427 int software_node_register(const struct software_node *node);
429 int software_node_notify(struct device *dev, unsigned long action);
431 struct fwnode_handle *
432 fwnode_create_software_node(const struct property_entry *properties,
433 const struct fwnode_handle *parent);
434 void fwnode_remove_software_node(struct fwnode_handle *fwnode);
436 #endif /* _LINUX_PROPERTY_H_ */