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 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/dmi.h>
19 #include <linux/init.h>
20 #include <linux/slab.h>
21 #include <linux/types.h>
22 #include <linux/jiffies.h>
23 #include <linux/kmod.h>
24 #include <linux/reboot.h>
25 #include <linux/device.h>
26 #include <linux/thermal.h>
27 #include <linux/acpi.h>
28 #include <linux/workqueue.h>
29 #include <linux/uaccess.h>
30 #include <linux/units.h>
32 #define PREFIX "ACPI: "
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 _COMPONENT ACPI_THERMAL_COMPONENT
47 ACPI_MODULE_NAME("thermal");
49 MODULE_AUTHOR("Paul Diefenbaugh");
50 MODULE_DESCRIPTION("ACPI Thermal Zone Driver");
51 MODULE_LICENSE("GPL");
54 module_param(act, int, 0644);
55 MODULE_PARM_DESC(act, "Disable or override all lowest active trip points.");
58 module_param(crt, int, 0644);
59 MODULE_PARM_DESC(crt, "Disable or lower all critical trip points.");
62 module_param(tzp, int, 0444);
63 MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.");
66 module_param(nocrt, int, 0);
67 MODULE_PARM_DESC(nocrt, "Set to take no action upon ACPI thermal zone critical trips points.");
70 module_param(off, int, 0);
71 MODULE_PARM_DESC(off, "Set to disable ACPI thermal support.");
74 module_param(psv, int, 0644);
75 MODULE_PARM_DESC(psv, "Disable or override all passive trip points.");
77 static struct workqueue_struct *acpi_thermal_pm_queue;
79 static int acpi_thermal_add(struct acpi_device *device);
80 static int acpi_thermal_remove(struct acpi_device *device);
81 static void acpi_thermal_notify(struct acpi_device *device, u32 event);
83 static const struct acpi_device_id thermal_device_ids[] = {
84 {ACPI_THERMAL_HID, 0},
87 MODULE_DEVICE_TABLE(acpi, thermal_device_ids);
89 #ifdef CONFIG_PM_SLEEP
90 static int acpi_thermal_suspend(struct device *dev);
91 static int acpi_thermal_resume(struct device *dev);
93 #define acpi_thermal_suspend NULL
94 #define acpi_thermal_resume NULL
96 static SIMPLE_DEV_PM_OPS(acpi_thermal_pm, acpi_thermal_suspend, acpi_thermal_resume);
98 static struct acpi_driver acpi_thermal_driver = {
100 .class = ACPI_THERMAL_CLASS,
101 .ids = thermal_device_ids,
103 .add = acpi_thermal_add,
104 .remove = acpi_thermal_remove,
105 .notify = acpi_thermal_notify,
107 .drv.pm = &acpi_thermal_pm,
110 struct acpi_thermal_state {
119 struct acpi_thermal_state_flags {
125 struct acpi_thermal_critical {
126 struct acpi_thermal_state_flags flags;
127 unsigned long temperature;
130 struct acpi_thermal_hot {
131 struct acpi_thermal_state_flags flags;
132 unsigned long temperature;
135 struct acpi_thermal_passive {
136 struct acpi_thermal_state_flags flags;
137 unsigned long temperature;
141 struct acpi_handle_list devices;
144 struct acpi_thermal_active {
145 struct acpi_thermal_state_flags flags;
146 unsigned long temperature;
147 struct acpi_handle_list devices;
150 struct acpi_thermal_trips {
151 struct acpi_thermal_critical critical;
152 struct acpi_thermal_hot hot;
153 struct acpi_thermal_passive passive;
154 struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
157 struct acpi_thermal_flags {
158 u8 cooling_mode:1; /* _SCP */
159 u8 devices:1; /* _TZD */
163 struct acpi_thermal {
164 struct acpi_device * device;
166 unsigned long temperature;
167 unsigned long last_temperature;
168 unsigned long polling_frequency;
170 struct acpi_thermal_flags flags;
171 struct acpi_thermal_state state;
172 struct acpi_thermal_trips trips;
173 struct acpi_handle_list devices;
174 struct thermal_zone_device *thermal_zone;
175 int kelvin_offset; /* in millidegrees */
176 struct work_struct thermal_check_work;
177 struct mutex thermal_check_lock;
178 refcount_t thermal_check_count;
181 /* --------------------------------------------------------------------------
182 Thermal Zone Management
183 -------------------------------------------------------------------------- */
185 static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
187 acpi_status status = AE_OK;
188 unsigned long long tmp;
193 tz->last_temperature = tz->temperature;
195 status = acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tmp);
196 if (ACPI_FAILURE(status))
199 tz->temperature = tmp;
200 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
206 static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
208 acpi_status status = AE_OK;
209 unsigned long long tmp;
214 status = acpi_evaluate_integer(tz->device->handle, "_TZP", NULL, &tmp);
215 if (ACPI_FAILURE(status))
218 tz->polling_frequency = tmp;
219 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
220 tz->polling_frequency));
225 static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
230 if (ACPI_FAILURE(acpi_execute_simple_method(tz->device->handle,
237 #define ACPI_TRIPS_CRITICAL 0x01
238 #define ACPI_TRIPS_HOT 0x02
239 #define ACPI_TRIPS_PASSIVE 0x04
240 #define ACPI_TRIPS_ACTIVE 0x08
241 #define ACPI_TRIPS_DEVICES 0x10
243 #define ACPI_TRIPS_REFRESH_THRESHOLDS (ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE)
244 #define ACPI_TRIPS_REFRESH_DEVICES ACPI_TRIPS_DEVICES
246 #define ACPI_TRIPS_INIT (ACPI_TRIPS_CRITICAL | ACPI_TRIPS_HOT | \
247 ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE | \
251 * This exception is thrown out in two cases:
252 * 1.An invalid trip point becomes invalid or a valid trip point becomes invalid
253 * when re-evaluating the AML code.
254 * 2.TODO: Devices listed in _PSL, _ALx, _TZD may change.
255 * We need to re-bind the cooling devices of a thermal zone when this occurs.
257 #define ACPI_THERMAL_TRIPS_EXCEPTION(flags, str) \
259 if (flags != ACPI_TRIPS_INIT) \
260 ACPI_EXCEPTION((AE_INFO, AE_ERROR, \
261 "ACPI thermal trip point %s changed\n" \
265 static int acpi_thermal_trips_update(struct acpi_thermal *tz, int flag)
267 acpi_status status = AE_OK;
268 unsigned long long tmp;
269 struct acpi_handle_list devices;
273 /* Critical Shutdown */
274 if (flag & ACPI_TRIPS_CRITICAL) {
275 status = acpi_evaluate_integer(tz->device->handle,
277 tz->trips.critical.temperature = tmp;
279 * Treat freezing temperatures as invalid as well; some
280 * BIOSes return really low values and cause reboots at startup.
281 * Below zero (Celsius) values clearly aren't right for sure..
282 * ... so lets discard those as invalid.
284 if (ACPI_FAILURE(status)) {
285 tz->trips.critical.flags.valid = 0;
286 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
287 "No critical threshold\n"));
288 } else if (tmp <= 2732) {
289 pr_warn(FW_BUG "Invalid critical threshold (%llu)\n",
291 tz->trips.critical.flags.valid = 0;
293 tz->trips.critical.flags.valid = 1;
294 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
295 "Found critical threshold [%lu]\n",
296 tz->trips.critical.temperature));
298 if (tz->trips.critical.flags.valid == 1) {
300 tz->trips.critical.flags.valid = 0;
301 } else if (crt > 0) {
302 unsigned long crt_k = celsius_to_deci_kelvin(crt);
305 * Allow override critical threshold
307 if (crt_k > tz->trips.critical.temperature)
308 pr_warn(PREFIX "Critical threshold %d C\n",
310 tz->trips.critical.temperature = crt_k;
315 /* Critical Sleep (optional) */
316 if (flag & ACPI_TRIPS_HOT) {
317 status = acpi_evaluate_integer(tz->device->handle,
319 if (ACPI_FAILURE(status)) {
320 tz->trips.hot.flags.valid = 0;
321 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
322 "No hot threshold\n"));
324 tz->trips.hot.temperature = tmp;
325 tz->trips.hot.flags.valid = 1;
326 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
327 "Found hot threshold [%lu]\n",
328 tz->trips.hot.temperature));
332 /* Passive (optional) */
333 if (((flag & ACPI_TRIPS_PASSIVE) && tz->trips.passive.flags.valid) ||
334 (flag == ACPI_TRIPS_INIT)) {
335 valid = tz->trips.passive.flags.valid;
338 } else if (psv > 0) {
339 tmp = celsius_to_deci_kelvin(psv);
342 status = acpi_evaluate_integer(tz->device->handle,
346 if (ACPI_FAILURE(status))
347 tz->trips.passive.flags.valid = 0;
349 tz->trips.passive.temperature = tmp;
350 tz->trips.passive.flags.valid = 1;
351 if (flag == ACPI_TRIPS_INIT) {
352 status = acpi_evaluate_integer(
353 tz->device->handle, "_TC1",
355 if (ACPI_FAILURE(status))
356 tz->trips.passive.flags.valid = 0;
358 tz->trips.passive.tc1 = tmp;
359 status = acpi_evaluate_integer(
360 tz->device->handle, "_TC2",
362 if (ACPI_FAILURE(status))
363 tz->trips.passive.flags.valid = 0;
365 tz->trips.passive.tc2 = tmp;
366 status = acpi_evaluate_integer(
367 tz->device->handle, "_TSP",
369 if (ACPI_FAILURE(status))
370 tz->trips.passive.flags.valid = 0;
372 tz->trips.passive.tsp = tmp;
376 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.passive.flags.valid) {
377 memset(&devices, 0, sizeof(struct acpi_handle_list));
378 status = acpi_evaluate_reference(tz->device->handle, "_PSL",
380 if (ACPI_FAILURE(status)) {
381 pr_warn(PREFIX "Invalid passive threshold\n");
382 tz->trips.passive.flags.valid = 0;
385 tz->trips.passive.flags.valid = 1;
387 if (memcmp(&tz->trips.passive.devices, &devices,
388 sizeof(struct acpi_handle_list))) {
389 memcpy(&tz->trips.passive.devices, &devices,
390 sizeof(struct acpi_handle_list));
391 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
394 if ((flag & ACPI_TRIPS_PASSIVE) || (flag & ACPI_TRIPS_DEVICES)) {
395 if (valid != tz->trips.passive.flags.valid)
396 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
399 /* Active (optional) */
400 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
401 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
402 valid = tz->trips.active[i].flags.valid;
405 break; /* disable all active trip points */
407 if ((flag == ACPI_TRIPS_INIT) || ((flag & ACPI_TRIPS_ACTIVE) &&
408 tz->trips.active[i].flags.valid)) {
409 status = acpi_evaluate_integer(tz->device->handle,
411 if (ACPI_FAILURE(status)) {
412 tz->trips.active[i].flags.valid = 0;
418 tz->trips.active[0].temperature =
419 celsius_to_deci_kelvin(act);
422 * Don't allow override higher than
423 * the next higher trip point
425 tz->trips.active[i - 1].temperature =
426 (tz->trips.active[i - 2].temperature <
427 celsius_to_deci_kelvin(act) ?
428 tz->trips.active[i - 2].temperature :
429 celsius_to_deci_kelvin(act));
432 tz->trips.active[i].temperature = tmp;
433 tz->trips.active[i].flags.valid = 1;
438 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.active[i].flags.valid ) {
439 memset(&devices, 0, sizeof(struct acpi_handle_list));
440 status = acpi_evaluate_reference(tz->device->handle,
441 name, NULL, &devices);
442 if (ACPI_FAILURE(status)) {
443 pr_warn(PREFIX "Invalid active%d threshold\n",
445 tz->trips.active[i].flags.valid = 0;
448 tz->trips.active[i].flags.valid = 1;
450 if (memcmp(&tz->trips.active[i].devices, &devices,
451 sizeof(struct acpi_handle_list))) {
452 memcpy(&tz->trips.active[i].devices, &devices,
453 sizeof(struct acpi_handle_list));
454 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
457 if ((flag & ACPI_TRIPS_ACTIVE) || (flag & ACPI_TRIPS_DEVICES))
458 if (valid != tz->trips.active[i].flags.valid)
459 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
461 if (!tz->trips.active[i].flags.valid)
465 if (flag & ACPI_TRIPS_DEVICES) {
466 memset(&devices, 0, sizeof(devices));
467 status = acpi_evaluate_reference(tz->device->handle, "_TZD",
469 if (ACPI_SUCCESS(status)
470 && memcmp(&tz->devices, &devices, sizeof(devices))) {
471 tz->devices = devices;
472 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
479 static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
481 int i, valid, ret = acpi_thermal_trips_update(tz, ACPI_TRIPS_INIT);
486 valid = tz->trips.critical.flags.valid |
487 tz->trips.hot.flags.valid |
488 tz->trips.passive.flags.valid;
490 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
491 valid |= tz->trips.active[i].flags.valid;
494 pr_warn(FW_BUG "No valid trip found\n");
500 /* sys I/F for generic thermal sysfs support */
502 static int thermal_get_temp(struct thermal_zone_device *thermal, int *temp)
504 struct acpi_thermal *tz = thermal->devdata;
510 result = acpi_thermal_get_temperature(tz);
514 *temp = deci_kelvin_to_millicelsius_with_offset(tz->temperature,
519 static int thermal_get_trip_type(struct thermal_zone_device *thermal,
520 int trip, enum thermal_trip_type *type)
522 struct acpi_thermal *tz = thermal->devdata;
528 if (tz->trips.critical.flags.valid) {
530 *type = THERMAL_TRIP_CRITICAL;
536 if (tz->trips.hot.flags.valid) {
538 *type = THERMAL_TRIP_HOT;
544 if (tz->trips.passive.flags.valid) {
546 *type = THERMAL_TRIP_PASSIVE;
552 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
553 tz->trips.active[i].flags.valid; i++) {
555 *type = THERMAL_TRIP_ACTIVE;
564 static int thermal_get_trip_temp(struct thermal_zone_device *thermal,
567 struct acpi_thermal *tz = thermal->devdata;
573 if (tz->trips.critical.flags.valid) {
575 *temp = deci_kelvin_to_millicelsius_with_offset(
576 tz->trips.critical.temperature,
583 if (tz->trips.hot.flags.valid) {
585 *temp = deci_kelvin_to_millicelsius_with_offset(
586 tz->trips.hot.temperature,
593 if (tz->trips.passive.flags.valid) {
595 *temp = deci_kelvin_to_millicelsius_with_offset(
596 tz->trips.passive.temperature,
603 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
604 tz->trips.active[i].flags.valid; i++) {
606 *temp = deci_kelvin_to_millicelsius_with_offset(
607 tz->trips.active[i].temperature,
617 static int thermal_get_crit_temp(struct thermal_zone_device *thermal,
620 struct acpi_thermal *tz = thermal->devdata;
622 if (tz->trips.critical.flags.valid) {
623 *temperature = deci_kelvin_to_millicelsius_with_offset(
624 tz->trips.critical.temperature,
631 static int thermal_get_trend(struct thermal_zone_device *thermal,
632 int trip, enum thermal_trend *trend)
634 struct acpi_thermal *tz = thermal->devdata;
635 enum thermal_trip_type type;
638 if (thermal_get_trip_type(thermal, trip, &type))
641 if (type == THERMAL_TRIP_ACTIVE) {
643 int temp = deci_kelvin_to_millicelsius_with_offset(
644 tz->temperature, tz->kelvin_offset);
645 if (thermal_get_trip_temp(thermal, trip, &trip_temp))
648 if (temp > trip_temp) {
649 *trend = THERMAL_TREND_RAISING;
652 /* Fall back on default trend */
658 * tz->temperature has already been updated by generic thermal layer,
659 * before this callback being invoked
661 i = (tz->trips.passive.tc1 * (tz->temperature - tz->last_temperature))
662 + (tz->trips.passive.tc2
663 * (tz->temperature - tz->trips.passive.temperature));
666 *trend = THERMAL_TREND_RAISING;
668 *trend = THERMAL_TREND_DROPPING;
670 *trend = THERMAL_TREND_STABLE;
675 static int thermal_notify(struct thermal_zone_device *thermal, int trip,
676 enum thermal_trip_type trip_type)
679 struct acpi_thermal *tz = thermal->devdata;
681 if (trip_type == THERMAL_TRIP_CRITICAL)
682 type = ACPI_THERMAL_NOTIFY_CRITICAL;
683 else if (trip_type == THERMAL_TRIP_HOT)
684 type = ACPI_THERMAL_NOTIFY_HOT;
688 acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
689 dev_name(&tz->device->dev), type, 1);
691 if (trip_type == THERMAL_TRIP_CRITICAL && nocrt)
697 static int acpi_thermal_cooling_device_cb(struct thermal_zone_device *thermal,
698 struct thermal_cooling_device *cdev,
701 struct acpi_device *device = cdev->devdata;
702 struct acpi_thermal *tz = thermal->devdata;
703 struct acpi_device *dev;
711 if (tz->trips.critical.flags.valid)
714 if (tz->trips.hot.flags.valid)
717 if (tz->trips.passive.flags.valid) {
719 for (i = 0; i < tz->trips.passive.devices.count;
721 handle = tz->trips.passive.devices.handles[i];
722 status = acpi_bus_get_device(handle, &dev);
723 if (ACPI_FAILURE(status) || dev != device)
727 thermal_zone_bind_cooling_device
728 (thermal, trip, cdev,
729 THERMAL_NO_LIMIT, THERMAL_NO_LIMIT,
730 THERMAL_WEIGHT_DEFAULT);
733 thermal_zone_unbind_cooling_device
734 (thermal, trip, cdev);
740 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
741 if (!tz->trips.active[i].flags.valid)
745 j < tz->trips.active[i].devices.count;
747 handle = tz->trips.active[i].devices.handles[j];
748 status = acpi_bus_get_device(handle, &dev);
749 if (ACPI_FAILURE(status) || dev != device)
752 result = thermal_zone_bind_cooling_device
753 (thermal, trip, cdev,
754 THERMAL_NO_LIMIT, THERMAL_NO_LIMIT,
755 THERMAL_WEIGHT_DEFAULT);
757 result = thermal_zone_unbind_cooling_device
758 (thermal, trip, cdev);
764 for (i = 0; i < tz->devices.count; i++) {
765 handle = tz->devices.handles[i];
766 status = acpi_bus_get_device(handle, &dev);
767 if (ACPI_SUCCESS(status) && (dev == device)) {
769 result = thermal_zone_bind_cooling_device
770 (thermal, THERMAL_TRIPS_NONE,
771 cdev, THERMAL_NO_LIMIT,
773 THERMAL_WEIGHT_DEFAULT);
775 result = thermal_zone_unbind_cooling_device
776 (thermal, THERMAL_TRIPS_NONE,
788 acpi_thermal_bind_cooling_device(struct thermal_zone_device *thermal,
789 struct thermal_cooling_device *cdev)
791 return acpi_thermal_cooling_device_cb(thermal, cdev, true);
795 acpi_thermal_unbind_cooling_device(struct thermal_zone_device *thermal,
796 struct thermal_cooling_device *cdev)
798 return acpi_thermal_cooling_device_cb(thermal, cdev, false);
801 static struct thermal_zone_device_ops acpi_thermal_zone_ops = {
802 .bind = acpi_thermal_bind_cooling_device,
803 .unbind = acpi_thermal_unbind_cooling_device,
804 .get_temp = thermal_get_temp,
805 .get_trip_type = thermal_get_trip_type,
806 .get_trip_temp = thermal_get_trip_temp,
807 .get_crit_temp = thermal_get_crit_temp,
808 .get_trend = thermal_get_trend,
809 .notify = thermal_notify,
812 static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz)
819 if (tz->trips.critical.flags.valid)
822 if (tz->trips.hot.flags.valid)
825 if (tz->trips.passive.flags.valid)
828 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
829 tz->trips.active[i].flags.valid; i++, trips++);
831 if (tz->trips.passive.flags.valid)
833 thermal_zone_device_register("acpitz", trips, 0, tz,
834 &acpi_thermal_zone_ops, NULL,
835 tz->trips.passive.tsp*100,
836 tz->polling_frequency*100);
839 thermal_zone_device_register("acpitz", trips, 0, tz,
840 &acpi_thermal_zone_ops, NULL,
841 0, tz->polling_frequency*100);
842 if (IS_ERR(tz->thermal_zone))
845 result = sysfs_create_link(&tz->device->dev.kobj,
846 &tz->thermal_zone->device.kobj, "thermal_zone");
850 result = sysfs_create_link(&tz->thermal_zone->device.kobj,
851 &tz->device->dev.kobj, "device");
855 status = acpi_bus_attach_private_data(tz->device->handle,
857 if (ACPI_FAILURE(status)) {
859 goto remove_dev_link;
862 result = thermal_zone_device_enable(tz->thermal_zone);
864 goto acpi_bus_detach;
866 dev_info(&tz->device->dev, "registered as thermal_zone%d\n",
867 tz->thermal_zone->id);
872 acpi_bus_detach_private_data(tz->device->handle);
874 sysfs_remove_link(&tz->thermal_zone->device.kobj, "device");
876 sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
878 thermal_zone_device_unregister(tz->thermal_zone);
883 static void acpi_thermal_unregister_thermal_zone(struct acpi_thermal *tz)
885 sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
886 sysfs_remove_link(&tz->thermal_zone->device.kobj, "device");
887 thermal_zone_device_unregister(tz->thermal_zone);
888 tz->thermal_zone = NULL;
889 acpi_bus_detach_private_data(tz->device->handle);
893 /* --------------------------------------------------------------------------
895 -------------------------------------------------------------------------- */
897 static void acpi_queue_thermal_check(struct acpi_thermal *tz)
899 if (!work_pending(&tz->thermal_check_work))
900 queue_work(acpi_thermal_pm_queue, &tz->thermal_check_work);
903 static void acpi_thermal_notify(struct acpi_device *device, u32 event)
905 struct acpi_thermal *tz = acpi_driver_data(device);
912 case ACPI_THERMAL_NOTIFY_TEMPERATURE:
913 acpi_queue_thermal_check(tz);
915 case ACPI_THERMAL_NOTIFY_THRESHOLDS:
916 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_THRESHOLDS);
917 acpi_queue_thermal_check(tz);
918 acpi_bus_generate_netlink_event(device->pnp.device_class,
919 dev_name(&device->dev), event, 0);
921 case ACPI_THERMAL_NOTIFY_DEVICES:
922 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_DEVICES);
923 acpi_queue_thermal_check(tz);
924 acpi_bus_generate_netlink_event(device->pnp.device_class,
925 dev_name(&device->dev), event, 0);
928 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
929 "Unsupported event [0x%x]\n", event));
935 * On some platforms, the AML code has dependency about
936 * the evaluating order of _TMP and _CRT/_HOT/_PSV/_ACx.
937 * 1. On HP Pavilion G4-1016tx, _TMP must be invoked after
938 * /_CRT/_HOT/_PSV/_ACx, or else system will be power off.
939 * 2. On HP Compaq 6715b/6715s, the return value of _PSV is 0
940 * if _TMP has never been evaluated.
942 * As this dependency is totally transparent to OS, evaluate
943 * all of them once, in the order of _CRT/_HOT/_PSV/_ACx,
944 * _TMP, before they are actually used.
946 static void acpi_thermal_aml_dependency_fix(struct acpi_thermal *tz)
948 acpi_handle handle = tz->device->handle;
949 unsigned long long value;
952 acpi_evaluate_integer(handle, "_CRT", NULL, &value);
953 acpi_evaluate_integer(handle, "_HOT", NULL, &value);
954 acpi_evaluate_integer(handle, "_PSV", NULL, &value);
955 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
956 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
959 status = acpi_evaluate_integer(handle, name, NULL, &value);
960 if (status == AE_NOT_FOUND)
963 acpi_evaluate_integer(handle, "_TMP", NULL, &value);
966 static int acpi_thermal_get_info(struct acpi_thermal *tz)
974 acpi_thermal_aml_dependency_fix(tz);
976 /* Get trip points [_CRT, _PSV, etc.] (required) */
977 result = acpi_thermal_get_trip_points(tz);
981 /* Get temperature [_TMP] (required) */
982 result = acpi_thermal_get_temperature(tz);
986 /* Set the cooling mode [_SCP] to active cooling (default) */
987 result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
989 tz->flags.cooling_mode = 1;
991 /* Get default polling frequency [_TZP] (optional) */
993 tz->polling_frequency = tzp;
995 acpi_thermal_get_polling_frequency(tz);
1001 * The exact offset between Kelvin and degree Celsius is 273.15. However ACPI
1002 * handles temperature values with a single decimal place. As a consequence,
1003 * some implementations use an offset of 273.1 and others use an offset of
1004 * 273.2. Try to find out which one is being used, to present the most
1005 * accurate and visually appealing number.
1007 * The heuristic below should work for all ACPI thermal zones which have a
1008 * critical trip point with a value being a multiple of 0.5 degree Celsius.
1010 static void acpi_thermal_guess_offset(struct acpi_thermal *tz)
1012 if (tz->trips.critical.flags.valid &&
1013 (tz->trips.critical.temperature % 5) == 1)
1014 tz->kelvin_offset = 273100;
1016 tz->kelvin_offset = 273200;
1019 static void acpi_thermal_check_fn(struct work_struct *work)
1021 struct acpi_thermal *tz = container_of(work, struct acpi_thermal,
1022 thermal_check_work);
1025 * In general, it is not sufficient to check the pending bit, because
1026 * subsequent instances of this function may be queued after one of them
1027 * has started running (e.g. if _TMP sleeps). Avoid bailing out if just
1028 * one of them is running, though, because it may have done the actual
1029 * check some time ago, so allow at least one of them to block on the
1030 * mutex while another one is running the update.
1032 if (!refcount_dec_not_one(&tz->thermal_check_count))
1035 mutex_lock(&tz->thermal_check_lock);
1037 thermal_zone_device_update(tz->thermal_zone, THERMAL_EVENT_UNSPECIFIED);
1039 refcount_inc(&tz->thermal_check_count);
1041 mutex_unlock(&tz->thermal_check_lock);
1044 static int acpi_thermal_add(struct acpi_device *device)
1047 struct acpi_thermal *tz = NULL;
1053 tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
1057 tz->device = device;
1058 strcpy(tz->name, device->pnp.bus_id);
1059 strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1060 strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
1061 device->driver_data = tz;
1063 result = acpi_thermal_get_info(tz);
1067 acpi_thermal_guess_offset(tz);
1069 result = acpi_thermal_register_thermal_zone(tz);
1073 refcount_set(&tz->thermal_check_count, 3);
1074 mutex_init(&tz->thermal_check_lock);
1075 INIT_WORK(&tz->thermal_check_work, acpi_thermal_check_fn);
1077 pr_info(PREFIX "%s [%s] (%ld C)\n", acpi_device_name(device),
1078 acpi_device_bid(device), deci_kelvin_to_celsius(tz->temperature));
1087 static int acpi_thermal_remove(struct acpi_device *device)
1089 struct acpi_thermal *tz = NULL;
1091 if (!device || !acpi_driver_data(device))
1094 flush_workqueue(acpi_thermal_pm_queue);
1095 tz = acpi_driver_data(device);
1097 acpi_thermal_unregister_thermal_zone(tz);
1102 #ifdef CONFIG_PM_SLEEP
1103 static int acpi_thermal_suspend(struct device *dev)
1105 /* Make sure the previously queued thermal check work has been done */
1106 flush_workqueue(acpi_thermal_pm_queue);
1110 static int acpi_thermal_resume(struct device *dev)
1112 struct acpi_thermal *tz;
1113 int i, j, power_state, result;
1118 tz = acpi_driver_data(to_acpi_device(dev));
1122 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1123 if (!(&tz->trips.active[i]))
1125 if (!tz->trips.active[i].flags.valid)
1127 tz->trips.active[i].flags.enabled = 1;
1128 for (j = 0; j < tz->trips.active[i].devices.count; j++) {
1129 result = acpi_bus_update_power(
1130 tz->trips.active[i].devices.handles[j],
1132 if (result || (power_state != ACPI_STATE_D0)) {
1133 tz->trips.active[i].flags.enabled = 0;
1137 tz->state.active |= tz->trips.active[i].flags.enabled;
1140 acpi_queue_thermal_check(tz);
1146 static int thermal_act(const struct dmi_system_id *d) {
1149 pr_notice(PREFIX "%s detected: "
1150 "disabling all active thermal trip points\n", d->ident);
1155 static int thermal_nocrt(const struct dmi_system_id *d) {
1157 pr_notice(PREFIX "%s detected: "
1158 "disabling all critical thermal trip point actions.\n", d->ident);
1162 static int thermal_tzp(const struct dmi_system_id *d) {
1165 pr_notice(PREFIX "%s detected: "
1166 "enabling thermal zone polling\n", d->ident);
1167 tzp = 300; /* 300 dS = 30 Seconds */
1171 static int thermal_psv(const struct dmi_system_id *d) {
1174 pr_notice(PREFIX "%s detected: "
1175 "disabling all passive thermal trip points\n", d->ident);
1181 static const struct dmi_system_id thermal_dmi_table[] __initconst = {
1183 * Award BIOS on this AOpen makes thermal control almost worthless.
1184 * http://bugzilla.kernel.org/show_bug.cgi?id=8842
1187 .callback = thermal_act,
1188 .ident = "AOpen i915GMm-HFS",
1190 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1191 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1195 .callback = thermal_psv,
1196 .ident = "AOpen i915GMm-HFS",
1198 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1199 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1203 .callback = thermal_tzp,
1204 .ident = "AOpen i915GMm-HFS",
1206 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1207 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1211 .callback = thermal_nocrt,
1212 .ident = "Gigabyte GA-7ZX",
1214 DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
1215 DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
1221 static int __init acpi_thermal_init(void)
1225 dmi_check_system(thermal_dmi_table);
1228 pr_notice(PREFIX "thermal control disabled\n");
1232 acpi_thermal_pm_queue = alloc_workqueue("acpi_thermal_pm",
1233 WQ_HIGHPRI | WQ_MEM_RECLAIM, 0);
1234 if (!acpi_thermal_pm_queue)
1237 result = acpi_bus_register_driver(&acpi_thermal_driver);
1239 destroy_workqueue(acpi_thermal_pm_queue);
1246 static void __exit acpi_thermal_exit(void)
1248 acpi_bus_unregister_driver(&acpi_thermal_driver);
1249 destroy_workqueue(acpi_thermal_pm_queue);
1254 module_init(acpi_thermal_init);
1255 module_exit(acpi_thermal_exit);