xref: /openbmc/linux/drivers/acpi/thermal.c (revision 59f5a149f5072f4da79656cde9972ba2b616634f)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  acpi_thermal.c - ACPI Thermal Zone Driver ($Revision: 41 $)
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
61da177e4SLinus Torvalds  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *  This driver fully implements the ACPI thermal policy as described in the
91da177e4SLinus Torvalds  *  ACPI 2.0 Specification.
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  *  TBD: 1. Implement passive cooling hysteresis.
121da177e4SLinus Torvalds  *       2. Enhance passive cooling (CPU) states/limit interface to support
131da177e4SLinus Torvalds  *          concepts of 'multiple limiters', upper/lower limits, etc.
141da177e4SLinus Torvalds  */
151da177e4SLinus Torvalds 
16f86b15a1SRafael J. Wysocki #define pr_fmt(fmt) "ACPI: thermal: " fmt
17f86b15a1SRafael J. Wysocki 
181da177e4SLinus Torvalds #include <linux/kernel.h>
191da177e4SLinus Torvalds #include <linux/module.h>
200b5bfa1cSLen Brown #include <linux/dmi.h>
211da177e4SLinus Torvalds #include <linux/init.h>
225a0e3ad6STejun Heo #include <linux/slab.h>
231da177e4SLinus Torvalds #include <linux/types.h>
24cd354f1aSTim Schmielau #include <linux/jiffies.h>
251da177e4SLinus Torvalds #include <linux/kmod.h>
2610a0a8d4SJeremy Fitzhardinge #include <linux/reboot.h>
27f6f5c45eSStephen Rothwell #include <linux/device.h>
283f655ef8SZhang Rui #include <linux/thermal.h>
298b48463fSLv Zheng #include <linux/acpi.h>
30a59ffb20SAaron Lu #include <linux/workqueue.h>
317c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
327f49a5cbSAkinobu Mita #include <linux/units.h>
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds #define ACPI_THERMAL_CLASS		"thermal_zone"
351da177e4SLinus Torvalds #define ACPI_THERMAL_DEVICE_NAME	"Thermal Zone"
361da177e4SLinus Torvalds #define ACPI_THERMAL_NOTIFY_TEMPERATURE	0x80
371da177e4SLinus Torvalds #define ACPI_THERMAL_NOTIFY_THRESHOLDS	0x81
381da177e4SLinus Torvalds #define ACPI_THERMAL_NOTIFY_DEVICES	0x82
391da177e4SLinus Torvalds #define ACPI_THERMAL_NOTIFY_CRITICAL	0xF0
401da177e4SLinus Torvalds #define ACPI_THERMAL_NOTIFY_HOT		0xF1
411da177e4SLinus Torvalds #define ACPI_THERMAL_MODE_ACTIVE	0x00
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds #define ACPI_THERMAL_MAX_ACTIVE		10
441da177e4SLinus Torvalds #define ACPI_THERMAL_MAX_LIMIT_STR_LEN	65
451da177e4SLinus Torvalds 
46bb5ab1fdSRafael J. Wysocki #define ACPI_TRIPS_CRITICAL	BIT(0)
47bb5ab1fdSRafael J. Wysocki #define ACPI_TRIPS_HOT		BIT(1)
48bb5ab1fdSRafael J. Wysocki #define ACPI_TRIPS_PASSIVE	BIT(2)
49bb5ab1fdSRafael J. Wysocki #define ACPI_TRIPS_ACTIVE	BIT(3)
50bb5ab1fdSRafael J. Wysocki #define ACPI_TRIPS_DEVICES	BIT(4)
51bb5ab1fdSRafael J. Wysocki 
52bb5ab1fdSRafael J. Wysocki #define ACPI_TRIPS_THRESHOLDS	(ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE)
53bb5ab1fdSRafael J. Wysocki 
54bb5ab1fdSRafael J. Wysocki #define ACPI_TRIPS_INIT		(ACPI_TRIPS_CRITICAL | ACPI_TRIPS_HOT | \
55bb5ab1fdSRafael J. Wysocki 				 ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE | \
56bb5ab1fdSRafael J. Wysocki 				 ACPI_TRIPS_DEVICES)
57bb5ab1fdSRafael J. Wysocki 
58bb5ab1fdSRafael J. Wysocki /*
59bb5ab1fdSRafael J. Wysocki  * This exception is thrown out in two cases:
60bb5ab1fdSRafael J. Wysocki  * 1.An invalid trip point becomes invalid or a valid trip point becomes invalid
61bb5ab1fdSRafael J. Wysocki  *   when re-evaluating the AML code.
62bb5ab1fdSRafael J. Wysocki  * 2.TODO: Devices listed in _PSL, _ALx, _TZD may change.
63bb5ab1fdSRafael J. Wysocki  *   We need to re-bind the cooling devices of a thermal zone when this occurs.
64bb5ab1fdSRafael J. Wysocki  */
65bb5ab1fdSRafael J. Wysocki #define ACPI_THERMAL_TRIPS_EXCEPTION(flags, tz, str) \
66bb5ab1fdSRafael J. Wysocki do { \
67bb5ab1fdSRafael J. Wysocki 	if (flags != ACPI_TRIPS_INIT) \
68bb5ab1fdSRafael J. Wysocki 		acpi_handle_info(tz->device->handle, \
69bb5ab1fdSRafael J. Wysocki 			"ACPI thermal trip point %s changed\n" \
70bb5ab1fdSRafael J. Wysocki 			"Please report to linux-acpi@vger.kernel.org\n", str); \
71bb5ab1fdSRafael J. Wysocki } while (0)
72bb5ab1fdSRafael J. Wysocki 
73f8707ec9SLen Brown static int act;
74f8707ec9SLen Brown module_param(act, int, 0644);
753c1d36daSLen Brown MODULE_PARM_DESC(act, "Disable or override all lowest active trip points.");
76f8707ec9SLen Brown 
77c52a7419SLen Brown static int crt;
78c52a7419SLen Brown module_param(crt, int, 0644);
79c52a7419SLen Brown MODULE_PARM_DESC(crt, "Disable or lower all critical trip points.");
80c52a7419SLen Brown 
811da177e4SLinus Torvalds static int tzp;
82730ff34dSLen Brown module_param(tzp, int, 0444);
833c1d36daSLen Brown MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.");
841da177e4SLinus Torvalds 
8572b33ef8SLen Brown static int off;
8672b33ef8SLen Brown module_param(off, int, 0);
873c1d36daSLen Brown MODULE_PARM_DESC(off, "Set to disable ACPI thermal support.");
8872b33ef8SLen Brown 
89a70cdc52SLen Brown static int psv;
90a70cdc52SLen Brown module_param(psv, int, 0644);
913c1d36daSLen Brown MODULE_PARM_DESC(psv, "Disable or override all passive trip points.");
92a70cdc52SLen Brown 
93a59ffb20SAaron Lu static struct workqueue_struct *acpi_thermal_pm_queue;
94a59ffb20SAaron Lu 
9568b77785SRafael J. Wysocki struct acpi_thermal_trip {
961da177e4SLinus Torvalds 	unsigned long temperature;
977266c88cSRafael J. Wysocki 	bool valid;
981da177e4SLinus Torvalds };
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds struct acpi_thermal_passive {
10168b77785SRafael J. Wysocki 	struct acpi_thermal_trip trip;
1027266c88cSRafael J. Wysocki 	struct acpi_handle_list devices;
1031da177e4SLinus Torvalds 	unsigned long tc1;
1041da177e4SLinus Torvalds 	unsigned long tc2;
1051da177e4SLinus Torvalds 	unsigned long tsp;
1061da177e4SLinus Torvalds };
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds struct acpi_thermal_active {
10968b77785SRafael J. Wysocki 	struct acpi_thermal_trip trip;
1101da177e4SLinus Torvalds 	struct acpi_handle_list devices;
1111da177e4SLinus Torvalds };
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds struct acpi_thermal_trips {
11468b77785SRafael J. Wysocki 	struct acpi_thermal_trip critical;
11568b77785SRafael J. Wysocki 	struct acpi_thermal_trip hot;
1161da177e4SLinus Torvalds 	struct acpi_thermal_passive passive;
1171da177e4SLinus Torvalds 	struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
1181da177e4SLinus Torvalds };
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds struct acpi_thermal {
1218348e1b1SPatrick Mochel 	struct acpi_device *device;
1221da177e4SLinus Torvalds 	acpi_bus_id name;
1231da177e4SLinus Torvalds 	unsigned long temperature;
1241da177e4SLinus Torvalds 	unsigned long last_temperature;
1251da177e4SLinus Torvalds 	unsigned long polling_frequency;
1261da177e4SLinus Torvalds 	volatile u8 zombie;
1271da177e4SLinus Torvalds 	struct acpi_thermal_trips trips;
128ec23c1c4SRafael J. Wysocki 	struct thermal_trip *trip_table;
1291da177e4SLinus Torvalds 	struct acpi_handle_list devices;
1303f655ef8SZhang Rui 	struct thermal_zone_device *thermal_zone;
1317f49a5cbSAkinobu Mita 	int kelvin_offset;	/* in millidegrees */
132a59ffb20SAaron Lu 	struct work_struct thermal_check_work;
13381b704d3SRafael J. Wysocki 	struct mutex thermal_check_lock;
13481b704d3SRafael J. Wysocki 	refcount_t thermal_check_count;
1351da177e4SLinus Torvalds };
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds /* --------------------------------------------------------------------------
1381da177e4SLinus Torvalds                              Thermal Zone Management
1391da177e4SLinus Torvalds    -------------------------------------------------------------------------- */
1401da177e4SLinus Torvalds 
acpi_thermal_get_temperature(struct acpi_thermal * tz)1414be44fcdSLen Brown static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
1421da177e4SLinus Torvalds {
1431da177e4SLinus Torvalds 	acpi_status status = AE_OK;
14427663c58SMatthew Wilcox 	unsigned long long tmp;
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds 	if (!tz)
147d550d98dSPatrick Mochel 		return -EINVAL;
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 	tz->last_temperature = tz->temperature;
1501da177e4SLinus Torvalds 
15127663c58SMatthew Wilcox 	status = acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tmp);
1521da177e4SLinus Torvalds 	if (ACPI_FAILURE(status))
153d550d98dSPatrick Mochel 		return -ENODEV;
1541da177e4SLinus Torvalds 
15527663c58SMatthew Wilcox 	tz->temperature = tmp;
156f86b15a1SRafael J. Wysocki 
157f86b15a1SRafael J. Wysocki 	acpi_handle_debug(tz->device->handle, "Temperature is %lu dK\n",
158f86b15a1SRafael J. Wysocki 			  tz->temperature);
1591da177e4SLinus Torvalds 
160d550d98dSPatrick Mochel 	return 0;
1611da177e4SLinus Torvalds }
1621da177e4SLinus Torvalds 
acpi_thermal_get_polling_frequency(struct acpi_thermal * tz)1634be44fcdSLen Brown static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
1641da177e4SLinus Torvalds {
1651da177e4SLinus Torvalds 	acpi_status status = AE_OK;
16627663c58SMatthew Wilcox 	unsigned long long tmp;
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 	if (!tz)
169d550d98dSPatrick Mochel 		return -EINVAL;
1701da177e4SLinus Torvalds 
17127663c58SMatthew Wilcox 	status = acpi_evaluate_integer(tz->device->handle, "_TZP", NULL, &tmp);
1721da177e4SLinus Torvalds 	if (ACPI_FAILURE(status))
173d550d98dSPatrick Mochel 		return -ENODEV;
1741da177e4SLinus Torvalds 
17527663c58SMatthew Wilcox 	tz->polling_frequency = tmp;
176f86b15a1SRafael J. Wysocki 	acpi_handle_debug(tz->device->handle, "Polling frequency is %lu dS\n",
177f86b15a1SRafael J. Wysocki 			  tz->polling_frequency);
1781da177e4SLinus Torvalds 
179d550d98dSPatrick Mochel 	return 0;
1801da177e4SLinus Torvalds }
1811da177e4SLinus Torvalds 
acpi_thermal_temp(struct acpi_thermal * tz,int temp_deci_k)182ec23c1c4SRafael J. Wysocki static int acpi_thermal_temp(struct acpi_thermal *tz, int temp_deci_k)
183ec23c1c4SRafael J. Wysocki {
184ec23c1c4SRafael J. Wysocki 	if (temp_deci_k == THERMAL_TEMP_INVALID)
185ec23c1c4SRafael J. Wysocki 		return THERMAL_TEMP_INVALID;
186ec23c1c4SRafael J. Wysocki 
187ec23c1c4SRafael J. Wysocki 	return deci_kelvin_to_millicelsius_with_offset(temp_deci_k,
188ec23c1c4SRafael J. Wysocki 						       tz->kelvin_offset);
189ec23c1c4SRafael J. Wysocki }
190ec23c1c4SRafael J. Wysocki 
__acpi_thermal_trips_update(struct acpi_thermal * tz,int flag)19168f4f037SRafael J. Wysocki static void __acpi_thermal_trips_update(struct acpi_thermal *tz, int flag)
1921da177e4SLinus Torvalds {
19336f55404SRafael J. Wysocki 	acpi_status status;
19427663c58SMatthew Wilcox 	unsigned long long tmp;
195ce44e197SZhang Rui 	struct acpi_handle_list devices;
1967266c88cSRafael J. Wysocki 	bool valid = false;
197ce44e197SZhang Rui 	int i;
1981da177e4SLinus Torvalds 
199fa809452SThomas Renninger 	/* Critical Shutdown */
200ce44e197SZhang Rui 	if (flag & ACPI_TRIPS_CRITICAL) {
20152ce5049SRafael J. Wysocki 		status = acpi_evaluate_integer(tz->device->handle, "_CRT", NULL, &tmp);
20227663c58SMatthew Wilcox 		tz->trips.critical.temperature = tmp;
203a39a2d7cSArjan van de Ven 		/*
204a39a2d7cSArjan van de Ven 		 * Treat freezing temperatures as invalid as well; some
205a39a2d7cSArjan van de Ven 		 * BIOSes return really low values and cause reboots at startup.
206b731d7b6SAdam Buchbinder 		 * Below zero (Celsius) values clearly aren't right for sure..
207a39a2d7cSArjan van de Ven 		 * ... so lets discard those as invalid.
208a39a2d7cSArjan van de Ven 		 */
209fa809452SThomas Renninger 		if (ACPI_FAILURE(status)) {
2107266c88cSRafael J. Wysocki 			tz->trips.critical.valid = false;
211f86b15a1SRafael J. Wysocki 			acpi_handle_debug(tz->device->handle,
212f86b15a1SRafael J. Wysocki 					  "No critical threshold\n");
213fa809452SThomas Renninger 		} else if (tmp <= 2732) {
21452ce5049SRafael J. Wysocki 			pr_info(FW_BUG "Invalid critical threshold (%llu)\n", tmp);
2157266c88cSRafael J. Wysocki 			tz->trips.critical.valid = false;
2164be44fcdSLen Brown 		} else {
2177266c88cSRafael J. Wysocki 			tz->trips.critical.valid = true;
218f86b15a1SRafael J. Wysocki 			acpi_handle_debug(tz->device->handle,
2194be44fcdSLen Brown 					  "Found critical threshold [%lu]\n",
220f86b15a1SRafael J. Wysocki 					  tz->trips.critical.temperature);
2211da177e4SLinus Torvalds 		}
2227266c88cSRafael J. Wysocki 		if (tz->trips.critical.valid) {
223c52a7419SLen Brown 			if (crt == -1) {
2247266c88cSRafael J. Wysocki 				tz->trips.critical.valid = false;
225c52a7419SLen Brown 			} else if (crt > 0) {
2267f49a5cbSAkinobu Mita 				unsigned long crt_k = celsius_to_deci_kelvin(crt);
2277f49a5cbSAkinobu Mita 
228c52a7419SLen Brown 				/*
22922a94d79SZhang Rui 				 * Allow override critical threshold
230c52a7419SLen Brown 				 */
23122a94d79SZhang Rui 				if (crt_k > tz->trips.critical.temperature)
232f86b15a1SRafael J. Wysocki 					pr_info("Critical threshold %d C\n", crt);
233f86b15a1SRafael J. Wysocki 
234c52a7419SLen Brown 				tz->trips.critical.temperature = crt_k;
235c52a7419SLen Brown 			}
236c52a7419SLen Brown 		}
2371da177e4SLinus Torvalds 	}
2381da177e4SLinus Torvalds 
239ce44e197SZhang Rui 	/* Critical Sleep (optional) */
240ce44e197SZhang Rui 	if (flag & ACPI_TRIPS_HOT) {
24152ce5049SRafael J. Wysocki 		status = acpi_evaluate_integer(tz->device->handle, "_HOT", NULL, &tmp);
242ce44e197SZhang Rui 		if (ACPI_FAILURE(status)) {
2437266c88cSRafael J. Wysocki 			tz->trips.hot.valid = false;
244f86b15a1SRafael J. Wysocki 			acpi_handle_debug(tz->device->handle,
245f86b15a1SRafael J. Wysocki 					  "No hot threshold\n");
246ce44e197SZhang Rui 		} else {
24727663c58SMatthew Wilcox 			tz->trips.hot.temperature = tmp;
2487266c88cSRafael J. Wysocki 			tz->trips.hot.valid = true;
249f86b15a1SRafael J. Wysocki 			acpi_handle_debug(tz->device->handle,
250ce44e197SZhang Rui 					  "Found hot threshold [%lu]\n",
251f86b15a1SRafael J. Wysocki 					  tz->trips.hot.temperature);
252ce44e197SZhang Rui 		}
253ce44e197SZhang Rui 	}
2541da177e4SLinus Torvalds 
255ce44e197SZhang Rui 	/* Passive (optional) */
25668b77785SRafael J. Wysocki 	if (((flag & ACPI_TRIPS_PASSIVE) && tz->trips.passive.trip.valid) ||
2579e8bc166SRafael J. Wysocki 	    flag == ACPI_TRIPS_INIT) {
25868b77785SRafael J. Wysocki 		valid = tz->trips.passive.trip.valid;
259a70cdc52SLen Brown 		if (psv == -1) {
260a70cdc52SLen Brown 			status = AE_SUPPORT;
261a70cdc52SLen Brown 		} else if (psv > 0) {
2627f49a5cbSAkinobu Mita 			tmp = celsius_to_deci_kelvin(psv);
263a70cdc52SLen Brown 			status = AE_OK;
264a70cdc52SLen Brown 		} else {
265a70cdc52SLen Brown 			status = acpi_evaluate_integer(tz->device->handle,
26627663c58SMatthew Wilcox 						       "_PSV", NULL, &tmp);
267a70cdc52SLen Brown 		}
268a70cdc52SLen Brown 
26952ce5049SRafael J. Wysocki 		if (ACPI_FAILURE(status)) {
27068b77785SRafael J. Wysocki 			tz->trips.passive.trip.valid = false;
27152ce5049SRafael J. Wysocki 		} else {
27268b77785SRafael J. Wysocki 			tz->trips.passive.trip.temperature = tmp;
27368b77785SRafael J. Wysocki 			tz->trips.passive.trip.valid = true;
274ce44e197SZhang Rui 			if (flag == ACPI_TRIPS_INIT) {
27552ce5049SRafael J. Wysocki 				status = acpi_evaluate_integer(tz->device->handle,
27652ce5049SRafael J. Wysocki 							       "_TC1", NULL, &tmp);
277ce44e197SZhang Rui 				if (ACPI_FAILURE(status))
27868b77785SRafael J. Wysocki 					tz->trips.passive.trip.valid = false;
27927663c58SMatthew Wilcox 				else
28027663c58SMatthew Wilcox 					tz->trips.passive.tc1 = tmp;
28152ce5049SRafael J. Wysocki 
28252ce5049SRafael J. Wysocki 				status = acpi_evaluate_integer(tz->device->handle,
28352ce5049SRafael J. Wysocki 							       "_TC2", NULL, &tmp);
284ce44e197SZhang Rui 				if (ACPI_FAILURE(status))
28568b77785SRafael J. Wysocki 					tz->trips.passive.trip.valid = false;
28627663c58SMatthew Wilcox 				else
28727663c58SMatthew Wilcox 					tz->trips.passive.tc2 = tmp;
28852ce5049SRafael J. Wysocki 
28952ce5049SRafael J. Wysocki 				status = acpi_evaluate_integer(tz->device->handle,
29052ce5049SRafael J. Wysocki 							       "_TSP", NULL, &tmp);
291ce44e197SZhang Rui 				if (ACPI_FAILURE(status))
29268b77785SRafael J. Wysocki 					tz->trips.passive.trip.valid = false;
29327663c58SMatthew Wilcox 				else
29427663c58SMatthew Wilcox 					tz->trips.passive.tsp = tmp;
295ce44e197SZhang Rui 			}
296ce44e197SZhang Rui 		}
297ce44e197SZhang Rui 	}
29868b77785SRafael J. Wysocki 	if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.passive.trip.valid) {
299ce44e197SZhang Rui 		memset(&devices, 0, sizeof(struct acpi_handle_list));
300ce44e197SZhang Rui 		status = acpi_evaluate_reference(tz->device->handle, "_PSL",
301ce44e197SZhang Rui 						 NULL, &devices);
3020e4240d9SZhang Rui 		if (ACPI_FAILURE(status)) {
303f86b15a1SRafael J. Wysocki 			acpi_handle_info(tz->device->handle,
304f86b15a1SRafael J. Wysocki 					 "Invalid passive threshold\n");
30568b77785SRafael J. Wysocki 			tz->trips.passive.trip.valid = false;
30652ce5049SRafael J. Wysocki 		} else {
30768b77785SRafael J. Wysocki 			tz->trips.passive.trip.valid = true;
30852ce5049SRafael J. Wysocki 		}
3091da177e4SLinus Torvalds 
310ce44e197SZhang Rui 		if (memcmp(&tz->trips.passive.devices, &devices,
311ce44e197SZhang Rui 			   sizeof(struct acpi_handle_list))) {
312ce44e197SZhang Rui 			memcpy(&tz->trips.passive.devices, &devices,
313ce44e197SZhang Rui 			       sizeof(struct acpi_handle_list));
314f86b15a1SRafael J. Wysocki 			ACPI_THERMAL_TRIPS_EXCEPTION(flag, tz, "device");
315ce44e197SZhang Rui 		}
316ce44e197SZhang Rui 	}
317ce44e197SZhang Rui 	if ((flag & ACPI_TRIPS_PASSIVE) || (flag & ACPI_TRIPS_DEVICES)) {
31868b77785SRafael J. Wysocki 		if (valid != tz->trips.passive.trip.valid)
319f86b15a1SRafael J. Wysocki 			ACPI_THERMAL_TRIPS_EXCEPTION(flag, tz, "state");
3201da177e4SLinus Torvalds 	}
3211da177e4SLinus Torvalds 
322ce44e197SZhang Rui 	/* Active (optional) */
3231da177e4SLinus Torvalds 	for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
3241da177e4SLinus Torvalds 		char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
32568b77785SRafael J. Wysocki 		valid = tz->trips.active[i].trip.valid;
3261da177e4SLinus Torvalds 
327f8707ec9SLen Brown 		if (act == -1)
328f8707ec9SLen Brown 			break; /* disable all active trip points */
329f8707ec9SLen Brown 
3309e8bc166SRafael J. Wysocki 		if (flag == ACPI_TRIPS_INIT || ((flag & ACPI_TRIPS_ACTIVE) &&
33168b77785SRafael J. Wysocki 		    tz->trips.active[i].trip.valid)) {
332f8707ec9SLen Brown 			status = acpi_evaluate_integer(tz->device->handle,
33327663c58SMatthew Wilcox 						       name, NULL, &tmp);
334f8707ec9SLen Brown 			if (ACPI_FAILURE(status)) {
33568b77785SRafael J. Wysocki 				tz->trips.active[i].trip.valid = false;
336ce44e197SZhang Rui 				if (i == 0)
3371da177e4SLinus Torvalds 					break;
33852ce5049SRafael J. Wysocki 
339ce44e197SZhang Rui 				if (act <= 0)
340f8707ec9SLen Brown 					break;
34152ce5049SRafael J. Wysocki 
342ce44e197SZhang Rui 				if (i == 1)
34368b77785SRafael J. Wysocki 					tz->trips.active[0].trip.temperature =
34468b77785SRafael J. Wysocki 							celsius_to_deci_kelvin(act);
345ce44e197SZhang Rui 				else
346f8707ec9SLen Brown 					/*
347f8707ec9SLen Brown 					 * Don't allow override higher than
348f8707ec9SLen Brown 					 * the next higher trip point
349f8707ec9SLen Brown 					 */
35068b77785SRafael J. Wysocki 					tz->trips.active[i-1].trip.temperature =
3510dc9a715SJiangshan Yi 						min_t(unsigned long,
35268b77785SRafael J. Wysocki 						      tz->trips.active[i-2].trip.temperature,
3537f49a5cbSAkinobu Mita 						      celsius_to_deci_kelvin(act));
35452ce5049SRafael J. Wysocki 
355f8707ec9SLen Brown 				break;
35627663c58SMatthew Wilcox 			} else {
35768b77785SRafael J. Wysocki 				tz->trips.active[i].trip.temperature = tmp;
35868b77785SRafael J. Wysocki 				tz->trips.active[i].trip.valid = true;
359f8707ec9SLen Brown 			}
36027663c58SMatthew Wilcox 		}
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds 		name[2] = 'L';
36368b77785SRafael J. Wysocki 		if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.active[i].trip.valid) {
364ce44e197SZhang Rui 			memset(&devices, 0, sizeof(struct acpi_handle_list));
365ce44e197SZhang Rui 			status = acpi_evaluate_reference(tz->device->handle,
366ce44e197SZhang Rui 							 name, NULL, &devices);
3670e4240d9SZhang Rui 			if (ACPI_FAILURE(status)) {
368f86b15a1SRafael J. Wysocki 				acpi_handle_info(tz->device->handle,
369f86b15a1SRafael J. Wysocki 						 "Invalid active%d threshold\n", i);
37068b77785SRafael J. Wysocki 				tz->trips.active[i].trip.valid = false;
37152ce5049SRafael J. Wysocki 			} else {
37268b77785SRafael J. Wysocki 				tz->trips.active[i].trip.valid = true;
37352ce5049SRafael J. Wysocki 			}
374ce44e197SZhang Rui 
375ce44e197SZhang Rui 			if (memcmp(&tz->trips.active[i].devices, &devices,
376ce44e197SZhang Rui 				   sizeof(struct acpi_handle_list))) {
377ce44e197SZhang Rui 				memcpy(&tz->trips.active[i].devices, &devices,
378ce44e197SZhang Rui 				       sizeof(struct acpi_handle_list));
379f86b15a1SRafael J. Wysocki 				ACPI_THERMAL_TRIPS_EXCEPTION(flag, tz, "device");
380ce44e197SZhang Rui 			}
381ce44e197SZhang Rui 		}
382ce44e197SZhang Rui 		if ((flag & ACPI_TRIPS_ACTIVE) || (flag & ACPI_TRIPS_DEVICES))
38368b77785SRafael J. Wysocki 			if (valid != tz->trips.active[i].trip.valid)
384f86b15a1SRafael J. Wysocki 				ACPI_THERMAL_TRIPS_EXCEPTION(flag, tz, "state");
385ce44e197SZhang Rui 
38668b77785SRafael J. Wysocki 		if (!tz->trips.active[i].trip.valid)
387ce44e197SZhang Rui 			break;
388ce44e197SZhang Rui 	}
389ce44e197SZhang Rui 
390e88c7409SKelsey Skunberg 	if (flag & ACPI_TRIPS_DEVICES) {
391668e0200SLan Tianyu 		memset(&devices, 0, sizeof(devices));
392ce44e197SZhang Rui 		status = acpi_evaluate_reference(tz->device->handle, "_TZD",
393ce44e197SZhang Rui 						 NULL, &devices);
39452ce5049SRafael J. Wysocki 		if (ACPI_SUCCESS(status) &&
39552ce5049SRafael J. Wysocki 		    memcmp(&tz->devices, &devices, sizeof(devices))) {
396668e0200SLan Tianyu 			tz->devices = devices;
397f86b15a1SRafael J. Wysocki 			ACPI_THERMAL_TRIPS_EXCEPTION(flag, tz, "device");
398ce44e197SZhang Rui 		}
399ce44e197SZhang Rui 	}
40068f4f037SRafael J. Wysocki }
4011da177e4SLinus Torvalds 
acpi_thermal_adjust_trip(struct thermal_trip * trip,void * data)402ec23c1c4SRafael J. Wysocki static int acpi_thermal_adjust_trip(struct thermal_trip *trip, void *data)
403ec23c1c4SRafael J. Wysocki {
404ec23c1c4SRafael J. Wysocki 	struct acpi_thermal_trip *acpi_trip = trip->priv;
405ec23c1c4SRafael J. Wysocki 	struct acpi_thermal *tz = data;
406ec23c1c4SRafael J. Wysocki 
407ec23c1c4SRafael J. Wysocki 	if (!acpi_trip)
408ec23c1c4SRafael J. Wysocki 		return 0;
409ec23c1c4SRafael J. Wysocki 
410ec23c1c4SRafael J. Wysocki 	if (acpi_trip->valid)
411ec23c1c4SRafael J. Wysocki 		trip->temperature = acpi_thermal_temp(tz, acpi_trip->temperature);
412ec23c1c4SRafael J. Wysocki 	else
413ec23c1c4SRafael J. Wysocki 		trip->temperature = THERMAL_TEMP_INVALID;
414ec23c1c4SRafael J. Wysocki 
415ec23c1c4SRafael J. Wysocki 	return 0;
416ec23c1c4SRafael J. Wysocki }
417ec23c1c4SRafael J. Wysocki 
acpi_thermal_adjust_thermal_zone(struct thermal_zone_device * thermal,unsigned long data)41868f4f037SRafael J. Wysocki static void acpi_thermal_adjust_thermal_zone(struct thermal_zone_device *thermal,
41968f4f037SRafael J. Wysocki 					     unsigned long data)
42068f4f037SRafael J. Wysocki {
421ec23c1c4SRafael J. Wysocki 	struct acpi_thermal *tz = thermal_zone_device_priv(thermal);
4224ab4b3b1SRafael J. Wysocki 	int flag = data == ACPI_THERMAL_NOTIFY_THRESHOLDS ?
4234ab4b3b1SRafael J. Wysocki 				ACPI_TRIPS_THRESHOLDS : ACPI_TRIPS_DEVICES;
424ec23c1c4SRafael J. Wysocki 
4254ab4b3b1SRafael J. Wysocki 	__acpi_thermal_trips_update(tz, flag);
426ec23c1c4SRafael J. Wysocki 
427ec23c1c4SRafael J. Wysocki 	for_each_thermal_trip(tz->thermal_zone, acpi_thermal_adjust_trip, tz);
42868f4f037SRafael J. Wysocki }
42968f4f037SRafael J. Wysocki 
acpi_queue_thermal_check(struct acpi_thermal * tz)43068f4f037SRafael J. Wysocki static void acpi_queue_thermal_check(struct acpi_thermal *tz)
43168f4f037SRafael J. Wysocki {
43268f4f037SRafael J. Wysocki 	if (!work_pending(&tz->thermal_check_work))
43368f4f037SRafael J. Wysocki 		queue_work(acpi_thermal_pm_queue, &tz->thermal_check_work);
43468f4f037SRafael J. Wysocki }
43568f4f037SRafael J. Wysocki 
acpi_thermal_trips_update(struct acpi_thermal * tz,u32 event)4364ab4b3b1SRafael J. Wysocki static void acpi_thermal_trips_update(struct acpi_thermal *tz, u32 event)
43768f4f037SRafael J. Wysocki {
4384ab4b3b1SRafael J. Wysocki 	struct acpi_device *adev = tz->device;
4394ab4b3b1SRafael J. Wysocki 
44068f4f037SRafael J. Wysocki 	/*
44168f4f037SRafael J. Wysocki 	 * Use thermal_zone_device_exec() to carry out the trip points
44268f4f037SRafael J. Wysocki 	 * update, so as to protect thermal_get_trend() from getting stale
44368f4f037SRafael J. Wysocki 	 * trip point temperatures and to prevent thermal_zone_device_update()
44468f4f037SRafael J. Wysocki 	 * invoked from acpi_thermal_check_fn() from producing inconsistent
44568f4f037SRafael J. Wysocki 	 * results.
44668f4f037SRafael J. Wysocki 	 */
44768f4f037SRafael J. Wysocki 	thermal_zone_device_exec(tz->thermal_zone,
4484ab4b3b1SRafael J. Wysocki 				 acpi_thermal_adjust_thermal_zone, event);
44968f4f037SRafael J. Wysocki 	acpi_queue_thermal_check(tz);
4504ab4b3b1SRafael J. Wysocki 	acpi_bus_generate_netlink_event(adev->pnp.device_class,
4514ab4b3b1SRafael J. Wysocki 					dev_name(&adev->dev), event, 0);
4521da177e4SLinus Torvalds }
4531da177e4SLinus Torvalds 
acpi_thermal_get_trip_points(struct acpi_thermal * tz)454ce44e197SZhang Rui static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
455ce44e197SZhang Rui {
4567266c88cSRafael J. Wysocki 	bool valid;
45768f4f037SRafael J. Wysocki 	int i;
4588b7ef6d8SThomas Renninger 
45968f4f037SRafael J. Wysocki 	__acpi_thermal_trips_update(tz, ACPI_TRIPS_INIT);
4608b7ef6d8SThomas Renninger 
4617266c88cSRafael J. Wysocki 	valid = tz->trips.critical.valid |
4627266c88cSRafael J. Wysocki 		tz->trips.hot.valid |
46368b77785SRafael J. Wysocki 		tz->trips.passive.trip.valid;
4648b7ef6d8SThomas Renninger 
4658b7ef6d8SThomas Renninger 	for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
46668b77785SRafael J. Wysocki 		valid = valid || tz->trips.active[i].trip.valid;
4678b7ef6d8SThomas Renninger 
4688b7ef6d8SThomas Renninger 	if (!valid) {
469766a8a6dSAndy Shevchenko 		pr_warn(FW_BUG "No valid trip found\n");
4708b7ef6d8SThomas Renninger 		return -ENODEV;
4718b7ef6d8SThomas Renninger 	}
4728b7ef6d8SThomas Renninger 	return 0;
473ce44e197SZhang Rui }
474ce44e197SZhang Rui 
4753f655ef8SZhang Rui /* sys I/F for generic thermal sysfs support */
4765e012760SZhang, Rui 
thermal_get_temp(struct thermal_zone_device * thermal,int * temp)47717e8351aSSascha Hauer static int thermal_get_temp(struct thermal_zone_device *thermal, int *temp)
4783f655ef8SZhang Rui {
4793d4e1badSDaniel Lezcano 	struct acpi_thermal *tz = thermal_zone_device_priv(thermal);
48076ecb4f2SZhang, Rui 	int result;
4813f655ef8SZhang Rui 
4823f655ef8SZhang Rui 	if (!tz)
4833f655ef8SZhang Rui 		return -EINVAL;
4843f655ef8SZhang Rui 
48576ecb4f2SZhang, Rui 	result = acpi_thermal_get_temperature(tz);
48676ecb4f2SZhang, Rui 	if (result)
48776ecb4f2SZhang, Rui 		return result;
48876ecb4f2SZhang, Rui 
4897f49a5cbSAkinobu Mita 	*temp = deci_kelvin_to_millicelsius_with_offset(tz->temperature,
4907b83fd9dSAaron Lu 							tz->kelvin_offset);
4916503e5dfSMatthew Garrett 	return 0;
4923f655ef8SZhang Rui }
4933f655ef8SZhang Rui 
thermal_get_trend(struct thermal_zone_device * thermal,const struct thermal_trip * trip,enum thermal_trend * trend)494601f3d42SZhang Rui static int thermal_get_trend(struct thermal_zone_device *thermal,
495ebc7abb3SRafael J. Wysocki 			     const struct thermal_trip *trip,
4968289d810SRafael J. Wysocki 			     enum thermal_trend *trend)
497601f3d42SZhang Rui {
4983d4e1badSDaniel Lezcano 	struct acpi_thermal *tz = thermal_zone_device_priv(thermal);
4999caaad2cSRafael J. Wysocki 	struct acpi_thermal_trip *acpi_trip;
5008289d810SRafael J. Wysocki 	int t;
501601f3d42SZhang Rui 
5028289d810SRafael J. Wysocki 	if (!tz || !trip)
503601f3d42SZhang Rui 		return -EINVAL;
504601f3d42SZhang Rui 
5058289d810SRafael J. Wysocki 	acpi_trip = trip->priv;
5068289d810SRafael J. Wysocki 	if (!acpi_trip || !acpi_trip->valid)
50794a40931SZhang Rui 		return -EINVAL;
50894a40931SZhang Rui 
5098289d810SRafael J. Wysocki 	switch (trip->type) {
5108289d810SRafael J. Wysocki 	case THERMAL_TRIP_PASSIVE:
5119caaad2cSRafael J. Wysocki 		t = tz->trips.passive.tc1 * (tz->temperature -
5129caaad2cSRafael J. Wysocki 						tz->last_temperature) +
5139caaad2cSRafael J. Wysocki 			tz->trips.passive.tc2 * (tz->temperature -
5149caaad2cSRafael J. Wysocki 						acpi_trip->temperature);
5159caaad2cSRafael J. Wysocki 		if (t > 0)
5164ae46befSZhang Rui 			*trend = THERMAL_TREND_RAISING;
5179caaad2cSRafael J. Wysocki 		else if (t < 0)
518601f3d42SZhang Rui 			*trend = THERMAL_TREND_DROPPING;
519601f3d42SZhang Rui 		else
520601f3d42SZhang Rui 			*trend = THERMAL_TREND_STABLE;
52152ce5049SRafael J. Wysocki 
522601f3d42SZhang Rui 		return 0;
523601f3d42SZhang Rui 
5248289d810SRafael J. Wysocki 	case THERMAL_TRIP_ACTIVE:
5259caaad2cSRafael J. Wysocki 		t = acpi_thermal_temp(tz, tz->temperature);
5268289d810SRafael J. Wysocki 		if (t <= trip->temperature)
5279caaad2cSRafael J. Wysocki 			break;
5288289d810SRafael J. Wysocki 
5298289d810SRafael J. Wysocki 		*trend = THERMAL_TREND_RAISING;
5308289d810SRafael J. Wysocki 
5318289d810SRafael J. Wysocki 		return 0;
5328289d810SRafael J. Wysocki 
5338289d810SRafael J. Wysocki 	default:
5348289d810SRafael J. Wysocki 		break;
5359caaad2cSRafael J. Wysocki 	}
5369caaad2cSRafael J. Wysocki 
5379caaad2cSRafael J. Wysocki 	return -EINVAL;
5389caaad2cSRafael J. Wysocki }
5399caaad2cSRafael J. Wysocki 
acpi_thermal_zone_device_hot(struct thermal_zone_device * thermal)540a73cb202SDaniel Lezcano static void acpi_thermal_zone_device_hot(struct thermal_zone_device *thermal)
541b1569e99SMatthew Garrett {
5423d4e1badSDaniel Lezcano 	struct acpi_thermal *tz = thermal_zone_device_priv(thermal);
543b1569e99SMatthew Garrett 
544a73cb202SDaniel Lezcano 	acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
545a73cb202SDaniel Lezcano 					dev_name(&tz->device->dev),
546a73cb202SDaniel Lezcano 					ACPI_THERMAL_NOTIFY_HOT, 1);
547a73cb202SDaniel Lezcano }
548a73cb202SDaniel Lezcano 
acpi_thermal_zone_device_critical(struct thermal_zone_device * thermal)549a73cb202SDaniel Lezcano static void acpi_thermal_zone_device_critical(struct thermal_zone_device *thermal)
550a73cb202SDaniel Lezcano {
5513d4e1badSDaniel Lezcano 	struct acpi_thermal *tz = thermal_zone_device_priv(thermal);
552b1569e99SMatthew Garrett 
553b1569e99SMatthew Garrett 	acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
554a73cb202SDaniel Lezcano 					dev_name(&tz->device->dev),
555a73cb202SDaniel Lezcano 					ACPI_THERMAL_NOTIFY_CRITICAL, 1);
556b1569e99SMatthew Garrett 
557a73cb202SDaniel Lezcano 	thermal_zone_device_critical(thermal);
558b1569e99SMatthew Garrett }
559b1569e99SMatthew Garrett 
acpi_thermal_cooling_device_cb(struct thermal_zone_device * thermal,struct thermal_cooling_device * cdev,bool bind)5603f655ef8SZhang Rui static int acpi_thermal_cooling_device_cb(struct thermal_zone_device *thermal,
5613f655ef8SZhang Rui 					  struct thermal_cooling_device *cdev,
5629d99842fSZhang Rui 					  bool bind)
5633f655ef8SZhang Rui {
5643f655ef8SZhang Rui 	struct acpi_device *device = cdev->devdata;
5653d4e1badSDaniel Lezcano 	struct acpi_thermal *tz = thermal_zone_device_priv(thermal);
566653a00c9SZhang Rui 	struct acpi_device *dev;
567653a00c9SZhang Rui 	acpi_handle handle;
5683f655ef8SZhang Rui 	int i;
5693f655ef8SZhang Rui 	int j;
5703f655ef8SZhang Rui 	int trip = -1;
5713f655ef8SZhang Rui 	int result = 0;
5723f655ef8SZhang Rui 
5737266c88cSRafael J. Wysocki 	if (tz->trips.critical.valid)
5743f655ef8SZhang Rui 		trip++;
5753f655ef8SZhang Rui 
5767266c88cSRafael J. Wysocki 	if (tz->trips.hot.valid)
5773f655ef8SZhang Rui 		trip++;
5783f655ef8SZhang Rui 
57968b77785SRafael J. Wysocki 	if (tz->trips.passive.trip.valid) {
5803f655ef8SZhang Rui 		trip++;
58152ce5049SRafael J. Wysocki 		for (i = 0; i < tz->trips.passive.devices.count; i++) {
582653a00c9SZhang Rui 			handle = tz->trips.passive.devices.handles[i];
58399ece713SRafael J. Wysocki 			dev = acpi_fetch_acpi_dev(handle);
58499ece713SRafael J. Wysocki 			if (dev != device)
5859d99842fSZhang Rui 				continue;
58652ce5049SRafael J. Wysocki 
5879d99842fSZhang Rui 			if (bind)
58852ce5049SRafael J. Wysocki 				result = thermal_zone_bind_cooling_device(
58952ce5049SRafael J. Wysocki 						thermal, trip, cdev,
59052ce5049SRafael J. Wysocki 						THERMAL_NO_LIMIT,
59152ce5049SRafael J. Wysocki 						THERMAL_NO_LIMIT,
5926cd9e9f6SKapileshwar Singh 						THERMAL_WEIGHT_DEFAULT);
5939d99842fSZhang Rui 			else
5949d99842fSZhang Rui 				result =
59552ce5049SRafael J. Wysocki 					thermal_zone_unbind_cooling_device(
59652ce5049SRafael J. Wysocki 						thermal, trip, cdev);
59752ce5049SRafael J. Wysocki 
5983f655ef8SZhang Rui 			if (result)
5993f655ef8SZhang Rui 				goto failed;
6003f655ef8SZhang Rui 		}
6013f655ef8SZhang Rui 	}
6023f655ef8SZhang Rui 
6033f655ef8SZhang Rui 	for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
60468b77785SRafael J. Wysocki 		if (!tz->trips.active[i].trip.valid)
6053f655ef8SZhang Rui 			break;
60652ce5049SRafael J. Wysocki 
6073f655ef8SZhang Rui 		trip++;
60852ce5049SRafael J. Wysocki 		for (j = 0; j < tz->trips.active[i].devices.count; j++) {
609653a00c9SZhang Rui 			handle = tz->trips.active[i].devices.handles[j];
61099ece713SRafael J. Wysocki 			dev = acpi_fetch_acpi_dev(handle);
61199ece713SRafael J. Wysocki 			if (dev != device)
6129d99842fSZhang Rui 				continue;
61352ce5049SRafael J. Wysocki 
6149d99842fSZhang Rui 			if (bind)
61552ce5049SRafael J. Wysocki 				result = thermal_zone_bind_cooling_device(
61652ce5049SRafael J. Wysocki 						thermal, trip, cdev,
61752ce5049SRafael J. Wysocki 						THERMAL_NO_LIMIT,
61852ce5049SRafael J. Wysocki 						THERMAL_NO_LIMIT,
6196cd9e9f6SKapileshwar Singh 						THERMAL_WEIGHT_DEFAULT);
6209d99842fSZhang Rui 			else
62152ce5049SRafael J. Wysocki 				result = thermal_zone_unbind_cooling_device(
62252ce5049SRafael J. Wysocki 						thermal, trip, cdev);
62352ce5049SRafael J. Wysocki 
6243f655ef8SZhang Rui 			if (result)
6253f655ef8SZhang Rui 				goto failed;
6263f655ef8SZhang Rui 		}
6273f655ef8SZhang Rui 	}
6283f655ef8SZhang Rui 
6293f655ef8SZhang Rui failed:
6303f655ef8SZhang Rui 	return result;
6313f655ef8SZhang Rui }
6323f655ef8SZhang Rui 
6333f655ef8SZhang Rui static int
acpi_thermal_bind_cooling_device(struct thermal_zone_device * thermal,struct thermal_cooling_device * cdev)6343f655ef8SZhang Rui acpi_thermal_bind_cooling_device(struct thermal_zone_device *thermal,
6353f655ef8SZhang Rui 				 struct thermal_cooling_device *cdev)
6363f655ef8SZhang Rui {
6379d99842fSZhang Rui 	return acpi_thermal_cooling_device_cb(thermal, cdev, true);
6383f655ef8SZhang Rui }
6393f655ef8SZhang Rui 
6403f655ef8SZhang Rui static int
acpi_thermal_unbind_cooling_device(struct thermal_zone_device * thermal,struct thermal_cooling_device * cdev)6413f655ef8SZhang Rui acpi_thermal_unbind_cooling_device(struct thermal_zone_device *thermal,
6423f655ef8SZhang Rui 				   struct thermal_cooling_device *cdev)
6433f655ef8SZhang Rui {
6449d99842fSZhang Rui 	return acpi_thermal_cooling_device_cb(thermal, cdev, false);
6453f655ef8SZhang Rui }
6463f655ef8SZhang Rui 
647ec9c9c2eSEmil Goode static struct thermal_zone_device_ops acpi_thermal_zone_ops = {
6483f655ef8SZhang Rui 	.bind = acpi_thermal_bind_cooling_device,
6493f655ef8SZhang Rui 	.unbind	= acpi_thermal_unbind_cooling_device,
6503f655ef8SZhang Rui 	.get_temp = thermal_get_temp,
651601f3d42SZhang Rui 	.get_trend = thermal_get_trend,
652a73cb202SDaniel Lezcano 	.hot = acpi_thermal_zone_device_hot,
653a73cb202SDaniel Lezcano 	.critical = acpi_thermal_zone_device_critical,
6543f655ef8SZhang Rui };
6553f655ef8SZhang Rui 
acpi_thermal_zone_sysfs_add(struct acpi_thermal * tz)656a4b81715SDaniel Lezcano static int acpi_thermal_zone_sysfs_add(struct acpi_thermal *tz)
657a4b81715SDaniel Lezcano {
658a4b81715SDaniel Lezcano 	struct device *tzdev = thermal_zone_device(tz->thermal_zone);
659a4b81715SDaniel Lezcano 	int ret;
660a4b81715SDaniel Lezcano 
661a4b81715SDaniel Lezcano 	ret = sysfs_create_link(&tz->device->dev.kobj,
662a4b81715SDaniel Lezcano 				&tzdev->kobj, "thermal_zone");
663a4b81715SDaniel Lezcano 	if (ret)
664a4b81715SDaniel Lezcano 		return ret;
665a4b81715SDaniel Lezcano 
666a4b81715SDaniel Lezcano 	ret = sysfs_create_link(&tzdev->kobj,
667a4b81715SDaniel Lezcano 				   &tz->device->dev.kobj, "device");
668a4b81715SDaniel Lezcano 	if (ret)
669a4b81715SDaniel Lezcano 		sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
670a4b81715SDaniel Lezcano 
671a4b81715SDaniel Lezcano 	return ret;
672a4b81715SDaniel Lezcano }
673a4b81715SDaniel Lezcano 
acpi_thermal_zone_sysfs_remove(struct acpi_thermal * tz)674a4b81715SDaniel Lezcano static void acpi_thermal_zone_sysfs_remove(struct acpi_thermal *tz)
675a4b81715SDaniel Lezcano {
676a4b81715SDaniel Lezcano 	struct device *tzdev = thermal_zone_device(tz->thermal_zone);
677a4b81715SDaniel Lezcano 
678a4b81715SDaniel Lezcano 	sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
679a4b81715SDaniel Lezcano 	sysfs_remove_link(&tzdev->kobj, "device");
680a4b81715SDaniel Lezcano }
681a4b81715SDaniel Lezcano 
acpi_thermal_register_thermal_zone(struct acpi_thermal * tz)6823f655ef8SZhang Rui static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz)
6833f655ef8SZhang Rui {
684ec23c1c4SRafael J. Wysocki 	struct acpi_thermal_trip *acpi_trip;
685ec23c1c4SRafael J. Wysocki 	struct thermal_trip *trip;
686bf07b4a3SRafael J. Wysocki 	int passive_delay = 0;
687bf07b4a3SRafael J. Wysocki 	int trip_count = 0;
6883f655ef8SZhang Rui 	int result;
6893f655ef8SZhang Rui 	int i;
6903f655ef8SZhang Rui 
6917266c88cSRafael J. Wysocki 	if (tz->trips.critical.valid)
692bf07b4a3SRafael J. Wysocki 		trip_count++;
6933f655ef8SZhang Rui 
6947266c88cSRafael J. Wysocki 	if (tz->trips.hot.valid)
695bf07b4a3SRafael J. Wysocki 		trip_count++;
6963f655ef8SZhang Rui 
69768b77785SRafael J. Wysocki 	if (tz->trips.passive.trip.valid) {
698bf07b4a3SRafael J. Wysocki 		trip_count++;
699bf07b4a3SRafael J. Wysocki 		passive_delay = tz->trips.passive.tsp * 100;
700bf07b4a3SRafael J. Wysocki 	}
7013f655ef8SZhang Rui 
70268b77785SRafael J. Wysocki 	for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE && tz->trips.active[i].trip.valid; i++)
703bf07b4a3SRafael J. Wysocki 		trip_count++;
704b1569e99SMatthew Garrett 
705ec23c1c4SRafael J. Wysocki 	trip = kcalloc(trip_count, sizeof(*trip), GFP_KERNEL);
706ec23c1c4SRafael J. Wysocki 	if (!trip)
707ec23c1c4SRafael J. Wysocki 		return -ENOMEM;
708ec23c1c4SRafael J. Wysocki 
709ec23c1c4SRafael J. Wysocki 	tz->trip_table = trip;
710ec23c1c4SRafael J. Wysocki 
711ec23c1c4SRafael J. Wysocki 	if (tz->trips.critical.valid) {
712ec23c1c4SRafael J. Wysocki 		trip->type = THERMAL_TRIP_CRITICAL;
713ec23c1c4SRafael J. Wysocki 		trip->temperature = acpi_thermal_temp(tz, tz->trips.critical.temperature);
714ec23c1c4SRafael J. Wysocki 		trip++;
715ec23c1c4SRafael J. Wysocki 	}
716ec23c1c4SRafael J. Wysocki 
717ec23c1c4SRafael J. Wysocki 	if (tz->trips.hot.valid) {
718ec23c1c4SRafael J. Wysocki 		trip->type = THERMAL_TRIP_HOT;
719ec23c1c4SRafael J. Wysocki 		trip->temperature = acpi_thermal_temp(tz, tz->trips.hot.temperature);
720ec23c1c4SRafael J. Wysocki 		trip++;
721ec23c1c4SRafael J. Wysocki 	}
722ec23c1c4SRafael J. Wysocki 
723ec23c1c4SRafael J. Wysocki 	acpi_trip = &tz->trips.passive.trip;
724ec23c1c4SRafael J. Wysocki 	if (acpi_trip->valid) {
725ec23c1c4SRafael J. Wysocki 		trip->type = THERMAL_TRIP_PASSIVE;
726ec23c1c4SRafael J. Wysocki 		trip->temperature = acpi_thermal_temp(tz, acpi_trip->temperature);
727ec23c1c4SRafael J. Wysocki 		trip->priv = acpi_trip;
728ec23c1c4SRafael J. Wysocki 		trip++;
729ec23c1c4SRafael J. Wysocki 	}
730ec23c1c4SRafael J. Wysocki 
731ec23c1c4SRafael J. Wysocki 	for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
732ec23c1c4SRafael J. Wysocki 		acpi_trip = &tz->trips.active[i].trip;
733ec23c1c4SRafael J. Wysocki 
734ec23c1c4SRafael J. Wysocki 		if (!acpi_trip->valid)
735ec23c1c4SRafael J. Wysocki 			break;
736ec23c1c4SRafael J. Wysocki 
737ec23c1c4SRafael J. Wysocki 		trip->type = THERMAL_TRIP_ACTIVE;
738ec23c1c4SRafael J. Wysocki 		trip->temperature = acpi_thermal_temp(tz, acpi_trip->temperature);
739ec23c1c4SRafael J. Wysocki 		trip->priv = acpi_trip;
740ec23c1c4SRafael J. Wysocki 		trip++;
741ec23c1c4SRafael J. Wysocki 	}
742ec23c1c4SRafael J. Wysocki 
743ec23c1c4SRafael J. Wysocki 	tz->thermal_zone = thermal_zone_device_register_with_trips("acpitz",
744ec23c1c4SRafael J. Wysocki 								   tz->trip_table,
745ec23c1c4SRafael J. Wysocki 								   trip_count,
746ec23c1c4SRafael J. Wysocki 								   0, tz,
747ec23c1c4SRafael J. Wysocki 								   &acpi_thermal_zone_ops,
748ec23c1c4SRafael J. Wysocki 								   NULL,
749ec23c1c4SRafael J. Wysocki 								   passive_delay,
750b1569e99SMatthew Garrett 								   tz->polling_frequency * 100);
751ec23c1c4SRafael J. Wysocki 	if (IS_ERR(tz->thermal_zone)) {
752ec23c1c4SRafael J. Wysocki 		result = PTR_ERR(tz->thermal_zone);
753ec23c1c4SRafael J. Wysocki 		goto free_trip_table;
754ec23c1c4SRafael J. Wysocki 	}
7553f655ef8SZhang Rui 
756a4b81715SDaniel Lezcano 	result = acpi_thermal_zone_sysfs_add(tz);
7573f655ef8SZhang Rui 	if (result)
758172066ccSAndrzej Pietrasiewicz 		goto unregister_tzd;
7593f655ef8SZhang Rui 
7607f4957beSAndrzej Pietrasiewicz 	result = thermal_zone_device_enable(tz->thermal_zone);
7617f4957beSAndrzej Pietrasiewicz 	if (result)
762868a4715SRafael J. Wysocki 		goto remove_links;
7633f655ef8SZhang Rui 
764fc3a8828SGreg Kroah-Hartman 	dev_info(&tz->device->dev, "registered as thermal_zone%d\n",
7653034f859SDaniel Lezcano 		 thermal_zone_device_id(tz->thermal_zone));
766172066ccSAndrzej Pietrasiewicz 
7673f655ef8SZhang Rui 	return 0;
768172066ccSAndrzej Pietrasiewicz 
769a4b81715SDaniel Lezcano remove_links:
770a4b81715SDaniel Lezcano 	acpi_thermal_zone_sysfs_remove(tz);
771172066ccSAndrzej Pietrasiewicz unregister_tzd:
772172066ccSAndrzej Pietrasiewicz 	thermal_zone_device_unregister(tz->thermal_zone);
773ec23c1c4SRafael J. Wysocki free_trip_table:
774ec23c1c4SRafael J. Wysocki 	kfree(tz->trip_table);
775172066ccSAndrzej Pietrasiewicz 
776172066ccSAndrzej Pietrasiewicz 	return result;
7773f655ef8SZhang Rui }
7783f655ef8SZhang Rui 
acpi_thermal_unregister_thermal_zone(struct acpi_thermal * tz)7793f655ef8SZhang Rui static void acpi_thermal_unregister_thermal_zone(struct acpi_thermal *tz)
7803f655ef8SZhang Rui {
781*17f44960SDan Carpenter 	thermal_zone_device_disable(tz->thermal_zone);
782a4b81715SDaniel Lezcano 	acpi_thermal_zone_sysfs_remove(tz);
7833f655ef8SZhang Rui 	thermal_zone_device_unregister(tz->thermal_zone);
7843f655ef8SZhang Rui 	tz->thermal_zone = NULL;
7853f655ef8SZhang Rui }
7863f655ef8SZhang Rui 
7873f655ef8SZhang Rui 
7881da177e4SLinus Torvalds /* --------------------------------------------------------------------------
7891da177e4SLinus Torvalds                                  Driver Interface
7901da177e4SLinus Torvalds    -------------------------------------------------------------------------- */
7911da177e4SLinus Torvalds 
acpi_thermal_notify(acpi_handle handle,u32 event,void * data)7929d67b6acSMichal Wilczynski static void acpi_thermal_notify(acpi_handle handle, u32 event, void *data)
7931da177e4SLinus Torvalds {
7949d67b6acSMichal Wilczynski 	struct acpi_device *device = data;
795342d550dSBjorn Helgaas 	struct acpi_thermal *tz = acpi_driver_data(device);
7961da177e4SLinus Torvalds 
7971da177e4SLinus Torvalds 	if (!tz)
798d550d98dSPatrick Mochel 		return;
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds 	switch (event) {
8011da177e4SLinus Torvalds 	case ACPI_THERMAL_NOTIFY_TEMPERATURE:
80281b704d3SRafael J. Wysocki 		acpi_queue_thermal_check(tz);
8031da177e4SLinus Torvalds 		break;
8041da177e4SLinus Torvalds 	case ACPI_THERMAL_NOTIFY_THRESHOLDS:
8051da177e4SLinus Torvalds 	case ACPI_THERMAL_NOTIFY_DEVICES:
8064ab4b3b1SRafael J. Wysocki 		acpi_thermal_trips_update(tz, event);
8071da177e4SLinus Torvalds 		break;
8081da177e4SLinus Torvalds 	default:
809f86b15a1SRafael J. Wysocki 		acpi_handle_debug(device->handle, "Unsupported event [0x%x]\n",
810f86b15a1SRafael J. Wysocki 				  event);
8111da177e4SLinus Torvalds 		break;
8121da177e4SLinus Torvalds 	}
8131da177e4SLinus Torvalds }
8141da177e4SLinus Torvalds 
815261cba2dSZhang Rui /*
816261cba2dSZhang Rui  * On some platforms, the AML code has dependency about
817261cba2dSZhang Rui  * the evaluating order of _TMP and _CRT/_HOT/_PSV/_ACx.
818261cba2dSZhang Rui  * 1. On HP Pavilion G4-1016tx, _TMP must be invoked after
819261cba2dSZhang Rui  *    /_CRT/_HOT/_PSV/_ACx, or else system will be power off.
820261cba2dSZhang Rui  * 2. On HP Compaq 6715b/6715s, the return value of _PSV is 0
821261cba2dSZhang Rui  *    if _TMP has never been evaluated.
822261cba2dSZhang Rui  *
823261cba2dSZhang Rui  * As this dependency is totally transparent to OS, evaluate
824261cba2dSZhang Rui  * all of them once, in the order of _CRT/_HOT/_PSV/_ACx,
825261cba2dSZhang Rui  * _TMP, before they are actually used.
826261cba2dSZhang Rui  */
acpi_thermal_aml_dependency_fix(struct acpi_thermal * tz)827261cba2dSZhang Rui static void acpi_thermal_aml_dependency_fix(struct acpi_thermal *tz)
828261cba2dSZhang Rui {
829261cba2dSZhang Rui 	acpi_handle handle = tz->device->handle;
830261cba2dSZhang Rui 	unsigned long long value;
831261cba2dSZhang Rui 	int i;
832261cba2dSZhang Rui 
833261cba2dSZhang Rui 	acpi_evaluate_integer(handle, "_CRT", NULL, &value);
834261cba2dSZhang Rui 	acpi_evaluate_integer(handle, "_HOT", NULL, &value);
835261cba2dSZhang Rui 	acpi_evaluate_integer(handle, "_PSV", NULL, &value);
836261cba2dSZhang Rui 	for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
837261cba2dSZhang Rui 		char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
838261cba2dSZhang Rui 		acpi_status status;
839261cba2dSZhang Rui 
840261cba2dSZhang Rui 		status = acpi_evaluate_integer(handle, name, NULL, &value);
841261cba2dSZhang Rui 		if (status == AE_NOT_FOUND)
842261cba2dSZhang Rui 			break;
843261cba2dSZhang Rui 	}
844261cba2dSZhang Rui 	acpi_evaluate_integer(handle, "_TMP", NULL, &value);
845261cba2dSZhang Rui }
846261cba2dSZhang Rui 
acpi_thermal_get_info(struct acpi_thermal * tz)8474be44fcdSLen Brown static int acpi_thermal_get_info(struct acpi_thermal *tz)
8481da177e4SLinus Torvalds {
84936f55404SRafael J. Wysocki 	int result;
8501da177e4SLinus Torvalds 
8511da177e4SLinus Torvalds 	if (!tz)
852d550d98dSPatrick Mochel 		return -EINVAL;
8531da177e4SLinus Torvalds 
854261cba2dSZhang Rui 	acpi_thermal_aml_dependency_fix(tz);
855261cba2dSZhang Rui 
8569bcb8118SMatthew Garrett 	/* Get trip points [_CRT, _PSV, etc.] (required) */
8579bcb8118SMatthew Garrett 	result = acpi_thermal_get_trip_points(tz);
8581da177e4SLinus Torvalds 	if (result)
859d550d98dSPatrick Mochel 		return result;
8601da177e4SLinus Torvalds 
8619bcb8118SMatthew Garrett 	/* Get temperature [_TMP] (required) */
8629bcb8118SMatthew Garrett 	result = acpi_thermal_get_temperature(tz);
8631da177e4SLinus Torvalds 	if (result)
864d550d98dSPatrick Mochel 		return result;
8651da177e4SLinus Torvalds 
8661da177e4SLinus Torvalds 	/* Set the cooling mode [_SCP] to active cooling (default) */
867c31b3a1bSRafael J. Wysocki 	acpi_execute_simple_method(tz->device->handle, "_SCP",
868c31b3a1bSRafael J. Wysocki 				   ACPI_THERMAL_MODE_ACTIVE);
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds 	/* Get default polling frequency [_TZP] (optional) */
8711da177e4SLinus Torvalds 	if (tzp)
8721da177e4SLinus Torvalds 		tz->polling_frequency = tzp;
8731da177e4SLinus Torvalds 	else
8741da177e4SLinus Torvalds 		acpi_thermal_get_polling_frequency(tz);
8751da177e4SLinus Torvalds 
876d550d98dSPatrick Mochel 	return 0;
8771da177e4SLinus Torvalds }
8781da177e4SLinus Torvalds 
87913614e37SJean Delvare /*
88013614e37SJean Delvare  * The exact offset between Kelvin and degree Celsius is 273.15. However ACPI
88113614e37SJean Delvare  * handles temperature values with a single decimal place. As a consequence,
88213614e37SJean Delvare  * some implementations use an offset of 273.1 and others use an offset of
88313614e37SJean Delvare  * 273.2. Try to find out which one is being used, to present the most
88413614e37SJean Delvare  * accurate and visually appealing number.
88513614e37SJean Delvare  *
88613614e37SJean Delvare  * The heuristic below should work for all ACPI thermal zones which have a
88713614e37SJean Delvare  * critical trip point with a value being a multiple of 0.5 degree Celsius.
88813614e37SJean Delvare  */
acpi_thermal_guess_offset(struct acpi_thermal * tz)88913614e37SJean Delvare static void acpi_thermal_guess_offset(struct acpi_thermal *tz)
89013614e37SJean Delvare {
8917266c88cSRafael J. Wysocki 	if (tz->trips.critical.valid &&
89213614e37SJean Delvare 	    (tz->trips.critical.temperature % 5) == 1)
8937f49a5cbSAkinobu Mita 		tz->kelvin_offset = 273100;
89413614e37SJean Delvare 	else
8957f49a5cbSAkinobu Mita 		tz->kelvin_offset = 273200;
89613614e37SJean Delvare }
89713614e37SJean Delvare 
acpi_thermal_check_fn(struct work_struct * work)898a59ffb20SAaron Lu static void acpi_thermal_check_fn(struct work_struct *work)
899a59ffb20SAaron Lu {
900a59ffb20SAaron Lu 	struct acpi_thermal *tz = container_of(work, struct acpi_thermal,
901a59ffb20SAaron Lu 					       thermal_check_work);
90281b704d3SRafael J. Wysocki 
90381b704d3SRafael J. Wysocki 	/*
90481b704d3SRafael J. Wysocki 	 * In general, it is not sufficient to check the pending bit, because
90581b704d3SRafael J. Wysocki 	 * subsequent instances of this function may be queued after one of them
90681b704d3SRafael J. Wysocki 	 * has started running (e.g. if _TMP sleeps).  Avoid bailing out if just
90781b704d3SRafael J. Wysocki 	 * one of them is running, though, because it may have done the actual
90881b704d3SRafael J. Wysocki 	 * check some time ago, so allow at least one of them to block on the
90981b704d3SRafael J. Wysocki 	 * mutex while another one is running the update.
91081b704d3SRafael J. Wysocki 	 */
91181b704d3SRafael J. Wysocki 	if (!refcount_dec_not_one(&tz->thermal_check_count))
91281b704d3SRafael J. Wysocki 		return;
91381b704d3SRafael J. Wysocki 
91481b704d3SRafael J. Wysocki 	mutex_lock(&tz->thermal_check_lock);
91581b704d3SRafael J. Wysocki 
91681b704d3SRafael J. Wysocki 	thermal_zone_device_update(tz->thermal_zone, THERMAL_EVENT_UNSPECIFIED);
91781b704d3SRafael J. Wysocki 
91881b704d3SRafael J. Wysocki 	refcount_inc(&tz->thermal_check_count);
91981b704d3SRafael J. Wysocki 
92081b704d3SRafael J. Wysocki 	mutex_unlock(&tz->thermal_check_lock);
921a59ffb20SAaron Lu }
922a59ffb20SAaron Lu 
acpi_thermal_add(struct acpi_device * device)9234be44fcdSLen Brown static int acpi_thermal_add(struct acpi_device *device)
9241da177e4SLinus Torvalds {
92536f55404SRafael J. Wysocki 	struct acpi_thermal *tz;
92636f55404SRafael J. Wysocki 	int result;
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds 	if (!device)
929d550d98dSPatrick Mochel 		return -EINVAL;
9301da177e4SLinus Torvalds 
93136bcbec7SBurman Yan 	tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
9321da177e4SLinus Torvalds 	if (!tz)
933d550d98dSPatrick Mochel 		return -ENOMEM;
9341da177e4SLinus Torvalds 
9358348e1b1SPatrick Mochel 	tz->device = device;
9361da177e4SLinus Torvalds 	strcpy(tz->name, device->pnp.bus_id);
9371da177e4SLinus Torvalds 	strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
9381da177e4SLinus Torvalds 	strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
939db89b4f0SPavel Machek 	device->driver_data = tz;
9403f655ef8SZhang Rui 
9411da177e4SLinus Torvalds 	result = acpi_thermal_get_info(tz);
9421da177e4SLinus Torvalds 	if (result)
9433f655ef8SZhang Rui 		goto free_memory;
9443f655ef8SZhang Rui 
94513614e37SJean Delvare 	acpi_thermal_guess_offset(tz);
94613614e37SJean Delvare 
9473f655ef8SZhang Rui 	result = acpi_thermal_register_thermal_zone(tz);
9483f655ef8SZhang Rui 	if (result)
9493f655ef8SZhang Rui 		goto free_memory;
9501da177e4SLinus Torvalds 
95181b704d3SRafael J. Wysocki 	refcount_set(&tz->thermal_check_count, 3);
95281b704d3SRafael J. Wysocki 	mutex_init(&tz->thermal_check_lock);
953a59ffb20SAaron Lu 	INIT_WORK(&tz->thermal_check_work, acpi_thermal_check_fn);
954a59ffb20SAaron Lu 
955f86b15a1SRafael J. Wysocki 	pr_info("%s [%s] (%ld C)\n", acpi_device_name(device),
9567f49a5cbSAkinobu Mita 		acpi_device_bid(device), deci_kelvin_to_celsius(tz->temperature));
9571da177e4SLinus Torvalds 
9589d67b6acSMichal Wilczynski 	result = acpi_dev_install_notify_handler(device, ACPI_DEVICE_NOTIFY,
9599d67b6acSMichal Wilczynski 						 acpi_thermal_notify);
9609d67b6acSMichal Wilczynski 	if (result)
9619d67b6acSMichal Wilczynski 		goto flush_wq;
9629d67b6acSMichal Wilczynski 
9639d67b6acSMichal Wilczynski 	return 0;
9649d67b6acSMichal Wilczynski 
9659d67b6acSMichal Wilczynski flush_wq:
9669d67b6acSMichal Wilczynski 	flush_workqueue(acpi_thermal_pm_queue);
9679d67b6acSMichal Wilczynski 	acpi_thermal_unregister_thermal_zone(tz);
9683f655ef8SZhang Rui free_memory:
9691da177e4SLinus Torvalds 	kfree(tz);
9709d67b6acSMichal Wilczynski 
971d550d98dSPatrick Mochel 	return result;
9721da177e4SLinus Torvalds }
9731da177e4SLinus Torvalds 
acpi_thermal_remove(struct acpi_device * device)9746c0eb5baSDawei Li static void acpi_thermal_remove(struct acpi_device *device)
9751da177e4SLinus Torvalds {
97636f55404SRafael J. Wysocki 	struct acpi_thermal *tz;
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds 	if (!device || !acpi_driver_data(device))
9796c0eb5baSDawei Li 		return;
9801da177e4SLinus Torvalds 
98150dd0969SJan Engelhardt 	tz = acpi_driver_data(device);
9821da177e4SLinus Torvalds 
9839d67b6acSMichal Wilczynski 	acpi_dev_remove_notify_handler(device, ACPI_DEVICE_NOTIFY,
9849d67b6acSMichal Wilczynski 				       acpi_thermal_notify);
9859d67b6acSMichal Wilczynski 
9869d67b6acSMichal Wilczynski 	flush_workqueue(acpi_thermal_pm_queue);
9873f655ef8SZhang Rui 	acpi_thermal_unregister_thermal_zone(tz);
988*17f44960SDan Carpenter 	kfree(tz->trip_table);
9891da177e4SLinus Torvalds 	kfree(tz);
9901da177e4SLinus Torvalds }
9911da177e4SLinus Torvalds 
99290692404SRafael J. Wysocki #ifdef CONFIG_PM_SLEEP
acpi_thermal_suspend(struct device * dev)993a59ffb20SAaron Lu static int acpi_thermal_suspend(struct device *dev)
994a59ffb20SAaron Lu {
995a59ffb20SAaron Lu 	/* Make sure the previously queued thermal check work has been done */
996a59ffb20SAaron Lu 	flush_workqueue(acpi_thermal_pm_queue);
997a59ffb20SAaron Lu 	return 0;
998a59ffb20SAaron Lu }
999a59ffb20SAaron Lu 
acpi_thermal_resume(struct device * dev)1000167cffb6SRafael J. Wysocki static int acpi_thermal_resume(struct device *dev)
100174ce1468SKonstantin Karasyov {
1002167cffb6SRafael J. Wysocki 	struct acpi_thermal *tz;
1003eca3f0a8SRafael J. Wysocki 	int i, j, power_state;
1004b1028c54SKonstantin Karasyov 
1005167cffb6SRafael J. Wysocki 	if (!dev)
1006d550d98dSPatrick Mochel 		return -EINVAL;
100774ce1468SKonstantin Karasyov 
1008167cffb6SRafael J. Wysocki 	tz = acpi_driver_data(to_acpi_device(dev));
1009167cffb6SRafael J. Wysocki 	if (!tz)
1010167cffb6SRafael J. Wysocki 		return -EINVAL;
101174ce1468SKonstantin Karasyov 
1012bed936f7SKonstantin Karasyov 	for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
101368b77785SRafael J. Wysocki 		if (!tz->trips.active[i].trip.valid)
1014b1028c54SKonstantin Karasyov 			break;
101552ce5049SRafael J. Wysocki 
1016b1028c54SKonstantin Karasyov 		for (j = 0; j < tz->trips.active[i].devices.count; j++) {
1017eca3f0a8SRafael J. Wysocki 			acpi_bus_update_power(tz->trips.active[i].devices.handles[j],
1018488a76c5SRafael J. Wysocki 					      &power_state);
1019bed936f7SKonstantin Karasyov 		}
1020b1028c54SKonstantin Karasyov 	}
1021bed936f7SKonstantin Karasyov 
102281b704d3SRafael J. Wysocki 	acpi_queue_thermal_check(tz);
102374ce1468SKonstantin Karasyov 
102474ce1468SKonstantin Karasyov 	return AE_OK;
102574ce1468SKonstantin Karasyov }
1026607d265fSRafael J. Wysocki #else
1027607d265fSRafael J. Wysocki #define acpi_thermal_suspend	NULL
1028607d265fSRafael J. Wysocki #define acpi_thermal_resume	NULL
102990692404SRafael J. Wysocki #endif
1030607d265fSRafael J. Wysocki static SIMPLE_DEV_PM_OPS(acpi_thermal_pm, acpi_thermal_suspend, acpi_thermal_resume);
1031607d265fSRafael J. Wysocki 
1032607d265fSRafael J. Wysocki static const struct acpi_device_id  thermal_device_ids[] = {
1033607d265fSRafael J. Wysocki 	{ACPI_THERMAL_HID, 0},
1034607d265fSRafael J. Wysocki 	{"", 0},
1035607d265fSRafael J. Wysocki };
1036607d265fSRafael J. Wysocki MODULE_DEVICE_TABLE(acpi, thermal_device_ids);
1037607d265fSRafael J. Wysocki 
1038607d265fSRafael J. Wysocki static struct acpi_driver acpi_thermal_driver = {
1039607d265fSRafael J. Wysocki 	.name = "thermal",
1040607d265fSRafael J. Wysocki 	.class = ACPI_THERMAL_CLASS,
1041607d265fSRafael J. Wysocki 	.ids = thermal_device_ids,
1042607d265fSRafael J. Wysocki 	.ops = {
1043607d265fSRafael J. Wysocki 		.add = acpi_thermal_add,
1044607d265fSRafael J. Wysocki 		.remove = acpi_thermal_remove,
1045607d265fSRafael J. Wysocki 		},
1046607d265fSRafael J. Wysocki 	.drv.pm = &acpi_thermal_pm,
1047607d265fSRafael J. Wysocki };
104874ce1468SKonstantin Karasyov 
thermal_act(const struct dmi_system_id * d)10491855256cSJeff Garzik static int thermal_act(const struct dmi_system_id *d) {
10500b5bfa1cSLen Brown 	if (act == 0) {
1051f86b15a1SRafael J. Wysocki 		pr_notice("%s detected: disabling all active thermal trip points\n",
1052f86b15a1SRafael J. Wysocki 			  d->ident);
10530b5bfa1cSLen Brown 		act = -1;
10540b5bfa1cSLen Brown 	}
10550b5bfa1cSLen Brown 	return 0;
10560b5bfa1cSLen Brown }
thermal_nocrt(const struct dmi_system_id * d)10571855256cSJeff Garzik static int thermal_nocrt(const struct dmi_system_id *d) {
1058f86b15a1SRafael J. Wysocki 	pr_notice("%s detected: disabling all critical thermal trip point actions.\n",
1059f86b15a1SRafael J. Wysocki 		  d->ident);
10605f641174SMario Limonciello 	crt = -1;
10618c99fdceSLen Brown 	return 0;
10628c99fdceSLen Brown }
thermal_tzp(const struct dmi_system_id * d)10631855256cSJeff Garzik static int thermal_tzp(const struct dmi_system_id *d) {
10640b5bfa1cSLen Brown 	if (tzp == 0) {
1065f86b15a1SRafael J. Wysocki 		pr_notice("%s detected: enabling thermal zone polling\n",
1066f86b15a1SRafael J. Wysocki 			  d->ident);
10670b5bfa1cSLen Brown 		tzp = 300;	/* 300 dS = 30 Seconds */
10680b5bfa1cSLen Brown 	}
10690b5bfa1cSLen Brown 	return 0;
10700b5bfa1cSLen Brown }
thermal_psv(const struct dmi_system_id * d)10711855256cSJeff Garzik static int thermal_psv(const struct dmi_system_id *d) {
10720b5bfa1cSLen Brown 	if (psv == 0) {
1073f86b15a1SRafael J. Wysocki 		pr_notice("%s detected: disabling all passive thermal trip points\n",
1074f86b15a1SRafael J. Wysocki 			  d->ident);
10750b5bfa1cSLen Brown 		psv = -1;
10760b5bfa1cSLen Brown 	}
10770b5bfa1cSLen Brown 	return 0;
10780b5bfa1cSLen Brown }
10790b5bfa1cSLen Brown 
10806faadbbbSChristoph Hellwig static const struct dmi_system_id thermal_dmi_table[] __initconst = {
10810b5bfa1cSLen Brown 	/*
10820b5bfa1cSLen Brown 	 * Award BIOS on this AOpen makes thermal control almost worthless.
10830b5bfa1cSLen Brown 	 * http://bugzilla.kernel.org/show_bug.cgi?id=8842
10840b5bfa1cSLen Brown 	 */
10850b5bfa1cSLen Brown 	{
10860b5bfa1cSLen Brown 	 .callback = thermal_act,
10870b5bfa1cSLen Brown 	 .ident = "AOpen i915GMm-HFS",
10880b5bfa1cSLen Brown 	 .matches = {
10890b5bfa1cSLen Brown 		DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
10900b5bfa1cSLen Brown 		DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
10910b5bfa1cSLen Brown 		},
10920b5bfa1cSLen Brown 	},
10930b5bfa1cSLen Brown 	{
10940b5bfa1cSLen Brown 	 .callback = thermal_psv,
10950b5bfa1cSLen Brown 	 .ident = "AOpen i915GMm-HFS",
10960b5bfa1cSLen Brown 	 .matches = {
10970b5bfa1cSLen Brown 		DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
10980b5bfa1cSLen Brown 		DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
10990b5bfa1cSLen Brown 		},
11000b5bfa1cSLen Brown 	},
11010b5bfa1cSLen Brown 	{
11020b5bfa1cSLen Brown 	 .callback = thermal_tzp,
11030b5bfa1cSLen Brown 	 .ident = "AOpen i915GMm-HFS",
11040b5bfa1cSLen Brown 	 .matches = {
11050b5bfa1cSLen Brown 		DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
11060b5bfa1cSLen Brown 		DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
11070b5bfa1cSLen Brown 		},
11080b5bfa1cSLen Brown 	},
11098c99fdceSLen Brown 	{
11108c99fdceSLen Brown 	 .callback = thermal_nocrt,
11118c99fdceSLen Brown 	 .ident = "Gigabyte GA-7ZX",
11128c99fdceSLen Brown 	 .matches = {
11138c99fdceSLen Brown 		DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
11148c99fdceSLen Brown 		DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
11158c99fdceSLen Brown 		},
11168c99fdceSLen Brown 	},
11170b5bfa1cSLen Brown 	{}
11180b5bfa1cSLen Brown };
11190b5bfa1cSLen Brown 
acpi_thermal_init(void)11204be44fcdSLen Brown static int __init acpi_thermal_init(void)
11211da177e4SLinus Torvalds {
112236f55404SRafael J. Wysocki 	int result;
11231da177e4SLinus Torvalds 
11240b5bfa1cSLen Brown 	dmi_check_system(thermal_dmi_table);
11250b5bfa1cSLen Brown 
112672b33ef8SLen Brown 	if (off) {
1127f86b15a1SRafael J. Wysocki 		pr_notice("thermal control disabled\n");
112872b33ef8SLen Brown 		return -ENODEV;
112972b33ef8SLen Brown 	}
113043d9f87bSZhang Rui 
1131e053dc90SBhaktipriya Shridhar 	acpi_thermal_pm_queue = alloc_workqueue("acpi_thermal_pm",
1132e053dc90SBhaktipriya Shridhar 						WQ_HIGHPRI | WQ_MEM_RECLAIM, 0);
1133a59ffb20SAaron Lu 	if (!acpi_thermal_pm_queue)
1134d550d98dSPatrick Mochel 		return -ENODEV;
11351da177e4SLinus Torvalds 
1136a59ffb20SAaron Lu 	result = acpi_bus_register_driver(&acpi_thermal_driver);
1137a59ffb20SAaron Lu 	if (result < 0) {
1138a59ffb20SAaron Lu 		destroy_workqueue(acpi_thermal_pm_queue);
1139a59ffb20SAaron Lu 		return -ENODEV;
1140a59ffb20SAaron Lu 	}
1141a59ffb20SAaron Lu 
1142d550d98dSPatrick Mochel 	return 0;
11431da177e4SLinus Torvalds }
11441da177e4SLinus Torvalds 
acpi_thermal_exit(void)11454be44fcdSLen Brown static void __exit acpi_thermal_exit(void)
11461da177e4SLinus Torvalds {
11471da177e4SLinus Torvalds 	acpi_bus_unregister_driver(&acpi_thermal_driver);
11482807bd18SAaron Lu 	destroy_workqueue(acpi_thermal_pm_queue);
11491da177e4SLinus Torvalds }
11501da177e4SLinus Torvalds 
11511da177e4SLinus Torvalds module_init(acpi_thermal_init);
11521da177e4SLinus Torvalds module_exit(acpi_thermal_exit);
1153607d265fSRafael J. Wysocki 
1154607d265fSRafael J. Wysocki MODULE_AUTHOR("Paul Diefenbaugh");
1155607d265fSRafael J. Wysocki MODULE_DESCRIPTION("ACPI Thermal Zone Driver");
1156607d265fSRafael J. Wysocki MODULE_LICENSE("GPL");
1157