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

source code of linux/include/linux/property.h