blob: bc36a476f1abf139586391c4e490cad56aff4368 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/uaccess.h>
Zhang Rui05a83d92008-01-17 15:51:24 +080031#include <linux/thermal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <acpi/acpi_bus.h>
33#include <acpi/acpi_drivers.h>
34
Len Browna192a952009-07-28 16:45:54 -040035#define PREFIX "ACPI: "
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define ACPI_FAN_CLASS "fan"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#define ACPI_FAN_FILE_STATE "state"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#define _COMPONENT ACPI_FAN_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050041ACPI_MODULE_NAME("fan");
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Len Brownf52fd662007-02-12 22:42:12 -050043MODULE_AUTHOR("Paul Diefenbaugh");
Len Brown7cda93e2007-02-12 23:50:02 -050044MODULE_DESCRIPTION("ACPI Fan Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070045MODULE_LICENSE("GPL");
46
Len Brown4be44fc2005-08-05 00:44:28 -040047static int acpi_fan_add(struct acpi_device *device);
48static int acpi_fan_remove(struct acpi_device *device, int type);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Thomas Renninger1ba90e32007-07-23 14:44:41 +020050static const struct acpi_device_id fan_device_ids[] = {
51 {"PNP0C0B", 0},
52 {"", 0},
53};
54MODULE_DEVICE_TABLE(acpi, fan_device_ids);
55
Rafael J. Wysocki90692402012-08-09 23:00:02 +020056#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki62fcbdd2012-06-27 23:26:07 +020057static int acpi_fan_suspend(struct device *dev);
58static int acpi_fan_resume(struct device *dev);
Rafael J. Wysocki90692402012-08-09 23:00:02 +020059#endif
Rafael J. Wysocki62fcbdd2012-06-27 23:26:07 +020060static SIMPLE_DEV_PM_OPS(acpi_fan_pm, acpi_fan_suspend, acpi_fan_resume);
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static struct acpi_driver acpi_fan_driver = {
Len Brownc2b67052007-02-12 23:33:40 -050063 .name = "fan",
Len Brown4be44fc2005-08-05 00:44:28 -040064 .class = ACPI_FAN_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +020065 .ids = fan_device_ids,
Len Brown4be44fc2005-08-05 00:44:28 -040066 .ops = {
67 .add = acpi_fan_add,
68 .remove = acpi_fan_remove,
69 },
Rafael J. Wysocki62fcbdd2012-06-27 23:26:07 +020070 .drv.pm = &acpi_fan_pm,
Linus Torvalds1da177e2005-04-16 15:20:36 -070071};
72
Zhang Rui05a83d92008-01-17 15:51:24 +080073/* thermal cooling device callbacks */
Matthew Garrett6503e5d2008-11-27 17:48:13 +000074static int fan_get_max_state(struct thermal_cooling_device *cdev, unsigned long
75 *state)
Zhang Rui05a83d92008-01-17 15:51:24 +080076{
77 /* ACPI fan device only support two states: ON/OFF */
Matthew Garrett6503e5d2008-11-27 17:48:13 +000078 *state = 1;
79 return 0;
Zhang Rui05a83d92008-01-17 15:51:24 +080080}
81
Matthew Garrett6503e5d2008-11-27 17:48:13 +000082static int fan_get_cur_state(struct thermal_cooling_device *cdev, unsigned long
83 *state)
Zhang Rui05a83d92008-01-17 15:51:24 +080084{
85 struct acpi_device *device = cdev->devdata;
Zhang Rui05a83d92008-01-17 15:51:24 +080086 int result;
Matthew Garrett6503e5d2008-11-27 17:48:13 +000087 int acpi_state;
Zhang Rui05a83d92008-01-17 15:51:24 +080088
89 if (!device)
90 return -EINVAL;
91
Rafael J. Wysocki488a76c2010-11-25 00:11:24 +010092 result = acpi_bus_update_power(device->handle, &acpi_state);
Zhang Rui05a83d92008-01-17 15:51:24 +080093 if (result)
94 return result;
95
Matthew Garrett6503e5d2008-11-27 17:48:13 +000096 *state = (acpi_state == ACPI_STATE_D3 ? 0 :
97 (acpi_state == ACPI_STATE_D0 ? 1 : -1));
98 return 0;
Zhang Rui05a83d92008-01-17 15:51:24 +080099}
100
101static int
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000102fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
Zhang Rui05a83d92008-01-17 15:51:24 +0800103{
104 struct acpi_device *device = cdev->devdata;
105 int result;
106
107 if (!device || (state != 0 && state != 1))
108 return -EINVAL;
109
110 result = acpi_bus_set_power(device->handle,
111 state ? ACPI_STATE_D0 : ACPI_STATE_D3);
112
113 return result;
114}
115
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +0400116static const struct thermal_cooling_device_ops fan_cooling_ops = {
Zhang Rui05a83d92008-01-17 15:51:24 +0800117 .get_max_state = fan_get_max_state,
118 .get_cur_state = fan_get_cur_state,
119 .set_cur_state = fan_set_cur_state,
120};
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122/* --------------------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 Driver Interface
124 -------------------------------------------------------------------------- */
125
Len Brown4be44fc2005-08-05 00:44:28 -0400126static int acpi_fan_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Len Brown4be44fc2005-08-05 00:44:28 -0400128 int result = 0;
Zhang Rui05a83d92008-01-17 15:51:24 +0800129 struct thermal_cooling_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400132 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Pavel Machekc65ade42005-08-05 00:37:45 -0400134 strcpy(acpi_device_name(device), "Fan");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 strcpy(acpi_device_class(device), ACPI_FAN_CLASS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Rafael J. Wysocki488a76c2010-11-25 00:11:24 +0100137 result = acpi_bus_update_power(device->handle, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 if (result) {
Rafael J. Wysocki488a76c2010-11-25 00:11:24 +0100139 printk(KERN_ERR PREFIX "Setting initial power state\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 goto end;
141 }
142
Zhang Rui05a83d92008-01-17 15:51:24 +0800143 cdev = thermal_cooling_device_register("Fan", device,
144 &fan_cooling_ops);
Thomas Sujith19b36782008-02-15 01:01:52 -0500145 if (IS_ERR(cdev)) {
146 result = PTR_ERR(cdev);
147 goto end;
148 }
Zhang Rui05a83d92008-01-17 15:51:24 +0800149
Mike Travis13c41152009-12-14 13:38:30 -0800150 dev_dbg(&device->dev, "registered as cooling_device%d\n", cdev->id);
Thomas Sujith19b36782008-02-15 01:01:52 -0500151
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700152 device->driver_data = cdev;
Julia Lawall90300622008-04-11 10:09:24 +0800153 result = sysfs_create_link(&device->dev.kobj,
154 &cdev->device.kobj,
155 "thermal_cooling");
156 if (result)
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200157 dev_err(&device->dev, "Failed to create sysfs link "
158 "'thermal_cooling'\n");
Julia Lawall90300622008-04-11 10:09:24 +0800159
160 result = sysfs_create_link(&cdev->device.kobj,
161 &device->dev.kobj,
162 "device");
163 if (result)
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200164 dev_err(&device->dev, "Failed to create sysfs link "
165 "'device'\n");
Zhang Rui05a83d92008-01-17 15:51:24 +0800166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400168 acpi_device_name(device), acpi_device_bid(device),
169 !device->power.state ? "on" : "off");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Len Brown4be44fc2005-08-05 00:44:28 -0400171 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400172 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Len Brown4be44fc2005-08-05 00:44:28 -0400175static int acpi_fan_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Zhang Rui05a83d92008-01-17 15:51:24 +0800177 struct thermal_cooling_device *cdev = acpi_driver_data(device);
178
179 if (!device || !cdev)
Patrick Mocheld550d982006-06-27 00:41:40 -0400180 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Zhang Rui05a83d92008-01-17 15:51:24 +0800182 sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
183 sysfs_remove_link(&cdev->device.kobj, "device");
184 thermal_cooling_device_unregister(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Patrick Mocheld550d982006-06-27 00:41:40 -0400186 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200189#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki62fcbdd2012-06-27 23:26:07 +0200190static int acpi_fan_suspend(struct device *dev)
Len Brownec683732008-01-23 22:41:20 -0500191{
Rafael J. Wysocki62fcbdd2012-06-27 23:26:07 +0200192 if (!dev)
Len Brownec683732008-01-23 22:41:20 -0500193 return -EINVAL;
194
Rafael J. Wysocki62fcbdd2012-06-27 23:26:07 +0200195 acpi_bus_set_power(to_acpi_device(dev)->handle, ACPI_STATE_D0);
Len Brownec683732008-01-23 22:41:20 -0500196
197 return AE_OK;
198}
199
Rafael J. Wysocki62fcbdd2012-06-27 23:26:07 +0200200static int acpi_fan_resume(struct device *dev)
Len Brownec683732008-01-23 22:41:20 -0500201{
Rafael J. Wysocki488a76c2010-11-25 00:11:24 +0100202 int result;
Len Brownec683732008-01-23 22:41:20 -0500203
Rafael J. Wysocki62fcbdd2012-06-27 23:26:07 +0200204 if (!dev)
Len Brownec683732008-01-23 22:41:20 -0500205 return -EINVAL;
206
Rafael J. Wysocki62fcbdd2012-06-27 23:26:07 +0200207 result = acpi_bus_update_power(to_acpi_device(dev)->handle, NULL);
Rafael J. Wysocki488a76c2010-11-25 00:11:24 +0100208 if (result)
209 printk(KERN_ERR PREFIX "Error updating fan power state\n");
Len Brownec683732008-01-23 22:41:20 -0500210
211 return result;
212}
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200213#endif
Len Brownec683732008-01-23 22:41:20 -0500214
Len Brown4be44fc2005-08-05 00:44:28 -0400215static int __init acpi_fan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Pavel Machekc65ade42005-08-05 00:37:45 -0400217 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 result = acpi_bus_register_driver(&acpi_fan_driver);
Zhang Ruib2a44982010-10-08 13:55:03 +0800220 if (result < 0)
Patrick Mocheld550d982006-06-27 00:41:40 -0400221 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Patrick Mocheld550d982006-06-27 00:41:40 -0400223 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
Len Brown4be44fc2005-08-05 00:44:28 -0400226static void __exit acpi_fan_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 acpi_bus_unregister_driver(&acpi_fan_driver);
230
Patrick Mocheld550d982006-06-27 00:41:40 -0400231 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234module_init(acpi_fan_init);
235module_exit(acpi_fan_exit);