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