Linux 6.12 ASoC Tri-Partite Split, 7x9 WM8960 Regmap & GPR1 MCLK Direction

An embedded audio subsystem spans three independent hardware domains — SoC DMA/SAI silicon, an I2C-controlled analog/digital codec with non-byte-aligned 9-bit registers, and board-level pad/clock wiring — which Linux 6.12 ASoC cleanly factors into Platform, Codec, and Machine drivers.

20 minfree previewLinux 6.12C11glibc 2.39→ module 1 lab
What you'll learn
  • Trace DAI registration and snd_soc_dai_link binding across fsl_sai.c, wm8960.c, and imx-wm8960.c.
  • Encode and decode 16-bit WM8960 [15:9] register + [8:0] payload frames across 8-bit I2C transactions.
  • Configure IOMUXC_GPR1 bit 20 (0x100000) to drive SAI2_MCLK out to a codec-master WM8960.

Decoupling Platform DMA, Codec Silicon, and Board Machine Topology in ASoC

On desktop PCs, a PCIe soundcard integrates DMA engines, digital interfaces, and analog converters behind one vendor ID, allowing a single monolithic driver to own the entire card. On ARMv7-A SoCs, that model collapses: i.MX6ULL SAI2 can connect to WM8960, SGTL5000, TLV320AIC3106, or Bluetooth PCM transceivers, while the WM8960 codec appears across dozens of unrelated SoC families. Linux kernel 6.12 ASoC solves this N-by-M explosion by splitting every sound card into three strictly isolated driver classes under sound/soc/.

First, the Platform layer (sound/soc/fsl/fsl_sai.c paired with devm_snd_dmaengine_pcm_register) manages SAI2 registers and cyclic SDMA ring buffers without knowing which codec sits on the wire. Second, the Codec driver (sound/soc/codecs/wm8960.c) registers an I2C client at 0x1a, exposing the wm8960-hifi Digital Audio Interface (DAI), mixer controls, and power widgets with zero Freescale/NXP headers. Third, the Machine driver (sound/soc/fsl/imx-wm8960.c or fsl-asoc-card.c) reads Device Tree phandles to bind SAI2 and WM8960 via snd_soc_dai_link.

fig_m01_l02_a.svg — ASoC Platform/Machine/Codec split and why clearing GPR1…

Figure 1.2a. ASoC Platform/Machine/Codec split and why clearing GPR1 bit 20 stalls codec-master I2S.

mental model: Think of a modular rail freight terminal: standardized locomotives (SoC SAI/DMA Platform) and interchangeable refrigerated cars (Codec silicon) never weld directly to each other; a yard manifest (Machine driver) couples a specific locomotive to a specific car and throws the power switch.

Where it stops working: Unlike passive rail couplings, the ASoC Machine driver actively renegotiates PLL clock parents, DAI master/slave roles, and SoC general-purpose registers on every PCM hw_params rate change.

definition (ALSA System on Chip (ASoC)): Merged into the Linux kernel (sound/soc/) to eliminate board-specific forks of audio drivers, ASoC splits embedded sound support into reusable Platform (CPU DAI + dmaengine PCM), Codec (codec DAI + regmap mixers/DAPM), and Machine (snd_soc_card + snd_soc_dai_link) modules.

ast_m01_l02.svg — ASoC isolates fsl_sai, wm8960 (7x9 I2C RBTREE cache),…step 1/4
  1. Step 1. Independently register the SoC fsl-sai CPU DAI/DMA component and the I2C2 wm8960@1a codec component.
  2. Step 2. Set IOMUXC_GPR1 bit 20 (0x100000) in the Machine driver so SAI2_MCLK drives the external codec.
  3. Step 3. Match CPU DAI (&sai2) and Codec DAI (&codec) via snd_soc_dai_link to instantiate ALSA card 0.
  4. Step 4. Update 9-bit WM8960 registers via REGCACHE_RBTREE shadow reads and 2-byte [7b reg | 9b val] I2C writes.

Figure 1.2b. ASoC isolates fsl_sai, wm8960 (7x9 I2C RBTREE cache), and imx-wm8960 (GPR1 bit 20 MCLK output).

