From: Bartosz Golaszewski <brgl@bgdev.pl>
To: Bjorn Andersson <andersson@kernel.org>,
Konrad Dybcio <konrad.dybcio@linaro.org>,
Rob Herring <robh@kernel.org>,
Krzysztof Kozlowski <krzysztof.kozlowski+dt@linaro.org>,
Conor Dooley <conor+dt@kernel.org>,
Marcel Holtmann <marcel@holtmann.org>,
Luiz Augusto von Dentz <luiz.dentz@gmail.com>,
Bjorn Helgaas <bhelgaas@google.com>,
Neil Armstrong <neil.armstrong@linaro.org>,
Alex Elder <elder@linaro.org>,
Srini Kandagatla <srinivas.kandagatla@linaro.org>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Arnd Bergmann <arnd@arndb.de>, Abel Vesa <abel.vesa@linaro.org>,
Manivannan Sadhasivam <mani@kernel.org>,
Lukas Wunner <lukas@wunner.de>
Cc: linux-arm-msm@vger.kernel.org, devicetree@vger.kernel.org,
linux-kernel@vger.kernel.org, linux-bluetooth@vger.kernel.org,
linux-pci@vger.kernel.org,
Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Subject: [RFC 8/9] PCI/pwrctl: add PCI power control core code
Date: Thu, 1 Feb 2024 16:55:31 +0100 [thread overview]
Message-ID: <20240201155532.49707-9-brgl@bgdev.pl> (raw)
In-Reply-To: <20240201155532.49707-1-brgl@bgdev.pl>
From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Some PCI devices must be powered-on before they can be detected on the
bus. Introduce a simple framework reusing the existing PCI OF
infrastructure.
The way this works is: a DT node representing a PCI device connected to
the port can be matched against its power control platform driver. If
the match succeeds, the driver is responsible for powering-up the device
and calling pcie_pwrctl_device_enable() which will trigger a PCI bus
rescan as well as subscribe to PCI bus notifications.
When the device is detected and created, we'll make it consume the same
DT node that the platform device did. When the device is bound, we'll
create a device link between it and the parent power control device.
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
---
drivers/pci/Kconfig | 1 +
drivers/pci/Makefile | 1 +
drivers/pci/pwrctl/Kconfig | 8 ++++
drivers/pci/pwrctl/Makefile | 3 ++
drivers/pci/pwrctl/core.c | 82 +++++++++++++++++++++++++++++++++++++
include/linux/pci-pwrctl.h | 24 +++++++++++
6 files changed, 119 insertions(+)
create mode 100644 drivers/pci/pwrctl/Kconfig
create mode 100644 drivers/pci/pwrctl/Makefile
create mode 100644 drivers/pci/pwrctl/core.c
create mode 100644 include/linux/pci-pwrctl.h
diff --git a/drivers/pci/Kconfig b/drivers/pci/Kconfig
index 74147262625b..5b9b84f8774f 100644
--- a/drivers/pci/Kconfig
+++ b/drivers/pci/Kconfig
@@ -291,5 +291,6 @@ source "drivers/pci/hotplug/Kconfig"
source "drivers/pci/controller/Kconfig"
source "drivers/pci/endpoint/Kconfig"
source "drivers/pci/switch/Kconfig"
+source "drivers/pci/pwrctl/Kconfig"
endif
diff --git a/drivers/pci/Makefile b/drivers/pci/Makefile
index cc8b4e01e29d..6ae202d950f8 100644
--- a/drivers/pci/Makefile
+++ b/drivers/pci/Makefile
@@ -9,6 +9,7 @@ obj-$(CONFIG_PCI) += access.o bus.o probe.o host-bridge.o \
obj-$(CONFIG_PCI) += msi/
obj-$(CONFIG_PCI) += pcie/
+obj-$(CONFIG_PCI) += pwrctl/
ifdef CONFIG_PCI
obj-$(CONFIG_PROC_FS) += proc.o
diff --git a/drivers/pci/pwrctl/Kconfig b/drivers/pci/pwrctl/Kconfig
new file mode 100644
index 000000000000..e2dc5e5d2af1
--- /dev/null
+++ b/drivers/pci/pwrctl/Kconfig
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0
+
+menu "PCI Power control drivers"
+
+config PCI_PWRCTL
+ bool
+
+endmenu
diff --git a/drivers/pci/pwrctl/Makefile b/drivers/pci/pwrctl/Makefile
new file mode 100644
index 000000000000..4381cfbf3f21
--- /dev/null
+++ b/drivers/pci/pwrctl/Makefile
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0
+
+obj-$(CONFIG_PCI_PWRCTL) += core.o
diff --git a/drivers/pci/pwrctl/core.c b/drivers/pci/pwrctl/core.c
new file mode 100644
index 000000000000..312e6fe95c31
--- /dev/null
+++ b/drivers/pci/pwrctl/core.c
@@ -0,0 +1,82 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2024 Linaro Ltd.
+ */
+
+#include <linux/device.h>
+#include <linux/export.h>
+#include <linux/kernel.h>
+#include <linux/pci.h>
+#include <linux/pci-pwrctl.h>
+#include <linux/property.h>
+#include <linux/slab.h>
+
+static int pci_pwrctl_notify(struct notifier_block *nb, unsigned long action,
+ void *data)
+{
+ struct pci_pwrctl *pwrctl = container_of(nb, struct pci_pwrctl, nb);
+ struct device *dev = data;
+
+ if (dev_fwnode(dev) != dev_fwnode(pwrctl->dev))
+ return NOTIFY_DONE;
+
+ switch (action) {
+ case BUS_NOTIFY_ADD_DEVICE:
+ device_set_of_node_from_dev(dev, pwrctl->dev);
+ break;
+ case BUS_NOTIFY_BOUND_DRIVER:
+ pwrctl->link = device_link_add(dev, pwrctl->dev,
+ DL_FLAG_AUTOREMOVE_CONSUMER);
+ if (!pwrctl->link)
+ dev_err(pwrctl->dev, "Failed to add device link\n");
+ break;
+ case BUS_NOTIFY_UNBOUND_DRIVER:
+ device_link_del(pwrctl->link);
+ break;
+ }
+
+ return NOTIFY_DONE;
+}
+
+int pci_pwrctl_device_enable(struct pci_pwrctl *pwrctl)
+{
+ if (!pwrctl->dev)
+ return -ENODEV;
+
+ pwrctl->nb.notifier_call = pci_pwrctl_notify;
+ bus_register_notifier(&pci_bus_type, &pwrctl->nb);
+
+ pci_lock_rescan_remove();
+ pci_rescan_bus(to_pci_dev(pwrctl->dev->parent)->bus);
+ pci_unlock_rescan_remove();
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(pci_pwrctl_device_enable);
+
+void pci_pwrctl_device_disable(struct pci_pwrctl *pwrctl)
+{
+ bus_unregister_notifier(&pci_bus_type, &pwrctl->nb);
+}
+EXPORT_SYMBOL_GPL(pci_pwrctl_device_disable);
+
+static void devm_pci_pwrctl_device_disable(void *data)
+{
+ struct pci_pwrctl *pwrctl = data;
+
+ pci_pwrctl_device_disable(pwrctl);
+}
+
+int devm_pci_pwrctl_device_enable(struct device *dev,
+ struct pci_pwrctl *pwrctl)
+{
+ int ret;
+
+ ret = pci_pwrctl_device_enable(pwrctl);
+ if (ret)
+ return ret;
+
+ return devm_add_action_or_reset(dev, devm_pci_pwrctl_device_disable,
+ pwrctl);
+}
+EXPORT_SYMBOL_GPL(devm_pci_pwrctl_device_enable);
diff --git a/include/linux/pci-pwrctl.h b/include/linux/pci-pwrctl.h
new file mode 100644
index 000000000000..8d16d27cbfeb
--- /dev/null
+++ b/include/linux/pci-pwrctl.h
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2024 Linaro Ltd.
+ */
+
+#ifndef __PCI_PWRCTL_H__
+#define __PCI_PWRCTL_H__
+
+#include <linux/notifier.h>
+
+struct device;
+
+struct pci_pwrctl {
+ struct notifier_block nb;
+ struct device *dev;
+ struct device_link *link;
+};
+
+int pci_pwrctl_device_enable(struct pci_pwrctl *pwrctl);
+void pci_pwrctl_device_disable(struct pci_pwrctl *pwrctl);
+int devm_pci_pwrctl_device_enable(struct device *dev,
+ struct pci_pwrctl *pwrctl);
+
+#endif /* __PCI_PWRCTL_H__ */
--
2.40.1
next prev parent reply other threads:[~2024-02-01 15:55 UTC|newest]
Thread overview: 41+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-01 15:55 [RFC 0/9] power: sequencing: implement the subsystem and add first users Bartosz Golaszewski
2024-02-01 15:55 ` [RFC 1/9] of: provide a cleanup helper for OF nodes Bartosz Golaszewski
2024-02-01 22:18 ` Rob Herring
2024-02-04 19:18 ` Bartosz Golaszewski
2024-02-01 15:55 ` [RFC 2/9] arm64: dts: qcom: qrb5165-rb5: model the PMU of the QCA6391 Bartosz Golaszewski
2024-02-02 4:34 ` Bjorn Andersson
2024-02-02 4:59 ` Dmitry Baryshkov
2024-02-02 16:09 ` Bjorn Andersson
2024-02-02 13:23 ` Bartosz Golaszewski
2024-02-02 16:41 ` Bjorn Andersson
2024-02-02 16:47 ` Bjorn Andersson
2024-02-05 7:51 ` Krzysztof Kozlowski
2024-02-01 15:55 ` [RFC 3/9] power: sequencing: new subsystem Bartosz Golaszewski
2024-02-01 22:25 ` Rob Herring
2024-02-01 15:55 ` [RFC 4/9] power: pwrseq: add a driver for the QCA6390 PMU module Bartosz Golaszewski
2024-02-02 4:54 ` Bjorn Andersson
2024-02-02 7:48 ` Dmitry Baryshkov
2024-02-02 9:01 ` Bartosz Golaszewski
2024-02-01 15:55 ` [RFC 5/9] Bluetooth: qca: use the power sequencer for QCA6390 Bartosz Golaszewski
2024-02-01 15:55 ` [RFC 6/9] PCI: create platform devices for child OF nodes of the port node Bartosz Golaszewski
2024-02-02 2:59 ` Bjorn Andersson
2024-02-02 9:03 ` Bartosz Golaszewski
2024-02-01 15:55 ` [RFC 7/9] PCI: hold the rescan mutex when scanning for the first time Bartosz Golaszewski
2024-02-01 15:55 ` Bartosz Golaszewski [this message]
2024-02-02 3:53 ` [RFC 8/9] PCI/pwrctl: add PCI power control core code Bjorn Andersson
2024-02-02 9:11 ` Bartosz Golaszewski
2024-02-02 16:52 ` Bjorn Andersson
2024-02-07 16:26 ` Bartosz Golaszewski
2024-02-09 9:04 ` Lukas Wunner
2024-02-09 9:38 ` Manivannan Sadhasivam
2024-02-08 11:32 ` Manivannan Sadhasivam
2024-02-09 23:43 ` Bjorn Andersson
2024-02-14 14:28 ` Manivannan Sadhasivam
2024-02-14 15:46 ` Bartosz Golaszewski
2024-02-01 15:55 ` [RFC 9/9] PCI/pwrctl: add a PCI power control driver for power sequenced devices Bartosz Golaszewski
2024-02-02 4:03 ` Bjorn Andersson
2024-02-02 13:05 ` Bartosz Golaszewski
2024-02-09 23:37 ` Bjorn Andersson
2024-02-02 0:40 ` [RFC 0/9] power: sequencing: implement the subsystem and add first users Bjorn Andersson
2024-02-02 8:53 ` Bartosz Golaszewski
2024-02-02 4:10 ` Bjorn Andersson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240201155532.49707-9-brgl@bgdev.pl \
--to=brgl@bgdev.pl \
--cc=abel.vesa@linaro.org \
--cc=andersson@kernel.org \
--cc=arnd@arndb.de \
--cc=bartosz.golaszewski@linaro.org \
--cc=bhelgaas@google.com \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=elder@linaro.org \
--cc=gregkh@linuxfoundation.org \
--cc=konrad.dybcio@linaro.org \
--cc=krzysztof.kozlowski+dt@linaro.org \
--cc=linux-arm-msm@vger.kernel.org \
--cc=linux-bluetooth@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pci@vger.kernel.org \
--cc=luiz.dentz@gmail.com \
--cc=lukas@wunner.de \
--cc=mani@kernel.org \
--cc=marcel@holtmann.org \
--cc=neil.armstrong@linaro.org \
--cc=robh@kernel.org \
--cc=srinivas.kandagatla@linaro.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).