OSDN Git Service

Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / drivers / base / property.c
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
13 #include <linux/acpi.h>
14 #include <linux/export.h>
15 #include <linux/kernel.h>
16 #include <linux/of.h>
17 #include <linux/of_address.h>
18 #include <linux/property.h>
19 #include <linux/etherdevice.h>
20 #include <linux/phy.h>
21
22 /**
23  * device_add_property_set - Add a collection of properties to a device object.
24  * @dev: Device to add properties to.
25  * @pset: Collection of properties to add.
26  *
27  * Associate a collection of device properties represented by @pset with @dev
28  * as its secondary firmware node.
29  */
30 void device_add_property_set(struct device *dev, struct property_set *pset)
31 {
32         if (pset)
33                 pset->fwnode.type = FWNODE_PDATA;
34
35         set_secondary_fwnode(dev, &pset->fwnode);
36 }
37 EXPORT_SYMBOL_GPL(device_add_property_set);
38
39 static inline bool is_pset(struct fwnode_handle *fwnode)
40 {
41         return fwnode && fwnode->type == FWNODE_PDATA;
42 }
43
44 static inline struct property_set *to_pset(struct fwnode_handle *fwnode)
45 {
46         return is_pset(fwnode) ?
47                 container_of(fwnode, struct property_set, fwnode) : NULL;
48 }
49
50 static struct property_entry *pset_prop_get(struct property_set *pset,
51                                             const char *name)
52 {
53         struct property_entry *prop;
54
55         if (!pset || !pset->properties)
56                 return NULL;
57
58         for (prop = pset->properties; prop->name; prop++)
59                 if (!strcmp(name, prop->name))
60                         return prop;
61
62         return NULL;
63 }
64
65 static int pset_prop_read_array(struct property_set *pset, const char *name,
66                                 enum dev_prop_type type, void *val, size_t nval)
67 {
68         struct property_entry *prop;
69         unsigned int item_size;
70
71         prop = pset_prop_get(pset, name);
72         if (!prop)
73                 return -ENODATA;
74
75         if (prop->type != type)
76                 return -EPROTO;
77
78         if (!val)
79                 return prop->nval;
80
81         if (prop->nval < nval)
82                 return -EOVERFLOW;
83
84         switch (type) {
85         case DEV_PROP_U8:
86                 item_size = sizeof(u8);
87                 break;
88         case DEV_PROP_U16:
89                 item_size = sizeof(u16);
90                 break;
91         case DEV_PROP_U32:
92                 item_size = sizeof(u32);
93                 break;
94         case DEV_PROP_U64:
95                 item_size = sizeof(u64);
96                 break;
97         case DEV_PROP_STRING:
98                 item_size = sizeof(const char *);
99                 break;
100         default:
101                 return -EINVAL;
102         }
103         memcpy(val, prop->value.raw_data, nval * item_size);
104         return 0;
105 }
106
107 static inline struct fwnode_handle *dev_fwnode(struct device *dev)
108 {
109         return IS_ENABLED(CONFIG_OF) && dev->of_node ?
110                 &dev->of_node->fwnode : dev->fwnode;
111 }
112
113 /**
114  * device_property_present - check if a property of a device is present
115  * @dev: Device whose property is being checked
116  * @propname: Name of the property
117  *
118  * Check if property @propname is present in the device firmware description.
119  */
120 bool device_property_present(struct device *dev, const char *propname)
121 {
122         return fwnode_property_present(dev_fwnode(dev), propname);
123 }
124 EXPORT_SYMBOL_GPL(device_property_present);
125
126 /**
127  * fwnode_property_present - check if a property of a firmware node is present
128  * @fwnode: Firmware node whose property to check
129  * @propname: Name of the property
130  */
131 bool fwnode_property_present(struct fwnode_handle *fwnode, const char *propname)
132 {
133         if (is_of_node(fwnode))
134                 return of_property_read_bool(to_of_node(fwnode), propname);
135         else if (is_acpi_node(fwnode))
136                 return !acpi_dev_prop_get(to_acpi_node(fwnode), propname, NULL);
137
138         return !!pset_prop_get(to_pset(fwnode), propname);
139 }
140 EXPORT_SYMBOL_GPL(fwnode_property_present);
141
142 /**
143  * device_property_read_u8_array - return a u8 array property of a device
144  * @dev: Device to get the property of
145  * @propname: Name of the property
146  * @val: The values are stored here or %NULL to return the number of values
147  * @nval: Size of the @val array
148  *
149  * Function reads an array of u8 properties with @propname from the device
150  * firmware description and stores them to @val if found.
151  *
152  * Return: number of values if @val was %NULL,
153  *         %0 if the property was found (success),
154  *         %-EINVAL if given arguments are not valid,
155  *         %-ENODATA if the property does not have a value,
156  *         %-EPROTO if the property is not an array of numbers,
157  *         %-EOVERFLOW if the size of the property is not as expected.
158  *         %-ENXIO if no suitable firmware interface is present.
159  */
160 int device_property_read_u8_array(struct device *dev, const char *propname,
161                                   u8 *val, size_t nval)
162 {
163         return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
164 }
165 EXPORT_SYMBOL_GPL(device_property_read_u8_array);
166
167 /**
168  * device_property_read_u16_array - return a u16 array property of a device
169  * @dev: Device to get the property of
170  * @propname: Name of the property
171  * @val: The values are stored here or %NULL to return the number of values
172  * @nval: Size of the @val array
173  *
174  * Function reads an array of u16 properties with @propname from the device
175  * firmware description and stores them to @val if found.
176  *
177  * Return: number of values if @val was %NULL,
178  *         %0 if the property was found (success),
179  *         %-EINVAL if given arguments are not valid,
180  *         %-ENODATA if the property does not have a value,
181  *         %-EPROTO if the property is not an array of numbers,
182  *         %-EOVERFLOW if the size of the property is not as expected.
183  *         %-ENXIO if no suitable firmware interface is present.
184  */
185 int device_property_read_u16_array(struct device *dev, const char *propname,
186                                    u16 *val, size_t nval)
187 {
188         return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
189 }
190 EXPORT_SYMBOL_GPL(device_property_read_u16_array);
191
192 /**
193  * device_property_read_u32_array - return a u32 array property of a device
194  * @dev: Device to get the property of
195  * @propname: Name of the property
196  * @val: The values are stored here or %NULL to return the number of values
197  * @nval: Size of the @val array
198  *
199  * Function reads an array of u32 properties with @propname from the device
200  * firmware description and stores them to @val if found.
201  *
202  * Return: number of values if @val was %NULL,
203  *         %0 if the property was found (success),
204  *         %-EINVAL if given arguments are not valid,
205  *         %-ENODATA if the property does not have a value,
206  *         %-EPROTO if the property is not an array of numbers,
207  *         %-EOVERFLOW if the size of the property is not as expected.
208  *         %-ENXIO if no suitable firmware interface is present.
209  */
210 int device_property_read_u32_array(struct device *dev, const char *propname,
211                                    u32 *val, size_t nval)
212 {
213         return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
214 }
215 EXPORT_SYMBOL_GPL(device_property_read_u32_array);
216
217 /**
218  * device_property_read_u64_array - return a u64 array property of a device
219  * @dev: Device to get the property of
220  * @propname: Name of the property
221  * @val: The values are stored here or %NULL to return the number of values
222  * @nval: Size of the @val array
223  *
224  * Function reads an array of u64 properties with @propname from the device
225  * firmware description and stores them to @val if found.
226  *
227  * Return: number of values if @val was %NULL,
228  *         %0 if the property was found (success),
229  *         %-EINVAL if given arguments are not valid,
230  *         %-ENODATA if the property does not have a value,
231  *         %-EPROTO if the property is not an array of numbers,
232  *         %-EOVERFLOW if the size of the property is not as expected.
233  *         %-ENXIO if no suitable firmware interface is present.
234  */
235 int device_property_read_u64_array(struct device *dev, const char *propname,
236                                    u64 *val, size_t nval)
237 {
238         return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
239 }
240 EXPORT_SYMBOL_GPL(device_property_read_u64_array);
241
242 /**
243  * device_property_read_string_array - return a string array property of device
244  * @dev: Device to get the property of
245  * @propname: Name of the property
246  * @val: The values are stored here or %NULL to return the number of values
247  * @nval: Size of the @val array
248  *
249  * Function reads an array of string properties with @propname from the device
250  * firmware description and stores them to @val if found.
251  *
252  * Return: number of values if @val was %NULL,
253  *         %0 if the property was found (success),
254  *         %-EINVAL if given arguments are not valid,
255  *         %-ENODATA if the property does not have a value,
256  *         %-EPROTO or %-EILSEQ if the property is not an array of strings,
257  *         %-EOVERFLOW if the size of the property is not as expected.
258  *         %-ENXIO if no suitable firmware interface is present.
259  */
260 int device_property_read_string_array(struct device *dev, const char *propname,
261                                       const char **val, size_t nval)
262 {
263         return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
264 }
265 EXPORT_SYMBOL_GPL(device_property_read_string_array);
266
267 /**
268  * device_property_read_string - return a string property of a device
269  * @dev: Device to get the property of
270  * @propname: Name of the property
271  * @val: The value is stored here
272  *
273  * Function reads property @propname from the device firmware description and
274  * stores the value into @val if found. The value is checked to be a string.
275  *
276  * Return: %0 if the property was found (success),
277  *         %-EINVAL if given arguments are not valid,
278  *         %-ENODATA if the property does not have a value,
279  *         %-EPROTO or %-EILSEQ if the property type is not a string.
280  *         %-ENXIO if no suitable firmware interface is present.
281  */
282 int device_property_read_string(struct device *dev, const char *propname,
283                                 const char **val)
284 {
285         return fwnode_property_read_string(dev_fwnode(dev), propname, val);
286 }
287 EXPORT_SYMBOL_GPL(device_property_read_string);
288
289 #define OF_DEV_PROP_READ_ARRAY(node, propname, type, val, nval) \
290         (val) ? of_property_read_##type##_array((node), (propname), (val), (nval)) \
291               : of_property_count_elems_of_size((node), (propname), sizeof(type))
292
293 #define FWNODE_PROP_READ_ARRAY(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
294 ({ \
295         int _ret_; \
296         if (is_of_node(_fwnode_)) \
297                 _ret_ = OF_DEV_PROP_READ_ARRAY(to_of_node(_fwnode_), _propname_, \
298                                                _type_, _val_, _nval_); \
299         else if (is_acpi_node(_fwnode_)) \
300                 _ret_ = acpi_dev_prop_read(to_acpi_node(_fwnode_), _propname_, \
301                                            _proptype_, _val_, _nval_); \
302         else if (is_pset(_fwnode_)) \
303                 _ret_ = pset_prop_read_array(to_pset(_fwnode_), _propname_, \
304                                              _proptype_, _val_, _nval_); \
305         else \
306                 _ret_ = -ENXIO; \
307         _ret_; \
308 })
309
310 /**
311  * fwnode_property_read_u8_array - return a u8 array property of firmware node
312  * @fwnode: Firmware node to get the property of
313  * @propname: Name of the property
314  * @val: The values are stored here or %NULL to return the number of values
315  * @nval: Size of the @val array
316  *
317  * Read an array of u8 properties with @propname from @fwnode and stores them to
318  * @val if found.
319  *
320  * Return: number of values if @val was %NULL,
321  *         %0 if the property was found (success),
322  *         %-EINVAL if given arguments are not valid,
323  *         %-ENODATA if the property does not have a value,
324  *         %-EPROTO if the property is not an array of numbers,
325  *         %-EOVERFLOW if the size of the property is not as expected,
326  *         %-ENXIO if no suitable firmware interface is present.
327  */
328 int fwnode_property_read_u8_array(struct fwnode_handle *fwnode,
329                                   const char *propname, u8 *val, size_t nval)
330 {
331         return FWNODE_PROP_READ_ARRAY(fwnode, propname, u8, DEV_PROP_U8,
332                                       val, nval);
333 }
334 EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
335
336 /**
337  * fwnode_property_read_u16_array - return a u16 array property of firmware node
338  * @fwnode: Firmware node to get the property of
339  * @propname: Name of the property
340  * @val: The values are stored here or %NULL to return the number of values
341  * @nval: Size of the @val array
342  *
343  * Read an array of u16 properties with @propname from @fwnode and store them to
344  * @val if found.
345  *
346  * Return: number of values if @val was %NULL,
347  *         %0 if the property was found (success),
348  *         %-EINVAL if given arguments are not valid,
349  *         %-ENODATA if the property does not have a value,
350  *         %-EPROTO if the property is not an array of numbers,
351  *         %-EOVERFLOW if the size of the property is not as expected,
352  *         %-ENXIO if no suitable firmware interface is present.
353  */
354 int fwnode_property_read_u16_array(struct fwnode_handle *fwnode,
355                                    const char *propname, u16 *val, size_t nval)
356 {
357         return FWNODE_PROP_READ_ARRAY(fwnode, propname, u16, DEV_PROP_U16,
358                                       val, nval);
359 }
360 EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
361
362 /**
363  * fwnode_property_read_u32_array - return a u32 array property of firmware node
364  * @fwnode: Firmware node to get the property of
365  * @propname: Name of the property
366  * @val: The values are stored here or %NULL to return the number of values
367  * @nval: Size of the @val array
368  *
369  * Read an array of u32 properties with @propname from @fwnode store them to
370  * @val if found.
371  *
372  * Return: number of values if @val was %NULL,
373  *         %0 if the property was found (success),
374  *         %-EINVAL if given arguments are not valid,
375  *         %-ENODATA if the property does not have a value,
376  *         %-EPROTO if the property is not an array of numbers,
377  *         %-EOVERFLOW if the size of the property is not as expected,
378  *         %-ENXIO if no suitable firmware interface is present.
379  */
380 int fwnode_property_read_u32_array(struct fwnode_handle *fwnode,
381                                    const char *propname, u32 *val, size_t nval)
382 {
383         return FWNODE_PROP_READ_ARRAY(fwnode, propname, u32, DEV_PROP_U32,
384                                       val, nval);
385 }
386 EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
387
388 /**
389  * fwnode_property_read_u64_array - return a u64 array property firmware node
390  * @fwnode: Firmware node to get the property of
391  * @propname: Name of the property
392  * @val: The values are stored here or %NULL to return the number of values
393  * @nval: Size of the @val array
394  *
395  * Read an array of u64 properties with @propname from @fwnode and store them to
396  * @val if found.
397  *
398  * Return: number of values if @val was %NULL,
399  *         %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 if the property is not an array of numbers,
403  *         %-EOVERFLOW if the size of the property is not as expected,
404  *         %-ENXIO if no suitable firmware interface is present.
405  */
406 int fwnode_property_read_u64_array(struct fwnode_handle *fwnode,
407                                    const char *propname, u64 *val, size_t nval)
408 {
409         return FWNODE_PROP_READ_ARRAY(fwnode, propname, u64, DEV_PROP_U64,
410                                       val, nval);
411 }
412 EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
413
414 /**
415  * fwnode_property_read_string_array - return string array property of a node
416  * @fwnode: Firmware node to get the property of
417  * @propname: Name of the property
418  * @val: The values are stored here or %NULL to return the number of values
419  * @nval: Size of the @val array
420  *
421  * Read an string list property @propname from the given firmware node and store
422  * them to @val if found.
423  *
424  * Return: number of values if @val was %NULL,
425  *         %0 if the property was found (success),
426  *         %-EINVAL if given arguments are not valid,
427  *         %-ENODATA if the property does not have a value,
428  *         %-EPROTO if the property is not an array of strings,
429  *         %-EOVERFLOW if the size of the property is not as expected,
430  *         %-ENXIO if no suitable firmware interface is present.
431  */
432 int fwnode_property_read_string_array(struct fwnode_handle *fwnode,
433                                       const char *propname, const char **val,
434                                       size_t nval)
435 {
436         if (is_of_node(fwnode))
437                 return val ?
438                         of_property_read_string_array(to_of_node(fwnode),
439                                                       propname, val, nval) :
440                         of_property_count_strings(to_of_node(fwnode), propname);
441         else if (is_acpi_node(fwnode))
442                 return acpi_dev_prop_read(to_acpi_node(fwnode), propname,
443                                           DEV_PROP_STRING, val, nval);
444         else if (is_pset(fwnode))
445                 return pset_prop_read_array(to_pset(fwnode), propname,
446                                             DEV_PROP_STRING, val, nval);
447         return -ENXIO;
448 }
449 EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
450
451 /**
452  * fwnode_property_read_string - return a string property of a firmware node
453  * @fwnode: Firmware node to get the property of
454  * @propname: Name of the property
455  * @val: The value is stored here
456  *
457  * Read property @propname from the given firmware node and store the value into
458  * @val if found.  The value is checked to be a string.
459  *
460  * Return: %0 if the property was found (success),
461  *         %-EINVAL if given arguments are not valid,
462  *         %-ENODATA if the property does not have a value,
463  *         %-EPROTO or %-EILSEQ if the property is not a string,
464  *         %-ENXIO if no suitable firmware interface is present.
465  */
466 int fwnode_property_read_string(struct fwnode_handle *fwnode,
467                                 const char *propname, const char **val)
468 {
469         if (is_of_node(fwnode))
470                 return of_property_read_string(to_of_node(fwnode), propname, val);
471         else if (is_acpi_node(fwnode))
472                 return acpi_dev_prop_read(to_acpi_node(fwnode), propname,
473                                           DEV_PROP_STRING, val, 1);
474
475         return -ENXIO;
476 }
477 EXPORT_SYMBOL_GPL(fwnode_property_read_string);
478
479 /**
480  * device_get_next_child_node - Return the next child node handle for a device
481  * @dev: Device to find the next child node for.
482  * @child: Handle to one of the device's child nodes or a null handle.
483  */
484 struct fwnode_handle *device_get_next_child_node(struct device *dev,
485                                                  struct fwnode_handle *child)
486 {
487         if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
488                 struct device_node *node;
489
490                 node = of_get_next_available_child(dev->of_node, to_of_node(child));
491                 if (node)
492                         return &node->fwnode;
493         } else if (IS_ENABLED(CONFIG_ACPI)) {
494                 struct acpi_device *node;
495
496                 node = acpi_get_next_child(dev, to_acpi_node(child));
497                 if (node)
498                         return acpi_fwnode_handle(node);
499         }
500         return NULL;
501 }
502 EXPORT_SYMBOL_GPL(device_get_next_child_node);
503
504 /**
505  * fwnode_handle_put - Drop reference to a device node
506  * @fwnode: Pointer to the device node to drop the reference to.
507  *
508  * This has to be used when terminating device_for_each_child_node() iteration
509  * with break or return to prevent stale device node references from being left
510  * behind.
511  */
512 void fwnode_handle_put(struct fwnode_handle *fwnode)
513 {
514         if (is_of_node(fwnode))
515                 of_node_put(to_of_node(fwnode));
516 }
517 EXPORT_SYMBOL_GPL(fwnode_handle_put);
518
519 /**
520  * device_get_child_node_count - return the number of child nodes for device
521  * @dev: Device to cound the child nodes for
522  */
523 unsigned int device_get_child_node_count(struct device *dev)
524 {
525         struct fwnode_handle *child;
526         unsigned int count = 0;
527
528         device_for_each_child_node(dev, child)
529                 count++;
530
531         return count;
532 }
533 EXPORT_SYMBOL_GPL(device_get_child_node_count);
534
535 bool device_dma_is_coherent(struct device *dev)
536 {
537         bool coherent = false;
538
539         if (IS_ENABLED(CONFIG_OF) && dev->of_node)
540                 coherent = of_dma_is_coherent(dev->of_node);
541         else
542                 acpi_check_dma(ACPI_COMPANION(dev), &coherent);
543
544         return coherent;
545 }
546 EXPORT_SYMBOL_GPL(device_dma_is_coherent);
547
548 /**
549  * device_get_phy_mode - Get phy mode for given device
550  * @dev:        Pointer to the given device
551  *
552  * The function gets phy interface string from property 'phy-mode' or
553  * 'phy-connection-type', and return its index in phy_modes table, or errno in
554  * error case.
555  */
556 int device_get_phy_mode(struct device *dev)
557 {
558         const char *pm;
559         int err, i;
560
561         err = device_property_read_string(dev, "phy-mode", &pm);
562         if (err < 0)
563                 err = device_property_read_string(dev,
564                                                   "phy-connection-type", &pm);
565         if (err < 0)
566                 return err;
567
568         for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
569                 if (!strcasecmp(pm, phy_modes(i)))
570                         return i;
571
572         return -ENODEV;
573 }
574 EXPORT_SYMBOL_GPL(device_get_phy_mode);
575
576 static void *device_get_mac_addr(struct device *dev,
577                                  const char *name, char *addr,
578                                  int alen)
579 {
580         int ret = device_property_read_u8_array(dev, name, addr, alen);
581
582         if (ret == 0 && alen == ETH_ALEN && is_valid_ether_addr(addr))
583                 return addr;
584         return NULL;
585 }
586
587 /**
588  * device_get_mac_address - Get the MAC for a given device
589  * @dev:        Pointer to the device
590  * @addr:       Address of buffer to store the MAC in
591  * @alen:       Length of the buffer pointed to by addr, should be ETH_ALEN
592  *
593  * Search the firmware node for the best MAC address to use.  'mac-address' is
594  * checked first, because that is supposed to contain to "most recent" MAC
595  * address. If that isn't set, then 'local-mac-address' is checked next,
596  * because that is the default address.  If that isn't set, then the obsolete
597  * 'address' is checked, just in case we're using an old device tree.
598  *
599  * Note that the 'address' property is supposed to contain a virtual address of
600  * the register set, but some DTS files have redefined that property to be the
601  * MAC address.
602  *
603  * All-zero MAC addresses are rejected, because those could be properties that
604  * exist in the firmware tables, but were not updated by the firmware.  For
605  * example, the DTS could define 'mac-address' and 'local-mac-address', with
606  * zero MAC addresses.  Some older U-Boots only initialized 'local-mac-address'.
607  * In this case, the real MAC is in 'local-mac-address', and 'mac-address'
608  * exists but is all zeros.
609 */
610 void *device_get_mac_address(struct device *dev, char *addr, int alen)
611 {
612         addr = device_get_mac_addr(dev, "mac-address", addr, alen);
613         if (addr)
614                 return addr;
615
616         addr = device_get_mac_addr(dev, "local-mac-address", addr, alen);
617         if (addr)
618                 return addr;
619
620         return device_get_mac_addr(dev, "address", addr, alen);
621 }
622 EXPORT_SYMBOL(device_get_mac_address);