blob: 5d3893558cf73566e128f36e6e11482c13ccbef2 [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 Brownc2b6705b2007-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 Karasyov74ce1462006-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{
Thomas Renninger8b7ef6d2010-02-16 22:55:51 +0100580 int i, valid, ret = acpi_thermal_trips_update(tz, ACPI_TRIPS_INIT);
581
582 if (ret)
583 return ret;
584
585 valid = tz->trips.critical.flags.valid |
586 tz->trips.hot.flags.valid |
587 tz->trips.passive.flags.valid;
588
589 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
590 valid |= tz->trips.active[i].flags.valid;
591
592 if (!valid) {
593 printk(KERN_WARNING FW_BUG "No valid trip found\n");
594 return -ENODEV;
595 }
596 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
Len Brown4be44fc2005-08-05 00:44:28 -0400599static void acpi_thermal_check(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200601 struct acpi_thermal *tz = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Matthew Garrettb1569e92008-12-03 17:55:32 +0000603 thermal_zone_device_update(tz->thermal_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
Zhang Rui3f655ef2008-01-17 15:51:11 +0800606/* sys I/F for generic thermal sysfs support */
Jean Delvare13614e32009-04-06 16:01:46 +0200607#define KELVIN_TO_MILLICELSIUS(t, off) (((t) - (off)) * 100)
Zhang, Rui5e012762008-02-28 07:51:30 +0800608
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000609static int thermal_get_temp(struct thermal_zone_device *thermal,
610 unsigned long *temp)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800611{
612 struct acpi_thermal *tz = thermal->devdata;
Zhang, Rui76ecb4f2008-04-10 16:20:23 +0800613 int result;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800614
615 if (!tz)
616 return -EINVAL;
617
Zhang, Rui76ecb4f2008-04-10 16:20:23 +0800618 result = acpi_thermal_get_temperature(tz);
619 if (result)
620 return result;
621
Jean Delvare13614e32009-04-06 16:01:46 +0200622 *temp = KELVIN_TO_MILLICELSIUS(tz->temperature, tz->kelvin_offset);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000623 return 0;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800624}
625
626static const char enabled[] = "kernel";
627static const char disabled[] = "user";
628static int thermal_get_mode(struct thermal_zone_device *thermal,
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000629 enum thermal_device_mode *mode)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800630{
631 struct acpi_thermal *tz = thermal->devdata;
632
633 if (!tz)
634 return -EINVAL;
635
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000636 *mode = tz->tz_enabled ? THERMAL_DEVICE_ENABLED :
637 THERMAL_DEVICE_DISABLED;
638
639 return 0;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800640}
641
642static int thermal_set_mode(struct thermal_zone_device *thermal,
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000643 enum thermal_device_mode mode)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800644{
645 struct acpi_thermal *tz = thermal->devdata;
646 int enable;
647
648 if (!tz)
649 return -EINVAL;
650
651 /*
652 * enable/disable thermal management from ACPI thermal driver
653 */
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000654 if (mode == THERMAL_DEVICE_ENABLED)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800655 enable = 1;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000656 else if (mode == THERMAL_DEVICE_DISABLED)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800657 enable = 0;
658 else
659 return -EINVAL;
660
661 if (enable != tz->tz_enabled) {
662 tz->tz_enabled = enable;
663 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
664 "%s ACPI thermal control\n",
665 tz->tz_enabled ? enabled : disabled));
666 acpi_thermal_check(tz);
667 }
668 return 0;
669}
670
671static int thermal_get_trip_type(struct thermal_zone_device *thermal,
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000672 int trip, enum thermal_trip_type *type)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800673{
674 struct acpi_thermal *tz = thermal->devdata;
675 int i;
676
677 if (!tz || trip < 0)
678 return -EINVAL;
679
680 if (tz->trips.critical.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000681 if (!trip) {
682 *type = THERMAL_TRIP_CRITICAL;
683 return 0;
684 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800685 trip--;
686 }
687
688 if (tz->trips.hot.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000689 if (!trip) {
690 *type = THERMAL_TRIP_HOT;
691 return 0;
692 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800693 trip--;
694 }
695
696 if (tz->trips.passive.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000697 if (!trip) {
698 *type = THERMAL_TRIP_PASSIVE;
699 return 0;
700 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800701 trip--;
702 }
703
704 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
705 tz->trips.active[i].flags.valid; i++) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000706 if (!trip) {
707 *type = THERMAL_TRIP_ACTIVE;
708 return 0;
709 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800710 trip--;
711 }
712
713 return -EINVAL;
714}
715
716static int thermal_get_trip_temp(struct thermal_zone_device *thermal,
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000717 int trip, unsigned long *temp)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800718{
719 struct acpi_thermal *tz = thermal->devdata;
720 int i;
721
722 if (!tz || trip < 0)
723 return -EINVAL;
724
725 if (tz->trips.critical.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000726 if (!trip) {
727 *temp = KELVIN_TO_MILLICELSIUS(
Jean Delvare13614e32009-04-06 16:01:46 +0200728 tz->trips.critical.temperature,
729 tz->kelvin_offset);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000730 return 0;
731 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800732 trip--;
733 }
734
735 if (tz->trips.hot.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000736 if (!trip) {
737 *temp = KELVIN_TO_MILLICELSIUS(
Jean Delvare13614e32009-04-06 16:01:46 +0200738 tz->trips.hot.temperature,
739 tz->kelvin_offset);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000740 return 0;
741 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800742 trip--;
743 }
744
745 if (tz->trips.passive.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000746 if (!trip) {
747 *temp = KELVIN_TO_MILLICELSIUS(
Jean Delvare13614e32009-04-06 16:01:46 +0200748 tz->trips.passive.temperature,
749 tz->kelvin_offset);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000750 return 0;
751 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800752 trip--;
753 }
754
755 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
756 tz->trips.active[i].flags.valid; i++) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000757 if (!trip) {
758 *temp = KELVIN_TO_MILLICELSIUS(
Jean Delvare13614e32009-04-06 16:01:46 +0200759 tz->trips.active[i].temperature,
760 tz->kelvin_offset);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000761 return 0;
762 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800763 trip--;
764 }
765
766 return -EINVAL;
767}
768
Zhang, Rui9ec732f2008-04-10 16:13:10 +0800769static int thermal_get_crit_temp(struct thermal_zone_device *thermal,
770 unsigned long *temperature) {
771 struct acpi_thermal *tz = thermal->devdata;
772
773 if (tz->trips.critical.flags.valid) {
774 *temperature = KELVIN_TO_MILLICELSIUS(
Jean Delvare13614e32009-04-06 16:01:46 +0200775 tz->trips.critical.temperature,
776 tz->kelvin_offset);
Zhang, Rui9ec732f2008-04-10 16:13:10 +0800777 return 0;
778 } else
779 return -EINVAL;
780}
781
Matthew Garrettb1569e92008-12-03 17:55:32 +0000782static int thermal_notify(struct thermal_zone_device *thermal, int trip,
783 enum thermal_trip_type trip_type)
784{
785 u8 type = 0;
786 struct acpi_thermal *tz = thermal->devdata;
787
788 if (trip_type == THERMAL_TRIP_CRITICAL)
789 type = ACPI_THERMAL_NOTIFY_CRITICAL;
790 else if (trip_type == THERMAL_TRIP_HOT)
791 type = ACPI_THERMAL_NOTIFY_HOT;
792 else
793 return 0;
794
795 acpi_bus_generate_proc_event(tz->device, type, 1);
796 acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
Stephen Rothwellf6f5c452009-03-03 12:47:17 +1100797 dev_name(&tz->device->dev), type, 1);
Matthew Garrettb1569e92008-12-03 17:55:32 +0000798
799 if (trip_type == THERMAL_TRIP_CRITICAL && nocrt)
800 return 1;
801
802 return 0;
803}
804
Zhang Rui3f655ef2008-01-17 15:51:11 +0800805typedef int (*cb)(struct thermal_zone_device *, int,
806 struct thermal_cooling_device *);
807static int acpi_thermal_cooling_device_cb(struct thermal_zone_device *thermal,
808 struct thermal_cooling_device *cdev,
809 cb action)
810{
811 struct acpi_device *device = cdev->devdata;
812 struct acpi_thermal *tz = thermal->devdata;
Zhang Rui653a00c2008-01-17 15:51:18 +0800813 struct acpi_device *dev;
814 acpi_status status;
815 acpi_handle handle;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800816 int i;
817 int j;
818 int trip = -1;
819 int result = 0;
820
821 if (tz->trips.critical.flags.valid)
822 trip++;
823
824 if (tz->trips.hot.flags.valid)
825 trip++;
826
827 if (tz->trips.passive.flags.valid) {
828 trip++;
829 for (i = 0; i < tz->trips.passive.devices.count;
830 i++) {
Zhang Rui653a00c2008-01-17 15:51:18 +0800831 handle = tz->trips.passive.devices.handles[i];
832 status = acpi_bus_get_device(handle, &dev);
833 if (ACPI_SUCCESS(status) && (dev == device)) {
834 result = action(thermal, trip, cdev);
835 if (result)
836 goto failed;
837 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800838 }
839 }
840
841 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
842 if (!tz->trips.active[i].flags.valid)
843 break;
844 trip++;
845 for (j = 0;
846 j < tz->trips.active[i].devices.count;
847 j++) {
Zhang Rui653a00c2008-01-17 15:51:18 +0800848 handle = tz->trips.active[i].devices.handles[j];
849 status = acpi_bus_get_device(handle, &dev);
850 if (ACPI_SUCCESS(status) && (dev == device)) {
851 result = action(thermal, trip, cdev);
852 if (result)
853 goto failed;
854 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800855 }
856 }
857
858 for (i = 0; i < tz->devices.count; i++) {
Zhang Rui653a00c2008-01-17 15:51:18 +0800859 handle = tz->devices.handles[i];
860 status = acpi_bus_get_device(handle, &dev);
861 if (ACPI_SUCCESS(status) && (dev == device)) {
862 result = action(thermal, -1, cdev);
863 if (result)
864 goto failed;
865 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800866 }
867
868failed:
869 return result;
870}
871
872static int
873acpi_thermal_bind_cooling_device(struct thermal_zone_device *thermal,
874 struct thermal_cooling_device *cdev)
875{
876 return acpi_thermal_cooling_device_cb(thermal, cdev,
877 thermal_zone_bind_cooling_device);
878}
879
880static int
881acpi_thermal_unbind_cooling_device(struct thermal_zone_device *thermal,
882 struct thermal_cooling_device *cdev)
883{
884 return acpi_thermal_cooling_device_cb(thermal, cdev,
885 thermal_zone_unbind_cooling_device);
886}
887
888static struct thermal_zone_device_ops acpi_thermal_zone_ops = {
889 .bind = acpi_thermal_bind_cooling_device,
890 .unbind = acpi_thermal_unbind_cooling_device,
891 .get_temp = thermal_get_temp,
892 .get_mode = thermal_get_mode,
893 .set_mode = thermal_set_mode,
894 .get_trip_type = thermal_get_trip_type,
895 .get_trip_temp = thermal_get_trip_temp,
Zhang, Rui9ec732f2008-04-10 16:13:10 +0800896 .get_crit_temp = thermal_get_crit_temp,
Matthew Garrettb1569e92008-12-03 17:55:32 +0000897 .notify = thermal_notify,
Zhang Rui3f655ef2008-01-17 15:51:11 +0800898};
899
900static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz)
901{
902 int trips = 0;
903 int result;
Zhang Rui20733932008-01-17 15:51:21 +0800904 acpi_status status;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800905 int i;
906
907 if (tz->trips.critical.flags.valid)
908 trips++;
909
910 if (tz->trips.hot.flags.valid)
911 trips++;
912
913 if (tz->trips.passive.flags.valid)
914 trips++;
915
916 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
917 tz->trips.active[i].flags.valid; i++, trips++);
Matthew Garrettb1569e92008-12-03 17:55:32 +0000918
919 if (tz->trips.passive.flags.valid)
920 tz->thermal_zone =
921 thermal_zone_device_register("acpitz", trips, tz,
922 &acpi_thermal_zone_ops,
923 tz->trips.passive.tc1,
924 tz->trips.passive.tc2,
925 tz->trips.passive.tsp*100,
926 tz->polling_frequency*100);
927 else
928 tz->thermal_zone =
929 thermal_zone_device_register("acpitz", trips, tz,
930 &acpi_thermal_zone_ops,
931 0, 0, 0,
Matthew Garrett67405432009-04-14 20:16:45 +0100932 tz->polling_frequency*100);
Krzysztof Heltbb070e42008-04-08 17:41:52 -0700933 if (IS_ERR(tz->thermal_zone))
Zhang Rui3f655ef2008-01-17 15:51:11 +0800934 return -ENODEV;
935
936 result = sysfs_create_link(&tz->device->dev.kobj,
937 &tz->thermal_zone->device.kobj, "thermal_zone");
938 if (result)
939 return result;
940
941 result = sysfs_create_link(&tz->thermal_zone->device.kobj,
942 &tz->device->dev.kobj, "device");
943 if (result)
944 return result;
945
Zhang Rui20733932008-01-17 15:51:21 +0800946 status = acpi_attach_data(tz->device->handle,
947 acpi_bus_private_data_handler,
948 tz->thermal_zone);
949 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800950 printk(KERN_ERR PREFIX
951 "Error attaching device data\n");
Zhang Rui20733932008-01-17 15:51:21 +0800952 return -ENODEV;
953 }
954
Zhang Rui3f655ef2008-01-17 15:51:11 +0800955 tz->tz_enabled = 1;
956
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200957 dev_info(&tz->device->dev, "registered as thermal_zone%d\n",
958 tz->thermal_zone->id);
Zhang Rui3f655ef2008-01-17 15:51:11 +0800959 return 0;
960}
961
962static void acpi_thermal_unregister_thermal_zone(struct acpi_thermal *tz)
963{
964 sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
965 sysfs_remove_link(&tz->thermal_zone->device.kobj, "device");
966 thermal_zone_device_unregister(tz->thermal_zone);
967 tz->thermal_zone = NULL;
Zhang Rui20733932008-01-17 15:51:21 +0800968 acpi_detach_data(tz->device->handle, acpi_bus_private_data_handler);
Zhang Rui3f655ef2008-01-17 15:51:11 +0800969}
970
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972/* --------------------------------------------------------------------------
973 FS Interface (/proc)
974 -------------------------------------------------------------------------- */
975
Len Brown4be44fc2005-08-05 00:44:28 -0400976static struct proc_dir_entry *acpi_thermal_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
979{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200980 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 if (!tz)
984 goto end;
985
986 seq_puts(seq, "state: ");
987
Len Brown4be44fc2005-08-05 00:44:28 -0400988 if (!tz->state.critical && !tz->state.hot && !tz->state.passive
989 && !tz->state.active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 seq_puts(seq, "ok\n");
991 else {
992 if (tz->state.critical)
993 seq_puts(seq, "critical ");
994 if (tz->state.hot)
995 seq_puts(seq, "hot ");
996 if (tz->state.passive)
997 seq_puts(seq, "passive ");
998 if (tz->state.active)
999 seq_printf(seq, "active[%d]", tz->state.active_index);
1000 seq_puts(seq, "\n");
1001 }
1002
Len Brown4be44fc2005-08-05 00:44:28 -04001003 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001004 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005}
1006
1007static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
1008{
1009 return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
1010}
1011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
1013{
Len Brown4be44fc2005-08-05 00:44:28 -04001014 int result = 0;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001015 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
1018 if (!tz)
1019 goto end;
1020
1021 result = acpi_thermal_get_temperature(tz);
1022 if (result)
1023 goto end;
1024
Len Brown4be44fc2005-08-05 00:44:28 -04001025 seq_printf(seq, "temperature: %ld C\n",
1026 KELVIN_TO_CELSIUS(tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Len Brown4be44fc2005-08-05 00:44:28 -04001028 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001029 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030}
1031
1032static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
1033{
1034 return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
1035}
1036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
1038{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001039 struct acpi_thermal *tz = seq->private;
Thomas Renninger68ccfaa2006-11-19 23:01:40 +01001040 struct acpi_device *device;
Thomas Renningere7c746e2007-06-18 00:40:51 -04001041 acpi_status status;
1042
Len Brown4be44fc2005-08-05 00:44:28 -04001043 int i = 0;
1044 int j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 if (!tz)
1048 goto end;
1049
1050 if (tz->trips.critical.flags.valid)
Len Brownf5487142007-08-12 00:12:44 -04001051 seq_printf(seq, "critical (S5): %ld C%s",
1052 KELVIN_TO_CELSIUS(tz->trips.critical.temperature),
1053 nocrt ? " <disabled>\n" : "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 if (tz->trips.hot.flags.valid)
Len Brownf5487142007-08-12 00:12:44 -04001056 seq_printf(seq, "hot (S4): %ld C%s",
1057 KELVIN_TO_CELSIUS(tz->trips.hot.temperature),
1058 nocrt ? " <disabled>\n" : "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 if (tz->trips.passive.flags.valid) {
Len Brown4be44fc2005-08-05 00:44:28 -04001061 seq_printf(seq,
1062 "passive: %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
1063 KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
1064 tz->trips.passive.tc1, tz->trips.passive.tc2,
1065 tz->trips.passive.tsp);
1066 for (j = 0; j < tz->trips.passive.devices.count; j++) {
Thomas Renningere7c746e2007-06-18 00:40:51 -04001067 status = acpi_bus_get_device(tz->trips.passive.devices.
1068 handles[j], &device);
1069 seq_printf(seq, "%4.4s ", status ? "" :
1070 acpi_device_bid(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 }
1072 seq_puts(seq, "\n");
Frans Pop7fb26162009-10-26 08:39:01 +01001073 } else {
1074 seq_printf(seq, "passive (forced):");
1075 if (tz->thermal_zone->forced_passive)
1076 seq_printf(seq, " %i C\n",
1077 tz->thermal_zone->forced_passive / 1000);
1078 else
1079 seq_printf(seq, "<not set>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 }
1081
1082 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1083 if (!(tz->trips.active[i].flags.valid))
1084 break;
1085 seq_printf(seq, "active[%d]: %ld C: devices=",
Len Brown4be44fc2005-08-05 00:44:28 -04001086 i,
1087 KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
Thomas Renninger68ccfaa2006-11-19 23:01:40 +01001088 for (j = 0; j < tz->trips.active[i].devices.count; j++){
Thomas Renningere7c746e2007-06-18 00:40:51 -04001089 status = acpi_bus_get_device(tz->trips.active[i].
1090 devices.handles[j],
1091 &device);
1092 seq_printf(seq, "%4.4s ", status ? "" :
1093 acpi_device_bid(device));
Thomas Renninger68ccfaa2006-11-19 23:01:40 +01001094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 seq_puts(seq, "\n");
1096 }
1097
Len Brown4be44fc2005-08-05 00:44:28 -04001098 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001099 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100}
1101
1102static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
1103{
1104 return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
1105}
1106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
1108{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001109 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 if (!tz)
1113 goto end;
1114
Len Browneaca2d32007-04-30 23:27:43 -04001115 if (!tz->flags.cooling_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 seq_puts(seq, "<setting not supported>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 else
Len Browneaca2d32007-04-30 23:27:43 -04001118 seq_puts(seq, "0 - Active; 1 - Passive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Len Brown4be44fc2005-08-05 00:44:28 -04001120 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001121 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122}
1123
1124static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
1125{
1126 return single_open(file, acpi_thermal_cooling_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -04001127 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128}
1129
1130static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001131acpi_thermal_write_cooling_mode(struct file *file,
1132 const char __user * buffer,
1133 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001135 struct seq_file *m = file->private_data;
1136 struct acpi_thermal *tz = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001137 int result = 0;
1138 char mode_string[12] = { '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141 if (!tz || (count > sizeof(mode_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -04001142 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
1144 if (!tz->flags.cooling_mode)
Patrick Mocheld550d982006-06-27 00:41:40 -04001145 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147 if (copy_from_user(mode_string, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001148 return -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -04001149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 mode_string[count] = '\0';
Len Brown4be44fc2005-08-05 00:44:28 -04001151
1152 result = acpi_thermal_set_cooling_mode(tz,
1153 simple_strtoul(mode_string, NULL,
1154 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001156 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158 acpi_thermal_check(tz);
1159
Patrick Mocheld550d982006-06-27 00:41:40 -04001160 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
1162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
1164{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001165 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 if (!tz)
1169 goto end;
1170
Matthew Garrettb1569e92008-12-03 17:55:32 +00001171 if (!tz->thermal_zone->polling_delay) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 seq_puts(seq, "<polling disabled>\n");
1173 goto end;
1174 }
1175
Matthew Garrettb1569e92008-12-03 17:55:32 +00001176 seq_printf(seq, "polling frequency: %d seconds\n",
1177 (tz->thermal_zone->polling_delay / 1000));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Len Brown4be44fc2005-08-05 00:44:28 -04001179 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001180 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181}
1182
1183static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
1184{
1185 return single_open(file, acpi_thermal_polling_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -04001186 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187}
1188
1189static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001190acpi_thermal_write_polling(struct file *file,
1191 const char __user * buffer,
1192 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001194 struct seq_file *m = file->private_data;
1195 struct acpi_thermal *tz = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001196 int result = 0;
1197 char polling_string[12] = { '\0' };
1198 int seconds = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 if (!tz || (count > sizeof(polling_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -04001202 return -EINVAL;
Len Brown4be44fc2005-08-05 00:44:28 -04001203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 if (copy_from_user(polling_string, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001205 return -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -04001206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 polling_string[count] = '\0';
1208
1209 seconds = simple_strtoul(polling_string, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 result = acpi_thermal_set_polling(tz, seconds);
1212 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001213 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 acpi_thermal_check(tz);
1216
Patrick Mocheld550d982006-06-27 00:41:40 -04001217 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218}
1219
Len Brown4be44fc2005-08-05 00:44:28 -04001220static int acpi_thermal_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
Len Brown4be44fc2005-08-05 00:44:28 -04001222 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225 if (!acpi_device_dir(device)) {
1226 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -04001227 acpi_thermal_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001229 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 }
1231
1232 /* 'state' [R] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001233 entry = proc_create_data(ACPI_THERMAL_FILE_STATE,
1234 S_IRUGO, acpi_device_dir(device),
1235 &acpi_thermal_state_fops,
1236 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001238 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 /* 'temperature' [R] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001241 entry = proc_create_data(ACPI_THERMAL_FILE_TEMPERATURE,
1242 S_IRUGO, acpi_device_dir(device),
1243 &acpi_thermal_temp_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
Pavel Machek2db9ccb2007-08-24 11:45:50 +02001248 /* 'trip_points' [R] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001249 entry = proc_create_data(ACPI_THERMAL_FILE_TRIP_POINTS,
1250 S_IRUGO,
1251 acpi_device_dir(device),
1252 &acpi_thermal_trip_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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 /* 'cooling_mode' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001258 entry = proc_create_data(ACPI_THERMAL_FILE_COOLING_MODE,
1259 S_IFREG | S_IRUGO | S_IWUSR,
1260 acpi_device_dir(device),
1261 &acpi_thermal_cooling_fops,
1262 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001264 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266 /* 'polling_frequency' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001267 entry = proc_create_data(ACPI_THERMAL_FILE_POLLING_FREQ,
1268 S_IFREG | S_IRUGO | S_IWUSR,
1269 acpi_device_dir(device),
1270 &acpi_thermal_polling_fops,
1271 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001273 return -ENODEV;
Patrick Mocheld550d982006-06-27 00:41:40 -04001274 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275}
1276
Len Brown4be44fc2005-08-05 00:44:28 -04001277static int acpi_thermal_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280 if (acpi_device_dir(device)) {
1281 remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1282 acpi_device_dir(device));
1283 remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1284 acpi_device_dir(device));
1285 remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1286 acpi_device_dir(device));
1287 remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1288 acpi_device_dir(device));
1289 remove_proc_entry(ACPI_THERMAL_FILE_STATE,
1290 acpi_device_dir(device));
1291 remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
1292 acpi_device_dir(device) = NULL;
1293 }
1294
Patrick Mocheld550d982006-06-27 00:41:40 -04001295 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296}
1297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298/* --------------------------------------------------------------------------
1299 Driver Interface
1300 -------------------------------------------------------------------------- */
1301
Bjorn Helgaas342d5502009-04-07 15:37:06 +00001302static void acpi_thermal_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
Bjorn Helgaas342d5502009-04-07 15:37:06 +00001304 struct acpi_thermal *tz = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001308 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 switch (event) {
1311 case ACPI_THERMAL_NOTIFY_TEMPERATURE:
1312 acpi_thermal_check(tz);
1313 break;
1314 case ACPI_THERMAL_NOTIFY_THRESHOLDS:
Zhang Ruice44e192008-01-17 15:51:25 +08001315 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_THRESHOLDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 acpi_thermal_check(tz);
Len Brown14e04fb2007-08-23 15:20:26 -04001317 acpi_bus_generate_proc_event(device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08001318 acpi_bus_generate_netlink_event(device->pnp.device_class,
Kay Sievers07944692008-10-30 01:18:59 +01001319 dev_name(&device->dev), event, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 break;
1321 case ACPI_THERMAL_NOTIFY_DEVICES:
Zhang Ruice44e192008-01-17 15:51:25 +08001322 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_DEVICES);
1323 acpi_thermal_check(tz);
Len Brown14e04fb2007-08-23 15:20:26 -04001324 acpi_bus_generate_proc_event(device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08001325 acpi_bus_generate_netlink_event(device->pnp.device_class,
Kay Sievers07944692008-10-30 01:18:59 +01001326 dev_name(&device->dev), event, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 break;
1328 default:
1329 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001330 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 break;
1332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333}
1334
Len Brown4be44fc2005-08-05 00:44:28 -04001335static int acpi_thermal_get_info(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
Len Brown4be44fc2005-08-05 00:44:28 -04001337 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001341 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
1343 /* Get temperature [_TMP] (required) */
1344 result = acpi_thermal_get_temperature(tz);
1345 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001346 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
1348 /* Get trip points [_CRT, _PSV, etc.] (required) */
1349 result = acpi_thermal_get_trip_points(tz);
1350 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001351 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 /* Set the cooling mode [_SCP] to active cooling (default) */
1354 result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
Len Brown4be44fc2005-08-05 00:44:28 -04001355 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 tz->flags.cooling_mode = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358 /* Get default polling frequency [_TZP] (optional) */
1359 if (tzp)
1360 tz->polling_frequency = tzp;
1361 else
1362 acpi_thermal_get_polling_frequency(tz);
1363
Patrick Mocheld550d982006-06-27 00:41:40 -04001364 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365}
1366
Jean Delvare13614e32009-04-06 16:01:46 +02001367/*
1368 * The exact offset between Kelvin and degree Celsius is 273.15. However ACPI
1369 * handles temperature values with a single decimal place. As a consequence,
1370 * some implementations use an offset of 273.1 and others use an offset of
1371 * 273.2. Try to find out which one is being used, to present the most
1372 * accurate and visually appealing number.
1373 *
1374 * The heuristic below should work for all ACPI thermal zones which have a
1375 * critical trip point with a value being a multiple of 0.5 degree Celsius.
1376 */
1377static void acpi_thermal_guess_offset(struct acpi_thermal *tz)
1378{
1379 if (tz->trips.critical.flags.valid &&
1380 (tz->trips.critical.temperature % 5) == 1)
1381 tz->kelvin_offset = 2731;
1382 else
1383 tz->kelvin_offset = 2732;
1384}
1385
Len Brown4be44fc2005-08-05 00:44:28 -04001386static int acpi_thermal_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
Len Brown4be44fc2005-08-05 00:44:28 -04001388 int result = 0;
Len Brown4be44fc2005-08-05 00:44:28 -04001389 struct acpi_thermal *tz = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001393 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Burman Yan36bcbec2006-12-19 12:56:11 -08001395 tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001397 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Patrick Mochel8348e1b2006-05-19 16:54:40 -04001399 tz->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 strcpy(tz->name, device->pnp.bus_id);
1401 strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1402 strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001403 device->driver_data = tz;
Alexey Starikovskiy6e215782007-09-01 00:11:59 +04001404 mutex_init(&tz->lock);
Zhang Rui3f655ef2008-01-17 15:51:11 +08001405
1406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 result = acpi_thermal_get_info(tz);
1408 if (result)
Zhang Rui3f655ef2008-01-17 15:51:11 +08001409 goto free_memory;
1410
Jean Delvare13614e32009-04-06 16:01:46 +02001411 acpi_thermal_guess_offset(tz);
1412
Zhang Rui3f655ef2008-01-17 15:51:11 +08001413 result = acpi_thermal_register_thermal_zone(tz);
1414 if (result)
1415 goto free_memory;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 result = acpi_thermal_add_fs(device);
1418 if (result)
Zhang Rui3f655ef2008-01-17 15:51:11 +08001419 goto unregister_thermal_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001422 acpi_device_name(device), acpi_device_bid(device),
1423 KELVIN_TO_CELSIUS(tz->temperature));
Zhang Rui3f655ef2008-01-17 15:51:11 +08001424 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Zhang Rui3f655ef2008-01-17 15:51:11 +08001426unregister_thermal_zone:
1427 thermal_zone_device_unregister(tz->thermal_zone);
1428free_memory:
1429 kfree(tz);
1430end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001431 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432}
1433
Len Brown4be44fc2005-08-05 00:44:28 -04001434static int acpi_thermal_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
Len Brown4be44fc2005-08-05 00:44:28 -04001436 struct acpi_thermal *tz = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001439 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001441 tz = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 acpi_thermal_remove_fs(device);
Zhang Rui3f655ef2008-01-17 15:51:11 +08001444 acpi_thermal_unregister_thermal_zone(tz);
Alexey Starikovskiy6e215782007-09-01 00:11:59 +04001445 mutex_destroy(&tz->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 kfree(tz);
Patrick Mocheld550d982006-06-27 00:41:40 -04001447 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448}
1449
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001450static int acpi_thermal_resume(struct acpi_device *device)
Konstantin Karasyov74ce1462006-05-08 08:32:00 -04001451{
1452 struct acpi_thermal *tz = NULL;
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001453 int i, j, power_state, result;
1454
Konstantin Karasyov74ce1462006-05-08 08:32:00 -04001455
1456 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001457 return -EINVAL;
Konstantin Karasyov74ce1462006-05-08 08:32:00 -04001458
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001459 tz = acpi_driver_data(device);
Konstantin Karasyov74ce1462006-05-08 08:32:00 -04001460
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001461 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001462 if (!(&tz->trips.active[i]))
1463 break;
1464 if (!tz->trips.active[i].flags.valid)
1465 break;
1466 tz->trips.active[i].flags.enabled = 1;
1467 for (j = 0; j < tz->trips.active[i].devices.count; j++) {
1468 result = acpi_bus_get_power(tz->trips.active[i].devices.
1469 handles[j], &power_state);
1470 if (result || (power_state != ACPI_STATE_D0)) {
1471 tz->trips.active[i].flags.enabled = 0;
1472 break;
1473 }
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001474 }
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001475 tz->state.active |= tz->trips.active[i].flags.enabled;
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001476 }
1477
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001478 acpi_thermal_check(tz);
Konstantin Karasyov74ce1462006-05-08 08:32:00 -04001479
1480 return AE_OK;
1481}
1482
Jeff Garzik18552562007-10-03 15:15:40 -04001483static int thermal_act(const struct dmi_system_id *d) {
Len Brown0b5bfa12007-08-12 00:13:02 -04001484
1485 if (act == 0) {
1486 printk(KERN_NOTICE "ACPI: %s detected: "
1487 "disabling all active thermal trip points\n", d->ident);
1488 act = -1;
1489 }
1490 return 0;
1491}
Jeff Garzik18552562007-10-03 15:15:40 -04001492static int thermal_nocrt(const struct dmi_system_id *d) {
Len Brown8c99fdc2007-08-20 18:46:50 -04001493
1494 printk(KERN_NOTICE "ACPI: %s detected: "
1495 "disabling all critical thermal trip point actions.\n", d->ident);
1496 nocrt = 1;
1497 return 0;
1498}
Jeff Garzik18552562007-10-03 15:15:40 -04001499static int thermal_tzp(const struct dmi_system_id *d) {
Len Brown0b5bfa12007-08-12 00:13:02 -04001500
1501 if (tzp == 0) {
1502 printk(KERN_NOTICE "ACPI: %s detected: "
1503 "enabling thermal zone polling\n", d->ident);
1504 tzp = 300; /* 300 dS = 30 Seconds */
1505 }
1506 return 0;
1507}
Jeff Garzik18552562007-10-03 15:15:40 -04001508static int thermal_psv(const struct dmi_system_id *d) {
Len Brown0b5bfa12007-08-12 00:13:02 -04001509
1510 if (psv == 0) {
1511 printk(KERN_NOTICE "ACPI: %s detected: "
1512 "disabling all passive thermal trip points\n", d->ident);
1513 psv = -1;
1514 }
1515 return 0;
1516}
1517
1518static struct dmi_system_id thermal_dmi_table[] __initdata = {
1519 /*
1520 * Award BIOS on this AOpen makes thermal control almost worthless.
1521 * http://bugzilla.kernel.org/show_bug.cgi?id=8842
1522 */
1523 {
1524 .callback = thermal_act,
1525 .ident = "AOpen i915GMm-HFS",
1526 .matches = {
1527 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1528 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1529 },
1530 },
1531 {
1532 .callback = thermal_psv,
1533 .ident = "AOpen i915GMm-HFS",
1534 .matches = {
1535 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1536 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1537 },
1538 },
1539 {
1540 .callback = thermal_tzp,
1541 .ident = "AOpen i915GMm-HFS",
1542 .matches = {
1543 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1544 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1545 },
1546 },
Len Brown8c99fdc2007-08-20 18:46:50 -04001547 {
1548 .callback = thermal_nocrt,
1549 .ident = "Gigabyte GA-7ZX",
1550 .matches = {
1551 DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
1552 DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
1553 },
1554 },
Len Brown0b5bfa12007-08-12 00:13:02 -04001555 {}
1556};
Len Brown0b5bfa12007-08-12 00:13:02 -04001557
Len Brown4be44fc2005-08-05 00:44:28 -04001558static int __init acpi_thermal_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
Len Brown4be44fc2005-08-05 00:44:28 -04001560 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Len Brown0b5bfa12007-08-12 00:13:02 -04001562 dmi_check_system(thermal_dmi_table);
1563
Len Brown72b33ef2007-08-12 00:12:17 -04001564 if (off) {
1565 printk(KERN_NOTICE "ACPI: thermal control disabled\n");
1566 return -ENODEV;
1567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
1569 if (!acpi_thermal_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001570 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
1572 result = acpi_bus_register_driver(&acpi_thermal_driver);
1573 if (result < 0) {
1574 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04001575 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 }
1577
Patrick Mocheld550d982006-06-27 00:41:40 -04001578 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579}
1580
Len Brown4be44fc2005-08-05 00:44:28 -04001581static void __exit acpi_thermal_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
1584 acpi_bus_unregister_driver(&acpi_thermal_driver);
1585
1586 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1587
Patrick Mocheld550d982006-06-27 00:41:40 -04001588 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589}
1590
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591module_init(acpi_thermal_init);
1592module_exit(acpi_thermal_exit);