// SPDX-License-Identifier: GPL-2.0
#include <dt-bindings/input/input.h>
#include <dt-bindings/reset/qcom,gcc-msm8960.h>

#include "qcom-msm8960.dtsi"
#include "pm8921.dtsi"
#include <dt-bindings/gpio/gpio.h>
#include <dt-bindings/pinctrl/qcom,pmic-gpio.h>
#include <dt-bindings/input/gpio-keys.h>

/ {
	model = "Samsung Galaxy Express SGH-I437";
	compatible = "samsung,expressatt", "qcom,msm8960";
	chassis-type = "handset";

	aliases {
		serial0 = &gsbi5_serial;
		mmc0 = &sdcc1; /* SDCC1 eMMC slot */
		mmc1 = &sdcc3; /* SDCC3 SD card slot */
	};

	chosen {
		stdout-path = "serial0:115200n8";
	};

	gpio-keys {
		compatible = "gpio-keys";

		pinctrl-names = "default";
		pinctrl-0 = <&gpio_keys_pin_a>;

		key-home {
			label = "Home";
			gpios = <&tlmm 40 GPIO_ACTIVE_LOW>;
			debounce-interval = <5>;
			linux,code = <KEY_HOMEPAGE>;
			wakeup-event-action = <EV_ACT_ASSERTED>;
			wakeup-source;
		};

		key-volume-up {
			label = "Volume Up";
			gpios = <&tlmm 50 GPIO_ACTIVE_LOW>;
			debounce-interval = <5>;
			linux,code = <KEY_VOLUMEUP>;
		};

		key-volume-down {
			label = "Volume Down";
			gpios = <&tlmm 81 GPIO_ACTIVE_LOW>;
			debounce-interval = <5>;
			linux,code = <KEY_VOLUMEDOWN>;
		};
	};

	touchkey_enable: touchkey-enable {
		compatible = "regulator-fixed";
		regulator-name = "touchkey_enable";
		gpio = <&tlmm 51 GPIO_ACTIVE_HIGH>;
		enable-active-high;
		regulator-boot-on;
	};

	i2c-gpio-touchkey {
		compatible = "i2c-gpio";
		#address-cells = <1>;
		#size-cells = <0>;
		sda-gpios = <&tlmm 71 (GPIO_ACTIVE_HIGH | GPIO_OPEN_DRAIN)>;
		scl-gpios = <&tlmm 72 (GPIO_ACTIVE_HIGH | GPIO_OPEN_DRAIN)>;
		pinctrl-0 = <&touchkey_i2c_pins>;
		pinctrl-names = "default";
		i2c-gpio,delay-us = <2>;

		touchkey@20 {
			compatible = "coreriver,tc360-touchkey";
			reg = <0x20>;

			interrupts-extended = <&tlmm 52 IRQ_TYPE_EDGE_FALLING>;
			pinctrl-0 = <&touchkey_irq_pin>;
			pinctrl-names = "default";

			vddio-supply = <&touchkey_enable>;
			vdd-supply = <&pm8921_l29>;
			vcc-supply = <&pm8921_l29>;

			linux,keycodes = <KEY_MENU KEY_BACK>;
		};
	};
};

&gsbi2 {
	qcom,mode = <GSBI_PROT_I2C>;

	status = "okay";
};

&gsbi2_i2c {
	status = "okay";

	light-sensor@39 {
		compatible = "amstaos,tmd2772";
		reg = <0x39>;
		interrupts-extended = <&pm8921_gpio 6 IRQ_TYPE_EDGE_FALLING>;
		vdd-supply = <&pm8921_l9>;
		vddio-supply = <&pm8921_lvs4>;

		/* TODO: Proximity doesn't work */
		amstaos,proximity-diodes = <0>;
		led-max-microamp = <100000>;

		pinctrl-0 = <&prox_sensor_int>;
		pinctrl-names = "default";
	};
};

