public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Gustavo Arantes <dev.gustavoa@gmail.com>
To: gregkh@linuxfoundation.org, marvin24@gmx.de
Cc: linux-staging@lists.linux.dev, ac100@lists.launchpad.net,
	linux-tegra@vger.kernel.org, linux-kernel@vger.kernel.org,
	dev.gustavoa@gmail.com
Subject: [PATCH 1/2] staging: nvec_power: make EC queries synchronous
Date: Thu, 12 Mar 2026 18:11:50 -0300	[thread overview]
Message-ID: <20260312211151.85379-2-dev.gustavoa@gmail.com> (raw)
In-Reply-To: <20260312211151.85379-1-dev.gustavoa@gmail.com>

The nvec power driver still submits its periodic AC and battery
queries, as well as the battery metadata requests, asynchronously.
That leaves it with several independent request sources and makes it
hard to quiesce the driver cleanly before system sleep.

Add a small core helper to feed a synchronous reply back through the
normal NVEC message parser, then switch nvec_power over to synchronous
queries. Move the battery metadata requests out of the notifier
callback into a worker so they can use the same synchronous path.

This keeps the existing notifier-based state updates intact while
serializing the driver's EC traffic.

Signed-off-by: Gustavo Arantes <dev.gustavoa@gmail.com>
---
 drivers/staging/nvec/nvec.c       |  7 ++--
 drivers/staging/nvec/nvec.h       |  2 +
 drivers/staging/nvec/nvec_power.c | 70 ++++++++++++++++++++++++-------
 3 files changed, 61 insertions(+), 18 deletions(-)

diff --git a/drivers/staging/nvec/nvec.c b/drivers/staging/nvec/nvec.c
index 952c5a849a56..cb1d92769941 100644
--- a/drivers/staging/nvec/nvec.c
+++ b/drivers/staging/nvec/nvec.c
@@ -408,14 +408,14 @@ static void nvec_request_master(struct work_struct *work)
 }
 
 /**
- * parse_msg - Print some information and call the notifiers on an RX message
+ * nvec_msg_parse - Print some information and call the notifiers on an RX message
  * @nvec: A &struct nvec_chip
  * @msg: A message received by @nvec
  *
  * Paarse some pieces of the message and then call the chain of notifiers
  * registered via nvec_register_notifier.
  */
-static int parse_msg(struct nvec_chip *nvec, struct nvec_msg *msg)
+int nvec_msg_parse(struct nvec_chip *nvec, struct nvec_msg *msg)
 {
 	if ((msg->data[0] & 1 << 7) == 0 && msg->data[3]) {
 		dev_err(nvec->dev, "ec responded %*ph\n", 4, msg->data);
@@ -432,6 +432,7 @@ static int parse_msg(struct nvec_chip *nvec, struct nvec_msg *msg)
 
 	return 0;
 }
+EXPORT_SYMBOL_GPL(nvec_msg_parse);
 
 /**
  * nvec_dispatch - Process messages received from the EC
@@ -459,7 +460,7 @@ static void nvec_dispatch(struct work_struct *work)
 			nvec->last_sync_msg = msg;
 			complete(&nvec->sync_write);
 		} else {
-			parse_msg(nvec, msg);
+			nvec_msg_parse(nvec, msg);
 			nvec_msg_free(nvec, msg);
 		}
 		spin_lock_irqsave(&nvec->rx_lock, flags);
diff --git a/drivers/staging/nvec/nvec.h b/drivers/staging/nvec/nvec.h
index 80c0353f141c..d6854c9d629d 100644
--- a/drivers/staging/nvec/nvec.h
+++ b/drivers/staging/nvec/nvec.h
@@ -168,6 +168,8 @@ int nvec_write_sync(struct nvec_chip *nvec,
 		    const unsigned char *data, short size,
 		    struct nvec_msg **msg);
 
+int nvec_msg_parse(struct nvec_chip *nvec, struct nvec_msg *msg);
+
 int nvec_register_notifier(struct nvec_chip *nvec,
 			   struct notifier_block *nb,
 			   unsigned int events);
diff --git a/drivers/staging/nvec/nvec_power.c b/drivers/staging/nvec/nvec_power.c
index 2faab9fdedef..6b3235f41d07 100644
--- a/drivers/staging/nvec/nvec_power.c
+++ b/drivers/staging/nvec/nvec_power.c
@@ -23,6 +23,7 @@
 struct nvec_power {
 	struct notifier_block notifier;
 	struct delayed_work poller;
+	struct work_struct bat_init;
 	struct nvec_chip *nvec;
 	int on;
 	int bat_present;
@@ -106,14 +107,38 @@ static const int bat_init[] = {
 	MANUFACTURER, MODEL, TYPE,
 };
 
-static void get_bat_mfg_data(struct nvec_power *power)
+static int nvec_power_write_sync(struct nvec_power *power,
+				 unsigned char *buf, short size)
 {
+	struct nvec_msg *msg;
+	int ret;
+
+	ret = nvec_write_sync(power->nvec, buf, size, &msg);
+	if (ret < 0)
+		return ret;
+
+	nvec_msg_parse(power->nvec, msg);
+	nvec_msg_free(power->nvec, msg);
+
+	return 0;
+}
+
+static void nvec_power_bat_init(struct work_struct *work)
+{
+	struct nvec_power *power = container_of(work, struct nvec_power,
+						 bat_init);
+	unsigned char buf[] = { NVEC_BAT, 0 };
 	int i;
-	char buf[] = { NVEC_BAT, SLOT_STATUS };
+	int ret;
 
 	for (i = 0; i < ARRAY_SIZE(bat_init); i++) {
 		buf[1] = bat_init[i];
-		nvec_write_async(power->nvec, buf, 2);
+
+		ret = nvec_power_write_sync(power, buf, sizeof(buf));
+		if (ret < 0)
+			dev_warn(power->nvec->dev,
+				 "failed to query battery data %u: %d\n",
+				 bat_init[i], ret);
 	}
 }
 
@@ -133,7 +158,7 @@ static int nvec_power_bat_notifier(struct notifier_block *nb,
 		if (res->plc[0] & 1) {
 			if (power->bat_present == 0) {
 				status_changed = 1;
-				get_bat_mfg_data(power);
+				schedule_work(&power->bat_init);
 			}
 
 			power->bat_present = 1;
@@ -347,15 +372,19 @@ static const int bat_iter[] = {
 
 static void nvec_power_poll(struct work_struct *work)
 {
-	char buf[] = { NVEC_SYS, GET_SYSTEM_STATUS };
+	unsigned char buf[] = { NVEC_SYS, GET_SYSTEM_STATUS };
 	struct nvec_power *power = container_of(work, struct nvec_power,
-						poller.work);
+							poller.work);
+	int ret;
 
 	if (counter >= ARRAY_SIZE(bat_iter))
 		counter = 0;
 
 	/* AC status via sys req */
-	nvec_write_async(power->nvec, buf, 2);
+	ret = nvec_power_write_sync(power, buf, sizeof(buf));
+	if (ret < 0)
+		dev_warn(power->nvec->dev,
+			 "failed to query AC status: %d\n", ret);
 	msleep(100);
 
 	/*
@@ -364,7 +393,10 @@ static void nvec_power_poll(struct work_struct *work)
 	 */
 	buf[0] = NVEC_BAT;
 	buf[1] = bat_iter[counter++];
-	nvec_write_async(power->nvec, buf, 2);
+	ret = nvec_power_write_sync(power, buf, sizeof(buf));
+	if (ret < 0)
+		dev_warn(power->nvec->dev,
+			 "failed to query battery status: %d\n", ret);
 
 	schedule_delayed_work(to_delayed_work(work), msecs_to_jiffies(5000));
 };
