/* Copyright (c) 2017-2018, The Linux Foundation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <dt-bindings/interrupt-controller/irq.h>
#include "mdm9650-v1.1.dtsi"
#include "mdm9650-pinctrl.dtsi"

/ {
	aliases {
		serial0 = &blsp1_uart3;
		serial1 = &blsp1_uart1;
	};
};

&soc {
	sound {
		status = "disabled";
	};

	sound-auto {
		compatible = "qcom,mdm-audio-auto";
		qcom,model = "mdm-auto-i2s-snd-card";

		qcom,msm-gpios =
			"pri_mi2s_aux_master",
			"pri_mi2s_aux_slave",
			"sec_mi2s_aux_master",
			"sec_mi2s_aux_slave";
		qcom,pinctrl-names =
			"all_off",
			"pri_mi2s_aux_master_active",
			"pri_mi2s_aux_slave_active",
			"invalid_state_1",
			"sec_mi2s_aux_master_active",
			"pri_master_active_sec_master_active",
			"pri_slave_active_sec_master_active",
			"invalid_state_2",
			"sec_mi2s_aux_slave_active",
			"pri_master_active_sec_slave_active",
			"pri_slave_active_sec_slave_active";
		pinctrl-names =
			"all_off",
			"pri_mi2s_aux_master_active",
			"pri_mi2s_aux_slave_active",
			"invalid_state_1",
			"sec_mi2s_aux_master_active",
			"pri_master_active_sec_master_active",
			"pri_slave_active_sec_master_active",
			"invalid_state_2",
			"sec_mi2s_aux_slave_active",
			"pri_master_active_sec_slave_active",
			"pri_slave_active_sec_slave_active";
		pinctrl-0 = <&pri_ws_sleep &pri_sck_sleep
				&pri_dout_sleep &pri_din_sleep
				&sec_ws_sleep &sec_sck_sleep
				&sec_dout_sleep &sec_din_sleep>;
		pinctrl-1 = <&pri_ws_active_master &pri_sck_active_master
				&pri_dout_active &pri_din_active
				&sec_ws_sleep &sec_sck_sleep
				&sec_dout_sleep &sec_din_sleep>;
		pinctrl-2 = <&pri_ws_active_slave &pri_sck_active_slave
				&pri_dout_active &pri_din_active
				&sec_ws_sleep &sec_sck_sleep
				&sec_dout_sleep &sec_din_sleep>;
		pinctrl-3 = <&pri_ws_sleep &pri_sck_sleep
				&pri_dout_sleep &pri_din_sleep
				&sec_ws_sleep &sec_sck_sleep
				&sec_dout_sleep &sec_din_sleep>;
		pinctrl-4 = <&pri_ws_sleep &pri_sck_sleep
				&pri_dout_sleep &pri_din_sleep
				&sec_ws_active_master &sec_sck_active_master
				&sec_dout_active &sec_din_active>;
		pinctrl-5 = <&pri_ws_active_master &pri_sck_active_master
				&pri_dout_active &pri_din_active
				&sec_ws_active_master &sec_sck_active_master
				&sec_dout_active &sec_din_active>;
		pinctrl-6 = <&pri_ws_active_slave &pri_sck_active_slave
				&pri_dout_active &pri_din_active
				&sec_ws_active_master &sec_sck_active_master
				&sec_dout_active &sec_din_active>;
		pinctrl-7 = <&pri_ws_sleep &pri_sck_sleep
				&pri_dout_sleep &pri_din_sleep
				&sec_ws_sleep &sec_sck_sleep
				&sec_dout_sleep &sec_din_sleep>;
		pinctrl-8 = <&pri_ws_sleep &pri_sck_sleep
				&pri_dout_sleep &pri_din_sleep
				&sec_ws_active_slave &sec_sck_active_slave
				&sec_dout_active &sec_din_active>;
		pinctrl-9 = <&pri_ws_active_master &pri_sck_active_master
				&pri_dout_active &pri_din_active
				&sec_ws_active_slave &sec_sck_active_slave
				&sec_dout_active &sec_din_active>;
		pinctrl-10 = <&pri_ws_active_slave &pri_sck_active_slave
				&pri_dout_active &pri_din_active
				&sec_ws_active_slave &sec_sck_active_slave
				&sec_dout_active &sec_din_active>;

		asoc-platform = <&pcm0>, <&pcm1>, <&voip>, <&voice>,
				<&loopback>, <&hostless>, <&afe>, <&routing>,
				<&pcm_dtmf>, <&host_pcm>, <&compress>;
		asoc-platform-names = "msm-pcm-dsp.0", "msm-pcm-dsp.1",
				"msm-voip-dsp", "msm-pcm-voice",
				"msm-pcm-loopback", "msm-pcm-hostless",
				"msm-pcm-afe", "msm-pcm-routing",
				"msm-pcm-dtmf", "msm-voice-host-pcm",
				"msm-compress-dsp";
		asoc-cpu = <&dai_pri_auxpcm>, <&mi2s_prim>, <&mi2s_sec>,
				<&dtmf_tx>,
				<&rx_capture_tx>, <&rx_playback_rx>,
				<&tx_capture_tx>, <&tx_playback_rx>,
				<&afe_pcm_rx>, <&afe_pcm_tx>, <&afe_proxy_rx>,
				<&afe_proxy_tx>, <&incall_record_rx>,
				<&incall_record_tx>, <&incall_music_rx>,
				<&dai_sec_auxpcm>;
		asoc-cpu-names = "msm-dai-q6-auxpcm.1",
				"msm-dai-q6-mi2s.0", "msm-dai-q6-mi2s.1",
				"msm-dai-stub-dev.4", "msm-dai-stub-dev.5",
				"msm-dai-stub-dev.6", "msm-dai-stub-dev.7",
				"msm-dai-stub-dev.8", "msm-dai-q6-dev.224",
				"msm-dai-q6-dev.225", "msm-dai-q6-dev.241",
				"msm-dai-q6-dev.240", "msm-dai-q6-dev.32771",
				"msm-dai-q6-dev.32772", "msm-dai-q6-dev.32773",
				"msm-dai-q6-auxpcm.2";
		asoc-codec = <&tlv320aic3x_codec>, <&stub_codec>;
		asoc-codec-names = "tlv320aic3x-codec", "msm-stub-codec.1";
	};

	ethernet {
		compatible = "qcom,ntn_avb";
		qcom,ntn-rst-delay-msec = <100>;
		qcom,ntn-rc-num = <0>;
		qcom,ntn-fw-load-delay-msec = <200>;
		qcom,ntn-pcierst-resx;
		qcom,ntn-timestamp-valid-window = <4>; /* 4 is approx 67 ms */
	};

	usb_detect {
		compatible = "qcom,gpio-usbdetect";
		interrupt-parent = <&spmi_bus>;
		interrupts = <0x0 0x0d 0x0>; /* PMD9655 VBUS DETECT */
		interrupt-names = "vbus_det_irq";
	};

	bt_qca6174 {
		qca,bt-vdd-core-supply = <&wlan_ext_vreg>;
		/delete-property/ qca,bt-vdd-xtal-supply;
	};

	/* 4V regulator for peripherals */
	periph_vreg: regulator-periph-lm53635 {
		status = "disabled";
		compatible = "regulator-fixed";
		regulator-name = "periph_vreg";
		startup-delay-us = <4000>;
		gpio = <&tlmm_pinmux 9 0>;
		pinctrl-names = "default";
		pinctrl-0 = <&periph_vreg_gpio>;
		enable-active-high;
		regulator-always-on;
	};

	/* 1.8V/3.3V external regulator for wlan */
	wlan_ext_vreg: regulator-wlan-tlv62065 {
		compatible = "regulator-fixed";
		regulator-name = "wlan_ext_vreg";
		regulator-min-microvolt = <1800000>;
		regulator-max-microvolt = <1800000>;
		startup-delay-us = <500>;
		gpio = <&tlmm_pinmux 97 0>;
		enable-active-high;
	};

	/* 1.8V/3.3V regulator for audio codec */
	codec_vreg: regulator-codec-tlv7103318 {
		compatible = "regulator-fixed";
		regulator-name = "codec_vreg";
		startup-delay-us = <100>;
		gpio = <&tlmm_pinmux 38 0>;
		enable-active-high;
	};

	/* 3.0V regulator for BTLE chip */
	btle_vreg: regulator-btle-lp5907 {
		compatible = "regulator-fixed";
		regulator-name = "btle_vreg";
		startup-delay-us = <150>;
		gpio = <&tlmm_pinmux 34 0>;
		enable-active-high;
		regulator-always-on;
	};

	/* 1.8V regulator for A2B transceiver */
	a2b_vreg: regulator-a2b-lp3996 {
		compatible = "regulator-fixed";
		regulator-name = "a2b_vreg";
		startup-delay-us = <300>;
		gpio = <&tlmm_pinmux 96 0>;
		enable-active-high;
		regulator-always-on;
	};

	/* 1.8V/3.3V regulator for gyro/accel */
	gyro_vreg: regulator-gyro-tlv7103318 {
		compatible = "regulator-fixed";
		regulator-name = "gyro_vreg";
		startup-delay-us = <100>;
		gpio = <&tlmm_pinmux 99 0>;
		enable-active-high;
		regulator-always-on;
	};
};

