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