summaryrefslogtreecommitdiff
path: root/drivers/power/sequencing
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2026-11-21 17:22:21 -0800
committerLinus Torvalds <torvalds@linux-foundation.org>2026-11-21 17:22:21 -0800
commit4843f6873f7c4835e62f2c73ba0fd538e57a2f22 (patch)
treeee620501a1ffbad9a83141e3a70154e1fe641040 /drivers/power/sequencing
downloadlinux-master.tar.gz
linux-master.zip
Merge tag 'sysctl-7.04-fixes-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/sysctl/sysctlgraftedHEADmaster
Pull sysctl fix from Joel Granados: "Fix sysctl jiffy / milliseconds conversion: - Writing a previously read value "X" shall keep the same "X" value - Remove truncation that was causing incorrect saturation of valid timeouts" * tag 'sysctl-7.04-fixes-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/sysctl/sysctl: sysctl: Do jiffy / ms conversion in ulong space
Diffstat (limited to 'drivers/power/sequencing')
-rw-r--r--drivers/power/sequencing/Kconfig69
-rw-r--r--drivers/power/sequencing/Makefile10
-rw-r--r--drivers/power/sequencing/core.c1194
-rw-r--r--drivers/power/sequencing/pwrseq-kunit.c1497
-rw-r--r--drivers/power/sequencing/pwrseq-pcie-m2.c653
-rw-r--r--drivers/power/sequencing/pwrseq-qcom-wcn.c613
-rw-r--r--drivers/power/sequencing/pwrseq-renesas-pwrrdy.c142
-rw-r--r--drivers/power/sequencing/pwrseq-thead-gpu.c249
8 files changed, 4427 insertions, 0 deletions
diff --git a/drivers/power/sequencing/Kconfig b/drivers/power/sequencing/Kconfig
new file mode 100644
index 000000000..b6a9f25fe
--- /dev/null
+++ b/drivers/power/sequencing/Kconfig
@@ -0,0 +1,69 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+menuconfig POWER_SEQUENCING
+ tristate "Power Sequencing support"
+ help
+ Say Y here to enable the Power Sequencing subsystem.
+
+ This subsystem is designed to control power to devices that share
+ complex resources and/or require specific power sequences to be run
+ during power-up.
+
+ If unsure, say no.
+
+if POWER_SEQUENCING
+
+config POWER_SEQUENCING_QCOM_WCN
+ tristate "Qualcomm WCN family PMU driver"
+ default m if ARCH_QCOM
+ depends on OF || COMPILE_TEST
+ help
+ Say Y here to enable the power sequencing driver for Qualcomm
+ WCN Bluetooth/WLAN chipsets.
+
+ Typically, a package from the Qualcomm WCN family contains the BT
+ and WLAN modules whose power is controlled by the PMU module. As the
+ former two share the power-up sequence which is executed by the PMU,
+ this driver is needed for correct power control or else we'd risk not
+ respecting the required delays between enabling Bluetooth and WLAN.
+
+config POWER_SEQUENCING_RENESAS_PWRRDY
+ tristate "Renesas Power Ready sequencing driver"
+ depends on SYSC_RZ || COMPILE_TEST
+ select AUXILIARY_BUS
+ help
+ Say Y here to enable the power sequencing driver for the Renesas
+ Power Ready signals. This driver handles the power ready signals
+ required to power on the various IP's on RZ/G3L platform.
+
+config POWER_SEQUENCING_TH1520_GPU
+ tristate "T-HEAD TH1520 GPU power sequencing driver"
+ depends on ARCH_THEAD || COMPILE_TEST
+ select AUXILIARY_BUS
+ help
+ Say Y here to enable the power sequencing driver for the TH1520 SoC
+ GPU. This driver handles the complex clock and reset sequence
+ required to power on the Imagination BXM GPU on this platform.
+
+config POWER_SEQUENCING_PCIE_M2
+ tristate "PCIe M.2 connector power sequencing driver"
+ depends on OF
+ depends on PCI
+ depends on SERIAL_DEV_BUS
+ select OF_DYNAMIC
+ help
+ Say Y here to enable the power sequencing driver for PCIe M.2
+ connectors. This driver handles the power sequencing for the M.2
+ connectors exposing multiple interfaces like PCIe, SATA, UART, etc...
+
+config POWER_SEQUENCING_KUNIT
+ tristate "Build power sequencing KUnit test cases"
+ depends on KUNIT
+ default KUNIT_ALL_TESTS
+ help
+ Say Y here to build the module containing KUnit test cases for the
+ power sequencing subsystem.
+
+ If unsure, say N.
+
+endif
diff --git a/drivers/power/sequencing/Makefile b/drivers/power/sequencing/Makefile
new file mode 100644
index 000000000..76a2775fa
--- /dev/null
+++ b/drivers/power/sequencing/Makefile
@@ -0,0 +1,10 @@
+# SPDX-License-Identifier: GPL-2.0
+
+obj-$(CONFIG_POWER_SEQUENCING) += pwrseq-core.o
+pwrseq-core-y := core.o
+
+obj-$(CONFIG_POWER_SEQUENCING_QCOM_WCN) += pwrseq-qcom-wcn.o
+obj-$(CONFIG_POWER_SEQUENCING_RENESAS_PWRRDY) += pwrseq-renesas-pwrrdy.o
+obj-$(CONFIG_POWER_SEQUENCING_TH1520_GPU) += pwrseq-thead-gpu.o
+obj-$(CONFIG_POWER_SEQUENCING_PCIE_M2) += pwrseq-pcie-m2.o
+obj-$(CONFIG_POWER_SEQUENCING_KUNIT) += pwrseq-kunit.o
diff --git a/drivers/power/sequencing/core.c b/drivers/power/sequencing/core.c
new file mode 100644
index 000000000..a40021526
--- /dev/null
+++ b/drivers/power/sequencing/core.c
@@ -0,0 +1,1194 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2024 Linaro Ltd.
+ */
+
+#include <linux/bug.h>
+#include <linux/cleanup.h>
+#include <linux/debugfs.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/export.h>
+#include <linux/idr.h>
+#include <linux/kernel.h>
+#include <linux/kref.h>
+#include <linux/list.h>
+#include <linux/lockdep.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/property.h>
+#include <linux/pwrseq/consumer.h>
+#include <linux/pwrseq/provider.h>
+#include <linux/radix-tree.h>
+#include <linux/rwsem.h>
+#include <linux/slab.h>
+
+/*
+ * Power-sequencing framework for linux.
+ *
+ * This subsystem allows power sequence providers to register a set of targets
+ * that consumers may request and power-up/down.
+ *
+ * Glossary:
+ *
+ * Unit - a unit is a discreet chunk of a power sequence. For instance one unit
+ * may enable a set of regulators, another may enable a specific GPIO. Units
+ * can define dependencies in the form of other units that must be enabled
+ * before it itself can be.
+ *
+ * Target - a target is a set of units (composed of the "final" unit and its
+ * dependencies) that a consumer selects by its name when requesting a handle
+ * to the power sequencer. Via the dependency system, multiple targets may
+ * share the same parts of a power sequence but ignore parts that are
+ * irrelevant.
+ *
+ * Descriptor - a handle passed by the pwrseq core to every consumer that
+ * serves as the entry point to the provider layer. It ensures coherence
+ * between different users and keeps reference counting consistent.
+ *
+ * Each provider must define a .match() callback whose role is to determine
+ * whether a potential consumer is in fact associated with this sequencer.
+ * This allows creating abstraction layers on top of regular device-tree
+ * resources like regulators, clocks and other nodes connected to the consumer
+ * via phandle.
+ */
+
+static DEFINE_IDA(pwrseq_ida);
+
+/*
+ * Protects the device list on the pwrseq bus from concurrent modifications
+ * but allows simultaneous read-only access.
+ */
+static DECLARE_RWSEM(pwrseq_sem);
+
+/**
+ * struct pwrseq_unit - Private power-sequence unit data.
+ * @ref: Reference count for this object. When it goes to 0, the object is
+ * destroyed.
+ * @name: Name of this target.
+ * @list: Link to siblings on the list of all units of a single sequencer.
+ * @deps: List of units on which this unit depends.
+ * @enable: Callback running the part of the power-on sequence provided by
+ * this unit.
+ * @disable: Callback running the part of the power-off sequence provided
+ * by this unit.
+ * @enable_count: Current number of users that enabled this unit. May be the
+ * consumer of the power sequencer or other units that depend
+ * on this one.
+ */
+struct pwrseq_unit {
+ struct kref ref;
+ const char *name;
+ struct list_head list;
+ struct list_head deps;
+ pwrseq_power_state_func enable;
+ pwrseq_power_state_func disable;
+ unsigned int enable_count;
+};
+
+static struct pwrseq_unit *pwrseq_unit_new(const struct pwrseq_unit_data *data)
+{
+ struct pwrseq_unit *unit;
+
+ unit = kzalloc_obj(*unit);
+ if (!unit)
+ return NULL;
+
+ unit->name = kstrdup_const(data->name, GFP_KERNEL);
+ if (!unit->name) {
+ kfree(unit);
+ return NULL;
+ }
+
+ kref_init(&unit->ref);
+ INIT_LIST_HEAD(&unit->list);
+ INIT_LIST_HEAD(&unit->deps);
+ unit->enable = data->enable;
+ unit->disable = data->disable;
+
+ return unit;
+}
+
+static struct pwrseq_unit *pwrseq_unit_get(struct pwrseq_unit *unit)
+{
+ kref_get(&unit->ref);
+
+ return unit;
+}
+
+static void pwrseq_unit_release(struct kref *ref);
+
+static void pwrseq_unit_put(struct pwrseq_unit *unit)
+{
+ kref_put(&unit->ref, pwrseq_unit_release);
+}
+
+/**
+ * struct pwrseq_unit_dep - Wrapper around a reference to the unit structure
+ * allowing to keep it on multiple dependency lists
+ * in different units.
+ * @list: Siblings on the list.
+ * @unit: Address of the referenced unit.
+ */
+struct pwrseq_unit_dep {
+ struct list_head list;
+ struct pwrseq_unit *unit;
+};
+
+static struct pwrseq_unit_dep *pwrseq_unit_dep_new(struct pwrseq_unit *unit)
+{
+ struct pwrseq_unit_dep *dep;
+
+ dep = kzalloc_obj(*dep);
+ if (!dep)
+ return NULL;
+
+ dep->unit = unit;
+
+ return dep;
+}
+
+static void pwrseq_unit_dep_free(struct pwrseq_unit_dep *ref)
+{
+ pwrseq_unit_put(ref->unit);
+ kfree(ref);
+}
+
+static void pwrseq_unit_free_deps(struct list_head *list)
+{
+ struct pwrseq_unit_dep *dep, *next;
+
+ list_for_each_entry_safe(dep, next, list, list) {
+ list_del(&dep->list);
+ pwrseq_unit_dep_free(dep);
+ }
+}
+
+static void pwrseq_unit_release(struct kref *ref)
+{
+ struct pwrseq_unit *unit = container_of(ref, struct pwrseq_unit, ref);
+
+ pwrseq_unit_free_deps(&unit->deps);
+ list_del(&unit->list);
+ kfree_const(unit->name);
+ kfree(unit);
+}
+
+/**
+ * struct pwrseq_target - Private power-sequence target data.
+ * @list: Siblings on the list of all targets exposed by a power sequencer.
+ * @name: Name of the target.
+ * @unit: Final unit for this target.
+ * @post_enable: Callback run after the target unit has been enabled, *after*
+ * the state lock has been released. It's useful for implementing
+ * boot-up delays without blocking other users from powering up
+ * using the same power sequencer.
+ * @is_controllable: Optional callback reporting whether enabling/disabling
+ * this target actually controls power.
+ */
+struct pwrseq_target {
+ struct list_head list;
+ const char *name;
+ struct pwrseq_unit *unit;
+ pwrseq_power_state_func post_enable;
+ pwrseq_is_controllable_func is_controllable;
+};
+
+static struct pwrseq_target *
+pwrseq_target_new(const struct pwrseq_target_data *data)
+{
+ struct pwrseq_target *target;
+
+ target = kzalloc_obj(*target);
+ if (!target)
+ return NULL;
+
+ target->name = kstrdup_const(data->name, GFP_KERNEL);
+ if (!target->name) {
+ kfree(target);
+ return NULL;
+ }
+
+ target->post_enable = data->post_enable;
+ target->is_controllable = data->is_controllable;
+
+ return target;
+}
+
+static void pwrseq_target_free(struct pwrseq_target *target)
+{
+ if (!IS_ERR_OR_NULL(target->unit))
+ pwrseq_unit_put(target->unit);
+ kfree_const(target->name);
+ kfree(target);
+}
+
+/**
+ * struct pwrseq_device - Private power sequencing data.
+ * @dev: Device struct associated with this sequencer.
+ * @id: Device ID.
+ * @owner: Prevents removal of active power sequencing providers.
+ * @rw_lock: Protects the device from being unregistered while in use.
+ * @state_lock: Prevents multiple users running the power sequence at the same
+ * time.
+ * @match: Power sequencer matching callback.
+ * @targets: List of targets exposed by this sequencer.
+ * @units: List of all units supported by this sequencer.
+ */
+struct pwrseq_device {
+ struct device dev;
+ int id;
+ struct module *owner;
+ struct rw_semaphore rw_lock;
+ struct mutex state_lock;
+ pwrseq_match_func match;
+ struct list_head targets;
+ struct list_head units;
+};
+
+static struct pwrseq_device *to_pwrseq_device(struct device *dev)
+{
+ return container_of(dev, struct pwrseq_device, dev);
+}
+
+static struct pwrseq_device *pwrseq_device_get(struct pwrseq_device *pwrseq)
+{
+ get_device(&pwrseq->dev);
+
+ return pwrseq;
+}
+
+static void pwrseq_device_put(struct pwrseq_device *pwrseq)
+{
+ put_device(&pwrseq->dev);
+}
+
+/**
+ * struct pwrseq_desc - Wraps access to the pwrseq_device and ensures that one
+ * user cannot break the reference counting for others.
+ * @pwrseq: Reference to the power sequencing device.
+ * @target: Reference to the target this descriptor allows to control.
+ * @powered_on: Power state set by the holder of the descriptor (not necessarily
+ * corresponding to the actual power state of the device).
+ *
+ * Note: The user that requested the descriptor from the provider is
+ * responsible for the serialization of access to it. Concurrent threads must
+ * not access the descriptor structure without synchronization as power
+ * sequencing core does not provide any per-descriptor locking.
+ */
+struct pwrseq_desc {
+ struct pwrseq_device *pwrseq;
+ struct pwrseq_target *target;
+ bool powered_on;
+};
+
+static const struct bus_type pwrseq_bus = {
+ .name = "pwrseq",
+};
+
+static void pwrseq_release(struct device *dev)
+{
+ struct pwrseq_device *pwrseq = to_pwrseq_device(dev);
+ struct pwrseq_target *target, *pos;
+
+ list_for_each_entry_safe(target, pos, &pwrseq->targets, list) {
+ list_del(&target->list);
+ pwrseq_target_free(target);
+ }
+
+ mutex_destroy(&pwrseq->state_lock);
+ ida_free(&pwrseq_ida, pwrseq->id);
+ kfree(pwrseq);
+}
+
+static const struct device_type pwrseq_device_type = {
+ .name = "power_sequencer",
+ .release = pwrseq_release,
+};
+
+static int pwrseq_check_unit_deps(const struct pwrseq_unit_data *data,
+ struct radix_tree_root *visited_units)
+{
+ const struct pwrseq_unit_data *tmp, **cur;
+ int ret;
+
+ ret = radix_tree_insert(visited_units, (unsigned long)data,
+ (void *)data);
+ if (ret)
+ return ret;
+
+ for (cur = data->deps; cur && *cur; cur++) {
+ tmp = radix_tree_lookup(visited_units, (unsigned long)*cur);
+ if (tmp) {
+ WARN(1, "Circular dependency in power sequencing flow detected!\n");
+ return -EINVAL;
+ }
+
+ ret = pwrseq_check_unit_deps(*cur, visited_units);
+ if (ret)
+ return ret;
+ }
+
+ return 0;
+}
+
+static int pwrseq_check_target_deps(const struct pwrseq_target_data *data)
+{
+ struct radix_tree_root visited_units;
+ struct radix_tree_iter iter;
+ void __rcu **slot;
+ int ret;
+
+ if (!data->unit)
+ return -EINVAL;
+
+ INIT_RADIX_TREE(&visited_units, GFP_KERNEL);
+ ret = pwrseq_check_unit_deps(data->unit, &visited_units);
+ radix_tree_for_each_slot(slot, &visited_units, &iter, 0)
+ radix_tree_delete(&visited_units, iter.index);
+
+ return ret;
+}
+
+static int pwrseq_unit_setup_deps(const struct pwrseq_unit_data **data,
+ struct list_head *dep_list,
+ struct list_head *unit_list,
+ struct radix_tree_root *processed_units);
+
+static struct pwrseq_unit *
+pwrseq_unit_setup(const struct pwrseq_unit_data *data,
+ struct list_head *unit_list,
+ struct radix_tree_root *processed_units)
+{
+ struct pwrseq_unit *unit;
+ int ret;
+
+ unit = radix_tree_lookup(processed_units, (unsigned long)data);
+ if (unit)
+ return pwrseq_unit_get(unit);
+
+ unit = pwrseq_unit_new(data);
+ if (!unit)
+ return ERR_PTR(-ENOMEM);
+
+ if (data->deps) {
+ ret = pwrseq_unit_setup_deps(data->deps, &unit->deps,
+ unit_list, processed_units);
+ if (ret) {
+ pwrseq_unit_put(unit);
+ return ERR_PTR(ret);
+ }
+ }
+
+ ret = radix_tree_insert(processed_units, (unsigned long)data, unit);
+ if (ret) {
+ pwrseq_unit_put(unit);
+ return ERR_PTR(ret);
+ }
+
+ list_add_tail(&unit->list, unit_list);
+
+ return unit;
+}
+
+static int pwrseq_unit_setup_deps(const struct pwrseq_unit_data **data,
+ struct list_head *dep_list,
+ struct list_head *unit_list,
+ struct radix_tree_root *processed_units)
+{
+ const struct pwrseq_unit_data *pos;
+ struct pwrseq_unit_dep *dep;
+ struct pwrseq_unit *unit;
+ int i;
+
+ for (i = 0; data[i]; i++) {
+ pos = data[i];
+
+ unit = pwrseq_unit_setup(pos, unit_list, processed_units);
+ if (IS_ERR(unit))
+ return PTR_ERR(unit);
+
+ dep = pwrseq_unit_dep_new(unit);
+ if (!dep) {
+ pwrseq_unit_put(unit);
+ return -ENOMEM;
+ }
+
+ list_add_tail(&dep->list, dep_list);
+ }
+
+ return 0;
+}
+
+static int pwrseq_do_setup_targets(const struct pwrseq_target_data **data,
+ struct pwrseq_device *pwrseq,
+ struct radix_tree_root *processed_units)
+{
+ const struct pwrseq_target_data *pos;
+ struct pwrseq_target *target;
+ int ret, i;
+
+ for (i = 0; data[i]; i++) {
+ pos = data[i];
+
+ ret = pwrseq_check_target_deps(pos);
+ if (ret)
+ return ret;
+
+ target = pwrseq_target_new(pos);
+ if (!target)
+ return -ENOMEM;
+
+ target->unit = pwrseq_unit_setup(pos->unit, &pwrseq->units,
+ processed_units);
+ if (IS_ERR(target->unit)) {
+ ret = PTR_ERR(target->unit);
+ pwrseq_target_free(target);
+ return ret;
+ }
+
+ list_add_tail(&target->list, &pwrseq->targets);
+ }
+
+ return 0;
+}
+
+static int pwrseq_setup_targets(const struct pwrseq_target_data **targets,
+ struct pwrseq_device *pwrseq)
+{
+ struct radix_tree_root processed_units;
+ struct radix_tree_iter iter;
+ void __rcu **slot;
+ int ret;
+
+ INIT_RADIX_TREE(&processed_units, GFP_KERNEL);
+ ret = pwrseq_do_setup_targets(targets, pwrseq, &processed_units);
+ radix_tree_for_each_slot(slot, &processed_units, &iter, 0)
+ radix_tree_delete(&processed_units, iter.index);
+
+ return ret;
+}
+
+/**
+ * pwrseq_device_register() - Register a new power sequencer.
+ * @config: Configuration of the new power sequencing device.
+ *
+ * The config structure is only used during the call and can be freed after
+ * the function returns. The config structure *must* have the parent device
+ * as well as the match() callback and at least one target set.
+ *
+ * Returns:
+ * Returns the address of the new pwrseq device or ERR_PTR() on failure.
+ */
+struct pwrseq_device *
+pwrseq_device_register(const struct pwrseq_config *config)
+{
+ struct pwrseq_device *pwrseq;
+ int ret, id;
+
+ if (!config->parent || !config->match || !config->targets ||
+ !config->targets[0])
+ return ERR_PTR(-EINVAL);
+
+ pwrseq = kzalloc_obj(*pwrseq);
+ if (!pwrseq)
+ return ERR_PTR(-ENOMEM);
+
+ pwrseq->dev.type = &pwrseq_device_type;
+ pwrseq->dev.bus = &pwrseq_bus;
+ pwrseq->dev.parent = config->parent;
+ device_set_node(&pwrseq->dev, dev_fwnode(config->parent));
+ dev_set_drvdata(&pwrseq->dev, config->drvdata);
+
+ id = ida_alloc(&pwrseq_ida, GFP_KERNEL);
+ if (id < 0) {
+ kfree(pwrseq);
+ return ERR_PTR(id);
+ }
+
+ pwrseq->id = id;
+
+ /*
+ * From this point onwards the device's release() callback is
+ * responsible for freeing resources.
+ */
+ device_initialize(&pwrseq->dev);
+
+ pwrseq->owner = config->owner ?: THIS_MODULE;
+ pwrseq->match = config->match;
+
+ init_rwsem(&pwrseq->rw_lock);
+ mutex_init(&pwrseq->state_lock);
+ INIT_LIST_HEAD(&pwrseq->targets);
+ INIT_LIST_HEAD(&pwrseq->units);
+
+ ret = dev_set_name(&pwrseq->dev, "pwrseq.%d", pwrseq->id);
+ if (ret)
+ goto err_put_pwrseq;
+
+ ret = pwrseq_setup_targets(config->targets, pwrseq);
+ if (ret)
+ goto err_put_pwrseq;
+
+ scoped_guard(rwsem_write, &pwrseq_sem) {
+ ret = device_add(&pwrseq->dev);
+ if (ret)
+ goto err_put_pwrseq;
+ }
+
+ return pwrseq;
+
+err_put_pwrseq:
+ pwrseq_device_put(pwrseq);
+ return ERR_PTR(ret);
+}
+EXPORT_SYMBOL_GPL(pwrseq_device_register);
+
+/**
+ * pwrseq_device_unregister() - Unregister the power sequencer.
+ * @pwrseq: Power sequencer to unregister.
+ */
+void pwrseq_device_unregister(struct pwrseq_device *pwrseq)
+{
+ struct device *dev = &pwrseq->dev;
+ struct pwrseq_target *target;
+
+ scoped_guard(rwsem_write, &pwrseq_sem) {
+ guard(rwsem_write)(&pwrseq->rw_lock);
+
+ /*
+ * Holding rw_lock for write excludes all power on/off callers
+ * (they hold it for read), so it's safe to read enable_count
+ * here without taking the state_lock.
+ */
+ list_for_each_entry(target, &pwrseq->targets, list)
+ WARN(target->unit->enable_count,
+ "REMOVING POWER SEQUENCER WITH ACTIVE USERS\n");
+
+ device_del(dev);
+ }
+
+ pwrseq_device_put(pwrseq);
+}
+EXPORT_SYMBOL_GPL(pwrseq_device_unregister);
+
+static void devm_pwrseq_device_unregister(void *data)
+{
+ struct pwrseq_device *pwrseq = data;
+
+ pwrseq_device_unregister(pwrseq);
+}
+
+/**
+ * devm_pwrseq_device_register() - Managed variant of pwrseq_device_register().
+ * @dev: Managing device.
+ * @config: Configuration of the new power sequencing device.
+ *
+ * Returns:
+ * Returns the address of the new pwrseq device or ERR_PTR() on failure.
+ */
+struct pwrseq_device *
+devm_pwrseq_device_register(struct device *dev,
+ const struct pwrseq_config *config)
+{
+ struct pwrseq_device *pwrseq;
+ int ret;
+
+ pwrseq = pwrseq_device_register(config);
+ if (IS_ERR(pwrseq))
+ return pwrseq;
+
+ ret = devm_add_action_or_reset(dev, devm_pwrseq_device_unregister,
+ pwrseq);
+ if (ret)
+ return ERR_PTR(ret);
+
+ return pwrseq;
+}
+EXPORT_SYMBOL_GPL(devm_pwrseq_device_register);
+
+/**
+ * pwrseq_device_get_drvdata() - Get the driver private data associated with
+ * this sequencer.
+ * @pwrseq: Power sequencer object.
+ *
+ * Returns:
+ * Address of the private driver data.
+ */
+void *pwrseq_device_get_drvdata(struct pwrseq_device *pwrseq)
+{
+ return dev_get_drvdata(&pwrseq->dev);
+}
+EXPORT_SYMBOL_GPL(pwrseq_device_get_drvdata);
+
+struct pwrseq_match_data {
+ struct pwrseq_desc *desc;
+ struct device *dev;
+ const char *target;
+};
+
+static int pwrseq_match_device(struct device *pwrseq_dev, void *data)
+{
+ struct pwrseq_device *pwrseq = to_pwrseq_device(pwrseq_dev);
+ struct pwrseq_match_data *match_data = data;
+ struct pwrseq_target *target;
+ int ret;
+
+ lockdep_assert_held_read(&pwrseq_sem);
+
+ guard(rwsem_read)(&pwrseq->rw_lock);
+ if (!device_is_registered(&pwrseq->dev))
+ return 0;
+
+ ret = pwrseq->match(pwrseq, match_data->dev);
+ if (ret == PWRSEQ_NO_MATCH || ret < 0)
+ return ret;
+
+ /* We got the matching device, let's find the right target. */
+ list_for_each_entry(target, &pwrseq->targets, list) {
+ if (strcmp(target->name, match_data->target))
+ continue;
+
+ match_data->desc->target = target;
+ }
+
+ /*
+ * This device does not have this target. No point in deferring as it
+ * will not get a new target dynamically later.
+ */
+ if (!match_data->desc->target)
+ return -ENOENT;
+
+ if (!try_module_get(pwrseq->owner))
+ return -EPROBE_DEFER;
+
+ match_data->desc->pwrseq = pwrseq_device_get(pwrseq);
+
+ return PWRSEQ_MATCH_OK;
+}
+
+/**
+ * pwrseq_get() - Get the power sequencer associated with this device.
+ * @dev: Device for which to get the sequencer.
+ * @target: Name of the target exposed by the sequencer this device wants to
+ * reach.
+ *
+ * Returns:
+ * New power sequencer descriptor for use by the consumer driver or ERR_PTR()
+ * on failure.
+ */
+struct pwrseq_desc *pwrseq_get(struct device *dev, const char *target)
+{
+ struct pwrseq_match_data match_data;
+ int ret;
+
+ struct pwrseq_desc *desc __free(kfree) = kzalloc_obj(*desc);
+ if (!desc)
+ return ERR_PTR(-ENOMEM);
+
+ match_data.desc = desc;
+ match_data.dev = dev;
+ match_data.target = target;
+
+ guard(rwsem_read)(&pwrseq_sem);
+
+ ret = bus_for_each_dev(&pwrseq_bus, NULL, &match_data,
+ pwrseq_match_device);
+ if (ret < 0)
+ return ERR_PTR(ret);
+ if (ret == PWRSEQ_NO_MATCH)
+ /* No device matched. */
+ return ERR_PTR(-EPROBE_DEFER);
+
+ return_ptr(desc);
+}
+EXPORT_SYMBOL_GPL(pwrseq_get);
+
+/**
+ * pwrseq_put() - Release the power sequencer descriptor.
+ * @desc: Descriptor to release.
+ */
+void pwrseq_put(struct pwrseq_desc *desc)
+{
+ struct pwrseq_device *pwrseq;
+
+ if (!desc)
+ return;
+
+ pwrseq = desc->pwrseq;
+
+ if (desc->powered_on)
+ pwrseq_disable(desc);
+
+ kfree(desc);
+ module_put(pwrseq->owner);
+ pwrseq_device_put(pwrseq);
+}
+EXPORT_SYMBOL_GPL(pwrseq_put);
+
+static void devm_pwrseq_put(void *data)
+{
+ struct pwrseq_desc *desc = data;
+
+ pwrseq_put(desc);
+}
+
+/**
+ * devm_pwrseq_get() - Managed variant of pwrseq_get().
+ * @dev: Device for which to get the sequencer and which also manages its
+ * lifetime.
+ * @target: Name of the target exposed by the sequencer this device wants to
+ * reach.
+ *
+ * Returns:
+ * New power sequencer descriptor for use by the consumer driver or ERR_PTR()
+ * on failure.
+ */
+struct pwrseq_desc *devm_pwrseq_get(struct device *dev, const char *target)
+{
+ struct pwrseq_desc *desc;
+ int ret;
+
+ desc = pwrseq_get(dev, target);
+ if (IS_ERR(desc))
+ return desc;
+
+ ret = devm_add_action_or_reset(dev, devm_pwrseq_put, desc);
+ if (ret)
+ return ERR_PTR(ret);
+
+ return desc;
+}
+EXPORT_SYMBOL_GPL(devm_pwrseq_get);
+
+static int pwrseq_unit_enable(struct pwrseq_device *pwrseq,
+ struct pwrseq_unit *target);
+static int pwrseq_unit_disable(struct pwrseq_device *pwrseq,
+ struct pwrseq_unit *target);
+
+static int pwrseq_unit_enable_deps(struct pwrseq_device *pwrseq,
+ struct list_head *list)
+{
+ struct pwrseq_unit_dep *pos;
+ int ret = 0;
+
+ list_for_each_entry(pos, list, list) {
+ ret = pwrseq_unit_enable(pwrseq, pos->unit);
+ if (ret) {
+ list_for_each_entry_continue_reverse(pos, list, list)
+ pwrseq_unit_disable(pwrseq, pos->unit);
+ break;
+ }
+ }
+
+ return ret;
+}
+
+static int pwrseq_unit_disable_deps(struct pwrseq_device *pwrseq,
+ struct list_head *list)
+{
+ struct pwrseq_unit_dep *pos;
+ int ret = 0;
+
+ list_for_each_entry_reverse(pos, list, list) {
+ ret = pwrseq_unit_disable(pwrseq, pos->unit);
+ if (ret) {
+ list_for_each_entry_continue(pos, list, list)
+ pwrseq_unit_enable(pwrseq, pos->unit);
+ break;
+ }
+ }
+
+ return ret;
+}
+
+static int pwrseq_unit_enable(struct pwrseq_device *pwrseq,
+ struct pwrseq_unit *unit)
+{
+ int ret;
+
+ lockdep_assert_held_read(&pwrseq->rw_lock);
+ lockdep_assert_held(&pwrseq->state_lock);
+
+ if (unit->enable_count != 0) {
+ unit->enable_count++;
+ return 0;
+ }
+
+ ret = pwrseq_unit_enable_deps(pwrseq, &unit->deps);
+ if (ret) {
+ dev_err(&pwrseq->dev,
+ "Failed to enable dependencies before power-on for target '%s': %d\n",
+ unit->name, ret);
+ return ret;
+ }
+
+ if (unit->enable) {
+ ret = unit->enable(pwrseq);
+ if (ret) {
+ dev_err(&pwrseq->dev,
+ "Failed to enable target '%s': %d\n",
+ unit->name, ret);
+ pwrseq_unit_disable_deps(pwrseq, &unit->deps);
+ return ret;
+ }
+ }
+
+ unit->enable_count++;
+
+ return 0;
+}
+
+static int pwrseq_unit_disable(struct pwrseq_device *pwrseq,
+ struct pwrseq_unit *unit)
+{
+ int ret;
+
+ lockdep_assert_held_read(&pwrseq->rw_lock);
+ lockdep_assert_held(&pwrseq->state_lock);
+
+ if (unit->enable_count == 0) {
+ WARN(1, "Unmatched power-off for target '%s'\n",
+ unit->name);
+ return -EBUSY;
+ }
+
+ if (unit->enable_count != 1) {
+ unit->enable_count--;
+ return 0;
+ }
+
+ if (unit->disable) {
+ ret = unit->disable(pwrseq);
+ if (ret) {
+ dev_err(&pwrseq->dev,
+ "Failed to disable target '%s': %d\n",
+ unit->name, ret);
+ return ret;
+ }
+ }
+
+ ret = pwrseq_unit_disable_deps(pwrseq, &unit->deps);
+ if (ret) {
+ dev_err(&pwrseq->dev,
+ "Failed to disable dependencies after power-off for target '%s': %d\n",
+ unit->name, ret);
+ if (unit->enable)
+ unit->enable(pwrseq);
+ return ret;
+ }
+
+ unit->enable_count--;
+
+ return 0;
+}
+
+/**
+ * pwrseq_enable() - Issue a power-on request on behalf of the consumer
+ * device.
+ * @desc: Descriptor referencing the power sequencer.
+ *
+ * This function tells the power sequencer that the consumer wants to be
+ * powered-up. The sequencer may already have powered-up the device in which
+ * case the function returns 0. If the power-up sequence is already in
+ * progress, the function will block until it's done and return 0. If this is
+ * the first request, the device will be powered up.
+ *
+ * Returns:
+ * 0 on success, negative error number on failure.
+ */
+int pwrseq_enable(struct pwrseq_desc *desc)
+{
+ struct pwrseq_device *pwrseq;
+ struct pwrseq_target *target;
+ struct pwrseq_unit *unit;
+ int ret;
+
+ might_sleep();
+
+ if (!desc || desc->powered_on)
+ return 0;
+
+ pwrseq = desc->pwrseq;
+ target = desc->target;
+ unit = target->unit;
+
+ guard(rwsem_read)(&pwrseq->rw_lock);
+ if (!device_is_registered(&pwrseq->dev))
+ return -ENODEV;
+
+ scoped_guard(mutex, &pwrseq->state_lock) {
+ ret = pwrseq_unit_enable(pwrseq, unit);
+ if (!ret)
+ desc->powered_on = true;
+ }
+ if (ret)
+ return ret;
+
+ if (target->post_enable) {
+ ret = target->post_enable(pwrseq);
+ if (ret) {
+ scoped_guard(mutex, &pwrseq->state_lock) {
+ pwrseq_unit_disable(pwrseq, unit);
+ desc->powered_on = false;
+ }
+ }
+ }
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(pwrseq_enable);
+
+/**
+ * pwrseq_disable() - Issue a power-off request on behalf of the consumer
+ * device.
+ * @desc: Descriptor referencing the power sequencer.
+ *
+ * This undoes the effects of pwrseq_enable(). It issues a power-off request
+ * on behalf of the consumer and when the last remaining user does so, the
+ * power-down sequence will be started. If one is in progress, the function
+ * will block until it's complete and then return.
+ *
+ * Returns:
+ * 0 on success, negative error number on failure.
+ */
+int pwrseq_disable(struct pwrseq_desc *desc)
+{
+ struct pwrseq_device *pwrseq;
+ struct pwrseq_unit *unit;
+ int ret;
+
+ might_sleep();
+
+ if (!desc || !desc->powered_on)
+ return 0;
+
+ pwrseq = desc->pwrseq;
+ unit = desc->target->unit;
+
+ guard(rwsem_read)(&pwrseq->rw_lock);
+ if (!device_is_registered(&pwrseq->dev))
+ return -ENODEV;
+
+ guard(mutex)(&pwrseq->state_lock);
+
+ ret = pwrseq_unit_disable(pwrseq, unit);
+ if (!ret)
+ desc->powered_on = false;
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(pwrseq_disable);
+
+/**
+ * pwrseq_to_device() - Get the pwrseq device pointer from a descriptor.
+ * @desc: Descriptor referencing the power sequencer.
+ *
+ * Return the 'dev' pointer of the power sequencer device associated with @desc.
+ * Consumer drivers can use this to query the pwrseq provider's device tree
+ * node, for example to check for the existence of specific properties.
+ *
+ * Since pwrseq_get() already takes a reference to the pwrseq device, this
+ * function does not take an additional reference.
+ *
+ * Returns:
+ * Pointer to the pwrseq struct device, or NULL if @desc is NULL.
+ */
+struct device *pwrseq_to_device(struct pwrseq_desc *desc)
+{
+ if (!desc)
+ return NULL;
+
+ return &desc->pwrseq->dev;
+}
+EXPORT_SYMBOL_GPL(pwrseq_to_device);
+
+/**
+ * pwrseq_is_controllable() - Check whether the target provides a
+ * host-controllable power actuator.
+ * @desc: Descriptor referencing the power sequencer.
+ *
+ * Some power sequencing targets provide no host-controllable enable for their
+ * function on a given board, for instance when the enable line is not wired up
+ * and is instead hardwired to an always-on level. For such targets a call to
+ * pwrseq_power_off() is still allowed, so that the consumer can drop its vote
+ * on the (possibly shared) resources, but the host cannot gate the function
+ * on its own.
+ *
+ * Returns:
+ * True if the target provides a host-controllable power actuator, false
+ * otherwise. Also returns false if @desc is NULL.
+ */
+bool pwrseq_is_controllable(struct pwrseq_desc *desc)
+{
+ struct pwrseq_device *pwrseq;
+ struct pwrseq_target *target;
+ struct pwrseq_unit *unit;
+
+ might_sleep();
+
+ if (!desc)
+ return false;
+
+ pwrseq = desc->pwrseq;
+ target = desc->target;
+ unit = target->unit;
+
+ guard(rwsem_read)(&pwrseq->rw_lock);
+ if (!device_is_registered(&pwrseq->dev))
+ return false;
+
+ if (!unit->enable && !unit->disable)
+ return false;
+
+ if (!target->is_controllable)
+ return true;
+
+ return target->is_controllable(pwrseq);
+}
+EXPORT_SYMBOL_GPL(pwrseq_is_controllable);
+
+#if IS_ENABLED(CONFIG_DEBUG_FS)
+
+struct pwrseq_debugfs_count_ctx {
+ struct device *dev;
+ loff_t index;
+};
+
+static int pwrseq_debugfs_seq_count(struct device *dev, void *data)
+{
+ struct pwrseq_debugfs_count_ctx *ctx = data;
+
+ ctx->dev = dev;
+
+ return ctx->index-- ? 0 : 1;
+}
+
+static void *pwrseq_debugfs_seq_start(struct seq_file *seq, loff_t *pos)
+{
+ struct pwrseq_debugfs_count_ctx ctx;
+
+ ctx.dev = NULL;
+ ctx.index = *pos;
+
+ /*
+ * Hold the lock for the entire printout to prevent device removal.
+ * Reference counts are managed by start()/next()/stop() as required
+ * by the seq_file contract.
+ */
+ down_read(&pwrseq_sem);
+
+ bus_for_each_dev(&pwrseq_bus, NULL, &ctx, pwrseq_debugfs_seq_count);
+ if (!ctx.index)
+ return NULL;
+
+ return get_device(ctx.dev);
+}
+
+static void *pwrseq_debugfs_seq_next(struct seq_file *seq, void *data,
+ loff_t *pos)
+{
+ struct device *curr = data;
+
+ ++*pos;
+
+ struct device *next = bus_find_next_device(&pwrseq_bus, curr);
+
+ put_device(curr);
+ return next;
+}
+
+static void pwrseq_debugfs_seq_show_target(struct seq_file *seq,
+ struct pwrseq_target *target)
+{
+ seq_printf(seq, " target: [%s] (target unit: [%s])\n",
+ target->name, target->unit->name);
+}
+
+static void pwrseq_debugfs_seq_show_unit(struct seq_file *seq,
+ struct pwrseq_unit *unit)
+{
+ struct pwrseq_unit_dep *ref;
+
+ seq_printf(seq, " unit: [%s] - enable count: %u\n",
+ unit->name, unit->enable_count);
+
+ if (list_empty(&unit->deps))
+ return;
+
+ seq_puts(seq, " dependencies:\n");
+ list_for_each_entry(ref, &unit->deps, list)
+ seq_printf(seq, " [%s]\n", ref->unit->name);
+}
+
+static int pwrseq_debugfs_seq_show(struct seq_file *seq, void *data)
+{
+ struct device *dev = data;
+ struct pwrseq_device *pwrseq = to_pwrseq_device(dev);
+ struct pwrseq_target *target;
+ struct pwrseq_unit *unit;
+
+ seq_printf(seq, "%s (%s):\n", dev_name(dev), dev_name(dev->parent));
+
+ seq_puts(seq, " targets:\n");
+ list_for_each_entry(target, &pwrseq->targets, list)
+ pwrseq_debugfs_seq_show_target(seq, target);
+
+ seq_puts(seq, " units:\n");
+ list_for_each_entry(unit, &pwrseq->units, list)
+ pwrseq_debugfs_seq_show_unit(seq, unit);
+
+ return 0;
+}
+
+static void pwrseq_debugfs_seq_stop(struct seq_file *seq, void *data)
+{
+ if (data)
+ put_device(data);
+ up_read(&pwrseq_sem);
+}
+
+static const struct seq_operations pwrseq_debugfs_sops = {
+ .start = pwrseq_debugfs_seq_start,
+ .next = pwrseq_debugfs_seq_next,
+ .show = pwrseq_debugfs_seq_show,
+ .stop = pwrseq_debugfs_seq_stop,
+};
+DEFINE_SEQ_ATTRIBUTE(pwrseq_debugfs);
+
+static struct dentry *pwrseq_debugfs_dentry;
+
+#endif /* CONFIG_DEBUG_FS */
+
+static int __init pwrseq_init(void)
+{
+ int ret;
+
+ ret = bus_register(&pwrseq_bus);
+ if (ret) {
+ pr_err("Failed to register the power sequencer bus\n");
+ return ret;
+ }
+
+#if IS_ENABLED(CONFIG_DEBUG_FS)
+ pwrseq_debugfs_dentry = debugfs_create_file("pwrseq", 0444, NULL, NULL,
+ &pwrseq_debugfs_fops);
+#endif /* CONFIG_DEBUG_FS */
+
+ return 0;
+}
+subsys_initcall(pwrseq_init);
+
+static void __exit pwrseq_exit(void)
+{
+#if IS_ENABLED(CONFIG_DEBUG_FS)
+ debugfs_remove_recursive(pwrseq_debugfs_dentry);
+#endif /* CONFIG_DEBUG_FS */
+
+ bus_unregister(&pwrseq_bus);
+}
+module_exit(pwrseq_exit);
+
+MODULE_AUTHOR("Bartosz Golaszewski <bartosz.golaszewski@linaro.org>");
+MODULE_DESCRIPTION("Power Sequencing subsystem core");
+MODULE_LICENSE("GPL");
diff --git a/drivers/power/sequencing/pwrseq-kunit.c b/drivers/power/sequencing/pwrseq-kunit.c
new file mode 100644
index 000000000..c2899f3b1
--- /dev/null
+++ b/drivers/power/sequencing/pwrseq-kunit.c
@@ -0,0 +1,1497 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2026 Qualcomm Technologies, Inc. and/or its subsidiaries
+ */
+
+#include <linux/err.h>
+#include <linux/fwnode.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/property.h>
+#include <linux/pwrseq/consumer.h>
+#include <linux/pwrseq/provider.h>
+
+#include <kunit/fwnode.h>
+#include <kunit/platform_device.h>
+#include <kunit/resource.h>
+#include <kunit/test.h>
+
+#define PWRSEQ_TEST_PARENT "pwrseq-test-parent"
+#define PWRSEQ_TEST_CONSUMER "pwrseq-test-consumer"
+
+#define PWRSEQ_SWNODE_TEST_PROVIDER "pwrseq-swnode-test-provider"
+#define PWRSEQ_SWNODE_TEST_PROVIDER_2 "pwrseq-swnode-test-provider-2"
+#define PWRSEQ_PROBE_ORDER_TEST_CONSUMER "pwrseq-probe-order-test-consumer"
+#define PWRSEQ_PROBE_DEFER_TEST_CONSUMER "pwrseq-probe-defer-test-consumer"
+
+static const struct software_node pwrseq_test_provider_swnode = {
+ .name = "pwrseq-test-provider",
+};
+
+KUNIT_DEFINE_ACTION_WRAPPER(pwrseq_device_unregister_wrapper,
+ pwrseq_device_unregister,
+ struct pwrseq_device *);
+
+static struct pwrseq_device *
+kunit_pwrseq_device_register(struct kunit *test,
+ const struct pwrseq_config *config)
+{
+ struct pwrseq_device *pwrseq;
+ int ret;
+
+ pwrseq = pwrseq_device_register(config);
+ if (IS_ERR(pwrseq))
+ return pwrseq;
+
+ ret = kunit_add_action_or_reset(test, pwrseq_device_unregister_wrapper,
+ pwrseq);
+ if (ret)
+ return ERR_PTR(ret);
+
+ return pwrseq;
+}
+
+KUNIT_DEFINE_ACTION_WRAPPER(pwrseq_put_wrapper, pwrseq_put,
+ struct pwrseq_desc *);
+
+static struct pwrseq_desc *
+kunit_pwrseq_get(struct kunit *test, struct device *dev, const char *target)
+{
+ struct pwrseq_desc *desc;
+ int ret;
+
+ desc = pwrseq_get(dev, target);
+ if (IS_ERR(desc))
+ return desc;
+
+ ret = kunit_add_action_or_reset(test, pwrseq_put_wrapper, desc);
+ if (ret)
+ return ERR_PTR(ret);
+
+ return desc;
+}
+
+struct pwrseq_test_ctx {
+ const char *consumer_name;
+ int enable_count_a;
+ int enable_count_b;
+ int disable_count_a;
+ int disable_count_b;
+ int enable_calls_a;
+ int post_enable_calls;
+ bool enable_error_a;
+ bool enable_error_b;
+ bool post_enable_error;
+};
+
+static int pwrseq_test_no_match(struct pwrseq_device *pwrseq,
+ struct device *dev)
+{
+ return PWRSEQ_NO_MATCH;
+}
+
+static int pwrseq_test_match_by_name(struct pwrseq_device *pwrseq,
+ struct device *dev)
+{
+ struct pwrseq_test_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
+
+ return strcmp(dev_name(dev), ctx->consumer_name) == 0 ?
+ PWRSEQ_MATCH_OK : PWRSEQ_NO_MATCH;
+}
+
+static int pwrseq_test_match_always(struct pwrseq_device *pwrseq,
+ struct device *dev)
+{
+ return PWRSEQ_MATCH_OK;
+}
+
+static int pwrseq_test_enable_a(struct pwrseq_device *pwrseq)
+{
+ struct pwrseq_test_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
+
+ ctx->enable_calls_a++;
+
+ if (ctx->enable_error_a)
+ return -EIO;
+
+ ctx->enable_count_a++;
+
+ return 0;
+}
+
+static int pwrseq_test_disable_a(struct pwrseq_device *pwrseq)
+{
+ struct pwrseq_test_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
+
+ ctx->disable_count_a++;
+
+ return 0;
+}
+
+static int pwrseq_test_enable_b(struct pwrseq_device *pwrseq)
+{
+ struct pwrseq_test_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
+
+ if (ctx->enable_error_b)
+ return -EIO;
+
+ ctx->enable_count_b++;
+
+ return 0;
+}
+
+static int pwrseq_test_disable_b(struct pwrseq_device *pwrseq)
+{
+ struct pwrseq_test_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
+
+ ctx->disable_count_b++;
+
+ return 0;
+}
+
+static int pwrseq_test_post_enable(struct pwrseq_device *pwrseq)
+{
+ struct pwrseq_test_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
+
+ ctx->post_enable_calls++;
+
+ return ctx->post_enable_error ? -EIO : 0;
+}
+
+static int pwrseq_test_parent_probe(struct platform_device *pdev)
+{
+ return 0;
+}
+
+static struct platform_driver pwrseq_test_parent_driver = {
+ .probe = pwrseq_test_parent_probe,
+ .driver = {
+ .name = PWRSEQ_TEST_PARENT,
+ },
+};
+
+static int pwrseq_test_parent_init(struct kunit *test)
+{
+ static const struct platform_device_info pdevinfo = {
+ .name = PWRSEQ_TEST_PARENT,
+ .id = PLATFORM_DEVID_NONE,
+ };
+
+ struct platform_device *pdev;
+ bool bound;
+ int ret;
+
+ ret = kunit_platform_driver_register(test, &pwrseq_test_parent_driver);
+ KUNIT_ASSERT_EQ(test, ret, 0);
+
+ pdev = kunit_platform_device_register_full(test, &pdevinfo);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
+
+ wait_for_device_probe();
+ scoped_guard(device, &pdev->dev)
+ bound = device_is_bound(&pdev->dev);
+
+ KUNIT_ASSERT_TRUE(test, bound);
+
+ test->priv = pdev;
+
+ return 0;
+}
+
+/*
+ * Test that registering a power sequencer whose unit dependency graph
+ * contains a two-node cycle (A -> B -> A) is rejected with -EINVAL.
+ */
+static void pwrseq_circular_deps(struct kunit *test)
+{
+ const struct pwrseq_unit_data *unit_a_deps[2] = { };
+ const struct pwrseq_unit_data *unit_b_deps[2] = { };
+ const struct pwrseq_target_data *targets[2] = { };
+ struct platform_device *pdev = test->priv;
+ struct pwrseq_unit_data unit_a, unit_b;
+ struct pwrseq_target_data target;
+ struct pwrseq_device *pwrseq;
+ struct pwrseq_config config;
+
+ unit_a = (struct pwrseq_unit_data){
+ .name = "unit-a",
+ .deps = unit_a_deps,
+ };
+
+ unit_b = (struct pwrseq_unit_data){
+ .name = "unit-b",
+ .deps = unit_b_deps,
+ };
+
+ unit_a_deps[0] = &unit_b;
+ unit_b_deps[0] = &unit_a;
+
+ target = (struct pwrseq_target_data){
+ .name = "test-target",
+ .unit = &unit_a,
+ };
+
+ targets[0] = &target;
+
+ config = (struct pwrseq_config){
+ .parent = &pdev->dev,
+ .match = pwrseq_test_no_match,
+ .targets = targets,
+ };
+
+ kunit_warning_suppress(test) {
+ pwrseq = kunit_pwrseq_device_register(test, &config);
+ KUNIT_EXPECT_SUPPRESSED_WARNING_COUNT(test, 1);
+ }
+
+ KUNIT_EXPECT_TRUE(test, IS_ERR(pwrseq));
+ KUNIT_EXPECT_EQ(test, PTR_ERR(pwrseq), -EINVAL);
+}
+
+/*
+ * Test that a longer chain cycle (A -> B -> C -> D -> A) is also rejected
+ * with -EINVAL.
+ */
+static void pwrseq_circular_deps_chain(struct kunit *test)
+{
+ struct pwrseq_unit_data unit_a, unit_b, unit_c, unit_d;
+ const struct pwrseq_unit_data *unit_a_deps[2] = { };
+ const struct pwrseq_unit_data *unit_b_deps[2] = { };
+ const struct pwrseq_unit_data *unit_c_deps[2] = { };
+ const struct pwrseq_unit_data *unit_d_deps[2] = { };
+ const struct pwrseq_target_data *targets[2] = { };
+ struct platform_device *pdev = test->priv;
+ struct pwrseq_target_data target;
+ struct pwrseq_device *pwrseq;
+ struct pwrseq_config config;
+
+ unit_a = (struct pwrseq_unit_data){
+ .name = "unit-a",
+ .deps = unit_a_deps,
+ };
+ unit_b = (struct pwrseq_unit_data){
+ .name = "unit-b",
+ .deps = unit_b_deps,
+ };
+ unit_c = (struct pwrseq_unit_data){
+ .name = "unit-c",
+ .deps = unit_c_deps,
+ };
+ unit_d = (struct pwrseq_unit_data){
+ .name = "unit-d",
+ .deps = unit_d_deps,
+ };
+
+ unit_a_deps[0] = &unit_b;
+ unit_b_deps[0] = &unit_c;
+ unit_c_deps[0] = &unit_d;
+ unit_d_deps[0] = &unit_a;
+
+ target = (struct pwrseq_target_data){
+ .name = "test-target",
+ .unit = &unit_a,
+ };
+
+ targets[0] = &target;
+
+ config = (struct pwrseq_config){
+ .parent = &pdev->dev,
+ .match = pwrseq_test_no_match,
+ .targets = targets,
+ };
+
+ kunit_warning_suppress(test) {
+ pwrseq = kunit_pwrseq_device_register(test, &config);
+ KUNIT_EXPECT_SUPPRESSED_WARNING_COUNT(test, 1);
+ }
+
+ KUNIT_EXPECT_TRUE(test, IS_ERR(pwrseq));
+ KUNIT_EXPECT_EQ(test, PTR_ERR(pwrseq), -EINVAL);
+}
+
+/*
+ * Test that a valid acyclic dependency graph (A -> B) registers successfully.
+ */
+static void pwrseq_register_valid(struct kunit *test)
+{
+ const struct pwrseq_unit_data *unit_a_deps[2] = { };
+ const struct pwrseq_target_data *targets[2] = { };
+ struct platform_device *pdev = test->priv;
+ struct pwrseq_unit_data unit_a, unit_b;
+ struct pwrseq_target_data target;
+ struct pwrseq_device *pwrseq;
+ struct pwrseq_config config;
+
+ unit_b = (struct pwrseq_unit_data){
+ .name = "unit-b",
+ };
+
+ unit_a_deps[0] = &unit_b;
+ unit_a = (struct pwrseq_unit_data){
+ .name = "unit-a",
+ .deps = unit_a_deps,
+ };
+
+ target = (struct pwrseq_target_data){
+ .name = "test-target",
+ .unit = &unit_a,
+ };
+
+ targets[0] = &target;
+
+ config = (struct pwrseq_config){
+ .parent = &pdev->dev,
+ .match = pwrseq_test_no_match,
+ .targets = targets,
+ };
+
+ pwrseq = kunit_pwrseq_device_register(test, &config);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pwrseq);
+}
+
+/*
+ * Test that registration is rejected with -EINVAL when .parent, .match or
+ * .targets is missing, or when .targets is a non-NULL but empty array.
+ */
+static void pwrseq_register_invalid_args(struct kunit *test)
+{
+ const struct pwrseq_target_data *empty_targets[1] = { };
+ const struct pwrseq_target_data *targets[2] = { };
+ struct platform_device *pdev = test->priv;
+ struct pwrseq_target_data target;
+ struct pwrseq_unit_data unit;
+ struct pwrseq_device *pwrseq;
+ struct pwrseq_config config;
+
+ unit = (struct pwrseq_unit_data){ .name = "unit-a" };
+ target = (struct pwrseq_target_data){
+ .name = "test-target",
+ .unit = &unit,
+ };
+ targets[0] = &target;
+
+ config = (struct pwrseq_config){
+ .parent = &pdev->dev,
+ .match = pwrseq_test_no_match,
+ .targets = targets,
+ };
+
+ config.parent = NULL;
+ pwrseq = kunit_pwrseq_device_register(test, &config);
+ KUNIT_EXPECT_TRUE(test, IS_ERR(pwrseq));
+ KUNIT_EXPECT_EQ(test, PTR_ERR(pwrseq), -EINVAL);
+ config.parent = &pdev->dev;
+
+ config.match = NULL;
+ pwrseq = kunit_pwrseq_device_register(test, &config);
+ KUNIT_EXPECT_TRUE(test, IS_ERR(pwrseq));
+ KUNIT_EXPECT_EQ(test, PTR_ERR(pwrseq), -EINVAL);
+ config.match = pwrseq_test_no_match;
+
+ config.targets = NULL;
+ pwrseq = kunit_pwrseq_device_register(test, &config);
+ KUNIT_EXPECT_TRUE(test, IS_ERR(pwrseq));
+ KUNIT_EXPECT_EQ(test, PTR_ERR(pwrseq), -EINVAL);
+
+ config.targets = empty_targets;
+ pwrseq = kunit_pwrseq_device_register(test, &config);
+ KUNIT_EXPECT_TRUE(test, IS_ERR(pwrseq));
+ KUNIT_EXPECT_EQ(test, PTR_ERR(pwrseq), -EINVAL);
+}
+
+/*
+ * Test that a target without a unit is rejected with -EINVAL.
+ */
+static void pwrseq_register_target_without_unit(struct kunit *test)
+{
+ const struct pwrseq_target_data *targets[2] = { };
+ struct platform_device *pdev = test->priv;
+ struct pwrseq_target_data target;
+ struct pwrseq_device *pwrseq;
+ struct pwrseq_config config;
+
+ target = (struct pwrseq_target_data){
+ .name = "test-target",
+ .unit = NULL,
+ };
+ targets[0] = &target;
+
+ config = (struct pwrseq_config){
+ .parent = &pdev->dev,
+ .match = pwrseq_test_no_match,
+ .targets = targets,
+ };
+
+ pwrseq = kunit_pwrseq_device_register(test, &config);
+ KUNIT_EXPECT_TRUE(test, IS_ERR(pwrseq));
+ KUNIT_EXPECT_EQ(test, PTR_ERR(pwrseq), -EINVAL);
+}
+
+/*
+ * Test that enabling and disabling a single-unit target increments and
+ * decrements enable_count correctly and fires the enable/disable callbacks
+ * exactly once.
+ */
+static void pwrseq_enable_disable(struct kunit *test)
+{
+ const struct pwrseq_target_data *targets[2] = { };
+ struct platform_device *pdev = test->priv;
+ struct platform_device_info pdevinfo;
+ struct pwrseq_target_data target;
+ struct pwrseq_test_ctx *ctx;
+ struct pwrseq_unit_data unit;
+ struct pwrseq_device *pwrseq;
+ struct pwrseq_config config;
+ struct pwrseq_desc *desc;
+ int ret;
+
+ ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+
+ unit = (struct pwrseq_unit_data){
+ .name = "unit-a",
+ .enable = pwrseq_test_enable_a,
+ .disable = pwrseq_test_disable_a,
+ };
+
+ target = (struct pwrseq_target_data){
+ .name = "test-target",
+ .unit = &unit,
+ };
+
+ targets[0] = &target;
+
+ ctx->consumer_name = PWRSEQ_TEST_CONSUMER;
+
+ config = (struct pwrseq_config){
+ .parent = &pdev->dev,
+ .drvdata = ctx,
+ .match = pwrseq_test_match_by_name,
+ .targets = targets,
+ };
+
+ pwrseq = kunit_pwrseq_device_register(test, &config);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pwrseq);
+
+ pdevinfo = (struct platform_device_info){
+ .name = PWRSEQ_TEST_CONSUMER,
+ .id = PLATFORM_DEVID_NONE,
+ };
+
+ pdev = kunit_platform_device_register_full(test, &pdevinfo);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
+
+ desc = kunit_pwrseq_get(test, &pdev->dev, "test-target");
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, desc);
+
+ ret = pwrseq_enable(desc);
+ KUNIT_EXPECT_EQ(test, ret, 0);
+ KUNIT_EXPECT_EQ(test, ctx->enable_count_a, 1);
+ KUNIT_EXPECT_EQ(test, ctx->disable_count_a, 0);
+
+ ret = pwrseq_disable(desc);
+ KUNIT_EXPECT_EQ(test, ret, 0);
+ KUNIT_EXPECT_EQ(test, ctx->enable_count_a, 1);
+ KUNIT_EXPECT_EQ(test, ctx->disable_count_a, 1);
+}
+
+/*
+ * Test that two consumers sharing a common dependency unit cause it to be
+ * enabled only once and disabled only once, while enable_count tracks each
+ * consumer correctly.
+ */
+static void pwrseq_shared_deps(struct kunit *test)
+{
+ const struct pwrseq_unit_data *unit_a_deps[2] = { };
+ const struct pwrseq_unit_data *unit_b_deps[2] = { };
+ const struct pwrseq_target_data *targets[3] = { };
+ struct pwrseq_unit_data dep_unit, unit_a, unit_b;
+ struct pwrseq_target_data target_a, target_b;
+ struct platform_device *parent = test->priv;
+ struct platform_device *pdev_a, *pdev_b;
+ struct platform_device_info pdevinfo;
+ struct pwrseq_desc *desc_a, *desc_b;
+ struct pwrseq_device *pwrseq;
+ struct pwrseq_config config;
+ struct pwrseq_test_ctx *ctx;
+ int ret;
+
+ ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+
+ dep_unit = (struct pwrseq_unit_data){
+ .name = "dep",
+ .enable = pwrseq_test_enable_a,
+ .disable = pwrseq_test_disable_a,
+ };
+
+ unit_a_deps[0] = &dep_unit;
+ unit_a = (struct pwrseq_unit_data){
+ .name = "unit-a",
+ .deps = unit_a_deps,
+ .enable = pwrseq_test_enable_b,
+ .disable = pwrseq_test_disable_b,
+ };
+
+ unit_b_deps[0] = &dep_unit;
+ unit_b = (struct pwrseq_unit_data){
+ .name = "unit-b",
+ .deps = unit_b_deps,
+ };
+
+ target_a = (struct pwrseq_target_data){
+ .name = "target-a",
+ .unit = &unit_a,
+ };
+ target_b = (struct pwrseq_target_data){
+ .name = "target-b",
+ .unit = &unit_b,
+ };
+
+ targets[0] = &target_a;
+ targets[1] = &target_b;
+
+ ctx->consumer_name = PWRSEQ_TEST_CONSUMER "-a";
+
+ config = (struct pwrseq_config){
+ .parent = &parent->dev,
+ .drvdata = ctx,
+ .match = pwrseq_test_match_by_name,
+ .targets = targets,
+ };
+
+ pwrseq = kunit_pwrseq_device_register(test, &config);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pwrseq);
+
+ pdevinfo = (struct platform_device_info){
+ .name = PWRSEQ_TEST_CONSUMER "-a",
+ .id = PLATFORM_DEVID_NONE,
+ };
+ pdev_a = kunit_platform_device_register_full(test, &pdevinfo);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev_a);
+
+ pdevinfo = (struct platform_device_info){
+ .name = PWRSEQ_TEST_CONSUMER "-b",
+ .id = PLATFORM_DEVID_NONE,
+ };
+ pdev_b = kunit_platform_device_register_full(test, &pdevinfo);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev_b);
+
+ /*
+ * match_by_name() only matches one consumer name at a time, so
+ * reconfigure ctx->consumer_name between the two pwrseq_get() calls
+ * below to acquire descriptors for both consumer-a and consumer-b.
+ */
+ desc_a = kunit_pwrseq_get(test, &pdev_a->dev, "target-a");
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, desc_a);
+
+ ctx->consumer_name = PWRSEQ_TEST_CONSUMER "-b";
+ desc_b = kunit_pwrseq_get(test, &pdev_b->dev, "target-b");
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, desc_b);
+
+ ret = pwrseq_enable(desc_a);
+ KUNIT_EXPECT_EQ(test, ret, 0);
+ KUNIT_EXPECT_EQ(test, ctx->enable_count_a, 1);
+ KUNIT_EXPECT_EQ(test, ctx->enable_count_b, 1);
+
+ ret = pwrseq_enable(desc_b);
+ KUNIT_EXPECT_EQ(test, ret, 0);
+ KUNIT_EXPECT_EQ(test, ctx->enable_count_a, 1);
+
+ ret = pwrseq_disable(desc_a);
+ KUNIT_EXPECT_EQ(test, ret, 0);
+ KUNIT_EXPECT_EQ(test, ctx->disable_count_a, 0);
+ KUNIT_EXPECT_EQ(test, ctx->disable_count_b, 1);
+
+ ret = pwrseq_disable(desc_b);
+ KUNIT_EXPECT_EQ(test, ret, 0);
+ KUNIT_EXPECT_EQ(test, ctx->disable_count_a, 1);
+}
+
+/*
+ * Test that calling pwrseq_enable() twice on the same descriptor has no
+ * effect.
+ */
+static void pwrseq_enable_idempotent(struct kunit *test)
+{
+ const struct pwrseq_target_data *targets[2] = { };
+ struct platform_device *pdev = test->priv;
+ struct platform_device_info pdevinfo;
+ struct pwrseq_target_data target;
+ struct pwrseq_device *pwrseq;
+ struct pwrseq_unit_data unit;
+ struct pwrseq_config config;
+ struct pwrseq_test_ctx *ctx;
+ struct pwrseq_desc *desc;
+ int ret;
+
+ ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+
+ unit = (struct pwrseq_unit_data){
+ .name = "unit-a",
+ .enable = pwrseq_test_enable_a,
+ .disable = pwrseq_test_disable_a,
+ };
+
+ target = (struct pwrseq_target_data){
+ .name = "test-target",
+ .unit = &unit,
+ };
+
+ targets[0] = &target;
+
+ ctx->consumer_name = PWRSEQ_TEST_CONSUMER;
+
+ config = (struct pwrseq_config){
+ .parent = &pdev->dev,
+ .drvdata = ctx,
+ .match = pwrseq_test_match_by_name,
+ .targets = targets,
+ };
+
+ pwrseq = kunit_pwrseq_device_register(test, &config);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pwrseq);
+
+ pdevinfo = (struct platform_device_info){
+ .name = PWRSEQ_TEST_CONSUMER,
+ .id = PLATFORM_DEVID_NONE,
+ };
+
+ pdev = kunit_platform_device_register_full(test, &pdevinfo);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
+
+ desc = kunit_pwrseq_get(test, &pdev->dev, "test-target");
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, desc);
+
+ ret = pwrseq_enable(desc);
+ KUNIT_EXPECT_EQ(test, ret, 0);
+ KUNIT_EXPECT_EQ(test, ctx->enable_count_a, 1);
+
+ /* Second power_on on same descriptor must be a no-op. */
+ ret = pwrseq_enable(desc);
+ KUNIT_EXPECT_EQ(test, ret, 0);
+ KUNIT_EXPECT_EQ(test, ctx->enable_count_a, 1);
+
+ ret = pwrseq_disable(desc);
+ KUNIT_EXPECT_EQ(test, ret, 0);
+ KUNIT_EXPECT_EQ(test, ctx->disable_count_a, 1);
+}
+
+/*
+ * Test that when a dependency unit's enable() callback fails, the error
+ * propagates to the caller, the top unit's enable callback is never reached,
+ * and target->post_enable() is never invoked.
+ */
+static void pwrseq_enable_enable_error(struct kunit *test)
+{
+ const struct pwrseq_target_data *targets[2] = { };
+ const struct pwrseq_unit_data *top_deps[2] = { };
+ struct pwrseq_unit_data dep_unit, top_unit;
+ struct platform_device *pdev = test->priv;
+ struct platform_device_info pdevinfo;
+ struct pwrseq_target_data target;
+ struct pwrseq_device *pwrseq;
+ struct pwrseq_config config;
+ struct pwrseq_test_ctx *ctx;
+ struct pwrseq_desc *desc;
+ int ret;
+
+ ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+
+ dep_unit = (struct pwrseq_unit_data){
+ .name = "dep",
+ .enable = pwrseq_test_enable_a,
+ };
+
+ top_deps[0] = &dep_unit;
+ top_unit = (struct pwrseq_unit_data){
+ .name = "top",
+ .deps = top_deps,
+ .enable = pwrseq_test_enable_b,
+ };
+
+ target = (struct pwrseq_target_data){
+ .name = "test-target",
+ .unit = &top_unit,
+ .post_enable = pwrseq_test_post_enable,
+ };
+
+ targets[0] = &target;
+
+ ctx->consumer_name = PWRSEQ_TEST_CONSUMER;
+ ctx->enable_error_a = true;
+
+ config = (struct pwrseq_config){
+ .parent = &pdev->dev,
+ .drvdata = ctx,
+ .match = pwrseq_test_match_by_name,
+ .targets = targets,
+ };
+
+ pwrseq = kunit_pwrseq_device_register(test, &config);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pwrseq);
+
+ pdevinfo = (struct platform_device_info){
+ .name = PWRSEQ_TEST_CONSUMER,
+ .id = PLATFORM_DEVID_NONE,
+ };
+
+ pdev = kunit_platform_device_register_full(test, &pdevinfo);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
+
+ desc = kunit_pwrseq_get(test, &pdev->dev, "test-target");
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, desc);
+
+ ret = pwrseq_enable(desc);
+ KUNIT_EXPECT_EQ(test, ret, -EIO);
+ /*
+ * Dependency's enable() was attempted but failed, so it never
+ * incremented.
+ */
+ KUNIT_EXPECT_EQ(test, ctx->enable_calls_a, 1);
+ KUNIT_EXPECT_EQ(test, ctx->enable_count_a, 0);
+ /* Target unit's enable() was never reached. */
+ KUNIT_EXPECT_EQ(test, ctx->enable_count_b, 0);
+ /* Target's .post_enable() must not run on an unpowered sequencer. */
+ KUNIT_EXPECT_EQ(test, ctx->post_enable_calls, 0);
+}
+
+/*
+ * Test that when the top unit's own enable() callback fails after its
+ * dependency's enable() already succeeded, the dependency is rolled back.
+ */
+static void pwrseq_enable_rollback_on_top_failure(struct kunit *test)
+{
+ const struct pwrseq_target_data *targets[2] = { };
+ const struct pwrseq_unit_data *top_deps[2] = { };
+ struct pwrseq_unit_data dep_unit, top_unit;
+ struct platform_device *pdev = test->priv;
+ struct platform_device_info pdevinfo;
+ struct pwrseq_target_data target;
+ struct pwrseq_device *pwrseq;
+ struct pwrseq_config config;
+ struct pwrseq_test_ctx *ctx;
+ struct pwrseq_desc *desc;
+ int ret;
+
+ ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+
+ dep_unit = (struct pwrseq_unit_data){
+ .name = "dep",
+ .enable = pwrseq_test_enable_a,
+ .disable = pwrseq_test_disable_a,
+ };
+
+ top_deps[0] = &dep_unit;
+ top_unit = (struct pwrseq_unit_data){
+ .name = "top",
+ .deps = top_deps,
+ .enable = pwrseq_test_enable_b,
+ };
+
+ target = (struct pwrseq_target_data){
+ .name = "test-target",
+ .unit = &top_unit,
+ };
+
+ targets[0] = &target;
+
+ ctx->consumer_name = PWRSEQ_TEST_CONSUMER;
+ ctx->enable_error_b = true;
+
+ config = (struct pwrseq_config){
+ .parent = &pdev->dev,
+ .drvdata = ctx,
+ .match = pwrseq_test_match_by_name,
+ .targets = targets,
+ };
+
+ pwrseq = kunit_pwrseq_device_register(test, &config);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pwrseq);
+
+ pdevinfo = (struct platform_device_info){
+ .name = PWRSEQ_TEST_CONSUMER,
+ .id = PLATFORM_DEVID_NONE,
+ };
+
+ pdev = kunit_platform_device_register_full(test, &pdevinfo);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
+
+ desc = kunit_pwrseq_get(test, &pdev->dev, "test-target");
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, desc);
+
+ ret = pwrseq_enable(desc);
+ KUNIT_EXPECT_EQ(test, ret, -EIO);
+ /*
+ * Dependency was enabled, then rolled back once top's own enable
+ * failed.
+ */
+ KUNIT_EXPECT_EQ(test, ctx->enable_count_a, 1);
+ KUNIT_EXPECT_EQ(test, ctx->disable_count_a, 1);
+ /*
+ * Target unit's own enable never incremented since it returned an
+ * error.
+ */
+ KUNIT_EXPECT_EQ(test, ctx->enable_count_b, 0);
+}
+
+/*
+ * Test target->post_enable() on both the success and failure paths: it must
+ * run exactly once per pwrseq_enable() call that actually powers the target
+ * unit on, and a failing post_enable() must roll the unit back and clear
+ * powered_on so a subsequent pwrseq_enable() is called again.
+ */
+static void pwrseq_enable_post_enable(struct kunit *test)
+{
+ const struct pwrseq_target_data *targets[2] = { };
+ struct platform_device *pdev = test->priv;
+ struct platform_device_info pdevinfo;
+ struct pwrseq_target_data target;
+ struct pwrseq_unit_data unit;
+ struct pwrseq_device *pwrseq;
+ struct pwrseq_config config;
+ struct pwrseq_test_ctx *ctx;
+ struct pwrseq_desc *desc;
+ int ret;
+
+ ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+
+ unit = (struct pwrseq_unit_data){
+ .name = "unit-a",
+ .enable = pwrseq_test_enable_a,
+ .disable = pwrseq_test_disable_a,
+ };
+
+ target = (struct pwrseq_target_data){
+ .name = "test-target",
+ .unit = &unit,
+ .post_enable = pwrseq_test_post_enable,
+ };
+
+ targets[0] = &target;
+
+ ctx->consumer_name = PWRSEQ_TEST_CONSUMER;
+
+ config = (struct pwrseq_config){
+ .parent = &pdev->dev,
+ .drvdata = ctx,
+ .match = pwrseq_test_match_by_name,
+ .targets = targets,
+ };
+
+ pwrseq = kunit_pwrseq_device_register(test, &config);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pwrseq);
+
+ pdevinfo = (struct platform_device_info){
+ .name = PWRSEQ_TEST_CONSUMER,
+ .id = PLATFORM_DEVID_NONE,
+ };
+
+ pdev = kunit_platform_device_register_full(test, &pdevinfo);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
+
+ desc = kunit_pwrseq_get(test, &pdev->dev, "test-target");
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, desc);
+
+ /* Success path: post_enable() runs once and reports success. */
+ ret = pwrseq_enable(desc);
+ KUNIT_EXPECT_EQ(test, ret, 0);
+ KUNIT_EXPECT_EQ(test, ctx->post_enable_calls, 1);
+ KUNIT_EXPECT_EQ(test, ctx->enable_count_a, 1);
+
+ ret = pwrseq_disable(desc);
+ KUNIT_ASSERT_EQ(test, ret, 0);
+
+ /* Failure path: post_enable() fails, unit is rolled back. */
+ ctx->post_enable_error = true;
+ ret = pwrseq_enable(desc);
+ KUNIT_EXPECT_EQ(test, ret, -EIO);
+ KUNIT_EXPECT_EQ(test, ctx->post_enable_calls, 2);
+ KUNIT_EXPECT_EQ(test, ctx->enable_count_a, 2);
+ KUNIT_EXPECT_EQ(test, ctx->disable_count_a, 2);
+
+ /* powered_on must have been cleared. */
+ ret = pwrseq_enable(desc);
+ KUNIT_EXPECT_EQ(test, ret, -EIO);
+ KUNIT_EXPECT_EQ(test, ctx->enable_count_a, 3);
+}
+
+/*
+ * Test that pwrseq_get() returns -ENOENT when the matched provider does not
+ * have the requested target name.
+ */
+static void pwrseq_get_target_not_found(struct kunit *test)
+{
+ const struct pwrseq_target_data *targets[2] = { };
+ struct platform_device *pdev = test->priv;
+ struct platform_device_info pdevinfo;
+ struct pwrseq_target_data target;
+ struct pwrseq_unit_data unit;
+ struct pwrseq_device *pwrseq;
+ struct pwrseq_config config;
+ struct pwrseq_desc *desc;
+
+ unit = (struct pwrseq_unit_data){
+ .name = "unit-a",
+ };
+
+ target = (struct pwrseq_target_data){
+ .name = "real-target",
+ .unit = &unit,
+ };
+
+ targets[0] = &target;
+
+ config = (struct pwrseq_config){
+ .parent = &pdev->dev,
+ .match = pwrseq_test_match_always,
+ .targets = targets,
+ };
+
+ pwrseq = kunit_pwrseq_device_register(test, &config);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pwrseq);
+
+ pdevinfo = (struct platform_device_info){
+ .name = PWRSEQ_TEST_CONSUMER,
+ .id = PLATFORM_DEVID_NONE,
+ };
+
+ pdev = kunit_platform_device_register_full(test, &pdevinfo);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
+
+ desc = kunit_pwrseq_get(test, &pdev->dev, "nonexistent-target");
+ KUNIT_EXPECT_TRUE(test, IS_ERR(desc));
+ KUNIT_EXPECT_EQ(test, PTR_ERR(desc), -ENOENT);
+}
+
+/*
+ * Test that pwrseq_put() on a descriptor that is still powered on disables
+ * it as part of tear-down, instead of leaking the power-on state.
+ */
+static void pwrseq_put_disables_powered_desc(struct kunit *test)
+{
+ const struct pwrseq_target_data *targets[2] = { };
+ struct platform_device *pdev = test->priv;
+ struct platform_device_info pdevinfo;
+ struct pwrseq_target_data target;
+ struct pwrseq_unit_data unit;
+ struct pwrseq_device *pwrseq;
+ struct pwrseq_test_ctx *ctx;
+ struct pwrseq_config config;
+ struct pwrseq_desc *desc;
+ int ret;
+
+ ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+
+ unit = (struct pwrseq_unit_data){
+ .name = "unit-a",
+ .enable = pwrseq_test_enable_a,
+ .disable = pwrseq_test_disable_a,
+ };
+
+ target = (struct pwrseq_target_data){
+ .name = "test-target",
+ .unit = &unit,
+ };
+
+ targets[0] = &target;
+
+ config = (struct pwrseq_config){
+ .parent = &pdev->dev,
+ .drvdata = ctx,
+ .match = pwrseq_test_match_always,
+ .targets = targets,
+ };
+
+ pwrseq = kunit_pwrseq_device_register(test, &config);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pwrseq);
+
+ pdevinfo = (struct platform_device_info){
+ .name = PWRSEQ_TEST_CONSUMER,
+ .id = PLATFORM_DEVID_NONE,
+ };
+
+ pdev = kunit_platform_device_register_full(test, &pdevinfo);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
+
+ desc = kunit_pwrseq_get(test, &pdev->dev, "test-target");
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, desc);
+
+ ret = pwrseq_enable(desc);
+ KUNIT_ASSERT_EQ(test, ret, 0);
+ KUNIT_EXPECT_EQ(test, ctx->disable_count_a, 0);
+
+ /*
+ * Run the deferred pwrseq_put() action now instead of at test
+ * teardown, so its effect can be asserted below.
+ */
+ kunit_release_action(test, pwrseq_put_wrapper, desc);
+ KUNIT_EXPECT_EQ(test, ctx->disable_count_a, 1);
+}
+
+/*
+ * Test that pwrseq_device_unregister() on a target with an active user
+ * triggers "REMOVING POWER SEQUENCER WITH ACTIVE USERS", and that both
+ * pwrseq_enable() and pwrseq_disable() start returning -ENODEV for
+ * previously acquired descriptors afterwards.
+ */
+static void pwrseq_unregister_with_active_user(struct kunit *test)
+{
+ struct platform_device *pdev = test->priv, *cons_a, *cons_b;
+ const struct pwrseq_target_data *targets[2] = { };
+ struct platform_device_info pdevinfo;
+ struct pwrseq_desc *desc_a, *desc_b;
+ struct pwrseq_target_data target;
+ struct pwrseq_unit_data unit;
+ struct pwrseq_device *pwrseq;
+ struct pwrseq_test_ctx *ctx;
+ struct pwrseq_config config;
+ bool bound;
+ int ret;
+
+ ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+
+ unit = (struct pwrseq_unit_data){
+ .name = "unit-a",
+ .enable = pwrseq_test_enable_a,
+ .disable = pwrseq_test_disable_a,
+ };
+
+ target = (struct pwrseq_target_data){
+ .name = "test-target",
+ .unit = &unit,
+ };
+
+ targets[0] = &target;
+
+ config = (struct pwrseq_config){
+ .parent = &pdev->dev,
+ .drvdata = ctx,
+ .match = pwrseq_test_match_always,
+ .targets = targets,
+ };
+
+ pwrseq = kunit_pwrseq_device_register(test, &config);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pwrseq);
+
+ pdevinfo = (struct platform_device_info){
+ .name = PWRSEQ_TEST_CONSUMER,
+ .id = 0,
+ };
+ cons_a = kunit_platform_device_register_full(test, &pdevinfo);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cons_a);
+
+ wait_for_device_probe();
+ scoped_guard(device, &cons_a->dev)
+ bound = device_is_bound(&cons_a->dev);
+
+ KUNIT_ASSERT_FALSE(test, bound);
+
+ pdevinfo.id = 1;
+ cons_b = kunit_platform_device_register_full(test, &pdevinfo);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cons_b);
+
+ wait_for_device_probe();
+ scoped_guard(device, &cons_b->dev)
+ bound = device_is_bound(&cons_b->dev);
+
+ KUNIT_ASSERT_FALSE(test, bound);
+
+ desc_a = kunit_pwrseq_get(test, &cons_a->dev, "test-target");
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, desc_a);
+ desc_b = kunit_pwrseq_get(test, &cons_b->dev, "test-target");
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, desc_b);
+
+ ret = pwrseq_enable(desc_a);
+ KUNIT_ASSERT_EQ(test, ret, 0);
+
+ kunit_warning_suppress(test) {
+ kunit_release_action(test, pwrseq_device_unregister_wrapper, pwrseq);
+ KUNIT_EXPECT_SUPPRESSED_WARNING_COUNT(test, 1);
+ }
+
+ ret = pwrseq_enable(desc_b);
+ KUNIT_EXPECT_EQ(test, ret, -ENODEV);
+
+ ret = pwrseq_disable(desc_a);
+ KUNIT_EXPECT_EQ(test, ret, -ENODEV);
+}
+
+/*
+ * Test that pwrseq_to_device() returns the pwrseq provider's own device,
+ * and NULL for a NULL descriptor.
+ */
+static void pwrseq_to_device_test(struct kunit *test)
+{
+ struct platform_device *parent = test->priv, *cons;
+ const struct pwrseq_target_data *targets[2] = { };
+ struct platform_device_info pdevinfo;
+ struct pwrseq_target_data target;
+ struct pwrseq_unit_data unit;
+ struct pwrseq_device *pwrseq;
+ struct pwrseq_config config;
+ struct pwrseq_desc *desc;
+ struct device *dev;
+
+ unit = (struct pwrseq_unit_data){ .name = "unit-a" };
+ target = (struct pwrseq_target_data){
+ .name = "test-target",
+ .unit = &unit,
+ };
+ targets[0] = &target;
+
+ config = (struct pwrseq_config){
+ .parent = &parent->dev,
+ .match = pwrseq_test_match_always,
+ .targets = targets,
+ };
+
+ pwrseq = kunit_pwrseq_device_register(test, &config);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pwrseq);
+
+ pdevinfo = (struct platform_device_info){
+ .name = PWRSEQ_TEST_CONSUMER,
+ .id = PLATFORM_DEVID_NONE,
+ };
+
+ cons = kunit_platform_device_register_full(test, &pdevinfo);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cons);
+
+ desc = kunit_pwrseq_get(test, &cons->dev, "test-target");
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, desc);
+
+ dev = pwrseq_to_device(desc);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev);
+ KUNIT_EXPECT_PTR_EQ(test, dev->parent, &parent->dev);
+ KUNIT_EXPECT_TRUE(test, device_is_registered(dev));
+
+ KUNIT_EXPECT_PTR_EQ(test, pwrseq_to_device(NULL), NULL);
+}
+
+static struct kunit_case pwrseq_tests[] = {
+ KUNIT_CASE(pwrseq_circular_deps),
+ KUNIT_CASE(pwrseq_circular_deps_chain),
+ KUNIT_CASE(pwrseq_register_valid),
+ KUNIT_CASE(pwrseq_register_invalid_args),
+ KUNIT_CASE(pwrseq_register_target_without_unit),
+ KUNIT_CASE(pwrseq_enable_disable),
+ KUNIT_CASE(pwrseq_shared_deps),
+ KUNIT_CASE(pwrseq_enable_idempotent),
+ KUNIT_CASE(pwrseq_enable_enable_error),
+ KUNIT_CASE(pwrseq_enable_rollback_on_top_failure),
+ KUNIT_CASE(pwrseq_enable_post_enable),
+ KUNIT_CASE(pwrseq_get_target_not_found),
+ KUNIT_CASE(pwrseq_put_disables_powered_desc),
+ KUNIT_CASE(pwrseq_unregister_with_active_user),
+ KUNIT_CASE(pwrseq_to_device_test),
+ { }
+};
+
+static struct kunit_suite pwrseq_test_suite = {
+ .name = "pwrseq",
+ .init = pwrseq_test_parent_init,
+ .test_cases = pwrseq_tests,
+};
+
+/*
+ * Match a consumer to this test provider by resolving its "pwrseq-provider"
+ * software-node reference and comparing it to the provider's own fwnode.
+ */
+static int pwrseq_test_match_by_swnode_ref(struct pwrseq_device *pwrseq,
+ struct device *dev)
+{
+ const struct software_node *provider_swnode;
+ struct fwnode_handle *provider_fwnode, *ref;
+ bool match;
+
+ if (!dev_fwnode(dev))
+ return PWRSEQ_NO_MATCH;
+
+ ref = fwnode_find_reference(dev_fwnode(dev), "pwrseq-provider", 0);
+ if (IS_ERR_OR_NULL(ref))
+ return PWRSEQ_NO_MATCH;
+
+ provider_swnode = pwrseq_device_get_drvdata(pwrseq);
+ provider_fwnode = software_node_fwnode(provider_swnode);
+ match = (ref == provider_fwnode);
+ fwnode_handle_put(ref);
+
+ return match ? PWRSEQ_MATCH_OK : PWRSEQ_NO_MATCH;
+}
+
+static const struct pwrseq_unit_data pwrseq_swnode_test_unit = {
+ .name = "test-target",
+};
+
+static const struct pwrseq_target_data pwrseq_swnode_test_target = {
+ .name = "test-target",
+ .unit = &pwrseq_swnode_test_unit,
+};
+
+static const struct pwrseq_target_data *pwrseq_swnode_test_targets[] = {
+ &pwrseq_swnode_test_target,
+ NULL,
+};
+
+struct pwrseq_swnode_provider_pdata {
+ const struct software_node *provider_swnode;
+};
+
+static int pwrseq_swnode_provider_probe(struct platform_device *pdev)
+{
+ const struct pwrseq_swnode_provider_pdata *pdata = dev_get_platdata(&pdev->dev);
+ struct pwrseq_config config;
+
+ config = (struct pwrseq_config){
+ .parent = &pdev->dev,
+ .drvdata = (void *)pdata->provider_swnode,
+ .match = pwrseq_test_match_by_swnode_ref,
+ .targets = pwrseq_swnode_test_targets,
+ };
+
+ return PTR_ERR_OR_ZERO(devm_pwrseq_device_register(&pdev->dev, &config));
+}
+
+static struct platform_driver pwrseq_swnode_provider_driver = {
+ .probe = pwrseq_swnode_provider_probe,
+ .driver = {
+ .name = PWRSEQ_SWNODE_TEST_PROVIDER,
+ },
+};
+
+static struct platform_driver pwrseq_swnode_provider_driver_2 = {
+ .probe = pwrseq_swnode_provider_probe,
+ .driver = {
+ .name = PWRSEQ_SWNODE_TEST_PROVIDER_2,
+ },
+};
+
+struct pwrseq_probe_order_pdata {
+ unsigned int probe_count;
+ int pwrseq_err;
+};
+
+static const struct pwrseq_probe_order_pdata pwrseq_probe_order_pdata_template;
+
+static int pwrseq_probe_order_consumer_probe(struct platform_device *pdev)
+{
+ struct pwrseq_probe_order_pdata *pdata = dev_get_platdata(&pdev->dev);
+ struct pwrseq_desc *desc;
+
+ pdata->probe_count++;
+
+ desc = devm_pwrseq_get(&pdev->dev, "test-target");
+ pdata->pwrseq_err = PTR_ERR_OR_ZERO(desc);
+ if (IS_ERR(desc))
+ return PTR_ERR(desc);
+
+ return 0;
+}
+
+static struct platform_driver pwrseq_probe_order_consumer_driver = {
+ .probe = pwrseq_probe_order_consumer_probe,
+ .driver = {
+ .name = PWRSEQ_PROBE_ORDER_TEST_CONSUMER,
+ },
+};
+
+static struct platform_driver pwrseq_probe_defer_consumer_driver = {
+ .probe = pwrseq_probe_order_consumer_probe,
+ .driver = {
+ .name = PWRSEQ_PROBE_DEFER_TEST_CONSUMER,
+ },
+};
+
+/*
+ * Verify that driver core orders the probe of a pwrseq consumer after its
+ * provider. The consumer references the provider through a software node and
+ * is registered first and we rely on devlink for ordering.
+ */
+static void pwrseq_swnode_probe_order(struct kunit *test)
+{
+ struct property_entry properties[2] = { };
+ struct pwrseq_probe_order_pdata *pdata;
+ struct platform_device_info pdevinfo;
+ struct platform_device *prvd, *cons;
+ struct fwnode_handle *fwnode;
+ bool bound = false;
+ int ret;
+
+ ret = kunit_platform_driver_register(test, &pwrseq_swnode_provider_driver);
+ KUNIT_ASSERT_EQ(test, ret, 0);
+
+ ret = kunit_platform_driver_register(test, &pwrseq_probe_order_consumer_driver);
+ KUNIT_ASSERT_EQ(test, ret, 0);
+
+ fwnode = kunit_software_node_register(test, &pwrseq_test_provider_swnode);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fwnode);
+
+ properties[0] = PROPERTY_ENTRY_REF("pwrseq-provider",
+ &pwrseq_test_provider_swnode);
+
+ pdevinfo = (struct platform_device_info){
+ .name = PWRSEQ_PROBE_ORDER_TEST_CONSUMER,
+ .id = PLATFORM_DEVID_NONE,
+ .data = &pwrseq_probe_order_pdata_template,
+ .size_data = sizeof(pwrseq_probe_order_pdata_template),
+ .properties = properties,
+ };
+
+ cons = kunit_platform_device_register_full(test, &pdevinfo);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cons);
+
+ wait_for_device_probe();
+ scoped_guard(device, &cons->dev)
+ bound = device_is_bound(&cons->dev);
+
+ KUNIT_ASSERT_FALSE(test, bound);
+
+ pdata = dev_get_platdata(&cons->dev);
+ KUNIT_ASSERT_EQ(test, pdata->probe_count, 0);
+
+ pdevinfo = (struct platform_device_info){
+ .name = PWRSEQ_SWNODE_TEST_PROVIDER,
+ .id = PLATFORM_DEVID_NONE,
+ .swnode = &pwrseq_test_provider_swnode,
+ .data = &(const struct pwrseq_swnode_provider_pdata){
+ .provider_swnode = &pwrseq_test_provider_swnode,
+ },
+ .size_data = sizeof(struct pwrseq_swnode_provider_pdata),
+ };
+
+ prvd = kunit_platform_device_register_full(test, &pdevinfo);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, prvd);
+
+ wait_for_device_probe();
+
+ scoped_guard(device, &prvd->dev)
+ bound = device_is_bound(&prvd->dev);
+ KUNIT_ASSERT_TRUE(test, bound);
+
+ scoped_guard(device, &cons->dev)
+ bound = device_is_bound(&cons->dev);
+ KUNIT_ASSERT_TRUE(test, bound);
+
+ pdata = dev_get_platdata(&cons->dev);
+ KUNIT_EXPECT_EQ(test, pdata->probe_count, 1);
+ KUNIT_EXPECT_EQ(test, pdata->pwrseq_err, 0);
+}
+
+/*
+ * Verify that a pwrseq consumer referencing a provider whose software node is
+ * not registered yet, defers its probe instead of failing.
+ *
+ * The provider software node is deliberately left unregistered when the
+ * consumer is added. fw_devlink cannot resolve the reference, so it creates no
+ * supplier link and does not order the consumer - the consumer's probe() runs
+ * and calls pwrseq_get(), which finds no matching provider and returns
+ * -EPROBE_DEFER. Once the provider software node and device appear, the
+ * deferred consumer probes again and binds.
+ */
+static void pwrseq_swnode_probe_defer_on_unregistered(struct kunit *test)
+{
+ struct property_entry properties[2] = { };
+ struct pwrseq_probe_order_pdata *pdata;
+ struct platform_device_info pdevinfo;
+ struct platform_device *prvd, *cons;
+ struct fwnode_handle *fwnode;
+ bool bound = false;
+ int ret;
+
+ ret = kunit_platform_driver_register(test, &pwrseq_swnode_provider_driver_2);
+ KUNIT_ASSERT_EQ(test, ret, 0);
+
+ ret = kunit_platform_driver_register(test, &pwrseq_probe_defer_consumer_driver);
+ KUNIT_ASSERT_EQ(test, ret, 0);
+
+ properties[0] = PROPERTY_ENTRY_REF("pwrseq-provider",
+ &pwrseq_test_provider_swnode);
+
+ pdevinfo = (struct platform_device_info){
+ .name = PWRSEQ_PROBE_DEFER_TEST_CONSUMER,
+ .id = PLATFORM_DEVID_NONE,
+ .data = &pwrseq_probe_order_pdata_template,
+ .size_data = sizeof(pwrseq_probe_order_pdata_template),
+ .properties = properties,
+ };
+
+ cons = kunit_platform_device_register_full(test, &pdevinfo);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cons);
+
+ wait_for_device_probe();
+ scoped_guard(device, &cons->dev)
+ bound = device_is_bound(&cons->dev);
+
+ KUNIT_ASSERT_FALSE(test, bound);
+
+ pdata = dev_get_platdata(&cons->dev);
+ KUNIT_ASSERT_GT(test, pdata->probe_count, 0);
+ KUNIT_ASSERT_EQ(test, pdata->pwrseq_err, -EPROBE_DEFER);
+
+ fwnode = kunit_software_node_register(test, &pwrseq_test_provider_swnode);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fwnode);
+
+ pdevinfo = (struct platform_device_info){
+ .name = PWRSEQ_SWNODE_TEST_PROVIDER_2,
+ .id = PLATFORM_DEVID_NONE,
+ .swnode = &pwrseq_test_provider_swnode,
+ .data = &(const struct pwrseq_swnode_provider_pdata){
+ .provider_swnode = &pwrseq_test_provider_swnode,
+ },
+ .size_data = sizeof(struct pwrseq_swnode_provider_pdata),
+ };
+
+ prvd = kunit_platform_device_register_full(test, &pdevinfo);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, prvd);
+
+ wait_for_device_probe();
+
+ scoped_guard(device, &prvd->dev)
+ bound = device_is_bound(&prvd->dev);
+ KUNIT_ASSERT_TRUE(test, bound);
+
+ scoped_guard(device, &cons->dev)
+ bound = device_is_bound(&cons->dev);
+ KUNIT_ASSERT_TRUE(test, bound);
+
+ pdata = dev_get_platdata(&cons->dev);
+ KUNIT_EXPECT_EQ(test, pdata->pwrseq_err, 0);
+}
+
+static int pwrseq_swnode_test_init(struct kunit *test)
+{
+ /*
+ * A device link teardown from an earlier test case, or from this
+ * suite's previous module load, may still be queued on device_link_mq.
+ * Flush it so software_node_register() below doesn't spuriously see
+ * the about-to-be-reused node name as still taken.
+ */
+ device_link_wait_removal();
+
+ return 0;
+}
+
+static struct kunit_case pwrseq_swnode_probe_order_tests[] = {
+ KUNIT_CASE(pwrseq_swnode_probe_order),
+ KUNIT_CASE(pwrseq_swnode_probe_defer_on_unregistered),
+ { }
+};
+
+static struct kunit_suite pwrseq_swnode_probe_order_test_suite = {
+ .name = "pwrseq-swnode-probe-order",
+ .init = pwrseq_swnode_test_init,
+ .test_cases = pwrseq_swnode_probe_order_tests,
+};
+
+kunit_test_suites(&pwrseq_test_suite,
+ &pwrseq_swnode_probe_order_test_suite);
+
+MODULE_DESCRIPTION("KUnit test cases for the power sequencing subsystem");
+MODULE_AUTHOR("Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>");
+MODULE_LICENSE("GPL");
diff --git a/drivers/power/sequencing/pwrseq-pcie-m2.c b/drivers/power/sequencing/pwrseq-pcie-m2.c
new file mode 100644
index 000000000..cdadb6617
--- /dev/null
+++ b/drivers/power/sequencing/pwrseq-pcie-m2.c
@@ -0,0 +1,653 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+ * Author: Manivannan Sadhasivam <manivannan.sadhasivam@oss.qualcomm.com>
+ */
+
+#include <linux/device.h>
+#include <linux/delay.h>
+#include <linux/gpio/consumer.h>
+#include <linux/list.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/of_graph.h>
+#include <linux/of_platform.h>
+#include <linux/pci.h>
+#include <linux/platform_device.h>
+#include <linux/pwrseq/provider.h>
+#include <linux/regulator/consumer.h>
+#include <linux/serdev.h>
+#include <linux/slab.h>
+
+struct pwrseq_pci_dev {
+ struct serdev_device *serdev;
+ struct of_changeset *ocs;
+ struct pci_dev *pdev;
+ struct list_head list;
+};
+
+struct pwrseq_pcie_m2_pdata {
+ const struct pwrseq_target_data **targets;
+};
+
+struct pwrseq_pcie_m2_ctx {
+ struct pwrseq_device *pwrseq;
+ struct device_node *of_node;
+ const struct pwrseq_pcie_m2_pdata *pdata;
+ struct regulator_bulk_data *regs;
+ size_t num_vregs;
+ struct notifier_block nb;
+ struct gpio_desc *w_disable1_gpio;
+ struct gpio_desc *w_disable2_gpio;
+ struct device *dev;
+ struct list_head pci_devices;
+ struct mutex list_lock;
+};
+
+static int pwrseq_pcie_m2_vregs_enable(struct pwrseq_device *pwrseq)
+{
+ struct pwrseq_pcie_m2_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
+
+ return regulator_bulk_enable(ctx->num_vregs, ctx->regs);
+}
+
+static int pwrseq_pcie_m2_vregs_disable(struct pwrseq_device *pwrseq)
+{
+ struct pwrseq_pcie_m2_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
+
+ return regulator_bulk_disable(ctx->num_vregs, ctx->regs);
+}
+
+static const struct pwrseq_unit_data pwrseq_pcie_m2_vregs_unit_data = {
+ .name = "regulators-enable",
+ .enable = pwrseq_pcie_m2_vregs_enable,
+ .disable = pwrseq_pcie_m2_vregs_disable,
+};
+
+static const struct pwrseq_unit_data *pwrseq_pcie_m2_unit_deps[] = {
+ &pwrseq_pcie_m2_vregs_unit_data,
+ NULL
+};
+
+static int pwrseq_pci_m2_e_uart_enable(struct pwrseq_device *pwrseq)
+{
+ struct pwrseq_pcie_m2_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
+
+ return gpiod_set_value_cansleep(ctx->w_disable2_gpio, 0);
+}
+
+static int pwrseq_pci_m2_e_uart_disable(struct pwrseq_device *pwrseq)
+{
+ struct pwrseq_pcie_m2_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
+
+ return gpiod_set_value_cansleep(ctx->w_disable2_gpio, 1);
+}
+
+static bool pwrseq_pci_m2_e_uart_is_controllable(struct pwrseq_device *pwrseq)
+{
+ struct pwrseq_pcie_m2_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
+
+ /*
+ * The UART enable is driven through the W_DISABLE2# line. When it is not
+ * wired up on this connector the enable/disable callbacks are no-ops, so
+ * the host cannot gate the Bluetooth function on its own.
+ */
+ return !!ctx->w_disable2_gpio;
+}
+
+static const struct pwrseq_unit_data pwrseq_pcie_m2_e_uart_unit_data = {
+ .name = "uart-enable",
+ .deps = pwrseq_pcie_m2_unit_deps,
+ .enable = pwrseq_pci_m2_e_uart_enable,
+ .disable = pwrseq_pci_m2_e_uart_disable,
+};
+
+static int pwrseq_pci_m2_e_pcie_enable(struct pwrseq_device *pwrseq)
+{
+ struct pwrseq_pcie_m2_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
+
+ return gpiod_set_value_cansleep(ctx->w_disable1_gpio, 0);
+}
+
+static int pwrseq_pci_m2_e_pcie_disable(struct pwrseq_device *pwrseq)
+{
+ struct pwrseq_pcie_m2_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
+
+ return gpiod_set_value_cansleep(ctx->w_disable1_gpio, 1);
+}
+
+static bool pwrseq_pci_m2_e_pcie_is_controllable(struct pwrseq_device *pwrseq)
+{
+ struct pwrseq_pcie_m2_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
+
+ /*
+ * The PCIe/WiFi enable is driven through the W_DISABLE1# line. When it
+ * is not wired up on this connector the enable/disable callbacks are
+ * no-ops, so the host cannot gate the PCIe/WiFi function on its own.
+ */
+ return !!ctx->w_disable1_gpio;
+}
+
+static const struct pwrseq_unit_data pwrseq_pcie_m2_e_pcie_unit_data = {
+ .name = "pcie-enable",
+ .deps = pwrseq_pcie_m2_unit_deps,
+ .enable = pwrseq_pci_m2_e_pcie_enable,
+ .disable = pwrseq_pci_m2_e_pcie_disable,
+};
+
+static const struct pwrseq_unit_data pwrseq_pcie_m2_m_pcie_unit_data = {
+ .name = "pcie-enable",
+ .deps = pwrseq_pcie_m2_unit_deps,
+};
+
+static int pwrseq_pcie_m2_e_pwup_delay(struct pwrseq_device *pwrseq)
+{
+ /*
+ * FIXME: This delay is only required for some Qcom WLAN/BT cards like
+ * WCN7850 and not for all devices. But currently, there is no way to
+ * identify the device model before enumeration.
+ */
+ msleep(50);
+
+ return 0;
+}
+
+static const struct pwrseq_target_data pwrseq_pcie_m2_e_uart_target_data = {
+ .name = "uart",
+ .unit = &pwrseq_pcie_m2_e_uart_unit_data,
+ .post_enable = pwrseq_pcie_m2_e_pwup_delay,
+ .is_controllable = pwrseq_pci_m2_e_uart_is_controllable,
+};
+
+static const struct pwrseq_target_data pwrseq_pcie_m2_e_pcie_target_data = {
+ .name = "pcie",
+ .unit = &pwrseq_pcie_m2_e_pcie_unit_data,
+ .post_enable = pwrseq_pcie_m2_e_pwup_delay,
+ .is_controllable = pwrseq_pci_m2_e_pcie_is_controllable,
+};
+
+static const struct pwrseq_target_data pwrseq_pcie_m2_m_pcie_target_data = {
+ .name = "pcie",
+ .unit = &pwrseq_pcie_m2_m_pcie_unit_data,
+};
+
+static const struct pwrseq_target_data *pwrseq_pcie_m2_e_targets[] = {
+ &pwrseq_pcie_m2_e_pcie_target_data,
+ &pwrseq_pcie_m2_e_uart_target_data,
+ NULL
+};
+
+static const struct pwrseq_target_data *pwrseq_pcie_m2_m_targets[] = {
+ &pwrseq_pcie_m2_m_pcie_target_data,
+ NULL
+};
+
+static const struct pwrseq_pcie_m2_pdata pwrseq_pcie_m2_e_of_data = {
+ .targets = pwrseq_pcie_m2_e_targets,
+};
+
+static const struct pwrseq_pcie_m2_pdata pwrseq_pcie_m2_m_of_data = {
+ .targets = pwrseq_pcie_m2_m_targets,
+};
+
+static int pwrseq_pcie_m2_match(struct pwrseq_device *pwrseq,
+ struct device *dev)
+{
+ struct pwrseq_pcie_m2_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
+ struct device_node *endpoint __free(device_node) = NULL;
+
+ /*
+ * Traverse the 'remote-endpoint' nodes and check if the remote node's
+ * parent matches the OF node of 'dev'.
+ */
+ for_each_endpoint_of_node(ctx->of_node, endpoint) {
+ struct device_node *remote __free(device_node) =
+ of_graph_get_remote_port_parent(endpoint);
+ if (remote && (remote == dev_of_node(dev)))
+ return PWRSEQ_MATCH_OK;
+ }
+
+ return PWRSEQ_NO_MATCH;
+}
+
+static const struct pci_device_id pwrseq_m2_pci_ids[] = {
+ { PCI_DEVICE(PCI_VENDOR_ID_MARVELL_EXT, 0x2b43),
+ .driver_data = (kernel_ulong_t)"nxp,88w8987-bt" },
+ { PCI_DEVICE(PCI_VENDOR_ID_PHILIPS, 0x3003),
+ .driver_data = (kernel_ulong_t)"nxp,88w8987-bt" },
+ { PCI_DEVICE_SUB(PCI_VENDOR_ID_QCOM, 0x1103, PCI_VENDOR_ID_QCOM, 0x0108),
+ .driver_data = (kernel_ulong_t)"qcom,qca2066-bt" },
+ { PCI_DEVICE_SUB(PCI_VENDOR_ID_QCOM, 0x1103, PCI_VENDOR_ID_FOXCONN, 0xe105),
+ .driver_data = (kernel_ulong_t)"qcom,wcn6855-bt" },
+ { PCI_DEVICE_SUB(PCI_VENDOR_ID_QCOM, 0x1103, PCI_VENDOR_ID_QCOM, 0x337e),
+ .driver_data = (kernel_ulong_t)"qcom,wcn6855-bt" },
+ { PCI_DEVICE_SUB(PCI_VENDOR_ID_QCOM, 0x1107, PCI_VENDOR_ID_QCOM, 0x337c),
+ .driver_data = (kernel_ulong_t)"qcom,wcn7850-bt" },
+ { PCI_DEVICE_SUB(PCI_VENDOR_ID_QCOM, 0x1107, PCI_VENDOR_ID_LENOVO, 0xe0e9),
+ .driver_data = (kernel_ulong_t)"qcom,wcn7850-bt" },
+ { PCI_DEVICE_SUB(PCI_VENDOR_ID_QCOM, 0x1107, PCI_VENDOR_ID_FOXCONN, 0xe0ee),
+ .driver_data = (kernel_ulong_t)"qcom,wcn7850-bt" },
+ { } /* Sentinel */
+};
+
+static int pwrseq_pcie_m2_create_bt_node(struct pwrseq_pcie_m2_ctx *ctx,
+ struct pwrseq_pci_dev *pci_dev,
+ struct device_node *parent,
+ struct pci_dev *pdev)
+{
+ const struct pci_device_id *id;
+ struct device *dev = ctx->dev;
+ const char *compatible;
+ struct device_node *np;
+ int ret;
+
+ id = pci_match_id(pwrseq_m2_pci_ids, pdev);
+ if (WARN_ON_ONCE(!id)) /* Shouldn't happen */
+ return -ENODEV;
+
+ compatible = (const char *)id->driver_data;
+
+ pci_dev->ocs = kzalloc_obj(*pci_dev->ocs);
+ if (!pci_dev->ocs)
+ return -ENOMEM;
+
+ of_changeset_init(pci_dev->ocs);
+
+ np = of_changeset_create_node(pci_dev->ocs, parent, "bluetooth");
+ if (!np) {
+ dev_err(dev, "Failed to create bluetooth node\n");
+ ret = -ENODEV;
+ goto err_destroy_changeset;
+ }
+
+ ret = of_changeset_add_prop_string(pci_dev->ocs, np, "compatible", compatible);
+ if (ret) {
+ dev_err(dev, "Failed to add bluetooth compatible: %d\n", ret);
+ goto err_destroy_changeset;
+ }
+
+ ret = of_changeset_apply(pci_dev->ocs);
+ if (ret) {
+ dev_err(dev, "Failed to apply changeset: %d\n", ret);
+ goto err_destroy_changeset;
+ }
+
+ ret = device_add_of_node(&pci_dev->serdev->dev, np);
+ if (ret) {
+ dev_err(dev, "Failed to add OF node: %d\n", ret);
+ goto err_revert_changeset;
+ }
+
+ return 0;
+
+err_revert_changeset:
+ of_changeset_revert(pci_dev->ocs);
+err_destroy_changeset:
+ of_changeset_destroy(pci_dev->ocs);
+ kfree(pci_dev->ocs);
+ pci_dev->ocs = NULL;
+
+ return ret;
+}
+
+static int pwrseq_pcie_m2_create_serdev_one(struct pwrseq_pcie_m2_ctx *ctx,
+ struct pci_dev *pdev)
+{
+ struct serdev_controller *serdev_ctrl;
+ struct device *dev = ctx->dev;
+ struct pwrseq_pci_dev *pci_dev;
+ int ret = 0;
+
+ struct device_node *serdev_parent __free(device_node) =
+ of_graph_get_remote_node(dev_of_node(ctx->dev), 3, 0);
+ if (!serdev_parent)
+ return 0;
+
+ serdev_ctrl = of_find_serdev_controller_by_node(serdev_parent);
+ if (!serdev_ctrl)
+ return 0;
+
+ /* Bail out if the device was already attached to this controller */
+ if (serdev_ctrl->serdev)
+ goto err_put_ctrl;
+
+ /* Bail out if the serdev device was already created for the PCI dev */
+ scoped_guard(mutex, &ctx->list_lock) {
+ list_for_each_entry(pci_dev, &ctx->pci_devices, list) {
+ if (pci_dev->pdev == pdev)
+ goto err_put_ctrl;
+ }
+ }
+
+ pci_dev = kzalloc_obj(*pci_dev);
+ if (!pci_dev) {
+ ret = -ENOMEM;
+ goto err_put_ctrl;
+ }
+
+ pci_dev->serdev = serdev_device_alloc(serdev_ctrl);
+ if (!pci_dev->serdev) {
+ ret = -ENOMEM;
+ goto err_free_pci_dev;
+ }
+
+ ret = pwrseq_pcie_m2_create_bt_node(ctx, pci_dev, serdev_parent, pdev);
+ if (ret)
+ goto err_free_serdev;
+
+ ret = serdev_device_add(pci_dev->serdev);
+ if (ret) {
+ dev_err(dev, "Failed to add serdev for PCI device (%s): %d\n",
+ pci_name(pdev), ret);
+ goto err_free_dt_node;
+ }
+
+ serdev_controller_put(serdev_ctrl);
+
+ pci_dev->pdev = pci_dev_get(pdev);
+
+ mutex_lock(&ctx->list_lock);
+ list_add_tail(&pci_dev->list, &ctx->pci_devices);
+ mutex_unlock(&ctx->list_lock);
+
+ return 0;
+
+err_free_dt_node:
+ device_remove_of_node(&pci_dev->serdev->dev);
+ of_changeset_revert(pci_dev->ocs);
+ of_changeset_destroy(pci_dev->ocs);
+ kfree(pci_dev->ocs);
+ pci_dev->ocs = NULL;
+err_free_serdev:
+ serdev_device_put(pci_dev->serdev);
+ pci_dev->serdev = NULL;
+err_free_pci_dev:
+ kfree(pci_dev);
+err_put_ctrl:
+ serdev_controller_put(serdev_ctrl);
+
+ return ret;
+}
+
+static void __pwrseq_pcie_m2_remove_serdev(struct pwrseq_pcie_m2_ctx *ctx,
+ struct pwrseq_pci_dev *pci_dev)
+{
+ if (pci_dev->serdev) {
+ device_remove_of_node(&pci_dev->serdev->dev);
+ serdev_device_remove(pci_dev->serdev);
+ }
+
+ if (pci_dev->ocs) {
+ of_changeset_revert(pci_dev->ocs);
+ of_changeset_destroy(pci_dev->ocs);
+ kfree(pci_dev->ocs);
+ }
+
+ pci_dev_put(pci_dev->pdev);
+ list_del(&pci_dev->list);
+ kfree(pci_dev);
+}
+
+static void pwrseq_pcie_m2_remove_serdev(struct pwrseq_pcie_m2_ctx *ctx,
+ struct pci_dev *pdev)
+{
+ struct pwrseq_pci_dev *pci_dev, *tmp;
+
+ mutex_lock(&ctx->list_lock);
+ list_for_each_entry_safe(pci_dev, tmp, &ctx->pci_devices, list) {
+ if (!pdev || pci_dev->pdev == pdev) {
+ __pwrseq_pcie_m2_remove_serdev(ctx, pci_dev);
+ if (pdev)
+ break;
+ }
+ }
+ mutex_unlock(&ctx->list_lock);
+}
+
+static bool pwrseq_pcie_m2_pci_parent_matches(struct pci_dev *pdev,
+ struct device_node *pci_parent)
+{
+ struct device *dev = pdev->dev.parent;
+
+ while (dev) {
+ if (dev->of_node == pci_parent)
+ return true;
+ if (!dev_is_pci(dev))
+ break;
+ dev = dev->parent;
+ }
+ return false;
+}
+
+static int pwrseq_pcie_m2_notify(struct notifier_block *nb, unsigned long action,
+ void *data)
+{
+ struct pwrseq_pcie_m2_ctx *ctx = container_of(nb, struct pwrseq_pcie_m2_ctx, nb);
+ struct pci_dev *pdev = to_pci_dev(data);
+ int ret;
+
+ /*
+ * Check whether the PCI device is associated with this M.2 connector or
+ * not, by comparing the OF node of the PCI device parent and the Port 0
+ * (PCIe) remote node parent OF node.
+ */
+ struct device_node *pci_parent __free(device_node) =
+ of_graph_get_remote_node(dev_of_node(ctx->dev), 0, 0);
+ if (!pci_parent || !pwrseq_pcie_m2_pci_parent_matches(pdev, pci_parent))
+ return NOTIFY_DONE;
+
+ switch (action) {
+ case BUS_NOTIFY_ADD_DEVICE:
+ if (pci_match_id(pwrseq_m2_pci_ids, pdev)) {
+ ret = pwrseq_pcie_m2_create_serdev_one(ctx, pdev);
+ if (ret)
+ return notifier_from_errno(ret);
+ }
+ break;
+ case BUS_NOTIFY_REMOVED_DEVICE:
+ if (pci_match_id(pwrseq_m2_pci_ids, pdev))
+ pwrseq_pcie_m2_remove_serdev(ctx, pdev);
+
+ break;
+ }
+
+ return NOTIFY_OK;
+}
+
+static bool pwrseq_pcie_m2_check_remote_node(struct device *dev, u8 port, u8 endpoint,
+ const char *node)
+{
+ struct device_node *remote __free(device_node) =
+ of_graph_get_remote_node(dev_of_node(dev), port, endpoint);
+
+ if (remote && of_node_name_eq(remote, node))
+ return true;
+
+ return false;
+}
+
+/*
+ * If the connector exposes a non-discoverable bus like UART, the respective
+ * protocol device needs to be created manually with the help of the notifier
+ * of the discoverable bus like PCIe.
+ */
+static int pwrseq_pcie_m2_register_notifier(struct pwrseq_pcie_m2_ctx *ctx)
+{
+ int ret;
+
+ /*
+ * Register a PCI notifier for Key E connector that has PCIe as Port
+ * 0/Endpoint 0 interface and Serial as Port 3/Endpoint 0 interface.
+ */
+ if (!pwrseq_pcie_m2_check_remote_node(ctx->dev, 3, 0, "serial") ||
+ !pwrseq_pcie_m2_check_remote_node(ctx->dev, 0, 0, "pcie"))
+ return 0;
+
+ ctx->nb.notifier_call = pwrseq_pcie_m2_notify;
+ ret = bus_register_notifier(&pci_bus_type, &ctx->nb);
+ if (ret)
+ return dev_err_probe(ctx->dev, ret,
+ "Failed to register notifier for serdev\n");
+ return 0;
+}
+
+static int pwrseq_pcie_m2_create_serdev(struct pwrseq_pcie_m2_ctx *ctx)
+{
+ struct pci_dev *pdev = NULL;
+ int ret;
+
+ if (!pwrseq_pcie_m2_check_remote_node(ctx->dev, 3, 0, "serial") ||
+ !pwrseq_pcie_m2_check_remote_node(ctx->dev, 0, 0, "pcie"))
+ return 0;
+
+ struct device_node *pci_parent __free(device_node) =
+ of_graph_get_remote_node(dev_of_node(ctx->dev), 0, 0);
+ if (!pci_parent)
+ return 0;
+
+ /* Create serdev for existing PCI devices if required */
+ for_each_pci_dev(pdev) {
+ if (!pwrseq_pcie_m2_pci_parent_matches(pdev, pci_parent))
+ continue;
+
+ if (!pci_match_id(pwrseq_m2_pci_ids, pdev))
+ continue;
+
+ ret = pwrseq_pcie_m2_create_serdev_one(ctx, pdev);
+ if (ret) {
+ dev_err_probe(ctx->dev, ret,
+ "Failed to create serdev for PCI device (%s)\n",
+ pci_name(pdev));
+ pci_dev_put(pdev);
+ goto err_remove_serdev;
+ }
+ }
+
+ return 0;
+
+err_remove_serdev:
+ pwrseq_pcie_m2_remove_serdev(ctx, NULL);
+
+ return ret;
+}
+
+static void pwrseq_pcie_m2_free_regulators(void *data)
+{
+ struct pwrseq_pcie_m2_ctx *ctx = data;
+
+ regulator_bulk_free(ctx->num_vregs, ctx->regs);
+ kfree(ctx->regs);
+}
+
+static int pwrseq_pcie_m2_probe(struct platform_device *pdev)
+{
+ struct device *dev = &pdev->dev;
+ struct pwrseq_pcie_m2_ctx *ctx;
+ struct pwrseq_config config = {};
+ int ret;
+
+ ctx = devm_kzalloc_obj(dev, *ctx);
+ if (!ctx)
+ return -ENOMEM;
+
+ platform_set_drvdata(pdev, ctx);
+ ctx->of_node = dev_of_node(dev);
+ ctx->pdata = device_get_match_data(dev);
+ if (!ctx->pdata)
+ return dev_err_probe(dev, -ENODEV,
+ "Failed to obtain platform data\n");
+
+ /*
+ * Currently, of_regulator_bulk_get_all() is the only regulator API that
+ * allows to get all supplies in the devicetree node without manually
+ * specifying them.
+ */
+ ret = of_regulator_bulk_get_all(dev, dev_of_node(dev), &ctx->regs);
+ if (ret < 0)
+ return dev_err_probe(dev, ret,
+ "Failed to get all regulators\n");
+
+ ctx->num_vregs = ret;
+
+ ret = devm_add_action_or_reset(dev, pwrseq_pcie_m2_free_regulators, ctx);
+ if (ret)
+ return ret;
+
+ ctx->w_disable1_gpio = devm_gpiod_get_optional(dev, "w-disable1", GPIOD_OUT_HIGH);
+ if (IS_ERR(ctx->w_disable1_gpio))
+ return dev_err_probe(dev, PTR_ERR(ctx->w_disable1_gpio),
+ "Failed to get the W_DISABLE_1# GPIO\n");
+
+ ctx->w_disable2_gpio = devm_gpiod_get_optional(dev, "w-disable2", GPIOD_OUT_HIGH);
+ if (IS_ERR(ctx->w_disable2_gpio))
+ return dev_err_probe(dev, PTR_ERR(ctx->w_disable2_gpio),
+ "Failed to get the W_DISABLE_2# GPIO\n");
+
+ config.parent = dev;
+ config.owner = THIS_MODULE;
+ config.drvdata = ctx;
+ config.match = pwrseq_pcie_m2_match;
+ config.targets = ctx->pdata->targets;
+
+ ctx->pwrseq = devm_pwrseq_device_register(dev, &config);
+ if (IS_ERR(ctx->pwrseq))
+ return dev_err_probe(dev, PTR_ERR(ctx->pwrseq),
+ "Failed to register the power sequencer\n");
+
+ ret = devm_mutex_init(dev, &ctx->list_lock);
+ if (ret)
+ return ret;
+
+ INIT_LIST_HEAD(&ctx->pci_devices);
+ ctx->dev = dev;
+
+ /* Create serdev for available PCI devices (if required) */
+ ret = pwrseq_pcie_m2_create_serdev(ctx);
+ if (ret)
+ return ret;
+
+ /*
+ * Register a notifier for creating protocol devices for
+ * non-discoverable busses like UART.
+ */
+ ret = pwrseq_pcie_m2_register_notifier(ctx);
+ if (ret)
+ pwrseq_pcie_m2_remove_serdev(ctx, NULL);
+
+ return ret;
+}
+
+static void pwrseq_pcie_m2_remove(struct platform_device *pdev)
+{
+ struct pwrseq_pcie_m2_ctx *ctx = platform_get_drvdata(pdev);
+
+ bus_unregister_notifier(&pci_bus_type, &ctx->nb);
+ pwrseq_pcie_m2_remove_serdev(ctx, NULL);
+}
+
+static const struct of_device_id pwrseq_pcie_m2_of_match[] = {
+ {
+ .compatible = "pcie-m2-m-connector",
+ .data = &pwrseq_pcie_m2_m_of_data,
+ },
+ {
+ .compatible = "pcie-m2-e-connector",
+ .data = &pwrseq_pcie_m2_e_of_data,
+ },
+ { }
+};
+MODULE_DEVICE_TABLE(of, pwrseq_pcie_m2_of_match);
+
+static struct platform_driver pwrseq_pcie_m2_driver = {
+ .driver = {
+ .name = "pwrseq-pcie-m2",
+ .of_match_table = pwrseq_pcie_m2_of_match,
+ },
+ .probe = pwrseq_pcie_m2_probe,
+ .remove = pwrseq_pcie_m2_remove,
+};
+module_platform_driver(pwrseq_pcie_m2_driver);
+
+MODULE_AUTHOR("Manivannan Sadhasivam <manivannan.sadhasivam@oss.qualcomm.com>");
+MODULE_DESCRIPTION("Power Sequencing driver for PCIe M.2 connector");
+MODULE_LICENSE("GPL");
diff --git a/drivers/power/sequencing/pwrseq-qcom-wcn.c b/drivers/power/sequencing/pwrseq-qcom-wcn.c
new file mode 100644
index 000000000..e51b655ad
--- /dev/null
+++ b/drivers/power/sequencing/pwrseq-qcom-wcn.c
@@ -0,0 +1,613 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2024 Linaro Ltd.
+ */
+
+#include <linux/clk.h>
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/gpio/consumer.h>
+#include <linux/jiffies.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/property.h>
+#include <linux/regulator/consumer.h>
+#include <linux/pwrseq/provider.h>
+#include <linux/string.h>
+#include <linux/types.h>
+
+struct pwrseq_qcom_wcn_pdata {
+ const char *const *vregs;
+ size_t num_vregs;
+ unsigned int pwup_delay_ms;
+ unsigned int gpio_enable_delay_ms;
+ const struct pwrseq_target_data **targets;
+ bool has_vddio; /* separate VDD IO regulator */
+ int (*match)(struct pwrseq_device *pwrseq, struct device *dev);
+};
+
+struct pwrseq_qcom_wcn_ctx {
+ struct pwrseq_device *pwrseq;
+ struct device_node *of_node;
+ const struct pwrseq_qcom_wcn_pdata *pdata;
+ struct regulator_bulk_data *regs;
+ struct regulator *vddio;
+ struct gpio_desc *bt_gpio;
+ struct gpio_desc *wlan_gpio;
+ struct gpio_desc *xo_clk_gpio;
+ struct clk *clk;
+ unsigned long last_gpio_enable_jf;
+};
+
+static void pwrseq_qcom_wcn_ensure_gpio_delay(struct pwrseq_qcom_wcn_ctx *ctx)
+{
+ unsigned long diff_jiffies;
+ unsigned int diff_msecs;
+
+ if (!ctx->pdata->gpio_enable_delay_ms)
+ return;
+
+ diff_jiffies = jiffies - ctx->last_gpio_enable_jf;
+ diff_msecs = jiffies_to_msecs(diff_jiffies);
+
+ if (diff_msecs < ctx->pdata->gpio_enable_delay_ms)
+ msleep(ctx->pdata->gpio_enable_delay_ms - diff_msecs);
+}
+
+static int pwrseq_qcom_wcn_vddio_enable(struct pwrseq_device *pwrseq)
+{
+ struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
+
+ return regulator_enable(ctx->vddio);
+}
+
+static int pwrseq_qcom_wcn_vddio_disable(struct pwrseq_device *pwrseq)
+{
+ struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
+
+ return regulator_disable(ctx->vddio);
+}
+
+static const struct pwrseq_unit_data pwrseq_qcom_wcn_vddio_unit_data = {
+ .name = "vddio-enable",
+ .enable = pwrseq_qcom_wcn_vddio_enable,
+ .disable = pwrseq_qcom_wcn_vddio_disable,
+};
+
+static int pwrseq_qcom_wcn_vregs_enable(struct pwrseq_device *pwrseq)
+{
+ struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
+
+ return regulator_bulk_enable(ctx->pdata->num_vregs, ctx->regs);
+}
+
+static int pwrseq_qcom_wcn_vregs_disable(struct pwrseq_device *pwrseq)
+{
+ struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
+
+ return regulator_bulk_disable(ctx->pdata->num_vregs, ctx->regs);
+}
+
+static const struct pwrseq_unit_data pwrseq_qcom_wcn_vregs_unit_data = {
+ .name = "regulators-enable",
+ .enable = pwrseq_qcom_wcn_vregs_enable,
+ .disable = pwrseq_qcom_wcn_vregs_disable,
+};
+
+static int pwrseq_qcom_wcn_clk_enable(struct pwrseq_device *pwrseq)
+{
+ struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
+
+ return clk_prepare_enable(ctx->clk);
+}
+
+static int pwrseq_qcom_wcn_clk_disable(struct pwrseq_device *pwrseq)
+{
+ struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
+
+ clk_disable_unprepare(ctx->clk);
+
+ return 0;
+}
+
+static const struct pwrseq_unit_data pwrseq_qcom_wcn_clk_unit_data = {
+ .name = "clock-enable",
+ .enable = pwrseq_qcom_wcn_clk_enable,
+ .disable = pwrseq_qcom_wcn_clk_disable,
+};
+
+static const struct pwrseq_unit_data *pwrseq_qcom_wcn3990_unit_deps[] = {
+ &pwrseq_qcom_wcn_vddio_unit_data,
+ &pwrseq_qcom_wcn_vregs_unit_data,
+ NULL,
+};
+
+static const struct pwrseq_unit_data pwrseq_qcom_wcn3990_unit_data = {
+ .name = "clock-enable",
+ .deps = pwrseq_qcom_wcn3990_unit_deps,
+ .enable = pwrseq_qcom_wcn_clk_enable,
+ .disable = pwrseq_qcom_wcn_clk_disable,
+};
+
+static const struct pwrseq_unit_data *pwrseq_qcom_wcn_unit_deps[] = {
+ &pwrseq_qcom_wcn_vregs_unit_data,
+ &pwrseq_qcom_wcn_clk_unit_data,
+ NULL
+};
+
+static int pwrseq_qcom_wcn6855_clk_assert(struct pwrseq_device *pwrseq)
+{
+ struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
+
+ if (!ctx->xo_clk_gpio)
+ return 0;
+
+ msleep(1);
+
+ gpiod_set_value_cansleep(ctx->xo_clk_gpio, 1);
+ usleep_range(100, 200);
+
+ return 0;
+}
+
+static const struct pwrseq_unit_data pwrseq_qcom_wcn6855_xo_clk_assert = {
+ .name = "xo-clk-assert",
+ .enable = pwrseq_qcom_wcn6855_clk_assert,
+};
+
+static const struct pwrseq_unit_data *pwrseq_qcom_wcn6855_unit_deps[] = {
+ &pwrseq_qcom_wcn_vregs_unit_data,
+ &pwrseq_qcom_wcn_clk_unit_data,
+ &pwrseq_qcom_wcn6855_xo_clk_assert,
+ NULL
+};
+
+static int pwrseq_qcom_wcn_bt_enable(struct pwrseq_device *pwrseq)
+{
+ struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
+
+ pwrseq_qcom_wcn_ensure_gpio_delay(ctx);
+ gpiod_set_value_cansleep(ctx->bt_gpio, 1);
+ ctx->last_gpio_enable_jf = jiffies;
+
+ return 0;
+}
+
+static int pwrseq_qcom_wcn_bt_disable(struct pwrseq_device *pwrseq)
+{
+ struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
+
+ gpiod_set_value_cansleep(ctx->bt_gpio, 0);
+
+ return 0;
+}
+
+static bool pwrseq_qcom_wcn_bt_is_controllable(struct pwrseq_device *pwrseq)
+{
+ struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
+
+ /*
+ * The Bluetooth enable is driven through the BT_EN GPIO. When it is not
+ * wired up (e.g. hardwired to an always-on pull-up), the enable/disable
+ * callbacks are no-ops, so the host cannot gate the Bluetooth function
+ * on its own.
+ */
+ return !!ctx->bt_gpio;
+}
+
+static const struct pwrseq_unit_data pwrseq_qcom_wcn_bt_unit_data = {
+ .name = "bluetooth-enable",
+ .deps = pwrseq_qcom_wcn_unit_deps,
+ .enable = pwrseq_qcom_wcn_bt_enable,
+ .disable = pwrseq_qcom_wcn_bt_disable,
+};
+
+static const struct pwrseq_unit_data pwrseq_qcom_wcn6855_bt_unit_data = {
+ .name = "bluetooth-enable",
+ .deps = pwrseq_qcom_wcn6855_unit_deps,
+ .enable = pwrseq_qcom_wcn_bt_enable,
+ .disable = pwrseq_qcom_wcn_bt_disable,
+};
+
+static int pwrseq_qcom_wcn_wlan_enable(struct pwrseq_device *pwrseq)
+{
+ struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
+
+ pwrseq_qcom_wcn_ensure_gpio_delay(ctx);
+ gpiod_set_value_cansleep(ctx->wlan_gpio, 1);
+ ctx->last_gpio_enable_jf = jiffies;
+
+ return 0;
+}
+
+static int pwrseq_qcom_wcn_wlan_disable(struct pwrseq_device *pwrseq)
+{
+ struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
+
+ gpiod_set_value_cansleep(ctx->wlan_gpio, 0);
+
+ return 0;
+}
+
+static bool pwrseq_qcom_wcn_wlan_is_controllable(struct pwrseq_device *pwrseq)
+{
+ struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
+
+ /*
+ * The WLAN enable is driven through the WLAN_EN GPIO. When it is not
+ * wired up (e.g. hardwired to an always-on pull-up), the enable/disable
+ * callbacks are no-ops, so the host cannot gate the WLAN function on
+ * its own.
+ */
+ return !!ctx->wlan_gpio;
+}
+
+static const struct pwrseq_unit_data pwrseq_qcom_wcn_wlan_unit_data = {
+ .name = "wlan-enable",
+ .deps = pwrseq_qcom_wcn_unit_deps,
+ .enable = pwrseq_qcom_wcn_wlan_enable,
+ .disable = pwrseq_qcom_wcn_wlan_disable,
+};
+
+static const struct pwrseq_unit_data pwrseq_qcom_wcn6855_wlan_unit_data = {
+ .name = "wlan-enable",
+ .deps = pwrseq_qcom_wcn6855_unit_deps,
+ .enable = pwrseq_qcom_wcn_wlan_enable,
+ .disable = pwrseq_qcom_wcn_wlan_disable,
+};
+
+static int pwrseq_qcom_wcn_pwup_delay(struct pwrseq_device *pwrseq)
+{
+ struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
+
+ if (ctx->pdata->pwup_delay_ms)
+ msleep(ctx->pdata->pwup_delay_ms);
+
+ return 0;
+}
+
+static int pwrseq_qcom_wcn6855_xo_clk_deassert(struct pwrseq_device *pwrseq)
+{
+ struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
+
+ if (ctx->xo_clk_gpio) {
+ usleep_range(2000, 5000);
+ gpiod_set_value_cansleep(ctx->xo_clk_gpio, 0);
+ }
+
+ return pwrseq_qcom_wcn_pwup_delay(pwrseq);
+}
+
+static const struct pwrseq_target_data pwrseq_qcom_wcn_bt_target_data = {
+ .name = "bluetooth",
+ .unit = &pwrseq_qcom_wcn_bt_unit_data,
+ .post_enable = pwrseq_qcom_wcn_pwup_delay,
+ .is_controllable = pwrseq_qcom_wcn_bt_is_controllable,
+};
+
+static const struct pwrseq_target_data pwrseq_qcom_wcn_wlan_target_data = {
+ .name = "wlan",
+ .unit = &pwrseq_qcom_wcn_wlan_unit_data,
+ .post_enable = pwrseq_qcom_wcn_pwup_delay,
+ .is_controllable = pwrseq_qcom_wcn_wlan_is_controllable,
+};
+
+/* There are no separate BT and WLAN enablement pins */
+static const struct pwrseq_target_data pwrseq_qcom_wcn3990_bt_target_data = {
+ .name = "bluetooth",
+ .unit = &pwrseq_qcom_wcn3990_unit_data,
+};
+
+static const struct pwrseq_target_data pwrseq_qcom_wcn3990_wlan_target_data = {
+ .name = "wlan",
+ .unit = &pwrseq_qcom_wcn3990_unit_data,
+};
+
+static const struct pwrseq_target_data pwrseq_qcom_wcn6855_bt_target_data = {
+ .name = "bluetooth",
+ .unit = &pwrseq_qcom_wcn6855_bt_unit_data,
+ .post_enable = pwrseq_qcom_wcn6855_xo_clk_deassert,
+ .is_controllable = pwrseq_qcom_wcn_bt_is_controllable,
+};
+
+static const struct pwrseq_target_data pwrseq_qcom_wcn6855_wlan_target_data = {
+ .name = "wlan",
+ .unit = &pwrseq_qcom_wcn6855_wlan_unit_data,
+ .post_enable = pwrseq_qcom_wcn6855_xo_clk_deassert,
+ .is_controllable = pwrseq_qcom_wcn_wlan_is_controllable,
+};
+
+static const struct pwrseq_target_data *pwrseq_qcom_wcn_targets[] = {
+ &pwrseq_qcom_wcn_bt_target_data,
+ &pwrseq_qcom_wcn_wlan_target_data,
+ NULL
+};
+
+static const struct pwrseq_target_data *pwrseq_qcom_wcn3990_targets[] = {
+ &pwrseq_qcom_wcn3990_bt_target_data,
+ &pwrseq_qcom_wcn3990_wlan_target_data,
+ NULL
+};
+
+static const struct pwrseq_target_data *pwrseq_qcom_wcn6855_targets[] = {
+ &pwrseq_qcom_wcn6855_bt_target_data,
+ &pwrseq_qcom_wcn6855_wlan_target_data,
+ NULL
+};
+
+static const char *const pwrseq_qca6390_vregs[] = {
+ "vddio",
+ "vddaon",
+ "vddpmu",
+ "vddrfa0p95",
+ "vddrfa1p3",
+ "vddrfa1p9",
+ "vddpcie1p3",
+ "vddpcie1p9",
+};
+
+static const struct pwrseq_qcom_wcn_pdata pwrseq_qca6390_of_data = {
+ .vregs = pwrseq_qca6390_vregs,
+ .num_vregs = ARRAY_SIZE(pwrseq_qca6390_vregs),
+ .pwup_delay_ms = 60,
+ .gpio_enable_delay_ms = 100,
+ .targets = pwrseq_qcom_wcn_targets,
+};
+
+static const char *const pwrseq_wcn3990_vregs[] = {
+ /* vddio is handled separately */
+ "vddxo",
+ "vddrf",
+ "vddch0",
+ "vddch1",
+};
+
+static int pwrseq_qcom_wcn3990_match(struct pwrseq_device *pwrseq,
+ struct device *dev);
+
+static const struct pwrseq_qcom_wcn_pdata pwrseq_wcn3990_of_data = {
+ .vregs = pwrseq_wcn3990_vregs,
+ .num_vregs = ARRAY_SIZE(pwrseq_wcn3990_vregs),
+ .pwup_delay_ms = 50,
+ .targets = pwrseq_qcom_wcn3990_targets,
+ .has_vddio = true,
+ .match = pwrseq_qcom_wcn3990_match,
+};
+
+static const char *const pwrseq_wcn6750_vregs[] = {
+ "vddaon",
+ "vddasd",
+ "vddpmu",
+ "vddrfa0p8",
+ "vddrfa1p2",
+ "vddrfa1p7",
+ "vddrfa2p2",
+};
+
+static const struct pwrseq_qcom_wcn_pdata pwrseq_wcn6750_of_data = {
+ .vregs = pwrseq_wcn6750_vregs,
+ .num_vregs = ARRAY_SIZE(pwrseq_wcn6750_vregs),
+ .pwup_delay_ms = 50,
+ .gpio_enable_delay_ms = 5,
+ .targets = pwrseq_qcom_wcn_targets,
+};
+
+static const char *const pwrseq_wcn6855_vregs[] = {
+ "vddio",
+ "vddaon",
+ "vddpmu",
+ "vddpmumx",
+ "vddpmucx",
+ "vddrfa0p95",
+ "vddrfa1p3",
+ "vddrfa1p9",
+ "vddpcie1p3",
+ "vddpcie1p9",
+};
+
+static const struct pwrseq_qcom_wcn_pdata pwrseq_wcn6855_of_data = {
+ .vregs = pwrseq_wcn6855_vregs,
+ .num_vregs = ARRAY_SIZE(pwrseq_wcn6855_vregs),
+ .pwup_delay_ms = 50,
+ .gpio_enable_delay_ms = 5,
+ .targets = pwrseq_qcom_wcn6855_targets,
+};
+
+static const char *const pwrseq_wcn7850_vregs[] = {
+ "vdd",
+ "vddio",
+ "vddio1p2",
+ "vddaon",
+ "vdddig",
+ "vddrfa1p2",
+ "vddrfa1p8",
+};
+
+static const struct pwrseq_qcom_wcn_pdata pwrseq_wcn7850_of_data = {
+ .vregs = pwrseq_wcn7850_vregs,
+ .num_vregs = ARRAY_SIZE(pwrseq_wcn7850_vregs),
+ .pwup_delay_ms = 100,
+ .targets = pwrseq_qcom_wcn_targets,
+};
+
+static int pwrseq_qcom_wcn_match_regulator(struct pwrseq_device *pwrseq,
+ struct device *dev,
+ const char *name)
+{
+ struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
+ struct device_node *dev_node = dev->of_node;
+
+ /*
+ * The PMU supplies power to the Bluetooth and WLAN modules. both
+ * consume the PMU AON output so check the presence of the
+ * 'vddaon-supply' property and whether it leads us to the right
+ * device.
+ */
+ if (!of_property_present(dev_node, name))
+ return PWRSEQ_NO_MATCH;
+
+ struct device_node *reg_node __free(device_node) =
+ of_parse_phandle(dev_node, name, 0);
+ if (!reg_node)
+ return PWRSEQ_NO_MATCH;
+
+ /*
+ * `reg_node` is the PMU AON regulator, its parent is the `regulators`
+ * node and finally its grandparent is the PMU device node that we're
+ * looking for.
+ */
+ if (!reg_node->parent || !reg_node->parent->parent ||
+ reg_node->parent->parent != ctx->of_node)
+ return PWRSEQ_NO_MATCH;
+
+ return PWRSEQ_MATCH_OK;
+}
+
+static int pwrseq_qcom_wcn_match(struct pwrseq_device *pwrseq,
+ struct device *dev)
+{
+ return pwrseq_qcom_wcn_match_regulator(pwrseq, dev, "vddaon-supply");
+}
+
+static int pwrseq_qcom_wcn3990_match(struct pwrseq_device *pwrseq,
+ struct device *dev)
+{
+ int ret;
+
+ /* BT device */
+ ret = pwrseq_qcom_wcn_match_regulator(pwrseq, dev, "vddio-supply");
+ if (ret == PWRSEQ_MATCH_OK)
+ return ret;
+
+ /* WiFi device match */
+ return pwrseq_qcom_wcn_match_regulator(pwrseq, dev, "vdd-1.8-xo-supply");
+}
+
+static int pwrseq_qcom_wcn_probe(struct platform_device *pdev)
+{
+ struct device *dev = &pdev->dev;
+ struct pwrseq_qcom_wcn_ctx *ctx;
+ struct pwrseq_config config;
+ int i, ret;
+
+ ctx = devm_kzalloc_obj(dev, *ctx);
+ if (!ctx)
+ return -ENOMEM;
+
+ ctx->of_node = dev->of_node;
+
+ ctx->pdata = device_get_match_data(dev);
+ if (!ctx->pdata)
+ return dev_err_probe(dev, -ENODEV,
+ "Failed to obtain platform data\n");
+
+ ctx->regs = devm_kzalloc_objs(dev, *ctx->regs, ctx->pdata->num_vregs);
+ if (!ctx->regs)
+ return -ENOMEM;
+
+ for (i = 0; i < ctx->pdata->num_vregs; i++)
+ ctx->regs[i].supply = ctx->pdata->vregs[i];
+
+ ret = devm_regulator_bulk_get(dev, ctx->pdata->num_vregs, ctx->regs);
+ if (ret < 0)
+ return dev_err_probe(dev, ret,
+ "Failed to get all regulators\n");
+
+ if (ctx->pdata->has_vddio) {
+ ctx->vddio = devm_regulator_get(dev, "vddio");
+ if (IS_ERR(ctx->vddio))
+ return dev_err_probe(dev, PTR_ERR(ctx->vddio), "Failed to get VDDIO\n");
+ }
+
+ ctx->bt_gpio = devm_gpiod_get_optional(dev, "bt-enable", GPIOD_OUT_LOW);
+ if (IS_ERR(ctx->bt_gpio))
+ return dev_err_probe(dev, PTR_ERR(ctx->bt_gpio),
+ "Failed to get the Bluetooth enable GPIO\n");
+
+ ctx->wlan_gpio = devm_gpiod_get_optional(dev, "wlan-enable",
+ GPIOD_OUT_LOW);
+ if (IS_ERR(ctx->wlan_gpio))
+ return dev_err_probe(dev, PTR_ERR(ctx->wlan_gpio),
+ "Failed to get the WLAN enable GPIO\n");
+
+ ctx->xo_clk_gpio = devm_gpiod_get_optional(dev, "xo-clk",
+ GPIOD_OUT_LOW);
+ if (IS_ERR(ctx->xo_clk_gpio))
+ return dev_err_probe(dev, PTR_ERR(ctx->xo_clk_gpio),
+ "Failed to get the XO_CLK GPIO\n");
+
+ ctx->clk = devm_clk_get_optional(dev, NULL);
+ if (IS_ERR(ctx->clk))
+ return dev_err_probe(dev, PTR_ERR(ctx->clk),
+ "Failed to get the reference clock\n");
+
+ memset(&config, 0, sizeof(config));
+
+ config.parent = dev;
+ config.owner = THIS_MODULE;
+ config.drvdata = ctx;
+ config.match = ctx->pdata->match ? : pwrseq_qcom_wcn_match;
+ config.targets = ctx->pdata->targets;
+
+ ctx->pwrseq = devm_pwrseq_device_register(dev, &config);
+ if (IS_ERR(ctx->pwrseq))
+ return dev_err_probe(dev, PTR_ERR(ctx->pwrseq),
+ "Failed to register the power sequencer\n");
+
+ return 0;
+}
+
+static const struct of_device_id pwrseq_qcom_wcn_of_match[] = {
+ {
+ .compatible = "qcom,wcn3950-pmu",
+ .data = &pwrseq_wcn3990_of_data,
+ },
+ {
+ .compatible = "qcom,wcn3988-pmu",
+ .data = &pwrseq_wcn3990_of_data,
+ },
+ {
+ .compatible = "qcom,wcn3990-pmu",
+ .data = &pwrseq_wcn3990_of_data,
+ },
+ {
+ .compatible = "qcom,wcn3991-pmu",
+ .data = &pwrseq_wcn3990_of_data,
+ },
+ {
+ .compatible = "qcom,wcn3998-pmu",
+ .data = &pwrseq_wcn3990_of_data,
+ },
+ {
+ .compatible = "qcom,qca6390-pmu",
+ .data = &pwrseq_qca6390_of_data,
+ },
+ {
+ .compatible = "qcom,wcn6855-pmu",
+ .data = &pwrseq_wcn6855_of_data,
+ },
+ {
+ .compatible = "qcom,wcn7850-pmu",
+ .data = &pwrseq_wcn7850_of_data,
+ },
+ {
+ .compatible = "qcom,wcn6750-pmu",
+ .data = &pwrseq_wcn6750_of_data,
+ },
+ { }
+};
+MODULE_DEVICE_TABLE(of, pwrseq_qcom_wcn_of_match);
+
+static struct platform_driver pwrseq_qcom_wcn_driver = {
+ .driver = {
+ .name = "pwrseq-qcom_wcn",
+ .of_match_table = pwrseq_qcom_wcn_of_match,
+ },
+ .probe = pwrseq_qcom_wcn_probe,
+};
+module_platform_driver(pwrseq_qcom_wcn_driver);
+
+MODULE_AUTHOR("Bartosz Golaszewski <bartosz.golaszewski@linaro.org>");
+MODULE_DESCRIPTION("Qualcomm WCN PMU power sequencing driver");
+MODULE_LICENSE("GPL");
diff --git a/drivers/power/sequencing/pwrseq-renesas-pwrrdy.c b/drivers/power/sequencing/pwrseq-renesas-pwrrdy.c
new file mode 100644
index 000000000..743855620
--- /dev/null
+++ b/drivers/power/sequencing/pwrseq-renesas-pwrrdy.c
@@ -0,0 +1,142 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Renesas RZ/G3L Power Ready driver
+ *
+ */
+
+#include <linux/auxiliary_bus.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/pwrseq/provider.h>
+#include <linux/regmap.h>
+
+#define SYS_PWRRDY_N 0xd70
+#define SYS_PWRRDY_N_USB_MASK BIT(0)
+#define SYS_PWRRDY_N_DSI_MASK BIT(1)
+#define SYS_PWRRDY_N_CSI_MASK BIT(2)
+
+static int pwrseq_rzg3l_set_pwrrdy(struct pwrseq_device *pwrseq, u32 mask,
+ bool val)
+{
+ struct regmap *regmap = pwrseq_device_get_drvdata(pwrseq);
+
+ return regmap_assign_bits(regmap, SYS_PWRRDY_N, mask, val);
+}
+
+static int pwrseq_rzg3l_usb_pwrrdy_enable(struct pwrseq_device *pwrseq)
+{
+ return pwrseq_rzg3l_set_pwrrdy(pwrseq, SYS_PWRRDY_N_USB_MASK, 0);
+}
+
+static int pwrseq_rzg3l_usb_pwrrdy_disable(struct pwrseq_device *pwrseq)
+{
+ return pwrseq_rzg3l_set_pwrrdy(pwrseq, SYS_PWRRDY_N_USB_MASK, 1);
+}
+
+static const struct pwrseq_unit_data pwrseq_rzg3l_usb_pwrrdy_unit = {
+ .name = "usb-pwrrdy-power-sequence",
+ .enable = pwrseq_rzg3l_usb_pwrrdy_enable,
+ .disable = pwrseq_rzg3l_usb_pwrrdy_disable,
+};
+
+static int pwrseq_rzg3l_dsi_pwrrdy_enable(struct pwrseq_device *pwrseq)
+{
+ return pwrseq_rzg3l_set_pwrrdy(pwrseq, SYS_PWRRDY_N_DSI_MASK, 0);
+}
+
+static int pwrseq_rzg3l_dsi_pwrrdy_disable(struct pwrseq_device *pwrseq)
+{
+ return pwrseq_rzg3l_set_pwrrdy(pwrseq, SYS_PWRRDY_N_DSI_MASK, 1);
+}
+
+static const struct pwrseq_unit_data pwrseq_rzg3l_dsi_pwrrdy_unit = {
+ .name = "dsi-pwrrdy-sequence",
+ .enable = pwrseq_rzg3l_dsi_pwrrdy_enable,
+ .disable = pwrseq_rzg3l_dsi_pwrrdy_disable,
+};
+
+static int pwrseq_rzg3l_csi_pwrrdy_enable(struct pwrseq_device *pwrseq)
+{
+ return pwrseq_rzg3l_set_pwrrdy(pwrseq, SYS_PWRRDY_N_CSI_MASK, 0);
+}
+
+static int pwrseq_rzg3l_csi_pwrrdy_disable(struct pwrseq_device *pwrseq)
+{
+ return pwrseq_rzg3l_set_pwrrdy(pwrseq, SYS_PWRRDY_N_CSI_MASK, 1);
+}
+
+static const struct pwrseq_unit_data pwrseq_rzg3l_csi_pwrrdy_unit = {
+ .name = "csi-pwrrdy-power-sequence",
+ .enable = pwrseq_rzg3l_csi_pwrrdy_enable,
+ .disable = pwrseq_rzg3l_csi_pwrrdy_disable,
+};
+
+static const struct pwrseq_target_data pwrseq_rzg3l_usb_pwrrdy_target = {
+ .name = "usb-pwrrdy",
+ .unit = &pwrseq_rzg3l_usb_pwrrdy_unit,
+};
+
+static const struct pwrseq_target_data pwrseq_rzg3l_dsi_pwrrdy_target = {
+ .name = "dsi-pwrrdy",
+ .unit = &pwrseq_rzg3l_dsi_pwrrdy_unit,
+};
+
+static const struct pwrseq_target_data pwrseq_rzg3l_csi_pwrrdy_target = {
+ .name = "csi-pwrrdy",
+ .unit = &pwrseq_rzg3l_csi_pwrrdy_unit,
+};
+
+static const struct pwrseq_target_data *pwrseq_rzg3l_pwrrdy_targets[] = {
+ &pwrseq_rzg3l_usb_pwrrdy_target,
+ &pwrseq_rzg3l_dsi_pwrrdy_target,
+ &pwrseq_rzg3l_csi_pwrrdy_target,
+ NULL
+};
+
+static const char * const pwrseq_rzg3l_pwrrdy_compats[] = {
+ "renesas,r9a08g046-usbphy-ctrl",
+ "renesas,r9a08g046-mipi-dsi",
+ NULL
+};
+
+static int pwrseq_rzg3l_pwrrdy_match(struct pwrseq_device *pwrseq,
+ struct device *dev)
+{
+ if (of_device_compatible_match(dev->of_node, pwrseq_rzg3l_pwrrdy_compats))
+ return PWRSEQ_MATCH_OK;
+
+ return PWRSEQ_NO_MATCH;
+}
+
+static int pwrseq_rzg3l_pwrrdy_probe(struct auxiliary_device *adev,
+ const struct auxiliary_device_id *id)
+{
+ struct pwrseq_config config = {
+ .parent = &adev->dev,
+ .owner = THIS_MODULE,
+ .drvdata = adev->dev.platform_data,
+ .match = pwrseq_rzg3l_pwrrdy_match,
+ .targets = pwrseq_rzg3l_pwrrdy_targets,
+ };
+
+ return PTR_ERR_OR_ZERO(devm_pwrseq_device_register(&adev->dev, &config));
+}
+
+static const struct auxiliary_device_id pwrseq_rzg3l_pwrrdy_id_table[] = {
+ { .name = "rz_sysc.pwrseq-pwrrdy" },
+ { /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(auxiliary, pwrseq_rzg3l_pwrrdy_id_table);
+
+static struct auxiliary_driver pwrseq_rzg3l_pwrrdy_driver = {
+ .driver = {
+ .name = "pwrseq-rzg3l-pwrrdy",
+ },
+ .probe = pwrseq_rzg3l_pwrrdy_probe,
+ .id_table = pwrseq_rzg3l_pwrrdy_id_table,
+};
+module_auxiliary_driver(pwrseq_rzg3l_pwrrdy_driver);
+
+MODULE_AUTHOR("Biju Das <biju.das.jz@bp.renesas.com>");
+MODULE_DESCRIPTION("Renesas RZ/G3L Power Ready Driver");
+MODULE_LICENSE("GPL");
diff --git a/drivers/power/sequencing/pwrseq-thead-gpu.c b/drivers/power/sequencing/pwrseq-thead-gpu.c
new file mode 100644
index 000000000..24cdfd5d7
--- /dev/null
+++ b/drivers/power/sequencing/pwrseq-thead-gpu.c
@@ -0,0 +1,249 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * T-HEAD TH1520 GPU Power Sequencer Driver
+ *
+ * Copyright (c) 2025 Samsung Electronics Co., Ltd.
+ * Author: Michal Wilczynski <m.wilczynski@samsung.com>
+ *
+ * This driver implements the power sequence for the Imagination BXM-4-64
+ * GPU on the T-HEAD TH1520 SoC. The sequence requires coordinating resources
+ * from both the sequencer's parent device node (clkgen_reset) and the GPU's
+ * device node (clocks and core reset).
+ *
+ * The `match` function is used to acquire the GPU's resources when the
+ * GPU driver requests the "gpu-power" sequence target.
+ */
+
+#include <linux/auxiliary_bus.h>
+#include <linux/clk.h>
+#include <linux/delay.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/pwrseq/provider.h>
+#include <linux/reset.h>
+#include <linux/slab.h>
+
+#include <dt-bindings/power/thead,th1520-power.h>
+
+struct pwrseq_thead_gpu_ctx {
+ struct pwrseq_device *pwrseq;
+ struct reset_control *clkgen_reset;
+ struct device_node *aon_node;
+
+ /* Consumer resources */
+ struct device_node *consumer_node;
+ struct clk_bulk_data *clks;
+ int num_clks;
+ struct reset_control *gpu_reset;
+};
+
+static int pwrseq_thead_gpu_enable(struct pwrseq_device *pwrseq)
+{
+ struct pwrseq_thead_gpu_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
+ int ret;
+
+ if (!ctx->clks || !ctx->gpu_reset)
+ return -ENODEV;
+
+ ret = clk_bulk_prepare_enable(ctx->num_clks, ctx->clks);
+ if (ret)
+ return ret;
+
+ ret = reset_control_deassert(ctx->clkgen_reset);
+ if (ret)
+ goto err_disable_clks;
+
+ /*
+ * According to the hardware manual, a delay of at least 32 clock
+ * cycles is required between de-asserting the clkgen reset and
+ * de-asserting the GPU reset. Assuming a worst-case scenario with
+ * a very high GPU clock frequency, a delay of 1 microsecond is
+ * sufficient to ensure this requirement is met across all
+ * feasible GPU clock speeds.
+ */
+ udelay(1);
+
+ ret = reset_control_deassert(ctx->gpu_reset);
+ if (ret)
+ goto err_assert_clkgen;
+
+ return 0;
+
+err_assert_clkgen:
+ reset_control_assert(ctx->clkgen_reset);
+err_disable_clks:
+ clk_bulk_disable_unprepare(ctx->num_clks, ctx->clks);
+ return ret;
+}
+
+static int pwrseq_thead_gpu_disable(struct pwrseq_device *pwrseq)
+{
+ struct pwrseq_thead_gpu_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
+ int ret = 0, err;
+
+ if (!ctx->clks || !ctx->gpu_reset)
+ return -ENODEV;
+
+ err = reset_control_assert(ctx->gpu_reset);
+ if (err)
+ ret = err;
+
+ err = reset_control_assert(ctx->clkgen_reset);
+ if (err && !ret)
+ ret = err;
+
+ clk_bulk_disable_unprepare(ctx->num_clks, ctx->clks);
+
+ /* ret stores values of the first error code */
+ return ret;
+}
+
+static const struct pwrseq_unit_data pwrseq_thead_gpu_unit = {
+ .name = "gpu-power-sequence",
+ .enable = pwrseq_thead_gpu_enable,
+ .disable = pwrseq_thead_gpu_disable,
+};
+
+static const struct pwrseq_target_data pwrseq_thead_gpu_target = {
+ .name = "gpu-power",
+ .unit = &pwrseq_thead_gpu_unit,
+};
+
+static const struct pwrseq_target_data *pwrseq_thead_gpu_targets[] = {
+ &pwrseq_thead_gpu_target,
+ NULL
+};
+
+static int pwrseq_thead_gpu_match(struct pwrseq_device *pwrseq,
+ struct device *dev)
+{
+ struct pwrseq_thead_gpu_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
+ static const char *const clk_names[] = { "core", "sys" };
+ struct of_phandle_args pwr_spec;
+ int i, ret;
+
+ /* We only match the specific T-HEAD TH1520 GPU compatible */
+ if (!of_device_is_compatible(dev->of_node, "thead,th1520-gpu"))
+ return PWRSEQ_NO_MATCH;
+
+ ret = of_parse_phandle_with_args(dev->of_node, "power-domains",
+ "#power-domain-cells", 0, &pwr_spec);
+ if (ret)
+ return PWRSEQ_NO_MATCH;
+
+ /* Additionally verify consumer device has AON as power-domain */
+ if (pwr_spec.np != ctx->aon_node || pwr_spec.args[0] != TH1520_GPU_PD) {
+ of_node_put(pwr_spec.np);
+ return PWRSEQ_NO_MATCH;
+ }
+
+ of_node_put(pwr_spec.np);
+
+ /* If a consumer is already bound, only allow a re-match from it */
+ if (ctx->consumer_node)
+ return ctx->consumer_node == dev->of_node ?
+ PWRSEQ_MATCH_OK : PWRSEQ_NO_MATCH;
+
+ ctx->num_clks = ARRAY_SIZE(clk_names);
+ ctx->clks = kzalloc_objs(*ctx->clks, ctx->num_clks);
+ if (!ctx->clks)
+ return -ENOMEM;
+
+ for (i = 0; i < ctx->num_clks; i++)
+ ctx->clks[i].id = clk_names[i];
+
+ ret = clk_bulk_get(dev, ctx->num_clks, ctx->clks);
+ if (ret)
+ goto err_free_clks;
+
+ ctx->gpu_reset = reset_control_get_shared(dev, NULL);
+ if (IS_ERR(ctx->gpu_reset)) {
+ ret = PTR_ERR(ctx->gpu_reset);
+ goto err_put_clks;
+ }
+
+ ctx->consumer_node = of_node_get(dev->of_node);
+
+ return PWRSEQ_MATCH_OK;
+
+err_put_clks:
+ clk_bulk_put(ctx->num_clks, ctx->clks);
+err_free_clks:
+ kfree(ctx->clks);
+ ctx->clks = NULL;
+
+ return ret;
+}
+
+static int pwrseq_thead_gpu_probe(struct auxiliary_device *adev,
+ const struct auxiliary_device_id *id)
+{
+ struct device *dev = &adev->dev;
+ struct device *parent_dev = dev->parent;
+ struct pwrseq_thead_gpu_ctx *ctx;
+ struct pwrseq_config config = {};
+
+ ctx = devm_kzalloc_obj(dev, *ctx);
+ if (!ctx)
+ return -ENOMEM;
+
+ ctx->aon_node = parent_dev->of_node;
+
+ ctx->clkgen_reset =
+ devm_reset_control_get_exclusive(parent_dev, "gpu-clkgen");
+ if (IS_ERR(ctx->clkgen_reset))
+ return dev_err_probe(
+ dev, PTR_ERR(ctx->clkgen_reset),
+ "Failed to get GPU clkgen reset from parent\n");
+
+ config.parent = dev;
+ config.owner = THIS_MODULE;
+ config.drvdata = ctx;
+ config.match = pwrseq_thead_gpu_match;
+ config.targets = pwrseq_thead_gpu_targets;
+
+ ctx->pwrseq = devm_pwrseq_device_register(dev, &config);
+ if (IS_ERR(ctx->pwrseq))
+ return dev_err_probe(dev, PTR_ERR(ctx->pwrseq),
+ "Failed to register power sequencer\n");
+
+ auxiliary_set_drvdata(adev, ctx);
+
+ return 0;
+}
+
+static void pwrseq_thead_gpu_remove(struct auxiliary_device *adev)
+{
+ struct pwrseq_thead_gpu_ctx *ctx = auxiliary_get_drvdata(adev);
+
+ if (ctx->gpu_reset)
+ reset_control_put(ctx->gpu_reset);
+
+ if (ctx->clks) {
+ clk_bulk_put(ctx->num_clks, ctx->clks);
+ kfree(ctx->clks);
+ }
+
+ if (ctx->consumer_node)
+ of_node_put(ctx->consumer_node);
+}
+
+static const struct auxiliary_device_id pwrseq_thead_gpu_id_table[] = {
+ { .name = "th1520_pm_domains.pwrseq-gpu" },
+ {},
+};
+MODULE_DEVICE_TABLE(auxiliary, pwrseq_thead_gpu_id_table);
+
+static struct auxiliary_driver pwrseq_thead_gpu_driver = {
+ .driver = {
+ .name = "pwrseq-thead-gpu",
+ },
+ .probe = pwrseq_thead_gpu_probe,
+ .remove = pwrseq_thead_gpu_remove,
+ .id_table = pwrseq_thead_gpu_id_table,
+};
+module_auxiliary_driver(pwrseq_thead_gpu_driver);
+
+MODULE_AUTHOR("Michal Wilczynski <m.wilczynski@samsung.com>");
+MODULE_DESCRIPTION("T-HEAD TH1520 GPU power sequencer driver");
+MODULE_LICENSE("GPL");