Define.MonoSpeaker ""
Define.AnalogMic "yes"
Define.DigitalMic ""

If.cfg-dmic1 {
	Condition {
		Type RegexMatch
		String "${CardLongName}"
		Regex "(ASUSTeK.*T100HAN|ASUSTeK.*T101HA)"
	}
	True {
		Define.AnalogMic ""
		Define.DigitalMic "DMIC1"
	}
}

If.cfg-dmic2 {
	Condition {
		Type RegexMatch
		String "${CardLongName}"
		Regex "(LENOVO.*LenovoMIIX320|MEDION.*Wingman|Standard-EF20EA-1.0)"
	}
	True {
		Define.AnalogMic ""
		Define.DigitalMic "DMIC2"
	}
}

If.cfg-mspk {
	Condition {
		Type RegexMatch
		String "${CardLongName}"
		Regex "(gpd-win-pocket-rt5645|TECLAST-X80Pro)"
	}
	True {
		Define.MonoSpeaker "yes"
	}
}

SectionVerb {
	# ALSA PCM
	Value {
		TQ "HiFi"
	}

	Include.ce.File "/codecs/rt5645/EnableSeq.conf"
	Include.cd.File "/codecs/rt5645/DisableSeq.conf"

	EnableSequence [
		cset "name='Stereo1 ADC1 Mux' 1"
		cset "name='I2S2 Func Switch' on"
		# 3/12 the headphone mic tends to be quite loud
		cset "name='IN1 Boost' 3"
		# 8/8 the internal analog mic tends to be quite soft
		cset "name='IN2 Boost' 8"
	]

	If.Controls {
		Condition {
			Type ControlExists
			Control "name='media0_in Gain 0 Switch'"
		}
		Before.EnableSequence "0"
		True {
			Include.pe.File "/platforms/bytcr/PlatformEnableSeq.conf"
			Include.pd.File "/platforms/bytcr/PlatformDisableSeq.conf"
		}
	}
}

SectionDevice."Speaker" {
	Comment "Speaker"

	Value {
		PlaybackPriority 100
		PlaybackPCM "hw:${CardId}"
	}

	ConflictingDevice [
		"Headphones"
	]

	Include.e.File "/codecs/rt5645/SpeakerEnableSeq.conf"

	If.mspk {
		Condition {
			Type String
			Empty "${var:MonoSpeaker}"
		}
		False {
			EnableSequence [
				# Monospeaker: Mix right to left
				cset "name='Stereo DAC MIXL DAC R1 Switch' 1"
				cset "name='Stereo DAC MIXR DAC R1 Switch' 0"
			]
			DisableSequence [
				# Undo monospeaker mixing
				cset "name='Stereo DAC MIXL DAC R1 Switch' 0"
				cset "name='Stereo DAC MIXR DAC R1 Switch' 1"
			]
		}
	}

	DisableSequence [
		cset "name='Ext Spk Switch' off"
		cset "name='Speaker Channel Switch' off"
	]
}

SectionDevice."Headphones" {
	Comment "Headphones"

	Value {
		PlaybackPriority 200
		PlaybackPCM "hw:${CardId}"
		JackControl "Headphone Jack"
	}

	ConflictingDevice [
		"Speaker"
	]

	Include.e.File "/codecs/rt5645/HeadphonesEnableSeq.conf"

	DisableSequence [
		cset "name='Headphone Switch' off"
		cset "name='Headphone Channel Switch' off"
	]
}

If.amic {
	Condition {
		Type String
		Empty "${var:AnalogMic}"
	}
	Before.SectionDevice "Headset"
	False.Include.amic.File "/codecs/rt5645/AnalogMic.conf"
}

If.dmic {
	Condition {
		Type String
		Empty "${var:DigitalMic}"
	}
	Before.SectionDevice "Headset"
	False {
		SectionDevice."Mic" {
			Comment "Internal Microphone"

			Value {
				CapturePriority 100
				CapturePCM "hw:${CardId}"
			}

			Include.e.File "/codecs/rt5645/DigitalMicEnableSeq.conf"
			Include.d.File "/codecs/rt5645/DigitalMicDisableSeq.conf"

			EnableSequence [
				cset "name='Stereo1 ADC2 Mux' DMIC"
				cset "name='Mono ADC L2 Mux' DMIC"
				cset "name='Mono ADC R2 Mux' DMIC"
			]

			If.dmic2 {
				Condition {
					Type String
					String1 "${var:DigitalMic}"
					String2 "DMIC1"
				}
				True {
					EnableSequence [
						cset "name='Stereo1 DMIC Mux' DMIC1"
					]
				}
				False {
					EnableSequence [
						cset "name='Stereo1 DMIC Mux' DMIC2"
						cset "name='Mono DMIC L Mux' DMIC2"
						cset "name='Mono DMIC R Mux' DMIC2"
					]
				}
			}
		}
	}
}

SectionDevice."Headset" {
	Comment "Headset Microphone"

	Value {
		CapturePriority 200
		CapturePCM "hw:${CardId}"
		JackControl "Headset Mic Jack"
	}

	Include.e.File "/codecs/rt5645/HSMicEnableSeq.conf"
	Include.d.File "/codecs/rt5645/HSMicDisableSeq.conf"

	EnableSequence [
		cset "name='Sto1 ADC MIXL ADC2 Switch' off"
		cset "name='Sto1 ADC MIXR ADC2 Switch' off"

		cset "name='Mono ADC MIXL ADC1 Switch' on"
		cset "name='Mono ADC MIXR ADC1 Switch' on"
		cset "name='Mono ADC MIXL ADC2 Switch' off"
		cset "name='Mono ADC MIXR ADC2 Switch' off"
	]
}
