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