/dts-v1/;

#include <tactility/bindings/root.h>
#include <tactility/bindings/esp32_ble.h>
#include <tactility/bindings/esp32_wifi_pinned.h>
#include <tactility/bindings/esp32_gpio.h>
#include <tactility/bindings/esp32_i2c.h>
#include <tactility/bindings/esp32_i2s.h>
#include <tactility/bindings/esp32_spi.h>
#include <tactility/bindings/esp32_uart.h>
#include <tactility/bindings/esp32_sdspi.h>
#include <tactility/bindings/esp32_pwm_ledc.h>
#include <tactility/bindings/esp32_usbdevice.h>
#include <tactility/bindings/pwm_backlight.h>
#include <gps_generic/bindings.h>
#include <bindings/st7796.h>
#include <bindings/bq27220.h>
#include <bindings/tca8418.h>
#include <bindings/bq25896.h>
#include <bindings/drv2605.h>
#include <lilygo/bindings/tpager_encoder.h>

// Reference: https://wiki.lilygo.cc/get_started/en/LoRa_GPS/T-LoraPager/T-LoraPager.html
/ {
	compatible = "root";
	model = "LilyGO T-Lora Pager";

	wifi0 {
		compatible = "espressif,esp32-wifi-pinned";
		status = "disabled";
	};

	ble0 {
		compatible = "espressif,esp32-ble";
		status = "disabled";
	};

	gpio0 {
		compatible = "espressif,esp32-gpio";
		gpio-count = <49>;
	};

	i2c0 {
		compatible = "espressif,esp32-i2c";
		port = <I2C_NUM_0>;
		clock-frequency = <100000>;
		pin-sda = <&gpio0 3 GPIO_FLAG_NONE>;
		pin-scl = <&gpio0 2 GPIO_FLAG_NONE>;

		bq27220 {
			compatible = "ti,bq27220";
			reg = <0x55>;
		};

		// ALT (shift) is row 2 col 8; SYM (symbol layer) is row 2 col 0.
		keyboard {
			compatible = "ti,tca8418";
			reg = <0x34>;
			rows = <4>;
			columns = <10>;
			keymap-lc = [
				113 119 101 114 116 121 117 105 111 112 // q w e r t y u i o p
				97 115 100 102 103 104 106 107 108 10   // a s d f g h j k l ENTER
				0 122 120 99 118 98 110 109 0 8          //   z x c v b n m   BACKSPACE
				32 0 0 0 0 0 0 0 0 0                     // SPC
			];
			keymap-uc = [
				81 87 69 82 84 89 85 73 79 80 // Q W E R T Y U I O P
				65 83 68 70 71 72 74 75 76 10 // A S D F G H J K L ENTER
				0 90 88 67 86 66 78 77 0 8    //   Z X C V B N M   BACKSPACE
				32 0 0 0 0 0 0 0 0 0          // SPC
			];
			keymap-sy = [
				49 50 51 52 53 54 55 56 57 48 // 1 2 3 4 5 6 7 8 9 0
				46 47 43 45 61 58 39 34 64 9  // . / + - = : ' " @ TAB
				0 95 36 59 63 33 44 46 0 8    //   _ $ ; ? ! , .   BACKSPACE
				32 0 0 0 0 0 0 0 0 0          // SPC
			];
			shift-row = <2>;
			shift-col = <8>;
			sym-row = <2>;
			sym-col = <0>;
		};

		bq25896 {
			compatible = "ti,bq25896";
			reg = <0x6B>;
		};

		// Plays a 3x strong-click "buzz" once at boot to confirm the motor works (matches the
		// deprecated HAL's Drv2605 default autoPlayStartupBuzz=true).
		drv2605 {
			compatible = "ti,drv2605";
			reg = <0x5A>;
		};
	};

	// Keyboard backlight (LED_PWM), GPIO46. The keypress-triggered flash/decay effect from the
	// pre-migration driver isn't reproduced here - kernel drivers don't have a home for that kind
	// of UI policy (same reasoning as the encoder/touch drivers elsewhere). Off by default; app
	// code can drive brightness via the generic pwm-backlight API if desired.
	keyboard_backlight_pwm {
		compatible = "espressif,esp32-pwm-ledc";
		pin = <&gpio0 46 GPIO_FLAG_NONE>;
		period-ns = <33333>;
		ledc-timer = <1>;
		ledc-channel = <1>;
	};

	keyboard_backlight {
		compatible = "pwm-backlight";
		status = "disabled";
		pwm = <&keyboard_backlight_pwm>;
	};

	display_backlight_pwm {
		compatible = "espressif,esp32-pwm-ledc";
		pin = <&gpio0 42 GPIO_FLAG_NONE>;
		// 32 KHz and higher causes the screen to start dimming again above 80% brightness
		// when moving the brightness slider rapidly from a lower setting to 100% (debug-traced, not a slider bug).
		period-ns = <33333>;
		ledc-timer = <0>;
		ledc-channel = <0>;
	};

	display_backlight {
		compatible = "pwm-backlight";
		// Off by default so display power-on won't show the screen from before the last power loss.
		// The display backlight is turned on during the boot process.
		status = "disabled";
		pwm = <&display_backlight_pwm>;
	};

	spi0 {
		compatible = "espressif,esp32-spi";
		host = <SPI2_HOST>;
		cs-gpios = <&gpio0 21 GPIO_FLAG_NONE>, // SD
		        <&gpio0 38 GPIO_FLAG_NONE>, // Display
		        <&gpio0 36 GPIO_FLAG_NONE>, // Lora
		        <&gpio0 39 GPIO_FLAG_NONE>; // NFC
		pin-mosi = <&gpio0 34 GPIO_FLAG_NONE>;
		pin-miso = <&gpio0 33 GPIO_FLAG_NONE>;
		pin-sclk = <&gpio0 35 GPIO_FLAG_NONE>;
		miso-pull-up;
		
		sdcard@0 {
			compatible = "espressif,esp32-sdspi";
			frequency-khz = <20000>;
		};
		
		display@1 {
			compatible = "sitronix,st7796";
			horizontal-resolution = <480>;
			vertical-resolution = <222>;
			swap-xy;
			mirror-x;
			mirror-y;
			invert-color;
			bgr-order;
			gap-x = <0>;
			gap-y = <49>;
			pixel-clock-hz = <80000000>;
			transaction-queue-depth = <2>;
			pin-dc = <&gpio0 37 GPIO_FLAG_NONE>;
			backlight = <&display_backlight>;
		};
	};

	// ES8311
	// TODO: init via I2C to enable audio playback
	i2s0 {
		compatible = "espressif,esp32-i2s";
		port = <I2S_NUM_0>;
		pin-bclk = <&gpio0 11 GPIO_FLAG_NONE>;
		pin-ws = <&gpio0 18 GPIO_FLAG_NONE>;
		pin-data-out = <&gpio0 45 GPIO_FLAG_NONE>;
		pin-data-in = <&gpio0 17 GPIO_FLAG_NONE>;
		pin-mclk = <&gpio0 10 GPIO_FLAG_NONE>;
	};

	uart_internal: uart0 {
		compatible = "espressif,esp32-uart";
		port = <UART_NUM_0>;
		pin-tx = <&gpio0 12 GPIO_FLAG_NONE>;
		pin-rx = <&gpio0 4 GPIO_FLAG_NONE>;

		gps {
			compatible = "tactility,gps-generic";
			status = "disabled";
			baud-rate = <38400>;
			model = <GPS_MODEL_UBLOX10>;
		};
	};

	uart_external: uart1 {
		compatible = "espressif,esp32-uart";
		port = <UART_NUM_1>;
		pin-tx = <&gpio0 43 GPIO_FLAG_NONE>;
		pin-rx = <&gpio0 44 GPIO_FLAG_NONE>;
	};

	// Encoder wheel next to the display.
	encoder {
		compatible = "lilygo,tpager-encoder";
		pin-a = <&gpio0 40 GPIO_FLAG_NONE>;
		pin-b = <&gpio0 41 GPIO_FLAG_NONE>;
		pin-enter = <&gpio0 7 GPIO_FLAG_NONE>;
	};

	usbdevice0 {
		compatible = "espressif,esp32-usbdevice";

		usbdevicemsc0 {
			compatible = "espressif,esp32-usbdevice-msc";
		};
	};
};
