Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
351 changes: 351 additions & 0 deletions device/tegu/USB-HOST-PLAN.md

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions device/tegu/overlays/usb-host-pmic.dtso
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// SPDX-License-Identifier: GPL-2.0-only
/dts-v1/;
/plugin/;

/*
* Pixel 9a bring-up only; in no device's default DTB_OVERLAYS
* (USB-HOST-PLAN H2b): the MAX77779 PMIC block at 0x66 on hsi2c6
* (maxim,max77779-pmic, linux-omarchy-mobile#139) with the chip's
* interrupt line on gpa8-0, pinned as stock's if_pmic_irq (EINT, pull-up),
* and the RT9470 boost's interrupt on its SGPIO6 with a pull-up, as stock's
* zumapro-buck-charge-rt9471.dtsi has it (rt,intr_gpio). The PMIC node and
* the pin are those of linux-omarchy-mobile#127 at 5ada9ab0f, as an overlay
* on the palmtop's gn3 DTB (which has the hsi2c6, pinctrl_alive, gpa8 and
* otg_boost labels). Not the boost's enable-gpios (stock's rt,ceb_gpio on
* SGPIO7): that pin is the RT9470's charge enable, and driving it with
* nothing managing the charger could start it charging; the line stays
* with #127. Numbers: IRQ_TYPE_LEVEL_LOW 8, GPIO_ACTIVE_LOW 1,
* GPIO_PULL_UP 16.
*/
&hsi2c6 {
max77779_pmic: pmic@66 {
compatible = "maxim,max77779-pmic";
reg = <0x66>;
interrupt-parent = <&gpa8>;
interrupts = <0 8>;
pinctrl-names = "default";
pinctrl-0 = <&if_pmic_irq>;
gpio-controller;
#gpio-cells = <2>;
interrupt-controller;
#interrupt-cells = <2>;
};
};

&pinctrl_alive {
if_pmic_irq: if-pmic-irq-pins {
samsung,pins = "gpa8-0";
samsung,pin-function = <0x0f>;
samsung,pin-pud = <0x03>;
samsung,pin-drv = <0>;
};
};

