Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/drivers/acpi/fan_core.c
29267 views
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
/*
3
* fan_core.c - ACPI Fan core Driver
4
*
5
* Copyright (C) 2001, 2002 Andy Grover <[email protected]>
6
* Copyright (C) 2001, 2002 Paul Diefenbaugh <[email protected]>
7
* Copyright (C) 2022 Intel Corporation. All rights reserved.
8
*/
9
10
#include <linux/kernel.h>
11
#include <linux/module.h>
12
#include <linux/init.h>
13
#include <linux/types.h>
14
#include <linux/uaccess.h>
15
#include <linux/thermal.h>
16
#include <linux/acpi.h>
17
#include <linux/platform_device.h>
18
#include <linux/sort.h>
19
20
#include "fan.h"
21
22
static const struct acpi_device_id fan_device_ids[] = {
23
ACPI_FAN_DEVICE_IDS,
24
{"", 0},
25
};
26
MODULE_DEVICE_TABLE(acpi, fan_device_ids);
27
28
/* thermal cooling device callbacks */
29
static int fan_get_max_state(struct thermal_cooling_device *cdev, unsigned long
30
*state)
31
{
32
struct acpi_device *device = cdev->devdata;
33
struct acpi_fan *fan = acpi_driver_data(device);
34
35
if (fan->acpi4) {
36
if (fan->fif.fine_grain_ctrl)
37
*state = 100 / fan->fif.step_size;
38
else
39
*state = fan->fps_count - 1;
40
} else {
41
*state = 1;
42
}
43
44
return 0;
45
}
46
47
int acpi_fan_get_fst(struct acpi_device *device, struct acpi_fan_fst *fst)
48
{
49
struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
50
union acpi_object *obj;
51
acpi_status status;
52
int ret = 0;
53
54
status = acpi_evaluate_object(device->handle, "_FST", NULL, &buffer);
55
if (ACPI_FAILURE(status)) {
56
dev_err(&device->dev, "Get fan state failed\n");
57
return -ENODEV;
58
}
59
60
obj = buffer.pointer;
61
if (!obj || obj->type != ACPI_TYPE_PACKAGE ||
62
obj->package.count != 3 ||
63
obj->package.elements[1].type != ACPI_TYPE_INTEGER) {
64
dev_err(&device->dev, "Invalid _FST data\n");
65
ret = -EINVAL;
66
goto err;
67
}
68
69
fst->revision = obj->package.elements[0].integer.value;
70
fst->control = obj->package.elements[1].integer.value;
71
fst->speed = obj->package.elements[2].integer.value;
72
73
err:
74
kfree(obj);
75
return ret;
76
}
77
78
static int fan_get_state_acpi4(struct acpi_device *device, unsigned long *state)
79
{
80
struct acpi_fan *fan = acpi_driver_data(device);
81
struct acpi_fan_fst fst;
82
int status, i;
83
84
status = acpi_fan_get_fst(device, &fst);
85
if (status)
86
return status;
87
88
if (fan->fif.fine_grain_ctrl) {
89
/* This control should be same what we set using _FSL by spec */
90
if (fst.control > 100) {
91
dev_dbg(&device->dev, "Invalid control value returned\n");
92
goto match_fps;
93
}
94
95
*state = (int) fst.control / fan->fif.step_size;
96
return 0;
97
}
98
99
match_fps:
100
for (i = 0; i < fan->fps_count; i++) {
101
if (fst.control == fan->fps[i].control)
102
break;
103
}
104
if (i == fan->fps_count) {
105
dev_dbg(&device->dev, "No matching fps control value\n");
106
return -EINVAL;
107
}
108
109
*state = i;
110
111
return status;
112
}
113
114
static int fan_get_state(struct acpi_device *device, unsigned long *state)
115
{
116
int result;
117
int acpi_state = ACPI_STATE_D0;
118
119
result = acpi_device_update_power(device, &acpi_state);
120
if (result)
121
return result;
122
123
*state = acpi_state == ACPI_STATE_D3_COLD
124
|| acpi_state == ACPI_STATE_D3_HOT ?
125
0 : (acpi_state == ACPI_STATE_D0 ? 1 : -1);
126
return 0;
127
}
128
129
static int fan_get_cur_state(struct thermal_cooling_device *cdev, unsigned long
130
*state)
131
{
132
struct acpi_device *device = cdev->devdata;
133
struct acpi_fan *fan = acpi_driver_data(device);
134
135
if (fan->acpi4)
136
return fan_get_state_acpi4(device, state);
137
else
138
return fan_get_state(device, state);
139
}
140
141
static int fan_set_state(struct acpi_device *device, unsigned long state)
142
{
143
if (state != 0 && state != 1)
144
return -EINVAL;
145
146
return acpi_device_set_power(device,
147
state ? ACPI_STATE_D0 : ACPI_STATE_D3_COLD);
148
}
149
150
static int fan_set_state_acpi4(struct acpi_device *device, unsigned long state)
151
{
152
struct acpi_fan *fan = acpi_driver_data(device);
153
acpi_status status;
154
u64 value = state;
155
int max_state;
156
157
if (fan->fif.fine_grain_ctrl)
158
max_state = 100 / fan->fif.step_size;
159
else
160
max_state = fan->fps_count - 1;
161
162
if (state > max_state)
163
return -EINVAL;
164
165
if (fan->fif.fine_grain_ctrl) {
166
value *= fan->fif.step_size;
167
/* Spec allows compensate the last step only */
168
if (value + fan->fif.step_size > 100)
169
value = 100;
170
} else {
171
value = fan->fps[state].control;
172
}
173
174
status = acpi_execute_simple_method(device->handle, "_FSL", value);
175
if (ACPI_FAILURE(status)) {
176
dev_dbg(&device->dev, "Failed to set state by _FSL\n");
177
return -ENODEV;
178
}
179
180
return 0;
181
}
182
183
static int
184
fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
185
{
186
struct acpi_device *device = cdev->devdata;
187
struct acpi_fan *fan = acpi_driver_data(device);
188
189
if (fan->acpi4)
190
return fan_set_state_acpi4(device, state);
191
else
192
return fan_set_state(device, state);
193
}
194
195
static const struct thermal_cooling_device_ops fan_cooling_ops = {
196
.get_max_state = fan_get_max_state,
197
.get_cur_state = fan_get_cur_state,
198
.set_cur_state = fan_set_cur_state,
199
};
200
201
/* --------------------------------------------------------------------------
202
* Driver Interface
203
* --------------------------------------------------------------------------
204
*/
205
206
static int acpi_fan_get_fif(struct acpi_device *device)
207
{
208
struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
209
struct acpi_fan *fan = acpi_driver_data(device);
210
struct acpi_buffer format = { sizeof("NNNN"), "NNNN" };
211
u64 fields[4];
212
struct acpi_buffer fif = { sizeof(fields), fields };
213
union acpi_object *obj;
214
acpi_status status;
215
216
status = acpi_evaluate_object(device->handle, "_FIF", NULL, &buffer);
217
if (ACPI_FAILURE(status))
218
return status;
219
220
obj = buffer.pointer;
221
if (!obj || obj->type != ACPI_TYPE_PACKAGE) {
222
dev_err(&device->dev, "Invalid _FIF data\n");
223
status = -EINVAL;
224
goto err;
225
}
226
227
status = acpi_extract_package(obj, &format, &fif);
228
if (ACPI_FAILURE(status)) {
229
dev_err(&device->dev, "Invalid _FIF element\n");
230
status = -EINVAL;
231
goto err;
232
}
233
234
fan->fif.revision = fields[0];
235
fan->fif.fine_grain_ctrl = fields[1];
236
fan->fif.step_size = fields[2];
237
fan->fif.low_speed_notification = fields[3];
238
239
/* If there is a bug in step size and set as 0, change to 1 */
240
if (!fan->fif.step_size)
241
fan->fif.step_size = 1;
242
/* If step size > 9, change to 9 (by spec valid values 1-9) */
243
else if (fan->fif.step_size > 9)
244
fan->fif.step_size = 9;
245
err:
246
kfree(obj);
247
return status;
248
}
249
250
static int acpi_fan_speed_cmp(const void *a, const void *b)
251
{
252
const struct acpi_fan_fps *fps1 = a;
253
const struct acpi_fan_fps *fps2 = b;
254
return fps1->speed - fps2->speed;
255
}
256
257
static int acpi_fan_get_fps(struct acpi_device *device)
258
{
259
struct acpi_fan *fan = acpi_driver_data(device);
260
struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
261
union acpi_object *obj;
262
acpi_status status;
263
int i;
264
265
status = acpi_evaluate_object(device->handle, "_FPS", NULL, &buffer);
266
if (ACPI_FAILURE(status))
267
return status;
268
269
obj = buffer.pointer;
270
if (!obj || obj->type != ACPI_TYPE_PACKAGE || obj->package.count < 2) {
271
dev_err(&device->dev, "Invalid _FPS data\n");
272
status = -EINVAL;
273
goto err;
274
}
275
276
fan->fps_count = obj->package.count - 1; /* minus revision field */
277
fan->fps = devm_kcalloc(&device->dev,
278
fan->fps_count, sizeof(struct acpi_fan_fps),
279
GFP_KERNEL);
280
if (!fan->fps) {
281
dev_err(&device->dev, "Not enough memory\n");
282
status = -ENOMEM;
283
goto err;
284
}
285
for (i = 0; i < fan->fps_count; i++) {
286
struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
287
struct acpi_buffer fps = { offsetof(struct acpi_fan_fps, name),
288
&fan->fps[i] };
289
status = acpi_extract_package(&obj->package.elements[i + 1],
290
&format, &fps);
291
if (ACPI_FAILURE(status)) {
292
dev_err(&device->dev, "Invalid _FPS element\n");
293
goto err;
294
}
295
}
296
297
/* sort the state array according to fan speed in increase order */
298
sort(fan->fps, fan->fps_count, sizeof(*fan->fps),
299
acpi_fan_speed_cmp, NULL);
300
301
err:
302
kfree(obj);
303
return status;
304
}
305
306
static int acpi_fan_probe(struct platform_device *pdev)
307
{
308
int result = 0;
309
struct thermal_cooling_device *cdev;
310
struct acpi_fan *fan;
311
struct acpi_device *device = ACPI_COMPANION(&pdev->dev);
312
char *name;
313
314
fan = devm_kzalloc(&pdev->dev, sizeof(*fan), GFP_KERNEL);
315
if (!fan) {
316
dev_err(&device->dev, "No memory for fan\n");
317
return -ENOMEM;
318
}
319
device->driver_data = fan;
320
platform_set_drvdata(pdev, fan);
321
322
if (acpi_has_method(device->handle, "_FST")) {
323
fan->has_fst = true;
324
fan->acpi4 = acpi_has_method(device->handle, "_FIF") &&
325
acpi_has_method(device->handle, "_FPS") &&
326
acpi_has_method(device->handle, "_FSL");
327
}
328
329
if (fan->acpi4) {
330
result = acpi_fan_get_fif(device);
331
if (result)
332
return result;
333
334
result = acpi_fan_get_fps(device);
335
if (result)
336
return result;
337
}
338
339
if (fan->has_fst) {
340
result = devm_acpi_fan_create_hwmon(device);
341
if (result)
342
return result;
343
344
result = acpi_fan_create_attributes(device);
345
if (result)
346
return result;
347
}
348
349
if (!fan->acpi4) {
350
result = acpi_device_update_power(device, NULL);
351
if (result) {
352
dev_err(&device->dev, "Failed to set initial power state\n");
353
goto err_end;
354
}
355
}
356
357
if (!strncmp(pdev->name, "PNP0C0B", strlen("PNP0C0B")))
358
name = "Fan";
359
else
360
name = acpi_device_bid(device);
361
362
cdev = thermal_cooling_device_register(name, device,
363
&fan_cooling_ops);
364
if (IS_ERR(cdev)) {
365
result = PTR_ERR(cdev);
366
goto err_end;
367
}
368
369
dev_dbg(&pdev->dev, "registered as cooling_device%d\n", cdev->id);
370
371
fan->cdev = cdev;
372
result = sysfs_create_link(&pdev->dev.kobj,
373
&cdev->device.kobj,
374
"thermal_cooling");
375
if (result) {
376
dev_err(&pdev->dev, "Failed to create sysfs link 'thermal_cooling'\n");
377
goto err_unregister;
378
}
379
380
result = sysfs_create_link(&cdev->device.kobj,
381
&pdev->dev.kobj,
382
"device");
383
if (result) {
384
dev_err(&pdev->dev, "Failed to create sysfs link 'device'\n");
385
goto err_remove_link;
386
}
387
388
return 0;
389
390
err_remove_link:
391
sysfs_remove_link(&pdev->dev.kobj, "thermal_cooling");
392
err_unregister:
393
thermal_cooling_device_unregister(cdev);
394
err_end:
395
if (fan->has_fst)
396
acpi_fan_delete_attributes(device);
397
398
return result;
399
}
400
401
static void acpi_fan_remove(struct platform_device *pdev)
402
{
403
struct acpi_fan *fan = platform_get_drvdata(pdev);
404
405
if (fan->has_fst) {
406
struct acpi_device *device = ACPI_COMPANION(&pdev->dev);
407
408
acpi_fan_delete_attributes(device);
409
}
410
sysfs_remove_link(&pdev->dev.kobj, "thermal_cooling");
411
sysfs_remove_link(&fan->cdev->device.kobj, "device");
412
thermal_cooling_device_unregister(fan->cdev);
413
}
414
415
#ifdef CONFIG_PM_SLEEP
416
static int acpi_fan_suspend(struct device *dev)
417
{
418
struct acpi_fan *fan = dev_get_drvdata(dev);
419
if (fan->acpi4)
420
return 0;
421
422
acpi_device_set_power(ACPI_COMPANION(dev), ACPI_STATE_D0);
423
424
return AE_OK;
425
}
426
427
static int acpi_fan_resume(struct device *dev)
428
{
429
int result;
430
struct acpi_fan *fan = dev_get_drvdata(dev);
431
432
if (fan->acpi4)
433
return 0;
434
435
result = acpi_device_update_power(ACPI_COMPANION(dev), NULL);
436
if (result)
437
dev_err(dev, "Error updating fan power state\n");
438
439
return result;
440
}
441
442
static const struct dev_pm_ops acpi_fan_pm = {
443
.resume = acpi_fan_resume,
444
.freeze = acpi_fan_suspend,
445
.thaw = acpi_fan_resume,
446
.restore = acpi_fan_resume,
447
};
448
#define FAN_PM_OPS_PTR (&acpi_fan_pm)
449
450
#else
451
452
#define FAN_PM_OPS_PTR NULL
453
454
#endif
455
456
static struct platform_driver acpi_fan_driver = {
457
.probe = acpi_fan_probe,
458
.remove = acpi_fan_remove,
459
.driver = {
460
.name = "acpi-fan",
461
.acpi_match_table = fan_device_ids,
462
.pm = FAN_PM_OPS_PTR,
463
},
464
};
465
466
module_platform_driver(acpi_fan_driver);
467
468
MODULE_AUTHOR("Paul Diefenbaugh");
469
MODULE_DESCRIPTION("ACPI Fan Driver");
470
MODULE_LICENSE("GPL");
471
472