WM8960 7x9-Bit Write-Only Regmap Packing and IOMUXC_GPR1 MCLK Routing

Inside wm8960.c, control transfers use Linux kernel regmap with an unusual geometry: reg_bits = 7 and val_bits = 9. WM8960 defines 56 registers (R0..R55, 0x00..0x37), each 9 bits wide so stereo PGA registers can store a 7-bit volume ([6:0]), a Zero-Cross enable ([7]), and a simultaneous Volume Update strobe (VU, bit [8]). Across 8-bit I2C frames to target 0x1a, the first payload byte carries (reg << 1) | ((val >> 8) & 1) and the second carries val & 0xFF. Crucially, WM8960 has no I2C readback hardware, so wm8960.c enables REGCACHE_RBTREE seeded from wm8960_reg_defaults[] to service every read-modify-write in RAM.

Meanwhile, the Machine driver enforces a board-critical clock mux inside IOMUXC_GPR1 (0x020E4004, gpr regmap offset 0x04). Pad JTAG_TMS (SAI2_MCLK) is bidirectional: when GPR1 bit 20 (SAI2_MCLK_DIR, mask 0x00100000) is 0, i.MX6ULL expects an external crystal on the pad; setting bit 20 to 1 drives internal PLL4_AUDIO_DIV out to WM8960 MCLK. With codec-master enabled, WM8960 locks its fractional PLL to that MCLK and drives exact BCLK and LRCLK clocks back into SAI2.

fig_m01_l02_b.svg — WM8960 7-bit address + 9-bit payload packing across two…

Figure 1.2b. WM8960 7-bit address + 9-bit payload packing across two 8-bit I2C bytes.

ASoC LayerLinux 6.12 Source PathKernel AbstractionOwned Hardware State
Platform (CPU DAI + PCM)sound/soc/fsl/fsl_sai.csnd_soc_dai_driver + dmaengine_pcmSAI2 0x0202C000 FIFOs & SDMA ch 37/38
Codec (Codec DAI)sound/soc/codecs/wm8960.csnd_soc_component_driver + regmap 7x9WM8960 I2C2 @ 0x1a R0..R55 (RBTREE cache)
Machine (Card Glue)sound/soc/fsl/imx-wm8960.csnd_soc_card + snd_soc_dai_linkIOMUXC_GPR1[20] MCLK dir, JD3 jack, routes
ALSA Core (VFS)sound/core/pcm_native.c/dev/snd/pcmC0D0{p,c}, controlC0hw_params negotiation, mmap ring, kcontrol
learner@ksa: ~/gl-e07/m01-l02
learner@ksa:~/gl-e07/m01-l02$ bat -H 14 -H 27 asoc_wm8960_regmap_gpr.c File: asoc_wm8960_regmap_gpr.c    /* Linux 6.12 excerpt pattern: WM8960 7x9 regmap + imx-wm8960 GPR1 MCLK dir */    #include <linux/regmap.h>    #include <sound/soc.h>        /* 1. Codec layer (sound/soc/codecs/wm8960.c): 7-bit reg + 9-bit val, write-only */    static const struct reg_default wm8960_reg_defaults[] = {        { 0x00, 0x0097 }, /* R0  Left Input Volume  (LINMUTE=1 default) */        { 0x02, 0x0000 }, /* R2  LOUT1 Volume       (0 = muted default) */        { 0x17, 0x01c0 }, /* R23 Additional Ctrl 1  (DATSEL=00 stereo)  */        { 0x18, 0x0000 }, /* R24 Additional Ctrl 2  (LRCM=0 default)    */    };        static const struct regmap_config wm8960_regmap = {      .reg_bits = 7,        .val_bits = 9,        .max_register = 0x37,        .reg_defaults = wm8960_reg_defaults,        .num_reg_defaults = ARRAY_SIZE(wm8960_reg_defaults),        .cache_type = REGCACHE_RBTREE, /* Mandatory: WM8960 has no I2C readback! */    };        /* 2. Machine layer (sound/soc/fsl/imx-wm8960.c): drive SAI2_MCLK out */    #define IMX6UL_GPR1_OFFSET        0x04u    #define IMX6UL_GPR1_SAI2_MCLK_DIR BIT(20) /* 0x00100000 */        static int imx_wm8960_enable_mclk_out(struct regmap *gpr) {      return regmap_update_bits(gpr, IMX6UL_GPR1_OFFSET,                                  IMX6UL_GPR1_SAI2_MCLK_DIR,                                  IMX6UL_GPR1_SAI2_MCLK_DIR);    }
  1. reg_bits=7 and val_bits=9 instruct regmap-i2c to pack [15:9] reg + [8:0] val into 2 bytes.
  2. Setting IOMUXC_GPR1 bit 20 (0x100000) configures pad SAI2_MCLK as an output toward WM8960.
