// SPDX-License-Identifier: GPL-2.0-or-later OR MIT
/*
 * Copyright 2024 Toradex
 *
 * Common dtsi for Verdin IMX8MM SoM on Ivy carrier board
 *
 * https://www.toradex.com/computer-on-modules/verdin-arm-family/nxp-imx-8m-mini-nano
 * https://www.toradex.com/products/carrier-board/ivy-carrier-board
 */

#include <dt-bindings/mux/mux.h>
#include <dt-bindings/leds/common.h>

/ {
	/* AIN1 Voltage w/o AIN1_MODE gpio control */
	ain1_voltage_unmanaged: voltage-divider-ain1 {
		compatible = "voltage-divider";
		#io-channel-cells = <1>;
		io-channels = <&ivy_adc1 0>;
		full-ohms = <19>;
		output-ohms = <1>;
	};

	/* AIN1 Current w/o AIN1_MODE gpio control */
	ain1_current_unmanaged: current-sense-shunt-ain1 {
		compatible = "current-sense-shunt";
		#io-channel-cells = <0>;
		io-channels = <&ivy_adc1 1>;
		shunt-resistor-micro-ohms = <100000000>;
	};

	/* AIN1_MODE - SODIMM 216 */
	ain1_mode_mux_ctrl: mux-controller-0 {
		compatible = "gpio-mux";
		pinctrl-names = "default";
		pinctrl-0 = <&pinctrl_gpio5>;
		#mux-control-cells = <0>;
		mux-gpios = <&gpio1 0 GPIO_ACTIVE_HIGH>;
	};

	ain1-voltage {
		compatible = "io-channel-mux";
		channels = "ain1_voltage", "";
		io-channels = <&ain1_voltage_unmanaged 0>;
		io-channel-names = "parent";
		mux-controls = <&ain1_mode_mux_ctrl>;
		settle-time-us = <1000>;
	};

	ain1-current {
		compatible = "io-channel-mux";
		channels = "", "ain1_current";
		io-channels = <&ain1_current_unmanaged>;
		io-channel-names = "parent";
		mux-controls = <&ain1_mode_mux_ctrl>;
		settle-time-us = <1000>;
	};

	/* AIN2 Voltage w/o AIN2_MODE gpio control */
	ain2_voltage_unmanaged: voltage-divider-ain2 {
		compatible = "voltage-divider";
		#io-channel-cells = <1>;
		io-channels = <&ivy_adc2 0>;
		full-ohms = <19>;
		output-ohms = <1>;
	};

	/* AIN2 Current w/o AIN2_MODE gpio control */
	ain2_current_unmanaged: current-sense-shunt-ain2 {
		compatible = "current-sense-shunt";
		#io-channel-cells = <0>;
		io-channels = <&ivy_adc2 1>;
		shunt-resistor-micro-ohms = <100000000>;
	};

	/* AIN2_MODE - SODIMM 218 */
	ain2_mode_mux_ctrl: mux-controller-1 {
		compatible = "gpio-mux";
		pinctrl-names = "default";
		pinctrl-0 = <&pinctrl_gpio6>;
		#mux-control-cells = <0>;
		mux-gpios = <&gpio1 11 GPIO_ACTIVE_HIGH>;
	};

	ain2-voltage {
		compatible = "io-channel-mux";
		channels = "ain2_voltage", "";
		io-channels = <&ain2_voltage_unmanaged 0>;
		io-channel-names = "parent";
		mux-controls = <&ain2_mode_mux_ctrl>;
		settle-time-us = <1000>;
	};

	ain2-current {
		compatible = "io-channel-mux";
		channels = "", "ain2_current";
		io-channels = <&ain2_current_unmanaged>;
		io-channel-names = "parent";
		mux-controls = <&ain2_mode_mux_ctrl>;
		settle-time-us = <1000>;
	};

	leds {
		compatible = "gpio-leds";
		pinctrl-names = "default";
		pinctrl-0 = <&pinctrl_ivy_leds>;

		/* D7 Blue - SODIMM 30 - LEDs.GPIO1 */
		led-0 {
			color = <LED_COLOR_ID_BLUE>;
			default-state = "off";
			function = LED_FUNCTION_STATUS;
			function-enumerator = <1>;
			gpios = <&gpio4 25 GPIO_ACTIVE_HIGH>;
		};

		/* D7 Green - SODIMM 32 - LEDs.GPIO2 */
		led-1 {
			color = <LED_COLOR_ID_GREEN>;
			default-state = "off";
			function = LED_FUNCTION_STATUS;
			function-enumerator = <1>;
			gpios = <&gpio4 24 GPIO_ACTIVE_HIGH>;
		};

		/* D7 Red - SODIMM 34 - LEDs.GPIO3 */
		led-2 {
			color = <LED_COLOR_ID_RED>;
			default-state = "off";
			function = LED_FUNCTION_STATUS;
			function-enumerator = <1>;
			gpios = <&gpio4 26 GPIO_ACTIVE_HIGH>;
		};

		/* D8 Blue - SODIMM 36 - LEDs.GPIO4 */
		led-3 {
			color = <LED_COLOR_ID_BLUE>;
			default-state = "off";
			function = LED_FUNCTION_STATUS;
			function-enumerator = <2>;
			gpios = <&gpio4 23 GPIO_ACTIVE_HIGH>;
		};

		/* D8 Green - SODIMM 54 - LEDs.GPIO5 */
		led-4 {
			color = <LED_COLOR_ID_GREEN>;
			default-state = "off";
			function = LED_FUNCTION_STATUS;
			function-enumerator = <2>;
			gpios = <&gpio3 1 GPIO_ACTIVE_HIGH>;
		};

		/* D8 Red - SODIMM 44 - LEDs.GPIO6 */
		led-5 {
			color = <LED_COLOR_ID_RED>;
			default-state = "off";
			function = LED_FUNCTION_STATUS;
			function-enumerator = <2>;
			gpios = <&gpio3 22 GPIO_ACTIVE_HIGH>;
		};

		/* D9 Blue - SODIMM 46 - LEDs.GPIO7 */
		led-6 {
			color = <LED_COLOR_ID_BLUE>;
			default-state = "off";
			function = LED_FUNCTION_STATUS;
			function-enumerator = <3>;
			gpios = <&gpio3 24 GPIO_ACTIVE_HIGH>;
		};

		/* D9 Red - SODIMM 48 - LEDs.GPIO8 */
		led-7 {
			color = <LED_COLOR_ID_RED>;
			default-state = "off";
			function = LED_FUNCTION_STATUS;
			function-enumerator = <3>;
			gpios = <&gpio3 21 GPIO_ACTIVE_HIGH>;
		};
	};

	reg_3v2_ain1: regulator-3v2-ain1 {
		compatible = "regulator-fixed";
		regulator-max-microvolt = <3200000>;
		regulator-min-microvolt = <3200000>;
		regulator-name = "+3V2_AIN1";
	};

	reg_3v2_ain2: regulator-3v2-ain2 {
		compatible = "regulator-fixed";
		regulator-max-microvolt = <3200000>;
		regulator-min-microvolt = <3200000>;
		regulator-name = "+3V2_AIN2";
	};

	/* Ivy Power Supply Input Voltage */
	ivy-input-voltage {
		compatible = "voltage-divider";
		/* Verdin ADC_1 */
		io-channels = <&verdin_som_adc 7>;
		full-ohms = <204700>; /* 200k + 4.7k */
		output-ohms = <4700>;
	};

	ivy-5v-voltage {
		compatible = "voltage-divider";
		/* Verdin ADC_2 */
		io-channels = <&verdin_som_adc 6>;
		full-ohms = <39000>; /* 27k + 12k */
		output-ohms = <12000>;
	};

	ivy-3v3-voltage {
		compatible = "voltage-divider";
		/* Verdin ADC_3 */
		io-channels = <&verdin_som_adc 5>;
		full-ohms = <54000>; /* 27k + 27k */
		output-ohms = <27000>;
	};

	ivy-1v8-voltage {
		compatible = "voltage-divider";
		/* Verdin ADC_4 */
		io-channels = <&verdin_som_adc 4>;
		full-ohms = <39000>; /* 12k + 27k */
		output-ohms = <27000>;
	};
};

