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