&gsbi5 {
	qcom,mode = <GSBI_PROT_I2C_UART>;
	status = "okay";
};

&gsbi5_serial {
	status = "okay";
};

&sdcc1 {
	vmmc-supply = <&pm8921_l5>;
	status = "okay";
};

&sdcc3 {
	vmmc-supply = <&pm8921_l6>;
	vqmmc-supply = <&pm8921_l7>;

	pinctrl-0 = <&sdcc3_default_state>;
	pinctrl-1 = <&sdcc3_sleep_state>;
	pinctrl-names = "default", "sleep";

	status = "okay";
};

&gsbi1 {
	qcom,mode = <GSBI_PROT_SPI>;
	pinctrl-0 = <&spi1_default>;
	pinctrl-names = "default";
	status = "okay";
};

&gsbi1_spi {
	status = "okay";
};

&gsbi3 {
	qcom,mode = <GSBI_PROT_I2C>;
	status = "okay";
};

&gsbi3_i2c {
	status = "okay";

	// Atmel mXT224S touchscreen
	touchscreen@4a {
		compatible = "atmel,maxtouch";
		reg = <0x4a>;
		interrupt-parent = <&tlmm>;
		interrupts = <11 IRQ_TYPE_EDGE_FALLING>;
		vdda-supply = <&pm8921_lvs6>;
		vdd-supply = <&pm8921_l17>;
		pinctrl-names = "default";
		pinctrl-0 = <&touchscreen>;
	};
};

&tlmm {
	spi1_default: spi1-default-state {
		mosi-pins {
			pins = "gpio6";
			function = "gsbi1";
			drive-strength = <12>;
			bias-disable;
		};

		miso-pins {
			pins = "gpio7";
			function = "gsbi1";
			drive-strength = <12>;
			bias-disable;
		};

		cs-pins {
			pins = "gpio8";
			function = "gsbi1";
			drive-strength = <12>;
			bias-disable;
			output-low;
		};

		clk-pins {
			pins = "gpio9";
			function = "gsbi1";
			drive-strength = <12>;
			bias-disable;
		};
	};

	gpio_keys_pin_a: gpio-keys-active-state {
		pins = "gpio40", "gpio50", "gpio81";
		function = "gpio";
		drive-strength = <8>;
		bias-disable;
	};

	touchscreen: touchscreen-int-state {
		pins = "gpio11";
		function = "gpio";
		output-enable;
		bias-disable;
		drive-strength = <2>;
	};

	nfc_default: nfc-default-state {
		irq-pins {
			pins = "gpio106";
			function = "gpio";
			drive-strength = <2>;
			bias-pull-down;
		};

		firmware-pins {
			pins = "gpio92";
			function = "gpio";
			drive-strength = <2>;
			bias-disable;
		};
	};

	touchkey_i2c_pins: touchkey-i2c-state {
		pins = "gpio71", "gpio72";
		function = "gpio";
		drive-strength = <2>;
		bias-disable;
	};

	touchkey_irq_pin: touchkey-irq-state {
		pins = "gpio52";
		function = "gpio";
		drive-strength = <2>;
		bias-disable;
	};
};

&pm8921 {
	interrupts-extended = <&tlmm 104 IRQ_TYPE_LEVEL_LOW>;
};

&pm8921_gpio {
	prox_sensor_int: prox-sensor-int-state {
		pins = "gpio6";
		function = "normal";
		input-enable;
		bias-disable;
	};

	nfc_enable: nfc-enable-state {
		pins = "gpio21";
		function = "normal";
		bias-disable;
		qcom,drive-strength = <PMIC_GPIO_STRENGTH_LOW>;
		power-source = <PM8921_GPIO_S4>;
	};
};

