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