2 * acpi_thermal.c - ACPI Thermal Zone Driver ($Revision: 41 $)
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or (at
12 * your option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21 * This driver fully implements the ACPI thermal policy as described in the
22 * ACPI 2.0 Specification.
24 * TBD: 1. Implement passive cooling hysteresis.
25 * 2. Enhance passive cooling (CPU) states/limit interface to support
26 * concepts of 'multiple limiters', upper/lower limits, etc.
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/dmi.h>
33 #include <linux/init.h>
34 #include <linux/slab.h>
35 #include <linux/types.h>
36 #include <linux/jiffies.h>
37 #include <linux/kmod.h>
38 #include <linux/reboot.h>
39 #include <linux/device.h>
40 #include <linux/thermal.h>
41 #include <linux/acpi.h>
42 #include <linux/workqueue.h>
43 #include <asm/uaccess.h>
45 #define PREFIX "ACPI: "
47 #define ACPI_THERMAL_CLASS "thermal_zone"
48 #define ACPI_THERMAL_DEVICE_NAME "Thermal Zone"
49 #define ACPI_THERMAL_NOTIFY_TEMPERATURE 0x80
50 #define ACPI_THERMAL_NOTIFY_THRESHOLDS 0x81
51 #define ACPI_THERMAL_NOTIFY_DEVICES 0x82
52 #define ACPI_THERMAL_NOTIFY_CRITICAL 0xF0
53 #define ACPI_THERMAL_NOTIFY_HOT 0xF1
54 #define ACPI_THERMAL_MODE_ACTIVE 0x00
56 #define ACPI_THERMAL_MAX_ACTIVE 10
57 #define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65
59 #define _COMPONENT ACPI_THERMAL_COMPONENT
60 ACPI_MODULE_NAME("thermal");
62 MODULE_AUTHOR("Paul Diefenbaugh");
63 MODULE_DESCRIPTION("ACPI Thermal Zone Driver");
64 MODULE_LICENSE("GPL");
67 module_param(act, int, 0644);
68 MODULE_PARM_DESC(act, "Disable or override all lowest active trip points.");
71 module_param(crt, int, 0644);
72 MODULE_PARM_DESC(crt, "Disable or lower all critical trip points.");
75 module_param(tzp, int, 0444);
76 MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.");
79 module_param(nocrt, int, 0);
80 MODULE_PARM_DESC(nocrt, "Set to take no action upon ACPI thermal zone critical trips points.");
83 module_param(off, int, 0);
84 MODULE_PARM_DESC(off, "Set to disable ACPI thermal support.");
87 module_param(psv, int, 0644);
88 MODULE_PARM_DESC(psv, "Disable or override all passive trip points.");
90 static struct workqueue_struct *acpi_thermal_pm_queue;
92 static int acpi_thermal_add(struct acpi_device *device);
93 static int acpi_thermal_remove(struct acpi_device *device);
94 static void acpi_thermal_notify(struct acpi_device *device, u32 event);
96 static const struct acpi_device_id thermal_device_ids[] = {
97 {ACPI_THERMAL_HID, 0},
100 MODULE_DEVICE_TABLE(acpi, thermal_device_ids);
102 #ifdef CONFIG_PM_SLEEP
103 static int acpi_thermal_suspend(struct device *dev);
104 static int acpi_thermal_resume(struct device *dev);
106 #define acpi_thermal_suspend NULL
107 #define acpi_thermal_resume NULL
109 static SIMPLE_DEV_PM_OPS(acpi_thermal_pm, acpi_thermal_suspend, acpi_thermal_resume);
111 static struct acpi_driver acpi_thermal_driver = {
113 .class = ACPI_THERMAL_CLASS,
114 .ids = thermal_device_ids,
116 .add = acpi_thermal_add,
117 .remove = acpi_thermal_remove,
118 .notify = acpi_thermal_notify,
120 .drv.pm = &acpi_thermal_pm,
123 struct acpi_thermal_state {
132 struct acpi_thermal_state_flags {
138 struct acpi_thermal_critical {
139 struct acpi_thermal_state_flags flags;
140 unsigned long temperature;
143 struct acpi_thermal_hot {
144 struct acpi_thermal_state_flags flags;
145 unsigned long temperature;
148 struct acpi_thermal_passive {
149 struct acpi_thermal_state_flags flags;
150 unsigned long temperature;
154 struct acpi_handle_list devices;
157 struct acpi_thermal_active {
158 struct acpi_thermal_state_flags flags;
159 unsigned long temperature;
160 struct acpi_handle_list devices;
163 struct acpi_thermal_trips {
164 struct acpi_thermal_critical critical;
165 struct acpi_thermal_hot hot;
166 struct acpi_thermal_passive passive;
167 struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
170 struct acpi_thermal_flags {
171 u8 cooling_mode:1; /* _SCP */
172 u8 devices:1; /* _TZD */
176 struct acpi_thermal {
177 struct acpi_device * device;
179 unsigned long temperature;
180 unsigned long last_temperature;
181 unsigned long polling_frequency;
183 struct acpi_thermal_flags flags;
184 struct acpi_thermal_state state;
185 struct acpi_thermal_trips trips;
186 struct acpi_handle_list devices;
187 struct thermal_zone_device *thermal_zone;
190 struct work_struct thermal_check_work;
191 struct mutex thermal_check_lock;
192 atomic_t thermal_check_count;
195 /* --------------------------------------------------------------------------
196 Thermal Zone Management
197 -------------------------------------------------------------------------- */
199 static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
201 acpi_status status = AE_OK;
202 unsigned long long tmp;
207 tz->last_temperature = tz->temperature;
209 status = acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tmp);
210 if (ACPI_FAILURE(status))
213 tz->temperature = tmp;
214 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
220 static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
222 acpi_status status = AE_OK;
223 unsigned long long tmp;
228 status = acpi_evaluate_integer(tz->device->handle, "_TZP", NULL, &tmp);
229 if (ACPI_FAILURE(status))
232 tz->polling_frequency = tmp;
233 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
234 tz->polling_frequency));
239 static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
244 if (!acpi_has_method(tz->device->handle, "_SCP")) {
245 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
247 } else if (ACPI_FAILURE(acpi_execute_simple_method(tz->device->handle,
255 #define ACPI_TRIPS_CRITICAL 0x01
256 #define ACPI_TRIPS_HOT 0x02
257 #define ACPI_TRIPS_PASSIVE 0x04
258 #define ACPI_TRIPS_ACTIVE 0x08
259 #define ACPI_TRIPS_DEVICES 0x10
261 #define ACPI_TRIPS_REFRESH_THRESHOLDS (ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE)
262 #define ACPI_TRIPS_REFRESH_DEVICES ACPI_TRIPS_DEVICES
264 #define ACPI_TRIPS_INIT (ACPI_TRIPS_CRITICAL | ACPI_TRIPS_HOT | \
265 ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE | \
269 * This exception is thrown out in two cases:
270 * 1.An invalid trip point becomes invalid or a valid trip point becomes invalid
271 * when re-evaluating the AML code.
272 * 2.TODO: Devices listed in _PSL, _ALx, _TZD may change.
273 * We need to re-bind the cooling devices of a thermal zone when this occurs.
275 #define ACPI_THERMAL_TRIPS_EXCEPTION(flags, str) \
277 if (flags != ACPI_TRIPS_INIT) \
278 ACPI_EXCEPTION((AE_INFO, AE_ERROR, \
279 "ACPI thermal trip point %s changed\n" \
280 "Please send acpidump to linux-acpi@vger.kernel.org", str)); \
283 static int acpi_thermal_trips_update(struct acpi_thermal *tz, int flag)
285 acpi_status status = AE_OK;
286 unsigned long long tmp;
287 struct acpi_handle_list devices;
291 /* Critical Shutdown */
292 if (flag & ACPI_TRIPS_CRITICAL) {
293 status = acpi_evaluate_integer(tz->device->handle,
295 tz->trips.critical.temperature = tmp;
297 * Treat freezing temperatures as invalid as well; some
298 * BIOSes return really low values and cause reboots at startup.
299 * Below zero (Celsius) values clearly aren't right for sure..
300 * ... so lets discard those as invalid.
302 if (ACPI_FAILURE(status)) {
303 tz->trips.critical.flags.valid = 0;
304 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
305 "No critical threshold\n"));
306 } else if (tmp <= 2732) {
307 pr_warn(FW_BUG "Invalid critical threshold (%llu)\n",
309 tz->trips.critical.flags.valid = 0;
311 tz->trips.critical.flags.valid = 1;
312 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
313 "Found critical threshold [%lu]\n",
314 tz->trips.critical.temperature));
316 if (tz->trips.critical.flags.valid == 1) {
318 tz->trips.critical.flags.valid = 0;
319 } else if (crt > 0) {
320 unsigned long crt_k = CELSIUS_TO_DECI_KELVIN(crt);
322 * Allow override critical threshold
324 if (crt_k > tz->trips.critical.temperature)
325 pr_warn(PREFIX "Critical threshold %d C\n",
327 tz->trips.critical.temperature = crt_k;
332 /* Critical Sleep (optional) */
333 if (flag & ACPI_TRIPS_HOT) {
334 status = acpi_evaluate_integer(tz->device->handle,
336 if (ACPI_FAILURE(status)) {
337 tz->trips.hot.flags.valid = 0;
338 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
339 "No hot threshold\n"));
341 tz->trips.hot.temperature = tmp;
342 tz->trips.hot.flags.valid = 1;
343 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
344 "Found hot threshold [%lu]\n",
345 tz->trips.hot.temperature));
349 /* Passive (optional) */
350 if (((flag & ACPI_TRIPS_PASSIVE) && tz->trips.passive.flags.valid) ||
351 (flag == ACPI_TRIPS_INIT)) {
352 valid = tz->trips.passive.flags.valid;
355 } else if (psv > 0) {
356 tmp = CELSIUS_TO_DECI_KELVIN(psv);
359 status = acpi_evaluate_integer(tz->device->handle,
363 if (ACPI_FAILURE(status))
364 tz->trips.passive.flags.valid = 0;
366 tz->trips.passive.temperature = tmp;
367 tz->trips.passive.flags.valid = 1;
368 if (flag == ACPI_TRIPS_INIT) {
369 status = acpi_evaluate_integer(
370 tz->device->handle, "_TC1",
372 if (ACPI_FAILURE(status))
373 tz->trips.passive.flags.valid = 0;
375 tz->trips.passive.tc1 = tmp;
376 status = acpi_evaluate_integer(
377 tz->device->handle, "_TC2",
379 if (ACPI_FAILURE(status))
380 tz->trips.passive.flags.valid = 0;
382 tz->trips.passive.tc2 = tmp;
383 status = acpi_evaluate_integer(
384 tz->device->handle, "_TSP",
386 if (ACPI_FAILURE(status))
387 tz->trips.passive.flags.valid = 0;
389 tz->trips.passive.tsp = tmp;
393 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.passive.flags.valid) {
394 memset(&devices, 0, sizeof(struct acpi_handle_list));
395 status = acpi_evaluate_reference(tz->device->handle, "_PSL",
397 if (ACPI_FAILURE(status)) {
398 pr_warn(PREFIX "Invalid passive threshold\n");
399 tz->trips.passive.flags.valid = 0;
402 tz->trips.passive.flags.valid = 1;
404 if (memcmp(&tz->trips.passive.devices, &devices,
405 sizeof(struct acpi_handle_list))) {
406 memcpy(&tz->trips.passive.devices, &devices,
407 sizeof(struct acpi_handle_list));
408 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
411 if ((flag & ACPI_TRIPS_PASSIVE) || (flag & ACPI_TRIPS_DEVICES)) {
412 if (valid != tz->trips.passive.flags.valid)
413 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
416 /* Active (optional) */
417 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
418 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
419 valid = tz->trips.active[i].flags.valid;
422 break; /* disable all active trip points */
424 if ((flag == ACPI_TRIPS_INIT) || ((flag & ACPI_TRIPS_ACTIVE) &&
425 tz->trips.active[i].flags.valid)) {
426 status = acpi_evaluate_integer(tz->device->handle,
428 if (ACPI_FAILURE(status)) {
429 tz->trips.active[i].flags.valid = 0;
435 tz->trips.active[0].temperature =
436 CELSIUS_TO_DECI_KELVIN(act);
439 * Don't allow override higher than
440 * the next higher trip point
442 tz->trips.active[i - 1].temperature =
443 (tz->trips.active[i - 2].temperature <
444 CELSIUS_TO_DECI_KELVIN(act) ?
445 tz->trips.active[i - 2].temperature :
446 CELSIUS_TO_DECI_KELVIN(act));
449 tz->trips.active[i].temperature = tmp;
450 tz->trips.active[i].flags.valid = 1;
455 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.active[i].flags.valid ) {
456 memset(&devices, 0, sizeof(struct acpi_handle_list));
457 status = acpi_evaluate_reference(tz->device->handle,
458 name, NULL, &devices);
459 if (ACPI_FAILURE(status)) {
460 pr_warn(PREFIX "Invalid active%d threshold\n",
462 tz->trips.active[i].flags.valid = 0;
465 tz->trips.active[i].flags.valid = 1;
467 if (memcmp(&tz->trips.active[i].devices, &devices,
468 sizeof(struct acpi_handle_list))) {
469 memcpy(&tz->trips.active[i].devices, &devices,
470 sizeof(struct acpi_handle_list));
471 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
474 if ((flag & ACPI_TRIPS_ACTIVE) || (flag & ACPI_TRIPS_DEVICES))
475 if (valid != tz->trips.active[i].flags.valid)
476 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
478 if (!tz->trips.active[i].flags.valid)
482 if ((flag & ACPI_TRIPS_DEVICES)
483 && acpi_has_method(tz->device->handle, "_TZD")) {
484 memset(&devices, 0, sizeof(devices));
485 status = acpi_evaluate_reference(tz->device->handle, "_TZD",
487 if (ACPI_SUCCESS(status)
488 && memcmp(&tz->devices, &devices, sizeof(devices))) {
489 tz->devices = devices;
490 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
497 static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
499 int i, valid, ret = acpi_thermal_trips_update(tz, ACPI_TRIPS_INIT);
504 valid = tz->trips.critical.flags.valid |
505 tz->trips.hot.flags.valid |
506 tz->trips.passive.flags.valid;
508 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
509 valid |= tz->trips.active[i].flags.valid;
512 pr_warn(FW_BUG "No valid trip found\n");
518 /* sys I/F for generic thermal sysfs support */
520 static int thermal_get_temp(struct thermal_zone_device *thermal, int *temp)
522 struct acpi_thermal *tz = thermal->devdata;
528 result = acpi_thermal_get_temperature(tz);
532 *temp = DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(tz->temperature,
537 static int thermal_get_mode(struct thermal_zone_device *thermal,
538 enum thermal_device_mode *mode)
540 struct acpi_thermal *tz = thermal->devdata;
545 *mode = tz->tz_enabled ? THERMAL_DEVICE_ENABLED :
546 THERMAL_DEVICE_DISABLED;
551 static void acpi_thermal_check_fn(struct work_struct *work);
553 static int thermal_set_mode(struct thermal_zone_device *thermal,
554 enum thermal_device_mode mode)
556 struct acpi_thermal *tz = thermal->devdata;
563 * enable/disable thermal management from ACPI thermal driver
565 if (mode == THERMAL_DEVICE_ENABLED)
567 else if (mode == THERMAL_DEVICE_DISABLED) {
569 pr_warn("thermal zone will be disabled\n");
573 if (enable != tz->tz_enabled) {
574 tz->tz_enabled = enable;
575 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
576 "%s kernel ACPI thermal control\n",
577 tz->tz_enabled ? "Enable" : "Disable"));
578 acpi_thermal_check_fn(&tz->thermal_check_work);
583 static int thermal_get_trip_type(struct thermal_zone_device *thermal,
584 int trip, enum thermal_trip_type *type)
586 struct acpi_thermal *tz = thermal->devdata;
592 if (tz->trips.critical.flags.valid) {
594 *type = THERMAL_TRIP_CRITICAL;
600 if (tz->trips.hot.flags.valid) {
602 *type = THERMAL_TRIP_HOT;
608 if (tz->trips.passive.flags.valid) {
610 *type = THERMAL_TRIP_PASSIVE;
616 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
617 tz->trips.active[i].flags.valid; i++) {
619 *type = THERMAL_TRIP_ACTIVE;
628 static int thermal_get_trip_temp(struct thermal_zone_device *thermal,
631 struct acpi_thermal *tz = thermal->devdata;
637 if (tz->trips.critical.flags.valid) {
639 *temp = DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(
640 tz->trips.critical.temperature,
647 if (tz->trips.hot.flags.valid) {
649 *temp = DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(
650 tz->trips.hot.temperature,
657 if (tz->trips.passive.flags.valid) {
659 *temp = DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(
660 tz->trips.passive.temperature,
667 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
668 tz->trips.active[i].flags.valid; i++) {
670 *temp = DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(
671 tz->trips.active[i].temperature,
681 static int thermal_get_crit_temp(struct thermal_zone_device *thermal,
684 struct acpi_thermal *tz = thermal->devdata;
686 if (tz->trips.critical.flags.valid) {
687 *temperature = DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(
688 tz->trips.critical.temperature,
695 static int thermal_get_trend(struct thermal_zone_device *thermal,
696 int trip, enum thermal_trend *trend)
698 struct acpi_thermal *tz = thermal->devdata;
699 enum thermal_trip_type type;
702 if (thermal_get_trip_type(thermal, trip, &type))
705 if (type == THERMAL_TRIP_ACTIVE) {
707 int temp = DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(
708 tz->temperature, tz->kelvin_offset);
709 if (thermal_get_trip_temp(thermal, trip, &trip_temp))
712 if (temp > trip_temp) {
713 *trend = THERMAL_TREND_RAISING;
716 /* Fall back on default trend */
722 * tz->temperature has already been updated by generic thermal layer,
723 * before this callback being invoked
725 i = (tz->trips.passive.tc1 * (tz->temperature - tz->last_temperature))
726 + (tz->trips.passive.tc2
727 * (tz->temperature - tz->trips.passive.temperature));
730 *trend = THERMAL_TREND_RAISING;
732 *trend = THERMAL_TREND_DROPPING;
734 *trend = THERMAL_TREND_STABLE;
739 static int thermal_notify(struct thermal_zone_device *thermal, int trip,
740 enum thermal_trip_type trip_type)
743 struct acpi_thermal *tz = thermal->devdata;
745 if (trip_type == THERMAL_TRIP_CRITICAL)
746 type = ACPI_THERMAL_NOTIFY_CRITICAL;
747 else if (trip_type == THERMAL_TRIP_HOT)
748 type = ACPI_THERMAL_NOTIFY_HOT;
752 acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
753 dev_name(&tz->device->dev), type, 1);
755 if (trip_type == THERMAL_TRIP_CRITICAL && nocrt)
761 static int acpi_thermal_cooling_device_cb(struct thermal_zone_device *thermal,
762 struct thermal_cooling_device *cdev,
765 struct acpi_device *device = cdev->devdata;
766 struct acpi_thermal *tz = thermal->devdata;
767 struct acpi_device *dev;
775 if (tz->trips.critical.flags.valid)
778 if (tz->trips.hot.flags.valid)
781 if (tz->trips.passive.flags.valid) {
783 for (i = 0; i < tz->trips.passive.devices.count;
785 handle = tz->trips.passive.devices.handles[i];
786 status = acpi_bus_get_device(handle, &dev);
787 if (ACPI_FAILURE(status) || dev != device)
791 thermal_zone_bind_cooling_device
792 (thermal, trip, cdev,
793 THERMAL_NO_LIMIT, THERMAL_NO_LIMIT,
794 THERMAL_WEIGHT_DEFAULT);
797 thermal_zone_unbind_cooling_device
798 (thermal, trip, cdev);
804 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
805 if (!tz->trips.active[i].flags.valid)
809 j < tz->trips.active[i].devices.count;
811 handle = tz->trips.active[i].devices.handles[j];
812 status = acpi_bus_get_device(handle, &dev);
813 if (ACPI_FAILURE(status) || dev != device)
816 result = thermal_zone_bind_cooling_device
817 (thermal, trip, cdev,
818 THERMAL_NO_LIMIT, THERMAL_NO_LIMIT,
819 THERMAL_WEIGHT_DEFAULT);
821 result = thermal_zone_unbind_cooling_device
822 (thermal, trip, cdev);
828 for (i = 0; i < tz->devices.count; i++) {
829 handle = tz->devices.handles[i];
830 status = acpi_bus_get_device(handle, &dev);
831 if (ACPI_SUCCESS(status) && (dev == device)) {
833 result = thermal_zone_bind_cooling_device
834 (thermal, THERMAL_TRIPS_NONE,
835 cdev, THERMAL_NO_LIMIT,
837 THERMAL_WEIGHT_DEFAULT);
839 result = thermal_zone_unbind_cooling_device
840 (thermal, THERMAL_TRIPS_NONE,
852 acpi_thermal_bind_cooling_device(struct thermal_zone_device *thermal,
853 struct thermal_cooling_device *cdev)
855 return acpi_thermal_cooling_device_cb(thermal, cdev, true);
859 acpi_thermal_unbind_cooling_device(struct thermal_zone_device *thermal,
860 struct thermal_cooling_device *cdev)
862 return acpi_thermal_cooling_device_cb(thermal, cdev, false);
865 static struct thermal_zone_device_ops acpi_thermal_zone_ops = {
866 .bind = acpi_thermal_bind_cooling_device,
867 .unbind = acpi_thermal_unbind_cooling_device,
868 .get_temp = thermal_get_temp,
869 .get_mode = thermal_get_mode,
870 .set_mode = thermal_set_mode,
871 .get_trip_type = thermal_get_trip_type,
872 .get_trip_temp = thermal_get_trip_temp,
873 .get_crit_temp = thermal_get_crit_temp,
874 .get_trend = thermal_get_trend,
875 .notify = thermal_notify,
878 static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz)
885 if (tz->trips.critical.flags.valid)
888 if (tz->trips.hot.flags.valid)
891 if (tz->trips.passive.flags.valid)
894 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
895 tz->trips.active[i].flags.valid; i++, trips++);
897 if (tz->trips.passive.flags.valid)
899 thermal_zone_device_register("acpitz", trips, 0, tz,
900 &acpi_thermal_zone_ops, NULL,
901 tz->trips.passive.tsp*100,
902 tz->polling_frequency*100);
905 thermal_zone_device_register("acpitz", trips, 0, tz,
906 &acpi_thermal_zone_ops, NULL,
907 0, tz->polling_frequency*100);
908 if (IS_ERR(tz->thermal_zone))
911 result = sysfs_create_link(&tz->device->dev.kobj,
912 &tz->thermal_zone->device.kobj, "thermal_zone");
916 result = sysfs_create_link(&tz->thermal_zone->device.kobj,
917 &tz->device->dev.kobj, "device");
921 status = acpi_bus_attach_private_data(tz->device->handle,
923 if (ACPI_FAILURE(status))
928 dev_info(&tz->device->dev, "registered as thermal_zone%d\n",
929 tz->thermal_zone->id);
933 static void acpi_thermal_unregister_thermal_zone(struct acpi_thermal *tz)
935 sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
936 sysfs_remove_link(&tz->thermal_zone->device.kobj, "device");
937 thermal_zone_device_unregister(tz->thermal_zone);
938 tz->thermal_zone = NULL;
939 acpi_bus_detach_private_data(tz->device->handle);
943 /* --------------------------------------------------------------------------
945 -------------------------------------------------------------------------- */
947 static void acpi_queue_thermal_check(struct acpi_thermal *tz)
949 if (!work_pending(&tz->thermal_check_work))
950 queue_work(acpi_thermal_pm_queue, &tz->thermal_check_work);
953 static void acpi_thermal_notify(struct acpi_device *device, u32 event)
955 struct acpi_thermal *tz = acpi_driver_data(device);
962 case ACPI_THERMAL_NOTIFY_TEMPERATURE:
963 acpi_queue_thermal_check(tz);
965 case ACPI_THERMAL_NOTIFY_THRESHOLDS:
966 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_THRESHOLDS);
967 acpi_queue_thermal_check(tz);
968 acpi_bus_generate_netlink_event(device->pnp.device_class,
969 dev_name(&device->dev), event, 0);
971 case ACPI_THERMAL_NOTIFY_DEVICES:
972 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_DEVICES);
973 acpi_queue_thermal_check(tz);
974 acpi_bus_generate_netlink_event(device->pnp.device_class,
975 dev_name(&device->dev), event, 0);
978 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
979 "Unsupported event [0x%x]\n", event));
985 * On some platforms, the AML code has dependency about
986 * the evaluating order of _TMP and _CRT/_HOT/_PSV/_ACx.
987 * 1. On HP Pavilion G4-1016tx, _TMP must be invoked after
988 * /_CRT/_HOT/_PSV/_ACx, or else system will be power off.
989 * 2. On HP Compaq 6715b/6715s, the return value of _PSV is 0
990 * if _TMP has never been evaluated.
992 * As this dependency is totally transparent to OS, evaluate
993 * all of them once, in the order of _CRT/_HOT/_PSV/_ACx,
994 * _TMP, before they are actually used.
996 static void acpi_thermal_aml_dependency_fix(struct acpi_thermal *tz)
998 acpi_handle handle = tz->device->handle;
999 unsigned long long value;
1002 acpi_evaluate_integer(handle, "_CRT", NULL, &value);
1003 acpi_evaluate_integer(handle, "_HOT", NULL, &value);
1004 acpi_evaluate_integer(handle, "_PSV", NULL, &value);
1005 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1006 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
1009 status = acpi_evaluate_integer(handle, name, NULL, &value);
1010 if (status == AE_NOT_FOUND)
1013 acpi_evaluate_integer(handle, "_TMP", NULL, &value);
1016 static int acpi_thermal_get_info(struct acpi_thermal *tz)
1024 acpi_thermal_aml_dependency_fix(tz);
1026 /* Get trip points [_CRT, _PSV, etc.] (required) */
1027 result = acpi_thermal_get_trip_points(tz);
1031 /* Get temperature [_TMP] (required) */
1032 result = acpi_thermal_get_temperature(tz);
1036 /* Set the cooling mode [_SCP] to active cooling (default) */
1037 result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
1039 tz->flags.cooling_mode = 1;
1041 /* Get default polling frequency [_TZP] (optional) */
1043 tz->polling_frequency = tzp;
1045 acpi_thermal_get_polling_frequency(tz);
1051 * The exact offset between Kelvin and degree Celsius is 273.15. However ACPI
1052 * handles temperature values with a single decimal place. As a consequence,
1053 * some implementations use an offset of 273.1 and others use an offset of
1054 * 273.2. Try to find out which one is being used, to present the most
1055 * accurate and visually appealing number.
1057 * The heuristic below should work for all ACPI thermal zones which have a
1058 * critical trip point with a value being a multiple of 0.5 degree Celsius.
1060 static void acpi_thermal_guess_offset(struct acpi_thermal *tz)
1062 if (tz->trips.critical.flags.valid &&
1063 (tz->trips.critical.temperature % 5) == 1)
1064 tz->kelvin_offset = 2731;
1066 tz->kelvin_offset = 2732;
1069 static void acpi_thermal_check_fn(struct work_struct *work)
1071 struct acpi_thermal *tz = container_of(work, struct acpi_thermal,
1072 thermal_check_work);
1074 if (!tz->tz_enabled)
1077 * In general, it is not sufficient to check the pending bit, because
1078 * subsequent instances of this function may be queued after one of them
1079 * has started running (e.g. if _TMP sleeps). Avoid bailing out if just
1080 * one of them is running, though, because it may have done the actual
1081 * check some time ago, so allow at least one of them to block on the
1082 * mutex while another one is running the update.
1084 if (!atomic_add_unless(&tz->thermal_check_count, -1, 1))
1087 mutex_lock(&tz->thermal_check_lock);
1089 thermal_zone_device_update(tz->thermal_zone);
1091 atomic_inc(&tz->thermal_check_count);
1093 mutex_unlock(&tz->thermal_check_lock);
1096 static int acpi_thermal_add(struct acpi_device *device)
1099 struct acpi_thermal *tz = NULL;
1105 tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
1109 tz->device = device;
1110 strcpy(tz->name, device->pnp.bus_id);
1111 strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1112 strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
1113 device->driver_data = tz;
1115 result = acpi_thermal_get_info(tz);
1119 acpi_thermal_guess_offset(tz);
1121 result = acpi_thermal_register_thermal_zone(tz);
1125 atomic_set(&tz->thermal_check_count, 3);
1126 mutex_init(&tz->thermal_check_lock);
1127 INIT_WORK(&tz->thermal_check_work, acpi_thermal_check_fn);
1129 pr_info(PREFIX "%s [%s] (%ld C)\n", acpi_device_name(device),
1130 acpi_device_bid(device), DECI_KELVIN_TO_CELSIUS(tz->temperature));
1139 static int acpi_thermal_remove(struct acpi_device *device)
1141 struct acpi_thermal *tz = NULL;
1143 if (!device || !acpi_driver_data(device))
1146 flush_workqueue(acpi_thermal_pm_queue);
1147 tz = acpi_driver_data(device);
1149 acpi_thermal_unregister_thermal_zone(tz);
1154 #ifdef CONFIG_PM_SLEEP
1155 static int acpi_thermal_suspend(struct device *dev)
1157 /* Make sure the previously queued thermal check work has been done */
1158 flush_workqueue(acpi_thermal_pm_queue);
1162 static int acpi_thermal_resume(struct device *dev)
1164 struct acpi_thermal *tz;
1165 int i, j, power_state, result;
1170 tz = acpi_driver_data(to_acpi_device(dev));
1174 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1175 if (!(&tz->trips.active[i]))
1177 if (!tz->trips.active[i].flags.valid)
1179 tz->trips.active[i].flags.enabled = 1;
1180 for (j = 0; j < tz->trips.active[i].devices.count; j++) {
1181 result = acpi_bus_update_power(
1182 tz->trips.active[i].devices.handles[j],
1184 if (result || (power_state != ACPI_STATE_D0)) {
1185 tz->trips.active[i].flags.enabled = 0;
1189 tz->state.active |= tz->trips.active[i].flags.enabled;
1192 acpi_queue_thermal_check(tz);
1198 static int thermal_act(const struct dmi_system_id *d) {
1201 pr_notice(PREFIX "%s detected: "
1202 "disabling all active thermal trip points\n", d->ident);
1207 static int thermal_nocrt(const struct dmi_system_id *d) {
1209 pr_notice(PREFIX "%s detected: "
1210 "disabling all critical thermal trip point actions.\n", d->ident);
1214 static int thermal_tzp(const struct dmi_system_id *d) {
1217 pr_notice(PREFIX "%s detected: "
1218 "enabling thermal zone polling\n", d->ident);
1219 tzp = 300; /* 300 dS = 30 Seconds */
1223 static int thermal_psv(const struct dmi_system_id *d) {
1226 pr_notice(PREFIX "%s detected: "
1227 "disabling all passive thermal trip points\n", d->ident);
1233 static struct dmi_system_id thermal_dmi_table[] __initdata = {
1235 * Award BIOS on this AOpen makes thermal control almost worthless.
1236 * http://bugzilla.kernel.org/show_bug.cgi?id=8842
1239 .callback = thermal_act,
1240 .ident = "AOpen i915GMm-HFS",
1242 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1243 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1247 .callback = thermal_psv,
1248 .ident = "AOpen i915GMm-HFS",
1250 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1251 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1255 .callback = thermal_tzp,
1256 .ident = "AOpen i915GMm-HFS",
1258 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1259 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1263 .callback = thermal_nocrt,
1264 .ident = "Gigabyte GA-7ZX",
1266 DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
1267 DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
1273 static int __init acpi_thermal_init(void)
1277 dmi_check_system(thermal_dmi_table);
1280 pr_notice(PREFIX "thermal control disabled\n");
1284 acpi_thermal_pm_queue = create_workqueue("acpi_thermal_pm");
1285 if (!acpi_thermal_pm_queue)
1288 result = acpi_bus_register_driver(&acpi_thermal_driver);
1290 destroy_workqueue(acpi_thermal_pm_queue);
1297 static void __exit acpi_thermal_exit(void)
1299 acpi_bus_unregister_driver(&acpi_thermal_driver);
1300 destroy_workqueue(acpi_thermal_pm_queue);
1305 module_init(acpi_thermal_init);
1306 module_exit(acpi_thermal_exit);