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 30
/*
 *  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>
#include <asm/uaccess.h>
31
#include <linux/thermal.h>
32
#include <linux/acpi.h>
L
Linus Torvalds 已提交
33

34 35
#define PREFIX "ACPI: "

L
Linus Torvalds 已提交
36 37 38 39
#define ACPI_FAN_CLASS			"fan"
#define ACPI_FAN_FILE_STATE		"state"

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

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

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

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

55
#ifdef CONFIG_PM_SLEEP
56 57
static int acpi_fan_suspend(struct device *dev);
static int acpi_fan_resume(struct device *dev);
58 59 60
#else
#define acpi_fan_suspend NULL
#define acpi_fan_resume NULL
61
#endif
62 63
static SIMPLE_DEV_PM_OPS(acpi_fan_pm, acpi_fan_suspend, acpi_fan_resume);

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

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

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

	if (!device)
		return -EINVAL;

94
	result = acpi_bus_update_power(device->handle, &acpi_state);
95 96 97
	if (result)
		return result;

98
	*state = (acpi_state == ACPI_STATE_D3_COLD ? 0 :
99 100
		 (acpi_state == ACPI_STATE_D0 ? 1 : -1));
	return 0;
101 102 103
}

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

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

	result = acpi_bus_set_power(device->handle,
113
				state ? ACPI_STATE_D0 : ACPI_STATE_D3_COLD);
114 115 116 117

	return result;
}

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

L
Linus Torvalds 已提交
124 125 126 127
/* --------------------------------------------------------------------------
                                 Driver Interface
   -------------------------------------------------------------------------- */

L
Len Brown 已提交
128
static int acpi_fan_add(struct acpi_device *device)
L
Linus Torvalds 已提交
129
{
L
Len Brown 已提交
130
	int result = 0;
131
	struct thermal_cooling_device *cdev;
L
Linus Torvalds 已提交
132 133

	if (!device)
134
		return -EINVAL;
L
Linus Torvalds 已提交
135

P
Pavel Machek 已提交
136
	strcpy(acpi_device_name(device), "Fan");
L
Linus Torvalds 已提交
137 138
	strcpy(acpi_device_class(device), ACPI_FAN_CLASS);

139
	result = acpi_bus_update_power(device->handle, NULL);
L
Linus Torvalds 已提交
140
	if (result) {
141
		printk(KERN_ERR PREFIX "Setting initial power state\n");
L
Linus Torvalds 已提交
142 143 144
		goto end;
	}

145 146
	cdev = thermal_cooling_device_register("Fan", device,
						&fan_cooling_ops);
147 148 149 150
	if (IS_ERR(cdev)) {
		result = PTR_ERR(cdev);
		goto end;
	}
151

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

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

	result = sysfs_create_link(&cdev->device.kobj,
				   &device->dev.kobj,
				   "device");
	if (result)
166 167
		dev_err(&device->dev, "Failed to create sysfs link "
			"'device'\n");
168

L
Linus Torvalds 已提交
169
	printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
L
Len Brown 已提交
170 171
	       acpi_device_name(device), acpi_device_bid(device),
	       !device->power.state ? "on" : "off");
L
Linus Torvalds 已提交
172

173
end:
174
	return result;
L
Linus Torvalds 已提交
175 176
}

177
static int acpi_fan_remove(struct acpi_device *device)
L
Linus Torvalds 已提交
178
{
179 180 181 182
	struct thermal_cooling_device *cdev;

	if (!device)
		return -EINVAL;
183

184 185
	cdev =  acpi_driver_data(device);
	if (!cdev)
186
		return -EINVAL;
L
Linus Torvalds 已提交
187

188 189 190
	sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
	sysfs_remove_link(&cdev->device.kobj, "device");
	thermal_cooling_device_unregister(cdev);
L
Linus Torvalds 已提交
191

192
	return 0;
L
Linus Torvalds 已提交
193 194
}

195
#ifdef CONFIG_PM_SLEEP
196
static int acpi_fan_suspend(struct device *dev)
197
{
198
	if (!dev)
199 200
		return -EINVAL;

201
	acpi_bus_set_power(to_acpi_device(dev)->handle, ACPI_STATE_D0);
202 203 204 205

	return AE_OK;
}

206
static int acpi_fan_resume(struct device *dev)
207
{
208
	int result;
209

210
	if (!dev)
211 212
		return -EINVAL;

213
	result = acpi_bus_update_power(to_acpi_device(dev)->handle, NULL);
214 215
	if (result)
		printk(KERN_ERR PREFIX "Error updating fan power state\n");
216 217 218

	return result;
}
219
#endif
220

221
module_acpi_driver(acpi_fan_driver);