From 3c5ae4441e29fc6219abd76862132bc086aeb38f Mon Sep 17 00:00:00 2001
From: avishad verma <avishad.verma@infineon.com>
Date: Tue, 26 Nov 2024 02:37:39 -0600
Subject: [PATCH 1/4] brcmfmac: Modified and extended sdio idle sleep param

Driver module parameter idle_time_zero is modified to
provide greater flexibility in managing SDIO bus power
consumption. The updated parameter, now called bus_idle_time,
allows for customizable SDIO bus idle time before sleep mode.

This feature enables to configure the optimal balance between
power savings and bus activity.
For instance, setting bus_idle_time to 0 ensures the SDIO
bus remains always active,
while a non-zero value (unit of 10ms) allows the bus to idle for
a specified period before entering sleep mode.
Default value of the module param is kept same as current configuration
of 20ms.
e.g.
insmod brcmfmac.ko bus_idle_time=0 (SDIO Bus always active)
insmod brcmfmac.ko bus_idle_time=N (SDIO Bus idle for N*10ms before sleep)

Fixes SWLINUX-4853
Signed-off-by: avishad verma <avishad.verma@infineon.com>
---
 .../wireless/broadcom/brcm80211/brcmfmac/common.c    | 12 ++++++------
 .../wireless/broadcom/brcm80211/brcmfmac/common.h    |  3 ++-
 .../net/wireless/broadcom/brcm80211/brcmfmac/sdio.c  |  8 ++------
 3 files changed, 10 insertions(+), 13 deletions(-)

diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/common.c b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/common.c
index 84f45ac45a30..63c4e8a4c7fc 100644
--- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/common.c
+++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/common.c
@@ -140,9 +140,9 @@ static int brcmf_sdio_idleclk_disable = BRCMFMAC_AUTO;
 module_param_named(sdio_idleclk_disable, brcmf_sdio_idleclk_disable, int, 0644);
 MODULE_PARM_DESC(sdio_idleclk_disable, "Disable SDIO idle clock");
 
-static int brcmf_idle_time_zero;
-module_param_named(idle_time_zero, brcmf_idle_time_zero, int, 0644);
-MODULE_PARM_DESC(idle_time_zero, "Set idle interval to zero");
+static int brcmf_sdio_bus_idle_time = BRCMF_DEFAULT_SDIO_IDLE_CONFIG;
+module_param_named(bus_idle_time, brcmf_sdio_bus_idle_time, int, 0644);
+MODULE_PARM_DESC(bus_idle_time, "Config SDIO bus idle time");
 
 static int brcmf_short_psq;
 module_param_named(short_psq, brcmf_short_psq, int, 0);
@@ -717,7 +717,7 @@ int brcmf_debugfs_param_read(struct seq_file *s, void *data)
 	seq_printf(s, "%-20s: 0x%x\n", "offload_feat", brcmf_offload_feat);
 	seq_printf(s, "%-20s: %d\n", "txglomsz", brcmf_sdiod_txglomsz);
 	seq_printf(s, "%-20s: %d\n", "bt_over_sdio", !!brcmf_bt_over_sdio);
-	seq_printf(s, "%-20s: %d\n", "idle_time_zero", !!brcmf_idle_time_zero);
+	seq_printf(s, "%-20s: %d\n", "bus_idle_time", brcmf_sdio_bus_idle_time);
 	seq_printf(s, "%-20s: %d\n", "short_psq", !!brcmf_short_psq);
 
 	return 0;
@@ -799,8 +799,8 @@ struct brcmf_mp_device *brcmf_get_module_param(struct device *dev,
 		brcmf_dbg(INFO, "txglomsz: %d\n", settings->bus.sdio.txglomsz);
 	}
 
-	settings->idle_time_zero = brcmf_idle_time_zero;
-	brcmf_dbg(INFO, "idle_time_zero: %d\n", settings->idle_time_zero);
+	settings->sdio_bus_idle_time = brcmf_sdio_bus_idle_time;
+	brcmf_dbg(INFO, "sdio_bus_idle_time: %d\n", settings->sdio_bus_idle_time);
 
 	/* See if there is any device specific platform data configured */
 	found = false;
diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/common.h b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/common.h
index f7ce5ca76647..6b7115c347d6 100644
--- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/common.h
+++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/common.h
@@ -15,6 +15,7 @@
 #define BRCMFMAC_ENABLE		1
 #define BRCMFMAC_AUTO		2
 
+#define BRCMF_DEFAULT_SDIO_IDLE_CONFIG		2
 /* Keeping these macro definition here because these are defined in mmc drivers.
  * So for 3rd party mmc, fmac build should not fail due to build error.
  */
@@ -96,7 +97,7 @@ struct brcmf_mp_device {
 	} bus;
 	bool		pkt_prio;
 	int			idleclk_disable;
-	bool		idle_time_zero;
+	int		sdio_bus_idle_time;
 };
 
 /**
diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/sdio.c b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/sdio.c
index 2c182694f209..5b68b2cff52a 100644
--- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/sdio.c
+++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/sdio.c
@@ -343,7 +343,6 @@ struct rte_console {
 					 * when idle
 					 */
 #define BRCMF_IDLE_STOP		(-1)	/* Request SD clock be stopped */
-#define BRCMF_IDLE_INTERVAL	1
 
 #define KSO_WAIT_US 50
 #define KSO_MAX_SEQ_TIME_NS (1000000 * 10) /* Ideal time for kso sequence 10ms in ns*/
@@ -4554,7 +4553,7 @@ static void brcmf_sdio_bus_watchdog(struct brcmf_sdio *bus)
 		if ((!bus->dpc_running) && (bus->idletime > 0) &&
 		    (bus->clkstate == CLK_AVAIL)) {
 			bus->idlecount++;
-			if (bus->idlecount > bus->idletime) {
+			if (bus->idlecount >= bus->idletime) {
 				brcmf_dbg(SDIO, "idle\n");
 				sdio_claim_host(bus->sdiodev->func1);
 #ifdef DEBUG
@@ -5754,10 +5753,7 @@ struct brcmf_sdio *brcmf_sdio_probe(struct brcmf_sdio_dev *sdiodev)
 	/* ...and initialize clock/power states */
 	bus->clkstate = CLK_SDONLY;
 
-	if (sdiodev->settings->idle_time_zero)
-		bus->idletime = 0;
-	else
-		bus->idletime = BRCMF_IDLE_INTERVAL;
+	bus->idletime = sdiodev->settings->sdio_bus_idle_time;
 
 	/* SR state */
 	bus->sr_enabled = false;
-- 
2.25.0

