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

#include "glove80.dtsi"
#include "glove80_lh-pinctrl.dtsi"

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

/ {
    model = "glove80_lh";
    compatible = "glove80_lh";

    chosen {
        zmk,underglow = &led_strip;
        zmk,backlight = &back_led_backlight;
        zmk,battery   = &vbatt;
    };

    back_led_backlight: pwmleds {
        compatible = "pwm-leds";
        pwm_led_0 {
            pwms = <&pwm0 0 PWM_USEC(20) PWM_POLARITY_NORMAL>;
        };
    };

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

    vbatt: vbatt {
        compatible = "zmk,battery-nrf-vddh";
    };
};

&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>; /* ignored, but necessary for SPI bindings */
        spi-max-frequency = <4000000>;

        /* WS2812 */
        chain-length = <40>; /* 40 keys have underglow at the moment */
        spi-one-frame = <0x70>;
        spi-zero-frame = <0x40>;

        color-mapping = <LED_COLOR_ID_GREEN LED_COLOR_ID_RED LED_COLOR_ID_BLUE>;
    };
};

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

&uart0 {
    compatible = "nordic,nrf-uarte";
    pinctrl-0 = <&uart0_default>;
    pinctrl-1 = <&uart0_sleep>;
    pinctrl-names = "default", "sleep";
};

&kscan0 {
    row-gpios
        = <&gpio0 26 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> // LH ROW1
        , <&gpio0 5 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> // LH ROW2
        , <&gpio0 6 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> // LH ROW3
        , <&gpio0 8 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> // LH ROW4
        , <&gpio0 7 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> // LH ROW5
        , <&gpio1 9 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> // LH ROW6
        ;
    col-gpios
        = <&gpio1 8 GPIO_ACTIVE_HIGH> // LH COL6
        , <&gpio1 4 GPIO_ACTIVE_HIGH> // LH COL5
        , <&gpio1 6 GPIO_ACTIVE_HIGH> // LH COL4
        , <&gpio1 7 GPIO_ACTIVE_HIGH> // LH COL3
        , <&gpio1 5 GPIO_ACTIVE_HIGH> // LH COL2
        , <&gpio1 3 GPIO_ACTIVE_HIGH> // LH COL1
        , <&gpio1 1 GPIO_ACTIVE_HIGH> // LH Thumb
        ;
};
