GNU Linux-libre 5.15.54-gnu
[releases.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 #include <linux/err.h>
15
16 struct fwnode_operations;
17 struct device;
18
19 /*
20  * fwnode link flags
21  *
22  * LINKS_ADDED: The fwnode has already be parsed to add fwnode links.
23  * NOT_DEVICE:  The fwnode will never be populated as a struct device.
24  * INITIALIZED: The hardware corresponding to fwnode has been initialized.
25  * NEEDS_CHILD_BOUND_ON_ADD: For this fwnode/device to probe successfully, its
26  *                           driver needs its child devices to be bound with
27  *                           their respective drivers as soon as they are
28  *                           added.
29  */
30 #define FWNODE_FLAG_LINKS_ADDED                 BIT(0)
31 #define FWNODE_FLAG_NOT_DEVICE                  BIT(1)
32 #define FWNODE_FLAG_INITIALIZED                 BIT(2)
33 #define FWNODE_FLAG_NEEDS_CHILD_BOUND_ON_ADD    BIT(3)
34
35 struct fwnode_handle {
36         struct fwnode_handle *secondary;
37         const struct fwnode_operations *ops;
38         struct device *dev;
39         struct list_head suppliers;
40         struct list_head consumers;
41         u8 flags;
42 };
43
44 struct fwnode_link {
45         struct fwnode_handle *supplier;
46         struct list_head s_hook;
47         struct fwnode_handle *consumer;
48         struct list_head c_hook;
49 };
50
51 /**
52  * struct fwnode_endpoint - Fwnode graph endpoint
53  * @port: Port number
54  * @id: Endpoint id
55  * @local_fwnode: reference to the related fwnode
56  */
57 struct fwnode_endpoint {
58         unsigned int port;
59         unsigned int id;
60         const struct fwnode_handle *local_fwnode;
61 };
62
63 /*
64  * ports and endpoints defined as software_nodes should all follow a common
65  * naming scheme; use these macros to ensure commonality.
66  */
67 #define SWNODE_GRAPH_PORT_NAME_FMT              "port@%u"
68 #define SWNODE_GRAPH_ENDPOINT_NAME_FMT          "endpoint@%u"
69
70 #define NR_FWNODE_REFERENCE_ARGS        8
71
72 /**
73  * struct fwnode_reference_args - Fwnode reference with additional arguments
74  * @fwnode:- A reference to the base fwnode
75  * @nargs: Number of elements in @args array
76  * @args: Integer arguments on the fwnode
77  */
78 struct fwnode_reference_args {
79         struct fwnode_handle *fwnode;
80         unsigned int nargs;
81         u64 args[NR_FWNODE_REFERENCE_ARGS];
82 };
83
84 /**
85  * struct fwnode_operations - Operations for fwnode interface
86  * @get: Get a reference to an fwnode.
87  * @put: Put a reference to an fwnode.
88  * @device_is_available: Return true if the device is available.
89  * @device_get_match_data: Return the device driver match data.
90  * @property_present: Return true if a property is present.
91  * @property_read_int_array: Read an array of integer properties. Return zero on
92  *                           success, a negative error code otherwise.
93  * @property_read_string_array: Read an array of string properties. Return zero
94  *                              on success, a negative error code otherwise.
95  * @get_name: Return the name of an fwnode.
96  * @get_name_prefix: Get a prefix for a node (for printing purposes).
97  * @get_parent: Return the parent of an fwnode.
98  * @get_next_child_node: Return the next child node in an iteration.
99  * @get_named_child_node: Return a child node with a given name.
100  * @get_reference_args: Return a reference pointed to by a property, with args
101  * @graph_get_next_endpoint: Return an endpoint node in an iteration.
102  * @graph_get_remote_endpoint: Return the remote endpoint node of a local
103  *                             endpoint node.
104  * @graph_get_port_parent: Return the parent node of a port node.
105  * @graph_parse_endpoint: Parse endpoint for port and endpoint id.
106  * @add_links:  Create fwnode links to all the suppliers of the fwnode. Return
107  *              zero on success, a negative error code otherwise.
108  */
109 struct fwnode_operations {
110         struct fwnode_handle *(*get)(struct fwnode_handle *fwnode);
111         void (*put)(struct fwnode_handle *fwnode);
112         bool (*device_is_available)(const struct fwnode_handle *fwnode);
113         const void *(*device_get_match_data)(const struct fwnode_handle *fwnode,
114                                              const struct device *dev);
115         bool (*property_present)(const struct fwnode_handle *fwnode,
116                                  const char *propname);
117         int (*property_read_int_array)(const struct fwnode_handle *fwnode,
118                                        const char *propname,
119                                        unsigned int elem_size, void *val,
120                                        size_t nval);
121         int
122         (*property_read_string_array)(const struct fwnode_handle *fwnode_handle,
123                                       const char *propname, const char **val,
124                                       size_t nval);
125         const char *(*get_name)(const struct fwnode_handle *fwnode);
126         const char *(*get_name_prefix)(const struct fwnode_handle *fwnode);
127         struct fwnode_handle *(*get_parent)(const struct fwnode_handle *fwnode);
128         struct fwnode_handle *
129         (*get_next_child_node)(const struct fwnode_handle *fwnode,
130                                struct fwnode_handle *child);
131         struct fwnode_handle *
132         (*get_named_child_node)(const struct fwnode_handle *fwnode,
133                                 const char *name);
134         int (*get_reference_args)(const struct fwnode_handle *fwnode,
135                                   const char *prop, const char *nargs_prop,
136                                   unsigned int nargs, unsigned int index,
137                                   struct fwnode_reference_args *args);
138         struct fwnode_handle *
139         (*graph_get_next_endpoint)(const struct fwnode_handle *fwnode,
140                                    struct fwnode_handle *prev);
141         struct fwnode_handle *
142         (*graph_get_remote_endpoint)(const struct fwnode_handle *fwnode);
143         struct fwnode_handle *
144         (*graph_get_port_parent)(struct fwnode_handle *fwnode);
145         int (*graph_parse_endpoint)(const struct fwnode_handle *fwnode,
146                                     struct fwnode_endpoint *endpoint);
147         int (*add_links)(struct fwnode_handle *fwnode);
148 };
149
150 #define fwnode_has_op(fwnode, op)                                       \
151         (!IS_ERR_OR_NULL(fwnode) && (fwnode)->ops && (fwnode)->ops->op)
152
153 #define fwnode_call_int_op(fwnode, op, ...)                             \
154         (fwnode_has_op(fwnode, op) ?                                    \
155          (fwnode)->ops->op(fwnode, ## __VA_ARGS__) : (IS_ERR_OR_NULL(fwnode) ? -EINVAL : -ENXIO))
156
157 #define fwnode_call_bool_op(fwnode, op, ...)            \
158         (fwnode_has_op(fwnode, op) ?                    \
159          (fwnode)->ops->op(fwnode, ## __VA_ARGS__) : false)
160
161 #define fwnode_call_ptr_op(fwnode, op, ...)             \
162         (fwnode_has_op(fwnode, op) ?                    \
163          (fwnode)->ops->op(fwnode, ## __VA_ARGS__) : NULL)
164 #define fwnode_call_void_op(fwnode, op, ...)                            \
165         do {                                                            \
166                 if (fwnode_has_op(fwnode, op))                          \
167                         (fwnode)->ops->op(fwnode, ## __VA_ARGS__);      \
168         } while (false)
169 #define get_dev_from_fwnode(fwnode)     get_device((fwnode)->dev)
170
171 static inline void fwnode_init(struct fwnode_handle *fwnode,
172                                const struct fwnode_operations *ops)
173 {
174         fwnode->ops = ops;
175         INIT_LIST_HEAD(&fwnode->consumers);
176         INIT_LIST_HEAD(&fwnode->suppliers);
177 }
178
179 static inline void fwnode_dev_initialized(struct fwnode_handle *fwnode,
180                                           bool initialized)
181 {
182         if (IS_ERR_OR_NULL(fwnode))
183                 return;
184
185         if (initialized)
186                 fwnode->flags |= FWNODE_FLAG_INITIALIZED;
187         else
188                 fwnode->flags &= ~FWNODE_FLAG_INITIALIZED;
189 }
190
191 extern u32 fw_devlink_get_flags(void);
192 extern bool fw_devlink_is_strict(void);
193 int fwnode_link_add(struct fwnode_handle *con, struct fwnode_handle *sup);
194 void fwnode_links_purge(struct fwnode_handle *fwnode);
195 void fw_devlink_purge_absent_suppliers(struct fwnode_handle *fwnode);
196
197 #endif