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 21static DEFINE_MUTEX(reset_controller_list_mutex); 22static 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 */ 31struct reset_control { 32 struct reset_controller_dev *rcdev; 33 struct device *dev; 34 unsigned int id; 35}; 36 37/** 38 * of_reset_simple_xlate - translate reset_spec to the reset line number 39 * @rcdev: a pointer to the reset controller device 40 * @reset_spec: reset line specifier as found in the device tree 41 * @flags: a flags pointer to fill in (optional) 42 * 43 * This simple translation function should be used for reset controllers 44 * with 1:1 mapping, where reset lines can be indexed by number without gaps. 45 */ 46static int of_reset_simple_xlate(struct reset_controller_dev *rcdev, 47 const struct of_phandle_args *reset_spec) 48{ 49 if (WARN_ON(reset_spec->args_count != rcdev->of_reset_n_cells)) 50 return -EINVAL; 51 52 if (reset_spec->args[0] >= rcdev->nr_resets) 53 return -EINVAL; 54 55 return reset_spec->args[0]; 56} 57 58/** 59 * reset_controller_register - register a reset controller device 60 * @rcdev: a pointer to the initialized reset controller device 61 */ 62int 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 69 mutex_lock(&reset_controller_list_mutex); 70 list_add(&rcdev->list, &reset_controller_list); 71 mutex_unlock(&reset_controller_list_mutex); 72 73 return 0; 74} 75EXPORT_SYMBOL_GPL(reset_controller_register); 76 77/** 78 * reset_controller_unregister - unregister a reset controller device 79 * @rcdev: a pointer to the reset controller device 80 */ 81void reset_controller_unregister(struct reset_controller_dev *rcdev) 82{ 83 mutex_lock(&reset_controller_list_mutex); 84 list_del(&rcdev->list); 85 mutex_unlock(&reset_controller_list_mutex); 86} 87EXPORT_SYMBOL_GPL(reset_controller_unregister); 88 89/** 90 * reset_control_reset - reset the controlled device 91 * @rstc: reset controller 92 */ 93int reset_control_reset(struct reset_control *rstc) 94{ 95 if (rstc->rcdev->ops->reset) 96 return rstc->rcdev->ops->reset(rstc->rcdev, rstc->id); 97 98 return -ENOSYS; 99} 100EXPORT_SYMBOL_GPL(reset_control_reset); 101 102/** 103 * reset_control_assert - asserts the reset line 104 * @rstc: reset controller 105 */ 106int reset_control_assert(struct reset_control *rstc) 107{ 108 if (rstc->rcdev->ops->assert) 109 return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id); 110 111 return -ENOSYS; 112} 113EXPORT_SYMBOL_GPL(reset_control_assert); 114 115/** 116 * reset_control_deassert - deasserts the reset line 117 * @rstc: reset controller 118 */ 119int reset_control_deassert(struct reset_control *rstc) 120{ 121 if (rstc->rcdev->ops->deassert) 122 return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id); 123 124 return -ENOSYS; 125} 126EXPORT_SYMBOL_GPL(reset_control_deassert); 127 128/** 129 * reset_control_status - returns a negative errno if not supported, a 130 * positive value if the reset line is asserted, or zero if the reset 131 * line is not asserted. 132 * @rstc: reset controller 133 */ 134int reset_control_status(struct reset_control *rstc) 135{ 136 if (rstc->rcdev->ops->status) 137 return rstc->rcdev->ops->status(rstc->rcdev, rstc->id); 138 139 return -ENOSYS; 140} 141EXPORT_SYMBOL_GPL(reset_control_status); 142 143/** 144 * of_reset_control_get - Lookup and obtain a reference to a reset controller. 145 * @node: device to be reset by the controller 146 * @id: reset line name 147 * 148 * Returns a struct reset_control or IS_ERR() condition containing errno. 149 * 150 * Use of id names is optional. 151 */ 152struct reset_control *of_reset_control_get(struct device_node *node, 153 const char *id) 154{ 155 struct reset_control *rstc = ERR_PTR(-EPROBE_DEFER); 156 struct reset_controller_dev *r, *rcdev; 157 struct of_phandle_args args; 158 int index = 0; 159 int rstc_id; 160 int ret; 161 162 if (id) 163 index = of_property_match_string(node, 164 "reset-names", id); 165 ret = of_parse_phandle_with_args(node, "resets", "#reset-cells", 166 index, &args); 167 if (ret) 168 return ERR_PTR(ret); 169 170 mutex_lock(&reset_controller_list_mutex); 171 rcdev = NULL; 172 list_for_each_entry(r, &reset_controller_list, list) { 173 if (args.np == r->of_node) { 174 rcdev = r; 175 break; 176 } 177 } 178 of_node_put(args.np); 179 180 if (!rcdev) { 181 mutex_unlock(&reset_controller_list_mutex); 182 return ERR_PTR(-EPROBE_DEFER); 183 } 184 185 rstc_id = rcdev->of_xlate(rcdev, &args); 186 if (rstc_id < 0) { 187 mutex_unlock(&reset_controller_list_mutex); 188 return ERR_PTR(rstc_id); 189 } 190 191 try_module_get(rcdev->owner); 192 mutex_unlock(&reset_controller_list_mutex); 193 194 rstc = kzalloc(sizeof(*rstc), GFP_KERNEL); 195 if (!rstc) { 196 module_put(rcdev->owner); 197 return ERR_PTR(-ENOMEM); 198 } 199 200 rstc->rcdev = rcdev; 201 rstc->id = rstc_id; 202 203 return rstc; 204} 205EXPORT_SYMBOL_GPL(of_reset_control_get); 206 207/** 208 * reset_control_get - Lookup and obtain a reference to a reset controller. 209 * @dev: device to be reset by the controller 210 * @id: reset line name 211 * 212 * Returns a struct reset_control or IS_ERR() condition containing errno. 213 * 214 * Use of id names is optional. 215 */ 216struct reset_control *reset_control_get(struct device *dev, const char *id) 217{ 218 struct reset_control *rstc; 219 220 if (!dev) 221 return ERR_PTR(-EINVAL); 222 223 rstc = of_reset_control_get(dev->of_node, id); 224 if (!IS_ERR(rstc)) 225 rstc->dev = dev; 226 227 return rstc; 228} 229EXPORT_SYMBOL_GPL(reset_control_get); 230 231/** 232 * reset_control_put - free the reset controller 233 * @rstc: reset controller 234 */ 235 236void reset_control_put(struct reset_control *rstc) 237{ 238 if (IS_ERR(rstc)) 239 return; 240 241 module_put(rstc->rcdev->owner); 242 kfree(rstc); 243} 244EXPORT_SYMBOL_GPL(reset_control_put); 245 246static void devm_reset_control_release(struct device *dev, void *res) 247{ 248 reset_control_put(*(struct reset_control **)res); 249} 250 251/** 252 * devm_reset_control_get - resource managed reset_control_get() 253 * @dev: device to be reset by the controller 254 * @id: reset line name 255 * 256 * Managed reset_control_get(). For reset controllers returned from this 257 * function, reset_control_put() is called automatically on driver detach. 258 * See reset_control_get() for more information. 259 */ 260struct reset_control *devm_reset_control_get(struct device *dev, const char *id) 261{ 262 struct reset_control **ptr, *rstc; 263 264 ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr), 265 GFP_KERNEL); 266 if (!ptr) 267 return ERR_PTR(-ENOMEM); 268 269 rstc = reset_control_get(dev, id); 270 if (!IS_ERR(rstc)) { 271 *ptr = rstc; 272 devres_add(dev, ptr); 273 } else { 274 devres_free(ptr); 275 } 276 277 return rstc; 278} 279EXPORT_SYMBOL_GPL(devm_reset_control_get); 280 281/** 282 * device_reset - find reset controller associated with the device 283 * and perform reset 284 * @dev: device to be reset by the controller 285 * 286 * Convenience wrapper for reset_control_get() and reset_control_reset(). 287 * This is useful for the common case of devices with single, dedicated reset 288 * lines. 289 */ 290int device_reset(struct device *dev) 291{ 292 struct reset_control *rstc; 293 int ret; 294 295 rstc = reset_control_get(dev, NULL); 296 if (IS_ERR(rstc)) 297 return PTR_ERR(rstc); 298 299 ret = reset_control_reset(rstc); 300 301 reset_control_put(rstc); 302 303 return ret; 304} 305EXPORT_SYMBOL_GPL(device_reset); 306