Bug 207407

Summary: ThinkPad X1 Carbon (7/8th Gen) / X1 Yoga (4th gen) - improve speakers configuration
Product: Drivers Reporter: chris (chris+linux)
Component: Sound(ALSA)Assignee: Jaroslav Kysela (perex)
Status: RESOLVED CODE_FIX    
Severity: normal CC: daniel-other+bugzillakernel, martin, nate, steffen.weber, tiwai
Priority: P1    
Hardware: Intel   
OS: Linux   
Kernel Version: 5.6.6 Subsystem:
Regression: No Bisected commit-id:
Attachments: alsa-info.sh output on 5.6.6 when only two are working
alsa-info.sh output on 5.6.6 when all four speakers are working
alsa-info.sh output on 5.4.8 when all speakers are working (without SOF)
alsa-info output with commit d2cd795c reverted
Codec diagram

Description chris 2020-04-22 16:14:38 UTC
Created attachment 288665 [details]
alsa-info.sh output on 5.6.6 when only two are working

The ThinkPad X1 Carbon (7th Gen) has four speakers, two on the top and two on the bottom. More information on this machine: https://wiki.archlinux.org/index.php/Lenovo_ThinkPad_X1_Carbon_(Gen_7).

I'm running Arch Linux on this system. On Windows 10 and Linux kernel 5.3.x to 5.4.8, all four speakers work properly. 

However, since kernel 5.4.10 (as recent as 5.6.6) on Arch, the two speakers on the bottom does not work when the machine is booted directly into Linux. The same issue also exists on Ubuntu 20.04 beta. However, if I boot into Windows 10 first, then reboot into Linux, all speakers work correctly. I'm using the SOF firmware and PulseAudio 13.99.1, but the problem still exists if I boot with 'snd_intel_dspcfg.dsp_driver=1' or 'snd_hda_intel.dmic_detect=0'.

I think firmware and EC aren't the issue here, since everything works correctly prior to kernel 5.4.10. If it helps, I've attached outputs of alsa-info.sh taken on 5.6.6 when all speakers are working (alsa-info-good), on 5.6.6 when only two are working (alsa-info-bad), and on 5.4.8 when speakers work without SOF (alsa-info-548).
Comment 1 chris 2020-04-22 16:15:11 UTC
Created attachment 288667 [details]
alsa-info.sh output on 5.6.6 when all four speakers are working
Comment 2 chris 2020-04-22 16:16:04 UTC
Created attachment 288669 [details]
alsa-info.sh output on 5.4.8 when all speakers are working (without SOF)
Comment 3 Takashi Iwai 2020-04-22 16:41:06 UTC
If it's a regression around 5.4.10, the most suspected one is the backport of commit d2cd795c4ece1a24fda170c35eeb4f17d9826cbb.

Could you try to revert it?
Comment 4 Jaroslav Kysela 2020-04-22 19:42:08 UTC
It is not necessary to revert this.. Just boot with:

options snd-intel-dspcfg dsp-driver=1
options snd-hda-intel model=thinkpad

The problem is that in this case the bottom speakers (controlled with Bass volume) are always at max (DAC without volume control at NID 0x06).

