This commit is contained in:
Pat Nakajima 2026-04-19 15:43:57 -07:00
parent 912e427243
commit fd2df1ece5

View File

@ -32,6 +32,23 @@ mkdir -p "$OVERLAYS"
RESTORE_HELPER="/usr/local/sbin/barepi-audio-restore"
RESTORE_SERVICE="/etc/systemd/system/barepi-audio-restore.service"
MODULES_LOAD_FILE="/etc/modules-load.d/barepi-audio.conf"
NEED_KERNEL_REBOOT=0
have_audio_modules() {
modinfo snd-soc-simple-card >/dev/null 2>&1 \
&& modinfo snd-soc-tlv320aic31xx >/dev/null 2>&1 \
&& modinfo snd-soc-ics43432 >/dev/null 2>&1
}
install_pkg_if_available() {
local pkg="$1"
if apt-cache show "$pkg" >/dev/null 2>&1; then
apt-get install -y "$pkg"
return 0
fi
return 1
}
echo "=== barepi audio setup ==="
echo "User: $TARGET_USER Home: $TARGET_HOME"
@ -43,6 +60,48 @@ echo ""
echo "[1/7] Installing packages..."
apt-get update
apt-get install -y alsa-utils libasound2-plugins swh-plugins i2c-tools device-tree-compiler
if ! have_audio_modules; then
KERNEL_MODULES_PKG="linux-modules-$(uname -r)"
if install_pkg_if_available "$KERNEL_MODULES_PKG"; then
echo " Installed $KERNEL_MODULES_PKG"
fi
fi
if ! have_audio_modules && [ -r /etc/os-release ]; then
. /etc/os-release
if [ "${ID:-}" = "ubuntu" ] && install_pkg_if_available linux-image-raspi-6.8; then
install_pkg_if_available linux-headers-raspi-6.8 || true
NEED_KERNEL_REBOOT=1
echo " Installed Ubuntu raspi 6.8 kernel fallback for ASoC modules."
fi
fi
if ! have_audio_modules; then
if [ "$NEED_KERNEL_REBOOT" -eq 1 ]; then
echo " Current kernel still lacks the required ASoC modules; continuing because a compatible kernel was installed." >&2
echo " Reboot into the new kernel after setup completes." >&2
else
echo "Missing required audio kernel modules: snd-soc-simple-card, snd-soc-tlv320aic31xx, snd-soc-ics43432" >&2
echo "Install a raspi kernel package that provides them, then rerun this script." >&2
exit 1
fi
fi
{
echo "i2c-dev"
for mod in snd-soc-simple-card snd-soc-tlv320aic31xx snd-soc-ics43432; do
if [ "$NEED_KERNEL_REBOOT" -eq 1 ] || modinfo "$mod" >/dev/null 2>&1; then
echo "$mod"
fi
done
} > "$MODULES_LOAD_FILE"
modprobe i2c-dev || true
modprobe snd-soc-simple-card || true
modprobe snd-soc-tlv320aic31xx || true
modprobe snd-soc-ics43432 || true
echo " Done."
# 2. Compile and install device tree overlay
@ -143,10 +202,10 @@ cat > "$DTS" << 'DTS_EOF'
};
fragment@3 {
target = <&sound>;
target-path = "/";
__overlay__ {
sound {
compatible = "simple-audio-card";
i2s-controller = <&i2s_clk_producer>;
#address-cells = <1>;
#size-cells = <0>;
simple-audio-card,name = "barepi-audio";
@ -156,14 +215,14 @@ cat > "$DTS" << 'DTS_EOF'
simple-audio-card,dai-link@0 {
reg = <0>;
format = "i2s";
bitclock-master = <&dailink0_master>;
frame-master = <&dailink0_master>;
bitclock-master = <&cpu0>;
frame-master = <&cpu0>;
dailink0_master: cpu {
cpu0: cpu {
sound-dai = <&i2s_clk_producer>;
};
codec {
codec0: codec {
sound-dai = <&dac_codec>;
system-clock-frequency = <1536000>;
};
@ -172,19 +231,20 @@ cat > "$DTS" << 'DTS_EOF'
simple-audio-card,dai-link@1 {
reg = <1>;
format = "i2s";
bitclock-master = <&dailink1_master>;
frame-master = <&dailink1_master>;
bitclock-master = <&cpu1>;
frame-master = <&cpu1>;
dailink1_master: cpu {
cpu1: cpu {
sound-dai = <&i2s_clk_producer>;
};
codec {
codec1: codec {
sound-dai = <&mic_codec>;
};
};
};
};
};
};
DTS_EOF
dtc -@ -I dts -O dtb -o "$OVERLAYS/i2s-audio.dtbo" "$DTS"
@ -686,10 +746,12 @@ fi
echo ""
echo "=== Done ==="
if [ "$CHANGED" -eq 1 ]; then
echo "config.txt was modified — reboot to load the overlay/reset config."
else
echo "config.txt unchanged. Reboot if this is a first-time setup."
echo "config.txt was modified."
fi
if [ "$NEED_KERNEL_REBOOT" -eq 1 ]; then
echo "A compatible Ubuntu raspi kernel was installed."
fi
echo "Reboot to load the updated overlay and audio kernel/module state."
echo "Speaker routing defaults and mic GPIO defaults will be restored automatically after boot."
echo "Manual speaker check/adjust: $TARGET_HOME/volume.sh"
echo "Manual mic test: $TARGET_HOME/test-mics.sh"