regmap_init_i2c / ASoC(9-ksa) at a glance — GL-E07
regmap_init_i2c / ASoC(9-ksa) GL-E07 Quick Reference regmap_init_i2c / ASoC(9-ksa) NAME       devm_regmap_init_i2c, snd_soc_register_card — 7x9 codec regmap & ASoC       card binding        WM8960 WIRE ENCODING (I2C 7-bit addr 0x1a) byte[0] = ((reg & 0x7F) <<       1) | ((val >> 8) & 0x01) byte[1] = val & 0xFF Readback: Unsupported in       hardware -> inspect /sys/kernel/debug/regmap/1-001a/registers        IOMUXC_GPR1 MCLK DIRECTION Device Tree : gpr = <&gpr 4 0x100000       0x100000>; Register : IOMUXC_GPR1 (0x020E4004), bit 20 (SAI2_MCLK_DIR =       1 -> output) Kernel Systems Academy        Lesson 1.2         regmap_init_i2c / ASoC(9-ksa) GL-E07 quick reference · full manual page: man 9-ksa regmap_init_i2c / ASoC 

Verifying 7x9 I2C Octet Packing, RBTREE Shadow Cache, and GPR1 Bitmasks

Hand-calculating WM8960 I2C logic-analyzer captures trips up engineers who forget that reg is shifted left by 1 bit inside byte[0] (0x17 becomes 0x2E or 0x2F depending on data bit 8!). Our ISO C11 verification model implements a deterministic shadow-cached 7x9 regmap (R0..R55), performing masked update_bits mutations and recording the exact two-byte I2C frames emitted.

We test two production register writes: updating R2 (LOUT1 headphone volume 0x02) to 121 (0 dB) with Zero-Cross (bit 7) and Volume Update (bit 8) set (0x01F9), and updating R23 (0x17) DATSEL to 01 (0x01C4), plus verifying IOMUXC_GPR1 bit 20 (0x00100000) read-modify-write isolation so neighbor GPR1 bits (ENET1_TX_CLK_DIR at bit 13, ENET2 at bit 14) stay intact.