&rpm {
	regulators {
		compatible = "qcom,rpm-pm8921-regulators";
		vin_lvs1_3_6-supply = <&pm8921_s4>;
		vin_lvs2-supply = <&pm8921_s4>;
		vin_lvs4_5_7-supply = <&pm8921_s4>;
		vdd_ncp-supply = <&pm8921_l6>;
		vdd_l1_l2_l12_l18-supply = <&pm8921_s4>;
		vdd_l21_l23_l29-supply = <&pm8921_s8>;
		vdd_l24-supply = <&pm8921_s1>;
		vdd_l25-supply = <&pm8921_s1>;
		vdd_l27-supply = <&pm8921_s7>;
		vdd_l28-supply = <&pm8921_s7>;

		/* Buck SMPS */
		pm8921_s1: s1 {
			regulator-always-on;
			regulator-min-microvolt = <1225000>;
			regulator-max-microvolt = <1225000>;
			qcom,switch-mode-frequency = <3200000>;
			bias-pull-down;
		};

		pm8921_s2: s2 {
			regulator-min-microvolt = <1300000>;
			regulator-max-microvolt = <1300000>;
			qcom,switch-mode-frequency = <1600000>;
			bias-pull-down;
		};

		pm8921_s3: s3 {
			regulator-min-microvolt = <500000>;
			regulator-max-microvolt = <1150000>;
			qcom,switch-mode-frequency = <4800000>;
			bias-pull-down;
		};

		pm8921_s4: s4 {
			regulator-always-on;
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <1800000>;
			qcom,switch-mode-frequency = <1600000>;
			bias-pull-down;
			qcom,force-mode = <QCOM_RPM_FORCE_MODE_AUTO>;
		};

		pm8921_s7: s7 {
			regulator-min-microvolt = <1150000>;
			regulator-max-microvolt = <1150000>;
			qcom,switch-mode-frequency = <3200000>;
			bias-pull-down;
		};

		pm8921_s8: s8 {
			regulator-always-on;
			regulator-min-microvolt = <2050000>;
			regulator-max-microvolt = <2050000>;
			qcom,switch-mode-frequency = <1600000>;
			bias-pull-down;
		};

		/* PMOS LDO */
		pm8921_l1: l1 {
			regulator-always-on;
			regulator-min-microvolt = <1050000>;
			regulator-max-microvolt = <1050000>;
			bias-pull-down;
		};

		pm8921_l2: l2 {
			regulator-min-microvolt = <1200000>;
			regulator-max-microvolt = <1200000>;
			bias-pull-down;
		};

		pm8921_l3: l3 {
			regulator-min-microvolt = <3075000>;
			regulator-max-microvolt = <3300000>;
			bias-pull-down;
		};

		pm8921_l4: l4 {
			regulator-always-on;
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <1800000>;
			bias-pull-down;
		};

		pm8921_l5: l5 {
			regulator-min-microvolt = <2950000>;
			regulator-max-microvolt = <2950000>;
			bias-pull-down;
		};

		pm8921_l6: l6 {
			regulator-min-microvolt = <2950000>;
			regulator-max-microvolt = <2950000>;
			bias-pull-down;
		};

		pm8921_l7: l7 {
			regulator-always-on;
			regulator-min-microvolt = <1850000>;
			regulator-max-microvolt = <2950000>;
			bias-pull-down;
		};

		pm8921_l8: l8 {
			regulator-min-microvolt = <3000000>;
			regulator-max-microvolt = <3100000>;
			bias-pull-down;
		};

		pm8921_l9: l9 {
			regulator-min-microvolt = <2850000>;
			regulator-max-microvolt = <2850000>;
			bias-pull-down;
		};

		pm8921_l10: l10 {
			regulator-min-microvolt = <3000000>;
			regulator-max-microvolt = <3000000>;
			bias-pull-down;
		};

		pm8921_l11: l11 {
			regulator-min-microvolt = <2800000>;
			regulator-max-microvolt = <3300000>;
			bias-pull-down;
		};

		pm8921_l12: l12 {
			regulator-min-microvolt = <1200000>;
			regulator-max-microvolt = <1200000>;
			bias-pull-down;
		};

		pm8921_l14: l14 {
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <1800000>;
			bias-pull-down;
		};

		pm8921_l15: l15 {
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <2950000>;
			bias-pull-down;
		};

		pm8921_l16: l16 {
			regulator-min-microvolt = <2800000>;
			regulator-max-microvolt = <3000000>;
			bias-pull-down;
		};

		pm8921_l17: l17 {
			regulator-min-microvolt = <3300000>;
			regulator-max-microvolt = <3300000>;
			bias-pull-down;
		};

		pm8921_l18: l18 {
			regulator-min-microvolt = <1200000>;
			regulator-max-microvolt = <1500000>;
			bias-pull-down;
		};

		pm8921_l21: l21 {
			regulator-min-microvolt = <1900000>;
			regulator-max-microvolt = <1900000>;
			bias-pull-down;
		};

		pm8921_l22: l22 {
			regulator-min-microvolt = <2750000>;
			regulator-max-microvolt = <2750000>;
			bias-pull-down;
		};

		pm8921_l23: l23 {
			regulator-always-on;
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <1800000>;
			bias-pull-down;
		};

		pm8921_l24: l24 {
			regulator-min-microvolt = <750000>;
			regulator-max-microvolt = <1150000>;
			bias-pull-down;
		};

		pm8921_l25: l25 {
			regulator-always-on;
			regulator-min-microvolt = <1225000>;
			regulator-max-microvolt = <1225000>;
			bias-pull-down;
		};

		pm8921_l29: l29 {
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <3300000>;
			bias-pull-down;
		};

		/* Low Voltage Switch */
		pm8921_lvs1: lvs1 {
			bias-pull-down;
		};

		pm8921_lvs2: lvs2 {
			bias-pull-down;
		};

		pm8921_lvs3: lvs3 {
			bias-pull-down;
		};

		pm8921_lvs4: lvs4 {
			bias-pull-down;
		};

		pm8921_lvs5: lvs5 {
			bias-pull-down;
		};

		pm8921_lvs6: lvs6 {
			bias-pull-down;
		};

		pm8921_lvs7: lvs7 {
			bias-pull-down;
		};

		pm8921_ncp: ncp {
			regulator-min-microvolt = <1800000>;
			regulator-max-microvolt = <1800000>;
			qcom,switch-mode-frequency = <1600000>;
		};
	};
};

