blob: 2f8f17131d9fb9b19094284fcfa2dd5f0092f7b8 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/types.h>
Zhang Rui43d9f872010-07-15 10:46:44 +080040
41#ifdef CONFIG_ACPI_PROCFS
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/proc_fs.h>
Zhang Rui43d9f872010-07-15 10:46:44 +080043#include <linux/seq_file.h>
44#endif
45
Tim Schmielaucd354f12007-02-14 00:33:14 -080046#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/kmod.h>
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -070048#include <linux/reboot.h>
Stephen Rothwellf6f5c452009-03-03 12:47:17 +110049#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <asm/uaccess.h>
Zhang Rui3f655ef2008-01-17 15:51:11 +080051#include <linux/thermal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <acpi/acpi_bus.h>
53#include <acpi/acpi_drivers.h>
54
Len Browna192a952009-07-28 16:45:54 -040055#define PREFIX "ACPI: "
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#define ACPI_THERMAL_CLASS "thermal_zone"
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#define ACPI_THERMAL_DEVICE_NAME "Thermal Zone"
59#define ACPI_THERMAL_FILE_STATE "state"
60#define ACPI_THERMAL_FILE_TEMPERATURE "temperature"
61#define ACPI_THERMAL_FILE_TRIP_POINTS "trip_points"
62#define ACPI_THERMAL_FILE_COOLING_MODE "cooling_mode"
63#define ACPI_THERMAL_FILE_POLLING_FREQ "polling_frequency"
64#define ACPI_THERMAL_NOTIFY_TEMPERATURE 0x80
65#define ACPI_THERMAL_NOTIFY_THRESHOLDS 0x81
66#define ACPI_THERMAL_NOTIFY_DEVICES 0x82
67#define ACPI_THERMAL_NOTIFY_CRITICAL 0xF0
68#define ACPI_THERMAL_NOTIFY_HOT 0xF1
69#define ACPI_THERMAL_MODE_ACTIVE 0x00
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71#define ACPI_THERMAL_MAX_ACTIVE 10
72#define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define _COMPONENT ACPI_THERMAL_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050075ACPI_MODULE_NAME("thermal");
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Thomas Renninger1cbf4c52004-09-16 11:07:00 -040077MODULE_AUTHOR("Paul Diefenbaugh");
Len Brown7cda93e2007-02-12 23:50:02 -050078MODULE_DESCRIPTION("ACPI Thermal Zone Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070079MODULE_LICENSE("GPL");
80
Len Brownf8707ec2007-08-12 00:12:54 -040081static int act;
82module_param(act, int, 0644);
Len Brown3c1d36d2007-08-14 15:12:56 -040083MODULE_PARM_DESC(act, "Disable or override all lowest active trip points.");
Len Brownf8707ec2007-08-12 00:12:54 -040084
Len Brownc52a7412007-08-14 15:49:32 -040085static int crt;
86module_param(crt, int, 0644);
87MODULE_PARM_DESC(crt, "Disable or lower all critical trip points.");
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089static int tzp;
Len Brown730ff342007-08-12 00:12:26 -040090module_param(tzp, int, 0444);
Len Brown3c1d36d2007-08-14 15:12:56 -040091MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Len Brownf5487142007-08-12 00:12:44 -040093static int nocrt;
94module_param(nocrt, int, 0);
Len Brown8c99fdc2007-08-20 18:46:50 -040095MODULE_PARM_DESC(nocrt, "Set to take no action upon ACPI thermal zone critical trips points.");
Len Brownf5487142007-08-12 00:12:44 -040096
Len Brown72b33ef2007-08-12 00:12:17 -040097static int off;
98module_param(off, int, 0);
Len Brown3c1d36d2007-08-14 15:12:56 -040099MODULE_PARM_DESC(off, "Set to disable ACPI thermal support.");
Len Brown72b33ef2007-08-12 00:12:17 -0400100
Len Browna70cdc52007-08-12 00:12:35 -0400101static int psv;
102module_param(psv, int, 0644);
Len Brown3c1d36d2007-08-14 15:12:56 -0400103MODULE_PARM_DESC(psv, "Disable or override all passive trip points.");
Len Browna70cdc52007-08-12 00:12:35 -0400104
Len Brown4be44fc2005-08-05 00:44:28 -0400105static int acpi_thermal_add(struct acpi_device *device);
106static int acpi_thermal_remove(struct acpi_device *device, int type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800107static int acpi_thermal_resume(struct acpi_device *device);
Bjorn Helgaas342d5502009-04-07 15:37:06 +0000108static void acpi_thermal_notify(struct acpi_device *device, u32 event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200110static const struct acpi_device_id thermal_device_ids[] = {
111 {ACPI_THERMAL_HID, 0},
112 {"", 0},
113};
114MODULE_DEVICE_TABLE(acpi, thermal_device_ids);
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116static struct acpi_driver acpi_thermal_driver = {
Len Brownc2b67052007-02-12 23:33:40 -0500117 .name = "thermal",
Len Brown4be44fc2005-08-05 00:44:28 -0400118 .class = ACPI_THERMAL_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200119 .ids = thermal_device_ids,
Len Brown4be44fc2005-08-05 00:44:28 -0400120 .ops = {
121 .add = acpi_thermal_add,
122 .remove = acpi_thermal_remove,
Konstantin Karasyov74ce14682006-05-08 08:32:00 -0400123 .resume = acpi_thermal_resume,
Bjorn Helgaas342d5502009-04-07 15:37:06 +0000124 .notify = acpi_thermal_notify,
Len Brown4be44fc2005-08-05 00:44:28 -0400125 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126};
127
128struct acpi_thermal_state {
Len Brown4be44fc2005-08-05 00:44:28 -0400129 u8 critical:1;
130 u8 hot:1;
131 u8 passive:1;
132 u8 active:1;
133 u8 reserved:4;
134 int active_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135};
136
137struct acpi_thermal_state_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400138 u8 valid:1;
139 u8 enabled:1;
140 u8 reserved:6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141};
142
143struct acpi_thermal_critical {
144 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400145 unsigned long temperature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146};
147
148struct acpi_thermal_hot {
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_passive {
154 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400155 unsigned long temperature;
156 unsigned long tc1;
157 unsigned long tc2;
158 unsigned long tsp;
159 struct acpi_handle_list devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160};
161
162struct acpi_thermal_active {
163 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400164 unsigned long temperature;
165 struct acpi_handle_list devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166};
167
168struct acpi_thermal_trips {
169 struct acpi_thermal_critical critical;
Len Brown4be44fc2005-08-05 00:44:28 -0400170 struct acpi_thermal_hot hot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 struct acpi_thermal_passive passive;
172 struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
173};
174
175struct acpi_thermal_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400176 u8 cooling_mode:1; /* _SCP */
177 u8 devices:1; /* _TZD */
178 u8 reserved:6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179};
180
181struct acpi_thermal {
Patrick Mochel8348e1b2006-05-19 16:54:40 -0400182 struct acpi_device * device;
Len Brown4be44fc2005-08-05 00:44:28 -0400183 acpi_bus_id name;
184 unsigned long temperature;
185 unsigned long last_temperature;
186 unsigned long polling_frequency;
Len Brown4be44fc2005-08-05 00:44:28 -0400187 volatile u8 zombie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 struct acpi_thermal_flags flags;
189 struct acpi_thermal_state state;
190 struct acpi_thermal_trips trips;
Len Brown4be44fc2005-08-05 00:44:28 -0400191 struct acpi_handle_list devices;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800192 struct thermal_zone_device *thermal_zone;
193 int tz_enabled;
Jean Delvare13614e32009-04-06 16:01:46 +0200194 int kelvin_offset;
Alexey Starikovskiy6e215782007-09-01 00:11:59 +0400195 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196};
197
Zhang Rui43d9f872010-07-15 10:46:44 +0800198#ifdef CONFIG_ACPI_PROCFS
199static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file);
200static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file);
201static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file);
202static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file);
203static ssize_t acpi_thermal_write_cooling_mode(struct file *,
204 const char __user *, size_t,
205 loff_t *);
206static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file);
207static ssize_t acpi_thermal_write_polling(struct file *, const char __user *,
208 size_t, loff_t *);
209
Arjan van de Vend7508032006-07-04 13:06:00 -0400210static const struct file_operations acpi_thermal_state_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700211 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400212 .open = acpi_thermal_state_open_fs,
213 .read = seq_read,
214 .llseek = seq_lseek,
215 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216};
217
Arjan van de Vend7508032006-07-04 13:06:00 -0400218static const struct file_operations acpi_thermal_temp_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700219 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400220 .open = acpi_thermal_temp_open_fs,
221 .read = seq_read,
222 .llseek = seq_lseek,
223 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224};
225
Arjan van de Vend7508032006-07-04 13:06:00 -0400226static const struct file_operations acpi_thermal_trip_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700227 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400228 .open = acpi_thermal_trip_open_fs,
229 .read = seq_read,
Len Brown4be44fc2005-08-05 00:44:28 -0400230 .llseek = seq_lseek,
231 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232};
233
Arjan van de Vend7508032006-07-04 13:06:00 -0400234static const struct file_operations acpi_thermal_cooling_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700235 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400236 .open = acpi_thermal_cooling_open_fs,
237 .read = seq_read,
238 .write = acpi_thermal_write_cooling_mode,
239 .llseek = seq_lseek,
240 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241};
242
Arjan van de Vend7508032006-07-04 13:06:00 -0400243static const struct file_operations acpi_thermal_polling_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700244 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400245 .open = acpi_thermal_polling_open_fs,
246 .read = seq_read,
247 .write = acpi_thermal_write_polling,
248 .llseek = seq_lseek,
249 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250};
Zhang Rui43d9f872010-07-15 10:46:44 +0800251#endif /* CONFIG_ACPI_PROCFS*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253/* --------------------------------------------------------------------------
254 Thermal Zone Management
255 -------------------------------------------------------------------------- */
256
Len Brown4be44fc2005-08-05 00:44:28 -0400257static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
Len Brown4be44fc2005-08-05 00:44:28 -0400259 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400260 unsigned long long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400263 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 tz->last_temperature = tz->temperature;
266
Matthew Wilcox27663c52008-10-10 02:22:59 -0400267 status = acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400269 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Matthew Wilcox27663c52008-10-10 02:22:59 -0400271 tz->temperature = tmp;
Len Brown4be44fc2005-08-05 00:44:28 -0400272 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
273 tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Patrick Mocheld550d982006-06-27 00:41:40 -0400275 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
Len Brown4be44fc2005-08-05 00:44:28 -0400278static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Len Brown4be44fc2005-08-05 00:44:28 -0400280 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400281 unsigned long long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400284 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Matthew Wilcox27663c52008-10-10 02:22:59 -0400286 status = acpi_evaluate_integer(tz->device->handle, "_TZP", NULL, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400288 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Matthew Wilcox27663c52008-10-10 02:22:59 -0400290 tz->polling_frequency = tmp;
Len Brown4be44fc2005-08-05 00:44:28 -0400291 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
292 tz->polling_frequency));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Patrick Mocheld550d982006-06-27 00:41:40 -0400294 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
Len Brown4be44fc2005-08-05 00:44:28 -0400297static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Len Brown4be44fc2005-08-05 00:44:28 -0400299 acpi_status status = AE_OK;
300 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
301 struct acpi_object_list arg_list = { 1, &arg0 };
302 acpi_handle handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400306 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400308 status = acpi_get_handle(tz->device->handle, "_SCP", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (ACPI_FAILURE(status)) {
310 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400311 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
313
314 arg0.integer.value = mode;
315
316 status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
317 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400318 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Patrick Mocheld550d982006-06-27 00:41:40 -0400320 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
Zhang Ruice44e192008-01-17 15:51:25 +0800323#define ACPI_TRIPS_CRITICAL 0x01
324#define ACPI_TRIPS_HOT 0x02
325#define ACPI_TRIPS_PASSIVE 0x04
326#define ACPI_TRIPS_ACTIVE 0x08
327#define ACPI_TRIPS_DEVICES 0x10
328
329#define ACPI_TRIPS_REFRESH_THRESHOLDS (ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE)
330#define ACPI_TRIPS_REFRESH_DEVICES ACPI_TRIPS_DEVICES
331
332#define ACPI_TRIPS_INIT (ACPI_TRIPS_CRITICAL | ACPI_TRIPS_HOT | \
333 ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE | \
334 ACPI_TRIPS_DEVICES)
335
336/*
337 * This exception is thrown out in two cases:
338 * 1.An invalid trip point becomes invalid or a valid trip point becomes invalid
339 * when re-evaluating the AML code.
340 * 2.TODO: Devices listed in _PSL, _ALx, _TZD may change.
341 * We need to re-bind the cooling devices of a thermal zone when this occurs.
342 */
343#define ACPI_THERMAL_TRIPS_EXCEPTION(flags, str) \
344do { \
345 if (flags != ACPI_TRIPS_INIT) \
346 ACPI_EXCEPTION((AE_INFO, AE_ERROR, \
347 "ACPI thermal trip point %s changed\n" \
348 "Please send acpidump to linux-acpi@vger.kernel.org\n", str)); \
349} while (0)
350
351static int acpi_thermal_trips_update(struct acpi_thermal *tz, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Len Brown4be44fc2005-08-05 00:44:28 -0400353 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400354 unsigned long long tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800355 struct acpi_handle_list devices;
356 int valid = 0;
357 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Thomas Renningerfa809452010-02-20 11:44:27 +0100359 /* Critical Shutdown */
Zhang Ruice44e192008-01-17 15:51:25 +0800360 if (flag & ACPI_TRIPS_CRITICAL) {
361 status = acpi_evaluate_integer(tz->device->handle,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400362 "_CRT", NULL, &tmp);
363 tz->trips.critical.temperature = tmp;
Arjan van de Vena39a2d72008-05-19 15:55:15 -0700364 /*
365 * Treat freezing temperatures as invalid as well; some
366 * BIOSes return really low values and cause reboots at startup.
Adam Buchbinderb731d7b2009-03-13 12:15:26 -0400367 * Below zero (Celsius) values clearly aren't right for sure..
Arjan van de Vena39a2d72008-05-19 15:55:15 -0700368 * ... so lets discard those as invalid.
369 */
Thomas Renningerfa809452010-02-20 11:44:27 +0100370 if (ACPI_FAILURE(status)) {
Len Brownc52a7412007-08-14 15:49:32 -0400371 tz->trips.critical.flags.valid = 0;
Thomas Renningerfa809452010-02-20 11:44:27 +0100372 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
373 "No critical threshold\n"));
374 } else if (tmp <= 2732) {
375 printk(KERN_WARNING FW_BUG "Invalid critical threshold "
376 "(%llu)\n", tmp);
377 tz->trips.critical.flags.valid = 0;
Zhang Ruice44e192008-01-17 15:51:25 +0800378 } else {
379 tz->trips.critical.flags.valid = 1;
380 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Thomas Renningerfa809452010-02-20 11:44:27 +0100381 "Found critical threshold [%lu]\n",
382 tz->trips.critical.temperature));
Zhang Ruice44e192008-01-17 15:51:25 +0800383 }
384 if (tz->trips.critical.flags.valid == 1) {
385 if (crt == -1) {
386 tz->trips.critical.flags.valid = 0;
387 } else if (crt > 0) {
388 unsigned long crt_k = CELSIUS_TO_KELVIN(crt);
389 /*
Zhang Rui22a94d72008-10-17 02:41:20 -0400390 * Allow override critical threshold
Zhang Ruice44e192008-01-17 15:51:25 +0800391 */
Zhang Rui22a94d72008-10-17 02:41:20 -0400392 if (crt_k > tz->trips.critical.temperature)
393 printk(KERN_WARNING PREFIX
394 "Critical threshold %d C\n", crt);
395 tz->trips.critical.temperature = crt_k;
Zhang Ruice44e192008-01-17 15:51:25 +0800396 }
Len Brownc52a7412007-08-14 15:49:32 -0400397 }
398 }
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 /* Critical Sleep (optional) */
Zhang Ruice44e192008-01-17 15:51:25 +0800401 if (flag & ACPI_TRIPS_HOT) {
Len Browna70cdc52007-08-12 00:12:35 -0400402 status = acpi_evaluate_integer(tz->device->handle,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400403 "_HOT", NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800404 if (ACPI_FAILURE(status)) {
405 tz->trips.hot.flags.valid = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400406 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Zhang Ruice44e192008-01-17 15:51:25 +0800407 "No hot threshold\n"));
408 } else {
Matthew Wilcox27663c52008-10-10 02:22:59 -0400409 tz->trips.hot.temperature = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800410 tz->trips.hot.flags.valid = 1;
411 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
412 "Found hot threshold [%lu]\n",
413 tz->trips.critical.temperature));
414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
416
Zhang Ruice44e192008-01-17 15:51:25 +0800417 /* Passive (optional) */
Zhang Rui0e4240d2009-01-16 12:53:42 -0500418 if (((flag & ACPI_TRIPS_PASSIVE) && tz->trips.passive.flags.valid) ||
419 (flag == ACPI_TRIPS_INIT)) {
Zhang Ruice44e192008-01-17 15:51:25 +0800420 valid = tz->trips.passive.flags.valid;
421 if (psv == -1) {
422 status = AE_SUPPORT;
423 } else if (psv > 0) {
Matthew Wilcox27663c52008-10-10 02:22:59 -0400424 tmp = CELSIUS_TO_KELVIN(psv);
Zhang Ruice44e192008-01-17 15:51:25 +0800425 status = AE_OK;
426 } else {
427 status = acpi_evaluate_integer(tz->device->handle,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400428 "_PSV", NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Zhang Ruice44e192008-01-17 15:51:25 +0800431 if (ACPI_FAILURE(status))
432 tz->trips.passive.flags.valid = 0;
433 else {
Matthew Wilcox27663c52008-10-10 02:22:59 -0400434 tz->trips.passive.temperature = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800435 tz->trips.passive.flags.valid = 1;
436 if (flag == ACPI_TRIPS_INIT) {
437 status = acpi_evaluate_integer(
438 tz->device->handle, "_TC1",
Matthew Wilcox27663c52008-10-10 02:22:59 -0400439 NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800440 if (ACPI_FAILURE(status))
441 tz->trips.passive.flags.valid = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400442 else
443 tz->trips.passive.tc1 = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800444 status = acpi_evaluate_integer(
445 tz->device->handle, "_TC2",
Matthew Wilcox27663c52008-10-10 02:22:59 -0400446 NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800447 if (ACPI_FAILURE(status))
448 tz->trips.passive.flags.valid = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400449 else
450 tz->trips.passive.tc2 = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800451 status = acpi_evaluate_integer(
452 tz->device->handle, "_TSP",
Matthew Wilcox27663c52008-10-10 02:22:59 -0400453 NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800454 if (ACPI_FAILURE(status))
455 tz->trips.passive.flags.valid = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400456 else
457 tz->trips.passive.tsp = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800458 }
459 }
460 }
461 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.passive.flags.valid) {
462 memset(&devices, 0, sizeof(struct acpi_handle_list));
463 status = acpi_evaluate_reference(tz->device->handle, "_PSL",
464 NULL, &devices);
Zhang Rui0e4240d2009-01-16 12:53:42 -0500465 if (ACPI_FAILURE(status)) {
466 printk(KERN_WARNING PREFIX
467 "Invalid passive threshold\n");
Zhang Ruice44e192008-01-17 15:51:25 +0800468 tz->trips.passive.flags.valid = 0;
Zhang Rui0e4240d2009-01-16 12:53:42 -0500469 }
Zhang Ruice44e192008-01-17 15:51:25 +0800470 else
471 tz->trips.passive.flags.valid = 1;
472
473 if (memcmp(&tz->trips.passive.devices, &devices,
474 sizeof(struct acpi_handle_list))) {
475 memcpy(&tz->trips.passive.devices, &devices,
476 sizeof(struct acpi_handle_list));
477 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
478 }
479 }
480 if ((flag & ACPI_TRIPS_PASSIVE) || (flag & ACPI_TRIPS_DEVICES)) {
481 if (valid != tz->trips.passive.flags.valid)
482 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
483 }
484
485 /* Active (optional) */
Len Brown4be44fc2005-08-05 00:44:28 -0400486 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Len Brown4be44fc2005-08-05 00:44:28 -0400487 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
Zhang Ruice44e192008-01-17 15:51:25 +0800488 valid = tz->trips.active[i].flags.valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Len Brownf8707ec2007-08-12 00:12:54 -0400490 if (act == -1)
Zhang Ruice44e192008-01-17 15:51:25 +0800491 break; /* disable all active trip points */
Len Brownf8707ec2007-08-12 00:12:54 -0400492
Zhang Rui0e4240d2009-01-16 12:53:42 -0500493 if ((flag == ACPI_TRIPS_INIT) || ((flag & ACPI_TRIPS_ACTIVE) &&
494 tz->trips.active[i].flags.valid)) {
Zhang Ruice44e192008-01-17 15:51:25 +0800495 status = acpi_evaluate_integer(tz->device->handle,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400496 name, NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800497 if (ACPI_FAILURE(status)) {
498 tz->trips.active[i].flags.valid = 0;
499 if (i == 0)
500 break;
501 if (act <= 0)
502 break;
503 if (i == 1)
504 tz->trips.active[0].temperature =
505 CELSIUS_TO_KELVIN(act);
506 else
507 /*
508 * Don't allow override higher than
509 * the next higher trip point
510 */
511 tz->trips.active[i - 1].temperature =
512 (tz->trips.active[i - 2].temperature <
513 CELSIUS_TO_KELVIN(act) ?
514 tz->trips.active[i - 2].temperature :
515 CELSIUS_TO_KELVIN(act));
Len Brownf8707ec2007-08-12 00:12:54 -0400516 break;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400517 } else {
518 tz->trips.active[i].temperature = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800519 tz->trips.active[i].flags.valid = 1;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400520 }
Len Brownf8707ec2007-08-12 00:12:54 -0400521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 name[2] = 'L';
Zhang Ruice44e192008-01-17 15:51:25 +0800524 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.active[i].flags.valid ) {
525 memset(&devices, 0, sizeof(struct acpi_handle_list));
526 status = acpi_evaluate_reference(tz->device->handle,
527 name, NULL, &devices);
Zhang Rui0e4240d2009-01-16 12:53:42 -0500528 if (ACPI_FAILURE(status)) {
529 printk(KERN_WARNING PREFIX
530 "Invalid active%d threshold\n", i);
Zhang Ruice44e192008-01-17 15:51:25 +0800531 tz->trips.active[i].flags.valid = 0;
Zhang Rui0e4240d2009-01-16 12:53:42 -0500532 }
Zhang Ruice44e192008-01-17 15:51:25 +0800533 else
534 tz->trips.active[i].flags.valid = 1;
535
536 if (memcmp(&tz->trips.active[i].devices, &devices,
537 sizeof(struct acpi_handle_list))) {
538 memcpy(&tz->trips.active[i].devices, &devices,
539 sizeof(struct acpi_handle_list));
540 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
541 }
542 }
543 if ((flag & ACPI_TRIPS_ACTIVE) || (flag & ACPI_TRIPS_DEVICES))
544 if (valid != tz->trips.active[i].flags.valid)
545 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
546
547 if (!tz->trips.active[i].flags.valid)
548 break;
549 }
550
551 if (flag & ACPI_TRIPS_DEVICES) {
552 memset(&devices, 0, sizeof(struct acpi_handle_list));
553 status = acpi_evaluate_reference(tz->device->handle, "_TZD",
554 NULL, &devices);
555 if (memcmp(&tz->devices, &devices,
556 sizeof(struct acpi_handle_list))) {
557 memcpy(&tz->devices, &devices,
558 sizeof(struct acpi_handle_list));
559 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
562
Patrick Mocheld550d982006-06-27 00:41:40 -0400563 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
Zhang Ruice44e192008-01-17 15:51:25 +0800566static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Thomas Renninger8b7ef6d2010-02-16 22:55:51 +0100568 int i, valid, ret = acpi_thermal_trips_update(tz, ACPI_TRIPS_INIT);
569
570 if (ret)
571 return ret;
572
573 valid = tz->trips.critical.flags.valid |
574 tz->trips.hot.flags.valid |
575 tz->trips.passive.flags.valid;
576
577 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
578 valid |= tz->trips.active[i].flags.valid;
579
580 if (!valid) {
581 printk(KERN_WARNING FW_BUG "No valid trip found\n");
582 return -ENODEV;
583 }
584 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
Len Brown4be44fc2005-08-05 00:44:28 -0400587static void acpi_thermal_check(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200589 struct acpi_thermal *tz = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Matthew Garrettb1569e92008-12-03 17:55:32 +0000591 thermal_zone_device_update(tz->thermal_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
Zhang Rui3f655ef2008-01-17 15:51:11 +0800594/* sys I/F for generic thermal sysfs support */
Jean Delvare13614e32009-04-06 16:01:46 +0200595#define KELVIN_TO_MILLICELSIUS(t, off) (((t) - (off)) * 100)
Zhang, Rui5e012762008-02-28 07:51:30 +0800596
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000597static int thermal_get_temp(struct thermal_zone_device *thermal,
598 unsigned long *temp)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800599{
600 struct acpi_thermal *tz = thermal->devdata;
Zhang, Rui76ecb4f2008-04-10 16:20:23 +0800601 int result;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800602
603 if (!tz)
604 return -EINVAL;
605
Zhang, Rui76ecb4f2008-04-10 16:20:23 +0800606 result = acpi_thermal_get_temperature(tz);
607 if (result)
608 return result;
609
Jean Delvare13614e32009-04-06 16:01:46 +0200610 *temp = KELVIN_TO_MILLICELSIUS(tz->temperature, tz->kelvin_offset);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000611 return 0;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800612}
613
614static const char enabled[] = "kernel";
615static const char disabled[] = "user";
616static int thermal_get_mode(struct thermal_zone_device *thermal,
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000617 enum thermal_device_mode *mode)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800618{
619 struct acpi_thermal *tz = thermal->devdata;
620
621 if (!tz)
622 return -EINVAL;
623
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000624 *mode = tz->tz_enabled ? THERMAL_DEVICE_ENABLED :
625 THERMAL_DEVICE_DISABLED;
626
627 return 0;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800628}
629
630static int thermal_set_mode(struct thermal_zone_device *thermal,
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000631 enum thermal_device_mode mode)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800632{
633 struct acpi_thermal *tz = thermal->devdata;
634 int enable;
635
636 if (!tz)
637 return -EINVAL;
638
639 /*
640 * enable/disable thermal management from ACPI thermal driver
641 */
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000642 if (mode == THERMAL_DEVICE_ENABLED)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800643 enable = 1;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000644 else if (mode == THERMAL_DEVICE_DISABLED)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800645 enable = 0;
646 else
647 return -EINVAL;
648
649 if (enable != tz->tz_enabled) {
650 tz->tz_enabled = enable;
651 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
652 "%s ACPI thermal control\n",
653 tz->tz_enabled ? enabled : disabled));
654 acpi_thermal_check(tz);
655 }
656 return 0;
657}
658
659static int thermal_get_trip_type(struct thermal_zone_device *thermal,
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000660 int trip, enum thermal_trip_type *type)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800661{
662 struct acpi_thermal *tz = thermal->devdata;
663 int i;
664
665 if (!tz || trip < 0)
666 return -EINVAL;
667
668 if (tz->trips.critical.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000669 if (!trip) {
670 *type = THERMAL_TRIP_CRITICAL;
671 return 0;
672 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800673 trip--;
674 }
675
676 if (tz->trips.hot.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000677 if (!trip) {
678 *type = THERMAL_TRIP_HOT;
679 return 0;
680 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800681 trip--;
682 }
683
684 if (tz->trips.passive.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000685 if (!trip) {
686 *type = THERMAL_TRIP_PASSIVE;
687 return 0;
688 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800689 trip--;
690 }
691
692 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
693 tz->trips.active[i].flags.valid; i++) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000694 if (!trip) {
695 *type = THERMAL_TRIP_ACTIVE;
696 return 0;
697 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800698 trip--;
699 }
700
701 return -EINVAL;
702}
703
704static int thermal_get_trip_temp(struct thermal_zone_device *thermal,
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000705 int trip, unsigned long *temp)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800706{
707 struct acpi_thermal *tz = thermal->devdata;
708 int i;
709
710 if (!tz || trip < 0)
711 return -EINVAL;
712
713 if (tz->trips.critical.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000714 if (!trip) {
715 *temp = KELVIN_TO_MILLICELSIUS(
Jean Delvare13614e32009-04-06 16:01:46 +0200716 tz->trips.critical.temperature,
717 tz->kelvin_offset);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000718 return 0;
719 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800720 trip--;
721 }
722
723 if (tz->trips.hot.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000724 if (!trip) {
725 *temp = KELVIN_TO_MILLICELSIUS(
Jean Delvare13614e32009-04-06 16:01:46 +0200726 tz->trips.hot.temperature,
727 tz->kelvin_offset);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000728 return 0;
729 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800730 trip--;
731 }
732
733 if (tz->trips.passive.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000734 if (!trip) {
735 *temp = KELVIN_TO_MILLICELSIUS(
Jean Delvare13614e32009-04-06 16:01:46 +0200736 tz->trips.passive.temperature,
737 tz->kelvin_offset);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000738 return 0;
739 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800740 trip--;
741 }
742
743 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
744 tz->trips.active[i].flags.valid; i++) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000745 if (!trip) {
746 *temp = KELVIN_TO_MILLICELSIUS(
Jean Delvare13614e32009-04-06 16:01:46 +0200747 tz->trips.active[i].temperature,
748 tz->kelvin_offset);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000749 return 0;
750 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800751 trip--;
752 }
753
754 return -EINVAL;
755}
756
Zhang, Rui9ec732f2008-04-10 16:13:10 +0800757static int thermal_get_crit_temp(struct thermal_zone_device *thermal,
758 unsigned long *temperature) {
759 struct acpi_thermal *tz = thermal->devdata;
760
761 if (tz->trips.critical.flags.valid) {
762 *temperature = KELVIN_TO_MILLICELSIUS(
Jean Delvare13614e32009-04-06 16:01:46 +0200763 tz->trips.critical.temperature,
764 tz->kelvin_offset);
Zhang, Rui9ec732f2008-04-10 16:13:10 +0800765 return 0;
766 } else
767 return -EINVAL;
768}
769
Matthew Garrettb1569e92008-12-03 17:55:32 +0000770static int thermal_notify(struct thermal_zone_device *thermal, int trip,
771 enum thermal_trip_type trip_type)
772{
773 u8 type = 0;
774 struct acpi_thermal *tz = thermal->devdata;
775
776 if (trip_type == THERMAL_TRIP_CRITICAL)
777 type = ACPI_THERMAL_NOTIFY_CRITICAL;
778 else if (trip_type == THERMAL_TRIP_HOT)
779 type = ACPI_THERMAL_NOTIFY_HOT;
780 else
781 return 0;
782
783 acpi_bus_generate_proc_event(tz->device, type, 1);
784 acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
Stephen Rothwellf6f5c452009-03-03 12:47:17 +1100785 dev_name(&tz->device->dev), type, 1);
Matthew Garrettb1569e92008-12-03 17:55:32 +0000786
787 if (trip_type == THERMAL_TRIP_CRITICAL && nocrt)
788 return 1;
789
790 return 0;
791}
792
Zhang Rui3f655ef2008-01-17 15:51:11 +0800793typedef int (*cb)(struct thermal_zone_device *, int,
794 struct thermal_cooling_device *);
795static int acpi_thermal_cooling_device_cb(struct thermal_zone_device *thermal,
796 struct thermal_cooling_device *cdev,
797 cb action)
798{
799 struct acpi_device *device = cdev->devdata;
800 struct acpi_thermal *tz = thermal->devdata;
Zhang Rui653a00c2008-01-17 15:51:18 +0800801 struct acpi_device *dev;
802 acpi_status status;
803 acpi_handle handle;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800804 int i;
805 int j;
806 int trip = -1;
807 int result = 0;
808
809 if (tz->trips.critical.flags.valid)
810 trip++;
811
812 if (tz->trips.hot.flags.valid)
813 trip++;
814
815 if (tz->trips.passive.flags.valid) {
816 trip++;
817 for (i = 0; i < tz->trips.passive.devices.count;
818 i++) {
Zhang Rui653a00c2008-01-17 15:51:18 +0800819 handle = tz->trips.passive.devices.handles[i];
820 status = acpi_bus_get_device(handle, &dev);
821 if (ACPI_SUCCESS(status) && (dev == device)) {
822 result = action(thermal, trip, cdev);
823 if (result)
824 goto failed;
825 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800826 }
827 }
828
829 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
830 if (!tz->trips.active[i].flags.valid)
831 break;
832 trip++;
833 for (j = 0;
834 j < tz->trips.active[i].devices.count;
835 j++) {
Zhang Rui653a00c2008-01-17 15:51:18 +0800836 handle = tz->trips.active[i].devices.handles[j];
837 status = acpi_bus_get_device(handle, &dev);
838 if (ACPI_SUCCESS(status) && (dev == device)) {
839 result = action(thermal, trip, cdev);
840 if (result)
841 goto failed;
842 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800843 }
844 }
845
846 for (i = 0; i < tz->devices.count; i++) {
Zhang Rui653a00c2008-01-17 15:51:18 +0800847 handle = tz->devices.handles[i];
848 status = acpi_bus_get_device(handle, &dev);
849 if (ACPI_SUCCESS(status) && (dev == device)) {
850 result = action(thermal, -1, cdev);
851 if (result)
852 goto failed;
853 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800854 }
855
856failed:
857 return result;
858}
859
860static int
861acpi_thermal_bind_cooling_device(struct thermal_zone_device *thermal,
862 struct thermal_cooling_device *cdev)
863{
864 return acpi_thermal_cooling_device_cb(thermal, cdev,
865 thermal_zone_bind_cooling_device);
866}
867
868static int
869acpi_thermal_unbind_cooling_device(struct thermal_zone_device *thermal,
870 struct thermal_cooling_device *cdev)
871{
872 return acpi_thermal_cooling_device_cb(thermal, cdev,
873 thermal_zone_unbind_cooling_device);
874}
875
876static struct thermal_zone_device_ops acpi_thermal_zone_ops = {
877 .bind = acpi_thermal_bind_cooling_device,
878 .unbind = acpi_thermal_unbind_cooling_device,
879 .get_temp = thermal_get_temp,
880 .get_mode = thermal_get_mode,
881 .set_mode = thermal_set_mode,
882 .get_trip_type = thermal_get_trip_type,
883 .get_trip_temp = thermal_get_trip_temp,
Zhang, Rui9ec732f2008-04-10 16:13:10 +0800884 .get_crit_temp = thermal_get_crit_temp,
Matthew Garrettb1569e92008-12-03 17:55:32 +0000885 .notify = thermal_notify,
Zhang Rui3f655ef2008-01-17 15:51:11 +0800886};
887
888static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz)
889{
890 int trips = 0;
891 int result;
Zhang Rui20733932008-01-17 15:51:21 +0800892 acpi_status status;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800893 int i;
894
895 if (tz->trips.critical.flags.valid)
896 trips++;
897
898 if (tz->trips.hot.flags.valid)
899 trips++;
900
901 if (tz->trips.passive.flags.valid)
902 trips++;
903
904 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
905 tz->trips.active[i].flags.valid; i++, trips++);
Matthew Garrettb1569e92008-12-03 17:55:32 +0000906
907 if (tz->trips.passive.flags.valid)
908 tz->thermal_zone =
909 thermal_zone_device_register("acpitz", trips, tz,
910 &acpi_thermal_zone_ops,
911 tz->trips.passive.tc1,
912 tz->trips.passive.tc2,
913 tz->trips.passive.tsp*100,
914 tz->polling_frequency*100);
915 else
916 tz->thermal_zone =
917 thermal_zone_device_register("acpitz", trips, tz,
918 &acpi_thermal_zone_ops,
919 0, 0, 0,
Matthew Garrett67405432009-04-14 20:16:45 +0100920 tz->polling_frequency*100);
Krzysztof Heltbb070e42008-04-08 17:41:52 -0700921 if (IS_ERR(tz->thermal_zone))
Zhang Rui3f655ef2008-01-17 15:51:11 +0800922 return -ENODEV;
923
924 result = sysfs_create_link(&tz->device->dev.kobj,
925 &tz->thermal_zone->device.kobj, "thermal_zone");
926 if (result)
927 return result;
928
929 result = sysfs_create_link(&tz->thermal_zone->device.kobj,
930 &tz->device->dev.kobj, "device");
931 if (result)
932 return result;
933
Zhang Rui20733932008-01-17 15:51:21 +0800934 status = acpi_attach_data(tz->device->handle,
935 acpi_bus_private_data_handler,
936 tz->thermal_zone);
937 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800938 printk(KERN_ERR PREFIX
939 "Error attaching device data\n");
Zhang Rui20733932008-01-17 15:51:21 +0800940 return -ENODEV;
941 }
942
Zhang Rui3f655ef2008-01-17 15:51:11 +0800943 tz->tz_enabled = 1;
944
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200945 dev_info(&tz->device->dev, "registered as thermal_zone%d\n",
946 tz->thermal_zone->id);
Zhang Rui3f655ef2008-01-17 15:51:11 +0800947 return 0;
948}
949
950static void acpi_thermal_unregister_thermal_zone(struct acpi_thermal *tz)
951{
952 sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
953 sysfs_remove_link(&tz->thermal_zone->device.kobj, "device");
954 thermal_zone_device_unregister(tz->thermal_zone);
955 tz->thermal_zone = NULL;
Zhang Rui20733932008-01-17 15:51:21 +0800956 acpi_detach_data(tz->device->handle, acpi_bus_private_data_handler);
Zhang Rui3f655ef2008-01-17 15:51:11 +0800957}
958
959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960/* --------------------------------------------------------------------------
961 FS Interface (/proc)
962 -------------------------------------------------------------------------- */
Zhang Rui43d9f872010-07-15 10:46:44 +0800963#ifdef CONFIG_ACPI_PROCFS
Len Brown4be44fc2005-08-05 00:44:28 -0400964static struct proc_dir_entry *acpi_thermal_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
967{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200968 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 if (!tz)
972 goto end;
973
974 seq_puts(seq, "state: ");
975
Len Brown4be44fc2005-08-05 00:44:28 -0400976 if (!tz->state.critical && !tz->state.hot && !tz->state.passive
977 && !tz->state.active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 seq_puts(seq, "ok\n");
979 else {
980 if (tz->state.critical)
981 seq_puts(seq, "critical ");
982 if (tz->state.hot)
983 seq_puts(seq, "hot ");
984 if (tz->state.passive)
985 seq_puts(seq, "passive ");
986 if (tz->state.active)
987 seq_printf(seq, "active[%d]", tz->state.active_index);
988 seq_puts(seq, "\n");
989 }
990
Len Brown4be44fc2005-08-05 00:44:28 -0400991 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400992 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993}
994
995static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
996{
997 return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
998}
999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
1001{
Len Brown4be44fc2005-08-05 00:44:28 -04001002 int result = 0;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001003 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 if (!tz)
1007 goto end;
1008
1009 result = acpi_thermal_get_temperature(tz);
1010 if (result)
1011 goto end;
1012
Len Brown4be44fc2005-08-05 00:44:28 -04001013 seq_printf(seq, "temperature: %ld C\n",
1014 KELVIN_TO_CELSIUS(tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Len Brown4be44fc2005-08-05 00:44:28 -04001016 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001017 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
1019
1020static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
1021{
1022 return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
1023}
1024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
1026{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001027 struct acpi_thermal *tz = seq->private;
Thomas Renninger68ccfaa2006-11-19 23:01:40 +01001028 struct acpi_device *device;
Thomas Renningere7c746e2007-06-18 00:40:51 -04001029 acpi_status status;
1030
Len Brown4be44fc2005-08-05 00:44:28 -04001031 int i = 0;
1032 int j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 if (!tz)
1036 goto end;
1037
1038 if (tz->trips.critical.flags.valid)
Len Brownf5487142007-08-12 00:12:44 -04001039 seq_printf(seq, "critical (S5): %ld C%s",
1040 KELVIN_TO_CELSIUS(tz->trips.critical.temperature),
1041 nocrt ? " <disabled>\n" : "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 if (tz->trips.hot.flags.valid)
Len Brownf5487142007-08-12 00:12:44 -04001044 seq_printf(seq, "hot (S4): %ld C%s",
1045 KELVIN_TO_CELSIUS(tz->trips.hot.temperature),
1046 nocrt ? " <disabled>\n" : "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048 if (tz->trips.passive.flags.valid) {
Len Brown4be44fc2005-08-05 00:44:28 -04001049 seq_printf(seq,
1050 "passive: %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
1051 KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
1052 tz->trips.passive.tc1, tz->trips.passive.tc2,
1053 tz->trips.passive.tsp);
1054 for (j = 0; j < tz->trips.passive.devices.count; j++) {
Thomas Renningere7c746e2007-06-18 00:40:51 -04001055 status = acpi_bus_get_device(tz->trips.passive.devices.
1056 handles[j], &device);
1057 seq_printf(seq, "%4.4s ", status ? "" :
1058 acpi_device_bid(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
1060 seq_puts(seq, "\n");
Frans Pop7fb26162009-10-26 08:39:01 +01001061 } else {
1062 seq_printf(seq, "passive (forced):");
1063 if (tz->thermal_zone->forced_passive)
1064 seq_printf(seq, " %i C\n",
1065 tz->thermal_zone->forced_passive / 1000);
1066 else
1067 seq_printf(seq, "<not set>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 }
1069
1070 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1071 if (!(tz->trips.active[i].flags.valid))
1072 break;
1073 seq_printf(seq, "active[%d]: %ld C: devices=",
Len Brown4be44fc2005-08-05 00:44:28 -04001074 i,
1075 KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
Thomas Renninger68ccfaa2006-11-19 23:01:40 +01001076 for (j = 0; j < tz->trips.active[i].devices.count; j++){
Thomas Renningere7c746e2007-06-18 00:40:51 -04001077 status = acpi_bus_get_device(tz->trips.active[i].
1078 devices.handles[j],
1079 &device);
1080 seq_printf(seq, "%4.4s ", status ? "" :
1081 acpi_device_bid(device));
Thomas Renninger68ccfaa2006-11-19 23:01:40 +01001082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 seq_puts(seq, "\n");
1084 }
1085
Len Brown4be44fc2005-08-05 00:44:28 -04001086 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001087 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088}
1089
1090static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
1091{
1092 return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
1093}
1094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
1096{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001097 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 if (!tz)
1101 goto end;
1102
Len Browneaca2d32007-04-30 23:27:43 -04001103 if (!tz->flags.cooling_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 seq_puts(seq, "<setting not supported>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 else
Len Browneaca2d32007-04-30 23:27:43 -04001106 seq_puts(seq, "0 - Active; 1 - Passive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Len Brown4be44fc2005-08-05 00:44:28 -04001108 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001109 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110}
1111
1112static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
1113{
1114 return single_open(file, acpi_thermal_cooling_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -04001115 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116}
1117
1118static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001119acpi_thermal_write_cooling_mode(struct file *file,
1120 const char __user * buffer,
1121 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001123 struct seq_file *m = file->private_data;
1124 struct acpi_thermal *tz = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001125 int result = 0;
1126 char mode_string[12] = { '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129 if (!tz || (count > sizeof(mode_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -04001130 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 if (!tz->flags.cooling_mode)
Patrick Mocheld550d982006-06-27 00:41:40 -04001133 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135 if (copy_from_user(mode_string, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001136 return -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -04001137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 mode_string[count] = '\0';
Len Brown4be44fc2005-08-05 00:44:28 -04001139
1140 result = acpi_thermal_set_cooling_mode(tz,
1141 simple_strtoul(mode_string, NULL,
1142 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001144 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 acpi_thermal_check(tz);
1147
Patrick Mocheld550d982006-06-27 00:41:40 -04001148 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149}
1150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
1152{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001153 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 if (!tz)
1157 goto end;
1158
Matthew Garrettb1569e92008-12-03 17:55:32 +00001159 if (!tz->thermal_zone->polling_delay) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 seq_puts(seq, "<polling disabled>\n");
1161 goto end;
1162 }
1163
Matthew Garrettb1569e92008-12-03 17:55:32 +00001164 seq_printf(seq, "polling frequency: %d seconds\n",
1165 (tz->thermal_zone->polling_delay / 1000));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Len Brown4be44fc2005-08-05 00:44:28 -04001167 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001168 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169}
1170
1171static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
1172{
1173 return single_open(file, acpi_thermal_polling_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -04001174 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175}
1176
Zhang Rui43d9f872010-07-15 10:46:44 +08001177static int acpi_thermal_set_polling(struct acpi_thermal *tz, int seconds)
1178{
1179 if (!tz)
1180 return -EINVAL;
1181
1182 /* Convert value to deci-seconds */
1183 tz->polling_frequency = seconds * 10;
1184
1185 tz->thermal_zone->polling_delay = seconds * 1000;
1186
1187 if (tz->tz_enabled)
1188 thermal_zone_device_update(tz->thermal_zone);
1189
1190 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1191 "Polling frequency set to %lu seconds\n",
1192 tz->polling_frequency/10));
1193
1194 return 0;
1195}
1196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001198acpi_thermal_write_polling(struct file *file,
1199 const char __user * buffer,
1200 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001202 struct seq_file *m = file->private_data;
1203 struct acpi_thermal *tz = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001204 int result = 0;
1205 char polling_string[12] = { '\0' };
1206 int seconds = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 if (!tz || (count > sizeof(polling_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -04001210 return -EINVAL;
Len Brown4be44fc2005-08-05 00:44:28 -04001211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 if (copy_from_user(polling_string, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001213 return -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -04001214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 polling_string[count] = '\0';
1216
1217 seconds = simple_strtoul(polling_string, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 result = acpi_thermal_set_polling(tz, seconds);
1220 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001221 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223 acpi_thermal_check(tz);
1224
Patrick Mocheld550d982006-06-27 00:41:40 -04001225 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226}
1227
Len Brown4be44fc2005-08-05 00:44:28 -04001228static int acpi_thermal_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
Len Brown4be44fc2005-08-05 00:44:28 -04001230 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233 if (!acpi_device_dir(device)) {
1234 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -04001235 acpi_thermal_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001237 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 }
1239
1240 /* 'state' [R] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001241 entry = proc_create_data(ACPI_THERMAL_FILE_STATE,
1242 S_IRUGO, acpi_device_dir(device),
1243 &acpi_thermal_state_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 /* 'temperature' [R] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001249 entry = proc_create_data(ACPI_THERMAL_FILE_TEMPERATURE,
1250 S_IRUGO, acpi_device_dir(device),
1251 &acpi_thermal_temp_fops,
1252 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001254 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Pavel Machek2db9ccb2007-08-24 11:45:50 +02001256 /* 'trip_points' [R] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001257 entry = proc_create_data(ACPI_THERMAL_FILE_TRIP_POINTS,
1258 S_IRUGO,
1259 acpi_device_dir(device),
1260 &acpi_thermal_trip_fops,
1261 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001263 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265 /* 'cooling_mode' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001266 entry = proc_create_data(ACPI_THERMAL_FILE_COOLING_MODE,
1267 S_IFREG | S_IRUGO | S_IWUSR,
1268 acpi_device_dir(device),
1269 &acpi_thermal_cooling_fops,
1270 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001272 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 /* 'polling_frequency' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001275 entry = proc_create_data(ACPI_THERMAL_FILE_POLLING_FREQ,
1276 S_IFREG | S_IRUGO | S_IWUSR,
1277 acpi_device_dir(device),
1278 &acpi_thermal_polling_fops,
1279 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001281 return -ENODEV;
Patrick Mocheld550d982006-06-27 00:41:40 -04001282 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283}
1284
Len Brown4be44fc2005-08-05 00:44:28 -04001285static int acpi_thermal_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 if (acpi_device_dir(device)) {
1289 remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1290 acpi_device_dir(device));
1291 remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1292 acpi_device_dir(device));
1293 remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1294 acpi_device_dir(device));
1295 remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1296 acpi_device_dir(device));
1297 remove_proc_entry(ACPI_THERMAL_FILE_STATE,
1298 acpi_device_dir(device));
1299 remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
1300 acpi_device_dir(device) = NULL;
1301 }
1302
Patrick Mocheld550d982006-06-27 00:41:40 -04001303 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304}
Zhang Rui43d9f872010-07-15 10:46:44 +08001305#else
1306static inline int acpi_thermal_add_fs(struct acpi_device *device) { return 0; }
1307static inline int acpi_thermal_remove_fs(struct acpi_device *device)
1308{
1309 return 0;
1310}
1311#endif /* CONFIG_ACPI_PROCFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312/* --------------------------------------------------------------------------
1313 Driver Interface
1314 -------------------------------------------------------------------------- */
1315
Bjorn Helgaas342d5502009-04-07 15:37:06 +00001316static void acpi_thermal_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
Bjorn Helgaas342d5502009-04-07 15:37:06 +00001318 struct acpi_thermal *tz = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001322 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 switch (event) {
1325 case ACPI_THERMAL_NOTIFY_TEMPERATURE:
1326 acpi_thermal_check(tz);
1327 break;
1328 case ACPI_THERMAL_NOTIFY_THRESHOLDS:
Zhang Ruice44e192008-01-17 15:51:25 +08001329 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_THRESHOLDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 acpi_thermal_check(tz);
Len Brown14e04fb2007-08-23 15:20:26 -04001331 acpi_bus_generate_proc_event(device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08001332 acpi_bus_generate_netlink_event(device->pnp.device_class,
Kay Sievers07944692008-10-30 01:18:59 +01001333 dev_name(&device->dev), event, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 break;
1335 case ACPI_THERMAL_NOTIFY_DEVICES:
Zhang Ruice44e192008-01-17 15:51:25 +08001336 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_DEVICES);
1337 acpi_thermal_check(tz);
Len Brown14e04fb2007-08-23 15:20:26 -04001338 acpi_bus_generate_proc_event(device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08001339 acpi_bus_generate_netlink_event(device->pnp.device_class,
Kay Sievers07944692008-10-30 01:18:59 +01001340 dev_name(&device->dev), event, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 break;
1342 default:
1343 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001344 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 break;
1346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347}
1348
Len Brown4be44fc2005-08-05 00:44:28 -04001349static int acpi_thermal_get_info(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
Len Brown4be44fc2005-08-05 00:44:28 -04001351 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001355 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 /* Get temperature [_TMP] (required) */
1358 result = acpi_thermal_get_temperature(tz);
1359 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001360 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362 /* Get trip points [_CRT, _PSV, etc.] (required) */
1363 result = acpi_thermal_get_trip_points(tz);
1364 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001365 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367 /* Set the cooling mode [_SCP] to active cooling (default) */
1368 result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
Len Brown4be44fc2005-08-05 00:44:28 -04001369 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 tz->flags.cooling_mode = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 /* Get default polling frequency [_TZP] (optional) */
1373 if (tzp)
1374 tz->polling_frequency = tzp;
1375 else
1376 acpi_thermal_get_polling_frequency(tz);
1377
Patrick Mocheld550d982006-06-27 00:41:40 -04001378 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379}
1380
Jean Delvare13614e32009-04-06 16:01:46 +02001381/*
1382 * The exact offset between Kelvin and degree Celsius is 273.15. However ACPI
1383 * handles temperature values with a single decimal place. As a consequence,
1384 * some implementations use an offset of 273.1 and others use an offset of
1385 * 273.2. Try to find out which one is being used, to present the most
1386 * accurate and visually appealing number.
1387 *
1388 * The heuristic below should work for all ACPI thermal zones which have a
1389 * critical trip point with a value being a multiple of 0.5 degree Celsius.
1390 */
1391static void acpi_thermal_guess_offset(struct acpi_thermal *tz)
1392{
1393 if (tz->trips.critical.flags.valid &&
1394 (tz->trips.critical.temperature % 5) == 1)
1395 tz->kelvin_offset = 2731;
1396 else
1397 tz->kelvin_offset = 2732;
1398}
1399
Len Brown4be44fc2005-08-05 00:44:28 -04001400static int acpi_thermal_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
Len Brown4be44fc2005-08-05 00:44:28 -04001402 int result = 0;
Len Brown4be44fc2005-08-05 00:44:28 -04001403 struct acpi_thermal *tz = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001407 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Burman Yan36bcbec2006-12-19 12:56:11 -08001409 tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001411 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Patrick Mochel8348e1b2006-05-19 16:54:40 -04001413 tz->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 strcpy(tz->name, device->pnp.bus_id);
1415 strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1416 strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001417 device->driver_data = tz;
Alexey Starikovskiy6e215782007-09-01 00:11:59 +04001418 mutex_init(&tz->lock);
Zhang Rui3f655ef2008-01-17 15:51:11 +08001419
1420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 result = acpi_thermal_get_info(tz);
1422 if (result)
Zhang Rui3f655ef2008-01-17 15:51:11 +08001423 goto free_memory;
1424
Jean Delvare13614e32009-04-06 16:01:46 +02001425 acpi_thermal_guess_offset(tz);
1426
Zhang Rui3f655ef2008-01-17 15:51:11 +08001427 result = acpi_thermal_register_thermal_zone(tz);
1428 if (result)
1429 goto free_memory;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
1431 result = acpi_thermal_add_fs(device);
1432 if (result)
Zhang Rui3f655ef2008-01-17 15:51:11 +08001433 goto unregister_thermal_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001436 acpi_device_name(device), acpi_device_bid(device),
1437 KELVIN_TO_CELSIUS(tz->temperature));
Zhang Rui3f655ef2008-01-17 15:51:11 +08001438 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Zhang Rui3f655ef2008-01-17 15:51:11 +08001440unregister_thermal_zone:
1441 thermal_zone_device_unregister(tz->thermal_zone);
1442free_memory:
1443 kfree(tz);
1444end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001445 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446}
1447
Len Brown4be44fc2005-08-05 00:44:28 -04001448static int acpi_thermal_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449{
Len Brown4be44fc2005-08-05 00:44:28 -04001450 struct acpi_thermal *tz = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001453 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001455 tz = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 acpi_thermal_remove_fs(device);
Zhang Rui3f655ef2008-01-17 15:51:11 +08001458 acpi_thermal_unregister_thermal_zone(tz);
Alexey Starikovskiy6e215782007-09-01 00:11:59 +04001459 mutex_destroy(&tz->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 kfree(tz);
Patrick Mocheld550d982006-06-27 00:41:40 -04001461 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462}
1463
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001464static int acpi_thermal_resume(struct acpi_device *device)
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001465{
1466 struct acpi_thermal *tz = NULL;
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001467 int i, j, power_state, result;
1468
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001469
1470 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001471 return -EINVAL;
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001472
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001473 tz = acpi_driver_data(device);
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001474
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001475 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001476 if (!(&tz->trips.active[i]))
1477 break;
1478 if (!tz->trips.active[i].flags.valid)
1479 break;
1480 tz->trips.active[i].flags.enabled = 1;
1481 for (j = 0; j < tz->trips.active[i].devices.count; j++) {
1482 result = acpi_bus_get_power(tz->trips.active[i].devices.
1483 handles[j], &power_state);
1484 if (result || (power_state != ACPI_STATE_D0)) {
1485 tz->trips.active[i].flags.enabled = 0;
1486 break;
1487 }
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001488 }
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001489 tz->state.active |= tz->trips.active[i].flags.enabled;
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001490 }
1491
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001492 acpi_thermal_check(tz);
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001493
1494 return AE_OK;
1495}
1496
Jeff Garzik18552562007-10-03 15:15:40 -04001497static int thermal_act(const struct dmi_system_id *d) {
Len Brown0b5bfa12007-08-12 00:13:02 -04001498
1499 if (act == 0) {
1500 printk(KERN_NOTICE "ACPI: %s detected: "
1501 "disabling all active thermal trip points\n", d->ident);
1502 act = -1;
1503 }
1504 return 0;
1505}
Jeff Garzik18552562007-10-03 15:15:40 -04001506static int thermal_nocrt(const struct dmi_system_id *d) {
Len Brown8c99fdc2007-08-20 18:46:50 -04001507
1508 printk(KERN_NOTICE "ACPI: %s detected: "
1509 "disabling all critical thermal trip point actions.\n", d->ident);
1510 nocrt = 1;
1511 return 0;
1512}
Jeff Garzik18552562007-10-03 15:15:40 -04001513static int thermal_tzp(const struct dmi_system_id *d) {
Len Brown0b5bfa12007-08-12 00:13:02 -04001514
1515 if (tzp == 0) {
1516 printk(KERN_NOTICE "ACPI: %s detected: "
1517 "enabling thermal zone polling\n", d->ident);
1518 tzp = 300; /* 300 dS = 30 Seconds */
1519 }
1520 return 0;
1521}
Jeff Garzik18552562007-10-03 15:15:40 -04001522static int thermal_psv(const struct dmi_system_id *d) {
Len Brown0b5bfa12007-08-12 00:13:02 -04001523
1524 if (psv == 0) {
1525 printk(KERN_NOTICE "ACPI: %s detected: "
1526 "disabling all passive thermal trip points\n", d->ident);
1527 psv = -1;
1528 }
1529 return 0;
1530}
1531
1532static struct dmi_system_id thermal_dmi_table[] __initdata = {
1533 /*
1534 * Award BIOS on this AOpen makes thermal control almost worthless.
1535 * http://bugzilla.kernel.org/show_bug.cgi?id=8842
1536 */
1537 {
1538 .callback = thermal_act,
1539 .ident = "AOpen i915GMm-HFS",
1540 .matches = {
1541 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1542 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1543 },
1544 },
1545 {
1546 .callback = thermal_psv,
1547 .ident = "AOpen i915GMm-HFS",
1548 .matches = {
1549 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1550 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1551 },
1552 },
1553 {
1554 .callback = thermal_tzp,
1555 .ident = "AOpen i915GMm-HFS",
1556 .matches = {
1557 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1558 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1559 },
1560 },
Len Brown8c99fdc2007-08-20 18:46:50 -04001561 {
1562 .callback = thermal_nocrt,
1563 .ident = "Gigabyte GA-7ZX",
1564 .matches = {
1565 DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
1566 DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
1567 },
1568 },
Len Brown0b5bfa12007-08-12 00:13:02 -04001569 {}
1570};
Len Brown0b5bfa12007-08-12 00:13:02 -04001571
Len Brown4be44fc2005-08-05 00:44:28 -04001572static int __init acpi_thermal_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573{
Len Brown4be44fc2005-08-05 00:44:28 -04001574 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Len Brown0b5bfa12007-08-12 00:13:02 -04001576 dmi_check_system(thermal_dmi_table);
1577
Len Brown72b33ef2007-08-12 00:12:17 -04001578 if (off) {
1579 printk(KERN_NOTICE "ACPI: thermal control disabled\n");
1580 return -ENODEV;
1581 }
Zhang Rui43d9f872010-07-15 10:46:44 +08001582
1583#ifdef CONFIG_ACPI_PROCFS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
1585 if (!acpi_thermal_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001586 return -ENODEV;
Zhang Rui43d9f872010-07-15 10:46:44 +08001587#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
1589 result = acpi_bus_register_driver(&acpi_thermal_driver);
1590 if (result < 0) {
Zhang Rui43d9f872010-07-15 10:46:44 +08001591#ifdef CONFIG_ACPI_PROCFS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
Zhang Rui43d9f872010-07-15 10:46:44 +08001593#endif
Patrick Mocheld550d982006-06-27 00:41:40 -04001594 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 }
1596
Patrick Mocheld550d982006-06-27 00:41:40 -04001597 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598}
1599
Len Brown4be44fc2005-08-05 00:44:28 -04001600static void __exit acpi_thermal_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
1603 acpi_bus_unregister_driver(&acpi_thermal_driver);
1604
Zhang Rui43d9f872010-07-15 10:46:44 +08001605#ifdef CONFIG_ACPI_PROCFS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
Zhang Rui43d9f872010-07-15 10:46:44 +08001607#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Patrick Mocheld550d982006-06-27 00:41:40 -04001609 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610}
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612module_init(acpi_thermal_init);
1613module_exit(acpi_thermal_exit);