&tlmm_pinmux {
	/* Set these up as hogs */
	pinctrl-names = "default";
	pinctrl-0 = <&ant_switch_gpio1>, <&ant_switch_gpio2>,
		<&ant_switch_gpio3>, <&eth_can_supply_gpio>,
		<&oabr_enable_gpio>;

	periph_vreg_gpio: periph_vreg_gpio {
		mux {
			pins = "gpio9";
			function = "gpio";
		};
		config {
			pins = "gpio9";
			drive-strength = <2>;
			bias-disable;
		};
	};

	/* This gpio controls ETH supply on v1 and CAN supply on v2 */
	eth_can_supply_gpio: eth_can_supply_gpio {
		mux {
			pins = "gpio69";
			function = "gpio";
		};

		config {
			pins = "gpio69";
			drive-strength = <2>;
			output-high;
			bias-pull-up;
		};
	};

	oabr_enable_gpio: oabr_enable_gpio {
		mux {
			pins = "gpio29";
			function = "gpio";
		};

		config {
			pins = "gpio29";
			drive-strength = <2>;
			output-high;
			bias-pull-up;
		};
	};

	pmx_sec_mi2s_aux {
		sec_ws_sleep: sec_ws_sleep {
			mux {
				pins = "gpio20";
				function = "gpio";
			};

			config {
				pins = "gpio20";
				drive-strength = <2>;	/* 2 mA */
				bias-pull-down;		/* PULL DOWN */
				input-enable;
			};
		};

		sec_sck_sleep: sec_sck_sleep {
			mux {
				pins = "gpio23";
				function = "gpio";
			};

			config {
				pins = "gpio23";
				drive-strength = <2>;	/* 2 mA */
				bias-pull-down;		/* PULL DOWN */
				input-enable;
			};
		};

		sec_dout_sleep: sec_dout_sleep {
			mux {
				pins = "gpio22";
				function = "gpio";
			};

			config {
				pins = "gpio22";
				drive-strength = <2>;	/* 2 mA */
				bias-pull-down;		/* PULL DOWN */
				input-enable;
			};
		};

		sec_ws_active_master: sec_ws_active_master {
			mux {
				pins = "gpio20";
				function = "sec_mi2s_ws_b";
			};

			config {
				pins = "gpio20";
				drive-strength = <8>;	/* 8 mA */
				bias-disable;		/* NO PULL*/
				output-high;
			};
		};

		sec_sck_active_master: sec_sck_active_master {
			mux {
				pins = "gpio23";
				function = "sec_mi2s_sck_b";
			};

			config {
				pins = "gpio23";
				drive-strength = <8>;	/* 8 mA */
				bias-disable;		/* NO PULL*/
				output-high;
			};
		};

		sec_ws_active_slave: sec_ws_active_slave {
			mux {
				pins = "gpio20";
				function = "sec_mi2s_ws_b";
			};

			config {
				pins = "gpio20";
				drive-strength = <8>;	/* 8 mA */
				bias-disable;		/* NO PULL*/
			};
		};

		sec_sck_active_slave: sec_sck_active_slave {
			mux {
				pins = "gpio23";
				function = "sec_mi2s_sck_b";
			};

			config {
				pins = "gpio23";
				drive-strength = <8>;	/* 8 mA */
				bias-disable;		/* NO PULL*/
			};
		};

		sec_dout_active: sec_dout_active {
			mux {
				pins = "gpio22";
				function = "sec_mi2s_data1_b";
			};

			config {
				pins = "gpio22";
				drive-strength = <8>;	/* 8 mA */
				bias-disable;		/* NO PULL*/
				output-high;
			};
		};
	};

	pmx_sec_mi2s_aux_din {
		sec_din_sleep: sec_din_sleep {
			mux {
				pins = "gpio21";
				function = "gpio";
			};

			config {
				pins = "gpio21";
				drive-strength = <2>;	/* 2 mA */
				bias-pull-down;		/* PULL DOWN */
				input-enable;
			};
		};

		sec_din_active: sec_din_active {
			mux {
				pins = "gpio21";
				function = "sec_mi2s_data0_b";
			};

			config {
				pins = "gpio21";
				drive-strength = <8>;	/* 8 mA */
				bias-disable;		/* NO PULL */
			};
		};
	};

	/* Pins for the antenna switch matrix */
	pmx_antenna_switch_matrix {
		ant_switch_gpio1: ant_switch_gpio1 {
			mux {
				pins = "gpio31";
				function = "gpio";
			};

			config {
				pins = "gpio31";
				drive-strength = <2>;
				output-low;
				bias-disable;
			};
		};

		ant_switch_gpio2: ant_switch_gpio2 {
			mux {
				pins = "gpio32";
				function = "gpio";
			};

			config {
				pins = "gpio32";
				drive-strength = <2>;
				output-high;
				bias-pull-up;
			};
		};

		ant_switch_gpio3: ant_switch_gpio3 {
			mux {
				pins = "gpio33";
				function = "gpio";
			};

			config {
				pins = "gpio33";
				drive-strength = <2>;
				output-high;
				bias-pull-up;
			};
		};
	};

	bmi160_int1_default: bmi160_int1_default {
		mux {
			pins = "gpio81";
			function = "gpio";
		};
		config {
			pins = "gpio81";
			drive-strength = <16>; /* 16 mA */
			bias-pull-down; /* pull down */
		};
	};

	bmi160_int2_default: bmi160_int2_default {
		mux {
			pins = "gpio94";
			function = "gpio";
		};
		config {
			pins = "gpio94";
			drive-strength = <16>; /* 16 mA */
			bias-pull-down; /* pull down */
		};
	};

	i2c_1b {
		i2c_1b_active: i2c_1b_active {
			mux {
				pins = "gpio84", "gpio85";
				function = "blsp_i2c1";
			};

			config {
				pins = "gpio84", "gpio85";
				drive-strength = <2>;
				bias-disable;
			};
		};

		i2c_1b_sleep: i2c_1b_sleep {
			mux {
				pins = "gpio84", "gpio85";
				function = "blsp_i2c1";
			};

			config {
				pins = "gpio84", "gpio85";
				drive-strength = <2>;
				bias-pull-up;
			};
		};
	};

	can_reset {
		can_rst_on: rst_on {
			mux {
				pins = "gpio89";
				function = "gpio";
			};

			config {
				pins = "gpio89";
				drive-strength = <2>; /* 2 mA */
				bias-pull-up;
			};
		};

		can_rst_off: rst_off {
			mux {
				pins = "gpio89";
				function = "gpio";
			};

			config {
				pins = "gpio89";
				drive-strength = <2>; /* 2 mA */
				bias-pull-up;
				output-high;
			};
		};
	};
};

