#!/bin/bash set -euo pipefail DEV=${DDR_DEV:-/sys/class/devfreq/dmc} STATE_DIR=${DDR_STATE_DIR:-/tmp/orangepi-ddr-mode} usage() { cat <<'EOF' Usage: sudo ./scripts/ddr_mode.sh status sudo ./scripts/ddr_mode.sh performance sudo ./scripts/ddr_mode.sh ondemand sudo ./scripts/ddr_mode.sh restore EOF } require_dev() { if [[ ! -d "$DEV" ]]; then echo "DDR devfreq node not found: $DEV" >&2 exit 1 fi } show_status() { require_dev echo "DDR devfreq node: $DEV" [[ -f "$DEV/name" ]] && echo "name: $(cat "$DEV/name")" [[ -f "$DEV/governor" ]] && echo "governor: $(cat "$DEV/governor")" [[ -f "$DEV/available_governors" ]] && echo "available_governors: $(cat "$DEV/available_governors")" [[ -f "$DEV/min_freq" ]] && echo "min_freq: $(cat "$DEV/min_freq")" [[ -f "$DEV/max_freq" ]] && echo "max_freq: $(cat "$DEV/max_freq")" [[ -f "$DEV/cur_freq" ]] && echo "cur_freq: $(cat "$DEV/cur_freq")" [[ -f "$DEV/available_frequencies" ]] && echo "available_frequencies: $(cat "$DEV/available_frequencies")" } save_state() { mkdir -p "$STATE_DIR" [[ -f "$DEV/governor" ]] && cat "$DEV/governor" > "$STATE_DIR/governor" [[ -f "$DEV/min_freq" ]] && cat "$DEV/min_freq" > "$STATE_DIR/min_freq" [[ -f "$DEV/max_freq" ]] && cat "$DEV/max_freq" > "$STATE_DIR/max_freq" } set_performance() { require_dev save_state if [[ -f "$DEV/available_governors" ]] && grep -qw performance "$DEV/available_governors"; then echo performance > "$DEV/governor" elif [[ -f "$DEV/available_frequencies" ]]; then max=$(tr ' ' '\n' < "$DEV/available_frequencies" | sort -n | tail -n1) echo "$max" > "$DEV/min_freq" echo "$max" > "$DEV/max_freq" else echo "No performance governor or available_frequencies for $DEV" >&2 exit 1 fi show_status } set_ondemand() { require_dev if [[ -f "$DEV/available_governors" ]] && grep -qw dmc_ondemand "$DEV/available_governors"; then echo dmc_ondemand > "$DEV/governor" else echo "dmc_ondemand governor not available for $DEV" >&2 exit 1 fi show_status } restore_state() { require_dev if [[ ! -d "$STATE_DIR" ]]; then echo "No saved DDR state found in $STATE_DIR" >&2 exit 1 fi [[ -f "$STATE_DIR/governor" ]] && cat "$STATE_DIR/governor" > "$DEV/governor" [[ -f "$STATE_DIR/min_freq" ]] && cat "$STATE_DIR/min_freq" > "$DEV/min_freq" [[ -f "$STATE_DIR/max_freq" ]] && cat "$STATE_DIR/max_freq" > "$DEV/max_freq" show_status } case "${1:-status}" in status) show_status ;; performance) set_performance ;; ondemand) set_ondemand ;; restore) restore_state ;; -h|--help|help) usage ;; *) usage exit 1 ;; esac