]>
Commit | Line | Data |
---|---|---|
b31384fa RW |
1 | /* |
2 | * property.c - 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 | ||
b31384fa | 13 | #include <linux/acpi.h> |
16ba08d5 RW |
14 | #include <linux/export.h> |
15 | #include <linux/kernel.h> | |
b31384fa | 16 | #include <linux/of.h> |
05ca5560 | 17 | #include <linux/of_address.h> |
07bb80d4 | 18 | #include <linux/of_graph.h> |
16ba08d5 | 19 | #include <linux/property.h> |
4c96b7dc JL |
20 | #include <linux/etherdevice.h> |
21 | #include <linux/phy.h> | |
16ba08d5 | 22 | |
f4d05266 HK |
23 | struct property_set { |
24 | struct fwnode_handle fwnode; | |
bec84da8 | 25 | const struct property_entry *properties; |
f4d05266 HK |
26 | }; |
27 | ||
61f5e294 | 28 | static inline bool is_pset_node(struct fwnode_handle *fwnode) |
16ba08d5 | 29 | { |
0224a4a3 | 30 | return !IS_ERR_OR_NULL(fwnode) && fwnode->type == FWNODE_PDATA; |
16ba08d5 RW |
31 | } |
32 | ||
61f5e294 | 33 | static inline struct property_set *to_pset_node(struct fwnode_handle *fwnode) |
16ba08d5 | 34 | { |
61f5e294 | 35 | return is_pset_node(fwnode) ? |
16ba08d5 RW |
36 | container_of(fwnode, struct property_set, fwnode) : NULL; |
37 | } | |
38 | ||
bec84da8 DT |
39 | static const struct property_entry *pset_prop_get(struct property_set *pset, |
40 | const char *name) | |
16ba08d5 | 41 | { |
bec84da8 | 42 | const struct property_entry *prop; |
16ba08d5 RW |
43 | |
44 | if (!pset || !pset->properties) | |
45 | return NULL; | |
46 | ||
47 | for (prop = pset->properties; prop->name; prop++) | |
48 | if (!strcmp(name, prop->name)) | |
49 | return prop; | |
50 | ||
51 | return NULL; | |
52 | } | |
53 | ||
bec84da8 DT |
54 | static const void *pset_prop_find(struct property_set *pset, |
55 | const char *propname, size_t length) | |
16ba08d5 | 56 | { |
bec84da8 DT |
57 | const struct property_entry *prop; |
58 | const void *pointer; | |
16ba08d5 | 59 | |
318a1971 AS |
60 | prop = pset_prop_get(pset, propname); |
61 | if (!prop) | |
62 | return ERR_PTR(-EINVAL); | |
66586bab AS |
63 | if (prop->is_array) |
64 | pointer = prop->pointer.raw_data; | |
65 | else | |
66 | pointer = &prop->value.raw_data; | |
318a1971 AS |
67 | if (!pointer) |
68 | return ERR_PTR(-ENODATA); | |
69 | if (length > prop->length) | |
70 | return ERR_PTR(-EOVERFLOW); | |
71 | return pointer; | |
72 | } | |
73 | ||
74 | static int pset_prop_read_u8_array(struct property_set *pset, | |
75 | const char *propname, | |
76 | u8 *values, size_t nval) | |
77 | { | |
bec84da8 | 78 | const void *pointer; |
318a1971 AS |
79 | size_t length = nval * sizeof(*values); |
80 | ||
81 | pointer = pset_prop_find(pset, propname, length); | |
82 | if (IS_ERR(pointer)) | |
83 | return PTR_ERR(pointer); | |
84 | ||
85 | memcpy(values, pointer, length); | |
86 | return 0; | |
87 | } | |
88 | ||
89 | static int pset_prop_read_u16_array(struct property_set *pset, | |
90 | const char *propname, | |
91 | u16 *values, size_t nval) | |
92 | { | |
bec84da8 | 93 | const void *pointer; |
318a1971 AS |
94 | size_t length = nval * sizeof(*values); |
95 | ||
96 | pointer = pset_prop_find(pset, propname, length); | |
97 | if (IS_ERR(pointer)) | |
98 | return PTR_ERR(pointer); | |
99 | ||
100 | memcpy(values, pointer, length); | |
101 | return 0; | |
102 | } | |
103 | ||
104 | static int pset_prop_read_u32_array(struct property_set *pset, | |
105 | const char *propname, | |
106 | u32 *values, size_t nval) | |
107 | { | |
bec84da8 | 108 | const void *pointer; |
318a1971 AS |
109 | size_t length = nval * sizeof(*values); |
110 | ||
111 | pointer = pset_prop_find(pset, propname, length); | |
112 | if (IS_ERR(pointer)) | |
113 | return PTR_ERR(pointer); | |
114 | ||
115 | memcpy(values, pointer, length); | |
116 | return 0; | |
117 | } | |
118 | ||
119 | static int pset_prop_read_u64_array(struct property_set *pset, | |
120 | const char *propname, | |
121 | u64 *values, size_t nval) | |
122 | { | |
bec84da8 | 123 | const void *pointer; |
318a1971 AS |
124 | size_t length = nval * sizeof(*values); |
125 | ||
126 | pointer = pset_prop_find(pset, propname, length); | |
127 | if (IS_ERR(pointer)) | |
128 | return PTR_ERR(pointer); | |
129 | ||
130 | memcpy(values, pointer, length); | |
131 | return 0; | |
132 | } | |
133 | ||
134 | static int pset_prop_count_elems_of_size(struct property_set *pset, | |
135 | const char *propname, size_t length) | |
136 | { | |
bec84da8 | 137 | const struct property_entry *prop; |
318a1971 AS |
138 | |
139 | prop = pset_prop_get(pset, propname); | |
16ba08d5 | 140 | if (!prop) |
16ba08d5 | 141 | return -EINVAL; |
318a1971 AS |
142 | |
143 | return prop->length / length; | |
144 | } | |
145 | ||
146 | static int pset_prop_read_string_array(struct property_set *pset, | |
147 | const char *propname, | |
148 | const char **strings, size_t nval) | |
149 | { | |
0f194992 | 150 | const struct property_entry *prop; |
bec84da8 | 151 | const void *pointer; |
0f194992 SA |
152 | size_t array_len, length; |
153 | ||
154 | /* Find out the array length. */ | |
155 | prop = pset_prop_get(pset, propname); | |
156 | if (!prop) | |
157 | return -EINVAL; | |
158 | ||
159 | if (!prop->is_array) | |
160 | /* The array length for a non-array string property is 1. */ | |
161 | array_len = 1; | |
162 | else | |
163 | /* Find the length of an array. */ | |
164 | array_len = pset_prop_count_elems_of_size(pset, propname, | |
165 | sizeof(const char *)); | |
166 | ||
167 | /* Return how many there are if strings is NULL. */ | |
168 | if (!strings) | |
169 | return array_len; | |
170 | ||
171 | array_len = min(nval, array_len); | |
172 | length = array_len * sizeof(*strings); | |
318a1971 AS |
173 | |
174 | pointer = pset_prop_find(pset, propname, length); | |
175 | if (IS_ERR(pointer)) | |
176 | return PTR_ERR(pointer); | |
177 | ||
178 | memcpy(strings, pointer, length); | |
0f194992 | 179 | |
b0b027ce | 180 | return array_len; |
16ba08d5 | 181 | } |
b31384fa | 182 | |
e44bb0cb | 183 | struct fwnode_handle *dev_fwnode(struct device *dev) |
9017f252 RW |
184 | { |
185 | return IS_ENABLED(CONFIG_OF) && dev->of_node ? | |
186 | &dev->of_node->fwnode : dev->fwnode; | |
187 | } | |
e44bb0cb | 188 | EXPORT_SYMBOL_GPL(dev_fwnode); |
b31384fa | 189 | |
3708184a SA |
190 | static bool pset_fwnode_property_present(struct fwnode_handle *fwnode, |
191 | const char *propname) | |
192 | { | |
193 | return !!pset_prop_get(to_pset_node(fwnode), propname); | |
194 | } | |
195 | ||
196 | static int pset_fwnode_read_int_array(struct fwnode_handle *fwnode, | |
197 | const char *propname, | |
198 | unsigned int elem_size, void *val, | |
199 | size_t nval) | |
200 | { | |
201 | struct property_set *node = to_pset_node(fwnode); | |
202 | ||
203 | if (!val) | |
204 | return pset_prop_count_elems_of_size(node, propname, elem_size); | |
205 | ||
206 | switch (elem_size) { | |
207 | case sizeof(u8): | |
208 | return pset_prop_read_u8_array(node, propname, val, nval); | |
209 | case sizeof(u16): | |
210 | return pset_prop_read_u16_array(node, propname, val, nval); | |
211 | case sizeof(u32): | |
212 | return pset_prop_read_u32_array(node, propname, val, nval); | |
213 | case sizeof(u64): | |
214 | return pset_prop_read_u64_array(node, propname, val, nval); | |
215 | } | |
216 | ||
217 | return -ENXIO; | |
218 | } | |
219 | ||
220 | static int pset_fwnode_property_read_string_array(struct fwnode_handle *fwnode, | |
221 | const char *propname, | |
222 | const char **val, size_t nval) | |
223 | { | |
224 | return pset_prop_read_string_array(to_pset_node(fwnode), propname, | |
225 | val, nval); | |
226 | } | |
227 | ||
228 | static const struct fwnode_operations pset_fwnode_ops = { | |
229 | .property_present = pset_fwnode_property_present, | |
230 | .property_read_int_array = pset_fwnode_read_int_array, | |
231 | .property_read_string_array = pset_fwnode_property_read_string_array, | |
232 | }; | |
233 | ||
b31384fa RW |
234 | /** |
235 | * device_property_present - check if a property of a device is present | |
236 | * @dev: Device whose property is being checked | |
237 | * @propname: Name of the property | |
238 | * | |
239 | * Check if property @propname is present in the device firmware description. | |
240 | */ | |
241 | bool device_property_present(struct device *dev, const char *propname) | |
242 | { | |
9017f252 | 243 | return fwnode_property_present(dev_fwnode(dev), propname); |
b31384fa RW |
244 | } |
245 | EXPORT_SYMBOL_GPL(device_property_present); | |
246 | ||
362c0b30 AS |
247 | /** |
248 | * fwnode_property_present - check if a property of a firmware node is present | |
249 | * @fwnode: Firmware node whose property to check | |
250 | * @propname: Name of the property | |
251 | */ | |
252 | bool fwnode_property_present(struct fwnode_handle *fwnode, const char *propname) | |
253 | { | |
254 | bool ret; | |
255 | ||
3708184a | 256 | ret = fwnode_call_int_op(fwnode, property_present, propname); |
0d67e0fa HK |
257 | if (ret == false && !IS_ERR_OR_NULL(fwnode) && |
258 | !IS_ERR_OR_NULL(fwnode->secondary)) | |
3708184a SA |
259 | ret = fwnode_call_int_op(fwnode->secondary, property_present, |
260 | propname); | |
362c0b30 AS |
261 | return ret; |
262 | } | |
8a0662d9 RW |
263 | EXPORT_SYMBOL_GPL(fwnode_property_present); |
264 | ||
b31384fa RW |
265 | /** |
266 | * device_property_read_u8_array - return a u8 array property of a device | |
267 | * @dev: Device to get the property of | |
268 | * @propname: Name of the property | |
5c0acf3b | 269 | * @val: The values are stored here or %NULL to return the number of values |
b31384fa RW |
270 | * @nval: Size of the @val array |
271 | * | |
272 | * Function reads an array of u8 properties with @propname from the device | |
273 | * firmware description and stores them to @val if found. | |
274 | * | |
5c0acf3b AH |
275 | * Return: number of values if @val was %NULL, |
276 | * %0 if the property was found (success), | |
b31384fa RW |
277 | * %-EINVAL if given arguments are not valid, |
278 | * %-ENODATA if the property does not have a value, | |
279 | * %-EPROTO if the property is not an array of numbers, | |
280 | * %-EOVERFLOW if the size of the property is not as expected. | |
4fa7508e | 281 | * %-ENXIO if no suitable firmware interface is present. |
b31384fa RW |
282 | */ |
283 | int device_property_read_u8_array(struct device *dev, const char *propname, | |
284 | u8 *val, size_t nval) | |
285 | { | |
9017f252 | 286 | return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval); |
b31384fa RW |
287 | } |
288 | EXPORT_SYMBOL_GPL(device_property_read_u8_array); | |
289 | ||
290 | /** | |
291 | * device_property_read_u16_array - return a u16 array property of a device | |
292 | * @dev: Device to get the property of | |
293 | * @propname: Name of the property | |
5c0acf3b | 294 | * @val: The values are stored here or %NULL to return the number of values |
b31384fa RW |
295 | * @nval: Size of the @val array |
296 | * | |
297 | * Function reads an array of u16 properties with @propname from the device | |
298 | * firmware description and stores them to @val if found. | |
299 | * | |
5c0acf3b AH |
300 | * Return: number of values if @val was %NULL, |
301 | * %0 if the property was found (success), | |
b31384fa RW |
302 | * %-EINVAL if given arguments are not valid, |
303 | * %-ENODATA if the property does not have a value, | |
304 | * %-EPROTO if the property is not an array of numbers, | |
305 | * %-EOVERFLOW if the size of the property is not as expected. | |
4fa7508e | 306 | * %-ENXIO if no suitable firmware interface is present. |
b31384fa RW |
307 | */ |
308 | int device_property_read_u16_array(struct device *dev, const char *propname, | |
309 | u16 *val, size_t nval) | |
310 | { | |
9017f252 | 311 | return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval); |
b31384fa RW |
312 | } |
313 | EXPORT_SYMBOL_GPL(device_property_read_u16_array); | |
314 | ||
315 | /** | |
316 | * device_property_read_u32_array - return a u32 array property of a device | |
317 | * @dev: Device to get the property of | |
318 | * @propname: Name of the property | |
5c0acf3b | 319 | * @val: The values are stored here or %NULL to return the number of values |
b31384fa RW |
320 | * @nval: Size of the @val array |
321 | * | |
322 | * Function reads an array of u32 properties with @propname from the device | |
323 | * firmware description and stores them to @val if found. | |
324 | * | |
5c0acf3b AH |
325 | * Return: number of values if @val was %NULL, |
326 | * %0 if the property was found (success), | |
b31384fa RW |
327 | * %-EINVAL if given arguments are not valid, |
328 | * %-ENODATA if the property does not have a value, | |
329 | * %-EPROTO if the property is not an array of numbers, | |
330 | * %-EOVERFLOW if the size of the property is not as expected. | |
4fa7508e | 331 | * %-ENXIO if no suitable firmware interface is present. |
b31384fa RW |
332 | */ |
333 | int device_property_read_u32_array(struct device *dev, const char *propname, | |
334 | u32 *val, size_t nval) | |
335 | { | |
9017f252 | 336 | return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval); |
b31384fa RW |
337 | } |
338 | EXPORT_SYMBOL_GPL(device_property_read_u32_array); | |
339 | ||
340 | /** | |
341 | * device_property_read_u64_array - return a u64 array property of a device | |
342 | * @dev: Device to get the property of | |
343 | * @propname: Name of the property | |
5c0acf3b | 344 | * @val: The values are stored here or %NULL to return the number of values |
b31384fa RW |
345 | * @nval: Size of the @val array |
346 | * | |
347 | * Function reads an array of u64 properties with @propname from the device | |
348 | * firmware description and stores them to @val if found. | |
349 | * | |
5c0acf3b AH |
350 | * Return: number of values if @val was %NULL, |
351 | * %0 if the property was found (success), | |
b31384fa RW |
352 | * %-EINVAL if given arguments are not valid, |
353 | * %-ENODATA if the property does not have a value, | |
354 | * %-EPROTO if the property is not an array of numbers, | |
355 | * %-EOVERFLOW if the size of the property is not as expected. | |
4fa7508e | 356 | * %-ENXIO if no suitable firmware interface is present. |
b31384fa RW |
357 | */ |
358 | int device_property_read_u64_array(struct device *dev, const char *propname, | |
359 | u64 *val, size_t nval) | |
360 | { | |
9017f252 | 361 | return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval); |
b31384fa RW |
362 | } |
363 | EXPORT_SYMBOL_GPL(device_property_read_u64_array); | |
364 | ||
365 | /** | |
366 | * device_property_read_string_array - return a string array property of device | |
367 | * @dev: Device to get the property of | |
368 | * @propname: Name of the property | |
5c0acf3b | 369 | * @val: The values are stored here or %NULL to return the number of values |
b31384fa RW |
370 | * @nval: Size of the @val array |
371 | * | |
372 | * Function reads an array of string properties with @propname from the device | |
373 | * firmware description and stores them to @val if found. | |
374 | * | |
b0b027ce SA |
375 | * Return: number of values read on success if @val is non-NULL, |
376 | * number of values available on success if @val is NULL, | |
b31384fa RW |
377 | * %-EINVAL if given arguments are not valid, |
378 | * %-ENODATA if the property does not have a value, | |
379 | * %-EPROTO or %-EILSEQ if the property is not an array of strings, | |
380 | * %-EOVERFLOW if the size of the property is not as expected. | |
4fa7508e | 381 | * %-ENXIO if no suitable firmware interface is present. |
b31384fa RW |
382 | */ |
383 | int device_property_read_string_array(struct device *dev, const char *propname, | |
384 | const char **val, size_t nval) | |
385 | { | |
9017f252 | 386 | return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval); |
b31384fa RW |
387 | } |
388 | EXPORT_SYMBOL_GPL(device_property_read_string_array); | |
389 | ||
390 | /** | |
391 | * device_property_read_string - return a string property of a device | |
392 | * @dev: Device to get the property of | |
393 | * @propname: Name of the property | |
394 | * @val: The value is stored here | |
395 | * | |
396 | * Function reads property @propname from the device firmware description and | |
397 | * stores the value into @val if found. The value is checked to be a string. | |
398 | * | |
399 | * Return: %0 if the property was found (success), | |
400 | * %-EINVAL if given arguments are not valid, | |
401 | * %-ENODATA if the property does not have a value, | |
402 | * %-EPROTO or %-EILSEQ if the property type is not a string. | |
4fa7508e | 403 | * %-ENXIO if no suitable firmware interface is present. |
b31384fa RW |
404 | */ |
405 | int device_property_read_string(struct device *dev, const char *propname, | |
406 | const char **val) | |
407 | { | |
9017f252 | 408 | return fwnode_property_read_string(dev_fwnode(dev), propname, val); |
b31384fa RW |
409 | } |
410 | EXPORT_SYMBOL_GPL(device_property_read_string); | |
8a0662d9 | 411 | |
3f5c8d31 MW |
412 | /** |
413 | * device_property_match_string - find a string in an array and return index | |
414 | * @dev: Device to get the property of | |
415 | * @propname: Name of the property holding the array | |
416 | * @string: String to look for | |
417 | * | |
418 | * Find a given string in a string array and if it is found return the | |
419 | * index back. | |
420 | * | |
421 | * Return: %0 if the property was found (success), | |
422 | * %-EINVAL if given arguments are not valid, | |
423 | * %-ENODATA if the property does not have a value, | |
424 | * %-EPROTO if the property is not an array of strings, | |
425 | * %-ENXIO if no suitable firmware interface is present. | |
426 | */ | |
427 | int device_property_match_string(struct device *dev, const char *propname, | |
428 | const char *string) | |
429 | { | |
430 | return fwnode_property_match_string(dev_fwnode(dev), propname, string); | |
431 | } | |
432 | EXPORT_SYMBOL_GPL(device_property_match_string); | |
433 | ||
3708184a SA |
434 | static int fwnode_property_read_int_array(struct fwnode_handle *fwnode, |
435 | const char *propname, | |
436 | unsigned int elem_size, void *val, | |
437 | size_t nval) | |
438 | { | |
439 | int ret; | |
440 | ||
441 | ret = fwnode_call_int_op(fwnode, property_read_int_array, propname, | |
442 | elem_size, val, nval); | |
443 | if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) && | |
444 | !IS_ERR_OR_NULL(fwnode->secondary)) | |
445 | ret = fwnode_call_int_op( | |
446 | fwnode->secondary, property_read_int_array, propname, | |
447 | elem_size, val, nval); | |
448 | ||
449 | return ret; | |
450 | } | |
362c0b30 | 451 | |
8a0662d9 RW |
452 | /** |
453 | * fwnode_property_read_u8_array - return a u8 array property of firmware node | |
454 | * @fwnode: Firmware node to get the property of | |
455 | * @propname: Name of the property | |
5c0acf3b | 456 | * @val: The values are stored here or %NULL to return the number of values |
8a0662d9 RW |
457 | * @nval: Size of the @val array |
458 | * | |
459 | * Read an array of u8 properties with @propname from @fwnode and stores them to | |
460 | * @val if found. | |
461 | * | |
5c0acf3b AH |
462 | * Return: number of values if @val was %NULL, |
463 | * %0 if the property was found (success), | |
8a0662d9 RW |
464 | * %-EINVAL if given arguments are not valid, |
465 | * %-ENODATA if the property does not have a value, | |
466 | * %-EPROTO if the property is not an array of numbers, | |
467 | * %-EOVERFLOW if the size of the property is not as expected, | |
468 | * %-ENXIO if no suitable firmware interface is present. | |
469 | */ | |
470 | int fwnode_property_read_u8_array(struct fwnode_handle *fwnode, | |
471 | const char *propname, u8 *val, size_t nval) | |
472 | { | |
3708184a SA |
473 | return fwnode_property_read_int_array(fwnode, propname, sizeof(u8), |
474 | val, nval); | |
8a0662d9 RW |
475 | } |
476 | EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array); | |
477 | ||
478 | /** | |
479 | * fwnode_property_read_u16_array - return a u16 array property of firmware node | |
480 | * @fwnode: Firmware node to get the property of | |
481 | * @propname: Name of the property | |
5c0acf3b | 482 | * @val: The values are stored here or %NULL to return the number of values |
8a0662d9 RW |
483 | * @nval: Size of the @val array |
484 | * | |
485 | * Read an array of u16 properties with @propname from @fwnode and store them to | |
486 | * @val if found. | |
487 | * | |
5c0acf3b AH |
488 | * Return: number of values if @val was %NULL, |
489 | * %0 if the property was found (success), | |
8a0662d9 RW |
490 | * %-EINVAL if given arguments are not valid, |
491 | * %-ENODATA if the property does not have a value, | |
492 | * %-EPROTO if the property is not an array of numbers, | |
493 | * %-EOVERFLOW if the size of the property is not as expected, | |
494 | * %-ENXIO if no suitable firmware interface is present. | |
495 | */ | |
496 | int fwnode_property_read_u16_array(struct fwnode_handle *fwnode, | |
497 | const char *propname, u16 *val, size_t nval) | |
498 | { | |
3708184a SA |
499 | return fwnode_property_read_int_array(fwnode, propname, sizeof(u16), |
500 | val, nval); | |
8a0662d9 RW |
501 | } |
502 | EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array); | |
503 | ||
504 | /** | |
505 | * fwnode_property_read_u32_array - return a u32 array property of firmware node | |
506 | * @fwnode: Firmware node to get the property of | |
507 | * @propname: Name of the property | |
5c0acf3b | 508 | * @val: The values are stored here or %NULL to return the number of values |
8a0662d9 RW |
509 | * @nval: Size of the @val array |
510 | * | |
511 | * Read an array of u32 properties with @propname from @fwnode store them to | |
512 | * @val if found. | |
513 | * | |
5c0acf3b AH |
514 | * Return: number of values if @val was %NULL, |
515 | * %0 if the property was found (success), | |
8a0662d9 RW |
516 | * %-EINVAL if given arguments are not valid, |
517 | * %-ENODATA if the property does not have a value, | |
518 | * %-EPROTO if the property is not an array of numbers, | |
519 | * %-EOVERFLOW if the size of the property is not as expected, | |
520 | * %-ENXIO if no suitable firmware interface is present. | |
521 | */ | |
522 | int fwnode_property_read_u32_array(struct fwnode_handle *fwnode, | |
523 | const char *propname, u32 *val, size_t nval) | |
524 | { | |
3708184a SA |
525 | return fwnode_property_read_int_array(fwnode, propname, sizeof(u32), |
526 | val, nval); | |
8a0662d9 RW |
527 | } |
528 | EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array); | |
529 | ||
530 | /** | |
531 | * fwnode_property_read_u64_array - return a u64 array property firmware node | |
532 | * @fwnode: Firmware node to get the property of | |
533 | * @propname: Name of the property | |
5c0acf3b | 534 | * @val: The values are stored here or %NULL to return the number of values |
8a0662d9 RW |
535 | * @nval: Size of the @val array |
536 | * | |
537 | * Read an array of u64 properties with @propname from @fwnode and store them to | |
538 | * @val if found. | |
539 | * | |
5c0acf3b AH |
540 | * Return: number of values if @val was %NULL, |
541 | * %0 if the property was found (success), | |
8a0662d9 RW |
542 | * %-EINVAL if given arguments are not valid, |
543 | * %-ENODATA if the property does not have a value, | |
544 | * %-EPROTO if the property is not an array of numbers, | |
545 | * %-EOVERFLOW if the size of the property is not as expected, | |
546 | * %-ENXIO if no suitable firmware interface is present. | |
547 | */ | |
548 | int fwnode_property_read_u64_array(struct fwnode_handle *fwnode, | |
549 | const char *propname, u64 *val, size_t nval) | |
550 | { | |
3708184a SA |
551 | return fwnode_property_read_int_array(fwnode, propname, sizeof(u64), |
552 | val, nval); | |
8a0662d9 RW |
553 | } |
554 | EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array); | |
555 | ||
556 | /** | |
557 | * fwnode_property_read_string_array - return string array property of a node | |
558 | * @fwnode: Firmware node to get the property of | |
559 | * @propname: Name of the property | |
5c0acf3b | 560 | * @val: The values are stored here or %NULL to return the number of values |
8a0662d9 RW |
561 | * @nval: Size of the @val array |
562 | * | |
563 | * Read an string list property @propname from the given firmware node and store | |
564 | * them to @val if found. | |
565 | * | |
b0b027ce SA |
566 | * Return: number of values read on success if @val is non-NULL, |
567 | * number of values available on success if @val is NULL, | |
8a0662d9 RW |
568 | * %-EINVAL if given arguments are not valid, |
569 | * %-ENODATA if the property does not have a value, | |
026b8217 | 570 | * %-EPROTO or %-EILSEQ if the property is not an array of strings, |
8a0662d9 RW |
571 | * %-EOVERFLOW if the size of the property is not as expected, |
572 | * %-ENXIO if no suitable firmware interface is present. | |
573 | */ | |
574 | int fwnode_property_read_string_array(struct fwnode_handle *fwnode, | |
575 | const char *propname, const char **val, | |
576 | size_t nval) | |
577 | { | |
362c0b30 AS |
578 | int ret; |
579 | ||
3708184a SA |
580 | ret = fwnode_call_int_op(fwnode, property_read_string_array, propname, |
581 | val, nval); | |
0d67e0fa HK |
582 | if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) && |
583 | !IS_ERR_OR_NULL(fwnode->secondary)) | |
3708184a SA |
584 | ret = fwnode_call_int_op(fwnode->secondary, |
585 | property_read_string_array, propname, | |
586 | val, nval); | |
362c0b30 | 587 | return ret; |
8a0662d9 RW |
588 | } |
589 | EXPORT_SYMBOL_GPL(fwnode_property_read_string_array); | |
590 | ||
591 | /** | |
592 | * fwnode_property_read_string - return a string property of a firmware node | |
593 | * @fwnode: Firmware node to get the property of | |
594 | * @propname: Name of the property | |
595 | * @val: The value is stored here | |
596 | * | |
597 | * Read property @propname from the given firmware node and store the value into | |
598 | * @val if found. The value is checked to be a string. | |
599 | * | |
600 | * Return: %0 if the property was found (success), | |
601 | * %-EINVAL if given arguments are not valid, | |
602 | * %-ENODATA if the property does not have a value, | |
603 | * %-EPROTO or %-EILSEQ if the property is not a string, | |
604 | * %-ENXIO if no suitable firmware interface is present. | |
605 | */ | |
606 | int fwnode_property_read_string(struct fwnode_handle *fwnode, | |
607 | const char *propname, const char **val) | |
608 | { | |
e4817477 | 609 | int ret = fwnode_property_read_string_array(fwnode, propname, val, 1); |
362c0b30 | 610 | |
b0b027ce | 611 | return ret < 0 ? ret : 0; |
8a0662d9 RW |
612 | } |
613 | EXPORT_SYMBOL_GPL(fwnode_property_read_string); | |
614 | ||
3f5c8d31 MW |
615 | /** |
616 | * fwnode_property_match_string - find a string in an array and return index | |
617 | * @fwnode: Firmware node to get the property of | |
618 | * @propname: Name of the property holding the array | |
619 | * @string: String to look for | |
620 | * | |
621 | * Find a given string in a string array and if it is found return the | |
622 | * index back. | |
623 | * | |
624 | * Return: %0 if the property was found (success), | |
625 | * %-EINVAL if given arguments are not valid, | |
626 | * %-ENODATA if the property does not have a value, | |
627 | * %-EPROTO if the property is not an array of strings, | |
628 | * %-ENXIO if no suitable firmware interface is present. | |
629 | */ | |
630 | int fwnode_property_match_string(struct fwnode_handle *fwnode, | |
631 | const char *propname, const char *string) | |
632 | { | |
633 | const char **values; | |
a7c1d0a9 | 634 | int nval, ret; |
3f5c8d31 MW |
635 | |
636 | nval = fwnode_property_read_string_array(fwnode, propname, NULL, 0); | |
637 | if (nval < 0) | |
638 | return nval; | |
639 | ||
f6740c18 AS |
640 | if (nval == 0) |
641 | return -ENODATA; | |
642 | ||
3f5c8d31 MW |
643 | values = kcalloc(nval, sizeof(*values), GFP_KERNEL); |
644 | if (!values) | |
645 | return -ENOMEM; | |
646 | ||
647 | ret = fwnode_property_read_string_array(fwnode, propname, values, nval); | |
648 | if (ret < 0) | |
649 | goto out; | |
650 | ||
a7c1d0a9 AS |
651 | ret = match_string(values, nval, string); |
652 | if (ret < 0) | |
653 | ret = -ENODATA; | |
3f5c8d31 MW |
654 | out: |
655 | kfree(values); | |
656 | return ret; | |
657 | } | |
658 | EXPORT_SYMBOL_GPL(fwnode_property_match_string); | |
659 | ||
2d479e1f DT |
660 | static int property_copy_string_array(struct property_entry *dst, |
661 | const struct property_entry *src) | |
13141e1c | 662 | { |
2d479e1f DT |
663 | char **d; |
664 | size_t nval = src->length / sizeof(*d); | |
665 | int i; | |
13141e1c | 666 | |
2d479e1f DT |
667 | d = kcalloc(nval, sizeof(*d), GFP_KERNEL); |
668 | if (!d) | |
669 | return -ENOMEM; | |
13141e1c | 670 | |
2d479e1f DT |
671 | for (i = 0; i < nval; i++) { |
672 | d[i] = kstrdup(src->pointer.str[i], GFP_KERNEL); | |
673 | if (!d[i] && src->pointer.str[i]) { | |
674 | while (--i >= 0) | |
675 | kfree(d[i]); | |
676 | kfree(d); | |
677 | return -ENOMEM; | |
13141e1c | 678 | } |
13141e1c MW |
679 | } |
680 | ||
2d479e1f DT |
681 | dst->pointer.raw_data = d; |
682 | return 0; | |
13141e1c MW |
683 | } |
684 | ||
2d479e1f DT |
685 | static int property_entry_copy_data(struct property_entry *dst, |
686 | const struct property_entry *src) | |
13141e1c | 687 | { |
2d479e1f | 688 | int error; |
13141e1c MW |
689 | |
690 | dst->name = kstrdup(src->name, GFP_KERNEL); | |
691 | if (!dst->name) | |
692 | return -ENOMEM; | |
693 | ||
694 | if (src->is_array) { | |
2d479e1f DT |
695 | if (!src->length) { |
696 | error = -ENODATA; | |
697 | goto out_free_name; | |
698 | } | |
f6740c18 | 699 | |
13141e1c | 700 | if (src->is_string) { |
2d479e1f DT |
701 | error = property_copy_string_array(dst, src); |
702 | if (error) | |
703 | goto out_free_name; | |
13141e1c MW |
704 | } else { |
705 | dst->pointer.raw_data = kmemdup(src->pointer.raw_data, | |
706 | src->length, GFP_KERNEL); | |
2d479e1f DT |
707 | if (!dst->pointer.raw_data) { |
708 | error = -ENOMEM; | |
709 | goto out_free_name; | |
710 | } | |
13141e1c MW |
711 | } |
712 | } else if (src->is_string) { | |
713 | dst->value.str = kstrdup(src->value.str, GFP_KERNEL); | |
2d479e1f DT |
714 | if (!dst->value.str && src->value.str) { |
715 | error = -ENOMEM; | |
716 | goto out_free_name; | |
717 | } | |
13141e1c MW |
718 | } else { |
719 | dst->value.raw_data = src->value.raw_data; | |
720 | } | |
721 | ||
722 | dst->length = src->length; | |
723 | dst->is_array = src->is_array; | |
724 | dst->is_string = src->is_string; | |
725 | ||
726 | return 0; | |
2d479e1f DT |
727 | |
728 | out_free_name: | |
729 | kfree(dst->name); | |
730 | return error; | |
731 | } | |
732 | ||
733 | static void property_entry_free_data(const struct property_entry *p) | |
734 | { | |
735 | size_t i, nval; | |
736 | ||
737 | if (p->is_array) { | |
738 | if (p->is_string && p->pointer.str) { | |
739 | nval = p->length / sizeof(const char *); | |
740 | for (i = 0; i < nval; i++) | |
741 | kfree(p->pointer.str[i]); | |
742 | } | |
743 | kfree(p->pointer.raw_data); | |
744 | } else if (p->is_string) { | |
745 | kfree(p->value.str); | |
746 | } | |
747 | kfree(p->name); | |
748 | } | |
749 | ||
750 | /** | |
751 | * property_entries_dup - duplicate array of properties | |
752 | * @properties: array of properties to copy | |
753 | * | |
754 | * This function creates a deep copy of the given NULL-terminated array | |
755 | * of property entries. | |
756 | */ | |
757 | struct property_entry * | |
758 | property_entries_dup(const struct property_entry *properties) | |
759 | { | |
760 | struct property_entry *p; | |
761 | int i, n = 0; | |
762 | ||
763 | while (properties[n].name) | |
764 | n++; | |
765 | ||
766 | p = kcalloc(n + 1, sizeof(*p), GFP_KERNEL); | |
767 | if (!p) | |
768 | return ERR_PTR(-ENOMEM); | |
769 | ||
770 | for (i = 0; i < n; i++) { | |
771 | int ret = property_entry_copy_data(&p[i], &properties[i]); | |
772 | if (ret) { | |
773 | while (--i >= 0) | |
774 | property_entry_free_data(&p[i]); | |
775 | kfree(p); | |
776 | return ERR_PTR(ret); | |
777 | } | |
778 | } | |
779 | ||
780 | return p; | |
781 | } | |
782 | EXPORT_SYMBOL_GPL(property_entries_dup); | |
783 | ||
784 | /** | |
785 | * property_entries_free - free previously allocated array of properties | |
786 | * @properties: array of properties to destroy | |
787 | * | |
788 | * This function frees given NULL-terminated array of property entries, | |
789 | * along with their data. | |
790 | */ | |
791 | void property_entries_free(const struct property_entry *properties) | |
792 | { | |
793 | const struct property_entry *p; | |
794 | ||
795 | for (p = properties; p->name; p++) | |
796 | property_entry_free_data(p); | |
797 | ||
798 | kfree(properties); | |
799 | } | |
800 | EXPORT_SYMBOL_GPL(property_entries_free); | |
801 | ||
802 | /** | |
803 | * pset_free_set - releases memory allocated for copied property set | |
804 | * @pset: Property set to release | |
805 | * | |
806 | * Function takes previously copied property set and releases all the | |
807 | * memory allocated to it. | |
808 | */ | |
809 | static void pset_free_set(struct property_set *pset) | |
810 | { | |
811 | if (!pset) | |
812 | return; | |
813 | ||
814 | property_entries_free(pset->properties); | |
815 | kfree(pset); | |
13141e1c MW |
816 | } |
817 | ||
818 | /** | |
819 | * pset_copy_set - copies property set | |
820 | * @pset: Property set to copy | |
821 | * | |
822 | * This function takes a deep copy of the given property set and returns | |
823 | * pointer to the copy. Call device_free_property_set() to free resources | |
824 | * allocated in this function. | |
825 | * | |
826 | * Return: Pointer to the new property set or error pointer. | |
827 | */ | |
828 | static struct property_set *pset_copy_set(const struct property_set *pset) | |
829 | { | |
2d479e1f | 830 | struct property_entry *properties; |
13141e1c | 831 | struct property_set *p; |
13141e1c MW |
832 | |
833 | p = kzalloc(sizeof(*p), GFP_KERNEL); | |
834 | if (!p) | |
835 | return ERR_PTR(-ENOMEM); | |
836 | ||
2d479e1f DT |
837 | properties = property_entries_dup(pset->properties); |
838 | if (IS_ERR(properties)) { | |
13141e1c | 839 | kfree(p); |
2d479e1f | 840 | return ERR_CAST(properties); |
13141e1c MW |
841 | } |
842 | ||
2d479e1f | 843 | p->properties = properties; |
13141e1c MW |
844 | return p; |
845 | } | |
846 | ||
847 | /** | |
f4d05266 | 848 | * device_remove_properties - Remove properties from a device object. |
13141e1c MW |
849 | * @dev: Device whose properties to remove. |
850 | * | |
851 | * The function removes properties previously associated to the device | |
f4d05266 | 852 | * secondary firmware node with device_add_properties(). Memory allocated |
13141e1c MW |
853 | * to the properties will also be released. |
854 | */ | |
f4d05266 | 855 | void device_remove_properties(struct device *dev) |
13141e1c MW |
856 | { |
857 | struct fwnode_handle *fwnode; | |
858 | ||
859 | fwnode = dev_fwnode(dev); | |
860 | if (!fwnode) | |
861 | return; | |
862 | /* | |
863 | * Pick either primary or secondary node depending which one holds | |
864 | * the pset. If there is no real firmware node (ACPI/DT) primary | |
865 | * will hold the pset. | |
866 | */ | |
0d67e0fa HK |
867 | if (is_pset_node(fwnode)) { |
868 | set_primary_fwnode(dev, NULL); | |
13141e1c | 869 | pset_free_set(to_pset_node(fwnode)); |
0d67e0fa HK |
870 | } else { |
871 | fwnode = fwnode->secondary; | |
872 | if (!IS_ERR(fwnode) && is_pset_node(fwnode)) { | |
873 | set_secondary_fwnode(dev, NULL); | |
874 | pset_free_set(to_pset_node(fwnode)); | |
875 | } | |
876 | } | |
13141e1c | 877 | } |
f4d05266 | 878 | EXPORT_SYMBOL_GPL(device_remove_properties); |
13141e1c MW |
879 | |
880 | /** | |
f4d05266 | 881 | * device_add_properties - Add a collection of properties to a device object. |
13141e1c | 882 | * @dev: Device to add properties to. |
f4d05266 | 883 | * @properties: Collection of properties to add. |
13141e1c | 884 | * |
f4d05266 HK |
885 | * Associate a collection of device properties represented by @properties with |
886 | * @dev as its secondary firmware node. The function takes a copy of | |
887 | * @properties. | |
13141e1c | 888 | */ |
bec84da8 DT |
889 | int device_add_properties(struct device *dev, |
890 | const struct property_entry *properties) | |
13141e1c | 891 | { |
f4d05266 | 892 | struct property_set *p, pset; |
13141e1c | 893 | |
f4d05266 | 894 | if (!properties) |
13141e1c MW |
895 | return -EINVAL; |
896 | ||
f4d05266 HK |
897 | pset.properties = properties; |
898 | ||
899 | p = pset_copy_set(&pset); | |
13141e1c MW |
900 | if (IS_ERR(p)) |
901 | return PTR_ERR(p); | |
902 | ||
903 | p->fwnode.type = FWNODE_PDATA; | |
3708184a | 904 | p->fwnode.ops = &pset_fwnode_ops; |
13141e1c MW |
905 | set_secondary_fwnode(dev, &p->fwnode); |
906 | return 0; | |
907 | } | |
f4d05266 | 908 | EXPORT_SYMBOL_GPL(device_add_properties); |
13141e1c | 909 | |
23387258 SA |
910 | /** |
911 | * fwnode_get_next_parent - Iterate to the node's parent | |
912 | * @fwnode: Firmware whose parent is retrieved | |
913 | * | |
914 | * This is like fwnode_get_parent() except that it drops the refcount | |
915 | * on the passed node, making it suitable for iterating through a | |
916 | * node's parents. | |
917 | * | |
918 | * Returns a node pointer with refcount incremented, use | |
919 | * fwnode_handle_node() on it when done. | |
920 | */ | |
921 | struct fwnode_handle *fwnode_get_next_parent(struct fwnode_handle *fwnode) | |
922 | { | |
923 | struct fwnode_handle *parent = fwnode_get_parent(fwnode); | |
924 | ||
925 | fwnode_handle_put(fwnode); | |
926 | ||
927 | return parent; | |
928 | } | |
929 | EXPORT_SYMBOL_GPL(fwnode_get_next_parent); | |
930 | ||
afaf26fd MW |
931 | /** |
932 | * fwnode_get_parent - Return parent firwmare node | |
933 | * @fwnode: Firmware whose parent is retrieved | |
934 | * | |
935 | * Return parent firmware node of the given node if possible or %NULL if no | |
936 | * parent was available. | |
937 | */ | |
938 | struct fwnode_handle *fwnode_get_parent(struct fwnode_handle *fwnode) | |
939 | { | |
3708184a | 940 | return fwnode_call_ptr_op(fwnode, get_parent); |
afaf26fd MW |
941 | } |
942 | EXPORT_SYMBOL_GPL(fwnode_get_parent); | |
943 | ||
8a0662d9 | 944 | /** |
34055190 MW |
945 | * fwnode_get_next_child_node - Return the next child node handle for a node |
946 | * @fwnode: Firmware node to find the next child node for. | |
947 | * @child: Handle to one of the node's child nodes or a %NULL handle. | |
8a0662d9 | 948 | */ |
34055190 | 949 | struct fwnode_handle *fwnode_get_next_child_node(struct fwnode_handle *fwnode, |
8a0662d9 RW |
950 | struct fwnode_handle *child) |
951 | { | |
3708184a | 952 | return fwnode_call_ptr_op(fwnode, get_next_child_node, child); |
8a0662d9 | 953 | } |
34055190 MW |
954 | EXPORT_SYMBOL_GPL(fwnode_get_next_child_node); |
955 | ||
956 | /** | |
957 | * device_get_next_child_node - Return the next child node handle for a device | |
958 | * @dev: Device to find the next child node for. | |
959 | * @child: Handle to one of the device's child nodes or a null handle. | |
960 | */ | |
961 | struct fwnode_handle *device_get_next_child_node(struct device *dev, | |
962 | struct fwnode_handle *child) | |
963 | { | |
964 | struct acpi_device *adev = ACPI_COMPANION(dev); | |
965 | struct fwnode_handle *fwnode = NULL; | |
966 | ||
967 | if (dev->of_node) | |
968 | fwnode = &dev->of_node->fwnode; | |
969 | else if (adev) | |
970 | fwnode = acpi_fwnode_handle(adev); | |
971 | ||
972 | return fwnode_get_next_child_node(fwnode, child); | |
973 | } | |
8a0662d9 RW |
974 | EXPORT_SYMBOL_GPL(device_get_next_child_node); |
975 | ||
613e9721 | 976 | /** |
21ea73f5 MW |
977 | * fwnode_get_named_child_node - Return first matching named child node handle |
978 | * @fwnode: Firmware node to find the named child node for. | |
613e9721 AT |
979 | * @childname: String to match child node name against. |
980 | */ | |
21ea73f5 | 981 | struct fwnode_handle *fwnode_get_named_child_node(struct fwnode_handle *fwnode, |
613e9721 AT |
982 | const char *childname) |
983 | { | |
3708184a | 984 | return fwnode_call_ptr_op(fwnode, get_named_child_node, childname); |
613e9721 | 985 | } |
21ea73f5 MW |
986 | EXPORT_SYMBOL_GPL(fwnode_get_named_child_node); |
987 | ||
988 | /** | |
989 | * device_get_named_child_node - Return first matching named child node handle | |
990 | * @dev: Device to find the named child node for. | |
991 | * @childname: String to match child node name against. | |
992 | */ | |
993 | struct fwnode_handle *device_get_named_child_node(struct device *dev, | |
994 | const char *childname) | |
995 | { | |
996 | return fwnode_get_named_child_node(dev_fwnode(dev), childname); | |
997 | } | |
613e9721 AT |
998 | EXPORT_SYMBOL_GPL(device_get_named_child_node); |
999 | ||
e7887c28 SA |
1000 | /** |
1001 | * fwnode_handle_get - Obtain a reference to a device node | |
1002 | * @fwnode: Pointer to the device node to obtain the reference to. | |
1003 | */ | |
1004 | void fwnode_handle_get(struct fwnode_handle *fwnode) | |
1005 | { | |
3708184a | 1006 | fwnode_call_void_op(fwnode, get); |
e7887c28 SA |
1007 | } |
1008 | EXPORT_SYMBOL_GPL(fwnode_handle_get); | |
1009 | ||
8a0662d9 RW |
1010 | /** |
1011 | * fwnode_handle_put - Drop reference to a device node | |
1012 | * @fwnode: Pointer to the device node to drop the reference to. | |
1013 | * | |
1014 | * This has to be used when terminating device_for_each_child_node() iteration | |
1015 | * with break or return to prevent stale device node references from being left | |
1016 | * behind. | |
1017 | */ | |
1018 | void fwnode_handle_put(struct fwnode_handle *fwnode) | |
1019 | { | |
3708184a | 1020 | fwnode_call_void_op(fwnode, put); |
8a0662d9 RW |
1021 | } |
1022 | EXPORT_SYMBOL_GPL(fwnode_handle_put); | |
1023 | ||
2294b3af SA |
1024 | /** |
1025 | * fwnode_device_is_available - check if a device is available for use | |
1026 | * @fwnode: Pointer to the fwnode of the device. | |
1027 | */ | |
1028 | bool fwnode_device_is_available(struct fwnode_handle *fwnode) | |
1029 | { | |
1030 | return fwnode_call_int_op(fwnode, device_is_available); | |
1031 | } | |
1032 | EXPORT_SYMBOL_GPL(fwnode_device_is_available); | |
1033 | ||
8a0662d9 RW |
1034 | /** |
1035 | * device_get_child_node_count - return the number of child nodes for device | |
1036 | * @dev: Device to cound the child nodes for | |
1037 | */ | |
1038 | unsigned int device_get_child_node_count(struct device *dev) | |
1039 | { | |
1040 | struct fwnode_handle *child; | |
1041 | unsigned int count = 0; | |
1042 | ||
1043 | device_for_each_child_node(dev, child) | |
1044 | count++; | |
1045 | ||
1046 | return count; | |
1047 | } | |
1048 | EXPORT_SYMBOL_GPL(device_get_child_node_count); | |
05ca5560 | 1049 | |
e5e55864 SS |
1050 | bool device_dma_supported(struct device *dev) |
1051 | { | |
1052 | /* For DT, this is always supported. | |
1053 | * For ACPI, this depends on CCA, which | |
1054 | * is determined by the acpi_dma_supported(). | |
1055 | */ | |
1056 | if (IS_ENABLED(CONFIG_OF) && dev->of_node) | |
1057 | return true; | |
1058 | ||
1059 | return acpi_dma_supported(ACPI_COMPANION(dev)); | |
1060 | } | |
1061 | EXPORT_SYMBOL_GPL(device_dma_supported); | |
1062 | ||
1063 | enum dev_dma_attr device_get_dma_attr(struct device *dev) | |
1064 | { | |
1065 | enum dev_dma_attr attr = DEV_DMA_NOT_SUPPORTED; | |
1066 | ||
1067 | if (IS_ENABLED(CONFIG_OF) && dev->of_node) { | |
1068 | if (of_dma_is_coherent(dev->of_node)) | |
1069 | attr = DEV_DMA_COHERENT; | |
1070 | else | |
1071 | attr = DEV_DMA_NON_COHERENT; | |
1072 | } else | |
1073 | attr = acpi_get_dma_attr(ACPI_COMPANION(dev)); | |
1074 | ||
1075 | return attr; | |
1076 | } | |
1077 | EXPORT_SYMBOL_GPL(device_get_dma_attr); | |
1078 | ||
4c96b7dc | 1079 | /** |
2f710a3a | 1080 | * device_get_phy_mode - Get phy mode for given device |
4c96b7dc JL |
1081 | * @dev: Pointer to the given device |
1082 | * | |
1083 | * The function gets phy interface string from property 'phy-mode' or | |
1084 | * 'phy-connection-type', and return its index in phy_modes table, or errno in | |
1085 | * error case. | |
1086 | */ | |
1087 | int device_get_phy_mode(struct device *dev) | |
1088 | { | |
1089 | const char *pm; | |
1090 | int err, i; | |
1091 | ||
1092 | err = device_property_read_string(dev, "phy-mode", &pm); | |
1093 | if (err < 0) | |
1094 | err = device_property_read_string(dev, | |
1095 | "phy-connection-type", &pm); | |
1096 | if (err < 0) | |
1097 | return err; | |
1098 | ||
1099 | for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++) | |
1100 | if (!strcasecmp(pm, phy_modes(i))) | |
1101 | return i; | |
1102 | ||
1103 | return -ENODEV; | |
1104 | } | |
1105 | EXPORT_SYMBOL_GPL(device_get_phy_mode); | |
1106 | ||
1107 | static void *device_get_mac_addr(struct device *dev, | |
1108 | const char *name, char *addr, | |
1109 | int alen) | |
1110 | { | |
1111 | int ret = device_property_read_u8_array(dev, name, addr, alen); | |
1112 | ||
2f710a3a | 1113 | if (ret == 0 && alen == ETH_ALEN && is_valid_ether_addr(addr)) |
4c96b7dc JL |
1114 | return addr; |
1115 | return NULL; | |
1116 | } | |
1117 | ||
1118 | /** | |
2f710a3a JL |
1119 | * device_get_mac_address - Get the MAC for a given device |
1120 | * @dev: Pointer to the device | |
1121 | * @addr: Address of buffer to store the MAC in | |
1122 | * @alen: Length of the buffer pointed to by addr, should be ETH_ALEN | |
1123 | * | |
1124 | * Search the firmware node for the best MAC address to use. 'mac-address' is | |
4c96b7dc JL |
1125 | * checked first, because that is supposed to contain to "most recent" MAC |
1126 | * address. If that isn't set, then 'local-mac-address' is checked next, | |
1127 | * because that is the default address. If that isn't set, then the obsolete | |
1128 | * 'address' is checked, just in case we're using an old device tree. | |
1129 | * | |
1130 | * Note that the 'address' property is supposed to contain a virtual address of | |
1131 | * the register set, but some DTS files have redefined that property to be the | |
1132 | * MAC address. | |
1133 | * | |
1134 | * All-zero MAC addresses are rejected, because those could be properties that | |
2f710a3a JL |
1135 | * exist in the firmware tables, but were not updated by the firmware. For |
1136 | * example, the DTS could define 'mac-address' and 'local-mac-address', with | |
1137 | * zero MAC addresses. Some older U-Boots only initialized 'local-mac-address'. | |
1138 | * In this case, the real MAC is in 'local-mac-address', and 'mac-address' | |
1139 | * exists but is all zeros. | |
4c96b7dc JL |
1140 | */ |
1141 | void *device_get_mac_address(struct device *dev, char *addr, int alen) | |
1142 | { | |
5b902d6f | 1143 | char *res; |
4c96b7dc | 1144 | |
5b902d6f JG |
1145 | res = device_get_mac_addr(dev, "mac-address", addr, alen); |
1146 | if (res) | |
1147 | return res; | |
1148 | ||
1149 | res = device_get_mac_addr(dev, "local-mac-address", addr, alen); | |
1150 | if (res) | |
1151 | return res; | |
4c96b7dc JL |
1152 | |
1153 | return device_get_mac_addr(dev, "address", addr, alen); | |
1154 | } | |
1155 | EXPORT_SYMBOL(device_get_mac_address); | |
07bb80d4 MW |
1156 | |
1157 | /** | |
1158 | * device_graph_get_next_endpoint - Get next endpoint firmware node | |
1159 | * @fwnode: Pointer to the parent firmware node | |
1160 | * @prev: Previous endpoint node or %NULL to get the first | |
1161 | * | |
1162 | * Returns an endpoint firmware node pointer or %NULL if no more endpoints | |
1163 | * are available. | |
1164 | */ | |
1165 | struct fwnode_handle * | |
1166 | fwnode_graph_get_next_endpoint(struct fwnode_handle *fwnode, | |
1167 | struct fwnode_handle *prev) | |
1168 | { | |
3b27d00e | 1169 | return fwnode_call_ptr_op(fwnode, graph_get_next_endpoint, prev); |
07bb80d4 MW |
1170 | } |
1171 | EXPORT_SYMBOL_GPL(fwnode_graph_get_next_endpoint); | |
1172 | ||
1173 | /** | |
1174 | * fwnode_graph_get_remote_port_parent - Return fwnode of a remote device | |
1175 | * @fwnode: Endpoint firmware node pointing to the remote endpoint | |
1176 | * | |
1177 | * Extracts firmware node of a remote device the @fwnode points to. | |
1178 | */ | |
1179 | struct fwnode_handle * | |
1180 | fwnode_graph_get_remote_port_parent(struct fwnode_handle *fwnode) | |
1181 | { | |
3b27d00e | 1182 | struct fwnode_handle *port, *parent; |
07bb80d4 | 1183 | |
3b27d00e SA |
1184 | port = fwnode_graph_get_remote_port(fwnode); |
1185 | parent = fwnode_call_ptr_op(port, graph_get_port_parent); | |
07bb80d4 | 1186 | |
3b27d00e | 1187 | fwnode_handle_put(port); |
07bb80d4 MW |
1188 | |
1189 | return parent; | |
1190 | } | |
1191 | EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port_parent); | |
1192 | ||
1193 | /** | |
1194 | * fwnode_graph_get_remote_port - Return fwnode of a remote port | |
1195 | * @fwnode: Endpoint firmware node pointing to the remote endpoint | |
1196 | * | |
1197 | * Extracts firmware node of a remote port the @fwnode points to. | |
1198 | */ | |
1199 | struct fwnode_handle *fwnode_graph_get_remote_port(struct fwnode_handle *fwnode) | |
1200 | { | |
3b27d00e | 1201 | return fwnode_get_next_parent(fwnode_graph_get_remote_endpoint(fwnode)); |
07bb80d4 MW |
1202 | } |
1203 | EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port); | |
1204 | ||
1205 | /** | |
1206 | * fwnode_graph_get_remote_endpoint - Return fwnode of a remote endpoint | |
1207 | * @fwnode: Endpoint firmware node pointing to the remote endpoint | |
1208 | * | |
1209 | * Extracts firmware node of a remote endpoint the @fwnode points to. | |
1210 | */ | |
1211 | struct fwnode_handle * | |
1212 | fwnode_graph_get_remote_endpoint(struct fwnode_handle *fwnode) | |
1213 | { | |
3b27d00e | 1214 | return fwnode_call_ptr_op(fwnode, graph_get_remote_endpoint); |
07bb80d4 MW |
1215 | } |
1216 | EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_endpoint); | |
2bd5452d | 1217 | |
125ee6b3 SA |
1218 | /** |
1219 | * fwnode_graph_get_remote_node - get remote parent node for given port/endpoint | |
1220 | * @fwnode: pointer to parent fwnode_handle containing graph port/endpoint | |
1221 | * @port_id: identifier of the parent port node | |
1222 | * @endpoint_id: identifier of the endpoint node | |
1223 | * | |
1224 | * Return: Remote fwnode handle associated with remote endpoint node linked | |
1225 | * to @node. Use fwnode_node_put() on it when done. | |
1226 | */ | |
1227 | struct fwnode_handle *fwnode_graph_get_remote_node(struct fwnode_handle *fwnode, | |
1228 | u32 port_id, u32 endpoint_id) | |
1229 | { | |
1230 | struct fwnode_handle *endpoint = NULL; | |
1231 | ||
1232 | while ((endpoint = fwnode_graph_get_next_endpoint(fwnode, endpoint))) { | |
1233 | struct fwnode_endpoint fwnode_ep; | |
1234 | struct fwnode_handle *remote; | |
1235 | int ret; | |
1236 | ||
1237 | ret = fwnode_graph_parse_endpoint(endpoint, &fwnode_ep); | |
1238 | if (ret < 0) | |
1239 | continue; | |
1240 | ||
1241 | if (fwnode_ep.port != port_id || fwnode_ep.id != endpoint_id) | |
1242 | continue; | |
1243 | ||
1244 | remote = fwnode_graph_get_remote_port_parent(endpoint); | |
1245 | if (!remote) | |
1246 | return NULL; | |
1247 | ||
1248 | return fwnode_device_is_available(remote) ? remote : NULL; | |
1249 | } | |
1250 | ||
1251 | return NULL; | |
1252 | } | |
1253 | EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_node); | |
1254 | ||
2bd5452d SA |
1255 | /** |
1256 | * fwnode_graph_parse_endpoint - parse common endpoint node properties | |
1257 | * @fwnode: pointer to endpoint fwnode_handle | |
1258 | * @endpoint: pointer to the fwnode endpoint data structure | |
1259 | * | |
1260 | * Parse @fwnode representing a graph endpoint node and store the | |
1261 | * information in @endpoint. The caller must hold a reference to | |
1262 | * @fwnode. | |
1263 | */ | |
1264 | int fwnode_graph_parse_endpoint(struct fwnode_handle *fwnode, | |
1265 | struct fwnode_endpoint *endpoint) | |
1266 | { | |
2bd5452d SA |
1267 | memset(endpoint, 0, sizeof(*endpoint)); |
1268 | ||
3b27d00e | 1269 | return fwnode_call_int_op(fwnode, graph_parse_endpoint, endpoint); |
2bd5452d SA |
1270 | } |
1271 | EXPORT_SYMBOL(fwnode_graph_parse_endpoint); |