blob: 669d9ee80d1678b7665a3b4b8b2ad709743a44aa [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. Wysocki62fcbdd2012-06-27 23:26:07 +020056static int acpi_fan_suspend(struct device *dev);
57static int acpi_fan_resume(struct device *dev);
58static SIMPLE_DEV_PM_OPS(acpi_fan_pm, acpi_fan_suspend, acpi_fan_resume);
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static struct acpi_driver acpi_fan_driver = {
Len Brownc2b67052007-02-12 23:33:40 -050061 .name = "fan",
Len Brown4be44fc2005-08-05 00:44:28 -040062 .class = ACPI_FAN_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +020063 .ids = fan_device_ids,
Len Brown4be44fc2005-08-05 00:44:28 -040064 .ops = {
65 .add = acpi_fan_add,
66 .remove = acpi_fan_remove,
67 },
Rafael J. Wysocki62fcbdd2012-06-27 23:26:07 +020068 .drv.pm = &acpi_fan_pm,
Linus Torvalds1da177e2005-04-16 15:20:36 -070069};
70
Zhang Rui05a83d92008-01-17 15:51:24 +080071/* thermal cooling device callbacks */
Matthew Garrett6503e5d2008-11-27 17:48:13 +000072static int fan_get_max_state(struct thermal_cooling_device *cdev, unsigned long
73 *state)
Zhang Rui05a83d92008-01-17 15:51:24 +080074{
75 /* ACPI fan device only support two states: ON/OFF */
Matthew Garrett6503e5d2008-11-27 17:48:13 +000076 *state = 1;
77 return 0;
Zhang Rui05a83d92008-01-17 15:51:24 +080078}
79
Matthew Garrett6503e5d2008-11-27 17:48:13 +000080static int fan_get_cur_state(struct thermal_cooling_device *cdev, unsigned long
81 *state)
Zhang Rui05a83d92008-01-17 15:51:24 +080082{
83 struct acpi_device *device = cdev->devdata;
Zhang Rui05a83d92008-01-17 15:51:24 +080084 int result;
Matthew Garrett6503e5d2008-11-27 17:48:13 +000085 int acpi_state;
Zhang Rui05a83d92008-01-17 15:51:24 +080086
87 if (!device)
88 return -EINVAL;
89
Rafael J. Wysocki488a76c2010-11-25 00:11:24 +010090 result = acpi_bus_update_power(device->handle, &acpi_state);
Zhang Rui05a83d92008-01-17 15:51:24 +080091 if (result)
92 return result;
93
Matthew Garrett6503e5d2008-11-27 17:48:13 +000094 *state = (acpi_state == ACPI_STATE_D3 ? 0 :
95 (acpi_state == ACPI_STATE_D0 ? 1 : -1));
96 return 0;
Zhang Rui05a83d92008-01-17 15:51:24 +080097}
98
99static int
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000100fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
Zhang Rui05a83d92008-01-17 15:51:24 +0800101{
102 struct acpi_device *device = cdev->devdata;
103 int result;
104
105 if (!device || (state != 0 && state != 1))
106 return -EINVAL;
107
108 result = acpi_bus_set_power(device->handle,
109 state ? ACPI_STATE_D0 : ACPI_STATE_D3);
110
111 return result;
112}
113
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +0400114static const struct thermal_cooling_device_ops fan_cooling_ops = {
Zhang Rui05a83d92008-01-17 15:51:24 +0800115 .get_max_state = fan_get_max_state,
116 .get_cur_state = fan_get_cur_state,
117 .set_cur_state = fan_set_cur_state,
118};
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120/* --------------------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 Driver Interface
122 -------------------------------------------------------------------------- */
123
Len Brown4be44fc2005-08-05 00:44:28 -0400124static int acpi_fan_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Len Brown4be44fc2005-08-05 00:44:28 -0400126 int result = 0;
Zhang Rui05a83d92008-01-17 15:51:24 +0800127 struct thermal_cooling_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400130 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Pavel Machekc65ade42005-08-05 00:37:45 -0400132 strcpy(acpi_device_name(device), "Fan");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 strcpy(acpi_device_class(device), ACPI_FAN_CLASS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Rafael J. Wysocki488a76c2010-11-25 00:11:24 +0100135 result = acpi_bus_update_power(device->handle, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 if (result) {
Rafael J. Wysocki488a76c2010-11-25 00:11:24 +0100137 printk(KERN_ERR PREFIX "Setting initial power state\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 goto end;
139 }
140
Zhang Rui05a83d92008-01-17 15:51:24 +0800141 cdev = thermal_cooling_device_register("Fan", device,
142 &fan_cooling_ops);
Thomas Sujith19b36782008-02-15 01:01:52 -0500143 if (IS_ERR(cdev)) {
144 result = PTR_ERR(cdev);
145 goto end;
146 }
Zhang Rui05a83d92008-01-17 15:51:24 +0800147
Mike Travis13c41152009-12-14 13:38:30 -0800148 dev_dbg(&device->dev, "registered as cooling_device%d\n", cdev->id);
Thomas Sujith19b36782008-02-15 01:01:52 -0500149
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700150 device->driver_data = cdev;
Julia Lawall90300622008-04-11 10:09:24 +0800151 result = sysfs_create_link(&device->dev.kobj,
152 &cdev->device.kobj,
153 "thermal_cooling");
154 if (result)
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200155 dev_err(&device->dev, "Failed to create sysfs link "
156 "'thermal_cooling'\n");
Julia Lawall90300622008-04-11 10:09:24 +0800157
158 result = sysfs_create_link(&cdev->device.kobj,
159 &device->dev.kobj,
160 "device");
161 if (result)
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200162 dev_err(&device->dev, "Failed to create sysfs link "
163 "'device'\n");
Zhang Rui05a83d92008-01-17 15:51:24 +0800164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400166 acpi_device_name(device), acpi_device_bid(device),
167 !device->power.state ? "on" : "off");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Len Brown4be44fc2005-08-05 00:44:28 -0400169 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400170 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
Len Brown4be44fc2005-08-05 00:44:28 -0400173static int acpi_fan_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Zhang Rui05a83d92008-01-17 15:51:24 +0800175 struct thermal_cooling_device *cdev = acpi_driver_data(device);
176
177 if (!device || !cdev)
Patrick Mocheld550d982006-06-27 00:41:40 -0400178 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Zhang Rui05a83d92008-01-17 15:51:24 +0800180 sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
181 sysfs_remove_link(&cdev->device.kobj, "device");
182 thermal_cooling_device_unregister(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Patrick Mocheld550d982006-06-27 00:41:40 -0400184 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
Rafael J. Wysocki62fcbdd2012-06-27 23:26:07 +0200187static int acpi_fan_suspend(struct device *dev)
Len Brownec683732008-01-23 22:41:20 -0500188{
Rafael J. Wysocki62fcbdd2012-06-27 23:26:07 +0200189 if (!dev)
Len Brownec683732008-01-23 22:41:20 -0500190 return -EINVAL;
191
Rafael J. Wysocki62fcbdd2012-06-27 23:26:07 +0200192 acpi_bus_set_power(to_acpi_device(dev)->handle, ACPI_STATE_D0);
Len Brownec683732008-01-23 22:41:20 -0500193
194 return AE_OK;
195}
196
Rafael J. Wysocki62fcbdd2012-06-27 23:26:07 +0200197static int acpi_fan_resume(struct device *dev)
Len Brownec683732008-01-23 22:41:20 -0500198{
Rafael J. Wysocki488a76c2010-11-25 00:11:24 +0100199 int result;
Len Brownec683732008-01-23 22:41:20 -0500200
Rafael J. Wysocki62fcbdd2012-06-27 23:26:07 +0200201 if (!dev)
Len Brownec683732008-01-23 22:41:20 -0500202 return -EINVAL;
203
Rafael J. Wysocki62fcbdd2012-06-27 23:26:07 +0200204 result = acpi_bus_update_power(to_acpi_device(dev)->handle, NULL);
Rafael J. Wysocki488a76c2010-11-25 00:11:24 +0100205 if (result)
206 printk(KERN_ERR PREFIX "Error updating fan power state\n");
Len Brownec683732008-01-23 22:41:20 -0500207
208 return result;
209}
210
Len Brown4be44fc2005-08-05 00:44:28 -0400211static int __init acpi_fan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
Pavel Machekc65ade42005-08-05 00:37:45 -0400213 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 result = acpi_bus_register_driver(&acpi_fan_driver);
Zhang Ruib2a44982010-10-08 13:55:03 +0800216 if (result < 0)
Patrick Mocheld550d982006-06-27 00:41:40 -0400217 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Patrick Mocheld550d982006-06-27 00:41:40 -0400219 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
Len Brown4be44fc2005-08-05 00:44:28 -0400222static void __exit acpi_fan_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 acpi_bus_unregister_driver(&acpi_fan_driver);
226
Patrick Mocheld550d982006-06-27 00:41:40 -0400227 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230module_init(acpi_fan_init);
231module_exit(acpi_fan_exit);