1
2
3
4
5
6
7
8
9
10#ifndef _LINUX_PROPERTY_H_
11#define _LINUX_PROPERTY_H_
12
13#include <linux/bits.h>
14#include <linux/fwnode.h>
15#include <linux/types.h>
16
17struct device;
18
19enum dev_prop_type {
20 DEV_PROP_U8,
21 DEV_PROP_U16,
22 DEV_PROP_U32,
23 DEV_PROP_U64,
24 DEV_PROP_STRING,
25 DEV_PROP_REF,
26};
27
28enum dev_dma_attr {
29 DEV_DMA_NOT_SUPPORTED,
30 DEV_DMA_NON_COHERENT,
31 DEV_DMA_COHERENT,
32};
33
34struct fwnode_handle *dev_fwnode(struct device *dev);
35
36bool device_property_present(struct device *dev, const char *propname);
37int device_property_read_u8_array(struct device *dev, const char *propname,
38 u8 *val, size_t nval);
39int device_property_read_u16_array(struct device *dev, const char *propname,
40 u16 *val, size_t nval);
41int device_property_read_u32_array(struct device *dev, const char *propname,
42 u32 *val, size_t nval);
43int device_property_read_u64_array(struct device *dev, const char *propname,
44 u64 *val, size_t nval);
45int device_property_read_string_array(struct device *dev, const char *propname,
46 const char **val, size_t nval);
47int device_property_read_string(struct device *dev, const char *propname,
48 const char **val);
49int device_property_match_string(struct device *dev,
50 const char *propname, const char *string);
51
52bool fwnode_device_is_available(const struct fwnode_handle *fwnode);
53bool fwnode_property_present(const struct fwnode_handle *fwnode,
54 const char *propname);
55int fwnode_property_read_u8_array(const struct fwnode_handle *fwnode,
56 const char *propname, u8 *val,
57 size_t nval);
58int fwnode_property_read_u16_array(const struct fwnode_handle *fwnode,
59 const char *propname, u16 *val,
60 size_t nval);
61int fwnode_property_read_u32_array(const struct fwnode_handle *fwnode,
62 const char *propname, u32 *val,
63 size_t nval);
64int fwnode_property_read_u64_array(const struct fwnode_handle *fwnode,
65 const char *propname, u64 *val,
66 size_t nval);
67int fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
68 const char *propname, const char **val,
69 size_t nval);
70int fwnode_property_read_string(const struct fwnode_handle *fwnode,
71 const char *propname, const char **val);
72int fwnode_property_match_string(const struct fwnode_handle *fwnode,
73 const char *propname, const char *string);
74int 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);
78
79struct fwnode_handle *fwnode_find_reference(const struct fwnode_handle *fwnode,
80 const char *name,
81 unsigned int index);
82
83const char *fwnode_get_name(const struct fwnode_handle *fwnode);
84const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode);
85struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode);
86struct fwnode_handle *fwnode_get_next_parent(
87 struct fwnode_handle *fwnode);
88unsigned int fwnode_count_parents(const struct fwnode_handle *fwn);
89struct fwnode_handle *fwnode_get_nth_parent(struct fwnode_handle *fwn,
90 unsigned int depth);
91struct fwnode_handle *fwnode_get_next_child_node(
92 const struct fwnode_handle *fwnode, struct fwnode_handle *child);
93struct fwnode_handle *fwnode_get_next_available_child_node(
94 const struct fwnode_handle *fwnode, struct fwnode_handle *child);
95
96#define fwnode_for_each_child_node(fwnode, child) \
97 for (child = fwnode_get_next_child_node(fwnode, NULL); child; \
98 child = fwnode_get_next_child_node(fwnode, child))
99
100#define fwnode_for_each_available_child_node(fwnode, child) \
101 for (child = fwnode_get_next_available_child_node(fwnode, NULL); child;\
102 child = fwnode_get_next_available_child_node(fwnode, child))
103
104struct fwnode_handle *device_get_next_child_node(
105 struct device *dev, struct fwnode_handle *child);
106
107#define device_for_each_child_node(dev, child) \
108 for (child = device_get_next_child_node(dev, NULL); child; \
109 child = device_get_next_child_node(dev, child))
110
111struct fwnode_handle *fwnode_get_named_child_node(
112 const struct fwnode_handle *fwnode, const char *childname);
113struct fwnode_handle *device_get_named_child_node(struct device *dev,
114 const char *childname);
115
116struct fwnode_handle *fwnode_handle_get(struct fwnode_handle *fwnode);
117void fwnode_handle_put(struct fwnode_handle *fwnode);
118
119int fwnode_irq_get(struct fwnode_handle *fwnode, unsigned int index);
120
121unsigned int device_get_child_node_count(struct device *dev);
122
123static inline bool device_property_read_bool(struct device *dev,
124 const char *propname)
125{
126 return device_property_present(dev, propname);
127}
128
129static inline int device_property_read_u8(struct device *dev,
130 const char *propname, u8 *val)
131{
132 return device_property_read_u8_array(dev, propname, val, 1);
133}
134
135static inline int device_property_read_u16(struct device *dev,
136 const char *propname, u16 *val)
137{
138 return device_property_read_u16_array(dev, propname, val, 1);
139}
140
141static inline int device_property_read_u32(struct device *dev,
142 const char *propname, u32 *val)
143{
144 return device_property_read_u32_array(dev, propname, val, 1);
145}
146
147static inline int device_property_read_u64(struct device *dev,
148 const char *propname, u64 *val)
149{
150 return device_property_read_u64_array(dev, propname, val, 1);
151}
152
153static inline int device_property_count_u8(struct device *dev, const char *propname)
154{
155 return device_property_read_u8_array(dev, propname, NULL, 0);
156}
157
158static inline int device_property_count_u16(struct device *dev, const char *propname)
159{
160 return device_property_read_u16_array(dev, propname, NULL, 0);
161}
162
163static inline int device_property_count_u32(struct device *dev, const char *propname)
164{
165 return device_property_read_u32_array(dev, propname, NULL, 0);
166}
167
168static inline int device_property_count_u64(struct device *dev, const char *propname)
169{
170 return device_property_read_u64_array(dev, propname, NULL, 0);
171}
172
173static inline bool fwnode_property_read_bool(const struct fwnode_handle *fwnode,
174 const char *propname)
175{
176 return fwnode_property_present(fwnode, propname);
177}
178
179static inline int fwnode_property_read_u8(const struct fwnode_handle *fwnode,
180 const char *propname, u8 *val)
181{
182 return fwnode_property_read_u8_array(fwnode, propname, val, 1);
183}
184
185static inline int fwnode_property_read_u16(const struct fwnode_handle *fwnode,
186 const char *propname, u16 *val)
187{
188 return fwnode_property_read_u16_array(fwnode, propname, val, 1);
189}
190
191static inline int fwnode_property_read_u32(const struct fwnode_handle *fwnode,
192 const char *propname, u32 *val)
193{
194 return fwnode_property_read_u32_array(fwnode, propname, val, 1);
195}
196
197static inline int fwnode_property_read_u64(const struct fwnode_handle *fwnode,
198 const char *propname, u64 *val)
199{
200 return fwnode_property_read_u64_array(fwnode, propname, val, 1);
201}
202
203static inline int fwnode_property_count_u8(const struct fwnode_handle *fwnode,
204 const char *propname)
205{
206 return fwnode_property_read_u8_array(fwnode, propname, NULL, 0);
207}
208
209static inline int fwnode_property_count_u16(const struct fwnode_handle *fwnode,
210 const char *propname)
211{
212 return fwnode_property_read_u16_array(fwnode, propname, NULL, 0);
213}
214
215static inline int fwnode_property_count_u32(const struct fwnode_handle *fwnode,
216 const char *propname)
217{
218 return fwnode_property_read_u32_array(fwnode, propname, NULL, 0);
219}
220
221static inline int fwnode_property_count_u64(const struct fwnode_handle *fwnode,
222 const char *propname)
223{
224 return fwnode_property_read_u64_array(fwnode, propname, NULL, 0);
225}
226
227struct software_node;
228
229
230
231
232
233
234
235struct software_node_ref_args {
236 const struct software_node *node;
237 unsigned int nargs;
238 u64 args[NR_FWNODE_REFERENCE_ARGS];
239};
240
241
242
243
244
245
246
247
248
249
250struct property_entry {
251 const char *name;
252 size_t length;
253 bool is_inline;
254 enum dev_prop_type type;
255 union {
256 const void *pointer;
257 union {
258 u8 u8_data[sizeof(u64) / sizeof(u8)];
259 u16 u16_data[sizeof(u64) / sizeof(u16)];
260 u32 u32_data[sizeof(u64) / sizeof(u32)];
261 u64 u64_data[sizeof(u64) / sizeof(u64)];
262 const char *str[sizeof(u64) / sizeof(char *)];
263 } value;
264 };
265};
266
267
268
269
270
271
272
273#define __PROPERTY_ENTRY_ELEMENT_SIZE(_elem_) \
274 sizeof(((struct property_entry *)NULL)->value._elem_[0])
275
276#define __PROPERTY_ENTRY_ARRAY_ELSIZE_LEN(_name_, _elsize_, _Type_, \
277 _val_, _len_) \
278(struct property_entry) { \
279 .name = _name_, \
280 .length = (_len_) * (_elsize_), \
281 .type = DEV_PROP_##_Type_, \
282 { .pointer = _val_ }, \
283}
284
285#define __PROPERTY_ENTRY_ARRAY_LEN(_name_, _elem_, _Type_, _val_, _len_)\
286 __PROPERTY_ENTRY_ARRAY_ELSIZE_LEN(_name_, \
287 __PROPERTY_ENTRY_ELEMENT_SIZE(_elem_), \
288 _Type_, _val_, _len_)
289
290#define PROPERTY_ENTRY_U8_ARRAY_LEN(_name_, _val_, _len_) \
291 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u8_data, U8, _val_, _len_)
292#define PROPERTY_ENTRY_U16_ARRAY_LEN(_name_, _val_, _len_) \
293 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u16_data, U16, _val_, _len_)
294#define PROPERTY_ENTRY_U32_ARRAY_LEN(_name_, _val_, _len_) \
295 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u32_data, U32, _val_, _len_)
296#define PROPERTY_ENTRY_U64_ARRAY_LEN(_name_, _val_, _len_) \
297 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u64_data, U64, _val_, _len_)
298#define PROPERTY_ENTRY_STRING_ARRAY_LEN(_name_, _val_, _len_) \
299 __PROPERTY_ENTRY_ARRAY_LEN(_name_, str, STRING, _val_, _len_)
300#define PROPERTY_ENTRY_REF_ARRAY_LEN(_name_, _val_, _len_) \
301 __PROPERTY_ENTRY_ARRAY_ELSIZE_LEN(_name_, \
302 sizeof(struct software_node_ref_args), \
303 REF, _val_, _len_)
304
305#define PROPERTY_ENTRY_U8_ARRAY(_name_, _val_) \
306 PROPERTY_ENTRY_U8_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
307#define PROPERTY_ENTRY_U16_ARRAY(_name_, _val_) \
308 PROPERTY_ENTRY_U16_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
309#define PROPERTY_ENTRY_U32_ARRAY(_name_, _val_) \
310 PROPERTY_ENTRY_U32_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
311#define PROPERTY_ENTRY_U64_ARRAY(_name_, _val_) \
312 PROPERTY_ENTRY_U64_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
313#define PROPERTY_ENTRY_STRING_ARRAY(_name_, _val_) \
314 PROPERTY_ENTRY_STRING_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
315#define PROPERTY_ENTRY_REF_ARRAY(_name_, _val_) \
316 PROPERTY_ENTRY_REF_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
317
318#define __PROPERTY_ENTRY_ELEMENT(_name_, _elem_, _Type_, _val_) \
319(struct property_entry) { \
320 .name = _name_, \
321 .length = __PROPERTY_ENTRY_ELEMENT_SIZE(_elem_), \
322 .is_inline = true, \
323 .type = DEV_PROP_##_Type_, \
324 { .value = { ._elem_[0] = _val_ } }, \
325}
326
327#define PROPERTY_ENTRY_U8(_name_, _val_) \
328 __PROPERTY_ENTRY_ELEMENT(_name_, u8_data, U8, _val_)
329#define PROPERTY_ENTRY_U16(_name_, _val_) \
330 __PROPERTY_ENTRY_ELEMENT(_name_, u16_data, U16, _val_)
331#define PROPERTY_ENTRY_U32(_name_, _val_) \
332 __PROPERTY_ENTRY_ELEMENT(_name_, u32_data, U32, _val_)
333#define PROPERTY_ENTRY_U64(_name_, _val_) \
334 __PROPERTY_ENTRY_ELEMENT(_name_, u64_data, U64, _val_)
335#define PROPERTY_ENTRY_STRING(_name_, _val_) \
336 __PROPERTY_ENTRY_ELEMENT(_name_, str, STRING, _val_)
337
338#define PROPERTY_ENTRY_BOOL(_name_) \
339(struct property_entry) { \
340 .name = _name_, \
341 .is_inline = true, \
342}
343
344#define PROPERTY_ENTRY_REF(_name_, _ref_, ...) \
345(struct property_entry) { \
346 .name = _name_, \
347 .length = sizeof(struct software_node_ref_args), \
348 .type = DEV_PROP_REF, \
349 { .pointer = &(const struct software_node_ref_args) { \
350 .node = _ref_, \
351 .nargs = ARRAY_SIZE(((u64[]){ 0, ##__VA_ARGS__ })) - 1, \
352 .args = { __VA_ARGS__ }, \
353 } }, \
354}
355
356struct property_entry *
357property_entries_dup(const struct property_entry *properties);
358
359void property_entries_free(const struct property_entry *properties);
360
361int device_add_properties(struct device *dev,
362 const struct property_entry *properties);
363void device_remove_properties(struct device *dev);
364
365bool device_dma_supported(struct device *dev);
366
367enum dev_dma_attr device_get_dma_attr(struct device *dev);
368
369const void *device_get_match_data(struct device *dev);
370
371int device_get_phy_mode(struct device *dev);
372
373void *device_get_mac_address(struct device *dev, char *addr, int alen);
374
375int fwnode_get_phy_mode(struct fwnode_handle *fwnode);
376void *fwnode_get_mac_address(struct fwnode_handle *fwnode,
377 char *addr, int alen);
378struct fwnode_handle *fwnode_graph_get_next_endpoint(
379 const struct fwnode_handle *fwnode, struct fwnode_handle *prev);
380struct fwnode_handle *
381fwnode_graph_get_port_parent(const struct fwnode_handle *fwnode);
382struct fwnode_handle *fwnode_graph_get_remote_port_parent(
383 const struct fwnode_handle *fwnode);
384struct fwnode_handle *fwnode_graph_get_remote_port(
385 const struct fwnode_handle *fwnode);
386struct fwnode_handle *fwnode_graph_get_remote_endpoint(
387 const struct fwnode_handle *fwnode);
388struct fwnode_handle *
389fwnode_graph_get_remote_node(const struct fwnode_handle *fwnode, u32 port,
390 u32 endpoint);
391
392static inline bool fwnode_graph_is_endpoint(struct fwnode_handle *fwnode)
393{
394 return fwnode_property_present(fwnode, "remote-endpoint");
395}
396
397
398
399
400
401
402
403
404
405
406
407#define FWNODE_GRAPH_ENDPOINT_NEXT BIT(0)
408#define FWNODE_GRAPH_DEVICE_DISABLED BIT(1)
409
410struct fwnode_handle *
411fwnode_graph_get_endpoint_by_id(const struct fwnode_handle *fwnode,
412 u32 port, u32 endpoint, unsigned long flags);
413
414#define fwnode_graph_for_each_endpoint(fwnode, child) \
415 for (child = NULL; \
416 (child = fwnode_graph_get_next_endpoint(fwnode, child)); )
417
418int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
419 struct fwnode_endpoint *endpoint);
420
421
422
423
424
425
426
427
428
429
430struct software_node {
431 const char *name;
432 const struct software_node *parent;
433 const struct property_entry *properties;
434};
435
436bool is_software_node(const struct fwnode_handle *fwnode);
437const struct software_node *
438to_software_node(const struct fwnode_handle *fwnode);
439struct fwnode_handle *software_node_fwnode(const struct software_node *node);
440
441const struct software_node *
442software_node_find_by_name(const struct software_node *parent,
443 const char *name);
444
445int software_node_register_nodes(const struct software_node *nodes);
446void software_node_unregister_nodes(const struct software_node *nodes);
447
448int software_node_register_node_group(const struct software_node **node_group);
449void software_node_unregister_node_group(const struct software_node **node_group);
450
451int software_node_register(const struct software_node *node);
452void software_node_unregister(const struct software_node *node);
453
454int software_node_notify(struct device *dev, unsigned long action);
455
456struct fwnode_handle *
457fwnode_create_software_node(const struct property_entry *properties,
458 const struct fwnode_handle *parent);
459void fwnode_remove_software_node(struct fwnode_handle *fwnode);
460
461#endif
462