/*
* Copyright (c) 2022 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/

/dts-v1/;
#include <nordic/nrf52840_qiaa.dtsi>

#include <dt-bindings/led/led.h>
#include <dt-bindings/zmk/matrix_transform.h>

#include "ckp-pinctrl.dtsi"

/ {
    model = "CKP";
    compatible = "polarityworks,ckp";

    chosen {
        zephyr,code-partition = &code_partition;
        zephyr,sram = &sram0;
        zephyr,flash = &flash0;
        zmk,kscan = &kscan0;
        zmk,underglow = &led_strip;
        zmk,backlight = &backlight;
        zmk,battery = &vbatt;
    };

    sensors: sensors {
        compatible = "zmk,keymap-sensors";
        sensors = <&encoder_1>;
        triggers-per-rotation = <20>;
    };

    kscan0: kscan_0 {
        compatible = "zmk,kscan-gpio-matrix";
        wakeup-source;

        diode-direction = "col2row";

        col-gpios
            = <&gpio1 11 GPIO_ACTIVE_HIGH>
            , <&gpio1 10 GPIO_ACTIVE_HIGH>
            , <&gpio1 13 GPIO_ACTIVE_HIGH>
            , <&gpio1 15 GPIO_ACTIVE_HIGH>
            , <&gpio0 3 GPIO_ACTIVE_HIGH>
            , <&gpio0 2 GPIO_ACTIVE_HIGH>
            , <&gpio0 28 GPIO_ACTIVE_HIGH>
            , <&gpio0 29 GPIO_ACTIVE_HIGH>
            , <&gpio0 30 GPIO_ACTIVE_HIGH>
            , <&gpio0 31 GPIO_ACTIVE_HIGH>
            , <&gpio0 5 GPIO_ACTIVE_HIGH>
            , <&gpio0 7 GPIO_ACTIVE_HIGH>
            , <&gpio1 9 GPIO_ACTIVE_HIGH>
            , <&gpio0 12 GPIO_ACTIVE_HIGH>
            , <&gpio0 23 GPIO_ACTIVE_HIGH>
            , <&gpio1 6 GPIO_ACTIVE_HIGH>
            ;

        row-gpios
            = <&gpio0 22 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)>
            , <&gpio1 0 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)>
            , <&gpio1 3 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)>
            , <&gpio1 1 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)>
            , <&gpio1 2 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)>
            , <&gpio1 4 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)>
            ;
    };

    // Node name must match original "EXT_POWER" label to preserve user settings.
    EXT_POWER {
        compatible = "zmk,ext-power-generic";
        control-gpios = <&gpio0 13 GPIO_ACTIVE_HIGH>;
    };

    encoder_1: encoder_1 {
        compatible = "alps,ec11";
        a-gpios = <&gpio0 21 (GPIO_ACTIVE_HIGH | GPIO_PULL_UP)>;
        b-gpios = <&gpio0 19 (GPIO_ACTIVE_HIGH | GPIO_PULL_UP)>;
        steps = <80>;
        status = "okay";
    };

    encoder_2: encoder_2 {
        compatible = "alps,ec11";
        a-gpios = <&gpio0 26 (GPIO_ACTIVE_HIGH | GPIO_PULL_UP)>;
        b-gpios = <&gpio0 6 (GPIO_ACTIVE_HIGH | GPIO_PULL_UP)>;
        steps = <80>;
        status = "okay";
    };

    encoder_3: encoder_3 {
        compatible = "alps,ec11";
        a-gpios = <&gpio0 9 (GPIO_ACTIVE_HIGH | GPIO_PULL_UP)>;
        b-gpios = <&gpio0 10 (GPIO_ACTIVE_HIGH | GPIO_PULL_UP)>;
        steps = <80>;
        status = "okay";
    };

    backlight: pwmleds {
        compatible = "pwm-leds";
        pwm_led_0 {
            pwms = <&pwm0 0 10000 PWM_POLARITY_NORMAL>;
        };
    };

    leds {
        compatible = "gpio-leds";
        blue_led: led_0 {
            gpios = <&gpio0 15 GPIO_ACTIVE_HIGH>;
        };
    };

    vbatt: vbatt {
        compatible = "zmk,battery-voltage-divider";
        io-channels = <&adc 2>;
        output-ohms = <100000>;
        full-ohms = <(100000 + 100000)>;
    };
};

&adc {
    status = "okay";
};

&pwm0 {
    status = "okay";
    pinctrl-0 = <&pwm0_default>;
    pinctrl-1 = <&pwm0_sleep>;
    pinctrl-names = "default", "sleep";
};

&gpiote {
    status = "okay";
};

&gpio0 {
    status = "okay";
};

&gpio1 {
    status = "okay";
};

zephyr_udc0: &usbd {
    status = "okay";
};


&flash0 {
    /*
     * For more information, see:
     * http://docs.zephyrproject.org/latest/devices/dts/flash_partitions.html
     */
    partitions {
        compatible = "fixed-partitions";
        #address-cells = <1>;
        #size-cells = <1>;

        sd_partition: partition@0 {
            reg = <0x00000000 0x00026000>;
        };
        code_partition: partition@26000 {
            reg = <0x00026000 0x000c6000>;
        };

        /*
         * The flash starting at 0x000ec000 and ending at
         * 0x000f3fff is reserved for use by the application.
         */

        /*
         * Storage partition will be used by FCB/LittleFS/NVS
         * if enabled.
         */
        storage_partition: partition@ec000 {
            reg = <0x000ec000 0x00008000>;
        };

        boot_partition: partition@f4000 {
            reg = <0x000f4000 0x0000c000>;
        };
    };
};

&spi3 {
    compatible = "nordic,nrf-spim";
    status = "okay";
    pinctrl-0 = <&spi3_default>;
    pinctrl-1 = <&spi3_sleep>;
    pinctrl-names = "default", "sleep";

    led_strip: ws2812@0 {
        compatible = "worldsemi,ws2812-spi";

        /* SPI */
        reg = <0>;
        spi-max-frequency = <4000000>;

        /* WS2812 */
        chain-length = <12>; /* number of LEDs */
        spi-one-frame = <0x70>;
        spi-zero-frame = <0x40>;
        color-mapping = <LED_COLOR_ID_GREEN
                   LED_COLOR_ID_RED
                   LED_COLOR_ID_BLUE>;
    };
};
