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