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