update system time after N900 resumes
[kernel-power] / kernel-power-settings / kernel-config
1 #!/bin/sh
2 # kernel configuration script for power user kernel
3 # (c) Copyright 2010 by Thomas Tanner <maemo@tannerlab.com>
4 # licensed under GPLv3
5 # version 0.3 (16. May 2010)
6
7 if test $# -eq 0; then
8     echo "$0 command [options]"
9     echo "commands:"
10     echo "  show        - show current settings"
11     echo "  load <file> - load settings from file"
12     echo "  save <file> - save settings to file (abs. path or filename in /home/user/.kernel)"
13     echo "  default [<file>] - set file or the current settings as default settings"
14     echo "  limits min max - set the frequency limits ('-' keeps the current setting)"
15     echo "  lock freq [volt] [dsp] - lock CPU at frequency with specific settings"
16     echo "  unlock       - unlock CPU frequency"
17     echo "filenames: absolute path or filename in search path or none=defaults"
18     echo "search path: .,/home/user/.kernel,/usr/share/kernel-power-settings/"
19     exit 1
20 fi
21 if test "`id -u`" -ne 0; then
22     sudo $0 $* # run as root
23     exit $?
24 fi
25
26 cfr=/sys/devices/system/cpu/cpu0/cpufreq
27 cfd=$cfr/ondemand
28 cfc=$cfr/conservative
29 pwr=/sys/power
30
31 if test -f $pwr/vdd1_opps_vsel -a -f $pwr/dsp_opps_rate; then :
32 else
33     echo This kernel version `uname -r` is not supported
34     echo Please make sure that kernel-power is installed and running.
35     echo If you have just installed the kernel, you need to shutdown and boot to activate it.
36     echo To reinstall the power user kernel run:
37     echo \'apt-get install --reinstall kernel-power kernel-power-flasher\'
38     exit 1
39 fi
40
41 allfreq=
42 for f in `cat $cfr/scaling_available_frequencies`; do
43     allfreq="$f $allfreq"
44 done
45 vsel=`cat $pwr/vdd1_opps_vsel` 
46 rate=`cat $pwr/dsp_opps_rate` 
47
48 cmd=$1
49 test "$cmd" = setdefault && cmd=default # old name
50 case " show load save default limits lock unlock " in
51 *" $cmd "*) ;;
52 *)  echo "invalid command $cmd";  exit 1 ;;
53 esac
54 arg=
55 shift
56 if test "$cmd" = unlock; then
57     echo 'unlocking frequency. reset to defaults'
58     cmd=loaddef
59 fi
60 if test "$cmd" = default && test $# -eq 0; then
61     echo 'saving current settings as defaults'
62     arg=/etc/default/kernel-power
63     cmd=savedef
64 fi
65
66 popvsel() { curvsel=$1; shift; vsel="$*"; }
67 poprate() { currate=$1; shift; rate="$*"; }
68
69 case $cmd in
70 show|save|savedef)
71     minfreq=$((`cat $cfr/scaling_min_freq`/1000))
72     maxfreq=$((`cat $cfr/scaling_max_freq`/1000))
73     test $maxfreq = 599 && maxfreq=600
74     gov=`cat $cfr/scaling_governor`
75     avoid=
76     test $gov = ondemand && avoid=`cat $cfd/avoid_frequencies`
77     freqs=
78     for f in $allfreq; do
79         popvsel $vsel
80         poprate $rate
81         case " $avoid " in *" $f "*) ;;
82         *) freqs="$freqs$((f/1000)):$curvsel,$currate " ;;
83         esac
84     done
85     if test $cmd = show; then
86         echo "current kernel configuration:"
87         echo "current frequency:" $((`cat $cfr/scaling_cur_freq`/1000))
88         echo -n "supported frequencies: "
89         for f in $allfreq; do echo -n "$((f/1000)) "; done
90         echo
91         echo "min. frequency:" $minfreq
92         echo "max. frequency:" $maxfreq
93         echo -n "avoid frequencies: "
94         for f in $avoid; do echo -n "$((f/1000)) "; done
95         echo
96         echo "active frequencies: $freqs"
97         echo "SmartReflex" "VDD1=`cat $pwr/sr_vdd1_autocomp`, VDD2=`cat $pwr/sr_vdd2_autocomp`"
98         echo -n "governor $gov:"
99         if test $gov = ondemand; then
100             echo -n " ignore nice load=" `cat $cfd/ignore_nice_load` 
101             echo -n ", up threshold=" `cat $cfd/up_threshold`
102             echo -n ", sampling rate=" `cat $cfd/sampling_rate`
103             echo ", powersave bias=" `cat $cfd/powersave_bias`
104         else
105             echo
106         fi
107     else
108         test $cmd = save && arg=$1
109         if test -z "$arg"; then
110             echo "filename argument is mssing"
111             exit 1
112         fi
113         if test `basename $arg` = $arg; then
114             u=/home/user/.kernel
115             arg=$u/$arg
116             if test ! -d $u; then
117                 mkdir -p $u
118                 chown user.users $u
119             fi
120             touch $arg
121             chown user.users $arg
122         fi
123         rm -f $arg
124         echo "saving to $arg"
125         echo "# kernel configuration file generated by $0" > $arg
126         echo "MINFREQ=$minfreq" >> $arg
127         echo "MAXFREQ=$maxfreq" >> $arg
128         echo "FREQS=\"$freqs\"" >> $arg
129         echo "SMARTREFLEX_VDD1=`cat $pwr/sr_vdd1_autocomp`" >> $arg
130         echo "SMARTREFLEX_VDD2=`cat $pwr/sr_vdd2_autocomp`" >> $arg
131         echo "GOVERNOR=$gov" >> $arg
132         if test $gov = ondemand; then
133             echo "IGNORE_NICE_LOAD=`cat $cfd/ignore_nice_load`" >> $arg
134             echo "UP_THRESHOLD=`cat $cfd/up_threshold`" >> $arg
135             echo "SAMPLING_RATE=`cat $cfd/sampling_rate`" >> $arg
136             echo "POWERSAVE_BIAS=`cat $cfd/powersave_bias`" >> $arg
137         fi
138     fi
139     ;;
140 load|loaddef)
141     test $cmd = loaddef || arg=$1
142     if test -z "$arg"; then
143         arg=/etc/default/kernel-power
144         test -f $arg || arg=/usr/share/kernel-power-settings/default
145     fi
146     if test `basename $arg` = $arg; then # not absolute
147         for p in . /home/user/.kernel /usr/share/kernel-power-settings; do
148             test -f $p/$arg && arg=$p/$arg && break
149         done
150     fi
151     if test ! -f "$arg"; then
152         echo "file not found $arg"
153         exit 1
154     fi
155     echo "loading $arg"
156     source $arg
157     if test -n "$VDD1_OPPS_VSEL" -o -n "$DSP_OPPS_RATE"; then
158         echo "warning: old configuration format detected. please save in the new format."
159         if test -n "$MAX_FREQ"; then
160             test $MAX_FREQ = 600000 && MAX_FREQ=599000
161             echo $MAX_FREQ > $cfr/scaling_max_freq
162         fi
163         test -n "$MIN_FREQ" && echo $MIN_FREQ > $cfr/scaling_min_freq
164         test -n "$MAX_FREQ" && echo $MAX_FREQ > $cfr/scaling_max_freq
165         test -n "$VDD1_OPPS_VSEL" && echo $VDD1_OPPS_VSEL > $pwr/vdd1_opps_vsel
166         test -n "$DSP_OPPS_RATE" && echo $DSP_OPPS_RATE > $pwr/dsp_opps_rate
167         test -n "$SMARTREFLEX_VDD1" && echo $SMARTREFLEX_VDD1 > $pwr/sr_vdd1_autocomp
168         test -n "$SMARTREFLEX_VDD2" && echo $SMARTREFLEX_VDD2 > $pwr/sr_vdd2_autocomp
169         test -n "$IGNORE_NICE_LOAD" && echo $IGNORE_NICE_LOAD > $cfd/ignore_nice_load
170         test -n "$UP_THRESHOLD" && echo $UP_THRESHOLD > $cfd/up_threshold
171         test -n "$SAMPLING_RATE" && echo $SAMPLING_RATE > $cfd/sampling_rate
172         exit 1
173     fi
174     active=
175     minfreq=
176     maxfreq=
177     for f in $FREQS; do
178         freq=`echo $f | cut -d: -f1`
179         f=`echo $f | cut -d: -f2`
180         volt=`echo $f | cut -d, -f1`
181         dsp=`echo $f | cut -d, -f2`
182         if test ! $freq = 0; then
183             test -z "$minfreq" -o "$freq" -lt "$minfreq" && minfreq=$freq
184             test -z "$maxfreq" -o "$freq" -gt "$maxfreq" && maxfreq=$freq
185             freq=$((freq*1000))
186             if test "$freq" = "750000"; then
187                 echo warning: $freq was changed to 720000;
188                 freq=720000
189             fi
190             case " $allfreq " in *" $freq "*) ;;
191             *)  echo warning: $freq not supported; continue ;;
192             esac
193             active="$active $freq"
194         fi
195         test -z "$volt" && test -z "$dsp" && continue
196         tvsel=
197         trate=
198         for fr in $allfreq; do
199             if test $fr = $freq; then
200                 popvsel $vsel
201                 poprate $rate
202                 test -z "$volt" && volt=$curvsel
203                 test -z "$dsp" && dsp=$currate
204                 tvsel="$tvsel $volt $vsel"
205                 trate="$trate $dsp $rate"
206                 break
207             fi
208             popvsel $vsel
209             tvsel="$tvsel $curvsel"
210             poprate $rate
211             trate="$trate $currate"
212         done
213         vsel=$tvsel
214         rate=$trate
215     done
216     test -z "$GOVERNOR" && GOVERNOR=ondemand
217     test -n "$MINFREQ" && minfreq=$MINFREQ
218     test -n "$MAXFREQ" && maxfreq=$MAXFREQ
219     test "$minfreq" -gt 100000 && minfreq=$((minfreq/1000))
220     test "$maxfreq" -gt 100000 && maxfreq=$((maxfreq/1000))
221     avoid=
222     for f in $allfreq; do
223         if test "$f" -lt $((minfreq*1000)); then
224             avoid="$avoid $f"
225             continue
226         fi
227         case " $active " in *" $f "*) ;;
228         *) avoid="$avoid $f" ;;
229         esac
230     done
231     echo $GOVERNOR > $cfr/scaling_governor
232     if test $GOVERNOR = ondemand; then
233         echo $avoid > $cfd/avoid_frequencies
234         test -n "$IGNORE_NICE_LOAD" && echo $IGNORE_NICE_LOAD > $cfd/ignore_nice_load
235         test -n "$UP_THRESHOLD" && echo $UP_THRESHOLD > $cfd/up_threshold
236         test -n "$SAMPLING_RATE" && echo $SAMPLING_RATE > $cfd/sampling_rate
237         test -n "$POWERSAVE_BIAS" && echo $POWERSAVE_BIAS > $cfd/powersave_bias
238     fi
239     echo $vsel > $pwr/vdd1_opps_vsel
240     echo $rate > $pwr/dsp_opps_rate
241     test $maxfreq = 600 && maxfreq=599
242     echo $((maxfreq*1000)) > $cfr/scaling_max_freq
243     echo $((minfreq*1000)) > $cfr/scaling_min_freq
244     echo $((maxfreq*1000)) > $cfr/scaling_max_freq
245     test -n "$SMARTREFLEX_VDD1" && echo $SMARTREFLEX_VDD1 > $pwr/sr_vdd1_autocomp
246     test -n "$SMARTREFLEX_VDD2" && echo $SMARTREFLEX_VDD2 > $pwr/sr_vdd2_autocomp
247     echo "successfully loaded."
248     ;;
249 default)
250     arg=$1
251     if test -z "$arg"; then
252         echo "filename missing"
253         exit 1
254     fi
255     if test `basename $arg` = $arg; then # not absolute
256         for p in . /home/user/.kernel /usr/share/kernel-power-settings; do
257             test -f $p/$arg && arg=$p/$arg && break
258         done
259     fi
260     def=/etc/default/kernel-power
261     rm -f $def
262     case $arg in 
263     /usr/share/kernel-power-settings/*) ln -s $arg $def ;;
264     *) cp $arg $def ;;
265     esac
266     echo "defaults set to $arg"
267     ;;
268 limits)
269     minfreq=$1
270     maxfreq=$2
271     if test "x$minfreq" = x -o "x$maxfreq" = x; then
272         echo "frequency limit arguments missing"
273         exit 1
274     fi
275     test "x$minfreq" = "x-" && minfreq=$((`cat $cfr/scaling_min_freq`/1000))
276     test "x$maxfreq" = "x-" && maxfreq=$((`cat $cfr/scaling_max_freq`/1000))
277     limits="$minfreq,$maxfreq"
278     minfreq=$((minfreq*1000))
279     maxfreq=$((maxfreq*1000))
280     case " $allfreq " in *" $minfreq "*) ;;
281     *) echo "invalid lower limit $1"; exit 1; ;;
282     esac
283     case " $allfreq " in *" $maxfreq "*) ;;
284     *) echo "invalid upper limit $2"; exit 1; ;;
285     esac
286     if test $minfreq -gt $maxfreq; then
287         echo "minimum must not be greater than maximum: $limits"
288         exit 1
289     fi
290     test $maxfreq = 600000 && maxfreq=599000
291     echo $maxfreq > $cfr/scaling_max_freq
292     echo $minfreq > $cfr/scaling_min_freq
293     echo $maxfreq > $cfr/scaling_max_freq
294     echo "the limits were set to [$limits]"
295     ;;
296 lock)
297     arg=$1
298     altfreq=
299     for f in $allfreq; do
300         test "$f" = "$arg" && continue
301         altfreq=$f
302         break
303     done
304     vsel=
305     dsp=
306     freq=$((arg*1000))
307     ifreq=
308     i=1
309     avoid=
310     for f in $allfreq; do
311         i=$((i+1))
312         if test $f = $freq; then
313             ifreq=$i
314             break
315         fi
316         avoid="$avoid $f"
317     done
318     if test -z "ifreq"; then
319         echo invalid frequency $freq
320         exit 1
321     fi
322     echo userspace > $cfr/scaling_governor 
323     echo $altfreq > $cfr/scaling_setspeed # temporarily set alternative frequencies
324     volt=$2
325     if test -n "$volt"; then
326         i=0
327         vsel=
328         for v in `cat $pwr/vdd1_opps_vsel`; do
329             i=$((i+1))
330             test $i = $ifreq && v=$volt
331             vsel="$vsel $v"
332         done
333         echo $vsel > $pwr/vdd1_opps_vsel
334     fi
335     dsp=$3
336     if test -n "$dsp"; then
337         i=0
338         dsp=
339         for r in `cat $pwr/dsp_opps_rate`; do
340             i=$((i+1))
341             test $i = $ifreq && r=$dsp
342             dsp="$dsp $r"
343         done
344         echo $dsp > $pwr/dsp_opps_rate
345     fi
346     echo locking frequency $freq
347     echo ondemand > $cfr/scaling_governor 
348     echo $avoid > $cfd/avoid_frequencies # avoid all other freqs
349     maxfreq=$freq
350     test $maxfreq = 600000 && maxfreq=599000
351     # min after max? kernel docs are wrong?
352     echo $maxfreq > $cfr/scaling_max_freq
353     echo $freq > $cfr/scaling_min_freq
354     echo $maxfreq > $cfr/scaling_max_freq
355     ;;
356 esac
357 exit 0