&otg_boost {
int-gpios = <&max77779_pmic 6 17>;
};
35 changes: 35 additions & 0 deletions device/tegu/usb-host/tegu-rt9470-early-read
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash
# SPDX-License-Identifier: MIT
# tegu-rt9470-early-read: one boot only (armed by its marker), read the
# RT9470's OTG_EN (FUNCTION 0x02 bit 1), ICSTAT (0x0f) and STAT registers as
# early as the I2C bus allows, before any RT9470 driver loads, then again at
# 60 s of uptime. Read-only: i2cget only, no writes. USB-HOST-PLAN step H5.
set -u
A=/var/lib/omamobile-tegu/rt9470-early-read
L=/var/log/omamobile/rt9470-early-read.log; mkdir -p /var/log/omamobile
[[ -e $A/armed ]] || exit 0
rm -f "$A/armed"; sync
log() { echo "$(awk '{print $1}' /proc/uptime)s $*" >> "$L"; }
bus=
for _ in $(seq 1 300); do
for d in /sys/bus/i2c/devices/i2c-*; do
readlink -f "$d" | grep -c 10ca0000 >/dev/null && { bus=${d##*-}; break 2; }
done
sleep 0.1
done
[[ -n $bus ]] || { log "no hsi2c10 bus"; exit 1; }
# i2c-dev is a module, and this runs before systemd-modules-load: load it
# here (it is not an RT9470 driver), then wait for the bus's device node.
modprobe i2c-dev 2>/dev/null
for _ in $(seq 1 100); do [[ -e /dev/i2c-$bus ]] && break; sleep 0.1; done
[[ -e /dev/i2c-$bus ]] || { log "no /dev/i2c-$bus"; exit 1; }
read_all() {
local r out=
for r in 0x01 0x02 0x0c 0x0f 0x10 0x11 0x12 0x13; do
out+=" $r=$(i2cget -f -y "$bus" 0x5b "$r" 2>/dev/null || echo ?)"
done
log "$1: driver $(lsmod | grep -c '^tegu_rt9471_boost') vbus-bus$bus$out"
}
read_all early
while (( $(awk '{print int($1)}' /proc/uptime) < 60 )); do sleep 1; done
read_all at-60s
14 changes: 14 additions & 0 deletions device/tegu/usb-host/tegu-rt9470-early-read.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# SPDX-License-Identifier: MIT
[Unit]
Description=Pixel 9a: read the RT9470's boost state before its driver loads (one boot)
ConditionPathExists=/var/lib/omamobile-tegu/rt9470-early-read/armed
DefaultDependencies=no
After=systemd-udevd.service systemd-remount-fs.service
Before=systemd-modules-load.service

[Service]
Type=simple
ExecStart=/usr/local/sbin/tegu-rt9470-early-read

[Install]
WantedBy=sysinit.target
60 changes: 60 additions & 0 deletions device/tegu/usb-host/tegu-usb-host-h4-unbind
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/bin/bash
# SPDX-License-Identifier: MIT
# tegu-usb-host-h4-unbind: USB-HOST-PLAN step H4, the published form of the
# script that runs while the laptop is out. Start it detached before the
# swap (systemd-run --unit=tegu-usb-host-h4 /usr/local/sbin/tegu-usb-host-h4-unbind).
# It waits for the laptop link to go and for a USB drive to enumerate in host
# mode, then 20 s later unbinds the MAX77779 charger (the chgin-otg provider)
# and logs what that leaves: kmsg, VBUS as the TCPC measures it (0x70/0x71)
# and CHG_CNFG_00 (0xbc) at 1, 5 and 20 s. Reads are i2cget only; the one
# write is the unbind. The swap timer the runbook arms (x-deadman) is the
# way back if the laptop link doesn't return.
set -u
L=/var/log/omamobile/usb-host-h4.log; mkdir -p /var/log/omamobile
log() { echo "$(date -Is) $*" >> "$L"; }
bus_of() { local d; for d in /sys/bus/i2c/devices/i2c-*; do readlink -f "$d" | grep -c "$1" >/dev/null && { echo "${d##*-}"; return; }; done; }
CB=$(bus_of 108d0000.i2c) # hsi2c6: the TCPC at 0x25, the charger at 0x69
RB=$(bus_of 10ca0000.i2c) # hsi2c10: the RT9470 at 0x5b
[[ -n $CB && -n $RB ]] || { log "no hsi2c6 or hsi2c10 bus: nothing done"; exit 1; }
dev=$CB-0069
vbus_mv() { # TCPCI VBUS_VOLTAGE: bits 9:0 in 25 mV steps, bits 11:10 the scale
local lo hi
lo=$(i2cget -f -y "$CB" 0x25 0x70 2>/dev/null) && hi=$(i2cget -f -y "$CB" 0x25 0x71 2>/dev/null) || { echo "?"; return; }
echo "$(( (((hi & 0x03) << 8 | lo) * 25) << ((hi >> 2) & 0x03) )) (0x71/0x70 $hi/$lo)"
}
snap() {
log "$1 vbus_mV=$(vbus_mv) chg_cnfg_00=$(i2cget -f -y "$CB" 0x69 0xbc 2>/dev/null || echo ?) rt function=$(i2cget -f -y "$RB" 0x5b 0x02 2>/dev/null || echo ?) icstat=$(i2cget -f -y "$RB" 0x5b 0x0f 2>/dev/null || echo ?)"
log "$1 typec: pr=$(cat /sys/class/typec/port0/power_role 2>/dev/null) dr=$(cat /sys/class/typec/port0/data_role 2>/dev/null) role=$(cat /sys/class/usb_role/*/role 2>/dev/null) udc=$(cat /sys/class/udc/*/state 2>/dev/null) regulators: $(for r in /sys/class/regulator/regulator.*; do n=$(cat "$r/name"); case $n in otg-boost|chgin-otg) printf '%s=%s ' "$n" "$(cat "$r/state")";; esac; done)charger=$(basename "$(readlink -f "/sys/bus/i2c/devices/$dev/driver")" 2>/dev/null)"
log "$1 usb: $(cat /sys/bus/usb/devices/*/product 2>/dev/null | tr '\n' '|') disks: $(lsblk -dno NAME,SIZE,TRAN 2>/dev/null | grep usb | tr '\n' '|')"
}
log "start"; snap pre
[[ -e /sys/bus/i2c/devices/$dev/driver ]] || { log "no driver bound at $dev: nothing to unbind"; exit 1; }
# the laptop goes out
for _ in $(seq 1 600); do [[ $(cat /sys/class/udc/*/state 2>/dev/null) != configured ]] && break; sleep 1; done
[[ $(cat /sys/class/udc/*/state 2>/dev/null) != configured ]] || { log "laptop never left in 10 min: nothing done"; exit 0; }
log "laptop link gone"
# the drive enumerates in host mode
drive=
for _ in $(seq 1 180); do
drive=$(lsblk -dno NAME,TRAN 2>/dev/null | awk '$2 == "usb" { print $1; exit }')
[[ -n $drive ]] && break
sleep 1
done
[[ -n $drive ]] || { snap no-drive; log "no USB drive in 3 min: nothing unbound"; exit 1; }
log "drive $drive enumerated"; snap attached
sleep 20; snap t+20
if echo "$dev" > "/sys/bus/i2c/devices/$dev/driver/unbind"; then
log "unbound the charger at $dev"
else
log "unbind of $dev failed"; exit 1
fi
sleep 1; snap unbound+1s
sleep 4; snap unbound+5s
sleep 15; snap unbound+20s
dmesg | grep -iE "max77779|regulator|otg|tcpm|tcpci|vbus|usb [0-9]|sd[a-z]|oops|bug|call trace" | tail -40 >> "$L"
cat /sys/kernel/debug/usb/tcpm-*/log 2>/dev/null | tail -40 >> "$L"
# the laptop goes back in; the swap timer reboots if it doesn't
for _ in $(seq 1 600); do [[ $(cat /sys/class/udc/*/state 2>/dev/null) == configured ]] && break; sleep 1; done
snap laptop-back
dmesg | grep -iE "max77779|regulator|otg|tcpm|vbus|oops|bug|call trace" | tail -20 >> "$L"
log "done"
64 changes: 64 additions & 0 deletions device/tegu/usb-host/tegu-usb-host-h5-reset
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/bin/bash
# SPDX-License-Identifier: MIT
# tegu-usb-host-h5-reset: USB-HOST-PLAN step H5, the published form of the
# script that runs while the laptop is out. Start it detached before the
# swap (systemd-run --unit=tegu-usb-host-h5 /usr/local/sbin/tegu-usb-host-h5-reset).
# It waits for the laptop link to go and for a USB drive to enumerate in host
# mode, logs the boost's state 20 s later, syncs, and resets the palmtop
# without a shutdown (echo b > /proc/sysrq-trigger), so that the next boot's
# tegu-rt9470-early-read shows what a reset leaves with host mode on. It
# refuses to reset unless that read is armed and its unit enabled, and
# unless a drive has enumerated: without them the reset would show nothing.
# On every exit without a reset it removes the armed marker, and it traps
# TERM to do the same (the swap timer's reboot stops this unit first), so a
# fallback reboot (the swap timer, a systemctl reboot) doesn't produce an
# early-read log that looks like this step's. The early read counts only when this
# script's log ends with the "resetting now" line.
# Reads are i2cget only; the writes are the marker's removal and the sysrq.
set -u
L=/var/log/omamobile/usb-host-h5.log; mkdir -p /var/log/omamobile
A=/var/lib/omamobile-tegu/rt9470-early-read/armed
log() { echo "$(date -Is) $*" >> "$L"; }
disarm() { rm -f "$A"; sync; log "no reset: early read disarmed ($A removed)"; }
trap 'disarm; exit 1' TERM
bus_of() { local d; for d in /sys/bus/i2c/devices/i2c-*; do readlink -f "$d" | grep -c "$1" >/dev/null && { echo "${d##*-}"; return; }; done; }
CB=$(bus_of 108d0000.i2c) # hsi2c6: the TCPC at 0x25, the charger at 0x69
RB=$(bus_of 10ca0000.i2c) # hsi2c10: the RT9470 at 0x5b
[[ -n $CB && -n $RB ]] || { log "no hsi2c6 or hsi2c10 bus: nothing done"; disarm; exit 1; }
vbus_mv() { # TCPCI VBUS_VOLTAGE: bits 9:0 in 25 mV steps, bits 11:10 the scale
local lo hi
lo=$(i2cget -f -y "$CB" 0x25 0x70 2>/dev/null) && hi=$(i2cget -f -y "$CB" 0x25 0x71 2>/dev/null) || { echo "?"; return; }
echo "$(( (((hi & 0x03) << 8 | lo) * 25) << ((hi >> 2) & 0x03) )) (0x71/0x70 $hi/$lo)"
}
snap() {
log "$1 vbus_mV=$(vbus_mv) chg_cnfg_00=$(i2cget -f -y "$CB" 0x69 0xbc 2>/dev/null || echo ?) rt function=$(i2cget -f -y "$RB" 0x5b 0x02 2>/dev/null || echo ?) icstat=$(i2cget -f -y "$RB" 0x5b 0x0f 2>/dev/null || echo ?) stat0-3=$(for r in 0x10 0x11 0x12 0x13; do i2cget -f -y "$RB" 0x5b $r 2>/dev/null || echo ?; done | tr '\n' '/')"
log "$1 typec: pr=$(cat /sys/class/typec/port0/power_role 2>/dev/null) dr=$(cat /sys/class/typec/port0/data_role 2>/dev/null) role=$(cat /sys/class/usb_role/*/role 2>/dev/null) udc=$(cat /sys/class/udc/*/state 2>/dev/null) regulators: $(for r in /sys/class/regulator/regulator.*; do n=$(cat "$r/name"); case $n in otg-boost|chgin-otg) printf '%s=%s ' "$n" "$(cat "$r/state")";; esac; done)"
log "$1 usb: $(cat /sys/bus/usb/devices/*/product 2>/dev/null | tr '\n' '|') disks: $(lsblk -dno NAME,SIZE,TRAN 2>/dev/null | grep usb | tr '\n' '|')"
}
log "start"
if [[ ! -e $A || $(systemctl is-enabled tegu-rt9470-early-read 2>/dev/null) != enabled ]]; then
log "early read not armed and enabled ($A, $(systemctl is-enabled tegu-rt9470-early-read 2>&1)): no reset"; disarm; exit 1
fi
snap pre
# the laptop goes out
for _ in $(seq 1 600); do [[ $(cat /sys/class/udc/*/state 2>/dev/null) != configured ]] && break; sleep 1; done
[[ $(cat /sys/class/udc/*/state 2>/dev/null) != configured ]] || { log "laptop never left in 10 min: no reset"; disarm; exit 0; }
log "laptop link gone"
# the drive enumerates in host mode
drive=
for _ in $(seq 1 180); do
drive=$(lsblk -dno NAME,TRAN 2>/dev/null | awk '$2 == "usb" { print $1; exit }')
[[ -n $drive ]] && break
sleep 1
done
[[ -n $drive ]] || { snap no-drive; log "no USB drive in 3 min: no reset"; disarm; exit 1; }
log "drive $drive enumerated"; snap attached
sleep 20; snap t+20
dmesg | grep -iE "max77779|rt9471|regulator|otg|tcpm|vbus|usb [0-9]|sd[a-z]" | tail -30 >> "$L"
log "resetting now: echo b > /proc/sysrq-trigger (the early read is armed for the next boot)"
sync; sleep 1
echo b > /proc/sysrq-trigger
sleep 10
log "still running 10 s after the sysrq: the reset did not happen"
disarm
exit 1