1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2024 Linaro Ltd. 4 */ 5 6 #include <linux/bug.h> 7 #include <linux/cleanup.h> 8 #include <linux/debugfs.h> 9 #include <linux/device.h> 10 #include <linux/err.h> 11 #include <linux/export.h> 12 #include <linux/idr.h> 13 #include <linux/kernel.h> 14 #include <linux/kref.h> 15 #include <linux/list.h> 16 #include <linux/lockdep.h> 17 #include <linux/module.h> 18 #include <linux/mutex.h> 19 #include <linux/property.h> 20 #include <linux/pwrseq/consumer.h> 21 #include <linux/pwrseq/provider.h> 22 #include <linux/radix-tree.h> 23 #include <linux/rwsem.h> 24 #include <linux/slab.h> 25 26 /* 27 * Power-sequencing framework for linux. 28 * 29 * This subsystem allows power sequence providers to register a set of targets 30 * that consumers may request and power-up/down. 31 * 32 * Glossary: 33 * 34 * Unit - a unit is a discreet chunk of a power sequence. For instance one unit 35 * may enable a set of regulators, another may enable a specific GPIO. Units 36 * can define dependencies in the form of other units that must be enabled 37 * before it itself can be. 38 * 39 * Target - a target is a set of units (composed of the "final" unit and its 40 * dependencies) that a consumer selects by its name when requesting a handle 41 * to the power sequencer. Via the dependency system, multiple targets may 42 * share the same parts of a power sequence but ignore parts that are 43 * irrelevant. 44 * 45 * Descriptor - a handle passed by the pwrseq core to every consumer that 46 * serves as the entry point to the provider layer. It ensures coherence 47 * between different users and keeps reference counting consistent. 48 * 49 * Each provider must define a .match() callback whose role is to determine 50 * whether a potential consumer is in fact associated with this sequencer. 51 * This allows creating abstraction layers on top of regular device-tree 52 * resources like regulators, clocks and other nodes connected to the consumer 53 * via phandle. 54 */ 55 56 static DEFINE_IDA(pwrseq_ida); 57 58 /* 59 * Protects the device list on the pwrseq bus from concurrent modifications 60 * but allows simultaneous read-only access. 61 */ 62 static DECLARE_RWSEM(pwrseq_sem); 63 64 /** 65 * struct pwrseq_unit - Private power-sequence unit data. 66 * @ref: Reference count for this object. When it goes to 0, the object is 67 * destroyed. 68 * @name: Name of this target. 69 * @list: Link to siblings on the list of all units of a single sequencer. 70 * @deps: List of units on which this unit depends. 71 * @enable: Callback running the part of the power-on sequence provided by 72 * this unit. 73 * @disable: Callback running the part of the power-off sequence provided 74 * by this unit. 75 * @enable_count: Current number of users that enabled this unit. May be the 76 * consumer of the power sequencer or other units that depend 77 * on this one. 78 */ 79 struct pwrseq_unit { 80 struct kref ref; 81 const char *name; 82 struct list_head list; 83 struct list_head deps; 84 pwrseq_power_state_func enable; 85 pwrseq_power_state_func disable; 86 unsigned int enable_count; 87 }; 88 89 static struct pwrseq_unit *pwrseq_unit_new(const struct pwrseq_unit_data *data) 90 { 91 struct pwrseq_unit *unit; 92 93 unit = kzalloc_obj(*unit); 94 if (!unit) 95 return NULL; 96 97 unit->name = kstrdup_const(data->name, GFP_KERNEL); 98 if (!unit->name) { 99 kfree(unit); 100 return NULL; 101 } 102 103 kref_init(&unit->ref); 104 INIT_LIST_HEAD(&unit->list); 105 INIT_LIST_HEAD(&unit->deps); 106 unit->enable = data->enable; 107 unit->disable = data->disable; 108 109 return unit; 110 } 111 112 static struct pwrseq_unit *pwrseq_unit_get(struct pwrseq_unit *unit) 113 { 114 kref_get(&unit->ref); 115 116 return unit; 117 } 118 119 static void pwrseq_unit_release(struct kref *ref); 120 121 static void pwrseq_unit_put(struct pwrseq_unit *unit) 122 { 123 kref_put(&unit->ref, pwrseq_unit_release); 124 } 125 126 /** 127 * struct pwrseq_unit_dep - Wrapper around a reference to the unit structure 128 * allowing to keep it on multiple dependency lists 129 * in different units. 130 * @list: Siblings on the list. 131 * @unit: Address of the referenced unit. 132 */ 133 struct pwrseq_unit_dep { 134 struct list_head list; 135 struct pwrseq_unit *unit; 136 }; 137 138 static struct pwrseq_unit_dep *pwrseq_unit_dep_new(struct pwrseq_unit *unit) 139 { 140 struct pwrseq_unit_dep *dep; 141 142 dep = kzalloc_obj(*dep); 143 if (!dep) 144 return NULL; 145 146 dep->unit = unit; 147 148 return dep; 149 } 150 151 static void pwrseq_unit_dep_free(struct pwrseq_unit_dep *ref) 152 { 153 pwrseq_unit_put(ref->unit); 154 kfree(ref); 155 } 156 157 static void pwrseq_unit_free_deps(struct list_head *list) 158 { 159 struct pwrseq_unit_dep *dep, *next; 160 161 list_for_each_entry_safe(dep, next, list, list) { 162 list_del(&dep->list); 163 pwrseq_unit_dep_free(dep); 164 } 165 } 166 167 static void pwrseq_unit_release(struct kref *ref) 168 { 169 struct pwrseq_unit *unit = container_of(ref, struct pwrseq_unit, ref); 170 171 pwrseq_unit_free_deps(&unit->deps); 172 list_del(&unit->list); 173 kfree_const(unit->name); 174 kfree(unit); 175 } 176 177 /** 178 * struct pwrseq_target - Private power-sequence target data. 179 * @list: Siblings on the list of all targets exposed by a power sequencer. 180 * @name: Name of the target. 181 * @unit: Final unit for this target. 182 * @post_enable: Callback run after the target unit has been enabled, *after* 183 * the state lock has been released. It's useful for implementing 184 * boot-up delays without blocking other users from powering up 185 * using the same power sequencer. 186 * @is_controllable: Optional callback reporting whether enabling/disabling 187 * this target actually controls power. 188 */ 189 struct pwrseq_target { 190 struct list_head list; 191 const char *name; 192 struct pwrseq_unit *unit; 193 pwrseq_power_state_func post_enable; 194 pwrseq_is_controllable_func is_controllable; 195 }; 196 197 static struct pwrseq_target * 198 pwrseq_target_new(const struct pwrseq_target_data *data) 199 { 200 struct pwrseq_target *target; 201 202 target = kzalloc_obj(*target); 203 if (!target) 204 return NULL; 205 206 target->name = kstrdup_const(data->name, GFP_KERNEL); 207 if (!target->name) { 208 kfree(target); 209 return NULL; 210 } 211 212 target->post_enable = data->post_enable; 213 target->is_controllable = data->is_controllable; 214 215 return target; 216 } 217 218 static void pwrseq_target_free(struct pwrseq_target *target) 219 { 220 if (!IS_ERR_OR_NULL(target->unit)) 221 pwrseq_unit_put(target->unit); 222 kfree_const(target->name); 223 kfree(target); 224 } 225 226 /** 227 * struct pwrseq_device - Private power sequencing data. 228 * @dev: Device struct associated with this sequencer. 229 * @id: Device ID. 230 * @owner: Prevents removal of active power sequencing providers. 231 * @rw_lock: Protects the device from being unregistered while in use. 232 * @state_lock: Prevents multiple users running the power sequence at the same 233 * time. 234 * @match: Power sequencer matching callback. 235 * @targets: List of targets exposed by this sequencer. 236 * @units: List of all units supported by this sequencer. 237 */ 238 struct pwrseq_device { 239 struct device dev; 240 int id; 241 struct module *owner; 242 struct rw_semaphore rw_lock; 243 struct mutex state_lock; 244 pwrseq_match_func match; 245 struct list_head targets; 246 struct list_head units; 247 }; 248 249 static struct pwrseq_device *to_pwrseq_device(struct device *dev) 250 { 251 return container_of(dev, struct pwrseq_device, dev); 252 } 253 254 static struct pwrseq_device *pwrseq_device_get(struct pwrseq_device *pwrseq) 255 { 256 get_device(&pwrseq->dev); 257 258 return pwrseq; 259 } 260 261 static void pwrseq_device_put(struct pwrseq_device *pwrseq) 262 { 263 put_device(&pwrseq->dev); 264 } 265 266 /** 267 * struct pwrseq_desc - Wraps access to the pwrseq_device and ensures that one 268 * user cannot break the reference counting for others. 269 * @pwrseq: Reference to the power sequencing device. 270 * @target: Reference to the target this descriptor allows to control. 271 * @powered_on: Power state set by the holder of the descriptor (not necessarily 272 * corresponding to the actual power state of the device). 273 * 274 * Note: The user that requested the descriptor from the provider is 275 * responsible for the serialization of access to it. Concurrent threads must 276 * not access the descriptor structure without synchronization as power 277 * sequencing core does not provide any per-descriptor locking. 278 */ 279 struct pwrseq_desc { 280 struct pwrseq_device *pwrseq; 281 struct pwrseq_target *target; 282 bool powered_on; 283 }; 284 285 static const struct bus_type pwrseq_bus = { 286 .name = "pwrseq", 287 }; 288 289 static void pwrseq_release(struct device *dev) 290 { 291 struct pwrseq_device *pwrseq = to_pwrseq_device(dev); 292 struct pwrseq_target *target, *pos; 293 294 list_for_each_entry_safe(target, pos, &pwrseq->targets, list) { 295 list_del(&target->list); 296 pwrseq_target_free(target); 297 } 298 299 mutex_destroy(&pwrseq->state_lock); 300 ida_free(&pwrseq_ida, pwrseq->id); 301 kfree(pwrseq); 302 } 303 304 static const struct device_type pwrseq_device_type = { 305 .name = "power_sequencer", 306 .release = pwrseq_release, 307 }; 308 309 static int pwrseq_check_unit_deps(const struct pwrseq_unit_data *data, 310 struct radix_tree_root *visited_units) 311 { 312 const struct pwrseq_unit_data *tmp, **cur; 313 int ret; 314 315 ret = radix_tree_insert(visited_units, (unsigned long)data, 316 (void *)data); 317 if (ret) 318 return ret; 319 320 for (cur = data->deps; cur && *cur; cur++) { 321 tmp = radix_tree_lookup(visited_units, (unsigned long)*cur); 322 if (tmp) { 323 WARN(1, "Circular dependency in power sequencing flow detected!\n"); 324 return -EINVAL; 325 } 326 327 ret = pwrseq_check_unit_deps(*cur, visited_units); 328 if (ret) 329 return ret; 330 } 331 332 return 0; 333 } 334 335 static int pwrseq_check_target_deps(const struct pwrseq_target_data *data) 336 { 337 struct radix_tree_root visited_units; 338 struct radix_tree_iter iter; 339 void __rcu **slot; 340 int ret; 341 342 if (!data->unit) 343 return -EINVAL; 344 345 INIT_RADIX_TREE(&visited_units, GFP_KERNEL); 346 ret = pwrseq_check_unit_deps(data->unit, &visited_units); 347 radix_tree_for_each_slot(slot, &visited_units, &iter, 0) 348 radix_tree_delete(&visited_units, iter.index); 349 350 return ret; 351 } 352 353 static int pwrseq_unit_setup_deps(const struct pwrseq_unit_data **data, 354 struct list_head *dep_list, 355 struct list_head *unit_list, 356 struct radix_tree_root *processed_units); 357 358 static struct pwrseq_unit * 359 pwrseq_unit_setup(const struct pwrseq_unit_data *data, 360 struct list_head *unit_list, 361 struct radix_tree_root *processed_units) 362 { 363 struct pwrseq_unit *unit; 364 int ret; 365 366 unit = radix_tree_lookup(processed_units, (unsigned long)data); 367 if (unit) 368 return pwrseq_unit_get(unit); 369 370 unit = pwrseq_unit_new(data); 371 if (!unit) 372 return ERR_PTR(-ENOMEM); 373 374 if (data->deps) { 375 ret = pwrseq_unit_setup_deps(data->deps, &unit->deps, 376 unit_list, processed_units); 377 if (ret) { 378 pwrseq_unit_put(unit); 379 return ERR_PTR(ret); 380 } 381 } 382 383 ret = radix_tree_insert(processed_units, (unsigned long)data, unit); 384 if (ret) { 385 pwrseq_unit_put(unit); 386 return ERR_PTR(ret); 387 } 388 389 list_add_tail(&unit->list, unit_list); 390 391 return unit; 392 } 393 394 static int pwrseq_unit_setup_deps(const struct pwrseq_unit_data **data, 395 struct list_head *dep_list, 396 struct list_head *unit_list, 397 struct radix_tree_root *processed_units) 398 { 399 const struct pwrseq_unit_data *pos; 400 struct pwrseq_unit_dep *dep; 401 struct pwrseq_unit *unit; 402 int i; 403 404 for (i = 0; data[i]; i++) { 405 pos = data[i]; 406 407 unit = pwrseq_unit_setup(pos, unit_list, processed_units); 408 if (IS_ERR(unit)) 409 return PTR_ERR(unit); 410 411 dep = pwrseq_unit_dep_new(unit); 412 if (!dep) { 413 pwrseq_unit_put(unit); 414 return -ENOMEM; 415 } 416 417 list_add_tail(&dep->list, dep_list); 418 } 419 420 return 0; 421 } 422 423 static int pwrseq_do_setup_targets(const struct pwrseq_target_data **data, 424 struct pwrseq_device *pwrseq, 425 struct radix_tree_root *processed_units) 426 { 427 const struct pwrseq_target_data *pos; 428 struct pwrseq_target *target; 429 int ret, i; 430 431 for (i = 0; data[i]; i++) { 432 pos = data[i]; 433 434 ret = pwrseq_check_target_deps(pos); 435 if (ret) 436 return ret; 437 438 target = pwrseq_target_new(pos); 439 if (!target) 440 return -ENOMEM; 441 442 target->unit = pwrseq_unit_setup(pos->unit, &pwrseq->units, 443 processed_units); 444 if (IS_ERR(target->unit)) { 445 ret = PTR_ERR(target->unit); 446 pwrseq_target_free(target); 447 return ret; 448 } 449 450 list_add_tail(&target->list, &pwrseq->targets); 451 } 452 453 return 0; 454 } 455 456 static int pwrseq_setup_targets(const struct pwrseq_target_data **targets, 457 struct pwrseq_device *pwrseq) 458 { 459 struct radix_tree_root processed_units; 460 struct radix_tree_iter iter; 461 void __rcu **slot; 462 int ret; 463 464 INIT_RADIX_TREE(&processed_units, GFP_KERNEL); 465 ret = pwrseq_do_setup_targets(targets, pwrseq, &processed_units); 466 radix_tree_for_each_slot(slot, &processed_units, &iter, 0) 467 radix_tree_delete(&processed_units, iter.index); 468 469 return ret; 470 } 471 472 /** 473 * pwrseq_device_register() - Register a new power sequencer. 474 * @config: Configuration of the new power sequencing device. 475 * 476 * The config structure is only used during the call and can be freed after 477 * the function returns. The config structure *must* have the parent device 478 * as well as the match() callback and at least one target set. 479 * 480 * Returns: 481 * Returns the address of the new pwrseq device or ERR_PTR() on failure. 482 */ 483 struct pwrseq_device * 484 pwrseq_device_register(const struct pwrseq_config *config) 485 { 486 struct pwrseq_device *pwrseq; 487 int ret, id; 488 489 if (!config->parent || !config->match || !config->targets || 490 !config->targets[0]) 491 return ERR_PTR(-EINVAL); 492 493 pwrseq = kzalloc_obj(*pwrseq); 494 if (!pwrseq) 495 return ERR_PTR(-ENOMEM); 496 497 pwrseq->dev.type = &pwrseq_device_type; 498 pwrseq->dev.bus = &pwrseq_bus; 499 pwrseq->dev.parent = config->parent; 500 device_set_node(&pwrseq->dev, dev_fwnode(config->parent)); 501 dev_set_drvdata(&pwrseq->dev, config->drvdata); 502 503 id = ida_alloc(&pwrseq_ida, GFP_KERNEL); 504 if (id < 0) { 505 kfree(pwrseq); 506 return ERR_PTR(id); 507 } 508 509 pwrseq->id = id; 510 511 /* 512 * From this point onwards the device's release() callback is 513 * responsible for freeing resources. 514 */ 515 device_initialize(&pwrseq->dev); 516 517 pwrseq->owner = config->owner ?: THIS_MODULE; 518 pwrseq->match = config->match; 519 520 init_rwsem(&pwrseq->rw_lock); 521 mutex_init(&pwrseq->state_lock); 522 INIT_LIST_HEAD(&pwrseq->targets); 523 INIT_LIST_HEAD(&pwrseq->units); 524 525 ret = dev_set_name(&pwrseq->dev, "pwrseq.%d", pwrseq->id); 526 if (ret) 527 goto err_put_pwrseq; 528 529 ret = pwrseq_setup_targets(config->targets, pwrseq); 530 if (ret) 531 goto err_put_pwrseq; 532 533 scoped_guard(rwsem_write, &pwrseq_sem) { 534 ret = device_add(&pwrseq->dev); 535 if (ret) 536 goto err_put_pwrseq; 537 } 538 539 return pwrseq; 540 541 err_put_pwrseq: 542 pwrseq_device_put(pwrseq); 543 return ERR_PTR(ret); 544 } 545 EXPORT_SYMBOL_GPL(pwrseq_device_register); 546 547 /** 548 * pwrseq_device_unregister() - Unregister the power sequencer. 549 * @pwrseq: Power sequencer to unregister. 550 */ 551 void pwrseq_device_unregister(struct pwrseq_device *pwrseq) 552 { 553 struct device *dev = &pwrseq->dev; 554 struct pwrseq_target *target; 555 556 scoped_guard(rwsem_write, &pwrseq_sem) { 557 guard(rwsem_write)(&pwrseq->rw_lock); 558 559 /* 560 * Holding rw_lock for write excludes all power on/off callers 561 * (they hold it for read), so it's safe to read enable_count 562 * here without taking the state_lock. 563 */ 564 list_for_each_entry(target, &pwrseq->targets, list) 565 WARN(target->unit->enable_count, 566 "REMOVING POWER SEQUENCER WITH ACTIVE USERS\n"); 567 568 device_del(dev); 569 } 570 571 pwrseq_device_put(pwrseq); 572 } 573 EXPORT_SYMBOL_GPL(pwrseq_device_unregister); 574 575 static void devm_pwrseq_device_unregister(void *data) 576 { 577 struct pwrseq_device *pwrseq = data; 578 579 pwrseq_device_unregister(pwrseq); 580 } 581 582 /** 583 * devm_pwrseq_device_register() - Managed variant of pwrseq_device_register(). 584 * @dev: Managing device. 585 * @config: Configuration of the new power sequencing device. 586 * 587 * Returns: 588 * Returns the address of the new pwrseq device or ERR_PTR() on failure. 589 */ 590 struct pwrseq_device * 591 devm_pwrseq_device_register(struct device *dev, 592 const struct pwrseq_config *config) 593 { 594 struct pwrseq_device *pwrseq; 595 int ret; 596 597 pwrseq = pwrseq_device_register(config); 598 if (IS_ERR(pwrseq)) 599 return pwrseq; 600 601 ret = devm_add_action_or_reset(dev, devm_pwrseq_device_unregister, 602 pwrseq); 603 if (ret) 604 return ERR_PTR(ret); 605 606 return pwrseq; 607 } 608 EXPORT_SYMBOL_GPL(devm_pwrseq_device_register); 609 610 /** 611 * pwrseq_device_get_drvdata() - Get the driver private data associated with 612 * this sequencer. 613 * @pwrseq: Power sequencer object. 614 * 615 * Returns: 616 * Address of the private driver data. 617 */ 618 void *pwrseq_device_get_drvdata(struct pwrseq_device *pwrseq) 619 { 620 return dev_get_drvdata(&pwrseq->dev); 621 } 622 EXPORT_SYMBOL_GPL(pwrseq_device_get_drvdata); 623 624 struct pwrseq_match_data { 625 struct pwrseq_desc *desc; 626 struct device *dev; 627 const char *target; 628 }; 629 630 static int pwrseq_match_device(struct device *pwrseq_dev, void *data) 631 { 632 struct pwrseq_device *pwrseq = to_pwrseq_device(pwrseq_dev); 633 struct pwrseq_match_data *match_data = data; 634 struct pwrseq_target *target; 635 int ret; 636 637 lockdep_assert_held_read(&pwrseq_sem); 638 639 guard(rwsem_read)(&pwrseq->rw_lock); 640 if (!device_is_registered(&pwrseq->dev)) 641 return 0; 642 643 ret = pwrseq->match(pwrseq, match_data->dev); 644 if (ret == PWRSEQ_NO_MATCH || ret < 0) 645 return ret; 646 647 /* We got the matching device, let's find the right target. */ 648 list_for_each_entry(target, &pwrseq->targets, list) { 649 if (strcmp(target->name, match_data->target)) 650 continue; 651 652 match_data->desc->target = target; 653 } 654 655 /* 656 * This device does not have this target. No point in deferring as it 657 * will not get a new target dynamically later. 658 */ 659 if (!match_data->desc->target) 660 return -ENOENT; 661 662 if (!try_module_get(pwrseq->owner)) 663 return -EPROBE_DEFER; 664 665 match_data->desc->pwrseq = pwrseq_device_get(pwrseq); 666 667 return PWRSEQ_MATCH_OK; 668 } 669 670 /** 671 * pwrseq_get() - Get the power sequencer associated with this device. 672 * @dev: Device for which to get the sequencer. 673 * @target: Name of the target exposed by the sequencer this device wants to 674 * reach. 675 * 676 * Returns: 677 * New power sequencer descriptor for use by the consumer driver or ERR_PTR() 678 * on failure. 679 */ 680 struct pwrseq_desc *pwrseq_get(struct device *dev, const char *target) 681 { 682 struct pwrseq_match_data match_data; 683 int ret; 684 685 struct pwrseq_desc *desc __free(kfree) = kzalloc_obj(*desc); 686 if (!desc) 687 return ERR_PTR(-ENOMEM); 688 689 match_data.desc = desc; 690 match_data.dev = dev; 691 match_data.target = target; 692 693 guard(rwsem_read)(&pwrseq_sem); 694 695 ret = bus_for_each_dev(&pwrseq_bus, NULL, &match_data, 696 pwrseq_match_device); 697 if (ret < 0) 698 return ERR_PTR(ret); 699 if (ret == PWRSEQ_NO_MATCH) 700 /* No device matched. */ 701 return ERR_PTR(-EPROBE_DEFER); 702 703 return_ptr(desc); 704 } 705 EXPORT_SYMBOL_GPL(pwrseq_get); 706 707 /** 708 * pwrseq_put() - Release the power sequencer descriptor. 709 * @desc: Descriptor to release. 710 */ 711 void pwrseq_put(struct pwrseq_desc *desc) 712 { 713 struct pwrseq_device *pwrseq; 714 715 if (!desc) 716 return; 717 718 pwrseq = desc->pwrseq; 719 720 if (desc->powered_on) 721 pwrseq_disable(desc); 722 723 kfree(desc); 724 module_put(pwrseq->owner); 725 pwrseq_device_put(pwrseq); 726 } 727 EXPORT_SYMBOL_GPL(pwrseq_put); 728 729 static void devm_pwrseq_put(void *data) 730 { 731 struct pwrseq_desc *desc = data; 732 733 pwrseq_put(desc); 734 } 735 736 /** 737 * devm_pwrseq_get() - Managed variant of pwrseq_get(). 738 * @dev: Device for which to get the sequencer and which also manages its 739 * lifetime. 740 * @target: Name of the target exposed by the sequencer this device wants to 741 * reach. 742 * 743 * Returns: 744 * New power sequencer descriptor for use by the consumer driver or ERR_PTR() 745 * on failure. 746 */ 747 struct pwrseq_desc *devm_pwrseq_get(struct device *dev, const char *target) 748 { 749 struct pwrseq_desc *desc; 750 int ret; 751 752 desc = pwrseq_get(dev, target); 753 if (IS_ERR(desc)) 754 return desc; 755 756 ret = devm_add_action_or_reset(dev, devm_pwrseq_put, desc); 757 if (ret) 758 return ERR_PTR(ret); 759 760 return desc; 761 } 762 EXPORT_SYMBOL_GPL(devm_pwrseq_get); 763 764 static int pwrseq_unit_enable(struct pwrseq_device *pwrseq, 765 struct pwrseq_unit *target); 766 static int pwrseq_unit_disable(struct pwrseq_device *pwrseq, 767 struct pwrseq_unit *target); 768 769 static int pwrseq_unit_enable_deps(struct pwrseq_device *pwrseq, 770 struct list_head *list) 771 { 772 struct pwrseq_unit_dep *pos; 773 int ret = 0; 774 775 list_for_each_entry(pos, list, list) { 776 ret = pwrseq_unit_enable(pwrseq, pos->unit); 777 if (ret) { 778 list_for_each_entry_continue_reverse(pos, list, list) 779 pwrseq_unit_disable(pwrseq, pos->unit); 780 break; 781 } 782 } 783 784 return ret; 785 } 786 787 static int pwrseq_unit_disable_deps(struct pwrseq_device *pwrseq, 788 struct list_head *list) 789 { 790 struct pwrseq_unit_dep *pos; 791 int ret = 0; 792 793 list_for_each_entry_reverse(pos, list, list) { 794 ret = pwrseq_unit_disable(pwrseq, pos->unit); 795 if (ret) { 796 list_for_each_entry_continue(pos, list, list) 797 pwrseq_unit_enable(pwrseq, pos->unit); 798 break; 799 } 800 } 801 802 return ret; 803 } 804 805 static int pwrseq_unit_enable(struct pwrseq_device *pwrseq, 806 struct pwrseq_unit *unit) 807 { 808 int ret; 809 810 lockdep_assert_held_read(&pwrseq->rw_lock); 811 lockdep_assert_held(&pwrseq->state_lock); 812 813 if (unit->enable_count != 0) { 814 unit->enable_count++; 815 return 0; 816 } 817 818 ret = pwrseq_unit_enable_deps(pwrseq, &unit->deps); 819 if (ret) { 820 dev_err(&pwrseq->dev, 821 "Failed to enable dependencies before power-on for target '%s': %d\n", 822 unit->name, ret); 823 return ret; 824 } 825 826 if (unit->enable) { 827 ret = unit->enable(pwrseq); 828 if (ret) { 829 dev_err(&pwrseq->dev, 830 "Failed to enable target '%s': %d\n", 831 unit->name, ret); 832 pwrseq_unit_disable_deps(pwrseq, &unit->deps); 833 return ret; 834 } 835 } 836 837 unit->enable_count++; 838 839 return 0; 840 } 841 842 static int pwrseq_unit_disable(struct pwrseq_device *pwrseq, 843 struct pwrseq_unit *unit) 844 { 845 int ret; 846 847 lockdep_assert_held_read(&pwrseq->rw_lock); 848 lockdep_assert_held(&pwrseq->state_lock); 849 850 if (unit->enable_count == 0) { 851 WARN(1, "Unmatched power-off for target '%s'\n", 852 unit->name); 853 return -EBUSY; 854 } 855 856 if (unit->enable_count != 1) { 857 unit->enable_count--; 858 return 0; 859 } 860 861 if (unit->disable) { 862 ret = unit->disable(pwrseq); 863 if (ret) { 864 dev_err(&pwrseq->dev, 865 "Failed to disable target '%s': %d\n", 866 unit->name, ret); 867 return ret; 868 } 869 } 870 871 ret = pwrseq_unit_disable_deps(pwrseq, &unit->deps); 872 if (ret) { 873 dev_err(&pwrseq->dev, 874 "Failed to disable dependencies after power-off for target '%s': %d\n", 875 unit->name, ret); 876 if (unit->enable) 877 unit->enable(pwrseq); 878 return ret; 879 } 880 881 unit->enable_count--; 882 883 return 0; 884 } 885 886 /** 887 * pwrseq_enable() - Issue a power-on request on behalf of the consumer 888 * device. 889 * @desc: Descriptor referencing the power sequencer. 890 * 891 * This function tells the power sequencer that the consumer wants to be 892 * powered-up. The sequencer may already have powered-up the device in which 893 * case the function returns 0. If the power-up sequence is already in 894 * progress, the function will block until it's done and return 0. If this is 895 * the first request, the device will be powered up. 896 * 897 * Returns: 898 * 0 on success, negative error number on failure. 899 */ 900 int pwrseq_enable(struct pwrseq_desc *desc) 901 { 902 struct pwrseq_device *pwrseq; 903 struct pwrseq_target *target; 904 struct pwrseq_unit *unit; 905 int ret; 906 907 might_sleep(); 908 909 if (!desc || desc->powered_on) 910 return 0; 911 912 pwrseq = desc->pwrseq; 913 target = desc->target; 914 unit = target->unit; 915 916 guard(rwsem_read)(&pwrseq->rw_lock); 917 if (!device_is_registered(&pwrseq->dev)) 918 return -ENODEV; 919 920 scoped_guard(mutex, &pwrseq->state_lock) { 921 ret = pwrseq_unit_enable(pwrseq, unit); 922 if (!ret) 923 desc->powered_on = true; 924 } 925 if (ret) 926 return ret; 927 928 if (target->post_enable) { 929 ret = target->post_enable(pwrseq); 930 if (ret) { 931 scoped_guard(mutex, &pwrseq->state_lock) { 932 pwrseq_unit_disable(pwrseq, unit); 933 desc->powered_on = false; 934 } 935 } 936 } 937 938 return ret; 939 } 940 EXPORT_SYMBOL_GPL(pwrseq_enable); 941 942 /** 943 * pwrseq_disable() - Issue a power-off request on behalf of the consumer 944 * device. 945 * @desc: Descriptor referencing the power sequencer. 946 * 947 * This undoes the effects of pwrseq_enable(). It issues a power-off request 948 * on behalf of the consumer and when the last remaining user does so, the 949 * power-down sequence will be started. If one is in progress, the function 950 * will block until it's complete and then return. 951 * 952 * Returns: 953 * 0 on success, negative error number on failure. 954 */ 955 int pwrseq_disable(struct pwrseq_desc *desc) 956 { 957 struct pwrseq_device *pwrseq; 958 struct pwrseq_unit *unit; 959 int ret; 960 961 might_sleep(); 962 963 if (!desc || !desc->powered_on) 964 return 0; 965 966 pwrseq = desc->pwrseq; 967 unit = desc->target->unit; 968 969 guard(rwsem_read)(&pwrseq->rw_lock); 970 if (!device_is_registered(&pwrseq->dev)) 971 return -ENODEV; 972 973 guard(mutex)(&pwrseq->state_lock); 974 975 ret = pwrseq_unit_disable(pwrseq, unit); 976 if (!ret) 977 desc->powered_on = false; 978 979 return ret; 980 } 981 EXPORT_SYMBOL_GPL(pwrseq_disable); 982 983 /** 984 * pwrseq_to_device() - Get the pwrseq device pointer from a descriptor. 985 * @desc: Descriptor referencing the power sequencer. 986 * 987 * Return the 'dev' pointer of the power sequencer device associated with @desc. 988 * Consumer drivers can use this to query the pwrseq provider's device tree 989 * node, for example to check for the existence of specific properties. 990 * 991 * Since pwrseq_get() already takes a reference to the pwrseq device, this 992 * function does not take an additional reference. 993 * 994 * Returns: 995 * Pointer to the pwrseq struct device, or NULL if @desc is NULL. 996 */ 997 struct device *pwrseq_to_device(struct pwrseq_desc *desc) 998 { 999 if (!desc) 1000 return NULL; 1001 1002 return &desc->pwrseq->dev; 1003 } 1004 EXPORT_SYMBOL_GPL(pwrseq_to_device); 1005 1006 /** 1007 * pwrseq_is_controllable() - Check whether the target provides a 1008 * host-controllable power actuator. 1009 * @desc: Descriptor referencing the power sequencer. 1010 * 1011 * Some power sequencing targets provide no host-controllable enable for their 1012 * function on a given board, for instance when the enable line is not wired up 1013 * and is instead hardwired to an always-on level. For such targets a call to 1014 * pwrseq_power_off() is still allowed, so that the consumer can drop its vote 1015 * on the (possibly shared) resources, but the host cannot gate the function 1016 * on its own. 1017 * 1018 * Returns: 1019 * True if the target provides a host-controllable power actuator, false 1020 * otherwise. Also returns false if @desc is NULL. 1021 */ 1022 bool pwrseq_is_controllable(struct pwrseq_desc *desc) 1023 { 1024 struct pwrseq_device *pwrseq; 1025 struct pwrseq_target *target; 1026 struct pwrseq_unit *unit; 1027 1028 might_sleep(); 1029 1030 if (!desc) 1031 return false; 1032 1033 pwrseq = desc->pwrseq; 1034 target = desc->target; 1035 unit = target->unit; 1036 1037 guard(rwsem_read)(&pwrseq->rw_lock); 1038 if (!device_is_registered(&pwrseq->dev)) 1039 return false; 1040 1041 if (!unit->enable && !unit->disable) 1042 return false; 1043 1044 if (!target->is_controllable) 1045 return true; 1046 1047 return target->is_controllable(pwrseq); 1048 } 1049 EXPORT_SYMBOL_GPL(pwrseq_is_controllable); 1050 1051 #if IS_ENABLED(CONFIG_DEBUG_FS) 1052 1053 struct pwrseq_debugfs_count_ctx { 1054 struct device *dev; 1055 loff_t index; 1056 }; 1057 1058 static int pwrseq_debugfs_seq_count(struct device *dev, void *data) 1059 { 1060 struct pwrseq_debugfs_count_ctx *ctx = data; 1061 1062 ctx->dev = dev; 1063 1064 return ctx->index-- ? 0 : 1; 1065 } 1066 1067 static void *pwrseq_debugfs_seq_start(struct seq_file *seq, loff_t *pos) 1068 { 1069 struct pwrseq_debugfs_count_ctx ctx; 1070 1071 ctx.dev = NULL; 1072 ctx.index = *pos; 1073 1074 /* 1075 * Hold the lock for the entire printout to prevent device removal. 1076 * Reference counts are managed by start()/next()/stop() as required 1077 * by the seq_file contract. 1078 */ 1079 down_read(&pwrseq_sem); 1080 1081 bus_for_each_dev(&pwrseq_bus, NULL, &ctx, pwrseq_debugfs_seq_count); 1082 if (!ctx.index) 1083 return NULL; 1084 1085 return get_device(ctx.dev); 1086 } 1087 1088 static void *pwrseq_debugfs_seq_next(struct seq_file *seq, void *data, 1089 loff_t *pos) 1090 { 1091 struct device *curr = data; 1092 1093 ++*pos; 1094 1095 struct device *next = bus_find_next_device(&pwrseq_bus, curr); 1096 1097 put_device(curr); 1098 return next; 1099 } 1100 1101 static void pwrseq_debugfs_seq_show_target(struct seq_file *seq, 1102 struct pwrseq_target *target) 1103 { 1104 seq_printf(seq, " target: [%s] (target unit: [%s])\n", 1105 target->name, target->unit->name); 1106 } 1107 1108 static void pwrseq_debugfs_seq_show_unit(struct seq_file *seq, 1109 struct pwrseq_unit *unit) 1110 { 1111 struct pwrseq_unit_dep *ref; 1112 1113 seq_printf(seq, " unit: [%s] - enable count: %u\n", 1114 unit->name, unit->enable_count); 1115 1116 if (list_empty(&unit->deps)) 1117 return; 1118 1119 seq_puts(seq, " dependencies:\n"); 1120 list_for_each_entry(ref, &unit->deps, list) 1121 seq_printf(seq, " [%s]\n", ref->unit->name); 1122 } 1123 1124 static int pwrseq_debugfs_seq_show(struct seq_file *seq, void *data) 1125 { 1126 struct device *dev = data; 1127 struct pwrseq_device *pwrseq = to_pwrseq_device(dev); 1128 struct pwrseq_target *target; 1129 struct pwrseq_unit *unit; 1130 1131 seq_printf(seq, "%s (%s):\n", dev_name(dev), dev_name(dev->parent)); 1132 1133 seq_puts(seq, " targets:\n"); 1134 list_for_each_entry(target, &pwrseq->targets, list) 1135 pwrseq_debugfs_seq_show_target(seq, target); 1136 1137 seq_puts(seq, " units:\n"); 1138 list_for_each_entry(unit, &pwrseq->units, list) 1139 pwrseq_debugfs_seq_show_unit(seq, unit); 1140 1141 return 0; 1142 } 1143 1144 static void pwrseq_debugfs_seq_stop(struct seq_file *seq, void *data) 1145 { 1146 if (data) 1147 put_device(data); 1148 up_read(&pwrseq_sem); 1149 } 1150 1151 static const struct seq_operations pwrseq_debugfs_sops = { 1152 .start = pwrseq_debugfs_seq_start, 1153 .next = pwrseq_debugfs_seq_next, 1154 .show = pwrseq_debugfs_seq_show, 1155 .stop = pwrseq_debugfs_seq_stop, 1156 }; 1157 DEFINE_SEQ_ATTRIBUTE(pwrseq_debugfs); 1158 1159 static struct dentry *pwrseq_debugfs_dentry; 1160 1161 #endif /* CONFIG_DEBUG_FS */ 1162 1163 static int __init pwrseq_init(void) 1164 { 1165 int ret; 1166 1167 ret = bus_register(&pwrseq_bus); 1168 if (ret) { 1169 pr_err("Failed to register the power sequencer bus\n"); 1170 return ret; 1171 } 1172 1173 #if IS_ENABLED(CONFIG_DEBUG_FS) 1174 pwrseq_debugfs_dentry = debugfs_create_file("pwrseq", 0444, NULL, NULL, 1175 &pwrseq_debugfs_fops); 1176 #endif /* CONFIG_DEBUG_FS */ 1177 1178 return 0; 1179 } 1180 subsys_initcall(pwrseq_init); 1181 1182 static void __exit pwrseq_exit(void) 1183 { 1184 #if IS_ENABLED(CONFIG_DEBUG_FS) 1185 debugfs_remove_recursive(pwrseq_debugfs_dentry); 1186 #endif /* CONFIG_DEBUG_FS */ 1187 1188 bus_unregister(&pwrseq_bus); 1189 } 1190 module_exit(pwrseq_exit); 1191 1192 MODULE_AUTHOR("Bartosz Golaszewski <bartosz.golaszewski@linaro.org>"); 1193 MODULE_DESCRIPTION("Power Sequencing subsystem core"); 1194 MODULE_LICENSE("GPL"); 1195