xref: /linux/include/linux/fwnode.h (revision 4793dae01f47754e288cdbb3a22581cac2317f2b)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * fwnode.h - Firmware device node object handle type definition.
4  *
5  * This header file provides low-level data types and definitions for firmware
6  * and device property providers. The respective API header files supplied by
7  * them should contain all of the requisite data types and definitions for end
8  * users, so including it directly should not be necessary.
9  *
10  * Copyright (C) 2015, Intel Corporation
11  * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
12  */
13 
14 #ifndef _LINUX_FWNODE_H_
15 #define _LINUX_FWNODE_H_
16 
17 #include <linux/bits.h>
18 #include <linux/bitops.h>
19 #include <linux/err.h>
20 #include <linux/list.h>
21 #include <linux/types.h>
22 
23 enum dev_dma_attr {
24 	DEV_DMA_NOT_SUPPORTED,
25 	DEV_DMA_NON_COHERENT,
26 	DEV_DMA_COHERENT,
27 };
28 
29 struct fwnode_operations;
30 struct device;
31 
32 /*
33  * fwnode flags
34  *
35  * LINKS_ADDED:	The fwnode has already be parsed to add fwnode links.
36  * NOT_DEVICE:	The fwnode will never be populated as a struct device.
37  * INITIALIZED: The hardware corresponding to fwnode has been initialized.
38  * NEEDS_CHILD_BOUND_ON_ADD: For this fwnode/device to probe successfully, its
39  *			     driver needs its child devices to be bound with
40  *			     their respective drivers as soon as they are
41  *			     added.
42  * BEST_EFFORT: The fwnode/device needs to probe early and might be missing some
43  *		suppliers. Only enforce ordering with suppliers that have
44  *		drivers.
45  */
46 #define FWNODE_FLAG_LINKS_ADDED			0
47 #define FWNODE_FLAG_NOT_DEVICE			1
48 #define FWNODE_FLAG_INITIALIZED			2
49 #define FWNODE_FLAG_NEEDS_CHILD_BOUND_ON_ADD	3
50 #define FWNODE_FLAG_BEST_EFFORT			4
51 #define FWNODE_FLAG_VISITED			5
52 
53 struct fwnode_handle {
54 	struct fwnode_handle *secondary;
55 	const struct fwnode_operations *ops;
56 
57 	/* The below is used solely by device links, don't use otherwise */
58 	struct device *dev;
59 	struct list_head suppliers;
60 	struct list_head consumers;
61 	unsigned long flags;
62 };
63 
64 /*
65  * fwnode link flags
66  *
67  * CYCLE:	The fwnode link is part of a cycle. Don't defer probe.
68  * IGNORE:	Completely ignore this link, even during cycle detection.
69  */
70 #define FWLINK_FLAG_CYCLE			BIT(0)
71 #define FWLINK_FLAG_IGNORE			BIT(1)
72 
73 struct fwnode_link {
74 	struct fwnode_handle *supplier;
75 	struct list_head s_hook;
76 	struct fwnode_handle *consumer;
77 	struct list_head c_hook;
78 	u8 flags;
79 };
80 
81 /**
82  * struct fwnode_endpoint - Fwnode graph endpoint
83  * @port: Port number
84  * @id: Endpoint id
85  * @local_fwnode: reference to the related fwnode
86  */
87 struct fwnode_endpoint {
88 	unsigned int port;
89 	unsigned int id;
90 	const struct fwnode_handle *local_fwnode;
91 };
92 
93 /*
94  * ports and endpoints defined as software_nodes should all follow a common
95  * naming scheme; use these macros to ensure commonality.
96  */
97 #define SWNODE_GRAPH_PORT_NAME_FMT		"port@%u"
98 #define SWNODE_GRAPH_ENDPOINT_NAME_FMT		"endpoint@%u"
99 
100 #define NR_FWNODE_REFERENCE_ARGS	16
101 
102 /**
103  * struct fwnode_reference_args - Fwnode reference with additional arguments
104  * @fwnode:- A reference to the base fwnode
105  * @nargs: Number of elements in @args array
106  * @args: Integer arguments on the fwnode
107  */
108 struct fwnode_reference_args {
109 	struct fwnode_handle *fwnode;
110 	unsigned int nargs;
111 	u64 args[NR_FWNODE_REFERENCE_ARGS];
112 };
113 
114 /**
115  * struct fwnode_operations - Operations for fwnode interface
116  * @get: Get a reference to an fwnode.
117  * @put: Put a reference to an fwnode.
118  * @device_is_available: Return true if the device is available.
119  * @device_get_match_data: Return the device driver match data.
120  * @property_present: Return true if a property is present.
121  * @property_read_bool: Return a boolean property value.
122  * @property_read_int_array: Read an array of integer properties. Return zero on
123  *			     success, a negative error code otherwise.
124  * @property_read_string_array: Read an array of string properties. Return zero
125  *				on success, a negative error code otherwise.
126  * @get_name: Return the name of an fwnode.
127  * @get_name_prefix: Get a prefix for a node (for printing purposes).
128  * @get_parent: Return the parent of an fwnode.
129  * @get_next_child_node: Return the next child node in an iteration.
130  * @get_named_child_node: Return a child node with a given name.
131  * @get_reference_args: Return a reference pointed to by a property, with args
132  * @graph_get_next_endpoint: Return an endpoint node in an iteration.
133  * @graph_get_remote_endpoint: Return the remote endpoint node of a local
134  *			       endpoint node.
135  * @graph_get_port_parent: Return the parent node of a port node.
136  * @graph_parse_endpoint: Parse endpoint for port and endpoint id.
137  * @add_links:	Create fwnode links to all the suppliers of the fwnode. Return
138  *		zero on success, a negative error code otherwise.
139  */
140 struct fwnode_operations {
141 	struct fwnode_handle *(*get)(struct fwnode_handle *fwnode);
142 	void (*put)(struct fwnode_handle *fwnode);
143 	bool (*device_is_available)(const struct fwnode_handle *fwnode);
144 	const void *(*device_get_match_data)(const struct fwnode_handle *fwnode,
145 					     const struct device *dev);
146 	bool (*device_dma_supported)(const struct fwnode_handle *fwnode);
147 	enum dev_dma_attr
148 	(*device_get_dma_attr)(const struct fwnode_handle *fwnode);
149 	bool (*property_present)(const struct fwnode_handle *fwnode,
150 				 const char *propname);
151 	bool (*property_read_bool)(const struct fwnode_handle *fwnode,
152 				   const char *propname);
153 	int (*property_read_int_array)(const struct fwnode_handle *fwnode,
154 				       const char *propname,
155 				       unsigned int elem_size, void *val,
156 				       size_t nval);
157 	int
158 	(*property_read_string_array)(const struct fwnode_handle *fwnode_handle,
159 				      const char *propname, const char **val,
160 				      size_t nval);
161 	const char *(*get_name)(const struct fwnode_handle *fwnode);
162 	const char *(*get_name_prefix)(const struct fwnode_handle *fwnode);
163 	struct fwnode_handle *(*get_parent)(const struct fwnode_handle *fwnode);
164 	struct fwnode_handle *
165 	(*get_next_child_node)(const struct fwnode_handle *fwnode,
166 			       struct fwnode_handle *child);
167 	struct fwnode_handle *
168 	(*get_named_child_node)(const struct fwnode_handle *fwnode,
169 				const char *name);
170 	int (*get_reference_args)(const struct fwnode_handle *fwnode,
171 				  const char *prop, const char *nargs_prop,
172 				  unsigned int nargs, unsigned int index,
173 				  struct fwnode_reference_args *args);
174 	struct fwnode_handle *
175 	(*graph_get_next_endpoint)(const struct fwnode_handle *fwnode,
176 				   struct fwnode_handle *prev);
177 	struct fwnode_handle *
178 	(*graph_get_remote_endpoint)(const struct fwnode_handle *fwnode);
179 	struct fwnode_handle *
180 	(*graph_get_port_parent)(struct fwnode_handle *fwnode);
181 	int (*graph_parse_endpoint)(const struct fwnode_handle *fwnode,
182 				    struct fwnode_endpoint *endpoint);
183 	void __iomem *(*iomap)(struct fwnode_handle *fwnode, int index);
184 	int (*irq_get)(const struct fwnode_handle *fwnode, unsigned int index);
185 	int (*add_links)(struct fwnode_handle *fwnode);
186 };
187 
188 #define fwnode_has_op(fwnode, op)					\
189 	(!IS_ERR_OR_NULL(fwnode) && (fwnode)->ops && (fwnode)->ops->op)
190 
191 #define fwnode_call_int_op(fwnode, op, ...)				\
192 	(fwnode_has_op(fwnode, op) ?					\
193 	 (fwnode)->ops->op(fwnode, ## __VA_ARGS__) : (IS_ERR_OR_NULL(fwnode) ? -EINVAL : -ENXIO))
194 
195 #define fwnode_call_bool_op(fwnode, op, ...)		\
196 	(fwnode_has_op(fwnode, op) ?			\
197 	 (fwnode)->ops->op(fwnode, ## __VA_ARGS__) : false)
198 
199 #define fwnode_call_ptr_op(fwnode, op, ...)		\
200 	(fwnode_has_op(fwnode, op) ?			\
201 	 (fwnode)->ops->op(fwnode, ## __VA_ARGS__) : NULL)
202 #define fwnode_call_void_op(fwnode, op, ...)				\
203 	do {								\
204 		if (fwnode_has_op(fwnode, op))				\
205 			(fwnode)->ops->op(fwnode, ## __VA_ARGS__);	\
206 	} while (false)
207 
fwnode_init(struct fwnode_handle * fwnode,const struct fwnode_operations * ops)208 static inline void fwnode_init(struct fwnode_handle *fwnode,
209 			       const struct fwnode_operations *ops)
210 {
211 	fwnode->ops = ops;
212 	INIT_LIST_HEAD(&fwnode->consumers);
213 	INIT_LIST_HEAD(&fwnode->suppliers);
214 }
215 
fwnode_set_flag(struct fwnode_handle * fwnode,unsigned int bit)216 static inline void fwnode_set_flag(struct fwnode_handle *fwnode,
217 				   unsigned int bit)
218 {
219 	set_bit(bit, &fwnode->flags);
220 }
221 
fwnode_clear_flag(struct fwnode_handle * fwnode,unsigned int bit)222 static inline void fwnode_clear_flag(struct fwnode_handle *fwnode,
223 				     unsigned int bit)
224 {
225 	clear_bit(bit, &fwnode->flags);
226 }
227 
fwnode_assign_flag(struct fwnode_handle * fwnode,unsigned int bit,bool value)228 static inline void fwnode_assign_flag(struct fwnode_handle *fwnode,
229 				      unsigned int bit, bool value)
230 {
231 	assign_bit(bit, &fwnode->flags, value);
232 }
233 
fwnode_test_flag(struct fwnode_handle * fwnode,unsigned int bit)234 static inline bool fwnode_test_flag(struct fwnode_handle *fwnode,
235 				    unsigned int bit)
236 {
237 	return test_bit(bit, &fwnode->flags);
238 }
239 
fwnode_dev_initialized(struct fwnode_handle * fwnode,bool initialized)240 static inline void fwnode_dev_initialized(struct fwnode_handle *fwnode,
241 					  bool initialized)
242 {
243 	if (IS_ERR_OR_NULL(fwnode))
244 		return;
245 
246 	fwnode_assign_flag(fwnode, FWNODE_FLAG_INITIALIZED, initialized);
247 }
248 
249 int fwnode_link_add(struct fwnode_handle *con, struct fwnode_handle *sup,
250 		    u8 flags);
251 void fwnode_links_purge(struct fwnode_handle *fwnode);
252 void fw_devlink_purge_absent_suppliers(struct fwnode_handle *fwnode);
253 bool fw_devlink_is_strict(void);
254 
255 #endif
256