blob: 6cf4988206f24e8292099ae7141914ced024affd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * acpi_fan.c - ACPI Fan Driver ($Revision: 29 $)
3 *
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6 *
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or (at
12 * your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 */
21
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/init.h>
25#include <linux/types.h>
Sudip Mukherjee88989fd2014-08-28 19:17:19 +053026#include <linux/uaccess.h>
Zhang Rui05a83d92008-01-17 15:51:24 +080027#include <linux/thermal.h>
Lv Zheng8b484632013-12-03 08:49:16 +080028#include <linux/acpi.h>
Aaron Lu19593a12013-11-19 16:59:20 +080029#include <linux/platform_device.h>
Aaron Lu9519a632014-04-01 15:50:27 +080030#include <linux/sort.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Len Brownf52fd662007-02-12 22:42:12 -050032MODULE_AUTHOR("Paul Diefenbaugh");
Len Brown7cda93e2007-02-12 23:50:02 -050033MODULE_DESCRIPTION("ACPI Fan Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070034MODULE_LICENSE("GPL");
35
Aaron Lu19593a12013-11-19 16:59:20 +080036static int acpi_fan_probe(struct platform_device *pdev);
37static int acpi_fan_remove(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Thomas Renninger1ba90e32007-07-23 14:44:41 +020039static const struct acpi_device_id fan_device_ids[] = {
40 {"PNP0C0B", 0},
Aaron Lud806c6e2014-04-01 15:54:05 +080041 {"INT3404", 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +020042 {"", 0},
43};
44MODULE_DEVICE_TABLE(acpi, fan_device_ids);
45
Rafael J. Wysocki90692402012-08-09 23:00:02 +020046#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki62fcbdd2012-06-27 23:26:07 +020047static int acpi_fan_suspend(struct device *dev);
48static int acpi_fan_resume(struct device *dev);
Kaiyen Chang1d751582016-02-10 15:23:27 -080049static const struct dev_pm_ops acpi_fan_pm = {
Aaron Lub9b85152014-02-19 12:16:48 +080050 .resume = acpi_fan_resume,
51 .freeze = acpi_fan_suspend,
52 .thaw = acpi_fan_resume,
53 .restore = acpi_fan_resume,
54};
55#define FAN_PM_OPS_PTR (&acpi_fan_pm)
Shuah Khanb108e0e2014-02-12 20:19:08 -070056#else
Aaron Lub9b85152014-02-19 12:16:48 +080057#define FAN_PM_OPS_PTR NULL
Rafael J. Wysocki90692402012-08-09 23:00:02 +020058#endif
Rafael J. Wysocki62fcbdd2012-06-27 23:26:07 +020059
Aaron Lu9519a632014-04-01 15:50:27 +080060struct acpi_fan_fps {
61 u64 control;
62 u64 trip_point;
63 u64 speed;
64 u64 noise_level;
65 u64 power;
66};
67
68struct acpi_fan_fif {
69 u64 revision;
70 u64 fine_grain_ctrl;
71 u64 step_size;
72 u64 low_speed_notification;
73};
74
75struct acpi_fan {
76 bool acpi4;
77 struct acpi_fan_fif fif;
78 struct acpi_fan_fps *fps;
79 int fps_count;
80 struct thermal_cooling_device *cdev;
81};
82
Aaron Lu19593a12013-11-19 16:59:20 +080083static struct platform_driver acpi_fan_driver = {
84 .probe = acpi_fan_probe,
85 .remove = acpi_fan_remove,
86 .driver = {
87 .name = "acpi-fan",
88 .acpi_match_table = fan_device_ids,
89 .pm = FAN_PM_OPS_PTR,
90 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070091};
92
Zhang Rui05a83d92008-01-17 15:51:24 +080093/* thermal cooling device callbacks */
Matthew Garrett6503e5d2008-11-27 17:48:13 +000094static int fan_get_max_state(struct thermal_cooling_device *cdev, unsigned long
95 *state)
Zhang Rui05a83d92008-01-17 15:51:24 +080096{
Aaron Lu9519a632014-04-01 15:50:27 +080097 struct acpi_device *device = cdev->devdata;
98 struct acpi_fan *fan = acpi_driver_data(device);
99
100 if (fan->acpi4)
101 *state = fan->fps_count - 1;
102 else
103 *state = 1;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000104 return 0;
Zhang Rui05a83d92008-01-17 15:51:24 +0800105}
106
Aaron Lu9519a632014-04-01 15:50:27 +0800107static int fan_get_state_acpi4(struct acpi_device *device, unsigned long *state)
Zhang Rui05a83d92008-01-17 15:51:24 +0800108{
Aaron Lu9519a632014-04-01 15:50:27 +0800109 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
110 struct acpi_fan *fan = acpi_driver_data(device);
111 union acpi_object *obj;
112 acpi_status status;
113 int control, i;
114
115 status = acpi_evaluate_object(device->handle, "_FST", NULL, &buffer);
116 if (ACPI_FAILURE(status)) {
117 dev_err(&device->dev, "Get fan state failed\n");
118 return status;
119 }
120
121 obj = buffer.pointer;
122 if (!obj || obj->type != ACPI_TYPE_PACKAGE ||
123 obj->package.count != 3 ||
124 obj->package.elements[1].type != ACPI_TYPE_INTEGER) {
125 dev_err(&device->dev, "Invalid _FST data\n");
126 status = -EINVAL;
127 goto err;
128 }
129
130 control = obj->package.elements[1].integer.value;
131 for (i = 0; i < fan->fps_count; i++) {
Srinivas Pandruvada84baf172016-10-04 13:51:40 -0700132 /*
133 * When Fine Grain Control is set, return the state
134 * corresponding to maximum fan->fps[i].control
135 * value compared to the current speed. Here the
136 * fan->fps[] is sorted array with increasing speed.
137 */
138 if (fan->fif.fine_grain_ctrl && control < fan->fps[i].control) {
139 i = (i > 0) ? i - 1 : 0;
Aaron Lu9519a632014-04-01 15:50:27 +0800140 break;
Srinivas Pandruvada84baf172016-10-04 13:51:40 -0700141 } else if (control == fan->fps[i].control) {
142 break;
143 }
Aaron Lu9519a632014-04-01 15:50:27 +0800144 }
145 if (i == fan->fps_count) {
146 dev_dbg(&device->dev, "Invalid control value returned\n");
147 status = -EINVAL;
148 goto err;
149 }
150
151 *state = i;
152
153err:
154 kfree(obj);
155 return status;
156}
157
158static int fan_get_state(struct acpi_device *device, unsigned long *state)
159{
Zhang Rui05a83d92008-01-17 15:51:24 +0800160 int result;
Naresh Bhat85eb9822013-07-01 19:51:00 +0530161 int acpi_state = ACPI_STATE_D0;
Zhang Rui05a83d92008-01-17 15:51:24 +0800162
Aaron Lu2bb3a2b2013-11-19 15:43:52 +0800163 result = acpi_device_update_power(device, &acpi_state);
Zhang Rui05a83d92008-01-17 15:51:24 +0800164 if (result)
165 return result;
166
Rafael J. Wysocki20dacb72015-05-16 01:55:35 +0200167 *state = acpi_state == ACPI_STATE_D3_COLD
168 || acpi_state == ACPI_STATE_D3_HOT ?
169 0 : (acpi_state == ACPI_STATE_D0 ? 1 : -1);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000170 return 0;
Zhang Rui05a83d92008-01-17 15:51:24 +0800171}
172
Aaron Lu9519a632014-04-01 15:50:27 +0800173static int fan_get_cur_state(struct thermal_cooling_device *cdev, unsigned long
174 *state)
175{
176 struct acpi_device *device = cdev->devdata;
177 struct acpi_fan *fan = acpi_driver_data(device);
178
179 if (fan->acpi4)
180 return fan_get_state_acpi4(device, state);
181 else
182 return fan_get_state(device, state);
183}
184
185static int fan_set_state(struct acpi_device *device, unsigned long state)
186{
187 if (state != 0 && state != 1)
188 return -EINVAL;
189
190 return acpi_device_set_power(device,
191 state ? ACPI_STATE_D0 : ACPI_STATE_D3_COLD);
192}
193
194static int fan_set_state_acpi4(struct acpi_device *device, unsigned long state)
195{
196 struct acpi_fan *fan = acpi_driver_data(device);
197 acpi_status status;
198
199 if (state >= fan->fps_count)
200 return -EINVAL;
201
202 status = acpi_execute_simple_method(device->handle, "_FSL",
203 fan->fps[state].control);
204 if (ACPI_FAILURE(status)) {
205 dev_dbg(&device->dev, "Failed to set state by _FSL\n");
206 return status;
207 }
208
209 return 0;
210}
211
Zhang Rui05a83d92008-01-17 15:51:24 +0800212static int
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000213fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
Zhang Rui05a83d92008-01-17 15:51:24 +0800214{
215 struct acpi_device *device = cdev->devdata;
Aaron Lu9519a632014-04-01 15:50:27 +0800216 struct acpi_fan *fan = acpi_driver_data(device);
Zhang Rui05a83d92008-01-17 15:51:24 +0800217
Aaron Lu9519a632014-04-01 15:50:27 +0800218 if (fan->acpi4)
219 return fan_set_state_acpi4(device, state);
220 else
221 return fan_set_state(device, state);
222 }
Zhang Rui05a83d92008-01-17 15:51:24 +0800223
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +0400224static const struct thermal_cooling_device_ops fan_cooling_ops = {
Zhang Rui05a83d92008-01-17 15:51:24 +0800225 .get_max_state = fan_get_max_state,
226 .get_cur_state = fan_get_cur_state,
227 .set_cur_state = fan_set_cur_state,
228};
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230/* --------------------------------------------------------------------------
Sudip Mukherjee88989fd2014-08-28 19:17:19 +0530231 * Driver Interface
232 * --------------------------------------------------------------------------
233*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Aaron Lu9519a632014-04-01 15:50:27 +0800235static bool acpi_fan_is_acpi4(struct acpi_device *device)
236{
237 return acpi_has_method(device->handle, "_FIF") &&
238 acpi_has_method(device->handle, "_FPS") &&
239 acpi_has_method(device->handle, "_FSL") &&
240 acpi_has_method(device->handle, "_FST");
241}
242
243static int acpi_fan_get_fif(struct acpi_device *device)
244{
245 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
246 struct acpi_fan *fan = acpi_driver_data(device);
247 struct acpi_buffer format = { sizeof("NNNN"), "NNNN" };
248 struct acpi_buffer fif = { sizeof(fan->fif), &fan->fif };
249 union acpi_object *obj;
250 acpi_status status;
251
252 status = acpi_evaluate_object(device->handle, "_FIF", NULL, &buffer);
253 if (ACPI_FAILURE(status))
254 return status;
255
256 obj = buffer.pointer;
257 if (!obj || obj->type != ACPI_TYPE_PACKAGE) {
258 dev_err(&device->dev, "Invalid _FIF data\n");
259 status = -EINVAL;
260 goto err;
261 }
262
263 status = acpi_extract_package(obj, &format, &fif);
264 if (ACPI_FAILURE(status)) {
265 dev_err(&device->dev, "Invalid _FIF element\n");
266 status = -EINVAL;
267 }
268
269err:
270 kfree(obj);
271 return status;
272}
273
274static int acpi_fan_speed_cmp(const void *a, const void *b)
275{
276 const struct acpi_fan_fps *fps1 = a;
277 const struct acpi_fan_fps *fps2 = b;
278 return fps1->speed - fps2->speed;
279}
280
281static int acpi_fan_get_fps(struct acpi_device *device)
282{
283 struct acpi_fan *fan = acpi_driver_data(device);
284 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
285 union acpi_object *obj;
286 acpi_status status;
287 int i;
288
289 status = acpi_evaluate_object(device->handle, "_FPS", NULL, &buffer);
290 if (ACPI_FAILURE(status))
291 return status;
292
293 obj = buffer.pointer;
294 if (!obj || obj->type != ACPI_TYPE_PACKAGE || obj->package.count < 2) {
295 dev_err(&device->dev, "Invalid _FPS data\n");
296 status = -EINVAL;
297 goto err;
298 }
299
300 fan->fps_count = obj->package.count - 1; /* minus revision field */
301 fan->fps = devm_kzalloc(&device->dev,
302 fan->fps_count * sizeof(struct acpi_fan_fps),
303 GFP_KERNEL);
304 if (!fan->fps) {
305 dev_err(&device->dev, "Not enough memory\n");
306 status = -ENOMEM;
307 goto err;
308 }
309 for (i = 0; i < fan->fps_count; i++) {
310 struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
311 struct acpi_buffer fps = { sizeof(fan->fps[i]), &fan->fps[i] };
312 status = acpi_extract_package(&obj->package.elements[i + 1],
313 &format, &fps);
314 if (ACPI_FAILURE(status)) {
315 dev_err(&device->dev, "Invalid _FPS element\n");
316 break;
317 }
318 }
319
320 /* sort the state array according to fan speed in increase order */
321 sort(fan->fps, fan->fps_count, sizeof(*fan->fps),
322 acpi_fan_speed_cmp, NULL);
323
324err:
325 kfree(obj);
326 return status;
327}
328
Aaron Lu19593a12013-11-19 16:59:20 +0800329static int acpi_fan_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Len Brown4be44fc2005-08-05 00:44:28 -0400331 int result = 0;
Zhang Rui05a83d92008-01-17 15:51:24 +0800332 struct thermal_cooling_device *cdev;
Aaron Lu9519a632014-04-01 15:50:27 +0800333 struct acpi_fan *fan;
Aaron Lu19593a12013-11-19 16:59:20 +0800334 struct acpi_device *device = ACPI_COMPANION(&pdev->dev);
Srinivas Pandruvadabbb16fe2014-12-09 16:15:40 -0800335 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Aaron Lu9519a632014-04-01 15:50:27 +0800337 fan = devm_kzalloc(&pdev->dev, sizeof(*fan), GFP_KERNEL);
338 if (!fan) {
339 dev_err(&device->dev, "No memory for fan\n");
340 return -ENOMEM;
341 }
342 device->driver_data = fan;
343 platform_set_drvdata(pdev, fan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Aaron Lu9519a632014-04-01 15:50:27 +0800345 if (acpi_fan_is_acpi4(device)) {
346 if (acpi_fan_get_fif(device) || acpi_fan_get_fps(device))
347 goto end;
348 fan->acpi4 = true;
349 } else {
350 result = acpi_device_update_power(device, NULL);
351 if (result) {
Andy Lutomirskif9727992016-01-13 14:25:19 -0800352 dev_err(&device->dev, "Failed to set initial power state\n");
Aaron Lu9519a632014-04-01 15:50:27 +0800353 goto end;
354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
356
Srinivas Pandruvadabbb16fe2014-12-09 16:15:40 -0800357 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,
Zhang Rui05a83d92008-01-17 15:51:24 +0800363 &fan_cooling_ops);
Thomas Sujith19b36782008-02-15 01:01:52 -0500364 if (IS_ERR(cdev)) {
365 result = PTR_ERR(cdev);
366 goto end;
367 }
Zhang Rui05a83d92008-01-17 15:51:24 +0800368
Aaron Lu19593a12013-11-19 16:59:20 +0800369 dev_dbg(&pdev->dev, "registered as cooling_device%d\n", cdev->id);
Thomas Sujith19b36782008-02-15 01:01:52 -0500370
Aaron Lu9519a632014-04-01 15:50:27 +0800371 fan->cdev = cdev;
Aaron Lu19593a12013-11-19 16:59:20 +0800372 result = sysfs_create_link(&pdev->dev.kobj,
Julia Lawall90300622008-04-11 10:09:24 +0800373 &cdev->device.kobj,
374 "thermal_cooling");
375 if (result)
Linus Torvalds8264fce2014-10-24 11:21:43 -0700376 dev_err(&pdev->dev, "Failed to create sysfs link 'thermal_cooling'\n");
Julia Lawall90300622008-04-11 10:09:24 +0800377
378 result = sysfs_create_link(&cdev->device.kobj,
Aaron Lu19593a12013-11-19 16:59:20 +0800379 &pdev->dev.kobj,
Julia Lawall90300622008-04-11 10:09:24 +0800380 "device");
381 if (result)
Linus Torvalds8264fce2014-10-24 11:21:43 -0700382 dev_err(&pdev->dev, "Failed to create sysfs link 'device'\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Felipe Contreras568b6ad2013-08-30 16:16:05 -0500384end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400385 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
Aaron Lu19593a12013-11-19 16:59:20 +0800388static int acpi_fan_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
Aaron Lu9519a632014-04-01 15:50:27 +0800390 struct acpi_fan *fan = platform_get_drvdata(pdev);
Zhang Rui05a83d92008-01-17 15:51:24 +0800391
Aaron Lu19593a12013-11-19 16:59:20 +0800392 sysfs_remove_link(&pdev->dev.kobj, "thermal_cooling");
Aaron Lu9519a632014-04-01 15:50:27 +0800393 sysfs_remove_link(&fan->cdev->device.kobj, "device");
394 thermal_cooling_device_unregister(fan->cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Patrick Mocheld550d982006-06-27 00:41:40 -0400396 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200399#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki62fcbdd2012-06-27 23:26:07 +0200400static int acpi_fan_suspend(struct device *dev)
Len Brownec683732008-01-23 22:41:20 -0500401{
Aaron Lu9519a632014-04-01 15:50:27 +0800402 struct acpi_fan *fan = dev_get_drvdata(dev);
403 if (fan->acpi4)
404 return 0;
Len Brownec683732008-01-23 22:41:20 -0500405
Aaron Lu19593a12013-11-19 16:59:20 +0800406 acpi_device_set_power(ACPI_COMPANION(dev), ACPI_STATE_D0);
Len Brownec683732008-01-23 22:41:20 -0500407
408 return AE_OK;
409}
410
Rafael J. Wysocki62fcbdd2012-06-27 23:26:07 +0200411static int acpi_fan_resume(struct device *dev)
Len Brownec683732008-01-23 22:41:20 -0500412{
Rafael J. Wysocki488a76c2010-11-25 00:11:24 +0100413 int result;
Aaron Lu9519a632014-04-01 15:50:27 +0800414 struct acpi_fan *fan = dev_get_drvdata(dev);
Len Brownec683732008-01-23 22:41:20 -0500415
Aaron Lu9519a632014-04-01 15:50:27 +0800416 if (fan->acpi4)
417 return 0;
Len Brownec683732008-01-23 22:41:20 -0500418
Aaron Lu19593a12013-11-19 16:59:20 +0800419 result = acpi_device_update_power(ACPI_COMPANION(dev), NULL);
Rafael J. Wysocki488a76c2010-11-25 00:11:24 +0100420 if (result)
Sudip Mukherjee88989fd2014-08-28 19:17:19 +0530421 dev_err(dev, "Error updating fan power state\n");
Len Brownec683732008-01-23 22:41:20 -0500422
423 return result;
424}
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200425#endif
Len Brownec683732008-01-23 22:41:20 -0500426
Aaron Lu19593a12013-11-19 16:59:20 +0800427module_platform_driver(acpi_fan_driver);