Skip to content

Commit f26f6ff

Browse files
committed
feat(audiodriver): add 02-stage-audiodriver-2michat-v2 for V2.0 HAT
The ReSpeaker 2-Mics Pi HAT V2.0 replaces the WM8960 codec (V1, I2C 0x1a) with a TLV320AIC3104 codec (I2C 0x18). The mainline kernel already ships the `snd-soc-tlv320aic3x` driver, so no DKMS module is needed — but a device-tree overlay and a separate set of mixer defaults are required. Without these two bits the card either never appears or appears silent. The new stage mirrors the v1 layout: - prerun.sh — copy_previous guard, same as v1 - 01-driver/ - 01-packages — build deps only (no dkms, no kernel headers needed) - 03-run-chroot.sh — clone seeed-linux-dtoverlays, compile respeaker-2mic-v2_0-overlay.dtbo with dtc, install into /boot/overlays, enable I2C + dtoverlay=respeaker-2mic-v2_0 in /boot/config.txt - 02-set-audio-volume/ - 01-run.sh — install script + oneshot unit (same pattern as v1) - files/configure_audio.sh — TLV320-aware defaults: PCM 85% (digital pre-DAC, 100% clips on typical JST speakers), HP DAC and Line DAC to 100% (defaults sit at -23.5 dB), HP and Line output amps unmuted at 100%, then alsactl store + wpctl sink to 1.0 - files/configure_audio.service — After=sound.target alsa-restore.service, no dependency on a seeed-voicecard.service (none needed for v2) The v2 stage is additive; the existing v1 stage is untouched. A build can pick v1 or v2 by including the matching directory in stage selection.
1 parent d66c4f0 commit f26f6ff

6 files changed

