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