&cnss_pcie {
	vdd-wlan-io-supply = <&wlan_ext_vreg>;
	/delete-property/ vdd-wlan-xtal-supply;
	/delete-property/ vdd-wlan-xtal-aon-supply;
};

/* BLE serial (no HCI) */
&blsp1_uart1 {
	status = "ok";
	pinctrl-names = "default";
	pinctrl-0 = <&uart1_console_active>;
};

/* Console conflicts with periph_vreg (GPIO_9) */
&blsp1_uart3 {
	status = "ok";
	pinctrl-names = "default";
	pinctrl-0 = <&uart3_console_active>;
};

/* BT classic */
&blsp1_uart2_hs {
	status = "ok";
};

&i2c_1 {
	status = "ok";
	pinctrl-0 = <&i2c_1b_active>;
	pinctrl-1 = <&i2c_1b_sleep>;
};

&i2c_3 {
	status = "ok";

	wcd9xxx_codec@d {
		status = "disabled";
	};

	tlv320aic3x_codec: tlv320aic3x@18 {
		compatible = "ti,tlv320aic3x";
		reg = <0x18>;
		gpio-reset = <&tlmm_pinmux 90 0>;
		AVDD-supply = <&codec_vreg>;
		IOVDD-supply = <&pmd9650_l6>;
	};

	lsm330-gyro@6a {
		compatible = "st,lsm330-gyro";
		reg = <0x6a>;
	};

	lsm330-accel@1d {
		compatible = "st,lsm330-accel";
		reg = <0x1d>;
	};

	bmi160@68 {
		compatible = "bosch-sensortec,bmi160";
		reg = <0x68>;
		pinctrl-names = "default";
		pinctrl-0 = <&bmi160_int1_default &bmi160_int2_default>;
		interrupt-parent = <&tlmm_pinmux>;
		interrupts = <81 0x2002>;
		bmi,init-interval = <200>;
		bmi,place = <1>;
		bmi,gpio_irq = <&tlmm_pinmux 81 0x2002>;
	};
};

