// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
/*
 * Copyright (C) STMicroelectronics 2021 - All Rights Reserved
 * Author: Alexandre Torgue <alexandre.torgue@foss.st.com> for STMicroelectronics.
 */

/dts-v1/;

#include <dt-bindings/gpio/gpio.h>
#include <dt-bindings/input/input.h>
#include <dt-bindings/leds/common.h>
#include <dt-bindings/regulator/st,stm32mp13-regulator.h>
#include "stm32mp135.dtsi"
#include "stm32mp13xf.dtsi"
#include "stm32mp13-pinctrl.dtsi"

/ {
	model = "STMicroelectronics STM32MP135F-DK Discovery Board";
	compatible = "st,stm32mp135f-dk", "st,stm32mp135";

	aliases {
		ethernet0 = &ethernet1;
		serial0 = &uart4;
		serial1 = &usart1;
		serial2 = &uart8;
		serial3 = &usart2;
	};

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

	clocks {
		clk_ext_camera: clk-ext-camera {
			#clock-cells = <0>;
			compatible = "fixed-clock";
			clock-frequency = <24000000>;
		};

		clk_mco1: clk-mco1 {
			#clock-cells = <0>;
			compatible = "fixed-clock";
			clock-frequency = <24000000>;
		};
	};

	memory@c0000000 {
		device_type = "memory";
		reg = <0xc0000000 0x20000000>;
	};

	reserved-memory {
		#address-cells = <1>;
		#size-cells = <1>;
		ranges;

		optee@dd000000 {
			reg = <0xdd000000 0x3000000>;
			no-map;
		};
	};

	gpio-keys {
		compatible = "gpio-keys";

		button-user {
			label = "User-PA13";
			linux,code = <BTN_1>;
			gpios = <&gpioa 13 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)>;
		};
	};

	leds {
		compatible = "gpio-leds";

		led-blue {
			function = LED_FUNCTION_HEARTBEAT;
			color = <LED_COLOR_ID_BLUE>;
			gpios = <&gpioa 14 GPIO_ACTIVE_LOW>;
			linux,default-trigger = "heartbeat";
			default-state = "off";
		};
	};

	panel_backlight: panel-backlight {
		compatible = "gpio-backlight";
		gpios = <&gpioe 12 GPIO_ACTIVE_HIGH>;
		default-on;
		status = "okay";
	};

	panel_rgb: panel-rgb {
		compatible = "rocktech,rk043fn48h";
		enable-gpios = <&gpioi 7 GPIO_ACTIVE_HIGH>;
		backlight = <&panel_backlight>;
		power-supply = <&scmi_v3v3_sw>;
		status = "okay";

		width-mm = <105>;
		height-mm = <67>;

		panel-timing {
			clock-frequency = <10000000>;
			hactive = <480>;
			hback-porch = <43>;
			hfront-porch = <10>;
			hsync-len = <1>;
			hsync-active = <0>;
			vactive = <272>;
			vback-porch = <26>;
			vfront-porch = <4>;
			vsync-len = <10>;
			vsync-active = <0>;
			de-active = <1>;
			pixelclk-active = <1>;
		};

		port {
			panel_in_rgb: endpoint {
				remote-endpoint = <&ltdc_out_rgb>;
			};
		};
	};
};

&adc_1 {
	pinctrl-names = "default";
	pinctrl-0 = <&adc1_usb_cc_pins_a>;
	vdda-supply = <&scmi_vdd_adc>;
	vref-supply = <&scmi_vdd_adc>;
	status = "okay";
	adc1: adc@0 {
		status = "okay";
		/*
		 * Type-C USB_PWR_CC1 & USB_PWR_CC2 on in6 & in12.
		 * Use at least 5 * RC time, e.g. 5 * (Rp + Rd) * C:
		 * 5 * (5.1 + 47kOhms) * 5pF => 1.3us.
		 * Use arbitrary margin here (e.g. 5us).
		 */
		channel@6 {
			reg = <6>;
			st,min-sample-time-ns = <5000>;
		};
		channel@12 {
			reg = <12>;
			st,min-sample-time-ns = <5000>;
		};
	};
};

&crc1 {
	status = "okay";
};

&cryp {
	status = "okay";
};

