reformd.sh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/bin/bash
  2. #
  3. # MNT Reform 0.3+ Daemon for Battery, Lid and Fan Control
  4. # Copyright 2018 MNT Media and Technology UG, Berlin
  5. # SPDX-License-Identifier: GPL-3.0-or-later
  6. #
  7. set_fan_speed () {
  8. set +e; echo 0 > /sys/class/pwm/pwmchip1/export 2>/dev/null ; set -e
  9. echo 10000 > /sys/class/pwm/pwmchip1/pwm0/period
  10. echo "$1" > /sys/class/pwm/pwmchip1/pwm0/duty_cycle
  11. echo 1 > /sys/class/pwm/pwmchip1/pwm0/enable
  12. }
  13. function setup_serial {
  14. # 2400 baud 8N1, raw
  15. # cargoculted by exporting parameters with stty after using screen
  16. stty 406:0:8bb:8a30:3:1c:7f:15:4:2:64:0:11:13:1a:0:12:f:17:16:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0 -F /dev/ttymxc1
  17. }
  18. function disable_echo {
  19. setup_serial
  20. exec 99<>/dev/ttymxc1
  21. :<&99
  22. printf "0e\r" >&99
  23. exec 99>&-
  24. set +e; timeout 2 head /dev/ttymxc1; set -e
  25. }
  26. function get_battery_state {
  27. setup_serial
  28. exec 99<>/dev/ttymxc1
  29. :<&99
  30. printf "p\r" >&99
  31. IFS=$':\t\r'
  32. read -r -t 1 msg bat_capacity bat_volts bat_amps <&99
  33. exec 99>&-
  34. }
  35. function get_soc_temperature {
  36. read soc_temperature < /sys/class/thermal/thermal_zone0/temp
  37. }
  38. function get_lid_state {
  39. setup_serial
  40. exec 99<>/dev/ttymxc1
  41. :<&99
  42. printf "l\r" >&99
  43. IFS=$':\t\r'
  44. read -r -t 1 msg lid_state <&99
  45. lid_state=$(echo "$lid_state" | tr -dc '0-9')
  46. exec 99>&-
  47. }
  48. function reset_bat_capacity {
  49. setup_serial
  50. exec 99<>/dev/ttymxc1
  51. :<&99
  52. printf "0600c\r" >&99
  53. exec 99>&-
  54. }
  55. function system_suspend {
  56. setup_serial
  57. # wake up on serial port 2 traffic
  58. echo enabled > /sys/devices/soc0/soc/2100000.aips-bus/21e8000.serial/tty/ttymxc1/power/wakeup
  59. # drain serial port
  60. set +e; timeout 1 head /dev/ttymxc1; set -e
  61. # zzZzzZ
  62. echo -n mem > /sys/power/state
  63. }
  64. function regulate_fan {
  65. get_soc_temperature
  66. if [ "$soc_temperature" -lt 65000 ]
  67. then
  68. set_fan_speed 5000
  69. fi
  70. if [ "$soc_temperature" -gt 70000 ]
  71. then
  72. set_fan_speed 10000
  73. fi
  74. }
  75. voltage_alert=0
  76. function regulate_voltage {
  77. # TODO low voltage/capacity alert
  78. echo "not yet implemented"
  79. }
  80. function main {
  81. # 1. if the system is getting too hot, we want to cool it with the fan
  82. regulate_fan
  83. # 2. log all stats, especially battery stats, to a TSV file
  84. # so it can be graphed and we can estimate remaining running time
  85. # TODO actually append to log and rotate it out
  86. # TODO interval?
  87. timestamp=$(date +%s)
  88. get_soc_temperature
  89. get_battery_state
  90. get_lid_state
  91. if [ "$bat_amps" == "0.00" ]
  92. then
  93. reset_bat_capacity
  94. fi
  95. printf '%s\t%s\t%s\t%s\t%s\t%s\t%s\n' "$timestamp" "$soc_temperature" "$bat_capacity" "$bat_volts" "$bat_amps" "$voltage_alert" "$lid_state"
  96. printf '%s,%s,%s,%s,%s,%s,%s\n' "$timestamp" "$soc_temperature" "$bat_capacity" "$bat_volts" "$bat_amps" "$voltage_alert" "$lid_state" > /var/log/reformd
  97. # 3. if the lid is closed, we want to suspend the system
  98. # important: this works only if the kernel option no_console_suspend=1 is set!
  99. # also, requires kernel patch when using PCIe cards: https://github.com/sakaki-/novena-kernel-patches/blob/master/0017-pci-fix-suspend-on-i.MX6.patch
  100. # (workaround for erratum "PCIe does not support L2 Power Down")
  101. #if [ "$lid_state" ] && [ "$lid_state" -eq "1" ]
  102. #then
  103. # system_suspend
  104. # exit
  105. #fi
  106. }
  107. disable_echo
  108. while true; do
  109. main
  110. sleep 1
  111. done