1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Reset Controller framework 4 * 5 * Copyright 2013 Philipp Zabel, Pengutronix 6 */ 7 #include <linux/atomic.h> 8 #include <linux/device.h> 9 #include <linux/err.h> 10 #include <linux/export.h> 11 #include <linux/kernel.h> 12 #include <linux/kref.h> 13 #include <linux/module.h> 14 #include <linux/of.h> 15 #include <linux/reset.h> 16 #include <linux/reset-controller.h> 17 #include <linux/slab.h> 18 19 static DEFINE_MUTEX(reset_list_mutex); 20 static LIST_HEAD(reset_controller_list); 21 22 static DEFINE_MUTEX(reset_lookup_mutex); 23 static LIST_HEAD(reset_lookup_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 29 * @list: list entry for the rcdev's reset controller list 30 * @id: ID of the reset controller in the reset 31 * controller device 32 * @refcnt: Number of gets of this reset_control 33 * @acquired: Only one reset_control may be acquired for a given rcdev and id. 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 acquired; 46 bool shared; 47 bool array; 48 atomic_t deassert_count; 49 atomic_t triggered_count; 50 }; 51 52 /** 53 * struct reset_control_array - an array of reset controls 54 * @base: reset control for compatibility with reset control API functions 55 * @num_rstcs: number of reset controls 56 * @rstc: array of reset controls 57 */ 58 struct reset_control_array { 59 struct reset_control base; 60 unsigned int num_rstcs; 61 struct reset_control *rstc[]; 62 }; 63 64 static const char *rcdev_name(struct reset_controller_dev *rcdev) 65 { 66 if (rcdev->dev) 67 return dev_name(rcdev->dev); 68 69 if (rcdev->of_node) 70 return rcdev->of_node->full_name; 71 72 return NULL; 73 } 74 75 /** 76 * of_reset_simple_xlate - translate reset_spec to the reset line number 77 * @rcdev: a pointer to the reset controller device 78 * @reset_spec: reset line specifier as found in the device tree 79 * @flags: a flags pointer to fill in (optional) 80 * 81 * This simple translation function should be used for reset controllers 82 * with 1:1 mapping, where reset lines can be indexed by number without gaps. 83 */ 84 static int of_reset_simple_xlate(struct reset_controller_dev *rcdev, 85 const struct of_phandle_args *reset_spec) 86 { 87 if (reset_spec->args[0] >= rcdev->nr_resets) 88 return -EINVAL; 89 90 return reset_spec->args[0]; 91 } 92 93 /** 94 * reset_controller_register - register a reset controller device 95 * @rcdev: a pointer to the initialized reset controller device 96 */ 97 int reset_controller_register(struct reset_controller_dev *rcdev) 98 { 99 if (!rcdev->of_xlate) { 100 rcdev->of_reset_n_cells = 1; 101 rcdev->of_xlate = of_reset_simple_xlate; 102 } 103 104 INIT_LIST_HEAD(&rcdev->reset_control_head); 105 106 mutex_lock(&reset_list_mutex); 107 list_add(&rcdev->list, &reset_controller_list); 108 mutex_unlock(&reset_list_mutex); 109 110 return 0; 111 } 112 EXPORT_SYMBOL_GPL(reset_controller_register); 113 114 /** 115 * reset_controller_unregister - unregister a reset controller device 116 * @rcdev: a pointer to the reset controller device 117 */ 118 void reset_controller_unregister(struct reset_controller_dev *rcdev) 119 { 120 mutex_lock(&reset_list_mutex); 121 list_del(&rcdev->list); 122 mutex_unlock(&reset_list_mutex); 123 } 124 EXPORT_SYMBOL_GPL(reset_controller_unregister); 125 126 static void devm_reset_controller_release(struct device *dev, void *res) 127 { 128 reset_controller_unregister(*(struct reset_controller_dev **)res); 129 } 130 131 /** 132 * devm_reset_controller_register - resource managed reset_controller_register() 133 * @dev: device that is registering this reset controller 134 * @rcdev: a pointer to the initialized reset controller device 135 * 136 * Managed reset_controller_register(). For reset controllers registered by 137 * this function, reset_controller_unregister() is automatically called on 138 * driver detach. See reset_controller_register() for more information. 139 */ 140 int devm_reset_controller_register(struct device *dev, 141 struct reset_controller_dev *rcdev) 142 { 143 struct reset_controller_dev **rcdevp; 144 int ret; 145 146 rcdevp = devres_alloc(devm_reset_controller_release, sizeof(*rcdevp), 147 GFP_KERNEL); 148 if (!rcdevp) 149 return -ENOMEM; 150 151 ret = reset_controller_register(rcdev); 152 if (!ret) { 153 *rcdevp = rcdev; 154 devres_add(dev, rcdevp); 155 } else { 156 devres_free(rcdevp); 157 } 158 159 return ret; 160 } 161 EXPORT_SYMBOL_GPL(devm_reset_controller_register); 162 163 /** 164 * reset_controller_add_lookup - register a set of lookup entries 165 * @lookup: array of reset lookup entries 166 * @num_entries: number of entries in the lookup array 167 */ 168 void reset_controller_add_lookup(struct reset_control_lookup *lookup, 169 unsigned int num_entries) 170 { 171 struct reset_control_lookup *entry; 172 unsigned int i; 173 174 mutex_lock(&reset_lookup_mutex); 175 for (i = 0; i < num_entries; i++) { 176 entry = &lookup[i]; 177 178 if (!entry->dev_id || !entry->provider) { 179 pr_warn("%s(): reset lookup entry badly specified, skipping\n", 180 __func__); 181 continue; 182 } 183 184 list_add_tail(&entry->list, &reset_lookup_list); 185 } 186 mutex_unlock(&reset_lookup_mutex); 187 } 188 EXPORT_SYMBOL_GPL(reset_controller_add_lookup); 189 190 static inline struct reset_control_array * 191 rstc_to_array(struct reset_control *rstc) { 192 return container_of(rstc, struct reset_control_array, base); 193 } 194 195 static int reset_control_array_reset(struct reset_control_array *resets) 196 { 197 int ret, i; 198 199 for (i = 0; i < resets->num_rstcs; i++) { 200 ret = reset_control_reset(resets->rstc[i]); 201 if (ret) 202 return ret; 203 } 204 205 return 0; 206 } 207 208 static int reset_control_array_assert(struct reset_control_array *resets) 209 { 210 int ret, i; 211 212 for (i = 0; i < resets->num_rstcs; i++) { 213 ret = reset_control_assert(resets->rstc[i]); 214 if (ret) 215 goto err; 216 } 217 218 return 0; 219 220 err: 221 while (i--) 222 reset_control_deassert(resets->rstc[i]); 223 return ret; 224 } 225 226 static int reset_control_array_deassert(struct reset_control_array *resets) 227 { 228 int ret, i; 229 230 for (i = 0; i < resets->num_rstcs; i++) { 231 ret = reset_control_deassert(resets->rstc[i]); 232 if (ret) 233 goto err; 234 } 235 236 return 0; 237 238 err: 239 while (i--) 240 reset_control_assert(resets->rstc[i]); 241 return ret; 242 } 243 244 static int reset_control_array_acquire(struct reset_control_array *resets) 245 { 246 unsigned int i; 247 int err; 248 249 for (i = 0; i < resets->num_rstcs; i++) { 250 err = reset_control_acquire(resets->rstc[i]); 251 if (err < 0) 252 goto release; 253 } 254 255 return 0; 256 257 release: 258 while (i--) 259 reset_control_release(resets->rstc[i]); 260 261 return err; 262 } 263 264 static void reset_control_array_release(struct reset_control_array *resets) 265 { 266 unsigned int i; 267 268 for (i = 0; i < resets->num_rstcs; i++) 269 reset_control_release(resets->rstc[i]); 270 } 271 272 static inline bool reset_control_is_array(struct reset_control *rstc) 273 { 274 return rstc->array; 275 } 276 277 /** 278 * reset_control_reset - reset the controlled device 279 * @rstc: reset controller 280 * 281 * On a shared reset line the actual reset pulse is only triggered once for the 282 * lifetime of the reset_control instance: for all but the first caller this is 283 * a no-op. 284 * Consumers must not use reset_control_(de)assert on shared reset lines when 285 * reset_control_reset has been used. 286 * 287 * If rstc is NULL it is an optional reset and the function will just 288 * return 0. 289 */ 290 int reset_control_reset(struct reset_control *rstc) 291 { 292 int ret; 293 294 if (!rstc) 295 return 0; 296 297 if (WARN_ON(IS_ERR(rstc))) 298 return -EINVAL; 299 300 if (reset_control_is_array(rstc)) 301 return reset_control_array_reset(rstc_to_array(rstc)); 302 303 if (!rstc->rcdev->ops->reset) 304 return -ENOTSUPP; 305 306 if (rstc->shared) { 307 if (WARN_ON(atomic_read(&rstc->deassert_count) != 0)) 308 return -EINVAL; 309 310 if (atomic_inc_return(&rstc->triggered_count) != 1) 311 return 0; 312 } else { 313 if (!rstc->acquired) 314 return -EPERM; 315 } 316 317 ret = rstc->rcdev->ops->reset(rstc->rcdev, rstc->id); 318 if (rstc->shared && ret) 319 atomic_dec(&rstc->triggered_count); 320 321 return ret; 322 } 323 EXPORT_SYMBOL_GPL(reset_control_reset); 324 325 /** 326 * reset_control_assert - asserts the reset line 327 * @rstc: reset controller 328 * 329 * Calling this on an exclusive reset controller guarantees that the reset 330 * will be asserted. When called on a shared reset controller the line may 331 * still be deasserted, as long as other users keep it so. 332 * 333 * For shared reset controls a driver cannot expect the hw's registers and 334 * internal state to be reset, but must be prepared for this to happen. 335 * Consumers must not use reset_control_reset on shared reset lines when 336 * reset_control_(de)assert has been used. 337 * 338 * If rstc is NULL it is an optional reset and the function will just 339 * return 0. 340 */ 341 int reset_control_assert(struct reset_control *rstc) 342 { 343 if (!rstc) 344 return 0; 345 346 if (WARN_ON(IS_ERR(rstc))) 347 return -EINVAL; 348 349 if (reset_control_is_array(rstc)) 350 return reset_control_array_assert(rstc_to_array(rstc)); 351 352 if (rstc->shared) { 353 if (WARN_ON(atomic_read(&rstc->triggered_count) != 0)) 354 return -EINVAL; 355 356 if (WARN_ON(atomic_read(&rstc->deassert_count) == 0)) 357 return -EINVAL; 358 359 if (atomic_dec_return(&rstc->deassert_count) != 0) 360 return 0; 361 362 /* 363 * Shared reset controls allow the reset line to be in any state 364 * after this call, so doing nothing is a valid option. 365 */ 366 if (!rstc->rcdev->ops->assert) 367 return 0; 368 } else { 369 /* 370 * If the reset controller does not implement .assert(), there 371 * is no way to guarantee that the reset line is asserted after 372 * this call. 373 */ 374 if (!rstc->rcdev->ops->assert) 375 return -ENOTSUPP; 376 377 if (!rstc->acquired) { 378 WARN(1, "reset %s (ID: %u) is not acquired\n", 379 rcdev_name(rstc->rcdev), rstc->id); 380 return -EPERM; 381 } 382 } 383 384 return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id); 385 } 386 EXPORT_SYMBOL_GPL(reset_control_assert); 387 388 /** 389 * reset_control_deassert - deasserts the reset line 390 * @rstc: reset controller 391 * 392 * After calling this function, the reset is guaranteed to be deasserted. 393 * Consumers must not use reset_control_reset on shared reset lines when 394 * reset_control_(de)assert has been used. 395 * 396 * If rstc is NULL it is an optional reset and the function will just 397 * return 0. 398 */ 399 int reset_control_deassert(struct reset_control *rstc) 400 { 401 if (!rstc) 402 return 0; 403 404 if (WARN_ON(IS_ERR(rstc))) 405 return -EINVAL; 406 407 if (reset_control_is_array(rstc)) 408 return reset_control_array_deassert(rstc_to_array(rstc)); 409 410 if (rstc->shared) { 411 if (WARN_ON(atomic_read(&rstc->triggered_count) != 0)) 412 return -EINVAL; 413 414 if (atomic_inc_return(&rstc->deassert_count) != 1) 415 return 0; 416 } else { 417 if (!rstc->acquired) { 418 WARN(1, "reset %s (ID: %u) is not acquired\n", 419 rcdev_name(rstc->rcdev), rstc->id); 420 return -EPERM; 421 } 422 } 423 424 /* 425 * If the reset controller does not implement .deassert(), we assume 426 * that it handles self-deasserting reset lines via .reset(). In that 427 * case, the reset lines are deasserted by default. If that is not the 428 * case, the reset controller driver should implement .deassert() and 429 * return -ENOTSUPP. 430 */ 431 if (!rstc->rcdev->ops->deassert) 432 return 0; 433 434 return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id); 435 } 436 EXPORT_SYMBOL_GPL(reset_control_deassert); 437 438 /** 439 * reset_control_status - returns a negative errno if not supported, a 440 * positive value if the reset line is asserted, or zero if the reset 441 * line is not asserted or if the desc is NULL (optional reset). 442 * @rstc: reset controller 443 */ 444 int reset_control_status(struct reset_control *rstc) 445 { 446 if (!rstc) 447 return 0; 448 449 if (WARN_ON(IS_ERR(rstc)) || reset_control_is_array(rstc)) 450 return -EINVAL; 451 452 if (rstc->rcdev->ops->status) 453 return rstc->rcdev->ops->status(rstc->rcdev, rstc->id); 454 455 return -ENOTSUPP; 456 } 457 EXPORT_SYMBOL_GPL(reset_control_status); 458 459 /** 460 * reset_control_acquire() - acquires a reset control for exclusive use 461 * @rstc: reset control 462 * 463 * This is used to explicitly acquire a reset control for exclusive use. Note 464 * that exclusive resets are requested as acquired by default. In order for a 465 * second consumer to be able to control the reset, the first consumer has to 466 * release it first. Typically the easiest way to achieve this is to call the 467 * reset_control_get_exclusive_released() to obtain an instance of the reset 468 * control. Such reset controls are not acquired by default. 469 * 470 * Consumers implementing shared access to an exclusive reset need to follow 471 * a specific protocol in order to work together. Before consumers can change 472 * a reset they must acquire exclusive access using reset_control_acquire(). 473 * After they are done operating the reset, they must release exclusive access 474 * with a call to reset_control_release(). Consumers are not granted exclusive 475 * access to the reset as long as another consumer hasn't released a reset. 476 * 477 * See also: reset_control_release() 478 */ 479 int reset_control_acquire(struct reset_control *rstc) 480 { 481 struct reset_control *rc; 482 483 if (!rstc) 484 return 0; 485 486 if (WARN_ON(IS_ERR(rstc))) 487 return -EINVAL; 488 489 if (reset_control_is_array(rstc)) 490 return reset_control_array_acquire(rstc_to_array(rstc)); 491 492 mutex_lock(&reset_list_mutex); 493 494 if (rstc->acquired) { 495 mutex_unlock(&reset_list_mutex); 496 return 0; 497 } 498 499 list_for_each_entry(rc, &rstc->rcdev->reset_control_head, list) { 500 if (rstc != rc && rstc->id == rc->id) { 501 if (rc->acquired) { 502 mutex_unlock(&reset_list_mutex); 503 return -EBUSY; 504 } 505 } 506 } 507 508 rstc->acquired = true; 509 510 mutex_unlock(&reset_list_mutex); 511 return 0; 512 } 513 EXPORT_SYMBOL_GPL(reset_control_acquire); 514 515 /** 516 * reset_control_release() - releases exclusive access to a reset control 517 * @rstc: reset control 518 * 519 * Releases exclusive access right to a reset control previously obtained by a 520 * call to reset_control_acquire(). Until a consumer calls this function, no 521 * other consumers will be granted exclusive access. 522 * 523 * See also: reset_control_acquire() 524 */ 525 void reset_control_release(struct reset_control *rstc) 526 { 527 if (!rstc || WARN_ON(IS_ERR(rstc))) 528 return; 529 530 if (reset_control_is_array(rstc)) 531 reset_control_array_release(rstc_to_array(rstc)); 532 else 533 rstc->acquired = false; 534 } 535 EXPORT_SYMBOL_GPL(reset_control_release); 536 537 static struct reset_control *__reset_control_get_internal( 538 struct reset_controller_dev *rcdev, 539 unsigned int index, bool shared, bool acquired) 540 { 541 struct reset_control *rstc; 542 543 lockdep_assert_held(&reset_list_mutex); 544 545 list_for_each_entry(rstc, &rcdev->reset_control_head, list) { 546 if (rstc->id == index) { 547 /* 548 * Allow creating a secondary exclusive reset_control 549 * that is initially not acquired for an already 550 * controlled reset line. 551 */ 552 if (!rstc->shared && !shared && !acquired) 553 break; 554 555 if (WARN_ON(!rstc->shared || !shared)) 556 return ERR_PTR(-EBUSY); 557 558 kref_get(&rstc->refcnt); 559 return rstc; 560 } 561 } 562 563 rstc = kzalloc(sizeof(*rstc), GFP_KERNEL); 564 if (!rstc) 565 return ERR_PTR(-ENOMEM); 566 567 try_module_get(rcdev->owner); 568 569 rstc->rcdev = rcdev; 570 list_add(&rstc->list, &rcdev->reset_control_head); 571 rstc->id = index; 572 kref_init(&rstc->refcnt); 573 rstc->acquired = acquired; 574 rstc->shared = shared; 575 576 return rstc; 577 } 578 579 static void __reset_control_release(struct kref *kref) 580 { 581 struct reset_control *rstc = container_of(kref, struct reset_control, 582 refcnt); 583 584 lockdep_assert_held(&reset_list_mutex); 585 586 module_put(rstc->rcdev->owner); 587 588 list_del(&rstc->list); 589 kfree(rstc); 590 } 591 592 static void __reset_control_put_internal(struct reset_control *rstc) 593 { 594 lockdep_assert_held(&reset_list_mutex); 595 596 kref_put(&rstc->refcnt, __reset_control_release); 597 } 598 599 struct reset_control *__of_reset_control_get(struct device_node *node, 600 const char *id, int index, bool shared, 601 bool optional, bool acquired) 602 { 603 struct reset_control *rstc; 604 struct reset_controller_dev *r, *rcdev; 605 struct of_phandle_args args; 606 int rstc_id; 607 int ret; 608 609 if (!node) 610 return ERR_PTR(-EINVAL); 611 612 if (id) { 613 index = of_property_match_string(node, 614 "reset-names", id); 615 if (index == -EILSEQ) 616 return ERR_PTR(index); 617 if (index < 0) 618 return optional ? NULL : ERR_PTR(-ENOENT); 619 } 620 621 ret = of_parse_phandle_with_args(node, "resets", "#reset-cells", 622 index, &args); 623 if (ret == -EINVAL) 624 return ERR_PTR(ret); 625 if (ret) 626 return optional ? NULL : ERR_PTR(ret); 627 628 mutex_lock(&reset_list_mutex); 629 rcdev = NULL; 630 list_for_each_entry(r, &reset_controller_list, list) { 631 if (args.np == r->of_node) { 632 rcdev = r; 633 break; 634 } 635 } 636 637 if (!rcdev) { 638 rstc = ERR_PTR(-EPROBE_DEFER); 639 goto out; 640 } 641 642 if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) { 643 rstc = ERR_PTR(-EINVAL); 644 goto out; 645 } 646 647 rstc_id = rcdev->of_xlate(rcdev, &args); 648 if (rstc_id < 0) { 649 rstc = ERR_PTR(rstc_id); 650 goto out; 651 } 652 653 /* reset_list_mutex also protects the rcdev's reset_control list */ 654 rstc = __reset_control_get_internal(rcdev, rstc_id, shared, acquired); 655 656 out: 657 mutex_unlock(&reset_list_mutex); 658 of_node_put(args.np); 659 660 return rstc; 661 } 662 EXPORT_SYMBOL_GPL(__of_reset_control_get); 663 664 static struct reset_controller_dev * 665 __reset_controller_by_name(const char *name) 666 { 667 struct reset_controller_dev *rcdev; 668 669 lockdep_assert_held(&reset_list_mutex); 670 671 list_for_each_entry(rcdev, &reset_controller_list, list) { 672 if (!rcdev->dev) 673 continue; 674 675 if (!strcmp(name, dev_name(rcdev->dev))) 676 return rcdev; 677 } 678 679 return NULL; 680 } 681 682 static struct reset_control * 683 __reset_control_get_from_lookup(struct device *dev, const char *con_id, 684 bool shared, bool optional, bool acquired) 685 { 686 const struct reset_control_lookup *lookup; 687 struct reset_controller_dev *rcdev; 688 const char *dev_id = dev_name(dev); 689 struct reset_control *rstc = NULL; 690 691 mutex_lock(&reset_lookup_mutex); 692 693 list_for_each_entry(lookup, &reset_lookup_list, list) { 694 if (strcmp(lookup->dev_id, dev_id)) 695 continue; 696 697 if ((!con_id && !lookup->con_id) || 698 ((con_id && lookup->con_id) && 699 !strcmp(con_id, lookup->con_id))) { 700 mutex_lock(&reset_list_mutex); 701 rcdev = __reset_controller_by_name(lookup->provider); 702 if (!rcdev) { 703 mutex_unlock(&reset_list_mutex); 704 mutex_unlock(&reset_lookup_mutex); 705 /* Reset provider may not be ready yet. */ 706 return ERR_PTR(-EPROBE_DEFER); 707 } 708 709 rstc = __reset_control_get_internal(rcdev, 710 lookup->index, 711 shared, acquired); 712 mutex_unlock(&reset_list_mutex); 713 break; 714 } 715 } 716 717 mutex_unlock(&reset_lookup_mutex); 718 719 if (!rstc) 720 return optional ? NULL : ERR_PTR(-ENOENT); 721 722 return rstc; 723 } 724 725 struct reset_control *__reset_control_get(struct device *dev, const char *id, 726 int index, bool shared, bool optional, 727 bool acquired) 728 { 729 if (WARN_ON(shared && acquired)) 730 return ERR_PTR(-EINVAL); 731 732 if (dev->of_node) 733 return __of_reset_control_get(dev->of_node, id, index, shared, 734 optional, acquired); 735 736 return __reset_control_get_from_lookup(dev, id, shared, optional, 737 acquired); 738 } 739 EXPORT_SYMBOL_GPL(__reset_control_get); 740 741 static void reset_control_array_put(struct reset_control_array *resets) 742 { 743 int i; 744 745 mutex_lock(&reset_list_mutex); 746 for (i = 0; i < resets->num_rstcs; i++) 747 __reset_control_put_internal(resets->rstc[i]); 748 mutex_unlock(&reset_list_mutex); 749 } 750 751 /** 752 * reset_control_put - free the reset controller 753 * @rstc: reset controller 754 */ 755 void reset_control_put(struct reset_control *rstc) 756 { 757 if (IS_ERR_OR_NULL(rstc)) 758 return; 759 760 if (reset_control_is_array(rstc)) { 761 reset_control_array_put(rstc_to_array(rstc)); 762 return; 763 } 764 765 mutex_lock(&reset_list_mutex); 766 __reset_control_put_internal(rstc); 767 mutex_unlock(&reset_list_mutex); 768 } 769 EXPORT_SYMBOL_GPL(reset_control_put); 770 771 static void devm_reset_control_release(struct device *dev, void *res) 772 { 773 reset_control_put(*(struct reset_control **)res); 774 } 775 776 struct reset_control *__devm_reset_control_get(struct device *dev, 777 const char *id, int index, bool shared, 778 bool optional, bool acquired) 779 { 780 struct reset_control **ptr, *rstc; 781 782 ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr), 783 GFP_KERNEL); 784 if (!ptr) 785 return ERR_PTR(-ENOMEM); 786 787 rstc = __reset_control_get(dev, id, index, shared, optional, acquired); 788 if (!IS_ERR(rstc)) { 789 *ptr = rstc; 790 devres_add(dev, ptr); 791 } else { 792 devres_free(ptr); 793 } 794 795 return rstc; 796 } 797 EXPORT_SYMBOL_GPL(__devm_reset_control_get); 798 799 /** 800 * device_reset - find reset controller associated with the device 801 * and perform reset 802 * @dev: device to be reset by the controller 803 * @optional: whether it is optional to reset the device 804 * 805 * Convenience wrapper for __reset_control_get() and reset_control_reset(). 806 * This is useful for the common case of devices with single, dedicated reset 807 * lines. 808 */ 809 int __device_reset(struct device *dev, bool optional) 810 { 811 struct reset_control *rstc; 812 int ret; 813 814 rstc = __reset_control_get(dev, NULL, 0, 0, optional, true); 815 if (IS_ERR(rstc)) 816 return PTR_ERR(rstc); 817 818 ret = reset_control_reset(rstc); 819 820 reset_control_put(rstc); 821 822 return ret; 823 } 824 EXPORT_SYMBOL_GPL(__device_reset); 825 826 /** 827 * APIs to manage an array of reset controls. 828 */ 829 /** 830 * of_reset_control_get_count - Count number of resets available with a device 831 * 832 * @node: device node that contains 'resets'. 833 * 834 * Returns positive reset count on success, or error number on failure and 835 * on count being zero. 836 */ 837 static int of_reset_control_get_count(struct device_node *node) 838 { 839 int count; 840 841 if (!node) 842 return -EINVAL; 843 844 count = of_count_phandle_with_args(node, "resets", "#reset-cells"); 845 if (count == 0) 846 count = -ENOENT; 847 848 return count; 849 } 850 851 /** 852 * of_reset_control_array_get - Get a list of reset controls using 853 * device node. 854 * 855 * @np: device node for the device that requests the reset controls array 856 * @shared: whether reset controls are shared or not 857 * @optional: whether it is optional to get the reset controls 858 * @acquired: only one reset control may be acquired for a given controller 859 * and ID 860 * 861 * Returns pointer to allocated reset_control_array on success or 862 * error on failure 863 */ 864 struct reset_control * 865 of_reset_control_array_get(struct device_node *np, bool shared, bool optional, 866 bool acquired) 867 { 868 struct reset_control_array *resets; 869 struct reset_control *rstc; 870 int num, i; 871 872 num = of_reset_control_get_count(np); 873 if (num < 0) 874 return optional ? NULL : ERR_PTR(num); 875 876 resets = kzalloc(struct_size(resets, rstc, num), GFP_KERNEL); 877 if (!resets) 878 return ERR_PTR(-ENOMEM); 879 880 for (i = 0; i < num; i++) { 881 rstc = __of_reset_control_get(np, NULL, i, shared, optional, 882 acquired); 883 if (IS_ERR(rstc)) 884 goto err_rst; 885 resets->rstc[i] = rstc; 886 } 887 resets->num_rstcs = num; 888 resets->base.array = true; 889 890 return &resets->base; 891 892 err_rst: 893 mutex_lock(&reset_list_mutex); 894 while (--i >= 0) 895 __reset_control_put_internal(resets->rstc[i]); 896 mutex_unlock(&reset_list_mutex); 897 898 kfree(resets); 899 900 return rstc; 901 } 902 EXPORT_SYMBOL_GPL(of_reset_control_array_get); 903 904 /** 905 * devm_reset_control_array_get - Resource managed reset control array get 906 * 907 * @dev: device that requests the list of reset controls 908 * @shared: whether reset controls are shared or not 909 * @optional: whether it is optional to get the reset controls 910 * 911 * The reset control array APIs are intended for a list of resets 912 * that just have to be asserted or deasserted, without any 913 * requirements on the order. 914 * 915 * Returns pointer to allocated reset_control_array on success or 916 * error on failure 917 */ 918 struct reset_control * 919 devm_reset_control_array_get(struct device *dev, bool shared, bool optional) 920 { 921 struct reset_control **devres; 922 struct reset_control *rstc; 923 924 devres = devres_alloc(devm_reset_control_release, sizeof(*devres), 925 GFP_KERNEL); 926 if (!devres) 927 return ERR_PTR(-ENOMEM); 928 929 rstc = of_reset_control_array_get(dev->of_node, shared, optional, true); 930 if (IS_ERR(rstc)) { 931 devres_free(devres); 932 return rstc; 933 } 934 935 *devres = rstc; 936 devres_add(dev, devres); 937 938 return rstc; 939 } 940 EXPORT_SYMBOL_GPL(devm_reset_control_array_get); 941 942 static int reset_control_get_count_from_lookup(struct device *dev) 943 { 944 const struct reset_control_lookup *lookup; 945 const char *dev_id; 946 int count = 0; 947 948 if (!dev) 949 return -EINVAL; 950 951 dev_id = dev_name(dev); 952 mutex_lock(&reset_lookup_mutex); 953 954 list_for_each_entry(lookup, &reset_lookup_list, list) { 955 if (!strcmp(lookup->dev_id, dev_id)) 956 count++; 957 } 958 959 mutex_unlock(&reset_lookup_mutex); 960 961 if (count == 0) 962 count = -ENOENT; 963 964 return count; 965 } 966 967 /** 968 * reset_control_get_count - Count number of resets available with a device 969 * 970 * @dev: device for which to return the number of resets 971 * 972 * Returns positive reset count on success, or error number on failure and 973 * on count being zero. 974 */ 975 int reset_control_get_count(struct device *dev) 976 { 977 if (dev->of_node) 978 return of_reset_control_get_count(dev->of_node); 979 980 return reset_control_get_count_from_lookup(dev); 981 } 982 EXPORT_SYMBOL_GPL(reset_control_get_count); 983