&dcmipp {
	pinctrl-names = "default", "sleep";
	pinctrl-0 = <&dcmipp_pins_a>;
	pinctrl-1 = <&dcmipp_sleep_pins_a>;
	status = "okay";

	port {
		dcmipp_0: endpoint {
			remote-endpoint = <&mipid02_2>;
			bus-width = <8>;
			hsync-active = <0>;
			vsync-active = <0>;
			pclk-sample = <0>;
		};
	};
};

&ethernet1 {
	status = "okay";
	pinctrl-0 = <&eth1_rmii_pins_a>;
	pinctrl-1 = <&eth1_rmii_sleep_pins_a>;
	pinctrl-names = "default", "sleep";
	phy-mode = "rmii";
	phy-handle = <&phy0_eth1>;

	mdio {
		#address-cells = <1>;
		#size-cells = <0>;
		compatible = "snps,dwmac-mdio";

		phy0_eth1: ethernet-phy@0 {
			compatible = "ethernet-phy-id0007.c131";
			reg = <0>;
			reset-gpios = <&mcp23017 9 GPIO_ACTIVE_LOW>;
			wakeup-source;
		};
	};
};

&i2c1 {
	pinctrl-names = "default", "sleep";
	pinctrl-0 = <&i2c1_pins_a>;
	pinctrl-1 = <&i2c1_sleep_pins_a>;
	i2c-scl-rising-time-ns = <96>;
	i2c-scl-falling-time-ns = <3>;
	clock-frequency = <1000000>;
	status = "okay";
	/* spare dmas for other usage */
	/delete-property/dmas;
	/delete-property/dma-names;

	mcp23017: pinctrl@21 {
		compatible = "microchip,mcp23017";
		reg = <0x21>;
		gpio-controller;
		#gpio-cells = <2>;
		interrupts = <12 IRQ_TYPE_LEVEL_LOW>;
		interrupt-parent = <&gpiog>;
		pinctrl-names = "default";
		pinctrl-0 = <&mcp23017_pins_a>;
		interrupt-controller;
		#interrupt-cells = <2>;
		microchip,irq-mirror;
	};

	typec@53 {
		compatible = "st,stm32g0-typec";
		reg = <0x53>;
		/* Alert pin on PI2 */
		interrupts = <2 IRQ_TYPE_EDGE_FALLING>;
		interrupt-parent = <&gpioi>;
		/* Internal pull-up on PI2 */
		pinctrl-names = "default";
		pinctrl-0 = <&stm32g0_intn_pins_a>;
		firmware-name = "stm32g0-ucsi.mp135f-dk.fw";
		connector {
			compatible = "usb-c-connector";
			label = "USB-C";

			port {
				con_usb_c_g0_ep: endpoint {
					remote-endpoint = <&usbotg_hs_ep>;
				};
			};
		};
	};
};

&i2c5 {
	pinctrl-names = "default", "sleep";
	pinctrl-0 = <&i2c5_pins_a>;
	pinctrl-1 = <&i2c5_sleep_pins_a>;
	i2c-scl-rising-time-ns = <170>;
	i2c-scl-falling-time-ns = <5>;
	clock-frequency = <400000>;
	status = "okay";
	/* spare dmas for other usage */
	/delete-property/dmas;
	/delete-property/dma-names;

	stmipi: csi2rx@14 {
		compatible = "st,st-mipid02";
		reg = <0x14>;
		clocks = <&clk_mco1>;
		clock-names = "xclk";
		VDDE-supply = <&scmi_v1v8_periph>;
		VDDIN-supply = <&scmi_v1v8_periph>;
		reset-gpios = <&mcp23017 2 (GPIO_ACTIVE_LOW | GPIO_PUSH_PULL)>;
		status = "okay";

		ports {
			#address-cells = <1>;
			#size-cells = <0>;
			port@0 {
				reg = <0>;

				mipid02_0: endpoint {
					data-lanes = <1 2>;
					lane-polarities = <0 0 0>;
					remote-endpoint = <&gc2145_ep>;
				};
			};
			port@2 {
				reg = <2>;

				mipid02_2: endpoint {
					bus-width = <8>;
					hsync-active = <0>;
					vsync-active = <0>;
					pclk-sample = <0>;
					remote-endpoint = <&dcmipp_0>;
				};
			};
		};
	};

	gc2145: camera@3c {
		compatible = "galaxycore,gc2145";
		reg = <0x3c>;
		clocks = <&clk_ext_camera>;
		iovdd-supply = <&scmi_v3v3_sw>;
		avdd-supply = <&scmi_v3v3_sw>;
		dvdd-supply = <&scmi_v3v3_sw>;
		powerdown-gpios = <&mcp23017 3 (GPIO_ACTIVE_LOW | GPIO_PUSH_PULL)>;
		reset-gpios = <&mcp23017 4 (GPIO_ACTIVE_LOW | GPIO_PUSH_PULL)>;
		status = "okay";

		port {
			gc2145_ep: endpoint {
				remote-endpoint = <&mipid02_0>;
				data-lanes = <1 2>;
				link-frequencies = /bits/ 64 <120000000 192000000 240000000>;
			};
		};
	};

	goodix: goodix-ts@5d {
		compatible = "goodix,gt911";
		reg = <0x5d>;
		pinctrl-names = "default";
		pinctrl-0 = <&goodix_pins_a>;
		interrupt-parent = <&gpiof>;
		interrupts = <5 IRQ_TYPE_EDGE_FALLING>;
		AVDD28-supply = <&scmi_v3v3_sw>;
		VDDIO-supply = <&scmi_v3v3_sw>;
		touchscreen-size-x = <480>;
		touchscreen-size-y = <272>;
		status = "okay" ;
	};
};