Could you also try to mute the Speaker in alsamixer (keep volume)?
Comment 5 chris 2020-04-22 19:54:14 UTC
(In reply to Jaroslav Kysela from comment #4)
> It is not necessary to revert this.. Just boot with:
> 
> options snd-intel-dspcfg dsp-driver=1
> options snd-hda-intel model=thinkpad
> 
> The problem is that in this case the bottom speakers (controlled with Bass
> volume) are always at max (DAC without volume control at NID 0x06).
> 
> Could you also try to mute the Speaker in alsamixer (keep volume)?

With these parameters, all speakers work properly (just like on 5.4.8). Volume control can be done by adding the following to /usr/share/pulseaudio/alsa-mixer/paths/analog-output.conf.common:

[Element Master]
switch = mute
volume = ignore

However, without SOF, the microphone array isn't detected. I'll try to revert commit d2cd795c4ece1a24fda170c35eeb4f17d9826cbb and report back.
Comment 6 chris 2020-04-22 23:33:26 UTC
Created attachment 288677 [details]
alsa-info output with commit d2cd795c reverted
Comment 7 chris 2020-04-22 23:34:03 UTC
Comment on attachment 288677 [details]
alsa-info output with commit d2cd795c reverted

I reverted commit d2cd795c4ece1a24fda170c35eeb4f17d9826cbb on kernel/git/stable/linux.git (linux-5.6.y branch), and built the kernel from source. Now internal speakers and mics work properly, but volume control does not work.

I attached the output of alsa-info.sh on this kernel, if it helps.
Comment 8 Jaroslav Kysela 2020-04-23 11:57:38 UTC
Could you compare, if you have the same result with the unmodified kernel when you mute (in alsamixer) the Speaker (not Bass Speaker)?
Comment 9 chris 2020-04-23 13:16:02 UTC
With unmodified kernel 5.6.6 and PulseAudio 13.0, speakers work correctly when I change 'Speaker' volume to 100 and mute 'Speaker' in alsamixer. With PulseAudio 13.99.1, speakers are muted when 'Speaker' is muted in alsamixer.
Comment 10 Jaroslav Kysela 2020-04-23 13:51:08 UTC
Created attachment 288681 [details]
Codec diagram
Comment 11 Jaroslav Kysela 2020-04-23 14:04:23 UTC
Here is the codec diagram for X1 7th. Basically, the current kernels try to use DAC NID 0x02 for both woofer (PIN NID 17h) and tweeter (PIN NID 14h). The DAC NID 0x02 has the volume control, because DAC NID 0x06 (used in previous kernels) was intended for S/PDIF and has no volume control.

I probably see similar thing on my test X1, but I cannot say that woofers (bottom) does not work. If woofers are on max, it's difficult to distinguish the sound source. It seems to me that tweeters (top speakers) are a bit low and when everything is unmuted, the sound looks a bit more balanced but weaker. Unfortunately, I don't have Windows here, so I cannot test this settings.

If you do hear a difference, then the Windows driver does something which Linux does not. I see equalizer and ClassD amp on the path. This part of hardware is undocumented and we can probably reverse engineer the communication from the Windows driver (e.g. https://github.com/Conmanx360/QemuHDADump).
Comment 12 Jaroslav Kysela 2020-04-23 19:23:49 UTC
I am doing another tests today with my X1 (without Windows) and all speakers are working correctly.

Kernel: 5.5.7

Firmware:

# fwupdmgr upgrade
No upgrades for Thunderbolt Controller: current version is 44.00: 44.00=same
No upgrades for System Firmware: current version is 0.1.30: 0.1.30=same, 0.1.29=older, 0.1.27=older, 0.1.26=older, 0.1.25=older
No upgrades for Touchpad: current version is 1.3.3013337: 1.3.3013337=same
No upgrades for UEFI Device Firmware: current version is 192.47.1524: 192.47.1524=same
No upgrades for UEFI Device Firmware: current version is 0.1.15: 0.1.15=same, 0.1.14=older, 0.1.13=older, 0.1.12=older, 0.1.10=older

Test procedure

- disable and kill pulseaudio (I usually rename /usr/bin/pulseaudio)
- run speaker-test (or any other app which directly use the speakers)
  - speaker-test -D hw:0 -c 2 -d 10000
- alsamixer
  - Master to 100%, unmute
  - Speaker - play with volume and mute/unmute
  - Bass Speaker - play with mute/unmute

Speaker mute switch correctly turns on/off tweeters on top and the Bass Speaker switch correctly turns on/off the bottom woofers.

Note that pulseaudio automatically turns off the Master when you mute Speaker, so it's not possible to test this with pulseaudio.
Comment 13 chris 2020-04-24 00:09:42 UTC
I did the same tests on my system (booted directly to Linux from poweroff). All speakers make sound and controls in alsamixer do work correctly, as you described.

My test procedure and results

- disabled and killed pulseaudio by renaming /usr/bin/pulseaudio
- Played song with VLC Media Player
  - set Audio > Output module to 'ALSA audio output'
  - set Audio > Device to 'sof-hda-dsp'
  - Played Faded by Alan Walker (has a wide range of frequencies)
- In alsamixer:
  - Master volume set to 100%, unmuteed
  - Speaker muted and Bass Speaker unmuted: Speaker and Master volume controls overall sound volume; sound is very similar to Windows
  - Speaker and Bass Speaker unmuted: all speakers work; volume is noticeably weaker;
  - Speaker unmuted and Bass Speaker muted: sound is very quiet; tremble only, no bass

I also observed the speakers' behavior on Windows. The majority of sound come from the bottom woofers, the top-firing tweeters are on but barely noticeable. To me, sound on Windows is not that different from the "Speaker muted and Bass Speaker unmuted" scenario.

I think the difference between Speaker muted and Speaker unmuted largely depends on preference. As many desktop Linux users come from Windows, I guess we should stay close to the sound quality on Windows? Let me know what you think.

FYI, I suspect that the undocumented parts of the codec diagram have something to do with the "Dolby Atmos Speaker System" and proprietary Dolby technology.

My firmware and kernel  information

- Kernel: 5.5.7

- Firmware:

# fwupdmgr get-devices
├─Thunderbolt Controller:
│     Current version:     44.00
│     Vendor:              Lenovo (TBT:0x0109)
├─Embedded Controller:
│     Current version:     0.1.15
│     Minimum Version:     0.1.15
│     Vendor:              DMI:LENOVO
├─Intel Management Engine:
│     Current version:     192.49.1534
│     Minimum Version:     0.0.1
│     Vendor:              DMI:LENOVO
├─System Firmware:
│     Current version:     0.1.30
│     Minimum Version:     0.0.1
│     Vendor:              LENOVO (DMI:LENOVO)
├─Touchpad:
│     Current version:     1.3.3013337
│     Bootloader Version:  8.0.0
│     Vendor:              Synaptics (HIDRAW:0x06CB)
├─UEFI Device Firmware:
│     Current version:     16777224
│     Minimum Version:     1
├─UEFI Device Firmware:
│     Current version:     4784136
│     Minimum Version:     4784136
│     Vendor:              DMI:LENOVO
Comment 14 Jaroslav Kysela 2020-04-24 18:18:44 UTC
Perhaps the Linux HDA driver should use separate 0x02 and 0x03 outputs like Windows driver does to maintain the volume controls separate. But there is a problem to map this in UCM (two volume controls with the preset offset).
Comment 15 martin 2020-05-07 08:45:49 UTC
I also got an X1C7 here (currently running Linux Mint 19.3/18.04LTS with 5.3.0-40 kernel. I currently have all 4 speakers working with SOF 1.4.2. Used this script to enable it https://gist.github.com/hamidzr/dd81e429dc86f4327ded7a2030e7d7d9
It breaky my audio though on 5.3.41+ kernels and only the tweeters work there. Anything i can do to help? I'm not so into the details of alsa but open for any instructions for testing!
Comment 16 chris 2020-06-25 02:27:28 UTC
So I just found another solution on ArchWiki. Doing the following makes the speakers behave the same as they do in Windows:
  - Apply this patch to '/usr/share/alsa/ucm2/sof-hda-dsp/HiFi.conf'

--- /usr/share/alsa/ucm2/sof-hda-dsp/HiFi.conf.old
+++ /usr/share/alsa/ucm2/sof-hda-dsp/HiFi.conf
@@ -20,9 +20,9 @@ SectionDevice."Headphones1" {
 	Value {
 		PlaybackPriority 200
 		PlaybackPCM "hw:${CardId}"
-		PlaybackMixerElem "Headphone"
+		PlaybackMixerElem "Master"
 		PlaybackMasterElem "Master"
-		PlaybackVolume "Headphone Playback Volume"
+		PlaybackVolume "Master Playback Volume"
 		PlaybackSwitch "Headphone Playback Switch"
 		If.jack {
 			Condition {
@@ -72,9 +72,9 @@ SectionDevice."Speaker" {
 	Value {
 		PlaybackPriority 100
 		PlaybackPCM "hw:${CardId}"
-		PlaybackMixerElem "Speaker"
+		PlaybackMixerElem "Master"
 		PlaybackMasterElem "Master"
-		PlaybackVolume "Speaker Playback Volume"
+		PlaybackVolume "Master Playback Volume"
 		PlaybackSwitch "Speaker Playback Switch"
 	}
 }

  - Run 'hda-verb /dev/snd/hwC0D0 0x17 SET_CONNECT_SEL 1' on startup

References:
  - https://wiki.archlinux.org/index.php/Talk:Lenovo_ThinkPad_X1_Carbon_(Gen_7)#New_audio_situation
  - https://gist.github.com/hamidzr/dd81e429dc86f4327ded7a2030e7d7d9#gistcomment-3335517
  - https://gist.github.com/hamidzr/dd81e429dc86f4327ded7a2030e7d7d9#gistcomment-3345062
Comment 17 Nate Graham 2020-06-25 02:53:23 UTC
That doesn't produce pleasing output on my X1 Yoga gen 4. The volume level is much too high and most of the volume adjustments can only be made between 1% and 5%. The front speakers do almost all the work while the rear speakers are practically inaudible.
Comment 18 Nate Graham 2020-06-25 03:00:15 UTC
Correction: the proposed modifications to HiFi.conf alone definitely produce better sound for me, but additionally running that hda-verb command ruins it and makes the front speakers much too loud. However without doing that, it's much improved with the HiFi.conf changes!
Comment 19 chris 2020-06-25 03:18:54 UTC
Hi Nate,

On my X1 Carbon 7th (not the Yoga, but hardware should be identical), patching HiFi.conf alone doesn't seem to make any difference. Patching HiFi.conf AND running that hda-verb command does help.

By the way, what version of PulseAudio and alsa-lib are you running? A few weeks ago, I found that running alsa-lib 1.2.3.1 and PulseAudio 13.99.1 (available in Ubuntu 20.04 and Fedora 32 repos) works much better than older versions of PulseAudio (because PulseAudio 13.99.1 correctly handles ucm2).

P.S. KDE Plasma user here and I LOVE it. Thanks for your work!

(In reply to Nate Graham from comment #18)
> Correction: the proposed modifications to HiFi.conf alone definitely produce
> better sound for me, but additionally running that hda-verb command ruins it
> and makes the front speakers much too loud. However without doing that, it's
> much improved with the HiFi.conf changes!
Comment 20 Nate Graham 2020-06-25 03:24:53 UTC
I'm using the latest PulseAudio built from git master, plus these patches installed on top:
- https://gitlab.freedesktop.org/pulseaudio/pulseaudio/-/merge_requests/271
- https://gitlab.freedesktop.org/pulseaudio/pulseaudio/-/merge_requests/295


I'm so glad you're enjoying Plasma! As a ThinkPad X1 user, you might appreciate https://invent.kde.org/plasma/plasma-pa/-/merge_requests/5 :)
Comment 21 Jaroslav Kysela 2020-06-25 07:42:11 UTC
A bit explanation 'hda-verb /dev/snd/hwC0D0 0x17 SET_CONNECT_SEL 1' is routing the bass/bottom speakers PIN 0x17 to DAC node 0x03 (this is the way I suggested in comment #14 and appaerently it's the routing which Windows use).

The UCM config change is wrong and it is not necessary (no volume range change).

The problem with this config / workaround is that the bass/bottom speaker is controlled with the 'Headphone' volume control (tweeters and bass speakers are separated). I need to find a way to describe in UCM, so pulseaudio can do the right volume control (busy with other UCM things right now). But - it's in my queue.
Comment 22 Jaroslav Kysela 2020-06-25 13:40:18 UTC
*** Bug 208133 has been marked as a duplicate of this bug. ***
Comment 23 Nate Graham 2020-06-25 14:13:14 UTC
(In reply to Jaroslav Kysela from comment #21)
> I need to find a way to describe in UCM, so pulseaudio can
> do the right volume control (busy with other UCM things right now). But -
> it's in my queue.

Thanks Jaroslav! That's great to hear.
Comment 25 chris 2020-09-29 00:16:32 UTC
On kernel 5.8.12 with SOF firmware, the issue seems to be resolved. Big thanks to everyone involved!
Comment 26 Nate Graham 2020-10-12 00:25:12 UTC
Can confirm. This is fixed for me too (X1 Yoga gen 3) with kernel 5.8.12+ and sof-firmware.

Great job!