Merge branches 'pm-cpufreq', 'pm-hibernate' and 'acpi-processor'
[linux-block.git] / drivers / acpi / fan.c
CommitLineData
1da177e4
LT
1/*
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>
1da177e4 30#include <asm/uaccess.h>
05a83d97 31#include <linux/thermal.h>
8b48463f 32#include <linux/acpi.h>
1da177e4 33
a192a958
LB
34#define PREFIX "ACPI: "
35
1da177e4 36#define ACPI_FAN_CLASS "fan"
1da177e4 37#define ACPI_FAN_FILE_STATE "state"
1da177e4
LT
38
39#define _COMPONENT ACPI_FAN_COMPONENT
f52fd66d 40ACPI_MODULE_NAME("fan");
1da177e4 41
f52fd66d 42MODULE_AUTHOR("Paul Diefenbaugh");
7cda93e0 43MODULE_DESCRIPTION("ACPI Fan Driver");
1da177e4
LT
44MODULE_LICENSE("GPL");
45
4be44fcd 46static int acpi_fan_add(struct acpi_device *device);
51fac838 47static int acpi_fan_remove(struct acpi_device *device);
1da177e4 48
1ba90e3a
TR
49static const struct acpi_device_id fan_device_ids[] = {
50 {"PNP0C0B", 0},
51 {"", 0},
52};
53MODULE_DEVICE_TABLE(acpi, fan_device_ids);
54
90692404 55#ifdef CONFIG_PM_SLEEP
62fcbdd9
RW
56static int acpi_fan_suspend(struct device *dev);
57static int acpi_fan_resume(struct device *dev);
b108e0ea
SK
58#else
59#define acpi_fan_suspend NULL
60#define acpi_fan_resume NULL
90692404 61#endif
62fcbdd9
RW
62static SIMPLE_DEV_PM_OPS(acpi_fan_pm, acpi_fan_suspend, acpi_fan_resume);
63
1da177e4 64static struct acpi_driver acpi_fan_driver = {
c2b6705b 65 .name = "fan",
4be44fcd 66 .class = ACPI_FAN_CLASS,
1ba90e3a 67 .ids = fan_device_ids,
4be44fcd
LB
68 .ops = {
69 .add = acpi_fan_add,
70 .remove = acpi_fan_remove,
71 },
62fcbdd9 72 .drv.pm = &acpi_fan_pm,
1da177e4
LT
73};
74
05a83d97 75/* thermal cooling device callbacks */
6503e5df
MG
76static int fan_get_max_state(struct thermal_cooling_device *cdev, unsigned long
77 *state)
05a83d97
ZR
78{
79 /* ACPI fan device only support two states: ON/OFF */
6503e5df
MG
80 *state = 1;
81 return 0;
05a83d97
ZR
82}
83
6503e5df
MG
84static int fan_get_cur_state(struct thermal_cooling_device *cdev, unsigned long
85 *state)
05a83d97
ZR
86{
87 struct acpi_device *device = cdev->devdata;
05a83d97 88 int result;
85eb9827 89 int acpi_state = ACPI_STATE_D0;
05a83d97
ZR
90
91 if (!device)
92 return -EINVAL;
93
488a76c5 94 result = acpi_bus_update_power(device->handle, &acpi_state);
05a83d97
ZR
95 if (result)
96 return result;
97
8ad928d5 98 *state = (acpi_state == ACPI_STATE_D3_COLD ? 0 :
6503e5df
MG
99 (acpi_state == ACPI_STATE_D0 ? 1 : -1));
100 return 0;
05a83d97
ZR
101}
102
103static int
6503e5df 104fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
05a83d97
ZR
105{
106 struct acpi_device *device = cdev->devdata;
107 int result;
108
109 if (!device || (state != 0 && state != 1))
110 return -EINVAL;
111
112 result = acpi_bus_set_power(device->handle,
8ad928d5 113 state ? ACPI_STATE_D0 : ACPI_STATE_D3_COLD);
05a83d97
ZR
114
115 return result;
116}
117
9c8b04be 118static const struct thermal_cooling_device_ops fan_cooling_ops = {
05a83d97
ZR
119 .get_max_state = fan_get_max_state,
120 .get_cur_state = fan_get_cur_state,
121 .set_cur_state = fan_set_cur_state,
122};
123
1da177e4
LT
124/* --------------------------------------------------------------------------
125 Driver Interface
126 -------------------------------------------------------------------------- */
127
4be44fcd 128static int acpi_fan_add(struct acpi_device *device)
1da177e4 129{
4be44fcd 130 int result = 0;
05a83d97 131 struct thermal_cooling_device *cdev;
1da177e4
LT
132
133 if (!device)
d550d98d 134 return -EINVAL;
1da177e4 135
c65ade4d 136 strcpy(acpi_device_name(device), "Fan");
1da177e4 137 strcpy(acpi_device_class(device), ACPI_FAN_CLASS);
1da177e4 138
488a76c5 139 result = acpi_bus_update_power(device->handle, NULL);
1da177e4 140 if (result) {
488a76c5 141 printk(KERN_ERR PREFIX "Setting initial power state\n");
1da177e4
LT
142 goto end;
143 }
144
05a83d97
ZR
145 cdev = thermal_cooling_device_register("Fan", device,
146 &fan_cooling_ops);
19b36780
TS
147 if (IS_ERR(cdev)) {
148 result = PTR_ERR(cdev);
149 goto end;
150 }
9030062f 151
13c41157 152 dev_dbg(&device->dev, "registered as cooling_device%d\n", cdev->id);
9030062f 153
db89b4f0 154 device->driver_data = cdev;
9030062f
JL
155 result = sysfs_create_link(&device->dev.kobj,
156 &cdev->device.kobj,
157 "thermal_cooling");
158 if (result)
fc3a8828
GKH
159 dev_err(&device->dev, "Failed to create sysfs link "
160 "'thermal_cooling'\n");
9030062f
JL
161
162 result = sysfs_create_link(&cdev->device.kobj,
163 &device->dev.kobj,
164 "device");
165 if (result)
fc3a8828
GKH
166 dev_err(&device->dev, "Failed to create sysfs link "
167 "'device'\n");
05a83d97 168
1da177e4 169 printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
4be44fcd
LB
170 acpi_device_name(device), acpi_device_bid(device),
171 !device->power.state ? "on" : "off");
1da177e4 172
568b6ad8 173end:
d550d98d 174 return result;
1da177e4
LT
175}
176
51fac838 177static int acpi_fan_remove(struct acpi_device *device)
1da177e4 178{
f0c29583
CIK
179 struct thermal_cooling_device *cdev;
180
181 if (!device)
182 return -EINVAL;
05a83d97 183
f0c29583
CIK
184 cdev = acpi_driver_data(device);
185 if (!cdev)
d550d98d 186 return -EINVAL;
1da177e4 187
05a83d97
ZR
188 sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
189 sysfs_remove_link(&cdev->device.kobj, "device");
190 thermal_cooling_device_unregister(cdev);
1da177e4 191
d550d98d 192 return 0;
1da177e4
LT
193}
194
90692404 195#ifdef CONFIG_PM_SLEEP
62fcbdd9 196static int acpi_fan_suspend(struct device *dev)
ec68373c 197{
62fcbdd9 198 if (!dev)
ec68373c
LB
199 return -EINVAL;
200
62fcbdd9 201 acpi_bus_set_power(to_acpi_device(dev)->handle, ACPI_STATE_D0);
ec68373c
LB
202
203 return AE_OK;
204}
205
62fcbdd9 206static int acpi_fan_resume(struct device *dev)
ec68373c 207{
488a76c5 208 int result;
ec68373c 209
62fcbdd9 210 if (!dev)
ec68373c
LB
211 return -EINVAL;
212
62fcbdd9 213 result = acpi_bus_update_power(to_acpi_device(dev)->handle, NULL);
488a76c5
RW
214 if (result)
215 printk(KERN_ERR PREFIX "Error updating fan power state\n");
ec68373c
LB
216
217 return result;
218}
90692404 219#endif
ec68373c 220
d8014c4b 221module_acpi_driver(acpi_fan_driver);