-
Notifications
You must be signed in to change notification settings - Fork 0
/
framework_hwmon.c
429 lines (343 loc) · 11 KB
/
framework_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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
// SPDX-License-Identifier: GPL-2.0+
/*
* Framework Laptop HWMON Driver
*
* Copyright (C) 2024 Stephen Horvath
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <linux/hwmon-sysfs.h>
#include <linux/hwmon.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_data/cros_ec_commands.h>
#include <linux/platform_data/cros_ec_proto.h>
#include <linux/platform_device.h>
#define DRV_NAME "framework_hwmon"
static struct device *hwmon_dev;
static struct device *ec_device;
// --- fanN_input ---
// Read the current fan speed from the EC's memory
static ssize_t ec_get_fan_speed(u8 idx, u16 *val)
{
if (!ec_device)
return -ENODEV;
struct cros_ec_device *ec = dev_get_drvdata(ec_device);
const u8 offset = EC_MEMMAP_FAN + 2 * idx;
return ec->cmd_readmem(ec, offset, sizeof(*val), val);
}
static ssize_t fw_fan_speed_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct sensor_device_attribute *sen_attr = to_sensor_dev_attr(attr);
u16 val;
if (ec_get_fan_speed(sen_attr->index, &val) < 0) {
return -EIO;
}
if (val == EC_FAN_SPEED_NOT_PRESENT || val == EC_FAN_SPEED_STALLED) {
return sysfs_emit(buf, "%u\n", 0);
}
// Format as string for sysfs
return sysfs_emit(buf, "%u\n", val);
}
// --- fanN_target ---
static ssize_t ec_set_target_rpm(u8 idx, u32 *val)
{
int ret;
if (!ec_device)
return -ENODEV;
struct cros_ec_device *ec = dev_get_drvdata(ec_device);
struct ec_params_pwm_set_fan_target_rpm_v1 params = {
.rpm = *val,
.fan_idx = idx,
};
ret = cros_ec_cmd(ec, 1, EC_CMD_PWM_SET_FAN_TARGET_RPM, ¶ms,
sizeof(params), NULL, 0);
if (ret < 0)
return -EIO;
return 0;
}
static ssize_t ec_get_target_rpm(u8 idx, u32 *val)
{
int ret;
if (!ec_device)
return -ENODEV;
struct cros_ec_device *ec = dev_get_drvdata(ec_device);
struct ec_response_pwm_get_fan_rpm resp;
// index isn't supported, it should only return fan 0's target
ret = cros_ec_cmd(ec, 0, EC_CMD_PWM_GET_FAN_TARGET_RPM, NULL, 0, &resp,
sizeof(resp));
if (ret < 0)
return -EIO;
*val = resp.rpm;
return 0;
}
static ssize_t fw_fan_target_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct sensor_device_attribute *sen_attr = to_sensor_dev_attr(attr);
u32 val;
int err;
err = kstrtou32(buf, 10, &val);
if (err < 0)
return err;
if (ec_set_target_rpm(sen_attr->index, &val) < 0) {
return -EIO;
}
return count;
}
static ssize_t fw_fan_target_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct sensor_device_attribute *sen_attr = to_sensor_dev_attr(attr);
// Only fan 0 is supported
if (sen_attr->index != 0) {
return -EINVAL;
}
u32 val;
if (ec_get_target_rpm(sen_attr->index, &val) < 0) {
return -EIO;
}
// Format as string for sysfs
return sysfs_emit(buf, "%u\n", val);
}
// --- fanN_fault ---
static ssize_t fw_fan_fault_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct sensor_device_attribute *sen_attr = to_sensor_dev_attr(attr);
u16 val;
if (ec_get_fan_speed(sen_attr->index, &val) < 0) {
return -EIO;
}
// Format as string for sysfs
return sysfs_emit(buf, "%u\n", val == EC_FAN_SPEED_NOT_PRESENT);
}
// --- fanN_alarm ---
static ssize_t fw_fan_alarm_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct sensor_device_attribute *sen_attr = to_sensor_dev_attr(attr);
u16 val;
if (ec_get_fan_speed(sen_attr->index, &val) < 0) {
return -EIO;
}
// Format as string for sysfs
return sysfs_emit(buf, "%u\n", val == EC_FAN_SPEED_STALLED);
}
// --- pwmN_enable ---
static ssize_t ec_set_auto_fan_ctrl(u8 idx)
{
int ret;
if (!ec_device)
return -ENODEV;
struct cros_ec_device *ec = dev_get_drvdata(ec_device);
struct ec_params_auto_fan_ctrl_v1 params = {
.fan_idx = idx,
};
ret = cros_ec_cmd(ec, 1, EC_CMD_THERMAL_AUTO_FAN_CTRL, ¶ms,
sizeof(params), NULL, 0);
if (ret < 0)
return -EIO;
return 0;
}
static ssize_t fw_pwm_enable_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct sensor_device_attribute *sen_attr = to_sensor_dev_attr(attr);
// The EC doesn't take any arguments for this command,
// so we don't need to parse the buffer
// u32 val;
// int err;
// err = kstrtou32(buf, 10, &val);
// if (err < 0)
// return err;
if (ec_set_auto_fan_ctrl(sen_attr->index) < 0) {
return -EIO;
}
return count;
}
// --- pwmN ---
static ssize_t ec_set_fan_duty(u8 idx, u32 *val)
{
int ret;
if (!ec_device)
return -ENODEV;
struct cros_ec_device *ec = dev_get_drvdata(ec_device);
struct ec_params_pwm_set_fan_duty_v1 params = {
.percent = *val,
.fan_idx = idx,
};
ret = cros_ec_cmd(ec, 1, EC_CMD_PWM_SET_FAN_DUTY, ¶ms,
sizeof(params), NULL, 0);
if (ret < 0)
return -EIO;
return 0;
}
static ssize_t fw_pwm_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
struct sensor_device_attribute *sen_attr = to_sensor_dev_attr(attr);
u32 val;
int err;
err = kstrtou32(buf, 10, &val);
if (err < 0)
return err;
if (ec_set_fan_duty(sen_attr->index, &val) < 0) {
return -EIO;
}
return count;
}
static ssize_t fw_pwm_min_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
return sysfs_emit(buf, "%i\n", 0);
}
static ssize_t fw_pwm_max_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
return sysfs_emit(buf, "%i\n", 100);
}
// --- utility functions ---
static ssize_t ec_count_fans(size_t *val)
{
if (!ec_device)
return -ENODEV;
struct cros_ec_device *ec = dev_get_drvdata(ec_device);
u16 fans[EC_FAN_SPEED_ENTRIES];
int ret = ec->cmd_readmem(ec, EC_MEMMAP_FAN, sizeof(fans), fans);
if (ret < 0)
return -EIO;
for (size_t i = 0; i < EC_FAN_SPEED_ENTRIES; i++) {
if (fans[i] == EC_FAN_SPEED_NOT_PRESENT) {
*val = i;
return 0;
}
}
*val = EC_FAN_SPEED_ENTRIES;
return 0;
}
#define FW_ATTRS_PER_FAN 8
// clang-format off
static SENSOR_DEVICE_ATTR_RO(fan1_input, fw_fan_speed, 0); // Fan Reading
static SENSOR_DEVICE_ATTR_RW(fan1_target, fw_fan_target, 0); // Target RPM (RW on fan 0 only)
static SENSOR_DEVICE_ATTR_RO(fan1_fault, fw_fan_fault, 0); // Fan Fault
static SENSOR_DEVICE_ATTR_RO(fan1_alarm, fw_fan_alarm, 0); // Fan Alarm
static SENSOR_DEVICE_ATTR_WO(pwm1_enable, fw_pwm_enable, 0); // Set Fan Control Mode
static SENSOR_DEVICE_ATTR_WO(pwm1, fw_pwm, 0); // Set Fan Speed
static SENSOR_DEVICE_ATTR_RO(pwm1_min, fw_pwm_min, 0); // Min Fan Speed
static SENSOR_DEVICE_ATTR_RO(pwm1_max, fw_pwm_max, 0); // Max Fan Speed
static SENSOR_DEVICE_ATTR_RO(fan2_input, fw_fan_speed, 1);
static SENSOR_DEVICE_ATTR_WO(fan2_target, fw_fan_target, 1);
static SENSOR_DEVICE_ATTR_RO(fan2_fault, fw_fan_fault, 1);
static SENSOR_DEVICE_ATTR_RO(fan2_alarm, fw_fan_alarm, 1);
static SENSOR_DEVICE_ATTR_WO(pwm2_enable, fw_pwm_enable, 1);
static SENSOR_DEVICE_ATTR_WO(pwm2, fw_pwm, 1);
static SENSOR_DEVICE_ATTR_RO(pwm2_min, fw_pwm_min, 1);
static SENSOR_DEVICE_ATTR_RO(pwm2_max, fw_pwm_max, 1);
static SENSOR_DEVICE_ATTR_RO(fan3_input, fw_fan_speed, 2);
static SENSOR_DEVICE_ATTR_WO(fan3_target, fw_fan_target, 2);
static SENSOR_DEVICE_ATTR_RO(fan3_fault, fw_fan_fault, 2);
static SENSOR_DEVICE_ATTR_RO(fan3_alarm, fw_fan_alarm, 2);
static SENSOR_DEVICE_ATTR_WO(pwm3_enable, fw_pwm_enable, 2);
static SENSOR_DEVICE_ATTR_WO(pwm3, fw_pwm, 2);
static SENSOR_DEVICE_ATTR_RO(pwm3_min, fw_pwm_min, 2);
static SENSOR_DEVICE_ATTR_RO(pwm3_max, fw_pwm_max, 2);
static SENSOR_DEVICE_ATTR_RO(fan4_input, fw_fan_speed, 3);
static SENSOR_DEVICE_ATTR_WO(fan4_target, fw_fan_target, 3);
static SENSOR_DEVICE_ATTR_RO(fan4_fault, fw_fan_fault, 3);
static SENSOR_DEVICE_ATTR_RO(fan4_alarm, fw_fan_alarm, 3);
static SENSOR_DEVICE_ATTR_WO(pwm4_enable, fw_pwm_enable, 3);
static SENSOR_DEVICE_ATTR_WO(pwm4, fw_pwm, 3);
static SENSOR_DEVICE_ATTR_RO(pwm4_min, fw_pwm_min, 3);
static SENSOR_DEVICE_ATTR_RO(pwm4_max, fw_pwm_max, 3);
// clang-format on
static struct attribute
*fw_hwmon_attrs[(EC_FAN_SPEED_ENTRIES * FW_ATTRS_PER_FAN) + 1] = {
&sensor_dev_attr_fan1_input.dev_attr.attr,
&sensor_dev_attr_fan1_target.dev_attr.attr,
&sensor_dev_attr_fan1_fault.dev_attr.attr,
&sensor_dev_attr_fan1_alarm.dev_attr.attr,
&sensor_dev_attr_pwm1_enable.dev_attr.attr,
&sensor_dev_attr_pwm1.dev_attr.attr,
&sensor_dev_attr_pwm1_min.dev_attr.attr,
&sensor_dev_attr_pwm1_max.dev_attr.attr,
&sensor_dev_attr_fan2_input.dev_attr.attr,
&sensor_dev_attr_fan2_target.dev_attr.attr,
&sensor_dev_attr_fan2_fault.dev_attr.attr,
&sensor_dev_attr_fan2_alarm.dev_attr.attr,
&sensor_dev_attr_pwm2_enable.dev_attr.attr,
&sensor_dev_attr_pwm2.dev_attr.attr,
&sensor_dev_attr_pwm2_min.dev_attr.attr,
&sensor_dev_attr_pwm2_max.dev_attr.attr,
&sensor_dev_attr_fan3_input.dev_attr.attr,
&sensor_dev_attr_fan3_target.dev_attr.attr,
&sensor_dev_attr_fan3_fault.dev_attr.attr,
&sensor_dev_attr_fan3_alarm.dev_attr.attr,
&sensor_dev_attr_pwm3_enable.dev_attr.attr,
&sensor_dev_attr_pwm3.dev_attr.attr,
&sensor_dev_attr_pwm3_min.dev_attr.attr,
&sensor_dev_attr_pwm3_max.dev_attr.attr,
&sensor_dev_attr_fan4_input.dev_attr.attr,
&sensor_dev_attr_fan4_target.dev_attr.attr,
&sensor_dev_attr_fan4_fault.dev_attr.attr,
&sensor_dev_attr_fan4_alarm.dev_attr.attr,
&sensor_dev_attr_pwm4_enable.dev_attr.attr,
&sensor_dev_attr_pwm4.dev_attr.attr,
&sensor_dev_attr_pwm4_min.dev_attr.attr,
&sensor_dev_attr_pwm4_max.dev_attr.attr,
NULL,
};
static const struct attribute_group fw_hwmon_group = {
.attrs = fw_hwmon_attrs,
};
static const struct attribute_group *fw_hwmon_groups[] = { &fw_hwmon_group,
NULL };
static int device_match_cros_ec(struct device *dev, const void *foo)
{
const char *name = dev_name(dev);
if (strncmp(name, "cros-ec-dev", 11))
return 0;
return 1;
}
static int __init fw_hwmon_init(void)
{
ec_device = bus_find_device(&platform_bus_type, NULL, NULL,
device_match_cros_ec);
if (!ec_device) {
printk(KERN_WARNING DRV_NAME ": failed to find EC.\n");
return -EINVAL;
}
ec_device = ec_device->parent;
struct cros_ec_device *ec = dev_get_drvdata(ec_device);
if (!ec->cmd_readmem) {
printk(KERN_WARNING DRV_NAME ": EC not supported.\n");
return -EINVAL;
}
size_t fan_count;
if (ec_count_fans(&fan_count) < 0) {
printk(KERN_WARNING DRV_NAME ": failed to count fans.\n");
return -EINVAL;
}
// NULL terminates the list after the last detected fan
fw_hwmon_attrs[fan_count * FW_ATTRS_PER_FAN] = NULL;
hwmon_dev = hwmon_device_register_with_groups(ec_device, DRV_NAME, NULL,
fw_hwmon_groups);
if (IS_ERR(hwmon_dev))
return PTR_ERR(hwmon_dev);
return 0;
}
static void __exit fw_hwmon_exit(void)
{
hwmon_device_unregister(hwmon_dev);
}
module_init(fw_hwmon_init);
module_exit(fw_hwmon_exit);
MODULE_DESCRIPTION("Framework Laptop HWMON Driver");
MODULE_AUTHOR("Stephen Horvath <[email protected]>");
MODULE_LICENSE("GPL");
MODULE_SOFTDEP("pre: cros_ec");