# Control audio processing in SOF

# File paths for controlling SOF processing

Define.SOFVendor "$${sys:devices/virtual/dmi/id/sys_vendor}"
Define.SOFProduct "$${sys:devices/virtual/dmi/id/product_name}"

If.SOFVendor {
	Condition {
		Type String
		Empty "${var:SOFVendor}"
	}
	True.Define.SOFVendor "${sys:devices/virtual/dmi/id/board_vendor}"
}

If.SOFProduct {
	Condition {
		Type String
		Empty "${var:SOFProduct}"
	}
	True.Define.SOFProduct "${sys:devices/virtual/dmi/id/board_name}"
}

Define {
	PostMixerAnalogPlaybackIirBytes "N/A"
	PostMixerAnalogPlaybackFirBytes "N/A"
	PostMixerAnalogPlaybackDrcBytes "N/A"
	PostMixerAnalogPlaybackDrcSwitch "N/A"
	PostMixerSpeakerPlaybackIirBytes "N/A"
	PostMixerSpeakerPlaybackFirBytes "N/A"
	PostMixerSpeakerPlaybackDrcBytes "N/A"
	PostMixerSpeakerPlaybackDrcSwitch "N/A"
	Dmic0CaptureIirBytes "N/A"
	Dmic0CaptureDrcBytes "N/A"
	Dmic0CaptureDrcSwitch "N/A"
	Dmic0CaptureBeamformerBytes "N/A"
	Dmic0CaptureBeamformerBeamSwitch "N/A"
	Dmic0CaptureBeamformerBeamAngleSet "N/A"
	MicArrayCaptureIirBytes "N/A"
	MicArrayCaptureDrcBytes "N/A"
	MicArrayCaptureDrcSwitch "N/A"
	MicArrayCaptureBeamformerBytes "N/A"
	MicArrayCaptureBeamformerBeamSwitch "N/A"
	MicArrayCaptureBeamformerBeamAngleSet "N/A"
}

If.SOFHdaIpc4 {
	Condition {
		Type ControlExists
		Control "name='Post Mixer Analog Playback IIR Eq bytes'"
	}
	True.Define {
	      SOFIPCVer "ipc4"
	      PostMixerAnalogPlaybackIirBytes "Post Mixer Analog Playback IIR Eq bytes"
	      PostMixerAnalogPlaybackFirBytes "Post Mixer Analog Playback FIR Eq bytes"
	      PostMixerAnalogPlaybackDrcBytes "Post Mixer Analog Playback DRC bytes"
	      PostMixerAnalogPlaybackDrcSwitch "Post Mixer Analog Playback DRC switch"
	}
	False.If.SOFSdwIpc4 {
		Condition {
			Type ControlExists
			Control "name='Post Mixer Speaker Playback IIR Eq bytes'"
		}
		True.Define {
			SOFIPCVer "ipc4"
			PostMixerSpeakerPlaybackIirBytes "Post Mixer Speaker Playback IIR Eq bytes"
			PostMixerSpeakerPlaybackFirBytes "Post Mixer Speaker Playback FIR Eq bytes"
			PostMixerSpeakerPlaybackDrcBytes "Post Mixer Speaker Playback DRC bytes"
			PostMixerSpeakerPlaybackDrcSwitch "Post Mixer Speaker Playback DRC switch"
		}
		False.Define {
		      SOFIPCVer "ipc3"
		      PostMixerAnalogPlaybackIirBytes "EQIIR1.0 eqiir_coef_1"
		      PostMixerAnalogPlaybackFirBytes "EQFIR1.0 eqfir_coef_1"
		}
	}
}

If.SOFDmic0Proc {
	Condition {
		Type ControlExists
		Control "name='DMIC0 Capture IIR Eq'"
	}
	True.Define {
	      Dmic0CaptureIirBytes "DMIC0 Capture IIR Eq"
	      Dmic0CaptureDrcBytes "Dmic0 Capture DRC bytes"
	      Dmic0CaptureDrcSwitch "Dmic0 Capture DRC switch"
	      Dmic0CaptureBeamformerBytes "Dmic0 Capture TDFB bytes"
	      Dmic0CaptureBeamformerBeamSwitch "Dmic0 Capture TDFB beam switch"
	      Dmic0CaptureBeamformerBeamAngleSet "Dmic0 Capture TDFB angle set enum"
	}
}

