-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathfan.c
212 lines (161 loc) · 3.95 KB
/
fan.c
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// SPDX-License-Identifier: GPL-2.0
#include "pr.h"
#include <linux/version.h>
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 12, 0)
static inline int fixp_linear_interpolate(int x0, int y0, int x1, int y1, int x)
{
if (y0 == y1 || x == x0)
return y0;
if (x1 == x0 || x == x1)
return y1;
return y0 + ((y1 - y0) * (x - x0) / (x1 - x0));
}
#else
#include <linux/bug.h> /* fixp-arith.h needs it, but doesn't include it */
#include <linux/fixp-arith.h>
#endif
#include <linux/lockdep.h>
#include <linux/mutex.h>
#include <linux/types.h>
#include "ec.h"
#include "fan.h"
#include "util.h"
/* ========================================================================== */
static const uint16_t qc71_fan_rpm_addrs[] = {
FAN_RPM_1_ADDR,
FAN_RPM_2_ADDR,
};
static const uint16_t qc71_fan_pwm_addrs[] = {
FAN_PWM_1_ADDR,
FAN_PWM_2_ADDR,
};
static const uint16_t qc71_fan_temp_addrs[] = {
FAN_TEMP_1_ADDR,
FAN_TEMP_2_ADDR,
};
/* ========================================================================== */
static DEFINE_MUTEX(fan_lock);
/* ========================================================================== */
static int qc71_fan_get_status(void)
{
return ec_read_byte(FAN_CTRL_ADDR);
}
/* 'fan_lock' must be held */
static int qc71_fan_get_mode_unlocked(void)
{
int err;
lockdep_assert_held(&fan_lock);
err = ec_read_byte(CTRL_1_ADDR);
if (err < 0)
return err;
if (err & CTRL_1_MANUAL_MODE) {
err = qc71_fan_get_status();
if (err < 0)
return err;
if (err & FAN_CTRL_FAN_BOOST) {
err = qc71_fan_get_pwm(0);
if (err < 0)
return err;
if (err == FAN_MAX_PWM)
err = 0; /* disengaged */
else
err = 1; /* manual */
} else if (err & FAN_CTRL_AUTO) {
err = 2; /* automatic fan control */
} else {
err = 1; /* manual */
}
} else {
err = 2; /* automatic fan control */
}
return err;
}
/* ========================================================================== */
int qc71_fan_get_rpm(uint8_t fan_index)
{
union qc71_ec_result res;
int err;
if (fan_index >= ARRAY_SIZE(qc71_fan_rpm_addrs))
return -EINVAL;
err = qc71_ec_read(qc71_fan_rpm_addrs[fan_index], &res);
if (err)
return err;
return res.bytes.b1 << 8 | res.bytes.b2;
}
int qc71_fan_query_abnorm(void)
{
int res = ec_read_byte(CTRL_1_ADDR);
if (res < 0)
return res;
return !!(res & CTRL_1_FAN_ABNORMAL);
}
int qc71_fan_get_pwm(uint8_t fan_index)
{
int err;
if (fan_index >= ARRAY_SIZE(qc71_fan_pwm_addrs))
return -EINVAL;
err = ec_read_byte(qc71_fan_pwm_addrs[fan_index]);
if (err < 0)
return err;
return fixp_linear_interpolate(0, 0, FAN_MAX_PWM, U8_MAX, err);
}
int qc71_fan_set_pwm(uint8_t fan_index, uint8_t pwm)
{
if (fan_index >= ARRAY_SIZE(qc71_fan_pwm_addrs))
return -EINVAL;
return ec_write_byte(qc71_fan_pwm_addrs[fan_index],
fixp_linear_interpolate(0, 0,
U8_MAX, FAN_MAX_PWM,
pwm));
}
int qc71_fan_get_temp(uint8_t fan_index)
{
if (fan_index >= ARRAY_SIZE(qc71_fan_temp_addrs))
return -EINVAL;
return ec_read_byte(qc71_fan_temp_addrs[fan_index]);
}
int qc71_fan_get_mode(void)
{
int err = mutex_lock_interruptible(&fan_lock);
if (err)
return err;
err = qc71_fan_get_mode_unlocked();
mutex_unlock(&fan_lock);
return err;
}
int qc71_fan_set_mode(uint8_t mode)
{
int err, oldpwm;
err = mutex_lock_interruptible(&fan_lock);
if (err)
return err;
switch (mode) {
case 0:
err = ec_write_byte(FAN_CTRL_ADDR, FAN_CTRL_FAN_BOOST);
if (err)
goto out;
err = qc71_fan_set_pwm(0, FAN_MAX_PWM);
break;
case 1:
oldpwm = err = qc71_fan_get_pwm(0);
if (err < 0)
goto out;
err = ec_write_byte(FAN_CTRL_ADDR, FAN_CTRL_FAN_BOOST);
if (err < 0)
goto out;
err = qc71_fan_set_pwm(0, oldpwm);
if (err < 0)
(void) ec_write_byte(FAN_CTRL_ADDR, 0x80 | FAN_CTRL_AUTO);
/* try to restore automatic fan control */
break;
case 2:
err = ec_write_byte(FAN_CTRL_ADDR, 0x80 | FAN_CTRL_AUTO);
break;
default:
err = -EINVAL;
break;
}
out:
mutex_unlock(&fan_lock);
return err;
}