&spi_4 {
	status = "ok";
	/delete-property/ qcom,use-bam;

	can-controller@0 {
		compatible = "qcom,nxp,mpc5746c";
		reg = <0>;
		spi-max-frequency = <9600000>;
		interrupt-parent = <&tlmm_pinmux>;
		interrupts = <87 2>;
		qcom,reset-gpio = <&tlmm_pinmux 89 0x1>;
		qcom,clk-freq-mhz = <20000000>;
		qcom,max-can-channels = <1>;
		qcom,bits-per-word = <8>;
		qcom,reset-delay-msec = <200>;
		qcom,can-fw-cmd-timeout-req;
		qcom,can-fw-cmd-timeout-ms = <400>;
		qcom,rem-all-buffering-timeout-ms = <2000>;
		pinctrl-names = "active", "sleep";
		pinctrl-0 = <&can_rst_on>;
		pinctrl-1 = <&can_rst_off>;
	};
};

&usb3 {
	qcom,charging-disabled;
};

/* Needed by blsp1_uart1 (BLE) */
&pmd9650_l13 {
	regulator-always-on;
};

&pmd9650_gpios {
	gpio@c000 { /* GPIO 1 - PCIESW_EN */
		status = "ok";
		qcom,mode = <1>;		/* Digital output*/
		qcom,output-type = <0>;		/* CMOS logic */
		qcom,invert = <1>;		/* Output high */
		qcom,vin-sel = <1>;		/* 1.8 V */
		qcom,src-sel = <0>;		/* Constant */
		qcom,out-strength = <1>;	/* High drive strength */
		qcom,master-en = <1>;		/* Enable GPIO */
	};

	gpio@c100 { /* GPIO 2 - VADC */
		/* GPIO should be left off, and in the high
		 * impedance state when the pin is used with the VADC
		 */
		status = "ok";
		qcom,master-en = <0>;		/* DISABLE GPIO */
	};

	gpio@c200 { /* GPIO 3 - CAN_ETH_EN, CAN on v1 and ETH on v2 */
		status = "ok";
		qcom,mode = <1>;		/* Digital output*/
		qcom,output-type = <0>;		/* CMOS logic */
		qcom,invert = <1>;		/* Output high */
		qcom,vin-sel = <1>;		/* 1.8 V */
		qcom,src-sel = <0>;		/* Constant */
		qcom,out-strength = <1>;	/* High drive strength */
		qcom,master-en = <1>;		/* Enable GPIO */
	};

	gpio@c400 { /* GPIO 5 - USB_ID */
		status = "ok";
		qcom,mode = <0>;		/* Digital input */
		qcom,pull = <1>;		/* Pull up 1.5 uA */
		qcom,vin-sel = <1>;		/* 1.8 V */
		qcom,master-en = <1>;		/* Enable GPIO */
	};

	gpio@c500 { /* GPIO 6 - Rome 3.3V control */
		status = "ok";
		qcom,mode = <1>;		/* Digital output*/
		qcom,output-type = <0>;		/* CMOS logic */
		qcom,invert = <1>;		/* Output high */
		qcom,vin-sel = <0>;		/* VPH_PWR */
		qcom,src-sel = <0>;		/* Constant */
		qcom,out-strength = <1>;	/* High drive strength */
		qcom,master-en = <1>;		/* Enable GPIO */
	};

	gpio@c700 { /* GPIO 8 - BT_EN */
		status = "ok";
		qcom,mode = <1>;		/* Digital output*/
		qcom,pull = <4>;		/* Pulldown 10uA */
		qcom,vin-sel = <0>;		/* VPH_PWR */
		qcom,src-sel = <0>;		/* GPIO */
		qcom,invert = <0>;		/* Invert */
		qcom,master-en = <1>;		/* Enable GPIO */
	};
};

