Merge tag 'platform-drivers-x86-v5.18-1' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-block.git] / include / linux / property.h
CommitLineData
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
17struct device;
d9eb4490 18struct net_device;
b31384fa
RW
19
20enum dev_prop_type {
21 DEV_PROP_U8,
22 DEV_PROP_U16,
23 DEV_PROP_U32,
24 DEV_PROP_U64,
25 DEV_PROP_STRING,
e64b674b 26 DEV_PROP_REF,
b31384fa
RW
27};
28
1b9863c6
SS
29enum dev_dma_attr {
30 DEV_DMA_NOT_SUPPORTED,
31 DEV_DMA_NON_COHERENT,
32 DEV_DMA_COHERENT,
33};
34
e44bb0cb
SA
35struct fwnode_handle *dev_fwnode(struct device *dev);
36
b31384fa
RW
37bool device_property_present(struct device *dev, const char *propname);
38int device_property_read_u8_array(struct device *dev, const char *propname,
39 u8 *val, size_t nval);
40int device_property_read_u16_array(struct device *dev, const char *propname,
41 u16 *val, size_t nval);
42int device_property_read_u32_array(struct device *dev, const char *propname,
43 u32 *val, size_t nval);
44int device_property_read_u64_array(struct device *dev, const char *propname,
45 u64 *val, size_t nval);
46int device_property_read_string_array(struct device *dev, const char *propname,
47 const char **val, size_t nval);
48int device_property_read_string(struct device *dev, const char *propname,
49 const char **val);
3f5c8d31
MW
50int device_property_match_string(struct device *dev,
51 const char *propname, const char *string);
b31384fa 52
37ba983c
SA
53bool fwnode_device_is_available(const struct fwnode_handle *fwnode);
54bool fwnode_property_present(const struct fwnode_handle *fwnode,
55 const char *propname);
56int fwnode_property_read_u8_array(const struct fwnode_handle *fwnode,
8a0662d9
RW
57 const char *propname, u8 *val,
58 size_t nval);
37ba983c 59int fwnode_property_read_u16_array(const struct fwnode_handle *fwnode,
8a0662d9
RW
60 const char *propname, u16 *val,
61 size_t nval);
37ba983c 62int fwnode_property_read_u32_array(const struct fwnode_handle *fwnode,
8a0662d9
RW
63 const char *propname, u32 *val,
64 size_t nval);
37ba983c 65int fwnode_property_read_u64_array(const struct fwnode_handle *fwnode,
8a0662d9
RW
66 const char *propname, u64 *val,
67 size_t nval);
37ba983c 68int fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
8a0662d9
RW
69 const char *propname, const char **val,
70 size_t nval);
37ba983c 71int fwnode_property_read_string(const struct fwnode_handle *fwnode,
8a0662d9 72 const char *propname, const char **val);
37ba983c 73int fwnode_property_match_string(const struct fwnode_handle *fwnode,
3f5c8d31 74 const char *propname, const char *string);
3e3119d3
SA
75int fwnode_property_get_reference_args(const struct fwnode_handle *fwnode,
76 const char *prop, const char *nargs_prop,
77 unsigned int nargs, unsigned int index,
78 struct fwnode_reference_args *args);
8a0662d9 79
83b34afb
HK
80struct fwnode_handle *fwnode_find_reference(const struct fwnode_handle *fwnode,
81 const char *name,
82 unsigned int index);
83
bc0500c1 84const char *fwnode_get_name(const struct fwnode_handle *fwnode);
e7e242bc 85const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode);
37ba983c
SA
86struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode);
87struct fwnode_handle *fwnode_get_next_parent(
88 struct fwnode_handle *fwnode);
b5d3e2fb 89struct device *fwnode_get_next_parent_dev(struct fwnode_handle *fwnode);
87e5e95d
SA
90unsigned int fwnode_count_parents(const struct fwnode_handle *fwn);
91struct fwnode_handle *fwnode_get_nth_parent(struct fwnode_handle *fwn,
92 unsigned int depth);
b5d3e2fb
SK
93bool fwnode_is_ancestor_of(struct fwnode_handle *test_ancestor,
94 struct fwnode_handle *test_child);
37ba983c
SA
95struct fwnode_handle *fwnode_get_next_child_node(
96 const struct fwnode_handle *fwnode, struct fwnode_handle *child);
3395de96
MW
97struct fwnode_handle *fwnode_get_next_available_child_node(
98 const struct fwnode_handle *fwnode, struct fwnode_handle *child);
34055190
MW
99
100#define fwnode_for_each_child_node(fwnode, child) \
101 for (child = fwnode_get_next_child_node(fwnode, NULL); child; \
102 child = fwnode_get_next_child_node(fwnode, child))
afaf26fd 103
3395de96
MW
104#define fwnode_for_each_available_child_node(fwnode, child) \
105 for (child = fwnode_get_next_available_child_node(fwnode, NULL); child;\
106 child = fwnode_get_next_available_child_node(fwnode, child))
107
37ba983c
SA
108struct fwnode_handle *device_get_next_child_node(
109 struct device *dev, struct fwnode_handle *child);
8a0662d9 110
1d656fb7
AS
111#define device_for_each_child_node(dev, child) \
112 for (child = device_get_next_child_node(dev, NULL); child; \
8a0662d9
RW
113 child = device_get_next_child_node(dev, child))
114
37ba983c
SA
115struct fwnode_handle *fwnode_get_named_child_node(
116 const struct fwnode_handle *fwnode, const char *childname);
613e9721
AT
117struct fwnode_handle *device_get_named_child_node(struct device *dev,
118 const char *childname);
119
cf89a31c 120struct fwnode_handle *fwnode_handle_get(struct fwnode_handle *fwnode);
8a0662d9
RW
121void fwnode_handle_put(struct fwnode_handle *fwnode);
122
fb38f314 123int fwnode_irq_get(const struct fwnode_handle *fwnode, unsigned int index);
7c6c57f2 124
eca6e2d4
AAD
125void __iomem *fwnode_iomap(struct fwnode_handle *fwnode, int index);
126
8a0662d9
RW
127unsigned int device_get_child_node_count(struct device *dev);
128
b31384fa
RW
129static inline bool device_property_read_bool(struct device *dev,
130 const char *propname)
131{
132 return device_property_present(dev, propname);
133}
134
135static inline int device_property_read_u8(struct device *dev,
136 const char *propname, u8 *val)
137{
138 return device_property_read_u8_array(dev, propname, val, 1);
139}
140
141static inline int device_property_read_u16(struct device *dev,
142 const char *propname, u16 *val)
143{
144 return device_property_read_u16_array(dev, propname, val, 1);
145}
146
147static inline int device_property_read_u32(struct device *dev,
148 const char *propname, u32 *val)
149{
150 return device_property_read_u32_array(dev, propname, val, 1);
151}
152
153static inline int device_property_read_u64(struct device *dev,
154 const char *propname, u64 *val)
155{
156 return device_property_read_u64_array(dev, propname, val, 1);
157}
158
33ee09cd
AS
159static inline int device_property_count_u8(struct device *dev, const char *propname)
160{
161 return device_property_read_u8_array(dev, propname, NULL, 0);
162}
163
164static inline int device_property_count_u16(struct device *dev, const char *propname)
165{
166 return device_property_read_u16_array(dev, propname, NULL, 0);
167}
168
169static inline int device_property_count_u32(struct device *dev, const char *propname)
170{
171 return device_property_read_u32_array(dev, propname, NULL, 0);
172}
173
174static inline int device_property_count_u64(struct device *dev, const char *propname)
175{
176 return device_property_read_u64_array(dev, propname, NULL, 0);
177}
178
6b6ff4ac
BG
179static inline int device_property_string_array_count(struct device *dev,
180 const char *propname)
181{
182 return device_property_read_string_array(dev, propname, NULL, 0);
183}
184
37ba983c 185static inline bool fwnode_property_read_bool(const struct fwnode_handle *fwnode,
8a0662d9
RW
186 const char *propname)
187{
188 return fwnode_property_present(fwnode, propname);
189}
190
37ba983c 191static inline int fwnode_property_read_u8(const struct fwnode_handle *fwnode,
8a0662d9
RW
192 const char *propname, u8 *val)
193{
194 return fwnode_property_read_u8_array(fwnode, propname, val, 1);
195}
196
37ba983c 197static inline int fwnode_property_read_u16(const struct fwnode_handle *fwnode,
8a0662d9
RW
198 const char *propname, u16 *val)
199{
200 return fwnode_property_read_u16_array(fwnode, propname, val, 1);
201}
202
37ba983c 203static inline int fwnode_property_read_u32(const struct fwnode_handle *fwnode,
8a0662d9
RW
204 const char *propname, u32 *val)
205{
206 return fwnode_property_read_u32_array(fwnode, propname, val, 1);
207}
208
37ba983c 209static inline int fwnode_property_read_u64(const struct fwnode_handle *fwnode,
8a0662d9
RW
210 const char *propname, u64 *val)
211{
212 return fwnode_property_read_u64_array(fwnode, propname, val, 1);
213}
214
33ee09cd
AS
215static inline int fwnode_property_count_u8(const struct fwnode_handle *fwnode,
216 const char *propname)
217{
218 return fwnode_property_read_u8_array(fwnode, propname, NULL, 0);
219}
220
221static inline int fwnode_property_count_u16(const struct fwnode_handle *fwnode,
222 const char *propname)
223{
224 return fwnode_property_read_u16_array(fwnode, propname, NULL, 0);
225}
226
227static inline int fwnode_property_count_u32(const struct fwnode_handle *fwnode,
228 const char *propname)
229{
230 return fwnode_property_read_u32_array(fwnode, propname, NULL, 0);
231}
232
233static inline int fwnode_property_count_u64(const struct fwnode_handle *fwnode,
234 const char *propname)
235{
236 return fwnode_property_read_u64_array(fwnode, propname, NULL, 0);
237}
238
6b6ff4ac
BG
239static inline int
240fwnode_property_string_array_count(const struct fwnode_handle *fwnode,
241 const char *propname)
242{
243 return fwnode_property_read_string_array(fwnode, propname, NULL, 0);
244}
245
e64b674b
DT
246struct software_node;
247
248/**
249 * struct software_node_ref_args - Reference property with additional arguments
250 * @node: Reference to a software node
251 * @nargs: Number of elements in @args array
252 * @args: Integer arguments
253 */
254struct software_node_ref_args {
255 const struct software_node *node;
256 unsigned int nargs;
257 u64 args[NR_FWNODE_REFERENCE_ARGS];
258};
259
e588fead
AS
260#define SOFTWARE_NODE_REFERENCE(_ref_, ...) \
261(const struct software_node_ref_args) { \
262 .node = _ref_, \
263 .nargs = ARRAY_SIZE(((u64[]){ 0, ##__VA_ARGS__ })) - 1, \
264 .args = { __VA_ARGS__ }, \
265}
266
16ba08d5
RW
267/**
268 * struct property_entry - "Built-in" device property representation.
269 * @name: Name of the property.
318a1971 270 * @length: Length of data making up the value.
e6bff466 271 * @is_inline: True when the property value is stored inline.
63dcc709 272 * @type: Type of the data in unions.
e6bff466
DT
273 * @pointer: Pointer to the property when it is not stored inline.
274 * @value: Value of the property when it is stored inline.
16ba08d5
RW
275 */
276struct property_entry {
277 const char *name;
318a1971 278 size_t length;
e6bff466 279 bool is_inline;
63dcc709 280 enum dev_prop_type type;
16ba08d5 281 union {
1f74d70f 282 const void *pointer;
66586bab 283 union {
996b0830
DT
284 u8 u8_data[sizeof(u64) / sizeof(u8)];
285 u16 u16_data[sizeof(u64) / sizeof(u16)];
286 u32 u32_data[sizeof(u64) / sizeof(u32)];
287 u64 u64_data[sizeof(u64) / sizeof(u64)];
288 const char *str[sizeof(u64) / sizeof(char *)];
66586bab
AS
289 } value;
290 };
16ba08d5
RW
291};
292
d76eebfa 293/*
daeba9bf 294 * Note: the below initializers for the anonymous union are carefully
d76eebfa
AM
295 * crafted to avoid gcc-4.4.4's problems with initialization of anon unions
296 * and structs.
297 */
298
daeba9bf 299#define __PROPERTY_ENTRY_ELEMENT_SIZE(_elem_) \
996b0830 300 sizeof(((struct property_entry *)NULL)->value._elem_[0])
daeba9bf 301
e64b674b
DT
302#define __PROPERTY_ENTRY_ARRAY_ELSIZE_LEN(_name_, _elsize_, _Type_, \
303 _val_, _len_) \
63dcc709
AS
304(struct property_entry) { \
305 .name = _name_, \
e64b674b 306 .length = (_len_) * (_elsize_), \
63dcc709 307 .type = DEV_PROP_##_Type_, \
1f74d70f 308 { .pointer = _val_ }, \
a85f4204
HK
309}
310
e64b674b
DT
311#define __PROPERTY_ENTRY_ARRAY_LEN(_name_, _elem_, _Type_, _val_, _len_)\
312 __PROPERTY_ENTRY_ARRAY_ELSIZE_LEN(_name_, \
313 __PROPERTY_ENTRY_ELEMENT_SIZE(_elem_), \
314 _Type_, _val_, _len_)
315
1741cfac 316#define PROPERTY_ENTRY_U8_ARRAY_LEN(_name_, _val_, _len_) \
daeba9bf 317 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u8_data, U8, _val_, _len_)
1741cfac 318#define PROPERTY_ENTRY_U16_ARRAY_LEN(_name_, _val_, _len_) \
daeba9bf 319 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u16_data, U16, _val_, _len_)
1741cfac 320#define PROPERTY_ENTRY_U32_ARRAY_LEN(_name_, _val_, _len_) \
daeba9bf 321 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u32_data, U32, _val_, _len_)
1741cfac 322#define PROPERTY_ENTRY_U64_ARRAY_LEN(_name_, _val_, _len_) \
daeba9bf 323 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u64_data, U64, _val_, _len_)
1741cfac 324#define PROPERTY_ENTRY_STRING_ARRAY_LEN(_name_, _val_, _len_) \
daeba9bf 325 __PROPERTY_ENTRY_ARRAY_LEN(_name_, str, STRING, _val_, _len_)
e64b674b
DT
326#define PROPERTY_ENTRY_REF_ARRAY_LEN(_name_, _val_, _len_) \
327 __PROPERTY_ENTRY_ARRAY_ELSIZE_LEN(_name_, \
328 sizeof(struct software_node_ref_args), \
329 REF, _val_, _len_)
a85f4204 330
1741cfac
DT
331#define PROPERTY_ENTRY_U8_ARRAY(_name_, _val_) \
332 PROPERTY_ENTRY_U8_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
333#define PROPERTY_ENTRY_U16_ARRAY(_name_, _val_) \
334 PROPERTY_ENTRY_U16_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
335#define PROPERTY_ENTRY_U32_ARRAY(_name_, _val_) \
336 PROPERTY_ENTRY_U32_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
337#define PROPERTY_ENTRY_U64_ARRAY(_name_, _val_) \
338 PROPERTY_ENTRY_U64_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
339#define PROPERTY_ENTRY_STRING_ARRAY(_name_, _val_) \
340 PROPERTY_ENTRY_STRING_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
e64b674b
DT
341#define PROPERTY_ENTRY_REF_ARRAY(_name_, _val_) \
342 PROPERTY_ENTRY_REF_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
1741cfac 343
daeba9bf
DT
344#define __PROPERTY_ENTRY_ELEMENT(_name_, _elem_, _Type_, _val_) \
345(struct property_entry) { \
346 .name = _name_, \
347 .length = __PROPERTY_ENTRY_ELEMENT_SIZE(_elem_), \
e6bff466 348 .is_inline = true, \
daeba9bf 349 .type = DEV_PROP_##_Type_, \
996b0830 350 { .value = { ._elem_[0] = _val_ } }, \
a85f4204
HK
351}
352
daeba9bf
DT
353#define PROPERTY_ENTRY_U8(_name_, _val_) \
354 __PROPERTY_ENTRY_ELEMENT(_name_, u8_data, U8, _val_)
355#define PROPERTY_ENTRY_U16(_name_, _val_) \
356 __PROPERTY_ENTRY_ELEMENT(_name_, u16_data, U16, _val_)
357#define PROPERTY_ENTRY_U32(_name_, _val_) \
358 __PROPERTY_ENTRY_ELEMENT(_name_, u32_data, U32, _val_)
359#define PROPERTY_ENTRY_U64(_name_, _val_) \
360 __PROPERTY_ENTRY_ELEMENT(_name_, u64_data, U64, _val_)
361#define PROPERTY_ENTRY_STRING(_name_, _val_) \
362 __PROPERTY_ENTRY_ELEMENT(_name_, str, STRING, _val_)
a85f4204
HK
363
364#define PROPERTY_ENTRY_BOOL(_name_) \
c505cbd4 365(struct property_entry) { \
a85f4204 366 .name = _name_, \
e6bff466 367 .is_inline = true, \
a85f4204
HK
368}
369
e64b674b
DT
370#define PROPERTY_ENTRY_REF(_name_, _ref_, ...) \
371(struct property_entry) { \
372 .name = _name_, \
373 .length = sizeof(struct software_node_ref_args), \
374 .type = DEV_PROP_REF, \
e588fead 375 { .pointer = &SOFTWARE_NODE_REFERENCE(_ref_, ##__VA_ARGS__), }, \
e64b674b
DT
376}
377
2d479e1f
DT
378struct property_entry *
379property_entries_dup(const struct property_entry *properties);
380
381void property_entries_free(const struct property_entry *properties);
382
e5e55864
SS
383bool device_dma_supported(struct device *dev);
384
385enum dev_dma_attr device_get_dma_attr(struct device *dev);
b283f157 386
67dcc26d 387const void *device_get_match_data(struct device *dev);
e5e55864 388
4c96b7dc
JL
389int device_get_phy_mode(struct device *dev);
390
b28f263b 391int fwnode_get_phy_mode(struct fwnode_handle *fwnode);
07bb80d4 392struct fwnode_handle *fwnode_graph_get_next_endpoint(
37ba983c 393 const struct fwnode_handle *fwnode, struct fwnode_handle *prev);
6a71d8d7 394struct fwnode_handle *
37ba983c 395fwnode_graph_get_port_parent(const struct fwnode_handle *fwnode);
07bb80d4 396struct fwnode_handle *fwnode_graph_get_remote_port_parent(
37ba983c 397 const struct fwnode_handle *fwnode);
07bb80d4 398struct fwnode_handle *fwnode_graph_get_remote_port(
37ba983c 399 const struct fwnode_handle *fwnode);
07bb80d4 400struct fwnode_handle *fwnode_graph_get_remote_endpoint(
37ba983c 401 const struct fwnode_handle *fwnode);
07bb80d4 402
35694afc
LP
403static inline bool fwnode_graph_is_endpoint(struct fwnode_handle *fwnode)
404{
405 return fwnode_property_present(fwnode, "remote-endpoint");
406}
407
0fcc2bdc
SA
408/*
409 * Fwnode lookup flags
410 *
411 * @FWNODE_GRAPH_ENDPOINT_NEXT: In the case of no exact match, look for the
412 * closest endpoint ID greater than the specified
413 * one.
414 * @FWNODE_GRAPH_DEVICE_DISABLED: That the device to which the remote
415 * endpoint of the given endpoint belongs to,
49f39cb0
SA
416 * may be disabled, or that the endpoint is not
417 * connected.
0fcc2bdc
SA
418 */
419#define FWNODE_GRAPH_ENDPOINT_NEXT BIT(0)
420#define FWNODE_GRAPH_DEVICE_DISABLED BIT(1)
421
422struct fwnode_handle *
423fwnode_graph_get_endpoint_by_id(const struct fwnode_handle *fwnode,
424 u32 port, u32 endpoint, unsigned long flags);
c87b8fc5
SA
425unsigned int fwnode_graph_get_endpoint_count(struct fwnode_handle *fwnode,
426 unsigned long flags);
0fcc2bdc 427
5e4b1b70
SA
428#define fwnode_graph_for_each_endpoint(fwnode, child) \
429 for (child = NULL; \
430 (child = fwnode_graph_get_next_endpoint(fwnode, child)); )
431
37ba983c 432int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
2bd5452d
SA
433 struct fwnode_endpoint *endpoint);
434
d7cf5590
HK
435typedef void *(*devcon_match_fn_t)(struct fwnode_handle *fwnode, const char *id,
436 void *data);
437
438void *fwnode_connection_find_match(struct fwnode_handle *fwnode,
439 const char *con_id, void *data,
440 devcon_match_fn_t match);
441
442static inline void *device_connection_find_match(struct device *dev,
443 const char *con_id, void *data,
444 devcon_match_fn_t match)
445{
446 return fwnode_connection_find_match(dev_fwnode(dev), con_id, data, match);
447}
448
59abd836
HK
449/* -------------------------------------------------------------------------- */
450/* Software fwnode support - when HW description is incomplete or missing */
451
80488a6b
HK
452/**
453 * struct software_node - Software node description
454 * @name: Name of the software node
455 * @parent: Parent of the software node
456 * @properties: Array of device properties
457 */
458struct software_node {
459 const char *name;
460 const struct software_node *parent;
461 const struct property_entry *properties;
462};
463
59abd836 464bool is_software_node(const struct fwnode_handle *fwnode);
56c9aa07
SA
465const struct software_node *
466to_software_node(const struct fwnode_handle *fwnode);
80488a6b
HK
467struct fwnode_handle *software_node_fwnode(const struct software_node *node);
468
1666faed
HK
469const struct software_node *
470software_node_find_by_name(const struct software_node *parent,
471 const char *name);
472
80488a6b
HK
473int software_node_register_nodes(const struct software_node *nodes);
474void software_node_unregister_nodes(const struct software_node *nodes);
475
02094d54
AS
476int software_node_register_node_group(const struct software_node **node_group);
477void software_node_unregister_node_group(const struct software_node **node_group);
478
80488a6b 479int software_node_register(const struct software_node *node);
46d26819 480void software_node_unregister(const struct software_node *node);
59abd836 481
59abd836
HK
482struct fwnode_handle *
483fwnode_create_software_node(const struct property_entry *properties,
484 const struct fwnode_handle *parent);
485void fwnode_remove_software_node(struct fwnode_handle *fwnode);
486
2a92c90f 487int device_add_software_node(struct device *dev, const struct software_node *node);
e68d0119
HK
488void device_remove_software_node(struct device *dev);
489
151f6ff7
HK
490int device_create_managed_software_node(struct device *dev,
491 const struct property_entry *properties,
492 const struct software_node *parent);
493
b31384fa 494#endif /* _LINUX_PROPERTY_H_ */