1 #ifndef __DTB_FWNODE_H__
2 #define __DTB_FWNODE_H__
3 
4 #include "libfdt_env.h"
5 #include <rtthread.h>
6 #include <stdint.h>
7 #include <stdbool.h>
8 
9 struct fwnode_operations;
10 struct rt_device;
11 
12 #define FWNODE_FLAG_LINKS_ADDED 0x01
13 #define FWNODE_FLAG_NOT_DEVICE 0x02
14 #define FWNODE_FLAG_INITIALIZED 0x04
15 
16 #define NR_FWNODE_REFERENCE_ARGS 8
17 
18 struct fwnode_handle
19 {
20     struct fwnode_handle *secondary;
21     const struct fwnode_operations *ops;
22     struct rt_device *dev;
23     struct rt_list_node suppliers;
24     struct rt_list_node consumers;
25     uint8_t flags;
26 };
27 
28 struct fwnode_link
29 {
30     struct fwnode_handle *supplier;
31     struct rt_list_node s_hook;
32     struct fwnode_handle *consumer;
33     struct rt_list_node c_hook;
34 };
35 
36 struct fwnode_endpoint
37 {
38     unsigned int port;
39     unsigned int id;
40     const struct fwnode_handle *local_fwnode;
41 };
42 
43 struct fwnode_reference_args
44 {
45     struct fwnode_handle *fwnode;
46     unsigned int nargs;
47     uint64_t args[NR_FWNODE_REFERENCE_ARGS];
48 };
49 
50 struct fwnode_operations
51 {
52     struct fwnode_handle *(*get)(struct fwnode_handle *fwnode);
53     void (*put)(struct fwnode_handle *fwnode);
54     bool (*device_is_available)(const struct fwnode_handle *fwnode);
55     const void *(*device_get_match_data)(const struct fwnode_handle *fwnode,
56                                          const struct rt_device *dev);
57     bool (*property_present)(const struct fwnode_handle *fwnode,
58                              const char *propname);
59     int (*property_read_int_array)(const struct fwnode_handle *fwnode,
60                                    const char *propname,
61                                    unsigned int elem_size, void *val,
62                                    size_t nval);
63     int (*property_read_string_array)(const struct fwnode_handle *fwnode_handle,
64                                       const char *propname, const char **val,
65                                       size_t nval);
66     const char *(*get_name)(const struct fwnode_handle *fwnode);
67     const char *(*get_name_prefix)(const struct fwnode_handle *fwnode);
68     struct fwnode_handle *(*get_parent)(const struct fwnode_handle *fwnode);
69     struct fwnode_handle *(*get_next_child_node)(const struct fwnode_handle *fwnode,
70                                                  struct fwnode_handle *child);
71     struct fwnode_handle *(*get_named_child_node)(const struct fwnode_handle *fwnode,
72                                                   const char *name);
73     int (*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);
77     struct fwnode_handle *(*graph_get_next_endpoint)(const struct fwnode_handle *fwnode,
78                                                      struct fwnode_handle *prev);
79     struct fwnode_handle *(*graph_get_remote_endpoint)(const struct fwnode_handle *fwnode);
80     struct fwnode_handle *(*graph_get_port_parent)(struct fwnode_handle *fwnode);
81     int (*graph_parse_endpoint)(const struct fwnode_handle *fwnode,
82                                 struct fwnode_endpoint *endpoint);
83     int (*add_links)(struct fwnode_handle *fwnode);
84 };
85 
86 #define fwnode_has_op(fwnode, op) \
87     ((fwnode) && (fwnode)->ops && (fwnode)->ops->op)
88 #define fwnode_call_int_op(fwnode, op, ...) \
89     (fwnode ? (fwnode_has_op(fwnode, op) ? (fwnode)->ops->op(fwnode, ##__VA_ARGS__) : -ENXIO) : -EINVAL)
90 
91 #define fwnode_call_bool_op(fwnode, op, ...) \
92     (fwnode_has_op(fwnode, op) ? (fwnode)->ops->op(fwnode, ##__VA_ARGS__) : false)
93 
94 #define fwnode_call_ptr_op(fwnode, op, ...) \
95     (fwnode_has_op(fwnode, op) ? (fwnode)->ops->op(fwnode, ##__VA_ARGS__) : NULL)
96 #define fwnode_call_void_op(fwnode, op, ...)          \
97     do                                                \
98     {                                                 \
99         if (fwnode_has_op(fwnode, op))                \
100             (fwnode)->ops->op(fwnode, ##__VA_ARGS__); \
101     } while (false)
102 
103 #define get_dev_from_fwnode(fwnode) ((fwnode)->dev)
104 
fwnode_init(struct fwnode_handle * fwnode,const struct fwnode_operations * ops)105 static inline void fwnode_init(struct fwnode_handle *fwnode,
106                                const struct fwnode_operations *ops)
107 {
108     fwnode->ops = ops;
109     rt_list_init(&fwnode->consumers);
110     rt_list_init(&fwnode->suppliers);
111 }
112 
fwnode_dev_initialized(struct fwnode_handle * fwnode,bool initialized)113 static inline void fwnode_dev_initialized(struct fwnode_handle *fwnode,
114                                           bool initialized)
115 {
116     if (!fwnode)
117         return;
118 
119     if (initialized)
120         fwnode->flags |= FWNODE_FLAG_INITIALIZED;
121     else
122         fwnode->flags &= ~FWNODE_FLAG_INITIALIZED;
123 }
124 
125 #endif //__DTB_FWNODE_H__
126