&pmd9650_vadc {
	chan@83 {
		label = "vph_pwr";
		reg = <0x83>;
		qcom,decimation = <2>;
		qcom,pre-div-channel-scaling = <1>;
		qcom,calibration-type = "absolute";
		qcom,scale-function = <0>;
		qcom,hw-settle-time = <0>;
		qcom,fast-avg-setup = <0>;
	};

	chan@4c {
		label = "xo_therm_buf";
		reg = <0x4c>;
		qcom,decimation = <2>;
		qcom,pre-div-channel-scaling = <0>;
		qcom,calibration-type = "ratiometric";
		qcom,scale-function = <4>;
		qcom,hw-settle-time = <2>;
		qcom,fast-avg-setup = <0>;
		qcom,vadc-thermal-node;
	};

	chan@53 {
		label = "ambient_therm";
		reg = <0x53>;
		qcom,decimation = <2>;
		qcom,pre-div-channel-scaling = <0>;
		qcom,calibration-type = "ratiometric";
		qcom,scale-function = <2>;
		qcom,hw-settle-time = <2>;
		qcom,fast-avg-setup = <0>;
		qcom,vadc-thermal-node;
	};

	chan@4d {
		label = "mdm_case_therm";
		reg = <0x4d>;
		qcom,decimation = <2>;
		qcom,pre-div-channel-scaling = <0>;
		qcom,calibration-type = "ratiometric";
		qcom,scale-function = <2>;
		qcom,hw-settle-time = <2>;
		qcom,fast-avg-setup = <0>;
		qcom,vadc-thermal-node;
	};

	chan@4e {
		label = "pa_therm1";
		reg = <0x4e>;
		qcom,decimation = <2>;
		qcom,pre-div-channel-scaling = <0>;
		qcom,calibration-type = "ratiometric";
		qcom,scale-function = <2>;
		qcom,hw-settle-time = <2>;
		qcom,fast-avg-setup = <0>;
		qcom,vadc-thermal-node;
	};

	chan@4f {
		label = "pa_therm2";
		reg = <0x4f>;
		qcom,decimation = <2>;
		qcom,pre-div-channel-scaling = <0>;
		qcom,calibration-type = "ratiometric";
		qcom,scale-function = <2>;
		qcom,hw-settle-time = <2>;
		qcom,fast-avg-setup = <0>;
		qcom,vadc-thermal-node;
	};
};
