// SPDX-License-Identifier: GPL-2.0-or-later OR MIT

#include "ar9331.dtsi"

#include <dt-bindings/gpio/gpio.h>
#include <dt-bindings/input/input.h>
#include <dt-bindings/leds/common.h>

/ {
	model = "Teltonika RUT230 v1";
	compatible = "teltonika,rut230-v1", "qca,ar9331";

	aliases {
		label-mac-device = &eth1;
		led-boot = &led_ss0;
		led-failsafe = &led_ss0;
		led-upgrade = &led_ss0;
	};

	keys {
		compatible = "gpio-keys";

		reset {
			label = "reset";
			linux,code = <KEY_RESTART>;
			gpios = <&gpio 22 GPIO_ACTIVE_HIGH>;
			debounce-interval = <60>;
		};

		input {
			label = "input";
			linux,code = <BTN_0>;
			gpios = <&gpio 16 GPIO_ACTIVE_LOW>;
			debounce-interval = <60>;
		};

		sim-tray {
			label = "sim-tray";
			linux,input-type = <EV_SW>;
			linux,code = <BTN_1>;
			gpios = <&gpio 20 GPIO_ACTIVE_LOW>;
			debounce-interval = <60>;
		};
	};

	leds {
		compatible = "gpio-leds";
		pinctrl-names = "default";
		pinctrl-0 = <&switch_led_disable_pins>;

		led_ss0: signal-strength-0 {
			label = "green:signal-strength-0";
			gpios = <&gpio 23 GPIO_ACTIVE_HIGH>;
		};

		signal-strength-1 {
			label = "green:signal-strength-1";
			gpios = <&gpio 7 GPIO_ACTIVE_HIGH>;
		};

		signal-strength-2 {
			label = "green:signal-strength-2";
			gpios = <&gpio 6 GPIO_ACTIVE_HIGH>;
		};

		signal-strength-3 {
			label = "green:signal-strength-3";
			gpios = <&gpio 26 GPIO_ACTIVE_HIGH>;
		};

		signal-strength-4 {
			label = "green:signal-strength-4";
			gpios = <&gpio 27 GPIO_ACTIVE_HIGH>;
		};

		2g {
			label = "green:2g";
			gpios = <&gpio 8 GPIO_ACTIVE_HIGH>;
		};

		3g {
			label = "green:3g";
			gpios = <&gpio 24 GPIO_ACTIVE_HIGH>;
		};

		lan {
			function = LED_FUNCTION_LAN;
			color = <LED_COLOR_ID_GREEN>;
			gpios = <&gpio 14 GPIO_ACTIVE_LOW>;
			/* GPIO 13 - ACTIVE HIGH for hwrev 0 */
		};

		wan {
			function = LED_FUNCTION_WAN;
			color = <LED_COLOR_ID_GREEN>;
			gpios = <&gpio 17 GPIO_ACTIVE_LOW>;
			/* GPIO 14 - ACTIVE HIGH for hwrev 0 */
		};

		/* 4G LED - GPIO21 ACTIVE_HIGH for RUT240 */
	};

	reg_usb_vbus: reg_usb_vbus {
		compatible = "regulator-fixed";
		regulator-name = "usb_vbus";
		regulator-min-microvolt = <5000000>;
		regulator-max-microvolt = <5000000>;
		gpios = <&gpio 19 GPIO_ACTIVE_HIGH>;
		enable-active-high;
	};
};

&eth0 {
	status = "okay";

	nvmem-cells = <&macaddr_config_0 1>;
	nvmem-cell-names = "mac-address";
};

&eth1 {
	status = "okay";

	nvmem-cells = <&macaddr_config_0 0>;
	nvmem-cell-names = "mac-address";
};

&spi {
	status = "okay";

	flash@0 {
		compatible = "jedec,spi-nor";
		reg = <0>;
		spi-max-frequency = <30000000>;

		partitions {
			compatible = "fixed-partitions";
			#address-cells = <1>;
			#size-cells = <1>;

			partition@0 {
				label = "u-boot";
				reg = <0x0 0x20000>;
				read-only;
			};

			config: partition@20000 {
				label = "config";
				reg = <0x20000 0x10000>;
				read-only;

				nvmem-layout {
					compatible = "fixed-layout";
					#address-cells = <1>;
					#size-cells = <1>;

					macaddr_config_0: macaddr@0 {
						compatible = "mac-base";
						reg = <0x0 0x6>;
						#nvmem-cell-cells = <1>;
					};
				};
			};

			partition@30000 {
				label = "art";
				reg = <0x30000 0x10000>;
				read-only;

				nvmem-layout {
					compatible = "fixed-layout";
					#address-cells = <1>;
					#size-cells = <1>;

					cal_art_1000: calibration@1000 {
						reg = <0x1000 0x440>;
					};
				};
			};

			partition@40000 {
				compatible = "tplink,firmware";
				label = "firmware";
				reg = <0x40000 0xf30000>;
			};

			partition@f70000 {
				label = "event-log";
				reg = <0xf70000 0x90000>;
				read-only;
			};
		};
	};
};

&usb {
	dr_mode = "host";
	vbus-supply = <&reg_usb_vbus>;
	status = "okay";
};

&gpio {
	modem-power {
		gpio-hog;
		output-low;
		gpios = <18 GPIO_ACTIVE_HIGH>;
		line-name = "modem-power";
	};
};

&usb_phy {
	status = "okay";
};

&wmac {
	status = "okay";

	nvmem-cells = <&macaddr_config_0 2>, <&cal_art_1000>;
	nvmem-cell-names = "mac-address", "calibration";
};
