1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * acpi_thermal.c - ACPI Thermal Zone Driver ($Revision: 41 $)
5 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
6 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
8 * This driver fully implements the ACPI thermal policy as described in the
9 * ACPI 2.0 Specification.
11 * TBD: 1. Implement passive cooling hysteresis.
12 * 2. Enhance passive cooling (CPU) states/limit interface to support
13 * concepts of 'multiple limiters', upper/lower limits, etc.
16 #define pr_fmt(fmt) "ACPI: thermal: " fmt
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/dmi.h>
21 #include <linux/init.h>
22 #include <linux/slab.h>
23 #include <linux/types.h>
24 #include <linux/jiffies.h>
25 #include <linux/kmod.h>
26 #include <linux/reboot.h>
27 #include <linux/device.h>
28 #include <linux/thermal.h>
29 #include <linux/acpi.h>
30 #include <linux/workqueue.h>
31 #include <linux/uaccess.h>
32 #include <linux/units.h>
34 #define ACPI_THERMAL_CLASS "thermal_zone"
35 #define ACPI_THERMAL_DEVICE_NAME "Thermal Zone"
36 #define ACPI_THERMAL_NOTIFY_TEMPERATURE 0x80
37 #define ACPI_THERMAL_NOTIFY_THRESHOLDS 0x81
38 #define ACPI_THERMAL_NOTIFY_DEVICES 0x82
39 #define ACPI_THERMAL_NOTIFY_CRITICAL 0xF0
40 #define ACPI_THERMAL_NOTIFY_HOT 0xF1
41 #define ACPI_THERMAL_MODE_ACTIVE 0x00
43 #define ACPI_THERMAL_MAX_ACTIVE 10
44 #define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65
46 #define ACPI_TRIPS_CRITICAL BIT(0)
47 #define ACPI_TRIPS_HOT BIT(1)
48 #define ACPI_TRIPS_PASSIVE BIT(2)
49 #define ACPI_TRIPS_ACTIVE BIT(3)
50 #define ACPI_TRIPS_DEVICES BIT(4)
52 #define ACPI_TRIPS_THRESHOLDS (ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE)
54 #define ACPI_TRIPS_INIT (ACPI_TRIPS_CRITICAL | ACPI_TRIPS_HOT | \
55 ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE | \
59 * This exception is thrown out in two cases:
60 * 1.An invalid trip point becomes invalid or a valid trip point becomes invalid
61 * when re-evaluating the AML code.
62 * 2.TODO: Devices listed in _PSL, _ALx, _TZD may change.
63 * We need to re-bind the cooling devices of a thermal zone when this occurs.
65 #define ACPI_THERMAL_TRIPS_EXCEPTION(flags, tz, str) \
67 if (flags != ACPI_TRIPS_INIT) \
68 acpi_handle_info(tz->device->handle, \
69 "ACPI thermal trip point %s changed\n" \
70 "Please report to linux-acpi@vger.kernel.org\n", str); \
74 module_param(act, int, 0644);
75 MODULE_PARM_DESC(act, "Disable or override all lowest active trip points.");
78 module_param(crt, int, 0644);
79 MODULE_PARM_DESC(crt, "Disable or lower all critical trip points.");
82 module_param(tzp, int, 0444);
83 MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.");
86 module_param(nocrt, int, 0);
87 MODULE_PARM_DESC(nocrt, "Set to take no action upon ACPI thermal zone critical trips points.");
90 module_param(off, int, 0);
91 MODULE_PARM_DESC(off, "Set to disable ACPI thermal support.");
94 module_param(psv, int, 0644);
95 MODULE_PARM_DESC(psv, "Disable or override all passive trip points.");
97 static struct workqueue_struct *acpi_thermal_pm_queue;
99 struct acpi_thermal_critical {
100 unsigned long temperature;
104 struct acpi_thermal_hot {
105 unsigned long temperature;
109 struct acpi_thermal_passive {
110 struct acpi_handle_list devices;
111 unsigned long temperature;
118 struct acpi_thermal_active {
119 struct acpi_handle_list devices;
120 unsigned long temperature;
125 struct acpi_thermal_trips {
126 struct acpi_thermal_critical critical;
127 struct acpi_thermal_hot hot;
128 struct acpi_thermal_passive passive;
129 struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
132 struct acpi_thermal {
133 struct acpi_device *device;
135 unsigned long temperature;
136 unsigned long last_temperature;
137 unsigned long polling_frequency;
139 struct acpi_thermal_trips trips;
140 struct acpi_handle_list devices;
141 struct thermal_zone_device *thermal_zone;
142 int kelvin_offset; /* in millidegrees */
143 struct work_struct thermal_check_work;
144 struct mutex thermal_check_lock;
145 refcount_t thermal_check_count;
148 /* --------------------------------------------------------------------------
149 Thermal Zone Management
150 -------------------------------------------------------------------------- */
152 static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
154 acpi_status status = AE_OK;
155 unsigned long long tmp;
160 tz->last_temperature = tz->temperature;
162 status = acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tmp);
163 if (ACPI_FAILURE(status))
166 tz->temperature = tmp;
168 acpi_handle_debug(tz->device->handle, "Temperature is %lu dK\n",
174 static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
176 acpi_status status = AE_OK;
177 unsigned long long tmp;
182 status = acpi_evaluate_integer(tz->device->handle, "_TZP", NULL, &tmp);
183 if (ACPI_FAILURE(status))
186 tz->polling_frequency = tmp;
187 acpi_handle_debug(tz->device->handle, "Polling frequency is %lu dS\n",
188 tz->polling_frequency);
193 static int acpi_thermal_trips_update(struct acpi_thermal *tz, int flag)
196 unsigned long long tmp;
197 struct acpi_handle_list devices;
201 /* Critical Shutdown */
202 if (flag & ACPI_TRIPS_CRITICAL) {
203 status = acpi_evaluate_integer(tz->device->handle, "_CRT", NULL, &tmp);
204 tz->trips.critical.temperature = tmp;
206 * Treat freezing temperatures as invalid as well; some
207 * BIOSes return really low values and cause reboots at startup.
208 * Below zero (Celsius) values clearly aren't right for sure..
209 * ... so lets discard those as invalid.
211 if (ACPI_FAILURE(status)) {
212 tz->trips.critical.valid = false;
213 acpi_handle_debug(tz->device->handle,
214 "No critical threshold\n");
215 } else if (tmp <= 2732) {
216 pr_info(FW_BUG "Invalid critical threshold (%llu)\n", tmp);
217 tz->trips.critical.valid = false;
219 tz->trips.critical.valid = true;
220 acpi_handle_debug(tz->device->handle,
221 "Found critical threshold [%lu]\n",
222 tz->trips.critical.temperature);
224 if (tz->trips.critical.valid) {
226 tz->trips.critical.valid = false;
227 } else if (crt > 0) {
228 unsigned long crt_k = celsius_to_deci_kelvin(crt);
231 * Allow override critical threshold
233 if (crt_k > tz->trips.critical.temperature)
234 pr_info("Critical threshold %d C\n", crt);
236 tz->trips.critical.temperature = crt_k;
241 /* Critical Sleep (optional) */
242 if (flag & ACPI_TRIPS_HOT) {
243 status = acpi_evaluate_integer(tz->device->handle, "_HOT", NULL, &tmp);
244 if (ACPI_FAILURE(status)) {
245 tz->trips.hot.valid = false;
246 acpi_handle_debug(tz->device->handle,
247 "No hot threshold\n");
249 tz->trips.hot.temperature = tmp;
250 tz->trips.hot.valid = true;
251 acpi_handle_debug(tz->device->handle,
252 "Found hot threshold [%lu]\n",
253 tz->trips.hot.temperature);
257 /* Passive (optional) */
258 if (((flag & ACPI_TRIPS_PASSIVE) && tz->trips.passive.valid) ||
259 flag == ACPI_TRIPS_INIT) {
260 valid = tz->trips.passive.valid;
263 } else if (psv > 0) {
264 tmp = celsius_to_deci_kelvin(psv);
267 status = acpi_evaluate_integer(tz->device->handle,
271 if (ACPI_FAILURE(status)) {
272 tz->trips.passive.valid = false;
274 tz->trips.passive.temperature = tmp;
275 tz->trips.passive.valid = true;
276 if (flag == ACPI_TRIPS_INIT) {
277 status = acpi_evaluate_integer(tz->device->handle,
279 if (ACPI_FAILURE(status))
280 tz->trips.passive.valid = false;
282 tz->trips.passive.tc1 = tmp;
284 status = acpi_evaluate_integer(tz->device->handle,
286 if (ACPI_FAILURE(status))
287 tz->trips.passive.valid = false;
289 tz->trips.passive.tc2 = tmp;
291 status = acpi_evaluate_integer(tz->device->handle,
293 if (ACPI_FAILURE(status))
294 tz->trips.passive.valid = false;
296 tz->trips.passive.tsp = tmp;
300 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.passive.valid) {
301 memset(&devices, 0, sizeof(struct acpi_handle_list));
302 status = acpi_evaluate_reference(tz->device->handle, "_PSL",
304 if (ACPI_FAILURE(status)) {
305 acpi_handle_info(tz->device->handle,
306 "Invalid passive threshold\n");
307 tz->trips.passive.valid = false;
309 tz->trips.passive.valid = true;
312 if (memcmp(&tz->trips.passive.devices, &devices,
313 sizeof(struct acpi_handle_list))) {
314 memcpy(&tz->trips.passive.devices, &devices,
315 sizeof(struct acpi_handle_list));
316 ACPI_THERMAL_TRIPS_EXCEPTION(flag, tz, "device");
319 if ((flag & ACPI_TRIPS_PASSIVE) || (flag & ACPI_TRIPS_DEVICES)) {
320 if (valid != tz->trips.passive.valid)
321 ACPI_THERMAL_TRIPS_EXCEPTION(flag, tz, "state");
324 /* Active (optional) */
325 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
326 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
327 valid = tz->trips.active[i].valid;
330 break; /* disable all active trip points */
332 if (flag == ACPI_TRIPS_INIT || ((flag & ACPI_TRIPS_ACTIVE) &&
333 tz->trips.active[i].valid)) {
334 status = acpi_evaluate_integer(tz->device->handle,
336 if (ACPI_FAILURE(status)) {
337 tz->trips.active[i].valid = false;
345 tz->trips.active[0].temperature = celsius_to_deci_kelvin(act);
348 * Don't allow override higher than
349 * the next higher trip point
351 tz->trips.active[i-1].temperature =
353 tz->trips.active[i-2].temperature,
354 celsius_to_deci_kelvin(act));
358 tz->trips.active[i].temperature = tmp;
359 tz->trips.active[i].valid = true;
364 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.active[i].valid) {
365 memset(&devices, 0, sizeof(struct acpi_handle_list));
366 status = acpi_evaluate_reference(tz->device->handle,
367 name, NULL, &devices);
368 if (ACPI_FAILURE(status)) {
369 acpi_handle_info(tz->device->handle,
370 "Invalid active%d threshold\n", i);
371 tz->trips.active[i].valid = false;
373 tz->trips.active[i].valid = true;
376 if (memcmp(&tz->trips.active[i].devices, &devices,
377 sizeof(struct acpi_handle_list))) {
378 memcpy(&tz->trips.active[i].devices, &devices,
379 sizeof(struct acpi_handle_list));
380 ACPI_THERMAL_TRIPS_EXCEPTION(flag, tz, "device");
383 if ((flag & ACPI_TRIPS_ACTIVE) || (flag & ACPI_TRIPS_DEVICES))
384 if (valid != tz->trips.active[i].valid)
385 ACPI_THERMAL_TRIPS_EXCEPTION(flag, tz, "state");
387 if (!tz->trips.active[i].valid)
391 if (flag & ACPI_TRIPS_DEVICES) {
392 memset(&devices, 0, sizeof(devices));
393 status = acpi_evaluate_reference(tz->device->handle, "_TZD",
395 if (ACPI_SUCCESS(status) &&
396 memcmp(&tz->devices, &devices, sizeof(devices))) {
397 tz->devices = devices;
398 ACPI_THERMAL_TRIPS_EXCEPTION(flag, tz, "device");
405 static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
407 int i, ret = acpi_thermal_trips_update(tz, ACPI_TRIPS_INIT);
413 valid = tz->trips.critical.valid |
414 tz->trips.hot.valid |
415 tz->trips.passive.valid;
417 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
418 valid = valid || tz->trips.active[i].valid;
421 pr_warn(FW_BUG "No valid trip found\n");
427 /* sys I/F for generic thermal sysfs support */
429 static int thermal_get_temp(struct thermal_zone_device *thermal, int *temp)
431 struct acpi_thermal *tz = thermal_zone_device_priv(thermal);
437 result = acpi_thermal_get_temperature(tz);
441 *temp = deci_kelvin_to_millicelsius_with_offset(tz->temperature,
446 static int thermal_get_trip_type(struct thermal_zone_device *thermal,
447 int trip, enum thermal_trip_type *type)
449 struct acpi_thermal *tz = thermal_zone_device_priv(thermal);
455 if (tz->trips.critical.valid) {
457 *type = THERMAL_TRIP_CRITICAL;
463 if (tz->trips.hot.valid) {
465 *type = THERMAL_TRIP_HOT;
471 if (tz->trips.passive.valid) {
473 *type = THERMAL_TRIP_PASSIVE;
479 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE && tz->trips.active[i].valid; i++) {
481 *type = THERMAL_TRIP_ACTIVE;
490 static int thermal_get_trip_temp(struct thermal_zone_device *thermal,
493 struct acpi_thermal *tz = thermal_zone_device_priv(thermal);
499 if (tz->trips.critical.valid) {
501 *temp = deci_kelvin_to_millicelsius_with_offset(
502 tz->trips.critical.temperature,
509 if (tz->trips.hot.valid) {
511 *temp = deci_kelvin_to_millicelsius_with_offset(
512 tz->trips.hot.temperature,
519 if (tz->trips.passive.valid) {
521 *temp = deci_kelvin_to_millicelsius_with_offset(
522 tz->trips.passive.temperature,
529 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
530 tz->trips.active[i].valid; i++) {
532 *temp = deci_kelvin_to_millicelsius_with_offset(
533 tz->trips.active[i].temperature,
543 static int thermal_get_crit_temp(struct thermal_zone_device *thermal,
546 struct acpi_thermal *tz = thermal_zone_device_priv(thermal);
548 if (tz->trips.critical.valid) {
549 *temperature = deci_kelvin_to_millicelsius_with_offset(
550 tz->trips.critical.temperature,
558 static int thermal_get_trend(struct thermal_zone_device *thermal,
559 int trip, enum thermal_trend *trend)
561 struct acpi_thermal *tz = thermal_zone_device_priv(thermal);
562 enum thermal_trip_type type;
565 if (thermal_get_trip_type(thermal, trip, &type))
568 if (type == THERMAL_TRIP_ACTIVE) {
570 int temp = deci_kelvin_to_millicelsius_with_offset(
571 tz->temperature, tz->kelvin_offset);
572 if (thermal_get_trip_temp(thermal, trip, &trip_temp))
575 if (temp > trip_temp) {
576 *trend = THERMAL_TREND_RAISING;
579 /* Fall back on default trend */
585 * tz->temperature has already been updated by generic thermal layer,
586 * before this callback being invoked
588 i = tz->trips.passive.tc1 * (tz->temperature - tz->last_temperature) +
589 tz->trips.passive.tc2 * (tz->temperature - tz->trips.passive.temperature);
592 *trend = THERMAL_TREND_RAISING;
594 *trend = THERMAL_TREND_DROPPING;
596 *trend = THERMAL_TREND_STABLE;
601 static void acpi_thermal_zone_device_hot(struct thermal_zone_device *thermal)
603 struct acpi_thermal *tz = thermal_zone_device_priv(thermal);
605 acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
606 dev_name(&tz->device->dev),
607 ACPI_THERMAL_NOTIFY_HOT, 1);
610 static void acpi_thermal_zone_device_critical(struct thermal_zone_device *thermal)
612 struct acpi_thermal *tz = thermal_zone_device_priv(thermal);
614 acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
615 dev_name(&tz->device->dev),
616 ACPI_THERMAL_NOTIFY_CRITICAL, 1);
618 thermal_zone_device_critical(thermal);
621 static int acpi_thermal_cooling_device_cb(struct thermal_zone_device *thermal,
622 struct thermal_cooling_device *cdev,
625 struct acpi_device *device = cdev->devdata;
626 struct acpi_thermal *tz = thermal_zone_device_priv(thermal);
627 struct acpi_device *dev;
634 if (tz->trips.critical.valid)
637 if (tz->trips.hot.valid)
640 if (tz->trips.passive.valid) {
642 for (i = 0; i < tz->trips.passive.devices.count; i++) {
643 handle = tz->trips.passive.devices.handles[i];
644 dev = acpi_fetch_acpi_dev(handle);
649 result = thermal_zone_bind_cooling_device(
653 THERMAL_WEIGHT_DEFAULT);
656 thermal_zone_unbind_cooling_device(
657 thermal, trip, cdev);
664 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
665 if (!tz->trips.active[i].valid)
669 for (j = 0; j < tz->trips.active[i].devices.count; j++) {
670 handle = tz->trips.active[i].devices.handles[j];
671 dev = acpi_fetch_acpi_dev(handle);
676 result = thermal_zone_bind_cooling_device(
680 THERMAL_WEIGHT_DEFAULT);
682 result = thermal_zone_unbind_cooling_device(
683 thermal, trip, cdev);
695 acpi_thermal_bind_cooling_device(struct thermal_zone_device *thermal,
696 struct thermal_cooling_device *cdev)
698 return acpi_thermal_cooling_device_cb(thermal, cdev, true);
702 acpi_thermal_unbind_cooling_device(struct thermal_zone_device *thermal,
703 struct thermal_cooling_device *cdev)
705 return acpi_thermal_cooling_device_cb(thermal, cdev, false);
708 static struct thermal_zone_device_ops acpi_thermal_zone_ops = {
709 .bind = acpi_thermal_bind_cooling_device,
710 .unbind = acpi_thermal_unbind_cooling_device,
711 .get_temp = thermal_get_temp,
712 .get_trip_type = thermal_get_trip_type,
713 .get_trip_temp = thermal_get_trip_temp,
714 .get_crit_temp = thermal_get_crit_temp,
715 .get_trend = thermal_get_trend,
716 .hot = acpi_thermal_zone_device_hot,
717 .critical = acpi_thermal_zone_device_critical,
720 static int acpi_thermal_zone_sysfs_add(struct acpi_thermal *tz)
722 struct device *tzdev = thermal_zone_device(tz->thermal_zone);
725 ret = sysfs_create_link(&tz->device->dev.kobj,
726 &tzdev->kobj, "thermal_zone");
730 ret = sysfs_create_link(&tzdev->kobj,
731 &tz->device->dev.kobj, "device");
733 sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
738 static void acpi_thermal_zone_sysfs_remove(struct acpi_thermal *tz)
740 struct device *tzdev = thermal_zone_device(tz->thermal_zone);
742 sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
743 sysfs_remove_link(&tzdev->kobj, "device");
746 static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz)
753 if (tz->trips.critical.valid)
756 if (tz->trips.hot.valid)
759 if (tz->trips.passive.valid)
762 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE && tz->trips.active[i].valid;
765 if (tz->trips.passive.valid)
766 tz->thermal_zone = thermal_zone_device_register("acpitz", trips, 0, tz,
767 &acpi_thermal_zone_ops, NULL,
768 tz->trips.passive.tsp * 100,
769 tz->polling_frequency * 100);
772 thermal_zone_device_register("acpitz", trips, 0, tz,
773 &acpi_thermal_zone_ops, NULL,
774 0, tz->polling_frequency * 100);
776 if (IS_ERR(tz->thermal_zone))
779 result = acpi_thermal_zone_sysfs_add(tz);
783 status = acpi_bus_attach_private_data(tz->device->handle,
785 if (ACPI_FAILURE(status)) {
790 result = thermal_zone_device_enable(tz->thermal_zone);
792 goto acpi_bus_detach;
794 dev_info(&tz->device->dev, "registered as thermal_zone%d\n",
795 thermal_zone_device_id(tz->thermal_zone));
800 acpi_bus_detach_private_data(tz->device->handle);
802 acpi_thermal_zone_sysfs_remove(tz);
804 thermal_zone_device_unregister(tz->thermal_zone);
809 static void acpi_thermal_unregister_thermal_zone(struct acpi_thermal *tz)
811 acpi_thermal_zone_sysfs_remove(tz);
812 thermal_zone_device_unregister(tz->thermal_zone);
813 tz->thermal_zone = NULL;
814 acpi_bus_detach_private_data(tz->device->handle);
818 /* --------------------------------------------------------------------------
820 -------------------------------------------------------------------------- */
822 static void acpi_queue_thermal_check(struct acpi_thermal *tz)
824 if (!work_pending(&tz->thermal_check_work))
825 queue_work(acpi_thermal_pm_queue, &tz->thermal_check_work);
828 static void acpi_thermal_notify(struct acpi_device *device, u32 event)
830 struct acpi_thermal *tz = acpi_driver_data(device);
836 case ACPI_THERMAL_NOTIFY_TEMPERATURE:
837 acpi_queue_thermal_check(tz);
839 case ACPI_THERMAL_NOTIFY_THRESHOLDS:
840 acpi_thermal_trips_update(tz, ACPI_TRIPS_THRESHOLDS);
841 acpi_queue_thermal_check(tz);
842 acpi_bus_generate_netlink_event(device->pnp.device_class,
843 dev_name(&device->dev), event, 0);
845 case ACPI_THERMAL_NOTIFY_DEVICES:
846 acpi_thermal_trips_update(tz, ACPI_TRIPS_DEVICES);
847 acpi_queue_thermal_check(tz);
848 acpi_bus_generate_netlink_event(device->pnp.device_class,
849 dev_name(&device->dev), event, 0);
852 acpi_handle_debug(device->handle, "Unsupported event [0x%x]\n",
859 * On some platforms, the AML code has dependency about
860 * the evaluating order of _TMP and _CRT/_HOT/_PSV/_ACx.
861 * 1. On HP Pavilion G4-1016tx, _TMP must be invoked after
862 * /_CRT/_HOT/_PSV/_ACx, or else system will be power off.
863 * 2. On HP Compaq 6715b/6715s, the return value of _PSV is 0
864 * if _TMP has never been evaluated.
866 * As this dependency is totally transparent to OS, evaluate
867 * all of them once, in the order of _CRT/_HOT/_PSV/_ACx,
868 * _TMP, before they are actually used.
870 static void acpi_thermal_aml_dependency_fix(struct acpi_thermal *tz)
872 acpi_handle handle = tz->device->handle;
873 unsigned long long value;
876 acpi_evaluate_integer(handle, "_CRT", NULL, &value);
877 acpi_evaluate_integer(handle, "_HOT", NULL, &value);
878 acpi_evaluate_integer(handle, "_PSV", NULL, &value);
879 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
880 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
883 status = acpi_evaluate_integer(handle, name, NULL, &value);
884 if (status == AE_NOT_FOUND)
887 acpi_evaluate_integer(handle, "_TMP", NULL, &value);
890 static int acpi_thermal_get_info(struct acpi_thermal *tz)
897 acpi_thermal_aml_dependency_fix(tz);
899 /* Get trip points [_CRT, _PSV, etc.] (required) */
900 result = acpi_thermal_get_trip_points(tz);
904 /* Get temperature [_TMP] (required) */
905 result = acpi_thermal_get_temperature(tz);
909 /* Set the cooling mode [_SCP] to active cooling (default) */
910 acpi_execute_simple_method(tz->device->handle, "_SCP",
911 ACPI_THERMAL_MODE_ACTIVE);
913 /* Get default polling frequency [_TZP] (optional) */
915 tz->polling_frequency = tzp;
917 acpi_thermal_get_polling_frequency(tz);
923 * The exact offset between Kelvin and degree Celsius is 273.15. However ACPI
924 * handles temperature values with a single decimal place. As a consequence,
925 * some implementations use an offset of 273.1 and others use an offset of
926 * 273.2. Try to find out which one is being used, to present the most
927 * accurate and visually appealing number.
929 * The heuristic below should work for all ACPI thermal zones which have a
930 * critical trip point with a value being a multiple of 0.5 degree Celsius.
932 static void acpi_thermal_guess_offset(struct acpi_thermal *tz)
934 if (tz->trips.critical.valid &&
935 (tz->trips.critical.temperature % 5) == 1)
936 tz->kelvin_offset = 273100;
938 tz->kelvin_offset = 273200;
941 static void acpi_thermal_check_fn(struct work_struct *work)
943 struct acpi_thermal *tz = container_of(work, struct acpi_thermal,
947 * In general, it is not sufficient to check the pending bit, because
948 * subsequent instances of this function may be queued after one of them
949 * has started running (e.g. if _TMP sleeps). Avoid bailing out if just
950 * one of them is running, though, because it may have done the actual
951 * check some time ago, so allow at least one of them to block on the
952 * mutex while another one is running the update.
954 if (!refcount_dec_not_one(&tz->thermal_check_count))
957 mutex_lock(&tz->thermal_check_lock);
959 thermal_zone_device_update(tz->thermal_zone, THERMAL_EVENT_UNSPECIFIED);
961 refcount_inc(&tz->thermal_check_count);
963 mutex_unlock(&tz->thermal_check_lock);
966 static int acpi_thermal_add(struct acpi_device *device)
968 struct acpi_thermal *tz;
974 tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
979 strcpy(tz->name, device->pnp.bus_id);
980 strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
981 strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
982 device->driver_data = tz;
984 result = acpi_thermal_get_info(tz);
988 acpi_thermal_guess_offset(tz);
990 result = acpi_thermal_register_thermal_zone(tz);
994 refcount_set(&tz->thermal_check_count, 3);
995 mutex_init(&tz->thermal_check_lock);
996 INIT_WORK(&tz->thermal_check_work, acpi_thermal_check_fn);
998 pr_info("%s [%s] (%ld C)\n", acpi_device_name(device),
999 acpi_device_bid(device), deci_kelvin_to_celsius(tz->temperature));
1008 static void acpi_thermal_remove(struct acpi_device *device)
1010 struct acpi_thermal *tz;
1012 if (!device || !acpi_driver_data(device))
1015 flush_workqueue(acpi_thermal_pm_queue);
1016 tz = acpi_driver_data(device);
1018 acpi_thermal_unregister_thermal_zone(tz);
1022 #ifdef CONFIG_PM_SLEEP
1023 static int acpi_thermal_suspend(struct device *dev)
1025 /* Make sure the previously queued thermal check work has been done */
1026 flush_workqueue(acpi_thermal_pm_queue);
1030 static int acpi_thermal_resume(struct device *dev)
1032 struct acpi_thermal *tz;
1033 int i, j, power_state, result;
1038 tz = acpi_driver_data(to_acpi_device(dev));
1042 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1043 if (!tz->trips.active[i].valid)
1046 tz->trips.active[i].enabled = true;
1047 for (j = 0; j < tz->trips.active[i].devices.count; j++) {
1048 result = acpi_bus_update_power(
1049 tz->trips.active[i].devices.handles[j],
1051 if (result || (power_state != ACPI_STATE_D0)) {
1052 tz->trips.active[i].enabled = false;
1058 acpi_queue_thermal_check(tz);
1063 #define acpi_thermal_suspend NULL
1064 #define acpi_thermal_resume NULL
1066 static SIMPLE_DEV_PM_OPS(acpi_thermal_pm, acpi_thermal_suspend, acpi_thermal_resume);
1068 static const struct acpi_device_id thermal_device_ids[] = {
1069 {ACPI_THERMAL_HID, 0},
1072 MODULE_DEVICE_TABLE(acpi, thermal_device_ids);
1074 static struct acpi_driver acpi_thermal_driver = {
1076 .class = ACPI_THERMAL_CLASS,
1077 .ids = thermal_device_ids,
1079 .add = acpi_thermal_add,
1080 .remove = acpi_thermal_remove,
1081 .notify = acpi_thermal_notify,
1083 .drv.pm = &acpi_thermal_pm,
1086 static int thermal_act(const struct dmi_system_id *d) {
1088 pr_notice("%s detected: disabling all active thermal trip points\n",
1094 static int thermal_nocrt(const struct dmi_system_id *d) {
1095 pr_notice("%s detected: disabling all critical thermal trip point actions.\n",
1100 static int thermal_tzp(const struct dmi_system_id *d) {
1102 pr_notice("%s detected: enabling thermal zone polling\n",
1104 tzp = 300; /* 300 dS = 30 Seconds */
1108 static int thermal_psv(const struct dmi_system_id *d) {
1110 pr_notice("%s detected: disabling all passive thermal trip points\n",
1117 static const struct dmi_system_id thermal_dmi_table[] __initconst = {
1119 * Award BIOS on this AOpen makes thermal control almost worthless.
1120 * http://bugzilla.kernel.org/show_bug.cgi?id=8842
1123 .callback = thermal_act,
1124 .ident = "AOpen i915GMm-HFS",
1126 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1127 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1131 .callback = thermal_psv,
1132 .ident = "AOpen i915GMm-HFS",
1134 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1135 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1139 .callback = thermal_tzp,
1140 .ident = "AOpen i915GMm-HFS",
1142 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1143 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1147 .callback = thermal_nocrt,
1148 .ident = "Gigabyte GA-7ZX",
1150 DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
1151 DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
1157 static int __init acpi_thermal_init(void)
1161 dmi_check_system(thermal_dmi_table);
1164 pr_notice("thermal control disabled\n");
1168 acpi_thermal_pm_queue = alloc_workqueue("acpi_thermal_pm",
1169 WQ_HIGHPRI | WQ_MEM_RECLAIM, 0);
1170 if (!acpi_thermal_pm_queue)
1173 result = acpi_bus_register_driver(&acpi_thermal_driver);
1175 destroy_workqueue(acpi_thermal_pm_queue);
1182 static void __exit acpi_thermal_exit(void)
1184 acpi_bus_unregister_driver(&acpi_thermal_driver);
1185 destroy_workqueue(acpi_thermal_pm_queue);
1188 module_init(acpi_thermal_init);
1189 module_exit(acpi_thermal_exit);
1191 MODULE_AUTHOR("Paul Diefenbaugh");
1192 MODULE_DESCRIPTION("ACPI Thermal Zone Driver");
1193 MODULE_LICENSE("GPL");