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