Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Reset Controller framework |
| 3 | * |
| 4 | * Copyright 2013 Philipp Zabel, Pengutronix |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | */ |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 11 | #include <linux/atomic.h> |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 12 | #include <linux/device.h> |
| 13 | #include <linux/err.h> |
| 14 | #include <linux/export.h> |
| 15 | #include <linux/kernel.h> |
Philipp Zabel | d25e433 | 2017-05-31 17:42:29 +0200 | [diff] [blame] | 16 | #include <linux/kref.h> |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 17 | #include <linux/module.h> |
| 18 | #include <linux/of.h> |
| 19 | #include <linux/reset.h> |
| 20 | #include <linux/reset-controller.h> |
| 21 | #include <linux/slab.h> |
| 22 | |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 23 | static DEFINE_MUTEX(reset_list_mutex); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 24 | static LIST_HEAD(reset_controller_list); |
| 25 | |
Bartosz Golaszewski | 6691dff | 2018-02-28 14:08:57 +0100 | [diff] [blame] | 26 | static DEFINE_MUTEX(reset_lookup_mutex); |
| 27 | static LIST_HEAD(reset_lookup_list); |
| 28 | |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 29 | /** |
| 30 | * struct reset_control - a reset control |
| 31 | * @rcdev: a pointer to the reset controller device |
| 32 | * this reset control belongs to |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 33 | * @list: list entry for the rcdev's reset controller list |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 34 | * @id: ID of the reset controller in the reset |
| 35 | * controller device |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 36 | * @refcnt: Number of gets of this reset_control |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 37 | * @shared: Is this a shared (1), or an exclusive (0) reset_control? |
| 38 | * @deassert_cnt: Number of times this reset line has been deasserted |
Martin Blumenstingl | 7da33a3 | 2016-11-12 14:13:03 +0100 | [diff] [blame] | 39 | * @triggered_count: Number of times this reset line has been reset. Currently |
| 40 | * only used for shared resets, which means that the value |
| 41 | * will be either 0 or 1. |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 42 | */ |
| 43 | struct reset_control { |
| 44 | struct reset_controller_dev *rcdev; |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 45 | struct list_head list; |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 46 | unsigned int id; |
Philipp Zabel | d25e433 | 2017-05-31 17:42:29 +0200 | [diff] [blame] | 47 | struct kref refcnt; |
Ramiro Oliveira | ee48c72 | 2017-01-13 17:57:40 +0000 | [diff] [blame] | 48 | bool shared; |
Vivek Gautam | 17c82e2 | 2017-05-22 16:53:25 +0530 | [diff] [blame] | 49 | bool array; |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 50 | atomic_t deassert_count; |
Martin Blumenstingl | 7da33a3 | 2016-11-12 14:13:03 +0100 | [diff] [blame] | 51 | atomic_t triggered_count; |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | /** |
Vivek Gautam | 17c82e2 | 2017-05-22 16:53:25 +0530 | [diff] [blame] | 55 | * struct reset_control_array - an array of reset controls |
| 56 | * @base: reset control for compatibility with reset control API functions |
| 57 | * @num_rstcs: number of reset controls |
| 58 | * @rstc: array of reset controls |
| 59 | */ |
| 60 | struct reset_control_array { |
| 61 | struct reset_control base; |
| 62 | unsigned int num_rstcs; |
| 63 | struct reset_control *rstc[]; |
| 64 | }; |
| 65 | |
| 66 | /** |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 67 | * of_reset_simple_xlate - translate reset_spec to the reset line number |
| 68 | * @rcdev: a pointer to the reset controller device |
| 69 | * @reset_spec: reset line specifier as found in the device tree |
| 70 | * @flags: a flags pointer to fill in (optional) |
| 71 | * |
| 72 | * This simple translation function should be used for reset controllers |
| 73 | * with 1:1 mapping, where reset lines can be indexed by number without gaps. |
| 74 | */ |
Rashika Kheria | 0c5b2b9 | 2013-12-19 14:11:10 +0530 | [diff] [blame] | 75 | static int of_reset_simple_xlate(struct reset_controller_dev *rcdev, |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 76 | const struct of_phandle_args *reset_spec) |
| 77 | { |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 78 | if (reset_spec->args[0] >= rcdev->nr_resets) |
| 79 | return -EINVAL; |
| 80 | |
| 81 | return reset_spec->args[0]; |
| 82 | } |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * reset_controller_register - register a reset controller device |
| 86 | * @rcdev: a pointer to the initialized reset controller device |
| 87 | */ |
| 88 | int reset_controller_register(struct reset_controller_dev *rcdev) |
| 89 | { |
| 90 | if (!rcdev->of_xlate) { |
| 91 | rcdev->of_reset_n_cells = 1; |
| 92 | rcdev->of_xlate = of_reset_simple_xlate; |
| 93 | } |
| 94 | |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 95 | INIT_LIST_HEAD(&rcdev->reset_control_head); |
| 96 | |
| 97 | mutex_lock(&reset_list_mutex); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 98 | list_add(&rcdev->list, &reset_controller_list); |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 99 | mutex_unlock(&reset_list_mutex); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | EXPORT_SYMBOL_GPL(reset_controller_register); |
| 104 | |
| 105 | /** |
| 106 | * reset_controller_unregister - unregister a reset controller device |
| 107 | * @rcdev: a pointer to the reset controller device |
| 108 | */ |
| 109 | void reset_controller_unregister(struct reset_controller_dev *rcdev) |
| 110 | { |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 111 | mutex_lock(&reset_list_mutex); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 112 | list_del(&rcdev->list); |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 113 | mutex_unlock(&reset_list_mutex); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 114 | } |
| 115 | EXPORT_SYMBOL_GPL(reset_controller_unregister); |
| 116 | |
Masahiro Yamada | 8d5b5d5 | 2016-05-01 19:36:57 +0900 | [diff] [blame] | 117 | static void devm_reset_controller_release(struct device *dev, void *res) |
| 118 | { |
| 119 | reset_controller_unregister(*(struct reset_controller_dev **)res); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * devm_reset_controller_register - resource managed reset_controller_register() |
| 124 | * @dev: device that is registering this reset controller |
| 125 | * @rcdev: a pointer to the initialized reset controller device |
| 126 | * |
| 127 | * Managed reset_controller_register(). For reset controllers registered by |
| 128 | * this function, reset_controller_unregister() is automatically called on |
| 129 | * driver detach. See reset_controller_register() for more information. |
| 130 | */ |
| 131 | int devm_reset_controller_register(struct device *dev, |
| 132 | struct reset_controller_dev *rcdev) |
| 133 | { |
| 134 | struct reset_controller_dev **rcdevp; |
| 135 | int ret; |
| 136 | |
| 137 | rcdevp = devres_alloc(devm_reset_controller_release, sizeof(*rcdevp), |
| 138 | GFP_KERNEL); |
| 139 | if (!rcdevp) |
| 140 | return -ENOMEM; |
| 141 | |
| 142 | ret = reset_controller_register(rcdev); |
| 143 | if (!ret) { |
| 144 | *rcdevp = rcdev; |
| 145 | devres_add(dev, rcdevp); |
| 146 | } else { |
| 147 | devres_free(rcdevp); |
| 148 | } |
| 149 | |
| 150 | return ret; |
| 151 | } |
| 152 | EXPORT_SYMBOL_GPL(devm_reset_controller_register); |
| 153 | |
Bartosz Golaszewski | 6691dff | 2018-02-28 14:08:57 +0100 | [diff] [blame] | 154 | /** |
| 155 | * reset_controller_add_lookup - register a set of lookup entries |
Bartosz Golaszewski | 6691dff | 2018-02-28 14:08:57 +0100 | [diff] [blame] | 156 | * @lookup: array of reset lookup entries |
| 157 | * @num_entries: number of entries in the lookup array |
| 158 | */ |
Bartosz Golaszewski | e2749bb | 2018-03-23 14:04:48 +0100 | [diff] [blame] | 159 | void reset_controller_add_lookup(struct reset_control_lookup *lookup, |
Bartosz Golaszewski | 6691dff | 2018-02-28 14:08:57 +0100 | [diff] [blame] | 160 | unsigned int num_entries) |
| 161 | { |
| 162 | struct reset_control_lookup *entry; |
| 163 | unsigned int i; |
| 164 | |
| 165 | mutex_lock(&reset_lookup_mutex); |
| 166 | for (i = 0; i < num_entries; i++) { |
| 167 | entry = &lookup[i]; |
| 168 | |
Bartosz Golaszewski | e2749bb | 2018-03-23 14:04:48 +0100 | [diff] [blame] | 169 | if (!entry->dev_id || !entry->provider) { |
| 170 | pr_warn("%s(): reset lookup entry badly specified, skipping\n", |
Bartosz Golaszewski | 6691dff | 2018-02-28 14:08:57 +0100 | [diff] [blame] | 171 | __func__); |
| 172 | continue; |
| 173 | } |
| 174 | |
Bartosz Golaszewski | 6691dff | 2018-02-28 14:08:57 +0100 | [diff] [blame] | 175 | list_add_tail(&entry->list, &reset_lookup_list); |
| 176 | } |
| 177 | mutex_unlock(&reset_lookup_mutex); |
| 178 | } |
| 179 | EXPORT_SYMBOL_GPL(reset_controller_add_lookup); |
| 180 | |
Vivek Gautam | 17c82e2 | 2017-05-22 16:53:25 +0530 | [diff] [blame] | 181 | static inline struct reset_control_array * |
| 182 | rstc_to_array(struct reset_control *rstc) { |
| 183 | return container_of(rstc, struct reset_control_array, base); |
| 184 | } |
| 185 | |
| 186 | static int reset_control_array_reset(struct reset_control_array *resets) |
| 187 | { |
| 188 | int ret, i; |
| 189 | |
| 190 | for (i = 0; i < resets->num_rstcs; i++) { |
| 191 | ret = reset_control_reset(resets->rstc[i]); |
| 192 | if (ret) |
| 193 | return ret; |
| 194 | } |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | static int reset_control_array_assert(struct reset_control_array *resets) |
| 200 | { |
| 201 | int ret, i; |
| 202 | |
| 203 | for (i = 0; i < resets->num_rstcs; i++) { |
| 204 | ret = reset_control_assert(resets->rstc[i]); |
| 205 | if (ret) |
| 206 | goto err; |
| 207 | } |
| 208 | |
| 209 | return 0; |
| 210 | |
| 211 | err: |
| 212 | while (i--) |
| 213 | reset_control_deassert(resets->rstc[i]); |
| 214 | return ret; |
| 215 | } |
| 216 | |
| 217 | static int reset_control_array_deassert(struct reset_control_array *resets) |
| 218 | { |
| 219 | int ret, i; |
| 220 | |
| 221 | for (i = 0; i < resets->num_rstcs; i++) { |
| 222 | ret = reset_control_deassert(resets->rstc[i]); |
| 223 | if (ret) |
| 224 | goto err; |
| 225 | } |
| 226 | |
| 227 | return 0; |
| 228 | |
| 229 | err: |
| 230 | while (i--) |
| 231 | reset_control_assert(resets->rstc[i]); |
| 232 | return ret; |
| 233 | } |
| 234 | |
| 235 | static inline bool reset_control_is_array(struct reset_control *rstc) |
| 236 | { |
| 237 | return rstc->array; |
| 238 | } |
| 239 | |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 240 | /** |
| 241 | * reset_control_reset - reset the controlled device |
| 242 | * @rstc: reset controller |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 243 | * |
Martin Blumenstingl | 7da33a3 | 2016-11-12 14:13:03 +0100 | [diff] [blame] | 244 | * On a shared reset line the actual reset pulse is only triggered once for the |
| 245 | * lifetime of the reset_control instance: for all but the first caller this is |
| 246 | * a no-op. |
| 247 | * Consumers must not use reset_control_(de)assert on shared reset lines when |
| 248 | * reset_control_reset has been used. |
Ramiro Oliveira | bb47523 | 2017-01-13 17:57:41 +0000 | [diff] [blame] | 249 | * |
| 250 | * If rstc is NULL it is an optional reset and the function will just |
| 251 | * return 0. |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 252 | */ |
| 253 | int reset_control_reset(struct reset_control *rstc) |
| 254 | { |
Martin Blumenstingl | 7da33a3 | 2016-11-12 14:13:03 +0100 | [diff] [blame] | 255 | int ret; |
| 256 | |
Ramiro Oliveira | bb47523 | 2017-01-13 17:57:41 +0000 | [diff] [blame] | 257 | if (!rstc) |
| 258 | return 0; |
| 259 | |
| 260 | if (WARN_ON(IS_ERR(rstc))) |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 261 | return -EINVAL; |
| 262 | |
Vivek Gautam | 17c82e2 | 2017-05-22 16:53:25 +0530 | [diff] [blame] | 263 | if (reset_control_is_array(rstc)) |
| 264 | return reset_control_array_reset(rstc_to_array(rstc)); |
| 265 | |
Martin Blumenstingl | 7da33a3 | 2016-11-12 14:13:03 +0100 | [diff] [blame] | 266 | if (!rstc->rcdev->ops->reset) |
| 267 | return -ENOTSUPP; |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 268 | |
Martin Blumenstingl | 7da33a3 | 2016-11-12 14:13:03 +0100 | [diff] [blame] | 269 | if (rstc->shared) { |
| 270 | if (WARN_ON(atomic_read(&rstc->deassert_count) != 0)) |
| 271 | return -EINVAL; |
| 272 | |
| 273 | if (atomic_inc_return(&rstc->triggered_count) != 1) |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | ret = rstc->rcdev->ops->reset(rstc->rcdev, rstc->id); |
Jerome Brunet | e5a1dad | 2017-02-15 19:15:51 +0100 | [diff] [blame] | 278 | if (rstc->shared && ret) |
Martin Blumenstingl | 7da33a3 | 2016-11-12 14:13:03 +0100 | [diff] [blame] | 279 | atomic_dec(&rstc->triggered_count); |
| 280 | |
| 281 | return ret; |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 282 | } |
| 283 | EXPORT_SYMBOL_GPL(reset_control_reset); |
| 284 | |
| 285 | /** |
| 286 | * reset_control_assert - asserts the reset line |
| 287 | * @rstc: reset controller |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 288 | * |
| 289 | * Calling this on an exclusive reset controller guarantees that the reset |
| 290 | * will be asserted. When called on a shared reset controller the line may |
| 291 | * still be deasserted, as long as other users keep it so. |
| 292 | * |
| 293 | * For shared reset controls a driver cannot expect the hw's registers and |
| 294 | * internal state to be reset, but must be prepared for this to happen. |
Martin Blumenstingl | 7da33a3 | 2016-11-12 14:13:03 +0100 | [diff] [blame] | 295 | * Consumers must not use reset_control_reset on shared reset lines when |
| 296 | * reset_control_(de)assert has been used. |
Ramiro Oliveira | bb47523 | 2017-01-13 17:57:41 +0000 | [diff] [blame] | 297 | * return 0. |
| 298 | * |
| 299 | * If rstc is NULL it is an optional reset and the function will just |
| 300 | * return 0. |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 301 | */ |
| 302 | int reset_control_assert(struct reset_control *rstc) |
| 303 | { |
Ramiro Oliveira | bb47523 | 2017-01-13 17:57:41 +0000 | [diff] [blame] | 304 | if (!rstc) |
| 305 | return 0; |
| 306 | |
| 307 | if (WARN_ON(IS_ERR(rstc))) |
Philipp Zabel | a3774e1 | 2016-06-20 13:05:14 +0200 | [diff] [blame] | 308 | return -EINVAL; |
| 309 | |
Vivek Gautam | 17c82e2 | 2017-05-22 16:53:25 +0530 | [diff] [blame] | 310 | if (reset_control_is_array(rstc)) |
| 311 | return reset_control_array_assert(rstc_to_array(rstc)); |
| 312 | |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 313 | if (rstc->shared) { |
Martin Blumenstingl | 7da33a3 | 2016-11-12 14:13:03 +0100 | [diff] [blame] | 314 | if (WARN_ON(atomic_read(&rstc->triggered_count) != 0)) |
| 315 | return -EINVAL; |
| 316 | |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 317 | if (WARN_ON(atomic_read(&rstc->deassert_count) == 0)) |
| 318 | return -EINVAL; |
| 319 | |
| 320 | if (atomic_dec_return(&rstc->deassert_count) != 0) |
| 321 | return 0; |
Philipp Zabel | 21240eb | 2017-07-12 17:51:22 +0200 | [diff] [blame] | 322 | |
| 323 | /* |
| 324 | * Shared reset controls allow the reset line to be in any state |
| 325 | * after this call, so doing nothing is a valid option. |
| 326 | */ |
| 327 | if (!rstc->rcdev->ops->assert) |
| 328 | return 0; |
| 329 | } else { |
| 330 | /* |
| 331 | * If the reset controller does not implement .assert(), there |
| 332 | * is no way to guarantee that the reset line is asserted after |
| 333 | * this call. |
| 334 | */ |
| 335 | if (!rstc->rcdev->ops->assert) |
| 336 | return -ENOTSUPP; |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 340 | } |
| 341 | EXPORT_SYMBOL_GPL(reset_control_assert); |
| 342 | |
| 343 | /** |
| 344 | * reset_control_deassert - deasserts the reset line |
| 345 | * @rstc: reset controller |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 346 | * |
| 347 | * After calling this function, the reset is guaranteed to be deasserted. |
Martin Blumenstingl | 7da33a3 | 2016-11-12 14:13:03 +0100 | [diff] [blame] | 348 | * Consumers must not use reset_control_reset on shared reset lines when |
| 349 | * reset_control_(de)assert has been used. |
Ramiro Oliveira | bb47523 | 2017-01-13 17:57:41 +0000 | [diff] [blame] | 350 | * return 0. |
| 351 | * |
| 352 | * If rstc is NULL it is an optional reset and the function will just |
| 353 | * return 0. |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 354 | */ |
| 355 | int reset_control_deassert(struct reset_control *rstc) |
| 356 | { |
Ramiro Oliveira | bb47523 | 2017-01-13 17:57:41 +0000 | [diff] [blame] | 357 | if (!rstc) |
| 358 | return 0; |
| 359 | |
| 360 | if (WARN_ON(IS_ERR(rstc))) |
Philipp Zabel | a3774e1 | 2016-06-20 13:05:14 +0200 | [diff] [blame] | 361 | return -EINVAL; |
| 362 | |
Vivek Gautam | 17c82e2 | 2017-05-22 16:53:25 +0530 | [diff] [blame] | 363 | if (reset_control_is_array(rstc)) |
| 364 | return reset_control_array_deassert(rstc_to_array(rstc)); |
| 365 | |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 366 | if (rstc->shared) { |
Martin Blumenstingl | 7da33a3 | 2016-11-12 14:13:03 +0100 | [diff] [blame] | 367 | if (WARN_ON(atomic_read(&rstc->triggered_count) != 0)) |
| 368 | return -EINVAL; |
| 369 | |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 370 | if (atomic_inc_return(&rstc->deassert_count) != 1) |
| 371 | return 0; |
| 372 | } |
| 373 | |
Philipp Zabel | 21240eb | 2017-07-12 17:51:22 +0200 | [diff] [blame] | 374 | /* |
| 375 | * If the reset controller does not implement .deassert(), we assume |
| 376 | * that it handles self-deasserting reset lines via .reset(). In that |
| 377 | * case, the reset lines are deasserted by default. If that is not the |
| 378 | * case, the reset controller driver should implement .deassert() and |
| 379 | * return -ENOTSUPP. |
| 380 | */ |
| 381 | if (!rstc->rcdev->ops->deassert) |
| 382 | return 0; |
| 383 | |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 384 | return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 385 | } |
| 386 | EXPORT_SYMBOL_GPL(reset_control_deassert); |
| 387 | |
| 388 | /** |
Dinh Nguyen | 729de41 | 2014-10-10 10:21:14 -0500 | [diff] [blame] | 389 | * reset_control_status - returns a negative errno if not supported, a |
| 390 | * positive value if the reset line is asserted, or zero if the reset |
Ramiro Oliveira | bb47523 | 2017-01-13 17:57:41 +0000 | [diff] [blame] | 391 | * line is not asserted or if the desc is NULL (optional reset). |
Dinh Nguyen | 729de41 | 2014-10-10 10:21:14 -0500 | [diff] [blame] | 392 | * @rstc: reset controller |
| 393 | */ |
| 394 | int reset_control_status(struct reset_control *rstc) |
| 395 | { |
Ramiro Oliveira | bb47523 | 2017-01-13 17:57:41 +0000 | [diff] [blame] | 396 | if (!rstc) |
| 397 | return 0; |
| 398 | |
Vivek Gautam | 17c82e2 | 2017-05-22 16:53:25 +0530 | [diff] [blame] | 399 | if (WARN_ON(IS_ERR(rstc)) || reset_control_is_array(rstc)) |
Philipp Zabel | a3774e1 | 2016-06-20 13:05:14 +0200 | [diff] [blame] | 400 | return -EINVAL; |
| 401 | |
Dinh Nguyen | 729de41 | 2014-10-10 10:21:14 -0500 | [diff] [blame] | 402 | if (rstc->rcdev->ops->status) |
| 403 | return rstc->rcdev->ops->status(rstc->rcdev, rstc->id); |
| 404 | |
Philipp Zabel | 39b4da7 | 2015-10-29 09:55:00 +0100 | [diff] [blame] | 405 | return -ENOTSUPP; |
Dinh Nguyen | 729de41 | 2014-10-10 10:21:14 -0500 | [diff] [blame] | 406 | } |
| 407 | EXPORT_SYMBOL_GPL(reset_control_status); |
| 408 | |
Philipp Zabel | 62e24c5 | 2016-02-05 13:41:39 +0100 | [diff] [blame] | 409 | static struct reset_control *__reset_control_get_internal( |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 410 | struct reset_controller_dev *rcdev, |
Ramiro Oliveira | ee48c72 | 2017-01-13 17:57:40 +0000 | [diff] [blame] | 411 | unsigned int index, bool shared) |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 412 | { |
| 413 | struct reset_control *rstc; |
| 414 | |
| 415 | lockdep_assert_held(&reset_list_mutex); |
| 416 | |
| 417 | list_for_each_entry(rstc, &rcdev->reset_control_head, list) { |
| 418 | if (rstc->id == index) { |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 419 | if (WARN_ON(!rstc->shared || !shared)) |
| 420 | return ERR_PTR(-EBUSY); |
| 421 | |
Philipp Zabel | d25e433 | 2017-05-31 17:42:29 +0200 | [diff] [blame] | 422 | kref_get(&rstc->refcnt); |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 423 | return rstc; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | rstc = kzalloc(sizeof(*rstc), GFP_KERNEL); |
| 428 | if (!rstc) |
| 429 | return ERR_PTR(-ENOMEM); |
| 430 | |
| 431 | try_module_get(rcdev->owner); |
| 432 | |
| 433 | rstc->rcdev = rcdev; |
| 434 | list_add(&rstc->list, &rcdev->reset_control_head); |
| 435 | rstc->id = index; |
Philipp Zabel | d25e433 | 2017-05-31 17:42:29 +0200 | [diff] [blame] | 436 | kref_init(&rstc->refcnt); |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 437 | rstc->shared = shared; |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 438 | |
| 439 | return rstc; |
| 440 | } |
| 441 | |
Philipp Zabel | d25e433 | 2017-05-31 17:42:29 +0200 | [diff] [blame] | 442 | static void __reset_control_release(struct kref *kref) |
| 443 | { |
| 444 | struct reset_control *rstc = container_of(kref, struct reset_control, |
| 445 | refcnt); |
| 446 | |
| 447 | lockdep_assert_held(&reset_list_mutex); |
| 448 | |
| 449 | module_put(rstc->rcdev->owner); |
| 450 | |
| 451 | list_del(&rstc->list); |
| 452 | kfree(rstc); |
| 453 | } |
| 454 | |
Philipp Zabel | 62e24c5 | 2016-02-05 13:41:39 +0100 | [diff] [blame] | 455 | static void __reset_control_put_internal(struct reset_control *rstc) |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 456 | { |
| 457 | lockdep_assert_held(&reset_list_mutex); |
| 458 | |
Philipp Zabel | d25e433 | 2017-05-31 17:42:29 +0200 | [diff] [blame] | 459 | kref_put(&rstc->refcnt, __reset_control_release); |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 460 | } |
| 461 | |
Hans de Goede | 6c96f05 | 2016-02-23 18:46:24 +0100 | [diff] [blame] | 462 | struct reset_control *__of_reset_control_get(struct device_node *node, |
Ramiro Oliveira | bb47523 | 2017-01-13 17:57:41 +0000 | [diff] [blame] | 463 | const char *id, int index, bool shared, |
| 464 | bool optional) |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 465 | { |
Philipp Zabel | d056c9b | 2015-12-08 18:49:53 +0100 | [diff] [blame] | 466 | struct reset_control *rstc; |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 467 | struct reset_controller_dev *r, *rcdev; |
| 468 | struct of_phandle_args args; |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 469 | int rstc_id; |
| 470 | int ret; |
| 471 | |
Hans de Goede | 6c96f05 | 2016-02-23 18:46:24 +0100 | [diff] [blame] | 472 | if (!node) |
| 473 | return ERR_PTR(-EINVAL); |
| 474 | |
| 475 | if (id) { |
| 476 | index = of_property_match_string(node, |
| 477 | "reset-names", id); |
Ramiro Oliveira | bb47523 | 2017-01-13 17:57:41 +0000 | [diff] [blame] | 478 | if (index == -EILSEQ) |
| 479 | return ERR_PTR(index); |
Hans de Goede | 6c96f05 | 2016-02-23 18:46:24 +0100 | [diff] [blame] | 480 | if (index < 0) |
Ramiro Oliveira | bb47523 | 2017-01-13 17:57:41 +0000 | [diff] [blame] | 481 | return optional ? NULL : ERR_PTR(-ENOENT); |
Hans de Goede | 6c96f05 | 2016-02-23 18:46:24 +0100 | [diff] [blame] | 482 | } |
| 483 | |
Maxime Ripard | fc0a592 | 2013-12-20 22:41:07 +0100 | [diff] [blame] | 484 | ret = of_parse_phandle_with_args(node, "resets", "#reset-cells", |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 485 | index, &args); |
Ramiro Oliveira | bb47523 | 2017-01-13 17:57:41 +0000 | [diff] [blame] | 486 | if (ret == -EINVAL) |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 487 | return ERR_PTR(ret); |
Ramiro Oliveira | bb47523 | 2017-01-13 17:57:41 +0000 | [diff] [blame] | 488 | if (ret) |
| 489 | return optional ? NULL : ERR_PTR(ret); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 490 | |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 491 | mutex_lock(&reset_list_mutex); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 492 | rcdev = NULL; |
| 493 | list_for_each_entry(r, &reset_controller_list, list) { |
| 494 | if (args.np == r->of_node) { |
| 495 | rcdev = r; |
| 496 | break; |
| 497 | } |
| 498 | } |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 499 | |
| 500 | if (!rcdev) { |
Geert Uytterhoeven | b790c8e | 2018-10-08 13:14:35 +0200 | [diff] [blame^] | 501 | rstc = ERR_PTR(-EPROBE_DEFER); |
| 502 | goto out; |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 503 | } |
| 504 | |
Maxime Ripard | e677774 | 2016-01-14 16:24:44 +0100 | [diff] [blame] | 505 | if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) { |
Geert Uytterhoeven | b790c8e | 2018-10-08 13:14:35 +0200 | [diff] [blame^] | 506 | rstc = ERR_PTR(-EINVAL); |
| 507 | goto out; |
Maxime Ripard | e677774 | 2016-01-14 16:24:44 +0100 | [diff] [blame] | 508 | } |
| 509 | |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 510 | rstc_id = rcdev->of_xlate(rcdev, &args); |
| 511 | if (rstc_id < 0) { |
Geert Uytterhoeven | b790c8e | 2018-10-08 13:14:35 +0200 | [diff] [blame^] | 512 | rstc = ERR_PTR(rstc_id); |
| 513 | goto out; |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 514 | } |
| 515 | |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 516 | /* reset_list_mutex also protects the rcdev's reset_control list */ |
Philipp Zabel | 62e24c5 | 2016-02-05 13:41:39 +0100 | [diff] [blame] | 517 | rstc = __reset_control_get_internal(rcdev, rstc_id, shared); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 518 | |
Geert Uytterhoeven | b790c8e | 2018-10-08 13:14:35 +0200 | [diff] [blame^] | 519 | out: |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 520 | mutex_unlock(&reset_list_mutex); |
Geert Uytterhoeven | b790c8e | 2018-10-08 13:14:35 +0200 | [diff] [blame^] | 521 | of_node_put(args.np); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 522 | |
| 523 | return rstc; |
| 524 | } |
Hans de Goede | 6c96f05 | 2016-02-23 18:46:24 +0100 | [diff] [blame] | 525 | EXPORT_SYMBOL_GPL(__of_reset_control_get); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 526 | |
Bartosz Golaszewski | e2749bb | 2018-03-23 14:04:48 +0100 | [diff] [blame] | 527 | static struct reset_controller_dev * |
| 528 | __reset_controller_by_name(const char *name) |
| 529 | { |
| 530 | struct reset_controller_dev *rcdev; |
| 531 | |
| 532 | lockdep_assert_held(&reset_list_mutex); |
| 533 | |
| 534 | list_for_each_entry(rcdev, &reset_controller_list, list) { |
| 535 | if (!rcdev->dev) |
| 536 | continue; |
| 537 | |
| 538 | if (!strcmp(name, dev_name(rcdev->dev))) |
| 539 | return rcdev; |
| 540 | } |
| 541 | |
| 542 | return NULL; |
| 543 | } |
| 544 | |
Bartosz Golaszewski | 6691dff | 2018-02-28 14:08:57 +0100 | [diff] [blame] | 545 | static struct reset_control * |
| 546 | __reset_control_get_from_lookup(struct device *dev, const char *con_id, |
| 547 | bool shared, bool optional) |
| 548 | { |
| 549 | const struct reset_control_lookup *lookup; |
Bartosz Golaszewski | e2749bb | 2018-03-23 14:04:48 +0100 | [diff] [blame] | 550 | struct reset_controller_dev *rcdev; |
Bartosz Golaszewski | 6691dff | 2018-02-28 14:08:57 +0100 | [diff] [blame] | 551 | const char *dev_id = dev_name(dev); |
| 552 | struct reset_control *rstc = NULL; |
| 553 | |
| 554 | if (!dev) |
| 555 | return ERR_PTR(-EINVAL); |
| 556 | |
| 557 | mutex_lock(&reset_lookup_mutex); |
| 558 | |
| 559 | list_for_each_entry(lookup, &reset_lookup_list, list) { |
| 560 | if (strcmp(lookup->dev_id, dev_id)) |
| 561 | continue; |
| 562 | |
| 563 | if ((!con_id && !lookup->con_id) || |
| 564 | ((con_id && lookup->con_id) && |
| 565 | !strcmp(con_id, lookup->con_id))) { |
| 566 | mutex_lock(&reset_list_mutex); |
Bartosz Golaszewski | e2749bb | 2018-03-23 14:04:48 +0100 | [diff] [blame] | 567 | rcdev = __reset_controller_by_name(lookup->provider); |
| 568 | if (!rcdev) { |
| 569 | mutex_unlock(&reset_list_mutex); |
| 570 | mutex_unlock(&reset_lookup_mutex); |
| 571 | /* Reset provider may not be ready yet. */ |
| 572 | return ERR_PTR(-EPROBE_DEFER); |
| 573 | } |
| 574 | |
| 575 | rstc = __reset_control_get_internal(rcdev, |
Bartosz Golaszewski | 6691dff | 2018-02-28 14:08:57 +0100 | [diff] [blame] | 576 | lookup->index, |
| 577 | shared); |
| 578 | mutex_unlock(&reset_list_mutex); |
| 579 | break; |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | mutex_unlock(&reset_lookup_mutex); |
| 584 | |
| 585 | if (!rstc) |
| 586 | return optional ? NULL : ERR_PTR(-ENOENT); |
| 587 | |
| 588 | return rstc; |
| 589 | } |
| 590 | |
Philipp Zabel | 62e24c5 | 2016-02-05 13:41:39 +0100 | [diff] [blame] | 591 | struct reset_control *__reset_control_get(struct device *dev, const char *id, |
| 592 | int index, bool shared, bool optional) |
| 593 | { |
| 594 | if (dev->of_node) |
| 595 | return __of_reset_control_get(dev->of_node, id, index, shared, |
| 596 | optional); |
| 597 | |
Bartosz Golaszewski | 6691dff | 2018-02-28 14:08:57 +0100 | [diff] [blame] | 598 | return __reset_control_get_from_lookup(dev, id, shared, optional); |
Philipp Zabel | 62e24c5 | 2016-02-05 13:41:39 +0100 | [diff] [blame] | 599 | } |
| 600 | EXPORT_SYMBOL_GPL(__reset_control_get); |
| 601 | |
Vivek Gautam | 17c82e2 | 2017-05-22 16:53:25 +0530 | [diff] [blame] | 602 | static void reset_control_array_put(struct reset_control_array *resets) |
| 603 | { |
| 604 | int i; |
| 605 | |
| 606 | mutex_lock(&reset_list_mutex); |
| 607 | for (i = 0; i < resets->num_rstcs; i++) |
| 608 | __reset_control_put_internal(resets->rstc[i]); |
| 609 | mutex_unlock(&reset_list_mutex); |
| 610 | } |
| 611 | |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 612 | /** |
| 613 | * reset_control_put - free the reset controller |
| 614 | * @rstc: reset controller |
| 615 | */ |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 616 | void reset_control_put(struct reset_control *rstc) |
| 617 | { |
Heiner Kallweit | 4891486 | 2017-02-01 08:05:22 +0100 | [diff] [blame] | 618 | if (IS_ERR_OR_NULL(rstc)) |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 619 | return; |
| 620 | |
Vivek Gautam | 17c82e2 | 2017-05-22 16:53:25 +0530 | [diff] [blame] | 621 | if (reset_control_is_array(rstc)) { |
| 622 | reset_control_array_put(rstc_to_array(rstc)); |
| 623 | return; |
| 624 | } |
| 625 | |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 626 | mutex_lock(&reset_list_mutex); |
Philipp Zabel | 62e24c5 | 2016-02-05 13:41:39 +0100 | [diff] [blame] | 627 | __reset_control_put_internal(rstc); |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 628 | mutex_unlock(&reset_list_mutex); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 629 | } |
| 630 | EXPORT_SYMBOL_GPL(reset_control_put); |
| 631 | |
| 632 | static void devm_reset_control_release(struct device *dev, void *res) |
| 633 | { |
| 634 | reset_control_put(*(struct reset_control **)res); |
| 635 | } |
| 636 | |
Hans de Goede | 6c96f05 | 2016-02-23 18:46:24 +0100 | [diff] [blame] | 637 | struct reset_control *__devm_reset_control_get(struct device *dev, |
Ramiro Oliveira | bb47523 | 2017-01-13 17:57:41 +0000 | [diff] [blame] | 638 | const char *id, int index, bool shared, |
| 639 | bool optional) |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 640 | { |
| 641 | struct reset_control **ptr, *rstc; |
| 642 | |
| 643 | ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr), |
| 644 | GFP_KERNEL); |
| 645 | if (!ptr) |
| 646 | return ERR_PTR(-ENOMEM); |
| 647 | |
Philipp Zabel | 62e24c5 | 2016-02-05 13:41:39 +0100 | [diff] [blame] | 648 | rstc = __reset_control_get(dev, id, index, shared, optional); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 649 | if (!IS_ERR(rstc)) { |
| 650 | *ptr = rstc; |
| 651 | devres_add(dev, ptr); |
| 652 | } else { |
| 653 | devres_free(ptr); |
| 654 | } |
| 655 | |
| 656 | return rstc; |
| 657 | } |
Hans de Goede | 6c96f05 | 2016-02-23 18:46:24 +0100 | [diff] [blame] | 658 | EXPORT_SYMBOL_GPL(__devm_reset_control_get); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 659 | |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 660 | /** |
| 661 | * device_reset - find reset controller associated with the device |
| 662 | * and perform reset |
| 663 | * @dev: device to be reset by the controller |
Masahiro Yamada | 1554bbd | 2017-10-29 01:50:06 +0900 | [diff] [blame] | 664 | * @optional: whether it is optional to reset the device |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 665 | * |
Masahiro Yamada | 1554bbd | 2017-10-29 01:50:06 +0900 | [diff] [blame] | 666 | * Convenience wrapper for __reset_control_get() and reset_control_reset(). |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 667 | * This is useful for the common case of devices with single, dedicated reset |
| 668 | * lines. |
| 669 | */ |
Masahiro Yamada | 1554bbd | 2017-10-29 01:50:06 +0900 | [diff] [blame] | 670 | int __device_reset(struct device *dev, bool optional) |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 671 | { |
| 672 | struct reset_control *rstc; |
| 673 | int ret; |
| 674 | |
Masahiro Yamada | 1554bbd | 2017-10-29 01:50:06 +0900 | [diff] [blame] | 675 | rstc = __reset_control_get(dev, NULL, 0, 0, optional); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 676 | if (IS_ERR(rstc)) |
| 677 | return PTR_ERR(rstc); |
| 678 | |
| 679 | ret = reset_control_reset(rstc); |
| 680 | |
| 681 | reset_control_put(rstc); |
| 682 | |
| 683 | return ret; |
| 684 | } |
Masahiro Yamada | 1554bbd | 2017-10-29 01:50:06 +0900 | [diff] [blame] | 685 | EXPORT_SYMBOL_GPL(__device_reset); |
Vivek Gautam | 17c82e2 | 2017-05-22 16:53:25 +0530 | [diff] [blame] | 686 | |
| 687 | /** |
| 688 | * APIs to manage an array of reset controls. |
| 689 | */ |
| 690 | /** |
| 691 | * of_reset_control_get_count - Count number of resets available with a device |
| 692 | * |
| 693 | * @node: device node that contains 'resets'. |
| 694 | * |
| 695 | * Returns positive reset count on success, or error number on failure and |
| 696 | * on count being zero. |
| 697 | */ |
| 698 | static int of_reset_control_get_count(struct device_node *node) |
| 699 | { |
| 700 | int count; |
| 701 | |
| 702 | if (!node) |
| 703 | return -EINVAL; |
| 704 | |
| 705 | count = of_count_phandle_with_args(node, "resets", "#reset-cells"); |
| 706 | if (count == 0) |
| 707 | count = -ENOENT; |
| 708 | |
| 709 | return count; |
| 710 | } |
| 711 | |
| 712 | /** |
| 713 | * of_reset_control_array_get - Get a list of reset controls using |
| 714 | * device node. |
| 715 | * |
| 716 | * @np: device node for the device that requests the reset controls array |
| 717 | * @shared: whether reset controls are shared or not |
| 718 | * @optional: whether it is optional to get the reset controls |
| 719 | * |
| 720 | * Returns pointer to allocated reset_control_array on success or |
| 721 | * error on failure |
| 722 | */ |
| 723 | struct reset_control * |
| 724 | of_reset_control_array_get(struct device_node *np, bool shared, bool optional) |
| 725 | { |
| 726 | struct reset_control_array *resets; |
| 727 | struct reset_control *rstc; |
| 728 | int num, i; |
| 729 | |
| 730 | num = of_reset_control_get_count(np); |
| 731 | if (num < 0) |
| 732 | return optional ? NULL : ERR_PTR(num); |
| 733 | |
Kees Cook | acafe7e | 2018-05-08 13:45:50 -0700 | [diff] [blame] | 734 | resets = kzalloc(struct_size(resets, rstc, num), GFP_KERNEL); |
Vivek Gautam | 17c82e2 | 2017-05-22 16:53:25 +0530 | [diff] [blame] | 735 | if (!resets) |
| 736 | return ERR_PTR(-ENOMEM); |
| 737 | |
| 738 | for (i = 0; i < num; i++) { |
| 739 | rstc = __of_reset_control_get(np, NULL, i, shared, optional); |
| 740 | if (IS_ERR(rstc)) |
| 741 | goto err_rst; |
| 742 | resets->rstc[i] = rstc; |
| 743 | } |
| 744 | resets->num_rstcs = num; |
| 745 | resets->base.array = true; |
| 746 | |
| 747 | return &resets->base; |
| 748 | |
| 749 | err_rst: |
| 750 | mutex_lock(&reset_list_mutex); |
| 751 | while (--i >= 0) |
| 752 | __reset_control_put_internal(resets->rstc[i]); |
| 753 | mutex_unlock(&reset_list_mutex); |
| 754 | |
| 755 | kfree(resets); |
| 756 | |
| 757 | return rstc; |
| 758 | } |
| 759 | EXPORT_SYMBOL_GPL(of_reset_control_array_get); |
| 760 | |
| 761 | /** |
| 762 | * devm_reset_control_array_get - Resource managed reset control array get |
| 763 | * |
| 764 | * @dev: device that requests the list of reset controls |
| 765 | * @shared: whether reset controls are shared or not |
| 766 | * @optional: whether it is optional to get the reset controls |
| 767 | * |
| 768 | * The reset control array APIs are intended for a list of resets |
| 769 | * that just have to be asserted or deasserted, without any |
| 770 | * requirements on the order. |
| 771 | * |
| 772 | * Returns pointer to allocated reset_control_array on success or |
| 773 | * error on failure |
| 774 | */ |
| 775 | struct reset_control * |
| 776 | devm_reset_control_array_get(struct device *dev, bool shared, bool optional) |
| 777 | { |
| 778 | struct reset_control **devres; |
| 779 | struct reset_control *rstc; |
| 780 | |
| 781 | devres = devres_alloc(devm_reset_control_release, sizeof(*devres), |
| 782 | GFP_KERNEL); |
| 783 | if (!devres) |
| 784 | return ERR_PTR(-ENOMEM); |
| 785 | |
| 786 | rstc = of_reset_control_array_get(dev->of_node, shared, optional); |
| 787 | if (IS_ERR(rstc)) { |
| 788 | devres_free(devres); |
| 789 | return rstc; |
| 790 | } |
| 791 | |
| 792 | *devres = rstc; |
| 793 | devres_add(dev, devres); |
| 794 | |
| 795 | return rstc; |
| 796 | } |
| 797 | EXPORT_SYMBOL_GPL(devm_reset_control_array_get); |