Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
b31384fa RW |
2 | /* |
3 | * property.h - Unified device property interface. | |
4 | * | |
5 | * Copyright (C) 2014, Intel Corporation | |
6 | * Authors: Rafael J. Wysocki <rafael.j.wysocki@intel.com> | |
7 | * Mika Westerberg <mika.westerberg@linux.intel.com> | |
b31384fa RW |
8 | */ |
9 | ||
10 | #ifndef _LINUX_PROPERTY_H_ | |
11 | #define _LINUX_PROPERTY_H_ | |
12 | ||
0fcc2bdc | 13 | #include <linux/bits.h> |
ce793486 | 14 | #include <linux/fwnode.h> |
b31384fa RW |
15 | #include <linux/types.h> |
16 | ||
17 | struct device; | |
18 | ||
19 | enum dev_prop_type { | |
20 | DEV_PROP_U8, | |
21 | DEV_PROP_U16, | |
22 | DEV_PROP_U32, | |
23 | DEV_PROP_U64, | |
24 | DEV_PROP_STRING, | |
25 | DEV_PROP_MAX, | |
26 | }; | |
27 | ||
1b9863c6 SS |
28 | enum dev_dma_attr { |
29 | DEV_DMA_NOT_SUPPORTED, | |
30 | DEV_DMA_NON_COHERENT, | |
31 | DEV_DMA_COHERENT, | |
32 | }; | |
33 | ||
e44bb0cb SA |
34 | struct fwnode_handle *dev_fwnode(struct device *dev); |
35 | ||
b31384fa RW |
36 | bool device_property_present(struct device *dev, const char *propname); |
37 | int device_property_read_u8_array(struct device *dev, const char *propname, | |
38 | u8 *val, size_t nval); | |
39 | int device_property_read_u16_array(struct device *dev, const char *propname, | |
40 | u16 *val, size_t nval); | |
41 | int device_property_read_u32_array(struct device *dev, const char *propname, | |
42 | u32 *val, size_t nval); | |
43 | int device_property_read_u64_array(struct device *dev, const char *propname, | |
44 | u64 *val, size_t nval); | |
45 | int device_property_read_string_array(struct device *dev, const char *propname, | |
46 | const char **val, size_t nval); | |
47 | int device_property_read_string(struct device *dev, const char *propname, | |
48 | const char **val); | |
3f5c8d31 MW |
49 | int device_property_match_string(struct device *dev, |
50 | const char *propname, const char *string); | |
b31384fa | 51 | |
37ba983c SA |
52 | bool fwnode_device_is_available(const struct fwnode_handle *fwnode); |
53 | bool fwnode_property_present(const struct fwnode_handle *fwnode, | |
54 | const char *propname); | |
55 | int fwnode_property_read_u8_array(const struct fwnode_handle *fwnode, | |
8a0662d9 RW |
56 | const char *propname, u8 *val, |
57 | size_t nval); | |
37ba983c | 58 | int fwnode_property_read_u16_array(const struct fwnode_handle *fwnode, |
8a0662d9 RW |
59 | const char *propname, u16 *val, |
60 | size_t nval); | |
37ba983c | 61 | int fwnode_property_read_u32_array(const struct fwnode_handle *fwnode, |
8a0662d9 RW |
62 | const char *propname, u32 *val, |
63 | size_t nval); | |
37ba983c | 64 | int fwnode_property_read_u64_array(const struct fwnode_handle *fwnode, |
8a0662d9 RW |
65 | const char *propname, u64 *val, |
66 | size_t nval); | |
37ba983c | 67 | int fwnode_property_read_string_array(const struct fwnode_handle *fwnode, |
8a0662d9 RW |
68 | const char *propname, const char **val, |
69 | size_t nval); | |
37ba983c | 70 | int fwnode_property_read_string(const struct fwnode_handle *fwnode, |
8a0662d9 | 71 | const char *propname, const char **val); |
37ba983c | 72 | int fwnode_property_match_string(const struct fwnode_handle *fwnode, |
3f5c8d31 | 73 | const char *propname, const char *string); |
3e3119d3 SA |
74 | int fwnode_property_get_reference_args(const struct fwnode_handle *fwnode, |
75 | const char *prop, const char *nargs_prop, | |
76 | unsigned int nargs, unsigned int index, | |
77 | struct fwnode_reference_args *args); | |
8a0662d9 | 78 | |
83b34afb HK |
79 | struct fwnode_handle *fwnode_find_reference(const struct fwnode_handle *fwnode, |
80 | const char *name, | |
81 | unsigned int index); | |
82 | ||
37ba983c SA |
83 | struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode); |
84 | struct fwnode_handle *fwnode_get_next_parent( | |
85 | struct fwnode_handle *fwnode); | |
86 | struct fwnode_handle *fwnode_get_next_child_node( | |
87 | const struct fwnode_handle *fwnode, struct fwnode_handle *child); | |
3395de96 MW |
88 | struct fwnode_handle *fwnode_get_next_available_child_node( |
89 | const struct fwnode_handle *fwnode, struct fwnode_handle *child); | |
34055190 MW |
90 | |
91 | #define fwnode_for_each_child_node(fwnode, child) \ | |
92 | for (child = fwnode_get_next_child_node(fwnode, NULL); child; \ | |
93 | child = fwnode_get_next_child_node(fwnode, child)) | |
afaf26fd | 94 | |
3395de96 MW |
95 | #define fwnode_for_each_available_child_node(fwnode, child) \ |
96 | for (child = fwnode_get_next_available_child_node(fwnode, NULL); child;\ | |
97 | child = fwnode_get_next_available_child_node(fwnode, child)) | |
98 | ||
37ba983c SA |
99 | struct fwnode_handle *device_get_next_child_node( |
100 | struct device *dev, struct fwnode_handle *child); | |
8a0662d9 | 101 | |
1d656fb7 AS |
102 | #define device_for_each_child_node(dev, child) \ |
103 | for (child = device_get_next_child_node(dev, NULL); child; \ | |
8a0662d9 RW |
104 | child = device_get_next_child_node(dev, child)) |
105 | ||
37ba983c SA |
106 | struct fwnode_handle *fwnode_get_named_child_node( |
107 | const struct fwnode_handle *fwnode, const char *childname); | |
613e9721 AT |
108 | struct fwnode_handle *device_get_named_child_node(struct device *dev, |
109 | const char *childname); | |
110 | ||
cf89a31c | 111 | struct fwnode_handle *fwnode_handle_get(struct fwnode_handle *fwnode); |
8a0662d9 RW |
112 | void fwnode_handle_put(struct fwnode_handle *fwnode); |
113 | ||
7c6c57f2 MW |
114 | int fwnode_irq_get(struct fwnode_handle *fwnode, unsigned int index); |
115 | ||
8a0662d9 RW |
116 | unsigned int device_get_child_node_count(struct device *dev); |
117 | ||
b31384fa RW |
118 | static inline bool device_property_read_bool(struct device *dev, |
119 | const char *propname) | |
120 | { | |
121 | return device_property_present(dev, propname); | |
122 | } | |
123 | ||
124 | static inline int device_property_read_u8(struct device *dev, | |
125 | const char *propname, u8 *val) | |
126 | { | |
127 | return device_property_read_u8_array(dev, propname, val, 1); | |
128 | } | |
129 | ||
130 | static inline int device_property_read_u16(struct device *dev, | |
131 | const char *propname, u16 *val) | |
132 | { | |
133 | return device_property_read_u16_array(dev, propname, val, 1); | |
134 | } | |
135 | ||
136 | static inline int device_property_read_u32(struct device *dev, | |
137 | const char *propname, u32 *val) | |
138 | { | |
139 | return device_property_read_u32_array(dev, propname, val, 1); | |
140 | } | |
141 | ||
142 | static inline int device_property_read_u64(struct device *dev, | |
143 | const char *propname, u64 *val) | |
144 | { | |
145 | return device_property_read_u64_array(dev, propname, val, 1); | |
146 | } | |
147 | ||
33ee09cd AS |
148 | static inline int device_property_count_u8(struct device *dev, const char *propname) |
149 | { | |
150 | return device_property_read_u8_array(dev, propname, NULL, 0); | |
151 | } | |
152 | ||
153 | static inline int device_property_count_u16(struct device *dev, const char *propname) | |
154 | { | |
155 | return device_property_read_u16_array(dev, propname, NULL, 0); | |
156 | } | |
157 | ||
158 | static inline int device_property_count_u32(struct device *dev, const char *propname) | |
159 | { | |
160 | return device_property_read_u32_array(dev, propname, NULL, 0); | |
161 | } | |
162 | ||
163 | static inline int device_property_count_u64(struct device *dev, const char *propname) | |
164 | { | |
165 | return device_property_read_u64_array(dev, propname, NULL, 0); | |
166 | } | |
167 | ||
37ba983c | 168 | static inline bool fwnode_property_read_bool(const struct fwnode_handle *fwnode, |
8a0662d9 RW |
169 | const char *propname) |
170 | { | |
171 | return fwnode_property_present(fwnode, propname); | |
172 | } | |
173 | ||
37ba983c | 174 | static inline int fwnode_property_read_u8(const struct fwnode_handle *fwnode, |
8a0662d9 RW |
175 | const char *propname, u8 *val) |
176 | { | |
177 | return fwnode_property_read_u8_array(fwnode, propname, val, 1); | |
178 | } | |
179 | ||
37ba983c | 180 | static inline int fwnode_property_read_u16(const struct fwnode_handle *fwnode, |
8a0662d9 RW |
181 | const char *propname, u16 *val) |
182 | { | |
183 | return fwnode_property_read_u16_array(fwnode, propname, val, 1); | |
184 | } | |
185 | ||
37ba983c | 186 | static inline int fwnode_property_read_u32(const struct fwnode_handle *fwnode, |
8a0662d9 RW |
187 | const char *propname, u32 *val) |
188 | { | |
189 | return fwnode_property_read_u32_array(fwnode, propname, val, 1); | |
190 | } | |
191 | ||
37ba983c | 192 | static inline int fwnode_property_read_u64(const struct fwnode_handle *fwnode, |
8a0662d9 RW |
193 | const char *propname, u64 *val) |
194 | { | |
195 | return fwnode_property_read_u64_array(fwnode, propname, val, 1); | |
196 | } | |
197 | ||
33ee09cd AS |
198 | static inline int fwnode_property_count_u8(const struct fwnode_handle *fwnode, |
199 | const char *propname) | |
200 | { | |
201 | return fwnode_property_read_u8_array(fwnode, propname, NULL, 0); | |
202 | } | |
203 | ||
204 | static inline int fwnode_property_count_u16(const struct fwnode_handle *fwnode, | |
205 | const char *propname) | |
206 | { | |
207 | return fwnode_property_read_u16_array(fwnode, propname, NULL, 0); | |
208 | } | |
209 | ||
210 | static inline int fwnode_property_count_u32(const struct fwnode_handle *fwnode, | |
211 | const char *propname) | |
212 | { | |
213 | return fwnode_property_read_u32_array(fwnode, propname, NULL, 0); | |
214 | } | |
215 | ||
216 | static inline int fwnode_property_count_u64(const struct fwnode_handle *fwnode, | |
217 | const char *propname) | |
218 | { | |
219 | return fwnode_property_read_u64_array(fwnode, propname, NULL, 0); | |
220 | } | |
221 | ||
16ba08d5 RW |
222 | /** |
223 | * struct property_entry - "Built-in" device property representation. | |
224 | * @name: Name of the property. | |
318a1971 | 225 | * @length: Length of data making up the value. |
66586bab | 226 | * @is_array: True when the property is an array. |
63dcc709 | 227 | * @type: Type of the data in unions. |
66586bab AS |
228 | * @pointer: Pointer to the property (an array of items of the given type). |
229 | * @value: Value of the property (when it is a single item of the given type). | |
16ba08d5 RW |
230 | */ |
231 | struct property_entry { | |
232 | const char *name; | |
318a1971 | 233 | size_t length; |
66586bab | 234 | bool is_array; |
63dcc709 | 235 | enum dev_prop_type type; |
16ba08d5 | 236 | union { |
66586bab | 237 | union { |
9426998c DT |
238 | const u8 *u8_data; |
239 | const u16 *u16_data; | |
240 | const u32 *u32_data; | |
241 | const u64 *u64_data; | |
242 | const char * const *str; | |
66586bab AS |
243 | } pointer; |
244 | union { | |
66586bab AS |
245 | u8 u8_data; |
246 | u16 u16_data; | |
247 | u32 u32_data; | |
248 | u64 u64_data; | |
249 | const char *str; | |
250 | } value; | |
251 | }; | |
16ba08d5 RW |
252 | }; |
253 | ||
d76eebfa AM |
254 | /* |
255 | * Note: the below four initializers for the anonymous union are carefully | |
256 | * crafted to avoid gcc-4.4.4's problems with initialization of anon unions | |
257 | * and structs. | |
258 | */ | |
259 | ||
63dcc709 AS |
260 | #define PROPERTY_ENTRY_INTEGER_ARRAY(_name_, _type_, _Type_, _val_) \ |
261 | (struct property_entry) { \ | |
262 | .name = _name_, \ | |
263 | .length = ARRAY_SIZE(_val_) * sizeof(_type_), \ | |
264 | .is_array = true, \ | |
265 | .type = DEV_PROP_##_Type_, \ | |
266 | { .pointer = { ._type_##_data = _val_ } }, \ | |
a85f4204 HK |
267 | } |
268 | ||
269 | #define PROPERTY_ENTRY_U8_ARRAY(_name_, _val_) \ | |
63dcc709 | 270 | PROPERTY_ENTRY_INTEGER_ARRAY(_name_, u8, U8, _val_) |
a85f4204 | 271 | #define PROPERTY_ENTRY_U16_ARRAY(_name_, _val_) \ |
63dcc709 | 272 | PROPERTY_ENTRY_INTEGER_ARRAY(_name_, u16, U16, _val_) |
a85f4204 | 273 | #define PROPERTY_ENTRY_U32_ARRAY(_name_, _val_) \ |
63dcc709 | 274 | PROPERTY_ENTRY_INTEGER_ARRAY(_name_, u32, U32, _val_) |
a85f4204 | 275 | #define PROPERTY_ENTRY_U64_ARRAY(_name_, _val_) \ |
63dcc709 | 276 | PROPERTY_ENTRY_INTEGER_ARRAY(_name_, u64, U64, _val_) |
a85f4204 HK |
277 | |
278 | #define PROPERTY_ENTRY_STRING_ARRAY(_name_, _val_) \ | |
c505cbd4 | 279 | (struct property_entry) { \ |
a85f4204 HK |
280 | .name = _name_, \ |
281 | .length = ARRAY_SIZE(_val_) * sizeof(const char *), \ | |
282 | .is_array = true, \ | |
63dcc709 | 283 | .type = DEV_PROP_STRING, \ |
d76eebfa | 284 | { .pointer = { .str = _val_ } }, \ |
a85f4204 HK |
285 | } |
286 | ||
63dcc709 AS |
287 | #define PROPERTY_ENTRY_INTEGER(_name_, _type_, _Type_, _val_) \ |
288 | (struct property_entry) { \ | |
289 | .name = _name_, \ | |
290 | .length = sizeof(_type_), \ | |
291 | .type = DEV_PROP_##_Type_, \ | |
292 | { .value = { ._type_##_data = _val_ } }, \ | |
a85f4204 HK |
293 | } |
294 | ||
295 | #define PROPERTY_ENTRY_U8(_name_, _val_) \ | |
63dcc709 | 296 | PROPERTY_ENTRY_INTEGER(_name_, u8, U8, _val_) |
a85f4204 | 297 | #define PROPERTY_ENTRY_U16(_name_, _val_) \ |
63dcc709 | 298 | PROPERTY_ENTRY_INTEGER(_name_, u16, U16, _val_) |
a85f4204 | 299 | #define PROPERTY_ENTRY_U32(_name_, _val_) \ |
63dcc709 | 300 | PROPERTY_ENTRY_INTEGER(_name_, u32, U32, _val_) |
a85f4204 | 301 | #define PROPERTY_ENTRY_U64(_name_, _val_) \ |
63dcc709 | 302 | PROPERTY_ENTRY_INTEGER(_name_, u64, U64, _val_) |
a85f4204 HK |
303 | |
304 | #define PROPERTY_ENTRY_STRING(_name_, _val_) \ | |
c505cbd4 | 305 | (struct property_entry) { \ |
a85f4204 | 306 | .name = _name_, \ |
2b6e4924 | 307 | .length = sizeof(const char *), \ |
63dcc709 | 308 | .type = DEV_PROP_STRING, \ |
aace7367 | 309 | { .value = { .str = _val_ } }, \ |
a85f4204 HK |
310 | } |
311 | ||
312 | #define PROPERTY_ENTRY_BOOL(_name_) \ | |
c505cbd4 | 313 | (struct property_entry) { \ |
a85f4204 HK |
314 | .name = _name_, \ |
315 | } | |
316 | ||
2d479e1f DT |
317 | struct property_entry * |
318 | property_entries_dup(const struct property_entry *properties); | |
319 | ||
320 | void property_entries_free(const struct property_entry *properties); | |
321 | ||
f4d05266 | 322 | int device_add_properties(struct device *dev, |
bec84da8 | 323 | const struct property_entry *properties); |
f4d05266 | 324 | void device_remove_properties(struct device *dev); |
16ba08d5 | 325 | |
e5e55864 SS |
326 | bool device_dma_supported(struct device *dev); |
327 | ||
328 | enum dev_dma_attr device_get_dma_attr(struct device *dev); | |
b283f157 | 329 | |
67dcc26d | 330 | const void *device_get_match_data(struct device *dev); |
e5e55864 | 331 | |
4c96b7dc JL |
332 | int device_get_phy_mode(struct device *dev); |
333 | ||
334 | void *device_get_mac_address(struct device *dev, char *addr, int alen); | |
335 | ||
b28f263b | 336 | int fwnode_get_phy_mode(struct fwnode_handle *fwnode); |
babe2dbb MW |
337 | void *fwnode_get_mac_address(struct fwnode_handle *fwnode, |
338 | char *addr, int alen); | |
07bb80d4 | 339 | struct fwnode_handle *fwnode_graph_get_next_endpoint( |
37ba983c | 340 | const struct fwnode_handle *fwnode, struct fwnode_handle *prev); |
6a71d8d7 | 341 | struct fwnode_handle * |
37ba983c | 342 | fwnode_graph_get_port_parent(const struct fwnode_handle *fwnode); |
07bb80d4 | 343 | struct fwnode_handle *fwnode_graph_get_remote_port_parent( |
37ba983c | 344 | const struct fwnode_handle *fwnode); |
07bb80d4 | 345 | struct fwnode_handle *fwnode_graph_get_remote_port( |
37ba983c | 346 | const struct fwnode_handle *fwnode); |
07bb80d4 | 347 | struct fwnode_handle *fwnode_graph_get_remote_endpoint( |
37ba983c SA |
348 | const struct fwnode_handle *fwnode); |
349 | struct fwnode_handle * | |
350 | fwnode_graph_get_remote_node(const struct fwnode_handle *fwnode, u32 port, | |
351 | u32 endpoint); | |
07bb80d4 | 352 | |
0fcc2bdc SA |
353 | /* |
354 | * Fwnode lookup flags | |
355 | * | |
356 | * @FWNODE_GRAPH_ENDPOINT_NEXT: In the case of no exact match, look for the | |
357 | * closest endpoint ID greater than the specified | |
358 | * one. | |
359 | * @FWNODE_GRAPH_DEVICE_DISABLED: That the device to which the remote | |
360 | * endpoint of the given endpoint belongs to, | |
361 | * may be disabled. | |
362 | */ | |
363 | #define FWNODE_GRAPH_ENDPOINT_NEXT BIT(0) | |
364 | #define FWNODE_GRAPH_DEVICE_DISABLED BIT(1) | |
365 | ||
366 | struct fwnode_handle * | |
367 | fwnode_graph_get_endpoint_by_id(const struct fwnode_handle *fwnode, | |
368 | u32 port, u32 endpoint, unsigned long flags); | |
369 | ||
5e4b1b70 SA |
370 | #define fwnode_graph_for_each_endpoint(fwnode, child) \ |
371 | for (child = NULL; \ | |
372 | (child = fwnode_graph_get_next_endpoint(fwnode, child)); ) | |
373 | ||
37ba983c | 374 | int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode, |
2bd5452d SA |
375 | struct fwnode_endpoint *endpoint); |
376 | ||
59abd836 HK |
377 | /* -------------------------------------------------------------------------- */ |
378 | /* Software fwnode support - when HW description is incomplete or missing */ | |
379 | ||
b06184ac HK |
380 | struct software_node; |
381 | ||
382 | /** | |
383 | * struct software_node_ref_args - Reference with additional arguments | |
384 | * @node: Reference to a software node | |
385 | * @nargs: Number of elements in @args array | |
386 | * @args: Integer arguments | |
387 | */ | |
388 | struct software_node_ref_args { | |
389 | const struct software_node *node; | |
390 | unsigned int nargs; | |
391 | u64 args[NR_FWNODE_REFERENCE_ARGS]; | |
392 | }; | |
393 | ||
394 | /** | |
395 | * struct software_node_reference - Named software node reference property | |
396 | * @name: Name of the property | |
397 | * @nrefs: Number of elements in @refs array | |
398 | * @refs: Array of references with optional arguments | |
399 | */ | |
400 | struct software_node_reference { | |
401 | const char *name; | |
402 | unsigned int nrefs; | |
403 | const struct software_node_ref_args *refs; | |
404 | }; | |
405 | ||
80488a6b HK |
406 | /** |
407 | * struct software_node - Software node description | |
408 | * @name: Name of the software node | |
409 | * @parent: Parent of the software node | |
410 | * @properties: Array of device properties | |
b06184ac | 411 | * @references: Array of software node reference properties |
80488a6b HK |
412 | */ |
413 | struct software_node { | |
414 | const char *name; | |
415 | const struct software_node *parent; | |
416 | const struct property_entry *properties; | |
b06184ac | 417 | const struct software_node_reference *references; |
80488a6b HK |
418 | }; |
419 | ||
59abd836 | 420 | bool is_software_node(const struct fwnode_handle *fwnode); |
80488a6b HK |
421 | const struct software_node *to_software_node(struct fwnode_handle *fwnode); |
422 | struct fwnode_handle *software_node_fwnode(const struct software_node *node); | |
423 | ||
1666faed HK |
424 | const struct software_node * |
425 | software_node_find_by_name(const struct software_node *parent, | |
426 | const char *name); | |
427 | ||
80488a6b HK |
428 | int software_node_register_nodes(const struct software_node *nodes); |
429 | void software_node_unregister_nodes(const struct software_node *nodes); | |
430 | ||
431 | int software_node_register(const struct software_node *node); | |
59abd836 HK |
432 | |
433 | int software_node_notify(struct device *dev, unsigned long action); | |
434 | ||
435 | struct fwnode_handle * | |
436 | fwnode_create_software_node(const struct property_entry *properties, | |
437 | const struct fwnode_handle *parent); | |
438 | void fwnode_remove_software_node(struct fwnode_handle *fwnode); | |
439 | ||
b31384fa | 440 | #endif /* _LINUX_PROPERTY_H_ */ |