Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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/args.h>
14#include <linux/array_size.h>
15#include <linux/bits.h>
16#include <linux/cleanup.h>
17#include <linux/fwnode.h>
18#include <linux/stddef.h>
19#include <linux/types.h>
20#include <linux/util_macros.h>
21
22struct device;
23
24enum dev_prop_type {
25 DEV_PROP_U8,
26 DEV_PROP_U16,
27 DEV_PROP_U32,
28 DEV_PROP_U64,
29 DEV_PROP_STRING,
30 DEV_PROP_REF,
31};
32
33const struct fwnode_handle *__dev_fwnode_const(const struct device *dev);
34struct fwnode_handle *__dev_fwnode(struct device *dev);
35#define dev_fwnode(dev) \
36 _Generic((dev), \
37 const struct device *: __dev_fwnode_const, \
38 struct device *: __dev_fwnode)(dev)
39
40bool device_property_present(const struct device *dev, const char *propname);
41bool device_property_read_bool(const struct device *dev, const char *propname);
42int device_property_read_u8_array(const struct device *dev, const char *propname,
43 u8 *val, size_t nval);
44int device_property_read_u16_array(const struct device *dev, const char *propname,
45 u16 *val, size_t nval);
46int device_property_read_u32_array(const struct device *dev, const char *propname,
47 u32 *val, size_t nval);
48int device_property_read_u64_array(const struct device *dev, const char *propname,
49 u64 *val, size_t nval);
50int device_property_read_string_array(const struct device *dev, const char *propname,
51 const char **val, size_t nval);
52int device_property_read_string(const struct device *dev, const char *propname,
53 const char **val);
54int device_property_match_string(const struct device *dev,
55 const char *propname, const char *string);
56
57bool fwnode_property_present(const struct fwnode_handle *fwnode,
58 const char *propname);
59bool fwnode_property_read_bool(const struct fwnode_handle *fwnode,
60 const char *propname);
61int fwnode_property_read_u8_array(const struct fwnode_handle *fwnode,
62 const char *propname, u8 *val,
63 size_t nval);
64int fwnode_property_read_u16_array(const struct fwnode_handle *fwnode,
65 const char *propname, u16 *val,
66 size_t nval);
67int fwnode_property_read_u32_array(const struct fwnode_handle *fwnode,
68 const char *propname, u32 *val,
69 size_t nval);
70int fwnode_property_read_u64_array(const struct fwnode_handle *fwnode,
71 const char *propname, u64 *val,
72 size_t nval);
73int fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
74 const char *propname, const char **val,
75 size_t nval);
76int fwnode_property_read_string(const struct fwnode_handle *fwnode,
77 const char *propname, const char **val);
78int fwnode_property_match_string(const struct fwnode_handle *fwnode,
79 const char *propname, const char *string);
80
81bool fwnode_device_is_available(const struct fwnode_handle *fwnode);
82
83static inline bool fwnode_device_is_big_endian(const struct fwnode_handle *fwnode)
84{
85 if (fwnode_property_present(fwnode, "big-endian"))
86 return true;
87 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
88 fwnode_property_present(fwnode, "native-endian"))
89 return true;
90 return false;
91}
92
93static inline
94bool fwnode_device_is_compatible(const struct fwnode_handle *fwnode, const char *compat)
95{
96 return fwnode_property_match_string(fwnode, "compatible", compat) >= 0;
97}
98
99/**
100 * device_is_big_endian - check if a device has BE registers
101 * @dev: Pointer to the struct device
102 *
103 * Returns: true if the device has a "big-endian" property, or if the kernel
104 * was compiled for BE *and* the device has a "native-endian" property.
105 * Returns false otherwise.
106 *
107 * Callers would nominally use ioread32be/iowrite32be if
108 * device_is_big_endian() == true, or readl/writel otherwise.
109 */
110static inline bool device_is_big_endian(const struct device *dev)
111{
112 return fwnode_device_is_big_endian(dev_fwnode(dev));
113}
114
115/**
116 * device_is_compatible - match 'compatible' property of the device with a given string
117 * @dev: Pointer to the struct device
118 * @compat: The string to match 'compatible' property with
119 *
120 * Returns: true if matches, otherwise false.
121 */
122static inline bool device_is_compatible(const struct device *dev, const char *compat)
123{
124 return fwnode_device_is_compatible(dev_fwnode(dev), compat);
125}
126
127int fwnode_property_match_property_string(const struct fwnode_handle *fwnode,
128 const char *propname,
129 const char * const *array, size_t n);
130
131static inline
132int device_property_match_property_string(const struct device *dev,
133 const char *propname,
134 const char * const *array, size_t n)
135{
136 return fwnode_property_match_property_string(dev_fwnode(dev), propname, array, n);
137}
138
139int fwnode_property_get_reference_args(const struct fwnode_handle *fwnode,
140 const char *prop, const char *nargs_prop,
141 unsigned int nargs, unsigned int index,
142 struct fwnode_reference_args *args);
143
144struct fwnode_handle *fwnode_find_reference(const struct fwnode_handle *fwnode,
145 const char *name,
146 unsigned int index);
147
148const char *fwnode_get_name(const struct fwnode_handle *fwnode);
149const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode);
150bool fwnode_name_eq(const struct fwnode_handle *fwnode, const char *name);
151
152struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode);
153struct fwnode_handle *fwnode_get_next_parent(struct fwnode_handle *fwnode);
154
155#define fwnode_for_each_parent_node(fwnode, parent) \
156 for (parent = fwnode_get_parent(fwnode); parent; \
157 parent = fwnode_get_next_parent(parent))
158
159unsigned int fwnode_count_parents(const struct fwnode_handle *fwn);
160struct fwnode_handle *fwnode_get_nth_parent(struct fwnode_handle *fwn,
161 unsigned int depth);
162struct fwnode_handle *fwnode_get_next_child_node(
163 const struct fwnode_handle *fwnode, struct fwnode_handle *child);
164struct fwnode_handle *fwnode_get_next_available_child_node(
165 const struct fwnode_handle *fwnode, struct fwnode_handle *child);
166
167#define fwnode_for_each_child_node(fwnode, child) \
168 for (child = fwnode_get_next_child_node(fwnode, NULL); child; \
169 child = fwnode_get_next_child_node(fwnode, child))
170
171#define fwnode_for_each_named_child_node(fwnode, child, name) \
172 fwnode_for_each_child_node(fwnode, child) \
173 for_each_if(fwnode_name_eq(child, name))
174
175#define fwnode_for_each_available_child_node(fwnode, child) \
176 for (child = fwnode_get_next_available_child_node(fwnode, NULL); child;\
177 child = fwnode_get_next_available_child_node(fwnode, child))
178
179struct fwnode_handle *device_get_next_child_node(const struct device *dev,
180 struct fwnode_handle *child);
181
182#define device_for_each_child_node(dev, child) \
183 for (child = device_get_next_child_node(dev, NULL); child; \
184 child = device_get_next_child_node(dev, child))
185
186#define device_for_each_named_child_node(dev, child, name) \
187 device_for_each_child_node(dev, child) \
188 for_each_if(fwnode_name_eq(child, name))
189
190#define device_for_each_child_node_scoped(dev, child) \
191 for (struct fwnode_handle *child __free(fwnode_handle) = \
192 device_get_next_child_node(dev, NULL); \
193 child; child = device_get_next_child_node(dev, child))
194
195#define device_for_each_named_child_node_scoped(dev, child, name) \
196 device_for_each_child_node_scoped(dev, child) \
197 for_each_if(fwnode_name_eq(child, name))
198
199struct fwnode_handle *fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
200 const char *childname);
201struct fwnode_handle *device_get_named_child_node(const struct device *dev,
202 const char *childname);
203
204struct fwnode_handle *fwnode_handle_get(struct fwnode_handle *fwnode);
205
206/**
207 * fwnode_handle_put - Drop reference to a device node
208 * @fwnode: Pointer to the device node to drop the reference to.
209 *
210 * This has to be used when terminating device_for_each_child_node() iteration
211 * with break or return to prevent stale device node references from being left
212 * behind.
213 */
214static inline void fwnode_handle_put(struct fwnode_handle *fwnode)
215{
216 fwnode_call_void_op(fwnode, put);
217}
218
219DEFINE_FREE(fwnode_handle, struct fwnode_handle *, fwnode_handle_put(_T))
220
221int fwnode_irq_get(const struct fwnode_handle *fwnode, unsigned int index);
222int fwnode_irq_get_byname(const struct fwnode_handle *fwnode, const char *name);
223
224unsigned int fwnode_get_child_node_count(const struct fwnode_handle *fwnode);
225
226static inline unsigned int device_get_child_node_count(const struct device *dev)
227{
228 return fwnode_get_child_node_count(dev_fwnode(dev));
229}
230
231unsigned int fwnode_get_named_child_node_count(const struct fwnode_handle *fwnode,
232 const char *name);
233static inline unsigned int device_get_named_child_node_count(const struct device *dev,
234 const char *name)
235{
236 return fwnode_get_named_child_node_count(dev_fwnode(dev), name);
237}
238
239static inline int device_property_read_u8(const struct device *dev,
240 const char *propname, u8 *val)
241{
242 return device_property_read_u8_array(dev, propname, val, 1);
243}
244
245static inline int device_property_read_u16(const struct device *dev,
246 const char *propname, u16 *val)
247{
248 return device_property_read_u16_array(dev, propname, val, 1);
249}
250
251static inline int device_property_read_u32(const struct device *dev,
252 const char *propname, u32 *val)
253{
254 return device_property_read_u32_array(dev, propname, val, 1);
255}
256
257static inline int device_property_read_u64(const struct device *dev,
258 const char *propname, u64 *val)
259{
260 return device_property_read_u64_array(dev, propname, val, 1);
261}
262
263static inline int device_property_count_u8(const struct device *dev, const char *propname)
264{
265 return device_property_read_u8_array(dev, propname, NULL, 0);
266}
267
268static inline int device_property_count_u16(const struct device *dev, const char *propname)
269{
270 return device_property_read_u16_array(dev, propname, NULL, 0);
271}
272
273static inline int device_property_count_u32(const struct device *dev, const char *propname)
274{
275 return device_property_read_u32_array(dev, propname, NULL, 0);
276}
277
278static inline int device_property_count_u64(const struct device *dev, const char *propname)
279{
280 return device_property_read_u64_array(dev, propname, NULL, 0);
281}
282
283static inline int device_property_string_array_count(const struct device *dev,
284 const char *propname)
285{
286 return device_property_read_string_array(dev, propname, NULL, 0);
287}
288
289static inline int fwnode_property_read_u8(const struct fwnode_handle *fwnode,
290 const char *propname, u8 *val)
291{
292 return fwnode_property_read_u8_array(fwnode, propname, val, 1);
293}
294
295static inline int fwnode_property_read_u16(const struct fwnode_handle *fwnode,
296 const char *propname, u16 *val)
297{
298 return fwnode_property_read_u16_array(fwnode, propname, val, 1);
299}
300
301static inline int fwnode_property_read_u32(const struct fwnode_handle *fwnode,
302 const char *propname, u32 *val)
303{
304 return fwnode_property_read_u32_array(fwnode, propname, val, 1);
305}
306
307static inline int fwnode_property_read_u64(const struct fwnode_handle *fwnode,
308 const char *propname, u64 *val)
309{
310 return fwnode_property_read_u64_array(fwnode, propname, val, 1);
311}
312
313static inline int fwnode_property_count_u8(const struct fwnode_handle *fwnode,
314 const char *propname)
315{
316 return fwnode_property_read_u8_array(fwnode, propname, NULL, 0);
317}
318
319static inline int fwnode_property_count_u16(const struct fwnode_handle *fwnode,
320 const char *propname)
321{
322 return fwnode_property_read_u16_array(fwnode, propname, NULL, 0);
323}
324
325static inline int fwnode_property_count_u32(const struct fwnode_handle *fwnode,
326 const char *propname)
327{
328 return fwnode_property_read_u32_array(fwnode, propname, NULL, 0);
329}
330
331static inline int fwnode_property_count_u64(const struct fwnode_handle *fwnode,
332 const char *propname)
333{
334 return fwnode_property_read_u64_array(fwnode, propname, NULL, 0);
335}
336
337static inline int
338fwnode_property_string_array_count(const struct fwnode_handle *fwnode,
339 const char *propname)
340{
341 return fwnode_property_read_string_array(fwnode, propname, NULL, 0);
342}
343
344struct software_node;
345
346/**
347 * struct software_node_ref_args - Reference property with additional arguments
348 * @node: Reference to a software node
349 * @nargs: Number of elements in @args array
350 * @args: Integer arguments
351 */
352struct software_node_ref_args {
353 const struct software_node *node;
354 unsigned int nargs;
355 u64 args[NR_FWNODE_REFERENCE_ARGS];
356};
357
358#define SOFTWARE_NODE_REFERENCE(_ref_, ...) \
359(const struct software_node_ref_args) { \
360 .node = _ref_, \
361 .nargs = COUNT_ARGS(__VA_ARGS__), \
362 .args = { __VA_ARGS__ }, \
363}
364
365/**
366 * struct property_entry - "Built-in" device property representation.
367 * @name: Name of the property.
368 * @length: Length of data making up the value.
369 * @is_inline: True when the property value is stored inline.
370 * @type: Type of the data in unions.
371 * @pointer: Pointer to the property when it is not stored inline.
372 * @value: Value of the property when it is stored inline.
373 */
374struct property_entry {
375 const char *name;
376 size_t length;
377 bool is_inline;
378 enum dev_prop_type type;
379 union {
380 const void *pointer;
381 union {
382 u8 u8_data[sizeof(u64) / sizeof(u8)];
383 u16 u16_data[sizeof(u64) / sizeof(u16)];
384 u32 u32_data[sizeof(u64) / sizeof(u32)];
385 u64 u64_data[sizeof(u64) / sizeof(u64)];
386 const char *str[sizeof(u64) / sizeof(char *)];
387 } value;
388 };
389};
390
391/*
392 * Note: the below initializers for the anonymous union are carefully
393 * crafted to avoid gcc-4.4.4's problems with initialization of anon unions
394 * and structs.
395 */
396#define __PROPERTY_ENTRY_ARRAY_LEN(_name_, _elem_, _Type_, _val_, _len_) \
397(struct property_entry) { \
398 .name = _name_, \
399 .length = (_len_) * sizeof_field(struct property_entry, value._elem_[0]), \
400 .type = DEV_PROP_##_Type_, \
401 { .pointer = _val_ }, \
402}
403
404#define PROPERTY_ENTRY_U8_ARRAY_LEN(_name_, _val_, _len_) \
405 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u8_data, U8, _val_, _len_)
406#define PROPERTY_ENTRY_U16_ARRAY_LEN(_name_, _val_, _len_) \
407 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u16_data, U16, _val_, _len_)
408#define PROPERTY_ENTRY_U32_ARRAY_LEN(_name_, _val_, _len_) \
409 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u32_data, U32, _val_, _len_)
410#define PROPERTY_ENTRY_U64_ARRAY_LEN(_name_, _val_, _len_) \
411 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u64_data, U64, _val_, _len_)
412#define PROPERTY_ENTRY_STRING_ARRAY_LEN(_name_, _val_, _len_) \
413 __PROPERTY_ENTRY_ARRAY_LEN(_name_, str, STRING, _val_, _len_)
414
415#define PROPERTY_ENTRY_REF_ARRAY_LEN(_name_, _val_, _len_) \
416(struct property_entry) { \
417 .name = _name_, \
418 .length = (_len_) * sizeof(struct software_node_ref_args), \
419 .type = DEV_PROP_REF, \
420 { .pointer = _val_ }, \
421}
422
423#define PROPERTY_ENTRY_U8_ARRAY(_name_, _val_) \
424 PROPERTY_ENTRY_U8_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
425#define PROPERTY_ENTRY_U16_ARRAY(_name_, _val_) \
426 PROPERTY_ENTRY_U16_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
427#define PROPERTY_ENTRY_U32_ARRAY(_name_, _val_) \
428 PROPERTY_ENTRY_U32_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
429#define PROPERTY_ENTRY_U64_ARRAY(_name_, _val_) \
430 PROPERTY_ENTRY_U64_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
431#define PROPERTY_ENTRY_STRING_ARRAY(_name_, _val_) \
432 PROPERTY_ENTRY_STRING_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
433#define PROPERTY_ENTRY_REF_ARRAY(_name_, _val_) \
434 PROPERTY_ENTRY_REF_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
435
436#define __PROPERTY_ENTRY_ELEMENT(_name_, _elem_, _Type_, _val_) \
437(struct property_entry) { \
438 .name = _name_, \
439 .length = sizeof_field(struct property_entry, value._elem_[0]), \
440 .is_inline = true, \
441 .type = DEV_PROP_##_Type_, \
442 { .value = { ._elem_[0] = _val_ } }, \
443}
444
445#define PROPERTY_ENTRY_U8(_name_, _val_) \
446 __PROPERTY_ENTRY_ELEMENT(_name_, u8_data, U8, _val_)
447#define PROPERTY_ENTRY_U16(_name_, _val_) \
448 __PROPERTY_ENTRY_ELEMENT(_name_, u16_data, U16, _val_)
449#define PROPERTY_ENTRY_U32(_name_, _val_) \
450 __PROPERTY_ENTRY_ELEMENT(_name_, u32_data, U32, _val_)
451#define PROPERTY_ENTRY_U64(_name_, _val_) \
452 __PROPERTY_ENTRY_ELEMENT(_name_, u64_data, U64, _val_)
453#define PROPERTY_ENTRY_STRING(_name_, _val_) \
454 __PROPERTY_ENTRY_ELEMENT(_name_, str, STRING, _val_)
455
456#define PROPERTY_ENTRY_REF(_name_, _ref_, ...) \
457(struct property_entry) { \
458 .name = _name_, \
459 .length = sizeof(struct software_node_ref_args), \
460 .type = DEV_PROP_REF, \
461 { .pointer = &SOFTWARE_NODE_REFERENCE(_ref_, ##__VA_ARGS__), }, \
462}
463
464#define PROPERTY_ENTRY_BOOL(_name_) \
465(struct property_entry) { \
466 .name = _name_, \
467 .is_inline = true, \
468}
469
470struct property_entry *
471property_entries_dup(const struct property_entry *properties);
472void property_entries_free(const struct property_entry *properties);
473
474bool device_dma_supported(const struct device *dev);
475enum dev_dma_attr device_get_dma_attr(const struct device *dev);
476
477const void *device_get_match_data(const struct device *dev);
478
479int device_get_phy_mode(struct device *dev);
480int fwnode_get_phy_mode(const struct fwnode_handle *fwnode);
481
482void __iomem *fwnode_iomap(struct fwnode_handle *fwnode, int index);
483
484struct fwnode_handle *fwnode_graph_get_next_endpoint(
485 const struct fwnode_handle *fwnode, struct fwnode_handle *prev);
486struct fwnode_handle *
487fwnode_graph_get_port_parent(const struct fwnode_handle *fwnode);
488struct fwnode_handle *fwnode_graph_get_remote_port_parent(
489 const struct fwnode_handle *fwnode);
490struct fwnode_handle *fwnode_graph_get_remote_port(
491 const struct fwnode_handle *fwnode);
492struct fwnode_handle *fwnode_graph_get_remote_endpoint(
493 const struct fwnode_handle *fwnode);
494
495static inline bool fwnode_graph_is_endpoint(const struct fwnode_handle *fwnode)
496{
497 return fwnode_property_present(fwnode, "remote-endpoint");
498}
499
500/*
501 * Fwnode lookup flags
502 *
503 * @FWNODE_GRAPH_ENDPOINT_NEXT: In the case of no exact match, look for the
504 * closest endpoint ID greater than the specified
505 * one.
506 * @FWNODE_GRAPH_DEVICE_DISABLED: That the device to which the remote
507 * endpoint of the given endpoint belongs to,
508 * may be disabled, or that the endpoint is not
509 * connected.
510 */
511#define FWNODE_GRAPH_ENDPOINT_NEXT BIT(0)
512#define FWNODE_GRAPH_DEVICE_DISABLED BIT(1)
513
514struct fwnode_handle *
515fwnode_graph_get_endpoint_by_id(const struct fwnode_handle *fwnode,
516 u32 port, u32 endpoint, unsigned long flags);
517unsigned int fwnode_graph_get_endpoint_count(const struct fwnode_handle *fwnode,
518 unsigned long flags);
519
520#define fwnode_graph_for_each_endpoint(fwnode, child) \
521 for (child = fwnode_graph_get_next_endpoint(fwnode, NULL); child; \
522 child = fwnode_graph_get_next_endpoint(fwnode, child))
523
524int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
525 struct fwnode_endpoint *endpoint);
526
527typedef void *(*devcon_match_fn_t)(const struct fwnode_handle *fwnode, const char *id,
528 void *data);
529
530void *fwnode_connection_find_match(const struct fwnode_handle *fwnode,
531 const char *con_id, void *data,
532 devcon_match_fn_t match);
533
534static inline void *device_connection_find_match(const struct device *dev,
535 const char *con_id, void *data,
536 devcon_match_fn_t match)
537{
538 return fwnode_connection_find_match(dev_fwnode(dev), con_id, data, match);
539}
540
541int fwnode_connection_find_matches(const struct fwnode_handle *fwnode,
542 const char *con_id, void *data,
543 devcon_match_fn_t match,
544 void **matches, unsigned int matches_len);
545
546/* -------------------------------------------------------------------------- */
547/* Software fwnode support - when HW description is incomplete or missing */
548
549/**
550 * struct software_node - Software node description
551 * @name: Name of the software node
552 * @parent: Parent of the software node
553 * @properties: Array of device properties
554 */
555struct software_node {
556 const char *name;
557 const struct software_node *parent;
558 const struct property_entry *properties;
559};
560
561#define SOFTWARE_NODE(_name_, _properties_, _parent_) \
562 (struct software_node) { \
563 .name = _name_, \
564 .properties = _properties_, \
565 .parent = _parent_, \
566 }
567
568bool is_software_node(const struct fwnode_handle *fwnode);
569const struct software_node *
570to_software_node(const struct fwnode_handle *fwnode);
571struct fwnode_handle *software_node_fwnode(const struct software_node *node);
572
573const struct software_node *
574software_node_find_by_name(const struct software_node *parent,
575 const char *name);
576
577int software_node_register_node_group(const struct software_node **node_group);
578void software_node_unregister_node_group(const struct software_node **node_group);
579
580int software_node_register(const struct software_node *node);
581void software_node_unregister(const struct software_node *node);
582
583struct fwnode_handle *
584fwnode_create_software_node(const struct property_entry *properties,
585 const struct fwnode_handle *parent);
586void fwnode_remove_software_node(struct fwnode_handle *fwnode);
587
588int device_add_software_node(struct device *dev, const struct software_node *node);
589void device_remove_software_node(struct device *dev);
590
591int device_create_managed_software_node(struct device *dev,
592 const struct property_entry *properties,
593 const struct software_node *parent);
594
595#endif /* _LINUX_PROPERTY_H_ */