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