blob: 77b8e1eaa71743e189b862f4dfd1229e93f84848 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * acpi_thermal.c - ACPI Thermal Zone Driver ($Revision: 41 $)
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 * This driver fully implements the ACPI thermal policy as described in the
26 * ACPI 2.0 Specification.
27 *
28 * TBD: 1. Implement passive cooling hysteresis.
29 * 2. Enhance passive cooling (CPU) states/limit interface to support
30 * concepts of 'multiple limiters', upper/lower limits, etc.
31 *
32 */
33
34#include <linux/kernel.h>
35#include <linux/module.h>
Len Brown0b5bfa12007-08-12 00:13:02 -040036#include <linux/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/init.h>
38#include <linux/types.h>
39#include <linux/proc_fs.h>
Tim Schmielaucd354f12007-02-14 00:33:14 -080040#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/kmod.h>
42#include <linux/seq_file.h>
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -070043#include <linux/reboot.h>
Stephen Rothwellf6f5c452009-03-03 12:47:17 +110044#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/uaccess.h>
Zhang Rui3f655ef2008-01-17 15:51:11 +080046#include <linux/thermal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <acpi/acpi_bus.h>
48#include <acpi/acpi_drivers.h>
49
Len Browna192a952009-07-28 16:45:54 -040050#define PREFIX "ACPI: "
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define ACPI_THERMAL_CLASS "thermal_zone"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define ACPI_THERMAL_DEVICE_NAME "Thermal Zone"
54#define ACPI_THERMAL_FILE_STATE "state"
55#define ACPI_THERMAL_FILE_TEMPERATURE "temperature"
56#define ACPI_THERMAL_FILE_TRIP_POINTS "trip_points"
57#define ACPI_THERMAL_FILE_COOLING_MODE "cooling_mode"
58#define ACPI_THERMAL_FILE_POLLING_FREQ "polling_frequency"
59#define ACPI_THERMAL_NOTIFY_TEMPERATURE 0x80
60#define ACPI_THERMAL_NOTIFY_THRESHOLDS 0x81
61#define ACPI_THERMAL_NOTIFY_DEVICES 0x82
62#define ACPI_THERMAL_NOTIFY_CRITICAL 0xF0
63#define ACPI_THERMAL_NOTIFY_HOT 0xF1
64#define ACPI_THERMAL_MODE_ACTIVE 0x00
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66#define ACPI_THERMAL_MAX_ACTIVE 10
67#define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#define _COMPONENT ACPI_THERMAL_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050070ACPI_MODULE_NAME("thermal");
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Thomas Renninger1cbf4c52004-09-16 11:07:00 -040072MODULE_AUTHOR("Paul Diefenbaugh");
Len Brown7cda93e2007-02-12 23:50:02 -050073MODULE_DESCRIPTION("ACPI Thermal Zone Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070074MODULE_LICENSE("GPL");
75
Len Brownf8707ec2007-08-12 00:12:54 -040076static int act;
77module_param(act, int, 0644);
Len Brown3c1d36d2007-08-14 15:12:56 -040078MODULE_PARM_DESC(act, "Disable or override all lowest active trip points.");
Len Brownf8707ec2007-08-12 00:12:54 -040079
Len Brownc52a7412007-08-14 15:49:32 -040080static int crt;
81module_param(crt, int, 0644);
82MODULE_PARM_DESC(crt, "Disable or lower all critical trip points.");
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084static int tzp;
Len Brown730ff342007-08-12 00:12:26 -040085module_param(tzp, int, 0444);
Len Brown3c1d36d2007-08-14 15:12:56 -040086MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Len Brownf5487142007-08-12 00:12:44 -040088static int nocrt;
89module_param(nocrt, int, 0);
Len Brown8c99fdc2007-08-20 18:46:50 -040090MODULE_PARM_DESC(nocrt, "Set to take no action upon ACPI thermal zone critical trips points.");
Len Brownf5487142007-08-12 00:12:44 -040091
Len Brown72b33ef2007-08-12 00:12:17 -040092static int off;
93module_param(off, int, 0);
Len Brown3c1d36d2007-08-14 15:12:56 -040094MODULE_PARM_DESC(off, "Set to disable ACPI thermal support.");
Len Brown72b33ef2007-08-12 00:12:17 -040095
Len Browna70cdc52007-08-12 00:12:35 -040096static int psv;
97module_param(psv, int, 0644);
Len Brown3c1d36d2007-08-14 15:12:56 -040098MODULE_PARM_DESC(psv, "Disable or override all passive trip points.");
Len Browna70cdc52007-08-12 00:12:35 -040099
Len Brown4be44fc2005-08-05 00:44:28 -0400100static int acpi_thermal_add(struct acpi_device *device);
101static int acpi_thermal_remove(struct acpi_device *device, int type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800102static int acpi_thermal_resume(struct acpi_device *device);
Bjorn Helgaas342d5502009-04-07 15:37:06 +0000103static void acpi_thermal_notify(struct acpi_device *device, u32 event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file);
105static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file);
106static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file);
Len Brown4be44fc2005-08-05 00:44:28 -0400108static ssize_t acpi_thermal_write_cooling_mode(struct file *,
109 const char __user *, size_t,
110 loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file);
Len Brown4be44fc2005-08-05 00:44:28 -0400112static ssize_t acpi_thermal_write_polling(struct file *, const char __user *,
113 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200115static const struct acpi_device_id thermal_device_ids[] = {
116 {ACPI_THERMAL_HID, 0},
117 {"", 0},
118};
119MODULE_DEVICE_TABLE(acpi, thermal_device_ids);
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121static struct acpi_driver acpi_thermal_driver = {
Len Brownc2b67052007-02-12 23:33:40 -0500122 .name = "thermal",
Len Brown4be44fc2005-08-05 00:44:28 -0400123 .class = ACPI_THERMAL_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200124 .ids = thermal_device_ids,
Len Brown4be44fc2005-08-05 00:44:28 -0400125 .ops = {
126 .add = acpi_thermal_add,
127 .remove = acpi_thermal_remove,
Konstantin Karasyov74ce14682006-05-08 08:32:00 -0400128 .resume = acpi_thermal_resume,
Bjorn Helgaas342d5502009-04-07 15:37:06 +0000129 .notify = acpi_thermal_notify,
Len Brown4be44fc2005-08-05 00:44:28 -0400130 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131};
132
133struct acpi_thermal_state {
Len Brown4be44fc2005-08-05 00:44:28 -0400134 u8 critical:1;
135 u8 hot:1;
136 u8 passive:1;
137 u8 active:1;
138 u8 reserved:4;
139 int active_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140};
141
142struct acpi_thermal_state_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400143 u8 valid:1;
144 u8 enabled:1;
145 u8 reserved:6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146};
147
148struct acpi_thermal_critical {
149 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400150 unsigned long temperature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151};
152
153struct acpi_thermal_hot {
154 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400155 unsigned long temperature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156};
157
158struct acpi_thermal_passive {
159 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400160 unsigned long temperature;
161 unsigned long tc1;
162 unsigned long tc2;
163 unsigned long tsp;
164 struct acpi_handle_list devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165};
166
167struct acpi_thermal_active {
168 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400169 unsigned long temperature;
170 struct acpi_handle_list devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171};
172
173struct acpi_thermal_trips {
174 struct acpi_thermal_critical critical;
Len Brown4be44fc2005-08-05 00:44:28 -0400175 struct acpi_thermal_hot hot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 struct acpi_thermal_passive passive;
177 struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
178};
179
180struct acpi_thermal_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400181 u8 cooling_mode:1; /* _SCP */
182 u8 devices:1; /* _TZD */
183 u8 reserved:6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184};
185
186struct acpi_thermal {
Patrick Mochel8348e1b2006-05-19 16:54:40 -0400187 struct acpi_device * device;
Len Brown4be44fc2005-08-05 00:44:28 -0400188 acpi_bus_id name;
189 unsigned long temperature;
190 unsigned long last_temperature;
191 unsigned long polling_frequency;
Len Brown4be44fc2005-08-05 00:44:28 -0400192 volatile u8 zombie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 struct acpi_thermal_flags flags;
194 struct acpi_thermal_state state;
195 struct acpi_thermal_trips trips;
Len Brown4be44fc2005-08-05 00:44:28 -0400196 struct acpi_handle_list devices;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800197 struct thermal_zone_device *thermal_zone;
198 int tz_enabled;
Jean Delvare13614e32009-04-06 16:01:46 +0200199 int kelvin_offset;
Alexey Starikovskiy6e215782007-09-01 00:11:59 +0400200 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201};
202
Arjan van de Vend7508032006-07-04 13:06:00 -0400203static const struct file_operations acpi_thermal_state_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700204 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400205 .open = acpi_thermal_state_open_fs,
206 .read = seq_read,
207 .llseek = seq_lseek,
208 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209};
210
Arjan van de Vend7508032006-07-04 13:06:00 -0400211static const struct file_operations acpi_thermal_temp_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700212 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400213 .open = acpi_thermal_temp_open_fs,
214 .read = seq_read,
215 .llseek = seq_lseek,
216 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217};
218
Arjan van de Vend7508032006-07-04 13:06:00 -0400219static const struct file_operations acpi_thermal_trip_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700220 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400221 .open = acpi_thermal_trip_open_fs,
222 .read = seq_read,
Len Brown4be44fc2005-08-05 00:44:28 -0400223 .llseek = seq_lseek,
224 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225};
226
Arjan van de Vend7508032006-07-04 13:06:00 -0400227static const struct file_operations acpi_thermal_cooling_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700228 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400229 .open = acpi_thermal_cooling_open_fs,
230 .read = seq_read,
231 .write = acpi_thermal_write_cooling_mode,
232 .llseek = seq_lseek,
233 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234};
235
Arjan van de Vend7508032006-07-04 13:06:00 -0400236static const struct file_operations acpi_thermal_polling_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700237 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400238 .open = acpi_thermal_polling_open_fs,
239 .read = seq_read,
240 .write = acpi_thermal_write_polling,
241 .llseek = seq_lseek,
242 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243};
244
245/* --------------------------------------------------------------------------
246 Thermal Zone Management
247 -------------------------------------------------------------------------- */
248
Len Brown4be44fc2005-08-05 00:44:28 -0400249static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Len Brown4be44fc2005-08-05 00:44:28 -0400251 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400252 unsigned long long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400255 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 tz->last_temperature = tz->temperature;
258
Matthew Wilcox27663c52008-10-10 02:22:59 -0400259 status = acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400261 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Matthew Wilcox27663c52008-10-10 02:22:59 -0400263 tz->temperature = tmp;
Len Brown4be44fc2005-08-05 00:44:28 -0400264 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
265 tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Patrick Mocheld550d982006-06-27 00:41:40 -0400267 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
Len Brown4be44fc2005-08-05 00:44:28 -0400270static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Len Brown4be44fc2005-08-05 00:44:28 -0400272 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400273 unsigned long long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400276 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Matthew Wilcox27663c52008-10-10 02:22:59 -0400278 status = acpi_evaluate_integer(tz->device->handle, "_TZP", NULL, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400280 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Matthew Wilcox27663c52008-10-10 02:22:59 -0400282 tz->polling_frequency = tmp;
Len Brown4be44fc2005-08-05 00:44:28 -0400283 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
284 tz->polling_frequency));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Patrick Mocheld550d982006-06-27 00:41:40 -0400286 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
Len Brown4be44fc2005-08-05 00:44:28 -0400289static int acpi_thermal_set_polling(struct acpi_thermal *tz, int seconds)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400293 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 tz->polling_frequency = seconds * 10; /* Convert value to deci-seconds */
296
Matthew Garrettb1569e92008-12-03 17:55:32 +0000297 tz->thermal_zone->polling_delay = seconds * 1000;
298
299 if (tz->tz_enabled)
300 thermal_zone_device_update(tz->thermal_zone);
301
Len Brown4be44fc2005-08-05 00:44:28 -0400302 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
303 "Polling frequency set to %lu seconds\n",
Sanjoy Mahajan636cedf2007-02-16 01:24:43 -0500304 tz->polling_frequency/10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Patrick Mocheld550d982006-06-27 00:41:40 -0400306 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
Len Brown4be44fc2005-08-05 00:44:28 -0400309static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Len Brown4be44fc2005-08-05 00:44:28 -0400311 acpi_status status = AE_OK;
312 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
313 struct acpi_object_list arg_list = { 1, &arg0 };
314 acpi_handle handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400318 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400320 status = acpi_get_handle(tz->device->handle, "_SCP", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 if (ACPI_FAILURE(status)) {
322 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400323 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
325
326 arg0.integer.value = mode;
327
328 status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
329 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400330 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Patrick Mocheld550d982006-06-27 00:41:40 -0400332 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
Zhang Ruice44e192008-01-17 15:51:25 +0800335#define ACPI_TRIPS_CRITICAL 0x01
336#define ACPI_TRIPS_HOT 0x02
337#define ACPI_TRIPS_PASSIVE 0x04
338#define ACPI_TRIPS_ACTIVE 0x08
339#define ACPI_TRIPS_DEVICES 0x10
340
341#define ACPI_TRIPS_REFRESH_THRESHOLDS (ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE)
342#define ACPI_TRIPS_REFRESH_DEVICES ACPI_TRIPS_DEVICES
343
344#define ACPI_TRIPS_INIT (ACPI_TRIPS_CRITICAL | ACPI_TRIPS_HOT | \
345 ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE | \
346 ACPI_TRIPS_DEVICES)
347
348/*
349 * This exception is thrown out in two cases:
350 * 1.An invalid trip point becomes invalid or a valid trip point becomes invalid
351 * when re-evaluating the AML code.
352 * 2.TODO: Devices listed in _PSL, _ALx, _TZD may change.
353 * We need to re-bind the cooling devices of a thermal zone when this occurs.
354 */
355#define ACPI_THERMAL_TRIPS_EXCEPTION(flags, str) \
356do { \
357 if (flags != ACPI_TRIPS_INIT) \
358 ACPI_EXCEPTION((AE_INFO, AE_ERROR, \
359 "ACPI thermal trip point %s changed\n" \
360 "Please send acpidump to linux-acpi@vger.kernel.org\n", str)); \
361} while (0)
362
363static int acpi_thermal_trips_update(struct acpi_thermal *tz, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Len Brown4be44fc2005-08-05 00:44:28 -0400365 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400366 unsigned long long tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800367 struct acpi_handle_list devices;
368 int valid = 0;
369 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Thomas Renningerfa809452010-02-20 11:44:27 +0100371 /* Critical Shutdown */
Zhang Ruice44e192008-01-17 15:51:25 +0800372 if (flag & ACPI_TRIPS_CRITICAL) {
373 status = acpi_evaluate_integer(tz->device->handle,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400374 "_CRT", NULL, &tmp);
375 tz->trips.critical.temperature = tmp;
Arjan van de Vena39a2d72008-05-19 15:55:15 -0700376 /*
377 * Treat freezing temperatures as invalid as well; some
378 * BIOSes return really low values and cause reboots at startup.
Adam Buchbinderb731d7b2009-03-13 12:15:26 -0400379 * Below zero (Celsius) values clearly aren't right for sure..
Arjan van de Vena39a2d72008-05-19 15:55:15 -0700380 * ... so lets discard those as invalid.
381 */
Thomas Renningerfa809452010-02-20 11:44:27 +0100382 if (ACPI_FAILURE(status)) {
Len Brownc52a7412007-08-14 15:49:32 -0400383 tz->trips.critical.flags.valid = 0;
Thomas Renningerfa809452010-02-20 11:44:27 +0100384 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
385 "No critical threshold\n"));
386 } else if (tmp <= 2732) {
387 printk(KERN_WARNING FW_BUG "Invalid critical threshold "
388 "(%llu)\n", tmp);
389 tz->trips.critical.flags.valid = 0;
Zhang Ruice44e192008-01-17 15:51:25 +0800390 } else {
391 tz->trips.critical.flags.valid = 1;
392 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Thomas Renningerfa809452010-02-20 11:44:27 +0100393 "Found critical threshold [%lu]\n",
394 tz->trips.critical.temperature));
Zhang Ruice44e192008-01-17 15:51:25 +0800395 }
396 if (tz->trips.critical.flags.valid == 1) {
397 if (crt == -1) {
398 tz->trips.critical.flags.valid = 0;
399 } else if (crt > 0) {
400 unsigned long crt_k = CELSIUS_TO_KELVIN(crt);
401 /*
Zhang Rui22a94d72008-10-17 02:41:20 -0400402 * Allow override critical threshold
Zhang Ruice44e192008-01-17 15:51:25 +0800403 */
Zhang Rui22a94d72008-10-17 02:41:20 -0400404 if (crt_k > tz->trips.critical.temperature)
405 printk(KERN_WARNING PREFIX
406 "Critical threshold %d C\n", crt);
407 tz->trips.critical.temperature = crt_k;
Zhang Ruice44e192008-01-17 15:51:25 +0800408 }
Len Brownc52a7412007-08-14 15:49:32 -0400409 }
410 }
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 /* Critical Sleep (optional) */
Zhang Ruice44e192008-01-17 15:51:25 +0800413 if (flag & ACPI_TRIPS_HOT) {
Len Browna70cdc52007-08-12 00:12:35 -0400414 status = acpi_evaluate_integer(tz->device->handle,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400415 "_HOT", NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800416 if (ACPI_FAILURE(status)) {
417 tz->trips.hot.flags.valid = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400418 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Zhang Ruice44e192008-01-17 15:51:25 +0800419 "No hot threshold\n"));
420 } else {
Matthew Wilcox27663c52008-10-10 02:22:59 -0400421 tz->trips.hot.temperature = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800422 tz->trips.hot.flags.valid = 1;
423 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
424 "Found hot threshold [%lu]\n",
425 tz->trips.critical.temperature));
426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
428
Zhang Ruice44e192008-01-17 15:51:25 +0800429 /* Passive (optional) */
Zhang Rui0e4240d2009-01-16 12:53:42 -0500430 if (((flag & ACPI_TRIPS_PASSIVE) && tz->trips.passive.flags.valid) ||
431 (flag == ACPI_TRIPS_INIT)) {
Zhang Ruice44e192008-01-17 15:51:25 +0800432 valid = tz->trips.passive.flags.valid;
433 if (psv == -1) {
434 status = AE_SUPPORT;
435 } else if (psv > 0) {
Matthew Wilcox27663c52008-10-10 02:22:59 -0400436 tmp = CELSIUS_TO_KELVIN(psv);
Zhang Ruice44e192008-01-17 15:51:25 +0800437 status = AE_OK;
438 } else {
439 status = acpi_evaluate_integer(tz->device->handle,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400440 "_PSV", NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Zhang Ruice44e192008-01-17 15:51:25 +0800443 if (ACPI_FAILURE(status))
444 tz->trips.passive.flags.valid = 0;
445 else {
Matthew Wilcox27663c52008-10-10 02:22:59 -0400446 tz->trips.passive.temperature = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800447 tz->trips.passive.flags.valid = 1;
448 if (flag == ACPI_TRIPS_INIT) {
449 status = acpi_evaluate_integer(
450 tz->device->handle, "_TC1",
Matthew Wilcox27663c52008-10-10 02:22:59 -0400451 NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800452 if (ACPI_FAILURE(status))
453 tz->trips.passive.flags.valid = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400454 else
455 tz->trips.passive.tc1 = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800456 status = acpi_evaluate_integer(
457 tz->device->handle, "_TC2",
Matthew Wilcox27663c52008-10-10 02:22:59 -0400458 NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800459 if (ACPI_FAILURE(status))
460 tz->trips.passive.flags.valid = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400461 else
462 tz->trips.passive.tc2 = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800463 status = acpi_evaluate_integer(
464 tz->device->handle, "_TSP",
Matthew Wilcox27663c52008-10-10 02:22:59 -0400465 NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800466 if (ACPI_FAILURE(status))
467 tz->trips.passive.flags.valid = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400468 else
469 tz->trips.passive.tsp = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800470 }
471 }
472 }
473 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.passive.flags.valid) {
474 memset(&devices, 0, sizeof(struct acpi_handle_list));
475 status = acpi_evaluate_reference(tz->device->handle, "_PSL",
476 NULL, &devices);
Zhang Rui0e4240d2009-01-16 12:53:42 -0500477 if (ACPI_FAILURE(status)) {
478 printk(KERN_WARNING PREFIX
479 "Invalid passive threshold\n");
Zhang Ruice44e192008-01-17 15:51:25 +0800480 tz->trips.passive.flags.valid = 0;
Zhang Rui0e4240d2009-01-16 12:53:42 -0500481 }
Zhang Ruice44e192008-01-17 15:51:25 +0800482 else
483 tz->trips.passive.flags.valid = 1;
484
485 if (memcmp(&tz->trips.passive.devices, &devices,
486 sizeof(struct acpi_handle_list))) {
487 memcpy(&tz->trips.passive.devices, &devices,
488 sizeof(struct acpi_handle_list));
489 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
490 }
491 }
492 if ((flag & ACPI_TRIPS_PASSIVE) || (flag & ACPI_TRIPS_DEVICES)) {
493 if (valid != tz->trips.passive.flags.valid)
494 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
495 }
496
497 /* Active (optional) */
Len Brown4be44fc2005-08-05 00:44:28 -0400498 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Len Brown4be44fc2005-08-05 00:44:28 -0400499 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
Zhang Ruice44e192008-01-17 15:51:25 +0800500 valid = tz->trips.active[i].flags.valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Len Brownf8707ec2007-08-12 00:12:54 -0400502 if (act == -1)
Zhang Ruice44e192008-01-17 15:51:25 +0800503 break; /* disable all active trip points */
Len Brownf8707ec2007-08-12 00:12:54 -0400504
Zhang Rui0e4240d2009-01-16 12:53:42 -0500505 if ((flag == ACPI_TRIPS_INIT) || ((flag & ACPI_TRIPS_ACTIVE) &&
506 tz->trips.active[i].flags.valid)) {
Zhang Ruice44e192008-01-17 15:51:25 +0800507 status = acpi_evaluate_integer(tz->device->handle,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400508 name, NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800509 if (ACPI_FAILURE(status)) {
510 tz->trips.active[i].flags.valid = 0;
511 if (i == 0)
512 break;
513 if (act <= 0)
514 break;
515 if (i == 1)
516 tz->trips.active[0].temperature =
517 CELSIUS_TO_KELVIN(act);
518 else
519 /*
520 * Don't allow override higher than
521 * the next higher trip point
522 */
523 tz->trips.active[i - 1].temperature =
524 (tz->trips.active[i - 2].temperature <
525 CELSIUS_TO_KELVIN(act) ?
526 tz->trips.active[i - 2].temperature :
527 CELSIUS_TO_KELVIN(act));
Len Brownf8707ec2007-08-12 00:12:54 -0400528 break;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400529 } else {
530 tz->trips.active[i].temperature = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800531 tz->trips.active[i].flags.valid = 1;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400532 }
Len Brownf8707ec2007-08-12 00:12:54 -0400533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 name[2] = 'L';
Zhang Ruice44e192008-01-17 15:51:25 +0800536 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.active[i].flags.valid ) {
537 memset(&devices, 0, sizeof(struct acpi_handle_list));
538 status = acpi_evaluate_reference(tz->device->handle,
539 name, NULL, &devices);
Zhang Rui0e4240d2009-01-16 12:53:42 -0500540 if (ACPI_FAILURE(status)) {
541 printk(KERN_WARNING PREFIX
542 "Invalid active%d threshold\n", i);
Zhang Ruice44e192008-01-17 15:51:25 +0800543 tz->trips.active[i].flags.valid = 0;
Zhang Rui0e4240d2009-01-16 12:53:42 -0500544 }
Zhang Ruice44e192008-01-17 15:51:25 +0800545 else
546 tz->trips.active[i].flags.valid = 1;
547
548 if (memcmp(&tz->trips.active[i].devices, &devices,
549 sizeof(struct acpi_handle_list))) {
550 memcpy(&tz->trips.active[i].devices, &devices,
551 sizeof(struct acpi_handle_list));
552 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
553 }
554 }
555 if ((flag & ACPI_TRIPS_ACTIVE) || (flag & ACPI_TRIPS_DEVICES))
556 if (valid != tz->trips.active[i].flags.valid)
557 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
558
559 if (!tz->trips.active[i].flags.valid)
560 break;
561 }
562
563 if (flag & ACPI_TRIPS_DEVICES) {
564 memset(&devices, 0, sizeof(struct acpi_handle_list));
565 status = acpi_evaluate_reference(tz->device->handle, "_TZD",
566 NULL, &devices);
567 if (memcmp(&tz->devices, &devices,
568 sizeof(struct acpi_handle_list))) {
569 memcpy(&tz->devices, &devices,
570 sizeof(struct acpi_handle_list));
571 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
574
Patrick Mocheld550d982006-06-27 00:41:40 -0400575 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
Zhang Ruice44e192008-01-17 15:51:25 +0800578static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Zhang Ruice44e192008-01-17 15:51:25 +0800580 return acpi_thermal_trips_update(tz, ACPI_TRIPS_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
Len Brown4be44fc2005-08-05 00:44:28 -0400583static void acpi_thermal_check(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200585 struct acpi_thermal *tz = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Matthew Garrettb1569e92008-12-03 17:55:32 +0000587 thermal_zone_device_update(tz->thermal_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
Zhang Rui3f655ef2008-01-17 15:51:11 +0800590/* sys I/F for generic thermal sysfs support */
Jean Delvare13614e32009-04-06 16:01:46 +0200591#define KELVIN_TO_MILLICELSIUS(t, off) (((t) - (off)) * 100)
Zhang, Rui5e012762008-02-28 07:51:30 +0800592
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000593static int thermal_get_temp(struct thermal_zone_device *thermal,
594 unsigned long *temp)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800595{
596 struct acpi_thermal *tz = thermal->devdata;
Zhang, Rui76ecb4f2008-04-10 16:20:23 +0800597 int result;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800598
599 if (!tz)
600 return -EINVAL;
601
Zhang, Rui76ecb4f2008-04-10 16:20:23 +0800602 result = acpi_thermal_get_temperature(tz);
603 if (result)
604 return result;
605
Jean Delvare13614e32009-04-06 16:01:46 +0200606 *temp = KELVIN_TO_MILLICELSIUS(tz->temperature, tz->kelvin_offset);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000607 return 0;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800608}
609
610static const char enabled[] = "kernel";
611static const char disabled[] = "user";
612static int thermal_get_mode(struct thermal_zone_device *thermal,
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000613 enum thermal_device_mode *mode)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800614{
615 struct acpi_thermal *tz = thermal->devdata;
616
617 if (!tz)
618 return -EINVAL;
619
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000620 *mode = tz->tz_enabled ? THERMAL_DEVICE_ENABLED :
621 THERMAL_DEVICE_DISABLED;
622
623 return 0;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800624}
625
626static int thermal_set_mode(struct thermal_zone_device *thermal,
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000627 enum thermal_device_mode mode)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800628{
629 struct acpi_thermal *tz = thermal->devdata;
630 int enable;
631
632 if (!tz)
633 return -EINVAL;
634
635 /*
636 * enable/disable thermal management from ACPI thermal driver
637 */
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000638 if (mode == THERMAL_DEVICE_ENABLED)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800639 enable = 1;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000640 else if (mode == THERMAL_DEVICE_DISABLED)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800641 enable = 0;
642 else
643 return -EINVAL;
644
645 if (enable != tz->tz_enabled) {
646 tz->tz_enabled = enable;
647 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
648 "%s ACPI thermal control\n",
649 tz->tz_enabled ? enabled : disabled));
650 acpi_thermal_check(tz);
651 }
652 return 0;
653}
654
655static int thermal_get_trip_type(struct thermal_zone_device *thermal,
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000656 int trip, enum thermal_trip_type *type)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800657{
658 struct acpi_thermal *tz = thermal->devdata;
659 int i;
660
661 if (!tz || trip < 0)
662 return -EINVAL;
663
664 if (tz->trips.critical.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000665 if (!trip) {
666 *type = THERMAL_TRIP_CRITICAL;
667 return 0;
668 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800669 trip--;
670 }
671
672 if (tz->trips.hot.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000673 if (!trip) {
674 *type = THERMAL_TRIP_HOT;
675 return 0;
676 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800677 trip--;
678 }
679
680 if (tz->trips.passive.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000681 if (!trip) {
682 *type = THERMAL_TRIP_PASSIVE;
683 return 0;
684 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800685 trip--;
686 }
687
688 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
689 tz->trips.active[i].flags.valid; i++) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000690 if (!trip) {
691 *type = THERMAL_TRIP_ACTIVE;
692 return 0;
693 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800694 trip--;
695 }
696
697 return -EINVAL;
698}
699
700static int thermal_get_trip_temp(struct thermal_zone_device *thermal,
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000701 int trip, unsigned long *temp)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800702{
703 struct acpi_thermal *tz = thermal->devdata;
704 int i;
705
706 if (!tz || trip < 0)
707 return -EINVAL;
708
709 if (tz->trips.critical.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000710 if (!trip) {
711 *temp = KELVIN_TO_MILLICELSIUS(
Jean Delvare13614e32009-04-06 16:01:46 +0200712 tz->trips.critical.temperature,
713 tz->kelvin_offset);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000714 return 0;
715 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800716 trip--;
717 }
718
719 if (tz->trips.hot.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000720 if (!trip) {
721 *temp = KELVIN_TO_MILLICELSIUS(
Jean Delvare13614e32009-04-06 16:01:46 +0200722 tz->trips.hot.temperature,
723 tz->kelvin_offset);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000724 return 0;
725 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800726 trip--;
727 }
728
729 if (tz->trips.passive.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000730 if (!trip) {
731 *temp = KELVIN_TO_MILLICELSIUS(
Jean Delvare13614e32009-04-06 16:01:46 +0200732 tz->trips.passive.temperature,
733 tz->kelvin_offset);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000734 return 0;
735 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800736 trip--;
737 }
738
739 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
740 tz->trips.active[i].flags.valid; i++) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000741 if (!trip) {
742 *temp = KELVIN_TO_MILLICELSIUS(
Jean Delvare13614e32009-04-06 16:01:46 +0200743 tz->trips.active[i].temperature,
744 tz->kelvin_offset);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000745 return 0;
746 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800747 trip--;
748 }
749
750 return -EINVAL;
751}
752
Zhang, Rui9ec732f2008-04-10 16:13:10 +0800753static int thermal_get_crit_temp(struct thermal_zone_device *thermal,
754 unsigned long *temperature) {
755 struct acpi_thermal *tz = thermal->devdata;
756
757 if (tz->trips.critical.flags.valid) {
758 *temperature = KELVIN_TO_MILLICELSIUS(
Jean Delvare13614e32009-04-06 16:01:46 +0200759 tz->trips.critical.temperature,
760 tz->kelvin_offset);
Zhang, Rui9ec732f2008-04-10 16:13:10 +0800761 return 0;
762 } else
763 return -EINVAL;
764}
765
Matthew Garrettb1569e92008-12-03 17:55:32 +0000766static int thermal_notify(struct thermal_zone_device *thermal, int trip,
767 enum thermal_trip_type trip_type)
768{
769 u8 type = 0;
770 struct acpi_thermal *tz = thermal->devdata;
771
772 if (trip_type == THERMAL_TRIP_CRITICAL)
773 type = ACPI_THERMAL_NOTIFY_CRITICAL;
774 else if (trip_type == THERMAL_TRIP_HOT)
775 type = ACPI_THERMAL_NOTIFY_HOT;
776 else
777 return 0;
778
779 acpi_bus_generate_proc_event(tz->device, type, 1);
780 acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
Stephen Rothwellf6f5c452009-03-03 12:47:17 +1100781 dev_name(&tz->device->dev), type, 1);
Matthew Garrettb1569e92008-12-03 17:55:32 +0000782
783 if (trip_type == THERMAL_TRIP_CRITICAL && nocrt)
784 return 1;
785
786 return 0;
787}
788
Zhang Rui3f655ef2008-01-17 15:51:11 +0800789typedef int (*cb)(struct thermal_zone_device *, int,
790 struct thermal_cooling_device *);
791static int acpi_thermal_cooling_device_cb(struct thermal_zone_device *thermal,
792 struct thermal_cooling_device *cdev,
793 cb action)
794{
795 struct acpi_device *device = cdev->devdata;
796 struct acpi_thermal *tz = thermal->devdata;
Zhang Rui653a00c2008-01-17 15:51:18 +0800797 struct acpi_device *dev;
798 acpi_status status;
799 acpi_handle handle;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800800 int i;
801 int j;
802 int trip = -1;
803 int result = 0;
804
805 if (tz->trips.critical.flags.valid)
806 trip++;
807
808 if (tz->trips.hot.flags.valid)
809 trip++;
810
811 if (tz->trips.passive.flags.valid) {
812 trip++;
813 for (i = 0; i < tz->trips.passive.devices.count;
814 i++) {
Zhang Rui653a00c2008-01-17 15:51:18 +0800815 handle = tz->trips.passive.devices.handles[i];
816 status = acpi_bus_get_device(handle, &dev);
817 if (ACPI_SUCCESS(status) && (dev == device)) {
818 result = action(thermal, trip, cdev);
819 if (result)
820 goto failed;
821 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800822 }
823 }
824
825 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
826 if (!tz->trips.active[i].flags.valid)
827 break;
828 trip++;
829 for (j = 0;
830 j < tz->trips.active[i].devices.count;
831 j++) {
Zhang Rui653a00c2008-01-17 15:51:18 +0800832 handle = tz->trips.active[i].devices.handles[j];
833 status = acpi_bus_get_device(handle, &dev);
834 if (ACPI_SUCCESS(status) && (dev == device)) {
835 result = action(thermal, trip, cdev);
836 if (result)
837 goto failed;
838 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800839 }
840 }
841
842 for (i = 0; i < tz->devices.count; i++) {
Zhang Rui653a00c2008-01-17 15:51:18 +0800843 handle = tz->devices.handles[i];
844 status = acpi_bus_get_device(handle, &dev);
845 if (ACPI_SUCCESS(status) && (dev == device)) {
846 result = action(thermal, -1, cdev);
847 if (result)
848 goto failed;
849 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800850 }
851
852failed:
853 return result;
854}
855
856static int
857acpi_thermal_bind_cooling_device(struct thermal_zone_device *thermal,
858 struct thermal_cooling_device *cdev)
859{
860 return acpi_thermal_cooling_device_cb(thermal, cdev,
861 thermal_zone_bind_cooling_device);
862}
863
864static int
865acpi_thermal_unbind_cooling_device(struct thermal_zone_device *thermal,
866 struct thermal_cooling_device *cdev)
867{
868 return acpi_thermal_cooling_device_cb(thermal, cdev,
869 thermal_zone_unbind_cooling_device);
870}
871
872static struct thermal_zone_device_ops acpi_thermal_zone_ops = {
873 .bind = acpi_thermal_bind_cooling_device,
874 .unbind = acpi_thermal_unbind_cooling_device,
875 .get_temp = thermal_get_temp,
876 .get_mode = thermal_get_mode,
877 .set_mode = thermal_set_mode,
878 .get_trip_type = thermal_get_trip_type,
879 .get_trip_temp = thermal_get_trip_temp,
Zhang, Rui9ec732f2008-04-10 16:13:10 +0800880 .get_crit_temp = thermal_get_crit_temp,
Matthew Garrettb1569e92008-12-03 17:55:32 +0000881 .notify = thermal_notify,
Zhang Rui3f655ef2008-01-17 15:51:11 +0800882};
883
884static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz)
885{
886 int trips = 0;
887 int result;
Zhang Rui20733932008-01-17 15:51:21 +0800888 acpi_status status;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800889 int i;
890
891 if (tz->trips.critical.flags.valid)
892 trips++;
893
894 if (tz->trips.hot.flags.valid)
895 trips++;
896
897 if (tz->trips.passive.flags.valid)
898 trips++;
899
900 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
901 tz->trips.active[i].flags.valid; i++, trips++);
Matthew Garrettb1569e92008-12-03 17:55:32 +0000902
903 if (tz->trips.passive.flags.valid)
904 tz->thermal_zone =
905 thermal_zone_device_register("acpitz", trips, tz,
906 &acpi_thermal_zone_ops,
907 tz->trips.passive.tc1,
908 tz->trips.passive.tc2,
909 tz->trips.passive.tsp*100,
910 tz->polling_frequency*100);
911 else
912 tz->thermal_zone =
913 thermal_zone_device_register("acpitz", trips, tz,
914 &acpi_thermal_zone_ops,
915 0, 0, 0,
Matthew Garrett67405432009-04-14 20:16:45 +0100916 tz->polling_frequency*100);
Krzysztof Heltbb070e42008-04-08 17:41:52 -0700917 if (IS_ERR(tz->thermal_zone))
Zhang Rui3f655ef2008-01-17 15:51:11 +0800918 return -ENODEV;
919
920 result = sysfs_create_link(&tz->device->dev.kobj,
921 &tz->thermal_zone->device.kobj, "thermal_zone");
922 if (result)
923 return result;
924
925 result = sysfs_create_link(&tz->thermal_zone->device.kobj,
926 &tz->device->dev.kobj, "device");
927 if (result)
928 return result;
929
Zhang Rui20733932008-01-17 15:51:21 +0800930 status = acpi_attach_data(tz->device->handle,
931 acpi_bus_private_data_handler,
932 tz->thermal_zone);
933 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800934 printk(KERN_ERR PREFIX
935 "Error attaching device data\n");
Zhang Rui20733932008-01-17 15:51:21 +0800936 return -ENODEV;
937 }
938
Zhang Rui3f655ef2008-01-17 15:51:11 +0800939 tz->tz_enabled = 1;
940
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200941 dev_info(&tz->device->dev, "registered as thermal_zone%d\n",
942 tz->thermal_zone->id);
Zhang Rui3f655ef2008-01-17 15:51:11 +0800943 return 0;
944}
945
946static void acpi_thermal_unregister_thermal_zone(struct acpi_thermal *tz)
947{
948 sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
949 sysfs_remove_link(&tz->thermal_zone->device.kobj, "device");
950 thermal_zone_device_unregister(tz->thermal_zone);
951 tz->thermal_zone = NULL;
Zhang Rui20733932008-01-17 15:51:21 +0800952 acpi_detach_data(tz->device->handle, acpi_bus_private_data_handler);
Zhang Rui3f655ef2008-01-17 15:51:11 +0800953}
954
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956/* --------------------------------------------------------------------------
957 FS Interface (/proc)
958 -------------------------------------------------------------------------- */
959
Len Brown4be44fc2005-08-05 00:44:28 -0400960static struct proc_dir_entry *acpi_thermal_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
963{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200964 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 if (!tz)
968 goto end;
969
970 seq_puts(seq, "state: ");
971
Len Brown4be44fc2005-08-05 00:44:28 -0400972 if (!tz->state.critical && !tz->state.hot && !tz->state.passive
973 && !tz->state.active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 seq_puts(seq, "ok\n");
975 else {
976 if (tz->state.critical)
977 seq_puts(seq, "critical ");
978 if (tz->state.hot)
979 seq_puts(seq, "hot ");
980 if (tz->state.passive)
981 seq_puts(seq, "passive ");
982 if (tz->state.active)
983 seq_printf(seq, "active[%d]", tz->state.active_index);
984 seq_puts(seq, "\n");
985 }
986
Len Brown4be44fc2005-08-05 00:44:28 -0400987 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400988 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989}
990
991static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
992{
993 return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
994}
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
997{
Len Brown4be44fc2005-08-05 00:44:28 -0400998 int result = 0;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200999 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 if (!tz)
1003 goto end;
1004
1005 result = acpi_thermal_get_temperature(tz);
1006 if (result)
1007 goto end;
1008
Len Brown4be44fc2005-08-05 00:44:28 -04001009 seq_printf(seq, "temperature: %ld C\n",
1010 KELVIN_TO_CELSIUS(tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Len Brown4be44fc2005-08-05 00:44:28 -04001012 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001013 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014}
1015
1016static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
1017{
1018 return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
1019}
1020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
1022{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001023 struct acpi_thermal *tz = seq->private;
Thomas Renninger68ccfaa2006-11-19 23:01:40 +01001024 struct acpi_device *device;
Thomas Renningere7c746e2007-06-18 00:40:51 -04001025 acpi_status status;
1026
Len Brown4be44fc2005-08-05 00:44:28 -04001027 int i = 0;
1028 int j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 if (!tz)
1032 goto end;
1033
1034 if (tz->trips.critical.flags.valid)
Len Brownf5487142007-08-12 00:12:44 -04001035 seq_printf(seq, "critical (S5): %ld C%s",
1036 KELVIN_TO_CELSIUS(tz->trips.critical.temperature),
1037 nocrt ? " <disabled>\n" : "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039 if (tz->trips.hot.flags.valid)
Len Brownf5487142007-08-12 00:12:44 -04001040 seq_printf(seq, "hot (S4): %ld C%s",
1041 KELVIN_TO_CELSIUS(tz->trips.hot.temperature),
1042 nocrt ? " <disabled>\n" : "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 if (tz->trips.passive.flags.valid) {
Len Brown4be44fc2005-08-05 00:44:28 -04001045 seq_printf(seq,
1046 "passive: %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
1047 KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
1048 tz->trips.passive.tc1, tz->trips.passive.tc2,
1049 tz->trips.passive.tsp);
1050 for (j = 0; j < tz->trips.passive.devices.count; j++) {
Thomas Renningere7c746e2007-06-18 00:40:51 -04001051 status = acpi_bus_get_device(tz->trips.passive.devices.
1052 handles[j], &device);
1053 seq_printf(seq, "%4.4s ", status ? "" :
1054 acpi_device_bid(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
1056 seq_puts(seq, "\n");
Frans Pop7fb26162009-10-26 08:39:01 +01001057 } else {
1058 seq_printf(seq, "passive (forced):");
1059 if (tz->thermal_zone->forced_passive)
1060 seq_printf(seq, " %i C\n",
1061 tz->thermal_zone->forced_passive / 1000);
1062 else
1063 seq_printf(seq, "<not set>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
1065
1066 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1067 if (!(tz->trips.active[i].flags.valid))
1068 break;
1069 seq_printf(seq, "active[%d]: %ld C: devices=",
Len Brown4be44fc2005-08-05 00:44:28 -04001070 i,
1071 KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
Thomas Renninger68ccfaa2006-11-19 23:01:40 +01001072 for (j = 0; j < tz->trips.active[i].devices.count; j++){
Thomas Renningere7c746e2007-06-18 00:40:51 -04001073 status = acpi_bus_get_device(tz->trips.active[i].
1074 devices.handles[j],
1075 &device);
1076 seq_printf(seq, "%4.4s ", status ? "" :
1077 acpi_device_bid(device));
Thomas Renninger68ccfaa2006-11-19 23:01:40 +01001078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 seq_puts(seq, "\n");
1080 }
1081
Len Brown4be44fc2005-08-05 00:44:28 -04001082 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001083 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084}
1085
1086static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
1087{
1088 return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
1089}
1090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
1092{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001093 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096 if (!tz)
1097 goto end;
1098
Len Browneaca2d32007-04-30 23:27:43 -04001099 if (!tz->flags.cooling_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 seq_puts(seq, "<setting not supported>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 else
Len Browneaca2d32007-04-30 23:27:43 -04001102 seq_puts(seq, "0 - Active; 1 - Passive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Len Brown4be44fc2005-08-05 00:44:28 -04001104 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001105 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106}
1107
1108static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
1109{
1110 return single_open(file, acpi_thermal_cooling_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -04001111 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112}
1113
1114static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001115acpi_thermal_write_cooling_mode(struct file *file,
1116 const char __user * buffer,
1117 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001119 struct seq_file *m = file->private_data;
1120 struct acpi_thermal *tz = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001121 int result = 0;
1122 char mode_string[12] = { '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 if (!tz || (count > sizeof(mode_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -04001126 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 if (!tz->flags.cooling_mode)
Patrick Mocheld550d982006-06-27 00:41:40 -04001129 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
1131 if (copy_from_user(mode_string, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001132 return -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -04001133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 mode_string[count] = '\0';
Len Brown4be44fc2005-08-05 00:44:28 -04001135
1136 result = acpi_thermal_set_cooling_mode(tz,
1137 simple_strtoul(mode_string, NULL,
1138 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001140 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
1142 acpi_thermal_check(tz);
1143
Patrick Mocheld550d982006-06-27 00:41:40 -04001144 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145}
1146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
1148{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001149 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 if (!tz)
1153 goto end;
1154
Matthew Garrettb1569e92008-12-03 17:55:32 +00001155 if (!tz->thermal_zone->polling_delay) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 seq_puts(seq, "<polling disabled>\n");
1157 goto end;
1158 }
1159
Matthew Garrettb1569e92008-12-03 17:55:32 +00001160 seq_printf(seq, "polling frequency: %d seconds\n",
1161 (tz->thermal_zone->polling_delay / 1000));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Len Brown4be44fc2005-08-05 00:44:28 -04001163 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001164 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165}
1166
1167static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
1168{
1169 return single_open(file, acpi_thermal_polling_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -04001170 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171}
1172
1173static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001174acpi_thermal_write_polling(struct file *file,
1175 const char __user * buffer,
1176 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001178 struct seq_file *m = file->private_data;
1179 struct acpi_thermal *tz = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001180 int result = 0;
1181 char polling_string[12] = { '\0' };
1182 int seconds = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 if (!tz || (count > sizeof(polling_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -04001186 return -EINVAL;
Len Brown4be44fc2005-08-05 00:44:28 -04001187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 if (copy_from_user(polling_string, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001189 return -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -04001190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 polling_string[count] = '\0';
1192
1193 seconds = simple_strtoul(polling_string, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 result = acpi_thermal_set_polling(tz, seconds);
1196 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001197 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 acpi_thermal_check(tz);
1200
Patrick Mocheld550d982006-06-27 00:41:40 -04001201 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202}
1203
Len Brown4be44fc2005-08-05 00:44:28 -04001204static int acpi_thermal_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
Len Brown4be44fc2005-08-05 00:44:28 -04001206 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 if (!acpi_device_dir(device)) {
1210 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -04001211 acpi_thermal_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001213 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 }
1215
1216 /* 'state' [R] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001217 entry = proc_create_data(ACPI_THERMAL_FILE_STATE,
1218 S_IRUGO, acpi_device_dir(device),
1219 &acpi_thermal_state_fops,
1220 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001222 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 /* 'temperature' [R] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001225 entry = proc_create_data(ACPI_THERMAL_FILE_TEMPERATURE,
1226 S_IRUGO, acpi_device_dir(device),
1227 &acpi_thermal_temp_fops,
1228 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001230 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Pavel Machek2db9ccb2007-08-24 11:45:50 +02001232 /* 'trip_points' [R] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001233 entry = proc_create_data(ACPI_THERMAL_FILE_TRIP_POINTS,
1234 S_IRUGO,
1235 acpi_device_dir(device),
1236 &acpi_thermal_trip_fops,
1237 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001239 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 /* 'cooling_mode' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001242 entry = proc_create_data(ACPI_THERMAL_FILE_COOLING_MODE,
1243 S_IFREG | S_IRUGO | S_IWUSR,
1244 acpi_device_dir(device),
1245 &acpi_thermal_cooling_fops,
1246 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001248 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 /* 'polling_frequency' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001251 entry = proc_create_data(ACPI_THERMAL_FILE_POLLING_FREQ,
1252 S_IFREG | S_IRUGO | S_IWUSR,
1253 acpi_device_dir(device),
1254 &acpi_thermal_polling_fops,
1255 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001257 return -ENODEV;
Patrick Mocheld550d982006-06-27 00:41:40 -04001258 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259}
1260
Len Brown4be44fc2005-08-05 00:44:28 -04001261static int acpi_thermal_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 if (acpi_device_dir(device)) {
1265 remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1266 acpi_device_dir(device));
1267 remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1268 acpi_device_dir(device));
1269 remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1270 acpi_device_dir(device));
1271 remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1272 acpi_device_dir(device));
1273 remove_proc_entry(ACPI_THERMAL_FILE_STATE,
1274 acpi_device_dir(device));
1275 remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
1276 acpi_device_dir(device) = NULL;
1277 }
1278
Patrick Mocheld550d982006-06-27 00:41:40 -04001279 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280}
1281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282/* --------------------------------------------------------------------------
1283 Driver Interface
1284 -------------------------------------------------------------------------- */
1285
Bjorn Helgaas342d5502009-04-07 15:37:06 +00001286static void acpi_thermal_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
Bjorn Helgaas342d5502009-04-07 15:37:06 +00001288 struct acpi_thermal *tz = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001292 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 switch (event) {
1295 case ACPI_THERMAL_NOTIFY_TEMPERATURE:
1296 acpi_thermal_check(tz);
1297 break;
1298 case ACPI_THERMAL_NOTIFY_THRESHOLDS:
Zhang Ruice44e192008-01-17 15:51:25 +08001299 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_THRESHOLDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 acpi_thermal_check(tz);
Len Brown14e04fb2007-08-23 15:20:26 -04001301 acpi_bus_generate_proc_event(device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08001302 acpi_bus_generate_netlink_event(device->pnp.device_class,
Kay Sievers07944692008-10-30 01:18:59 +01001303 dev_name(&device->dev), event, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 break;
1305 case ACPI_THERMAL_NOTIFY_DEVICES:
Zhang Ruice44e192008-01-17 15:51:25 +08001306 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_DEVICES);
1307 acpi_thermal_check(tz);
Len Brown14e04fb2007-08-23 15:20:26 -04001308 acpi_bus_generate_proc_event(device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08001309 acpi_bus_generate_netlink_event(device->pnp.device_class,
Kay Sievers07944692008-10-30 01:18:59 +01001310 dev_name(&device->dev), event, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 break;
1312 default:
1313 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001314 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 break;
1316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317}
1318
Len Brown4be44fc2005-08-05 00:44:28 -04001319static int acpi_thermal_get_info(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
Len Brown4be44fc2005-08-05 00:44:28 -04001321 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001325 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327 /* Get temperature [_TMP] (required) */
1328 result = acpi_thermal_get_temperature(tz);
1329 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001330 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332 /* Get trip points [_CRT, _PSV, etc.] (required) */
1333 result = acpi_thermal_get_trip_points(tz);
1334 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001335 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 /* Set the cooling mode [_SCP] to active cooling (default) */
1338 result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
Len Brown4be44fc2005-08-05 00:44:28 -04001339 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 tz->flags.cooling_mode = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 /* Get default polling frequency [_TZP] (optional) */
1343 if (tzp)
1344 tz->polling_frequency = tzp;
1345 else
1346 acpi_thermal_get_polling_frequency(tz);
1347
Patrick Mocheld550d982006-06-27 00:41:40 -04001348 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349}
1350
Jean Delvare13614e32009-04-06 16:01:46 +02001351/*
1352 * The exact offset between Kelvin and degree Celsius is 273.15. However ACPI
1353 * handles temperature values with a single decimal place. As a consequence,
1354 * some implementations use an offset of 273.1 and others use an offset of
1355 * 273.2. Try to find out which one is being used, to present the most
1356 * accurate and visually appealing number.
1357 *
1358 * The heuristic below should work for all ACPI thermal zones which have a
1359 * critical trip point with a value being a multiple of 0.5 degree Celsius.
1360 */
1361static void acpi_thermal_guess_offset(struct acpi_thermal *tz)
1362{
1363 if (tz->trips.critical.flags.valid &&
1364 (tz->trips.critical.temperature % 5) == 1)
1365 tz->kelvin_offset = 2731;
1366 else
1367 tz->kelvin_offset = 2732;
1368}
1369
Len Brown4be44fc2005-08-05 00:44:28 -04001370static int acpi_thermal_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
Len Brown4be44fc2005-08-05 00:44:28 -04001372 int result = 0;
Len Brown4be44fc2005-08-05 00:44:28 -04001373 struct acpi_thermal *tz = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001377 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Burman Yan36bcbec2006-12-19 12:56:11 -08001379 tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001381 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Patrick Mochel8348e1b2006-05-19 16:54:40 -04001383 tz->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 strcpy(tz->name, device->pnp.bus_id);
1385 strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1386 strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001387 device->driver_data = tz;
Alexey Starikovskiy6e215782007-09-01 00:11:59 +04001388 mutex_init(&tz->lock);
Zhang Rui3f655ef2008-01-17 15:51:11 +08001389
1390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 result = acpi_thermal_get_info(tz);
1392 if (result)
Zhang Rui3f655ef2008-01-17 15:51:11 +08001393 goto free_memory;
1394
Jean Delvare13614e32009-04-06 16:01:46 +02001395 acpi_thermal_guess_offset(tz);
1396
Zhang Rui3f655ef2008-01-17 15:51:11 +08001397 result = acpi_thermal_register_thermal_zone(tz);
1398 if (result)
1399 goto free_memory;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 result = acpi_thermal_add_fs(device);
1402 if (result)
Zhang Rui3f655ef2008-01-17 15:51:11 +08001403 goto unregister_thermal_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001406 acpi_device_name(device), acpi_device_bid(device),
1407 KELVIN_TO_CELSIUS(tz->temperature));
Zhang Rui3f655ef2008-01-17 15:51:11 +08001408 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Zhang Rui3f655ef2008-01-17 15:51:11 +08001410unregister_thermal_zone:
1411 thermal_zone_device_unregister(tz->thermal_zone);
1412free_memory:
1413 kfree(tz);
1414end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001415 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416}
1417
Len Brown4be44fc2005-08-05 00:44:28 -04001418static int acpi_thermal_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
Len Brown4be44fc2005-08-05 00:44:28 -04001420 struct acpi_thermal *tz = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001423 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001425 tz = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 acpi_thermal_remove_fs(device);
Zhang Rui3f655ef2008-01-17 15:51:11 +08001428 acpi_thermal_unregister_thermal_zone(tz);
Alexey Starikovskiy6e215782007-09-01 00:11:59 +04001429 mutex_destroy(&tz->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 kfree(tz);
Patrick Mocheld550d982006-06-27 00:41:40 -04001431 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432}
1433
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001434static int acpi_thermal_resume(struct acpi_device *device)
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001435{
1436 struct acpi_thermal *tz = NULL;
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001437 int i, j, power_state, result;
1438
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001439
1440 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001441 return -EINVAL;
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001442
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001443 tz = acpi_driver_data(device);
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001444
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001445 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001446 if (!(&tz->trips.active[i]))
1447 break;
1448 if (!tz->trips.active[i].flags.valid)
1449 break;
1450 tz->trips.active[i].flags.enabled = 1;
1451 for (j = 0; j < tz->trips.active[i].devices.count; j++) {
1452 result = acpi_bus_get_power(tz->trips.active[i].devices.
1453 handles[j], &power_state);
1454 if (result || (power_state != ACPI_STATE_D0)) {
1455 tz->trips.active[i].flags.enabled = 0;
1456 break;
1457 }
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001458 }
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001459 tz->state.active |= tz->trips.active[i].flags.enabled;
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001460 }
1461
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001462 acpi_thermal_check(tz);
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001463
1464 return AE_OK;
1465}
1466
Jeff Garzik18552562007-10-03 15:15:40 -04001467static int thermal_act(const struct dmi_system_id *d) {
Len Brown0b5bfa12007-08-12 00:13:02 -04001468
1469 if (act == 0) {
1470 printk(KERN_NOTICE "ACPI: %s detected: "
1471 "disabling all active thermal trip points\n", d->ident);
1472 act = -1;
1473 }
1474 return 0;
1475}
Jeff Garzik18552562007-10-03 15:15:40 -04001476static int thermal_nocrt(const struct dmi_system_id *d) {
Len Brown8c99fdc2007-08-20 18:46:50 -04001477
1478 printk(KERN_NOTICE "ACPI: %s detected: "
1479 "disabling all critical thermal trip point actions.\n", d->ident);
1480 nocrt = 1;
1481 return 0;
1482}
Jeff Garzik18552562007-10-03 15:15:40 -04001483static int thermal_tzp(const struct dmi_system_id *d) {
Len Brown0b5bfa12007-08-12 00:13:02 -04001484
1485 if (tzp == 0) {
1486 printk(KERN_NOTICE "ACPI: %s detected: "
1487 "enabling thermal zone polling\n", d->ident);
1488 tzp = 300; /* 300 dS = 30 Seconds */
1489 }
1490 return 0;
1491}
Jeff Garzik18552562007-10-03 15:15:40 -04001492static int thermal_psv(const struct dmi_system_id *d) {
Len Brown0b5bfa12007-08-12 00:13:02 -04001493
1494 if (psv == 0) {
1495 printk(KERN_NOTICE "ACPI: %s detected: "
1496 "disabling all passive thermal trip points\n", d->ident);
1497 psv = -1;
1498 }
1499 return 0;
1500}
1501
1502static struct dmi_system_id thermal_dmi_table[] __initdata = {
1503 /*
1504 * Award BIOS on this AOpen makes thermal control almost worthless.
1505 * http://bugzilla.kernel.org/show_bug.cgi?id=8842
1506 */
1507 {
1508 .callback = thermal_act,
1509 .ident = "AOpen i915GMm-HFS",
1510 .matches = {
1511 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1512 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1513 },
1514 },
1515 {
1516 .callback = thermal_psv,
1517 .ident = "AOpen i915GMm-HFS",
1518 .matches = {
1519 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1520 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1521 },
1522 },
1523 {
1524 .callback = thermal_tzp,
1525 .ident = "AOpen i915GMm-HFS",
1526 .matches = {
1527 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1528 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1529 },
1530 },
Len Brown8c99fdc2007-08-20 18:46:50 -04001531 {
1532 .callback = thermal_nocrt,
1533 .ident = "Gigabyte GA-7ZX",
1534 .matches = {
1535 DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
1536 DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
1537 },
1538 },
Len Brown0b5bfa12007-08-12 00:13:02 -04001539 {}
1540};
Len Brown0b5bfa12007-08-12 00:13:02 -04001541
Len Brown4be44fc2005-08-05 00:44:28 -04001542static int __init acpi_thermal_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
Len Brown4be44fc2005-08-05 00:44:28 -04001544 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Len Brown0b5bfa12007-08-12 00:13:02 -04001546 dmi_check_system(thermal_dmi_table);
1547
Len Brown72b33ef2007-08-12 00:12:17 -04001548 if (off) {
1549 printk(KERN_NOTICE "ACPI: thermal control disabled\n");
1550 return -ENODEV;
1551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
1553 if (!acpi_thermal_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001554 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
1556 result = acpi_bus_register_driver(&acpi_thermal_driver);
1557 if (result < 0) {
1558 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04001559 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 }
1561
Patrick Mocheld550d982006-06-27 00:41:40 -04001562 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563}
1564
Len Brown4be44fc2005-08-05 00:44:28 -04001565static void __exit acpi_thermal_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
1568 acpi_bus_unregister_driver(&acpi_thermal_driver);
1569
1570 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1571
Patrick Mocheld550d982006-06-27 00:41:40 -04001572 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573}
1574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575module_init(acpi_thermal_init);
1576module_exit(acpi_thermal_exit);