xref: /OK3568_Linux_fs/kernel/drivers/acpi/thermal.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *  acpi_thermal.c - ACPI Thermal Zone Driver ($Revision: 41 $)
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
6*4882a593Smuzhiyun  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  *  This driver fully implements the ACPI thermal policy as described in the
9*4882a593Smuzhiyun  *  ACPI 2.0 Specification.
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  *  TBD: 1. Implement passive cooling hysteresis.
12*4882a593Smuzhiyun  *       2. Enhance passive cooling (CPU) states/limit interface to support
13*4882a593Smuzhiyun  *          concepts of 'multiple limiters', upper/lower limits, etc.
14*4882a593Smuzhiyun  */
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #include <linux/kernel.h>
17*4882a593Smuzhiyun #include <linux/module.h>
18*4882a593Smuzhiyun #include <linux/dmi.h>
19*4882a593Smuzhiyun #include <linux/init.h>
20*4882a593Smuzhiyun #include <linux/slab.h>
21*4882a593Smuzhiyun #include <linux/types.h>
22*4882a593Smuzhiyun #include <linux/jiffies.h>
23*4882a593Smuzhiyun #include <linux/kmod.h>
24*4882a593Smuzhiyun #include <linux/reboot.h>
25*4882a593Smuzhiyun #include <linux/device.h>
26*4882a593Smuzhiyun #include <linux/thermal.h>
27*4882a593Smuzhiyun #include <linux/acpi.h>
28*4882a593Smuzhiyun #include <linux/workqueue.h>
29*4882a593Smuzhiyun #include <linux/uaccess.h>
30*4882a593Smuzhiyun #include <linux/units.h>
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun #define PREFIX "ACPI: "
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun #define ACPI_THERMAL_CLASS		"thermal_zone"
35*4882a593Smuzhiyun #define ACPI_THERMAL_DEVICE_NAME	"Thermal Zone"
36*4882a593Smuzhiyun #define ACPI_THERMAL_NOTIFY_TEMPERATURE	0x80
37*4882a593Smuzhiyun #define ACPI_THERMAL_NOTIFY_THRESHOLDS	0x81
38*4882a593Smuzhiyun #define ACPI_THERMAL_NOTIFY_DEVICES	0x82
39*4882a593Smuzhiyun #define ACPI_THERMAL_NOTIFY_CRITICAL	0xF0
40*4882a593Smuzhiyun #define ACPI_THERMAL_NOTIFY_HOT		0xF1
41*4882a593Smuzhiyun #define ACPI_THERMAL_MODE_ACTIVE	0x00
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun #define ACPI_THERMAL_MAX_ACTIVE	10
44*4882a593Smuzhiyun #define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun #define _COMPONENT		ACPI_THERMAL_COMPONENT
47*4882a593Smuzhiyun ACPI_MODULE_NAME("thermal");
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun MODULE_AUTHOR("Paul Diefenbaugh");
50*4882a593Smuzhiyun MODULE_DESCRIPTION("ACPI Thermal Zone Driver");
51*4882a593Smuzhiyun MODULE_LICENSE("GPL");
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun static int act;
54*4882a593Smuzhiyun module_param(act, int, 0644);
55*4882a593Smuzhiyun MODULE_PARM_DESC(act, "Disable or override all lowest active trip points.");
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun static int crt;
58*4882a593Smuzhiyun module_param(crt, int, 0644);
59*4882a593Smuzhiyun MODULE_PARM_DESC(crt, "Disable or lower all critical trip points.");
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun static int tzp;
62*4882a593Smuzhiyun module_param(tzp, int, 0444);
63*4882a593Smuzhiyun MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.");
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun static int nocrt;
66*4882a593Smuzhiyun module_param(nocrt, int, 0);
67*4882a593Smuzhiyun MODULE_PARM_DESC(nocrt, "Set to take no action upon ACPI thermal zone critical trips points.");
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun static int off;
70*4882a593Smuzhiyun module_param(off, int, 0);
71*4882a593Smuzhiyun MODULE_PARM_DESC(off, "Set to disable ACPI thermal support.");
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun static int psv;
74*4882a593Smuzhiyun module_param(psv, int, 0644);
75*4882a593Smuzhiyun MODULE_PARM_DESC(psv, "Disable or override all passive trip points.");
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun static struct workqueue_struct *acpi_thermal_pm_queue;
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun static int acpi_thermal_add(struct acpi_device *device);
80*4882a593Smuzhiyun static int acpi_thermal_remove(struct acpi_device *device);
81*4882a593Smuzhiyun static void acpi_thermal_notify(struct acpi_device *device, u32 event);
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun static const struct acpi_device_id  thermal_device_ids[] = {
84*4882a593Smuzhiyun 	{ACPI_THERMAL_HID, 0},
85*4882a593Smuzhiyun 	{"", 0},
86*4882a593Smuzhiyun };
87*4882a593Smuzhiyun MODULE_DEVICE_TABLE(acpi, thermal_device_ids);
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun #ifdef CONFIG_PM_SLEEP
90*4882a593Smuzhiyun static int acpi_thermal_suspend(struct device *dev);
91*4882a593Smuzhiyun static int acpi_thermal_resume(struct device *dev);
92*4882a593Smuzhiyun #else
93*4882a593Smuzhiyun #define acpi_thermal_suspend NULL
94*4882a593Smuzhiyun #define acpi_thermal_resume NULL
95*4882a593Smuzhiyun #endif
96*4882a593Smuzhiyun static SIMPLE_DEV_PM_OPS(acpi_thermal_pm, acpi_thermal_suspend, acpi_thermal_resume);
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun static struct acpi_driver acpi_thermal_driver = {
99*4882a593Smuzhiyun 	.name = "thermal",
100*4882a593Smuzhiyun 	.class = ACPI_THERMAL_CLASS,
101*4882a593Smuzhiyun 	.ids = thermal_device_ids,
102*4882a593Smuzhiyun 	.ops = {
103*4882a593Smuzhiyun 		.add = acpi_thermal_add,
104*4882a593Smuzhiyun 		.remove = acpi_thermal_remove,
105*4882a593Smuzhiyun 		.notify = acpi_thermal_notify,
106*4882a593Smuzhiyun 		},
107*4882a593Smuzhiyun 	.drv.pm = &acpi_thermal_pm,
108*4882a593Smuzhiyun };
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun struct acpi_thermal_state {
111*4882a593Smuzhiyun 	u8 critical:1;
112*4882a593Smuzhiyun 	u8 hot:1;
113*4882a593Smuzhiyun 	u8 passive:1;
114*4882a593Smuzhiyun 	u8 active:1;
115*4882a593Smuzhiyun 	u8 reserved:4;
116*4882a593Smuzhiyun 	int active_index;
117*4882a593Smuzhiyun };
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun struct acpi_thermal_state_flags {
120*4882a593Smuzhiyun 	u8 valid:1;
121*4882a593Smuzhiyun 	u8 enabled:1;
122*4882a593Smuzhiyun 	u8 reserved:6;
123*4882a593Smuzhiyun };
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun struct acpi_thermal_critical {
126*4882a593Smuzhiyun 	struct acpi_thermal_state_flags flags;
127*4882a593Smuzhiyun 	unsigned long temperature;
128*4882a593Smuzhiyun };
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun struct acpi_thermal_hot {
131*4882a593Smuzhiyun 	struct acpi_thermal_state_flags flags;
132*4882a593Smuzhiyun 	unsigned long temperature;
133*4882a593Smuzhiyun };
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun struct acpi_thermal_passive {
136*4882a593Smuzhiyun 	struct acpi_thermal_state_flags flags;
137*4882a593Smuzhiyun 	unsigned long temperature;
138*4882a593Smuzhiyun 	unsigned long tc1;
139*4882a593Smuzhiyun 	unsigned long tc2;
140*4882a593Smuzhiyun 	unsigned long tsp;
141*4882a593Smuzhiyun 	struct acpi_handle_list devices;
142*4882a593Smuzhiyun };
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun struct acpi_thermal_active {
145*4882a593Smuzhiyun 	struct acpi_thermal_state_flags flags;
146*4882a593Smuzhiyun 	unsigned long temperature;
147*4882a593Smuzhiyun 	struct acpi_handle_list devices;
148*4882a593Smuzhiyun };
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun struct acpi_thermal_trips {
151*4882a593Smuzhiyun 	struct acpi_thermal_critical critical;
152*4882a593Smuzhiyun 	struct acpi_thermal_hot hot;
153*4882a593Smuzhiyun 	struct acpi_thermal_passive passive;
154*4882a593Smuzhiyun 	struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
155*4882a593Smuzhiyun };
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun struct acpi_thermal_flags {
158*4882a593Smuzhiyun 	u8 cooling_mode:1;	/* _SCP */
159*4882a593Smuzhiyun 	u8 devices:1;		/* _TZD */
160*4882a593Smuzhiyun 	u8 reserved:6;
161*4882a593Smuzhiyun };
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun struct acpi_thermal {
164*4882a593Smuzhiyun 	struct acpi_device * device;
165*4882a593Smuzhiyun 	acpi_bus_id name;
166*4882a593Smuzhiyun 	unsigned long temperature;
167*4882a593Smuzhiyun 	unsigned long last_temperature;
168*4882a593Smuzhiyun 	unsigned long polling_frequency;
169*4882a593Smuzhiyun 	volatile u8 zombie;
170*4882a593Smuzhiyun 	struct acpi_thermal_flags flags;
171*4882a593Smuzhiyun 	struct acpi_thermal_state state;
172*4882a593Smuzhiyun 	struct acpi_thermal_trips trips;
173*4882a593Smuzhiyun 	struct acpi_handle_list devices;
174*4882a593Smuzhiyun 	struct thermal_zone_device *thermal_zone;
175*4882a593Smuzhiyun 	int kelvin_offset;	/* in millidegrees */
176*4882a593Smuzhiyun 	struct work_struct thermal_check_work;
177*4882a593Smuzhiyun 	struct mutex thermal_check_lock;
178*4882a593Smuzhiyun 	refcount_t thermal_check_count;
179*4882a593Smuzhiyun };
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun /* --------------------------------------------------------------------------
182*4882a593Smuzhiyun                              Thermal Zone Management
183*4882a593Smuzhiyun    -------------------------------------------------------------------------- */
184*4882a593Smuzhiyun 
acpi_thermal_get_temperature(struct acpi_thermal * tz)185*4882a593Smuzhiyun static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
186*4882a593Smuzhiyun {
187*4882a593Smuzhiyun 	acpi_status status = AE_OK;
188*4882a593Smuzhiyun 	unsigned long long tmp;
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 	if (!tz)
191*4882a593Smuzhiyun 		return -EINVAL;
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	tz->last_temperature = tz->temperature;
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 	status = acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tmp);
196*4882a593Smuzhiyun 	if (ACPI_FAILURE(status))
197*4882a593Smuzhiyun 		return -ENODEV;
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 	tz->temperature = tmp;
200*4882a593Smuzhiyun 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
201*4882a593Smuzhiyun 			  tz->temperature));
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun 	return 0;
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun 
acpi_thermal_get_polling_frequency(struct acpi_thermal * tz)206*4882a593Smuzhiyun static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
207*4882a593Smuzhiyun {
208*4882a593Smuzhiyun 	acpi_status status = AE_OK;
209*4882a593Smuzhiyun 	unsigned long long tmp;
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun 	if (!tz)
212*4882a593Smuzhiyun 		return -EINVAL;
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 	status = acpi_evaluate_integer(tz->device->handle, "_TZP", NULL, &tmp);
215*4882a593Smuzhiyun 	if (ACPI_FAILURE(status))
216*4882a593Smuzhiyun 		return -ENODEV;
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	tz->polling_frequency = tmp;
219*4882a593Smuzhiyun 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
220*4882a593Smuzhiyun 			  tz->polling_frequency));
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	return 0;
223*4882a593Smuzhiyun }
224*4882a593Smuzhiyun 
acpi_thermal_set_cooling_mode(struct acpi_thermal * tz,int mode)225*4882a593Smuzhiyun static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
226*4882a593Smuzhiyun {
227*4882a593Smuzhiyun 	if (!tz)
228*4882a593Smuzhiyun 		return -EINVAL;
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 	if (ACPI_FAILURE(acpi_execute_simple_method(tz->device->handle,
231*4882a593Smuzhiyun 						    "_SCP", mode)))
232*4882a593Smuzhiyun 		return -ENODEV;
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	return 0;
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun #define ACPI_TRIPS_CRITICAL	0x01
238*4882a593Smuzhiyun #define ACPI_TRIPS_HOT		0x02
239*4882a593Smuzhiyun #define ACPI_TRIPS_PASSIVE	0x04
240*4882a593Smuzhiyun #define ACPI_TRIPS_ACTIVE	0x08
241*4882a593Smuzhiyun #define ACPI_TRIPS_DEVICES	0x10
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun #define ACPI_TRIPS_REFRESH_THRESHOLDS	(ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE)
244*4882a593Smuzhiyun #define ACPI_TRIPS_REFRESH_DEVICES	ACPI_TRIPS_DEVICES
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun #define ACPI_TRIPS_INIT      (ACPI_TRIPS_CRITICAL | ACPI_TRIPS_HOT |	\
247*4882a593Smuzhiyun 			      ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE |	\
248*4882a593Smuzhiyun 			      ACPI_TRIPS_DEVICES)
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun /*
251*4882a593Smuzhiyun  * This exception is thrown out in two cases:
252*4882a593Smuzhiyun  * 1.An invalid trip point becomes invalid or a valid trip point becomes invalid
253*4882a593Smuzhiyun  *   when re-evaluating the AML code.
254*4882a593Smuzhiyun  * 2.TODO: Devices listed in _PSL, _ALx, _TZD may change.
255*4882a593Smuzhiyun  *   We need to re-bind the cooling devices of a thermal zone when this occurs.
256*4882a593Smuzhiyun  */
257*4882a593Smuzhiyun #define ACPI_THERMAL_TRIPS_EXCEPTION(flags, str)	\
258*4882a593Smuzhiyun do {	\
259*4882a593Smuzhiyun 	if (flags != ACPI_TRIPS_INIT)	\
260*4882a593Smuzhiyun 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,	\
261*4882a593Smuzhiyun 		"ACPI thermal trip point %s changed\n"	\
262*4882a593Smuzhiyun 		"Please send acpidump to linux-acpi@vger.kernel.org", str)); \
263*4882a593Smuzhiyun } while (0)
264*4882a593Smuzhiyun 
acpi_thermal_trips_update(struct acpi_thermal * tz,int flag)265*4882a593Smuzhiyun static int acpi_thermal_trips_update(struct acpi_thermal *tz, int flag)
266*4882a593Smuzhiyun {
267*4882a593Smuzhiyun 	acpi_status status = AE_OK;
268*4882a593Smuzhiyun 	unsigned long long tmp;
269*4882a593Smuzhiyun 	struct acpi_handle_list devices;
270*4882a593Smuzhiyun 	int valid = 0;
271*4882a593Smuzhiyun 	int i;
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 	/* Critical Shutdown */
274*4882a593Smuzhiyun 	if (flag & ACPI_TRIPS_CRITICAL) {
275*4882a593Smuzhiyun 		status = acpi_evaluate_integer(tz->device->handle,
276*4882a593Smuzhiyun 				"_CRT", NULL, &tmp);
277*4882a593Smuzhiyun 		tz->trips.critical.temperature = tmp;
278*4882a593Smuzhiyun 		/*
279*4882a593Smuzhiyun 		 * Treat freezing temperatures as invalid as well; some
280*4882a593Smuzhiyun 		 * BIOSes return really low values and cause reboots at startup.
281*4882a593Smuzhiyun 		 * Below zero (Celsius) values clearly aren't right for sure..
282*4882a593Smuzhiyun 		 * ... so lets discard those as invalid.
283*4882a593Smuzhiyun 		 */
284*4882a593Smuzhiyun 		if (ACPI_FAILURE(status)) {
285*4882a593Smuzhiyun 			tz->trips.critical.flags.valid = 0;
286*4882a593Smuzhiyun 			ACPI_DEBUG_PRINT((ACPI_DB_INFO,
287*4882a593Smuzhiyun 					  "No critical threshold\n"));
288*4882a593Smuzhiyun 		} else if (tmp <= 2732) {
289*4882a593Smuzhiyun 			pr_warn(FW_BUG "Invalid critical threshold (%llu)\n",
290*4882a593Smuzhiyun 				tmp);
291*4882a593Smuzhiyun 			tz->trips.critical.flags.valid = 0;
292*4882a593Smuzhiyun 		} else {
293*4882a593Smuzhiyun 			tz->trips.critical.flags.valid = 1;
294*4882a593Smuzhiyun 			ACPI_DEBUG_PRINT((ACPI_DB_INFO,
295*4882a593Smuzhiyun 					  "Found critical threshold [%lu]\n",
296*4882a593Smuzhiyun 					  tz->trips.critical.temperature));
297*4882a593Smuzhiyun 		}
298*4882a593Smuzhiyun 		if (tz->trips.critical.flags.valid == 1) {
299*4882a593Smuzhiyun 			if (crt == -1) {
300*4882a593Smuzhiyun 				tz->trips.critical.flags.valid = 0;
301*4882a593Smuzhiyun 			} else if (crt > 0) {
302*4882a593Smuzhiyun 				unsigned long crt_k = celsius_to_deci_kelvin(crt);
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun 				/*
305*4882a593Smuzhiyun 				 * Allow override critical threshold
306*4882a593Smuzhiyun 				 */
307*4882a593Smuzhiyun 				if (crt_k > tz->trips.critical.temperature)
308*4882a593Smuzhiyun 					pr_warn(PREFIX "Critical threshold %d C\n",
309*4882a593Smuzhiyun 						crt);
310*4882a593Smuzhiyun 				tz->trips.critical.temperature = crt_k;
311*4882a593Smuzhiyun 			}
312*4882a593Smuzhiyun 		}
313*4882a593Smuzhiyun 	}
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun 	/* Critical Sleep (optional) */
316*4882a593Smuzhiyun 	if (flag & ACPI_TRIPS_HOT) {
317*4882a593Smuzhiyun 		status = acpi_evaluate_integer(tz->device->handle,
318*4882a593Smuzhiyun 				"_HOT", NULL, &tmp);
319*4882a593Smuzhiyun 		if (ACPI_FAILURE(status)) {
320*4882a593Smuzhiyun 			tz->trips.hot.flags.valid = 0;
321*4882a593Smuzhiyun 			ACPI_DEBUG_PRINT((ACPI_DB_INFO,
322*4882a593Smuzhiyun 					"No hot threshold\n"));
323*4882a593Smuzhiyun 		} else {
324*4882a593Smuzhiyun 			tz->trips.hot.temperature = tmp;
325*4882a593Smuzhiyun 			tz->trips.hot.flags.valid = 1;
326*4882a593Smuzhiyun 			ACPI_DEBUG_PRINT((ACPI_DB_INFO,
327*4882a593Smuzhiyun 					"Found hot threshold [%lu]\n",
328*4882a593Smuzhiyun 					tz->trips.hot.temperature));
329*4882a593Smuzhiyun 		}
330*4882a593Smuzhiyun 	}
331*4882a593Smuzhiyun 
332*4882a593Smuzhiyun 	/* Passive (optional) */
333*4882a593Smuzhiyun 	if (((flag & ACPI_TRIPS_PASSIVE) && tz->trips.passive.flags.valid) ||
334*4882a593Smuzhiyun 		(flag == ACPI_TRIPS_INIT)) {
335*4882a593Smuzhiyun 		valid = tz->trips.passive.flags.valid;
336*4882a593Smuzhiyun 		if (psv == -1) {
337*4882a593Smuzhiyun 			status = AE_SUPPORT;
338*4882a593Smuzhiyun 		} else if (psv > 0) {
339*4882a593Smuzhiyun 			tmp = celsius_to_deci_kelvin(psv);
340*4882a593Smuzhiyun 			status = AE_OK;
341*4882a593Smuzhiyun 		} else {
342*4882a593Smuzhiyun 			status = acpi_evaluate_integer(tz->device->handle,
343*4882a593Smuzhiyun 				"_PSV", NULL, &tmp);
344*4882a593Smuzhiyun 		}
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun 		if (ACPI_FAILURE(status))
347*4882a593Smuzhiyun 			tz->trips.passive.flags.valid = 0;
348*4882a593Smuzhiyun 		else {
349*4882a593Smuzhiyun 			tz->trips.passive.temperature = tmp;
350*4882a593Smuzhiyun 			tz->trips.passive.flags.valid = 1;
351*4882a593Smuzhiyun 			if (flag == ACPI_TRIPS_INIT) {
352*4882a593Smuzhiyun 				status = acpi_evaluate_integer(
353*4882a593Smuzhiyun 						tz->device->handle, "_TC1",
354*4882a593Smuzhiyun 						NULL, &tmp);
355*4882a593Smuzhiyun 				if (ACPI_FAILURE(status))
356*4882a593Smuzhiyun 					tz->trips.passive.flags.valid = 0;
357*4882a593Smuzhiyun 				else
358*4882a593Smuzhiyun 					tz->trips.passive.tc1 = tmp;
359*4882a593Smuzhiyun 				status = acpi_evaluate_integer(
360*4882a593Smuzhiyun 						tz->device->handle, "_TC2",
361*4882a593Smuzhiyun 						NULL, &tmp);
362*4882a593Smuzhiyun 				if (ACPI_FAILURE(status))
363*4882a593Smuzhiyun 					tz->trips.passive.flags.valid = 0;
364*4882a593Smuzhiyun 				else
365*4882a593Smuzhiyun 					tz->trips.passive.tc2 = tmp;
366*4882a593Smuzhiyun 				status = acpi_evaluate_integer(
367*4882a593Smuzhiyun 						tz->device->handle, "_TSP",
368*4882a593Smuzhiyun 						NULL, &tmp);
369*4882a593Smuzhiyun 				if (ACPI_FAILURE(status))
370*4882a593Smuzhiyun 					tz->trips.passive.flags.valid = 0;
371*4882a593Smuzhiyun 				else
372*4882a593Smuzhiyun 					tz->trips.passive.tsp = tmp;
373*4882a593Smuzhiyun 			}
374*4882a593Smuzhiyun 		}
375*4882a593Smuzhiyun 	}
376*4882a593Smuzhiyun 	if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.passive.flags.valid) {
377*4882a593Smuzhiyun 		memset(&devices, 0, sizeof(struct acpi_handle_list));
378*4882a593Smuzhiyun 		status = acpi_evaluate_reference(tz->device->handle, "_PSL",
379*4882a593Smuzhiyun 							NULL, &devices);
380*4882a593Smuzhiyun 		if (ACPI_FAILURE(status)) {
381*4882a593Smuzhiyun 			pr_warn(PREFIX "Invalid passive threshold\n");
382*4882a593Smuzhiyun 			tz->trips.passive.flags.valid = 0;
383*4882a593Smuzhiyun 		}
384*4882a593Smuzhiyun 		else
385*4882a593Smuzhiyun 			tz->trips.passive.flags.valid = 1;
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun 		if (memcmp(&tz->trips.passive.devices, &devices,
388*4882a593Smuzhiyun 				sizeof(struct acpi_handle_list))) {
389*4882a593Smuzhiyun 			memcpy(&tz->trips.passive.devices, &devices,
390*4882a593Smuzhiyun 				sizeof(struct acpi_handle_list));
391*4882a593Smuzhiyun 			ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
392*4882a593Smuzhiyun 		}
393*4882a593Smuzhiyun 	}
394*4882a593Smuzhiyun 	if ((flag & ACPI_TRIPS_PASSIVE) || (flag & ACPI_TRIPS_DEVICES)) {
395*4882a593Smuzhiyun 		if (valid != tz->trips.passive.flags.valid)
396*4882a593Smuzhiyun 				ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
397*4882a593Smuzhiyun 	}
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun 	/* Active (optional) */
400*4882a593Smuzhiyun 	for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
401*4882a593Smuzhiyun 		char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
402*4882a593Smuzhiyun 		valid = tz->trips.active[i].flags.valid;
403*4882a593Smuzhiyun 
404*4882a593Smuzhiyun 		if (act == -1)
405*4882a593Smuzhiyun 			break; /* disable all active trip points */
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun 		if ((flag == ACPI_TRIPS_INIT) || ((flag & ACPI_TRIPS_ACTIVE) &&
408*4882a593Smuzhiyun 			tz->trips.active[i].flags.valid)) {
409*4882a593Smuzhiyun 			status = acpi_evaluate_integer(tz->device->handle,
410*4882a593Smuzhiyun 							name, NULL, &tmp);
411*4882a593Smuzhiyun 			if (ACPI_FAILURE(status)) {
412*4882a593Smuzhiyun 				tz->trips.active[i].flags.valid = 0;
413*4882a593Smuzhiyun 				if (i == 0)
414*4882a593Smuzhiyun 					break;
415*4882a593Smuzhiyun 				if (act <= 0)
416*4882a593Smuzhiyun 					break;
417*4882a593Smuzhiyun 				if (i == 1)
418*4882a593Smuzhiyun 					tz->trips.active[0].temperature =
419*4882a593Smuzhiyun 						celsius_to_deci_kelvin(act);
420*4882a593Smuzhiyun 				else
421*4882a593Smuzhiyun 					/*
422*4882a593Smuzhiyun 					 * Don't allow override higher than
423*4882a593Smuzhiyun 					 * the next higher trip point
424*4882a593Smuzhiyun 					 */
425*4882a593Smuzhiyun 					tz->trips.active[i - 1].temperature =
426*4882a593Smuzhiyun 						(tz->trips.active[i - 2].temperature <
427*4882a593Smuzhiyun 						celsius_to_deci_kelvin(act) ?
428*4882a593Smuzhiyun 						tz->trips.active[i - 2].temperature :
429*4882a593Smuzhiyun 						celsius_to_deci_kelvin(act));
430*4882a593Smuzhiyun 				break;
431*4882a593Smuzhiyun 			} else {
432*4882a593Smuzhiyun 				tz->trips.active[i].temperature = tmp;
433*4882a593Smuzhiyun 				tz->trips.active[i].flags.valid = 1;
434*4882a593Smuzhiyun 			}
435*4882a593Smuzhiyun 		}
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun 		name[2] = 'L';
438*4882a593Smuzhiyun 		if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.active[i].flags.valid ) {
439*4882a593Smuzhiyun 			memset(&devices, 0, sizeof(struct acpi_handle_list));
440*4882a593Smuzhiyun 			status = acpi_evaluate_reference(tz->device->handle,
441*4882a593Smuzhiyun 						name, NULL, &devices);
442*4882a593Smuzhiyun 			if (ACPI_FAILURE(status)) {
443*4882a593Smuzhiyun 				pr_warn(PREFIX "Invalid active%d threshold\n",
444*4882a593Smuzhiyun 					i);
445*4882a593Smuzhiyun 				tz->trips.active[i].flags.valid = 0;
446*4882a593Smuzhiyun 			}
447*4882a593Smuzhiyun 			else
448*4882a593Smuzhiyun 				tz->trips.active[i].flags.valid = 1;
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun 			if (memcmp(&tz->trips.active[i].devices, &devices,
451*4882a593Smuzhiyun 					sizeof(struct acpi_handle_list))) {
452*4882a593Smuzhiyun 				memcpy(&tz->trips.active[i].devices, &devices,
453*4882a593Smuzhiyun 					sizeof(struct acpi_handle_list));
454*4882a593Smuzhiyun 				ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
455*4882a593Smuzhiyun 			}
456*4882a593Smuzhiyun 		}
457*4882a593Smuzhiyun 		if ((flag & ACPI_TRIPS_ACTIVE) || (flag & ACPI_TRIPS_DEVICES))
458*4882a593Smuzhiyun 			if (valid != tz->trips.active[i].flags.valid)
459*4882a593Smuzhiyun 				ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun 		if (!tz->trips.active[i].flags.valid)
462*4882a593Smuzhiyun 			break;
463*4882a593Smuzhiyun 	}
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun 	if (flag & ACPI_TRIPS_DEVICES) {
466*4882a593Smuzhiyun 		memset(&devices, 0, sizeof(devices));
467*4882a593Smuzhiyun 		status = acpi_evaluate_reference(tz->device->handle, "_TZD",
468*4882a593Smuzhiyun 						NULL, &devices);
469*4882a593Smuzhiyun 		if (ACPI_SUCCESS(status)
470*4882a593Smuzhiyun 		    && memcmp(&tz->devices, &devices, sizeof(devices))) {
471*4882a593Smuzhiyun 			tz->devices = devices;
472*4882a593Smuzhiyun 			ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
473*4882a593Smuzhiyun 		}
474*4882a593Smuzhiyun 	}
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun 	return 0;
477*4882a593Smuzhiyun }
478*4882a593Smuzhiyun 
acpi_thermal_get_trip_points(struct acpi_thermal * tz)479*4882a593Smuzhiyun static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
480*4882a593Smuzhiyun {
481*4882a593Smuzhiyun 	int i, valid, ret = acpi_thermal_trips_update(tz, ACPI_TRIPS_INIT);
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun 	if (ret)
484*4882a593Smuzhiyun 		return ret;
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun 	valid = tz->trips.critical.flags.valid |
487*4882a593Smuzhiyun 		tz->trips.hot.flags.valid |
488*4882a593Smuzhiyun 		tz->trips.passive.flags.valid;
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun 	for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
491*4882a593Smuzhiyun 		valid |= tz->trips.active[i].flags.valid;
492*4882a593Smuzhiyun 
493*4882a593Smuzhiyun 	if (!valid) {
494*4882a593Smuzhiyun 		pr_warn(FW_BUG "No valid trip found\n");
495*4882a593Smuzhiyun 		return -ENODEV;
496*4882a593Smuzhiyun 	}
497*4882a593Smuzhiyun 	return 0;
498*4882a593Smuzhiyun }
499*4882a593Smuzhiyun 
500*4882a593Smuzhiyun /* sys I/F for generic thermal sysfs support */
501*4882a593Smuzhiyun 
thermal_get_temp(struct thermal_zone_device * thermal,int * temp)502*4882a593Smuzhiyun static int thermal_get_temp(struct thermal_zone_device *thermal, int *temp)
503*4882a593Smuzhiyun {
504*4882a593Smuzhiyun 	struct acpi_thermal *tz = thermal->devdata;
505*4882a593Smuzhiyun 	int result;
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun 	if (!tz)
508*4882a593Smuzhiyun 		return -EINVAL;
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun 	result = acpi_thermal_get_temperature(tz);
511*4882a593Smuzhiyun 	if (result)
512*4882a593Smuzhiyun 		return result;
513*4882a593Smuzhiyun 
514*4882a593Smuzhiyun 	*temp = deci_kelvin_to_millicelsius_with_offset(tz->temperature,
515*4882a593Smuzhiyun 							tz->kelvin_offset);
516*4882a593Smuzhiyun 	return 0;
517*4882a593Smuzhiyun }
518*4882a593Smuzhiyun 
thermal_get_trip_type(struct thermal_zone_device * thermal,int trip,enum thermal_trip_type * type)519*4882a593Smuzhiyun static int thermal_get_trip_type(struct thermal_zone_device *thermal,
520*4882a593Smuzhiyun 				 int trip, enum thermal_trip_type *type)
521*4882a593Smuzhiyun {
522*4882a593Smuzhiyun 	struct acpi_thermal *tz = thermal->devdata;
523*4882a593Smuzhiyun 	int i;
524*4882a593Smuzhiyun 
525*4882a593Smuzhiyun 	if (!tz || trip < 0)
526*4882a593Smuzhiyun 		return -EINVAL;
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun 	if (tz->trips.critical.flags.valid) {
529*4882a593Smuzhiyun 		if (!trip) {
530*4882a593Smuzhiyun 			*type = THERMAL_TRIP_CRITICAL;
531*4882a593Smuzhiyun 			return 0;
532*4882a593Smuzhiyun 		}
533*4882a593Smuzhiyun 		trip--;
534*4882a593Smuzhiyun 	}
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun 	if (tz->trips.hot.flags.valid) {
537*4882a593Smuzhiyun 		if (!trip) {
538*4882a593Smuzhiyun 			*type = THERMAL_TRIP_HOT;
539*4882a593Smuzhiyun 			return 0;
540*4882a593Smuzhiyun 		}
541*4882a593Smuzhiyun 		trip--;
542*4882a593Smuzhiyun 	}
543*4882a593Smuzhiyun 
544*4882a593Smuzhiyun 	if (tz->trips.passive.flags.valid) {
545*4882a593Smuzhiyun 		if (!trip) {
546*4882a593Smuzhiyun 			*type = THERMAL_TRIP_PASSIVE;
547*4882a593Smuzhiyun 			return 0;
548*4882a593Smuzhiyun 		}
549*4882a593Smuzhiyun 		trip--;
550*4882a593Smuzhiyun 	}
551*4882a593Smuzhiyun 
552*4882a593Smuzhiyun 	for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
553*4882a593Smuzhiyun 		tz->trips.active[i].flags.valid; i++) {
554*4882a593Smuzhiyun 		if (!trip) {
555*4882a593Smuzhiyun 			*type = THERMAL_TRIP_ACTIVE;
556*4882a593Smuzhiyun 			return 0;
557*4882a593Smuzhiyun 		}
558*4882a593Smuzhiyun 		trip--;
559*4882a593Smuzhiyun 	}
560*4882a593Smuzhiyun 
561*4882a593Smuzhiyun 	return -EINVAL;
562*4882a593Smuzhiyun }
563*4882a593Smuzhiyun 
thermal_get_trip_temp(struct thermal_zone_device * thermal,int trip,int * temp)564*4882a593Smuzhiyun static int thermal_get_trip_temp(struct thermal_zone_device *thermal,
565*4882a593Smuzhiyun 				 int trip, int *temp)
566*4882a593Smuzhiyun {
567*4882a593Smuzhiyun 	struct acpi_thermal *tz = thermal->devdata;
568*4882a593Smuzhiyun 	int i;
569*4882a593Smuzhiyun 
570*4882a593Smuzhiyun 	if (!tz || trip < 0)
571*4882a593Smuzhiyun 		return -EINVAL;
572*4882a593Smuzhiyun 
573*4882a593Smuzhiyun 	if (tz->trips.critical.flags.valid) {
574*4882a593Smuzhiyun 		if (!trip) {
575*4882a593Smuzhiyun 			*temp = deci_kelvin_to_millicelsius_with_offset(
576*4882a593Smuzhiyun 				tz->trips.critical.temperature,
577*4882a593Smuzhiyun 				tz->kelvin_offset);
578*4882a593Smuzhiyun 			return 0;
579*4882a593Smuzhiyun 		}
580*4882a593Smuzhiyun 		trip--;
581*4882a593Smuzhiyun 	}
582*4882a593Smuzhiyun 
583*4882a593Smuzhiyun 	if (tz->trips.hot.flags.valid) {
584*4882a593Smuzhiyun 		if (!trip) {
585*4882a593Smuzhiyun 			*temp = deci_kelvin_to_millicelsius_with_offset(
586*4882a593Smuzhiyun 				tz->trips.hot.temperature,
587*4882a593Smuzhiyun 				tz->kelvin_offset);
588*4882a593Smuzhiyun 			return 0;
589*4882a593Smuzhiyun 		}
590*4882a593Smuzhiyun 		trip--;
591*4882a593Smuzhiyun 	}
592*4882a593Smuzhiyun 
593*4882a593Smuzhiyun 	if (tz->trips.passive.flags.valid) {
594*4882a593Smuzhiyun 		if (!trip) {
595*4882a593Smuzhiyun 			*temp = deci_kelvin_to_millicelsius_with_offset(
596*4882a593Smuzhiyun 				tz->trips.passive.temperature,
597*4882a593Smuzhiyun 				tz->kelvin_offset);
598*4882a593Smuzhiyun 			return 0;
599*4882a593Smuzhiyun 		}
600*4882a593Smuzhiyun 		trip--;
601*4882a593Smuzhiyun 	}
602*4882a593Smuzhiyun 
603*4882a593Smuzhiyun 	for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
604*4882a593Smuzhiyun 		tz->trips.active[i].flags.valid; i++) {
605*4882a593Smuzhiyun 		if (!trip) {
606*4882a593Smuzhiyun 			*temp = deci_kelvin_to_millicelsius_with_offset(
607*4882a593Smuzhiyun 				tz->trips.active[i].temperature,
608*4882a593Smuzhiyun 				tz->kelvin_offset);
609*4882a593Smuzhiyun 			return 0;
610*4882a593Smuzhiyun 		}
611*4882a593Smuzhiyun 		trip--;
612*4882a593Smuzhiyun 	}
613*4882a593Smuzhiyun 
614*4882a593Smuzhiyun 	return -EINVAL;
615*4882a593Smuzhiyun }
616*4882a593Smuzhiyun 
thermal_get_crit_temp(struct thermal_zone_device * thermal,int * temperature)617*4882a593Smuzhiyun static int thermal_get_crit_temp(struct thermal_zone_device *thermal,
618*4882a593Smuzhiyun 				int *temperature)
619*4882a593Smuzhiyun {
620*4882a593Smuzhiyun 	struct acpi_thermal *tz = thermal->devdata;
621*4882a593Smuzhiyun 
622*4882a593Smuzhiyun 	if (tz->trips.critical.flags.valid) {
623*4882a593Smuzhiyun 		*temperature = deci_kelvin_to_millicelsius_with_offset(
624*4882a593Smuzhiyun 				tz->trips.critical.temperature,
625*4882a593Smuzhiyun 				tz->kelvin_offset);
626*4882a593Smuzhiyun 		return 0;
627*4882a593Smuzhiyun 	} else
628*4882a593Smuzhiyun 		return -EINVAL;
629*4882a593Smuzhiyun }
630*4882a593Smuzhiyun 
thermal_get_trend(struct thermal_zone_device * thermal,int trip,enum thermal_trend * trend)631*4882a593Smuzhiyun static int thermal_get_trend(struct thermal_zone_device *thermal,
632*4882a593Smuzhiyun 				int trip, enum thermal_trend *trend)
633*4882a593Smuzhiyun {
634*4882a593Smuzhiyun 	struct acpi_thermal *tz = thermal->devdata;
635*4882a593Smuzhiyun 	enum thermal_trip_type type;
636*4882a593Smuzhiyun 	int i;
637*4882a593Smuzhiyun 
638*4882a593Smuzhiyun 	if (thermal_get_trip_type(thermal, trip, &type))
639*4882a593Smuzhiyun 		return -EINVAL;
640*4882a593Smuzhiyun 
641*4882a593Smuzhiyun 	if (type == THERMAL_TRIP_ACTIVE) {
642*4882a593Smuzhiyun 		int trip_temp;
643*4882a593Smuzhiyun 		int temp = deci_kelvin_to_millicelsius_with_offset(
644*4882a593Smuzhiyun 					tz->temperature, tz->kelvin_offset);
645*4882a593Smuzhiyun 		if (thermal_get_trip_temp(thermal, trip, &trip_temp))
646*4882a593Smuzhiyun 			return -EINVAL;
647*4882a593Smuzhiyun 
648*4882a593Smuzhiyun 		if (temp > trip_temp) {
649*4882a593Smuzhiyun 			*trend = THERMAL_TREND_RAISING;
650*4882a593Smuzhiyun 			return 0;
651*4882a593Smuzhiyun 		} else {
652*4882a593Smuzhiyun 			/* Fall back on default trend */
653*4882a593Smuzhiyun 			return -EINVAL;
654*4882a593Smuzhiyun 		}
655*4882a593Smuzhiyun 	}
656*4882a593Smuzhiyun 
657*4882a593Smuzhiyun 	/*
658*4882a593Smuzhiyun 	 * tz->temperature has already been updated by generic thermal layer,
659*4882a593Smuzhiyun 	 * before this callback being invoked
660*4882a593Smuzhiyun 	 */
661*4882a593Smuzhiyun 	i = (tz->trips.passive.tc1 * (tz->temperature - tz->last_temperature))
662*4882a593Smuzhiyun 		+ (tz->trips.passive.tc2
663*4882a593Smuzhiyun 		* (tz->temperature - tz->trips.passive.temperature));
664*4882a593Smuzhiyun 
665*4882a593Smuzhiyun 	if (i > 0)
666*4882a593Smuzhiyun 		*trend = THERMAL_TREND_RAISING;
667*4882a593Smuzhiyun 	else if (i < 0)
668*4882a593Smuzhiyun 		*trend = THERMAL_TREND_DROPPING;
669*4882a593Smuzhiyun 	else
670*4882a593Smuzhiyun 		*trend = THERMAL_TREND_STABLE;
671*4882a593Smuzhiyun 	return 0;
672*4882a593Smuzhiyun }
673*4882a593Smuzhiyun 
674*4882a593Smuzhiyun 
thermal_notify(struct thermal_zone_device * thermal,int trip,enum thermal_trip_type trip_type)675*4882a593Smuzhiyun static int thermal_notify(struct thermal_zone_device *thermal, int trip,
676*4882a593Smuzhiyun 			   enum thermal_trip_type trip_type)
677*4882a593Smuzhiyun {
678*4882a593Smuzhiyun 	u8 type = 0;
679*4882a593Smuzhiyun 	struct acpi_thermal *tz = thermal->devdata;
680*4882a593Smuzhiyun 
681*4882a593Smuzhiyun 	if (trip_type == THERMAL_TRIP_CRITICAL)
682*4882a593Smuzhiyun 		type = ACPI_THERMAL_NOTIFY_CRITICAL;
683*4882a593Smuzhiyun 	else if (trip_type == THERMAL_TRIP_HOT)
684*4882a593Smuzhiyun 		type = ACPI_THERMAL_NOTIFY_HOT;
685*4882a593Smuzhiyun 	else
686*4882a593Smuzhiyun 		return 0;
687*4882a593Smuzhiyun 
688*4882a593Smuzhiyun 	acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
689*4882a593Smuzhiyun 					dev_name(&tz->device->dev), type, 1);
690*4882a593Smuzhiyun 
691*4882a593Smuzhiyun 	if (trip_type == THERMAL_TRIP_CRITICAL && nocrt)
692*4882a593Smuzhiyun 		return 1;
693*4882a593Smuzhiyun 
694*4882a593Smuzhiyun 	return 0;
695*4882a593Smuzhiyun }
696*4882a593Smuzhiyun 
acpi_thermal_cooling_device_cb(struct thermal_zone_device * thermal,struct thermal_cooling_device * cdev,bool bind)697*4882a593Smuzhiyun static int acpi_thermal_cooling_device_cb(struct thermal_zone_device *thermal,
698*4882a593Smuzhiyun 					struct thermal_cooling_device *cdev,
699*4882a593Smuzhiyun 					bool bind)
700*4882a593Smuzhiyun {
701*4882a593Smuzhiyun 	struct acpi_device *device = cdev->devdata;
702*4882a593Smuzhiyun 	struct acpi_thermal *tz = thermal->devdata;
703*4882a593Smuzhiyun 	struct acpi_device *dev;
704*4882a593Smuzhiyun 	acpi_status status;
705*4882a593Smuzhiyun 	acpi_handle handle;
706*4882a593Smuzhiyun 	int i;
707*4882a593Smuzhiyun 	int j;
708*4882a593Smuzhiyun 	int trip = -1;
709*4882a593Smuzhiyun 	int result = 0;
710*4882a593Smuzhiyun 
711*4882a593Smuzhiyun 	if (tz->trips.critical.flags.valid)
712*4882a593Smuzhiyun 		trip++;
713*4882a593Smuzhiyun 
714*4882a593Smuzhiyun 	if (tz->trips.hot.flags.valid)
715*4882a593Smuzhiyun 		trip++;
716*4882a593Smuzhiyun 
717*4882a593Smuzhiyun 	if (tz->trips.passive.flags.valid) {
718*4882a593Smuzhiyun 		trip++;
719*4882a593Smuzhiyun 		for (i = 0; i < tz->trips.passive.devices.count;
720*4882a593Smuzhiyun 		    i++) {
721*4882a593Smuzhiyun 			handle = tz->trips.passive.devices.handles[i];
722*4882a593Smuzhiyun 			status = acpi_bus_get_device(handle, &dev);
723*4882a593Smuzhiyun 			if (ACPI_FAILURE(status) || dev != device)
724*4882a593Smuzhiyun 				continue;
725*4882a593Smuzhiyun 			if (bind)
726*4882a593Smuzhiyun 				result =
727*4882a593Smuzhiyun 					thermal_zone_bind_cooling_device
728*4882a593Smuzhiyun 					(thermal, trip, cdev,
729*4882a593Smuzhiyun 					 THERMAL_NO_LIMIT, THERMAL_NO_LIMIT,
730*4882a593Smuzhiyun 					 THERMAL_WEIGHT_DEFAULT);
731*4882a593Smuzhiyun 			else
732*4882a593Smuzhiyun 				result =
733*4882a593Smuzhiyun 					thermal_zone_unbind_cooling_device
734*4882a593Smuzhiyun 					(thermal, trip, cdev);
735*4882a593Smuzhiyun 			if (result)
736*4882a593Smuzhiyun 				goto failed;
737*4882a593Smuzhiyun 		}
738*4882a593Smuzhiyun 	}
739*4882a593Smuzhiyun 
740*4882a593Smuzhiyun 	for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
741*4882a593Smuzhiyun 		if (!tz->trips.active[i].flags.valid)
742*4882a593Smuzhiyun 			break;
743*4882a593Smuzhiyun 		trip++;
744*4882a593Smuzhiyun 		for (j = 0;
745*4882a593Smuzhiyun 		    j < tz->trips.active[i].devices.count;
746*4882a593Smuzhiyun 		    j++) {
747*4882a593Smuzhiyun 			handle = tz->trips.active[i].devices.handles[j];
748*4882a593Smuzhiyun 			status = acpi_bus_get_device(handle, &dev);
749*4882a593Smuzhiyun 			if (ACPI_FAILURE(status) || dev != device)
750*4882a593Smuzhiyun 				continue;
751*4882a593Smuzhiyun 			if (bind)
752*4882a593Smuzhiyun 				result = thermal_zone_bind_cooling_device
753*4882a593Smuzhiyun 					(thermal, trip, cdev,
754*4882a593Smuzhiyun 					 THERMAL_NO_LIMIT, THERMAL_NO_LIMIT,
755*4882a593Smuzhiyun 					 THERMAL_WEIGHT_DEFAULT);
756*4882a593Smuzhiyun 			else
757*4882a593Smuzhiyun 				result = thermal_zone_unbind_cooling_device
758*4882a593Smuzhiyun 					(thermal, trip, cdev);
759*4882a593Smuzhiyun 			if (result)
760*4882a593Smuzhiyun 				goto failed;
761*4882a593Smuzhiyun 		}
762*4882a593Smuzhiyun 	}
763*4882a593Smuzhiyun 
764*4882a593Smuzhiyun 	for (i = 0; i < tz->devices.count; i++) {
765*4882a593Smuzhiyun 		handle = tz->devices.handles[i];
766*4882a593Smuzhiyun 		status = acpi_bus_get_device(handle, &dev);
767*4882a593Smuzhiyun 		if (ACPI_SUCCESS(status) && (dev == device)) {
768*4882a593Smuzhiyun 			if (bind)
769*4882a593Smuzhiyun 				result = thermal_zone_bind_cooling_device
770*4882a593Smuzhiyun 						(thermal, THERMAL_TRIPS_NONE,
771*4882a593Smuzhiyun 						 cdev, THERMAL_NO_LIMIT,
772*4882a593Smuzhiyun 						 THERMAL_NO_LIMIT,
773*4882a593Smuzhiyun 						 THERMAL_WEIGHT_DEFAULT);
774*4882a593Smuzhiyun 			else
775*4882a593Smuzhiyun 				result = thermal_zone_unbind_cooling_device
776*4882a593Smuzhiyun 						(thermal, THERMAL_TRIPS_NONE,
777*4882a593Smuzhiyun 						 cdev);
778*4882a593Smuzhiyun 			if (result)
779*4882a593Smuzhiyun 				goto failed;
780*4882a593Smuzhiyun 		}
781*4882a593Smuzhiyun 	}
782*4882a593Smuzhiyun 
783*4882a593Smuzhiyun failed:
784*4882a593Smuzhiyun 	return result;
785*4882a593Smuzhiyun }
786*4882a593Smuzhiyun 
787*4882a593Smuzhiyun static int
acpi_thermal_bind_cooling_device(struct thermal_zone_device * thermal,struct thermal_cooling_device * cdev)788*4882a593Smuzhiyun acpi_thermal_bind_cooling_device(struct thermal_zone_device *thermal,
789*4882a593Smuzhiyun 					struct thermal_cooling_device *cdev)
790*4882a593Smuzhiyun {
791*4882a593Smuzhiyun 	return acpi_thermal_cooling_device_cb(thermal, cdev, true);
792*4882a593Smuzhiyun }
793*4882a593Smuzhiyun 
794*4882a593Smuzhiyun static int
acpi_thermal_unbind_cooling_device(struct thermal_zone_device * thermal,struct thermal_cooling_device * cdev)795*4882a593Smuzhiyun acpi_thermal_unbind_cooling_device(struct thermal_zone_device *thermal,
796*4882a593Smuzhiyun 					struct thermal_cooling_device *cdev)
797*4882a593Smuzhiyun {
798*4882a593Smuzhiyun 	return acpi_thermal_cooling_device_cb(thermal, cdev, false);
799*4882a593Smuzhiyun }
800*4882a593Smuzhiyun 
801*4882a593Smuzhiyun static struct thermal_zone_device_ops acpi_thermal_zone_ops = {
802*4882a593Smuzhiyun 	.bind = acpi_thermal_bind_cooling_device,
803*4882a593Smuzhiyun 	.unbind	= acpi_thermal_unbind_cooling_device,
804*4882a593Smuzhiyun 	.get_temp = thermal_get_temp,
805*4882a593Smuzhiyun 	.get_trip_type = thermal_get_trip_type,
806*4882a593Smuzhiyun 	.get_trip_temp = thermal_get_trip_temp,
807*4882a593Smuzhiyun 	.get_crit_temp = thermal_get_crit_temp,
808*4882a593Smuzhiyun 	.get_trend = thermal_get_trend,
809*4882a593Smuzhiyun 	.notify = thermal_notify,
810*4882a593Smuzhiyun };
811*4882a593Smuzhiyun 
acpi_thermal_register_thermal_zone(struct acpi_thermal * tz)812*4882a593Smuzhiyun static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz)
813*4882a593Smuzhiyun {
814*4882a593Smuzhiyun 	int trips = 0;
815*4882a593Smuzhiyun 	int result;
816*4882a593Smuzhiyun 	acpi_status status;
817*4882a593Smuzhiyun 	int i;
818*4882a593Smuzhiyun 
819*4882a593Smuzhiyun 	if (tz->trips.critical.flags.valid)
820*4882a593Smuzhiyun 		trips++;
821*4882a593Smuzhiyun 
822*4882a593Smuzhiyun 	if (tz->trips.hot.flags.valid)
823*4882a593Smuzhiyun 		trips++;
824*4882a593Smuzhiyun 
825*4882a593Smuzhiyun 	if (tz->trips.passive.flags.valid)
826*4882a593Smuzhiyun 		trips++;
827*4882a593Smuzhiyun 
828*4882a593Smuzhiyun 	for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
829*4882a593Smuzhiyun 			tz->trips.active[i].flags.valid; i++, trips++);
830*4882a593Smuzhiyun 
831*4882a593Smuzhiyun 	if (tz->trips.passive.flags.valid)
832*4882a593Smuzhiyun 		tz->thermal_zone =
833*4882a593Smuzhiyun 			thermal_zone_device_register("acpitz", trips, 0, tz,
834*4882a593Smuzhiyun 						&acpi_thermal_zone_ops, NULL,
835*4882a593Smuzhiyun 						     tz->trips.passive.tsp*100,
836*4882a593Smuzhiyun 						     tz->polling_frequency*100);
837*4882a593Smuzhiyun 	else
838*4882a593Smuzhiyun 		tz->thermal_zone =
839*4882a593Smuzhiyun 			thermal_zone_device_register("acpitz", trips, 0, tz,
840*4882a593Smuzhiyun 						&acpi_thermal_zone_ops, NULL,
841*4882a593Smuzhiyun 						0, tz->polling_frequency*100);
842*4882a593Smuzhiyun 	if (IS_ERR(tz->thermal_zone))
843*4882a593Smuzhiyun 		return -ENODEV;
844*4882a593Smuzhiyun 
845*4882a593Smuzhiyun 	result = sysfs_create_link(&tz->device->dev.kobj,
846*4882a593Smuzhiyun 				   &tz->thermal_zone->device.kobj, "thermal_zone");
847*4882a593Smuzhiyun 	if (result)
848*4882a593Smuzhiyun 		goto unregister_tzd;
849*4882a593Smuzhiyun 
850*4882a593Smuzhiyun 	result = sysfs_create_link(&tz->thermal_zone->device.kobj,
851*4882a593Smuzhiyun 				   &tz->device->dev.kobj, "device");
852*4882a593Smuzhiyun 	if (result)
853*4882a593Smuzhiyun 		goto remove_tz_link;
854*4882a593Smuzhiyun 
855*4882a593Smuzhiyun 	status =  acpi_bus_attach_private_data(tz->device->handle,
856*4882a593Smuzhiyun 					       tz->thermal_zone);
857*4882a593Smuzhiyun 	if (ACPI_FAILURE(status)) {
858*4882a593Smuzhiyun 		result = -ENODEV;
859*4882a593Smuzhiyun 		goto remove_dev_link;
860*4882a593Smuzhiyun 	}
861*4882a593Smuzhiyun 
862*4882a593Smuzhiyun 	result = thermal_zone_device_enable(tz->thermal_zone);
863*4882a593Smuzhiyun 	if (result)
864*4882a593Smuzhiyun 		goto acpi_bus_detach;
865*4882a593Smuzhiyun 
866*4882a593Smuzhiyun 	dev_info(&tz->device->dev, "registered as thermal_zone%d\n",
867*4882a593Smuzhiyun 		 tz->thermal_zone->id);
868*4882a593Smuzhiyun 
869*4882a593Smuzhiyun 	return 0;
870*4882a593Smuzhiyun 
871*4882a593Smuzhiyun acpi_bus_detach:
872*4882a593Smuzhiyun 	acpi_bus_detach_private_data(tz->device->handle);
873*4882a593Smuzhiyun remove_dev_link:
874*4882a593Smuzhiyun 	sysfs_remove_link(&tz->thermal_zone->device.kobj, "device");
875*4882a593Smuzhiyun remove_tz_link:
876*4882a593Smuzhiyun 	sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
877*4882a593Smuzhiyun unregister_tzd:
878*4882a593Smuzhiyun 	thermal_zone_device_unregister(tz->thermal_zone);
879*4882a593Smuzhiyun 
880*4882a593Smuzhiyun 	return result;
881*4882a593Smuzhiyun }
882*4882a593Smuzhiyun 
acpi_thermal_unregister_thermal_zone(struct acpi_thermal * tz)883*4882a593Smuzhiyun static void acpi_thermal_unregister_thermal_zone(struct acpi_thermal *tz)
884*4882a593Smuzhiyun {
885*4882a593Smuzhiyun 	sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
886*4882a593Smuzhiyun 	sysfs_remove_link(&tz->thermal_zone->device.kobj, "device");
887*4882a593Smuzhiyun 	thermal_zone_device_unregister(tz->thermal_zone);
888*4882a593Smuzhiyun 	tz->thermal_zone = NULL;
889*4882a593Smuzhiyun 	acpi_bus_detach_private_data(tz->device->handle);
890*4882a593Smuzhiyun }
891*4882a593Smuzhiyun 
892*4882a593Smuzhiyun 
893*4882a593Smuzhiyun /* --------------------------------------------------------------------------
894*4882a593Smuzhiyun                                  Driver Interface
895*4882a593Smuzhiyun    -------------------------------------------------------------------------- */
896*4882a593Smuzhiyun 
acpi_queue_thermal_check(struct acpi_thermal * tz)897*4882a593Smuzhiyun static void acpi_queue_thermal_check(struct acpi_thermal *tz)
898*4882a593Smuzhiyun {
899*4882a593Smuzhiyun 	if (!work_pending(&tz->thermal_check_work))
900*4882a593Smuzhiyun 		queue_work(acpi_thermal_pm_queue, &tz->thermal_check_work);
901*4882a593Smuzhiyun }
902*4882a593Smuzhiyun 
acpi_thermal_notify(struct acpi_device * device,u32 event)903*4882a593Smuzhiyun static void acpi_thermal_notify(struct acpi_device *device, u32 event)
904*4882a593Smuzhiyun {
905*4882a593Smuzhiyun 	struct acpi_thermal *tz = acpi_driver_data(device);
906*4882a593Smuzhiyun 
907*4882a593Smuzhiyun 
908*4882a593Smuzhiyun 	if (!tz)
909*4882a593Smuzhiyun 		return;
910*4882a593Smuzhiyun 
911*4882a593Smuzhiyun 	switch (event) {
912*4882a593Smuzhiyun 	case ACPI_THERMAL_NOTIFY_TEMPERATURE:
913*4882a593Smuzhiyun 		acpi_queue_thermal_check(tz);
914*4882a593Smuzhiyun 		break;
915*4882a593Smuzhiyun 	case ACPI_THERMAL_NOTIFY_THRESHOLDS:
916*4882a593Smuzhiyun 		acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_THRESHOLDS);
917*4882a593Smuzhiyun 		acpi_queue_thermal_check(tz);
918*4882a593Smuzhiyun 		acpi_bus_generate_netlink_event(device->pnp.device_class,
919*4882a593Smuzhiyun 						  dev_name(&device->dev), event, 0);
920*4882a593Smuzhiyun 		break;
921*4882a593Smuzhiyun 	case ACPI_THERMAL_NOTIFY_DEVICES:
922*4882a593Smuzhiyun 		acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_DEVICES);
923*4882a593Smuzhiyun 		acpi_queue_thermal_check(tz);
924*4882a593Smuzhiyun 		acpi_bus_generate_netlink_event(device->pnp.device_class,
925*4882a593Smuzhiyun 						  dev_name(&device->dev), event, 0);
926*4882a593Smuzhiyun 		break;
927*4882a593Smuzhiyun 	default:
928*4882a593Smuzhiyun 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
929*4882a593Smuzhiyun 				  "Unsupported event [0x%x]\n", event));
930*4882a593Smuzhiyun 		break;
931*4882a593Smuzhiyun 	}
932*4882a593Smuzhiyun }
933*4882a593Smuzhiyun 
934*4882a593Smuzhiyun /*
935*4882a593Smuzhiyun  * On some platforms, the AML code has dependency about
936*4882a593Smuzhiyun  * the evaluating order of _TMP and _CRT/_HOT/_PSV/_ACx.
937*4882a593Smuzhiyun  * 1. On HP Pavilion G4-1016tx, _TMP must be invoked after
938*4882a593Smuzhiyun  *    /_CRT/_HOT/_PSV/_ACx, or else system will be power off.
939*4882a593Smuzhiyun  * 2. On HP Compaq 6715b/6715s, the return value of _PSV is 0
940*4882a593Smuzhiyun  *    if _TMP has never been evaluated.
941*4882a593Smuzhiyun  *
942*4882a593Smuzhiyun  * As this dependency is totally transparent to OS, evaluate
943*4882a593Smuzhiyun  * all of them once, in the order of _CRT/_HOT/_PSV/_ACx,
944*4882a593Smuzhiyun  * _TMP, before they are actually used.
945*4882a593Smuzhiyun  */
acpi_thermal_aml_dependency_fix(struct acpi_thermal * tz)946*4882a593Smuzhiyun static void acpi_thermal_aml_dependency_fix(struct acpi_thermal *tz)
947*4882a593Smuzhiyun {
948*4882a593Smuzhiyun 	acpi_handle handle = tz->device->handle;
949*4882a593Smuzhiyun 	unsigned long long value;
950*4882a593Smuzhiyun 	int i;
951*4882a593Smuzhiyun 
952*4882a593Smuzhiyun 	acpi_evaluate_integer(handle, "_CRT", NULL, &value);
953*4882a593Smuzhiyun 	acpi_evaluate_integer(handle, "_HOT", NULL, &value);
954*4882a593Smuzhiyun 	acpi_evaluate_integer(handle, "_PSV", NULL, &value);
955*4882a593Smuzhiyun 	for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
956*4882a593Smuzhiyun 		char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
957*4882a593Smuzhiyun 		acpi_status status;
958*4882a593Smuzhiyun 
959*4882a593Smuzhiyun 		status = acpi_evaluate_integer(handle, name, NULL, &value);
960*4882a593Smuzhiyun 		if (status == AE_NOT_FOUND)
961*4882a593Smuzhiyun 			break;
962*4882a593Smuzhiyun 	}
963*4882a593Smuzhiyun 	acpi_evaluate_integer(handle, "_TMP", NULL, &value);
964*4882a593Smuzhiyun }
965*4882a593Smuzhiyun 
acpi_thermal_get_info(struct acpi_thermal * tz)966*4882a593Smuzhiyun static int acpi_thermal_get_info(struct acpi_thermal *tz)
967*4882a593Smuzhiyun {
968*4882a593Smuzhiyun 	int result = 0;
969*4882a593Smuzhiyun 
970*4882a593Smuzhiyun 
971*4882a593Smuzhiyun 	if (!tz)
972*4882a593Smuzhiyun 		return -EINVAL;
973*4882a593Smuzhiyun 
974*4882a593Smuzhiyun 	acpi_thermal_aml_dependency_fix(tz);
975*4882a593Smuzhiyun 
976*4882a593Smuzhiyun 	/* Get trip points [_CRT, _PSV, etc.] (required) */
977*4882a593Smuzhiyun 	result = acpi_thermal_get_trip_points(tz);
978*4882a593Smuzhiyun 	if (result)
979*4882a593Smuzhiyun 		return result;
980*4882a593Smuzhiyun 
981*4882a593Smuzhiyun 	/* Get temperature [_TMP] (required) */
982*4882a593Smuzhiyun 	result = acpi_thermal_get_temperature(tz);
983*4882a593Smuzhiyun 	if (result)
984*4882a593Smuzhiyun 		return result;
985*4882a593Smuzhiyun 
986*4882a593Smuzhiyun 	/* Set the cooling mode [_SCP] to active cooling (default) */
987*4882a593Smuzhiyun 	result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
988*4882a593Smuzhiyun 	if (!result)
989*4882a593Smuzhiyun 		tz->flags.cooling_mode = 1;
990*4882a593Smuzhiyun 
991*4882a593Smuzhiyun 	/* Get default polling frequency [_TZP] (optional) */
992*4882a593Smuzhiyun 	if (tzp)
993*4882a593Smuzhiyun 		tz->polling_frequency = tzp;
994*4882a593Smuzhiyun 	else
995*4882a593Smuzhiyun 		acpi_thermal_get_polling_frequency(tz);
996*4882a593Smuzhiyun 
997*4882a593Smuzhiyun 	return 0;
998*4882a593Smuzhiyun }
999*4882a593Smuzhiyun 
1000*4882a593Smuzhiyun /*
1001*4882a593Smuzhiyun  * The exact offset between Kelvin and degree Celsius is 273.15. However ACPI
1002*4882a593Smuzhiyun  * handles temperature values with a single decimal place. As a consequence,
1003*4882a593Smuzhiyun  * some implementations use an offset of 273.1 and others use an offset of
1004*4882a593Smuzhiyun  * 273.2. Try to find out which one is being used, to present the most
1005*4882a593Smuzhiyun  * accurate and visually appealing number.
1006*4882a593Smuzhiyun  *
1007*4882a593Smuzhiyun  * The heuristic below should work for all ACPI thermal zones which have a
1008*4882a593Smuzhiyun  * critical trip point with a value being a multiple of 0.5 degree Celsius.
1009*4882a593Smuzhiyun  */
acpi_thermal_guess_offset(struct acpi_thermal * tz)1010*4882a593Smuzhiyun static void acpi_thermal_guess_offset(struct acpi_thermal *tz)
1011*4882a593Smuzhiyun {
1012*4882a593Smuzhiyun 	if (tz->trips.critical.flags.valid &&
1013*4882a593Smuzhiyun 	    (tz->trips.critical.temperature % 5) == 1)
1014*4882a593Smuzhiyun 		tz->kelvin_offset = 273100;
1015*4882a593Smuzhiyun 	else
1016*4882a593Smuzhiyun 		tz->kelvin_offset = 273200;
1017*4882a593Smuzhiyun }
1018*4882a593Smuzhiyun 
acpi_thermal_check_fn(struct work_struct * work)1019*4882a593Smuzhiyun static void acpi_thermal_check_fn(struct work_struct *work)
1020*4882a593Smuzhiyun {
1021*4882a593Smuzhiyun 	struct acpi_thermal *tz = container_of(work, struct acpi_thermal,
1022*4882a593Smuzhiyun 					       thermal_check_work);
1023*4882a593Smuzhiyun 
1024*4882a593Smuzhiyun 	/*
1025*4882a593Smuzhiyun 	 * In general, it is not sufficient to check the pending bit, because
1026*4882a593Smuzhiyun 	 * subsequent instances of this function may be queued after one of them
1027*4882a593Smuzhiyun 	 * has started running (e.g. if _TMP sleeps).  Avoid bailing out if just
1028*4882a593Smuzhiyun 	 * one of them is running, though, because it may have done the actual
1029*4882a593Smuzhiyun 	 * check some time ago, so allow at least one of them to block on the
1030*4882a593Smuzhiyun 	 * mutex while another one is running the update.
1031*4882a593Smuzhiyun 	 */
1032*4882a593Smuzhiyun 	if (!refcount_dec_not_one(&tz->thermal_check_count))
1033*4882a593Smuzhiyun 		return;
1034*4882a593Smuzhiyun 
1035*4882a593Smuzhiyun 	mutex_lock(&tz->thermal_check_lock);
1036*4882a593Smuzhiyun 
1037*4882a593Smuzhiyun 	thermal_zone_device_update(tz->thermal_zone, THERMAL_EVENT_UNSPECIFIED);
1038*4882a593Smuzhiyun 
1039*4882a593Smuzhiyun 	refcount_inc(&tz->thermal_check_count);
1040*4882a593Smuzhiyun 
1041*4882a593Smuzhiyun 	mutex_unlock(&tz->thermal_check_lock);
1042*4882a593Smuzhiyun }
1043*4882a593Smuzhiyun 
acpi_thermal_add(struct acpi_device * device)1044*4882a593Smuzhiyun static int acpi_thermal_add(struct acpi_device *device)
1045*4882a593Smuzhiyun {
1046*4882a593Smuzhiyun 	int result = 0;
1047*4882a593Smuzhiyun 	struct acpi_thermal *tz = NULL;
1048*4882a593Smuzhiyun 
1049*4882a593Smuzhiyun 
1050*4882a593Smuzhiyun 	if (!device)
1051*4882a593Smuzhiyun 		return -EINVAL;
1052*4882a593Smuzhiyun 
1053*4882a593Smuzhiyun 	tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
1054*4882a593Smuzhiyun 	if (!tz)
1055*4882a593Smuzhiyun 		return -ENOMEM;
1056*4882a593Smuzhiyun 
1057*4882a593Smuzhiyun 	tz->device = device;
1058*4882a593Smuzhiyun 	strcpy(tz->name, device->pnp.bus_id);
1059*4882a593Smuzhiyun 	strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1060*4882a593Smuzhiyun 	strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
1061*4882a593Smuzhiyun 	device->driver_data = tz;
1062*4882a593Smuzhiyun 
1063*4882a593Smuzhiyun 	result = acpi_thermal_get_info(tz);
1064*4882a593Smuzhiyun 	if (result)
1065*4882a593Smuzhiyun 		goto free_memory;
1066*4882a593Smuzhiyun 
1067*4882a593Smuzhiyun 	acpi_thermal_guess_offset(tz);
1068*4882a593Smuzhiyun 
1069*4882a593Smuzhiyun 	result = acpi_thermal_register_thermal_zone(tz);
1070*4882a593Smuzhiyun 	if (result)
1071*4882a593Smuzhiyun 		goto free_memory;
1072*4882a593Smuzhiyun 
1073*4882a593Smuzhiyun 	refcount_set(&tz->thermal_check_count, 3);
1074*4882a593Smuzhiyun 	mutex_init(&tz->thermal_check_lock);
1075*4882a593Smuzhiyun 	INIT_WORK(&tz->thermal_check_work, acpi_thermal_check_fn);
1076*4882a593Smuzhiyun 
1077*4882a593Smuzhiyun 	pr_info(PREFIX "%s [%s] (%ld C)\n", acpi_device_name(device),
1078*4882a593Smuzhiyun 		acpi_device_bid(device), deci_kelvin_to_celsius(tz->temperature));
1079*4882a593Smuzhiyun 	goto end;
1080*4882a593Smuzhiyun 
1081*4882a593Smuzhiyun free_memory:
1082*4882a593Smuzhiyun 	kfree(tz);
1083*4882a593Smuzhiyun end:
1084*4882a593Smuzhiyun 	return result;
1085*4882a593Smuzhiyun }
1086*4882a593Smuzhiyun 
acpi_thermal_remove(struct acpi_device * device)1087*4882a593Smuzhiyun static int acpi_thermal_remove(struct acpi_device *device)
1088*4882a593Smuzhiyun {
1089*4882a593Smuzhiyun 	struct acpi_thermal *tz = NULL;
1090*4882a593Smuzhiyun 
1091*4882a593Smuzhiyun 	if (!device || !acpi_driver_data(device))
1092*4882a593Smuzhiyun 		return -EINVAL;
1093*4882a593Smuzhiyun 
1094*4882a593Smuzhiyun 	flush_workqueue(acpi_thermal_pm_queue);
1095*4882a593Smuzhiyun 	tz = acpi_driver_data(device);
1096*4882a593Smuzhiyun 
1097*4882a593Smuzhiyun 	acpi_thermal_unregister_thermal_zone(tz);
1098*4882a593Smuzhiyun 	kfree(tz);
1099*4882a593Smuzhiyun 	return 0;
1100*4882a593Smuzhiyun }
1101*4882a593Smuzhiyun 
1102*4882a593Smuzhiyun #ifdef CONFIG_PM_SLEEP
acpi_thermal_suspend(struct device * dev)1103*4882a593Smuzhiyun static int acpi_thermal_suspend(struct device *dev)
1104*4882a593Smuzhiyun {
1105*4882a593Smuzhiyun 	/* Make sure the previously queued thermal check work has been done */
1106*4882a593Smuzhiyun 	flush_workqueue(acpi_thermal_pm_queue);
1107*4882a593Smuzhiyun 	return 0;
1108*4882a593Smuzhiyun }
1109*4882a593Smuzhiyun 
acpi_thermal_resume(struct device * dev)1110*4882a593Smuzhiyun static int acpi_thermal_resume(struct device *dev)
1111*4882a593Smuzhiyun {
1112*4882a593Smuzhiyun 	struct acpi_thermal *tz;
1113*4882a593Smuzhiyun 	int i, j, power_state, result;
1114*4882a593Smuzhiyun 
1115*4882a593Smuzhiyun 	if (!dev)
1116*4882a593Smuzhiyun 		return -EINVAL;
1117*4882a593Smuzhiyun 
1118*4882a593Smuzhiyun 	tz = acpi_driver_data(to_acpi_device(dev));
1119*4882a593Smuzhiyun 	if (!tz)
1120*4882a593Smuzhiyun 		return -EINVAL;
1121*4882a593Smuzhiyun 
1122*4882a593Smuzhiyun 	for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1123*4882a593Smuzhiyun 		if (!(&tz->trips.active[i]))
1124*4882a593Smuzhiyun 			break;
1125*4882a593Smuzhiyun 		if (!tz->trips.active[i].flags.valid)
1126*4882a593Smuzhiyun 			break;
1127*4882a593Smuzhiyun 		tz->trips.active[i].flags.enabled = 1;
1128*4882a593Smuzhiyun 		for (j = 0; j < tz->trips.active[i].devices.count; j++) {
1129*4882a593Smuzhiyun 			result = acpi_bus_update_power(
1130*4882a593Smuzhiyun 					tz->trips.active[i].devices.handles[j],
1131*4882a593Smuzhiyun 					&power_state);
1132*4882a593Smuzhiyun 			if (result || (power_state != ACPI_STATE_D0)) {
1133*4882a593Smuzhiyun 				tz->trips.active[i].flags.enabled = 0;
1134*4882a593Smuzhiyun 				break;
1135*4882a593Smuzhiyun 			}
1136*4882a593Smuzhiyun 		}
1137*4882a593Smuzhiyun 		tz->state.active |= tz->trips.active[i].flags.enabled;
1138*4882a593Smuzhiyun 	}
1139*4882a593Smuzhiyun 
1140*4882a593Smuzhiyun 	acpi_queue_thermal_check(tz);
1141*4882a593Smuzhiyun 
1142*4882a593Smuzhiyun 	return AE_OK;
1143*4882a593Smuzhiyun }
1144*4882a593Smuzhiyun #endif
1145*4882a593Smuzhiyun 
thermal_act(const struct dmi_system_id * d)1146*4882a593Smuzhiyun static int thermal_act(const struct dmi_system_id *d) {
1147*4882a593Smuzhiyun 
1148*4882a593Smuzhiyun 	if (act == 0) {
1149*4882a593Smuzhiyun 		pr_notice(PREFIX "%s detected: "
1150*4882a593Smuzhiyun 			  "disabling all active thermal trip points\n", d->ident);
1151*4882a593Smuzhiyun 		act = -1;
1152*4882a593Smuzhiyun 	}
1153*4882a593Smuzhiyun 	return 0;
1154*4882a593Smuzhiyun }
thermal_nocrt(const struct dmi_system_id * d)1155*4882a593Smuzhiyun static int thermal_nocrt(const struct dmi_system_id *d) {
1156*4882a593Smuzhiyun 
1157*4882a593Smuzhiyun 	pr_notice(PREFIX "%s detected: "
1158*4882a593Smuzhiyun 		  "disabling all critical thermal trip point actions.\n", d->ident);
1159*4882a593Smuzhiyun 	nocrt = 1;
1160*4882a593Smuzhiyun 	return 0;
1161*4882a593Smuzhiyun }
thermal_tzp(const struct dmi_system_id * d)1162*4882a593Smuzhiyun static int thermal_tzp(const struct dmi_system_id *d) {
1163*4882a593Smuzhiyun 
1164*4882a593Smuzhiyun 	if (tzp == 0) {
1165*4882a593Smuzhiyun 		pr_notice(PREFIX "%s detected: "
1166*4882a593Smuzhiyun 			  "enabling thermal zone polling\n", d->ident);
1167*4882a593Smuzhiyun 		tzp = 300;	/* 300 dS = 30 Seconds */
1168*4882a593Smuzhiyun 	}
1169*4882a593Smuzhiyun 	return 0;
1170*4882a593Smuzhiyun }
thermal_psv(const struct dmi_system_id * d)1171*4882a593Smuzhiyun static int thermal_psv(const struct dmi_system_id *d) {
1172*4882a593Smuzhiyun 
1173*4882a593Smuzhiyun 	if (psv == 0) {
1174*4882a593Smuzhiyun 		pr_notice(PREFIX "%s detected: "
1175*4882a593Smuzhiyun 			  "disabling all passive thermal trip points\n", d->ident);
1176*4882a593Smuzhiyun 		psv = -1;
1177*4882a593Smuzhiyun 	}
1178*4882a593Smuzhiyun 	return 0;
1179*4882a593Smuzhiyun }
1180*4882a593Smuzhiyun 
1181*4882a593Smuzhiyun static const struct dmi_system_id thermal_dmi_table[] __initconst = {
1182*4882a593Smuzhiyun 	/*
1183*4882a593Smuzhiyun 	 * Award BIOS on this AOpen makes thermal control almost worthless.
1184*4882a593Smuzhiyun 	 * http://bugzilla.kernel.org/show_bug.cgi?id=8842
1185*4882a593Smuzhiyun 	 */
1186*4882a593Smuzhiyun 	{
1187*4882a593Smuzhiyun 	 .callback = thermal_act,
1188*4882a593Smuzhiyun 	 .ident = "AOpen i915GMm-HFS",
1189*4882a593Smuzhiyun 	 .matches = {
1190*4882a593Smuzhiyun 		DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1191*4882a593Smuzhiyun 		DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1192*4882a593Smuzhiyun 		},
1193*4882a593Smuzhiyun 	},
1194*4882a593Smuzhiyun 	{
1195*4882a593Smuzhiyun 	 .callback = thermal_psv,
1196*4882a593Smuzhiyun 	 .ident = "AOpen i915GMm-HFS",
1197*4882a593Smuzhiyun 	 .matches = {
1198*4882a593Smuzhiyun 		DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1199*4882a593Smuzhiyun 		DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1200*4882a593Smuzhiyun 		},
1201*4882a593Smuzhiyun 	},
1202*4882a593Smuzhiyun 	{
1203*4882a593Smuzhiyun 	 .callback = thermal_tzp,
1204*4882a593Smuzhiyun 	 .ident = "AOpen i915GMm-HFS",
1205*4882a593Smuzhiyun 	 .matches = {
1206*4882a593Smuzhiyun 		DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1207*4882a593Smuzhiyun 		DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1208*4882a593Smuzhiyun 		},
1209*4882a593Smuzhiyun 	},
1210*4882a593Smuzhiyun 	{
1211*4882a593Smuzhiyun 	 .callback = thermal_nocrt,
1212*4882a593Smuzhiyun 	 .ident = "Gigabyte GA-7ZX",
1213*4882a593Smuzhiyun 	 .matches = {
1214*4882a593Smuzhiyun 		DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
1215*4882a593Smuzhiyun 		DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
1216*4882a593Smuzhiyun 		},
1217*4882a593Smuzhiyun 	},
1218*4882a593Smuzhiyun 	{}
1219*4882a593Smuzhiyun };
1220*4882a593Smuzhiyun 
acpi_thermal_init(void)1221*4882a593Smuzhiyun static int __init acpi_thermal_init(void)
1222*4882a593Smuzhiyun {
1223*4882a593Smuzhiyun 	int result = 0;
1224*4882a593Smuzhiyun 
1225*4882a593Smuzhiyun 	dmi_check_system(thermal_dmi_table);
1226*4882a593Smuzhiyun 
1227*4882a593Smuzhiyun 	if (off) {
1228*4882a593Smuzhiyun 		pr_notice(PREFIX "thermal control disabled\n");
1229*4882a593Smuzhiyun 		return -ENODEV;
1230*4882a593Smuzhiyun 	}
1231*4882a593Smuzhiyun 
1232*4882a593Smuzhiyun 	acpi_thermal_pm_queue = alloc_workqueue("acpi_thermal_pm",
1233*4882a593Smuzhiyun 						WQ_HIGHPRI | WQ_MEM_RECLAIM, 0);
1234*4882a593Smuzhiyun 	if (!acpi_thermal_pm_queue)
1235*4882a593Smuzhiyun 		return -ENODEV;
1236*4882a593Smuzhiyun 
1237*4882a593Smuzhiyun 	result = acpi_bus_register_driver(&acpi_thermal_driver);
1238*4882a593Smuzhiyun 	if (result < 0) {
1239*4882a593Smuzhiyun 		destroy_workqueue(acpi_thermal_pm_queue);
1240*4882a593Smuzhiyun 		return -ENODEV;
1241*4882a593Smuzhiyun 	}
1242*4882a593Smuzhiyun 
1243*4882a593Smuzhiyun 	return 0;
1244*4882a593Smuzhiyun }
1245*4882a593Smuzhiyun 
acpi_thermal_exit(void)1246*4882a593Smuzhiyun static void __exit acpi_thermal_exit(void)
1247*4882a593Smuzhiyun {
1248*4882a593Smuzhiyun 	acpi_bus_unregister_driver(&acpi_thermal_driver);
1249*4882a593Smuzhiyun 	destroy_workqueue(acpi_thermal_pm_queue);
1250*4882a593Smuzhiyun 
1251*4882a593Smuzhiyun 	return;
1252*4882a593Smuzhiyun }
1253*4882a593Smuzhiyun 
1254*4882a593Smuzhiyun module_init(acpi_thermal_init);
1255*4882a593Smuzhiyun module_exit(acpi_thermal_exit);
1256