blob: bea0bbaafa979dd998d71695c62ac3366bd48e43 [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 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 *
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24 */
25
26#include <linux/kernel.h>
27#include <linux/module.h>
28#include <linux/init.h>
29#include <linux/types.h>
Sudip Mukherjee88989fd2014-08-28 19:17:19 +053030#include <linux/uaccess.h>
Zhang Rui05a83d92008-01-17 15:51:24 +080031#include <linux/thermal.h>
Lv Zheng8b484632013-12-03 08:49:16 +080032#include <linux/acpi.h>
Aaron Lu19593a12013-11-19 16:59:20 +080033#include <linux/platform_device.h>
Aaron Lu9519a632014-04-01 15:50:27 +080034#include <linux/sort.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Len Brownf52fd662007-02-12 22:42:12 -050036MODULE_AUTHOR("Paul Diefenbaugh");
Len Brown7cda93e2007-02-12 23:50:02 -050037MODULE_DESCRIPTION("ACPI Fan Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070038MODULE_LICENSE("GPL");
39
Aaron Lu19593a12013-11-19 16:59:20 +080040static int acpi_fan_probe(struct platform_device *pdev);
41static int acpi_fan_remove(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Thomas Renninger1ba90e32007-07-23 14:44:41 +020043static const struct acpi_device_id fan_device_ids[] = {
44 {"PNP0C0B", 0},
Aaron Lud806c6e2014-04-01 15:54:05 +080045 {"INT3404", 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +020046 {"", 0},
47};
48MODULE_DEVICE_TABLE(acpi, fan_device_ids);
49
Rafael J. Wysocki90692402012-08-09 23:00:02 +020050#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki62fcbdd2012-06-27 23:26:07 +020051static int acpi_fan_suspend(struct device *dev);
52static int acpi_fan_resume(struct device *dev);
Aaron Lub9b85152014-02-19 12:16:48 +080053static struct dev_pm_ops acpi_fan_pm = {
54 .resume = acpi_fan_resume,
55 .freeze = acpi_fan_suspend,
56 .thaw = acpi_fan_resume,
57 .restore = acpi_fan_resume,
58};
59#define FAN_PM_OPS_PTR (&acpi_fan_pm)
Shuah Khanb108e0e2014-02-12 20:19:08 -070060#else
Aaron Lub9b85152014-02-19 12:16:48 +080061#define FAN_PM_OPS_PTR NULL
Rafael J. Wysocki90692402012-08-09 23:00:02 +020062#endif
Rafael J. Wysocki62fcbdd2012-06-27 23:26:07 +020063
Aaron Lu9519a632014-04-01 15:50:27 +080064struct acpi_fan_fps {
65 u64 control;
66 u64 trip_point;
67 u64 speed;
68 u64 noise_level;
69 u64 power;
70};
71
72struct acpi_fan_fif {
73 u64 revision;
74 u64 fine_grain_ctrl;
75 u64 step_size;
76 u64 low_speed_notification;
77};
78
79struct acpi_fan {
80 bool acpi4;
81 struct acpi_fan_fif fif;
82 struct acpi_fan_fps *fps;
83 int fps_count;
84 struct thermal_cooling_device *cdev;
85};
86
Aaron Lu19593a12013-11-19 16:59:20 +080087static struct platform_driver acpi_fan_driver = {
88 .probe = acpi_fan_probe,
89 .remove = acpi_fan_remove,
90 .driver = {
91 .name = "acpi-fan",
92 .acpi_match_table = fan_device_ids,
93 .pm = FAN_PM_OPS_PTR,
94 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070095};
96
Zhang Rui05a83d92008-01-17 15:51:24 +080097/* thermal cooling device callbacks */
Matthew Garrett6503e5d2008-11-27 17:48:13 +000098static int fan_get_max_state(struct thermal_cooling_device *cdev, unsigned long
99 *state)
Zhang Rui05a83d92008-01-17 15:51:24 +0800100{
Aaron Lu9519a632014-04-01 15:50:27 +0800101 struct acpi_device *device = cdev->devdata;
102 struct acpi_fan *fan = acpi_driver_data(device);
103
104 if (fan->acpi4)
105 *state = fan->fps_count - 1;
106 else
107 *state = 1;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000108 return 0;
Zhang Rui05a83d92008-01-17 15:51:24 +0800109}
110
Aaron Lu9519a632014-04-01 15:50:27 +0800111static int fan_get_state_acpi4(struct acpi_device *device, unsigned long *state)
Zhang Rui05a83d92008-01-17 15:51:24 +0800112{
Aaron Lu9519a632014-04-01 15:50:27 +0800113 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
114 struct acpi_fan *fan = acpi_driver_data(device);
115 union acpi_object *obj;
116 acpi_status status;
117 int control, i;
118
119 status = acpi_evaluate_object(device->handle, "_FST", NULL, &buffer);
120 if (ACPI_FAILURE(status)) {
121 dev_err(&device->dev, "Get fan state failed\n");
122 return status;
123 }
124
125 obj = buffer.pointer;
126 if (!obj || obj->type != ACPI_TYPE_PACKAGE ||
127 obj->package.count != 3 ||
128 obj->package.elements[1].type != ACPI_TYPE_INTEGER) {
129 dev_err(&device->dev, "Invalid _FST data\n");
130 status = -EINVAL;
131 goto err;
132 }
133
134 control = obj->package.elements[1].integer.value;
135 for (i = 0; i < fan->fps_count; i++) {
136 if (control == fan->fps[i].control)
137 break;
138 }
139 if (i == fan->fps_count) {
140 dev_dbg(&device->dev, "Invalid control value returned\n");
141 status = -EINVAL;
142 goto err;
143 }
144
145 *state = i;
146
147err:
148 kfree(obj);
149 return status;
150}
151
152static int fan_get_state(struct acpi_device *device, unsigned long *state)
153{
Zhang Rui05a83d92008-01-17 15:51:24 +0800154 int result;
Naresh Bhat85eb9822013-07-01 19:51:00 +0530155 int acpi_state = ACPI_STATE_D0;
Zhang Rui05a83d92008-01-17 15:51:24 +0800156
Aaron Lu2bb3a2b2013-11-19 15:43:52 +0800157 result = acpi_device_update_power(device, &acpi_state);
Zhang Rui05a83d92008-01-17 15:51:24 +0800158 if (result)
159 return result;
160
Rafael J. Wysocki20dacb72015-05-16 01:55:35 +0200161 *state = acpi_state == ACPI_STATE_D3_COLD
162 || acpi_state == ACPI_STATE_D3_HOT ?
163 0 : (acpi_state == ACPI_STATE_D0 ? 1 : -1);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000164 return 0;
Zhang Rui05a83d92008-01-17 15:51:24 +0800165}
166
Aaron Lu9519a632014-04-01 15:50:27 +0800167static int fan_get_cur_state(struct thermal_cooling_device *cdev, unsigned long
168 *state)
169{
170 struct acpi_device *device = cdev->devdata;
171 struct acpi_fan *fan = acpi_driver_data(device);
172
173 if (fan->acpi4)
174 return fan_get_state_acpi4(device, state);
175 else
176 return fan_get_state(device, state);
177}
178
179static int fan_set_state(struct acpi_device *device, unsigned long state)
180{
181 if (state != 0 && state != 1)
182 return -EINVAL;
183
184 return acpi_device_set_power(device,
185 state ? ACPI_STATE_D0 : ACPI_STATE_D3_COLD);
186}
187
188static int fan_set_state_acpi4(struct acpi_device *device, unsigned long state)
189{
190 struct acpi_fan *fan = acpi_driver_data(device);
191 acpi_status status;
192
193 if (state >= fan->fps_count)
194 return -EINVAL;
195
196 status = acpi_execute_simple_method(device->handle, "_FSL",
197 fan->fps[state].control);
198 if (ACPI_FAILURE(status)) {
199 dev_dbg(&device->dev, "Failed to set state by _FSL\n");
200 return status;
201 }
202
203 return 0;
204}
205
Zhang Rui05a83d92008-01-17 15:51:24 +0800206static int
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000207fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
Zhang Rui05a83d92008-01-17 15:51:24 +0800208{
209 struct acpi_device *device = cdev->devdata;
Aaron Lu9519a632014-04-01 15:50:27 +0800210 struct acpi_fan *fan = acpi_driver_data(device);
Zhang Rui05a83d92008-01-17 15:51:24 +0800211
Aaron Lu9519a632014-04-01 15:50:27 +0800212 if (fan->acpi4)
213 return fan_set_state_acpi4(device, state);
214 else
215 return fan_set_state(device, state);
216 }
Zhang Rui05a83d92008-01-17 15:51:24 +0800217
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +0400218static const struct thermal_cooling_device_ops fan_cooling_ops = {
Zhang Rui05a83d92008-01-17 15:51:24 +0800219 .get_max_state = fan_get_max_state,
220 .get_cur_state = fan_get_cur_state,
221 .set_cur_state = fan_set_cur_state,
222};
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224/* --------------------------------------------------------------------------
Sudip Mukherjee88989fd2014-08-28 19:17:19 +0530225 * Driver Interface
226 * --------------------------------------------------------------------------
227*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Aaron Lu9519a632014-04-01 15:50:27 +0800229static bool acpi_fan_is_acpi4(struct acpi_device *device)
230{
231 return acpi_has_method(device->handle, "_FIF") &&
232 acpi_has_method(device->handle, "_FPS") &&
233 acpi_has_method(device->handle, "_FSL") &&
234 acpi_has_method(device->handle, "_FST");
235}
236
237static int acpi_fan_get_fif(struct acpi_device *device)
238{
239 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
240 struct acpi_fan *fan = acpi_driver_data(device);
241 struct acpi_buffer format = { sizeof("NNNN"), "NNNN" };
242 struct acpi_buffer fif = { sizeof(fan->fif), &fan->fif };
243 union acpi_object *obj;
244 acpi_status status;
245
246 status = acpi_evaluate_object(device->handle, "_FIF", NULL, &buffer);
247 if (ACPI_FAILURE(status))
248 return status;
249
250 obj = buffer.pointer;
251 if (!obj || obj->type != ACPI_TYPE_PACKAGE) {
252 dev_err(&device->dev, "Invalid _FIF data\n");
253 status = -EINVAL;
254 goto err;
255 }
256
257 status = acpi_extract_package(obj, &format, &fif);
258 if (ACPI_FAILURE(status)) {
259 dev_err(&device->dev, "Invalid _FIF element\n");
260 status = -EINVAL;
261 }
262
263err:
264 kfree(obj);
265 return status;
266}
267
268static int acpi_fan_speed_cmp(const void *a, const void *b)
269{
270 const struct acpi_fan_fps *fps1 = a;
271 const struct acpi_fan_fps *fps2 = b;
272 return fps1->speed - fps2->speed;
273}
274
275static int acpi_fan_get_fps(struct acpi_device *device)
276{
277 struct acpi_fan *fan = acpi_driver_data(device);
278 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
279 union acpi_object *obj;
280 acpi_status status;
281 int i;
282
283 status = acpi_evaluate_object(device->handle, "_FPS", NULL, &buffer);
284 if (ACPI_FAILURE(status))
285 return status;
286
287 obj = buffer.pointer;
288 if (!obj || obj->type != ACPI_TYPE_PACKAGE || obj->package.count < 2) {
289 dev_err(&device->dev, "Invalid _FPS data\n");
290 status = -EINVAL;
291 goto err;
292 }
293
294 fan->fps_count = obj->package.count - 1; /* minus revision field */
295 fan->fps = devm_kzalloc(&device->dev,
296 fan->fps_count * sizeof(struct acpi_fan_fps),
297 GFP_KERNEL);
298 if (!fan->fps) {
299 dev_err(&device->dev, "Not enough memory\n");
300 status = -ENOMEM;
301 goto err;
302 }
303 for (i = 0; i < fan->fps_count; i++) {
304 struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
305 struct acpi_buffer fps = { sizeof(fan->fps[i]), &fan->fps[i] };
306 status = acpi_extract_package(&obj->package.elements[i + 1],
307 &format, &fps);
308 if (ACPI_FAILURE(status)) {
309 dev_err(&device->dev, "Invalid _FPS element\n");
310 break;
311 }
312 }
313
314 /* sort the state array according to fan speed in increase order */
315 sort(fan->fps, fan->fps_count, sizeof(*fan->fps),
316 acpi_fan_speed_cmp, NULL);
317
318err:
319 kfree(obj);
320 return status;
321}
322
Aaron Lu19593a12013-11-19 16:59:20 +0800323static int acpi_fan_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Len Brown4be44fc2005-08-05 00:44:28 -0400325 int result = 0;
Zhang Rui05a83d92008-01-17 15:51:24 +0800326 struct thermal_cooling_device *cdev;
Aaron Lu9519a632014-04-01 15:50:27 +0800327 struct acpi_fan *fan;
Aaron Lu19593a12013-11-19 16:59:20 +0800328 struct acpi_device *device = ACPI_COMPANION(&pdev->dev);
Srinivas Pandruvadabbb16fe2014-12-09 16:15:40 -0800329 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Aaron Lu9519a632014-04-01 15:50:27 +0800331 fan = devm_kzalloc(&pdev->dev, sizeof(*fan), GFP_KERNEL);
332 if (!fan) {
333 dev_err(&device->dev, "No memory for fan\n");
334 return -ENOMEM;
335 }
336 device->driver_data = fan;
337 platform_set_drvdata(pdev, fan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Aaron Lu9519a632014-04-01 15:50:27 +0800339 if (acpi_fan_is_acpi4(device)) {
340 if (acpi_fan_get_fif(device) || acpi_fan_get_fps(device))
341 goto end;
342 fan->acpi4 = true;
343 } else {
344 result = acpi_device_update_power(device, NULL);
345 if (result) {
346 dev_err(&device->dev, "Setting initial power state\n");
347 goto end;
348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350
Srinivas Pandruvadabbb16fe2014-12-09 16:15:40 -0800351 if (!strncmp(pdev->name, "PNP0C0B", strlen("PNP0C0B")))
352 name = "Fan";
353 else
354 name = acpi_device_bid(device);
355
356 cdev = thermal_cooling_device_register(name, device,
Zhang Rui05a83d92008-01-17 15:51:24 +0800357 &fan_cooling_ops);
Thomas Sujith19b36782008-02-15 01:01:52 -0500358 if (IS_ERR(cdev)) {
359 result = PTR_ERR(cdev);
360 goto end;
361 }
Zhang Rui05a83d92008-01-17 15:51:24 +0800362
Aaron Lu19593a12013-11-19 16:59:20 +0800363 dev_dbg(&pdev->dev, "registered as cooling_device%d\n", cdev->id);
Thomas Sujith19b36782008-02-15 01:01:52 -0500364
Aaron Lu9519a632014-04-01 15:50:27 +0800365 fan->cdev = cdev;
Aaron Lu19593a12013-11-19 16:59:20 +0800366 result = sysfs_create_link(&pdev->dev.kobj,
Julia Lawall90300622008-04-11 10:09:24 +0800367 &cdev->device.kobj,
368 "thermal_cooling");
369 if (result)
Linus Torvalds8264fce2014-10-24 11:21:43 -0700370 dev_err(&pdev->dev, "Failed to create sysfs link 'thermal_cooling'\n");
Julia Lawall90300622008-04-11 10:09:24 +0800371
372 result = sysfs_create_link(&cdev->device.kobj,
Aaron Lu19593a12013-11-19 16:59:20 +0800373 &pdev->dev.kobj,
Julia Lawall90300622008-04-11 10:09:24 +0800374 "device");
375 if (result)
Linus Torvalds8264fce2014-10-24 11:21:43 -0700376 dev_err(&pdev->dev, "Failed to create sysfs link 'device'\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Felipe Contreras568b6ad2013-08-30 16:16:05 -0500378end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400379 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
Aaron Lu19593a12013-11-19 16:59:20 +0800382static int acpi_fan_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
Aaron Lu9519a632014-04-01 15:50:27 +0800384 struct acpi_fan *fan = platform_get_drvdata(pdev);
Zhang Rui05a83d92008-01-17 15:51:24 +0800385
Aaron Lu19593a12013-11-19 16:59:20 +0800386 sysfs_remove_link(&pdev->dev.kobj, "thermal_cooling");
Aaron Lu9519a632014-04-01 15:50:27 +0800387 sysfs_remove_link(&fan->cdev->device.kobj, "device");
388 thermal_cooling_device_unregister(fan->cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Patrick Mocheld550d982006-06-27 00:41:40 -0400390 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200393#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki62fcbdd2012-06-27 23:26:07 +0200394static int acpi_fan_suspend(struct device *dev)
Len Brownec683732008-01-23 22:41:20 -0500395{
Aaron Lu9519a632014-04-01 15:50:27 +0800396 struct acpi_fan *fan = dev_get_drvdata(dev);
397 if (fan->acpi4)
398 return 0;
Len Brownec683732008-01-23 22:41:20 -0500399
Aaron Lu19593a12013-11-19 16:59:20 +0800400 acpi_device_set_power(ACPI_COMPANION(dev), ACPI_STATE_D0);
Len Brownec683732008-01-23 22:41:20 -0500401
402 return AE_OK;
403}
404
Rafael J. Wysocki62fcbdd2012-06-27 23:26:07 +0200405static int acpi_fan_resume(struct device *dev)
Len Brownec683732008-01-23 22:41:20 -0500406{
Rafael J. Wysocki488a76c2010-11-25 00:11:24 +0100407 int result;
Aaron Lu9519a632014-04-01 15:50:27 +0800408 struct acpi_fan *fan = dev_get_drvdata(dev);
Len Brownec683732008-01-23 22:41:20 -0500409
Aaron Lu9519a632014-04-01 15:50:27 +0800410 if (fan->acpi4)
411 return 0;
Len Brownec683732008-01-23 22:41:20 -0500412
Aaron Lu19593a12013-11-19 16:59:20 +0800413 result = acpi_device_update_power(ACPI_COMPANION(dev), NULL);
Rafael J. Wysocki488a76c2010-11-25 00:11:24 +0100414 if (result)
Sudip Mukherjee88989fd2014-08-28 19:17:19 +0530415 dev_err(dev, "Error updating fan power state\n");
Len Brownec683732008-01-23 22:41:20 -0500416
417 return result;
418}
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200419#endif
Len Brownec683732008-01-23 22:41:20 -0500420
Aaron Lu19593a12013-11-19 16:59:20 +0800421module_platform_driver(acpi_fan_driver);