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