xref: /openbmc/linux/include/linux/fwnode.h (revision c494a447)
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 (*device_dma_supported)(const struct fwnode_handle *fwnode);
117 	enum dev_dma_attr
118 	(*device_get_dma_attr)(const struct fwnode_handle *fwnode);
119 	bool (*property_present)(const struct fwnode_handle *fwnode,
120 				 const char *propname);
121 	int (*property_read_int_array)(const struct fwnode_handle *fwnode,
122 				       const char *propname,
123 				       unsigned int elem_size, void *val,
124 				       size_t nval);
125 	int
126 	(*property_read_string_array)(const struct fwnode_handle *fwnode_handle,
127 				      const char *propname, const char **val,
128 				      size_t nval);
129 	const char *(*get_name)(const struct fwnode_handle *fwnode);
130 	const char *(*get_name_prefix)(const struct fwnode_handle *fwnode);
131 	struct fwnode_handle *(*get_parent)(const struct fwnode_handle *fwnode);
132 	struct fwnode_handle *
133 	(*get_next_child_node)(const struct fwnode_handle *fwnode,
134 			       struct fwnode_handle *child);
135 	struct fwnode_handle *
136 	(*get_named_child_node)(const struct fwnode_handle *fwnode,
137 				const char *name);
138 	int (*get_reference_args)(const struct fwnode_handle *fwnode,
139 				  const char *prop, const char *nargs_prop,
140 				  unsigned int nargs, unsigned int index,
141 				  struct fwnode_reference_args *args);
142 	struct fwnode_handle *
143 	(*graph_get_next_endpoint)(const struct fwnode_handle *fwnode,
144 				   struct fwnode_handle *prev);
145 	struct fwnode_handle *
146 	(*graph_get_remote_endpoint)(const struct fwnode_handle *fwnode);
147 	struct fwnode_handle *
148 	(*graph_get_port_parent)(struct fwnode_handle *fwnode);
149 	int (*graph_parse_endpoint)(const struct fwnode_handle *fwnode,
150 				    struct fwnode_endpoint *endpoint);
151 	void __iomem *(*iomap)(struct fwnode_handle *fwnode, int index);
152 	int (*irq_get)(const struct fwnode_handle *fwnode, unsigned int index);
153 	int (*add_links)(struct fwnode_handle *fwnode);
154 };
155 
156 #define fwnode_has_op(fwnode, op)					\
157 	(!IS_ERR_OR_NULL(fwnode) && (fwnode)->ops && (fwnode)->ops->op)
158 
159 #define fwnode_call_int_op(fwnode, op, ...)				\
160 	(fwnode_has_op(fwnode, op) ?					\
161 	 (fwnode)->ops->op(fwnode, ## __VA_ARGS__) : (IS_ERR_OR_NULL(fwnode) ? -EINVAL : -ENXIO))
162 
163 #define fwnode_call_bool_op(fwnode, op, ...)		\
164 	(fwnode_has_op(fwnode, op) ?			\
165 	 (fwnode)->ops->op(fwnode, ## __VA_ARGS__) : false)
166 
167 #define fwnode_call_ptr_op(fwnode, op, ...)		\
168 	(fwnode_has_op(fwnode, op) ?			\
169 	 (fwnode)->ops->op(fwnode, ## __VA_ARGS__) : NULL)
170 #define fwnode_call_void_op(fwnode, op, ...)				\
171 	do {								\
172 		if (fwnode_has_op(fwnode, op))				\
173 			(fwnode)->ops->op(fwnode, ## __VA_ARGS__);	\
174 	} while (false)
175 #define get_dev_from_fwnode(fwnode)	get_device((fwnode)->dev)
176 
177 static inline void fwnode_init(struct fwnode_handle *fwnode,
178 			       const struct fwnode_operations *ops)
179 {
180 	fwnode->ops = ops;
181 	INIT_LIST_HEAD(&fwnode->consumers);
182 	INIT_LIST_HEAD(&fwnode->suppliers);
183 }
184 
185 static inline void fwnode_dev_initialized(struct fwnode_handle *fwnode,
186 					  bool initialized)
187 {
188 	if (IS_ERR_OR_NULL(fwnode))
189 		return;
190 
191 	if (initialized)
192 		fwnode->flags |= FWNODE_FLAG_INITIALIZED;
193 	else
194 		fwnode->flags &= ~FWNODE_FLAG_INITIALIZED;
195 }
196 
197 extern u32 fw_devlink_get_flags(void);
198 extern bool fw_devlink_is_strict(void);
199 int fwnode_link_add(struct fwnode_handle *con, struct fwnode_handle *sup);
200 void fwnode_links_purge(struct fwnode_handle *fwnode);
201 void fw_devlink_purge_absent_suppliers(struct fwnode_handle *fwnode);
202 
203 #endif
204