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 #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>
31 #define PREFIX "ACPI: "
33 #define ACPI_THERMAL_CLASS "thermal_zone"
34 #define ACPI_THERMAL_DEVICE_NAME "Thermal Zone"
35 #define ACPI_THERMAL_NOTIFY_TEMPERATURE 0x80
36 #define ACPI_THERMAL_NOTIFY_THRESHOLDS 0x81
37 #define ACPI_THERMAL_NOTIFY_DEVICES 0x82
38 #define ACPI_THERMAL_NOTIFY_CRITICAL 0xF0
39 #define ACPI_THERMAL_NOTIFY_HOT 0xF1
40 #define ACPI_THERMAL_MODE_ACTIVE 0x00
42 #define ACPI_THERMAL_MAX_ACTIVE 10
43 #define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65
45 #define _COMPONENT ACPI_THERMAL_COMPONENT
46 ACPI_MODULE_NAME("thermal");
48 MODULE_AUTHOR("Paul Diefenbaugh");
49 MODULE_DESCRIPTION("ACPI Thermal Zone Driver");
50 MODULE_LICENSE("GPL");
53 module_param(act, int, 0644);
54 MODULE_PARM_DESC(act, "Disable or override all lowest active trip points.");
57 module_param(crt, int, 0644);
58 MODULE_PARM_DESC(crt, "Disable or lower all critical trip points.");
61 module_param(tzp, int, 0444);
62 MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.");
65 module_param(nocrt, int, 0);
66 MODULE_PARM_DESC(nocrt, "Set to take no action upon ACPI thermal zone critical trips points.");
69 module_param(off, int, 0);
70 MODULE_PARM_DESC(off, "Set to disable ACPI thermal support.");
73 module_param(psv, int, 0644);
74 MODULE_PARM_DESC(psv, "Disable or override all passive trip points.");
76 static struct workqueue_struct *acpi_thermal_pm_queue;
78 static int acpi_thermal_add(struct acpi_device *device);
79 static int acpi_thermal_remove(struct acpi_device *device);
80 static void acpi_thermal_notify(struct acpi_device *device, u32 event);
82 static const struct acpi_device_id thermal_device_ids[] = {
83 {ACPI_THERMAL_HID, 0},
86 MODULE_DEVICE_TABLE(acpi, thermal_device_ids);
88 #ifdef CONFIG_PM_SLEEP
89 static int acpi_thermal_suspend(struct device *dev);
90 static int acpi_thermal_resume(struct device *dev);
92 #define acpi_thermal_suspend NULL
93 #define acpi_thermal_resume NULL
95 static SIMPLE_DEV_PM_OPS(acpi_thermal_pm, acpi_thermal_suspend, acpi_thermal_resume);
97 static struct acpi_driver acpi_thermal_driver = {
99 .class = ACPI_THERMAL_CLASS,
100 .ids = thermal_device_ids,
102 .add = acpi_thermal_add,
103 .remove = acpi_thermal_remove,
104 .notify = acpi_thermal_notify,
106 .drv.pm = &acpi_thermal_pm,
109 struct acpi_thermal_state {
118 struct acpi_thermal_state_flags {
124 struct acpi_thermal_critical {
125 struct acpi_thermal_state_flags flags;
126 unsigned long temperature;
129 struct acpi_thermal_hot {
130 struct acpi_thermal_state_flags flags;
131 unsigned long temperature;
134 struct acpi_thermal_passive {
135 struct acpi_thermal_state_flags flags;
136 unsigned long temperature;
140 struct acpi_handle_list devices;
143 struct acpi_thermal_active {
144 struct acpi_thermal_state_flags flags;
145 unsigned long temperature;
146 struct acpi_handle_list devices;
149 struct acpi_thermal_trips {
150 struct acpi_thermal_critical critical;
151 struct acpi_thermal_hot hot;
152 struct acpi_thermal_passive passive;
153 struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
156 struct acpi_thermal_flags {
157 u8 cooling_mode:1; /* _SCP */
158 u8 devices:1; /* _TZD */
162 struct acpi_thermal {
163 struct acpi_device * device;
165 unsigned long temperature;
166 unsigned long last_temperature;
167 unsigned long polling_frequency;
169 struct acpi_thermal_flags flags;
170 struct acpi_thermal_state state;
171 struct acpi_thermal_trips trips;
172 struct acpi_handle_list devices;
173 struct thermal_zone_device *thermal_zone;
176 struct work_struct thermal_check_work;
179 /* --------------------------------------------------------------------------
180 Thermal Zone Management
181 -------------------------------------------------------------------------- */
183 static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
185 acpi_status status = AE_OK;
186 unsigned long long tmp;
191 tz->last_temperature = tz->temperature;
193 status = acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tmp);
194 if (ACPI_FAILURE(status))
197 tz->temperature = tmp;
198 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "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_DEBUG_PRINT((ACPI_DB_INFO, "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_has_method(tz->device->handle, "_SCP")) {
229 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
231 } else if (ACPI_FAILURE(acpi_execute_simple_method(tz->device->handle,
239 #define ACPI_TRIPS_CRITICAL 0x01
240 #define ACPI_TRIPS_HOT 0x02
241 #define ACPI_TRIPS_PASSIVE 0x04
242 #define ACPI_TRIPS_ACTIVE 0x08
243 #define ACPI_TRIPS_DEVICES 0x10
245 #define ACPI_TRIPS_REFRESH_THRESHOLDS (ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE)
246 #define ACPI_TRIPS_REFRESH_DEVICES ACPI_TRIPS_DEVICES
248 #define ACPI_TRIPS_INIT (ACPI_TRIPS_CRITICAL | ACPI_TRIPS_HOT | \
249 ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE | \
253 * This exception is thrown out in two cases:
254 * 1.An invalid trip point becomes invalid or a valid trip point becomes invalid
255 * when re-evaluating the AML code.
256 * 2.TODO: Devices listed in _PSL, _ALx, _TZD may change.
257 * We need to re-bind the cooling devices of a thermal zone when this occurs.
259 #define ACPI_THERMAL_TRIPS_EXCEPTION(flags, str) \
261 if (flags != ACPI_TRIPS_INIT) \
262 ACPI_EXCEPTION((AE_INFO, AE_ERROR, \
263 "ACPI thermal trip point %s changed\n" \
264 "Please send acpidump to linux-acpi@vger.kernel.org", str)); \
267 static int acpi_thermal_trips_update(struct acpi_thermal *tz, int flag)
269 acpi_status status = AE_OK;
270 unsigned long long tmp;
271 struct acpi_handle_list devices;
275 /* Critical Shutdown */
276 if (flag & ACPI_TRIPS_CRITICAL) {
277 status = acpi_evaluate_integer(tz->device->handle,
279 tz->trips.critical.temperature = tmp;
281 * Treat freezing temperatures as invalid as well; some
282 * BIOSes return really low values and cause reboots at startup.
283 * Below zero (Celsius) values clearly aren't right for sure..
284 * ... so lets discard those as invalid.
286 if (ACPI_FAILURE(status)) {
287 tz->trips.critical.flags.valid = 0;
288 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
289 "No critical threshold\n"));
290 } else if (tmp <= 2732) {
291 pr_warn(FW_BUG "Invalid critical threshold (%llu)\n",
293 tz->trips.critical.flags.valid = 0;
295 tz->trips.critical.flags.valid = 1;
296 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
297 "Found critical threshold [%lu]\n",
298 tz->trips.critical.temperature));
300 if (tz->trips.critical.flags.valid == 1) {
302 tz->trips.critical.flags.valid = 0;
303 } else if (crt > 0) {
304 unsigned long crt_k = CELSIUS_TO_DECI_KELVIN(crt);
306 * Allow override critical threshold
308 if (crt_k > tz->trips.critical.temperature)
309 pr_warn(PREFIX "Critical threshold %d C\n",
311 tz->trips.critical.temperature = crt_k;
316 /* Critical Sleep (optional) */
317 if (flag & ACPI_TRIPS_HOT) {
318 status = acpi_evaluate_integer(tz->device->handle,
320 if (ACPI_FAILURE(status)) {
321 tz->trips.hot.flags.valid = 0;
322 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
323 "No hot threshold\n"));
325 tz->trips.hot.temperature = tmp;
326 tz->trips.hot.flags.valid = 1;
327 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
328 "Found hot threshold [%lu]\n",
329 tz->trips.hot.temperature));
333 /* Passive (optional) */
334 if (((flag & ACPI_TRIPS_PASSIVE) && tz->trips.passive.flags.valid) ||
335 (flag == ACPI_TRIPS_INIT)) {
336 valid = tz->trips.passive.flags.valid;
339 } else if (psv > 0) {
340 tmp = CELSIUS_TO_DECI_KELVIN(psv);
343 status = acpi_evaluate_integer(tz->device->handle,
347 if (ACPI_FAILURE(status))
348 tz->trips.passive.flags.valid = 0;
350 tz->trips.passive.temperature = tmp;
351 tz->trips.passive.flags.valid = 1;
352 if (flag == ACPI_TRIPS_INIT) {
353 status = acpi_evaluate_integer(
354 tz->device->handle, "_TC1",
356 if (ACPI_FAILURE(status))
357 tz->trips.passive.flags.valid = 0;
359 tz->trips.passive.tc1 = tmp;
360 status = acpi_evaluate_integer(
361 tz->device->handle, "_TC2",
363 if (ACPI_FAILURE(status))
364 tz->trips.passive.flags.valid = 0;
366 tz->trips.passive.tc2 = tmp;
367 status = acpi_evaluate_integer(
368 tz->device->handle, "_TSP",
370 if (ACPI_FAILURE(status))
371 tz->trips.passive.flags.valid = 0;
373 tz->trips.passive.tsp = tmp;
377 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.passive.flags.valid) {
378 memset(&devices, 0, sizeof(struct acpi_handle_list));
379 status = acpi_evaluate_reference(tz->device->handle, "_PSL",
381 if (ACPI_FAILURE(status)) {
382 pr_warn(PREFIX "Invalid passive threshold\n");
383 tz->trips.passive.flags.valid = 0;
386 tz->trips.passive.flags.valid = 1;
388 if (memcmp(&tz->trips.passive.devices, &devices,
389 sizeof(struct acpi_handle_list))) {
390 memcpy(&tz->trips.passive.devices, &devices,
391 sizeof(struct acpi_handle_list));
392 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
395 if ((flag & ACPI_TRIPS_PASSIVE) || (flag & ACPI_TRIPS_DEVICES)) {
396 if (valid != tz->trips.passive.flags.valid)
397 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
400 /* Active (optional) */
401 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
402 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
403 valid = tz->trips.active[i].flags.valid;
406 break; /* disable all active trip points */
408 if ((flag == ACPI_TRIPS_INIT) || ((flag & ACPI_TRIPS_ACTIVE) &&
409 tz->trips.active[i].flags.valid)) {
410 status = acpi_evaluate_integer(tz->device->handle,
412 if (ACPI_FAILURE(status)) {
413 tz->trips.active[i].flags.valid = 0;
419 tz->trips.active[0].temperature =
420 CELSIUS_TO_DECI_KELVIN(act);
423 * Don't allow override higher than
424 * the next higher trip point
426 tz->trips.active[i - 1].temperature =
427 (tz->trips.active[i - 2].temperature <
428 CELSIUS_TO_DECI_KELVIN(act) ?
429 tz->trips.active[i - 2].temperature :
430 CELSIUS_TO_DECI_KELVIN(act));
433 tz->trips.active[i].temperature = tmp;
434 tz->trips.active[i].flags.valid = 1;
439 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.active[i].flags.valid ) {
440 memset(&devices, 0, sizeof(struct acpi_handle_list));
441 status = acpi_evaluate_reference(tz->device->handle,
442 name, NULL, &devices);
443 if (ACPI_FAILURE(status)) {
444 pr_warn(PREFIX "Invalid active%d threshold\n",
446 tz->trips.active[i].flags.valid = 0;
449 tz->trips.active[i].flags.valid = 1;
451 if (memcmp(&tz->trips.active[i].devices, &devices,
452 sizeof(struct acpi_handle_list))) {
453 memcpy(&tz->trips.active[i].devices, &devices,
454 sizeof(struct acpi_handle_list));
455 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
458 if ((flag & ACPI_TRIPS_ACTIVE) || (flag & ACPI_TRIPS_DEVICES))
459 if (valid != tz->trips.active[i].flags.valid)
460 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
462 if (!tz->trips.active[i].flags.valid)
466 if ((flag & ACPI_TRIPS_DEVICES)
467 && acpi_has_method(tz->device->handle, "_TZD")) {
468 memset(&devices, 0, sizeof(devices));
469 status = acpi_evaluate_reference(tz->device->handle, "_TZD",
471 if (ACPI_SUCCESS(status)
472 && memcmp(&tz->devices, &devices, sizeof(devices))) {
473 tz->devices = devices;
474 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
481 static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
483 int i, valid, ret = acpi_thermal_trips_update(tz, ACPI_TRIPS_INIT);
488 valid = tz->trips.critical.flags.valid |
489 tz->trips.hot.flags.valid |
490 tz->trips.passive.flags.valid;
492 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
493 valid |= tz->trips.active[i].flags.valid;
496 pr_warn(FW_BUG "No valid trip found\n");
502 static void acpi_thermal_check(void *data)
504 struct acpi_thermal *tz = data;
509 thermal_zone_device_update(tz->thermal_zone,
510 THERMAL_EVENT_UNSPECIFIED);
513 /* sys I/F for generic thermal sysfs support */
515 static int thermal_get_temp(struct thermal_zone_device *thermal, int *temp)
517 struct acpi_thermal *tz = thermal->devdata;
523 result = acpi_thermal_get_temperature(tz);
527 *temp = DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(tz->temperature,
532 static int thermal_get_mode(struct thermal_zone_device *thermal,
533 enum thermal_device_mode *mode)
535 struct acpi_thermal *tz = thermal->devdata;
540 *mode = tz->tz_enabled ? THERMAL_DEVICE_ENABLED :
541 THERMAL_DEVICE_DISABLED;
546 static int thermal_set_mode(struct thermal_zone_device *thermal,
547 enum thermal_device_mode mode)
549 struct acpi_thermal *tz = thermal->devdata;
556 * enable/disable thermal management from ACPI thermal driver
558 if (mode == THERMAL_DEVICE_ENABLED)
560 else if (mode == THERMAL_DEVICE_DISABLED) {
562 pr_warn("thermal zone will be disabled\n");
566 if (enable != tz->tz_enabled) {
567 tz->tz_enabled = enable;
568 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
569 "%s kernel ACPI thermal control\n",
570 tz->tz_enabled ? "Enable" : "Disable"));
571 acpi_thermal_check(tz);
576 static int thermal_get_trip_type(struct thermal_zone_device *thermal,
577 int trip, enum thermal_trip_type *type)
579 struct acpi_thermal *tz = thermal->devdata;
585 if (tz->trips.critical.flags.valid) {
587 *type = THERMAL_TRIP_CRITICAL;
593 if (tz->trips.hot.flags.valid) {
595 *type = THERMAL_TRIP_HOT;
601 if (tz->trips.passive.flags.valid) {
603 *type = THERMAL_TRIP_PASSIVE;
609 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
610 tz->trips.active[i].flags.valid; i++) {
612 *type = THERMAL_TRIP_ACTIVE;
621 static int thermal_get_trip_temp(struct thermal_zone_device *thermal,
624 struct acpi_thermal *tz = thermal->devdata;
630 if (tz->trips.critical.flags.valid) {
632 *temp = DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(
633 tz->trips.critical.temperature,
640 if (tz->trips.hot.flags.valid) {
642 *temp = DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(
643 tz->trips.hot.temperature,
650 if (tz->trips.passive.flags.valid) {
652 *temp = DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(
653 tz->trips.passive.temperature,
660 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
661 tz->trips.active[i].flags.valid; i++) {
663 *temp = DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(
664 tz->trips.active[i].temperature,
674 static int thermal_get_crit_temp(struct thermal_zone_device *thermal,
677 struct acpi_thermal *tz = thermal->devdata;
679 if (tz->trips.critical.flags.valid) {
680 *temperature = DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(
681 tz->trips.critical.temperature,
688 static int thermal_get_trend(struct thermal_zone_device *thermal,
689 int trip, enum thermal_trend *trend)
691 struct acpi_thermal *tz = thermal->devdata;
692 enum thermal_trip_type type;
695 if (thermal_get_trip_type(thermal, trip, &type))
698 if (type == THERMAL_TRIP_ACTIVE) {
700 int temp = DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(
701 tz->temperature, tz->kelvin_offset);
702 if (thermal_get_trip_temp(thermal, trip, &trip_temp))
705 if (temp > trip_temp) {
706 *trend = THERMAL_TREND_RAISING;
709 /* Fall back on default trend */
715 * tz->temperature has already been updated by generic thermal layer,
716 * before this callback being invoked
718 i = (tz->trips.passive.tc1 * (tz->temperature - tz->last_temperature))
719 + (tz->trips.passive.tc2
720 * (tz->temperature - tz->trips.passive.temperature));
723 *trend = THERMAL_TREND_RAISING;
725 *trend = THERMAL_TREND_DROPPING;
727 *trend = THERMAL_TREND_STABLE;
732 static int thermal_notify(struct thermal_zone_device *thermal, int trip,
733 enum thermal_trip_type trip_type)
736 struct acpi_thermal *tz = thermal->devdata;
738 if (trip_type == THERMAL_TRIP_CRITICAL)
739 type = ACPI_THERMAL_NOTIFY_CRITICAL;
740 else if (trip_type == THERMAL_TRIP_HOT)
741 type = ACPI_THERMAL_NOTIFY_HOT;
745 acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
746 dev_name(&tz->device->dev), type, 1);
748 if (trip_type == THERMAL_TRIP_CRITICAL && nocrt)
754 static int acpi_thermal_cooling_device_cb(struct thermal_zone_device *thermal,
755 struct thermal_cooling_device *cdev,
758 struct acpi_device *device = cdev->devdata;
759 struct acpi_thermal *tz = thermal->devdata;
760 struct acpi_device *dev;
768 if (tz->trips.critical.flags.valid)
771 if (tz->trips.hot.flags.valid)
774 if (tz->trips.passive.flags.valid) {
776 for (i = 0; i < tz->trips.passive.devices.count;
778 handle = tz->trips.passive.devices.handles[i];
779 status = acpi_bus_get_device(handle, &dev);
780 if (ACPI_FAILURE(status) || dev != device)
784 thermal_zone_bind_cooling_device
785 (thermal, trip, cdev,
786 THERMAL_NO_LIMIT, THERMAL_NO_LIMIT,
787 THERMAL_WEIGHT_DEFAULT);
790 thermal_zone_unbind_cooling_device
791 (thermal, trip, cdev);
797 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
798 if (!tz->trips.active[i].flags.valid)
802 j < tz->trips.active[i].devices.count;
804 handle = tz->trips.active[i].devices.handles[j];
805 status = acpi_bus_get_device(handle, &dev);
806 if (ACPI_FAILURE(status) || dev != device)
809 result = thermal_zone_bind_cooling_device
810 (thermal, trip, cdev,
811 THERMAL_NO_LIMIT, THERMAL_NO_LIMIT,
812 THERMAL_WEIGHT_DEFAULT);
814 result = thermal_zone_unbind_cooling_device
815 (thermal, trip, cdev);
821 for (i = 0; i < tz->devices.count; i++) {
822 handle = tz->devices.handles[i];
823 status = acpi_bus_get_device(handle, &dev);
824 if (ACPI_SUCCESS(status) && (dev == device)) {
826 result = thermal_zone_bind_cooling_device
827 (thermal, THERMAL_TRIPS_NONE,
828 cdev, THERMAL_NO_LIMIT,
830 THERMAL_WEIGHT_DEFAULT);
832 result = thermal_zone_unbind_cooling_device
833 (thermal, THERMAL_TRIPS_NONE,
845 acpi_thermal_bind_cooling_device(struct thermal_zone_device *thermal,
846 struct thermal_cooling_device *cdev)
848 return acpi_thermal_cooling_device_cb(thermal, cdev, true);
852 acpi_thermal_unbind_cooling_device(struct thermal_zone_device *thermal,
853 struct thermal_cooling_device *cdev)
855 return acpi_thermal_cooling_device_cb(thermal, cdev, false);
858 static struct thermal_zone_device_ops acpi_thermal_zone_ops = {
859 .bind = acpi_thermal_bind_cooling_device,
860 .unbind = acpi_thermal_unbind_cooling_device,
861 .get_temp = thermal_get_temp,
862 .get_mode = thermal_get_mode,
863 .set_mode = thermal_set_mode,
864 .get_trip_type = thermal_get_trip_type,
865 .get_trip_temp = thermal_get_trip_temp,
866 .get_crit_temp = thermal_get_crit_temp,
867 .get_trend = thermal_get_trend,
868 .notify = thermal_notify,
871 static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz)
878 if (tz->trips.critical.flags.valid)
881 if (tz->trips.hot.flags.valid)
884 if (tz->trips.passive.flags.valid)
887 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
888 tz->trips.active[i].flags.valid; i++, trips++);
890 if (tz->trips.passive.flags.valid)
892 thermal_zone_device_register("acpitz", trips, 0, tz,
893 &acpi_thermal_zone_ops, NULL,
894 tz->trips.passive.tsp*100,
895 tz->polling_frequency*100);
898 thermal_zone_device_register("acpitz", trips, 0, tz,
899 &acpi_thermal_zone_ops, NULL,
900 0, tz->polling_frequency*100);
901 if (IS_ERR(tz->thermal_zone))
904 result = sysfs_create_link(&tz->device->dev.kobj,
905 &tz->thermal_zone->device.kobj, "thermal_zone");
909 result = sysfs_create_link(&tz->thermal_zone->device.kobj,
910 &tz->device->dev.kobj, "device");
914 status = acpi_bus_attach_private_data(tz->device->handle,
916 if (ACPI_FAILURE(status))
921 dev_info(&tz->device->dev, "registered as thermal_zone%d\n",
922 tz->thermal_zone->id);
926 static void acpi_thermal_unregister_thermal_zone(struct acpi_thermal *tz)
928 sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
929 sysfs_remove_link(&tz->thermal_zone->device.kobj, "device");
930 thermal_zone_device_unregister(tz->thermal_zone);
931 tz->thermal_zone = NULL;
932 acpi_bus_detach_private_data(tz->device->handle);
936 /* --------------------------------------------------------------------------
938 -------------------------------------------------------------------------- */
940 static void acpi_thermal_notify(struct acpi_device *device, u32 event)
942 struct acpi_thermal *tz = acpi_driver_data(device);
949 case ACPI_THERMAL_NOTIFY_TEMPERATURE:
950 acpi_thermal_check(tz);
952 case ACPI_THERMAL_NOTIFY_THRESHOLDS:
953 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_THRESHOLDS);
954 acpi_thermal_check(tz);
955 acpi_bus_generate_netlink_event(device->pnp.device_class,
956 dev_name(&device->dev), event, 0);
958 case ACPI_THERMAL_NOTIFY_DEVICES:
959 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_DEVICES);
960 acpi_thermal_check(tz);
961 acpi_bus_generate_netlink_event(device->pnp.device_class,
962 dev_name(&device->dev), event, 0);
965 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
966 "Unsupported event [0x%x]\n", event));
972 * On some platforms, the AML code has dependency about
973 * the evaluating order of _TMP and _CRT/_HOT/_PSV/_ACx.
974 * 1. On HP Pavilion G4-1016tx, _TMP must be invoked after
975 * /_CRT/_HOT/_PSV/_ACx, or else system will be power off.
976 * 2. On HP Compaq 6715b/6715s, the return value of _PSV is 0
977 * if _TMP has never been evaluated.
979 * As this dependency is totally transparent to OS, evaluate
980 * all of them once, in the order of _CRT/_HOT/_PSV/_ACx,
981 * _TMP, before they are actually used.
983 static void acpi_thermal_aml_dependency_fix(struct acpi_thermal *tz)
985 acpi_handle handle = tz->device->handle;
986 unsigned long long value;
989 acpi_evaluate_integer(handle, "_CRT", NULL, &value);
990 acpi_evaluate_integer(handle, "_HOT", NULL, &value);
991 acpi_evaluate_integer(handle, "_PSV", NULL, &value);
992 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
993 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
996 status = acpi_evaluate_integer(handle, name, NULL, &value);
997 if (status == AE_NOT_FOUND)
1000 acpi_evaluate_integer(handle, "_TMP", NULL, &value);
1003 static int acpi_thermal_get_info(struct acpi_thermal *tz)
1011 acpi_thermal_aml_dependency_fix(tz);
1013 /* Get trip points [_CRT, _PSV, etc.] (required) */
1014 result = acpi_thermal_get_trip_points(tz);
1018 /* Get temperature [_TMP] (required) */
1019 result = acpi_thermal_get_temperature(tz);
1023 /* Set the cooling mode [_SCP] to active cooling (default) */
1024 result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
1026 tz->flags.cooling_mode = 1;
1028 /* Get default polling frequency [_TZP] (optional) */
1030 tz->polling_frequency = tzp;
1032 acpi_thermal_get_polling_frequency(tz);
1038 * The exact offset between Kelvin and degree Celsius is 273.15. However ACPI
1039 * handles temperature values with a single decimal place. As a consequence,
1040 * some implementations use an offset of 273.1 and others use an offset of
1041 * 273.2. Try to find out which one is being used, to present the most
1042 * accurate and visually appealing number.
1044 * The heuristic below should work for all ACPI thermal zones which have a
1045 * critical trip point with a value being a multiple of 0.5 degree Celsius.
1047 static void acpi_thermal_guess_offset(struct acpi_thermal *tz)
1049 if (tz->trips.critical.flags.valid &&
1050 (tz->trips.critical.temperature % 5) == 1)
1051 tz->kelvin_offset = 2731;
1053 tz->kelvin_offset = 2732;
1056 static void acpi_thermal_check_fn(struct work_struct *work)
1058 struct acpi_thermal *tz = container_of(work, struct acpi_thermal,
1059 thermal_check_work);
1060 acpi_thermal_check(tz);
1063 static int acpi_thermal_add(struct acpi_device *device)
1066 struct acpi_thermal *tz = NULL;
1072 tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
1076 tz->device = device;
1077 strcpy(tz->name, device->pnp.bus_id);
1078 strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1079 strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
1080 device->driver_data = tz;
1082 result = acpi_thermal_get_info(tz);
1086 acpi_thermal_guess_offset(tz);
1088 result = acpi_thermal_register_thermal_zone(tz);
1092 INIT_WORK(&tz->thermal_check_work, acpi_thermal_check_fn);
1094 pr_info(PREFIX "%s [%s] (%ld C)\n", acpi_device_name(device),
1095 acpi_device_bid(device), DECI_KELVIN_TO_CELSIUS(tz->temperature));
1104 static int acpi_thermal_remove(struct acpi_device *device)
1106 struct acpi_thermal *tz = NULL;
1108 if (!device || !acpi_driver_data(device))
1111 flush_workqueue(acpi_thermal_pm_queue);
1112 tz = acpi_driver_data(device);
1114 acpi_thermal_unregister_thermal_zone(tz);
1119 #ifdef CONFIG_PM_SLEEP
1120 static int acpi_thermal_suspend(struct device *dev)
1122 /* Make sure the previously queued thermal check work has been done */
1123 flush_workqueue(acpi_thermal_pm_queue);
1127 static int acpi_thermal_resume(struct device *dev)
1129 struct acpi_thermal *tz;
1130 int i, j, power_state, result;
1135 tz = acpi_driver_data(to_acpi_device(dev));
1139 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1140 if (!(&tz->trips.active[i]))
1142 if (!tz->trips.active[i].flags.valid)
1144 tz->trips.active[i].flags.enabled = 1;
1145 for (j = 0; j < tz->trips.active[i].devices.count; j++) {
1146 result = acpi_bus_update_power(
1147 tz->trips.active[i].devices.handles[j],
1149 if (result || (power_state != ACPI_STATE_D0)) {
1150 tz->trips.active[i].flags.enabled = 0;
1154 tz->state.active |= tz->trips.active[i].flags.enabled;
1157 queue_work(acpi_thermal_pm_queue, &tz->thermal_check_work);
1163 static int thermal_act(const struct dmi_system_id *d) {
1166 pr_notice(PREFIX "%s detected: "
1167 "disabling all active thermal trip points\n", d->ident);
1172 static int thermal_nocrt(const struct dmi_system_id *d) {
1174 pr_notice(PREFIX "%s detected: "
1175 "disabling all critical thermal trip point actions.\n", d->ident);
1179 static int thermal_tzp(const struct dmi_system_id *d) {
1182 pr_notice(PREFIX "%s detected: "
1183 "enabling thermal zone polling\n", d->ident);
1184 tzp = 300; /* 300 dS = 30 Seconds */
1188 static int thermal_psv(const struct dmi_system_id *d) {
1191 pr_notice(PREFIX "%s detected: "
1192 "disabling all passive thermal trip points\n", d->ident);
1198 static const struct dmi_system_id thermal_dmi_table[] __initconst = {
1200 * Award BIOS on this AOpen makes thermal control almost worthless.
1201 * http://bugzilla.kernel.org/show_bug.cgi?id=8842
1204 .callback = thermal_act,
1205 .ident = "AOpen i915GMm-HFS",
1207 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1208 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1212 .callback = thermal_psv,
1213 .ident = "AOpen i915GMm-HFS",
1215 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1216 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1220 .callback = thermal_tzp,
1221 .ident = "AOpen i915GMm-HFS",
1223 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1224 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1228 .callback = thermal_nocrt,
1229 .ident = "Gigabyte GA-7ZX",
1231 DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
1232 DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
1238 static int __init acpi_thermal_init(void)
1242 dmi_check_system(thermal_dmi_table);
1245 pr_notice(PREFIX "thermal control disabled\n");
1249 acpi_thermal_pm_queue = alloc_workqueue("acpi_thermal_pm",
1250 WQ_HIGHPRI | WQ_MEM_RECLAIM, 0);
1251 if (!acpi_thermal_pm_queue)
1254 result = acpi_bus_register_driver(&acpi_thermal_driver);
1256 destroy_workqueue(acpi_thermal_pm_queue);
1263 static void __exit acpi_thermal_exit(void)
1265 acpi_bus_unregister_driver(&acpi_thermal_driver);
1266 destroy_workqueue(acpi_thermal_pm_queue);
1271 module_init(acpi_thermal_init);
1272 module_exit(acpi_thermal_exit);