fan.c 5.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
 *  acpi_fan.c - ACPI Fan Driver ($Revision: 29 $)
 *
 *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
 *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or (at
 *  your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/types.h>
S
Sudip Mukherjee 已提交
30
#include <linux/uaccess.h>
31
#include <linux/thermal.h>
32
#include <linux/acpi.h>
L
Linus Torvalds 已提交
33 34 35 36 37

#define ACPI_FAN_CLASS			"fan"
#define ACPI_FAN_FILE_STATE		"state"

#define _COMPONENT		ACPI_FAN_COMPONENT
38
ACPI_MODULE_NAME("fan");
L
Linus Torvalds 已提交
39

40
MODULE_AUTHOR("Paul Diefenbaugh");
41
MODULE_DESCRIPTION("ACPI Fan Driver");
L
Linus Torvalds 已提交
42 43
MODULE_LICENSE("GPL");

L
Len Brown 已提交
44
static int acpi_fan_add(struct acpi_device *device);
45
static int acpi_fan_remove(struct acpi_device *device);
L
Linus Torvalds 已提交
46

47 48 49 50 51 52
static const struct acpi_device_id fan_device_ids[] = {
	{"PNP0C0B", 0},
	{"", 0},
};
MODULE_DEVICE_TABLE(acpi, fan_device_ids);

53
#ifdef CONFIG_PM_SLEEP
54 55
static int acpi_fan_suspend(struct device *dev);
static int acpi_fan_resume(struct device *dev);
56 57 58 59 60 61 62
static struct dev_pm_ops acpi_fan_pm = {
	.resume = acpi_fan_resume,
	.freeze = acpi_fan_suspend,
	.thaw = acpi_fan_resume,
	.restore = acpi_fan_resume,
};
#define FAN_PM_OPS_PTR (&acpi_fan_pm)
63
#else
64
#define FAN_PM_OPS_PTR NULL
65
#endif
66

L
Linus Torvalds 已提交
67
static struct acpi_driver acpi_fan_driver = {
L
Len Brown 已提交
68
	.name = "fan",
L
Len Brown 已提交
69
	.class = ACPI_FAN_CLASS,
70
	.ids = fan_device_ids,
L
Len Brown 已提交
71 72 73 74
	.ops = {
		.add = acpi_fan_add,
		.remove = acpi_fan_remove,
		},
75
	.drv.pm = FAN_PM_OPS_PTR,
L
Linus Torvalds 已提交
76 77
};

78
/* thermal cooling device callbacks */
79 80
static int fan_get_max_state(struct thermal_cooling_device *cdev, unsigned long
			     *state)
81 82
{
	/* ACPI fan device only support two states: ON/OFF */
83 84
	*state = 1;
	return 0;
85 86
}

87 88
static int fan_get_cur_state(struct thermal_cooling_device *cdev, unsigned long
			     *state)
89 90 91
{
	struct acpi_device *device = cdev->devdata;
	int result;
92
	int acpi_state = ACPI_STATE_D0;
93 94 95 96

	if (!device)
		return -EINVAL;

97
	result = acpi_bus_update_power(device->handle, &acpi_state);
98 99 100
	if (result)
		return result;

101
	*state = (acpi_state == ACPI_STATE_D3_COLD ? 0 :
102 103
		 (acpi_state == ACPI_STATE_D0 ? 1 : -1));
	return 0;
104 105 106
}

static int
107
fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
108 109 110 111 112 113 114 115
{
	struct acpi_device *device = cdev->devdata;
	int result;

	if (!device || (state != 0 && state != 1))
		return -EINVAL;

	result = acpi_bus_set_power(device->handle,
116
				state ? ACPI_STATE_D0 : ACPI_STATE_D3_COLD);
117 118 119 120

	return result;
}

V
Vasiliy Kulikov 已提交
121
static const struct thermal_cooling_device_ops fan_cooling_ops = {
122 123 124 125 126
	.get_max_state = fan_get_max_state,
	.get_cur_state = fan_get_cur_state,
	.set_cur_state = fan_set_cur_state,
};

L
Linus Torvalds 已提交
127
/* --------------------------------------------------------------------------
S
Sudip Mukherjee 已提交
128 129 130
 *                               Driver Interface
 * --------------------------------------------------------------------------
*/
L
Linus Torvalds 已提交
131

L
Len Brown 已提交
132
static int acpi_fan_add(struct acpi_device *device)
L
Linus Torvalds 已提交
133
{
L
Len Brown 已提交
134
	int result = 0;
135
	struct thermal_cooling_device *cdev;
L
Linus Torvalds 已提交
136 137

	if (!device)
138
		return -EINVAL;
L
Linus Torvalds 已提交
139

P
Pavel Machek 已提交
140
	strcpy(acpi_device_name(device), "Fan");
L
Linus Torvalds 已提交
141 142
	strcpy(acpi_device_class(device), ACPI_FAN_CLASS);

143
	result = acpi_bus_update_power(device->handle, NULL);
L
Linus Torvalds 已提交
144
	if (result) {
S
Sudip Mukherjee 已提交
145
		dev_err(&device->dev, "Setting initial power state\n");
L
Linus Torvalds 已提交
146 147 148
		goto end;
	}

149 150
	cdev = thermal_cooling_device_register("Fan", device,
						&fan_cooling_ops);
151 152 153 154
	if (IS_ERR(cdev)) {
		result = PTR_ERR(cdev);
		goto end;
	}
155

156
	dev_dbg(&device->dev, "registered as cooling_device%d\n", cdev->id);
157

158
	device->driver_data = cdev;
159 160 161 162
	result = sysfs_create_link(&device->dev.kobj,
				   &cdev->device.kobj,
				   "thermal_cooling");
	if (result)
163 164
		dev_err(&device->dev, "Failed to create sysfs link "
			"'thermal_cooling'\n");
165 166 167 168 169

	result = sysfs_create_link(&cdev->device.kobj,
				   &device->dev.kobj,
				   "device");
	if (result)
S
Sudip Mukherjee 已提交
170
		dev_err(&device->dev, "Failed to create sysfs link 'device'\n");
171

S
Sudip Mukherjee 已提交
172
	dev_info(&device->dev, "ACPI: %s [%s] (%s)\n",
L
Len Brown 已提交
173 174
	       acpi_device_name(device), acpi_device_bid(device),
	       !device->power.state ? "on" : "off");
L
Linus Torvalds 已提交
175

176
end:
177
	return result;
L
Linus Torvalds 已提交
178 179
}

180
static int acpi_fan_remove(struct acpi_device *device)
L
Linus Torvalds 已提交
181
{
182 183 184 185
	struct thermal_cooling_device *cdev;

	if (!device)
		return -EINVAL;
186

187 188
	cdev =  acpi_driver_data(device);
	if (!cdev)
189
		return -EINVAL;
L
Linus Torvalds 已提交
190

191 192 193
	sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
	sysfs_remove_link(&cdev->device.kobj, "device");
	thermal_cooling_device_unregister(cdev);
L
Linus Torvalds 已提交
194

195
	return 0;
L
Linus Torvalds 已提交
196 197
}

198
#ifdef CONFIG_PM_SLEEP
199
static int acpi_fan_suspend(struct device *dev)
200
{
201
	if (!dev)
202 203
		return -EINVAL;

204
	acpi_bus_set_power(to_acpi_device(dev)->handle, ACPI_STATE_D0);
205 206 207 208

	return AE_OK;
}

209
static int acpi_fan_resume(struct device *dev)
210
{
211
	int result;
212

213
	if (!dev)
214 215
		return -EINVAL;

216
	result = acpi_bus_update_power(to_acpi_device(dev)->handle, NULL);
217
	if (result)
S
Sudip Mukherjee 已提交
218
		dev_err(dev, "Error updating fan power state\n");
219 220 221

	return result;
}
222
#endif
223

224
module_acpi_driver(acpi_fan_driver);