From 0272057a7d03a2dd0d10fa35390f223230d618c3 Mon Sep 17 00:00:00 2001
From: Ondrej Jirman <megi@xff.cz>
Date: Sun, 4 Sep 2022 23:25:19 +0200
Subject: [PATCH 371/463] arm64: dts: rk3399-pinephone-pro: Add sound support

Add support for Pinephone Pro sound card and codec.

It also adds a jack detection GPIO to the sound card definition, and
creates a new set of ADC keys for handling headset buttons.

Co-developed-by: Arnaud Ferraris <arnaud.ferraris@gmail.com>
Signed-off-by: Arnaud Ferraris <arnaud.ferraris@gmail.com>
Signed-off-by: Ondrej Jirman <megi@xff.cz>
---
 .../dts/rockchip/rk3399-pinephone-pro.dts     | 119 ++++++++++++++++++
 1 file changed, 119 insertions(+)

diff --git a/arch/arm64/boot/dts/rockchip/rk3399-pinephone-pro.dts b/arch/arm64/boot/dts/rockchip/rk3399-pinephone-pro.dts
index 5bef46c587439..6959ebf4701d3 100644
--- a/arch/arm64/boot/dts/rockchip/rk3399-pinephone-pro.dts
+++ b/arch/arm64/boot/dts/rockchip/rk3399-pinephone-pro.dts
@@ -135,6 +135,70 @@ multi-led {
 		leds = <&led_red>, <&led_green>, <&led_blue>;
 	};
 
+	// in1  - digital mic daughhterboard
+	// in2  - headset mic
+	// in3  - modem output  (muxed with mono)
+	// spol - earphone
+	// hpo  - heaphones
+	// lout - modem input
+	// spaker - amp enabled by SPK_CTL_H
+	//
+	// mclk - GPIO4_A0/I2S_CLK
+
+	rt5640-sound {
+		compatible = "simple-audio-card";
+		pinctrl-names = "default";
+		pinctrl-0 = <&hp_det>;
+		simple-audio-card,name = "PinePhonePro";
+		simple-audio-card,aux-devs = <&speaker_amp>;
+		simple-audio-card,format = "i2s";
+		simple-audio-card,mclk-fs = <256>;
+		simple-audio-card,hp-det-gpio = <&gpio4 RK_PD4 GPIO_ACTIVE_LOW>;
+		simple-audio-card,pin-switches = "Internal Speaker";
+
+		simple-audio-card,widgets =
+			"Microphone", "Headset Microphone",
+			"Microphone", "Internal Microphone",
+			"Headphone", "Headphones",
+			"Speaker", "Internal Earpiece",
+			"Speaker", "Internal Speaker",
+			"Line", "Line In Modem",
+			"Line", "Line Out Modem";
+
+		simple-audio-card,routing =
+			"Headphones", "HPOL",
+			"Headphones", "HPOR",
+			"Internal Earpiece", "SPOLP",
+			"Internal Earpiece", "SPOLN",
+			"Internal Speaker", "Speaker Amp OUTL",
+			"Internal Speaker", "Speaker Amp OUTR",
+			"Speaker Amp INL", "HPOL",
+			"Speaker Amp INR", "HPOR",
+			"DMIC1", "Internal Microphone",
+			"Headset Microphone", "MICBIAS1",
+			"IN2P", "Headset Microphone",
+			"Line Out Modem", "LOUTL",
+			"Line Out Modem", "LOUTR",
+			"IN3P", "Line In Modem",
+			"IN3N", "Line In Modem";
+
+		simple-audio-card,cpu {
+			sound-dai = <&i2s0>;
+		};
+
+		simple-audio-card,codec {
+			sound-dai = <&rt5640>;
+		};
+	};
+
+	speaker_amp: audio-amplifier {
+		compatible = "simple-audio-amplifier";
+		pinctrl-names = "default";
+		pinctrl-0 = <&spk_en>;
+		enable-gpios = <&gpio0 RK_PB3 GPIO_ACTIVE_HIGH>;
+		sound-name-prefix = "Speaker Amp";
+	};
+
 	vcc_sys: vcc-sys-regulator {
 		compatible = "regulator-fixed";
 		regulator-name = "vcc_sys";
@@ -213,6 +277,8 @@ vcc1v8_codec: vcc1v8-codec-regulator {
 		pinctrl-names = "default";
 		pinctrl-0 = <&vcc1v8_codec_en>;
 		regulator-name = "vcc1v8_codec";
+		regulator-always-on;
+		regulator-boot-on;
 		regulator-min-microvolt = <1800000>;
 		regulator-max-microvolt = <1800000>;
 		vin-supply = <&vcc3v3_sys>;
@@ -387,8 +453,13 @@ regulator-state-mem {
 
 			vcca3v0_codec: LDO_REG1 {
 				regulator-name = "vcca3v0_codec";
+				regulator-always-on;
+				regulator-boot-on;
 				regulator-min-microvolt = <3000000>;
 				regulator-max-microvolt = <3000000>;
+				regulator-state-mem {
+					regulator-on-in-suspend;
+				};
 			};
 
 			vcc3v0_touch: LDO_REG2 {
@@ -402,8 +473,13 @@ regulator-state-mem {
 
 			vcca1v8_codec: LDO_REG3 {
 				regulator-name = "vcca1v8_codec";
+				regulator-always-on;
+				regulator-boot-on;
 				regulator-min-microvolt = <1800000>;
 				regulator-max-microvolt = <1800000>;
+				regulator-state-mem {
+					regulator-on-in-suspend;
+				};
 			};
 
 			rk818_pwr_on: LDO_REG4 {
@@ -616,6 +692,41 @@ opp07 {
 	};
 };
 
+&i2c1 {
+	status = "okay";
+
+	clock-frequency = <400000>;
+	i2c-scl-rising-time-ns = <300>;
+	i2c-scl-falling-time-ns = <15>;
+	// XXX: modem codec supplies:
+	// - vcc1v8_codec
+	// - vcca3v0_codec
+
+	// supplies: (always on currently)
+	// - vcca3v0_codec
+	// - vcca1v8_codec
+	// - vcc5v0_sys - spk_vcc
+	rt5640: rt5640@1c {
+		compatible = "realtek,rt5640";
+		reg = <0x1c>;
+		clocks = <&cru SCLK_I2S_8CH_OUT>;
+		clock-names = "mclk";
+		assigned-clocks = <&cru SCLK_I2S0_8CH>;
+		assigned-clock-rates = <11289599>;
+		realtek,dmic1-data-pin = <1>;
+		realtek,in3-differential;
+		realtek,lout-differential;
+		#sound-dai-cells = <0>;
+	};
+};
+
+&i2s0 {
+	rockchip,playback-channels = <2>;
+	rockchip,capture-channels = <2>;
+	pinctrl-0 = <&i2s0_2ch_bus>;
+	status = "okay";
+};
+
 &io_domains {
 	bt656-supply = <&vcc1v8_dvp>;
 	audio-supply = <&vcca1v8_codec>;
@@ -722,6 +833,14 @@ sound {
 		vcc1v8_codec_en: vcc1v8-codec-en {
 			rockchip,pins = <3 RK_PA4 RK_FUNC_GPIO &pcfg_pull_down>;
 		};
+
+		hp_det: hp-det {
+			rockchip,pins = <4 RK_PD4 RK_FUNC_GPIO &pcfg_pull_none>;
+		};
+
+		spk_en: spk-en {
+			rockchip,pins = <0 RK_PB3 RK_FUNC_GPIO &pcfg_pull_none>;
+		};
 	};
 
 	wireless-bluetooth {
-- 
2.46.0

