mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Vladyslav Tymoshchuk <tvlad744@gmail.com>
To: Bjorn Andersson <andersson@kernel.org>,
	Konrad Dybcio <konradybcio@kernel.org>,
	Sebastian Reichel <sre@kernel.org>,
	Heikki Krogerus <heikki.krogerus@linux.intel.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Neil Armstrong <neil.armstrong@linaro.org>,
	Abel Vesa <abel.vesa@oss.qualcomm.com>,
	Jishnu Prakash <jishnu.prakash@oss.qualcomm.com>,
	Joonhoe Kim <26rote@gmail.com>,
	linux-arm-msm@vger.kernel.org, linux-pm@vger.kernel.org,
	linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v2 3/5] soc: qcom: pmic_glink: Report wakeup events on behalf of clients
Date: Sun, 29 Nov 2026 15:44:39 +0200	[thread overview]
Message-ID: <20261129134442.579-4-tvlad744@gmail.com> (raw)
In-Reply-To: <20261129134442.579-1-tvlad744@gmail.com>

The GLINK interrupt is not a wakeup interrupt, as most PMIC GLINK
messages must not wake up the system, and the clients get notifications
only after the system has resumed.

Report the wakeup events in pmic_glink, when a notification arrives: it
is a wakeup event if the device of the client may wake up the system and
the optional wakeup filter of the client accepts it. Use a wakeup source
of pmic_glink, and keep it active until the queued notifications have
been passed on, so that the system does not go back to sleep before that.

No client enables wakeup yet, so nothing changes for them.

Signed-off-by: Vladyslav Tymoshchuk <tvlad744@gmail.com>
---
 drivers/soc/qcom/pmic_glink.c       | 80 +++++++++++++++++++++++++++++
 include/linux/soc/qcom/pmic_glink.h |  2 +
 2 files changed, 82 insertions(+)

diff --git a/drivers/soc/qcom/pmic_glink.c b/drivers/soc/qcom/pmic_glink.c
index 805c4ee85bba..a25d7967629d 100644
--- a/drivers/soc/qcom/pmic_glink.c
+++ b/drivers/soc/qcom/pmic_glink.c
@@ -9,12 +9,14 @@
 #include <linux/module.h>
 #include <linux/of.h>
 #include <linux/platform_device.h>
+#include <linux/pm_wakeup.h>
 #include <linux/rpmsg.h>
 #include <linux/slab.h>
 #include <linux/soc/qcom/pdr.h>
 #include <linux/soc/qcom/pmic_glink.h>
 #include <linux/spinlock.h>
 #include <linux/string.h>
+#include <linux/suspend.h>
 #include <linux/workqueue.h>
 
 #define PMIC_GLINK_SEND_TIMEOUT (5 * HZ)
@@ -61,6 +63,8 @@ struct pmic_glink {
 	unsigned int num_notifications;
 	unsigned int num_dropped;
 	struct work_struct deliver_work;
+
+	struct wakeup_source *ws;
 };
 
 static struct pmic_glink *__pmic_glink;
@@ -70,10 +74,12 @@ struct pmic_glink_client {
 	struct list_head node;
 
 	struct pmic_glink *pg;
+	struct device *dev;
 	unsigned int id;
 
 	void (*cb)(const void *data, size_t len, void *priv);
 	void (*pdr_notify)(void *priv, int state);
+	bool (*wakeup_filter)(const void *data, size_t len, void *priv);
 	void *priv;
 };
 
@@ -96,6 +102,22 @@ static void _devm_pmic_glink_release_client(struct device *dev, void *res)
 	spin_unlock_irqrestore(&pg->client_lock, flags);
 }
 
