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 <rafael.j.wysocki@intel.com>
7 * Mika Westerberg <mika.westerberg@linux.intel.com>
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/types.h>
16
17 struct device;
18
19 enum dev_prop_type {
20 DEV_PROP_U8,
21 DEV_PROP_U16,
22 DEV_PROP_U32,
23 DEV_PROP_U64,
24 DEV_PROP_STRING,
25 DEV_PROP_MAX,
26 };
27
28 enum dev_dma_attr {
29 DEV_DMA_NOT_SUPPORTED,
30 DEV_DMA_NON_COHERENT,
31 DEV_DMA_COHERENT,
32 };
33
34 struct fwnode_handle *dev_fwnode(struct device *dev);
35
36 bool device_property_present(struct device *dev, const char *propname);
37 int device_property_read_u8_array(struct device *dev, const char *propname,
38 u8 *val, size_t nval);
39 int device_property_read_u16_array(struct device *dev, const char *propname,
40 u16 *val, size_t nval);
41 int device_property_read_u32_array(struct device *dev, const char *propname,
42 u32 *val, size_t nval);
43 int device_property_read_u64_array(struct device *dev, const char *propname,
44 u64 *val, size_t nval);
45 int device_property_read_string_array(struct device *dev, const char *propname,
46 const char **val, size_t nval);
47 int device_property_read_string(struct device *dev, const char *propname,
48 const char **val);
49 int device_property_match_string(struct device *dev,
50 const char *propname, const char *string);
51
52 bool fwnode_device_is_available(const struct fwnode_handle *fwnode);
53 bool fwnode_property_present(const struct fwnode_handle *fwnode,
54 const char *propname);
55 int fwnode_property_read_u8_array(const struct fwnode_handle *fwnode,
56 const char *propname, u8 *val,
57 size_t nval);
58 int fwnode_property_read_u16_array(const struct fwnode_handle *fwnode,
59 const char *propname, u16 *val,
60 size_t nval);
61 int fwnode_property_read_u32_array(const struct fwnode_handle *fwnode,
62 const char *propname, u32 *val,
63 size_t nval);
64 int fwnode_property_read_u64_array(const struct fwnode_handle *fwnode,
65 const char *propname, u64 *val,
66 size_t nval);
67 int fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
68 const char *propname, const char **val,
69 size_t nval);
70 int fwnode_property_read_string(const struct fwnode_handle *fwnode,
71 const char *propname, const char **val);
72 int fwnode_property_match_string(const struct fwnode_handle *fwnode,
73 const char *propname, const char *string);
74 int fwnode_property_get_reference_args(const struct fwnode_handle *fwnode,
75 const char *prop, const char *nargs_prop,
76 unsigned int nargs, unsigned int index,
77 struct fwnode_reference_args *args);
78
79 struct fwnode_handle *fwnode_find_reference(const struct fwnode_handle *fwnode,
80 const char *name,
81 unsigned int index);
82
83 struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode);
84 struct fwnode_handle *fwnode_get_next_parent(
85 struct fwnode_handle *fwnode);
86 struct fwnode_handle *fwnode_get_next_child_node(
87 const struct fwnode_handle *fwnode, struct fwnode_handle *child);
88 struct fwnode_handle *fwnode_get_next_available_child_node(
89 const struct fwnode_handle *fwnode, struct fwnode_handle *child);
90
91 #define fwnode_for_each_child_node(fwnode, child) \
92 for (child = fwnode_get_next_child_node(fwnode, NULL); child; \
93 child = fwnode_get_next_child_node(fwnode, child))
94
95 #define fwnode_for_each_available_child_node(fwnode, child) \
96 for (child = fwnode_get_next_available_child_node(fwnode, NULL); child;\
97 child = fwnode_get_next_available_child_node(fwnode, child))
98
99 struct fwnode_handle *device_get_next_child_node(
100 struct device *dev, struct fwnode_handle *child);
101
102 #define device_for_each_child_node(dev, child) \
103 for (child = device_get_next_child_node(dev, NULL); child; \
104 child = device_get_next_child_node(dev, child))
105
106 struct fwnode_handle *fwnode_get_named_child_node(
107 const struct fwnode_handle *fwnode, const char *childname);
108 struct fwnode_handle *device_get_named_child_node(struct device *dev,
109 const char *childname);
110
111 struct fwnode_handle *fwnode_handle_get(struct fwnode_handle *fwnode);
112 void fwnode_handle_put(struct fwnode_handle *fwnode);
113
114 int fwnode_irq_get(struct fwnode_handle *fwnode, unsigned int index);
115
116 unsigned int device_get_child_node_count(struct device *dev);
117
device_property_read_bool(struct device * dev,const char * propname)118 static inline bool device_property_read_bool(struct device *dev,
119 const char *propname)
120 {
121 return device_property_present(dev, propname);
122 }
123
device_property_read_u8(struct device * dev,const char * propname,u8 * val)124 static inline int device_property_read_u8(struct device *dev,
125 const char *propname, u8 *val)
126 {
127 return device_property_read_u8_array(dev, propname, val, 1);
128 }
129
device_property_read_u16(struct device * dev,const char * propname,u16 * val)130 static inline int device_property_read_u16(struct device *dev,
131 const char *propname, u16 *val)
132 {
133 return device_property_read_u16_array(dev, propname, val, 1);
134 }
135
device_property_read_u32(struct device * dev,const char * propname,u32 * val)136 static inline int device_property_read_u32(struct device *dev,
137 const char *propname, u32 *val)
138 {
139 return device_property_read_u32_array(dev, propname, val, 1);
140 }
141
device_property_read_u64(struct device * dev,const char * propname,u64 * val)142 static inline int device_property_read_u64(struct device *dev,
143 const char *propname, u64 *val)
144 {
145 return device_property_read_u64_array(dev, propname, val, 1);
146 }
147
device_property_count_u8(struct device * dev,const char * propname)148 static inline int device_property_count_u8(struct device *dev, const char *propname)
149 {
150 return device_property_read_u8_array(dev, propname, NULL, 0);
151 }
152
device_property_count_u16(struct device * dev,const char * propname)153 static inline int device_property_count_u16(struct device *dev, const char *propname)
154 {
155 return device_property_read_u16_array(dev, propname, NULL, 0);
156 }
157
device_property_count_u32(struct device * dev,const char * propname)158 static inline int device_property_count_u32(struct device *dev, const char *propname)
159 {
160 return device_property_read_u32_array(dev, propname, NULL, 0);
161 }
162
device_property_count_u64(struct device * dev,const char * propname)163 static inline int device_property_count_u64(struct device *dev, const char *propname)
164 {
165 return device_property_read_u64_array(dev, propname, NULL, 0);
166 }
167
fwnode_property_read_bool(const struct fwnode_handle * fwnode,const char * propname)168 static inline bool fwnode_property_read_bool(const struct fwnode_handle *fwnode,
169 const char *propname)
170 {
171 return fwnode_property_present(fwnode, propname);
172 }
173
fwnode_property_read_u8(const struct fwnode_handle * fwnode,const char * propname,u8 * val)174 static inline int fwnode_property_read_u8(const struct fwnode_handle *fwnode,
175 const char *propname, u8 *val)
176 {
177 return fwnode_property_read_u8_array(fwnode, propname, val, 1);
178 }
179
fwnode_property_read_u16(const struct fwnode_handle * fwnode,const char * propname,u16 * val)180 static inline int fwnode_property_read_u16(const struct fwnode_handle *fwnode,
181 const char *propname, u16 *val)
182 {
183 return fwnode_property_read_u16_array(fwnode, propname, val, 1);
184 }
185
fwnode_property_read_u32(const struct fwnode_handle * fwnode,const char * propname,u32 * val)186 static inline int fwnode_property_read_u32(const struct fwnode_handle *fwnode,
187 const char *propname, u32 *val)
188 {
189 return fwnode_property_read_u32_array(fwnode, propname, val, 1);
190 }
191
fwnode_property_read_u64(const struct fwnode_handle * fwnode,const char * propname,u64 * val)192 static inline int fwnode_property_read_u64(const struct fwnode_handle *fwnode,
193 const char *propname, u64 *val)
194 {
195 return fwnode_property_read_u64_array(fwnode, propname, val, 1);
196 }
197
fwnode_property_count_u8(const struct fwnode_handle * fwnode,const char * propname)198 static inline int fwnode_property_count_u8(const struct fwnode_handle *fwnode,
199 const char *propname)
200 {
201 return fwnode_property_read_u8_array(fwnode, propname, NULL, 0);
202 }
203
fwnode_property_count_u16(const struct fwnode_handle * fwnode,const char * propname)204 static inline int fwnode_property_count_u16(const struct fwnode_handle *fwnode,
205 const char *propname)
206 {
207 return fwnode_property_read_u16_array(fwnode, propname, NULL, 0);
208 }
209
fwnode_property_count_u32(const struct fwnode_handle * fwnode,const char * propname)210 static inline int fwnode_property_count_u32(const struct fwnode_handle *fwnode,
211 const char *propname)
212 {
213 return fwnode_property_read_u32_array(fwnode, propname, NULL, 0);
214 }
215
fwnode_property_count_u64(const struct fwnode_handle * fwnode,const char * propname)216 static inline int fwnode_property_count_u64(const struct fwnode_handle *fwnode,
217 const char *propname)
218 {
219 return fwnode_property_read_u64_array(fwnode, propname, NULL, 0);
220 }
221
222 /**
223 * struct property_entry - "Built-in" device property representation.
224 * @name: Name of the property.
225 * @length: Length of data making up the value.
226 * @is_array: True when the property is an array.
227 * @type: Type of the data in unions.
228 * @pointer: Pointer to the property (an array of items of the given type).
229 * @value: Value of the property (when it is a single item of the given type).
230 */
231 struct property_entry {
232 const char *name;
233 size_t length;
234 bool is_array;
235 enum dev_prop_type type;
236 union {
237 union {
238 const u8 *u8_data;
239 const u16 *u16_data;
240 const u32 *u32_data;
241 const u64 *u64_data;
242 const char * const *str;
243 } pointer;
244 union {
245 u8 u8_data;
246 u16 u16_data;
247 u32 u32_data;
248 u64 u64_data;
249 const char *str;
250 } value;
251 };
252 };
253
254 /*
255 * Note: the below four initializers for the anonymous union are carefully
256 * crafted to avoid gcc-4.4.4's problems with initialization of anon unions
257 * and structs.
258 */
259
260 #define PROPERTY_ENTRY_INTEGER_ARRAY(_name_, _type_, _Type_, _val_) \
261 (struct property_entry) { \
262 .name = _name_, \
263 .length = ARRAY_SIZE(_val_) * sizeof(_type_), \
264 .is_array = true, \
265 .type = DEV_PROP_##_Type_, \
266 { .pointer = { ._type_##_data = _val_ } }, \
267 }
268
269 #define PROPERTY_ENTRY_U8_ARRAY(_name_, _val_) \
270 PROPERTY_ENTRY_INTEGER_ARRAY(_name_, u8, U8, _val_)
271 #define PROPERTY_ENTRY_U16_ARRAY(_name_, _val_) \
272 PROPERTY_ENTRY_INTEGER_ARRAY(_name_, u16, U16, _val_)
273 #define PROPERTY_ENTRY_U32_ARRAY(_name_, _val_) \
274 PROPERTY_ENTRY_INTEGER_ARRAY(_name_, u32, U32, _val_)
275 #define PROPERTY_ENTRY_U64_ARRAY(_name_, _val_) \
276 PROPERTY_ENTRY_INTEGER_ARRAY(_name_, u64, U64, _val_)
277
278 #define PROPERTY_ENTRY_STRING_ARRAY(_name_, _val_) \
279 (struct property_entry) { \
280 .name = _name_, \
281 .length = ARRAY_SIZE(_val_) * sizeof(const char *), \
282 .is_array = true, \
283 .type = DEV_PROP_STRING, \
284 { .pointer = { .str = _val_ } }, \
285 }
286
287 #define PROPERTY_ENTRY_INTEGER(_name_, _type_, _Type_, _val_) \
288 (struct property_entry) { \
289 .name = _name_, \
290 .length = sizeof(_type_), \
291 .type = DEV_PROP_##_Type_, \
292 { .value = { ._type_##_data = _val_ } }, \
293 }
294
295 #define PROPERTY_ENTRY_U8(_name_, _val_) \
296 PROPERTY_ENTRY_INTEGER(_name_, u8, U8, _val_)
297 #define PROPERTY_ENTRY_U16(_name_, _val_) \
298 PROPERTY_ENTRY_INTEGER(_name_, u16, U16, _val_)
299 #define PROPERTY_ENTRY_U32(_name_, _val_) \
300 PROPERTY_ENTRY_INTEGER(_name_, u32, U32, _val_)
301 #define PROPERTY_ENTRY_U64(_name_, _val_) \
302 PROPERTY_ENTRY_INTEGER(_name_, u64, U64, _val_)
303
304 #define PROPERTY_ENTRY_STRING(_name_, _val_) \
305 (struct property_entry) { \
306 .name = _name_, \
307 .length = sizeof(const char *), \
308 .type = DEV_PROP_STRING, \
309 { .value = { .str = _val_ } }, \
310 }
311
312 #define PROPERTY_ENTRY_BOOL(_name_) \
313 (struct property_entry) { \
314 .name = _name_, \
315 }
316
317 struct property_entry *
318 property_entries_dup(const struct property_entry *properties);
319
320 void property_entries_free(const struct property_entry *properties);
321
322 int device_add_properties(struct device *dev,
323 const struct property_entry *properties);
324 void device_remove_properties(struct device *dev);
325
326 bool device_dma_supported(struct device *dev);
327
328 enum dev_dma_attr device_get_dma_attr(struct device *dev);
329
330 const void *device_get_match_data(struct device *dev);
331
332 int device_get_phy_mode(struct device *dev);
333
334 void *device_get_mac_address(struct device *dev, char *addr, int alen);
335
336 int fwnode_get_phy_mode(struct fwnode_handle *fwnode);
337 void *fwnode_get_mac_address(struct fwnode_handle *fwnode,
338 char *addr, int alen);
339 struct fwnode_handle *fwnode_graph_get_next_endpoint(
340 const struct fwnode_handle *fwnode, struct fwnode_handle *prev);
341 struct fwnode_handle *
342 fwnode_graph_get_port_parent(const struct fwnode_handle *fwnode);
343 struct fwnode_handle *fwnode_graph_get_remote_port_parent(
344 const struct fwnode_handle *fwnode);
345 struct fwnode_handle *fwnode_graph_get_remote_port(
346 const struct fwnode_handle *fwnode);
347 struct fwnode_handle *fwnode_graph_get_remote_endpoint(
348 const struct fwnode_handle *fwnode);
349 struct fwnode_handle *
350 fwnode_graph_get_remote_node(const struct fwnode_handle *fwnode, u32 port,
351 u32 endpoint);
352
353 /*
354 * Fwnode lookup flags
355 *
356 * @FWNODE_GRAPH_ENDPOINT_NEXT: In the case of no exact match, look for the
357 * closest endpoint ID greater than the specified
358 * one.
359 * @FWNODE_GRAPH_DEVICE_DISABLED: That the device to which the remote
360 * endpoint of the given endpoint belongs to,
361 * may be disabled.
362 */
363 #define FWNODE_GRAPH_ENDPOINT_NEXT BIT(0)
364 #define FWNODE_GRAPH_DEVICE_DISABLED BIT(1)
365
366 struct fwnode_handle *
367 fwnode_graph_get_endpoint_by_id(const struct fwnode_handle *fwnode,
368 u32 port, u32 endpoint, unsigned long flags);
369
370 #define fwnode_graph_for_each_endpoint(fwnode, child) \
371 for (child = NULL; \
372 (child = fwnode_graph_get_next_endpoint(fwnode, child)); )
373
374 int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
375 struct fwnode_endpoint *endpoint);
376
377 /* -------------------------------------------------------------------------- */
378 /* Software fwnode support - when HW description is incomplete or missing */
379
380 struct software_node;
381
382 /**
383 * struct software_node_ref_args - Reference with additional arguments
384 * @node: Reference to a software node
385 * @nargs: Number of elements in @args array
386 * @args: Integer arguments
387 */
388 struct software_node_ref_args {
389 const struct software_node *node;
390 unsigned int nargs;
391 u64 args[NR_FWNODE_REFERENCE_ARGS];
392 };
393
394 /**
395 * struct software_node_reference - Named software node reference property
396 * @name: Name of the property
397 * @nrefs: Number of elements in @refs array
398 * @refs: Array of references with optional arguments
399 */
400 struct software_node_reference {
401 const char *name;
402 unsigned int nrefs;
403 const struct software_node_ref_args *refs;
404 };
405
406 /**
407 * struct software_node - Software node description
408 * @name: Name of the software node
409 * @parent: Parent of the software node
410 * @properties: Array of device properties
411 * @references: Array of software node reference properties
412 */
413 struct software_node {
414 const char *name;
415 const struct software_node *parent;
416 const struct property_entry *properties;
417 const struct software_node_reference *references;
418 };
419
420 bool is_software_node(const struct fwnode_handle *fwnode);
421 const struct software_node *to_software_node(struct fwnode_handle *fwnode);
422 struct fwnode_handle *software_node_fwnode(const struct software_node *node);
423
424 const struct software_node *
425 software_node_find_by_name(const struct software_node *parent,
426 const char *name);
427
428 int software_node_register_nodes(const struct software_node *nodes);
429 void software_node_unregister_nodes(const struct software_node *nodes);
430
431 int software_node_register(const struct software_node *node);
432
433 int software_node_notify(struct device *dev, unsigned long action);
434
435 struct fwnode_handle *
436 fwnode_create_software_node(const struct property_entry *properties,
437 const struct fwnode_handle *parent);
438 void fwnode_remove_software_node(struct fwnode_handle *fwnode);
439
440 #endif /* _LINUX_PROPERTY_H_ */
441