1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Reset Controller framework 4 * 5 * Copyright 2013 Philipp Zabel, Pengutronix 6 */ 7 8 #include <linux/acpi.h> 9 #include <linux/atomic.h> 10 #include <linux/auxiliary_bus.h> 11 #include <linux/cleanup.h> 12 #include <linux/device.h> 13 #include <linux/err.h> 14 #include <linux/export.h> 15 #include <linux/gpio/driver.h> 16 #include <linux/gpio/machine.h> 17 #include <linux/gpio/property.h> 18 #include <linux/idr.h> 19 #include <linux/kernel.h> 20 #include <linux/kref.h> 21 #include <linux/module.h> 22 #include <linux/of.h> 23 #include <linux/reset.h> 24 #include <linux/reset-controller.h> 25 #include <linux/slab.h> 26 27 static DEFINE_MUTEX(reset_list_mutex); 28 static LIST_HEAD(reset_controller_list); 29 30 static DEFINE_MUTEX(reset_lookup_mutex); 31 static LIST_HEAD(reset_lookup_list); 32 33 /* Protects reset_gpio_lookup_list */ 34 static DEFINE_MUTEX(reset_gpio_lookup_mutex); 35 static LIST_HEAD(reset_gpio_lookup_list); 36 static DEFINE_IDA(reset_gpio_ida); 37 38 /** 39 * struct reset_control - a reset control 40 * @rcdev: a pointer to the reset controller device 41 * this reset control belongs to 42 * @list: list entry for the rcdev's reset controller list 43 * @id: ID of the reset controller in the reset 44 * controller device 45 * @refcnt: Number of gets of this reset_control 46 * @acquired: Only one reset_control may be acquired for a given rcdev and id. 47 * @shared: Is this a shared (1), or an exclusive (0) reset_control? 48 * @array: Is this an array of reset controls (1)? 49 * @deassert_count: Number of times this reset line has been deasserted 50 * @triggered_count: Number of times this reset line has been reset. Currently 51 * only used for shared resets, which means that the value 52 * will be either 0 or 1. 53 */ 54 struct reset_control { 55 struct reset_controller_dev *rcdev; 56 struct list_head list; 57 unsigned int id; 58 struct kref refcnt; 59 bool acquired; 60 bool shared; 61 bool array; 62 atomic_t deassert_count; 63 atomic_t triggered_count; 64 }; 65 66 /** 67 * struct reset_control_array - an array of reset controls 68 * @base: reset control for compatibility with reset control API functions 69 * @num_rstcs: number of reset controls 70 * @rstc: array of reset controls 71 */ 72 struct reset_control_array { 73 struct reset_control base; 74 unsigned int num_rstcs; 75 struct reset_control *rstc[] __counted_by(num_rstcs); 76 }; 77 78 /** 79 * struct reset_gpio_lookup - lookup key for ad-hoc created reset-gpio devices 80 * @of_args: phandle to the reset controller with all the args like GPIO number 81 * @swnode: Software node containing the reference to the GPIO provider 82 * @list: list entry for the reset_gpio_lookup_list 83 */ 84 struct reset_gpio_lookup { 85 struct of_phandle_args of_args; 86 struct fwnode_handle *swnode; 87 struct list_head list; 88 }; 89 90 static const char *rcdev_name(struct reset_controller_dev *rcdev) 91 { 92 if (rcdev->dev) 93 return dev_name(rcdev->dev); 94 95 if (rcdev->of_node) 96 return rcdev->of_node->full_name; 97 98 if (rcdev->of_args) 99 return rcdev->of_args->np->full_name; 100 101 return NULL; 102 } 103 104 /** 105 * of_reset_simple_xlate - translate reset_spec to the reset line number 106 * @rcdev: a pointer to the reset controller device 107 * @reset_spec: reset line specifier as found in the device tree 108 * 109 * This static translation function is used by default if of_xlate in 110 * :c:type:`reset_controller_dev` is not set. It is useful for all reset 111 * controllers with 1:1 mapping, where reset lines can be indexed by number 112 * without gaps. 113 */ 114 static int of_reset_simple_xlate(struct reset_controller_dev *rcdev, 115 const struct of_phandle_args *reset_spec) 116 { 117 if (reset_spec->args[0] >= rcdev->nr_resets) 118 return -EINVAL; 119 120 return reset_spec->args[0]; 121 } 122 123 /** 124 * reset_controller_register - register a reset controller device 125 * @rcdev: a pointer to the initialized reset controller device 126 */ 127 int reset_controller_register(struct reset_controller_dev *rcdev) 128 { 129 if (rcdev->of_node && rcdev->of_args) 130 return -EINVAL; 131 132 if (!rcdev->of_xlate) { 133 rcdev->of_reset_n_cells = 1; 134 rcdev->of_xlate = of_reset_simple_xlate; 135 } 136 137 INIT_LIST_HEAD(&rcdev->reset_control_head); 138 139 mutex_lock(&reset_list_mutex); 140 list_add(&rcdev->list, &reset_controller_list); 141 mutex_unlock(&reset_list_mutex); 142 143 return 0; 144 } 145 EXPORT_SYMBOL_GPL(reset_controller_register); 146 147 /** 148 * reset_controller_unregister - unregister a reset controller device 149 * @rcdev: a pointer to the reset controller device 150 */ 151 void reset_controller_unregister(struct reset_controller_dev *rcdev) 152 { 153 mutex_lock(&reset_list_mutex); 154 list_del(&rcdev->list); 155 mutex_unlock(&reset_list_mutex); 156 } 157 EXPORT_SYMBOL_GPL(reset_controller_unregister); 158 159 static void devm_reset_controller_release(struct device *dev, void *res) 160 { 161 reset_controller_unregister(*(struct reset_controller_dev **)res); 162 } 163 164 /** 165 * devm_reset_controller_register - resource managed reset_controller_register() 166 * @dev: device that is registering this reset controller 167 * @rcdev: a pointer to the initialized reset controller device 168 * 169 * Managed reset_controller_register(). For reset controllers registered by 170 * this function, reset_controller_unregister() is automatically called on 171 * driver detach. See reset_controller_register() for more information. 172 */ 173 int devm_reset_controller_register(struct device *dev, 174 struct reset_controller_dev *rcdev) 175 { 176 struct reset_controller_dev **rcdevp; 177 int ret; 178 179 rcdevp = devres_alloc(devm_reset_controller_release, sizeof(*rcdevp), 180 GFP_KERNEL); 181 if (!rcdevp) 182 return -ENOMEM; 183 184 ret = reset_controller_register(rcdev); 185 if (ret) { 186 devres_free(rcdevp); 187 return ret; 188 } 189 190 *rcdevp = rcdev; 191 devres_add(dev, rcdevp); 192 193 return ret; 194 } 195 EXPORT_SYMBOL_GPL(devm_reset_controller_register); 196 197 /** 198 * reset_controller_add_lookup - register a set of lookup entries 199 * @lookup: array of reset lookup entries 200 * @num_entries: number of entries in the lookup array 201 */ 202 void reset_controller_add_lookup(struct reset_control_lookup *lookup, 203 unsigned int num_entries) 204 { 205 struct reset_control_lookup *entry; 206 unsigned int i; 207 208 mutex_lock(&reset_lookup_mutex); 209 for (i = 0; i < num_entries; i++) { 210 entry = &lookup[i]; 211 212 if (!entry->dev_id || !entry->provider) { 213 pr_warn("%s(): reset lookup entry badly specified, skipping\n", 214 __func__); 215 continue; 216 } 217 218 list_add_tail(&entry->list, &reset_lookup_list); 219 } 220 mutex_unlock(&reset_lookup_mutex); 221 } 222 EXPORT_SYMBOL_GPL(reset_controller_add_lookup); 223 224 static inline struct reset_control_array * 225 rstc_to_array(struct reset_control *rstc) { 226 return container_of(rstc, struct reset_control_array, base); 227 } 228 229 static int reset_control_array_reset(struct reset_control_array *resets) 230 { 231 int ret, i; 232 233 for (i = 0; i < resets->num_rstcs; i++) { 234 ret = reset_control_reset(resets->rstc[i]); 235 if (ret) 236 return ret; 237 } 238 239 return 0; 240 } 241 242 static int reset_control_array_rearm(struct reset_control_array *resets) 243 { 244 struct reset_control *rstc; 245 int i; 246 247 for (i = 0; i < resets->num_rstcs; i++) { 248 rstc = resets->rstc[i]; 249 250 if (!rstc) 251 continue; 252 253 if (WARN_ON(IS_ERR(rstc))) 254 return -EINVAL; 255 256 if (rstc->shared) { 257 if (WARN_ON(atomic_read(&rstc->deassert_count) != 0)) 258 return -EINVAL; 259 } else { 260 if (!rstc->acquired) 261 return -EPERM; 262 } 263 } 264 265 for (i = 0; i < resets->num_rstcs; i++) { 266 rstc = resets->rstc[i]; 267 268 if (rstc && rstc->shared) 269 WARN_ON(atomic_dec_return(&rstc->triggered_count) < 0); 270 } 271 272 return 0; 273 } 274 275 static int reset_control_array_assert(struct reset_control_array *resets) 276 { 277 int ret, i; 278 279 for (i = 0; i < resets->num_rstcs; i++) { 280 ret = reset_control_assert(resets->rstc[i]); 281 if (ret) 282 goto err; 283 } 284 285 return 0; 286 287 err: 288 while (i--) 289 reset_control_deassert(resets->rstc[i]); 290 return ret; 291 } 292 293 static int reset_control_array_deassert(struct reset_control_array *resets) 294 { 295 int ret, i; 296 297 for (i = 0; i < resets->num_rstcs; i++) { 298 ret = reset_control_deassert(resets->rstc[i]); 299 if (ret) 300 goto err; 301 } 302 303 return 0; 304 305 err: 306 while (i--) 307 reset_control_assert(resets->rstc[i]); 308 return ret; 309 } 310 311 static int reset_control_array_acquire(struct reset_control_array *resets) 312 { 313 unsigned int i; 314 int err; 315 316 for (i = 0; i < resets->num_rstcs; i++) { 317 err = reset_control_acquire(resets->rstc[i]); 318 if (err < 0) 319 goto release; 320 } 321 322 return 0; 323 324 release: 325 while (i--) 326 reset_control_release(resets->rstc[i]); 327 328 return err; 329 } 330 331 static void reset_control_array_release(struct reset_control_array *resets) 332 { 333 unsigned int i; 334 335 for (i = 0; i < resets->num_rstcs; i++) 336 reset_control_release(resets->rstc[i]); 337 } 338 339 static inline bool reset_control_is_array(struct reset_control *rstc) 340 { 341 return rstc->array; 342 } 343 344 /** 345 * reset_control_reset - reset the controlled device 346 * @rstc: reset controller 347 * 348 * On a shared reset line the actual reset pulse is only triggered once for the 349 * lifetime of the reset_control instance: for all but the first caller this is 350 * a no-op. 351 * Consumers must not use reset_control_(de)assert on shared reset lines when 352 * reset_control_reset has been used. 353 * 354 * If rstc is NULL it is an optional reset and the function will just 355 * return 0. 356 */ 357 int reset_control_reset(struct reset_control *rstc) 358 { 359 int ret; 360 361 if (!rstc) 362 return 0; 363 364 if (WARN_ON(IS_ERR(rstc))) 365 return -EINVAL; 366 367 if (reset_control_is_array(rstc)) 368 return reset_control_array_reset(rstc_to_array(rstc)); 369 370 if (!rstc->rcdev->ops->reset) 371 return -ENOTSUPP; 372 373 if (rstc->shared) { 374 if (WARN_ON(atomic_read(&rstc->deassert_count) != 0)) 375 return -EINVAL; 376 377 if (atomic_inc_return(&rstc->triggered_count) != 1) 378 return 0; 379 } else { 380 if (!rstc->acquired) 381 return -EPERM; 382 } 383 384 ret = rstc->rcdev->ops->reset(rstc->rcdev, rstc->id); 385 if (rstc->shared && ret) 386 atomic_dec(&rstc->triggered_count); 387 388 return ret; 389 } 390 EXPORT_SYMBOL_GPL(reset_control_reset); 391 392 /** 393 * reset_control_bulk_reset - reset the controlled devices in order 394 * @num_rstcs: number of entries in rstcs array 395 * @rstcs: array of struct reset_control_bulk_data with reset controls set 396 * 397 * Issue a reset on all provided reset controls, in order. 398 * 399 * See also: reset_control_reset() 400 */ 401 int reset_control_bulk_reset(int num_rstcs, 402 struct reset_control_bulk_data *rstcs) 403 { 404 int ret, i; 405 406 for (i = 0; i < num_rstcs; i++) { 407 ret = reset_control_reset(rstcs[i].rstc); 408 if (ret) 409 return ret; 410 } 411 412 return 0; 413 } 414 EXPORT_SYMBOL_GPL(reset_control_bulk_reset); 415 416 /** 417 * reset_control_rearm - allow shared reset line to be re-triggered" 418 * @rstc: reset controller 419 * 420 * On a shared reset line the actual reset pulse is only triggered once for the 421 * lifetime of the reset_control instance, except if this call is used. 422 * 423 * Calls to this function must be balanced with calls to reset_control_reset, 424 * a warning is thrown in case triggered_count ever dips below 0. 425 * 426 * Consumers must not use reset_control_(de)assert on shared reset lines when 427 * reset_control_reset or reset_control_rearm have been used. 428 * 429 * If rstc is NULL the function will just return 0. 430 */ 431 int reset_control_rearm(struct reset_control *rstc) 432 { 433 if (!rstc) 434 return 0; 435 436 if (WARN_ON(IS_ERR(rstc))) 437 return -EINVAL; 438 439 if (reset_control_is_array(rstc)) 440 return reset_control_array_rearm(rstc_to_array(rstc)); 441 442 if (rstc->shared) { 443 if (WARN_ON(atomic_read(&rstc->deassert_count) != 0)) 444 return -EINVAL; 445 446 WARN_ON(atomic_dec_return(&rstc->triggered_count) < 0); 447 } else { 448 if (!rstc->acquired) 449 return -EPERM; 450 } 451 452 return 0; 453 } 454 EXPORT_SYMBOL_GPL(reset_control_rearm); 455 456 /** 457 * reset_control_assert - asserts the reset line 458 * @rstc: reset controller 459 * 460 * Calling this on an exclusive reset controller guarantees that the reset 461 * will be asserted. When called on a shared reset controller the line may 462 * still be deasserted, as long as other users keep it so. 463 * 464 * For shared reset controls a driver cannot expect the hw's registers and 465 * internal state to be reset, but must be prepared for this to happen. 466 * Consumers must not use reset_control_reset on shared reset lines when 467 * reset_control_(de)assert has been used. 468 * 469 * If rstc is NULL it is an optional reset and the function will just 470 * return 0. 471 */ 472 int reset_control_assert(struct reset_control *rstc) 473 { 474 if (!rstc) 475 return 0; 476 477 if (WARN_ON(IS_ERR(rstc))) 478 return -EINVAL; 479 480 if (reset_control_is_array(rstc)) 481 return reset_control_array_assert(rstc_to_array(rstc)); 482 483 if (rstc->shared) { 484 if (WARN_ON(atomic_read(&rstc->triggered_count) != 0)) 485 return -EINVAL; 486 487 if (WARN_ON(atomic_read(&rstc->deassert_count) == 0)) 488 return -EINVAL; 489 490 if (atomic_dec_return(&rstc->deassert_count) != 0) 491 return 0; 492 493 /* 494 * Shared reset controls allow the reset line to be in any state 495 * after this call, so doing nothing is a valid option. 496 */ 497 if (!rstc->rcdev->ops->assert) 498 return 0; 499 } else { 500 /* 501 * If the reset controller does not implement .assert(), there 502 * is no way to guarantee that the reset line is asserted after 503 * this call. 504 */ 505 if (!rstc->rcdev->ops->assert) 506 return -ENOTSUPP; 507 508 if (!rstc->acquired) { 509 WARN(1, "reset %s (ID: %u) is not acquired\n", 510 rcdev_name(rstc->rcdev), rstc->id); 511 return -EPERM; 512 } 513 } 514 515 return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id); 516 } 517 EXPORT_SYMBOL_GPL(reset_control_assert); 518 519 /** 520 * reset_control_bulk_assert - asserts the reset lines in order 521 * @num_rstcs: number of entries in rstcs array 522 * @rstcs: array of struct reset_control_bulk_data with reset controls set 523 * 524 * Assert the reset lines for all provided reset controls, in order. 525 * If an assertion fails, already asserted resets are deasserted again. 526 * 527 * See also: reset_control_assert() 528 */ 529 int reset_control_bulk_assert(int num_rstcs, 530 struct reset_control_bulk_data *rstcs) 531 { 532 int ret, i; 533 534 for (i = 0; i < num_rstcs; i++) { 535 ret = reset_control_assert(rstcs[i].rstc); 536 if (ret) 537 goto err; 538 } 539 540 return 0; 541 542 err: 543 while (i--) 544 reset_control_deassert(rstcs[i].rstc); 545 return ret; 546 } 547 EXPORT_SYMBOL_GPL(reset_control_bulk_assert); 548 549 /** 550 * reset_control_deassert - deasserts the reset line 551 * @rstc: reset controller 552 * 553 * After calling this function, the reset is guaranteed to be deasserted. 554 * Consumers must not use reset_control_reset on shared reset lines when 555 * reset_control_(de)assert has been used. 556 * 557 * If rstc is NULL it is an optional reset and the function will just 558 * return 0. 559 */ 560 int reset_control_deassert(struct reset_control *rstc) 561 { 562 if (!rstc) 563 return 0; 564 565 if (WARN_ON(IS_ERR(rstc))) 566 return -EINVAL; 567 568 if (reset_control_is_array(rstc)) 569 return reset_control_array_deassert(rstc_to_array(rstc)); 570 571 if (rstc->shared) { 572 if (WARN_ON(atomic_read(&rstc->triggered_count) != 0)) 573 return -EINVAL; 574 575 if (atomic_inc_return(&rstc->deassert_count) != 1) 576 return 0; 577 } else { 578 if (!rstc->acquired) { 579 WARN(1, "reset %s (ID: %u) is not acquired\n", 580 rcdev_name(rstc->rcdev), rstc->id); 581 return -EPERM; 582 } 583 } 584 585 /* 586 * If the reset controller does not implement .deassert(), we assume 587 * that it handles self-deasserting reset lines via .reset(). In that 588 * case, the reset lines are deasserted by default. If that is not the 589 * case, the reset controller driver should implement .deassert() and 590 * return -ENOTSUPP. 591 */ 592 if (!rstc->rcdev->ops->deassert) 593 return 0; 594 595 return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id); 596 } 597 EXPORT_SYMBOL_GPL(reset_control_deassert); 598 599 /** 600 * reset_control_bulk_deassert - deasserts the reset lines in reverse order 601 * @num_rstcs: number of entries in rstcs array 602 * @rstcs: array of struct reset_control_bulk_data with reset controls set 603 * 604 * Deassert the reset lines for all provided reset controls, in reverse order. 605 * If a deassertion fails, already deasserted resets are asserted again. 606 * 607 * See also: reset_control_deassert() 608 */ 609 int reset_control_bulk_deassert(int num_rstcs, 610 struct reset_control_bulk_data *rstcs) 611 { 612 int ret, i; 613 614 for (i = num_rstcs - 1; i >= 0; i--) { 615 ret = reset_control_deassert(rstcs[i].rstc); 616 if (ret) 617 goto err; 618 } 619 620 return 0; 621 622 err: 623 while (i < num_rstcs) 624 reset_control_assert(rstcs[i++].rstc); 625 return ret; 626 } 627 EXPORT_SYMBOL_GPL(reset_control_bulk_deassert); 628 629 /** 630 * reset_control_status - returns a negative errno if not supported, a 631 * positive value if the reset line is asserted, or zero if the reset 632 * line is not asserted or if the desc is NULL (optional reset). 633 * @rstc: reset controller 634 */ 635 int reset_control_status(struct reset_control *rstc) 636 { 637 if (!rstc) 638 return 0; 639 640 if (WARN_ON(IS_ERR(rstc)) || reset_control_is_array(rstc)) 641 return -EINVAL; 642 643 if (rstc->rcdev->ops->status) 644 return rstc->rcdev->ops->status(rstc->rcdev, rstc->id); 645 646 return -ENOTSUPP; 647 } 648 EXPORT_SYMBOL_GPL(reset_control_status); 649 650 /** 651 * reset_control_acquire() - acquires a reset control for exclusive use 652 * @rstc: reset control 653 * 654 * This is used to explicitly acquire a reset control for exclusive use. Note 655 * that exclusive resets are requested as acquired by default. In order for a 656 * second consumer to be able to control the reset, the first consumer has to 657 * release it first. Typically the easiest way to achieve this is to call the 658 * reset_control_get_exclusive_released() to obtain an instance of the reset 659 * control. Such reset controls are not acquired by default. 660 * 661 * Consumers implementing shared access to an exclusive reset need to follow 662 * a specific protocol in order to work together. Before consumers can change 663 * a reset they must acquire exclusive access using reset_control_acquire(). 664 * After they are done operating the reset, they must release exclusive access 665 * with a call to reset_control_release(). Consumers are not granted exclusive 666 * access to the reset as long as another consumer hasn't released a reset. 667 * 668 * See also: reset_control_release() 669 */ 670 int reset_control_acquire(struct reset_control *rstc) 671 { 672 struct reset_control *rc; 673 674 if (!rstc) 675 return 0; 676 677 if (WARN_ON(IS_ERR(rstc))) 678 return -EINVAL; 679 680 if (reset_control_is_array(rstc)) 681 return reset_control_array_acquire(rstc_to_array(rstc)); 682 683 mutex_lock(&reset_list_mutex); 684 685 if (rstc->acquired) { 686 mutex_unlock(&reset_list_mutex); 687 return 0; 688 } 689 690 list_for_each_entry(rc, &rstc->rcdev->reset_control_head, list) { 691 if (rstc != rc && rstc->id == rc->id) { 692 if (rc->acquired) { 693 mutex_unlock(&reset_list_mutex); 694 return -EBUSY; 695 } 696 } 697 } 698 699 rstc->acquired = true; 700 701 mutex_unlock(&reset_list_mutex); 702 return 0; 703 } 704 EXPORT_SYMBOL_GPL(reset_control_acquire); 705 706 /** 707 * reset_control_bulk_acquire - acquires reset controls for exclusive use 708 * @num_rstcs: number of entries in rstcs array 709 * @rstcs: array of struct reset_control_bulk_data with reset controls set 710 * 711 * This is used to explicitly acquire reset controls requested with 712 * reset_control_bulk_get_exclusive_release() for temporary exclusive use. 713 * 714 * See also: reset_control_acquire(), reset_control_bulk_release() 715 */ 716 int reset_control_bulk_acquire(int num_rstcs, 717 struct reset_control_bulk_data *rstcs) 718 { 719 int ret, i; 720 721 for (i = 0; i < num_rstcs; i++) { 722 ret = reset_control_acquire(rstcs[i].rstc); 723 if (ret) 724 goto err; 725 } 726 727 return 0; 728 729 err: 730 while (i--) 731 reset_control_release(rstcs[i].rstc); 732 return ret; 733 } 734 EXPORT_SYMBOL_GPL(reset_control_bulk_acquire); 735 736 /** 737 * reset_control_release() - releases exclusive access to a reset control 738 * @rstc: reset control 739 * 740 * Releases exclusive access right to a reset control previously obtained by a 741 * call to reset_control_acquire(). Until a consumer calls this function, no 742 * other consumers will be granted exclusive access. 743 * 744 * See also: reset_control_acquire() 745 */ 746 void reset_control_release(struct reset_control *rstc) 747 { 748 if (!rstc || WARN_ON(IS_ERR(rstc))) 749 return; 750 751 if (reset_control_is_array(rstc)) 752 reset_control_array_release(rstc_to_array(rstc)); 753 else 754 rstc->acquired = false; 755 } 756 EXPORT_SYMBOL_GPL(reset_control_release); 757 758 /** 759 * reset_control_bulk_release() - releases exclusive access to reset controls 760 * @num_rstcs: number of entries in rstcs array 761 * @rstcs: array of struct reset_control_bulk_data with reset controls set 762 * 763 * Releases exclusive access right to reset controls previously obtained by a 764 * call to reset_control_bulk_acquire(). 765 * 766 * See also: reset_control_release(), reset_control_bulk_acquire() 767 */ 768 void reset_control_bulk_release(int num_rstcs, 769 struct reset_control_bulk_data *rstcs) 770 { 771 int i; 772 773 for (i = 0; i < num_rstcs; i++) 774 reset_control_release(rstcs[i].rstc); 775 } 776 EXPORT_SYMBOL_GPL(reset_control_bulk_release); 777 778 static struct reset_control * 779 __reset_control_get_internal(struct reset_controller_dev *rcdev, 780 unsigned int index, enum reset_control_flags flags) 781 { 782 bool shared = flags & RESET_CONTROL_FLAGS_BIT_SHARED; 783 bool acquired = flags & RESET_CONTROL_FLAGS_BIT_ACQUIRED; 784 struct reset_control *rstc; 785 786 lockdep_assert_held(&reset_list_mutex); 787 788 /* Expect callers to filter out OPTIONAL and DEASSERTED bits */ 789 if (WARN_ON(flags & ~(RESET_CONTROL_FLAGS_BIT_SHARED | 790 RESET_CONTROL_FLAGS_BIT_ACQUIRED))) 791 return ERR_PTR(-EINVAL); 792 793 list_for_each_entry(rstc, &rcdev->reset_control_head, list) { 794 if (rstc->id == index) { 795 /* 796 * Allow creating a secondary exclusive reset_control 797 * that is initially not acquired for an already 798 * controlled reset line. 799 */ 800 if (!rstc->shared && !shared && !acquired) 801 break; 802 803 if (WARN_ON(!rstc->shared || !shared)) 804 return ERR_PTR(-EBUSY); 805 806 kref_get(&rstc->refcnt); 807 return rstc; 808 } 809 } 810 811 rstc = kzalloc(sizeof(*rstc), GFP_KERNEL); 812 if (!rstc) 813 return ERR_PTR(-ENOMEM); 814 815 if (!try_module_get(rcdev->owner)) { 816 kfree(rstc); 817 return ERR_PTR(-ENODEV); 818 } 819 820 rstc->rcdev = rcdev; 821 list_add(&rstc->list, &rcdev->reset_control_head); 822 rstc->id = index; 823 kref_init(&rstc->refcnt); 824 rstc->acquired = acquired; 825 rstc->shared = shared; 826 get_device(rcdev->dev); 827 828 return rstc; 829 } 830 831 static void __reset_control_release(struct kref *kref) 832 { 833 struct reset_control *rstc = container_of(kref, struct reset_control, 834 refcnt); 835 836 lockdep_assert_held(&reset_list_mutex); 837 838 module_put(rstc->rcdev->owner); 839 840 list_del(&rstc->list); 841 put_device(rstc->rcdev->dev); 842 kfree(rstc); 843 } 844 845 static void __reset_control_put_internal(struct reset_control *rstc) 846 { 847 lockdep_assert_held(&reset_list_mutex); 848 849 if (IS_ERR_OR_NULL(rstc)) 850 return; 851 852 kref_put(&rstc->refcnt, __reset_control_release); 853 } 854 855 static void reset_gpio_aux_device_release(struct device *dev) 856 { 857 struct auxiliary_device *adev = to_auxiliary_dev(dev); 858 859 kfree(adev); 860 } 861 862 static int reset_add_gpio_aux_device(struct device *parent, 863 struct fwnode_handle *swnode, 864 int id, void *pdata) 865 { 866 struct auxiliary_device *adev; 867 int ret; 868 869 adev = kzalloc(sizeof(*adev), GFP_KERNEL); 870 if (!adev) 871 return -ENOMEM; 872 873 adev->id = id; 874 adev->name = "gpio"; 875 adev->dev.parent = parent; 876 adev->dev.platform_data = pdata; 877 adev->dev.release = reset_gpio_aux_device_release; 878 device_set_node(&adev->dev, swnode); 879 880 ret = auxiliary_device_init(adev); 881 if (ret) { 882 kfree(adev); 883 return ret; 884 } 885 886 ret = __auxiliary_device_add(adev, "reset"); 887 if (ret) { 888 auxiliary_device_uninit(adev); 889 kfree(adev); 890 return ret; 891 } 892 893 return ret; 894 } 895 896 /* 897 * @args: phandle to the GPIO provider with all the args like GPIO number 898 */ 899 static int __reset_add_reset_gpio_device(const struct of_phandle_args *args) 900 { 901 struct property_entry properties[2] = { }; 902 unsigned int offset, of_flags, lflags; 903 struct reset_gpio_lookup *rgpio_dev; 904 struct device *parent; 905 int id, ret; 906 907 /* 908 * Currently only #gpio-cells=2 is supported with the meaning of: 909 * args[0]: GPIO number 910 * args[1]: GPIO flags 911 * TODO: Handle other cases. 912 */ 913 if (args->args_count != 2) 914 return -ENOENT; 915 916 /* 917 * Registering reset-gpio device might cause immediate 918 * bind, resulting in its probe() registering new reset controller thus 919 * taking reset_list_mutex lock via reset_controller_register(). 920 */ 921 lockdep_assert_not_held(&reset_list_mutex); 922 923 offset = args->args[0]; 924 of_flags = args->args[1]; 925 926 /* 927 * Later we map GPIO flags between OF and Linux, however not all 928 * constants from include/dt-bindings/gpio/gpio.h and 929 * include/linux/gpio/machine.h match each other. 930 * 931 * FIXME: Find a better way of translating OF flags to GPIO lookup 932 * flags. 933 */ 934 if (of_flags > GPIO_ACTIVE_LOW) { 935 pr_err("reset-gpio code does not support GPIO flags %u for GPIO %u\n", 936 of_flags, offset); 937 return -EINVAL; 938 } 939 940 struct gpio_device *gdev __free(gpio_device_put) = 941 gpio_device_find_by_fwnode(of_fwnode_handle(args->np)); 942 if (!gdev) 943 return -EPROBE_DEFER; 944 945 guard(mutex)(&reset_gpio_lookup_mutex); 946 947 list_for_each_entry(rgpio_dev, &reset_gpio_lookup_list, list) { 948 if (args->np == rgpio_dev->of_args.np) { 949 if (of_phandle_args_equal(args, &rgpio_dev->of_args)) 950 return 0; /* Already on the list, done */ 951 } 952 } 953 954 lflags = GPIO_PERSISTENT | (of_flags & GPIO_ACTIVE_LOW); 955 parent = gpio_device_to_device(gdev); 956 properties[0] = PROPERTY_ENTRY_GPIO("reset-gpios", parent->fwnode, offset, lflags); 957 958 id = ida_alloc(&reset_gpio_ida, GFP_KERNEL); 959 if (id < 0) 960 return id; 961 962 /* Not freed on success, because it is persisent subsystem data. */ 963 rgpio_dev = kzalloc(sizeof(*rgpio_dev), GFP_KERNEL); 964 if (!rgpio_dev) { 965 ret = -ENOMEM; 966 goto err_ida_free; 967 } 968 969 rgpio_dev->of_args = *args; 970 /* 971 * We keep the device_node reference, but of_args.np is put at the end 972 * of __of_reset_control_get(), so get it one more time. 973 * Hold reference as long as rgpio_dev memory is valid. 974 */ 975 of_node_get(rgpio_dev->of_args.np); 976 977 rgpio_dev->swnode = fwnode_create_software_node(properties, NULL); 978 if (IS_ERR(rgpio_dev->swnode)) { 979 ret = PTR_ERR(rgpio_dev->swnode); 980 goto err_put_of_node; 981 } 982 983 ret = reset_add_gpio_aux_device(parent, rgpio_dev->swnode, id, 984 &rgpio_dev->of_args); 985 if (ret) 986 goto err_del_swnode; 987 988 list_add(&rgpio_dev->list, &reset_gpio_lookup_list); 989 990 return 0; 991 992 err_del_swnode: 993 fwnode_remove_software_node(rgpio_dev->swnode); 994 err_put_of_node: 995 of_node_put(rgpio_dev->of_args.np); 996 kfree(rgpio_dev); 997 err_ida_free: 998 ida_free(&reset_gpio_ida, id); 999 1000 return ret; 1001 } 1002 1003 static struct reset_controller_dev *__reset_find_rcdev(const struct of_phandle_args *args, 1004 bool gpio_fallback) 1005 { 1006 struct reset_controller_dev *rcdev; 1007 1008 lockdep_assert_held(&reset_list_mutex); 1009 1010 list_for_each_entry(rcdev, &reset_controller_list, list) { 1011 if (gpio_fallback) { 1012 if (rcdev->of_args && of_phandle_args_equal(args, 1013 rcdev->of_args)) 1014 return rcdev; 1015 } else { 1016 if (args->np == rcdev->of_node) 1017 return rcdev; 1018 } 1019 } 1020 1021 return NULL; 1022 } 1023 1024 struct reset_control * 1025 __of_reset_control_get(struct device_node *node, const char *id, int index, 1026 enum reset_control_flags flags) 1027 { 1028 bool optional = flags & RESET_CONTROL_FLAGS_BIT_OPTIONAL; 1029 bool gpio_fallback = false; 1030 struct reset_control *rstc; 1031 struct reset_controller_dev *rcdev; 1032 struct of_phandle_args args; 1033 int rstc_id; 1034 int ret; 1035 1036 if (!node) 1037 return ERR_PTR(-EINVAL); 1038 1039 if (id) { 1040 index = of_property_match_string(node, 1041 "reset-names", id); 1042 if (index == -EILSEQ) 1043 return ERR_PTR(index); 1044 if (index < 0) 1045 return optional ? NULL : ERR_PTR(-ENOENT); 1046 } 1047 1048 ret = of_parse_phandle_with_args(node, "resets", "#reset-cells", 1049 index, &args); 1050 if (ret == -EINVAL) 1051 return ERR_PTR(ret); 1052 if (ret) { 1053 if (!IS_ENABLED(CONFIG_RESET_GPIO)) 1054 return optional ? NULL : ERR_PTR(ret); 1055 1056 /* 1057 * There can be only one reset-gpio for regular devices, so 1058 * don't bother with the "reset-gpios" phandle index. 1059 */ 1060 ret = of_parse_phandle_with_args(node, "reset-gpios", "#gpio-cells", 1061 0, &args); 1062 if (ret) 1063 return optional ? NULL : ERR_PTR(ret); 1064 1065 gpio_fallback = true; 1066 1067 ret = __reset_add_reset_gpio_device(&args); 1068 if (ret) { 1069 rstc = ERR_PTR(ret); 1070 goto out_put; 1071 } 1072 } 1073 1074 mutex_lock(&reset_list_mutex); 1075 rcdev = __reset_find_rcdev(&args, gpio_fallback); 1076 if (!rcdev) { 1077 rstc = ERR_PTR(-EPROBE_DEFER); 1078 goto out_unlock; 1079 } 1080 1081 if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) { 1082 rstc = ERR_PTR(-EINVAL); 1083 goto out_unlock; 1084 } 1085 1086 rstc_id = rcdev->of_xlate(rcdev, &args); 1087 if (rstc_id < 0) { 1088 rstc = ERR_PTR(rstc_id); 1089 goto out_unlock; 1090 } 1091 1092 flags &= ~RESET_CONTROL_FLAGS_BIT_OPTIONAL; 1093 1094 /* reset_list_mutex also protects the rcdev's reset_control list */ 1095 rstc = __reset_control_get_internal(rcdev, rstc_id, flags); 1096 1097 out_unlock: 1098 mutex_unlock(&reset_list_mutex); 1099 out_put: 1100 of_node_put(args.np); 1101 1102 return rstc; 1103 } 1104 EXPORT_SYMBOL_GPL(__of_reset_control_get); 1105 1106 static struct reset_controller_dev * 1107 __reset_controller_by_name(const char *name) 1108 { 1109 struct reset_controller_dev *rcdev; 1110 1111 lockdep_assert_held(&reset_list_mutex); 1112 1113 list_for_each_entry(rcdev, &reset_controller_list, list) { 1114 if (!rcdev->dev) 1115 continue; 1116 1117 if (!strcmp(name, dev_name(rcdev->dev))) 1118 return rcdev; 1119 } 1120 1121 return NULL; 1122 } 1123 1124 static struct reset_control * 1125 __reset_control_get_from_lookup(struct device *dev, const char *con_id, 1126 enum reset_control_flags flags) 1127 { 1128 bool optional = flags & RESET_CONTROL_FLAGS_BIT_OPTIONAL; 1129 const struct reset_control_lookup *lookup; 1130 struct reset_controller_dev *rcdev; 1131 const char *dev_id = dev_name(dev); 1132 struct reset_control *rstc = NULL; 1133 1134 mutex_lock(&reset_lookup_mutex); 1135 1136 list_for_each_entry(lookup, &reset_lookup_list, list) { 1137 if (strcmp(lookup->dev_id, dev_id)) 1138 continue; 1139 1140 if ((!con_id && !lookup->con_id) || 1141 ((con_id && lookup->con_id) && 1142 !strcmp(con_id, lookup->con_id))) { 1143 mutex_lock(&reset_list_mutex); 1144 rcdev = __reset_controller_by_name(lookup->provider); 1145 if (!rcdev) { 1146 mutex_unlock(&reset_list_mutex); 1147 mutex_unlock(&reset_lookup_mutex); 1148 /* Reset provider may not be ready yet. */ 1149 return ERR_PTR(-EPROBE_DEFER); 1150 } 1151 1152 flags &= ~RESET_CONTROL_FLAGS_BIT_OPTIONAL; 1153 1154 rstc = __reset_control_get_internal(rcdev, 1155 lookup->index, 1156 flags); 1157 mutex_unlock(&reset_list_mutex); 1158 break; 1159 } 1160 } 1161 1162 mutex_unlock(&reset_lookup_mutex); 1163 1164 if (!rstc) 1165 return optional ? NULL : ERR_PTR(-ENOENT); 1166 1167 return rstc; 1168 } 1169 1170 struct reset_control *__reset_control_get(struct device *dev, const char *id, 1171 int index, enum reset_control_flags flags) 1172 { 1173 bool shared = flags & RESET_CONTROL_FLAGS_BIT_SHARED; 1174 bool acquired = flags & RESET_CONTROL_FLAGS_BIT_ACQUIRED; 1175 1176 if (WARN_ON(shared && acquired)) 1177 return ERR_PTR(-EINVAL); 1178 1179 if (dev->of_node) 1180 return __of_reset_control_get(dev->of_node, id, index, flags); 1181 1182 return __reset_control_get_from_lookup(dev, id, flags); 1183 } 1184 EXPORT_SYMBOL_GPL(__reset_control_get); 1185 1186 int __reset_control_bulk_get(struct device *dev, int num_rstcs, 1187 struct reset_control_bulk_data *rstcs, 1188 enum reset_control_flags flags) 1189 { 1190 int ret, i; 1191 1192 for (i = 0; i < num_rstcs; i++) { 1193 rstcs[i].rstc = __reset_control_get(dev, rstcs[i].id, 0, flags); 1194 if (IS_ERR(rstcs[i].rstc)) { 1195 ret = PTR_ERR(rstcs[i].rstc); 1196 goto err; 1197 } 1198 } 1199 1200 return 0; 1201 1202 err: 1203 mutex_lock(&reset_list_mutex); 1204 while (i--) 1205 __reset_control_put_internal(rstcs[i].rstc); 1206 mutex_unlock(&reset_list_mutex); 1207 return ret; 1208 } 1209 EXPORT_SYMBOL_GPL(__reset_control_bulk_get); 1210 1211 static void reset_control_array_put(struct reset_control_array *resets) 1212 { 1213 int i; 1214 1215 mutex_lock(&reset_list_mutex); 1216 for (i = 0; i < resets->num_rstcs; i++) 1217 __reset_control_put_internal(resets->rstc[i]); 1218 mutex_unlock(&reset_list_mutex); 1219 kfree(resets); 1220 } 1221 1222 /** 1223 * reset_control_put - free the reset controller 1224 * @rstc: reset controller 1225 */ 1226 void reset_control_put(struct reset_control *rstc) 1227 { 1228 if (IS_ERR_OR_NULL(rstc)) 1229 return; 1230 1231 if (reset_control_is_array(rstc)) { 1232 reset_control_array_put(rstc_to_array(rstc)); 1233 return; 1234 } 1235 1236 mutex_lock(&reset_list_mutex); 1237 __reset_control_put_internal(rstc); 1238 mutex_unlock(&reset_list_mutex); 1239 } 1240 EXPORT_SYMBOL_GPL(reset_control_put); 1241 1242 /** 1243 * reset_control_bulk_put - free the reset controllers 1244 * @num_rstcs: number of entries in rstcs array 1245 * @rstcs: array of struct reset_control_bulk_data with reset controls set 1246 */ 1247 void reset_control_bulk_put(int num_rstcs, struct reset_control_bulk_data *rstcs) 1248 { 1249 mutex_lock(&reset_list_mutex); 1250 while (num_rstcs--) 1251 __reset_control_put_internal(rstcs[num_rstcs].rstc); 1252 mutex_unlock(&reset_list_mutex); 1253 } 1254 EXPORT_SYMBOL_GPL(reset_control_bulk_put); 1255 1256 static void devm_reset_control_release(struct device *dev, void *res) 1257 { 1258 reset_control_put(*(struct reset_control **)res); 1259 } 1260 1261 static void devm_reset_control_release_deasserted(struct device *dev, void *res) 1262 { 1263 struct reset_control *rstc = *(struct reset_control **)res; 1264 1265 reset_control_assert(rstc); 1266 reset_control_put(rstc); 1267 } 1268 1269 struct reset_control * 1270 __devm_reset_control_get(struct device *dev, const char *id, int index, 1271 enum reset_control_flags flags) 1272 { 1273 struct reset_control **ptr, *rstc; 1274 bool deasserted = flags & RESET_CONTROL_FLAGS_BIT_DEASSERTED; 1275 1276 ptr = devres_alloc(deasserted ? devm_reset_control_release_deasserted : 1277 devm_reset_control_release, sizeof(*ptr), 1278 GFP_KERNEL); 1279 if (!ptr) 1280 return ERR_PTR(-ENOMEM); 1281 1282 flags &= ~RESET_CONTROL_FLAGS_BIT_DEASSERTED; 1283 1284 rstc = __reset_control_get(dev, id, index, flags); 1285 if (IS_ERR_OR_NULL(rstc)) { 1286 devres_free(ptr); 1287 return rstc; 1288 } 1289 1290 if (deasserted) { 1291 int ret; 1292 1293 ret = reset_control_deassert(rstc); 1294 if (ret) { 1295 reset_control_put(rstc); 1296 devres_free(ptr); 1297 return ERR_PTR(ret); 1298 } 1299 } 1300 1301 *ptr = rstc; 1302 devres_add(dev, ptr); 1303 1304 return rstc; 1305 } 1306 EXPORT_SYMBOL_GPL(__devm_reset_control_get); 1307 1308 struct reset_control_bulk_devres { 1309 int num_rstcs; 1310 struct reset_control_bulk_data *rstcs; 1311 }; 1312 1313 static void devm_reset_control_bulk_release(struct device *dev, void *res) 1314 { 1315 struct reset_control_bulk_devres *devres = res; 1316 1317 reset_control_bulk_put(devres->num_rstcs, devres->rstcs); 1318 } 1319 1320 static void devm_reset_control_bulk_release_deasserted(struct device *dev, void *res) 1321 { 1322 struct reset_control_bulk_devres *devres = res; 1323 1324 reset_control_bulk_assert(devres->num_rstcs, devres->rstcs); 1325 reset_control_bulk_put(devres->num_rstcs, devres->rstcs); 1326 } 1327 1328 int __devm_reset_control_bulk_get(struct device *dev, int num_rstcs, 1329 struct reset_control_bulk_data *rstcs, 1330 enum reset_control_flags flags) 1331 { 1332 struct reset_control_bulk_devres *ptr; 1333 bool deasserted = flags & RESET_CONTROL_FLAGS_BIT_DEASSERTED; 1334 int ret; 1335 1336 ptr = devres_alloc(deasserted ? devm_reset_control_bulk_release_deasserted : 1337 devm_reset_control_bulk_release, sizeof(*ptr), 1338 GFP_KERNEL); 1339 if (!ptr) 1340 return -ENOMEM; 1341 1342 flags &= ~RESET_CONTROL_FLAGS_BIT_DEASSERTED; 1343 1344 ret = __reset_control_bulk_get(dev, num_rstcs, rstcs, flags); 1345 if (ret < 0) { 1346 devres_free(ptr); 1347 return ret; 1348 } 1349 1350 if (deasserted) { 1351 ret = reset_control_bulk_deassert(num_rstcs, rstcs); 1352 if (ret) { 1353 reset_control_bulk_put(num_rstcs, rstcs); 1354 devres_free(ptr); 1355 return ret; 1356 } 1357 } 1358 1359 ptr->num_rstcs = num_rstcs; 1360 ptr->rstcs = rstcs; 1361 devres_add(dev, ptr); 1362 1363 return 0; 1364 } 1365 EXPORT_SYMBOL_GPL(__devm_reset_control_bulk_get); 1366 1367 /** 1368 * __device_reset - find reset controller associated with the device 1369 * and perform reset 1370 * @dev: device to be reset by the controller 1371 * @optional: whether it is optional to reset the device 1372 * 1373 * Convenience wrapper for __reset_control_get() and reset_control_reset(). 1374 * This is useful for the common case of devices with single, dedicated reset 1375 * lines. _RST firmware method will be called for devices with ACPI. 1376 */ 1377 int __device_reset(struct device *dev, bool optional) 1378 { 1379 enum reset_control_flags flags; 1380 struct reset_control *rstc; 1381 int ret; 1382 1383 #ifdef CONFIG_ACPI 1384 acpi_handle handle = ACPI_HANDLE(dev); 1385 1386 if (handle) { 1387 if (!acpi_has_method(handle, "_RST")) 1388 return optional ? 0 : -ENOENT; 1389 if (ACPI_FAILURE(acpi_evaluate_object(handle, "_RST", NULL, 1390 NULL))) 1391 return -EIO; 1392 } 1393 #endif 1394 1395 flags = optional ? RESET_CONTROL_OPTIONAL_EXCLUSIVE : RESET_CONTROL_EXCLUSIVE; 1396 rstc = __reset_control_get(dev, NULL, 0, flags); 1397 if (IS_ERR(rstc)) 1398 return PTR_ERR(rstc); 1399 1400 ret = reset_control_reset(rstc); 1401 1402 reset_control_put(rstc); 1403 1404 return ret; 1405 } 1406 EXPORT_SYMBOL_GPL(__device_reset); 1407 1408 /* 1409 * APIs to manage an array of reset controls. 1410 */ 1411 1412 /** 1413 * of_reset_control_get_count - Count number of resets available with a device 1414 * 1415 * @node: device node that contains 'resets'. 1416 * 1417 * Returns positive reset count on success, or error number on failure and 1418 * on count being zero. 1419 */ 1420 static int of_reset_control_get_count(struct device_node *node) 1421 { 1422 int count; 1423 1424 if (!node) 1425 return -EINVAL; 1426 1427 count = of_count_phandle_with_args(node, "resets", "#reset-cells"); 1428 if (count == 0) 1429 count = -ENOENT; 1430 1431 return count; 1432 } 1433 1434 /** 1435 * of_reset_control_array_get - Get a list of reset controls using 1436 * device node. 1437 * 1438 * @np: device node for the device that requests the reset controls array 1439 * @flags: whether reset controls are shared, optional, acquired 1440 * 1441 * Returns pointer to allocated reset_control on success or error on failure 1442 */ 1443 struct reset_control * 1444 of_reset_control_array_get(struct device_node *np, enum reset_control_flags flags) 1445 { 1446 bool optional = flags & RESET_CONTROL_FLAGS_BIT_OPTIONAL; 1447 struct reset_control_array *resets; 1448 struct reset_control *rstc; 1449 int num, i; 1450 1451 num = of_reset_control_get_count(np); 1452 if (num < 0) 1453 return optional ? NULL : ERR_PTR(num); 1454 1455 resets = kzalloc(struct_size(resets, rstc, num), GFP_KERNEL); 1456 if (!resets) 1457 return ERR_PTR(-ENOMEM); 1458 resets->num_rstcs = num; 1459 1460 for (i = 0; i < num; i++) { 1461 rstc = __of_reset_control_get(np, NULL, i, flags); 1462 if (IS_ERR(rstc)) 1463 goto err_rst; 1464 resets->rstc[i] = rstc; 1465 } 1466 resets->base.array = true; 1467 1468 return &resets->base; 1469 1470 err_rst: 1471 mutex_lock(&reset_list_mutex); 1472 while (--i >= 0) 1473 __reset_control_put_internal(resets->rstc[i]); 1474 mutex_unlock(&reset_list_mutex); 1475 1476 kfree(resets); 1477 1478 return rstc; 1479 } 1480 EXPORT_SYMBOL_GPL(of_reset_control_array_get); 1481 1482 /** 1483 * devm_reset_control_array_get - Resource managed reset control array get 1484 * 1485 * @dev: device that requests the list of reset controls 1486 * @flags: whether reset controls are shared, optional, acquired 1487 * 1488 * The reset control array APIs are intended for a list of resets 1489 * that just have to be asserted or deasserted, without any 1490 * requirements on the order. 1491 * 1492 * Returns pointer to allocated reset_control on success or error on failure 1493 */ 1494 struct reset_control * 1495 devm_reset_control_array_get(struct device *dev, enum reset_control_flags flags) 1496 { 1497 struct reset_control **ptr, *rstc; 1498 1499 ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr), 1500 GFP_KERNEL); 1501 if (!ptr) 1502 return ERR_PTR(-ENOMEM); 1503 1504 rstc = of_reset_control_array_get(dev->of_node, flags); 1505 if (IS_ERR_OR_NULL(rstc)) { 1506 devres_free(ptr); 1507 return rstc; 1508 } 1509 1510 *ptr = rstc; 1511 devres_add(dev, ptr); 1512 1513 return rstc; 1514 } 1515 EXPORT_SYMBOL_GPL(devm_reset_control_array_get); 1516 1517 static int reset_control_get_count_from_lookup(struct device *dev) 1518 { 1519 const struct reset_control_lookup *lookup; 1520 const char *dev_id; 1521 int count = 0; 1522 1523 if (!dev) 1524 return -EINVAL; 1525 1526 dev_id = dev_name(dev); 1527 mutex_lock(&reset_lookup_mutex); 1528 1529 list_for_each_entry(lookup, &reset_lookup_list, list) { 1530 if (!strcmp(lookup->dev_id, dev_id)) 1531 count++; 1532 } 1533 1534 mutex_unlock(&reset_lookup_mutex); 1535 1536 if (count == 0) 1537 count = -ENOENT; 1538 1539 return count; 1540 } 1541 1542 /** 1543 * reset_control_get_count - Count number of resets available with a device 1544 * 1545 * @dev: device for which to return the number of resets 1546 * 1547 * Returns positive reset count on success, or error number on failure and 1548 * on count being zero. 1549 */ 1550 int reset_control_get_count(struct device *dev) 1551 { 1552 if (dev->of_node) 1553 return of_reset_control_get_count(dev->of_node); 1554 1555 return reset_control_get_count_from_lookup(dev); 1556 } 1557 EXPORT_SYMBOL_GPL(reset_control_get_count); 1558