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