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