board: lsxl: convert to DM_GPIO

Use the new mvebu GPIO driver and convert all the function calls to the
former kirkwood GPIO driver. This means that we are now using the LED
uclass and the regulator uclass. Unfortunately, the GPIO LED doesn't
offer a blinking method. Thus we are now stuck with solid on and off
states, which makes debugging a bit harder. Also, there is no GPIO fan
driver for now.

Signed-off-by: Michael Walle <michael@walle.cc>
Reviewed-by: Stefan Roese <sr@denx.de>
diff --git a/arch/arm/dts/kirkwood-lschlv2-u-boot.dtsi b/arch/arm/dts/kirkwood-lschlv2-u-boot.dtsi
new file mode 100644
index 0000000..208b02c
--- /dev/null
+++ b/arch/arm/dts/kirkwood-lschlv2-u-boot.dtsi
@@ -0,0 +1,9 @@
+// SPDX-License-Identifier: GPL-2.0+
+
+&hdd_power {
+	/delete-property/ regulator-always-on;
+};
+
+&usb_power {
+	/delete-property/ regulator-always-on;
+};
diff --git a/arch/arm/dts/kirkwood-lsxhl-u-boot.dtsi b/arch/arm/dts/kirkwood-lsxhl-u-boot.dtsi
new file mode 100644
index 0000000..208b02c
--- /dev/null
+++ b/arch/arm/dts/kirkwood-lsxhl-u-boot.dtsi
@@ -0,0 +1,9 @@
+// SPDX-License-Identifier: GPL-2.0+
+
+&hdd_power {
+	/delete-property/ regulator-always-on;
+};
+
+&usb_power {
+	/delete-property/ regulator-always-on;
+};
diff --git a/board/buffalo/lsxl/lsxl.c b/board/buffalo/lsxl/lsxl.c
index 7fab5fb..6a866b5 100644
--- a/board/buffalo/lsxl/lsxl.c
+++ b/board/buffalo/lsxl/lsxl.c
@@ -9,16 +9,18 @@
 
 #include <common.h>
 #include <bootstage.h>
+#include <button.h>
 #include <command.h>
 #include <env.h>
 #include <init.h>
+#include <led.h>
+#include <power/regulator.h>
 #include <spi.h>
 #include <spi_flash.h>
-#include <asm/arch/soc.h>
 #include <asm/arch/cpu.h>
 #include <asm/arch/mpp.h>
-#include <asm/arch/gpio.h>
 #include <asm/global_data.h>
+#include <asm/io.h>
 #include <linux/delay.h>
 
 #include "lsxl.h"
@@ -118,48 +120,43 @@
 	return 0;
 }
 
-#define LED_OFF             0
-#define LED_ALARM_ON        1
-#define LED_ALARM_BLINKING  2
-#define LED_POWER_ON        3
-#define LED_POWER_BLINKING  4
-#define LED_INFO_ON         5
-#define LED_INFO_BLINKING   6
+enum {
+	LSXL_LED_OFF,
+	LSXL_LED_ALARM,
+	LSXL_LED_POWER,
+	LSXL_LED_INFO,
+};
 
-static void __set_led(int blink_alarm, int blink_info, int blink_power,
-		int value_alarm, int value_info, int value_power)
+static void __set_led(int alarm, int info, int power)
 {
-	kw_gpio_set_blink(GPIO_ALARM_LED, blink_alarm);
-	kw_gpio_set_blink(GPIO_INFO_LED, blink_info);
-	kw_gpio_set_blink(GPIO_POWER_LED, blink_power);
-	kw_gpio_set_value(GPIO_ALARM_LED, value_alarm);
-	kw_gpio_set_value(GPIO_INFO_LED, value_info);
-	kw_gpio_set_value(GPIO_POWER_LED, value_power);
+	struct udevice *led;
+	int ret;
+
+	ret = led_get_by_label("lsxl:red:alarm", &led);
+	if (!ret)
+		led_set_state(led, alarm);
+	ret = led_get_by_label("lsxl:amber:info", &led);
+	if (!ret)
+		led_set_state(led, info);
+	ret = led_get_by_label("lsxl:blue:power", &led);
+	if (!ret)
+		led_set_state(led, power);
 }
 
 static void set_led(int state)
 {
 	switch (state) {
-	case LED_OFF:
-		__set_led(0, 0, 0, 1, 1, 1);
+	case LSXL_LED_OFF:
+		__set_led(0, 0, 0);
 		break;
-	case LED_ALARM_ON:
-		__set_led(0, 0, 0, 0, 1, 1);
+	case LSXL_LED_ALARM:
+		__set_led(1, 0, 0);
 		break;
-	case LED_ALARM_BLINKING:
-		__set_led(1, 0, 0, 1, 1, 1);
+	case LSXL_LED_INFO:
+		__set_led(0, 1, 0);
 		break;
-	case LED_INFO_ON:
-		__set_led(0, 0, 0, 1, 0, 1);
-		break;
-	case LED_INFO_BLINKING:
-		__set_led(0, 1, 0, 1, 1, 1);
-		break;
-	case LED_POWER_ON:
-		__set_led(0, 0, 0, 1, 1, 0);
-		break;
-	case LED_POWER_BLINKING:
-		__set_led(0, 0, 1, 1, 1, 1);
+	case LSXL_LED_POWER:
+		__set_led(0, 0, 1);
 		break;
 	}
 }
