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