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