@@ -169,32 +166,56 @@
 	/* address of boot parameters */
 	gd->bd->bi_boot_params = mvebu_sdram_bar(0) + 0x100;
 
-	set_led(LED_POWER_BLINKING);
+	set_led(LSXL_LED_POWER);
 
 	return 0;
 }
 
 static void check_power_switch(void)
 {
-	if (kw_gpio_get_value(GPIO_POWER_SWITCH)) {
-		/* turn off fan, HDD and USB power */
-		kw_gpio_set_value(GPIO_HDD_POWER, 0);
-		kw_gpio_set_value(GPIO_USB_VBUS, 0);
-		kw_gpio_set_value(GPIO_FAN_HIGH, 1);
-		kw_gpio_set_value(GPIO_FAN_LOW, 1);
-		set_led(LED_OFF);
+	struct udevice *power_button, *hdd_power, *usb_power;
+	int ret;
+
+	ret = button_get_by_label("Power-on Switch", &power_button);
+	if (ret)
+		goto err;
+
+	ret = regulator_get_by_platname("HDD Power", &hdd_power);
+	if (ret)
+		goto err;
+
+	ret = regulator_get_by_platname("USB Power", &usb_power);
+	if (ret)
+		goto err;
+
+	if (button_get_state(power_button) == BUTTON_OFF) {
+		ret = regulator_set_enable(hdd_power, false);
+		if (ret)
+			goto err;
+		ret = regulator_set_enable(usb_power, false);
+		if (ret)
+			goto err;
+		/* TODO: fan off */
+		set_led(LSXL_LED_OFF);
 
 		/* loop until released */
-		while (kw_gpio_get_value(GPIO_POWER_SWITCH))
+		while (button_get_state(power_button) == BUTTON_OFF)
 			;
 
 		/* turn power on again */
-		kw_gpio_set_value(GPIO_HDD_POWER, 1);
-		kw_gpio_set_value(GPIO_USB_VBUS, 1);
-		kw_gpio_set_value(GPIO_FAN_HIGH, 0);
-		kw_gpio_set_value(GPIO_FAN_LOW, 0);
-		set_led(LED_POWER_BLINKING);
-	}
+		ret = regulator_set_enable(hdd_power, true);
+		if (ret)
+			goto err;
+		ret = regulator_set_enable(usb_power, true);
+		if (ret)
+			goto err;
+		/* TODO: fan on */
+		set_led(LSXL_LED_POWER);
+	};
+
+	return;
+err:
+	printf("error in %s\n", __func__);
 }
 
 void check_enetaddr(void)
@@ -203,7 +224,7 @@
 
 	if (!eth_env_get_enetaddr("ethaddr", enetaddr)) {
 		/* signal unset/invalid ethaddr to user */
-		set_led(LED_INFO_BLINKING);
+		set_led(LSXL_LED_INFO);
 	}
 }
 
@@ -231,17 +252,24 @@
 
 static void check_push_button(void)
 {
+	struct udevice *func_button;
 	int i = 0;
 
-	while (!kw_gpio_get_value(GPIO_FUNC_BUTTON)) {
+	int ret;
+
+	ret = button_get_by_label("Function Button", &func_button);
+	if (ret)
+		goto err;
+
+	while (button_get_state(func_button) == BUTTON_ON) {
 		udelay(100000);
 		i++;
 
 		if (i == 10)
-			set_led(LED_INFO_ON);
+			set_led(LSXL_LED_INFO);
 
 		if (i >= 100) {
-			set_led(LED_INFO_BLINKING);
+			set_led(LSXL_LED_ALARM);
 			break;
 		}
 	}
@@ -250,6 +278,10 @@
 		erase_environment();
 	else if (i >= 10)
 		force_rescue_mode = true;
+
+	return;
+err:
+	printf("error in %s\n", __func__);
 }
 
 int board_early_init_r(void)
@@ -279,6 +311,6 @@
 	if (progress == -BOOTSTAGE_ID_NET_LOADED)
 		return;
 
-	set_led(LED_ALARM_BLINKING);
+	set_led(LSXL_LED_ALARM);
 }
 #endif
