1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * acpi_thermal.c - ACPI Thermal Zone Driver ($Revision: 41 $)
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 MODULE_AUTHOR("Paul Diefenbaugh");
47 MODULE_DESCRIPTION("ACPI Thermal Zone Driver");
48 MODULE_LICENSE("GPL");
51 module_param(act, int, 0644);
52 MODULE_PARM_DESC(act, "Disable or override all lowest active trip points.");
55 module_param(crt, int, 0644);
56 MODULE_PARM_DESC(crt, "Disable or lower all critical trip points.");
59 module_param(tzp, int, 0444);
60 MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.");
63 module_param(nocrt, int, 0);
64 MODULE_PARM_DESC(nocrt, "Set to take no action upon ACPI thermal zone critical trips points.");
67 module_param(off, int, 0);
68 MODULE_PARM_DESC(off, "Set to disable ACPI thermal support.");
71 module_param(psv, int, 0644);
72 MODULE_PARM_DESC(psv, "Disable or override all passive trip points.");
74 static struct workqueue_struct *acpi_thermal_pm_queue;
76 static int acpi_thermal_add(struct acpi_device *device);
77 static void acpi_thermal_remove(struct acpi_device *device);
78 static void acpi_thermal_notify(struct acpi_device *device, u32 event);
80 static const struct acpi_device_id thermal_device_ids[] = {
81 {ACPI_THERMAL_HID, 0},
84 MODULE_DEVICE_TABLE(acpi, thermal_device_ids);
86 #ifdef CONFIG_PM_SLEEP
87 static int acpi_thermal_suspend(struct device *dev);
88 static int acpi_thermal_resume(struct device *dev);
90 #define acpi_thermal_suspend NULL
91 #define acpi_thermal_resume NULL
93 static SIMPLE_DEV_PM_OPS(acpi_thermal_pm, acpi_thermal_suspend, acpi_thermal_resume);
95 static struct acpi_driver acpi_thermal_driver = {
97 .class = ACPI_THERMAL_CLASS,
98 .ids = thermal_device_ids,
100 .add = acpi_thermal_add,
101 .remove = acpi_thermal_remove,
102 .notify = acpi_thermal_notify,
104 .drv.pm = &acpi_thermal_pm,
107 struct acpi_thermal_state {
116 struct acpi_thermal_state_flags {
122 struct acpi_thermal_critical {
123 struct acpi_thermal_state_flags flags;
124 unsigned long temperature;
127 struct acpi_thermal_hot {
128 struct acpi_thermal_state_flags flags;
129 unsigned long temperature;
132 struct acpi_thermal_passive {
133 struct acpi_thermal_state_flags flags;
134 unsigned long temperature;
138 struct acpi_handle_list devices;
141 struct acpi_thermal_active {
142 struct acpi_thermal_state_flags flags;
143 unsigned long temperature;
144 struct acpi_handle_list devices;
147 struct acpi_thermal_trips {
148 struct acpi_thermal_critical critical;
149 struct acpi_thermal_hot hot;
150 struct acpi_thermal_passive passive;
151 struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
154 struct acpi_thermal_flags {
155 u8 cooling_mode:1; /* _SCP */
156 u8 devices:1; /* _TZD */
160 struct acpi_thermal {
161 struct acpi_device *device;
163 unsigned long temperature;
164 unsigned long last_temperature;
165 unsigned long polling_frequency;
167 struct acpi_thermal_flags flags;
168 struct acpi_thermal_state state;
169 struct acpi_thermal_trips trips;
170 struct acpi_handle_list devices;
171 struct thermal_zone_device *thermal_zone;
172 int kelvin_offset; /* in millidegrees */
173 struct work_struct thermal_check_work;
174 struct mutex thermal_check_lock;
175 refcount_t thermal_check_count;
178 /* --------------------------------------------------------------------------
179 Thermal Zone Management
180 -------------------------------------------------------------------------- */
182 static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
184 acpi_status status = AE_OK;
185 unsigned long long tmp;
190 tz->last_temperature = tz->temperature;
192 status = acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tmp);
193 if (ACPI_FAILURE(status))
196 tz->temperature = tmp;
198 acpi_handle_debug(tz->device->handle, "Temperature is %lu dK\n",
204 static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
206 acpi_status status = AE_OK;
207 unsigned long long tmp;
212 status = acpi_evaluate_integer(tz->device->handle, "_TZP", NULL, &tmp);
213 if (ACPI_FAILURE(status))
216 tz->polling_frequency = tmp;
217 acpi_handle_debug(tz->device->handle, "Polling frequency is %lu dS\n",
218 tz->polling_frequency);
223 static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
228 if (ACPI_FAILURE(acpi_execute_simple_method(tz->device->handle,
235 #define ACPI_TRIPS_CRITICAL 0x01
236 #define ACPI_TRIPS_HOT 0x02
237 #define ACPI_TRIPS_PASSIVE 0x04
238 #define ACPI_TRIPS_ACTIVE 0x08
239 #define ACPI_TRIPS_DEVICES 0x10
241 #define ACPI_TRIPS_REFRESH_THRESHOLDS (ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE)
242 #define ACPI_TRIPS_REFRESH_DEVICES ACPI_TRIPS_DEVICES
244 #define ACPI_TRIPS_INIT (ACPI_TRIPS_CRITICAL | ACPI_TRIPS_HOT | \
245 ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE | \
249 * This exception is thrown out in two cases:
250 * 1.An invalid trip point becomes invalid or a valid trip point becomes invalid
251 * when re-evaluating the AML code.
252 * 2.TODO: Devices listed in _PSL, _ALx, _TZD may change.
253 * We need to re-bind the cooling devices of a thermal zone when this occurs.
255 #define ACPI_THERMAL_TRIPS_EXCEPTION(flags, tz, str) \
257 if (flags != ACPI_TRIPS_INIT) \
258 acpi_handle_info(tz->device->handle, \
259 "ACPI thermal trip point %s changed\n" \
263 static int acpi_thermal_trips_update(struct acpi_thermal *tz, int flag)
266 unsigned long long tmp;
267 struct acpi_handle_list devices;
271 /* Critical Shutdown */
272 if (flag & ACPI_TRIPS_CRITICAL) {
273 status = acpi_evaluate_integer(tz->device->handle, "_CRT", NULL, &tmp);
274 tz->trips.critical.temperature = tmp;
276 * Treat freezing temperatures as invalid as well; some
277 * BIOSes return really low values and cause reboots at startup.
278 * Below zero (Celsius) values clearly aren't right for sure..
279 * ... so lets discard those as invalid.
281 if (ACPI_FAILURE(status)) {
282 tz->trips.critical.flags.valid = 0;
283 acpi_handle_debug(tz->device->handle,
284 "No critical threshold\n");
285 } else if (tmp <= 2732) {
286 pr_info(FW_BUG "Invalid critical threshold (%llu)\n", tmp);
287 tz->trips.critical.flags.valid = 0;
289 tz->trips.critical.flags.valid = 1;
290 acpi_handle_debug(tz->device->handle,
291 "Found critical threshold [%lu]\n",
292 tz->trips.critical.temperature);
294 if (tz->trips.critical.flags.valid) {
296 tz->trips.critical.flags.valid = 0;
297 } else if (crt > 0) {
298 unsigned long crt_k = celsius_to_deci_kelvin(crt);
301 * Allow override critical threshold
303 if (crt_k > tz->trips.critical.temperature)
304 pr_info("Critical threshold %d C\n", crt);
306 tz->trips.critical.temperature = crt_k;
311 /* Critical Sleep (optional) */
312 if (flag & ACPI_TRIPS_HOT) {
313 status = acpi_evaluate_integer(tz->device->handle, "_HOT", NULL, &tmp);
314 if (ACPI_FAILURE(status)) {
315 tz->trips.hot.flags.valid = 0;
316 acpi_handle_debug(tz->device->handle,
317 "No hot threshold\n");
319 tz->trips.hot.temperature = tmp;
320 tz->trips.hot.flags.valid = 1;
321 acpi_handle_debug(tz->device->handle,
322 "Found hot threshold [%lu]\n",
323 tz->trips.hot.temperature);
327 /* Passive (optional) */
328 if (((flag & ACPI_TRIPS_PASSIVE) && tz->trips.passive.flags.valid) ||
329 flag == ACPI_TRIPS_INIT) {
330 valid = tz->trips.passive.flags.valid;
333 } else if (psv > 0) {
334 tmp = celsius_to_deci_kelvin(psv);
337 status = acpi_evaluate_integer(tz->device->handle,
341 if (ACPI_FAILURE(status)) {
342 tz->trips.passive.flags.valid = 0;
344 tz->trips.passive.temperature = tmp;
345 tz->trips.passive.flags.valid = 1;
346 if (flag == ACPI_TRIPS_INIT) {
347 status = acpi_evaluate_integer(tz->device->handle,
349 if (ACPI_FAILURE(status))
350 tz->trips.passive.flags.valid = 0;
352 tz->trips.passive.tc1 = tmp;
354 status = acpi_evaluate_integer(tz->device->handle,
356 if (ACPI_FAILURE(status))
357 tz->trips.passive.flags.valid = 0;
359 tz->trips.passive.tc2 = tmp;
361 status = acpi_evaluate_integer(tz->device->handle,
363 if (ACPI_FAILURE(status))
364 tz->trips.passive.flags.valid = 0;
366 tz->trips.passive.tsp = tmp;
370 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.passive.flags.valid) {
371 memset(&devices, 0, sizeof(struct acpi_handle_list));
372 status = acpi_evaluate_reference(tz->device->handle, "_PSL",
374 if (ACPI_FAILURE(status)) {
375 acpi_handle_info(tz->device->handle,
376 "Invalid passive threshold\n");
377 tz->trips.passive.flags.valid = 0;
379 tz->trips.passive.flags.valid = 1;
382 if (memcmp(&tz->trips.passive.devices, &devices,
383 sizeof(struct acpi_handle_list))) {
384 memcpy(&tz->trips.passive.devices, &devices,
385 sizeof(struct acpi_handle_list));
386 ACPI_THERMAL_TRIPS_EXCEPTION(flag, tz, "device");
389 if ((flag & ACPI_TRIPS_PASSIVE) || (flag & ACPI_TRIPS_DEVICES)) {
390 if (valid != tz->trips.passive.flags.valid)
391 ACPI_THERMAL_TRIPS_EXCEPTION(flag, tz, "state");
394 /* Active (optional) */
395 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
396 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
397 valid = tz->trips.active[i].flags.valid;
400 break; /* disable all active trip points */
402 if (flag == ACPI_TRIPS_INIT || ((flag & ACPI_TRIPS_ACTIVE) &&
403 tz->trips.active[i].flags.valid)) {
404 status = acpi_evaluate_integer(tz->device->handle,
406 if (ACPI_FAILURE(status)) {
407 tz->trips.active[i].flags.valid = 0;
415 tz->trips.active[0].temperature = celsius_to_deci_kelvin(act);
418 * Don't allow override higher than
419 * the next higher trip point
421 tz->trips.active[i-1].temperature =
423 tz->trips.active[i-2].temperature,
424 celsius_to_deci_kelvin(act));
428 tz->trips.active[i].temperature = tmp;
429 tz->trips.active[i].flags.valid = 1;
434 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.active[i].flags.valid) {
435 memset(&devices, 0, sizeof(struct acpi_handle_list));
436 status = acpi_evaluate_reference(tz->device->handle,
437 name, NULL, &devices);
438 if (ACPI_FAILURE(status)) {
439 acpi_handle_info(tz->device->handle,
440 "Invalid active%d threshold\n", i);
441 tz->trips.active[i].flags.valid = 0;
443 tz->trips.active[i].flags.valid = 1;
446 if (memcmp(&tz->trips.active[i].devices, &devices,
447 sizeof(struct acpi_handle_list))) {
448 memcpy(&tz->trips.active[i].devices, &devices,
449 sizeof(struct acpi_handle_list));
450 ACPI_THERMAL_TRIPS_EXCEPTION(flag, tz, "device");
453 if ((flag & ACPI_TRIPS_ACTIVE) || (flag & ACPI_TRIPS_DEVICES))
454 if (valid != tz->trips.active[i].flags.valid)
455 ACPI_THERMAL_TRIPS_EXCEPTION(flag, tz, "state");
457 if (!tz->trips.active[i].flags.valid)
461 if (flag & ACPI_TRIPS_DEVICES) {
462 memset(&devices, 0, sizeof(devices));
463 status = acpi_evaluate_reference(tz->device->handle, "_TZD",
465 if (ACPI_SUCCESS(status) &&
466 memcmp(&tz->devices, &devices, sizeof(devices))) {
467 tz->devices = devices;
468 ACPI_THERMAL_TRIPS_EXCEPTION(flag, tz, "device");
475 static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
477 int i, valid, ret = acpi_thermal_trips_update(tz, ACPI_TRIPS_INIT);
482 valid = tz->trips.critical.flags.valid |
483 tz->trips.hot.flags.valid |
484 tz->trips.passive.flags.valid;
486 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
487 valid |= tz->trips.active[i].flags.valid;
490 pr_warn(FW_BUG "No valid trip found\n");
496 /* sys I/F for generic thermal sysfs support */
498 static int thermal_get_temp(struct thermal_zone_device *thermal, int *temp)
500 struct acpi_thermal *tz = thermal_zone_device_priv(thermal);
506 result = acpi_thermal_get_temperature(tz);
510 *temp = deci_kelvin_to_millicelsius_with_offset(tz->temperature,
515 static int thermal_get_trip_type(struct thermal_zone_device *thermal,
516 int trip, enum thermal_trip_type *type)
518 struct acpi_thermal *tz = thermal_zone_device_priv(thermal);
524 if (tz->trips.critical.flags.valid) {
526 *type = THERMAL_TRIP_CRITICAL;
532 if (tz->trips.hot.flags.valid) {
534 *type = THERMAL_TRIP_HOT;
540 if (tz->trips.passive.flags.valid) {
542 *type = THERMAL_TRIP_PASSIVE;
548 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE && tz->trips.active[i].flags.valid; i++) {
550 *type = THERMAL_TRIP_ACTIVE;
559 static int thermal_get_trip_temp(struct thermal_zone_device *thermal,
562 struct acpi_thermal *tz = thermal_zone_device_priv(thermal);
568 if (tz->trips.critical.flags.valid) {
570 *temp = deci_kelvin_to_millicelsius_with_offset(
571 tz->trips.critical.temperature,
578 if (tz->trips.hot.flags.valid) {
580 *temp = deci_kelvin_to_millicelsius_with_offset(
581 tz->trips.hot.temperature,
588 if (tz->trips.passive.flags.valid) {
590 *temp = deci_kelvin_to_millicelsius_with_offset(
591 tz->trips.passive.temperature,
598 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
599 tz->trips.active[i].flags.valid; i++) {
601 *temp = deci_kelvin_to_millicelsius_with_offset(
602 tz->trips.active[i].temperature,
612 static int thermal_get_crit_temp(struct thermal_zone_device *thermal,
615 struct acpi_thermal *tz = thermal_zone_device_priv(thermal);
617 if (tz->trips.critical.flags.valid) {
618 *temperature = deci_kelvin_to_millicelsius_with_offset(
619 tz->trips.critical.temperature,
627 static int thermal_get_trend(struct thermal_zone_device *thermal,
628 int trip, enum thermal_trend *trend)
630 struct acpi_thermal *tz = thermal_zone_device_priv(thermal);
631 enum thermal_trip_type type;
634 if (thermal_get_trip_type(thermal, trip, &type))
637 if (type == THERMAL_TRIP_ACTIVE) {
639 int temp = deci_kelvin_to_millicelsius_with_offset(
640 tz->temperature, tz->kelvin_offset);
641 if (thermal_get_trip_temp(thermal, trip, &trip_temp))
644 if (temp > trip_temp) {
645 *trend = THERMAL_TREND_RAISING;
648 /* Fall back on default trend */
654 * tz->temperature has already been updated by generic thermal layer,
655 * before this callback being invoked
657 i = tz->trips.passive.tc1 * (tz->temperature - tz->last_temperature) +
658 tz->trips.passive.tc2 * (tz->temperature - tz->trips.passive.temperature);
661 *trend = THERMAL_TREND_RAISING;
663 *trend = THERMAL_TREND_DROPPING;
665 *trend = THERMAL_TREND_STABLE;
670 static void acpi_thermal_zone_device_hot(struct thermal_zone_device *thermal)
672 struct acpi_thermal *tz = thermal_zone_device_priv(thermal);
674 acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
675 dev_name(&tz->device->dev),
676 ACPI_THERMAL_NOTIFY_HOT, 1);
679 static void acpi_thermal_zone_device_critical(struct thermal_zone_device *thermal)
681 struct acpi_thermal *tz = thermal_zone_device_priv(thermal);
683 acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
684 dev_name(&tz->device->dev),
685 ACPI_THERMAL_NOTIFY_CRITICAL, 1);
687 thermal_zone_device_critical(thermal);
690 static int acpi_thermal_cooling_device_cb(struct thermal_zone_device *thermal,
691 struct thermal_cooling_device *cdev,
694 struct acpi_device *device = cdev->devdata;
695 struct acpi_thermal *tz = thermal_zone_device_priv(thermal);
696 struct acpi_device *dev;
703 if (tz->trips.critical.flags.valid)
706 if (tz->trips.hot.flags.valid)
709 if (tz->trips.passive.flags.valid) {
711 for (i = 0; i < tz->trips.passive.devices.count; i++) {
712 handle = tz->trips.passive.devices.handles[i];
713 dev = acpi_fetch_acpi_dev(handle);
718 result = thermal_zone_bind_cooling_device(
722 THERMAL_WEIGHT_DEFAULT);
725 thermal_zone_unbind_cooling_device(
726 thermal, trip, cdev);
733 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
734 if (!tz->trips.active[i].flags.valid)
738 for (j = 0; j < tz->trips.active[i].devices.count; j++) {
739 handle = tz->trips.active[i].devices.handles[j];
740 dev = acpi_fetch_acpi_dev(handle);
745 result = thermal_zone_bind_cooling_device(
749 THERMAL_WEIGHT_DEFAULT);
751 result = thermal_zone_unbind_cooling_device(
752 thermal, trip, cdev);
764 acpi_thermal_bind_cooling_device(struct thermal_zone_device *thermal,
765 struct thermal_cooling_device *cdev)
767 return acpi_thermal_cooling_device_cb(thermal, cdev, true);
771 acpi_thermal_unbind_cooling_device(struct thermal_zone_device *thermal,
772 struct thermal_cooling_device *cdev)
774 return acpi_thermal_cooling_device_cb(thermal, cdev, false);
777 static struct thermal_zone_device_ops acpi_thermal_zone_ops = {
778 .bind = acpi_thermal_bind_cooling_device,
779 .unbind = acpi_thermal_unbind_cooling_device,
780 .get_temp = thermal_get_temp,
781 .get_trip_type = thermal_get_trip_type,
782 .get_trip_temp = thermal_get_trip_temp,
783 .get_crit_temp = thermal_get_crit_temp,
784 .get_trend = thermal_get_trend,
785 .hot = acpi_thermal_zone_device_hot,
786 .critical = acpi_thermal_zone_device_critical,
789 static int acpi_thermal_zone_sysfs_add(struct acpi_thermal *tz)
791 struct device *tzdev = thermal_zone_device(tz->thermal_zone);
794 ret = sysfs_create_link(&tz->device->dev.kobj,
795 &tzdev->kobj, "thermal_zone");
799 ret = sysfs_create_link(&tzdev->kobj,
800 &tz->device->dev.kobj, "device");
802 sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
807 static void acpi_thermal_zone_sysfs_remove(struct acpi_thermal *tz)
809 struct device *tzdev = thermal_zone_device(tz->thermal_zone);
811 sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
812 sysfs_remove_link(&tzdev->kobj, "device");
815 static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz)
822 if (tz->trips.critical.flags.valid)
825 if (tz->trips.hot.flags.valid)
828 if (tz->trips.passive.flags.valid)
831 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE && tz->trips.active[i].flags.valid;
834 if (tz->trips.passive.flags.valid)
835 tz->thermal_zone = thermal_zone_device_register("acpitz", trips, 0, tz,
836 &acpi_thermal_zone_ops, NULL,
837 tz->trips.passive.tsp * 100,
838 tz->polling_frequency * 100);
841 thermal_zone_device_register("acpitz", trips, 0, tz,
842 &acpi_thermal_zone_ops, NULL,
843 0, tz->polling_frequency * 100);
845 if (IS_ERR(tz->thermal_zone))
848 result = acpi_thermal_zone_sysfs_add(tz);
852 status = acpi_bus_attach_private_data(tz->device->handle,
854 if (ACPI_FAILURE(status)) {
859 result = thermal_zone_device_enable(tz->thermal_zone);
861 goto acpi_bus_detach;
863 dev_info(&tz->device->dev, "registered as thermal_zone%d\n",
864 thermal_zone_device_id(tz->thermal_zone));
869 acpi_bus_detach_private_data(tz->device->handle);
871 acpi_thermal_zone_sysfs_remove(tz);
873 thermal_zone_device_unregister(tz->thermal_zone);
878 static void acpi_thermal_unregister_thermal_zone(struct acpi_thermal *tz)
880 acpi_thermal_zone_sysfs_remove(tz);
881 thermal_zone_device_unregister(tz->thermal_zone);
882 tz->thermal_zone = NULL;
883 acpi_bus_detach_private_data(tz->device->handle);
887 /* --------------------------------------------------------------------------
889 -------------------------------------------------------------------------- */
891 static void acpi_queue_thermal_check(struct acpi_thermal *tz)
893 if (!work_pending(&tz->thermal_check_work))
894 queue_work(acpi_thermal_pm_queue, &tz->thermal_check_work);
897 static void acpi_thermal_notify(struct acpi_device *device, u32 event)
899 struct acpi_thermal *tz = acpi_driver_data(device);
905 case ACPI_THERMAL_NOTIFY_TEMPERATURE:
906 acpi_queue_thermal_check(tz);
908 case ACPI_THERMAL_NOTIFY_THRESHOLDS:
909 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_THRESHOLDS);
910 acpi_queue_thermal_check(tz);
911 acpi_bus_generate_netlink_event(device->pnp.device_class,
912 dev_name(&device->dev), event, 0);
914 case ACPI_THERMAL_NOTIFY_DEVICES:
915 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_DEVICES);
916 acpi_queue_thermal_check(tz);
917 acpi_bus_generate_netlink_event(device->pnp.device_class,
918 dev_name(&device->dev), event, 0);
921 acpi_handle_debug(device->handle, "Unsupported event [0x%x]\n",
928 * On some platforms, the AML code has dependency about
929 * the evaluating order of _TMP and _CRT/_HOT/_PSV/_ACx.
930 * 1. On HP Pavilion G4-1016tx, _TMP must be invoked after
931 * /_CRT/_HOT/_PSV/_ACx, or else system will be power off.
932 * 2. On HP Compaq 6715b/6715s, the return value of _PSV is 0
933 * if _TMP has never been evaluated.
935 * As this dependency is totally transparent to OS, evaluate
936 * all of them once, in the order of _CRT/_HOT/_PSV/_ACx,
937 * _TMP, before they are actually used.
939 static void acpi_thermal_aml_dependency_fix(struct acpi_thermal *tz)
941 acpi_handle handle = tz->device->handle;
942 unsigned long long value;
945 acpi_evaluate_integer(handle, "_CRT", NULL, &value);
946 acpi_evaluate_integer(handle, "_HOT", NULL, &value);
947 acpi_evaluate_integer(handle, "_PSV", NULL, &value);
948 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
949 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
952 status = acpi_evaluate_integer(handle, name, NULL, &value);
953 if (status == AE_NOT_FOUND)
956 acpi_evaluate_integer(handle, "_TMP", NULL, &value);
959 static int acpi_thermal_get_info(struct acpi_thermal *tz)
966 acpi_thermal_aml_dependency_fix(tz);
968 /* Get trip points [_CRT, _PSV, etc.] (required) */
969 result = acpi_thermal_get_trip_points(tz);
973 /* Get temperature [_TMP] (required) */
974 result = acpi_thermal_get_temperature(tz);
978 /* Set the cooling mode [_SCP] to active cooling (default) */
979 result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
981 tz->flags.cooling_mode = 1;
983 /* Get default polling frequency [_TZP] (optional) */
985 tz->polling_frequency = tzp;
987 acpi_thermal_get_polling_frequency(tz);
993 * The exact offset between Kelvin and degree Celsius is 273.15. However ACPI
994 * handles temperature values with a single decimal place. As a consequence,
995 * some implementations use an offset of 273.1 and others use an offset of
996 * 273.2. Try to find out which one is being used, to present the most
997 * accurate and visually appealing number.
999 * The heuristic below should work for all ACPI thermal zones which have a
1000 * critical trip point with a value being a multiple of 0.5 degree Celsius.
1002 static void acpi_thermal_guess_offset(struct acpi_thermal *tz)
1004 if (tz->trips.critical.flags.valid &&
1005 (tz->trips.critical.temperature % 5) == 1)
1006 tz->kelvin_offset = 273100;
1008 tz->kelvin_offset = 273200;
1011 static void acpi_thermal_check_fn(struct work_struct *work)
1013 struct acpi_thermal *tz = container_of(work, struct acpi_thermal,
1014 thermal_check_work);
1017 * In general, it is not sufficient to check the pending bit, because
1018 * subsequent instances of this function may be queued after one of them
1019 * has started running (e.g. if _TMP sleeps). Avoid bailing out if just
1020 * one of them is running, though, because it may have done the actual
1021 * check some time ago, so allow at least one of them to block on the
1022 * mutex while another one is running the update.
1024 if (!refcount_dec_not_one(&tz->thermal_check_count))
1027 mutex_lock(&tz->thermal_check_lock);
1029 thermal_zone_device_update(tz->thermal_zone, THERMAL_EVENT_UNSPECIFIED);
1031 refcount_inc(&tz->thermal_check_count);
1033 mutex_unlock(&tz->thermal_check_lock);
1036 static int acpi_thermal_add(struct acpi_device *device)
1038 struct acpi_thermal *tz;
1044 tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
1048 tz->device = device;
1049 strcpy(tz->name, device->pnp.bus_id);
1050 strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1051 strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
1052 device->driver_data = tz;
1054 result = acpi_thermal_get_info(tz);
1058 acpi_thermal_guess_offset(tz);
1060 result = acpi_thermal_register_thermal_zone(tz);
1064 refcount_set(&tz->thermal_check_count, 3);
1065 mutex_init(&tz->thermal_check_lock);
1066 INIT_WORK(&tz->thermal_check_work, acpi_thermal_check_fn);
1068 pr_info("%s [%s] (%ld C)\n", acpi_device_name(device),
1069 acpi_device_bid(device), deci_kelvin_to_celsius(tz->temperature));
1078 static void acpi_thermal_remove(struct acpi_device *device)
1080 struct acpi_thermal *tz;
1082 if (!device || !acpi_driver_data(device))
1085 flush_workqueue(acpi_thermal_pm_queue);
1086 tz = acpi_driver_data(device);
1088 acpi_thermal_unregister_thermal_zone(tz);
1092 #ifdef CONFIG_PM_SLEEP
1093 static int acpi_thermal_suspend(struct device *dev)
1095 /* Make sure the previously queued thermal check work has been done */
1096 flush_workqueue(acpi_thermal_pm_queue);
1100 static int acpi_thermal_resume(struct device *dev)
1102 struct acpi_thermal *tz;
1103 int i, j, power_state, result;
1108 tz = acpi_driver_data(to_acpi_device(dev));
1112 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1113 if (!tz->trips.active[i].flags.valid)
1116 tz->trips.active[i].flags.enabled = 1;
1117 for (j = 0; j < tz->trips.active[i].devices.count; j++) {
1118 result = acpi_bus_update_power(
1119 tz->trips.active[i].devices.handles[j],
1121 if (result || (power_state != ACPI_STATE_D0)) {
1122 tz->trips.active[i].flags.enabled = 0;
1126 tz->state.active |= tz->trips.active[i].flags.enabled;
1129 acpi_queue_thermal_check(tz);
1135 static int thermal_act(const struct dmi_system_id *d) {
1137 pr_notice("%s detected: disabling all active thermal trip points\n",
1143 static int thermal_nocrt(const struct dmi_system_id *d) {
1144 pr_notice("%s detected: disabling all critical thermal trip point actions.\n",
1149 static int thermal_tzp(const struct dmi_system_id *d) {
1151 pr_notice("%s detected: enabling thermal zone polling\n",
1153 tzp = 300; /* 300 dS = 30 Seconds */
1157 static int thermal_psv(const struct dmi_system_id *d) {
1159 pr_notice("%s detected: disabling all passive thermal trip points\n",
1166 static const struct dmi_system_id thermal_dmi_table[] __initconst = {
1168 * Award BIOS on this AOpen makes thermal control almost worthless.
1169 * http://bugzilla.kernel.org/show_bug.cgi?id=8842
1172 .callback = thermal_act,
1173 .ident = "AOpen i915GMm-HFS",
1175 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1176 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1180 .callback = thermal_psv,
1181 .ident = "AOpen i915GMm-HFS",
1183 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1184 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1188 .callback = thermal_tzp,
1189 .ident = "AOpen i915GMm-HFS",
1191 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1192 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1196 .callback = thermal_nocrt,
1197 .ident = "Gigabyte GA-7ZX",
1199 DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
1200 DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
1206 static int __init acpi_thermal_init(void)
1210 dmi_check_system(thermal_dmi_table);
1213 pr_notice("thermal control disabled\n");
1217 acpi_thermal_pm_queue = alloc_workqueue("acpi_thermal_pm",
1218 WQ_HIGHPRI | WQ_MEM_RECLAIM, 0);
1219 if (!acpi_thermal_pm_queue)
1222 result = acpi_bus_register_driver(&acpi_thermal_driver);
1224 destroy_workqueue(acpi_thermal_pm_queue);
1231 static void __exit acpi_thermal_exit(void)
1233 acpi_bus_unregister_driver(&acpi_thermal_driver);
1234 destroy_workqueue(acpi_thermal_pm_queue);
1237 module_init(acpi_thermal_init);
1238 module_exit(acpi_thermal_exit);