&usb_hs1_phy {
	v3p3-supply = <&pm8921_l3>;
	v1p8-supply = <&pm8921_l4>;
};

&usb1 {
	dr_mode = "otg";
	status = "okay";
};

&gsbi7 {
	qcom,mode = <GSBI_PROT_I2C>;

	status = "okay";
};

&gsbi7_i2c {
	status = "okay";

	nfc@2b {
		compatible = "nxp,pn544-i2c";
		reg = <0x2b>;
		interrupts-extended = <&tlmm 106 IRQ_TYPE_EDGE_RISING>;
		enable-gpios = <&pm8921_gpio 21 GPIO_ACTIVE_HIGH>;
		firmware-gpios = <&tlmm 92 GPIO_ACTIVE_HIGH>;

		pinctrl-0 = <&nfc_default &nfc_enable>;
		pinctrl-names = "default";
	};
};

&gsbi12 {
	qcom,mode = <GSBI_PROT_I2C>;

	status = "okay";
};

&gsbi12_i2c {
	status = "okay";

	accelerometer@18 {
		compatible = "bosch,bma254";
		reg = <0x18>;
		vdd-supply = <&pm8921_l9>;
		vddio-supply = <&pm8921_lvs4>;

		mount-matrix =  "-1", "0", "0",
				"0",  "-1", "0",
				"0",  "0", "1";
	};

	magnetometer@2e {
		compatible = "yamaha,yas532";
		reg = <0x2e>;
		vdd-supply = <&pm8921_l9>;
		iovdd-supply = <&pm8921_lvs4>;

		/* TODO: Figure out Mount Matrix */
	};
};