/* Verdin SPI_1 */
&ecspi2 {
	pinctrl-0 = <&pinctrl_ecspi2>,
		    <&pinctrl_gpio1>,
		    <&pinctrl_gpio4>;
	cs-gpios = <&gpio5 13 GPIO_ACTIVE_LOW>,
		   <&gpio3 4 GPIO_ACTIVE_LOW>,
		   <&gpio5 27 GPIO_ACTIVE_LOW>;
	status = "okay";

	tpm@1 {
		compatible = "infineon,slb9670", "tcg,tpm_tis-spi";
		reg = <1>;
		spi-max-frequency = <18500000>;
	};

	fram@2 {
		compatible = "fujitsu,mb85rs256", "atmel,at25";
		reg = <2>;
		address-width = <16>;
		size = <32768>;
		spi-max-frequency = <33000000>;
		pagesize = <1>;
	};
};

/* EEPROM on Ivy */
&eeprom_carrier_board {
	status = "okay";
};

/* Verdin ETH_1 */
&fec1 {
	status = "okay";
};

&gpio3 {
	gpio-line-names =
		"", /* 0 */
		"",
		"REL3", /* SODIMM 64 */
		"",
		"",
		"",
		"DIG_1", /* SODIMM 56 */
		"DIG_2", /* SODIMM 58 */
		"REL1",  /* SODIMM 60 */
		"REL2",  /* SODIMM 62 */
		"", /* 10 */
		"",
		"",
		"",
		"REL4",  /* SODIMM 66 */
		"",
		"",
		"",
		"",
		"",
		"", /* 20 */
		"",
		"",
		"",
		"",
		"";
};

