/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_master.h>
#include <tactility/bindings/esp32_spi.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 <bindings/cst328.h>
#include <bindings/gdeq031t10.h>
#include <bindings/tca8418.h>
#include <bindings/bq25896.h>

// Reference for V1.0 (not V1.1!): https://github.com/Xinyuan-LilyGO/T-Deck-Pro/tree/HD-V1-250326
// lib/TDeckMaxBoard/src/TDeckMaxBoard.h, docs/pinmap.md
/ {
	compatible = "root";
	model = "LilyGO T-Deck Pro";

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

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

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

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

		touch {
			compatible = "hynitron,cst328";
			reg = <0x1A>;
			pin-reset = <&gpio0 45 GPIO_FLAG_NONE>;
		};

		// TCA8418 4x10 matrix scanner. Keymaps are written in the vendor's row/column
		// orientation (Xinyuan-LilyGO/T-Deck-MAX examples/factory/peri_keypad.cpp); the
		// TCA8418 columns are wired in reverse of that order, hence reverse-columns.
		// ALT (shift/uppercase) is row 2 col 0; SYM (symbol layer) is row 3 col 8.
		keyboard {
			compatible = "ti,tca8418";
			reg = <0x34>;
			rows = <4>;
			columns = <10>;
			reverse-columns;
			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 8    // a s d f g h j k l BACKSPACE
				0 122 120 99 118 98 110 109 36 10       //   z x c v b n m $ ENTER
				0 0 0 0 0 11 48 32 0 9                  //         PREV 0 SPC   NEXT
			];
			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 8  // A S D F G H J K L BACKSPACE
				0 90 88 67 86 66 78 77 36 10  //   Z X C V B N M $ ENTER
				0 0 0 0 0 11 48 32 0 9        //         PREV 0 SPC   NEXT
			];
			keymap-sy = [
				49 50 51 52 53 54 55 56 57 48 // 1 2 3 4 5 6 7 8 9 0
				64 35 43 45 42 47 40 41 95 8  // @ # + - * / ( ) _ BACKSPACE
				0 33 63 59 58 39 34 44 46 10  //   ! ? ; : ' " , . ENTER
				0 0 0 0 0 11 48 32 0 9        //         PREV 0 SPC   NEXT
			];
			shift-row = <2>;
			shift-col = <0>;
			sym-row = <3>;
			sym-col = <8>;
		};

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

	// Keyboard backlight (LED_PWM), GPIO42. 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 touch driver's dropped bezel
	// keys). 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 42 GPIO_FLAG_NONE>;
		period-ns = <33333>;
		ledc-timer = <0>;
		ledc-channel = <0>;
	};

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

	spi0 {
		compatible = "espressif,esp32-spi";
		host = <SPI2_HOST>;
		cs-gpios = <&gpio0 34 GPIO_FLAG_NONE>, // 0: EPD display
		           <&gpio0 48 GPIO_FLAG_NONE>, // 1: SD card
		           <&gpio0 3 GPIO_FLAG_NONE>;  // 2: LoRa radio (SX1262, not wired up yet)
		pin-mosi = <&gpio0 33 GPIO_FLAG_NONE>;
		pin-miso = <&gpio0 47 GPIO_FLAG_NONE>;
		pin-sclk = <&gpio0 36 GPIO_FLAG_NONE>;
		// The EPD pushes its whole 9600-byte framebuffer in one SPI write, which
		// exceeds the default ~4 KB transfer limit. Raise the bus limit to fit it.
		max-transfer-size = <65536>;

		display@0 {
			compatible = "gooddisplay,gdeq031t10";
			pin-dc = <&gpio0 35 GPIO_FLAG_NONE>;
			pin-busy = <&gpio0 37 GPIO_FLAG_NONE>;
			clock-speed-hz = <10000000>;
			// Default to a fast (~1s) refresh for the automatic ghost-clears so the
			// full-screen flash they cause is as short as possible.
			refresh-mode = <GDEQ031T10_REFRESH_FAST>;
		};

		sdcard@1 {
			compatible = "espressif,esp32-sdspi";
			status = "disabled";
			frequency-khz = <20000>;
		};
	};

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

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