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