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