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/atomic.h> 12 #include <linux/device.h> 13 #include <linux/err.h> 14 #include <linux/export.h> 15 #include <linux/kernel.h> 16 #include <linux/kref.h> 17 #include <linux/module.h> 18 #include <linux/of.h> 19 #include <linux/reset.h> 20 #include <linux/reset-controller.h> 21 #include <linux/slab.h> 22 23 static DEFINE_MUTEX(reset_list_mutex); 24 static LIST_HEAD(reset_controller_list); 25 26 /** 27 * struct reset_control - a reset control 28 * @rcdev: a pointer to the reset controller device 29 * this reset control belongs to 30 * @list: list entry for the rcdev's reset controller list 31 * @id: ID of the reset controller in the reset 32 * controller device 33 * @refcnt: Number of gets of this reset_control 34 * @shared: Is this a shared (1), or an exclusive (0) reset_control? 35 * @deassert_cnt: Number of times this reset line has been deasserted 36 * @triggered_count: Number of times this reset line has been reset. Currently 37 * only used for shared resets, which means that the value 38 * will be either 0 or 1. 39 */ 40 struct reset_control { 41 struct reset_controller_dev *rcdev; 42 struct list_head list; 43 unsigned int id; 44 struct kref refcnt; 45 bool shared; 46 atomic_t deassert_count; 47 atomic_t triggered_count; 48 }; 49 50 /** 51 * of_reset_simple_xlate - translate reset_spec to the reset line number 52 * @rcdev: a pointer to the reset controller device 53 * @reset_spec: reset line specifier as found in the device tree 54 * @flags: a flags pointer to fill in (optional) 55 * 56 * This simple translation function should be used for reset controllers 57 * with 1:1 mapping, where reset lines can be indexed by number without gaps. 58 */ 59 static int of_reset_simple_xlate(struct reset_controller_dev *rcdev, 60 const struct of_phandle_args *reset_spec) 61 { 62 if (reset_spec->args[0] >= rcdev->nr_resets) 63 return -EINVAL; 64 65 return reset_spec->args[0]; 66 } 67 68 /** 69 * reset_controller_register - register a reset controller device 70 * @rcdev: a pointer to the initialized reset controller device 71 */ 72 int reset_controller_register(struct reset_controller_dev *rcdev) 73 { 74 if (!rcdev->of_xlate) { 75 rcdev->of_reset_n_cells = 1; 76 rcdev->of_xlate = of_reset_simple_xlate; 77 } 78 79 INIT_LIST_HEAD(&rcdev->reset_control_head); 80 81 mutex_lock(&reset_list_mutex); 82 list_add(&rcdev->list, &reset_controller_list); 83 mutex_unlock(&reset_list_mutex); 84 85 return 0; 86 } 87 EXPORT_SYMBOL_GPL(reset_controller_register); 88 89 /** 90 * reset_controller_unregister - unregister a reset controller device 91 * @rcdev: a pointer to the reset controller device 92 */ 93 void reset_controller_unregister(struct reset_controller_dev *rcdev) 94 { 95 mutex_lock(&reset_list_mutex); 96 list_del(&rcdev->list); 97 mutex_unlock(&reset_list_mutex); 98 } 99 EXPORT_SYMBOL_GPL(reset_controller_unregister); 100 101 static void devm_reset_controller_release(struct device *dev, void *res) 102 { 103 reset_controller_unregister(*(struct reset_controller_dev **)res); 104 } 105 106 /** 107 * devm_reset_controller_register - resource managed reset_controller_register() 108 * @dev: device that is registering this reset controller 109 * @rcdev: a pointer to the initialized reset controller device 110 * 111 * Managed reset_controller_register(). For reset controllers registered by 112 * this function, reset_controller_unregister() is automatically called on 113 * driver detach. See reset_controller_register() for more information. 114 */ 115 int devm_reset_controller_register(struct device *dev, 116 struct reset_controller_dev *rcdev) 117 { 118 struct reset_controller_dev **rcdevp; 119 int ret; 120 121 rcdevp = devres_alloc(devm_reset_controller_release, sizeof(*rcdevp), 122 GFP_KERNEL); 123 if (!rcdevp) 124 return -ENOMEM; 125 126 ret = reset_controller_register(rcdev); 127 if (!ret) { 128 *rcdevp = rcdev; 129 devres_add(dev, rcdevp); 130 } else { 131 devres_free(rcdevp); 132 } 133 134 return ret; 135 } 136 EXPORT_SYMBOL_GPL(devm_reset_controller_register); 137 138 /** 139 * reset_control_reset - reset the controlled device 140 * @rstc: reset controller 141 * 142 * On a shared reset line the actual reset pulse is only triggered once for the 143 * lifetime of the reset_control instance: for all but the first caller this is 144 * a no-op. 145 * Consumers must not use reset_control_(de)assert on shared reset lines when 146 * reset_control_reset has been used. 147 * 148 * If rstc is NULL it is an optional reset and the function will just 149 * return 0. 150 */ 151 int reset_control_reset(struct reset_control *rstc) 152 { 153 int ret; 154 155 if (!rstc) 156 return 0; 157 158 if (WARN_ON(IS_ERR(rstc))) 159 return -EINVAL; 160 161 if (!rstc->rcdev->ops->reset) 162 return -ENOTSUPP; 163 164 if (rstc->shared) { 165 if (WARN_ON(atomic_read(&rstc->deassert_count) != 0)) 166 return -EINVAL; 167 168 if (atomic_inc_return(&rstc->triggered_count) != 1) 169 return 0; 170 } 171 172 ret = rstc->rcdev->ops->reset(rstc->rcdev, rstc->id); 173 if (rstc->shared && ret) 174 atomic_dec(&rstc->triggered_count); 175 176 return ret; 177 } 178 EXPORT_SYMBOL_GPL(reset_control_reset); 179 180 /** 181 * reset_control_assert - asserts the reset line 182 * @rstc: reset controller 183 * 184 * Calling this on an exclusive reset controller guarantees that the reset 185 * will be asserted. When called on a shared reset controller the line may 186 * still be deasserted, as long as other users keep it so. 187 * 188 * For shared reset controls a driver cannot expect the hw's registers and 189 * internal state to be reset, but must be prepared for this to happen. 190 * Consumers must not use reset_control_reset on shared reset lines when 191 * reset_control_(de)assert has been used. 192 * return 0. 193 * 194 * If rstc is NULL it is an optional reset and the function will just 195 * return 0. 196 */ 197 int reset_control_assert(struct reset_control *rstc) 198 { 199 if (!rstc) 200 return 0; 201 202 if (WARN_ON(IS_ERR(rstc))) 203 return -EINVAL; 204 205 if (!rstc->rcdev->ops->assert) 206 return -ENOTSUPP; 207 208 if (rstc->shared) { 209 if (WARN_ON(atomic_read(&rstc->triggered_count) != 0)) 210 return -EINVAL; 211 212 if (WARN_ON(atomic_read(&rstc->deassert_count) == 0)) 213 return -EINVAL; 214 215 if (atomic_dec_return(&rstc->deassert_count) != 0) 216 return 0; 217 } 218 219 return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id); 220 } 221 EXPORT_SYMBOL_GPL(reset_control_assert); 222 223 /** 224 * reset_control_deassert - deasserts the reset line 225 * @rstc: reset controller 226 * 227 * After calling this function, the reset is guaranteed to be deasserted. 228 * Consumers must not use reset_control_reset on shared reset lines when 229 * reset_control_(de)assert has been used. 230 * return 0. 231 * 232 * If rstc is NULL it is an optional reset and the function will just 233 * return 0. 234 */ 235 int reset_control_deassert(struct reset_control *rstc) 236 { 237 if (!rstc) 238 return 0; 239 240 if (WARN_ON(IS_ERR(rstc))) 241 return -EINVAL; 242 243 if (!rstc->rcdev->ops->deassert) 244 return -ENOTSUPP; 245 246 if (rstc->shared) { 247 if (WARN_ON(atomic_read(&rstc->triggered_count) != 0)) 248 return -EINVAL; 249 250 if (atomic_inc_return(&rstc->deassert_count) != 1) 251 return 0; 252 } 253 254 return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id); 255 } 256 EXPORT_SYMBOL_GPL(reset_control_deassert); 257 258 /** 259 * reset_control_status - returns a negative errno if not supported, a 260 * positive value if the reset line is asserted, or zero if the reset 261 * line is not asserted or if the desc is NULL (optional reset). 262 * @rstc: reset controller 263 */ 264 int reset_control_status(struct reset_control *rstc) 265 { 266 if (!rstc) 267 return 0; 268 269 if (WARN_ON(IS_ERR(rstc))) 270 return -EINVAL; 271 272 if (rstc->rcdev->ops->status) 273 return rstc->rcdev->ops->status(rstc->rcdev, rstc->id); 274 275 return -ENOTSUPP; 276 } 277 EXPORT_SYMBOL_GPL(reset_control_status); 278 279 static struct reset_control *__reset_control_get_internal( 280 struct reset_controller_dev *rcdev, 281 unsigned int index, bool shared) 282 { 283 struct reset_control *rstc; 284 285 lockdep_assert_held(&reset_list_mutex); 286 287 list_for_each_entry(rstc, &rcdev->reset_control_head, list) { 288 if (rstc->id == index) { 289 if (WARN_ON(!rstc->shared || !shared)) 290 return ERR_PTR(-EBUSY); 291 292 kref_get(&rstc->refcnt); 293 return rstc; 294 } 295 } 296 297 rstc = kzalloc(sizeof(*rstc), GFP_KERNEL); 298 if (!rstc) 299 return ERR_PTR(-ENOMEM); 300 301 try_module_get(rcdev->owner); 302 303 rstc->rcdev = rcdev; 304 list_add(&rstc->list, &rcdev->reset_control_head); 305 rstc->id = index; 306 kref_init(&rstc->refcnt); 307 rstc->shared = shared; 308 309 return rstc; 310 } 311 312 static void __reset_control_release(struct kref *kref) 313 { 314 struct reset_control *rstc = container_of(kref, struct reset_control, 315 refcnt); 316 317 lockdep_assert_held(&reset_list_mutex); 318 319 module_put(rstc->rcdev->owner); 320 321 list_del(&rstc->list); 322 kfree(rstc); 323 } 324 325 static void __reset_control_put_internal(struct reset_control *rstc) 326 { 327 lockdep_assert_held(&reset_list_mutex); 328 329 kref_put(&rstc->refcnt, __reset_control_release); 330 } 331 332 struct reset_control *__of_reset_control_get(struct device_node *node, 333 const char *id, int index, bool shared, 334 bool optional) 335 { 336 struct reset_control *rstc; 337 struct reset_controller_dev *r, *rcdev; 338 struct of_phandle_args args; 339 int rstc_id; 340 int ret; 341 342 if (!node) 343 return ERR_PTR(-EINVAL); 344 345 if (id) { 346 index = of_property_match_string(node, 347 "reset-names", id); 348 if (index == -EILSEQ) 349 return ERR_PTR(index); 350 if (index < 0) 351 return optional ? NULL : ERR_PTR(-ENOENT); 352 } 353 354 ret = of_parse_phandle_with_args(node, "resets", "#reset-cells", 355 index, &args); 356 if (ret == -EINVAL) 357 return ERR_PTR(ret); 358 if (ret) 359 return optional ? NULL : ERR_PTR(ret); 360 361 mutex_lock(&reset_list_mutex); 362 rcdev = NULL; 363 list_for_each_entry(r, &reset_controller_list, list) { 364 if (args.np == r->of_node) { 365 rcdev = r; 366 break; 367 } 368 } 369 of_node_put(args.np); 370 371 if (!rcdev) { 372 mutex_unlock(&reset_list_mutex); 373 return ERR_PTR(-EPROBE_DEFER); 374 } 375 376 if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) { 377 mutex_unlock(&reset_list_mutex); 378 return ERR_PTR(-EINVAL); 379 } 380 381 rstc_id = rcdev->of_xlate(rcdev, &args); 382 if (rstc_id < 0) { 383 mutex_unlock(&reset_list_mutex); 384 return ERR_PTR(rstc_id); 385 } 386 387 /* reset_list_mutex also protects the rcdev's reset_control list */ 388 rstc = __reset_control_get_internal(rcdev, rstc_id, shared); 389 390 mutex_unlock(&reset_list_mutex); 391 392 return rstc; 393 } 394 EXPORT_SYMBOL_GPL(__of_reset_control_get); 395 396 struct reset_control *__reset_control_get(struct device *dev, const char *id, 397 int index, bool shared, bool optional) 398 { 399 if (dev->of_node) 400 return __of_reset_control_get(dev->of_node, id, index, shared, 401 optional); 402 403 return optional ? NULL : ERR_PTR(-EINVAL); 404 } 405 EXPORT_SYMBOL_GPL(__reset_control_get); 406 407 /** 408 * reset_control_put - free the reset controller 409 * @rstc: reset controller 410 */ 411 void reset_control_put(struct reset_control *rstc) 412 { 413 if (IS_ERR_OR_NULL(rstc)) 414 return; 415 416 mutex_lock(&reset_list_mutex); 417 __reset_control_put_internal(rstc); 418 mutex_unlock(&reset_list_mutex); 419 } 420 EXPORT_SYMBOL_GPL(reset_control_put); 421 422 static void devm_reset_control_release(struct device *dev, void *res) 423 { 424 reset_control_put(*(struct reset_control **)res); 425 } 426 427 struct reset_control *__devm_reset_control_get(struct device *dev, 428 const char *id, int index, bool shared, 429 bool optional) 430 { 431 struct reset_control **ptr, *rstc; 432 433 ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr), 434 GFP_KERNEL); 435 if (!ptr) 436 return ERR_PTR(-ENOMEM); 437 438 rstc = __reset_control_get(dev, id, index, shared, optional); 439 if (!IS_ERR(rstc)) { 440 *ptr = rstc; 441 devres_add(dev, ptr); 442 } else { 443 devres_free(ptr); 444 } 445 446 return rstc; 447 } 448 EXPORT_SYMBOL_GPL(__devm_reset_control_get); 449 450 /** 451 * device_reset - find reset controller associated with the device 452 * and perform reset 453 * @dev: device to be reset by the controller 454 * 455 * Convenience wrapper for reset_control_get() and reset_control_reset(). 456 * This is useful for the common case of devices with single, dedicated reset 457 * lines. 458 */ 459 int device_reset(struct device *dev) 460 { 461 struct reset_control *rstc; 462 int ret; 463 464 rstc = reset_control_get(dev, NULL); 465 if (IS_ERR(rstc)) 466 return PTR_ERR(rstc); 467 468 ret = reset_control_reset(rstc); 469 470 reset_control_put(rstc); 471 472 return ret; 473 } 474 EXPORT_SYMBOL_GPL(device_reset); 475