1 /*
2  * property.h - Unified device property interface.
3  *
4  * Copyright (C) 2014, Intel Corporation
5  * Authors: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
6  *          Mika Westerberg <mika.westerberg@linux.intel.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12 
13 #ifndef _LINUX_PROPERTY_H_
14 #define _LINUX_PROPERTY_H_
15 
16 #include <linux/fwnode.h>
17 #include <linux/types.h>
18 
19 struct device;
20 
21 enum dev_prop_type {
22 	DEV_PROP_U8,
23 	DEV_PROP_U16,
24 	DEV_PROP_U32,
25 	DEV_PROP_U64,
26 	DEV_PROP_STRING,
27 	DEV_PROP_MAX,
28 };
29 
30 enum dev_dma_attr {
31 	DEV_DMA_NOT_SUPPORTED,
32 	DEV_DMA_NON_COHERENT,
33 	DEV_DMA_COHERENT,
34 };
35 
36 struct fwnode_handle *dev_fwnode(struct device *dev);
37 
38 bool device_property_present(struct device *dev, const char *propname);
39 int device_property_read_u8_array(struct device *dev, const char *propname,
40 				  u8 *val, size_t nval);
41 int device_property_read_u16_array(struct device *dev, const char *propname,
42 				   u16 *val, size_t nval);
43 int device_property_read_u32_array(struct device *dev, const char *propname,
44 				   u32 *val, size_t nval);
45 int device_property_read_u64_array(struct device *dev, const char *propname,
46 				   u64 *val, size_t nval);
47 int device_property_read_string_array(struct device *dev, const char *propname,
48 				      const char **val, size_t nval);
49 int device_property_read_string(struct device *dev, const char *propname,
50 				const char **val);
51 int device_property_match_string(struct device *dev,
52 				 const char *propname, const char *string);
53 
54 bool fwnode_device_is_available(const struct fwnode_handle *fwnode);
55 bool fwnode_property_present(const struct fwnode_handle *fwnode,
56 			     const char *propname);
57 int fwnode_property_read_u8_array(const struct fwnode_handle *fwnode,
58 				  const char *propname, u8 *val,
59 				  size_t nval);
60 int fwnode_property_read_u16_array(const struct fwnode_handle *fwnode,
61 				   const char *propname, u16 *val,
62 				   size_t nval);
63 int fwnode_property_read_u32_array(const struct fwnode_handle *fwnode,
64 				   const char *propname, u32 *val,
65 				   size_t nval);
66 int fwnode_property_read_u64_array(const struct fwnode_handle *fwnode,
67 				   const char *propname, u64 *val,
68 				   size_t nval);
69 int fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
70 				      const char *propname, const char **val,
71 				      size_t nval);
72 int fwnode_property_read_string(const struct fwnode_handle *fwnode,
73 				const char *propname, const char **val);
74 int fwnode_property_match_string(const struct fwnode_handle *fwnode,
75 				 const char *propname, const char *string);
76 int fwnode_property_get_reference_args(const struct fwnode_handle *fwnode,
77 				       const char *prop, const char *nargs_prop,
78 				       unsigned int nargs, unsigned int index,
79 				       struct fwnode_reference_args *args);
80 
81 struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode);
82 struct fwnode_handle *fwnode_get_next_parent(
83 	struct fwnode_handle *fwnode);
84 struct fwnode_handle *fwnode_get_next_child_node(
85 	const struct fwnode_handle *fwnode, struct fwnode_handle *child);
86 struct fwnode_handle *fwnode_get_next_available_child_node(
87 	const struct fwnode_handle *fwnode, struct fwnode_handle *child);
88 
89 #define fwnode_for_each_child_node(fwnode, child)			\
90 	for (child = fwnode_get_next_child_node(fwnode, NULL); child;	\
91 	     child = fwnode_get_next_child_node(fwnode, child))
92 
93 #define fwnode_for_each_available_child_node(fwnode, child)		       \
94 	for (child = fwnode_get_next_available_child_node(fwnode, NULL); child;\
95 	     child = fwnode_get_next_available_child_node(fwnode, child))
96 
97 struct fwnode_handle *device_get_next_child_node(
98 	struct device *dev, struct fwnode_handle *child);
99 
100 #define device_for_each_child_node(dev, child)				\
101 	for (child = device_get_next_child_node(dev, NULL); child;	\
102 	     child = device_get_next_child_node(dev, child))
103 
104 struct fwnode_handle *fwnode_get_named_child_node(
105 	const struct fwnode_handle *fwnode, const char *childname);
106 struct fwnode_handle *device_get_named_child_node(struct device *dev,
107 						  const char *childname);
108 
109 struct fwnode_handle *fwnode_handle_get(struct fwnode_handle *fwnode);
110 void fwnode_handle_put(struct fwnode_handle *fwnode);
111 
112 int fwnode_irq_get(struct fwnode_handle *fwnode, unsigned int index);
113 
114 unsigned int device_get_child_node_count(struct device *dev);
115 
device_property_read_bool(struct device * dev,const char * propname)116 static inline bool device_property_read_bool(struct device *dev,
117 					     const char *propname)
118 {
119 	return device_property_present(dev, propname);
120 }
121 
device_property_read_u8(struct device * dev,const char * propname,u8 * val)122 static inline int device_property_read_u8(struct device *dev,
123 					  const char *propname, u8 *val)
124 {
125 	return device_property_read_u8_array(dev, propname, val, 1);
126 }
127 
device_property_read_u16(struct device * dev,const char * propname,u16 * val)128 static inline int device_property_read_u16(struct device *dev,
129 					   const char *propname, u16 *val)
130 {
131 	return device_property_read_u16_array(dev, propname, val, 1);
132 }
133 
device_property_read_u32(struct device * dev,const char * propname,u32 * val)134 static inline int device_property_read_u32(struct device *dev,
135 					   const char *propname, u32 *val)
136 {
137 	return device_property_read_u32_array(dev, propname, val, 1);
138 }
139 
device_property_read_u64(struct device * dev,const char * propname,u64 * val)140 static inline int device_property_read_u64(struct device *dev,
141 					   const char *propname, u64 *val)
142 {
143 	return device_property_read_u64_array(dev, propname, val, 1);
144 }
145 
fwnode_property_read_bool(const struct fwnode_handle * fwnode,const char * propname)146 static inline bool fwnode_property_read_bool(const struct fwnode_handle *fwnode,
147 					     const char *propname)
148 {
149 	return fwnode_property_present(fwnode, propname);
150 }
151 
fwnode_property_read_u8(const struct fwnode_handle * fwnode,const char * propname,u8 * val)152 static inline int fwnode_property_read_u8(const struct fwnode_handle *fwnode,
153 					  const char *propname, u8 *val)
154 {
155 	return fwnode_property_read_u8_array(fwnode, propname, val, 1);
156 }
157 
fwnode_property_read_u16(const struct fwnode_handle * fwnode,const char * propname,u16 * val)158 static inline int fwnode_property_read_u16(const struct fwnode_handle *fwnode,
159 					   const char *propname, u16 *val)
160 {
161 	return fwnode_property_read_u16_array(fwnode, propname, val, 1);
162 }
163 
fwnode_property_read_u32(const struct fwnode_handle * fwnode,const char * propname,u32 * val)164 static inline int fwnode_property_read_u32(const struct fwnode_handle *fwnode,
165 					   const char *propname, u32 *val)
166 {
167 	return fwnode_property_read_u32_array(fwnode, propname, val, 1);
168 }
169 
fwnode_property_read_u64(const struct fwnode_handle * fwnode,const char * propname,u64 * val)170 static inline int fwnode_property_read_u64(const struct fwnode_handle *fwnode,
171 					   const char *propname, u64 *val)
172 {
173 	return fwnode_property_read_u64_array(fwnode, propname, val, 1);
174 }
175 
176 /**
177  * struct property_entry - "Built-in" device property representation.
178  * @name: Name of the property.
179  * @length: Length of data making up the value.
180  * @is_array: True when the property is an array.
181  * @type: Type of the data in unions.
182  * @pointer: Pointer to the property (an array of items of the given type).
183  * @value: Value of the property (when it is a single item of the given type).
184  */
185 struct property_entry {
186 	const char *name;
187 	size_t length;
188 	bool is_array;
189 	enum dev_prop_type type;
190 	union {
191 		union {
192 			const u8 *u8_data;
193 			const u16 *u16_data;
194 			const u32 *u32_data;
195 			const u64 *u64_data;
196 			const char * const *str;
197 		} pointer;
198 		union {
199 			u8 u8_data;
200 			u16 u16_data;
201 			u32 u32_data;
202 			u64 u64_data;
203 			const char *str;
204 		} value;
205 	};
206 };
207 
208 /*
209  * Note: the below four initializers for the anonymous union are carefully
210  * crafted to avoid gcc-4.4.4's problems with initialization of anon unions
211  * and structs.
212  */
213 
214 #define PROPERTY_ENTRY_INTEGER_ARRAY(_name_, _type_, _Type_, _val_)	\
215 (struct property_entry) {						\
216 	.name = _name_,							\
217 	.length = ARRAY_SIZE(_val_) * sizeof(_type_),			\
218 	.is_array = true,						\
219 	.type = DEV_PROP_##_Type_,					\
220 	{ .pointer = { ._type_##_data = _val_ } },			\
221 }
222 
223 #define PROPERTY_ENTRY_U8_ARRAY(_name_, _val_)			\
224 	PROPERTY_ENTRY_INTEGER_ARRAY(_name_, u8, U8, _val_)
225 #define PROPERTY_ENTRY_U16_ARRAY(_name_, _val_)			\
226 	PROPERTY_ENTRY_INTEGER_ARRAY(_name_, u16, U16, _val_)
227 #define PROPERTY_ENTRY_U32_ARRAY(_name_, _val_)			\
228 	PROPERTY_ENTRY_INTEGER_ARRAY(_name_, u32, U32, _val_)
229 #define PROPERTY_ENTRY_U64_ARRAY(_name_, _val_)			\
230 	PROPERTY_ENTRY_INTEGER_ARRAY(_name_, u64, U64, _val_)
231 
232 #define PROPERTY_ENTRY_STRING_ARRAY(_name_, _val_)		\
233 (struct property_entry) {					\
234 	.name = _name_,						\
235 	.length = ARRAY_SIZE(_val_) * sizeof(const char *),	\
236 	.is_array = true,					\
237 	.type = DEV_PROP_STRING,				\
238 	{ .pointer = { .str = _val_ } },			\
239 }
240 
241 #define PROPERTY_ENTRY_INTEGER(_name_, _type_, _Type_, _val_)	\
242 (struct property_entry) {					\
243 	.name = _name_,						\
244 	.length = sizeof(_type_),				\
245 	.type = DEV_PROP_##_Type_,				\
246 	{ .value = { ._type_##_data = _val_ } },		\
247 }
248 
249 #define PROPERTY_ENTRY_U8(_name_, _val_)		\
250 	PROPERTY_ENTRY_INTEGER(_name_, u8, U8, _val_)
251 #define PROPERTY_ENTRY_U16(_name_, _val_)		\
252 	PROPERTY_ENTRY_INTEGER(_name_, u16, U16, _val_)
253 #define PROPERTY_ENTRY_U32(_name_, _val_)		\
254 	PROPERTY_ENTRY_INTEGER(_name_, u32, U32, _val_)
255 #define PROPERTY_ENTRY_U64(_name_, _val_)		\
256 	PROPERTY_ENTRY_INTEGER(_name_, u64, U64, _val_)
257 
258 #define PROPERTY_ENTRY_STRING(_name_, _val_)		\
259 (struct property_entry) {				\
260 	.name = _name_,					\
261 	.length = sizeof(const char *),			\
262 	.type = DEV_PROP_STRING,			\
263 	{ .value = { .str = _val_ } },			\
264 }
265 
266 #define PROPERTY_ENTRY_BOOL(_name_)		\
267 (struct property_entry) {			\
268 	.name = _name_,				\
269 }
270 
271 struct property_entry *
272 property_entries_dup(const struct property_entry *properties);
273 
274 void property_entries_free(const struct property_entry *properties);
275 
276 int device_add_properties(struct device *dev,
277 			  const struct property_entry *properties);
278 void device_remove_properties(struct device *dev);
279 
280 bool device_dma_supported(struct device *dev);
281 
282 enum dev_dma_attr device_get_dma_attr(struct device *dev);
283 
284 const void *device_get_match_data(struct device *dev);
285 
286 int device_get_phy_mode(struct device *dev);
287 
288 void *device_get_mac_address(struct device *dev, char *addr, int alen);
289 
290 int fwnode_get_phy_mode(struct fwnode_handle *fwnode);
291 void *fwnode_get_mac_address(struct fwnode_handle *fwnode,
292 			     char *addr, int alen);
293 struct fwnode_handle *fwnode_graph_get_next_endpoint(
294 	const struct fwnode_handle *fwnode, struct fwnode_handle *prev);
295 struct fwnode_handle *
296 fwnode_graph_get_port_parent(const struct fwnode_handle *fwnode);
297 struct fwnode_handle *fwnode_graph_get_remote_port_parent(
298 	const struct fwnode_handle *fwnode);
299 struct fwnode_handle *fwnode_graph_get_remote_port(
300 	const struct fwnode_handle *fwnode);
301 struct fwnode_handle *fwnode_graph_get_remote_endpoint(
302 	const struct fwnode_handle *fwnode);
303 struct fwnode_handle *
304 fwnode_graph_get_remote_node(const struct fwnode_handle *fwnode, u32 port,
305 			     u32 endpoint);
306 
307 #define fwnode_graph_for_each_endpoint(fwnode, child)			\
308 	for (child = NULL;						\
309 	     (child = fwnode_graph_get_next_endpoint(fwnode, child)); )
310 
311 int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
312 				struct fwnode_endpoint *endpoint);
313 
314 #endif /* _LINUX_PROPERTY_H_ */
315