// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
/*
 * Copyright (C) 2019 Icenowy Zheng <icenowy@aosc.xyz>
 *
 */

/dts-v1/;

#include "sun50i-a64.dtsi"
#include "sun50i-a64-cpu-opp.dtsi"

#include <dt-bindings/gpio/gpio.h>
#include <dt-bindings/input/input.h>
#include <dt-bindings/pwm/pwm.h>

/ {
	model = "PineTab, Development Sample";
	compatible = "pine64,pinetab", "allwinner,sun50i-a64";
	chassis-type = "tablet";

	aliases {
		serial0 = &uart0;
		ethernet0 = &rtl8723cs;
	};

	backlight: backlight {
		compatible = "pwm-backlight";
		pwms = <&pwm 0 50000 PWM_POLARITY_INVERTED>;
		brightness-levels = <0 16 18 20 22 24 26 29 32 35 38 42 46 51 56 62 68 75 83 91 100>;
		default-brightness-level = <15>;
		enable-gpios = <&pio 3 23 GPIO_ACTIVE_HIGH>; /* PD23 */
		power-supply = <&vdd_bl>;
	};

	chosen {
		stdout-path = "serial0:115200n8";
	};

	hdmi-connector {
		compatible = "hdmi-connector";
		type = "c";

		port {
			hdmi_con_in: endpoint {
				remote-endpoint = <&hdmi_out_con>;
			};
		};
	};

	i2c-csi {
		compatible = "i2c-gpio";
		sda-gpios = <&pio 4 13 GPIO_ACTIVE_HIGH>; /* PE13 */
		scl-gpios = <&pio 4 12 GPIO_ACTIVE_HIGH>; /* PE12 */
		i2c-gpio,delay-us = <5>;
		#address-cells = <1>;
		#size-cells = <0>;

		/* Rear camera */
		ov5640: camera@3c {
			compatible = "ovti,ov5640";
			reg = <0x3c>;
			pinctrl-names = "default";
			pinctrl-0 = <&csi_mclk_pin>;
			clocks = <&ccu CLK_CSI_MCLK>;
			clock-names = "xclk";

			AVDD-supply = <&reg_dldo3>;
			DOVDD-supply = <&reg_aldo1>;
			DVDD-supply = <&reg_eldo3>;
			reset-gpios = <&pio 4 14 GPIO_ACTIVE_LOW>; /* PE14 */
			powerdown-gpios = <&pio 4 15 GPIO_ACTIVE_HIGH>; /* PE15 */

			port {
				ov5640_ep: endpoint {
					remote-endpoint = <&csi_ep>;
					bus-width = <8>;
					hsync-active = <1>; /* Active high */
					vsync-active = <0>; /* Active low */
					data-active = <1>;  /* Active high */
					pclk-sample = <1>;  /* Rising */
				};
			};
		};
	};

	speaker_amp: audio-amplifier {
		compatible = "simple-audio-amplifier";
		enable-gpios = <&pio 7 7 GPIO_ACTIVE_HIGH>; /* PH7 */
		sound-name-prefix = "Speaker Amp";
	};

	vdd_bl: regulator {
		compatible = "regulator-fixed";
		regulator-name = "bl-3v3";
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;
		gpio = <&pio 7 6 GPIO_ACTIVE_HIGH>; /* PH6 */
		enable-active-high;
	};

	wifi_pwrseq: wifi_pwrseq {
		compatible = "mmc-pwrseq-simple";
		reset-gpios = <&r_pio 0 2 GPIO_ACTIVE_LOW>; /* PL2 */
		post-power-on-delay-ms = <200>;
	};
};

&codec {
	status = "okay";
};

&codec_analog {
	cpvdd-supply = <&reg_eldo1>;
	status = "okay";
};

&cpu0 {
	cpu-supply = <&reg_dcdc2>;
};

&cpu1 {
	cpu-supply = <&reg_dcdc2>;
};

&cpu2 {
	cpu-supply = <&reg_dcdc2>;
};

&cpu3 {
	cpu-supply = <&reg_dcdc2>;
};

&csi {
	status = "okay";

	port {
		csi_ep: endpoint {
			remote-endpoint = <&ov5640_ep>;
			bus-width = <8>;
			hsync-active = <1>; /* Active high */
			vsync-active = <0>; /* Active low */
			data-active = <1>;  /* Active high */
			pclk-sample = <1>;  /* Rising */
		};
	};
};

&dai {
	status = "okay";
};

&de {
	status = "okay";
};

