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