driver core: Redefine the meaning of fwnode_operations.add_links()
[linux-2.6-microblaze.git] / include / linux / fwnode.h
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * fwnode.h - Firmware device node object handle type definition.
4  *
5  * Copyright (C) 2015, Intel Corporation
6  * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
7  */
8
9 #ifndef _LINUX_FWNODE_H_
10 #define _LINUX_FWNODE_H_
11
12 #include <linux/types.h>
13 #include <linux/list.h>
14
15 struct fwnode_operations;
16 struct device;
17
18 struct fwnode_handle {
19         struct fwnode_handle *secondary;
20         const struct fwnode_operations *ops;
21         struct device *dev;
22         struct list_head suppliers;
23         struct list_head consumers;
24 };
25
26 struct fwnode_link {
27         struct fwnode_handle *supplier;
28         struct list_head s_hook;
29         struct fwnode_handle *consumer;
30         struct list_head c_hook;
31 };
32
33 /**
34  * struct fwnode_endpoint - Fwnode graph endpoint
35  * @port: Port number
36  * @id: Endpoint id
37  * @local_fwnode: reference to the related fwnode
38  */
39 struct fwnode_endpoint {
40         unsigned int port;
41         unsigned int id;
42         const struct fwnode_handle *local_fwnode;
43 };
44
45 #define NR_FWNODE_REFERENCE_ARGS        8
46
47 /**
48  * struct fwnode_reference_args - Fwnode reference with additional arguments
49  * @fwnode:- A reference to the base fwnode
50  * @nargs: Number of elements in @args array
51  * @args: Integer arguments on the fwnode
52  */
53 struct fwnode_reference_args {
54         struct fwnode_handle *fwnode;
55         unsigned int nargs;
56         u64 args[NR_FWNODE_REFERENCE_ARGS];
57 };
58
59 /**
60  * struct fwnode_operations - Operations for fwnode interface
61  * @get: Get a reference to an fwnode.
62  * @put: Put a reference to an fwnode.
63  * @device_is_available: Return true if the device is available.
64  * @device_get_match_data: Return the device driver match data.
65  * @property_present: Return true if a property is present.
66  * @property_read_int_array: Read an array of integer properties. Return zero on
67  *                           success, a negative error code otherwise.
68  * @property_read_string_array: Read an array of string properties. Return zero
69  *                              on success, a negative error code otherwise.
70  * @get_name: Return the name of an fwnode.
71  * @get_name_prefix: Get a prefix for a node (for printing purposes).
72  * @get_parent: Return the parent of an fwnode.
73  * @get_next_child_node: Return the next child node in an iteration.
74  * @get_named_child_node: Return a child node with a given name.
75  * @get_reference_args: Return a reference pointed to by a property, with args
76  * @graph_get_next_endpoint: Return an endpoint node in an iteration.
77  * @graph_get_remote_endpoint: Return the remote endpoint node of a local
78  *                             endpoint node.
79  * @graph_get_port_parent: Return the parent node of a port node.
80  * @graph_parse_endpoint: Parse endpoint for port and endpoint id.
81  * @add_links:  Create fwnode links to all the suppliers of the fwnode. Return
82  *              zero on success, a negative error code otherwise.
83  */
84 struct fwnode_operations {
85         struct fwnode_handle *(*get)(struct fwnode_handle *fwnode);
86         void (*put)(struct fwnode_handle *fwnode);
87         bool (*device_is_available)(const struct fwnode_handle *fwnode);
88         const void *(*device_get_match_data)(const struct fwnode_handle *fwnode,
89                                              const struct device *dev);
90         bool (*property_present)(const struct fwnode_handle *fwnode,
91                                  const char *propname);
92         int (*property_read_int_array)(const struct fwnode_handle *fwnode,
93                                        const char *propname,
94                                        unsigned int elem_size, void *val,
95                                        size_t nval);
96         int
97         (*property_read_string_array)(const struct fwnode_handle *fwnode_handle,
98                                       const char *propname, const char **val,
99                                       size_t nval);
100         const char *(*get_name)(const struct fwnode_handle *fwnode);
101         const char *(*get_name_prefix)(const struct fwnode_handle *fwnode);
102         struct fwnode_handle *(*get_parent)(const struct fwnode_handle *fwnode);
103         struct fwnode_handle *
104         (*get_next_child_node)(const struct fwnode_handle *fwnode,
105                                struct fwnode_handle *child);
106         struct fwnode_handle *
107         (*get_named_child_node)(const struct fwnode_handle *fwnode,
108                                 const char *name);
109         int (*get_reference_args)(const struct fwnode_handle *fwnode,
110                                   const char *prop, const char *nargs_prop,
111                                   unsigned int nargs, unsigned int index,
112                                   struct fwnode_reference_args *args);
113         struct fwnode_handle *
114         (*graph_get_next_endpoint)(const struct fwnode_handle *fwnode,
115                                    struct fwnode_handle *prev);
116         struct fwnode_handle *
117         (*graph_get_remote_endpoint)(const struct fwnode_handle *fwnode);
118         struct fwnode_handle *
119         (*graph_get_port_parent)(struct fwnode_handle *fwnode);
120         int (*graph_parse_endpoint)(const struct fwnode_handle *fwnode,
121                                     struct fwnode_endpoint *endpoint);
122         int (*add_links)(struct fwnode_handle *fwnode,
123                          struct device *dev);
124 };
125
126 #define fwnode_has_op(fwnode, op)                               \
127         ((fwnode) && (fwnode)->ops && (fwnode)->ops->op)
128 #define fwnode_call_int_op(fwnode, op, ...)                             \
129         (fwnode ? (fwnode_has_op(fwnode, op) ?                          \
130                    (fwnode)->ops->op(fwnode, ## __VA_ARGS__) : -ENXIO) : \
131          -EINVAL)
132
133 #define fwnode_call_bool_op(fwnode, op, ...)            \
134         (fwnode_has_op(fwnode, op) ?                    \
135          (fwnode)->ops->op(fwnode, ## __VA_ARGS__) : false)
136
137 #define fwnode_call_ptr_op(fwnode, op, ...)             \
138         (fwnode_has_op(fwnode, op) ?                    \
139          (fwnode)->ops->op(fwnode, ## __VA_ARGS__) : NULL)
140 #define fwnode_call_void_op(fwnode, op, ...)                            \
141         do {                                                            \
142                 if (fwnode_has_op(fwnode, op))                          \
143                         (fwnode)->ops->op(fwnode, ## __VA_ARGS__);      \
144         } while (false)
145 #define get_dev_from_fwnode(fwnode)     get_device((fwnode)->dev)
146
147 static inline void fwnode_init(struct fwnode_handle *fwnode,
148                                const struct fwnode_operations *ops)
149 {
150         fwnode->ops = ops;
151         INIT_LIST_HEAD(&fwnode->consumers);
152         INIT_LIST_HEAD(&fwnode->suppliers);
153 }
154
155 extern u32 fw_devlink_get_flags(void);
156 int fwnode_link_add(struct fwnode_handle *con, struct fwnode_handle *sup);
157 void fwnode_links_purge(struct fwnode_handle *fwnode);
158
159 #endif