+/**
+ * devm_pmic_glink_client_alloc() - allocate a client of the PMIC GLINK service
+ * @dev:	client device, a child of the pmic_glink device
+ * @id:		owner of the messages for the client
+ * @cb:		called, in atomic context, with the messages for the client
+ * @pdr:	called, in atomic context, with the state of the remote service
+ * @priv:	passed to @cb, @pdr and the wakeup filter
+ *
+ * A notification for a client whose @dev may wake up the system, see
+ * device_may_wakeup(), is a wakeup event, unless the wakeup filter of the
+ * client rejects it. Notifications that arrive during system sleep reach @cb
+ * once the system has resumed, only the latest one of identical notifications.
+ *
+ * Return: the client, to be registered with pmic_glink_client_register(), or
+ * an ERR_PTR() on failure.
+ */
 struct pmic_glink_client *devm_pmic_glink_client_alloc(struct device *dev,
 						       unsigned int id,
 						       void (*cb)(const void *, size_t, void *),
@@ -110,6 +132,7 @@ struct pmic_glink_client *devm_pmic_glink_client_alloc(struct device *dev,
 		return ERR_PTR(-ENOMEM);
 
 	client->pg = pg;
+	client->dev = dev;
 	client->id = id;
 	client->cb = cb;
 	client->pdr_notify = pdr;
@@ -122,6 +145,22 @@ struct pmic_glink_client *devm_pmic_glink_client_alloc(struct device *dev,
 }
 EXPORT_SYMBOL_GPL(devm_pmic_glink_client_alloc);
 
+/**
+ * pmic_glink_client_set_wakeup_filter() - select the wakeup notifications
+ * @client:	client from devm_pmic_glink_client_alloc(), not registered yet
+ * @filter:	called, in atomic context, with each notification for the client
+ *		while its device may wake up the system; returns true to wake
+ *		the system up
+ *
+ * Without a filter, every notification for the client is a wakeup event.
+ */
+void pmic_glink_client_set_wakeup_filter(struct pmic_glink_client *client,
+					 bool (*filter)(const void *, size_t, void *))
+{
+	client->wakeup_filter = filter;
+}
+EXPORT_SYMBOL_GPL(pmic_glink_client_set_wakeup_filter);
+
 void pmic_glink_client_register(struct pmic_glink_client *client)
 {
 	struct pmic_glink *pg = client->pg;
@@ -168,6 +207,24 @@ int pmic_glink_send(struct pmic_glink_client *client, void *data, size_t len)
 }
 EXPORT_SYMBOL_GPL(pmic_glink_send);
 
+static bool pmic_glink_is_wakeup_event(struct pmic_glink *pg, const void *data, size_t len)
+{
+	const struct pmic_glink_hdr *hdr = data;
+	struct pmic_glink_client *client;
+
+	guard(spinlock_irqsave)(&pg->client_lock);
+
+	list_for_each_entry(client, &pg->clients, node) {
+		if (client->id != le32_to_cpu(hdr->owner) || !device_may_wakeup(client->dev))
+			continue;
+
+		if (!client->wakeup_filter || client->wakeup_filter(data, len, client->priv))
+			return true;
+	}
+
+	return false;
+}
+
 static void pmic_glink_dispatch(struct pmic_glink *pg, const void *data, size_t len)
 {
 	const struct pmic_glink_hdr *hdr = data;
@@ -224,6 +281,7 @@ static int pmic_glink_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
 {
 	struct pmic_glink_hdr *hdr;
 	struct pmic_glink *pg = dev_get_drvdata(&rpdev->dev);
+	bool wakeup;
 
 	if (len < sizeof(*hdr)) {
 		dev_warn(pg->dev, "ignoring truncated message\n");
@@ -238,8 +296,16 @@ static int pmic_glink_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
 		return 0;
 	}
 
+	wakeup = pmic_glink_is_wakeup_event(pg, data, len);
+
 	guard(spinlock_irqsave)(&pg->event_lock);
 
+	/* Keep the system awake until the notification has been passed on */
+	if (wakeup) {
+		__pm_stay_awake(pg->ws);
+		pm_system_wakeup();
+	}
+
 	pmic_glink_add_notification(pg, data, len);
 	queue_work(system_freezable_wq, &pg->deliver_work);
 
@@ -340,6 +406,7 @@ static void pmic_glink_deliver_work(struct work_struct *work)
 		if (!event) {
 			dropped = pg->num_dropped;
 			pg->num_dropped = 0;
+			__pm_relax(pg->ws);
 			spin_unlock_irqrestore(&pg->event_lock, flags);
 			break;
 		}
@@ -435,6 +502,11 @@ static struct rpmsg_driver pmic_glink_rpmsg_driver = {
 	},
 };
 
+static void pmic_glink_unregister_wakeup_source(void *ws)
+{
+	wakeup_source_unregister(ws);
+}
+
 static int pmic_glink_probe(struct platform_device *pdev)
 {
 	struct pdr_service *service;
@@ -453,6 +525,14 @@ static int pmic_glink_probe(struct platform_device *pdev)
 	spin_lock_init(&pg->client_lock);
 	mutex_init(&pg->state_lock);
 
+	pg->ws = wakeup_source_register(&pdev->dev, dev_name(&pdev->dev));
+	if (!pg->ws && IS_ENABLED(CONFIG_PM_SLEEP))
+		return -ENOMEM;
+
+	ret = devm_add_action_or_reset(&pdev->dev, pmic_glink_unregister_wakeup_source, pg->ws);
+	if (ret)
+		return ret;
+
 	INIT_LIST_HEAD(&pg->events);
 	spin_lock_init(&pg->event_lock);
 	INIT_WORK(&pg->deliver_work, pmic_glink_deliver_work);
diff --git a/include/linux/soc/qcom/pmic_glink.h b/include/linux/soc/qcom/pmic_glink.h
index 7cddf1027752..786d2f35a9a9 100644
--- a/include/linux/soc/qcom/pmic_glink.h
+++ b/include/linux/soc/qcom/pmic_glink.h
@@ -28,6 +28,8 @@ struct pmic_glink_client *devm_pmic_glink_client_alloc(struct device *dev,
 						       void (*cb)(const void *, size_t, void *),
 						       void (*pdr)(void *, int),
 						       void *priv);
+void pmic_glink_client_set_wakeup_filter(struct pmic_glink_client *client,
+					 bool (*filter)(const void *, size_t, void *));
 void pmic_glink_client_register(struct pmic_glink_client *client);
 
 #endif

  parent reply	other threads:[~2026-11-29 13:46 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29 13:44 [PATCH v2 0/5] soc: qcom: pmic_glink: Deliver notifications from a freezable workqueue Vladyslav Tymoshchuk
2026-11-29 13:44 ` [PATCH v2 1/5] usb: typec: ucsi: glink: Handle a restart of the remote before the work runs Vladyslav Tymoshchuk
2026-11-29 13:44 ` [PATCH v2 2/5] soc: qcom: pmic_glink: Deliver notifications from a freezable workqueue Vladyslav Tymoshchuk
2026-11-29 13:44 ` Vladyslav Tymoshchuk [this message]
2026-11-29 13:44 ` [PATCH v2 4/5] usb: typec: ucsi: glink: Wake up the system on connector changes Vladyslav Tymoshchuk
2026-11-29 13:44 ` [PATCH v2 5/5] power: supply: qcom_battmgr: Allow charger changes to wake up the system Vladyslav Tymoshchuk

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=20261129134442.579-4-tvlad744@gmail.com \
    --to=tvlad744@gmail.com \
    --cc=26rote@gmail.com \
    --cc=abel.vesa@oss.qualcomm.com \
    --cc=andersson@kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=heikki.krogerus@linux.intel.com \
    --cc=jishnu.prakash@oss.qualcomm.com \
    --cc=konradybcio@kernel.org \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=neil.armstrong@linaro.org \
    --cc=sre@kernel.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

all inboxes | Powered by JetHome®