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