blob: 00f12a86ecbd42fc5ca401bef64b2a03d8249c7f [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * acpi_thermal.c - ACPI Thermal Zone Driver ($Revision: 41 $)
4 *
5 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
6 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * This driver fully implements the ACPI thermal policy as described in the
9 * ACPI 2.0 Specification.
10 *
11 * TBD: 1. Implement passive cooling hysteresis.
12 * 2. Enhance passive cooling (CPU) states/limit interface to support
13 * concepts of 'multiple limiters', upper/lower limits, etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
16#include <linux/kernel.h>
17#include <linux/module.h>
Len Brown0b5bfa12007-08-12 00:13:02 -040018#include <linux/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/types.h>
Tim Schmielaucd354f12007-02-14 00:33:14 -080022#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/kmod.h>
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -070024#include <linux/reboot.h>
Stephen Rothwellf6f5c452009-03-03 12:47:17 +110025#include <linux/device.h>
Zhang Rui3f655ef2008-01-17 15:51:11 +080026#include <linux/thermal.h>
Lv Zheng8b484632013-12-03 08:49:16 +080027#include <linux/acpi.h>
Aaron Lua59ffb22014-03-04 14:24:46 +080028#include <linux/workqueue.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080029#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Len Browna192a952009-07-28 16:45:54 -040031#define PREFIX "ACPI: "
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#define ACPI_THERMAL_CLASS "thermal_zone"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#define ACPI_THERMAL_DEVICE_NAME "Thermal Zone"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#define ACPI_THERMAL_NOTIFY_TEMPERATURE 0x80
36#define ACPI_THERMAL_NOTIFY_THRESHOLDS 0x81
37#define ACPI_THERMAL_NOTIFY_DEVICES 0x82
38#define ACPI_THERMAL_NOTIFY_CRITICAL 0xF0
39#define ACPI_THERMAL_NOTIFY_HOT 0xF1
40#define ACPI_THERMAL_MODE_ACTIVE 0x00
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#define ACPI_THERMAL_MAX_ACTIVE 10
43#define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#define _COMPONENT ACPI_THERMAL_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050046ACPI_MODULE_NAME("thermal");
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Thomas Renninger1cbf4c52004-09-16 11:07:00 -040048MODULE_AUTHOR("Paul Diefenbaugh");
Len Brown7cda93e2007-02-12 23:50:02 -050049MODULE_DESCRIPTION("ACPI Thermal Zone Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070050MODULE_LICENSE("GPL");
51
Len Brownf8707ec2007-08-12 00:12:54 -040052static int act;
53module_param(act, int, 0644);
Len Brown3c1d36d2007-08-14 15:12:56 -040054MODULE_PARM_DESC(act, "Disable or override all lowest active trip points.");
Len Brownf8707ec2007-08-12 00:12:54 -040055
Len Brownc52a7412007-08-14 15:49:32 -040056static int crt;
57module_param(crt, int, 0644);
58MODULE_PARM_DESC(crt, "Disable or lower all critical trip points.");
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static int tzp;
Len Brown730ff342007-08-12 00:12:26 -040061module_param(tzp, int, 0444);
Len Brown3c1d36d2007-08-14 15:12:56 -040062MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Len Brownf5487142007-08-12 00:12:44 -040064static int nocrt;
65module_param(nocrt, int, 0);
Len Brown8c99fdc2007-08-20 18:46:50 -040066MODULE_PARM_DESC(nocrt, "Set to take no action upon ACPI thermal zone critical trips points.");
Len Brownf5487142007-08-12 00:12:44 -040067
Len Brown72b33ef2007-08-12 00:12:17 -040068static int off;
69module_param(off, int, 0);
Len Brown3c1d36d2007-08-14 15:12:56 -040070MODULE_PARM_DESC(off, "Set to disable ACPI thermal support.");
Len Brown72b33ef2007-08-12 00:12:17 -040071
Len Browna70cdc52007-08-12 00:12:35 -040072static int psv;
73module_param(psv, int, 0644);
Len Brown3c1d36d2007-08-14 15:12:56 -040074MODULE_PARM_DESC(psv, "Disable or override all passive trip points.");
Len Browna70cdc52007-08-12 00:12:35 -040075
Aaron Lua59ffb22014-03-04 14:24:46 +080076static struct workqueue_struct *acpi_thermal_pm_queue;
77
Len Brown4be44fc2005-08-05 00:44:28 -040078static int acpi_thermal_add(struct acpi_device *device);
Rafael J. Wysocki51fac832013-01-24 00:24:48 +010079static int acpi_thermal_remove(struct acpi_device *device);
Bjorn Helgaas342d5502009-04-07 15:37:06 +000080static void acpi_thermal_notify(struct acpi_device *device, u32 event);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Thomas Renninger1ba90e32007-07-23 14:44:41 +020082static const struct acpi_device_id thermal_device_ids[] = {
83 {ACPI_THERMAL_HID, 0},
84 {"", 0},
85};
86MODULE_DEVICE_TABLE(acpi, thermal_device_ids);
87
Rafael J. Wysocki90692402012-08-09 23:00:02 +020088#ifdef CONFIG_PM_SLEEP
Aaron Lua59ffb22014-03-04 14:24:46 +080089static int acpi_thermal_suspend(struct device *dev);
Rafael J. Wysocki167cffb2012-06-27 23:26:18 +020090static int acpi_thermal_resume(struct device *dev);
Shuah Khanfae9e2a2014-02-12 20:19:10 -070091#else
Aaron Lua59ffb22014-03-04 14:24:46 +080092#define acpi_thermal_suspend NULL
Shuah Khanfae9e2a2014-02-12 20:19:10 -070093#define acpi_thermal_resume NULL
Rafael J. Wysocki90692402012-08-09 23:00:02 +020094#endif
Aaron Lua59ffb22014-03-04 14:24:46 +080095static SIMPLE_DEV_PM_OPS(acpi_thermal_pm, acpi_thermal_suspend, acpi_thermal_resume);
Rafael J. Wysocki167cffb2012-06-27 23:26:18 +020096
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static struct acpi_driver acpi_thermal_driver = {
Len Brownc2b67052007-02-12 23:33:40 -050098 .name = "thermal",
Len Brown4be44fc2005-08-05 00:44:28 -040099 .class = ACPI_THERMAL_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200100 .ids = thermal_device_ids,
Len Brown4be44fc2005-08-05 00:44:28 -0400101 .ops = {
102 .add = acpi_thermal_add,
103 .remove = acpi_thermal_remove,
Bjorn Helgaas342d5502009-04-07 15:37:06 +0000104 .notify = acpi_thermal_notify,
Len Brown4be44fc2005-08-05 00:44:28 -0400105 },
Rafael J. Wysocki167cffb2012-06-27 23:26:18 +0200106 .drv.pm = &acpi_thermal_pm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107};
108
109struct acpi_thermal_state {
Len Brown4be44fc2005-08-05 00:44:28 -0400110 u8 critical:1;
111 u8 hot:1;
112 u8 passive:1;
113 u8 active:1;
114 u8 reserved:4;
115 int active_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116};
117
118struct acpi_thermal_state_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400119 u8 valid:1;
120 u8 enabled:1;
121 u8 reserved:6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122};
123
124struct acpi_thermal_critical {
125 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400126 unsigned long temperature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127};
128
129struct acpi_thermal_hot {
130 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400131 unsigned long temperature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132};
133
134struct acpi_thermal_passive {
135 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400136 unsigned long temperature;
137 unsigned long tc1;
138 unsigned long tc2;
139 unsigned long tsp;
140 struct acpi_handle_list devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141};
142
143struct acpi_thermal_active {
144 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400145 unsigned long temperature;
146 struct acpi_handle_list devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147};
148
149struct acpi_thermal_trips {
150 struct acpi_thermal_critical critical;
Len Brown4be44fc2005-08-05 00:44:28 -0400151 struct acpi_thermal_hot hot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 struct acpi_thermal_passive passive;
153 struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
154};
155
156struct acpi_thermal_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400157 u8 cooling_mode:1; /* _SCP */
158 u8 devices:1; /* _TZD */
159 u8 reserved:6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160};
161
162struct acpi_thermal {
Patrick Mochel8348e1b2006-05-19 16:54:40 -0400163 struct acpi_device * device;
Len Brown4be44fc2005-08-05 00:44:28 -0400164 acpi_bus_id name;
165 unsigned long temperature;
166 unsigned long last_temperature;
167 unsigned long polling_frequency;
Len Brown4be44fc2005-08-05 00:44:28 -0400168 volatile u8 zombie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 struct acpi_thermal_flags flags;
170 struct acpi_thermal_state state;
171 struct acpi_thermal_trips trips;
Len Brown4be44fc2005-08-05 00:44:28 -0400172 struct acpi_handle_list devices;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800173 struct thermal_zone_device *thermal_zone;
174 int tz_enabled;
Jean Delvare13614e32009-04-06 16:01:46 +0200175 int kelvin_offset;
Aaron Lua59ffb22014-03-04 14:24:46 +0800176 struct work_struct thermal_check_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177};
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179/* --------------------------------------------------------------------------
180 Thermal Zone Management
181 -------------------------------------------------------------------------- */
182
Len Brown4be44fc2005-08-05 00:44:28 -0400183static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Len Brown4be44fc2005-08-05 00:44:28 -0400185 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400186 unsigned long long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400189 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 tz->last_temperature = tz->temperature;
192
Matthew Wilcox27663c52008-10-10 02:22:59 -0400193 status = acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400195 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Matthew Wilcox27663c52008-10-10 02:22:59 -0400197 tz->temperature = tmp;
Len Brown4be44fc2005-08-05 00:44:28 -0400198 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
199 tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Patrick Mocheld550d982006-06-27 00:41:40 -0400201 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
Len Brown4be44fc2005-08-05 00:44:28 -0400204static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Len Brown4be44fc2005-08-05 00:44:28 -0400206 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400207 unsigned long long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400210 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Matthew Wilcox27663c52008-10-10 02:22:59 -0400212 status = acpi_evaluate_integer(tz->device->handle, "_TZP", NULL, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400214 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Matthew Wilcox27663c52008-10-10 02:22:59 -0400216 tz->polling_frequency = tmp;
Len Brown4be44fc2005-08-05 00:44:28 -0400217 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
218 tz->polling_frequency));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Patrick Mocheld550d982006-06-27 00:41:40 -0400220 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
Len Brown4be44fc2005-08-05 00:44:28 -0400223static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400226 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Jiang Liu0db98202013-06-29 00:24:39 +0800228 if (!acpi_has_method(tz->device->handle, "_SCP")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400230 return -ENODEV;
Jiang Liu0db98202013-06-29 00:24:39 +0800231 } else if (ACPI_FAILURE(acpi_execute_simple_method(tz->device->handle,
232 "_SCP", mode))) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400233 return -ENODEV;
Jiang Liu0db98202013-06-29 00:24:39 +0800234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Patrick Mocheld550d982006-06-27 00:41:40 -0400236 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
Zhang Ruice44e192008-01-17 15:51:25 +0800239#define ACPI_TRIPS_CRITICAL 0x01
240#define ACPI_TRIPS_HOT 0x02
241#define ACPI_TRIPS_PASSIVE 0x04
242#define ACPI_TRIPS_ACTIVE 0x08
243#define ACPI_TRIPS_DEVICES 0x10
244
245#define ACPI_TRIPS_REFRESH_THRESHOLDS (ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE)
246#define ACPI_TRIPS_REFRESH_DEVICES ACPI_TRIPS_DEVICES
247
248#define ACPI_TRIPS_INIT (ACPI_TRIPS_CRITICAL | ACPI_TRIPS_HOT | \
249 ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE | \
250 ACPI_TRIPS_DEVICES)
251
252/*
253 * This exception is thrown out in two cases:
254 * 1.An invalid trip point becomes invalid or a valid trip point becomes invalid
255 * when re-evaluating the AML code.
256 * 2.TODO: Devices listed in _PSL, _ALx, _TZD may change.
257 * We need to re-bind the cooling devices of a thermal zone when this occurs.
258 */
259#define ACPI_THERMAL_TRIPS_EXCEPTION(flags, str) \
260do { \
261 if (flags != ACPI_TRIPS_INIT) \
262 ACPI_EXCEPTION((AE_INFO, AE_ERROR, \
263 "ACPI thermal trip point %s changed\n" \
Colin Ian King7e3cf242012-11-29 11:53:16 +0000264 "Please send acpidump to linux-acpi@vger.kernel.org", str)); \
Zhang Ruice44e192008-01-17 15:51:25 +0800265} while (0)
266
267static int acpi_thermal_trips_update(struct acpi_thermal *tz, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
Len Brown4be44fc2005-08-05 00:44:28 -0400269 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400270 unsigned long long tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800271 struct acpi_handle_list devices;
272 int valid = 0;
273 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Thomas Renningerfa809452010-02-20 11:44:27 +0100275 /* Critical Shutdown */
Zhang Ruice44e192008-01-17 15:51:25 +0800276 if (flag & ACPI_TRIPS_CRITICAL) {
277 status = acpi_evaluate_integer(tz->device->handle,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400278 "_CRT", NULL, &tmp);
279 tz->trips.critical.temperature = tmp;
Arjan van de Vena39a2d72008-05-19 15:55:15 -0700280 /*
281 * Treat freezing temperatures as invalid as well; some
282 * BIOSes return really low values and cause reboots at startup.
Adam Buchbinderb731d7b2009-03-13 12:15:26 -0400283 * Below zero (Celsius) values clearly aren't right for sure..
Arjan van de Vena39a2d72008-05-19 15:55:15 -0700284 * ... so lets discard those as invalid.
285 */
Thomas Renningerfa809452010-02-20 11:44:27 +0100286 if (ACPI_FAILURE(status)) {
Len Brownc52a7412007-08-14 15:49:32 -0400287 tz->trips.critical.flags.valid = 0;
Thomas Renningerfa809452010-02-20 11:44:27 +0100288 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
289 "No critical threshold\n"));
290 } else if (tmp <= 2732) {
Andy Shevchenko766a8a62013-09-13 18:15:46 +0300291 pr_warn(FW_BUG "Invalid critical threshold (%llu)\n",
292 tmp);
Thomas Renningerfa809452010-02-20 11:44:27 +0100293 tz->trips.critical.flags.valid = 0;
Zhang Ruice44e192008-01-17 15:51:25 +0800294 } else {
295 tz->trips.critical.flags.valid = 1;
296 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Thomas Renningerfa809452010-02-20 11:44:27 +0100297 "Found critical threshold [%lu]\n",
298 tz->trips.critical.temperature));
Zhang Ruice44e192008-01-17 15:51:25 +0800299 }
300 if (tz->trips.critical.flags.valid == 1) {
301 if (crt == -1) {
302 tz->trips.critical.flags.valid = 0;
303 } else if (crt > 0) {
Rasmus Villemoese866a2e2015-10-01 23:45:31 +0200304 unsigned long crt_k = CELSIUS_TO_DECI_KELVIN(crt);
Zhang Ruice44e192008-01-17 15:51:25 +0800305 /*
Zhang Rui22a94d72008-10-17 02:41:20 -0400306 * Allow override critical threshold
Zhang Ruice44e192008-01-17 15:51:25 +0800307 */
Zhang Rui22a94d72008-10-17 02:41:20 -0400308 if (crt_k > tz->trips.critical.temperature)
Andy Shevchenko766a8a62013-09-13 18:15:46 +0300309 pr_warn(PREFIX "Critical threshold %d C\n",
310 crt);
Zhang Rui22a94d72008-10-17 02:41:20 -0400311 tz->trips.critical.temperature = crt_k;
Zhang Ruice44e192008-01-17 15:51:25 +0800312 }
Len Brownc52a7412007-08-14 15:49:32 -0400313 }
314 }
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 /* Critical Sleep (optional) */
Zhang Ruice44e192008-01-17 15:51:25 +0800317 if (flag & ACPI_TRIPS_HOT) {
Len Browna70cdc52007-08-12 00:12:35 -0400318 status = acpi_evaluate_integer(tz->device->handle,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400319 "_HOT", NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800320 if (ACPI_FAILURE(status)) {
321 tz->trips.hot.flags.valid = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400322 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Zhang Ruice44e192008-01-17 15:51:25 +0800323 "No hot threshold\n"));
324 } else {
Matthew Wilcox27663c52008-10-10 02:22:59 -0400325 tz->trips.hot.temperature = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800326 tz->trips.hot.flags.valid = 1;
327 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
328 "Found hot threshold [%lu]\n",
Zhihui Zhangee17fdf2014-03-29 09:25:47 -0400329 tz->trips.hot.temperature));
Zhang Ruice44e192008-01-17 15:51:25 +0800330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
332
Zhang Ruice44e192008-01-17 15:51:25 +0800333 /* Passive (optional) */
Zhang Rui0e4240d2009-01-16 12:53:42 -0500334 if (((flag & ACPI_TRIPS_PASSIVE) && tz->trips.passive.flags.valid) ||
335 (flag == ACPI_TRIPS_INIT)) {
Zhang Ruice44e192008-01-17 15:51:25 +0800336 valid = tz->trips.passive.flags.valid;
337 if (psv == -1) {
338 status = AE_SUPPORT;
339 } else if (psv > 0) {
Rasmus Villemoese866a2e2015-10-01 23:45:31 +0200340 tmp = CELSIUS_TO_DECI_KELVIN(psv);
Zhang Ruice44e192008-01-17 15:51:25 +0800341 status = AE_OK;
342 } else {
343 status = acpi_evaluate_integer(tz->device->handle,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400344 "_PSV", NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Zhang Ruice44e192008-01-17 15:51:25 +0800347 if (ACPI_FAILURE(status))
348 tz->trips.passive.flags.valid = 0;
349 else {
Matthew Wilcox27663c52008-10-10 02:22:59 -0400350 tz->trips.passive.temperature = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800351 tz->trips.passive.flags.valid = 1;
352 if (flag == ACPI_TRIPS_INIT) {
353 status = acpi_evaluate_integer(
354 tz->device->handle, "_TC1",
Matthew Wilcox27663c52008-10-10 02:22:59 -0400355 NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800356 if (ACPI_FAILURE(status))
357 tz->trips.passive.flags.valid = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400358 else
359 tz->trips.passive.tc1 = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800360 status = acpi_evaluate_integer(
361 tz->device->handle, "_TC2",
Matthew Wilcox27663c52008-10-10 02:22:59 -0400362 NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800363 if (ACPI_FAILURE(status))
364 tz->trips.passive.flags.valid = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400365 else
366 tz->trips.passive.tc2 = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800367 status = acpi_evaluate_integer(
368 tz->device->handle, "_TSP",
Matthew Wilcox27663c52008-10-10 02:22:59 -0400369 NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800370 if (ACPI_FAILURE(status))
371 tz->trips.passive.flags.valid = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400372 else
373 tz->trips.passive.tsp = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800374 }
375 }
376 }
377 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.passive.flags.valid) {
378 memset(&devices, 0, sizeof(struct acpi_handle_list));
379 status = acpi_evaluate_reference(tz->device->handle, "_PSL",
380 NULL, &devices);
Zhang Rui0e4240d2009-01-16 12:53:42 -0500381 if (ACPI_FAILURE(status)) {
Andy Shevchenko766a8a62013-09-13 18:15:46 +0300382 pr_warn(PREFIX "Invalid passive threshold\n");
Zhang Ruice44e192008-01-17 15:51:25 +0800383 tz->trips.passive.flags.valid = 0;
Zhang Rui0e4240d2009-01-16 12:53:42 -0500384 }
Zhang Ruice44e192008-01-17 15:51:25 +0800385 else
386 tz->trips.passive.flags.valid = 1;
387
388 if (memcmp(&tz->trips.passive.devices, &devices,
389 sizeof(struct acpi_handle_list))) {
390 memcpy(&tz->trips.passive.devices, &devices,
391 sizeof(struct acpi_handle_list));
392 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
393 }
394 }
395 if ((flag & ACPI_TRIPS_PASSIVE) || (flag & ACPI_TRIPS_DEVICES)) {
396 if (valid != tz->trips.passive.flags.valid)
397 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
398 }
399
400 /* Active (optional) */
Len Brown4be44fc2005-08-05 00:44:28 -0400401 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Len Brown4be44fc2005-08-05 00:44:28 -0400402 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
Zhang Ruice44e192008-01-17 15:51:25 +0800403 valid = tz->trips.active[i].flags.valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Len Brownf8707ec2007-08-12 00:12:54 -0400405 if (act == -1)
Zhang Ruice44e192008-01-17 15:51:25 +0800406 break; /* disable all active trip points */
Len Brownf8707ec2007-08-12 00:12:54 -0400407
Zhang Rui0e4240d2009-01-16 12:53:42 -0500408 if ((flag == ACPI_TRIPS_INIT) || ((flag & ACPI_TRIPS_ACTIVE) &&
409 tz->trips.active[i].flags.valid)) {
Zhang Ruice44e192008-01-17 15:51:25 +0800410 status = acpi_evaluate_integer(tz->device->handle,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400411 name, NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800412 if (ACPI_FAILURE(status)) {
413 tz->trips.active[i].flags.valid = 0;
414 if (i == 0)
415 break;
416 if (act <= 0)
417 break;
418 if (i == 1)
419 tz->trips.active[0].temperature =
Rasmus Villemoese866a2e2015-10-01 23:45:31 +0200420 CELSIUS_TO_DECI_KELVIN(act);
Zhang Ruice44e192008-01-17 15:51:25 +0800421 else
422 /*
423 * Don't allow override higher than
424 * the next higher trip point
425 */
426 tz->trips.active[i - 1].temperature =
427 (tz->trips.active[i - 2].temperature <
Rasmus Villemoese866a2e2015-10-01 23:45:31 +0200428 CELSIUS_TO_DECI_KELVIN(act) ?
Zhang Ruice44e192008-01-17 15:51:25 +0800429 tz->trips.active[i - 2].temperature :
Rasmus Villemoese866a2e2015-10-01 23:45:31 +0200430 CELSIUS_TO_DECI_KELVIN(act));
Len Brownf8707ec2007-08-12 00:12:54 -0400431 break;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400432 } else {
433 tz->trips.active[i].temperature = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800434 tz->trips.active[i].flags.valid = 1;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400435 }
Len Brownf8707ec2007-08-12 00:12:54 -0400436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 name[2] = 'L';
Zhang Ruice44e192008-01-17 15:51:25 +0800439 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.active[i].flags.valid ) {
440 memset(&devices, 0, sizeof(struct acpi_handle_list));
441 status = acpi_evaluate_reference(tz->device->handle,
442 name, NULL, &devices);
Zhang Rui0e4240d2009-01-16 12:53:42 -0500443 if (ACPI_FAILURE(status)) {
Andy Shevchenko766a8a62013-09-13 18:15:46 +0300444 pr_warn(PREFIX "Invalid active%d threshold\n",
445 i);
Zhang Ruice44e192008-01-17 15:51:25 +0800446 tz->trips.active[i].flags.valid = 0;
Zhang Rui0e4240d2009-01-16 12:53:42 -0500447 }
Zhang Ruice44e192008-01-17 15:51:25 +0800448 else
449 tz->trips.active[i].flags.valid = 1;
450
451 if (memcmp(&tz->trips.active[i].devices, &devices,
452 sizeof(struct acpi_handle_list))) {
453 memcpy(&tz->trips.active[i].devices, &devices,
454 sizeof(struct acpi_handle_list));
455 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
456 }
457 }
458 if ((flag & ACPI_TRIPS_ACTIVE) || (flag & ACPI_TRIPS_DEVICES))
459 if (valid != tz->trips.active[i].flags.valid)
460 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
461
462 if (!tz->trips.active[i].flags.valid)
463 break;
464 }
465
Lan Tianyu668e0202013-08-27 16:29:31 +0800466 if ((flag & ACPI_TRIPS_DEVICES)
467 && acpi_has_method(tz->device->handle, "_TZD")) {
468 memset(&devices, 0, sizeof(devices));
Zhang Ruice44e192008-01-17 15:51:25 +0800469 status = acpi_evaluate_reference(tz->device->handle, "_TZD",
470 NULL, &devices);
Lan Tianyu668e0202013-08-27 16:29:31 +0800471 if (ACPI_SUCCESS(status)
472 && memcmp(&tz->devices, &devices, sizeof(devices))) {
473 tz->devices = devices;
Zhang Ruice44e192008-01-17 15:51:25 +0800474 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
477
Patrick Mocheld550d982006-06-27 00:41:40 -0400478 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
Zhang Ruice44e192008-01-17 15:51:25 +0800481static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Thomas Renninger8b7ef6d2010-02-16 22:55:51 +0100483 int i, valid, ret = acpi_thermal_trips_update(tz, ACPI_TRIPS_INIT);
484
485 if (ret)
486 return ret;
487
488 valid = tz->trips.critical.flags.valid |
489 tz->trips.hot.flags.valid |
490 tz->trips.passive.flags.valid;
491
492 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
493 valid |= tz->trips.active[i].flags.valid;
494
495 if (!valid) {
Andy Shevchenko766a8a62013-09-13 18:15:46 +0300496 pr_warn(FW_BUG "No valid trip found\n");
Thomas Renninger8b7ef6d2010-02-16 22:55:51 +0100497 return -ENODEV;
498 }
499 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
Len Brown4be44fc2005-08-05 00:44:28 -0400502static void acpi_thermal_check(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200504 struct acpi_thermal *tz = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Srinivas Pandruvadae994d712013-09-25 09:29:50 -0700506 if (!tz->tz_enabled)
Srinivas Pandruvadaca4e7132013-01-15 23:57:16 +0100507 return;
Srinivas Pandruvadae994d712013-09-25 09:29:50 -0700508
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700509 thermal_zone_device_update(tz->thermal_zone,
510 THERMAL_EVENT_UNSPECIFIED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
Zhang Rui3f655ef2008-01-17 15:51:11 +0800513/* sys I/F for generic thermal sysfs support */
Zhang, Rui5e012762008-02-28 07:51:30 +0800514
Sascha Hauer17e83512015-07-24 08:12:54 +0200515static int thermal_get_temp(struct thermal_zone_device *thermal, int *temp)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800516{
517 struct acpi_thermal *tz = thermal->devdata;
Zhang, Rui76ecb4f2008-04-10 16:20:23 +0800518 int result;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800519
520 if (!tz)
521 return -EINVAL;
522
Zhang, Rui76ecb4f2008-04-10 16:20:23 +0800523 result = acpi_thermal_get_temperature(tz);
524 if (result)
525 return result;
526
Aaron Lu7b83fd92014-03-25 10:40:09 +0800527 *temp = DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(tz->temperature,
528 tz->kelvin_offset);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000529 return 0;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800530}
531
Zhang Rui3f655ef2008-01-17 15:51:11 +0800532static int thermal_get_mode(struct thermal_zone_device *thermal,
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000533 enum thermal_device_mode *mode)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800534{
535 struct acpi_thermal *tz = thermal->devdata;
536
537 if (!tz)
538 return -EINVAL;
539
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000540 *mode = tz->tz_enabled ? THERMAL_DEVICE_ENABLED :
541 THERMAL_DEVICE_DISABLED;
542
543 return 0;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800544}
545
546static int thermal_set_mode(struct thermal_zone_device *thermal,
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000547 enum thermal_device_mode mode)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800548{
549 struct acpi_thermal *tz = thermal->devdata;
550 int enable;
551
552 if (!tz)
553 return -EINVAL;
554
555 /*
556 * enable/disable thermal management from ACPI thermal driver
557 */
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000558 if (mode == THERMAL_DEVICE_ENABLED)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800559 enable = 1;
Srinivas Pandruvadae994d712013-09-25 09:29:50 -0700560 else if (mode == THERMAL_DEVICE_DISABLED) {
Zhang Rui3f655ef2008-01-17 15:51:11 +0800561 enable = 0;
Srinivas Pandruvadae994d712013-09-25 09:29:50 -0700562 pr_warn("thermal zone will be disabled\n");
563 } else
Zhang Rui3f655ef2008-01-17 15:51:11 +0800564 return -EINVAL;
565
566 if (enable != tz->tz_enabled) {
567 tz->tz_enabled = enable;
568 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Zhang Rui8eaa8d62012-07-25 10:11:00 +0800569 "%s kernel ACPI thermal control\n",
570 tz->tz_enabled ? "Enable" : "Disable"));
Zhang Rui3f655ef2008-01-17 15:51:11 +0800571 acpi_thermal_check(tz);
572 }
573 return 0;
574}
575
576static int thermal_get_trip_type(struct thermal_zone_device *thermal,
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000577 int trip, enum thermal_trip_type *type)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800578{
579 struct acpi_thermal *tz = thermal->devdata;
580 int i;
581
582 if (!tz || trip < 0)
583 return -EINVAL;
584
585 if (tz->trips.critical.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000586 if (!trip) {
587 *type = THERMAL_TRIP_CRITICAL;
588 return 0;
589 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800590 trip--;
591 }
592
593 if (tz->trips.hot.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000594 if (!trip) {
595 *type = THERMAL_TRIP_HOT;
596 return 0;
597 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800598 trip--;
599 }
600
601 if (tz->trips.passive.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000602 if (!trip) {
603 *type = THERMAL_TRIP_PASSIVE;
604 return 0;
605 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800606 trip--;
607 }
608
609 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
610 tz->trips.active[i].flags.valid; i++) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000611 if (!trip) {
612 *type = THERMAL_TRIP_ACTIVE;
613 return 0;
614 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800615 trip--;
616 }
617
618 return -EINVAL;
619}
620
621static int thermal_get_trip_temp(struct thermal_zone_device *thermal,
Sascha Hauer17e83512015-07-24 08:12:54 +0200622 int trip, int *temp)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800623{
624 struct acpi_thermal *tz = thermal->devdata;
625 int i;
626
627 if (!tz || trip < 0)
628 return -EINVAL;
629
630 if (tz->trips.critical.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000631 if (!trip) {
Aaron Lu7b83fd92014-03-25 10:40:09 +0800632 *temp = DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(
Jean Delvare13614e32009-04-06 16:01:46 +0200633 tz->trips.critical.temperature,
634 tz->kelvin_offset);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000635 return 0;
636 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800637 trip--;
638 }
639
640 if (tz->trips.hot.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000641 if (!trip) {
Aaron Lu7b83fd92014-03-25 10:40:09 +0800642 *temp = DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(
Jean Delvare13614e32009-04-06 16:01:46 +0200643 tz->trips.hot.temperature,
644 tz->kelvin_offset);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000645 return 0;
646 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800647 trip--;
648 }
649
650 if (tz->trips.passive.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000651 if (!trip) {
Aaron Lu7b83fd92014-03-25 10:40:09 +0800652 *temp = DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(
Jean Delvare13614e32009-04-06 16:01:46 +0200653 tz->trips.passive.temperature,
654 tz->kelvin_offset);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000655 return 0;
656 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800657 trip--;
658 }
659
660 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
661 tz->trips.active[i].flags.valid; i++) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000662 if (!trip) {
Aaron Lu7b83fd92014-03-25 10:40:09 +0800663 *temp = DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(
Jean Delvare13614e32009-04-06 16:01:46 +0200664 tz->trips.active[i].temperature,
665 tz->kelvin_offset);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000666 return 0;
667 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800668 trip--;
669 }
670
671 return -EINVAL;
672}
673
Zhang, Rui9ec732f2008-04-10 16:13:10 +0800674static int thermal_get_crit_temp(struct thermal_zone_device *thermal,
Sascha Hauer17e83512015-07-24 08:12:54 +0200675 int *temperature)
676{
Zhang, Rui9ec732f2008-04-10 16:13:10 +0800677 struct acpi_thermal *tz = thermal->devdata;
678
679 if (tz->trips.critical.flags.valid) {
Aaron Lu7b83fd92014-03-25 10:40:09 +0800680 *temperature = DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(
Jean Delvare13614e32009-04-06 16:01:46 +0200681 tz->trips.critical.temperature,
682 tz->kelvin_offset);
Zhang, Rui9ec732f2008-04-10 16:13:10 +0800683 return 0;
684 } else
685 return -EINVAL;
686}
687
Zhang Rui601f3d42012-06-27 09:54:33 +0800688static int thermal_get_trend(struct thermal_zone_device *thermal,
689 int trip, enum thermal_trend *trend)
690{
691 struct acpi_thermal *tz = thermal->devdata;
692 enum thermal_trip_type type;
693 int i;
694
695 if (thermal_get_trip_type(thermal, trip, &type))
696 return -EINVAL;
697
Zhang Rui4ae46be2012-06-27 10:05:39 +0800698 if (type == THERMAL_TRIP_ACTIVE) {
Sascha Hauer17e83512015-07-24 08:12:54 +0200699 int trip_temp;
700 int temp = DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(
Aaron Lu7b83fd92014-03-25 10:40:09 +0800701 tz->temperature, tz->kelvin_offset);
Zhang Rui94a40932013-04-26 09:19:53 +0000702 if (thermal_get_trip_temp(thermal, trip, &trip_temp))
703 return -EINVAL;
704
705 if (temp > trip_temp) {
706 *trend = THERMAL_TREND_RAISING;
707 return 0;
708 } else {
709 /* Fall back on default trend */
710 return -EINVAL;
711 }
Zhang Rui4ae46be2012-06-27 10:05:39 +0800712 }
Zhang Rui601f3d42012-06-27 09:54:33 +0800713
714 /*
715 * tz->temperature has already been updated by generic thermal layer,
716 * before this callback being invoked
717 */
718 i = (tz->trips.passive.tc1 * (tz->temperature - tz->last_temperature))
719 + (tz->trips.passive.tc2
720 * (tz->temperature - tz->trips.passive.temperature));
721
722 if (i > 0)
723 *trend = THERMAL_TREND_RAISING;
724 else if (i < 0)
725 *trend = THERMAL_TREND_DROPPING;
726 else
727 *trend = THERMAL_TREND_STABLE;
728 return 0;
729}
730
731
Matthew Garrettb1569e92008-12-03 17:55:32 +0000732static int thermal_notify(struct thermal_zone_device *thermal, int trip,
733 enum thermal_trip_type trip_type)
734{
735 u8 type = 0;
736 struct acpi_thermal *tz = thermal->devdata;
737
738 if (trip_type == THERMAL_TRIP_CRITICAL)
739 type = ACPI_THERMAL_NOTIFY_CRITICAL;
740 else if (trip_type == THERMAL_TRIP_HOT)
741 type = ACPI_THERMAL_NOTIFY_HOT;
742 else
743 return 0;
744
Matthew Garrettb1569e92008-12-03 17:55:32 +0000745 acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
Stephen Rothwellf6f5c452009-03-03 12:47:17 +1100746 dev_name(&tz->device->dev), type, 1);
Matthew Garrettb1569e92008-12-03 17:55:32 +0000747
748 if (trip_type == THERMAL_TRIP_CRITICAL && nocrt)
749 return 1;
750
751 return 0;
752}
753
Zhang Rui3f655ef2008-01-17 15:51:11 +0800754static int acpi_thermal_cooling_device_cb(struct thermal_zone_device *thermal,
755 struct thermal_cooling_device *cdev,
Zhang Rui9d998422012-06-26 16:35:57 +0800756 bool bind)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800757{
758 struct acpi_device *device = cdev->devdata;
759 struct acpi_thermal *tz = thermal->devdata;
Zhang Rui653a00c2008-01-17 15:51:18 +0800760 struct acpi_device *dev;
761 acpi_status status;
762 acpi_handle handle;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800763 int i;
764 int j;
765 int trip = -1;
766 int result = 0;
767
768 if (tz->trips.critical.flags.valid)
769 trip++;
770
771 if (tz->trips.hot.flags.valid)
772 trip++;
773
774 if (tz->trips.passive.flags.valid) {
775 trip++;
776 for (i = 0; i < tz->trips.passive.devices.count;
777 i++) {
Zhang Rui653a00c2008-01-17 15:51:18 +0800778 handle = tz->trips.passive.devices.handles[i];
779 status = acpi_bus_get_device(handle, &dev);
Zhang Rui9d998422012-06-26 16:35:57 +0800780 if (ACPI_FAILURE(status) || dev != device)
781 continue;
782 if (bind)
783 result =
784 thermal_zone_bind_cooling_device
785 (thermal, trip, cdev,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000786 THERMAL_NO_LIMIT, THERMAL_NO_LIMIT,
787 THERMAL_WEIGHT_DEFAULT);
Zhang Rui9d998422012-06-26 16:35:57 +0800788 else
789 result =
790 thermal_zone_unbind_cooling_device
791 (thermal, trip, cdev);
792 if (result)
793 goto failed;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800794 }
795 }
796
797 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
798 if (!tz->trips.active[i].flags.valid)
799 break;
800 trip++;
801 for (j = 0;
802 j < tz->trips.active[i].devices.count;
803 j++) {
Zhang Rui653a00c2008-01-17 15:51:18 +0800804 handle = tz->trips.active[i].devices.handles[j];
805 status = acpi_bus_get_device(handle, &dev);
Zhang Rui9d998422012-06-26 16:35:57 +0800806 if (ACPI_FAILURE(status) || dev != device)
807 continue;
808 if (bind)
809 result = thermal_zone_bind_cooling_device
810 (thermal, trip, cdev,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000811 THERMAL_NO_LIMIT, THERMAL_NO_LIMIT,
812 THERMAL_WEIGHT_DEFAULT);
Zhang Rui9d998422012-06-26 16:35:57 +0800813 else
814 result = thermal_zone_unbind_cooling_device
815 (thermal, trip, cdev);
816 if (result)
817 goto failed;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800818 }
819 }
820
821 for (i = 0; i < tz->devices.count; i++) {
Zhang Rui653a00c2008-01-17 15:51:18 +0800822 handle = tz->devices.handles[i];
823 status = acpi_bus_get_device(handle, &dev);
824 if (ACPI_SUCCESS(status) && (dev == device)) {
Zhang Rui9d998422012-06-26 16:35:57 +0800825 if (bind)
826 result = thermal_zone_bind_cooling_device
Lan Tianyu70f29032013-08-14 21:00:39 +0800827 (thermal, THERMAL_TRIPS_NONE,
828 cdev, THERMAL_NO_LIMIT,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000829 THERMAL_NO_LIMIT,
830 THERMAL_WEIGHT_DEFAULT);
Zhang Rui9d998422012-06-26 16:35:57 +0800831 else
832 result = thermal_zone_unbind_cooling_device
Lan Tianyu70f29032013-08-14 21:00:39 +0800833 (thermal, THERMAL_TRIPS_NONE,
834 cdev);
Zhang Rui653a00c2008-01-17 15:51:18 +0800835 if (result)
836 goto failed;
837 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800838 }
839
840failed:
841 return result;
842}
843
844static int
845acpi_thermal_bind_cooling_device(struct thermal_zone_device *thermal,
846 struct thermal_cooling_device *cdev)
847{
Zhang Rui9d998422012-06-26 16:35:57 +0800848 return acpi_thermal_cooling_device_cb(thermal, cdev, true);
Zhang Rui3f655ef2008-01-17 15:51:11 +0800849}
850
851static int
852acpi_thermal_unbind_cooling_device(struct thermal_zone_device *thermal,
853 struct thermal_cooling_device *cdev)
854{
Zhang Rui9d998422012-06-26 16:35:57 +0800855 return acpi_thermal_cooling_device_cb(thermal, cdev, false);
Zhang Rui3f655ef2008-01-17 15:51:11 +0800856}
857
Emil Goodeec9c9c22014-01-07 17:24:55 +0100858static struct thermal_zone_device_ops acpi_thermal_zone_ops = {
Zhang Rui3f655ef2008-01-17 15:51:11 +0800859 .bind = acpi_thermal_bind_cooling_device,
860 .unbind = acpi_thermal_unbind_cooling_device,
861 .get_temp = thermal_get_temp,
862 .get_mode = thermal_get_mode,
863 .set_mode = thermal_set_mode,
864 .get_trip_type = thermal_get_trip_type,
865 .get_trip_temp = thermal_get_trip_temp,
Zhang, Rui9ec732f2008-04-10 16:13:10 +0800866 .get_crit_temp = thermal_get_crit_temp,
Zhang Rui601f3d42012-06-27 09:54:33 +0800867 .get_trend = thermal_get_trend,
Matthew Garrettb1569e92008-12-03 17:55:32 +0000868 .notify = thermal_notify,
Zhang Rui3f655ef2008-01-17 15:51:11 +0800869};
870
871static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz)
872{
873 int trips = 0;
874 int result;
Zhang Rui20733932008-01-17 15:51:21 +0800875 acpi_status status;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800876 int i;
877
878 if (tz->trips.critical.flags.valid)
879 trips++;
880
881 if (tz->trips.hot.flags.valid)
882 trips++;
883
884 if (tz->trips.passive.flags.valid)
885 trips++;
886
887 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
888 tz->trips.active[i].flags.valid; i++, trips++);
Matthew Garrettb1569e92008-12-03 17:55:32 +0000889
890 if (tz->trips.passive.flags.valid)
891 tz->thermal_zone =
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800892 thermal_zone_device_register("acpitz", trips, 0, tz,
Durgadoss R50125a92012-09-18 11:04:56 +0530893 &acpi_thermal_zone_ops, NULL,
Matthew Garrettb1569e92008-12-03 17:55:32 +0000894 tz->trips.passive.tsp*100,
895 tz->polling_frequency*100);
896 else
897 tz->thermal_zone =
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800898 thermal_zone_device_register("acpitz", trips, 0, tz,
Durgadoss R50125a92012-09-18 11:04:56 +0530899 &acpi_thermal_zone_ops, NULL,
900 0, tz->polling_frequency*100);
Krzysztof Heltbb070e42008-04-08 17:41:52 -0700901 if (IS_ERR(tz->thermal_zone))
Zhang Rui3f655ef2008-01-17 15:51:11 +0800902 return -ENODEV;
903
904 result = sysfs_create_link(&tz->device->dev.kobj,
905 &tz->thermal_zone->device.kobj, "thermal_zone");
906 if (result)
907 return result;
908
909 result = sysfs_create_link(&tz->thermal_zone->device.kobj,
910 &tz->device->dev.kobj, "device");
911 if (result)
912 return result;
913
Lan Tianyue6f8a4d2014-05-20 20:59:22 +0800914 status = acpi_bus_attach_private_data(tz->device->handle,
915 tz->thermal_zone);
916 if (ACPI_FAILURE(status))
Zhang Rui20733932008-01-17 15:51:21 +0800917 return -ENODEV;
Zhang Rui20733932008-01-17 15:51:21 +0800918
Zhang Rui3f655ef2008-01-17 15:51:11 +0800919 tz->tz_enabled = 1;
920
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200921 dev_info(&tz->device->dev, "registered as thermal_zone%d\n",
922 tz->thermal_zone->id);
Zhang Rui3f655ef2008-01-17 15:51:11 +0800923 return 0;
924}
925
926static void acpi_thermal_unregister_thermal_zone(struct acpi_thermal *tz)
927{
928 sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
929 sysfs_remove_link(&tz->thermal_zone->device.kobj, "device");
930 thermal_zone_device_unregister(tz->thermal_zone);
931 tz->thermal_zone = NULL;
Lan Tianyue6f8a4d2014-05-20 20:59:22 +0800932 acpi_bus_detach_private_data(tz->device->handle);
Zhang Rui3f655ef2008-01-17 15:51:11 +0800933}
934
935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936/* --------------------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 Driver Interface
938 -------------------------------------------------------------------------- */
939
Bjorn Helgaas342d5502009-04-07 15:37:06 +0000940static void acpi_thermal_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Bjorn Helgaas342d5502009-04-07 15:37:06 +0000942 struct acpi_thermal *tz = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400946 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 switch (event) {
949 case ACPI_THERMAL_NOTIFY_TEMPERATURE:
950 acpi_thermal_check(tz);
951 break;
952 case ACPI_THERMAL_NOTIFY_THRESHOLDS:
Zhang Ruice44e192008-01-17 15:51:25 +0800953 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_THRESHOLDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 acpi_thermal_check(tz);
Zhang Rui962ce8c2007-08-23 01:24:31 +0800955 acpi_bus_generate_netlink_event(device->pnp.device_class,
Kay Sievers07944692008-10-30 01:18:59 +0100956 dev_name(&device->dev), event, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 break;
958 case ACPI_THERMAL_NOTIFY_DEVICES:
Zhang Ruice44e192008-01-17 15:51:25 +0800959 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_DEVICES);
960 acpi_thermal_check(tz);
Zhang Rui962ce8c2007-08-23 01:24:31 +0800961 acpi_bus_generate_netlink_event(device->pnp.device_class,
Kay Sievers07944692008-10-30 01:18:59 +0100962 dev_name(&device->dev), event, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 break;
964 default:
965 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400966 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 break;
968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969}
970
Zhang Rui261cba22012-11-27 20:42:11 +0100971/*
972 * On some platforms, the AML code has dependency about
973 * the evaluating order of _TMP and _CRT/_HOT/_PSV/_ACx.
974 * 1. On HP Pavilion G4-1016tx, _TMP must be invoked after
975 * /_CRT/_HOT/_PSV/_ACx, or else system will be power off.
976 * 2. On HP Compaq 6715b/6715s, the return value of _PSV is 0
977 * if _TMP has never been evaluated.
978 *
979 * As this dependency is totally transparent to OS, evaluate
980 * all of them once, in the order of _CRT/_HOT/_PSV/_ACx,
981 * _TMP, before they are actually used.
982 */
983static void acpi_thermal_aml_dependency_fix(struct acpi_thermal *tz)
984{
985 acpi_handle handle = tz->device->handle;
986 unsigned long long value;
987 int i;
988
989 acpi_evaluate_integer(handle, "_CRT", NULL, &value);
990 acpi_evaluate_integer(handle, "_HOT", NULL, &value);
991 acpi_evaluate_integer(handle, "_PSV", NULL, &value);
992 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
993 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
994 acpi_status status;
995
996 status = acpi_evaluate_integer(handle, name, NULL, &value);
997 if (status == AE_NOT_FOUND)
998 break;
999 }
1000 acpi_evaluate_integer(handle, "_TMP", NULL, &value);
1001}
1002
Len Brown4be44fc2005-08-05 00:44:28 -04001003static int acpi_thermal_get_info(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
Len Brown4be44fc2005-08-05 00:44:28 -04001005 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001009 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Zhang Rui261cba22012-11-27 20:42:11 +01001011 acpi_thermal_aml_dependency_fix(tz);
1012
Matthew Garrett9bcb8112012-02-01 10:26:54 -05001013 /* Get trip points [_CRT, _PSV, etc.] (required) */
1014 result = acpi_thermal_get_trip_points(tz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001016 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Matthew Garrett9bcb8112012-02-01 10:26:54 -05001018 /* Get temperature [_TMP] (required) */
1019 result = acpi_thermal_get_temperature(tz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001021 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 /* Set the cooling mode [_SCP] to active cooling (default) */
1024 result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
Len Brown4be44fc2005-08-05 00:44:28 -04001025 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 tz->flags.cooling_mode = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028 /* Get default polling frequency [_TZP] (optional) */
1029 if (tzp)
1030 tz->polling_frequency = tzp;
1031 else
1032 acpi_thermal_get_polling_frequency(tz);
1033
Patrick Mocheld550d982006-06-27 00:41:40 -04001034 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
1036
Jean Delvare13614e32009-04-06 16:01:46 +02001037/*
1038 * The exact offset between Kelvin and degree Celsius is 273.15. However ACPI
1039 * handles temperature values with a single decimal place. As a consequence,
1040 * some implementations use an offset of 273.1 and others use an offset of
1041 * 273.2. Try to find out which one is being used, to present the most
1042 * accurate and visually appealing number.
1043 *
1044 * The heuristic below should work for all ACPI thermal zones which have a
1045 * critical trip point with a value being a multiple of 0.5 degree Celsius.
1046 */
1047static void acpi_thermal_guess_offset(struct acpi_thermal *tz)
1048{
1049 if (tz->trips.critical.flags.valid &&
1050 (tz->trips.critical.temperature % 5) == 1)
1051 tz->kelvin_offset = 2731;
1052 else
1053 tz->kelvin_offset = 2732;
1054}
1055
Aaron Lua59ffb22014-03-04 14:24:46 +08001056static void acpi_thermal_check_fn(struct work_struct *work)
1057{
1058 struct acpi_thermal *tz = container_of(work, struct acpi_thermal,
1059 thermal_check_work);
1060 acpi_thermal_check(tz);
1061}
1062
Len Brown4be44fc2005-08-05 00:44:28 -04001063static int acpi_thermal_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
Len Brown4be44fc2005-08-05 00:44:28 -04001065 int result = 0;
Len Brown4be44fc2005-08-05 00:44:28 -04001066 struct acpi_thermal *tz = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001070 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Burman Yan36bcbec2006-12-19 12:56:11 -08001072 tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001074 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Patrick Mochel8348e1b2006-05-19 16:54:40 -04001076 tz->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 strcpy(tz->name, device->pnp.bus_id);
1078 strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1079 strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001080 device->driver_data = tz;
Zhang Rui3f655ef2008-01-17 15:51:11 +08001081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 result = acpi_thermal_get_info(tz);
1083 if (result)
Zhang Rui3f655ef2008-01-17 15:51:11 +08001084 goto free_memory;
1085
Jean Delvare13614e32009-04-06 16:01:46 +02001086 acpi_thermal_guess_offset(tz);
1087
Zhang Rui3f655ef2008-01-17 15:51:11 +08001088 result = acpi_thermal_register_thermal_zone(tz);
1089 if (result)
1090 goto free_memory;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Aaron Lua59ffb22014-03-04 14:24:46 +08001092 INIT_WORK(&tz->thermal_check_work, acpi_thermal_check_fn);
1093
Andy Shevchenko766a8a62013-09-13 18:15:46 +03001094 pr_info(PREFIX "%s [%s] (%ld C)\n", acpi_device_name(device),
Rasmus Villemoese866a2e2015-10-01 23:45:31 +02001095 acpi_device_bid(device), DECI_KELVIN_TO_CELSIUS(tz->temperature));
Zhang Rui3f655ef2008-01-17 15:51:11 +08001096 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Zhang Rui3f655ef2008-01-17 15:51:11 +08001098free_memory:
1099 kfree(tz);
1100end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001101 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102}
1103
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01001104static int acpi_thermal_remove(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
Len Brown4be44fc2005-08-05 00:44:28 -04001106 struct acpi_thermal *tz = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001109 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Aaron Lua59ffb22014-03-04 14:24:46 +08001111 flush_workqueue(acpi_thermal_pm_queue);
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001112 tz = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Zhang Rui3f655ef2008-01-17 15:51:11 +08001114 acpi_thermal_unregister_thermal_zone(tz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 kfree(tz);
Patrick Mocheld550d982006-06-27 00:41:40 -04001116 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117}
1118
Rafael J. Wysocki90692402012-08-09 23:00:02 +02001119#ifdef CONFIG_PM_SLEEP
Aaron Lua59ffb22014-03-04 14:24:46 +08001120static int acpi_thermal_suspend(struct device *dev)
1121{
1122 /* Make sure the previously queued thermal check work has been done */
1123 flush_workqueue(acpi_thermal_pm_queue);
1124 return 0;
1125}
1126
Rafael J. Wysocki167cffb2012-06-27 23:26:18 +02001127static int acpi_thermal_resume(struct device *dev)
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001128{
Rafael J. Wysocki167cffb2012-06-27 23:26:18 +02001129 struct acpi_thermal *tz;
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001130 int i, j, power_state, result;
1131
Rafael J. Wysocki167cffb2012-06-27 23:26:18 +02001132 if (!dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001133 return -EINVAL;
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001134
Rafael J. Wysocki167cffb2012-06-27 23:26:18 +02001135 tz = acpi_driver_data(to_acpi_device(dev));
1136 if (!tz)
1137 return -EINVAL;
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001138
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001139 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001140 if (!(&tz->trips.active[i]))
1141 break;
1142 if (!tz->trips.active[i].flags.valid)
1143 break;
1144 tz->trips.active[i].flags.enabled = 1;
1145 for (j = 0; j < tz->trips.active[i].devices.count; j++) {
Rafael J. Wysocki488a76c2010-11-25 00:11:24 +01001146 result = acpi_bus_update_power(
1147 tz->trips.active[i].devices.handles[j],
1148 &power_state);
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001149 if (result || (power_state != ACPI_STATE_D0)) {
1150 tz->trips.active[i].flags.enabled = 0;
1151 break;
1152 }
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001153 }
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001154 tz->state.active |= tz->trips.active[i].flags.enabled;
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001155 }
1156
Aaron Lua59ffb22014-03-04 14:24:46 +08001157 queue_work(acpi_thermal_pm_queue, &tz->thermal_check_work);
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001158
1159 return AE_OK;
1160}
Rafael J. Wysocki90692402012-08-09 23:00:02 +02001161#endif
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001162
Jeff Garzik18552562007-10-03 15:15:40 -04001163static int thermal_act(const struct dmi_system_id *d) {
Len Brown0b5bfa12007-08-12 00:13:02 -04001164
1165 if (act == 0) {
Andy Shevchenko766a8a62013-09-13 18:15:46 +03001166 pr_notice(PREFIX "%s detected: "
1167 "disabling all active thermal trip points\n", d->ident);
Len Brown0b5bfa12007-08-12 00:13:02 -04001168 act = -1;
1169 }
1170 return 0;
1171}
Jeff Garzik18552562007-10-03 15:15:40 -04001172static int thermal_nocrt(const struct dmi_system_id *d) {
Len Brown8c99fdc2007-08-20 18:46:50 -04001173
Andy Shevchenko766a8a62013-09-13 18:15:46 +03001174 pr_notice(PREFIX "%s detected: "
1175 "disabling all critical thermal trip point actions.\n", d->ident);
Len Brown8c99fdc2007-08-20 18:46:50 -04001176 nocrt = 1;
1177 return 0;
1178}
Jeff Garzik18552562007-10-03 15:15:40 -04001179static int thermal_tzp(const struct dmi_system_id *d) {
Len Brown0b5bfa12007-08-12 00:13:02 -04001180
1181 if (tzp == 0) {
Andy Shevchenko766a8a62013-09-13 18:15:46 +03001182 pr_notice(PREFIX "%s detected: "
1183 "enabling thermal zone polling\n", d->ident);
Len Brown0b5bfa12007-08-12 00:13:02 -04001184 tzp = 300; /* 300 dS = 30 Seconds */
1185 }
1186 return 0;
1187}
Jeff Garzik18552562007-10-03 15:15:40 -04001188static int thermal_psv(const struct dmi_system_id *d) {
Len Brown0b5bfa12007-08-12 00:13:02 -04001189
1190 if (psv == 0) {
Andy Shevchenko766a8a62013-09-13 18:15:46 +03001191 pr_notice(PREFIX "%s detected: "
1192 "disabling all passive thermal trip points\n", d->ident);
Len Brown0b5bfa12007-08-12 00:13:02 -04001193 psv = -1;
1194 }
1195 return 0;
1196}
1197
Christoph Hellwig6faadbb2017-09-14 11:59:30 +02001198static const struct dmi_system_id thermal_dmi_table[] __initconst = {
Len Brown0b5bfa12007-08-12 00:13:02 -04001199 /*
1200 * Award BIOS on this AOpen makes thermal control almost worthless.
1201 * http://bugzilla.kernel.org/show_bug.cgi?id=8842
1202 */
1203 {
1204 .callback = thermal_act,
1205 .ident = "AOpen i915GMm-HFS",
1206 .matches = {
1207 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1208 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1209 },
1210 },
1211 {
1212 .callback = thermal_psv,
1213 .ident = "AOpen i915GMm-HFS",
1214 .matches = {
1215 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1216 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1217 },
1218 },
1219 {
1220 .callback = thermal_tzp,
1221 .ident = "AOpen i915GMm-HFS",
1222 .matches = {
1223 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1224 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1225 },
1226 },
Len Brown8c99fdc2007-08-20 18:46:50 -04001227 {
1228 .callback = thermal_nocrt,
1229 .ident = "Gigabyte GA-7ZX",
1230 .matches = {
1231 DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
1232 DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
1233 },
1234 },
Len Brown0b5bfa12007-08-12 00:13:02 -04001235 {}
1236};
Len Brown0b5bfa12007-08-12 00:13:02 -04001237
Len Brown4be44fc2005-08-05 00:44:28 -04001238static int __init acpi_thermal_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
Len Brown4be44fc2005-08-05 00:44:28 -04001240 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Len Brown0b5bfa12007-08-12 00:13:02 -04001242 dmi_check_system(thermal_dmi_table);
1243
Len Brown72b33ef2007-08-12 00:12:17 -04001244 if (off) {
Andy Shevchenko766a8a62013-09-13 18:15:46 +03001245 pr_notice(PREFIX "thermal control disabled\n");
Len Brown72b33ef2007-08-12 00:12:17 -04001246 return -ENODEV;
1247 }
Zhang Rui43d9f872010-07-15 10:46:44 +08001248
Bhaktipriya Shridhare053dc92016-06-07 08:45:40 +05301249 acpi_thermal_pm_queue = alloc_workqueue("acpi_thermal_pm",
1250 WQ_HIGHPRI | WQ_MEM_RECLAIM, 0);
Aaron Lua59ffb22014-03-04 14:24:46 +08001251 if (!acpi_thermal_pm_queue)
Patrick Mocheld550d982006-06-27 00:41:40 -04001252 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Aaron Lua59ffb22014-03-04 14:24:46 +08001254 result = acpi_bus_register_driver(&acpi_thermal_driver);
1255 if (result < 0) {
1256 destroy_workqueue(acpi_thermal_pm_queue);
1257 return -ENODEV;
1258 }
1259
Patrick Mocheld550d982006-06-27 00:41:40 -04001260 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261}
1262
Len Brown4be44fc2005-08-05 00:44:28 -04001263static void __exit acpi_thermal_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 acpi_bus_unregister_driver(&acpi_thermal_driver);
Aaron Lu2807bd12014-05-26 14:34:07 +02001266 destroy_workqueue(acpi_thermal_pm_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Patrick Mocheld550d982006-06-27 00:41:40 -04001268 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269}
1270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271module_init(acpi_thermal_init);
1272module_exit(acpi_thermal_exit);