&iwdg2 {
	timeout-sec = <32>;
	status = "okay";
};

&ltdc {
	pinctrl-names = "default", "sleep";
	pinctrl-0 = <&ltdc_pins_a>;
	pinctrl-1 = <&ltdc_sleep_pins_a>;
	status = "okay";

	port {
		ltdc_out_rgb: endpoint {
			remote-endpoint = <&panel_in_rgb>;
		};
	};
};

&rtc {
	status = "okay";
};

&scmi_regu {
	scmi_vdd_adc: regulator@10 {
		reg = <VOLTD_SCMI_STPMIC1_LDO1>;
		regulator-name = "vdd_adc";
	};
	scmi_vdd_usb: regulator@13 {
		reg = <VOLTD_SCMI_STPMIC1_LDO4>;
		regulator-name = "vdd_usb";
	};
	scmi_vdd_sd: regulator@14 {
		reg = <VOLTD_SCMI_STPMIC1_LDO5>;
		regulator-name = "vdd_sd";
	};
	scmi_v1v8_periph: regulator@15 {
		reg = <VOLTD_SCMI_STPMIC1_LDO6>;
		regulator-name = "v1v8_periph";
	};
	scmi_v3v3_sw: regulator@19 {
		reg = <VOLTD_SCMI_STPMIC1_PWR_SW2>;
		regulator-name = "v3v3_sw";
	};
};

&sdmmc1 {
	pinctrl-names = "default", "opendrain", "sleep";
	pinctrl-0 = <&sdmmc1_b4_pins_a &sdmmc1_clk_pins_a>;
	pinctrl-1 = <&sdmmc1_b4_od_pins_a &sdmmc1_clk_pins_a>;
	pinctrl-2 = <&sdmmc1_b4_sleep_pins_a>;
	cd-gpios = <&gpioh 4 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)>;
	disable-wp;
	st,neg-edge;
	bus-width = <4>;
	vmmc-supply = <&scmi_vdd_sd>;
	status = "okay";
};

&spi5 {
	pinctrl-names = "default", "sleep";
	pinctrl-0 = <&spi5_pins_a>;
	pinctrl-1 = <&spi5_sleep_pins_a>;
	status = "disabled";
};

&timers3 {
	/delete-property/dmas;
	/delete-property/dma-names;
	status = "disabled";
	pwm {
		/* PWM output on pin 7 of the expansion connector (CN8.7) using TIM3_CH4 func */
		pinctrl-0 = <&pwm3_pins_a>;
		pinctrl-1 = <&pwm3_sleep_pins_a>;
		pinctrl-names = "default", "sleep";
		status = "okay";
	};
	timer@2 {
		status = "okay";
	};
};

