#!/bin/bash
# Print the SCL rate of every I2C controller on a Jetson, as the device tree requests it (clock-frequency).
# Usage: i2c_bus_freq.sh            # on the board
#        i2c_bus_freq.sh <ssh-host> # from the laptop, runs itself remotely (e.g. agx)
# Mux channels (MAX96712 downstream) have no clock-frequency and inherit their parent's rate.
# This is the requested rate; the driver may clamp it (see dmesg | grep tegra-i2c). Only a scope proves SCL.
set -u

if [ $# -ge 1 ]; then
  exec ssh "$1" 'bash -s' < "$0"
fi

found=0
for b in /sys/bus/i2c/devices/i2c-*; do
  n=$(readlink -f "$b/of_node" 2>/dev/null) || continue
  [ -f "$n/clock-frequency" ] || continue
  hz=$(od -An -tu4 --endian=big "$n/clock-frequency" | tr -d ' ')
  printf '%-8s %-20s %-10s %s\n' "${b##*/}" "${n##*/}" "$hz" "$(cat "$b/name")"
  found=1
done

[ "$found" = 1 ] || { echo "no I2C controller with a DT clock-frequency found" >&2; exit 1; }
