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