// checked against pine64-org/linux 6.6-20231104-22589
//   i.e. the kernel used by postmarketOS
// schematics for reference: <https://files.pine64.org/doc/PinePhonePro/PinephonePro-Schematic-V1.0-20211127.pdf>
//
// modem gets power on these lines:
// VCC3V3_S3
//   - comes from RK818's LDO8; already enable in mainline DTS
// VCC_4G
//   - generated by feeding VCC_SYS through some gates
//     - requires 4G_PWR (RK3399:GPIO4_C7) to be high
//     - requires hardware kill-switch to be enabled
//   - VCC_SYS is surely already enabled (this is the main power rails)
// VCC_4G_5V
//   - generated by feeding VCC5V0_SYS through some gates
//     - requires 4G_5V_ON (RK3399:GPIO1_C7) to be high
//   - VCC5V0_SYS appears to come from RK818 BOOST_1/BOOST_2
//     alternately supplied by Type-C VCONN_12,VCONN_13
//
// modem is connected to RK3399 I2C1, via level shifter
// - pine64-org/linux specs i2c1 as 400kHz, which is precisely what RK818 (the PMIC) specs in its datasheet
//   however, RK818 isn't documented as being on any I2C line in the PPP schematics.
// - I2C1 frequency would be limited by EG25 modem itself
//   and by the level shifter
//   and by the ALC5640 codec (system's)
//   - max I2C clock is 400kHz
//   - max rise: 300ns
//   - max fall: 300ns
//   and by the ALC5616 codec (modem's)
//   and by the AXT530124 MIPI controllers for the front and rear cameras
//   and by the OV8858 front camera (via MIPI)  (maybe?)
//   and by the IMX258 rear camera (via MIPI)   (maybe?)
// - I2C1 is pulled up by VCC1V8_CODEC (4.7kOhm)

// for RK_PC7, other pins
#include <dt-bindings/pinctrl/rockchip.h>
// for GPIO_ACTIVE_HIGH
#include <dt-bindings/gpio/gpio.h>

/dts-v1/;
/plugin/;

/ {
        /* ensure this overlay applies only to the correct board */
        compatible = "pine64,pinephone-pro";
};

&{/} {
        vcc_4g_5v: vcc-4g-5v {
                compatible = "regulator-fixed";
                enable-active-high;
                gpio = <&gpio1 RK_PC7 GPIO_ACTIVE_HIGH>;
                pinctrl-names = "default";
                pinctrl-0 = <&vcc_4g_5v_en>;
                regulator-name = "vcc_4g_5v";
                regulator-min-microvolt = <5000000>;
                regulator-max-microvolt = <5000000>;
                vin-supply = <&vcc5v0_sys>;
                regulator-always-on;
        };

        vcc_4g: vcc-4g {
                compatible = "regulator-fixed";
                enable-active-high;
                gpio = <&gpio4 RK_PC7 GPIO_ACTIVE_HIGH>;
                pinctrl-names = "default";
                pinctrl-0 = <&vcc_4g_en>;
                regulator-name = "vcc_4g";
                // 3.8V is copied from pine64-org/linux.
                // i think vcc_4g should match whatever vcc_sys is set to (it's input).
                // and vcc_sys would seem to be the battery voltage?
                //   (even when powered via USB)
                regulator-min-microvolt = <3800000>;
                regulator-max-microvolt = <3800000>;
                vin-supply = <&vcc_sys>;
                regulator-always-on;
        };

        vcc5v0_sys: vcc5v0-host-regulator {
                compatible = "regulator-fixed";
                regulator-name = "vcc5v0_sys";
                regulator-min-microvolt = <5000000>;
                regulator-max-microvolt = <5000000>;
                regulator-always-on;
                regulator-boot-on;
                vin-supply = <&boost_otg>;

                regulator-state-mem {
                        regulator-on-in-suspend;
                };
        };
};

&pinctrl {
        modem {
                vcc_4g_5v_en: vcc-4g-5v-en-pin {
                        rockchip,pins = <1 RK_PC7 RK_FUNC_GPIO &pcfg_pull_none>;
                };

                vcc_4g_en: vcc-4g-en-pin {
                        rockchip,pins = <4 RK_PC7 RK_FUNC_GPIO &pcfg_pull_none>;
                };
        };
};

// rk3399-pinephone-pro-sound sets this 'okay', but do that here also to avoid implicit dependencies.
// TODO: how can the modem require i2c1 if i'm not hanging any devices off of it here??
&i2c1 {
        status = "okay";
        // N.B.: these values are duplicated in rk3399-pinephone-pro-sound.dtso
        clock-frequency = <400000>;
        i2c-scl-rising-time-ns = <300>;
        i2c-scl-falling-time-ns = <15>;
};

&rk818 {
        regulators {
                boost_otg: DCDC_BOOST {
                        regulator-name = "boost_otg";
                        regulator-always-on;
                        regulator-boot-on;
                        regulator-min-microvolt = <5000000>;
                        regulator-max-microvolt = <5000000>;
                        regulator-state-mem {
                                regulator-on-in-suspend;
                        };
                };

                // probably not strictly required here
                otg_switch: OTG_SWITCH {
                        regulator-name = "otg_switch";
                };
        };
};

// modem is connected to RK3399 via UART3
&uart3 {
        status = "okay";
};

// taken from megi's PREF377-arm64-dts-rk3399-pinephone-pro-Add-modem-support.patch
// the modem communicates with RK3399 over USB2.0 PHY 1  (e.g. /dev/bus/usb).
// that PHY appears to be exclusively for the modem; the type-c stuff is NC.
&u2phy1 {
        status = "okay";
};
&u2phy1_host {
        status = "okay";
        phy-supply = <&vcc5v0_sys>;
};
// USB 2.0 transfers
&usb_host1_ehci {
        status = "okay";
};
// USB < 2.0 transfers
&usb_host1_ohci {
        status = "okay";
};

// TODO: megi includes this in the USB supply path.
//
// /*
//  * This is not a regulator. GPIO0_A6 is used to force enable VCC_SYS <->
//  * VBAT+ Q7 switch that helps boost the RK818's power path's current carrying
//  * capacity when operating the phone from the battery in case VBUS_TYPEC
//  * has 5V provided by the phone itself and not by the external power
//  * supply. (this happens in USB host mode)
//  *
//  * GPIO0_A6 low: Q7 is enabled if there's 0V on VBUS_TYPEC (phone is not
//  *               powered from external power supply)
//  * GPIO0_A6 high: Q7 is enabled no matter what
//  *
//  * GPIO0_A6 must be high when the phone is providing VBUS_TYPEC power.
//  * This keeps the power path boost enabled for all cases when the phone
//  * runs from the battery.
//  *
//  * In other words, GPIO0_A6 is used to override the automatic disabling
//  * of Q7 when VBUS_TYPEC has 5V in USB host mode.
//  */
// vcc5v0_typec: vcc5v0-typec-regulator {
//         compatible = "regulator-fixed";
//         enable-active-high;
//         gpio = <&gpio0 RK_PA6 GPIO_ACTIVE_HIGH>;
//         pinctrl-names = "default";
//         pinctrl-0 = <&vcc5v0_typec_en>;
//         regulator-min-microvolt = <5000000>;
//         regulator-max-microvolt = <5000000>;
//         regulator-name = "vcc5v0_typec";
//         vin-supply = <&boost_otg>;
// };
// &rk818 { regulator { +▷······▷·······usb-supply = <&vcc5v0_typec>; }; };
