1#!/bin/bash
2
3# defines
4FREQ=0 #percent
5SERVICES=(perfd thermal-engine thermald mpdecision)
6DIR="/sys/class/kgsl/kgsl-3d0"
7
8
9###################### SETUP ######################
10
11# helper functions
12fileexists() {
13  [ `adb shell "[ -f $1 ] && echo found"` ]
14}
15getprop() {
16  if fileexists $1; then
17    echo `adb shell cat $1 | tr -d '\r'`
18  else
19    echo "FILE $1 NOT FOUND"
20  fi
21}
22setprop() {
23  if fileexists $1; then
24    adb shell "echo -n $2 > $1"
25  else
26    echo "FILE $1 NOT FOUND"
27  fi
28}
29
30# use passed in percent frequency
31if [[ $# -eq 1 ]]; then
32  FREQ=$1
33fi
34
35# setup
36if [[ "`adb shell id | tr -d '\r' | awk -F'[()]' '{print $2}'`" != "root" ]]; then
37  adb root
38fi
39adb wait-for-device
40
41# device name
42echo Device: `adb shell getprop ro.product.model`
43
44# get available gpu frequencies in sorted order
45if fileexists $DIR/devfreq/available_frequencies; then
46  frequencies=(`getprop $DIR/devfreq/available_frequencies`)
47elif fileexists $DIR/gpu_available_frequencies; then
48  frequencies=(`getprop $DIR/gpu_available_frequencies`)
49elif fileexists /d/clock/gbus/possible_rates; then
50  frequencies=(`getprop /d/clock/gbus/possible_rates`)
51else
52  echo "Unable to find available GPU frequencies"
53  echo
54  exit
55fi
56frequencies=( `printf "%s\n" "${frequencies[@]}" | sort -n` )
57if [ ${frequencies[0]} == "(kHz)" ]; then
58  frequencies=(`printf "%s000\n" "${frequencies[@]:1}"`)
59fi
60minFreq=${frequencies[0]}
61maxFreq=${frequencies[-1]}
62echo Frequencies: ${frequencies[@]}
63
64# find closest frequency
65targetFreq=$(( FREQ * ( maxFreq - minFreq ) / 100 + minFreq ))
66freq=`printf "%d\n" "${frequencies[@]}" | awk -v closest=${frequencies[0]} -v target=$targetFreq 'BEGIN{diff=$0-target;if(diff<0)diff=-diff;lowest=diff};{diff=$0-target;if(diff<0)diff=-diff;if(diff<lowest){lowest=diff;closest=$0}};END{print closest}'`
67echo Selected: $freq
68
69echo
70
71# freeze system
72for service in ${SERVICES[@]}; do
73  adb shell stop $service
74done
75
76# set GPU properties if possible
77setprop $DIR/bus_split 0
78setprop $DIR/devfreq/governor performance
79setprop $DIR/force_bus_on 1
80setprop $DIR/force_rail_on 1
81setprop $DIR/force_clk_on 1
82setprop $DIR/idle_timer 1000000
83setprop $DIR/max_gpuclk $freq
84setprop $DIR/gpuclk $freq
85setprop /d/clock/override.gbus/state 1
86setprop /d/clock/override.gbus/rate $freq
87
88# final message
89echo "GPU Frequency set to $freq"
90
91
92