toes-audio/reinit-codec.sh
2026-04-17 11:14:45 -07:00

74 lines
2.1 KiB
Bash
Executable File

#!/bin/bash
# Reinitialize the TLV320DAC3100 codec without a full reboot.
# Tears down the ALSA driver, resets the codec via i2c, then re-binds
# the driver so ALSA comes back fresh.
#
# Usage: sudo ./reinit-codec.sh
set -euo pipefail
BUS=1
ADDR=0x18
DRIVER_PATH="/sys/bus/i2c/drivers/tlv320aic31xx-codec"
DEVICE="1-0018"
w() { i2cset -y $BUS $ADDR "$@"; }
echo "=== Reinitializing TLV320DAC3100 ==="
# 1. Stop anything using the sound card
echo "[1/5] Stopping audio consumers..."
if command -v pulseaudio &>/dev/null; then
pulseaudio --kill 2>/dev/null || true
fi
if command -v pipewire &>/dev/null; then
systemctl --user stop pipewire pipewire-pulse 2>/dev/null || true
fi
fuser -k /dev/snd/* 2>/dev/null || true
sleep 0.2
# 2. Unbind the kernel driver so it releases the codec
echo "[2/5] Unbinding kernel driver..."
if [ -e "$DRIVER_PATH/$DEVICE" ]; then
echo "$DEVICE" > "$DRIVER_PATH/unbind"
sleep 0.3
echo " Driver unbound."
else
echo " Device not bound, skipping."
fi
# 3. Software-reset the codec over i2c
echo "[3/5] Resetting codec via i2c..."
# Select page 0
w 0x00 0x00
# Software reset
w 0x01 0x01
sleep 0.5
echo " Codec reset."
# 4. Re-bind the kernel driver (it will re-probe and init the codec)
echo "[4/5] Re-binding kernel driver..."
echo "$DEVICE" > "$DRIVER_PATH/bind"
sleep 1
echo " Driver bound."
# 5. Restore mixer defaults
echo "[5/5] Restoring mixer settings..."
sleep 0.5
# Unmute and set sensible defaults via amixer
amixer -c0 sset 'DAC' 127 127 2>/dev/null || true
amixer -c0 sset 'Speaker Driver' 0 2>/dev/null || true
amixer -c0 sset 'Speaker Analog' 100 2>/dev/null || true
amixer -c0 sset 'HP Analog' 100 100 2>/dev/null || true
amixer -c0 sset 'HP Driver' 0 0 2>/dev/null || true
# Restart audio daemons if they were running
if command -v pipewire &>/dev/null; then
systemctl --user start pipewire pipewire-pulse 2>/dev/null || true
fi
echo ""
echo "=== Codec reinitialized ==="
aplay -l 2>/dev/null | grep -A1 "barepiaudio" || echo "(warning: card not visible yet)"