If.SOFSdwMicArrayProc {
	Condition {
		Type ControlExists
		Control "name='Microphone Capture IIR Eq'"
	}
	True.Define {
	      MicArrayCaptureIirBytes "Microphone Capture IIR Eq"
	      MicArrayCaptureDrcBytes "Microphone Capture DRC bytes"
	      MicArrayCaptureDrcSwitch "Microphone Capture DRC switch"
	      MicArrayCaptureBeamformerBytes "Microphone Capture TDFB bytes"
	      MicArrayCaptureBeamformerBeamSwitch "Microphone Capture TDFB beam switch"
	      MicArrayCaptureBeamformerBeamAngleSet "Microphone Capture TDFB angle set enum"
	}
}

If.SOFPath {
	Condition { Type AlwaysTrue }
	True.Define {
		BlobPath "${ConfTopDir}/blobs/sof/${var:SOFIPCVer}"
		PostMixerAnalogPlaybackSpeakerIirBlob "${var:BlobPath}/eq_iir/highpass_100hz_0db_48khz.bin"
		PostMixerAnalogPlaybackSpeakerFirBlob "${var:BlobPath}/eq_fir/pass.bin"
		PostMixerAnalogPlaybackSpeakerDrcBlob "${var:BlobPath}/drc/speaker_default.bin"
		PostMixerAnalogPlaybackHeadphoneIirBlob "${var:BlobPath}/eq_iir/pass.bin"
		PostMixerAnalogPlaybackHeadphoneFirBlob "${var:BlobPath}/eq_fir/pass.bin"
		PostMixerAnalogPlaybackHeadphoneDrcBlob "${var:BlobPath}/drc/passthrough.bin"
		# Leave Dmic0 default blobs empty to not override blobs in topology by default.
		# The DMI ID based customization in blobs/sof/user_configs or
		# blobs/sof/product_configs may set these as FixedBootConfig.
		Dmic0CaptureDrcBlob ""
		Dmic0CaptureBeamformerBlob ""
		Dmic0CaptureIirBlob ""
		# Similarly leave SDW speaker default blobs empty.
		PostMixerSpeakerPlaybackIirBlob ""
		PostMixerSpeakerPlaybackFirBlob ""
		PostMixerSpeakerPlaybackDrcBlob ""
		# Also leave SDW microphone array default blobs empty.
		MicArrayCaptureDrcBlob ""
		MicArrayCaptureBeamformerBlob ""
		MicArrayCaptureIirBlob ""
		ConfPathFromDMI "${var:SOFVendor}/${var:SOFProduct}.conf"
		SOFProductConfig "/blobs/sof/product_configs/${var:ConfPathFromDMI}"
		SOFConfFullPath "${ConfTopDir}${var:SOFProductConfig}"
		SOFUserConfig "/blobs/sof/user_configs/${var:ConfPathFromDMI}"
		SOFUserConfFullPath "${ConfTopDir}${var:SOFUserConfig}"
	}
}

If.HasUserConfig {
	Condition {
		Type Path
		Mode read
		Path "${var:SOFUserConfFullPath}"
	}
	True {
		Include.product-config.File "${var:SOFUserConfig}"
	}
	False.If.HasProductConfig {
		Condition {
			Type Path
			Mode read
			Path "${var:SOFConfFullPath}"
		}
		True {
			Include.product-config.File "${var:SOFProductConfig}"
		}
	}
}

