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 | */ |
| 11 | #include <linux/device.h> |
| 12 | #include <linux/err.h> |
| 13 | #include <linux/export.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/of.h> |
| 17 | #include <linux/reset.h> |
| 18 | #include <linux/reset-controller.h> |
| 19 | #include <linux/slab.h> |
| 20 | |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame^] | 21 | static DEFINE_MUTEX(reset_list_mutex); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 22 | static LIST_HEAD(reset_controller_list); |
| 23 | |
| 24 | /** |
| 25 | * struct reset_control - a reset control |
| 26 | * @rcdev: a pointer to the reset controller device |
| 27 | * this reset control belongs to |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame^] | 28 | * @list: list entry for the rcdev's reset controller list |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 29 | * @id: ID of the reset controller in the reset |
| 30 | * controller device |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame^] | 31 | * @refcnt: Number of gets of this reset_control |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 32 | */ |
| 33 | struct reset_control { |
| 34 | struct reset_controller_dev *rcdev; |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame^] | 35 | struct list_head list; |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 36 | unsigned int id; |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame^] | 37 | unsigned int refcnt; |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | /** |
| 41 | * of_reset_simple_xlate - translate reset_spec to the reset line number |
| 42 | * @rcdev: a pointer to the reset controller device |
| 43 | * @reset_spec: reset line specifier as found in the device tree |
| 44 | * @flags: a flags pointer to fill in (optional) |
| 45 | * |
| 46 | * This simple translation function should be used for reset controllers |
| 47 | * with 1:1 mapping, where reset lines can be indexed by number without gaps. |
| 48 | */ |
Rashika Kheria | 0c5b2b9 | 2013-12-19 14:11:10 +0530 | [diff] [blame] | 49 | static int of_reset_simple_xlate(struct reset_controller_dev *rcdev, |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 50 | const struct of_phandle_args *reset_spec) |
| 51 | { |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 52 | if (reset_spec->args[0] >= rcdev->nr_resets) |
| 53 | return -EINVAL; |
| 54 | |
| 55 | return reset_spec->args[0]; |
| 56 | } |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * reset_controller_register - register a reset controller device |
| 60 | * @rcdev: a pointer to the initialized reset controller device |
| 61 | */ |
| 62 | int reset_controller_register(struct reset_controller_dev *rcdev) |
| 63 | { |
| 64 | if (!rcdev->of_xlate) { |
| 65 | rcdev->of_reset_n_cells = 1; |
| 66 | rcdev->of_xlate = of_reset_simple_xlate; |
| 67 | } |
| 68 | |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame^] | 69 | INIT_LIST_HEAD(&rcdev->reset_control_head); |
| 70 | |
| 71 | mutex_lock(&reset_list_mutex); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 72 | list_add(&rcdev->list, &reset_controller_list); |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame^] | 73 | mutex_unlock(&reset_list_mutex); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | EXPORT_SYMBOL_GPL(reset_controller_register); |
| 78 | |
| 79 | /** |
| 80 | * reset_controller_unregister - unregister a reset controller device |
| 81 | * @rcdev: a pointer to the reset controller device |
| 82 | */ |
| 83 | void reset_controller_unregister(struct reset_controller_dev *rcdev) |
| 84 | { |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame^] | 85 | mutex_lock(&reset_list_mutex); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 86 | list_del(&rcdev->list); |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame^] | 87 | mutex_unlock(&reset_list_mutex); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 88 | } |
| 89 | EXPORT_SYMBOL_GPL(reset_controller_unregister); |
| 90 | |
| 91 | /** |
| 92 | * reset_control_reset - reset the controlled device |
| 93 | * @rstc: reset controller |
| 94 | */ |
| 95 | int reset_control_reset(struct reset_control *rstc) |
| 96 | { |
| 97 | if (rstc->rcdev->ops->reset) |
| 98 | return rstc->rcdev->ops->reset(rstc->rcdev, rstc->id); |
| 99 | |
Philipp Zabel | 39b4da7 | 2015-10-29 09:55:00 +0100 | [diff] [blame] | 100 | return -ENOTSUPP; |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 101 | } |
| 102 | EXPORT_SYMBOL_GPL(reset_control_reset); |
| 103 | |
| 104 | /** |
| 105 | * reset_control_assert - asserts the reset line |
| 106 | * @rstc: reset controller |
| 107 | */ |
| 108 | int reset_control_assert(struct reset_control *rstc) |
| 109 | { |
| 110 | if (rstc->rcdev->ops->assert) |
| 111 | return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id); |
| 112 | |
Philipp Zabel | 39b4da7 | 2015-10-29 09:55:00 +0100 | [diff] [blame] | 113 | return -ENOTSUPP; |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 114 | } |
| 115 | EXPORT_SYMBOL_GPL(reset_control_assert); |
| 116 | |
| 117 | /** |
| 118 | * reset_control_deassert - deasserts the reset line |
| 119 | * @rstc: reset controller |
| 120 | */ |
| 121 | int reset_control_deassert(struct reset_control *rstc) |
| 122 | { |
| 123 | if (rstc->rcdev->ops->deassert) |
| 124 | return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id); |
| 125 | |
Philipp Zabel | 39b4da7 | 2015-10-29 09:55:00 +0100 | [diff] [blame] | 126 | return -ENOTSUPP; |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 127 | } |
| 128 | EXPORT_SYMBOL_GPL(reset_control_deassert); |
| 129 | |
| 130 | /** |
Dinh Nguyen | 729de41 | 2014-10-10 10:21:14 -0500 | [diff] [blame] | 131 | * reset_control_status - returns a negative errno if not supported, a |
| 132 | * positive value if the reset line is asserted, or zero if the reset |
| 133 | * line is not asserted. |
| 134 | * @rstc: reset controller |
| 135 | */ |
| 136 | int reset_control_status(struct reset_control *rstc) |
| 137 | { |
| 138 | if (rstc->rcdev->ops->status) |
| 139 | return rstc->rcdev->ops->status(rstc->rcdev, rstc->id); |
| 140 | |
Philipp Zabel | 39b4da7 | 2015-10-29 09:55:00 +0100 | [diff] [blame] | 141 | return -ENOTSUPP; |
Dinh Nguyen | 729de41 | 2014-10-10 10:21:14 -0500 | [diff] [blame] | 142 | } |
| 143 | EXPORT_SYMBOL_GPL(reset_control_status); |
| 144 | |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame^] | 145 | static struct reset_control *__reset_control_get( |
| 146 | struct reset_controller_dev *rcdev, |
| 147 | unsigned int index) |
| 148 | { |
| 149 | struct reset_control *rstc; |
| 150 | |
| 151 | lockdep_assert_held(&reset_list_mutex); |
| 152 | |
| 153 | list_for_each_entry(rstc, &rcdev->reset_control_head, list) { |
| 154 | if (rstc->id == index) { |
| 155 | rstc->refcnt++; |
| 156 | return rstc; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | rstc = kzalloc(sizeof(*rstc), GFP_KERNEL); |
| 161 | if (!rstc) |
| 162 | return ERR_PTR(-ENOMEM); |
| 163 | |
| 164 | try_module_get(rcdev->owner); |
| 165 | |
| 166 | rstc->rcdev = rcdev; |
| 167 | list_add(&rstc->list, &rcdev->reset_control_head); |
| 168 | rstc->id = index; |
| 169 | rstc->refcnt = 1; |
| 170 | |
| 171 | return rstc; |
| 172 | } |
| 173 | |
| 174 | static void __reset_control_put(struct reset_control *rstc) |
| 175 | { |
| 176 | lockdep_assert_held(&reset_list_mutex); |
| 177 | |
| 178 | if (--rstc->refcnt) |
| 179 | return; |
| 180 | |
| 181 | module_put(rstc->rcdev->owner); |
| 182 | |
| 183 | list_del(&rstc->list); |
| 184 | kfree(rstc); |
| 185 | } |
| 186 | |
Hans de Goede | 6c96f05 | 2016-02-23 18:46:24 +0100 | [diff] [blame] | 187 | struct reset_control *__of_reset_control_get(struct device_node *node, |
| 188 | const char *id, int index) |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 189 | { |
Philipp Zabel | d056c9b | 2015-12-08 18:49:53 +0100 | [diff] [blame] | 190 | struct reset_control *rstc; |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 191 | struct reset_controller_dev *r, *rcdev; |
| 192 | struct of_phandle_args args; |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 193 | int rstc_id; |
| 194 | int ret; |
| 195 | |
Hans de Goede | 6c96f05 | 2016-02-23 18:46:24 +0100 | [diff] [blame] | 196 | if (!node) |
| 197 | return ERR_PTR(-EINVAL); |
| 198 | |
| 199 | if (id) { |
| 200 | index = of_property_match_string(node, |
| 201 | "reset-names", id); |
| 202 | if (index < 0) |
| 203 | return ERR_PTR(-ENOENT); |
| 204 | } |
| 205 | |
Maxime Ripard | fc0a592 | 2013-12-20 22:41:07 +0100 | [diff] [blame] | 206 | ret = of_parse_phandle_with_args(node, "resets", "#reset-cells", |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 207 | index, &args); |
| 208 | if (ret) |
| 209 | return ERR_PTR(ret); |
| 210 | |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame^] | 211 | mutex_lock(&reset_list_mutex); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 212 | rcdev = NULL; |
| 213 | list_for_each_entry(r, &reset_controller_list, list) { |
| 214 | if (args.np == r->of_node) { |
| 215 | rcdev = r; |
| 216 | break; |
| 217 | } |
| 218 | } |
| 219 | of_node_put(args.np); |
| 220 | |
| 221 | if (!rcdev) { |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame^] | 222 | mutex_unlock(&reset_list_mutex); |
Philipp Zabel | 3d10302 | 2013-07-18 13:55:22 +0200 | [diff] [blame] | 223 | return ERR_PTR(-EPROBE_DEFER); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 224 | } |
| 225 | |
Maxime Ripard | e677774 | 2016-01-14 16:24:44 +0100 | [diff] [blame] | 226 | if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) { |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame^] | 227 | mutex_unlock(&reset_list_mutex); |
Maxime Ripard | e677774 | 2016-01-14 16:24:44 +0100 | [diff] [blame] | 228 | return ERR_PTR(-EINVAL); |
| 229 | } |
| 230 | |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 231 | rstc_id = rcdev->of_xlate(rcdev, &args); |
| 232 | if (rstc_id < 0) { |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame^] | 233 | mutex_unlock(&reset_list_mutex); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 234 | return ERR_PTR(rstc_id); |
| 235 | } |
| 236 | |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame^] | 237 | /* reset_list_mutex also protects the rcdev's reset_control list */ |
| 238 | rstc = __reset_control_get(rcdev, rstc_id); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 239 | |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame^] | 240 | mutex_unlock(&reset_list_mutex); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 241 | |
| 242 | return rstc; |
| 243 | } |
Hans de Goede | 6c96f05 | 2016-02-23 18:46:24 +0100 | [diff] [blame] | 244 | EXPORT_SYMBOL_GPL(__of_reset_control_get); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 245 | |
| 246 | /** |
| 247 | * reset_control_put - free the reset controller |
| 248 | * @rstc: reset controller |
| 249 | */ |
| 250 | |
| 251 | void reset_control_put(struct reset_control *rstc) |
| 252 | { |
| 253 | if (IS_ERR(rstc)) |
| 254 | return; |
| 255 | |
Hans de Goede | c15ddec | 2016-02-23 18:46:25 +0100 | [diff] [blame^] | 256 | mutex_lock(&reset_list_mutex); |
| 257 | __reset_control_put(rstc); |
| 258 | mutex_unlock(&reset_list_mutex); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 259 | } |
| 260 | EXPORT_SYMBOL_GPL(reset_control_put); |
| 261 | |
| 262 | static void devm_reset_control_release(struct device *dev, void *res) |
| 263 | { |
| 264 | reset_control_put(*(struct reset_control **)res); |
| 265 | } |
| 266 | |
Hans de Goede | 6c96f05 | 2016-02-23 18:46:24 +0100 | [diff] [blame] | 267 | struct reset_control *__devm_reset_control_get(struct device *dev, |
| 268 | const char *id, int index) |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 269 | { |
| 270 | struct reset_control **ptr, *rstc; |
| 271 | |
| 272 | ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr), |
| 273 | GFP_KERNEL); |
| 274 | if (!ptr) |
| 275 | return ERR_PTR(-ENOMEM); |
| 276 | |
Hans de Goede | 6c96f05 | 2016-02-23 18:46:24 +0100 | [diff] [blame] | 277 | rstc = __of_reset_control_get(dev ? dev->of_node : NULL, id, index); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 278 | if (!IS_ERR(rstc)) { |
| 279 | *ptr = rstc; |
| 280 | devres_add(dev, ptr); |
| 281 | } else { |
| 282 | devres_free(ptr); |
| 283 | } |
| 284 | |
| 285 | return rstc; |
| 286 | } |
Hans de Goede | 6c96f05 | 2016-02-23 18:46:24 +0100 | [diff] [blame] | 287 | EXPORT_SYMBOL_GPL(__devm_reset_control_get); |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 288 | |
Philipp Zabel | 61fc413 | 2012-11-19 17:23:13 +0100 | [diff] [blame] | 289 | /** |
| 290 | * device_reset - find reset controller associated with the device |
| 291 | * and perform reset |
| 292 | * @dev: device to be reset by the controller |
| 293 | * |
| 294 | * Convenience wrapper for reset_control_get() and reset_control_reset(). |
| 295 | * This is useful for the common case of devices with single, dedicated reset |
| 296 | * lines. |
| 297 | */ |
| 298 | int device_reset(struct device *dev) |
| 299 | { |
| 300 | struct reset_control *rstc; |
| 301 | int ret; |
| 302 | |
| 303 | rstc = reset_control_get(dev, NULL); |
| 304 | if (IS_ERR(rstc)) |
| 305 | return PTR_ERR(rstc); |
| 306 | |
| 307 | ret = reset_control_reset(rstc); |
| 308 | |
| 309 | reset_control_put(rstc); |
| 310 | |
| 311 | return ret; |
| 312 | } |
| 313 | EXPORT_SYMBOL_GPL(device_reset); |