&dphy {
	status = "okay";
};

&dsi {
	vcc-dsi-supply = <&reg_dldo1>;
	status = "okay";

	panel@0 {
		compatible = "feixin,k101-im2ba02";
		reg = <0>;
		avdd-supply = <&reg_dc1sw>;
		dvdd-supply = <&reg_dc1sw>;
		cvdd-supply = <&reg_ldo_io1>;
		reset-gpios = <&pio 3 24 GPIO_ACTIVE_HIGH>; /* PD24 */
		backlight = <&backlight>;
	};
};

&ehci0 {
	status = "okay";
};

&ehci1 {
	status = "okay";
};

&i2c0 {
	status = "okay";

	touchscreen@5d {
		compatible = "goodix,gt9271";
		reg = <0x5d>;
		interrupt-parent = <&pio>;
		interrupts = <7 4 IRQ_TYPE_LEVEL_HIGH>; /* PH4 */
		irq-gpios = <&pio 7 4 GPIO_ACTIVE_HIGH>; /* PH4 */
		reset-gpios = <&pio 7 8 GPIO_ACTIVE_HIGH>; /* PH8 */
		AVDD28-supply = <&reg_ldo_io1>;
	};
};

&i2c0_pins {
	bias-pull-up;
};

&i2c1 {
	status = "okay";

	/* TODO: add Bochs BMA223 accelerometer here */
};

&lradc {
	vref-supply = <&reg_aldo3>;
	status = "okay";

	button-200 {
		label = "Volume Up";
		linux,code = <KEY_VOLUMEUP>;
		channel = <0>;
		voltage = <200000>;
	};

	button-400 {
		label = "Volume Down";
		linux,code = <KEY_VOLUMEDOWN>;
		channel = <0>;
		voltage = <400000>;
	};
};

&mixer1 {
	status = "okay";
};

&mmc0 {
	pinctrl-names = "default";
	pinctrl-0 = <&mmc0_pins>;
	vmmc-supply = <&reg_dcdc1>;
	cd-gpios = <&pio 5 6 GPIO_ACTIVE_LOW>;
	disable-wp;
	bus-width = <4>;
	status = "okay";
};

&mmc1 {
	pinctrl-names = "default";
	pinctrl-0 = <&mmc1_pins>;
	vmmc-supply = <&reg_dldo4>;
	vqmmc-supply = <&reg_eldo1>;
	mmc-pwrseq = <&wifi_pwrseq>;
	bus-width = <4>;
	non-removable;
	status = "okay";

	rtl8723cs: wifi@1 {
		reg = <1>;
	};
};

&mmc2 {
	pinctrl-names = "default";
	pinctrl-0 = <&mmc2_pins>;
	vmmc-supply = <&reg_dcdc1>;
	vqmmc-supply = <&reg_dcdc1>;
	bus-width = <8>;
	non-removable;
	cap-mmc-hw-reset;
	status = "okay";
};

&ohci0 {
	status = "okay";
};

&pwm {
	status = "okay";
};

&r_rsb {
	status = "okay";

	axp803: pmic@3a3 {
		compatible = "x-powers,axp803";
		reg = <0x3a3>;
		interrupt-parent = <&r_intc>;
		interrupts = <GIC_SPI 32 IRQ_TYPE_LEVEL_LOW>;
		x-powers,drive-vbus-en;
	};
};

#include "axp803.dtsi"

&ac_power_supply {
	status = "okay";
};

&battery_power_supply {
	status = "okay";
};

&reg_aldo1 {
	regulator-min-microvolt = <2800000>;
	regulator-max-microvolt = <2800000>;
	regulator-name = "dovdd-csi";
};

&reg_aldo2 {
	regulator-always-on;
	regulator-min-microvolt = <1800000>;
	regulator-max-microvolt = <3300000>;
	regulator-name = "vcc-pl";
};

&reg_aldo3 {
	regulator-always-on;
	regulator-min-microvolt = <2700000>;
	regulator-max-microvolt = <3300000>;
	regulator-name = "vcc-pll-avcc";
};

&reg_dc1sw {
	regulator-name = "vcc-lcd";
};

&reg_dcdc1 {
	regulator-always-on;
	regulator-min-microvolt = <3300000>;
	regulator-max-microvolt = <3300000>;
	regulator-name = "vcc-3v3";
};

&reg_dcdc2 {
	regulator-always-on;
	regulator-min-microvolt = <1000000>;
	regulator-max-microvolt = <1300000>;
	regulator-name = "vdd-cpux";
};

