/**
 * displayboard.h
 *
 * Pin defines and clock definitions for the board hardware.
 * 
 * Copyright (c) 2022 Bigscreen, Inc.
 */


#ifndef DISPLAYBOARD_H_
#define DISPLAYBOARD_H_

#include <conf_board.h>
#include <compiler.h>

// Name string macro 
#define BOARD_NAME                "DISPLAYBOARD V0.4.2"

// Resonator definitions
#define BOARD_FREQ_SLCK_XTAL      (32768U)
#define BOARD_FREQ_SLCK_BYPASS    (32768U)
#define BOARD_FREQ_MAINCK_XTAL    0 /* Not Mounted */
#define BOARD_FREQ_MAINCK_BYPASS  0 /* Not Mounted */
#define BOARD_MCK                 CHIP_FREQ_CPU_MAX
// TBD startup time needs to be adjusted according to measurements 
#define BOARD_OSC_STARTUP_US      15625

#define DEFAULT_FAN_SPEED		(80)

/*** External board defines - the Linkbox HPD proprietary signaling ***/
// Asserting the CC wire 1.24k pulldowns changes the effective
// pulldown resistance from 5.1k to 1.0k. This signals to the 
// linkbox that Hot Plug Detect should be set HIGH on the DisplayPort
// input to the PC.
#define HPD_DISABLED			(false)
#define HPD_ENABLED				(true)

// Pin definitions for Beyond Displayboard v0.4.1

#if defined(CONF_BOARD_UART_CONSOLE)
#define CONSOLE_UART               USART3
#define CONSOLE_UART_ID            ID_FLEXCOM3
/** USART3 pins (TXD3 and RXD3) definitions, PA3,4. */
#define PINS_USART3			(PIO_PA3A_TXD3 | PIO_PA4A_RXD3)
#define PINS_USART3_FLAGS	(IOPORT_MODE_MUX_A)

#define PINS_USART3_PORT   IOPORT_PIOA
#define PINS_USART3_MASK   (PIO_PA3A_TXD3 | PIO_PA4A_RXD3)
#define PINS_USART3_PIO    PIOA
#define PINS_USART3_ID     ID_PIOA
#define PINS_USART3_TYPE   PIO_PERIPH_A
#define PINS_USART3_ATTR   PIO_DEFAULT
#endif

// FAN Control
#define FAN_PIO				PIOA
#define PIN_FAN_FREQ		PIO_PA0_IDX
#define PINMODE_FAN_FREQ	IOPORT_MODE_MUX_B
#define PIN_FAN_PWM			PIO_PA16_IDX
#define PINMODE_FAN_PWM		IOPORT_MODE_MUX_B

// Aux USB Power Control
#define AUX_USB_CTRL_PIO	PIOA
#define PIN_AUX_USB_EN		PIO_PA1_IDX
#define PIN_AUX_USB_FAULT	PIO_PA2_IDX

// UART for debugging
#define UART_PIO			PIOA
#define PIN_UART_TX			PIO_PA3_IDX
#define PINMODE_UART_TX		IOPORT_MODE_MUX_A
#define PIN_UART_RX			PIO_PA4_IDX
#define PINMODE_UART_RX		IOPORT_MODE_MUX_A

// Tracking GPIO
#define TRACK_PIO			PIOA
#define PIN_TRACK_GPIO2		PIO_PA5_IDX
#define PIN_TRACK_GPIO1		PIO_PA18_IDX

// Power supply enables
#define EN_1V_PIO			PIOA
#define PIN_EN_1V			PIO_PA6_IDX
#define EN_VOLEDn_PIO		PIOA
#define PIN_EN_VOLEDn		PIO_PA24_IDX
#define EN_VOLEDp_PIO		PIOB
#define PIN_EN_VOLEDp		PIO_PB0_IDX
#define EN_1V8_PIO			PIOB
#define PIN_EN_1V8			PIO_PB1_IDX