&gpio5 {
	gpio-line-names =
		"", /* 0 */
		"",
		"",
		"",
		"",
		"GPIO2", /* Verdin GPIO_2 - SODIMM 208 */
		"",
		"",
		"",
		"",
		"", /* 10 */
		"",
		"",
		"",
		"",
		"",
		"",
		"",
		"",
		"",
		"", /* 20 */
		"",
		"",
		"",
		"",
		"",
		"GPIO3", /* Verdin GPIO_3 - SODIMM 210 */
		"",
		"",
		"";
};

/* Temperature sensor on Ivy */
&hwmon_temp {
	compatible = "ti,tmp1075";
	status = "okay";
};

/* Verdin I2C_4 CSI */
&i2c3 {
	status = "okay";

	ivy_adc1: adc@40 {
		compatible = "ti,ads1119";
		reg = <0x40>;
		pinctrl-names = "default";
		pinctrl-0 = <&pinctrl_gpio7>;
		interrupt-parent = <&gpio1>;
		interrupts = <8 IRQ_TYPE_EDGE_FALLING>;
		avdd-supply = <&reg_3v2_ain1>;
		dvdd-supply = <&reg_3v2_ain1>;
		vref-supply = <&reg_3v2_ain1>;
		#address-cells = <1>;
		#io-channel-cells = <1>;
		#size-cells = <0>;

		/* AIN1 0-33V Voltage Input */
		channel@0 {
			reg = <0>;
			diff-channels = <0 1>;
		};

		/* AIN1 0-20mA Current Input */
		channel@1 {
			reg = <1>;
			diff-channels = <2 3>;
		};
	};

	ivy_adc2: adc@41 {
		compatible = "ti,ads1119";
		reg = <0x41>;
		pinctrl-names = "default";
		pinctrl-0 = <&pinctrl_gpio8>;
		interrupt-parent = <&gpio1>;
		interrupts = <9 IRQ_TYPE_EDGE_FALLING>;
		avdd-supply = <&reg_3v2_ain2>;
		dvdd-supply = <&reg_3v2_ain2>;
		vref-supply = <&reg_3v2_ain2>;
		#address-cells = <1>;
		#io-channel-cells = <1>;
		#size-cells = <0>;

		/* AIN2 0-33V Voltage Input */
		channel@0 {
			reg = <0>;
			diff-channels = <0 1>;
		};

		/* AIN2 0-20mA Current Input */
		channel@1 {
			reg = <1>;
			diff-channels = <2 3>;
		};
	};
};