DefineMacro.SofDrcBootSetup {
	Define.ep_SwitchVar "${var:__endpoint}${var:__direction}DrcSwitch"
	Define.ep_BytesVar "${var:__endpoint}${var:__direction}DrcBytes"
	Define.ep_BlobVar "${var:__endpoint}${var:__direction}DrcBlob"
	Define.EndpointDrcSwitch "${var:$ep_SwitchVar}"
	Define.EndpointDrcBytes "${var:$ep_BytesVar}"
	Define.EndpointDrcBlob "${var:$ep_BlobVar}"
	If.EndpointMicDrc {
		Condition {
			Type ControlExists
			Control "name='${var:EndpointDrcSwitch}'"
		}
		True {
			If.EndpointDrcBlobDefined {
				Condition {
					Type String
					Empty "${var:EndpointDrcBlob}"
				}
				False.FixedBootSequence [
					#shell "/bin/echo 'Setting ${var:EndpointDrcBytes} to ${var:EndpointDrcBlob}' >> /tmp/alsa-ucm.txt"
					cset-tlv "name='${var:EndpointDrcBytes}' ${var:EndpointDrcBlob}"
				]
			}
			BootSequence [
				#shell "/bin/echo 'Switching ${var:EndpointDrcSwitch} on' >> /tmp/alsa-ucm.txt"
				cset "name='${var:EndpointDrcSwitch}' on"
			]
		}
	}
}

DefineMacro.SofBeamformerBootSetup {
	Define.ep_BeamSwitchVar "${var:__endpoint}${var:__direction}BeamformerBeamSwitch"
	Define.ep_BeamAngleSetVar "${var:__endpoint}${var:__direction}BeamformerBeamAngleSet"
	Define.ep_BytesVar "${var:__endpoint}${var:__direction}BeamformerBytes"
	Define.ep_BlobVar "${var:__endpoint}${var:__direction}BeamformerBlob"
	Define.EndpointBeamformerBeamSwitch "${var:$ep_BeamSwitchVar}"
	Define.EndpointBeamformerBeamAngleSet "${var:$ep_BeamAngleSetVar}"
	Define.EndpointBeamformerBytes "${var:$ep_BytesVar}"
	Define.EndpointBeamformerBlob "${var:$ep_BlobVar}"
	If.EndpointMicBeamformer {
		Condition {
			Type ControlExists
			Control "name='${var:EndpointBeamformerBeamSwitch}'"
		}
		True {
			If.EndpointBeamformerBlobDefined {
				Condition {
					Type String
					Empty "${var:EndpointBeamformerBlob}"
				}
				False.FixedBootSequence [
					#shell "/bin/echo 'Setting ${var:EndpointBeamformerBytes} to ${var:EndpointBeamformerBlob}' >> /tmp/alsa-ucm.txt"
					cset-tlv "name='${var:EndpointBeamformerBytes}' ${var:EndpointBeamformerBlob}"
				]
			}
			BootSequence [
				#shell "/bin/echo 'Switching ${var:EndpointBeamformerBeamSwitch} on' >> /tmp/alsa-ucm.txt"
				#shell "/bin/echo 'Setting ${var:EndpointBeamformerBeamAngleSet} 0' >> /tmp/alsa-ucm.txt"
				cset "name='${var:EndpointBeamformerBeamSwitch}' on"
				cset "name='${var:EndpointBeamformerBeamAngleSet}' 0"
			]
		}
	}
}

DefineMacro.SofEqBootSetup {
	Define.ep_BytesVar "${var:__endpoint}${var:__direction}${var:__eqtype}Bytes"
	Define.ep_BlobVar "${var:__endpoint}${var:__direction}${var:__eqtype}Blob"
	Define.EndpointEqBytes "${var:$ep_BytesVar}"
	Define.EndpointEqBlob "${var:$ep_BlobVar}"
	If.EndpointEq {
		Condition {
			Type ControlExists
			Control "name='${var:EndpointEqBytes}'"
		}
		True {
			If.EndpointEqBlobDefined {
				Condition {
					Type String
					Empty "${var:EndpointEqBlob}"
				}
				False.FixedBootSequence [
					#shell "/bin/echo 'Setting ${var:EndpointEqBytes} to ${var:EndpointEqBlob}' >> /tmp/alsa-ucm.txt"
					cset-tlv "name='${var:EndpointEqBytes}' ${var:EndpointEqBlob}"
				]
			}
		}
	}
}
