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