/* Verdin I2C_1 */
&i2c4 {
	status = "okay";
};

/* Verdin PCIE_1 */
&pcie0 {
	status = "okay";
};

&pcie_phy {
	status = "okay";
};

/* Verdin UART_3 */
&uart1 {
	status = "okay";
};

/* Verdin UART_1 */
&uart2 {
	status = "okay";
};

/* Verdin UART_2 */
&uart3 {
	linux,rs485-enabled-at-boot-time;
	rs485-rx-during-tx;
	status = "okay";
};

/* Verdin USB_1*/
&usbotg1 {
	status = "okay";
};

/* Verdin USB_2 */
&usbotg2 {
	status = "okay";
};

/* Verdin SD_1 */
&usdhc2 {
	status = "okay";
};

&iomuxc {

	pinctrl-names = "default";
	pinctrl-0 = <&pinctrl_gpio2>, <&pinctrl_gpio3>,
		    <&pinctrl_ivy_dig_inputs>, <&pinctrl_ivy_relays>;

	pinctrl_ivy_dig_inputs: ivydiginputsgrp {
		fsl,pins =
			<MX8MM_IOMUXC_NAND_DATA00_GPIO3_IO6	0x96>, /* SODIMM 56 */
			<MX8MM_IOMUXC_NAND_DATA01_GPIO3_IO7	0x96>; /* SODIMM 58 */
	};

	pinctrl_ivy_leds: ivyledsgrp {
		fsl,pins =
			<MX8MM_IOMUXC_SAI2_TXC_GPIO4_IO25	0x16>, /* SODIMM 30 */
			<MX8MM_IOMUXC_SAI2_TXFS_GPIO4_IO24	0x16>, /* SODIMM 32 */
			<MX8MM_IOMUXC_SAI2_TXD0_GPIO4_IO26	0x16>, /* SODIMM 34 */
			<MX8MM_IOMUXC_SAI2_RXD0_GPIO4_IO23	0x16>, /* SODIMM 36 */
			<MX8MM_IOMUXC_SAI5_RXD1_GPIO3_IO22	0x16>, /* SODIMM 44 */
			<MX8MM_IOMUXC_SAI5_RXD3_GPIO3_IO24	0x16>, /* SODIMM 46 */
			<MX8MM_IOMUXC_SAI5_RXD0_GPIO3_IO21	0x16>, /* SODIMM 48 */
			<MX8MM_IOMUXC_NAND_CE0_B_GPIO3_IO1	0x16>; /* SODIMM 54 */
	};

	pinctrl_ivy_relays: ivyrelaysgrp {
		fsl,pins =
			<MX8MM_IOMUXC_NAND_DATA02_GPIO3_IO8	0x16>, /* SODIMM 60 */
			<MX8MM_IOMUXC_NAND_DATA03_GPIO3_IO9	0x16>, /* SODIMM 62 */
			<MX8MM_IOMUXC_NAND_CE1_B_GPIO3_IO2	0x16>, /* SODIMM 64 */
			<MX8MM_IOMUXC_NAND_DQS_GPIO3_IO14	0x16>; /* SODIMM 66 */
	};
};
