Commit 71f71c1c authored by Martin Kepplinger's avatar Martin Kepplinger
Browse files

power_key.py: make compatible with 5.6 and later and the devkit

devfreq is being handled differently on our kernels. With 5.6+ frequency
is switched automatically by the kernel for devfreq, when the powersave
governor is set. So in that case we only to that (done by udev in the future).

This also makes devfreq settings compatible with the devkit again because
it reads the minimum frequency which is different than on the phone,
from sysfs.

Also, we particularly handle 5.6-rc based kernel versions that
include a broken devfreq implementation / setup. starting with version
5.6.0 we need to increment the ABI version to 2.
parent f3cafa1e
......@@ -6,6 +6,22 @@ from select import select
import os
import sys
import evdev
import subprocess
is_56rc_installed = None
# 5.X
def kernel_version():
with open("/proc/version") as f:
proc_version = f.read()
if 'version 5.3.' in proc_version:
return 3
elif 'version 5.6.' in proc_version:
return 6
else:
return None
def check_screen():
......@@ -20,6 +36,14 @@ def check_screen():
return True
def find_noc_devfreq():
for node in ['devfreq1', '32700000.noc', '32700000.interconnect']:
noc = "/sys/class/devfreq/{}".format(node)
if os.path.isdir(noc):
break
return noc
def find_dram_devfreq():
for node in ['devfreq0', '3d400000.memory-controller']:
dram = "/sys/class/devfreq/{}".format(node)
......@@ -28,20 +52,65 @@ def find_dram_devfreq():
return dram
set_freq = os.path.join(find_dram_devfreq(), 'userspace/set_freq')
def set_dram_freq(freq):
with open(set_freq, "w+") as f:
with open(os.path.join(find_dram_devfreq(), 'userspace/set_freq'), "w+") as f:
f.write(freq + "\n")
def get_low_freq():
with open("/proc/version") as f:
return ["25000000", "100000000"]['version 5.3.' in f.read()]
def get_low_freq_dram():
if kernel_version() == 3:
return "100000000"
else:
with open(os.path.join(find_dram_devfreq(), 'min_freq')) as f:
freq = f.read()
return freq
def is_56_rc_installed():
global is_56rc_installed
if is_56rc_installed is not None:
return is_56rc_installed
kernel_apt = subprocess.check_output(["uname", "-r"])
if (b"5.6.0-1" in kernel_apt):
# 5.6-rc kernel without devfreq interconnect functionality
is_56rc_installed = True
else:
is_56rc_installed = False
return is_56rc_installed
def maybe_set_dram_freq(freq):
if supports_powersave_gov():
return False
set_dram_freq(freq)
return True
def supports_powersave_gov():
if kernel_version() >= 6 and not is_56_rc_installed():
return True
else:
return False
def set_governor(gov):
with open(os.path.join(find_dram_devfreq(), 'governor'), "w+") as f:
f.write("%s\n" % gov)
try:
with open(os.path.join(find_noc_devfreq(), 'governor'), "w+") as f:
f.write("%s\n" % gov)
except IOError:
# not writable in case the passive governor is (wrongly) in use
pass
def main(args):
print("for kernel version 5.%d" % (kernel_version()))
powerkeyname = '30370000.snvs:snvs-powerkey'
hi_speed = "800000000"
......@@ -49,10 +118,12 @@ def main(args):
foundpath = ''.join(path)
dev = InputDevice(foundpath)
with open(os.path.join(find_dram_devfreq(), 'governor'), "w+") as f:
f.write("userspace\n")
if supports_powersave_gov():
set_governor("powersave")
else:
set_governor("userspace")
set_dram_freq(hi_speed)
maybe_set_dram_freq(hi_speed)
power_wait_time = 1.5
......@@ -60,7 +131,7 @@ def main(args):
blank = False
else:
blank = True
set_dram_freq(get_low_freq())
maybe_set_dram_freq(get_low_freq_dram())
# wait for the key release event so that we can differentiate
# between long and short press
......@@ -78,7 +149,7 @@ def main(args):
if event.code == ecodes.KEY_POWER and blank:
print("screen wakeup")
blank = False
set_dram_freq(hi_speed)
maybe_set_dram_freq(hi_speed)
else:
state[event.code] = event.timestamp(), event
print("Key: %s %d %d pressed" % (ecodes.KEY[event.code], event.code, ecodes.KEY_POWER))
......@@ -90,7 +161,7 @@ def main(args):
if event.code == ecodes.KEY_POWER and delta < power_wait_time:
# short power key press blanks the screen so change the DDR freq
blank = True
set_dram_freq(get_low_freq())
maybe_set_dram_freq(get_low_freq_dram())
# os.system("systemctl suspend -q")
del state[event.code]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment