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