// SPDX-License-Identifier: GPL-2.0
// Device Tree Overlay for PCM1808 ADC + PCM5102 DAC I2S combo
// Pi Multi-FX Pedal — Ourpad Networks
//
// PCM1808: 24-bit stereo ADC (we use single-channel mono guitar input)
//   - I2S slave mode (BCLK + LRCK from RPi)
//   - FMT = GND (I2S, 24-bit left-justified)
//   - MD1 = 3.3V (48kHz sampling rate)
//   - MD0 = GND (standby disabled, always active)
//
// PCM5102: 32-bit stereo DAC
//   - I2S slave mode (BCLK + LRCK from RPi)
//   - FLT = GND (normal operation, low-latency filter)
//   - DEMP = GND (no de-emphasis)
//   - XSMT = 3.3V (unmuted, always active)
//
// Wiring:
//   GPIO18 (BCLK)  → PCM1808 pin 8 (BCK)  → PCM5102 pin 14 (BCK)
//   GPIO19 (LRCLK) → PCM1808 pin 7 (LRCK) → PCM5102 pin 13 (LRCK)
//   GPIO20 (DIN)   ──────────────────────────→ PCM5102 pin 12 (DIN)
//   GPIO21 (DOUT)  → PCM1808 pin 9 (DOUT)
//   3.3V           → PCM1808 VCC           → PCM5102 Vin
//   GND            → PCM1808 GND           → PCM5102 GND

/dts-v1/;
/plugin/;

/ {
    compatible = "brcm,bcm2711";  // RPi 4B

    fragment@0 {
        target = <&i2s>;
        __overlay__ {
            status = "okay";
            #sound-dai-cells = <0>;
        };
    };

    // PCM1808 ADC — captured via I2S DOUT (GPIO21)
    fragment@1 {
        target = <&i2s>;
        __overlay__ {
            #address-cells = <1>;
            #size-cells = <0>;

            pcm1808: pcm1808@0 {
                compatible = "ti,pcm1808";
                reg = <0>;              // I2S device 0 (first codec on bus)
                status = "okay";
                #sound-dai-cells = <0>;
            };
        };
    };

    // PCM5102 DAC — driven via I2S DIN (GPIO20)
    fragment@2 {
        target = <&i2s>;
        __overlay__ {
            #address-cells = <1>;
            #size-cells = <0>;

            pcm5102: pcm5102@1 {
                compatible = "ti,pcm5102a";
                reg = <1>;              // I2S device 1 (second codec on bus)
                status = "okay";
                #sound-dai-cells = <0>;
            };
        };
    };

    // Simple audio card binding ADC + DAC
    fragment@3 {
        target = <&sound>;
        __overlay__ {
            compatible = "simple-audio-card";
            status = "okay";

            simple-audio-card,name = "pcm1808-pcm5102-hat";

            // RPi is I2S master
            simple-audio-card,format = "i2s";
            simple-audio-card,bitclock-master = <&cpu_dai>;
            simple-audio-card,frame-master = <&cpu_dai>;

            // 48kHz / 24-bit
            simple-audio-card,mclk-fs = <256>;  // 48kHz * 256 = 12.288MHz

            cpu_dai: simple-audio-card,cpu {
                sound-dai = <&i2s>;
            };

            // PCM1808 ADC — capture path
            pcm1808_dai: simple-audio-card,codec@0 {
                sound-dai = <&pcm1808>;
                format = "i2s";
                bitclock-master;
                frame-master;
            };

            // PCM5102 DAC — playback path
            pcm5102_dai: simple-audio-card,codec@1 {
                sound-dai = <&pcm5102>;
                format = "i2s";
                bitclock-master;
                frame-master;
            };
        };
    };

    __overrides__ {
        bclk-pin = <&i2s>,"bcm,pin-functions:18";
        lrclk-pin = <&i2s>,"bcm,pin-functions:19";
        din-pin   = <&i2s>,"bcm,pin-functions:20";
        dout-pin  = <&i2s>,"bcm,pin-functions:21";
    };
};