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