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