diff --git a/configs/lschlv2_defconfig b/configs/lschlv2_defconfig
index b3ad20c..2146a33 100644
--- a/configs/lschlv2_defconfig
+++ b/configs/lschlv2_defconfig
@@ -14,6 +14,7 @@
 CONFIG_ENV_SIZE=0x10000
 CONFIG_ENV_OFFSET=0x70000
 CONFIG_ENV_SECT_SIZE=0x10000
+CONFIG_DM_GPIO=y
 CONFIG_DEFAULT_DEVICE_TREE="kirkwood-lschlv2"
 CONFIG_IDENT_STRING=" LS-CHLv2"
 CONFIG_SYS_LOAD_ADDR=0x800000
@@ -37,6 +38,9 @@
 CONFIG_CMD_SPI=y
 CONFIG_CMD_USB=y
 # CONFIG_CMD_SETEXPR is not set
+# CONFIG_CMD_BUTTON is not set
+# CONFIG_CMD_LED is not set
+CONFIG_CMD_REGULATOR=y
 CONFIG_OF_CONTROL=y
 CONFIG_ENV_IS_IN_SPI_FLASH=y
 CONFIG_SYS_RELOC_GD_ENV_ADDR=y
@@ -48,7 +52,10 @@
 CONFIG_SYS_SATA_MAX_DEVICE=1
 CONFIG_LBA48=y
 CONFIG_SYS_64BIT_LBA=y
-CONFIG_KIRKWOOD_GPIO=y
+CONFIG_BUTTON=y
+CONFIG_BUTTON_GPIO=y
+CONFIG_LED=y
+CONFIG_LED_GPIO=y
 # CONFIG_MMC is not set
 CONFIG_MTD=y
 CONFIG_DM_SPI_FLASH=y
@@ -56,6 +63,8 @@
 CONFIG_SPI_FLASH_STMICRO=y
 CONFIG_MVGBE=y
 CONFIG_MII=y
+CONFIG_DM_REGULATOR=y
+CONFIG_DM_REGULATOR_FIXED=y
 CONFIG_SYS_NS16550=y
 CONFIG_SPI=y
 CONFIG_DM_SPI=y
diff --git a/configs/lsxhl_defconfig b/configs/lsxhl_defconfig
index 747a224..d895312 100644
--- a/configs/lsxhl_defconfig
+++ b/configs/lsxhl_defconfig
@@ -15,6 +15,7 @@
 CONFIG_ENV_SIZE=0x10000
 CONFIG_ENV_OFFSET=0x70000
 CONFIG_ENV_SECT_SIZE=0x10000
+CONFIG_DM_GPIO=y
 CONFIG_DEFAULT_DEVICE_TREE="kirkwood-lsxhl"
 CONFIG_IDENT_STRING=" LS-XHL"
 CONFIG_SYS_LOAD_ADDR=0x800000
@@ -38,6 +39,9 @@
 CONFIG_CMD_SPI=y
 CONFIG_CMD_USB=y
 # CONFIG_CMD_SETEXPR is not set
+# CONFIG_CMD_BUTTON is not set
+# CONFIG_CMD_LED is not set
+CONFIG_CMD_REGULATOR=y
 CONFIG_OF_CONTROL=y
 CONFIG_ENV_IS_IN_SPI_FLASH=y
 CONFIG_SYS_RELOC_GD_ENV_ADDR=y
@@ -49,7 +53,10 @@
 CONFIG_SYS_SATA_MAX_DEVICE=1
 CONFIG_LBA48=y
 CONFIG_SYS_64BIT_LBA=y
-CONFIG_KIRKWOOD_GPIO=y
+CONFIG_BUTTON=y
+CONFIG_BUTTON_GPIO=y
+CONFIG_LED=y
+CONFIG_LED_GPIO=y
 # CONFIG_MMC is not set
 CONFIG_MTD=y
 CONFIG_DM_SPI_FLASH=y
@@ -57,6 +64,8 @@
 CONFIG_SPI_FLASH_STMICRO=y
 CONFIG_MVGBE=y
 CONFIG_MII=y
+CONFIG_DM_REGULATOR=y
+CONFIG_DM_REGULATOR_FIXED=y
 CONFIG_SYS_NS16550=y
 CONFIG_SPI=y
 CONFIG_DM_SPI=y