1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * fan_core.c - ACPI Fan core Driver
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.
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>
22 MODULE_AUTHOR("Paul Diefenbaugh");
23 MODULE_DESCRIPTION("ACPI Fan Driver");
24 MODULE_LICENSE("GPL");
26 static int acpi_fan_probe(struct platform_device *pdev);
27 static int acpi_fan_remove(struct platform_device *pdev);
29 static const struct acpi_device_id fan_device_ids[] = {
33 MODULE_DEVICE_TABLE(acpi, fan_device_ids);
35 #ifdef CONFIG_PM_SLEEP
36 static int acpi_fan_suspend(struct device *dev);
37 static int acpi_fan_resume(struct device *dev);
38 static const struct dev_pm_ops acpi_fan_pm = {
39 .resume = acpi_fan_resume,
40 .freeze = acpi_fan_suspend,
41 .thaw = acpi_fan_resume,
42 .restore = acpi_fan_resume,
44 #define FAN_PM_OPS_PTR (&acpi_fan_pm)
46 #define FAN_PM_OPS_PTR NULL
49 static struct platform_driver acpi_fan_driver = {
50 .probe = acpi_fan_probe,
51 .remove = acpi_fan_remove,
54 .acpi_match_table = fan_device_ids,
59 /* thermal cooling device callbacks */
60 static int fan_get_max_state(struct thermal_cooling_device *cdev, unsigned long
63 struct acpi_device *device = cdev->devdata;
64 struct acpi_fan *fan = acpi_driver_data(device);
67 if (fan->fif.fine_grain_ctrl)
68 *state = 100 / fan->fif.step_size;
70 *state = fan->fps_count - 1;
78 int acpi_fan_get_fst(struct acpi_device *device, struct acpi_fan_fst *fst)
80 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
81 union acpi_object *obj;
85 status = acpi_evaluate_object(device->handle, "_FST", NULL, &buffer);
86 if (ACPI_FAILURE(status)) {
87 dev_err(&device->dev, "Get fan state failed\n");
92 if (!obj || obj->type != ACPI_TYPE_PACKAGE ||
93 obj->package.count != 3 ||
94 obj->package.elements[1].type != ACPI_TYPE_INTEGER) {
95 dev_err(&device->dev, "Invalid _FST data\n");
100 fst->revision = obj->package.elements[0].integer.value;
101 fst->control = obj->package.elements[1].integer.value;
102 fst->speed = obj->package.elements[2].integer.value;
109 static int fan_get_state_acpi4(struct acpi_device *device, unsigned long *state)
111 struct acpi_fan *fan = acpi_driver_data(device);
112 struct acpi_fan_fst fst;
115 status = acpi_fan_get_fst(device, &fst);
119 if (fan->fif.fine_grain_ctrl) {
120 /* This control should be same what we set using _FSL by spec */
121 if (fst.control > 100) {
122 dev_dbg(&device->dev, "Invalid control value returned\n");
126 *state = (int) fst.control / fan->fif.step_size;
131 for (i = 0; i < fan->fps_count; i++) {
132 if (fst.control == fan->fps[i].control)
135 if (i == fan->fps_count) {
136 dev_dbg(&device->dev, "Invalid control value returned\n");
145 static int fan_get_state(struct acpi_device *device, unsigned long *state)
148 int acpi_state = ACPI_STATE_D0;
150 result = acpi_device_update_power(device, &acpi_state);
154 *state = acpi_state == ACPI_STATE_D3_COLD
155 || acpi_state == ACPI_STATE_D3_HOT ?
156 0 : (acpi_state == ACPI_STATE_D0 ? 1 : -1);
160 static int fan_get_cur_state(struct thermal_cooling_device *cdev, unsigned long
163 struct acpi_device *device = cdev->devdata;
164 struct acpi_fan *fan = acpi_driver_data(device);
167 return fan_get_state_acpi4(device, state);
169 return fan_get_state(device, state);
172 static int fan_set_state(struct acpi_device *device, unsigned long state)
174 if (state != 0 && state != 1)
177 return acpi_device_set_power(device,
178 state ? ACPI_STATE_D0 : ACPI_STATE_D3_COLD);
181 static int fan_set_state_acpi4(struct acpi_device *device, unsigned long state)
183 struct acpi_fan *fan = acpi_driver_data(device);
188 if (fan->fif.fine_grain_ctrl)
189 max_state = 100 / fan->fif.step_size;
191 max_state = fan->fps_count - 1;
193 if (state > max_state)
196 if (fan->fif.fine_grain_ctrl) {
197 value *= fan->fif.step_size;
198 /* Spec allows compensate the last step only */
199 if (value + fan->fif.step_size > 100)
202 value = fan->fps[state].control;
205 status = acpi_execute_simple_method(device->handle, "_FSL", value);
206 if (ACPI_FAILURE(status)) {
207 dev_dbg(&device->dev, "Failed to set state by _FSL\n");
215 fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
217 struct acpi_device *device = cdev->devdata;
218 struct acpi_fan *fan = acpi_driver_data(device);
221 return fan_set_state_acpi4(device, state);
223 return fan_set_state(device, state);
226 static const struct thermal_cooling_device_ops fan_cooling_ops = {
227 .get_max_state = fan_get_max_state,
228 .get_cur_state = fan_get_cur_state,
229 .set_cur_state = fan_set_cur_state,
232 /* --------------------------------------------------------------------------
234 * --------------------------------------------------------------------------
237 static bool acpi_fan_is_acpi4(struct acpi_device *device)
239 return acpi_has_method(device->handle, "_FIF") &&
240 acpi_has_method(device->handle, "_FPS") &&
241 acpi_has_method(device->handle, "_FSL") &&
242 acpi_has_method(device->handle, "_FST");
245 static int acpi_fan_get_fif(struct acpi_device *device)
247 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
248 struct acpi_fan *fan = acpi_driver_data(device);
249 struct acpi_buffer format = { sizeof("NNNN"), "NNNN" };
251 struct acpi_buffer fif = { sizeof(fields), fields };
252 union acpi_object *obj;
255 status = acpi_evaluate_object(device->handle, "_FIF", NULL, &buffer);
256 if (ACPI_FAILURE(status))
259 obj = buffer.pointer;
260 if (!obj || obj->type != ACPI_TYPE_PACKAGE) {
261 dev_err(&device->dev, "Invalid _FIF data\n");
266 status = acpi_extract_package(obj, &format, &fif);
267 if (ACPI_FAILURE(status)) {
268 dev_err(&device->dev, "Invalid _FIF element\n");
272 fan->fif.revision = fields[0];
273 fan->fif.fine_grain_ctrl = fields[1];
274 fan->fif.step_size = fields[2];
275 fan->fif.low_speed_notification = fields[3];
277 /* If there is a bug in step size and set as 0, change to 1 */
278 if (!fan->fif.step_size)
279 fan->fif.step_size = 1;
280 /* If step size > 9, change to 9 (by spec valid values 1-9) */
281 else if (fan->fif.step_size > 9)
282 fan->fif.step_size = 9;
288 static int acpi_fan_speed_cmp(const void *a, const void *b)
290 const struct acpi_fan_fps *fps1 = a;
291 const struct acpi_fan_fps *fps2 = b;
292 return fps1->speed - fps2->speed;
295 static int acpi_fan_get_fps(struct acpi_device *device)
297 struct acpi_fan *fan = acpi_driver_data(device);
298 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
299 union acpi_object *obj;
303 status = acpi_evaluate_object(device->handle, "_FPS", NULL, &buffer);
304 if (ACPI_FAILURE(status))
307 obj = buffer.pointer;
308 if (!obj || obj->type != ACPI_TYPE_PACKAGE || obj->package.count < 2) {
309 dev_err(&device->dev, "Invalid _FPS data\n");
314 fan->fps_count = obj->package.count - 1; /* minus revision field */
315 fan->fps = devm_kcalloc(&device->dev,
316 fan->fps_count, sizeof(struct acpi_fan_fps),
319 dev_err(&device->dev, "Not enough memory\n");
323 for (i = 0; i < fan->fps_count; i++) {
324 struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
325 struct acpi_buffer fps = { offsetof(struct acpi_fan_fps, name),
327 status = acpi_extract_package(&obj->package.elements[i + 1],
329 if (ACPI_FAILURE(status)) {
330 dev_err(&device->dev, "Invalid _FPS element\n");
335 /* sort the state array according to fan speed in increase order */
336 sort(fan->fps, fan->fps_count, sizeof(*fan->fps),
337 acpi_fan_speed_cmp, NULL);
344 static int acpi_fan_probe(struct platform_device *pdev)
347 struct thermal_cooling_device *cdev;
348 struct acpi_fan *fan;
349 struct acpi_device *device = ACPI_COMPANION(&pdev->dev);
352 fan = devm_kzalloc(&pdev->dev, sizeof(*fan), GFP_KERNEL);
354 dev_err(&device->dev, "No memory for fan\n");
357 device->driver_data = fan;
358 platform_set_drvdata(pdev, fan);
360 if (acpi_fan_is_acpi4(device)) {
361 result = acpi_fan_get_fif(device);
365 result = acpi_fan_get_fps(device);
369 result = acpi_fan_create_attributes(device);
375 result = acpi_device_update_power(device, NULL);
377 dev_err(&device->dev, "Failed to set initial power state\n");
382 if (!strncmp(pdev->name, "PNP0C0B", strlen("PNP0C0B")))
385 name = acpi_device_bid(device);
387 cdev = thermal_cooling_device_register(name, device,
390 result = PTR_ERR(cdev);
394 dev_dbg(&pdev->dev, "registered as cooling_device%d\n", cdev->id);
397 result = sysfs_create_link(&pdev->dev.kobj,
401 dev_err(&pdev->dev, "Failed to create sysfs link 'thermal_cooling'\n");
403 result = sysfs_create_link(&cdev->device.kobj,
407 dev_err(&pdev->dev, "Failed to create sysfs link 'device'\n");
415 acpi_fan_delete_attributes(device);
420 static int acpi_fan_remove(struct platform_device *pdev)
422 struct acpi_fan *fan = platform_get_drvdata(pdev);
425 struct acpi_device *device = ACPI_COMPANION(&pdev->dev);
427 acpi_fan_delete_attributes(device);
429 sysfs_remove_link(&pdev->dev.kobj, "thermal_cooling");
430 sysfs_remove_link(&fan->cdev->device.kobj, "device");
431 thermal_cooling_device_unregister(fan->cdev);
436 #ifdef CONFIG_PM_SLEEP
437 static int acpi_fan_suspend(struct device *dev)
439 struct acpi_fan *fan = dev_get_drvdata(dev);
443 acpi_device_set_power(ACPI_COMPANION(dev), ACPI_STATE_D0);
448 static int acpi_fan_resume(struct device *dev)
451 struct acpi_fan *fan = dev_get_drvdata(dev);
456 result = acpi_device_update_power(ACPI_COMPANION(dev), NULL);
458 dev_err(dev, "Error updating fan power state\n");
464 module_platform_driver(acpi_fan_driver);