2 * acpi_fan.c - ACPI Fan Driver ($Revision: 29 $)
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
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.
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/types.h>
30 #include <linux/uaccess.h>
31 #include <linux/thermal.h>
32 #include <linux/acpi.h>
33 #include <linux/platform_device.h>
34 #include <linux/sort.h>
36 MODULE_AUTHOR("Paul Diefenbaugh");
37 MODULE_DESCRIPTION("ACPI Fan Driver");
38 MODULE_LICENSE("GPL");
40 static int acpi_fan_probe(struct platform_device
*pdev
);
41 static int acpi_fan_remove(struct platform_device
*pdev
);
43 static const struct acpi_device_id fan_device_ids
[] = {
48 MODULE_DEVICE_TABLE(acpi
, fan_device_ids
);
50 #ifdef CONFIG_PM_SLEEP
51 static int acpi_fan_suspend(struct device
*dev
);
52 static int acpi_fan_resume(struct device
*dev
);
53 static 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
,
59 #define FAN_PM_OPS_PTR (&acpi_fan_pm)
61 #define FAN_PM_OPS_PTR NULL
76 u64 low_speed_notification
;
81 struct acpi_fan_fif fif
;
82 struct acpi_fan_fps
*fps
;
84 struct thermal_cooling_device
*cdev
;
87 static struct platform_driver acpi_fan_driver
= {
88 .probe
= acpi_fan_probe
,
89 .remove
= acpi_fan_remove
,
92 .acpi_match_table
= fan_device_ids
,
97 /* thermal cooling device callbacks */
98 static int fan_get_max_state(struct thermal_cooling_device
*cdev
, unsigned long
101 struct acpi_device
*device
= cdev
->devdata
;
102 struct acpi_fan
*fan
= acpi_driver_data(device
);
105 *state
= fan
->fps_count
- 1;
111 static int fan_get_state_acpi4(struct acpi_device
*device
, unsigned long *state
)
113 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
114 struct acpi_fan
*fan
= acpi_driver_data(device
);
115 union acpi_object
*obj
;
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");
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");
134 control
= obj
->package
.elements
[1].integer
.value
;
135 for (i
= 0; i
< fan
->fps_count
; i
++) {
136 if (control
== fan
->fps
[i
].control
)
139 if (i
== fan
->fps_count
) {
140 dev_dbg(&device
->dev
, "Invalid control value returned\n");
152 static int fan_get_state(struct acpi_device
*device
, unsigned long *state
)
155 int acpi_state
= ACPI_STATE_D0
;
157 result
= acpi_device_update_power(device
, &acpi_state
);
161 *state
= (acpi_state
== ACPI_STATE_D3_COLD
? 0 :
162 (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 int acpi_fan_get_fps(struct acpi_device
*device
)
276 struct acpi_fan
*fan
= acpi_driver_data(device
);
277 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
278 union acpi_object
*obj
;
282 status
= acpi_evaluate_object(device
->handle
, "_FPS", NULL
, &buffer
);
283 if (ACPI_FAILURE(status
))
286 obj
= buffer
.pointer
;
287 if (!obj
|| obj
->type
!= ACPI_TYPE_PACKAGE
|| obj
->package
.count
< 2) {
288 dev_err(&device
->dev
, "Invalid _FPS data\n");
293 fan
->fps_count
= obj
->package
.count
- 1; /* minus revision field */
294 fan
->fps
= devm_kzalloc(&device
->dev
,
295 fan
->fps_count
* sizeof(struct acpi_fan_fps
),
298 dev_err(&device
->dev
, "Not enough memory\n");
302 for (i
= 0; i
< fan
->fps_count
; i
++) {
303 struct acpi_buffer format
= { sizeof("NNNNN"), "NNNNN" };
304 struct acpi_buffer fps
= { sizeof(fan
->fps
[i
]), &fan
->fps
[i
] };
305 status
= acpi_extract_package(&obj
->package
.elements
[i
+ 1],
307 if (ACPI_FAILURE(status
)) {
308 dev_err(&device
->dev
, "Invalid _FPS element\n");
313 /* sort the state array according to fan speed in increase order */
314 sort(fan
->fps
, fan
->fps_count
, sizeof(*fan
->fps
),
315 acpi_fan_speed_cmp
, NULL
);
322 static int acpi_fan_probe(struct platform_device
*pdev
)
325 struct thermal_cooling_device
*cdev
;
326 struct acpi_fan
*fan
;
327 struct acpi_device
*device
= ACPI_COMPANION(&pdev
->dev
);
330 fan
= devm_kzalloc(&pdev
->dev
, sizeof(*fan
), GFP_KERNEL
);
332 dev_err(&device
->dev
, "No memory for fan\n");
335 device
->driver_data
= fan
;
336 platform_set_drvdata(pdev
, fan
);
338 if (acpi_fan_is_acpi4(device
)) {
339 if (acpi_fan_get_fif(device
) || acpi_fan_get_fps(device
))
343 result
= acpi_device_update_power(device
, NULL
);
345 dev_err(&device
->dev
, "Setting initial power state\n");
350 if (!strncmp(pdev
->name
, "PNP0C0B", strlen("PNP0C0B")))
353 name
= acpi_device_bid(device
);
355 cdev
= thermal_cooling_device_register(name
, device
,
358 result
= PTR_ERR(cdev
);
362 dev_dbg(&pdev
->dev
, "registered as cooling_device%d\n", cdev
->id
);
365 result
= sysfs_create_link(&pdev
->dev
.kobj
,
369 dev_err(&pdev
->dev
, "Failed to create sysfs link 'thermal_cooling'\n");
371 result
= sysfs_create_link(&cdev
->device
.kobj
,
375 dev_err(&pdev
->dev
, "Failed to create sysfs link 'device'\n");
381 static int acpi_fan_remove(struct platform_device
*pdev
)
383 struct acpi_fan
*fan
= platform_get_drvdata(pdev
);
385 sysfs_remove_link(&pdev
->dev
.kobj
, "thermal_cooling");
386 sysfs_remove_link(&fan
->cdev
->device
.kobj
, "device");
387 thermal_cooling_device_unregister(fan
->cdev
);
392 #ifdef CONFIG_PM_SLEEP
393 static int acpi_fan_suspend(struct device
*dev
)
395 struct acpi_fan
*fan
= dev_get_drvdata(dev
);
399 acpi_device_set_power(ACPI_COMPANION(dev
), ACPI_STATE_D0
);
404 static int acpi_fan_resume(struct device
*dev
)
407 struct acpi_fan
*fan
= dev_get_drvdata(dev
);
412 result
= acpi_device_update_power(ACPI_COMPANION(dev
), NULL
);
414 dev_err(dev
, "Error updating fan power state\n");
420 module_platform_driver(acpi_fan_driver
);