Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 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 | |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 13 | #include <linux/acpi.h> |
Rafael J. Wysocki | 16ba08d | 2015-04-03 16:05:11 +0200 | [diff] [blame] | 14 | #include <linux/export.h> |
| 15 | #include <linux/kernel.h> |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 16 | #include <linux/of.h> |
Suthikulpanit, Suravee | 05ca556 | 2015-06-10 11:08:54 -0500 | [diff] [blame] | 17 | #include <linux/of_address.h> |
Rafael J. Wysocki | 16ba08d | 2015-04-03 16:05:11 +0200 | [diff] [blame] | 18 | #include <linux/property.h> |
Jeremy Linton | 4c96b7d | 2015-08-12 17:06:26 -0500 | [diff] [blame^] | 19 | #include <linux/etherdevice.h> |
| 20 | #include <linux/phy.h> |
Rafael J. Wysocki | 16ba08d | 2015-04-03 16:05:11 +0200 | [diff] [blame] | 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 | } |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 106 | |
Rafael J. Wysocki | 9017f25 | 2015-03-24 00:24:16 +0100 | [diff] [blame] | 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 | } |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 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 | { |
Rafael J. Wysocki | 9017f25 | 2015-03-24 00:24:16 +0100 | [diff] [blame] | 122 | return fwnode_property_present(dev_fwnode(dev), propname); |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 123 | } |
| 124 | EXPORT_SYMBOL_GPL(device_property_present); |
| 125 | |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 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)) |
Alexander Sverdlin | c181fb3 | 2015-06-22 22:38:53 +0200 | [diff] [blame] | 134 | return of_property_read_bool(to_of_node(fwnode), propname); |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 135 | else if (is_acpi_node(fwnode)) |
Alexander Sverdlin | c181fb3 | 2015-06-22 22:38:53 +0200 | [diff] [blame] | 136 | return !acpi_dev_prop_get(to_acpi_node(fwnode), propname, NULL); |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 137 | |
Rafael J. Wysocki | 16ba08d | 2015-04-03 16:05:11 +0200 | [diff] [blame] | 138 | return !!pset_prop_get(to_pset(fwnode), propname); |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 139 | } |
| 140 | EXPORT_SYMBOL_GPL(fwnode_property_present); |
| 141 | |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 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 |
Adrian Hunter | 5c0acf3 | 2015-03-17 09:58:58 +0200 | [diff] [blame] | 146 | * @val: The values are stored here or %NULL to return the number of values |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 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 | * |
Adrian Hunter | 5c0acf3 | 2015-03-17 09:58:58 +0200 | [diff] [blame] | 152 | * Return: number of values if @val was %NULL, |
| 153 | * %0 if the property was found (success), |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 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 | */ |
| 159 | int device_property_read_u8_array(struct device *dev, const char *propname, |
| 160 | u8 *val, size_t nval) |
| 161 | { |
Rafael J. Wysocki | 9017f25 | 2015-03-24 00:24:16 +0100 | [diff] [blame] | 162 | return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval); |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 163 | } |
| 164 | EXPORT_SYMBOL_GPL(device_property_read_u8_array); |
| 165 | |
| 166 | /** |
| 167 | * device_property_read_u16_array - return a u16 array property of a device |
| 168 | * @dev: Device to get the property of |
| 169 | * @propname: Name of the property |
Adrian Hunter | 5c0acf3 | 2015-03-17 09:58:58 +0200 | [diff] [blame] | 170 | * @val: The values are stored here or %NULL to return the number of values |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 171 | * @nval: Size of the @val array |
| 172 | * |
| 173 | * Function reads an array of u16 properties with @propname from the device |
| 174 | * firmware description and stores them to @val if found. |
| 175 | * |
Adrian Hunter | 5c0acf3 | 2015-03-17 09:58:58 +0200 | [diff] [blame] | 176 | * Return: number of values if @val was %NULL, |
| 177 | * %0 if the property was found (success), |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 178 | * %-EINVAL if given arguments are not valid, |
| 179 | * %-ENODATA if the property does not have a value, |
| 180 | * %-EPROTO if the property is not an array of numbers, |
| 181 | * %-EOVERFLOW if the size of the property is not as expected. |
| 182 | */ |
| 183 | int device_property_read_u16_array(struct device *dev, const char *propname, |
| 184 | u16 *val, size_t nval) |
| 185 | { |
Rafael J. Wysocki | 9017f25 | 2015-03-24 00:24:16 +0100 | [diff] [blame] | 186 | return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval); |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 187 | } |
| 188 | EXPORT_SYMBOL_GPL(device_property_read_u16_array); |
| 189 | |
| 190 | /** |
| 191 | * device_property_read_u32_array - return a u32 array property of a device |
| 192 | * @dev: Device to get the property of |
| 193 | * @propname: Name of the property |
Adrian Hunter | 5c0acf3 | 2015-03-17 09:58:58 +0200 | [diff] [blame] | 194 | * @val: The values are stored here or %NULL to return the number of values |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 195 | * @nval: Size of the @val array |
| 196 | * |
| 197 | * Function reads an array of u32 properties with @propname from the device |
| 198 | * firmware description and stores them to @val if found. |
| 199 | * |
Adrian Hunter | 5c0acf3 | 2015-03-17 09:58:58 +0200 | [diff] [blame] | 200 | * Return: number of values if @val was %NULL, |
| 201 | * %0 if the property was found (success), |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 202 | * %-EINVAL if given arguments are not valid, |
| 203 | * %-ENODATA if the property does not have a value, |
| 204 | * %-EPROTO if the property is not an array of numbers, |
| 205 | * %-EOVERFLOW if the size of the property is not as expected. |
| 206 | */ |
| 207 | int device_property_read_u32_array(struct device *dev, const char *propname, |
| 208 | u32 *val, size_t nval) |
| 209 | { |
Rafael J. Wysocki | 9017f25 | 2015-03-24 00:24:16 +0100 | [diff] [blame] | 210 | return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval); |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 211 | } |
| 212 | EXPORT_SYMBOL_GPL(device_property_read_u32_array); |
| 213 | |
| 214 | /** |
| 215 | * device_property_read_u64_array - return a u64 array property of a device |
| 216 | * @dev: Device to get the property of |
| 217 | * @propname: Name of the property |
Adrian Hunter | 5c0acf3 | 2015-03-17 09:58:58 +0200 | [diff] [blame] | 218 | * @val: The values are stored here or %NULL to return the number of values |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 219 | * @nval: Size of the @val array |
| 220 | * |
| 221 | * Function reads an array of u64 properties with @propname from the device |
| 222 | * firmware description and stores them to @val if found. |
| 223 | * |
Adrian Hunter | 5c0acf3 | 2015-03-17 09:58:58 +0200 | [diff] [blame] | 224 | * Return: number of values if @val was %NULL, |
| 225 | * %0 if the property was found (success), |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 226 | * %-EINVAL if given arguments are not valid, |
| 227 | * %-ENODATA if the property does not have a value, |
| 228 | * %-EPROTO if the property is not an array of numbers, |
| 229 | * %-EOVERFLOW if the size of the property is not as expected. |
| 230 | */ |
| 231 | int device_property_read_u64_array(struct device *dev, const char *propname, |
| 232 | u64 *val, size_t nval) |
| 233 | { |
Rafael J. Wysocki | 9017f25 | 2015-03-24 00:24:16 +0100 | [diff] [blame] | 234 | return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval); |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 235 | } |
| 236 | EXPORT_SYMBOL_GPL(device_property_read_u64_array); |
| 237 | |
| 238 | /** |
| 239 | * device_property_read_string_array - return a string array property of device |
| 240 | * @dev: Device to get the property of |
| 241 | * @propname: Name of the property |
Adrian Hunter | 5c0acf3 | 2015-03-17 09:58:58 +0200 | [diff] [blame] | 242 | * @val: The values are stored here or %NULL to return the number of values |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 243 | * @nval: Size of the @val array |
| 244 | * |
| 245 | * Function reads an array of string properties with @propname from the device |
| 246 | * firmware description and stores them to @val if found. |
| 247 | * |
Adrian Hunter | 5c0acf3 | 2015-03-17 09:58:58 +0200 | [diff] [blame] | 248 | * Return: number of values if @val was %NULL, |
| 249 | * %0 if the property was found (success), |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 250 | * %-EINVAL if given arguments are not valid, |
| 251 | * %-ENODATA if the property does not have a value, |
| 252 | * %-EPROTO or %-EILSEQ if the property is not an array of strings, |
| 253 | * %-EOVERFLOW if the size of the property is not as expected. |
| 254 | */ |
| 255 | int device_property_read_string_array(struct device *dev, const char *propname, |
| 256 | const char **val, size_t nval) |
| 257 | { |
Rafael J. Wysocki | 9017f25 | 2015-03-24 00:24:16 +0100 | [diff] [blame] | 258 | return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval); |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 259 | } |
| 260 | EXPORT_SYMBOL_GPL(device_property_read_string_array); |
| 261 | |
| 262 | /** |
| 263 | * device_property_read_string - return a string property of a device |
| 264 | * @dev: Device to get the property of |
| 265 | * @propname: Name of the property |
| 266 | * @val: The value is stored here |
| 267 | * |
| 268 | * Function reads property @propname from the device firmware description and |
| 269 | * stores the value into @val if found. The value is checked to be a string. |
| 270 | * |
| 271 | * Return: %0 if the property was found (success), |
| 272 | * %-EINVAL if given arguments are not valid, |
| 273 | * %-ENODATA if the property does not have a value, |
| 274 | * %-EPROTO or %-EILSEQ if the property type is not a string. |
| 275 | */ |
| 276 | int device_property_read_string(struct device *dev, const char *propname, |
| 277 | const char **val) |
| 278 | { |
Rafael J. Wysocki | 9017f25 | 2015-03-24 00:24:16 +0100 | [diff] [blame] | 279 | return fwnode_property_read_string(dev_fwnode(dev), propname, val); |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 280 | } |
| 281 | EXPORT_SYMBOL_GPL(device_property_read_string); |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 282 | |
Rafael J. Wysocki | 9017f25 | 2015-03-24 00:24:16 +0100 | [diff] [blame] | 283 | #define OF_DEV_PROP_READ_ARRAY(node, propname, type, val, nval) \ |
| 284 | (val) ? of_property_read_##type##_array((node), (propname), (val), (nval)) \ |
| 285 | : of_property_count_elems_of_size((node), (propname), sizeof(type)) |
| 286 | |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 287 | #define FWNODE_PROP_READ_ARRAY(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \ |
| 288 | ({ \ |
| 289 | int _ret_; \ |
| 290 | if (is_of_node(_fwnode_)) \ |
Alexander Sverdlin | c181fb3 | 2015-06-22 22:38:53 +0200 | [diff] [blame] | 291 | _ret_ = OF_DEV_PROP_READ_ARRAY(to_of_node(_fwnode_), _propname_, \ |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 292 | _type_, _val_, _nval_); \ |
| 293 | else if (is_acpi_node(_fwnode_)) \ |
Alexander Sverdlin | c181fb3 | 2015-06-22 22:38:53 +0200 | [diff] [blame] | 294 | _ret_ = acpi_dev_prop_read(to_acpi_node(_fwnode_), _propname_, \ |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 295 | _proptype_, _val_, _nval_); \ |
| 296 | else \ |
Rafael J. Wysocki | 16ba08d | 2015-04-03 16:05:11 +0200 | [diff] [blame] | 297 | _ret_ = pset_prop_read_array(to_pset(_fwnode_), _propname_, \ |
| 298 | _proptype_, _val_, _nval_); \ |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 299 | _ret_; \ |
| 300 | }) |
| 301 | |
| 302 | /** |
| 303 | * fwnode_property_read_u8_array - return a u8 array property of firmware node |
| 304 | * @fwnode: Firmware node to get the property of |
| 305 | * @propname: Name of the property |
Adrian Hunter | 5c0acf3 | 2015-03-17 09:58:58 +0200 | [diff] [blame] | 306 | * @val: The values are stored here or %NULL to return the number of values |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 307 | * @nval: Size of the @val array |
| 308 | * |
| 309 | * Read an array of u8 properties with @propname from @fwnode and stores them to |
| 310 | * @val if found. |
| 311 | * |
Adrian Hunter | 5c0acf3 | 2015-03-17 09:58:58 +0200 | [diff] [blame] | 312 | * Return: number of values if @val was %NULL, |
| 313 | * %0 if the property was found (success), |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 314 | * %-EINVAL if given arguments are not valid, |
| 315 | * %-ENODATA if the property does not have a value, |
| 316 | * %-EPROTO if the property is not an array of numbers, |
| 317 | * %-EOVERFLOW if the size of the property is not as expected, |
| 318 | * %-ENXIO if no suitable firmware interface is present. |
| 319 | */ |
| 320 | int fwnode_property_read_u8_array(struct fwnode_handle *fwnode, |
| 321 | const char *propname, u8 *val, size_t nval) |
| 322 | { |
| 323 | return FWNODE_PROP_READ_ARRAY(fwnode, propname, u8, DEV_PROP_U8, |
| 324 | val, nval); |
| 325 | } |
| 326 | EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array); |
| 327 | |
| 328 | /** |
| 329 | * fwnode_property_read_u16_array - return a u16 array property of firmware node |
| 330 | * @fwnode: Firmware node to get the property of |
| 331 | * @propname: Name of the property |
Adrian Hunter | 5c0acf3 | 2015-03-17 09:58:58 +0200 | [diff] [blame] | 332 | * @val: The values are stored here or %NULL to return the number of values |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 333 | * @nval: Size of the @val array |
| 334 | * |
| 335 | * Read an array of u16 properties with @propname from @fwnode and store them to |
| 336 | * @val if found. |
| 337 | * |
Adrian Hunter | 5c0acf3 | 2015-03-17 09:58:58 +0200 | [diff] [blame] | 338 | * Return: number of values if @val was %NULL, |
| 339 | * %0 if the property was found (success), |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 340 | * %-EINVAL if given arguments are not valid, |
| 341 | * %-ENODATA if the property does not have a value, |
| 342 | * %-EPROTO if the property is not an array of numbers, |
| 343 | * %-EOVERFLOW if the size of the property is not as expected, |
| 344 | * %-ENXIO if no suitable firmware interface is present. |
| 345 | */ |
| 346 | int fwnode_property_read_u16_array(struct fwnode_handle *fwnode, |
| 347 | const char *propname, u16 *val, size_t nval) |
| 348 | { |
| 349 | return FWNODE_PROP_READ_ARRAY(fwnode, propname, u16, DEV_PROP_U16, |
| 350 | val, nval); |
| 351 | } |
| 352 | EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array); |
| 353 | |
| 354 | /** |
| 355 | * fwnode_property_read_u32_array - return a u32 array property of firmware node |
| 356 | * @fwnode: Firmware node to get the property of |
| 357 | * @propname: Name of the property |
Adrian Hunter | 5c0acf3 | 2015-03-17 09:58:58 +0200 | [diff] [blame] | 358 | * @val: The values are stored here or %NULL to return the number of values |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 359 | * @nval: Size of the @val array |
| 360 | * |
| 361 | * Read an array of u32 properties with @propname from @fwnode store them to |
| 362 | * @val if found. |
| 363 | * |
Adrian Hunter | 5c0acf3 | 2015-03-17 09:58:58 +0200 | [diff] [blame] | 364 | * Return: number of values if @val was %NULL, |
| 365 | * %0 if the property was found (success), |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 366 | * %-EINVAL if given arguments are not valid, |
| 367 | * %-ENODATA if the property does not have a value, |
| 368 | * %-EPROTO if the property is not an array of numbers, |
| 369 | * %-EOVERFLOW if the size of the property is not as expected, |
| 370 | * %-ENXIO if no suitable firmware interface is present. |
| 371 | */ |
| 372 | int fwnode_property_read_u32_array(struct fwnode_handle *fwnode, |
| 373 | const char *propname, u32 *val, size_t nval) |
| 374 | { |
| 375 | return FWNODE_PROP_READ_ARRAY(fwnode, propname, u32, DEV_PROP_U32, |
| 376 | val, nval); |
| 377 | } |
| 378 | EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array); |
| 379 | |
| 380 | /** |
| 381 | * fwnode_property_read_u64_array - return a u64 array property firmware node |
| 382 | * @fwnode: Firmware node to get the property of |
| 383 | * @propname: Name of the property |
Adrian Hunter | 5c0acf3 | 2015-03-17 09:58:58 +0200 | [diff] [blame] | 384 | * @val: The values are stored here or %NULL to return the number of values |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 385 | * @nval: Size of the @val array |
| 386 | * |
| 387 | * Read an array of u64 properties with @propname from @fwnode and store them to |
| 388 | * @val if found. |
| 389 | * |
Adrian Hunter | 5c0acf3 | 2015-03-17 09:58:58 +0200 | [diff] [blame] | 390 | * Return: number of values if @val was %NULL, |
| 391 | * %0 if the property was found (success), |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 392 | * %-EINVAL if given arguments are not valid, |
| 393 | * %-ENODATA if the property does not have a value, |
| 394 | * %-EPROTO if the property is not an array of numbers, |
| 395 | * %-EOVERFLOW if the size of the property is not as expected, |
| 396 | * %-ENXIO if no suitable firmware interface is present. |
| 397 | */ |
| 398 | int fwnode_property_read_u64_array(struct fwnode_handle *fwnode, |
| 399 | const char *propname, u64 *val, size_t nval) |
| 400 | { |
| 401 | return FWNODE_PROP_READ_ARRAY(fwnode, propname, u64, DEV_PROP_U64, |
| 402 | val, nval); |
| 403 | } |
| 404 | EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array); |
| 405 | |
| 406 | /** |
| 407 | * fwnode_property_read_string_array - return string array property of a node |
| 408 | * @fwnode: Firmware node to get the property of |
| 409 | * @propname: Name of the property |
Adrian Hunter | 5c0acf3 | 2015-03-17 09:58:58 +0200 | [diff] [blame] | 410 | * @val: The values are stored here or %NULL to return the number of values |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 411 | * @nval: Size of the @val array |
| 412 | * |
| 413 | * Read an string list property @propname from the given firmware node and store |
| 414 | * them to @val if found. |
| 415 | * |
Adrian Hunter | 5c0acf3 | 2015-03-17 09:58:58 +0200 | [diff] [blame] | 416 | * Return: number of values if @val was %NULL, |
| 417 | * %0 if the property was found (success), |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 418 | * %-EINVAL if given arguments are not valid, |
| 419 | * %-ENODATA if the property does not have a value, |
| 420 | * %-EPROTO if the property is not an array of strings, |
| 421 | * %-EOVERFLOW if the size of the property is not as expected, |
| 422 | * %-ENXIO if no suitable firmware interface is present. |
| 423 | */ |
| 424 | int fwnode_property_read_string_array(struct fwnode_handle *fwnode, |
| 425 | const char *propname, const char **val, |
| 426 | size_t nval) |
| 427 | { |
| 428 | if (is_of_node(fwnode)) |
Rafael J. Wysocki | f42712a | 2015-03-24 00:18:05 +0100 | [diff] [blame] | 429 | return val ? |
Alexander Sverdlin | c181fb3 | 2015-06-22 22:38:53 +0200 | [diff] [blame] | 430 | of_property_read_string_array(to_of_node(fwnode), |
| 431 | propname, val, nval) : |
| 432 | of_property_count_strings(to_of_node(fwnode), propname); |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 433 | else if (is_acpi_node(fwnode)) |
Alexander Sverdlin | c181fb3 | 2015-06-22 22:38:53 +0200 | [diff] [blame] | 434 | return acpi_dev_prop_read(to_acpi_node(fwnode), propname, |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 435 | DEV_PROP_STRING, val, nval); |
| 436 | |
Rafael J. Wysocki | 16ba08d | 2015-04-03 16:05:11 +0200 | [diff] [blame] | 437 | return pset_prop_read_array(to_pset(fwnode), propname, |
| 438 | DEV_PROP_STRING, val, nval); |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 439 | } |
| 440 | EXPORT_SYMBOL_GPL(fwnode_property_read_string_array); |
| 441 | |
| 442 | /** |
| 443 | * fwnode_property_read_string - return a string property of a firmware node |
| 444 | * @fwnode: Firmware node to get the property of |
| 445 | * @propname: Name of the property |
| 446 | * @val: The value is stored here |
| 447 | * |
| 448 | * Read property @propname from the given firmware node and store the value into |
| 449 | * @val if found. The value is checked to be a string. |
| 450 | * |
| 451 | * Return: %0 if the property was found (success), |
| 452 | * %-EINVAL if given arguments are not valid, |
| 453 | * %-ENODATA if the property does not have a value, |
| 454 | * %-EPROTO or %-EILSEQ if the property is not a string, |
| 455 | * %-ENXIO if no suitable firmware interface is present. |
| 456 | */ |
| 457 | int fwnode_property_read_string(struct fwnode_handle *fwnode, |
| 458 | const char *propname, const char **val) |
| 459 | { |
| 460 | if (is_of_node(fwnode)) |
Alexander Sverdlin | c181fb3 | 2015-06-22 22:38:53 +0200 | [diff] [blame] | 461 | return of_property_read_string(to_of_node(fwnode), propname, val); |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 462 | else if (is_acpi_node(fwnode)) |
Alexander Sverdlin | c181fb3 | 2015-06-22 22:38:53 +0200 | [diff] [blame] | 463 | return acpi_dev_prop_read(to_acpi_node(fwnode), propname, |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 464 | DEV_PROP_STRING, val, 1); |
| 465 | |
| 466 | return -ENXIO; |
| 467 | } |
| 468 | EXPORT_SYMBOL_GPL(fwnode_property_read_string); |
| 469 | |
| 470 | /** |
| 471 | * device_get_next_child_node - Return the next child node handle for a device |
| 472 | * @dev: Device to find the next child node for. |
| 473 | * @child: Handle to one of the device's child nodes or a null handle. |
| 474 | */ |
| 475 | struct fwnode_handle *device_get_next_child_node(struct device *dev, |
| 476 | struct fwnode_handle *child) |
| 477 | { |
| 478 | if (IS_ENABLED(CONFIG_OF) && dev->of_node) { |
| 479 | struct device_node *node; |
| 480 | |
Alexander Sverdlin | c181fb3 | 2015-06-22 22:38:53 +0200 | [diff] [blame] | 481 | node = of_get_next_available_child(dev->of_node, to_of_node(child)); |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 482 | if (node) |
| 483 | return &node->fwnode; |
| 484 | } else if (IS_ENABLED(CONFIG_ACPI)) { |
| 485 | struct acpi_device *node; |
| 486 | |
Alexander Sverdlin | c181fb3 | 2015-06-22 22:38:53 +0200 | [diff] [blame] | 487 | node = acpi_get_next_child(dev, to_acpi_node(child)); |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 488 | if (node) |
| 489 | return acpi_fwnode_handle(node); |
| 490 | } |
| 491 | return NULL; |
| 492 | } |
| 493 | EXPORT_SYMBOL_GPL(device_get_next_child_node); |
| 494 | |
| 495 | /** |
| 496 | * fwnode_handle_put - Drop reference to a device node |
| 497 | * @fwnode: Pointer to the device node to drop the reference to. |
| 498 | * |
| 499 | * This has to be used when terminating device_for_each_child_node() iteration |
| 500 | * with break or return to prevent stale device node references from being left |
| 501 | * behind. |
| 502 | */ |
| 503 | void fwnode_handle_put(struct fwnode_handle *fwnode) |
| 504 | { |
| 505 | if (is_of_node(fwnode)) |
Alexander Sverdlin | c181fb3 | 2015-06-22 22:38:53 +0200 | [diff] [blame] | 506 | of_node_put(to_of_node(fwnode)); |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 507 | } |
| 508 | EXPORT_SYMBOL_GPL(fwnode_handle_put); |
| 509 | |
| 510 | /** |
| 511 | * device_get_child_node_count - return the number of child nodes for device |
| 512 | * @dev: Device to cound the child nodes for |
| 513 | */ |
| 514 | unsigned int device_get_child_node_count(struct device *dev) |
| 515 | { |
| 516 | struct fwnode_handle *child; |
| 517 | unsigned int count = 0; |
| 518 | |
| 519 | device_for_each_child_node(dev, child) |
| 520 | count++; |
| 521 | |
| 522 | return count; |
| 523 | } |
| 524 | EXPORT_SYMBOL_GPL(device_get_child_node_count); |
Suthikulpanit, Suravee | 05ca556 | 2015-06-10 11:08:54 -0500 | [diff] [blame] | 525 | |
| 526 | bool device_dma_is_coherent(struct device *dev) |
| 527 | { |
| 528 | bool coherent = false; |
| 529 | |
| 530 | if (IS_ENABLED(CONFIG_OF) && dev->of_node) |
| 531 | coherent = of_dma_is_coherent(dev->of_node); |
| 532 | else |
| 533 | acpi_check_dma(ACPI_COMPANION(dev), &coherent); |
| 534 | |
| 535 | return coherent; |
| 536 | } |
| 537 | EXPORT_SYMBOL_GPL(device_dma_is_coherent); |
Jeremy Linton | 4c96b7d | 2015-08-12 17:06:26 -0500 | [diff] [blame^] | 538 | |
| 539 | /** |
| 540 | * device_get_phy_mode - Get phy mode for given device_node |
| 541 | * @dev: Pointer to the given device |
| 542 | * |
| 543 | * The function gets phy interface string from property 'phy-mode' or |
| 544 | * 'phy-connection-type', and return its index in phy_modes table, or errno in |
| 545 | * error case. |
| 546 | */ |
| 547 | int device_get_phy_mode(struct device *dev) |
| 548 | { |
| 549 | const char *pm; |
| 550 | int err, i; |
| 551 | |
| 552 | err = device_property_read_string(dev, "phy-mode", &pm); |
| 553 | if (err < 0) |
| 554 | err = device_property_read_string(dev, |
| 555 | "phy-connection-type", &pm); |
| 556 | if (err < 0) |
| 557 | return err; |
| 558 | |
| 559 | for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++) |
| 560 | if (!strcasecmp(pm, phy_modes(i))) |
| 561 | return i; |
| 562 | |
| 563 | return -ENODEV; |
| 564 | } |
| 565 | EXPORT_SYMBOL_GPL(device_get_phy_mode); |
| 566 | |
| 567 | static void *device_get_mac_addr(struct device *dev, |
| 568 | const char *name, char *addr, |
| 569 | int alen) |
| 570 | { |
| 571 | int ret = device_property_read_u8_array(dev, name, addr, alen); |
| 572 | |
| 573 | if (ret == 0 && is_valid_ether_addr(addr)) |
| 574 | return addr; |
| 575 | return NULL; |
| 576 | } |
| 577 | |
| 578 | /** |
| 579 | * Search the device tree for the best MAC address to use. 'mac-address' is |
| 580 | * checked first, because that is supposed to contain to "most recent" MAC |
| 581 | * address. If that isn't set, then 'local-mac-address' is checked next, |
| 582 | * because that is the default address. If that isn't set, then the obsolete |
| 583 | * 'address' is checked, just in case we're using an old device tree. |
| 584 | * |
| 585 | * Note that the 'address' property is supposed to contain a virtual address of |
| 586 | * the register set, but some DTS files have redefined that property to be the |
| 587 | * MAC address. |
| 588 | * |
| 589 | * All-zero MAC addresses are rejected, because those could be properties that |
| 590 | * exist in the device tree, but were not set by U-Boot. For example, the |
| 591 | * DTS could define 'mac-address' and 'local-mac-address', with zero MAC |
| 592 | * addresses. Some older U-Boots only initialized 'local-mac-address'. In |
| 593 | * this case, the real MAC is in 'local-mac-address', and 'mac-address' exists |
| 594 | * but is all zeros. |
| 595 | */ |
| 596 | void *device_get_mac_address(struct device *dev, char *addr, int alen) |
| 597 | { |
| 598 | addr = device_get_mac_addr(dev, "mac-address", addr, alen); |
| 599 | if (addr) |
| 600 | return addr; |
| 601 | |
| 602 | addr = device_get_mac_addr(dev, "local-mac-address", addr, alen); |
| 603 | if (addr) |
| 604 | return addr; |
| 605 | |
| 606 | return device_get_mac_addr(dev, "address", addr, alen); |
| 607 | } |
| 608 | EXPORT_SYMBOL(device_get_mac_address); |