learner@ksa: ~/gl-e07/m01-l02
learner@ksa:~/gl-e07/m01-l02$ bat -H 12 -H 26 verify.c File: verify.c    #include <inttypes.h>    #include <stdbool.h>    #include <stdint.h>    #include <stdio.h>        struct wm8960_shadow {        uint16_t regs[56];        uint32_t bus_writes;    };        static int wm8960_pack(uint8_t reg, uint16_t val, uint8_t out[2]) {      if (reg > 0x37u || val > 0x01FFu) return -1;        out[0] = (uint8_t)((reg << 1) | ((val >> 8) & 0x01u));        out[1] = (uint8_t)(val & 0xFFu);        return 0;    }        static void wm8960_unpack(const uint8_t in[2], uint8_t *reg, uint16_t *val) {        *reg = (uint8_t)(in[0] >> 1);        *val = (uint16_t)((((uint16_t)(in[0] & 0x01u)) << 8) | in[1]);    }        static int wm8960_update_bits(struct wm8960_shadow *s, uint8_t reg,                                  uint16_t mask, uint16_t val, uint8_t wire[2]) {        if (reg > 0x37u) return -1;      uint16_t next = (uint16_t)((s->regs[reg] & ~mask) | (val & mask)) & 0x01FFu;        if (next == s->regs[reg]) return 0; /* Cache hit: suppress redundant I2C write */        s->regs[reg] = next;        s->bus_writes++;        return wm8960_pack(reg, next, wire);    }        int main(void) {        struct wm8960_shadow codec = {0};        codec.regs[0x17] = 0x01C0u; /* Default R23 */        uint8_t w_r2[2] = {0}, w_r23[2] = {0};        uint8_t dec_reg = 0;        uint16_t dec_val = 0;            wm8960_update_bits(&codec, 0x02u, 0x01FFu, 0x01F9u, w_r2);        wm8960_update_bits(&codec, 0x17u, 0x000Cu, 0x0004u, w_r23);        wm8960_update_bits(&codec, 0x17u, 0x000Cu, 0x0004u, w_r23); /* No-op cache hit */        wm8960_unpack(w_r23, &dec_reg, &dec_val);            uint32_t gpr1_enet_preserved = (1u << 13) | (1u << 14); /* ENET1/2 clk bits */        uint32_t gpr1_after = (gpr1_enet_preserved & ~(1u << 20)) | (1u << 20);            printf("R2  (0x02=0x01F9) -> I2C [0x%02X, 0x%02X]\n", w_r2[0], w_r2[1]);        printf("R23 (0x17=0x01C4) -> I2C [0x%02X, 0x%02X] unpack=(reg=0x%02X val=0x%03X) writes=%u\n",               w_r23[0], w_r23[1], dec_reg, dec_val, codec.bus_writes);        printf("GPR1: 0x%08X -> 0x%08X (SAI2_MCLK_DIR=%u ENET1_2_OK=%u)\n",               gpr1_enet_preserved, gpr1_after,               (gpr1_after >> 20) & 1u, ((gpr1_after >> 13) & 3u) == 3u);            return (w_r2[0] == 0x05u && w_r2[1] == 0xF9u &&                w_r23[0] == 0x2Fu && w_r23[1] == 0xC4u &&                codec.bus_writes == 2u && gpr1_after == 0x00106000u) ? 0 : 1;    }
  1. byte[0] packs 7-bit register index in bits [7:1] and bit 8 of the 9-bit value in bit [0].
  2. Shadow cache suppresses redundant I2C transactions when a masked update leaves the 9-bit word unchanged.

Tracing Regmap Shadow Updates and MCLK Direction Enforcement

Examine the verified stdout lines: writing 0x01F9 (VU=1, LO1ZC=1, LOUT1VOL=121) to R2 (0x02) emits [0x05, 0xF9] because (0x02 << 1) | 1 == 0x05. Updating R23 (0x17) from 0x01C0 to 0x01C4 emits [0x2F, 0xC4], and repeating the identical update leaves writes=2, confirming that the shadow cache suppresses redundant I2C traffic.

Notice also the GPR1 bitmask transition 0x00006000 -> 0x00106000: regmap_update_bits sets bit 20 (SAI2_MCLK_DIR = 1) without disturbing ENET1/ENET2 reference-clock direction bits [14:13], preventing a classic bug where raw writel() to IOMUXC_GPR1 breaks on-board Ethernet PHYs!

learner@ksa: ~/gl-e07/m01-l02
learner@ksa:~/gl-e07/m01-l02gcc -std=c11 -Wall -Wextra -Werror verify.c -o verifylearner@ksa:~/gl-e07/m01-l02./verifyR2  (0x02=0x01F9) -> I2C [0x05, 0xF9]R23 (0x17=0x01C4) -> I2C [0x2F, 0xC4] unpack=(reg=0x17 val=0x1C4) writes=2GPR1: 0x00006000 -> 0x00106000 (SAI2_MCLK_DIR=1 ENET1_2_OK=1)learner@ksa:~/gl-e07/m01-l02
strace — learner@ksa: ~/gl-e07/m01-l02
learner@ksa:~/gl-e07/m01-l02strace -e trace=write,exit_group ./verifywrite(1, "R2  (0x02=0x01F9) -> I2C [0x05, "..., 175) = 175exit_group(0)                           = ?+++ exited with 0 +++

Cross-Layer ASoC Binding Contracts and Silent Clock Pitfalls