Lines changed: 159 additions & 0 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
git
2+
build-essential
3+
device-tree-compiler
4+
i2c-tools
5+
libasound2-plugins
6+
alsa-utils
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/bin/bash -e
2+
3+
# ReSpeaker 2-Mics Pi HAT **V2.0** uses a TLV320AIC3104 codec on I2C 0x18 —
4+
# NOT the WM8960 codec (0x1a) used on V1. No out-of-tree DKMS module is
5+
# needed: the mainline `snd-soc-tlv320aic3x` driver already ships in the
6+
# kernel. We just need the device-tree overlay that wires it up to the Pi's
7+
# I2S pins. The overlay source is maintained by Seeed in their
8+
# seeed-linux-dtoverlays repo.
9+
10+
echo "Building ReSpeaker 2-Mic HAT V2.0 device-tree overlay..."
11+
12+
SRC=/tmp/seeed-linux-dtoverlays
13+
rm -rf "$SRC"
14+
git clone --depth 1 https://github.com/Seeed-Studio/seeed-linux-dtoverlays.git "$SRC"
15+
16+
cd "$SRC"
17+
make overlays/rpi/respeaker-2mic-v2_0-overlay.dtbo
18+
19+
# detect boot config + overlays location
20+
OVERLAYS_DIR=/boot/overlays
21+
CONFIG=/boot/config.txt
22+
if [ -d /boot/firmware/overlays ]; then
23+
OVERLAYS_DIR=/boot/firmware/overlays
24+
fi
25+
if [ -f /boot/firmware/config.txt ]; then
26+
CONFIG=/boot/firmware/config.txt
27+
fi
28+
if [ -f /boot/firmware/usercfg.txt ]; then
29+
CONFIG=/boot/firmware/usercfg.txt
30+
fi
31+
32+
install -v -m 644 overlays/rpi/respeaker-2mic-v2_0-overlay.dtbo \
33+
"${OVERLAYS_DIR}/respeaker-2mic-v2_0.dtbo"
34+
35+
# set boot params: enable I2C and load the v2 overlay
36+
sed -i -e 's:#dtparam=i2c_arm=on:dtparam=i2c_arm=on:g' "$CONFIG" || true
37+
grep -q "^dtparam=i2c_arm=on$" "$CONFIG" || echo "dtparam=i2c_arm=on" >> "$CONFIG"
38+
grep -q "^dtoverlay=respeaker-2mic-v2_0$" "$CONFIG" || \
39+
echo "dtoverlay=respeaker-2mic-v2_0" >> "$CONFIG"
40+
41+
cd /
42+
rm -rf "$SRC"
43+
44+
echo "Done. After boot the card appears as 'seeed2micvoicec' (bcm2835-i2s-tlv320aic3x-hifi)."
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash -e
2+
3+
install -v -m 755 files/configure_audio.sh "${ROOTFS_DIR}/usr/bin/configure_audio.sh"
4+
install -v -m 644 files/configure_audio.service "${ROOTFS_DIR}/etc/systemd/system/configure_audio.service"
5+
6+
on_chroot << EOF
7+
echo "Enable audio services"
8+
systemctl daemon-reload
9+
systemctl enable configure_audio.service
10+
EOF
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[Unit]
2+
Description=PiCompose - Configure Audio volume setting (ReSpeaker 2-Mic HAT V2.0)
3+
After=sound.target alsa-restore.service
4+
Wants=sound.target alsa-restore.service
5+
6+
[Service]
7+
Type=oneshot
8+
ExecStart=/usr/bin/configure_audio.sh
9+
RemainAfterExit=yes
10+
Restart=on-failure
11+
RestartSec=30s
12+
13+
[Install]
14+
WantedBy=multi-user.target
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/bin/bash
2+
set -u
3+
4+
# TLV320AIC3104 mixer defaults on the V2.0 HAT are extremely quiet: the
5+
# HP DAC attenuates by -23.5 dB and the HP analog amp sits at ~89%. Without
6+
# tuning these three separate gain stages, end users report "silent card"
7+
# even with HA / LVA output at maximum.
8+
#
9+
# Values below are the safe defaults calibrated against JST-connected
10+
# speakers (peaks clip at 100% PCM). Tune further in-field with `amixer`
11+
# and persist via `alsactl store`.
12+
13+
wait_for_card_and_control() {
14+
local card="$1"
15+
local control="$2"
16+
local max_tries=30
17+
local count=0
18+
19+
while [ "$count" -lt "$max_tries" ]; do
20+
count=$((count + 1))
21+
22+
if amixer -c "$card" info >/dev/null 2>&1; then
23+
if amixer -c "$card" scontrols | grep -Fq "'$control'"; then
24+
echo "Card $card with control '$control' is ready ($count/$max_tries)"
25+
return 0
26+
fi
27+
echo "Card $card found, but control '$control' not ready yet ($count/$max_tries)"
28+
else
29+
echo "Card $card not ready yet ($count/$max_tries)"
30+
fi
31+
32+
sleep 1
33+
done
34+
35+
echo "Card $card with control '$control' did not become ready"
36+
return 1
37+
}
38+
39+
set_control_if_exists() {
40+
local card="$1"
41+
local control="$2"
42+
shift 2
43+
44+
if amixer -c "$card" scontrols | grep -Fq "'$control'"; then
45+
echo "Setting $control on $card to $*"
46+
amixer -c "$card" set "$control" "$@"
47+
return 0
48+
fi
49+
50+
echo "Control '$control' not found on $card, skipping"
51+
return 0
52+
}
53+
54+
# The kernel alias is the same for V1 and V2: `seeed2micvoicec`. The V2 HAT
55+
# is distinguished by the presence of `PCM`/`HP DAC`/`Line DAC` controls
56+
# (from tlv320aic3x) rather than V1's `Headphone`/`Speaker` (wm8960).
57+
if ! wait_for_card_and_control seeed2micvoicec PCM; then
58+
echo "No TLV320AIC3104-based card became ready; is the V2.0 overlay loaded?"
59+
exit 1
60+
fi
61+
CARD="seeed2micvoicec"
62+
63+
# Digital pre-DAC attenuator. 100% clips on typical speakers; 85% keeps
64+
# headroom while still being clearly audible.
65+
set_control_if_exists "$CARD" "PCM" 85%
66+
67+
# DAC output gains (ship at -23.5 dB by default — main source of quietness).
68+
set_control_if_exists "$CARD" "HP DAC" 100%
69+
set_control_if_exists "$CARD" "Line DAC" 100%
70+
71+
# Analog output amps (ship slightly below max and muted on some units).
72+
set_control_if_exists "$CARD" "HP" 100% unmute
73+
set_control_if_exists "$CARD" "Line" 100% unmute
74+
75+
# Default-route PipeWire sink to max; application-level volume still applies.
76+
if command -v wpctl >/dev/null 2>&1; then
77+
wpctl set-volume @DEFAULT_AUDIO_SINK@ 1.0 || true
78+
fi
79+
80+
alsactl store
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash -e
2+
3+
if [ ! -d "${ROOTFS_DIR}" ]; then
4+
copy_previous
5+
fi

0 commit comments

Comments
 (0)