xref: /OK3568_Linux_fs/kernel/include/linux/fwnode.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-only */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * fwnode.h - Firmware device node object handle type definition.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2015, Intel Corporation
6*4882a593Smuzhiyun  * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #ifndef _LINUX_FWNODE_H_
10*4882a593Smuzhiyun #define _LINUX_FWNODE_H_
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <linux/types.h>
13*4882a593Smuzhiyun #include <linux/list.h>
14*4882a593Smuzhiyun #include <linux/bits.h>
15*4882a593Smuzhiyun #include <linux/err.h>
16*4882a593Smuzhiyun #include <linux/android_kabi.h>
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun struct fwnode_operations;
19*4882a593Smuzhiyun struct device;
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun /*
22*4882a593Smuzhiyun  * fwnode link flags
23*4882a593Smuzhiyun  *
24*4882a593Smuzhiyun  * LINKS_ADDED:	The fwnode has already be parsed to add fwnode links.
25*4882a593Smuzhiyun  * NOT_DEVICE:	The fwnode will never be populated as a struct device.
26*4882a593Smuzhiyun  * INITIALIZED: The hardware corresponding to fwnode has been initialized.
27*4882a593Smuzhiyun  */
28*4882a593Smuzhiyun #define FWNODE_FLAG_LINKS_ADDED		BIT(0)
29*4882a593Smuzhiyun #define FWNODE_FLAG_NOT_DEVICE		BIT(1)
30*4882a593Smuzhiyun #define FWNODE_FLAG_INITIALIZED		BIT(2)
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun struct fwnode_handle {
33*4882a593Smuzhiyun 	struct fwnode_handle *secondary;
34*4882a593Smuzhiyun 	const struct fwnode_operations *ops;
35*4882a593Smuzhiyun 	struct device *dev;
36*4882a593Smuzhiyun 	struct list_head suppliers;
37*4882a593Smuzhiyun 	struct list_head consumers;
38*4882a593Smuzhiyun 	u8 flags;
39*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(1);
40*4882a593Smuzhiyun };
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun struct fwnode_link {
43*4882a593Smuzhiyun 	struct fwnode_handle *supplier;
44*4882a593Smuzhiyun 	struct list_head s_hook;
45*4882a593Smuzhiyun 	struct fwnode_handle *consumer;
46*4882a593Smuzhiyun 	struct list_head c_hook;
47*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(1);
48*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(2);
49*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(3);
50*4882a593Smuzhiyun };
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun /**
53*4882a593Smuzhiyun  * struct fwnode_endpoint - Fwnode graph endpoint
54*4882a593Smuzhiyun  * @port: Port number
55*4882a593Smuzhiyun  * @id: Endpoint id
56*4882a593Smuzhiyun  * @local_fwnode: reference to the related fwnode
57*4882a593Smuzhiyun  */
58*4882a593Smuzhiyun struct fwnode_endpoint {
59*4882a593Smuzhiyun 	unsigned int port;
60*4882a593Smuzhiyun 	unsigned int id;
61*4882a593Smuzhiyun 	const struct fwnode_handle *local_fwnode;
62*4882a593Smuzhiyun };
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun #define NR_FWNODE_REFERENCE_ARGS	8
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun /**
67*4882a593Smuzhiyun  * struct fwnode_reference_args - Fwnode reference with additional arguments
68*4882a593Smuzhiyun  * @fwnode:- A reference to the base fwnode
69*4882a593Smuzhiyun  * @nargs: Number of elements in @args array
70*4882a593Smuzhiyun  * @args: Integer arguments on the fwnode
71*4882a593Smuzhiyun  */
72*4882a593Smuzhiyun struct fwnode_reference_args {
73*4882a593Smuzhiyun 	struct fwnode_handle *fwnode;
74*4882a593Smuzhiyun 	unsigned int nargs;
75*4882a593Smuzhiyun 	u64 args[NR_FWNODE_REFERENCE_ARGS];
76*4882a593Smuzhiyun };
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun /**
79*4882a593Smuzhiyun  * struct fwnode_operations - Operations for fwnode interface
80*4882a593Smuzhiyun  * @get: Get a reference to an fwnode.
81*4882a593Smuzhiyun  * @put: Put a reference to an fwnode.
82*4882a593Smuzhiyun  * @device_is_available: Return true if the device is available.
83*4882a593Smuzhiyun  * @device_get_match_data: Return the device driver match data.
84*4882a593Smuzhiyun  * @property_present: Return true if a property is present.
85*4882a593Smuzhiyun  * @property_read_int_array: Read an array of integer properties. Return zero on
86*4882a593Smuzhiyun  *			     success, a negative error code otherwise.
87*4882a593Smuzhiyun  * @property_read_string_array: Read an array of string properties. Return zero
88*4882a593Smuzhiyun  *				on success, a negative error code otherwise.
89*4882a593Smuzhiyun  * @get_name: Return the name of an fwnode.
90*4882a593Smuzhiyun  * @get_name_prefix: Get a prefix for a node (for printing purposes).
91*4882a593Smuzhiyun  * @get_parent: Return the parent of an fwnode.
92*4882a593Smuzhiyun  * @get_next_child_node: Return the next child node in an iteration.
93*4882a593Smuzhiyun  * @get_named_child_node: Return a child node with a given name.
94*4882a593Smuzhiyun  * @get_reference_args: Return a reference pointed to by a property, with args
95*4882a593Smuzhiyun  * @graph_get_next_endpoint: Return an endpoint node in an iteration.
96*4882a593Smuzhiyun  * @graph_get_remote_endpoint: Return the remote endpoint node of a local
97*4882a593Smuzhiyun  *			       endpoint node.
98*4882a593Smuzhiyun  * @graph_get_port_parent: Return the parent node of a port node.
99*4882a593Smuzhiyun  * @graph_parse_endpoint: Parse endpoint for port and endpoint id.
100*4882a593Smuzhiyun  * @add_links:	Create fwnode links to all the suppliers of the fwnode. Return
101*4882a593Smuzhiyun  *		zero on success, a negative error code otherwise.
102*4882a593Smuzhiyun  */
103*4882a593Smuzhiyun struct fwnode_operations {
104*4882a593Smuzhiyun 	struct fwnode_handle *(*get)(struct fwnode_handle *fwnode);
105*4882a593Smuzhiyun 	void (*put)(struct fwnode_handle *fwnode);
106*4882a593Smuzhiyun 	bool (*device_is_available)(const struct fwnode_handle *fwnode);
107*4882a593Smuzhiyun 	const void *(*device_get_match_data)(const struct fwnode_handle *fwnode,
108*4882a593Smuzhiyun 					     const struct device *dev);
109*4882a593Smuzhiyun 	bool (*property_present)(const struct fwnode_handle *fwnode,
110*4882a593Smuzhiyun 				 const char *propname);
111*4882a593Smuzhiyun 	int (*property_read_int_array)(const struct fwnode_handle *fwnode,
112*4882a593Smuzhiyun 				       const char *propname,
113*4882a593Smuzhiyun 				       unsigned int elem_size, void *val,
114*4882a593Smuzhiyun 				       size_t nval);
115*4882a593Smuzhiyun 	int
116*4882a593Smuzhiyun 	(*property_read_string_array)(const struct fwnode_handle *fwnode_handle,
117*4882a593Smuzhiyun 				      const char *propname, const char **val,
118*4882a593Smuzhiyun 				      size_t nval);
119*4882a593Smuzhiyun 	const char *(*get_name)(const struct fwnode_handle *fwnode);
120*4882a593Smuzhiyun 	const char *(*get_name_prefix)(const struct fwnode_handle *fwnode);
121*4882a593Smuzhiyun 	struct fwnode_handle *(*get_parent)(const struct fwnode_handle *fwnode);
122*4882a593Smuzhiyun 	struct fwnode_handle *
123*4882a593Smuzhiyun 	(*get_next_child_node)(const struct fwnode_handle *fwnode,
124*4882a593Smuzhiyun 			       struct fwnode_handle *child);
125*4882a593Smuzhiyun 	struct fwnode_handle *
126*4882a593Smuzhiyun 	(*get_named_child_node)(const struct fwnode_handle *fwnode,
127*4882a593Smuzhiyun 				const char *name);
128*4882a593Smuzhiyun 	int (*get_reference_args)(const struct fwnode_handle *fwnode,
129*4882a593Smuzhiyun 				  const char *prop, const char *nargs_prop,
130*4882a593Smuzhiyun 				  unsigned int nargs, unsigned int index,
131*4882a593Smuzhiyun 				  struct fwnode_reference_args *args);
132*4882a593Smuzhiyun 	struct fwnode_handle *
133*4882a593Smuzhiyun 	(*graph_get_next_endpoint)(const struct fwnode_handle *fwnode,
134*4882a593Smuzhiyun 				   struct fwnode_handle *prev);
135*4882a593Smuzhiyun 	struct fwnode_handle *
136*4882a593Smuzhiyun 	(*graph_get_remote_endpoint)(const struct fwnode_handle *fwnode);
137*4882a593Smuzhiyun 	struct fwnode_handle *
138*4882a593Smuzhiyun 	(*graph_get_port_parent)(struct fwnode_handle *fwnode);
139*4882a593Smuzhiyun 	int (*graph_parse_endpoint)(const struct fwnode_handle *fwnode,
140*4882a593Smuzhiyun 				    struct fwnode_endpoint *endpoint);
141*4882a593Smuzhiyun 	int (*add_links)(struct fwnode_handle *fwnode);
142*4882a593Smuzhiyun };
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun #define fwnode_has_op(fwnode, op)				\
145*4882a593Smuzhiyun 	((fwnode) && (fwnode)->ops && (fwnode)->ops->op)
146*4882a593Smuzhiyun #define fwnode_call_int_op(fwnode, op, ...)				\
147*4882a593Smuzhiyun 	(fwnode ? (fwnode_has_op(fwnode, op) ?				\
148*4882a593Smuzhiyun 		   (fwnode)->ops->op(fwnode, ## __VA_ARGS__) : -ENXIO) : \
149*4882a593Smuzhiyun 	 -EINVAL)
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun #define fwnode_call_bool_op(fwnode, op, ...)		\
152*4882a593Smuzhiyun 	(fwnode_has_op(fwnode, op) ?			\
153*4882a593Smuzhiyun 	 (fwnode)->ops->op(fwnode, ## __VA_ARGS__) : false)
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun #define fwnode_call_ptr_op(fwnode, op, ...)		\
156*4882a593Smuzhiyun 	(fwnode_has_op(fwnode, op) ?			\
157*4882a593Smuzhiyun 	 (fwnode)->ops->op(fwnode, ## __VA_ARGS__) : NULL)
158*4882a593Smuzhiyun #define fwnode_call_void_op(fwnode, op, ...)				\
159*4882a593Smuzhiyun 	do {								\
160*4882a593Smuzhiyun 		if (fwnode_has_op(fwnode, op))				\
161*4882a593Smuzhiyun 			(fwnode)->ops->op(fwnode, ## __VA_ARGS__);	\
162*4882a593Smuzhiyun 	} while (false)
163*4882a593Smuzhiyun #define get_dev_from_fwnode(fwnode)	get_device((fwnode)->dev)
164*4882a593Smuzhiyun 
fwnode_init(struct fwnode_handle * fwnode,const struct fwnode_operations * ops)165*4882a593Smuzhiyun static inline void fwnode_init(struct fwnode_handle *fwnode,
166*4882a593Smuzhiyun 			       const struct fwnode_operations *ops)
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun 	fwnode->ops = ops;
169*4882a593Smuzhiyun 	INIT_LIST_HEAD(&fwnode->consumers);
170*4882a593Smuzhiyun 	INIT_LIST_HEAD(&fwnode->suppliers);
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun 
fwnode_dev_initialized(struct fwnode_handle * fwnode,bool initialized)173*4882a593Smuzhiyun static inline void fwnode_dev_initialized(struct fwnode_handle *fwnode,
174*4882a593Smuzhiyun 					  bool initialized)
175*4882a593Smuzhiyun {
176*4882a593Smuzhiyun 	if (IS_ERR_OR_NULL(fwnode))
177*4882a593Smuzhiyun 		return;
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	if (initialized)
180*4882a593Smuzhiyun 		fwnode->flags |= FWNODE_FLAG_INITIALIZED;
181*4882a593Smuzhiyun 	else
182*4882a593Smuzhiyun 		fwnode->flags &= ~FWNODE_FLAG_INITIALIZED;
183*4882a593Smuzhiyun }
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun extern u32 fw_devlink_get_flags(void);
186*4882a593Smuzhiyun extern bool fw_devlink_is_strict(void);
187*4882a593Smuzhiyun int fwnode_link_add(struct fwnode_handle *con, struct fwnode_handle *sup);
188*4882a593Smuzhiyun void fwnode_links_purge(struct fwnode_handle *fwnode);
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun #endif
191