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> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/of.h> |
| 18 | #include <linux/reset.h> |
| 19 | #include <linux/reset-controller.h> |
| 20 | #include <linux/slab.h> |
| 21 | |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 22 | static DEFINE_MUTEX(reset_list_mutex); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 23 | static LIST_HEAD(reset_controller_list); |
| 24 | |
| 25 | /** |
| 26 | * struct reset_control - a reset control |
| 27 | * @rcdev: a pointer to the reset controller device |
| 28 | * this reset control belongs to |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 29 | * @list: list entry for the rcdev's reset controller list |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 30 | * @id: ID of the reset controller in the reset |
| 31 | * controller device |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 32 | * @refcnt: Number of gets of this reset_control |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 33 | * @shared: Is this a shared (1), or an exclusive (0) reset_control? |
| 34 | * @deassert_cnt: Number of times this reset line has been deasserted |
Martin Blumenstingl | 7da33a3 | 2016-11-12 14:13:03 +0100 | [diff] [blame] | 35 | * @triggered_count: Number of times this reset line has been reset. Currently |
| 36 | * only used for shared resets, which means that the value |
| 37 | * will be either 0 or 1. |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 38 | */ |
| 39 | struct reset_control { |
| 40 | struct reset_controller_dev *rcdev; |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 41 | struct list_head list; |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 42 | unsigned int id; |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 43 | unsigned int refcnt; |
Ramiro Oliveira | ee48c72 | 2017-01-13 17:57:40 +0000 | [diff] [blame] | 44 | bool shared; |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 45 | atomic_t deassert_count; |
Martin Blumenstingl | 7da33a3 | 2016-11-12 14:13:03 +0100 | [diff] [blame] | 46 | atomic_t triggered_count; |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | /** |
| 50 | * of_reset_simple_xlate - translate reset_spec to the reset line number |
| 51 | * @rcdev: a pointer to the reset controller device |
| 52 | * @reset_spec: reset line specifier as found in the device tree |
| 53 | * @flags: a flags pointer to fill in (optional) |
| 54 | * |
| 55 | * This simple translation function should be used for reset controllers |
| 56 | * with 1:1 mapping, where reset lines can be indexed by number without gaps. |
| 57 | */ |
Rashika Kheria | 0c5b2b9 | 2013-12-19 14:11:10 +0530 | [diff] [blame] | 58 | static int of_reset_simple_xlate(struct reset_controller_dev *rcdev, |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 59 | const struct of_phandle_args *reset_spec) |
| 60 | { |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 61 | if (reset_spec->args[0] >= rcdev->nr_resets) |
| 62 | return -EINVAL; |
| 63 | |
| 64 | return reset_spec->args[0]; |
| 65 | } |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * reset_controller_register - register a reset controller device |
| 69 | * @rcdev: a pointer to the initialized reset controller device |
| 70 | */ |
| 71 | int reset_controller_register(struct reset_controller_dev *rcdev) |
| 72 | { |
| 73 | if (!rcdev->of_xlate) { |
| 74 | rcdev->of_reset_n_cells = 1; |
| 75 | rcdev->of_xlate = of_reset_simple_xlate; |
| 76 | } |
| 77 | |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 78 | INIT_LIST_HEAD(&rcdev->reset_control_head); |
| 79 | |
| 80 | mutex_lock(&reset_list_mutex); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 81 | list_add(&rcdev->list, &reset_controller_list); |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 82 | mutex_unlock(&reset_list_mutex); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | EXPORT_SYMBOL_GPL(reset_controller_register); |
| 87 | |
| 88 | /** |
| 89 | * reset_controller_unregister - unregister a reset controller device |
| 90 | * @rcdev: a pointer to the reset controller device |
| 91 | */ |
| 92 | void reset_controller_unregister(struct reset_controller_dev *rcdev) |
| 93 | { |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 94 | mutex_lock(&reset_list_mutex); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 95 | list_del(&rcdev->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 | EXPORT_SYMBOL_GPL(reset_controller_unregister); |
| 99 | |
Masahiro Yamada | 8d5b5d5 | 2016-05-01 19:36:57 +0900 | [diff] [blame] | 100 | static void devm_reset_controller_release(struct device *dev, void *res) |
| 101 | { |
| 102 | reset_controller_unregister(*(struct reset_controller_dev **)res); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * devm_reset_controller_register - resource managed reset_controller_register() |
| 107 | * @dev: device that is registering this reset controller |
| 108 | * @rcdev: a pointer to the initialized reset controller device |
| 109 | * |
| 110 | * Managed reset_controller_register(). For reset controllers registered by |
| 111 | * this function, reset_controller_unregister() is automatically called on |
| 112 | * driver detach. See reset_controller_register() for more information. |
| 113 | */ |
| 114 | int devm_reset_controller_register(struct device *dev, |
| 115 | struct reset_controller_dev *rcdev) |
| 116 | { |
| 117 | struct reset_controller_dev **rcdevp; |
| 118 | int ret; |
| 119 | |
| 120 | rcdevp = devres_alloc(devm_reset_controller_release, sizeof(*rcdevp), |
| 121 | GFP_KERNEL); |
| 122 | if (!rcdevp) |
| 123 | return -ENOMEM; |
| 124 | |
| 125 | ret = reset_controller_register(rcdev); |
| 126 | if (!ret) { |
| 127 | *rcdevp = rcdev; |
| 128 | devres_add(dev, rcdevp); |
| 129 | } else { |
| 130 | devres_free(rcdevp); |
| 131 | } |
| 132 | |
| 133 | return ret; |
| 134 | } |
| 135 | EXPORT_SYMBOL_GPL(devm_reset_controller_register); |
| 136 | |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 137 | /** |
| 138 | * reset_control_reset - reset the controlled device |
| 139 | * @rstc: reset controller |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 140 | * |
Martin Blumenstingl | 7da33a3 | 2016-11-12 14:13:03 +0100 | [diff] [blame] | 141 | * On a shared reset line the actual reset pulse is only triggered once for the |
| 142 | * lifetime of the reset_control instance: for all but the first caller this is |
| 143 | * a no-op. |
| 144 | * Consumers must not use reset_control_(de)assert on shared reset lines when |
| 145 | * reset_control_reset has been used. |
Ramiro Oliveira | bb47523 | 2017-01-13 17:57:41 +0000 | [diff] [blame] | 146 | * |
| 147 | * If rstc is NULL it is an optional reset and the function will just |
| 148 | * return 0. |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 149 | */ |
| 150 | int reset_control_reset(struct reset_control *rstc) |
| 151 | { |
Martin Blumenstingl | 7da33a3 | 2016-11-12 14:13:03 +0100 | [diff] [blame] | 152 | int ret; |
| 153 | |
Ramiro Oliveira | bb47523 | 2017-01-13 17:57:41 +0000 | [diff] [blame] | 154 | if (!rstc) |
| 155 | return 0; |
| 156 | |
| 157 | if (WARN_ON(IS_ERR(rstc))) |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 158 | return -EINVAL; |
| 159 | |
Martin Blumenstingl | 7da33a3 | 2016-11-12 14:13:03 +0100 | [diff] [blame] | 160 | if (!rstc->rcdev->ops->reset) |
| 161 | return -ENOTSUPP; |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 162 | |
Martin Blumenstingl | 7da33a3 | 2016-11-12 14:13:03 +0100 | [diff] [blame] | 163 | if (rstc->shared) { |
| 164 | if (WARN_ON(atomic_read(&rstc->deassert_count) != 0)) |
| 165 | return -EINVAL; |
| 166 | |
| 167 | if (atomic_inc_return(&rstc->triggered_count) != 1) |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | ret = rstc->rcdev->ops->reset(rstc->rcdev, rstc->id); |
Jerome Brunet | e5a1dad | 2017-02-15 19:15:51 +0100 | [diff] [blame] | 172 | if (rstc->shared && ret) |
Martin Blumenstingl | 7da33a3 | 2016-11-12 14:13:03 +0100 | [diff] [blame] | 173 | atomic_dec(&rstc->triggered_count); |
| 174 | |
| 175 | return ret; |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 176 | } |
| 177 | EXPORT_SYMBOL_GPL(reset_control_reset); |
| 178 | |
| 179 | /** |
| 180 | * reset_control_assert - asserts the reset line |
| 181 | * @rstc: reset controller |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 182 | * |
| 183 | * Calling this on an exclusive reset controller guarantees that the reset |
| 184 | * will be asserted. When called on a shared reset controller the line may |
| 185 | * still be deasserted, as long as other users keep it so. |
| 186 | * |
| 187 | * For shared reset controls a driver cannot expect the hw's registers and |
| 188 | * 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] | 189 | * Consumers must not use reset_control_reset on shared reset lines when |
| 190 | * reset_control_(de)assert has been used. |
Ramiro Oliveira | bb47523 | 2017-01-13 17:57:41 +0000 | [diff] [blame] | 191 | * return 0. |
| 192 | * |
| 193 | * If rstc is NULL it is an optional reset and the function will just |
| 194 | * return 0. |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 195 | */ |
| 196 | int reset_control_assert(struct reset_control *rstc) |
| 197 | { |
Ramiro Oliveira | bb47523 | 2017-01-13 17:57:41 +0000 | [diff] [blame] | 198 | if (!rstc) |
| 199 | return 0; |
| 200 | |
| 201 | if (WARN_ON(IS_ERR(rstc))) |
Philipp Zabel | a3774e1 | 2016-06-20 13:05:14 +0200 | [diff] [blame] | 202 | return -EINVAL; |
| 203 | |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 204 | if (!rstc->rcdev->ops->assert) |
| 205 | return -ENOTSUPP; |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 206 | |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 207 | if (rstc->shared) { |
Martin Blumenstingl | 7da33a3 | 2016-11-12 14:13:03 +0100 | [diff] [blame] | 208 | if (WARN_ON(atomic_read(&rstc->triggered_count) != 0)) |
| 209 | return -EINVAL; |
| 210 | |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 211 | if (WARN_ON(atomic_read(&rstc->deassert_count) == 0)) |
| 212 | return -EINVAL; |
| 213 | |
| 214 | if (atomic_dec_return(&rstc->deassert_count) != 0) |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 219 | } |
| 220 | EXPORT_SYMBOL_GPL(reset_control_assert); |
| 221 | |
| 222 | /** |
| 223 | * reset_control_deassert - deasserts the reset line |
| 224 | * @rstc: reset controller |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 225 | * |
| 226 | * After calling this function, the reset is guaranteed to be deasserted. |
Martin Blumenstingl | 7da33a3 | 2016-11-12 14:13:03 +0100 | [diff] [blame] | 227 | * Consumers must not use reset_control_reset on shared reset lines when |
| 228 | * reset_control_(de)assert has been used. |
Ramiro Oliveira | bb47523 | 2017-01-13 17:57:41 +0000 | [diff] [blame] | 229 | * return 0. |
| 230 | * |
| 231 | * If rstc is NULL it is an optional reset and the function will just |
| 232 | * return 0. |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 233 | */ |
| 234 | int reset_control_deassert(struct reset_control *rstc) |
| 235 | { |
Ramiro Oliveira | bb47523 | 2017-01-13 17:57:41 +0000 | [diff] [blame] | 236 | if (!rstc) |
| 237 | return 0; |
| 238 | |
| 239 | if (WARN_ON(IS_ERR(rstc))) |
Philipp Zabel | a3774e1 | 2016-06-20 13:05:14 +0200 | [diff] [blame] | 240 | return -EINVAL; |
| 241 | |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 242 | if (!rstc->rcdev->ops->deassert) |
| 243 | return -ENOTSUPP; |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 244 | |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 245 | if (rstc->shared) { |
Martin Blumenstingl | 7da33a3 | 2016-11-12 14:13:03 +0100 | [diff] [blame] | 246 | if (WARN_ON(atomic_read(&rstc->triggered_count) != 0)) |
| 247 | return -EINVAL; |
| 248 | |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 249 | if (atomic_inc_return(&rstc->deassert_count) != 1) |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 254 | } |
| 255 | EXPORT_SYMBOL_GPL(reset_control_deassert); |
| 256 | |
| 257 | /** |
Dinh Nguyen | 729de41 | 2014-10-10 10:21:14 -0500 | [diff] [blame] | 258 | * reset_control_status - returns a negative errno if not supported, a |
| 259 | * 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] | 260 | * line is not asserted or if the desc is NULL (optional reset). |
Dinh Nguyen | 729de41 | 2014-10-10 10:21:14 -0500 | [diff] [blame] | 261 | * @rstc: reset controller |
| 262 | */ |
| 263 | int reset_control_status(struct reset_control *rstc) |
| 264 | { |
Ramiro Oliveira | bb47523 | 2017-01-13 17:57:41 +0000 | [diff] [blame] | 265 | if (!rstc) |
| 266 | return 0; |
| 267 | |
| 268 | if (WARN_ON(IS_ERR(rstc))) |
Philipp Zabel | a3774e1 | 2016-06-20 13:05:14 +0200 | [diff] [blame] | 269 | return -EINVAL; |
| 270 | |
Dinh Nguyen | 729de41 | 2014-10-10 10:21:14 -0500 | [diff] [blame] | 271 | if (rstc->rcdev->ops->status) |
| 272 | return rstc->rcdev->ops->status(rstc->rcdev, rstc->id); |
| 273 | |
Philipp Zabel | 39b4da7 | 2015-10-29 09:55:00 +0100 | [diff] [blame] | 274 | return -ENOTSUPP; |
Dinh Nguyen | 729de41 | 2014-10-10 10:21:14 -0500 | [diff] [blame] | 275 | } |
| 276 | EXPORT_SYMBOL_GPL(reset_control_status); |
| 277 | |
Philipp Zabel | 62e24c5 | 2016-02-05 13:41:39 +0100 | [diff] [blame] | 278 | static struct reset_control *__reset_control_get_internal( |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 279 | struct reset_controller_dev *rcdev, |
Ramiro Oliveira | ee48c72 | 2017-01-13 17:57:40 +0000 | [diff] [blame] | 280 | unsigned int index, bool shared) |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 281 | { |
| 282 | struct reset_control *rstc; |
| 283 | |
| 284 | lockdep_assert_held(&reset_list_mutex); |
| 285 | |
| 286 | list_for_each_entry(rstc, &rcdev->reset_control_head, list) { |
| 287 | if (rstc->id == index) { |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 288 | if (WARN_ON(!rstc->shared || !shared)) |
| 289 | return ERR_PTR(-EBUSY); |
| 290 | |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 291 | rstc->refcnt++; |
| 292 | return rstc; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | rstc = kzalloc(sizeof(*rstc), GFP_KERNEL); |
| 297 | if (!rstc) |
| 298 | return ERR_PTR(-ENOMEM); |
| 299 | |
| 300 | try_module_get(rcdev->owner); |
| 301 | |
| 302 | rstc->rcdev = rcdev; |
| 303 | list_add(&rstc->list, &rcdev->reset_control_head); |
| 304 | rstc->id = index; |
| 305 | rstc->refcnt = 1; |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 306 | rstc->shared = shared; |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 307 | |
| 308 | return rstc; |
| 309 | } |
| 310 | |
Philipp Zabel | 62e24c5 | 2016-02-05 13:41:39 +0100 | [diff] [blame] | 311 | static void __reset_control_put_internal(struct reset_control *rstc) |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 312 | { |
| 313 | lockdep_assert_held(&reset_list_mutex); |
| 314 | |
| 315 | if (--rstc->refcnt) |
| 316 | return; |
| 317 | |
| 318 | module_put(rstc->rcdev->owner); |
| 319 | |
| 320 | list_del(&rstc->list); |
| 321 | kfree(rstc); |
| 322 | } |
| 323 | |
Hans de Goede | 6c96f05 | 2016-02-23 18:46:24 +0100 | [diff] [blame] | 324 | struct reset_control *__of_reset_control_get(struct device_node *node, |
Ramiro Oliveira | bb47523 | 2017-01-13 17:57:41 +0000 | [diff] [blame] | 325 | const char *id, int index, bool shared, |
| 326 | bool optional) |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 327 | { |
Philipp Zabel | d056c9b | 2015-12-08 18:49:53 +0100 | [diff] [blame] | 328 | struct reset_control *rstc; |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 329 | struct reset_controller_dev *r, *rcdev; |
| 330 | struct of_phandle_args args; |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 331 | int rstc_id; |
| 332 | int ret; |
| 333 | |
Hans de Goede | 6c96f05 | 2016-02-23 18:46:24 +0100 | [diff] [blame] | 334 | if (!node) |
| 335 | return ERR_PTR(-EINVAL); |
| 336 | |
| 337 | if (id) { |
| 338 | index = of_property_match_string(node, |
| 339 | "reset-names", id); |
Ramiro Oliveira | bb47523 | 2017-01-13 17:57:41 +0000 | [diff] [blame] | 340 | if (index == -EILSEQ) |
| 341 | return ERR_PTR(index); |
Hans de Goede | 6c96f05 | 2016-02-23 18:46:24 +0100 | [diff] [blame] | 342 | if (index < 0) |
Ramiro Oliveira | bb47523 | 2017-01-13 17:57:41 +0000 | [diff] [blame] | 343 | return optional ? NULL : ERR_PTR(-ENOENT); |
Hans de Goede | 6c96f05 | 2016-02-23 18:46:24 +0100 | [diff] [blame] | 344 | } |
| 345 | |
Maxime Ripard | fc0a592 | 2013-12-20 22:41:07 +0100 | [diff] [blame] | 346 | ret = of_parse_phandle_with_args(node, "resets", "#reset-cells", |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 347 | index, &args); |
Ramiro Oliveira | bb47523 | 2017-01-13 17:57:41 +0000 | [diff] [blame] | 348 | if (ret == -EINVAL) |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 349 | return ERR_PTR(ret); |
Ramiro Oliveira | bb47523 | 2017-01-13 17:57:41 +0000 | [diff] [blame] | 350 | if (ret) |
| 351 | return optional ? NULL : ERR_PTR(ret); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 352 | |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 353 | mutex_lock(&reset_list_mutex); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 354 | rcdev = NULL; |
| 355 | list_for_each_entry(r, &reset_controller_list, list) { |
| 356 | if (args.np == r->of_node) { |
| 357 | rcdev = r; |
| 358 | break; |
| 359 | } |
| 360 | } |
| 361 | of_node_put(args.np); |
| 362 | |
| 363 | if (!rcdev) { |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 364 | mutex_unlock(&reset_list_mutex); |
Philipp Zabel | 3d10302 | 2013-07-18 13:55:22 +0200 | [diff] [blame] | 365 | return ERR_PTR(-EPROBE_DEFER); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 366 | } |
| 367 | |
Maxime Ripard | e677774 | 2016-01-14 16:24:44 +0100 | [diff] [blame] | 368 | if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) { |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 369 | mutex_unlock(&reset_list_mutex); |
Maxime Ripard | e677774 | 2016-01-14 16:24:44 +0100 | [diff] [blame] | 370 | return ERR_PTR(-EINVAL); |
| 371 | } |
| 372 | |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 373 | rstc_id = rcdev->of_xlate(rcdev, &args); |
| 374 | if (rstc_id < 0) { |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 375 | mutex_unlock(&reset_list_mutex); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 376 | return ERR_PTR(rstc_id); |
| 377 | } |
| 378 | |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 379 | /* reset_list_mutex also protects the rcdev's reset_control list */ |
Philipp Zabel | 62e24c5 | 2016-02-05 13:41:39 +0100 | [diff] [blame] | 380 | rstc = __reset_control_get_internal(rcdev, rstc_id, shared); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 381 | |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 382 | mutex_unlock(&reset_list_mutex); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 383 | |
| 384 | return rstc; |
| 385 | } |
Hans de Goede | 6c96f05 | 2016-02-23 18:46:24 +0100 | [diff] [blame] | 386 | EXPORT_SYMBOL_GPL(__of_reset_control_get); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 387 | |
Philipp Zabel | 62e24c5 | 2016-02-05 13:41:39 +0100 | [diff] [blame] | 388 | struct reset_control *__reset_control_get(struct device *dev, const char *id, |
| 389 | int index, bool shared, bool optional) |
| 390 | { |
| 391 | if (dev->of_node) |
| 392 | return __of_reset_control_get(dev->of_node, id, index, shared, |
| 393 | optional); |
| 394 | |
| 395 | return optional ? NULL : ERR_PTR(-EINVAL); |
| 396 | } |
| 397 | EXPORT_SYMBOL_GPL(__reset_control_get); |
| 398 | |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 399 | /** |
| 400 | * reset_control_put - free the reset controller |
| 401 | * @rstc: reset controller |
| 402 | */ |
| 403 | |
| 404 | void reset_control_put(struct reset_control *rstc) |
| 405 | { |
Heiner Kallweit | 4891486 | 2017-02-01 08:05:22 +0100 | [diff] [blame] | 406 | if (IS_ERR_OR_NULL(rstc)) |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 407 | return; |
| 408 | |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 409 | mutex_lock(&reset_list_mutex); |
Philipp Zabel | 62e24c5 | 2016-02-05 13:41:39 +0100 | [diff] [blame] | 410 | __reset_control_put_internal(rstc); |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 411 | mutex_unlock(&reset_list_mutex); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 412 | } |
| 413 | EXPORT_SYMBOL_GPL(reset_control_put); |
| 414 | |
| 415 | static void devm_reset_control_release(struct device *dev, void *res) |
| 416 | { |
| 417 | reset_control_put(*(struct reset_control **)res); |
| 418 | } |
| 419 | |
Hans de Goede | 6c96f05 | 2016-02-23 18:46:24 +0100 | [diff] [blame] | 420 | struct reset_control *__devm_reset_control_get(struct device *dev, |
Ramiro Oliveira | bb47523 | 2017-01-13 17:57:41 +0000 | [diff] [blame] | 421 | const char *id, int index, bool shared, |
| 422 | bool optional) |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 423 | { |
| 424 | struct reset_control **ptr, *rstc; |
| 425 | |
| 426 | ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr), |
| 427 | GFP_KERNEL); |
| 428 | if (!ptr) |
| 429 | return ERR_PTR(-ENOMEM); |
| 430 | |
Philipp Zabel | 62e24c5 | 2016-02-05 13:41:39 +0100 | [diff] [blame] | 431 | rstc = __reset_control_get(dev, id, index, shared, optional); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 432 | if (!IS_ERR(rstc)) { |
| 433 | *ptr = rstc; |
| 434 | devres_add(dev, ptr); |
| 435 | } else { |
| 436 | devres_free(ptr); |
| 437 | } |
| 438 | |
| 439 | return rstc; |
| 440 | } |
Hans de Goede | 6c96f05 | 2016-02-23 18:46:24 +0100 | [diff] [blame] | 441 | EXPORT_SYMBOL_GPL(__devm_reset_control_get); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 442 | |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 443 | /** |
| 444 | * device_reset - find reset controller associated with the device |
| 445 | * and perform reset |
| 446 | * @dev: device to be reset by the controller |
| 447 | * |
| 448 | * Convenience wrapper for reset_control_get() and reset_control_reset(). |
| 449 | * This is useful for the common case of devices with single, dedicated reset |
| 450 | * lines. |
| 451 | */ |
| 452 | int device_reset(struct device *dev) |
| 453 | { |
| 454 | struct reset_control *rstc; |
| 455 | int ret; |
| 456 | |
| 457 | rstc = reset_control_get(dev, NULL); |
| 458 | if (IS_ERR(rstc)) |
| 459 | return PTR_ERR(rstc); |
| 460 | |
| 461 | ret = reset_control_reset(rstc); |
| 462 | |
| 463 | reset_control_put(rstc); |
| 464 | |
| 465 | return ret; |
| 466 | } |
| 467 | EXPORT_SYMBOL_GPL(device_reset); |