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