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