// PDM mic
#define PDM_PIO				PIOA
#define PIN_PDM_DAT			PIO_PA9_IDX
#define PINMODE_PDM_DAT		IOPORT_MODE_MUX_B
#define PIN_PDM_CLK			PIO_PA10_IDX
#define PINMODE_PDM_CLK		IOPORT_MODE_MUX_B

// USB C Power Delivery
#define USBCC_PIO			PIOA
#define PIN_USBCC_EN		PIO_PA11_IDX
#define PIN_USBC_ADC_CC1	PIO_PA19_IDX
#define USBC_ADC_CC1_CH		ADC_CHANNEL_2
#define PIN_USBC_ADC_CC2	PIO_PA20_IDX
#define USBC_ADC_CC2_CH		ADC_CHANNEL_3

// Prox Sensor logic level GPIO
#define EXP_IRQ_PIO			PIOA
#define PIN_EXP_IRQ			PIO_PA12_IDX

// DDIC logic level GPIO
#define DDIC_LOGIC_PIO		PIOA
#define PIN_DDIC_GPIO7		PIO_PA13_IDX
#define PIN_DDIC_RST		PIO_PA14_IDX
#define PIN_DDIC_HPD		PIO_PA15_IDX
#define DDIC_GPIO4_PIO		PIOB
#define PIN_DDIC_GPIO4		PIO_PB8_IDX

// Temperature sensor (DDIC)
#define TEMP_SENSE_PIO		PIOA
#define PIN_TEMP_SENSE		PIO_PA17_IDX
#define TEMP_SENSE_ADC_CH	ADC_CHANNEL_0

// Two I2C buses exist, named "DDIC" and "MCU"
// DDIC (display driver integrated circuit) is a legacy name, 
// in reality the chip we're talking to is a bridge IC and
// the DDICs are embedded in the oled panels
// 
// Devices on DDIC_I2C:
//  - Synaptics VXR7200 DisplayPort to MIPI VR Bridge
//  - SeeYa SY103WAM01 (left panel)
//  - Diodes PI3USB31532 USB-C switch
//
// Devices on MCU_I2C:
//  - SeeYa SY103WAM01 (right panel)
//  - Microchip USB3803 USB Hub
//  - ams TMD2635 IR proximity sensor
//  - Awinic AW2033 RBG LED controller

// I2C for the DDIC
#define DDIC_I2C_PIO		PIOB
#define DDIC_I2C_PIO_ID     ID_PIOB
#define PIN_DDIC_I2C_SCL	PIO_PB2_IDX
#define PINMODE_DDIC_I2C_SCL	IOPORT_MODE_MUX_A
#define PIN_DDIC_I2C_SDA	PIO_PB3_IDX
#define PINMODE_DDIC_I2C_SDA	IOPORT_MODE_MUX_A
#define DDIC_I2C_FLEXCOM    FLEXCOM1

// Prox sense / USB hub / RGB LED I2C
#define HUB_I2C_PIO			PIOB
#define PIN_HUB_I2C_SCL		PIO_PB11_IDX
#define PINMODE_HUB_I2C_SDA	IOPORT_MODE_MUX_A
#define PIN_HUB_I2C_SDA		PIO_PB10_IDX
#define PINMODE_HUB_I2C_SCL	IOPORT_MODE_MUX_A
#define HUB_I2C_FLEXCOM     FLEXCOM4

// USB Hub logic level GPIO
#define USBHUB_LOGIC_PIO	PIOB
#define PIN_USBHUB_INT		PIO_PB4_IDX
#define PIN_USBHUB_RESET	PIO_PB5_IDX
#define PIN_USBHUB_CLKEN	PIO_PB9_IDX

// Spare pin (for setting high impedance with weak pullup)
#define SPARE_PIN_PIO		PIOA
#define PIN_SPARE			PIO_PA23_IDX

// Display panel logic level GPIO
#define OLED_PIO			PIOA
#define PIN_OLED_RESX		PIO_PA8_IDX

void displayboard_system_board_init(void);

#endif /* DISPLAYBOARD_H_ */