@@ -394,13 +426,13 @@ static int nvec_power_probe(struct platform_device *pdev)
 		power->notifier.notifier_call = nvec_power_notifier;
 
 		INIT_DELAYED_WORK(&power->poller, nvec_power_poll);
-		schedule_delayed_work(&power->poller, msecs_to_jiffies(5000));
 		break;
 	case BAT:
 		psy = &nvec_bat_psy;
 		psy_desc = &nvec_bat_psy_desc;
 
 		power->notifier.notifier_call = nvec_power_bat_notifier;
+		INIT_WORK(&power->bat_init, nvec_power_bat_init);
 		break;
 	default:
 		return -ENODEV;
@@ -408,25 +440,33 @@ static int nvec_power_probe(struct platform_device *pdev)
 
 	nvec_register_notifier(nvec, &power->notifier, NVEC_SYS);
 
-	if (pdev->id == BAT)
-		get_bat_mfg_data(power);
-
 	*psy = power_supply_register(&pdev->dev, psy_desc, &psy_cfg);
 
-	return PTR_ERR_OR_ZERO(*psy);
+	if (IS_ERR(*psy)) {
+		nvec_unregister_notifier(nvec, &power->notifier);
+		return PTR_ERR(*psy);
+	}
+
+	if (pdev->id == AC)
+		schedule_delayed_work(&power->poller, msecs_to_jiffies(5000));
+	else
+		schedule_work(&power->bat_init);
+
+	return 0;
 }
 
 static void nvec_power_remove(struct platform_device *pdev)
 {
 	struct nvec_power *power = platform_get_drvdata(pdev);
 
-	cancel_delayed_work_sync(&power->poller);
 	nvec_unregister_notifier(power->nvec, &power->notifier);
 	switch (pdev->id) {
 	case AC:
+		cancel_delayed_work_sync(&power->poller);
 		power_supply_unregister(nvec_psy);
 		break;
 	case BAT:
+		cancel_work_sync(&power->bat_init);
 		power_supply_unregister(nvec_bat_psy);
 	}
 }
@@ -435,7 +475,7 @@ static struct platform_driver nvec_power_driver = {
 	.probe = nvec_power_probe,
 	.remove = nvec_power_remove,
 	.driver = {
-		   .name = "nvec-power",
+		.name = "nvec-power",
 	}
 };
 
-- 
2.53.0


  reply	other threads:[~2026-03-12 21:12 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-12 21:11 [PATCH 0/2] staging: nvec_power: quiesce EC queries for system suspend Gustavo Arantes
2026-03-12 21:11 ` Gustavo Arantes [this message]
2026-03-12 21:11 ` [PATCH 2/2] staging: nvec_power: stop EC queries during " Gustavo Arantes
2026-03-15 20:48 ` [PATCH 0/2] staging: nvec_power: quiesce EC queries for " Marc Dietrich
2026-03-15 21:48   ` Gustavo Arantes

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=20260312211151.85379-2-dev.gustavoa@gmail.com \
    --to=dev.gustavoa@gmail.com \
    --cc=ac100@lists.launchpad.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-staging@lists.linux.dev \
    --cc=linux-tegra@vger.kernel.org \
    --cc=marvin24@gmx.de \
    /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