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(off, int, 0);
87 MODULE_PARM_DESC(off, "Set to disable ACPI thermal support.");
90 module_param(psv, int, 0644);
91 MODULE_PARM_DESC(psv, "Disable or override all passive trip points.");
93 static struct workqueue_struct *acpi_thermal_pm_queue;
95 struct acpi_thermal_trip {
96 unsigned long temperature;
100 struct acpi_thermal_passive {
101 struct acpi_thermal_trip trip;
102 struct acpi_handle_list devices;
108 struct acpi_thermal_active {
109 struct acpi_thermal_trip trip;
110 struct acpi_handle_list devices;
113 struct acpi_thermal_trips {
114 struct acpi_thermal_trip critical;
115 struct acpi_thermal_trip hot;
116 struct acpi_thermal_passive passive;
117 struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
120 struct acpi_thermal {
121 struct acpi_device *device;
123 unsigned long temperature;
124 unsigned long last_temperature;
125 unsigned long polling_frequency;
127 struct acpi_thermal_trips trips;
128 struct thermal_trip *trip_table;
129 struct acpi_handle_list devices;
130 struct thermal_zone_device *thermal_zone;
131 int kelvin_offset; /* in millidegrees */
132 struct work_struct thermal_check_work;
133 struct mutex thermal_check_lock;
134 refcount_t thermal_check_count;
137 /* --------------------------------------------------------------------------
138 Thermal Zone Management
139 -------------------------------------------------------------------------- */
141 static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
143 acpi_status status = AE_OK;
144 unsigned long long tmp;
149 tz->last_temperature = tz->temperature;
151 status = acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tmp);
152 if (ACPI_FAILURE(status))
155 tz->temperature = tmp;
157 acpi_handle_debug(tz->device->handle, "Temperature is %lu dK\n",
163 static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
165 acpi_status status = AE_OK;
166 unsigned long long tmp;
171 status = acpi_evaluate_integer(tz->device->handle, "_TZP", NULL, &tmp);
172 if (ACPI_FAILURE(status))
175 tz->polling_frequency = tmp;
176 acpi_handle_debug(tz->device->handle, "Polling frequency is %lu dS\n",
177 tz->polling_frequency);
182 static int acpi_thermal_temp(struct acpi_thermal *tz, int temp_deci_k)
184 if (temp_deci_k == THERMAL_TEMP_INVALID)
185 return THERMAL_TEMP_INVALID;
187 return deci_kelvin_to_millicelsius_with_offset(temp_deci_k,
191 static void __acpi_thermal_trips_update(struct acpi_thermal *tz, int flag)
194 unsigned long long tmp;
195 struct acpi_handle_list devices;
199 /* Critical Shutdown */
200 if (flag & ACPI_TRIPS_CRITICAL) {
201 status = acpi_evaluate_integer(tz->device->handle, "_CRT", NULL, &tmp);
202 tz->trips.critical.temperature = tmp;
204 * Treat freezing temperatures as invalid as well; some
205 * BIOSes return really low values and cause reboots at startup.
206 * Below zero (Celsius) values clearly aren't right for sure..
207 * ... so lets discard those as invalid.
209 if (ACPI_FAILURE(status)) {
210 tz->trips.critical.valid = false;
211 acpi_handle_debug(tz->device->handle,
212 "No critical threshold\n");
213 } else if (tmp <= 2732) {
214 pr_info(FW_BUG "Invalid critical threshold (%llu)\n", tmp);
215 tz->trips.critical.valid = false;
217 tz->trips.critical.valid = true;
218 acpi_handle_debug(tz->device->handle,
219 "Found critical threshold [%lu]\n",
220 tz->trips.critical.temperature);
222 if (tz->trips.critical.valid) {
224 tz->trips.critical.valid = false;
225 } else if (crt > 0) {
226 unsigned long crt_k = celsius_to_deci_kelvin(crt);
229 * Allow override critical threshold
231 if (crt_k > tz->trips.critical.temperature)
232 pr_info("Critical threshold %d C\n", crt);
234 tz->trips.critical.temperature = crt_k;
239 /* Critical Sleep (optional) */
240 if (flag & ACPI_TRIPS_HOT) {
241 status = acpi_evaluate_integer(tz->device->handle, "_HOT", NULL, &tmp);
242 if (ACPI_FAILURE(status)) {
243 tz->trips.hot.valid = false;
244 acpi_handle_debug(tz->device->handle,
245 "No hot threshold\n");
247 tz->trips.hot.temperature = tmp;
248 tz->trips.hot.valid = true;
249 acpi_handle_debug(tz->device->handle,
250 "Found hot threshold [%lu]\n",
251 tz->trips.hot.temperature);
255 /* Passive (optional) */
256 if (((flag & ACPI_TRIPS_PASSIVE) && tz->trips.passive.trip.valid) ||
257 flag == ACPI_TRIPS_INIT) {
258 valid = tz->trips.passive.trip.valid;
261 } else if (psv > 0) {
262 tmp = celsius_to_deci_kelvin(psv);
265 status = acpi_evaluate_integer(tz->device->handle,
269 if (ACPI_FAILURE(status)) {
270 tz->trips.passive.trip.valid = false;
272 tz->trips.passive.trip.temperature = tmp;
273 tz->trips.passive.trip.valid = true;
274 if (flag == ACPI_TRIPS_INIT) {
275 status = acpi_evaluate_integer(tz->device->handle,
277 if (ACPI_FAILURE(status))
278 tz->trips.passive.trip.valid = false;
280 tz->trips.passive.tc1 = tmp;
282 status = acpi_evaluate_integer(tz->device->handle,
284 if (ACPI_FAILURE(status))
285 tz->trips.passive.trip.valid = false;
287 tz->trips.passive.tc2 = tmp;
289 status = acpi_evaluate_integer(tz->device->handle,
291 if (ACPI_FAILURE(status))
292 tz->trips.passive.trip.valid = false;
294 tz->trips.passive.tsp = tmp;
298 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.passive.trip.valid) {
299 memset(&devices, 0, sizeof(struct acpi_handle_list));
300 status = acpi_evaluate_reference(tz->device->handle, "_PSL",
302 if (ACPI_FAILURE(status)) {
303 acpi_handle_info(tz->device->handle,
304 "Invalid passive threshold\n");
305 tz->trips.passive.trip.valid = false;
307 tz->trips.passive.trip.valid = true;
310 if (memcmp(&tz->trips.passive.devices, &devices,
311 sizeof(struct acpi_handle_list))) {
312 memcpy(&tz->trips.passive.devices, &devices,
313 sizeof(struct acpi_handle_list));
314 ACPI_THERMAL_TRIPS_EXCEPTION(flag, tz, "device");
317 if ((flag & ACPI_TRIPS_PASSIVE) || (flag & ACPI_TRIPS_DEVICES)) {
318 if (valid != tz->trips.passive.trip.valid)
319 ACPI_THERMAL_TRIPS_EXCEPTION(flag, tz, "state");
322 /* Active (optional) */
323 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
324 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
325 valid = tz->trips.active[i].trip.valid;
328 break; /* disable all active trip points */
330 if (flag == ACPI_TRIPS_INIT || ((flag & ACPI_TRIPS_ACTIVE) &&
331 tz->trips.active[i].trip.valid)) {
332 status = acpi_evaluate_integer(tz->device->handle,
334 if (ACPI_FAILURE(status)) {
335 tz->trips.active[i].trip.valid = false;
343 tz->trips.active[0].trip.temperature =
344 celsius_to_deci_kelvin(act);
347 * Don't allow override higher than
348 * the next higher trip point
350 tz->trips.active[i-1].trip.temperature =
352 tz->trips.active[i-2].trip.temperature,
353 celsius_to_deci_kelvin(act));
357 tz->trips.active[i].trip.temperature = tmp;
358 tz->trips.active[i].trip.valid = true;
363 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.active[i].trip.valid) {
364 memset(&devices, 0, sizeof(struct acpi_handle_list));
365 status = acpi_evaluate_reference(tz->device->handle,
366 name, NULL, &devices);
367 if (ACPI_FAILURE(status)) {
368 acpi_handle_info(tz->device->handle,
369 "Invalid active%d threshold\n", i);
370 tz->trips.active[i].trip.valid = false;
372 tz->trips.active[i].trip.valid = true;
375 if (memcmp(&tz->trips.active[i].devices, &devices,
376 sizeof(struct acpi_handle_list))) {
377 memcpy(&tz->trips.active[i].devices, &devices,
378 sizeof(struct acpi_handle_list));
379 ACPI_THERMAL_TRIPS_EXCEPTION(flag, tz, "device");
382 if ((flag & ACPI_TRIPS_ACTIVE) || (flag & ACPI_TRIPS_DEVICES))
383 if (valid != tz->trips.active[i].trip.valid)
384 ACPI_THERMAL_TRIPS_EXCEPTION(flag, tz, "state");
386 if (!tz->trips.active[i].trip.valid)
390 if (flag & ACPI_TRIPS_DEVICES) {
391 memset(&devices, 0, sizeof(devices));
392 status = acpi_evaluate_reference(tz->device->handle, "_TZD",
394 if (ACPI_SUCCESS(status) &&
395 memcmp(&tz->devices, &devices, sizeof(devices))) {
396 tz->devices = devices;
397 ACPI_THERMAL_TRIPS_EXCEPTION(flag, tz, "device");
402 static int acpi_thermal_adjust_trip(struct thermal_trip *trip, void *data)
404 struct acpi_thermal_trip *acpi_trip = trip->priv;
405 struct acpi_thermal *tz = data;
410 if (acpi_trip->valid)
411 trip->temperature = acpi_thermal_temp(tz, acpi_trip->temperature);
413 trip->temperature = THERMAL_TEMP_INVALID;
418 static void acpi_thermal_adjust_thermal_zone(struct thermal_zone_device *thermal,
421 struct acpi_thermal *tz = thermal_zone_device_priv(thermal);
422 int flag = data == ACPI_THERMAL_NOTIFY_THRESHOLDS ?
423 ACPI_TRIPS_THRESHOLDS : ACPI_TRIPS_DEVICES;
425 __acpi_thermal_trips_update(tz, flag);
427 for_each_thermal_trip(tz->thermal_zone, acpi_thermal_adjust_trip, tz);
430 static void acpi_queue_thermal_check(struct acpi_thermal *tz)
432 if (!work_pending(&tz->thermal_check_work))
433 queue_work(acpi_thermal_pm_queue, &tz->thermal_check_work);
436 static void acpi_thermal_trips_update(struct acpi_thermal *tz, u32 event)
438 struct acpi_device *adev = tz->device;
441 * Use thermal_zone_device_exec() to carry out the trip points
442 * update, so as to protect thermal_get_trend() from getting stale
443 * trip point temperatures and to prevent thermal_zone_device_update()
444 * invoked from acpi_thermal_check_fn() from producing inconsistent
447 thermal_zone_device_exec(tz->thermal_zone,
448 acpi_thermal_adjust_thermal_zone, event);
449 acpi_queue_thermal_check(tz);
450 acpi_bus_generate_netlink_event(adev->pnp.device_class,
451 dev_name(&adev->dev), event, 0);
454 static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
459 __acpi_thermal_trips_update(tz, ACPI_TRIPS_INIT);
461 valid = tz->trips.critical.valid |
462 tz->trips.hot.valid |
463 tz->trips.passive.trip.valid;
465 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
466 valid = valid || tz->trips.active[i].trip.valid;
469 pr_warn(FW_BUG "No valid trip found\n");
475 /* sys I/F for generic thermal sysfs support */
477 static int thermal_get_temp(struct thermal_zone_device *thermal, int *temp)
479 struct acpi_thermal *tz = thermal_zone_device_priv(thermal);
485 result = acpi_thermal_get_temperature(tz);
489 *temp = deci_kelvin_to_millicelsius_with_offset(tz->temperature,
494 static int thermal_get_trend(struct thermal_zone_device *thermal,
495 const struct thermal_trip *trip,
496 enum thermal_trend *trend)
498 struct acpi_thermal *tz = thermal_zone_device_priv(thermal);
499 struct acpi_thermal_trip *acpi_trip;
505 acpi_trip = trip->priv;
506 if (!acpi_trip || !acpi_trip->valid)
509 switch (trip->type) {
510 case THERMAL_TRIP_PASSIVE:
511 t = tz->trips.passive.tc1 * (tz->temperature -
512 tz->last_temperature) +
513 tz->trips.passive.tc2 * (tz->temperature -
514 acpi_trip->temperature);
516 *trend = THERMAL_TREND_RAISING;
518 *trend = THERMAL_TREND_DROPPING;
520 *trend = THERMAL_TREND_STABLE;
524 case THERMAL_TRIP_ACTIVE:
525 t = acpi_thermal_temp(tz, tz->temperature);
526 if (t <= trip->temperature)
529 *trend = THERMAL_TREND_RAISING;
540 static void acpi_thermal_zone_device_hot(struct thermal_zone_device *thermal)
542 struct acpi_thermal *tz = thermal_zone_device_priv(thermal);
544 acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
545 dev_name(&tz->device->dev),
546 ACPI_THERMAL_NOTIFY_HOT, 1);
549 static void acpi_thermal_zone_device_critical(struct thermal_zone_device *thermal)
551 struct acpi_thermal *tz = thermal_zone_device_priv(thermal);
553 acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
554 dev_name(&tz->device->dev),
555 ACPI_THERMAL_NOTIFY_CRITICAL, 1);
557 thermal_zone_device_critical(thermal);
560 static int acpi_thermal_cooling_device_cb(struct thermal_zone_device *thermal,
561 struct thermal_cooling_device *cdev,
564 struct acpi_device *device = cdev->devdata;
565 struct acpi_thermal *tz = thermal_zone_device_priv(thermal);
566 struct acpi_device *dev;
573 if (tz->trips.critical.valid)
576 if (tz->trips.hot.valid)
579 if (tz->trips.passive.trip.valid) {
581 for (i = 0; i < tz->trips.passive.devices.count; i++) {
582 handle = tz->trips.passive.devices.handles[i];
583 dev = acpi_fetch_acpi_dev(handle);
588 result = thermal_zone_bind_cooling_device(
592 THERMAL_WEIGHT_DEFAULT);
595 thermal_zone_unbind_cooling_device(
596 thermal, trip, cdev);
603 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
604 if (!tz->trips.active[i].trip.valid)
608 for (j = 0; j < tz->trips.active[i].devices.count; j++) {
609 handle = tz->trips.active[i].devices.handles[j];
610 dev = acpi_fetch_acpi_dev(handle);
615 result = thermal_zone_bind_cooling_device(
619 THERMAL_WEIGHT_DEFAULT);
621 result = thermal_zone_unbind_cooling_device(
622 thermal, trip, cdev);
634 acpi_thermal_bind_cooling_device(struct thermal_zone_device *thermal,
635 struct thermal_cooling_device *cdev)
637 return acpi_thermal_cooling_device_cb(thermal, cdev, true);
641 acpi_thermal_unbind_cooling_device(struct thermal_zone_device *thermal,
642 struct thermal_cooling_device *cdev)
644 return acpi_thermal_cooling_device_cb(thermal, cdev, false);
647 static struct thermal_zone_device_ops acpi_thermal_zone_ops = {
648 .bind = acpi_thermal_bind_cooling_device,
649 .unbind = acpi_thermal_unbind_cooling_device,
650 .get_temp = thermal_get_temp,
651 .get_trend = thermal_get_trend,
652 .hot = acpi_thermal_zone_device_hot,
653 .critical = acpi_thermal_zone_device_critical,
656 static int acpi_thermal_zone_sysfs_add(struct acpi_thermal *tz)
658 struct device *tzdev = thermal_zone_device(tz->thermal_zone);
661 ret = sysfs_create_link(&tz->device->dev.kobj,
662 &tzdev->kobj, "thermal_zone");
666 ret = sysfs_create_link(&tzdev->kobj,
667 &tz->device->dev.kobj, "device");
669 sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
674 static void acpi_thermal_zone_sysfs_remove(struct acpi_thermal *tz)
676 struct device *tzdev = thermal_zone_device(tz->thermal_zone);
678 sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
679 sysfs_remove_link(&tzdev->kobj, "device");
682 static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz)
684 struct acpi_thermal_trip *acpi_trip;
685 struct thermal_trip *trip;
686 int passive_delay = 0;
691 if (tz->trips.critical.valid)
694 if (tz->trips.hot.valid)
697 if (tz->trips.passive.trip.valid) {
699 passive_delay = tz->trips.passive.tsp * 100;
702 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE && tz->trips.active[i].trip.valid; i++)
705 trip = kcalloc(trip_count, sizeof(*trip), GFP_KERNEL);
709 tz->trip_table = trip;
711 if (tz->trips.critical.valid) {
712 trip->type = THERMAL_TRIP_CRITICAL;
713 trip->temperature = acpi_thermal_temp(tz, tz->trips.critical.temperature);
717 if (tz->trips.hot.valid) {
718 trip->type = THERMAL_TRIP_HOT;
719 trip->temperature = acpi_thermal_temp(tz, tz->trips.hot.temperature);
723 acpi_trip = &tz->trips.passive.trip;
724 if (acpi_trip->valid) {
725 trip->type = THERMAL_TRIP_PASSIVE;
726 trip->temperature = acpi_thermal_temp(tz, acpi_trip->temperature);
727 trip->priv = acpi_trip;
731 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
732 acpi_trip = &tz->trips.active[i].trip;
734 if (!acpi_trip->valid)
737 trip->type = THERMAL_TRIP_ACTIVE;
738 trip->temperature = acpi_thermal_temp(tz, acpi_trip->temperature);
739 trip->priv = acpi_trip;
743 tz->thermal_zone = thermal_zone_device_register_with_trips("acpitz",
747 &acpi_thermal_zone_ops,
750 tz->polling_frequency * 100);
751 if (IS_ERR(tz->thermal_zone)) {
752 result = PTR_ERR(tz->thermal_zone);
753 goto free_trip_table;
756 result = acpi_thermal_zone_sysfs_add(tz);
760 result = thermal_zone_device_enable(tz->thermal_zone);
764 dev_info(&tz->device->dev, "registered as thermal_zone%d\n",
765 thermal_zone_device_id(tz->thermal_zone));
770 acpi_thermal_zone_sysfs_remove(tz);
772 thermal_zone_device_unregister(tz->thermal_zone);
774 kfree(tz->trip_table);
779 static void acpi_thermal_unregister_thermal_zone(struct acpi_thermal *tz)
781 thermal_zone_device_disable(tz->thermal_zone);
782 acpi_thermal_zone_sysfs_remove(tz);
783 thermal_zone_device_unregister(tz->thermal_zone);
784 tz->thermal_zone = NULL;
788 /* --------------------------------------------------------------------------
790 -------------------------------------------------------------------------- */
792 static void acpi_thermal_notify(acpi_handle handle, u32 event, void *data)
794 struct acpi_device *device = data;
795 struct acpi_thermal *tz = acpi_driver_data(device);
801 case ACPI_THERMAL_NOTIFY_TEMPERATURE:
802 acpi_queue_thermal_check(tz);
804 case ACPI_THERMAL_NOTIFY_THRESHOLDS:
805 case ACPI_THERMAL_NOTIFY_DEVICES:
806 acpi_thermal_trips_update(tz, event);
809 acpi_handle_debug(device->handle, "Unsupported event [0x%x]\n",
816 * On some platforms, the AML code has dependency about
817 * the evaluating order of _TMP and _CRT/_HOT/_PSV/_ACx.
818 * 1. On HP Pavilion G4-1016tx, _TMP must be invoked after
819 * /_CRT/_HOT/_PSV/_ACx, or else system will be power off.
820 * 2. On HP Compaq 6715b/6715s, the return value of _PSV is 0
821 * if _TMP has never been evaluated.
823 * As this dependency is totally transparent to OS, evaluate
824 * all of them once, in the order of _CRT/_HOT/_PSV/_ACx,
825 * _TMP, before they are actually used.
827 static void acpi_thermal_aml_dependency_fix(struct acpi_thermal *tz)
829 acpi_handle handle = tz->device->handle;
830 unsigned long long value;
833 acpi_evaluate_integer(handle, "_CRT", NULL, &value);
834 acpi_evaluate_integer(handle, "_HOT", NULL, &value);
835 acpi_evaluate_integer(handle, "_PSV", NULL, &value);
836 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
837 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
840 status = acpi_evaluate_integer(handle, name, NULL, &value);
841 if (status == AE_NOT_FOUND)
844 acpi_evaluate_integer(handle, "_TMP", NULL, &value);
847 static int acpi_thermal_get_info(struct acpi_thermal *tz)
854 acpi_thermal_aml_dependency_fix(tz);
856 /* Get trip points [_CRT, _PSV, etc.] (required) */
857 result = acpi_thermal_get_trip_points(tz);
861 /* Get temperature [_TMP] (required) */
862 result = acpi_thermal_get_temperature(tz);
866 /* Set the cooling mode [_SCP] to active cooling (default) */
867 acpi_execute_simple_method(tz->device->handle, "_SCP",
868 ACPI_THERMAL_MODE_ACTIVE);
870 /* Get default polling frequency [_TZP] (optional) */
872 tz->polling_frequency = tzp;
874 acpi_thermal_get_polling_frequency(tz);
880 * The exact offset between Kelvin and degree Celsius is 273.15. However ACPI
881 * handles temperature values with a single decimal place. As a consequence,
882 * some implementations use an offset of 273.1 and others use an offset of
883 * 273.2. Try to find out which one is being used, to present the most
884 * accurate and visually appealing number.
886 * The heuristic below should work for all ACPI thermal zones which have a
887 * critical trip point with a value being a multiple of 0.5 degree Celsius.
889 static void acpi_thermal_guess_offset(struct acpi_thermal *tz)
891 if (tz->trips.critical.valid &&
892 (tz->trips.critical.temperature % 5) == 1)
893 tz->kelvin_offset = 273100;
895 tz->kelvin_offset = 273200;
898 static void acpi_thermal_check_fn(struct work_struct *work)
900 struct acpi_thermal *tz = container_of(work, struct acpi_thermal,
904 * In general, it is not sufficient to check the pending bit, because
905 * subsequent instances of this function may be queued after one of them
906 * has started running (e.g. if _TMP sleeps). Avoid bailing out if just
907 * one of them is running, though, because it may have done the actual
908 * check some time ago, so allow at least one of them to block on the
909 * mutex while another one is running the update.
911 if (!refcount_dec_not_one(&tz->thermal_check_count))
914 mutex_lock(&tz->thermal_check_lock);
916 thermal_zone_device_update(tz->thermal_zone, THERMAL_EVENT_UNSPECIFIED);
918 refcount_inc(&tz->thermal_check_count);
920 mutex_unlock(&tz->thermal_check_lock);
923 static int acpi_thermal_add(struct acpi_device *device)
925 struct acpi_thermal *tz;
931 tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
936 strcpy(tz->name, device->pnp.bus_id);
937 strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
938 strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
939 device->driver_data = tz;
941 result = acpi_thermal_get_info(tz);
945 acpi_thermal_guess_offset(tz);
947 result = acpi_thermal_register_thermal_zone(tz);
951 refcount_set(&tz->thermal_check_count, 3);
952 mutex_init(&tz->thermal_check_lock);
953 INIT_WORK(&tz->thermal_check_work, acpi_thermal_check_fn);
955 pr_info("%s [%s] (%ld C)\n", acpi_device_name(device),
956 acpi_device_bid(device), deci_kelvin_to_celsius(tz->temperature));
958 result = acpi_dev_install_notify_handler(device, ACPI_DEVICE_NOTIFY,
959 acpi_thermal_notify);
966 flush_workqueue(acpi_thermal_pm_queue);
967 acpi_thermal_unregister_thermal_zone(tz);
974 static void acpi_thermal_remove(struct acpi_device *device)
976 struct acpi_thermal *tz;
978 if (!device || !acpi_driver_data(device))
981 tz = acpi_driver_data(device);
983 acpi_dev_remove_notify_handler(device, ACPI_DEVICE_NOTIFY,
984 acpi_thermal_notify);
986 flush_workqueue(acpi_thermal_pm_queue);
987 acpi_thermal_unregister_thermal_zone(tz);
988 kfree(tz->trip_table);
992 #ifdef CONFIG_PM_SLEEP
993 static int acpi_thermal_suspend(struct device *dev)
995 /* Make sure the previously queued thermal check work has been done */
996 flush_workqueue(acpi_thermal_pm_queue);
1000 static int acpi_thermal_resume(struct device *dev)
1002 struct acpi_thermal *tz;
1003 int i, j, power_state;
1008 tz = acpi_driver_data(to_acpi_device(dev));
1012 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1013 if (!tz->trips.active[i].trip.valid)
1016 for (j = 0; j < tz->trips.active[i].devices.count; j++) {
1017 acpi_bus_update_power(tz->trips.active[i].devices.handles[j],
1022 acpi_queue_thermal_check(tz);
1027 #define acpi_thermal_suspend NULL
1028 #define acpi_thermal_resume NULL
1030 static SIMPLE_DEV_PM_OPS(acpi_thermal_pm, acpi_thermal_suspend, acpi_thermal_resume);
1032 static const struct acpi_device_id thermal_device_ids[] = {
1033 {ACPI_THERMAL_HID, 0},
1036 MODULE_DEVICE_TABLE(acpi, thermal_device_ids);
1038 static struct acpi_driver acpi_thermal_driver = {
1040 .class = ACPI_THERMAL_CLASS,
1041 .ids = thermal_device_ids,
1043 .add = acpi_thermal_add,
1044 .remove = acpi_thermal_remove,
1046 .drv.pm = &acpi_thermal_pm,
1049 static int thermal_act(const struct dmi_system_id *d) {
1051 pr_notice("%s detected: disabling all active thermal trip points\n",
1057 static int thermal_nocrt(const struct dmi_system_id *d) {
1058 pr_notice("%s detected: disabling all critical thermal trip point actions.\n",
1063 static int thermal_tzp(const struct dmi_system_id *d) {
1065 pr_notice("%s detected: enabling thermal zone polling\n",
1067 tzp = 300; /* 300 dS = 30 Seconds */
1071 static int thermal_psv(const struct dmi_system_id *d) {
1073 pr_notice("%s detected: disabling all passive thermal trip points\n",
1080 static const struct dmi_system_id thermal_dmi_table[] __initconst = {
1082 * Award BIOS on this AOpen makes thermal control almost worthless.
1083 * http://bugzilla.kernel.org/show_bug.cgi?id=8842
1086 .callback = thermal_act,
1087 .ident = "AOpen i915GMm-HFS",
1089 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1090 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1094 .callback = thermal_psv,
1095 .ident = "AOpen i915GMm-HFS",
1097 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1098 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1102 .callback = thermal_tzp,
1103 .ident = "AOpen i915GMm-HFS",
1105 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1106 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1110 .callback = thermal_nocrt,
1111 .ident = "Gigabyte GA-7ZX",
1113 DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
1114 DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
1120 static int __init acpi_thermal_init(void)
1124 dmi_check_system(thermal_dmi_table);
1127 pr_notice("thermal control disabled\n");
1131 acpi_thermal_pm_queue = alloc_workqueue("acpi_thermal_pm",
1132 WQ_HIGHPRI | WQ_MEM_RECLAIM, 0);
1133 if (!acpi_thermal_pm_queue)
1136 result = acpi_bus_register_driver(&acpi_thermal_driver);
1138 destroy_workqueue(acpi_thermal_pm_queue);
1145 static void __exit acpi_thermal_exit(void)
1147 acpi_bus_unregister_driver(&acpi_thermal_driver);
1148 destroy_workqueue(acpi_thermal_pm_queue);
1151 module_init(acpi_thermal_init);
1152 module_exit(acpi_thermal_exit);
1154 MODULE_AUTHOR("Paul Diefenbaugh");
1155 MODULE_DESCRIPTION("ACPI Thermal Zone Driver");
1156 MODULE_LICENSE("GPL");