The ASoC tri-partite split pays off whenever you diagnose audio bring-up systematically by layer: I2C NACK during probe points to the Codec layer (0x1a power/pins), missing /dev/snd/pcmC0D0p points to deferred Machine DAI matching (cpu-dai or audio-codec phandle), and PCM write error after open points to SAI2_MCLK (GPR1[20]) or SDMA clock stalling.

Keep the write-only nature of WM8960 front of mind during debugging: user-space i2cget or i2cdump against 0x1a cannot read registers and may disturb bus state. Always read Linux kernel regmap cache entries under /sys/kernel/debug/regmap/1-001a/registers.

Running i2cdump -y 1 0x1a to Inspect WM8960 Registers:

  • Symptom: Every cell prints XX (EREMOTEIO NACK) and engineers falsely conclude WM8960 is dead or unpowered.
  • Root Cause: WM8960 implements a write-only 2-wire serial control interface; it never ACKs I2C read frames (0x35).
  • Fix: Inspect /sys/kernel/debug/regmap/1-001a/registers or amixer contents backed by REGCACHE_RBTREE.

Overwriting IOMUXC_GPR1 Without Masked regmap_update_bits:

  • Symptom: Audio playback starts working, but eth0/eth1 RMII link immediately drops TX packets.
  • Root Cause: IOMUXC_GPR1 shares SAI2_MCLK_DIR (bit 20) with ENET1_TX_CLK_DIR (bit 13) and ENET2_TX_CLK_DIR (bit 14).
  • Fix: Always mutate GPR1 via syscon regmap_update_bits(gpr, 4, 0x100000, 0x100000) or DTS gpr = <&gpr 4 0x100000 0x100000>.

Guided Practice

Hand-compute the two I2C payload bytes (byte[0], byte[1]) sent to target address 0x1a when wm8960.c writes R15 (Reset, register 0x0F, value 0x0000) followed by R25 (Power Mgmt 1, register 0x19, value 0x01FC enabling VMID 50 kOhm, VREF, AINL, AINR, ADCL, ADCR, and MICB). Check bit 0 of byte[0] in both frames.

Independent Practice

Extend verify.c to simulate regcache_sync() after suspend-to-RAM: mark codec.dirty_mask across all 56 registers, skip registers whose cached value still equals wm8960_reg_defaults[], and emit only modified (reg, val) I2C byte pairs in ascending register order (R0 before R25/R26).

Verification Checks

quiz · 1 of 3

What two data bytes cross I2C2 (after address byte 0x34) when wm8960.c writes 9-bit value 0x01C4 into register R23 (0x17)?

quiz · 2 of 3

Why does sound/soc/codecs/wm8960.c configure cache_type = REGCACHE_RBTREE in wm8960_regmap?

quiz · 3 of 3

On i.MX6ULL with WM8960 configured as codec-master, what symptom appears if IOMUXC_GPR1 bit 20 (0x100000) remains 0?

Key takeaways

  • Linux kernel 6.12 ASoC decouples audio drivers into Platform (sound/soc/fsl/fsl_sai.c + cyclic dmaengine_pcm), Codec (sound/soc/codecs/wm8960.c at I2C2 @ 0x1a), and Machine (sound/soc/fsl/imx-wm8960.c snd_soc_dai_link).
  • WM8960 packs a 7-bit register address [15:9] and 9-bit value [8:0] into two I2C bytes (byte0 = (reg << 1) | ((val >> 8) & 1), byte1 = val & 0xFF) backed by REGCACHE_RBTREE shadow RAM because the chip has no I2C readback.
  • Setting IOMUXC_GPR1 bit 20 (0x00100000, SAI2_MCLK_DIR) at gpr offset 0x04 switches the i.MX6ULL SAI2_MCLK pad from high-impedance input to output so PLL4 clocks WM8960.
  • In codec-master topology (SND_SOC_DAIFMT_CBP_CFP), i.MX6ULL outputs SAI2_MCLK while WM8960's internal PLL drives BCLK and LRCLK back into SAI2 for exact 44.1 kHz and 48 kHz synthesis.
Progress is saved on this device.

search — fzf

0/0 ───────────────────────────────────────────

keyboard shortcuts — less

/
search
f
focus mode
[
previous
]
next
?
help
Esc
close

figure