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[] = {
31 MODULE_DEVICE_TABLE(acpi, fan_device_ids);
33 #ifdef CONFIG_PM_SLEEP
34 static int acpi_fan_suspend(struct device *dev);
35 static int acpi_fan_resume(struct device *dev);
36 static const struct dev_pm_ops acpi_fan_pm = {
37 .resume = acpi_fan_resume,
38 .freeze = acpi_fan_suspend,
39 .thaw = acpi_fan_resume,
40 .restore = acpi_fan_resume,
42 #define FAN_PM_OPS_PTR (&acpi_fan_pm)
44 #define FAN_PM_OPS_PTR NULL
59 u64 low_speed_notification;
64 struct acpi_fan_fif fif;
65 struct acpi_fan_fps *fps;
67 struct thermal_cooling_device *cdev;
70 static struct platform_driver acpi_fan_driver = {
71 .probe = acpi_fan_probe,
72 .remove = acpi_fan_remove,
75 .acpi_match_table = fan_device_ids,
80 /* thermal cooling device callbacks */
81 static int fan_get_max_state(struct thermal_cooling_device *cdev, unsigned long
84 struct acpi_device *device = cdev->devdata;
85 struct acpi_fan *fan = acpi_driver_data(device);
88 *state = fan->fps_count - 1;
94 static int fan_get_state_acpi4(struct acpi_device *device, unsigned long *state)
96 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
97 struct acpi_fan *fan = acpi_driver_data(device);
98 union acpi_object *obj;
102 status = acpi_evaluate_object(device->handle, "_FST", NULL, &buffer);
103 if (ACPI_FAILURE(status)) {
104 dev_err(&device->dev, "Get fan state failed\n");
108 obj = buffer.pointer;
109 if (!obj || obj->type != ACPI_TYPE_PACKAGE ||
110 obj->package.count != 3 ||
111 obj->package.elements[1].type != ACPI_TYPE_INTEGER) {
112 dev_err(&device->dev, "Invalid _FST data\n");
117 control = obj->package.elements[1].integer.value;
118 for (i = 0; i < fan->fps_count; i++) {
120 * When Fine Grain Control is set, return the state
121 * corresponding to maximum fan->fps[i].control
122 * value compared to the current speed. Here the
123 * fan->fps[] is sorted array with increasing speed.
125 if (fan->fif.fine_grain_ctrl && control < fan->fps[i].control) {
126 i = (i > 0) ? i - 1 : 0;
128 } else if (control == fan->fps[i].control) {
132 if (i == fan->fps_count) {
133 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);
186 if (state >= fan->fps_count)
189 status = acpi_execute_simple_method(device->handle, "_FSL",
190 fan->fps[state].control);
191 if (ACPI_FAILURE(status)) {
192 dev_dbg(&device->dev, "Failed to set state by _FSL\n");
200 fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
202 struct acpi_device *device = cdev->devdata;
203 struct acpi_fan *fan = acpi_driver_data(device);
206 return fan_set_state_acpi4(device, state);
208 return fan_set_state(device, state);
211 static const struct thermal_cooling_device_ops fan_cooling_ops = {
212 .get_max_state = fan_get_max_state,
213 .get_cur_state = fan_get_cur_state,
214 .set_cur_state = fan_set_cur_state,
217 /* --------------------------------------------------------------------------
219 * --------------------------------------------------------------------------
222 static bool acpi_fan_is_acpi4(struct acpi_device *device)
224 return acpi_has_method(device->handle, "_FIF") &&
225 acpi_has_method(device->handle, "_FPS") &&
226 acpi_has_method(device->handle, "_FSL") &&
227 acpi_has_method(device->handle, "_FST");
230 static int acpi_fan_get_fif(struct acpi_device *device)
232 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
233 struct acpi_fan *fan = acpi_driver_data(device);
234 struct acpi_buffer format = { sizeof("NNNN"), "NNNN" };
235 struct acpi_buffer fif = { sizeof(fan->fif), &fan->fif };
236 union acpi_object *obj;
239 status = acpi_evaluate_object(device->handle, "_FIF", NULL, &buffer);
240 if (ACPI_FAILURE(status))
243 obj = buffer.pointer;
244 if (!obj || obj->type != ACPI_TYPE_PACKAGE) {
245 dev_err(&device->dev, "Invalid _FIF data\n");
250 status = acpi_extract_package(obj, &format, &fif);
251 if (ACPI_FAILURE(status)) {
252 dev_err(&device->dev, "Invalid _FIF element\n");
261 static int acpi_fan_speed_cmp(const void *a, const void *b)
263 const struct acpi_fan_fps *fps1 = a;
264 const struct acpi_fan_fps *fps2 = b;
265 return fps1->speed - fps2->speed;
268 static int acpi_fan_get_fps(struct acpi_device *device)
270 struct acpi_fan *fan = acpi_driver_data(device);
271 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
272 union acpi_object *obj;
276 status = acpi_evaluate_object(device->handle, "_FPS", NULL, &buffer);
277 if (ACPI_FAILURE(status))
280 obj = buffer.pointer;
281 if (!obj || obj->type != ACPI_TYPE_PACKAGE || obj->package.count < 2) {
282 dev_err(&device->dev, "Invalid _FPS data\n");
287 fan->fps_count = obj->package.count - 1; /* minus revision field */
288 fan->fps = devm_kcalloc(&device->dev,
289 fan->fps_count, sizeof(struct acpi_fan_fps),
292 dev_err(&device->dev, "Not enough memory\n");
296 for (i = 0; i < fan->fps_count; i++) {
297 struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
298 struct acpi_buffer fps = { sizeof(fan->fps[i]), &fan->fps[i] };
299 status = acpi_extract_package(&obj->package.elements[i + 1],
301 if (ACPI_FAILURE(status)) {
302 dev_err(&device->dev, "Invalid _FPS element\n");
307 /* sort the state array according to fan speed in increase order */
308 sort(fan->fps, fan->fps_count, sizeof(*fan->fps),
309 acpi_fan_speed_cmp, NULL);
316 static int acpi_fan_probe(struct platform_device *pdev)
319 struct thermal_cooling_device *cdev;
320 struct acpi_fan *fan;
321 struct acpi_device *device = ACPI_COMPANION(&pdev->dev);
324 fan = devm_kzalloc(&pdev->dev, sizeof(*fan), GFP_KERNEL);
326 dev_err(&device->dev, "No memory for fan\n");
329 device->driver_data = fan;
330 platform_set_drvdata(pdev, fan);
332 if (acpi_fan_is_acpi4(device)) {
333 if (acpi_fan_get_fif(device) || acpi_fan_get_fps(device))
337 result = acpi_device_update_power(device, NULL);
339 dev_err(&device->dev, "Failed to set initial power state\n");
344 if (!strncmp(pdev->name, "PNP0C0B", strlen("PNP0C0B")))
347 name = acpi_device_bid(device);
349 cdev = thermal_cooling_device_register(name, device,
352 result = PTR_ERR(cdev);
356 dev_dbg(&pdev->dev, "registered as cooling_device%d\n", cdev->id);
359 result = sysfs_create_link(&pdev->dev.kobj,
363 dev_err(&pdev->dev, "Failed to create sysfs link 'thermal_cooling'\n");
365 result = sysfs_create_link(&cdev->device.kobj,
369 dev_err(&pdev->dev, "Failed to create sysfs link 'device'\n");
375 static int acpi_fan_remove(struct platform_device *pdev)
377 struct acpi_fan *fan = platform_get_drvdata(pdev);
379 sysfs_remove_link(&pdev->dev.kobj, "thermal_cooling");
380 sysfs_remove_link(&fan->cdev->device.kobj, "device");
381 thermal_cooling_device_unregister(fan->cdev);
386 #ifdef CONFIG_PM_SLEEP
387 static int acpi_fan_suspend(struct device *dev)
389 struct acpi_fan *fan = dev_get_drvdata(dev);
393 acpi_device_set_power(ACPI_COMPANION(dev), ACPI_STATE_D0);
398 static int acpi_fan_resume(struct device *dev)
401 struct acpi_fan *fan = dev_get_drvdata(dev);
406 result = acpi_device_update_power(ACPI_COMPANION(dev), NULL);
408 dev_err(dev, "Error updating fan power state\n");
414 module_platform_driver(acpi_fan_driver);