/* DCDC3 is polyphased with DCDC2 */

&reg_dcdc5 {
	regulator-always-on;
	regulator-min-microvolt = <1200000>;
	regulator-max-microvolt = <1200000>;
	regulator-name = "vcc-dram";
};

&reg_dcdc6 {
	regulator-always-on;
	regulator-min-microvolt = <1100000>;
	regulator-max-microvolt = <1100000>;
	regulator-name = "vdd-sys";
};

&reg_dldo1 {
	regulator-always-on;
	regulator-min-microvolt = <3300000>;
	regulator-max-microvolt = <3300000>;
	regulator-name = "vcc-hdmi-dsi-sensor";
};

&reg_dldo3 {
	regulator-min-microvolt = <2800000>;
	regulator-max-microvolt = <2800000>;
	regulator-name = "avdd-csi";
};

&reg_dldo4 {
	regulator-min-microvolt = <3300000>;
	regulator-max-microvolt = <3300000>;
	regulator-name = "vcc-wifi";
};

&reg_drivevbus {
	regulator-name = "usb0-vbus";
	status = "okay";
};

&reg_eldo1 {
	regulator-always-on;
	regulator-min-microvolt = <1800000>;
	regulator-max-microvolt = <1800000>;
	regulator-name = "cpvdd";
};

&reg_eldo2 {
	regulator-min-microvolt = <1800000>;
	regulator-max-microvolt = <1800000>;
	regulator-name = "vcca-1v8";
};

&reg_eldo3 {
	regulator-min-microvolt = <1800000>;
	regulator-max-microvolt = <1800000>;
	regulator-name = "dvdd-1v8-csi";
};

&reg_fldo1 {
	regulator-min-microvolt = <1200000>;
	regulator-max-microvolt = <1200000>;
	regulator-name = "vcc-1v2-hsic";
};

&reg_fldo2 {
	regulator-always-on;
	regulator-min-microvolt = <1100000>;
	regulator-max-microvolt = <1100000>;
	regulator-name = "vdd-cpus";
};

&reg_ldo_io0 {
	regulator-min-microvolt = <3300000>;
	regulator-max-microvolt = <3300000>;
	regulator-name = "vcc-usb";
	status = "okay";
};

&reg_ldo_io1 {
	regulator-min-microvolt = <3300000>;
	regulator-max-microvolt = <3300000>;
	regulator-enable-ramp-delay = <3500000>;
	regulator-name = "vcc-touchscreen";
	status = "okay";
};

&reg_rtc_ldo {
	regulator-name = "vcc-rtc";
};

&simplefb_hdmi {
	vcc-hdmi-supply = <&reg_dldo1>;
};

&hdmi {
	hvcc-supply = <&reg_dldo1>;
	status = "okay";
};

&hdmi_out {
	hdmi_out_con: endpoint {
		remote-endpoint = <&hdmi_con_in>;
	};
};

&sound {
	status = "okay";
	simple-audio-card,aux-devs = <&codec_analog>, <&speaker_amp>;
	simple-audio-card,widgets = "Microphone", "Internal Microphone Left",
				    "Microphone", "Internal Microphone Right",
				    "Headphone", "Headphone Jack",
				    "Speaker", "Internal Speaker";
	simple-audio-card,routing =
			"Left DAC", "DACL",
			"Right DAC", "DACR",
			"Speaker Amp INL", "LINEOUT",
			"Speaker Amp INR", "LINEOUT",
			"Internal Speaker", "Speaker Amp OUTL",
			"Internal Speaker", "Speaker Amp OUTR",
			"Headphone Jack", "HP",
			"ADCL", "Left ADC",
			"ADCR", "Right ADC",
			"Internal Microphone Left", "MBIAS",
			"MIC1", "Internal Microphone Left",
			"Internal Microphone Right", "HBIAS",
			"MIC2", "Internal Microphone Right";
};

&uart0 {
	pinctrl-names = "default";
	pinctrl-0 = <&uart0_pb_pins>;
	status = "okay";
};

&usb_otg {
	dr_mode = "otg";
	status = "okay";
};

&usb_power_supply {
	status = "okay";
};

&usbphy {
	usb0_id_det-gpios = <&pio 7 9 GPIO_ACTIVE_HIGH>; /* PH9 */
	usb0_vbus_power-supply = <&usb_power_supply>;
	usb0_vbus-supply = <&reg_drivevbus>;
	usb1_vbus-supply = <&reg_ldo_io0>;
	status = "okay";
};