-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem76_legacy_hwmon.c
280 lines (249 loc) · 7.58 KB
/
system76_legacy_hwmon.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* fan.c
*
* Copyright (C) 2019 Simon Doppler <[email protected]>
* Copyright (C) 2017 Jeremy Soller <[email protected]>
* Copyright (C) 2014-2016 Arnoud Willemsen <[email protected]>
* Copyright (C) 2013-2015 TUXEDO Computers GmbH <[email protected]>
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#define EXPERIMENTAL
#if S76LEGACY_HAS_HWMON
struct s76_hwmon {
struct device *dev;
};
static struct s76_hwmon *s76_hwmon = NULL;
static int
s76_read_fan(int idx) {
u8 value;
int raw_rpm;
ec_read(0xd0 + 0x2 * idx, &value);
raw_rpm = value << 8;
ec_read(0xd1 + 0x2 * idx, &value);
raw_rpm += value;
if (!raw_rpm)
return 0;
return 2156220 / raw_rpm;
}
static int
s76_read_pwm(int idx) {
u8 value;
ec_read(0xce + idx, &value);
return value;
}
static int
s76_write_pwm(int idx, u8 duty) {
u8 values[] = {idx + 1, duty};
return ec_transaction(0x99, values, sizeof(values), NULL, 0);
}
static int
s76_write_pwm_auto(int idx) {
u8 values[] = {0xff, idx + 1};
return ec_transaction(0x99, values, sizeof(values), NULL, 0);
}
static ssize_t
s76_hwmon_show_name(struct device *dev, struct device_attribute *attr,
char *buf)
{
return sprintf(buf, S76LEGACY_DRIVER_NAME "\n");
}
static ssize_t
s76_hwmon_show_fan_input(struct device *dev, struct device_attribute *attr,
char *buf)
{
int index = to_sensor_dev_attr(attr)->index;
return sprintf(buf, "%i\n", s76_read_fan(index));
}
static ssize_t
s76_hwmon_show_fan_label(struct device *dev, struct device_attribute *attr,
char *buf)
{
switch (to_sensor_dev_attr(attr)->index) {
case 0:
return sprintf(buf, "CPU fan\n");
case 1:
return sprintf(buf, "GPU fan\n");
}
return 0;
}
static int pwm_enabled[] = {2, 2};
static ssize_t
s76_hwmon_show_pwm(struct device *dev, struct device_attribute *attr,
char *buf)
{
int index = to_sensor_dev_attr(attr)->index;
return sprintf(buf, "%i\n", s76_read_pwm(index));
}
static ssize_t
s76_hwmon_set_pwm(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
u32 value;
int err;
int index = to_sensor_dev_attr(attr)->index;
err = kstrtou32(buf, 10, &value);
if (err) return err;
if (value > 255) return -EINVAL;
err = s76_write_pwm(index, value);
if (err) return err;
pwm_enabled[index] = 1;
return count;
}
static ssize_t
s76_hwmon_show_pwm_enable(struct device *dev, struct device_attribute *attr,
char *buf)
{
int index = to_sensor_dev_attr(attr)->index;
return sprintf(buf, "%i\n", pwm_enabled[index]);
}
static ssize_t
s76_hwmon_set_pwm_enable(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
u32 value;
int err;
int index = to_sensor_dev_attr(attr)->index;
err = kstrtou32(buf, 10, &value);
if (err) return err;
if (value == 0) {
err = s76_write_pwm(index, 255);
if (err) return err;
pwm_enabled[index] = value;
return count;
}
if (value == 1) {
err = s76_write_pwm(index, 0);
if (err) return err;
pwm_enabled[index] = value;
return count;
}
if (value == 2) {
err = s76_write_pwm_auto(index);
if (err) return err;
pwm_enabled[index] = value;
return count;
}
return -EINVAL;
}
static ssize_t
s76_hwmon_show_temp1_input(struct device *dev, struct device_attribute *attr, char *buf) {
u8 value;
ec_read(0x07, &value);
return sprintf(buf, "%i\n", value * 1000);
}
static ssize_t
s76_hwmon_show_temp1_label(struct device *dev, struct device_attribute *attr, char *buf) {
return sprintf(buf, "CPU temperature\n");
}
#ifdef EXPERIMENTAL
static ssize_t
s76_hwmon_show_temp2_input(struct device *dev, struct device_attribute *attr, char *buf) {
u8 value;
ec_read(0xcd, &value);
return sprintf(buf, "%i\n", value * 1000);
}
static ssize_t
s76_hwmon_show_temp2_label(struct device *dev, struct device_attribute *attr, char *buf) {
return sprintf(buf, "GPU temperature\n");
}
#endif
static SENSOR_DEVICE_ATTR(name, S_IRUGO, s76_hwmon_show_name, NULL, 0);
static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, s76_hwmon_show_fan_input, NULL, 0);
static SENSOR_DEVICE_ATTR(fan1_label, S_IRUGO, s76_hwmon_show_fan_label, NULL, 0);
static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, s76_hwmon_show_pwm, s76_hwmon_set_pwm, 0);
static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, s76_hwmon_show_pwm_enable, s76_hwmon_set_pwm_enable, 0);
#ifdef EXPERIMENTAL
static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, s76_hwmon_show_fan_input, NULL, 1);
static SENSOR_DEVICE_ATTR(fan2_label, S_IRUGO, s76_hwmon_show_fan_label, NULL, 1);
static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, s76_hwmon_show_pwm, s76_hwmon_set_pwm, 1);
static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR, s76_hwmon_show_pwm_enable, s76_hwmon_set_pwm_enable, 1);
#endif
static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, s76_hwmon_show_temp1_input, NULL, 0);
static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, s76_hwmon_show_temp1_label, NULL, 0);
#ifdef EXPERIMENTAL
static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, s76_hwmon_show_temp2_input, NULL, 1);
static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, s76_hwmon_show_temp2_label, NULL, 1);
#endif
static struct attribute *hwmon_default_attributes[] = {
&sensor_dev_attr_name.dev_attr.attr,
&sensor_dev_attr_fan1_input.dev_attr.attr,
&sensor_dev_attr_fan1_label.dev_attr.attr,
&sensor_dev_attr_pwm1.dev_attr.attr,
&sensor_dev_attr_pwm1_enable.dev_attr.attr,
#ifdef EXPERIMENTAL
&sensor_dev_attr_fan2_input.dev_attr.attr,
&sensor_dev_attr_fan2_label.dev_attr.attr,
&sensor_dev_attr_pwm2.dev_attr.attr,
&sensor_dev_attr_pwm2_enable.dev_attr.attr,
#endif
&sensor_dev_attr_temp1_input.dev_attr.attr,
&sensor_dev_attr_temp1_label.dev_attr.attr,
#ifdef EXPERIMENTAL
&sensor_dev_attr_temp2_input.dev_attr.attr,
&sensor_dev_attr_temp2_label.dev_attr.attr,
#endif
NULL
};
static const struct attribute_group hwmon_default_attrgroup = {
.attrs = hwmon_default_attributes,
};
static int s76_hwmon_reboot_callback(struct notifier_block *nb,
unsigned long action, void *data) {
s76_write_pwm_auto(0);
#ifdef EXPERIMENTAL
s76_write_pwm_auto(1);
#endif
return NOTIFY_DONE;
}
static struct notifier_block s76_hwmon_reboot_notifier = {
.notifier_call = s76_hwmon_reboot_callback
};
static int
s76_hwmon_init(struct device *dev) {
int ret;
s76_hwmon = kzalloc(sizeof(*s76_hwmon), GFP_KERNEL);
if (!s76_hwmon)
return -ENOMEM;
s76_hwmon->dev = hwmon_device_register(dev);
if (IS_ERR(s76_hwmon->dev)) {
ret = PTR_ERR(s76_hwmon->dev);
s76_hwmon->dev = NULL;
return ret;
}
ret = sysfs_create_group(&s76_hwmon->dev->kobj, &hwmon_default_attrgroup);
if (ret)
return ret;
register_reboot_notifier(&s76_hwmon_reboot_notifier);
s76_write_pwm_auto(0);
#ifdef EXPERIMENTAL
s76_write_pwm_auto(1);
#endif
return 0;
}
static int
s76_hwmon_fini(struct device *dev) {
if (!s76_hwmon || !s76_hwmon->dev)
return 0;
s76_write_pwm_auto(0);
#ifdef EXPERIMENTAL
s76_write_pwm_auto(1);
#endif
unregister_reboot_notifier(&s76_hwmon_reboot_notifier);
sysfs_remove_group(&s76_hwmon->dev->kobj, &hwmon_default_attrgroup);
hwmon_device_unregister(s76_hwmon->dev);
kfree(s76_hwmon);
return 0;
}
#endif // S76LEGACY_HAS_HWMON