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