summarylogtreecommitdiffstats
path: root/gpdfanspeed
blob: f698fa6c19a46185e521ab130e45a4f9a5d5e172 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/bin/bash
# See https://github.com/Cryolitia/gpd-fan-driver for a documentation of this interface.
# See https://www.kernel.org/doc/Documentation/hwmon/sysfs-interface for documentation of the kernel sysfs interface.

### LICENSING INFORMATION: GPL 2.0 OR LATER
#
# 'gpdfanspeed': Companion application to the kernel driver 'gpd-fan' (https://github.com/Cryolitia/gpd-fan-driver) to make easy command line control of the driver's settings.
# Copyright (C) 2024+ dreieck.
# 
#
# This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Also add information on how to contact you by electronic and paper mail.
#
###

set -e
shopt -s extglob

PROGRAMMENAME="gpdfanspeed"
VERSION=20240429.01

FANMONDIR="$(ls -1d "/sys/devices/platform/gpd_fan/hwmon"/hwmon[0-9] | head -n1)"
DEBUGFSDIR="/sys/kernel/debug/gpdfan"

## In the hwmon interface ($FANMONDIR/):
_namefile="name"
_rpmfile="fan1_input"
_pwmsettingfile="pwm1"
_controlmodefile="pwm1_enable"
_pwmmodefile="pwm1_mode"
_updateintervalfile="update_interval"

## In debugfs ($DEBUGFSDIR/):
_debugpwmsettingfile="pwm_reg"
_debugcontrolmodefile="manual_control_reg"


msg() {
  printf '%s\n' "$@"
}

msgnonl() {
  printf '%s' "$@"
}

errmsg() {
  msg "$@" > /dev/stderr
}

printlicensestub() {
  msg "${PROGRAMMENAME} version ${VERSION}, Copyright (C) 2024+ dreieck"
  msg "${PROGRAMMENAME} comes with ABSOLUTELY NO WARRANTY. It is licensed under GPL-2.0-or-later."
  msg "This is free software, and you are welcome to redistribute it under certain conditions."
  msg "For details, see https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html"
}

printusage() {
  msg "Usage:"
  msg ""
  msg "  $0"
  msg "    Show current setting of gpd-fan."
  msg ""
  msg "  $0 <setting>"
  msg "    Sets GPD fan setting. '<setting>' can be:"
  msg "    * Number in 0..255:  Sets the fan to manual control and that speed (values outside that range will be truncated)."
  msg "    * 'auto':            Sets to automatically controlled by the firmware."
  msg "    * 'manual':          Sets to manually controlled."
  msg "    * 'max':             Sets to full speed."
  msg "    * [0-9]+ms:          Sets the interval in ms in which the chip updates the readings."
  msg ""
  msg "  $0 <-h|--help|help>"
  msg "    Prints this help message."
  msg ""
  msg "  $0 <-V|--version|version>"
  msg "    Prints out the programme's version."
}

case "$1" in
  "-h"|"--help"|"help")
    printlicensestub
    msg ""
    printusage
    exit 0
  ;;
  "-V"|"--version"|"version")
    msg "${VERSION}"
    exit 0
  ;;
esac

_action=show
if [ "$#" -ge 1 ]; then
  _action=set
  _setting="$1"
fi

case "${_action}" in
  'show')
    _name="$(<"${FANMONDIR}/${_namefile}")"
    _rpm="$(<"${FANMONDIR}/${_rpmfile}")"
    _controlmode="$(<"${FANMONDIR}/${_controlmodefile}")"
    _setting="$(<"${FANMONDIR}/${_pwmsettingfile}")"
    _pwmmode="$(<"${FANMONDIR}/${_pwmmodefile}")"
    _updateinterval="$(<"${FANMONDIR}/${_updateintervalfile}")"
    if [ -r "${DEBUGFSDIR}/${_debugpwmsettingfile}" ]; then
      _pwm_reg="$(<"${DEBUGFSDIR}/${_debugpwmsettingfile}")"
    fi
    if [ -r "${DEBUGFSDIR}/${_debugcontrolmodefile}" ]; then
      _manual_control_reg="$(<"${DEBUGFSDIR}/${_debugcontrolmodefile}")"
    fi
    case "${_controlmode}" in
      "0")
        _controlmodename="full speed"
      ;;
      "1")
        _controlmodename="manual"
      ;;
      "2")
        _controlmodename="auto"
      ;;
      *)
        _controlmodename="unknown (${_controlmode})"
      ;;
    esac
    case "${_pwmmode}" in
      "0")
        _pwmmodename="DC"
      ;;
      "1")
        _pwmmodename="PWM"
      ;;
      *)
        _pwmmodename="unknown (${_pwmmode})"
      ;;
    esac
    msg     "${_name}:"
    msgnonl "  Speed:                  ${_rpm} rpm"
    if [ -n "${_pwm_reg}" ]; then
      msgnonl " (HW register: ${_pwm_reg})"
    fi
    msg ""
    msgnonl "  Control mode:           ${_controlmodename}"
    if [ -n "${_manual_control_reg}" ]; then
      msgnonl " (HW register: ${_manual_control_reg})"
    fi
    msg ""
    msg     "  Speed setting (0..255): ${_setting}"

    msg     "  PWM Mode:               ${_pwmmodename}"
    msg     "  Info update interval:   ${_updateinterval} ms"
  ;;
  'set')
  case "${_setting}" in
    ?(-)+([0-9]))
      if [ "${_setting}" -lt 0 ]; then
        _setting=0
      fi
      if [ "${_setting}" -gt 255 ]; then
        _setting=255
      fi
      printf '1' > "${FANMONDIR}/${_controlmodefile}"
      printf '%s' "${_setting}" > "${FANMONDIR}/${_pwmsettingfile}"
    ;;
    'auto')
      printf '%s' '2' > "${FANMONDIR}/${_controlmodefile}"
    ;;
    'manual')
      printf '%s' '1' > "${FANMONDIR}/${_controlmodefile}"
    ;;
    'max')
      printf '%s' '0' > "${FANMONDIR}/${_controlmodefile}"
    ;;
    +([0-9])ms)
      _value="${_setting%ms}"
      printf '%s' "${_value}" > "${FANMONDIR}/${_updateintervalfile}"
    ;;
    *)
      errmsg "$0: Invalid setting '${_setting}'".
      errmsg ""
      errmsg "$(printusage)"
      errmsg ""
      errmsg "Aborting."
      exit 1
    ;;
  esac
  ;;
  *)
    errmsg "$0: Internal error: This state should never bee reached."
    errmsg "This is a bug in $0, please report."
    errmsg "Reference: This message is at line number ${LINENO}."
    errmsg "Aborting."
    exit 255
  ;;
esac