&timers4 {
	/delete-property/dmas;
	/delete-property/dma-names;
	status = "disabled";
	pwm {
		/* PWM output on pin 31 of the expansion connector (CN8.31) using TIM4_CH2 func */
		pinctrl-0 = <&pwm4_pins_a>;
		pinctrl-1 = <&pwm4_sleep_pins_a>;
		pinctrl-names = "default", "sleep";
		status = "okay";
	};
	timer@3 {
		status = "okay";
	};
};

&timers8 {
	/delete-property/dmas;
	/delete-property/dma-names;
	status = "disabled";
	pwm {
		/* PWM output on pin 32 of the expansion connector (CN8.32) using TIM8_CH3 func */
		pinctrl-0 = <&pwm8_pins_a>;
		pinctrl-1 = <&pwm8_sleep_pins_a>;
		pinctrl-names = "default", "sleep";
		status = "okay";
	};
	timer@7 {
		status = "okay";
	};
};

&timers14 {
	status = "disabled";
	pwm {
		/* PWM output on pin 33 of the expansion connector (CN8.33) using TIM14_CH1 func */
		pinctrl-0 = <&pwm14_pins_a>;
		pinctrl-1 = <&pwm14_sleep_pins_a>;
		pinctrl-names = "default", "sleep";
		status = "okay";
	};
	timer@13 {
		status = "okay";
	};
};

&uart4 {
	pinctrl-names = "default", "sleep", "idle";
	pinctrl-0 = <&uart4_pins_a>;
	pinctrl-1 = <&uart4_sleep_pins_a>;
	pinctrl-2 = <&uart4_idle_pins_a>;
	/delete-property/dmas;
	/delete-property/dma-names;
	status = "okay";
};

&uart8 {
	pinctrl-names = "default", "sleep", "idle";
	pinctrl-0 = <&uart8_pins_a>;
	pinctrl-1 = <&uart8_sleep_pins_a>;
	pinctrl-2 = <&uart8_idle_pins_a>;
	/delete-property/dmas;
	/delete-property/dma-names;
	status = "disabled";
};

&usart1 {
	pinctrl-names = "default", "sleep", "idle";
	pinctrl-0 = <&usart1_pins_a>;
	pinctrl-1 = <&usart1_sleep_pins_a>;
	pinctrl-2 = <&usart1_idle_pins_a>;
	uart-has-rtscts;
	status = "disabled";
};

/* Bluetooth */
&usart2 {
	pinctrl-names = "default", "sleep", "idle";
	pinctrl-0 = <&usart2_pins_a>;
	pinctrl-1 = <&usart2_sleep_pins_a>;
	pinctrl-2 = <&usart2_idle_pins_a>;
	uart-has-rtscts;
	status = "okay";
};

&usbh_ehci {
	phys = <&usbphyc_port0>;
	status = "okay";
	#address-cells = <1>;
	#size-cells = <0>;
	/* onboard HUB */
	hub@1 {
		compatible = "usb424,2514";
		reg = <1>;
		vdd-supply = <&scmi_v3v3_sw>;
	};
};

&usbotg_hs {
	phys = <&usbphyc_port1 0>;
	phy-names = "usb2-phy";
	usb-role-switch;
	status = "okay";
	port {
		usbotg_hs_ep: endpoint {
			remote-endpoint = <&con_usb_c_g0_ep>;
		};
	};
};

&usbphyc {
	status = "okay";
};

&usbphyc_port0 {
	phy-supply = <&scmi_vdd_usb>;
	st,current-boost-microamp = <1000>;
	st,decrease-hs-slew-rate;
	st,tune-hs-dc-level = <2>;
	st,enable-hs-rftime-reduction;
	st,trim-hs-current = <11>;
	st,trim-hs-impedance = <2>;
	st,tune-squelch-level = <1>;
	st,enable-hs-rx-gain-eq;
	st,no-hs-ftime-ctrl;
	st,no-lsfs-sc;
};

&usbphyc_port1 {
	phy-supply = <&scmi_vdd_usb>;
	st,current-boost-microamp = <1000>;
	st,decrease-hs-slew-rate;
	st,tune-hs-dc-level = <2>;
	st,enable-hs-rftime-reduction;
	st,trim-hs-current = <11>;
	st,trim-hs-impedance = <2>;
	st,tune-squelch-level = <1>;
	st,enable-hs-rx-gain-eq;
	st,no-hs-ftime-ctrl;
	st,no-lsfs-sc;
};
