blob: 9073ada888354ac7cff6f0b8d38803495b5f763d [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
371 /* Critical Shutdown (required) */
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 */
382 if (ACPI_FAILURE(status) ||
383 tz->trips.critical.temperature <= 2732) {
Len Brownc52a7412007-08-14 15:49:32 -0400384 tz->trips.critical.flags.valid = 0;
Zhang Ruice44e192008-01-17 15:51:25 +0800385 ACPI_EXCEPTION((AE_INFO, status,
Arjan van de Vena39a2d72008-05-19 15:55:15 -0700386 "No or invalid critical threshold"));
Zhang Ruice44e192008-01-17 15:51:25 +0800387 return -ENODEV;
388 } else {
389 tz->trips.critical.flags.valid = 1;
390 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
391 "Found critical threshold [%lu]\n",
392 tz->trips.critical.temperature));
393 }
394 if (tz->trips.critical.flags.valid == 1) {
395 if (crt == -1) {
396 tz->trips.critical.flags.valid = 0;
397 } else if (crt > 0) {
398 unsigned long crt_k = CELSIUS_TO_KELVIN(crt);
399 /*
Zhang Rui22a94d72008-10-17 02:41:20 -0400400 * Allow override critical threshold
Zhang Ruice44e192008-01-17 15:51:25 +0800401 */
Zhang Rui22a94d72008-10-17 02:41:20 -0400402 if (crt_k > tz->trips.critical.temperature)
403 printk(KERN_WARNING PREFIX
404 "Critical threshold %d C\n", crt);
405 tz->trips.critical.temperature = crt_k;
Zhang Ruice44e192008-01-17 15:51:25 +0800406 }
Len Brownc52a7412007-08-14 15:49:32 -0400407 }
408 }
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 /* Critical Sleep (optional) */
Zhang Ruice44e192008-01-17 15:51:25 +0800411 if (flag & ACPI_TRIPS_HOT) {
Len Browna70cdc52007-08-12 00:12:35 -0400412 status = acpi_evaluate_integer(tz->device->handle,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400413 "_HOT", NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800414 if (ACPI_FAILURE(status)) {
415 tz->trips.hot.flags.valid = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400416 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Zhang Ruice44e192008-01-17 15:51:25 +0800417 "No hot threshold\n"));
418 } else {
Matthew Wilcox27663c52008-10-10 02:22:59 -0400419 tz->trips.hot.temperature = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800420 tz->trips.hot.flags.valid = 1;
421 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
422 "Found hot threshold [%lu]\n",
423 tz->trips.critical.temperature));
424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
426
Zhang Ruice44e192008-01-17 15:51:25 +0800427 /* Passive (optional) */
Zhang Rui0e4240d2009-01-16 12:53:42 -0500428 if (((flag & ACPI_TRIPS_PASSIVE) && tz->trips.passive.flags.valid) ||
429 (flag == ACPI_TRIPS_INIT)) {
Zhang Ruice44e192008-01-17 15:51:25 +0800430 valid = tz->trips.passive.flags.valid;
431 if (psv == -1) {
432 status = AE_SUPPORT;
433 } else if (psv > 0) {
Matthew Wilcox27663c52008-10-10 02:22:59 -0400434 tmp = CELSIUS_TO_KELVIN(psv);
Zhang Ruice44e192008-01-17 15:51:25 +0800435 status = AE_OK;
436 } else {
437 status = acpi_evaluate_integer(tz->device->handle,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400438 "_PSV", NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Zhang Ruice44e192008-01-17 15:51:25 +0800441 if (ACPI_FAILURE(status))
442 tz->trips.passive.flags.valid = 0;
443 else {
Matthew Wilcox27663c52008-10-10 02:22:59 -0400444 tz->trips.passive.temperature = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800445 tz->trips.passive.flags.valid = 1;
446 if (flag == ACPI_TRIPS_INIT) {
447 status = acpi_evaluate_integer(
448 tz->device->handle, "_TC1",
Matthew Wilcox27663c52008-10-10 02:22:59 -0400449 NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800450 if (ACPI_FAILURE(status))
451 tz->trips.passive.flags.valid = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400452 else
453 tz->trips.passive.tc1 = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800454 status = acpi_evaluate_integer(
455 tz->device->handle, "_TC2",
Matthew Wilcox27663c52008-10-10 02:22:59 -0400456 NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800457 if (ACPI_FAILURE(status))
458 tz->trips.passive.flags.valid = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400459 else
460 tz->trips.passive.tc2 = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800461 status = acpi_evaluate_integer(
462 tz->device->handle, "_TSP",
Matthew Wilcox27663c52008-10-10 02:22:59 -0400463 NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800464 if (ACPI_FAILURE(status))
465 tz->trips.passive.flags.valid = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400466 else
467 tz->trips.passive.tsp = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800468 }
469 }
470 }
471 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.passive.flags.valid) {
472 memset(&devices, 0, sizeof(struct acpi_handle_list));
473 status = acpi_evaluate_reference(tz->device->handle, "_PSL",
474 NULL, &devices);
Zhang Rui0e4240d2009-01-16 12:53:42 -0500475 if (ACPI_FAILURE(status)) {
476 printk(KERN_WARNING PREFIX
477 "Invalid passive threshold\n");
Zhang Ruice44e192008-01-17 15:51:25 +0800478 tz->trips.passive.flags.valid = 0;
Zhang Rui0e4240d2009-01-16 12:53:42 -0500479 }
Zhang Ruice44e192008-01-17 15:51:25 +0800480 else
481 tz->trips.passive.flags.valid = 1;
482
483 if (memcmp(&tz->trips.passive.devices, &devices,
484 sizeof(struct acpi_handle_list))) {
485 memcpy(&tz->trips.passive.devices, &devices,
486 sizeof(struct acpi_handle_list));
487 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
488 }
489 }
490 if ((flag & ACPI_TRIPS_PASSIVE) || (flag & ACPI_TRIPS_DEVICES)) {
491 if (valid != tz->trips.passive.flags.valid)
492 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
493 }
494
495 /* Active (optional) */
Len Brown4be44fc2005-08-05 00:44:28 -0400496 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Len Brown4be44fc2005-08-05 00:44:28 -0400497 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
Zhang Ruice44e192008-01-17 15:51:25 +0800498 valid = tz->trips.active[i].flags.valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Len Brownf8707ec2007-08-12 00:12:54 -0400500 if (act == -1)
Zhang Ruice44e192008-01-17 15:51:25 +0800501 break; /* disable all active trip points */
Len Brownf8707ec2007-08-12 00:12:54 -0400502
Zhang Rui0e4240d2009-01-16 12:53:42 -0500503 if ((flag == ACPI_TRIPS_INIT) || ((flag & ACPI_TRIPS_ACTIVE) &&
504 tz->trips.active[i].flags.valid)) {
Zhang Ruice44e192008-01-17 15:51:25 +0800505 status = acpi_evaluate_integer(tz->device->handle,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400506 name, NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800507 if (ACPI_FAILURE(status)) {
508 tz->trips.active[i].flags.valid = 0;
509 if (i == 0)
510 break;
511 if (act <= 0)
512 break;
513 if (i == 1)
514 tz->trips.active[0].temperature =
515 CELSIUS_TO_KELVIN(act);
516 else
517 /*
518 * Don't allow override higher than
519 * the next higher trip point
520 */
521 tz->trips.active[i - 1].temperature =
522 (tz->trips.active[i - 2].temperature <
523 CELSIUS_TO_KELVIN(act) ?
524 tz->trips.active[i - 2].temperature :
525 CELSIUS_TO_KELVIN(act));
Len Brownf8707ec2007-08-12 00:12:54 -0400526 break;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400527 } else {
528 tz->trips.active[i].temperature = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800529 tz->trips.active[i].flags.valid = 1;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400530 }
Len Brownf8707ec2007-08-12 00:12:54 -0400531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 name[2] = 'L';
Zhang Ruice44e192008-01-17 15:51:25 +0800534 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.active[i].flags.valid ) {
535 memset(&devices, 0, sizeof(struct acpi_handle_list));
536 status = acpi_evaluate_reference(tz->device->handle,
537 name, NULL, &devices);
Zhang Rui0e4240d2009-01-16 12:53:42 -0500538 if (ACPI_FAILURE(status)) {
539 printk(KERN_WARNING PREFIX
540 "Invalid active%d threshold\n", i);
Zhang Ruice44e192008-01-17 15:51:25 +0800541 tz->trips.active[i].flags.valid = 0;
Zhang Rui0e4240d2009-01-16 12:53:42 -0500542 }
Zhang Ruice44e192008-01-17 15:51:25 +0800543 else
544 tz->trips.active[i].flags.valid = 1;
545
546 if (memcmp(&tz->trips.active[i].devices, &devices,
547 sizeof(struct acpi_handle_list))) {
548 memcpy(&tz->trips.active[i].devices, &devices,
549 sizeof(struct acpi_handle_list));
550 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
551 }
552 }
553 if ((flag & ACPI_TRIPS_ACTIVE) || (flag & ACPI_TRIPS_DEVICES))
554 if (valid != tz->trips.active[i].flags.valid)
555 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
556
557 if (!tz->trips.active[i].flags.valid)
558 break;
559 }
560
561 if (flag & ACPI_TRIPS_DEVICES) {
562 memset(&devices, 0, sizeof(struct acpi_handle_list));
563 status = acpi_evaluate_reference(tz->device->handle, "_TZD",
564 NULL, &devices);
565 if (memcmp(&tz->devices, &devices,
566 sizeof(struct acpi_handle_list))) {
567 memcpy(&tz->devices, &devices,
568 sizeof(struct acpi_handle_list));
569 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
572
Patrick Mocheld550d982006-06-27 00:41:40 -0400573 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
Zhang Ruice44e192008-01-17 15:51:25 +0800576static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
Zhang Ruice44e192008-01-17 15:51:25 +0800578 return acpi_thermal_trips_update(tz, ACPI_TRIPS_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
Len Brown4be44fc2005-08-05 00:44:28 -0400581static void acpi_thermal_check(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200583 struct acpi_thermal *tz = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Matthew Garrettb1569e92008-12-03 17:55:32 +0000585 thermal_zone_device_update(tz->thermal_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
Zhang Rui3f655ef2008-01-17 15:51:11 +0800588/* sys I/F for generic thermal sysfs support */
Jean Delvare13614e32009-04-06 16:01:46 +0200589#define KELVIN_TO_MILLICELSIUS(t, off) (((t) - (off)) * 100)
Zhang, Rui5e012762008-02-28 07:51:30 +0800590
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000591static int thermal_get_temp(struct thermal_zone_device *thermal,
592 unsigned long *temp)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800593{
594 struct acpi_thermal *tz = thermal->devdata;
Zhang, Rui76ecb4f2008-04-10 16:20:23 +0800595 int result;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800596
597 if (!tz)
598 return -EINVAL;
599
Zhang, Rui76ecb4f2008-04-10 16:20:23 +0800600 result = acpi_thermal_get_temperature(tz);
601 if (result)
602 return result;
603
Jean Delvare13614e32009-04-06 16:01:46 +0200604 *temp = KELVIN_TO_MILLICELSIUS(tz->temperature, tz->kelvin_offset);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000605 return 0;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800606}
607
608static const char enabled[] = "kernel";
609static const char disabled[] = "user";
610static int thermal_get_mode(struct thermal_zone_device *thermal,
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000611 enum thermal_device_mode *mode)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800612{
613 struct acpi_thermal *tz = thermal->devdata;
614
615 if (!tz)
616 return -EINVAL;
617
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000618 *mode = tz->tz_enabled ? THERMAL_DEVICE_ENABLED :
619 THERMAL_DEVICE_DISABLED;
620
621 return 0;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800622}
623
624static int thermal_set_mode(struct thermal_zone_device *thermal,
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000625 enum thermal_device_mode mode)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800626{
627 struct acpi_thermal *tz = thermal->devdata;
628 int enable;
629
630 if (!tz)
631 return -EINVAL;
632
633 /*
634 * enable/disable thermal management from ACPI thermal driver
635 */
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000636 if (mode == THERMAL_DEVICE_ENABLED)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800637 enable = 1;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000638 else if (mode == THERMAL_DEVICE_DISABLED)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800639 enable = 0;
640 else
641 return -EINVAL;
642
643 if (enable != tz->tz_enabled) {
644 tz->tz_enabled = enable;
645 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
646 "%s ACPI thermal control\n",
647 tz->tz_enabled ? enabled : disabled));
648 acpi_thermal_check(tz);
649 }
650 return 0;
651}
652
653static int thermal_get_trip_type(struct thermal_zone_device *thermal,
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000654 int trip, enum thermal_trip_type *type)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800655{
656 struct acpi_thermal *tz = thermal->devdata;
657 int i;
658
659 if (!tz || trip < 0)
660 return -EINVAL;
661
662 if (tz->trips.critical.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000663 if (!trip) {
664 *type = THERMAL_TRIP_CRITICAL;
665 return 0;
666 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800667 trip--;
668 }
669
670 if (tz->trips.hot.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000671 if (!trip) {
672 *type = THERMAL_TRIP_HOT;
673 return 0;
674 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800675 trip--;
676 }
677
678 if (tz->trips.passive.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000679 if (!trip) {
680 *type = THERMAL_TRIP_PASSIVE;
681 return 0;
682 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800683 trip--;
684 }
685
686 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
687 tz->trips.active[i].flags.valid; i++) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000688 if (!trip) {
689 *type = THERMAL_TRIP_ACTIVE;
690 return 0;
691 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800692 trip--;
693 }
694
695 return -EINVAL;
696}
697
698static int thermal_get_trip_temp(struct thermal_zone_device *thermal,
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000699 int trip, unsigned long *temp)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800700{
701 struct acpi_thermal *tz = thermal->devdata;
702 int i;
703
704 if (!tz || trip < 0)
705 return -EINVAL;
706
707 if (tz->trips.critical.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000708 if (!trip) {
709 *temp = KELVIN_TO_MILLICELSIUS(
Jean Delvare13614e32009-04-06 16:01:46 +0200710 tz->trips.critical.temperature,
711 tz->kelvin_offset);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000712 return 0;
713 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800714 trip--;
715 }
716
717 if (tz->trips.hot.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000718 if (!trip) {
719 *temp = KELVIN_TO_MILLICELSIUS(
Jean Delvare13614e32009-04-06 16:01:46 +0200720 tz->trips.hot.temperature,
721 tz->kelvin_offset);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000722 return 0;
723 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800724 trip--;
725 }
726
727 if (tz->trips.passive.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000728 if (!trip) {
729 *temp = KELVIN_TO_MILLICELSIUS(
Jean Delvare13614e32009-04-06 16:01:46 +0200730 tz->trips.passive.temperature,
731 tz->kelvin_offset);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000732 return 0;
733 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800734 trip--;
735 }
736
737 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
738 tz->trips.active[i].flags.valid; i++) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000739 if (!trip) {
740 *temp = KELVIN_TO_MILLICELSIUS(
Jean Delvare13614e32009-04-06 16:01:46 +0200741 tz->trips.active[i].temperature,
742 tz->kelvin_offset);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000743 return 0;
744 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800745 trip--;
746 }
747
748 return -EINVAL;
749}
750
Zhang, Rui9ec732f2008-04-10 16:13:10 +0800751static int thermal_get_crit_temp(struct thermal_zone_device *thermal,
752 unsigned long *temperature) {
753 struct acpi_thermal *tz = thermal->devdata;
754
755 if (tz->trips.critical.flags.valid) {
756 *temperature = KELVIN_TO_MILLICELSIUS(
Jean Delvare13614e32009-04-06 16:01:46 +0200757 tz->trips.critical.temperature,
758 tz->kelvin_offset);
Zhang, Rui9ec732f2008-04-10 16:13:10 +0800759 return 0;
760 } else
761 return -EINVAL;
762}
763
Matthew Garrettb1569e92008-12-03 17:55:32 +0000764static int thermal_notify(struct thermal_zone_device *thermal, int trip,
765 enum thermal_trip_type trip_type)
766{
767 u8 type = 0;
768 struct acpi_thermal *tz = thermal->devdata;
769
770 if (trip_type == THERMAL_TRIP_CRITICAL)
771 type = ACPI_THERMAL_NOTIFY_CRITICAL;
772 else if (trip_type == THERMAL_TRIP_HOT)
773 type = ACPI_THERMAL_NOTIFY_HOT;
774 else
775 return 0;
776
777 acpi_bus_generate_proc_event(tz->device, type, 1);
778 acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
Stephen Rothwellf6f5c452009-03-03 12:47:17 +1100779 dev_name(&tz->device->dev), type, 1);
Matthew Garrettb1569e92008-12-03 17:55:32 +0000780
781 if (trip_type == THERMAL_TRIP_CRITICAL && nocrt)
782 return 1;
783
784 return 0;
785}
786
Zhang Rui3f655ef2008-01-17 15:51:11 +0800787typedef int (*cb)(struct thermal_zone_device *, int,
788 struct thermal_cooling_device *);
789static int acpi_thermal_cooling_device_cb(struct thermal_zone_device *thermal,
790 struct thermal_cooling_device *cdev,
791 cb action)
792{
793 struct acpi_device *device = cdev->devdata;
794 struct acpi_thermal *tz = thermal->devdata;
Zhang Rui653a00c2008-01-17 15:51:18 +0800795 struct acpi_device *dev;
796 acpi_status status;
797 acpi_handle handle;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800798 int i;
799 int j;
800 int trip = -1;
801 int result = 0;
802
803 if (tz->trips.critical.flags.valid)
804 trip++;
805
806 if (tz->trips.hot.flags.valid)
807 trip++;
808
809 if (tz->trips.passive.flags.valid) {
810 trip++;
811 for (i = 0; i < tz->trips.passive.devices.count;
812 i++) {
Zhang Rui653a00c2008-01-17 15:51:18 +0800813 handle = tz->trips.passive.devices.handles[i];
814 status = acpi_bus_get_device(handle, &dev);
815 if (ACPI_SUCCESS(status) && (dev == device)) {
816 result = action(thermal, trip, cdev);
817 if (result)
818 goto failed;
819 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800820 }
821 }
822
823 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
824 if (!tz->trips.active[i].flags.valid)
825 break;
826 trip++;
827 for (j = 0;
828 j < tz->trips.active[i].devices.count;
829 j++) {
Zhang Rui653a00c2008-01-17 15:51:18 +0800830 handle = tz->trips.active[i].devices.handles[j];
831 status = acpi_bus_get_device(handle, &dev);
832 if (ACPI_SUCCESS(status) && (dev == device)) {
833 result = action(thermal, trip, cdev);
834 if (result)
835 goto failed;
836 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800837 }
838 }
839
840 for (i = 0; i < tz->devices.count; i++) {
Zhang Rui653a00c2008-01-17 15:51:18 +0800841 handle = tz->devices.handles[i];
842 status = acpi_bus_get_device(handle, &dev);
843 if (ACPI_SUCCESS(status) && (dev == device)) {
844 result = action(thermal, -1, cdev);
845 if (result)
846 goto failed;
847 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800848 }
849
850failed:
851 return result;
852}
853
854static int
855acpi_thermal_bind_cooling_device(struct thermal_zone_device *thermal,
856 struct thermal_cooling_device *cdev)
857{
858 return acpi_thermal_cooling_device_cb(thermal, cdev,
859 thermal_zone_bind_cooling_device);
860}
861
862static int
863acpi_thermal_unbind_cooling_device(struct thermal_zone_device *thermal,
864 struct thermal_cooling_device *cdev)
865{
866 return acpi_thermal_cooling_device_cb(thermal, cdev,
867 thermal_zone_unbind_cooling_device);
868}
869
870static struct thermal_zone_device_ops acpi_thermal_zone_ops = {
871 .bind = acpi_thermal_bind_cooling_device,
872 .unbind = acpi_thermal_unbind_cooling_device,
873 .get_temp = thermal_get_temp,
874 .get_mode = thermal_get_mode,
875 .set_mode = thermal_set_mode,
876 .get_trip_type = thermal_get_trip_type,
877 .get_trip_temp = thermal_get_trip_temp,
Zhang, Rui9ec732f2008-04-10 16:13:10 +0800878 .get_crit_temp = thermal_get_crit_temp,
Matthew Garrettb1569e92008-12-03 17:55:32 +0000879 .notify = thermal_notify,
Zhang Rui3f655ef2008-01-17 15:51:11 +0800880};
881
882static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz)
883{
884 int trips = 0;
885 int result;
Zhang Rui20733932008-01-17 15:51:21 +0800886 acpi_status status;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800887 int i;
888
889 if (tz->trips.critical.flags.valid)
890 trips++;
891
892 if (tz->trips.hot.flags.valid)
893 trips++;
894
895 if (tz->trips.passive.flags.valid)
896 trips++;
897
898 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
899 tz->trips.active[i].flags.valid; i++, trips++);
Matthew Garrettb1569e92008-12-03 17:55:32 +0000900
901 if (tz->trips.passive.flags.valid)
902 tz->thermal_zone =
903 thermal_zone_device_register("acpitz", trips, tz,
904 &acpi_thermal_zone_ops,
905 tz->trips.passive.tc1,
906 tz->trips.passive.tc2,
907 tz->trips.passive.tsp*100,
908 tz->polling_frequency*100);
909 else
910 tz->thermal_zone =
911 thermal_zone_device_register("acpitz", trips, tz,
912 &acpi_thermal_zone_ops,
913 0, 0, 0,
Matthew Garrett67405432009-04-14 20:16:45 +0100914 tz->polling_frequency*100);
Krzysztof Heltbb070e42008-04-08 17:41:52 -0700915 if (IS_ERR(tz->thermal_zone))
Zhang Rui3f655ef2008-01-17 15:51:11 +0800916 return -ENODEV;
917
918 result = sysfs_create_link(&tz->device->dev.kobj,
919 &tz->thermal_zone->device.kobj, "thermal_zone");
920 if (result)
921 return result;
922
923 result = sysfs_create_link(&tz->thermal_zone->device.kobj,
924 &tz->device->dev.kobj, "device");
925 if (result)
926 return result;
927
Zhang Rui20733932008-01-17 15:51:21 +0800928 status = acpi_attach_data(tz->device->handle,
929 acpi_bus_private_data_handler,
930 tz->thermal_zone);
931 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800932 printk(KERN_ERR PREFIX
933 "Error attaching device data\n");
Zhang Rui20733932008-01-17 15:51:21 +0800934 return -ENODEV;
935 }
936
Zhang Rui3f655ef2008-01-17 15:51:11 +0800937 tz->tz_enabled = 1;
938
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200939 dev_info(&tz->device->dev, "registered as thermal_zone%d\n",
940 tz->thermal_zone->id);
Zhang Rui3f655ef2008-01-17 15:51:11 +0800941 return 0;
942}
943
944static void acpi_thermal_unregister_thermal_zone(struct acpi_thermal *tz)
945{
946 sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
947 sysfs_remove_link(&tz->thermal_zone->device.kobj, "device");
948 thermal_zone_device_unregister(tz->thermal_zone);
949 tz->thermal_zone = NULL;
Zhang Rui20733932008-01-17 15:51:21 +0800950 acpi_detach_data(tz->device->handle, acpi_bus_private_data_handler);
Zhang Rui3f655ef2008-01-17 15:51:11 +0800951}
952
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954/* --------------------------------------------------------------------------
955 FS Interface (/proc)
956 -------------------------------------------------------------------------- */
957
Len Brown4be44fc2005-08-05 00:44:28 -0400958static struct proc_dir_entry *acpi_thermal_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
961{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200962 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 if (!tz)
966 goto end;
967
968 seq_puts(seq, "state: ");
969
Len Brown4be44fc2005-08-05 00:44:28 -0400970 if (!tz->state.critical && !tz->state.hot && !tz->state.passive
971 && !tz->state.active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 seq_puts(seq, "ok\n");
973 else {
974 if (tz->state.critical)
975 seq_puts(seq, "critical ");
976 if (tz->state.hot)
977 seq_puts(seq, "hot ");
978 if (tz->state.passive)
979 seq_puts(seq, "passive ");
980 if (tz->state.active)
981 seq_printf(seq, "active[%d]", tz->state.active_index);
982 seq_puts(seq, "\n");
983 }
984
Len Brown4be44fc2005-08-05 00:44:28 -0400985 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400986 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
988
989static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
990{
991 return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
992}
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
995{
Len Brown4be44fc2005-08-05 00:44:28 -0400996 int result = 0;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200997 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000 if (!tz)
1001 goto end;
1002
1003 result = acpi_thermal_get_temperature(tz);
1004 if (result)
1005 goto end;
1006
Len Brown4be44fc2005-08-05 00:44:28 -04001007 seq_printf(seq, "temperature: %ld C\n",
1008 KELVIN_TO_CELSIUS(tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Len Brown4be44fc2005-08-05 00:44:28 -04001010 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001011 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012}
1013
1014static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
1015{
1016 return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
1017}
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
1020{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001021 struct acpi_thermal *tz = seq->private;
Thomas Renninger68ccfaa2006-11-19 23:01:40 +01001022 struct acpi_device *device;
Thomas Renningere7c746e2007-06-18 00:40:51 -04001023 acpi_status status;
1024
Len Brown4be44fc2005-08-05 00:44:28 -04001025 int i = 0;
1026 int j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 if (!tz)
1030 goto end;
1031
1032 if (tz->trips.critical.flags.valid)
Len Brownf5487142007-08-12 00:12:44 -04001033 seq_printf(seq, "critical (S5): %ld C%s",
1034 KELVIN_TO_CELSIUS(tz->trips.critical.temperature),
1035 nocrt ? " <disabled>\n" : "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037 if (tz->trips.hot.flags.valid)
Len Brownf5487142007-08-12 00:12:44 -04001038 seq_printf(seq, "hot (S4): %ld C%s",
1039 KELVIN_TO_CELSIUS(tz->trips.hot.temperature),
1040 nocrt ? " <disabled>\n" : "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 if (tz->trips.passive.flags.valid) {
Len Brown4be44fc2005-08-05 00:44:28 -04001043 seq_printf(seq,
1044 "passive: %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
1045 KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
1046 tz->trips.passive.tc1, tz->trips.passive.tc2,
1047 tz->trips.passive.tsp);
1048 for (j = 0; j < tz->trips.passive.devices.count; j++) {
Thomas Renningere7c746e2007-06-18 00:40:51 -04001049 status = acpi_bus_get_device(tz->trips.passive.devices.
1050 handles[j], &device);
1051 seq_printf(seq, "%4.4s ", status ? "" :
1052 acpi_device_bid(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 }
1054 seq_puts(seq, "\n");
Frans Pop7fb26162009-10-26 08:39:01 +01001055 } else {
1056 seq_printf(seq, "passive (forced):");
1057 if (tz->thermal_zone->forced_passive)
1058 seq_printf(seq, " %i C\n",
1059 tz->thermal_zone->forced_passive / 1000);
1060 else
1061 seq_printf(seq, "<not set>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 }
1063
1064 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1065 if (!(tz->trips.active[i].flags.valid))
1066 break;
1067 seq_printf(seq, "active[%d]: %ld C: devices=",
Len Brown4be44fc2005-08-05 00:44:28 -04001068 i,
1069 KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
Thomas Renninger68ccfaa2006-11-19 23:01:40 +01001070 for (j = 0; j < tz->trips.active[i].devices.count; j++){
Thomas Renningere7c746e2007-06-18 00:40:51 -04001071 status = acpi_bus_get_device(tz->trips.active[i].
1072 devices.handles[j],
1073 &device);
1074 seq_printf(seq, "%4.4s ", status ? "" :
1075 acpi_device_bid(device));
Thomas Renninger68ccfaa2006-11-19 23:01:40 +01001076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 seq_puts(seq, "\n");
1078 }
1079
Len Brown4be44fc2005-08-05 00:44:28 -04001080 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001081 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082}
1083
1084static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
1085{
1086 return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
1087}
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
1090{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001091 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 if (!tz)
1095 goto end;
1096
Len Browneaca2d32007-04-30 23:27:43 -04001097 if (!tz->flags.cooling_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 seq_puts(seq, "<setting not supported>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 else
Len Browneaca2d32007-04-30 23:27:43 -04001100 seq_puts(seq, "0 - Active; 1 - Passive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Len Brown4be44fc2005-08-05 00:44:28 -04001102 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001103 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104}
1105
1106static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
1107{
1108 return single_open(file, acpi_thermal_cooling_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -04001109 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110}
1111
1112static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001113acpi_thermal_write_cooling_mode(struct file *file,
1114 const char __user * buffer,
1115 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001117 struct seq_file *m = file->private_data;
1118 struct acpi_thermal *tz = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001119 int result = 0;
1120 char mode_string[12] = { '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 if (!tz || (count > sizeof(mode_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -04001124 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126 if (!tz->flags.cooling_mode)
Patrick Mocheld550d982006-06-27 00:41:40 -04001127 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129 if (copy_from_user(mode_string, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001130 return -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -04001131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 mode_string[count] = '\0';
Len Brown4be44fc2005-08-05 00:44:28 -04001133
1134 result = acpi_thermal_set_cooling_mode(tz,
1135 simple_strtoul(mode_string, NULL,
1136 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001138 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140 acpi_thermal_check(tz);
1141
Patrick Mocheld550d982006-06-27 00:41:40 -04001142 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143}
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
1146{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001147 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150 if (!tz)
1151 goto end;
1152
Matthew Garrettb1569e92008-12-03 17:55:32 +00001153 if (!tz->thermal_zone->polling_delay) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 seq_puts(seq, "<polling disabled>\n");
1155 goto end;
1156 }
1157
Matthew Garrettb1569e92008-12-03 17:55:32 +00001158 seq_printf(seq, "polling frequency: %d seconds\n",
1159 (tz->thermal_zone->polling_delay / 1000));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Len Brown4be44fc2005-08-05 00:44:28 -04001161 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001162 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163}
1164
1165static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
1166{
1167 return single_open(file, acpi_thermal_polling_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -04001168 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169}
1170
1171static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001172acpi_thermal_write_polling(struct file *file,
1173 const char __user * buffer,
1174 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001176 struct seq_file *m = file->private_data;
1177 struct acpi_thermal *tz = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001178 int result = 0;
1179 char polling_string[12] = { '\0' };
1180 int seconds = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183 if (!tz || (count > sizeof(polling_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -04001184 return -EINVAL;
Len Brown4be44fc2005-08-05 00:44:28 -04001185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 if (copy_from_user(polling_string, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001187 return -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -04001188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 polling_string[count] = '\0';
1190
1191 seconds = simple_strtoul(polling_string, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 result = acpi_thermal_set_polling(tz, seconds);
1194 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001195 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 acpi_thermal_check(tz);
1198
Patrick Mocheld550d982006-06-27 00:41:40 -04001199 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200}
1201
Len Brown4be44fc2005-08-05 00:44:28 -04001202static int acpi_thermal_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
Len Brown4be44fc2005-08-05 00:44:28 -04001204 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 if (!acpi_device_dir(device)) {
1208 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -04001209 acpi_thermal_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001211 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 }
1213
1214 /* 'state' [R] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001215 entry = proc_create_data(ACPI_THERMAL_FILE_STATE,
1216 S_IRUGO, acpi_device_dir(device),
1217 &acpi_thermal_state_fops,
1218 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001220 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
1222 /* 'temperature' [R] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001223 entry = proc_create_data(ACPI_THERMAL_FILE_TEMPERATURE,
1224 S_IRUGO, acpi_device_dir(device),
1225 &acpi_thermal_temp_fops,
1226 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001228 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Pavel Machek2db9ccb2007-08-24 11:45:50 +02001230 /* 'trip_points' [R] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001231 entry = proc_create_data(ACPI_THERMAL_FILE_TRIP_POINTS,
1232 S_IRUGO,
1233 acpi_device_dir(device),
1234 &acpi_thermal_trip_fops,
1235 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001237 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
1239 /* 'cooling_mode' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001240 entry = proc_create_data(ACPI_THERMAL_FILE_COOLING_MODE,
1241 S_IFREG | S_IRUGO | S_IWUSR,
1242 acpi_device_dir(device),
1243 &acpi_thermal_cooling_fops,
1244 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001246 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 /* 'polling_frequency' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001249 entry = proc_create_data(ACPI_THERMAL_FILE_POLLING_FREQ,
1250 S_IFREG | S_IRUGO | S_IWUSR,
1251 acpi_device_dir(device),
1252 &acpi_thermal_polling_fops,
1253 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001255 return -ENODEV;
Patrick Mocheld550d982006-06-27 00:41:40 -04001256 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257}
1258
Len Brown4be44fc2005-08-05 00:44:28 -04001259static int acpi_thermal_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262 if (acpi_device_dir(device)) {
1263 remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1264 acpi_device_dir(device));
1265 remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1266 acpi_device_dir(device));
1267 remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1268 acpi_device_dir(device));
1269 remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1270 acpi_device_dir(device));
1271 remove_proc_entry(ACPI_THERMAL_FILE_STATE,
1272 acpi_device_dir(device));
1273 remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
1274 acpi_device_dir(device) = NULL;
1275 }
1276
Patrick Mocheld550d982006-06-27 00:41:40 -04001277 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280/* --------------------------------------------------------------------------
1281 Driver Interface
1282 -------------------------------------------------------------------------- */
1283
Bjorn Helgaas342d5502009-04-07 15:37:06 +00001284static void acpi_thermal_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
Bjorn Helgaas342d5502009-04-07 15:37:06 +00001286 struct acpi_thermal *tz = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001290 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 switch (event) {
1293 case ACPI_THERMAL_NOTIFY_TEMPERATURE:
1294 acpi_thermal_check(tz);
1295 break;
1296 case ACPI_THERMAL_NOTIFY_THRESHOLDS:
Zhang Ruice44e192008-01-17 15:51:25 +08001297 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_THRESHOLDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 acpi_thermal_check(tz);
Len Brown14e04fb2007-08-23 15:20:26 -04001299 acpi_bus_generate_proc_event(device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08001300 acpi_bus_generate_netlink_event(device->pnp.device_class,
Kay Sievers07944692008-10-30 01:18:59 +01001301 dev_name(&device->dev), event, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 break;
1303 case ACPI_THERMAL_NOTIFY_DEVICES:
Zhang Ruice44e192008-01-17 15:51:25 +08001304 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_DEVICES);
1305 acpi_thermal_check(tz);
Len Brown14e04fb2007-08-23 15:20:26 -04001306 acpi_bus_generate_proc_event(device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08001307 acpi_bus_generate_netlink_event(device->pnp.device_class,
Kay Sievers07944692008-10-30 01:18:59 +01001308 dev_name(&device->dev), event, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 break;
1310 default:
1311 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001312 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 break;
1314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315}
1316
Len Brown4be44fc2005-08-05 00:44:28 -04001317static int acpi_thermal_get_info(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
Len Brown4be44fc2005-08-05 00:44:28 -04001319 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001323 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 /* Get temperature [_TMP] (required) */
1326 result = acpi_thermal_get_temperature(tz);
1327 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001328 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 /* Get trip points [_CRT, _PSV, etc.] (required) */
1331 result = acpi_thermal_get_trip_points(tz);
1332 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001333 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 /* Set the cooling mode [_SCP] to active cooling (default) */
1336 result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
Len Brown4be44fc2005-08-05 00:44:28 -04001337 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 tz->flags.cooling_mode = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340 /* Get default polling frequency [_TZP] (optional) */
1341 if (tzp)
1342 tz->polling_frequency = tzp;
1343 else
1344 acpi_thermal_get_polling_frequency(tz);
1345
Patrick Mocheld550d982006-06-27 00:41:40 -04001346 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347}
1348
Jean Delvare13614e32009-04-06 16:01:46 +02001349/*
1350 * The exact offset between Kelvin and degree Celsius is 273.15. However ACPI
1351 * handles temperature values with a single decimal place. As a consequence,
1352 * some implementations use an offset of 273.1 and others use an offset of
1353 * 273.2. Try to find out which one is being used, to present the most
1354 * accurate and visually appealing number.
1355 *
1356 * The heuristic below should work for all ACPI thermal zones which have a
1357 * critical trip point with a value being a multiple of 0.5 degree Celsius.
1358 */
1359static void acpi_thermal_guess_offset(struct acpi_thermal *tz)
1360{
1361 if (tz->trips.critical.flags.valid &&
1362 (tz->trips.critical.temperature % 5) == 1)
1363 tz->kelvin_offset = 2731;
1364 else
1365 tz->kelvin_offset = 2732;
1366}
1367
Len Brown4be44fc2005-08-05 00:44:28 -04001368static int acpi_thermal_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
Len Brown4be44fc2005-08-05 00:44:28 -04001370 int result = 0;
Len Brown4be44fc2005-08-05 00:44:28 -04001371 struct acpi_thermal *tz = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001375 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Burman Yan36bcbec2006-12-19 12:56:11 -08001377 tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001379 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Patrick Mochel8348e1b2006-05-19 16:54:40 -04001381 tz->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 strcpy(tz->name, device->pnp.bus_id);
1383 strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1384 strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001385 device->driver_data = tz;
Alexey Starikovskiy6e215782007-09-01 00:11:59 +04001386 mutex_init(&tz->lock);
Zhang Rui3f655ef2008-01-17 15:51:11 +08001387
1388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 result = acpi_thermal_get_info(tz);
1390 if (result)
Zhang Rui3f655ef2008-01-17 15:51:11 +08001391 goto free_memory;
1392
Jean Delvare13614e32009-04-06 16:01:46 +02001393 acpi_thermal_guess_offset(tz);
1394
Zhang Rui3f655ef2008-01-17 15:51:11 +08001395 result = acpi_thermal_register_thermal_zone(tz);
1396 if (result)
1397 goto free_memory;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 result = acpi_thermal_add_fs(device);
1400 if (result)
Zhang Rui3f655ef2008-01-17 15:51:11 +08001401 goto unregister_thermal_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001404 acpi_device_name(device), acpi_device_bid(device),
1405 KELVIN_TO_CELSIUS(tz->temperature));
Zhang Rui3f655ef2008-01-17 15:51:11 +08001406 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Zhang Rui3f655ef2008-01-17 15:51:11 +08001408unregister_thermal_zone:
1409 thermal_zone_device_unregister(tz->thermal_zone);
1410free_memory:
1411 kfree(tz);
1412end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001413 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414}
1415
Len Brown4be44fc2005-08-05 00:44:28 -04001416static int acpi_thermal_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
Len Brown4be44fc2005-08-05 00:44:28 -04001418 struct acpi_thermal *tz = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001421 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001423 tz = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 acpi_thermal_remove_fs(device);
Zhang Rui3f655ef2008-01-17 15:51:11 +08001426 acpi_thermal_unregister_thermal_zone(tz);
Alexey Starikovskiy6e215782007-09-01 00:11:59 +04001427 mutex_destroy(&tz->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 kfree(tz);
Patrick Mocheld550d982006-06-27 00:41:40 -04001429 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430}
1431
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001432static int acpi_thermal_resume(struct acpi_device *device)
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001433{
1434 struct acpi_thermal *tz = NULL;
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001435 int i, j, power_state, result;
1436
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001437
1438 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001439 return -EINVAL;
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001440
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001441 tz = acpi_driver_data(device);
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001442
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001443 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001444 if (!(&tz->trips.active[i]))
1445 break;
1446 if (!tz->trips.active[i].flags.valid)
1447 break;
1448 tz->trips.active[i].flags.enabled = 1;
1449 for (j = 0; j < tz->trips.active[i].devices.count; j++) {
1450 result = acpi_bus_get_power(tz->trips.active[i].devices.
1451 handles[j], &power_state);
1452 if (result || (power_state != ACPI_STATE_D0)) {
1453 tz->trips.active[i].flags.enabled = 0;
1454 break;
1455 }
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001456 }
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001457 tz->state.active |= tz->trips.active[i].flags.enabled;
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001458 }
1459
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001460 acpi_thermal_check(tz);
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001461
1462 return AE_OK;
1463}
1464
Jeff Garzik18552562007-10-03 15:15:40 -04001465static int thermal_act(const struct dmi_system_id *d) {
Len Brown0b5bfa12007-08-12 00:13:02 -04001466
1467 if (act == 0) {
1468 printk(KERN_NOTICE "ACPI: %s detected: "
1469 "disabling all active thermal trip points\n", d->ident);
1470 act = -1;
1471 }
1472 return 0;
1473}
Jeff Garzik18552562007-10-03 15:15:40 -04001474static int thermal_nocrt(const struct dmi_system_id *d) {
Len Brown8c99fdc2007-08-20 18:46:50 -04001475
1476 printk(KERN_NOTICE "ACPI: %s detected: "
1477 "disabling all critical thermal trip point actions.\n", d->ident);
1478 nocrt = 1;
1479 return 0;
1480}
Jeff Garzik18552562007-10-03 15:15:40 -04001481static int thermal_tzp(const struct dmi_system_id *d) {
Len Brown0b5bfa12007-08-12 00:13:02 -04001482
1483 if (tzp == 0) {
1484 printk(KERN_NOTICE "ACPI: %s detected: "
1485 "enabling thermal zone polling\n", d->ident);
1486 tzp = 300; /* 300 dS = 30 Seconds */
1487 }
1488 return 0;
1489}
Jeff Garzik18552562007-10-03 15:15:40 -04001490static int thermal_psv(const struct dmi_system_id *d) {
Len Brown0b5bfa12007-08-12 00:13:02 -04001491
1492 if (psv == 0) {
1493 printk(KERN_NOTICE "ACPI: %s detected: "
1494 "disabling all passive thermal trip points\n", d->ident);
1495 psv = -1;
1496 }
1497 return 0;
1498}
1499
1500static struct dmi_system_id thermal_dmi_table[] __initdata = {
1501 /*
1502 * Award BIOS on this AOpen makes thermal control almost worthless.
1503 * http://bugzilla.kernel.org/show_bug.cgi?id=8842
1504 */
1505 {
1506 .callback = thermal_act,
1507 .ident = "AOpen i915GMm-HFS",
1508 .matches = {
1509 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1510 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1511 },
1512 },
1513 {
1514 .callback = thermal_psv,
1515 .ident = "AOpen i915GMm-HFS",
1516 .matches = {
1517 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1518 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1519 },
1520 },
1521 {
1522 .callback = thermal_tzp,
1523 .ident = "AOpen i915GMm-HFS",
1524 .matches = {
1525 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1526 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1527 },
1528 },
Len Brown8c99fdc2007-08-20 18:46:50 -04001529 {
1530 .callback = thermal_nocrt,
1531 .ident = "Gigabyte GA-7ZX",
1532 .matches = {
1533 DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
1534 DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
1535 },
1536 },
Len Brown0b5bfa12007-08-12 00:13:02 -04001537 {}
1538};
Len Brown0b5bfa12007-08-12 00:13:02 -04001539
Len Brown4be44fc2005-08-05 00:44:28 -04001540static int __init acpi_thermal_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
Len Brown4be44fc2005-08-05 00:44:28 -04001542 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Len Brown0b5bfa12007-08-12 00:13:02 -04001544 dmi_check_system(thermal_dmi_table);
1545
Len Brown72b33ef2007-08-12 00:12:17 -04001546 if (off) {
1547 printk(KERN_NOTICE "ACPI: thermal control disabled\n");
1548 return -ENODEV;
1549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
1551 if (!acpi_thermal_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001552 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
1554 result = acpi_bus_register_driver(&acpi_thermal_driver);
1555 if (result < 0) {
1556 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04001557 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 }
1559
Patrick Mocheld550d982006-06-27 00:41:40 -04001560 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561}
1562
Len Brown4be44fc2005-08-05 00:44:28 -04001563static void __exit acpi_thermal_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566 acpi_bus_unregister_driver(&acpi_thermal_driver);
1567
1568 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1569
Patrick Mocheld550d982006-06-27 00:41:40 -04001570 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571}
1572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573module_init(acpi_thermal_init);
1574module_exit(acpi_thermal_exit);