xref: /linux-6.15/include/linux/fwnode.h (revision 1fc3d0ee)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * fwnode.h - Firmware device node object handle type definition.
4  *
5  * Copyright (C) 2015, Intel Corporation
6  * Author: Rafael J. Wysocki <[email protected]>
7  */
8 
9 #ifndef _LINUX_FWNODE_H_
10 #define _LINUX_FWNODE_H_
11 
12 #include <linux/types.h>
13 
14 struct fwnode_operations;
15 struct device;
16 
17 struct fwnode_handle {
18 	struct fwnode_handle *secondary;
19 	const struct fwnode_operations *ops;
20 };
21 
22 /**
23  * struct fwnode_endpoint - Fwnode graph endpoint
24  * @port: Port number
25  * @id: Endpoint id
26  * @local_fwnode: reference to the related fwnode
27  */
28 struct fwnode_endpoint {
29 	unsigned int port;
30 	unsigned int id;
31 	const struct fwnode_handle *local_fwnode;
32 };
33 
34 #define NR_FWNODE_REFERENCE_ARGS	8
35 
36 /**
37  * struct fwnode_reference_args - Fwnode reference with additional arguments
38  * @fwnode:- A reference to the base fwnode
39  * @nargs: Number of elements in @args array
40  * @args: Integer arguments on the fwnode
41  */
42 struct fwnode_reference_args {
43 	struct fwnode_handle *fwnode;
44 	unsigned int nargs;
45 	u64 args[NR_FWNODE_REFERENCE_ARGS];
46 };
47 
48 /**
49  * struct fwnode_operations - Operations for fwnode interface
50  * @get: Get a reference to an fwnode.
51  * @put: Put a reference to an fwnode.
52  * @device_is_available: Return true if the device is available.
53  * @device_get_match_data: Return the device driver match data.
54  * @property_present: Return true if a property is present.
55  * @property_read_int_array: Read an array of integer properties. Return zero on
56  *			     success, a negative error code otherwise.
57  * @property_read_string_array: Read an array of string properties. Return zero
58  *				on success, a negative error code otherwise.
59  * @get_name: Return the name of an fwnode.
60  * @get_name_prefix: Get a prefix for a node (for printing purposes).
61  * @get_parent: Return the parent of an fwnode.
62  * @get_next_child_node: Return the next child node in an iteration.
63  * @get_named_child_node: Return a child node with a given name.
64  * @get_reference_args: Return a reference pointed to by a property, with args
65  * @graph_get_next_endpoint: Return an endpoint node in an iteration.
66  * @graph_get_remote_endpoint: Return the remote endpoint node of a local
67  *			       endpoint node.
68  * @graph_get_port_parent: Return the parent node of a port node.
69  * @graph_parse_endpoint: Parse endpoint for port and endpoint id.
70  */
71 struct fwnode_operations {
72 	struct fwnode_handle *(*get)(struct fwnode_handle *fwnode);
73 	void (*put)(struct fwnode_handle *fwnode);
74 	bool (*device_is_available)(const struct fwnode_handle *fwnode);
75 	const void *(*device_get_match_data)(const struct fwnode_handle *fwnode,
76 					     const struct device *dev);
77 	bool (*property_present)(const struct fwnode_handle *fwnode,
78 				 const char *propname);
79 	int (*property_read_int_array)(const struct fwnode_handle *fwnode,
80 				       const char *propname,
81 				       unsigned int elem_size, void *val,
82 				       size_t nval);
83 	int
84 	(*property_read_string_array)(const struct fwnode_handle *fwnode_handle,
85 				      const char *propname, const char **val,
86 				      size_t nval);
87 	const char *(*get_name)(const struct fwnode_handle *fwnode);
88 	const char *(*get_name_prefix)(const struct fwnode_handle *fwnode);
89 	struct fwnode_handle *(*get_parent)(const struct fwnode_handle *fwnode);
90 	struct fwnode_handle *
91 	(*get_next_child_node)(const struct fwnode_handle *fwnode,
92 			       struct fwnode_handle *child);
93 	struct fwnode_handle *
94 	(*get_named_child_node)(const struct fwnode_handle *fwnode,
95 				const char *name);
96 	int (*get_reference_args)(const struct fwnode_handle *fwnode,
97 				  const char *prop, const char *nargs_prop,
98 				  unsigned int nargs, unsigned int index,
99 				  struct fwnode_reference_args *args);
100 	struct fwnode_handle *
101 	(*graph_get_next_endpoint)(const struct fwnode_handle *fwnode,
102 				   struct fwnode_handle *prev);
103 	struct fwnode_handle *
104 	(*graph_get_remote_endpoint)(const struct fwnode_handle *fwnode);
105 	struct fwnode_handle *
106 	(*graph_get_port_parent)(struct fwnode_handle *fwnode);
107 	int (*graph_parse_endpoint)(const struct fwnode_handle *fwnode,
108 				    struct fwnode_endpoint *endpoint);
109 };
110 
111 #define fwnode_has_op(fwnode, op)				\
112 	((fwnode) && (fwnode)->ops && (fwnode)->ops->op)
113 #define fwnode_call_int_op(fwnode, op, ...)				\
114 	(fwnode ? (fwnode_has_op(fwnode, op) ?				\
115 		   (fwnode)->ops->op(fwnode, ## __VA_ARGS__) : -ENXIO) : \
116 	 -EINVAL)
117 
118 #define fwnode_call_bool_op(fwnode, op, ...)		\
119 	(fwnode_has_op(fwnode, op) ?			\
120 	 (fwnode)->ops->op(fwnode, ## __VA_ARGS__) : false)
121 
122 #define fwnode_call_ptr_op(fwnode, op, ...)		\
123 	(fwnode_has_op(fwnode, op) ?			\
124 	 (fwnode)->ops->op(fwnode, ## __VA_ARGS__) : NULL)
125 #define fwnode_call_void_op(fwnode, op, ...)				\
126 	do {								\
127 		if (fwnode_has_op(fwnode, op))				\
128 			(fwnode)->ops->op(fwnode, ## __VA_ARGS__);	\
129 	} while (false)
130 
131 #endif
132