xref: /linux/include/linux/fwnode.h (revision f345fc7a07065902fab1d33c11dfe631ee95357c)
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/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  * IGNORE:	Completely ignore this link, even during cycle detection.
63  */
64 #define FWLINK_FLAG_CYCLE			BIT(0)
65 #define FWLINK_FLAG_IGNORE			BIT(1)
66 
67 struct fwnode_link {
68 	struct fwnode_handle *supplier;
69 	struct list_head s_hook;
70 	struct fwnode_handle *consumer;
71 	struct list_head c_hook;
72 	u8 flags;
73 };
74 
75 /**
76  * struct fwnode_endpoint - Fwnode graph endpoint
77  * @port: Port number
78  * @id: Endpoint id
79  * @local_fwnode: reference to the related fwnode
80  */
81 struct fwnode_endpoint {
82 	unsigned int port;
83 	unsigned int id;
84 	const struct fwnode_handle *local_fwnode;
85 };
86 
87 /*
88  * ports and endpoints defined as software_nodes should all follow a common
89  * naming scheme; use these macros to ensure commonality.
90  */
91 #define SWNODE_GRAPH_PORT_NAME_FMT		"port@%u"
92 #define SWNODE_GRAPH_ENDPOINT_NAME_FMT		"endpoint@%u"
93 
94 #define NR_FWNODE_REFERENCE_ARGS	8
95 
96 /**
97  * struct fwnode_reference_args - Fwnode reference with additional arguments
98  * @fwnode:- A reference to the base fwnode
99  * @nargs: Number of elements in @args array
100  * @args: Integer arguments on the fwnode
101  */
102 struct fwnode_reference_args {
103 	struct fwnode_handle *fwnode;
104 	unsigned int nargs;
105 	u64 args[NR_FWNODE_REFERENCE_ARGS];
106 };
107 
108 /**
109  * struct fwnode_operations - Operations for fwnode interface
110  * @get: Get a reference to an fwnode.
111  * @put: Put a reference to an fwnode.
112  * @device_is_available: Return true if the device is available.
113  * @device_get_match_data: Return the device driver match data.
114  * @property_present: Return true if a property is present.
115  * @property_read_bool: Return a boolean property value.
116  * @property_read_int_array: Read an array of integer properties. Return zero on
117  *			     success, a negative error code otherwise.
118  * @property_read_string_array: Read an array of string properties. Return zero
119  *				on success, a negative error code otherwise.
120  * @get_name: Return the name of an fwnode.
121  * @get_name_prefix: Get a prefix for a node (for printing purposes).
122  * @get_parent: Return the parent of an fwnode.
123  * @get_next_child_node: Return the next child node in an iteration.
124  * @get_named_child_node: Return a child node with a given name.
125  * @get_reference_args: Return a reference pointed to by a property, with args
126  * @graph_get_next_endpoint: Return an endpoint node in an iteration.
127  * @graph_get_remote_endpoint: Return the remote endpoint node of a local
128  *			       endpoint node.
129  * @graph_get_port_parent: Return the parent node of a port node.
130  * @graph_parse_endpoint: Parse endpoint for port and endpoint id.
131  * @add_links:	Create fwnode links to all the suppliers of the fwnode. Return
132  *		zero on success, a negative error code otherwise.
133  */
134 struct fwnode_operations {
135 	struct fwnode_handle *(*get)(struct fwnode_handle *fwnode);
136 	void (*put)(struct fwnode_handle *fwnode);
137 	bool (*device_is_available)(const struct fwnode_handle *fwnode);
138 	const void *(*device_get_match_data)(const struct fwnode_handle *fwnode,
139 					     const struct device *dev);
140 	bool (*device_dma_supported)(const struct fwnode_handle *fwnode);
141 	enum dev_dma_attr
142 	(*device_get_dma_attr)(const struct fwnode_handle *fwnode);
143 	bool (*property_present)(const struct fwnode_handle *fwnode,
144 				 const char *propname);
145 	bool (*property_read_bool)(const struct fwnode_handle *fwnode,
146 				   const char *propname);
147 	int (*property_read_int_array)(const struct fwnode_handle *fwnode,
148 				       const char *propname,
149 				       unsigned int elem_size, void *val,
150 				       size_t nval);
151 	int
152 	(*property_read_string_array)(const struct fwnode_handle *fwnode_handle,
153 				      const char *propname, const char **val,
154 				      size_t nval);
155 	const char *(*get_name)(const struct fwnode_handle *fwnode);
156 	const char *(*get_name_prefix)(const struct fwnode_handle *fwnode);
157 	struct fwnode_handle *(*get_parent)(const struct fwnode_handle *fwnode);
158 	struct fwnode_handle *
159 	(*get_next_child_node)(const struct fwnode_handle *fwnode,
160 			       struct fwnode_handle *child);
161 	struct fwnode_handle *
162 	(*get_named_child_node)(const struct fwnode_handle *fwnode,
163 				const char *name);
164 	int (*get_reference_args)(const struct fwnode_handle *fwnode,
165 				  const char *prop, const char *nargs_prop,
166 				  unsigned int nargs, unsigned int index,
167 				  struct fwnode_reference_args *args);
168 	struct fwnode_handle *
169 	(*graph_get_next_endpoint)(const struct fwnode_handle *fwnode,
170 				   struct fwnode_handle *prev);
171 	struct fwnode_handle *
172 	(*graph_get_remote_endpoint)(const struct fwnode_handle *fwnode);
173 	struct fwnode_handle *
174 	(*graph_get_port_parent)(struct fwnode_handle *fwnode);
175 	int (*graph_parse_endpoint)(const struct fwnode_handle *fwnode,
176 				    struct fwnode_endpoint *endpoint);
177 	void __iomem *(*iomap)(struct fwnode_handle *fwnode, int index);
178 	int (*irq_get)(const struct fwnode_handle *fwnode, unsigned int index);
179 	int (*add_links)(struct fwnode_handle *fwnode);
180 };
181 
182 #define fwnode_has_op(fwnode, op)					\
183 	(!IS_ERR_OR_NULL(fwnode) && (fwnode)->ops && (fwnode)->ops->op)
184 
185 #define fwnode_call_int_op(fwnode, op, ...)				\
186 	(fwnode_has_op(fwnode, op) ?					\
187 	 (fwnode)->ops->op(fwnode, ## __VA_ARGS__) : (IS_ERR_OR_NULL(fwnode) ? -EINVAL : -ENXIO))
188 
189 #define fwnode_call_bool_op(fwnode, op, ...)		\
190 	(fwnode_has_op(fwnode, op) ?			\
191 	 (fwnode)->ops->op(fwnode, ## __VA_ARGS__) : false)
192 
193 #define fwnode_call_ptr_op(fwnode, op, ...)		\
194 	(fwnode_has_op(fwnode, op) ?			\
195 	 (fwnode)->ops->op(fwnode, ## __VA_ARGS__) : NULL)
196 #define fwnode_call_void_op(fwnode, op, ...)				\
197 	do {								\
198 		if (fwnode_has_op(fwnode, op))				\
199 			(fwnode)->ops->op(fwnode, ## __VA_ARGS__);	\
200 	} while (false)
201 
202 static inline void fwnode_init(struct fwnode_handle *fwnode,
203 			       const struct fwnode_operations *ops)
204 {
205 	fwnode->ops = ops;
206 	INIT_LIST_HEAD(&fwnode->consumers);
207 	INIT_LIST_HEAD(&fwnode->suppliers);
208 }
209 
210 static inline void fwnode_dev_initialized(struct fwnode_handle *fwnode,
211 					  bool initialized)
212 {
213 	if (IS_ERR_OR_NULL(fwnode))
214 		return;
215 
216 	if (initialized)
217 		fwnode->flags |= FWNODE_FLAG_INITIALIZED;
218 	else
219 		fwnode->flags &= ~FWNODE_FLAG_INITIALIZED;
220 }
221 
222 int fwnode_link_add(struct fwnode_handle *con, struct fwnode_handle *sup,
223 		    u8 flags);
224 void fwnode_links_purge(struct fwnode_handle *fwnode);
225 void fw_devlink_purge_absent_suppliers(struct fwnode_handle *fwnode);
226 bool fw_devlink_is_strict(void);
227 
228 #endif
229