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