From b5b257b45c5be19f550959177b5ba47d45d0849c Mon Sep 17 00:00:00 2001
From: Ondrej Jirman <megi@xff.cz>
Date: Sat, 29 Jul 2023 23:30:19 +0200
Subject: [PATCH 006/480] bes2600: Use device tree exclusively for platform
 data

Signed-off-by: Ondrej Jirman <megi@xff.cz>
---
 drivers/staging/bes2600/bes2600_plat.h |   8 +-
 drivers/staging/bes2600/bes2600_sdio.c | 240 +++++--------------------
 2 files changed, 51 insertions(+), 197 deletions(-)

diff --git a/drivers/staging/bes2600/bes2600_plat.h b/drivers/staging/bes2600/bes2600_plat.h
index 5838435b9315..aaec3b9e1c99 100644
--- a/drivers/staging/bes2600/bes2600_plat.h
+++ b/drivers/staging/bes2600/bes2600_plat.h
@@ -33,10 +33,10 @@ struct bes2600_platform_data_sdio {
 	/* All others are optional */
 	bool have_5ghz;
 	bool no_nptb;       /* SDIO hardware does not support non-power-of-2-blocksizes */
-	int reset;          /* GPIO to RSTn signal (0 disables) */
-	int powerup;        /* GPIO to POWERUP signal (0 disables) */
-	int wakeup;         /* GPIO to WAKEUP signal (0 disables) */
-	int host_wakeup;    /* wifi GPIO to WAKEUP host signal (0 disables) */
+	struct gpio_desc *reset;          /* GPIO to RSTn signal (0 disables) */
+	struct gpio_desc *powerup;        /* GPIO to POWERUP signal (0 disables) */
+	struct gpio_desc *wakeup;         /* GPIO to WAKEUP signal (0 disables) */
+	struct gpio_desc *host_wakeup;    /* wifi GPIO to WAKEUP host signal (0 disables) */
 	bool wlan_bt_hostwake_registered;/* wifi request_irq success or not */
 	int gpio_irq;       /* IRQ line or 0 to use SDIO IRQ */
 	int (*power_ctrl)(const struct bes2600_platform_data_sdio *pdata,
diff --git a/drivers/staging/bes2600/bes2600_sdio.c b/drivers/staging/bes2600/bes2600_sdio.c
index d9b60c72fd45..83a1d7a7eebc 100644
--- a/drivers/staging/bes2600/bes2600_sdio.c
+++ b/drivers/staging/bes2600/bes2600_sdio.c
@@ -606,33 +606,9 @@ static int bes2600_sdio_off(const struct bes2600_platform_data_sdio *pdata)
 {
 	bes2600_info(BES2600_DBG_SDIO, "%s enter\n", __func__);
 
-#if defined(PLAT_ALLWINNER)
-	sunxi_wlan_set_power(false);
-#endif
-
-#if defined(PLAT_ROCKCHIP)
-	rockchip_wifi_set_carddetect(0);
-	rockchip_wifi_power(0);
-#endif
-
-#ifdef PLAT_CVITEK_182X
-	if (gpio_is_valid(pdata->powerup)) {
-		gpio_direction_output(pdata->powerup, 0);
-	}
-
-	if (gpio_is_valid(pdata->reset)) {
-		gpio_direction_output(pdata->reset, 0);
-	}
-#endif
-
-	if (pdata == NULL)
-		return 0;
+	gpiod_direction_output(pdata->powerup, 0);
 
-#ifdef BES2600_INDEPENDENT_EVB
-	if (gpio_is_valid(pdata->powerup)) {
-		gpio_direction_output(pdata->powerup, 0);
-	}
-#endif
+	gpiod_direction_output(pdata->reset, 0);
 
 	return 0;
 }
@@ -642,36 +618,13 @@ static int bes2600_sdio_on(const struct bes2600_platform_data_sdio *pdata)
 
 	bes2600_info(BES2600_DBG_SDIO, "%s enter\n", __func__);
 
-#if defined(PLAT_ALLWINNER)
-	sunxi_wlan_set_power(true);
-#endif
-
-#ifdef PLAT_ROCKCHIP
-	rockchip_wifi_power(0);
-	rockchip_wifi_power(1);
-	bes2600_chrdev_start_bus_probe();
-	rockchip_wifi_set_carddetect(1);
-#endif
-
-#ifdef PLAT_CVITEK_182X
-	if (gpio_is_valid(pdata->powerup)) {
-		gpio_direction_output(pdata->powerup, 1);
-	}
+	gpiod_direction_output(pdata->powerup, 1);
 
 	msleep(10);
-	if (gpio_is_valid(pdata->reset)) {
-		gpio_direction_output(pdata->reset, 0);
-	}
-	cvi_sdio_rescan();
-#endif
 
-	if (pdata != NULL) {
-#ifdef BES2600_INDEPENDENT_EVB
-		if (gpio_is_valid(pdata->powerup)) {
-			gpio_direction_output(pdata->powerup, 1);
-		}
-#endif
-	}
+	gpiod_direction_output(pdata->reset, 0);
+
+	bes2600_chrdev_start_bus_probe();
 
 #if defined(BES2600_BOOT_UART_TO_SDIO)
 	return bes2600_boot_uart_to_sdio(&bes2600_sdio_sbus_ops);
@@ -1405,151 +1358,63 @@ err2:
 	return 0;
 }
 
-#if defined(PLAT_ALLWINNER)|| defined (PLAT_ROCKCHIP) || defined(PLAT_CVITEK_182X)
 static struct bes2600_platform_data_sdio bes_sdio_plat_data = {
-#if defined(BES2600_INDEPENDENT_EVB)
-	.reset = GPIOA(9),
-	.powerup = GPIOC(3),
-	.wakeup = -1,
-#elif defined(BES2600_INTEGRATED_MODULE_V1)
-	.reset = GPIOA(0),
-	.powerup = -1,
-	.wakeup = -1,
-#elif defined(BES2600_INTEGRATED_MODULE_V2)
-	.reset = GPIOM(2),
-	.powerup = -1,
-	.wakeup = GPIOM(5),
-#elif defined(PLAT_ROCKCHIP)
-	.reset = -1,
-	.powerup = -1,
-	.wakeup = -1,
-#elif defined(PLAT_CVITEK_182X)
-	.reset = -1,
-	.powerup = -1,
-	.wakeup = -1,
-	.host_wakeup = -1,
-#endif
 };
-#endif
 
 struct bes2600_platform_data_sdio *bes2600_get_platform_data(void)
 {
-#if defined(PLAT_ALLWINNER)|| defined (PLAT_ROCKCHIP) || defined(PLAT_CVITEK_182X)
 	return &bes_sdio_plat_data;
-#else
-	return NULL;
-#endif
-}
-
-
-static void __attribute__((unused)) bes2600_get_gpio_from_dts(int *gpio_num, const char *gpio_name)
-{
-	int wakeup_gpio;
-	// enum of_gpio_flags flags;
-	struct device_node *wireless_node;
-	wireless_node = of_find_node_with_property(NULL, gpio_name);
-	if(wireless_node != NULL){
-		// wakeup_gpio = of_get_named_gpio_flags(wireless_node, gpio_name, 0, &flags);
-		wakeup_gpio = of_get_named_gpio(wireless_node, gpio_name, 0);
-		if (gpio_is_valid(wakeup_gpio))
-			*gpio_num = wakeup_gpio;
-	}else{
-		bes2600_err(BES2600_DBG_SDIO, "find node for %s failed\n", gpio_name);
-	}
 }
 
-
 static int bes2600_platform_data_init(void)
 {
-	int ret = 0;
 	struct bes2600_platform_data_sdio *pdata = bes2600_get_platform_data();
-	if (pdata == NULL)
-		return 0;
+	struct device_node *np;
+	int ret = 0;
 
-		/* Ensure I/Os are pulled low */
-#ifdef PLAT_CVITEK_182X
-	pdata->reset=cvi_get_wifi_reset_gpio();
-#endif
-	if (gpio_is_valid(pdata->reset)) {
-		ret = gpio_request(pdata->reset, "bes2600_wlan_reset");
-		if (ret) {
-			bes2600_err(BES2600_DBG_SDIO, "can't reqest reset_gpio:%d\n", ret);
-			goto exit;
-		} else {
-			gpio_direction_output(pdata->reset, 0);
-		}
-	} else {
-		bes2600_err(BES2600_DBG_SDIO, "reset is invalid\n");
+	np = of_find_compatible_node(NULL, NULL, "bestechnic,bes2600");
+	if (!np) {
+		pr_err("bestechnic,bes2600 device node found!\n");
+		return -ENODEV;
 	}
-#ifdef PLAT_CVITEK_182X
-	pdata->powerup=cvi_get_wifi_pwr_on_gpio();
-#endif
-	if (gpio_is_valid(pdata->powerup)) {
-		ret = gpio_request(pdata->powerup, "bes2600_wlan_powerup");
-		if (ret) {
-			bes2600_err(BES2600_DBG_SDIO, "can't request powerup_gpio:%d\n", ret);
-			goto exit;
-		} else {
-			gpio_direction_output(pdata->powerup, 0);
-		}
-	} else {
-		bes2600_err(BES2600_DBG_SDIO, "powerup is invalid\n");
+
+	pdata->reset = fwnode_gpiod_get_index(&np->fwnode, "reset", 0, GPIOD_OUT_HIGH, "bes2600-reset");
+	if (IS_ERR(pdata->reset)) {
+		bes2600_err(BES2600_DBG_SDIO, "Can't request reset gpio (%ld)\n", PTR_ERR(pdata->reset));
+		pdata->reset = NULL;
 	}
-#ifdef PLAT_CVITEK_182X
-	pdata->wakeup=cvi_get_wifi_wakeup_gpio();
-#else
-	bes2600_get_gpio_from_dts(&pdata->wakeup, "WIFI,host_wakeup_wifi");
-#endif
-	if (gpio_is_valid(pdata->wakeup)) {
-		ret = gpio_request(pdata->wakeup, "bes2600_wakeup");
-		if (ret) {
-			bes2600_err(BES2600_DBG_SDIO, "can't request wakeup_gpio:%d\n", ret);
-			goto exit;
-		} else {
-			gpio_direction_output(pdata->wakeup, 0);
-		}
-	} else {
-		bes2600_err(BES2600_DBG_SDIO, "wakeup is invalid\n");
+
+	pdata->powerup = fwnode_gpiod_get_index(&np->fwnode, "powerup", 0, GPIOD_OUT_LOW, "bes2600-powerup");
+	if (IS_ERR(pdata->powerup)) {
+		bes2600_err(BES2600_DBG_SDIO, "Can't request powerup gpio (%ld)\n", PTR_ERR(pdata->powerup));
+		pdata->powerup = NULL;
 	}
-#ifndef PLAT_CVITEK_182X
-	bes2600_get_gpio_from_dts(&pdata->host_wakeup, "WIFI,host_wake_irq");
-#endif
-	if (gpio_is_valid(pdata->host_wakeup)) {
-		ret = gpio_request(pdata->host_wakeup, "bes2600_host_irq");
-		if (ret) {
-			bes2600_err(BES2600_DBG_SDIO, "can't reqest host_wake_gpio:%d\n", ret);
-			goto exit;
-		} else {
-			gpio_direction_input(pdata->host_wakeup);
-		}
-	} else {
-		bes2600_err(BES2600_DBG_SDIO, "host_wakeup is invalid\n");
+
+	pdata->wakeup = fwnode_gpiod_get_index(&np->fwnode, "device-wakeup", 0, GPIOD_OUT_LOW, "bes2600-device-wakeup");
+	if (IS_ERR(pdata->wakeup)) {
+		bes2600_err(BES2600_DBG_SDIO, "Can't request device wakeup gpio (%ld)\n", PTR_ERR(pdata->wakeup));
+		pdata->wakeup = NULL;
 	}
 
-	pdata->wlan_bt_hostwake_registered = false;
-exit:
+	pdata->host_wakeup = fwnode_gpiod_get_index(&np->fwnode, "host-wakeup", 0, GPIOD_IN, "bes2600-host-wakeup");
+	if (IS_ERR(pdata->host_wakeup)) {
+		bes2600_err(BES2600_DBG_SDIO, "Can't request host wakeup gpio (%ld)\n", PTR_ERR(pdata->host_wakeup));
+		pdata->host_wakeup = NULL;
+	}
+
+	of_node_put(np);
+
 	return ret;
 }
 
 static void bes2600_platform_data_deinit(void)
 {
 	const struct bes2600_platform_data_sdio *pdata = bes2600_get_platform_data();
-	if (pdata == NULL) {
-		return;
-	}
 
-	if (gpio_is_valid(pdata->reset)) {
-		gpio_free(pdata->reset);
-	}
-	if (gpio_is_valid(pdata->powerup)) {
-		gpio_free(pdata->powerup);
-	}
-	if (gpio_is_valid(pdata->wakeup)) {
-		gpio_free(pdata->wakeup);
-	}
-	if (gpio_is_valid(pdata->host_wakeup)) {
-		gpio_free(pdata->host_wakeup);
-	}
+	gpiod_put(pdata->reset);
+	gpiod_put(pdata->powerup);
+	gpiod_put(pdata->wakeup);
+	gpiod_put(pdata->host_wakeup);
 }
 
 static int bes2600_sdio_reset(struct sbus_priv *self)
@@ -1562,10 +1427,11 @@ static int bes2600_sdio_reset(struct sbus_priv *self)
 		return 0;
 
 	if (plat_data->reset) {
-		gpio_set_value(plat_data->reset, 1);
+		gpiod_set_value(plat_data->reset, 1);
 		mdelay(50);
-		gpio_set_value(plat_data->reset, 0);
+		gpiod_set_value(plat_data->reset, 0);
 	}
+
 	return 0;
 }
 
@@ -1623,8 +1489,8 @@ static void bes2600_gpio_wakeup_mcu(struct sbus_priv *self, int flag)
 	/* do wakeup mcu operation */
 	if(gpio_wakeup) {
 		bes2600_dbg(BES2600_DBG_SDIO, "pull high gpio by flag:%d\n", flag);
-		if (gpio_is_valid(pdata->wakeup)) {
-			gpio_set_value(pdata->wakeup, 1);
+		if (pdata->wakeup) {
+			gpiod_set_value(pdata->wakeup, 1);
 			msleep(10);
 		} else {
 			bes2600_err(BES2600_DBG_SDIO,
@@ -1666,8 +1532,8 @@ static void bes2600_gpio_allow_mcu_sleep(struct sbus_priv *self, int flag)
 	/* do wakeup mcu operation */
 	if(gpio_sleep) {
 		bes2600_dbg(BES2600_DBG_SDIO, "pull low gpio by flag:%d\n", flag);
-		if (gpio_is_valid(pdata->wakeup)) {
-			gpio_set_value(pdata->wakeup, 0);
+		if (pdata->wakeup) {
+			gpiod_set_value(pdata->wakeup, 0);
 		} else {
 			bes2600_err(BES2600_DBG_SDIO,
 				"%s, wakeup gpio is invalid\n", __func__);
@@ -2010,28 +1876,16 @@ static void bes2600_sdio_power_down(struct sbus_priv *self)
 	sdio_writeb(self->func, tmp_val, BES_HOST_INT_REG_ID, &ret);
 	sdio_release_host(self->func);
 #else
-#if defined(PLAT_ROCKCHIP)
-	rockchip_wifi_power(0);
-#endif
 
-#if defined(PLAT_ALLWINNER)
-	sunxi_wlan_set_power(false);
-#endif
-
-#ifdef PLAT_CVITEK_182X
 	struct bes2600_platform_data_sdio *pdata = bes2600_get_platform_data();
 
-	if (gpio_is_valid(pdata->powerup)) {
-		gpio_direction_output(pdata->powerup, 0);
-	}
-#endif
+	gpiod_direction_output(pdata->powerup, 0);
 #endif
 
 	msleep(10);
 
 	self->func->card->host->caps &= ~MMC_CAP_NONREMOVABLE;
 	schedule_work(&self->sdio_scan_work);
-
 }
 
 static int bes2600_sdio_power_switch(struct sbus_priv *self, int on)
-- 
2.49.0

