blob: 5328b1090e08681dc4905585470fcd43b2b514f1 [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>
Sudip Mukherjee88989fd2014-08-28 19:17:19 +053030#include <linux/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
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#define ACPI_FAN_CLASS "fan"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#define ACPI_FAN_FILE_STATE "state"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#define _COMPONENT ACPI_FAN_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050038ACPI_MODULE_NAME("fan");
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Len Brownf52fd662007-02-12 22:42:12 -050040MODULE_AUTHOR("Paul Diefenbaugh");
Len Brown7cda93e2007-02-12 23:50:02 -050041MODULE_DESCRIPTION("ACPI Fan Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070042MODULE_LICENSE("GPL");
43
Len Brown4be44fc2005-08-05 00:44:28 -040044static int acpi_fan_add(struct acpi_device *device);
Rafael J. Wysocki51fac832013-01-24 00:24:48 +010045static int acpi_fan_remove(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Thomas Renninger1ba90e32007-07-23 14:44:41 +020047static const struct acpi_device_id fan_device_ids[] = {
48 {"PNP0C0B", 0},
49 {"", 0},
50};
51MODULE_DEVICE_TABLE(acpi, fan_device_ids);
52
Rafael J. Wysocki90692402012-08-09 23:00:02 +020053#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki62fcbdd2012-06-27 23:26:07 +020054static int acpi_fan_suspend(struct device *dev);
55static int acpi_fan_resume(struct device *dev);
Aaron Lub9b85152014-02-19 12:16:48 +080056static struct dev_pm_ops acpi_fan_pm = {
57 .resume = acpi_fan_resume,
58 .freeze = acpi_fan_suspend,
59 .thaw = acpi_fan_resume,
60 .restore = acpi_fan_resume,
61};
62#define FAN_PM_OPS_PTR (&acpi_fan_pm)
Shuah Khanb108e0e2014-02-12 20:19:08 -070063#else
Aaron Lub9b85152014-02-19 12:16:48 +080064#define FAN_PM_OPS_PTR NULL
Rafael J. Wysocki90692402012-08-09 23:00:02 +020065#endif
Rafael J. Wysocki62fcbdd2012-06-27 23:26:07 +020066
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static struct acpi_driver acpi_fan_driver = {
Len Brownc2b67052007-02-12 23:33:40 -050068 .name = "fan",
Len Brown4be44fc2005-08-05 00:44:28 -040069 .class = ACPI_FAN_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +020070 .ids = fan_device_ids,
Len Brown4be44fc2005-08-05 00:44:28 -040071 .ops = {
72 .add = acpi_fan_add,
73 .remove = acpi_fan_remove,
74 },
Aaron Lub9b85152014-02-19 12:16:48 +080075 .drv.pm = FAN_PM_OPS_PTR,
Linus Torvalds1da177e2005-04-16 15:20:36 -070076};
77
Zhang Rui05a83d92008-01-17 15:51:24 +080078/* thermal cooling device callbacks */
Matthew Garrett6503e5d2008-11-27 17:48:13 +000079static int fan_get_max_state(struct thermal_cooling_device *cdev, unsigned long
80 *state)
Zhang Rui05a83d92008-01-17 15:51:24 +080081{
82 /* ACPI fan device only support two states: ON/OFF */
Matthew Garrett6503e5d2008-11-27 17:48:13 +000083 *state = 1;
84 return 0;
Zhang Rui05a83d92008-01-17 15:51:24 +080085}
86
Matthew Garrett6503e5d2008-11-27 17:48:13 +000087static int fan_get_cur_state(struct thermal_cooling_device *cdev, unsigned long
88 *state)
Zhang Rui05a83d92008-01-17 15:51:24 +080089{
90 struct acpi_device *device = cdev->devdata;
Zhang Rui05a83d92008-01-17 15:51:24 +080091 int result;
Naresh Bhat85eb9822013-07-01 19:51:00 +053092 int acpi_state = ACPI_STATE_D0;
Zhang Rui05a83d92008-01-17 15:51:24 +080093
94 if (!device)
95 return -EINVAL;
96
Rafael J. Wysocki488a76c2010-11-25 00:11:24 +010097 result = acpi_bus_update_power(device->handle, &acpi_state);
Zhang Rui05a83d92008-01-17 15:51:24 +080098 if (result)
99 return result;
100
Rafael J. Wysocki8ad928d2013-07-30 14:36:20 +0200101 *state = (acpi_state == ACPI_STATE_D3_COLD ? 0 :
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000102 (acpi_state == ACPI_STATE_D0 ? 1 : -1));
103 return 0;
Zhang Rui05a83d92008-01-17 15:51:24 +0800104}
105
106static int
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000107fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
Zhang Rui05a83d92008-01-17 15:51:24 +0800108{
109 struct acpi_device *device = cdev->devdata;
110 int result;
111
112 if (!device || (state != 0 && state != 1))
113 return -EINVAL;
114
115 result = acpi_bus_set_power(device->handle,
Rafael J. Wysocki8ad928d2013-07-30 14:36:20 +0200116 state ? ACPI_STATE_D0 : ACPI_STATE_D3_COLD);
Zhang Rui05a83d92008-01-17 15:51:24 +0800117
118 return result;
119}
120
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +0400121static const struct thermal_cooling_device_ops fan_cooling_ops = {
Zhang Rui05a83d92008-01-17 15:51:24 +0800122 .get_max_state = fan_get_max_state,
123 .get_cur_state = fan_get_cur_state,
124 .set_cur_state = fan_set_cur_state,
125};
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127/* --------------------------------------------------------------------------
Sudip Mukherjee88989fd2014-08-28 19:17:19 +0530128 * Driver Interface
129 * --------------------------------------------------------------------------
130*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Len Brown4be44fc2005-08-05 00:44:28 -0400132static int acpi_fan_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Len Brown4be44fc2005-08-05 00:44:28 -0400134 int result = 0;
Zhang Rui05a83d92008-01-17 15:51:24 +0800135 struct thermal_cooling_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400138 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Pavel Machekc65ade42005-08-05 00:37:45 -0400140 strcpy(acpi_device_name(device), "Fan");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 strcpy(acpi_device_class(device), ACPI_FAN_CLASS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Rafael J. Wysocki488a76c2010-11-25 00:11:24 +0100143 result = acpi_bus_update_power(device->handle, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 if (result) {
Sudip Mukherjee88989fd2014-08-28 19:17:19 +0530145 dev_err(&device->dev, "Setting initial power state\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 goto end;
147 }
148
Zhang Rui05a83d92008-01-17 15:51:24 +0800149 cdev = thermal_cooling_device_register("Fan", device,
150 &fan_cooling_ops);
Thomas Sujith19b36782008-02-15 01:01:52 -0500151 if (IS_ERR(cdev)) {
152 result = PTR_ERR(cdev);
153 goto end;
154 }
Zhang Rui05a83d92008-01-17 15:51:24 +0800155
Mike Travis13c41152009-12-14 13:38:30 -0800156 dev_dbg(&device->dev, "registered as cooling_device%d\n", cdev->id);
Thomas Sujith19b36782008-02-15 01:01:52 -0500157
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700158 device->driver_data = cdev;
Julia Lawall90300622008-04-11 10:09:24 +0800159 result = sysfs_create_link(&device->dev.kobj,
160 &cdev->device.kobj,
161 "thermal_cooling");
162 if (result)
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200163 dev_err(&device->dev, "Failed to create sysfs link "
164 "'thermal_cooling'\n");
Julia Lawall90300622008-04-11 10:09:24 +0800165
166 result = sysfs_create_link(&cdev->device.kobj,
167 &device->dev.kobj,
168 "device");
169 if (result)
Sudip Mukherjee88989fd2014-08-28 19:17:19 +0530170 dev_err(&device->dev, "Failed to create sysfs link 'device'\n");
Zhang Rui05a83d92008-01-17 15:51:24 +0800171
Sudip Mukherjee88989fd2014-08-28 19:17:19 +0530172 dev_info(&device->dev, "ACPI: %s [%s] (%s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400173 acpi_device_name(device), acpi_device_bid(device),
174 !device->power.state ? "on" : "off");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Felipe Contreras568b6ad2013-08-30 16:16:05 -0500176end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400177 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
Rafael J. Wysocki51fac832013-01-24 00:24:48 +0100180static int acpi_fan_remove(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Colin Ian Kingf0c29582013-03-25 10:50:05 +0000182 struct thermal_cooling_device *cdev;
Zhang Rui05a83d92008-01-17 15:51:24 +0800183
Colin Ian Kingf0c29582013-03-25 10:50:05 +0000184 if (!device)
185 return -EINVAL;
186
187 cdev = acpi_driver_data(device);
188 if (!cdev)
Patrick Mocheld550d982006-06-27 00:41:40 -0400189 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Zhang Rui05a83d92008-01-17 15:51:24 +0800191 sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
192 sysfs_remove_link(&cdev->device.kobj, "device");
193 thermal_cooling_device_unregister(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Patrick Mocheld550d982006-06-27 00:41:40 -0400195 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200198#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki62fcbdd2012-06-27 23:26:07 +0200199static int acpi_fan_suspend(struct device *dev)
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 acpi_bus_set_power(to_acpi_device(dev)->handle, ACPI_STATE_D0);
Len Brownec683732008-01-23 22:41:20 -0500205
206 return AE_OK;
207}
208
Rafael J. Wysocki62fcbdd2012-06-27 23:26:07 +0200209static int acpi_fan_resume(struct device *dev)
Len Brownec683732008-01-23 22:41:20 -0500210{
Rafael J. Wysocki488a76c2010-11-25 00:11:24 +0100211 int result;
Len Brownec683732008-01-23 22:41:20 -0500212
Rafael J. Wysocki62fcbdd2012-06-27 23:26:07 +0200213 if (!dev)
Len Brownec683732008-01-23 22:41:20 -0500214 return -EINVAL;
215
Rafael J. Wysocki62fcbdd2012-06-27 23:26:07 +0200216 result = acpi_bus_update_power(to_acpi_device(dev)->handle, NULL);
Rafael J. Wysocki488a76c2010-11-25 00:11:24 +0100217 if (result)
Sudip Mukherjee88989fd2014-08-28 19:17:19 +0530218 dev_err(dev, "Error updating fan power state\n");
Len Brownec683732008-01-23 22:41:20 -0500219
220 return result;
221}
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200222#endif
Len Brownec683732008-01-23 22:41:20 -0500223
Mika Westerbergd8014c42012-09-07 10:31:40 +0300224module_acpi_driver(acpi_fan_driver);