1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * acpi_fan.c - ACPI Fan Driver ($Revision: 29 $)
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/types.h>
13 #include <linux/uaccess.h>
14 #include <linux/thermal.h>
15 #include <linux/acpi.h>
16 #include <linux/platform_device.h>
17 #include <linux/sort.h>
19 MODULE_AUTHOR("Paul Diefenbaugh");
20 MODULE_DESCRIPTION("ACPI Fan Driver");
21 MODULE_LICENSE("GPL");
23 static int acpi_fan_probe(struct platform_device *pdev);
24 static int acpi_fan_remove(struct platform_device *pdev);
26 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 #define ACPI_FPS_NAME_LEN 20
57 char name[ACPI_FPS_NAME_LEN];
58 struct device_attribute dev_attr;
65 u64 low_speed_notification;
70 struct acpi_fan_fif fif;
71 struct acpi_fan_fps *fps;
73 struct thermal_cooling_device *cdev;
76 static struct platform_driver acpi_fan_driver = {
77 .probe = acpi_fan_probe,
78 .remove = acpi_fan_remove,
81 .acpi_match_table = fan_device_ids,
86 /* thermal cooling device callbacks */
87 static int fan_get_max_state(struct thermal_cooling_device *cdev, unsigned long
90 struct acpi_device *device = cdev->devdata;
91 struct acpi_fan *fan = acpi_driver_data(device);
94 *state = fan->fps_count - 1;
100 static int fan_get_state_acpi4(struct acpi_device *device, unsigned long *state)
102 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
103 struct acpi_fan *fan = acpi_driver_data(device);
104 union acpi_object *obj;
108 status = acpi_evaluate_object(device->handle, "_FST", NULL, &buffer);
109 if (ACPI_FAILURE(status)) {
110 dev_err(&device->dev, "Get fan state failed\n");
114 obj = buffer.pointer;
115 if (!obj || obj->type != ACPI_TYPE_PACKAGE ||
116 obj->package.count != 3 ||
117 obj->package.elements[1].type != ACPI_TYPE_INTEGER) {
118 dev_err(&device->dev, "Invalid _FST data\n");
123 control = obj->package.elements[1].integer.value;
124 for (i = 0; i < fan->fps_count; i++) {
126 * When Fine Grain Control is set, return the state
127 * corresponding to maximum fan->fps[i].control
128 * value compared to the current speed. Here the
129 * fan->fps[] is sorted array with increasing speed.
131 if (fan->fif.fine_grain_ctrl && control < fan->fps[i].control) {
132 i = (i > 0) ? i - 1 : 0;
134 } else if (control == fan->fps[i].control) {
138 if (i == fan->fps_count) {
139 dev_dbg(&device->dev, "Invalid control value returned\n");
151 static int fan_get_state(struct acpi_device *device, unsigned long *state)
154 int acpi_state = ACPI_STATE_D0;
156 result = acpi_device_update_power(device, &acpi_state);
160 *state = acpi_state == ACPI_STATE_D3_COLD
161 || acpi_state == ACPI_STATE_D3_HOT ?
162 0 : (acpi_state == ACPI_STATE_D0 ? 1 : -1);
166 static int fan_get_cur_state(struct thermal_cooling_device *cdev, unsigned long
169 struct acpi_device *device = cdev->devdata;
170 struct acpi_fan *fan = acpi_driver_data(device);
173 return fan_get_state_acpi4(device, state);
175 return fan_get_state(device, state);
178 static int fan_set_state(struct acpi_device *device, unsigned long state)
180 if (state != 0 && state != 1)
183 return acpi_device_set_power(device,
184 state ? ACPI_STATE_D0 : ACPI_STATE_D3_COLD);
187 static int fan_set_state_acpi4(struct acpi_device *device, unsigned long state)
189 struct acpi_fan *fan = acpi_driver_data(device);
192 if (state >= fan->fps_count)
195 status = acpi_execute_simple_method(device->handle, "_FSL",
196 fan->fps[state].control);
197 if (ACPI_FAILURE(status)) {
198 dev_dbg(&device->dev, "Failed to set state by _FSL\n");
206 fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
208 struct acpi_device *device = cdev->devdata;
209 struct acpi_fan *fan = acpi_driver_data(device);
212 return fan_set_state_acpi4(device, state);
214 return fan_set_state(device, state);
217 static const struct thermal_cooling_device_ops fan_cooling_ops = {
218 .get_max_state = fan_get_max_state,
219 .get_cur_state = fan_get_cur_state,
220 .set_cur_state = fan_set_cur_state,
223 /* --------------------------------------------------------------------------
225 * --------------------------------------------------------------------------
228 static bool acpi_fan_is_acpi4(struct acpi_device *device)
230 return acpi_has_method(device->handle, "_FIF") &&
231 acpi_has_method(device->handle, "_FPS") &&
232 acpi_has_method(device->handle, "_FSL") &&
233 acpi_has_method(device->handle, "_FST");
236 static int acpi_fan_get_fif(struct acpi_device *device)
238 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
239 struct acpi_fan *fan = acpi_driver_data(device);
240 struct acpi_buffer format = { sizeof("NNNN"), "NNNN" };
241 struct acpi_buffer fif = { sizeof(fan->fif), &fan->fif };
242 union acpi_object *obj;
245 status = acpi_evaluate_object(device->handle, "_FIF", NULL, &buffer);
246 if (ACPI_FAILURE(status))
249 obj = buffer.pointer;
250 if (!obj || obj->type != ACPI_TYPE_PACKAGE) {
251 dev_err(&device->dev, "Invalid _FIF data\n");
256 status = acpi_extract_package(obj, &format, &fif);
257 if (ACPI_FAILURE(status)) {
258 dev_err(&device->dev, "Invalid _FIF element\n");
267 static int acpi_fan_speed_cmp(const void *a, const void *b)
269 const struct acpi_fan_fps *fps1 = a;
270 const struct acpi_fan_fps *fps2 = b;
271 return fps1->speed - fps2->speed;
274 static ssize_t show_state(struct device *dev, struct device_attribute *attr, char *buf)
276 struct acpi_fan_fps *fps = container_of(attr, struct acpi_fan_fps, dev_attr);
279 if (fps->control == 0xFFFFFFFF || fps->control > 100)
280 count = scnprintf(buf, PAGE_SIZE, "not-defined:");
282 count = scnprintf(buf, PAGE_SIZE, "%lld:", fps->control);
284 if (fps->trip_point == 0xFFFFFFFF || fps->trip_point > 9)
285 count += scnprintf(&buf[count], PAGE_SIZE - count, "not-defined:");
287 count += scnprintf(&buf[count], PAGE_SIZE - count, "%lld:", fps->trip_point);
289 if (fps->speed == 0xFFFFFFFF)
290 count += scnprintf(&buf[count], PAGE_SIZE - count, "not-defined:");
292 count += scnprintf(&buf[count], PAGE_SIZE - count, "%lld:", fps->speed);
294 if (fps->noise_level == 0xFFFFFFFF)
295 count += scnprintf(&buf[count], PAGE_SIZE - count, "not-defined:");
297 count += scnprintf(&buf[count], PAGE_SIZE - count, "%lld:", fps->noise_level * 100);
299 if (fps->power == 0xFFFFFFFF)
300 count += scnprintf(&buf[count], PAGE_SIZE - count, "not-defined\n");
302 count += scnprintf(&buf[count], PAGE_SIZE - count, "%lld\n", fps->power);
307 static int acpi_fan_get_fps(struct acpi_device *device)
309 struct acpi_fan *fan = acpi_driver_data(device);
310 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
311 union acpi_object *obj;
315 status = acpi_evaluate_object(device->handle, "_FPS", NULL, &buffer);
316 if (ACPI_FAILURE(status))
319 obj = buffer.pointer;
320 if (!obj || obj->type != ACPI_TYPE_PACKAGE || obj->package.count < 2) {
321 dev_err(&device->dev, "Invalid _FPS data\n");
326 fan->fps_count = obj->package.count - 1; /* minus revision field */
327 fan->fps = devm_kcalloc(&device->dev,
328 fan->fps_count, sizeof(struct acpi_fan_fps),
331 dev_err(&device->dev, "Not enough memory\n");
335 for (i = 0; i < fan->fps_count; i++) {
336 struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
337 struct acpi_buffer fps = { offsetof(struct acpi_fan_fps, name),
339 status = acpi_extract_package(&obj->package.elements[i + 1],
341 if (ACPI_FAILURE(status)) {
342 dev_err(&device->dev, "Invalid _FPS element\n");
347 /* sort the state array according to fan speed in increase order */
348 sort(fan->fps, fan->fps_count, sizeof(*fan->fps),
349 acpi_fan_speed_cmp, NULL);
351 for (i = 0; i < fan->fps_count; ++i) {
352 struct acpi_fan_fps *fps = &fan->fps[i];
354 snprintf(fps->name, ACPI_FPS_NAME_LEN, "state%d", i);
355 sysfs_attr_init(&fps->dev_attr.attr);
356 fps->dev_attr.show = show_state;
357 fps->dev_attr.store = NULL;
358 fps->dev_attr.attr.name = fps->name;
359 fps->dev_attr.attr.mode = 0444;
360 status = sysfs_create_file(&device->dev.kobj, &fps->dev_attr.attr);
364 for (j = 0; j < i; ++j)
365 sysfs_remove_file(&device->dev.kobj, &fan->fps[j].dev_attr.attr);
375 static int acpi_fan_probe(struct platform_device *pdev)
378 struct thermal_cooling_device *cdev;
379 struct acpi_fan *fan;
380 struct acpi_device *device = ACPI_COMPANION(&pdev->dev);
383 fan = devm_kzalloc(&pdev->dev, sizeof(*fan), GFP_KERNEL);
385 dev_err(&device->dev, "No memory for fan\n");
388 device->driver_data = fan;
389 platform_set_drvdata(pdev, fan);
391 if (acpi_fan_is_acpi4(device)) {
392 result = acpi_fan_get_fif(device);
396 result = acpi_fan_get_fps(device);
402 result = acpi_device_update_power(device, NULL);
404 dev_err(&device->dev, "Failed to set initial power state\n");
409 if (!strncmp(pdev->name, "PNP0C0B", strlen("PNP0C0B")))
412 name = acpi_device_bid(device);
414 cdev = thermal_cooling_device_register(name, device,
417 result = PTR_ERR(cdev);
421 dev_dbg(&pdev->dev, "registered as cooling_device%d\n", cdev->id);
424 result = sysfs_create_link(&pdev->dev.kobj,
428 dev_err(&pdev->dev, "Failed to create sysfs link 'thermal_cooling'\n");
430 result = sysfs_create_link(&cdev->device.kobj,
434 dev_err(&pdev->dev, "Failed to create sysfs link 'device'\n");
444 for (i = 0; i < fan->fps_count; ++i)
445 sysfs_remove_file(&device->dev.kobj, &fan->fps[i].dev_attr.attr);
451 static int acpi_fan_remove(struct platform_device *pdev)
453 struct acpi_fan *fan = platform_get_drvdata(pdev);
456 struct acpi_device *device = ACPI_COMPANION(&pdev->dev);
459 for (i = 0; i < fan->fps_count; ++i)
460 sysfs_remove_file(&device->dev.kobj, &fan->fps[i].dev_attr.attr);
462 sysfs_remove_link(&pdev->dev.kobj, "thermal_cooling");
463 sysfs_remove_link(&fan->cdev->device.kobj, "device");
464 thermal_cooling_device_unregister(fan->cdev);
469 #ifdef CONFIG_PM_SLEEP
470 static int acpi_fan_suspend(struct device *dev)
472 struct acpi_fan *fan = dev_get_drvdata(dev);
476 acpi_device_set_power(ACPI_COMPANION(dev), ACPI_STATE_D0);
481 static int acpi_fan_resume(struct device *dev)
484 struct acpi_fan *fan = dev_get_drvdata(dev);
489 result = acpi_device_update_power(ACPI_COMPANION(dev), NULL);
491 dev_err(dev, "Error updating fan power state\n");
497 module_platform_driver(acpi_fan_driver);