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 | |
| 96 | /** |
| 97 | * reset_control_reset - reset the controlled device |
| 98 | * @rstc: reset controller |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 99 | * |
| 100 | * Calling this on a shared reset controller is an error. |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 101 | */ |
| 102 | int reset_control_reset(struct reset_control *rstc) |
| 103 | { |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 104 | if (WARN_ON(rstc->shared)) |
| 105 | return -EINVAL; |
| 106 | |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 107 | if (rstc->rcdev->ops->reset) |
| 108 | return rstc->rcdev->ops->reset(rstc->rcdev, rstc->id); |
| 109 | |
Philipp Zabel | 39b4da7 | 2015-10-29 09:55:00 +0100 | [diff] [blame] | 110 | return -ENOTSUPP; |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 111 | } |
| 112 | EXPORT_SYMBOL_GPL(reset_control_reset); |
| 113 | |
| 114 | /** |
| 115 | * reset_control_assert - asserts the reset line |
| 116 | * @rstc: reset controller |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 117 | * |
| 118 | * Calling this on an exclusive reset controller guarantees that the reset |
| 119 | * will be asserted. When called on a shared reset controller the line may |
| 120 | * still be deasserted, as long as other users keep it so. |
| 121 | * |
| 122 | * For shared reset controls a driver cannot expect the hw's registers and |
| 123 | * internal state to be reset, but must be prepared for this to happen. |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 124 | */ |
| 125 | int reset_control_assert(struct reset_control *rstc) |
| 126 | { |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 127 | if (!rstc->rcdev->ops->assert) |
| 128 | return -ENOTSUPP; |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 129 | |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 130 | if (rstc->shared) { |
| 131 | if (WARN_ON(atomic_read(&rstc->deassert_count) == 0)) |
| 132 | return -EINVAL; |
| 133 | |
| 134 | if (atomic_dec_return(&rstc->deassert_count) != 0) |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 139 | } |
| 140 | EXPORT_SYMBOL_GPL(reset_control_assert); |
| 141 | |
| 142 | /** |
| 143 | * reset_control_deassert - deasserts the reset line |
| 144 | * @rstc: reset controller |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 145 | * |
| 146 | * After calling this function, the reset is guaranteed to be deasserted. |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 147 | */ |
| 148 | int reset_control_deassert(struct reset_control *rstc) |
| 149 | { |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 150 | if (!rstc->rcdev->ops->deassert) |
| 151 | return -ENOTSUPP; |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 152 | |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 153 | if (rstc->shared) { |
| 154 | if (atomic_inc_return(&rstc->deassert_count) != 1) |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 159 | } |
| 160 | EXPORT_SYMBOL_GPL(reset_control_deassert); |
| 161 | |
| 162 | /** |
Dinh Nguyen | 729de41 | 2014-10-10 10:21:14 -0500 | [diff] [blame] | 163 | * reset_control_status - returns a negative errno if not supported, a |
| 164 | * positive value if the reset line is asserted, or zero if the reset |
| 165 | * line is not asserted. |
| 166 | * @rstc: reset controller |
| 167 | */ |
| 168 | int reset_control_status(struct reset_control *rstc) |
| 169 | { |
| 170 | if (rstc->rcdev->ops->status) |
| 171 | return rstc->rcdev->ops->status(rstc->rcdev, rstc->id); |
| 172 | |
Philipp Zabel | 39b4da7 | 2015-10-29 09:55:00 +0100 | [diff] [blame] | 173 | return -ENOTSUPP; |
Dinh Nguyen | 729de41 | 2014-10-10 10:21:14 -0500 | [diff] [blame] | 174 | } |
| 175 | EXPORT_SYMBOL_GPL(reset_control_status); |
| 176 | |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 177 | static struct reset_control *__reset_control_get( |
| 178 | struct reset_controller_dev *rcdev, |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 179 | unsigned int index, int shared) |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 180 | { |
| 181 | struct reset_control *rstc; |
| 182 | |
| 183 | lockdep_assert_held(&reset_list_mutex); |
| 184 | |
| 185 | list_for_each_entry(rstc, &rcdev->reset_control_head, list) { |
| 186 | if (rstc->id == index) { |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 187 | if (WARN_ON(!rstc->shared || !shared)) |
| 188 | return ERR_PTR(-EBUSY); |
| 189 | |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 190 | rstc->refcnt++; |
| 191 | return rstc; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | rstc = kzalloc(sizeof(*rstc), GFP_KERNEL); |
| 196 | if (!rstc) |
| 197 | return ERR_PTR(-ENOMEM); |
| 198 | |
| 199 | try_module_get(rcdev->owner); |
| 200 | |
| 201 | rstc->rcdev = rcdev; |
| 202 | list_add(&rstc->list, &rcdev->reset_control_head); |
| 203 | rstc->id = index; |
| 204 | rstc->refcnt = 1; |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 205 | rstc->shared = shared; |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 206 | |
| 207 | return rstc; |
| 208 | } |
| 209 | |
| 210 | static void __reset_control_put(struct reset_control *rstc) |
| 211 | { |
| 212 | lockdep_assert_held(&reset_list_mutex); |
| 213 | |
| 214 | if (--rstc->refcnt) |
| 215 | return; |
| 216 | |
| 217 | module_put(rstc->rcdev->owner); |
| 218 | |
| 219 | list_del(&rstc->list); |
| 220 | kfree(rstc); |
| 221 | } |
| 222 | |
Hans de Goede | 6c96f05 | 2016-02-23 18:46:24 +0100 | [diff] [blame] | 223 | struct reset_control *__of_reset_control_get(struct device_node *node, |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 224 | const char *id, int index, int shared) |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 225 | { |
Philipp Zabel | d056c9b | 2015-12-08 18:49:53 +0100 | [diff] [blame] | 226 | struct reset_control *rstc; |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 227 | struct reset_controller_dev *r, *rcdev; |
| 228 | struct of_phandle_args args; |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 229 | int rstc_id; |
| 230 | int ret; |
| 231 | |
Hans de Goede | 6c96f05 | 2016-02-23 18:46:24 +0100 | [diff] [blame] | 232 | if (!node) |
| 233 | return ERR_PTR(-EINVAL); |
| 234 | |
| 235 | if (id) { |
| 236 | index = of_property_match_string(node, |
| 237 | "reset-names", id); |
| 238 | if (index < 0) |
| 239 | return ERR_PTR(-ENOENT); |
| 240 | } |
| 241 | |
Maxime Ripard | fc0a592 | 2013-12-20 22:41:07 +0100 | [diff] [blame] | 242 | ret = of_parse_phandle_with_args(node, "resets", "#reset-cells", |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 243 | index, &args); |
| 244 | if (ret) |
| 245 | return ERR_PTR(ret); |
| 246 | |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 247 | mutex_lock(&reset_list_mutex); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 248 | rcdev = NULL; |
| 249 | list_for_each_entry(r, &reset_controller_list, list) { |
| 250 | if (args.np == r->of_node) { |
| 251 | rcdev = r; |
| 252 | break; |
| 253 | } |
| 254 | } |
| 255 | of_node_put(args.np); |
| 256 | |
| 257 | if (!rcdev) { |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 258 | mutex_unlock(&reset_list_mutex); |
Philipp Zabel | 3d10302 | 2013-07-18 13:55:22 +0200 | [diff] [blame] | 259 | return ERR_PTR(-EPROBE_DEFER); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 260 | } |
| 261 | |
Maxime Ripard | e677774 | 2016-01-14 16:24:44 +0100 | [diff] [blame] | 262 | if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) { |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 263 | mutex_unlock(&reset_list_mutex); |
Maxime Ripard | e677774 | 2016-01-14 16:24:44 +0100 | [diff] [blame] | 264 | return ERR_PTR(-EINVAL); |
| 265 | } |
| 266 | |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 267 | rstc_id = rcdev->of_xlate(rcdev, &args); |
| 268 | if (rstc_id < 0) { |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 269 | mutex_unlock(&reset_list_mutex); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 270 | return ERR_PTR(rstc_id); |
| 271 | } |
| 272 | |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 273 | /* reset_list_mutex also protects the rcdev's reset_control list */ |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 274 | rstc = __reset_control_get(rcdev, rstc_id, shared); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 275 | |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 276 | mutex_unlock(&reset_list_mutex); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 277 | |
| 278 | return rstc; |
| 279 | } |
Hans de Goede | 6c96f05 | 2016-02-23 18:46:24 +0100 | [diff] [blame] | 280 | EXPORT_SYMBOL_GPL(__of_reset_control_get); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 281 | |
| 282 | /** |
| 283 | * reset_control_put - free the reset controller |
| 284 | * @rstc: reset controller |
| 285 | */ |
| 286 | |
| 287 | void reset_control_put(struct reset_control *rstc) |
| 288 | { |
| 289 | if (IS_ERR(rstc)) |
| 290 | return; |
| 291 | |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame] | 292 | mutex_lock(&reset_list_mutex); |
| 293 | __reset_control_put(rstc); |
| 294 | mutex_unlock(&reset_list_mutex); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 295 | } |
| 296 | EXPORT_SYMBOL_GPL(reset_control_put); |
| 297 | |
| 298 | static void devm_reset_control_release(struct device *dev, void *res) |
| 299 | { |
| 300 | reset_control_put(*(struct reset_control **)res); |
| 301 | } |
| 302 | |
Hans de Goede | 6c96f05 | 2016-02-23 18:46:24 +0100 | [diff] [blame] | 303 | struct reset_control *__devm_reset_control_get(struct device *dev, |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 304 | const char *id, int index, int shared) |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 305 | { |
| 306 | struct reset_control **ptr, *rstc; |
| 307 | |
| 308 | ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr), |
| 309 | GFP_KERNEL); |
| 310 | if (!ptr) |
| 311 | return ERR_PTR(-ENOMEM); |
| 312 | |
Hans de Goede | 0b52297 | 2016-02-23 18:46:26 +0100 | [diff] [blame] | 313 | rstc = __of_reset_control_get(dev ? dev->of_node : NULL, |
| 314 | id, index, shared); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 315 | if (!IS_ERR(rstc)) { |
| 316 | *ptr = rstc; |
| 317 | devres_add(dev, ptr); |
| 318 | } else { |
| 319 | devres_free(ptr); |
| 320 | } |
| 321 | |
| 322 | return rstc; |
| 323 | } |
Hans de Goede | 6c96f05 | 2016-02-23 18:46:24 +0100 | [diff] [blame] | 324 | EXPORT_SYMBOL_GPL(__devm_reset_control_get); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 325 | |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 326 | /** |
| 327 | * device_reset - find reset controller associated with the device |
| 328 | * and perform reset |
| 329 | * @dev: device to be reset by the controller |
| 330 | * |
| 331 | * Convenience wrapper for reset_control_get() and reset_control_reset(). |
| 332 | * This is useful for the common case of devices with single, dedicated reset |
| 333 | * lines. |
| 334 | */ |
| 335 | int device_reset(struct device *dev) |
| 336 | { |
| 337 | struct reset_control *rstc; |
| 338 | int ret; |
| 339 | |
| 340 | rstc = reset_control_get(dev, NULL); |
| 341 | if (IS_ERR(rstc)) |
| 342 | return PTR_ERR(rstc); |
| 343 | |
| 344 | ret = reset_control_reset(rstc); |
| 345 | |
| 346 | reset_control_put(rstc); |
| 347 | |
| 348 | return ret; |
| 349 | } |
| 350 | EXPORT_SYMBOL_GPL(device_reset); |