blob: 09e423f3d8ad30fbd16d2d2b2e2766e69e64de82 [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>
Lv Zheng8b484632013-12-03 08:49:16 +080032#include <linux/acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Len Browna192a952009-07-28 16:45:54 -040034#define PREFIX "ACPI: "
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#define ACPI_FAN_CLASS "fan"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define ACPI_FAN_FILE_STATE "state"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#define _COMPONENT ACPI_FAN_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050040ACPI_MODULE_NAME("fan");
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Len Brownf52fd662007-02-12 22:42:12 -050042MODULE_AUTHOR("Paul Diefenbaugh");
Len Brown7cda93e2007-02-12 23:50:02 -050043MODULE_DESCRIPTION("ACPI Fan Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070044MODULE_LICENSE("GPL");
45
Len Brown4be44fc2005-08-05 00:44:28 -040046static int acpi_fan_add(struct acpi_device *device);
Rafael J. Wysocki51fac832013-01-24 00:24:48 +010047static int acpi_fan_remove(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Thomas Renninger1ba90e32007-07-23 14:44:41 +020049static const struct acpi_device_id fan_device_ids[] = {
50 {"PNP0C0B", 0},
51 {"", 0},
52};
53MODULE_DEVICE_TABLE(acpi, fan_device_ids);
54
Rafael J. Wysocki90692402012-08-09 23:00:02 +020055#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki62fcbdd2012-06-27 23:26:07 +020056static int acpi_fan_suspend(struct device *dev);
57static int acpi_fan_resume(struct device *dev);
Shuah Khanb108e0e2014-02-12 20:19:08 -070058#else
59#define acpi_fan_suspend NULL
60#define acpi_fan_resume NULL
Rafael J. Wysocki90692402012-08-09 23:00:02 +020061#endif
Rafael J. Wysocki62fcbdd2012-06-27 23:26:07 +020062static SIMPLE_DEV_PM_OPS(acpi_fan_pm, acpi_fan_suspend, acpi_fan_resume);
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static struct acpi_driver acpi_fan_driver = {
Len Brownc2b67052007-02-12 23:33:40 -050065 .name = "fan",
Len Brown4be44fc2005-08-05 00:44:28 -040066 .class = ACPI_FAN_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +020067 .ids = fan_device_ids,
Len Brown4be44fc2005-08-05 00:44:28 -040068 .ops = {
69 .add = acpi_fan_add,
70 .remove = acpi_fan_remove,
71 },
Rafael J. Wysocki62fcbdd2012-06-27 23:26:07 +020072 .drv.pm = &acpi_fan_pm,
Linus Torvalds1da177e2005-04-16 15:20:36 -070073};
74
Zhang Rui05a83d92008-01-17 15:51:24 +080075/* thermal cooling device callbacks */
Matthew Garrett6503e5d2008-11-27 17:48:13 +000076static int fan_get_max_state(struct thermal_cooling_device *cdev, unsigned long
77 *state)
Zhang Rui05a83d92008-01-17 15:51:24 +080078{
79 /* ACPI fan device only support two states: ON/OFF */
Matthew Garrett6503e5d2008-11-27 17:48:13 +000080 *state = 1;
81 return 0;
Zhang Rui05a83d92008-01-17 15:51:24 +080082}
83
Matthew Garrett6503e5d2008-11-27 17:48:13 +000084static int fan_get_cur_state(struct thermal_cooling_device *cdev, unsigned long
85 *state)
Zhang Rui05a83d92008-01-17 15:51:24 +080086{
87 struct acpi_device *device = cdev->devdata;
Zhang Rui05a83d92008-01-17 15:51:24 +080088 int result;
Naresh Bhat85eb9822013-07-01 19:51:00 +053089 int acpi_state = ACPI_STATE_D0;
Zhang Rui05a83d92008-01-17 15:51:24 +080090
91 if (!device)
92 return -EINVAL;
93
Rafael J. Wysocki488a76c2010-11-25 00:11:24 +010094 result = acpi_bus_update_power(device->handle, &acpi_state);
Zhang Rui05a83d92008-01-17 15:51:24 +080095 if (result)
96 return result;
97
Rafael J. Wysocki8ad928d2013-07-30 14:36:20 +020098 *state = (acpi_state == ACPI_STATE_D3_COLD ? 0 :
Matthew Garrett6503e5d2008-11-27 17:48:13 +000099 (acpi_state == ACPI_STATE_D0 ? 1 : -1));
100 return 0;
Zhang Rui05a83d92008-01-17 15:51:24 +0800101}
102
103static int
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000104fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
Zhang Rui05a83d92008-01-17 15:51:24 +0800105{
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,
Rafael J. Wysocki8ad928d2013-07-30 14:36:20 +0200113 state ? ACPI_STATE_D0 : ACPI_STATE_D3_COLD);
Zhang Rui05a83d92008-01-17 15:51:24 +0800114
115 return result;
116}
117
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +0400118static const struct thermal_cooling_device_ops fan_cooling_ops = {
Zhang Rui05a83d92008-01-17 15:51:24 +0800119 .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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124/* --------------------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 Driver Interface
126 -------------------------------------------------------------------------- */
127
Len Brown4be44fc2005-08-05 00:44:28 -0400128static int acpi_fan_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Len Brown4be44fc2005-08-05 00:44:28 -0400130 int result = 0;
Zhang Rui05a83d92008-01-17 15:51:24 +0800131 struct thermal_cooling_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400134 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Pavel Machekc65ade42005-08-05 00:37:45 -0400136 strcpy(acpi_device_name(device), "Fan");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 strcpy(acpi_device_class(device), ACPI_FAN_CLASS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Rafael J. Wysocki488a76c2010-11-25 00:11:24 +0100139 result = acpi_bus_update_power(device->handle, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if (result) {
Rafael J. Wysocki488a76c2010-11-25 00:11:24 +0100141 printk(KERN_ERR PREFIX "Setting initial power state\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 goto end;
143 }
144
Zhang Rui05a83d92008-01-17 15:51:24 +0800145 cdev = thermal_cooling_device_register("Fan", device,
146 &fan_cooling_ops);
Thomas Sujith19b36782008-02-15 01:01:52 -0500147 if (IS_ERR(cdev)) {
148 result = PTR_ERR(cdev);
149 goto end;
150 }
Zhang Rui05a83d92008-01-17 15:51:24 +0800151
Mike Travis13c41152009-12-14 13:38:30 -0800152 dev_dbg(&device->dev, "registered as cooling_device%d\n", cdev->id);
Thomas Sujith19b36782008-02-15 01:01:52 -0500153
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700154 device->driver_data = cdev;
Julia Lawall90300622008-04-11 10:09:24 +0800155 result = sysfs_create_link(&device->dev.kobj,
156 &cdev->device.kobj,
157 "thermal_cooling");
158 if (result)
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200159 dev_err(&device->dev, "Failed to create sysfs link "
160 "'thermal_cooling'\n");
Julia Lawall90300622008-04-11 10:09:24 +0800161
162 result = sysfs_create_link(&cdev->device.kobj,
163 &device->dev.kobj,
164 "device");
165 if (result)
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200166 dev_err(&device->dev, "Failed to create sysfs link "
167 "'device'\n");
Zhang Rui05a83d92008-01-17 15:51:24 +0800168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400170 acpi_device_name(device), acpi_device_bid(device),
171 !device->power.state ? "on" : "off");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Felipe Contreras568b6ad2013-08-30 16:16:05 -0500173end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400174 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
Rafael J. Wysocki51fac832013-01-24 00:24:48 +0100177static int acpi_fan_remove(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Colin Ian Kingf0c29582013-03-25 10:50:05 +0000179 struct thermal_cooling_device *cdev;
Zhang Rui05a83d92008-01-17 15:51:24 +0800180
Colin Ian Kingf0c29582013-03-25 10:50:05 +0000181 if (!device)
182 return -EINVAL;
183
184 cdev = acpi_driver_data(device);
185 if (!cdev)
Patrick Mocheld550d982006-06-27 00:41:40 -0400186 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Zhang Rui05a83d92008-01-17 15:51:24 +0800188 sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
189 sysfs_remove_link(&cdev->device.kobj, "device");
190 thermal_cooling_device_unregister(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Patrick Mocheld550d982006-06-27 00:41:40 -0400192 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200195#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki62fcbdd2012-06-27 23:26:07 +0200196static int acpi_fan_suspend(struct device *dev)
Len Brownec683732008-01-23 22:41:20 -0500197{
Rafael J. Wysocki62fcbdd2012-06-27 23:26:07 +0200198 if (!dev)
Len Brownec683732008-01-23 22:41:20 -0500199 return -EINVAL;
200
Rafael J. Wysocki62fcbdd2012-06-27 23:26:07 +0200201 acpi_bus_set_power(to_acpi_device(dev)->handle, ACPI_STATE_D0);
Len Brownec683732008-01-23 22:41:20 -0500202
203 return AE_OK;
204}
205
Rafael J. Wysocki62fcbdd2012-06-27 23:26:07 +0200206static int acpi_fan_resume(struct device *dev)
Len Brownec683732008-01-23 22:41:20 -0500207{
Rafael J. Wysocki488a76c2010-11-25 00:11:24 +0100208 int result;
Len Brownec683732008-01-23 22:41:20 -0500209
Rafael J. Wysocki62fcbdd2012-06-27 23:26:07 +0200210 if (!dev)
Len Brownec683732008-01-23 22:41:20 -0500211 return -EINVAL;
212
Rafael J. Wysocki62fcbdd2012-06-27 23:26:07 +0200213 result = acpi_bus_update_power(to_acpi_device(dev)->handle, NULL);
Rafael J. Wysocki488a76c2010-11-25 00:11:24 +0100214 if (result)
215 printk(KERN_ERR PREFIX "Error updating fan power state\n");
Len Brownec683732008-01-23 22:41:20 -0500216
217 return result;
218}
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200219#endif
Len Brownec683732008-01-23 22:41:20 -0500220
Mika Westerbergd8014c42012-09-07 10:31:40 +0300221module_acpi_driver(acpi_fan_driver);