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_THERMAL_TRIP_PASSIVE (-1)
49 * This exception is thrown out in two cases:
50 * 1.An invalid trip point becomes invalid or a valid trip point becomes invalid
51 * when re-evaluating the AML code.
52 * 2.TODO: Devices listed in _PSL, _ALx, _TZD may change.
53 * We need to re-bind the cooling devices of a thermal zone when this occurs.
55 #define ACPI_THERMAL_TRIPS_EXCEPTION(tz, str) \
57 acpi_handle_info(tz->device->handle, \
58 "ACPI thermal trip point %s changed\n" \
59 "Please report to linux-acpi@vger.kernel.org\n", str); \
63 module_param(act, int, 0644);
64 MODULE_PARM_DESC(act, "Disable or override all lowest active trip points.");
67 module_param(crt, int, 0644);
68 MODULE_PARM_DESC(crt, "Disable or lower all critical trip points.");
71 module_param(tzp, int, 0444);
72 MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.");
75 module_param(off, int, 0);
76 MODULE_PARM_DESC(off, "Set to disable ACPI thermal support.");
79 module_param(psv, int, 0644);
80 MODULE_PARM_DESC(psv, "Disable or override all passive trip points.");
82 static struct workqueue_struct *acpi_thermal_pm_queue;
84 struct acpi_thermal_trip {
85 unsigned long temp_dk;
86 struct acpi_handle_list devices;
89 struct acpi_thermal_passive {
90 struct acpi_thermal_trip trip;
96 struct acpi_thermal_active {
97 struct acpi_thermal_trip trip;
100 struct acpi_thermal_trips {
101 struct acpi_thermal_passive passive;
102 struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
105 struct acpi_thermal {
106 struct acpi_device *device;
108 unsigned long temp_dk;
109 unsigned long last_temp_dk;
110 unsigned long polling_frequency;
112 struct acpi_thermal_trips trips;
113 struct thermal_trip *trip_table;
114 struct thermal_zone_device *thermal_zone;
115 int kelvin_offset; /* in millidegrees */
116 struct work_struct thermal_check_work;
117 struct mutex thermal_check_lock;
118 refcount_t thermal_check_count;
121 /* --------------------------------------------------------------------------
122 Thermal Zone Management
123 -------------------------------------------------------------------------- */
125 static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
127 acpi_status status = AE_OK;
128 unsigned long long tmp;
133 tz->last_temp_dk = tz->temp_dk;
135 status = acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tmp);
136 if (ACPI_FAILURE(status))
141 acpi_handle_debug(tz->device->handle, "Temperature is %lu dK\n",
147 static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
149 acpi_status status = AE_OK;
150 unsigned long long tmp;
155 status = acpi_evaluate_integer(tz->device->handle, "_TZP", NULL, &tmp);
156 if (ACPI_FAILURE(status))
159 tz->polling_frequency = tmp;
160 acpi_handle_debug(tz->device->handle, "Polling frequency is %lu dS\n",
161 tz->polling_frequency);
166 static int acpi_thermal_temp(struct acpi_thermal *tz, int temp_deci_k)
168 if (temp_deci_k == THERMAL_TEMP_INVALID)
169 return THERMAL_TEMP_INVALID;
171 return deci_kelvin_to_millicelsius_with_offset(temp_deci_k,
175 static bool acpi_thermal_trip_valid(struct acpi_thermal_trip *acpi_trip)
177 return acpi_trip->temp_dk != THERMAL_TEMP_INVALID;
180 static int active_trip_index(struct acpi_thermal *tz,
181 struct acpi_thermal_trip *acpi_trip)
183 struct acpi_thermal_active *active;
185 active = container_of(acpi_trip, struct acpi_thermal_active, trip);
186 return active - tz->trips.active;
189 static long get_passive_temp(struct acpi_thermal *tz)
191 unsigned long long tmp;
194 status = acpi_evaluate_integer(tz->device->handle, "_PSV", NULL, &tmp);
195 if (ACPI_FAILURE(status))
196 return THERMAL_TEMP_INVALID;
201 static long get_active_temp(struct acpi_thermal *tz, int index)
203 char method[] = { '_', 'A', 'C', '0' + index, '\0' };
204 unsigned long long tmp;
207 status = acpi_evaluate_integer(tz->device->handle, method, NULL, &tmp);
208 if (ACPI_FAILURE(status))
209 return THERMAL_TEMP_INVALID;
212 * If an override has been provided, apply it so there are no active
213 * trips with thresholds greater than the override.
216 unsigned long long override = celsius_to_deci_kelvin(act);
224 static void acpi_thermal_update_trip(struct acpi_thermal *tz,
225 const struct thermal_trip *trip)
227 struct acpi_thermal_trip *acpi_trip = trip->priv;
229 if (trip->type == THERMAL_TRIP_PASSIVE) {
233 acpi_trip->temp_dk = get_passive_temp(tz);
235 int index = active_trip_index(tz, acpi_trip);
237 acpi_trip->temp_dk = get_active_temp(tz, index);
240 if (!acpi_thermal_trip_valid(acpi_trip))
241 ACPI_THERMAL_TRIPS_EXCEPTION(tz, "state");
244 static bool update_trip_devices(struct acpi_thermal *tz,
245 struct acpi_thermal_trip *acpi_trip,
246 int index, bool compare)
248 struct acpi_handle_list devices = { 0 };
249 char method[] = "_PSL";
252 if (index != ACPI_THERMAL_TRIP_PASSIVE) {
255 method[3] = '0' + index;
258 status = acpi_evaluate_reference(tz->device->handle, method, NULL, &devices);
259 if (ACPI_FAILURE(status)) {
260 acpi_handle_info(tz->device->handle, "%s evaluation failure\n", method);
264 if (acpi_handle_list_equal(&acpi_trip->devices, &devices)) {
265 acpi_handle_list_free(&devices);
270 ACPI_THERMAL_TRIPS_EXCEPTION(tz, "device");
272 acpi_handle_list_replace(&acpi_trip->devices, &devices);
276 static void acpi_thermal_update_trip_devices(struct acpi_thermal *tz,
277 const struct thermal_trip *trip)
279 struct acpi_thermal_trip *acpi_trip = trip->priv;
280 int index = trip->type == THERMAL_TRIP_PASSIVE ?
281 ACPI_THERMAL_TRIP_PASSIVE : active_trip_index(tz, acpi_trip);
283 if (update_trip_devices(tz, acpi_trip, index, true))
286 acpi_trip->temp_dk = THERMAL_TEMP_INVALID;
287 ACPI_THERMAL_TRIPS_EXCEPTION(tz, "state");
290 struct adjust_trip_data {
291 struct acpi_thermal *tz;
295 static int acpi_thermal_adjust_trip(struct thermal_trip *trip, void *data)
297 struct acpi_thermal_trip *acpi_trip = trip->priv;
298 struct adjust_trip_data *atd = data;
299 struct acpi_thermal *tz = atd->tz;
301 if (!acpi_trip || !acpi_thermal_trip_valid(acpi_trip))
304 if (atd->event == ACPI_THERMAL_NOTIFY_THRESHOLDS)
305 acpi_thermal_update_trip(tz, trip);
307 acpi_thermal_update_trip_devices(tz, trip);
309 if (acpi_thermal_trip_valid(acpi_trip))
310 trip->temperature = acpi_thermal_temp(tz, acpi_trip->temp_dk);
312 trip->temperature = THERMAL_TEMP_INVALID;
317 static void acpi_queue_thermal_check(struct acpi_thermal *tz)
319 if (!work_pending(&tz->thermal_check_work))
320 queue_work(acpi_thermal_pm_queue, &tz->thermal_check_work);
323 static void acpi_thermal_trips_update(struct acpi_thermal *tz, u32 event)
325 struct adjust_trip_data atd = { .tz = tz, .event = event };
326 struct acpi_device *adev = tz->device;
329 * Use thermal_zone_for_each_trip() to carry out the trip points
330 * update, so as to protect thermal_get_trend() from getting stale
331 * trip point temperatures and to prevent thermal_zone_device_update()
332 * invoked from acpi_thermal_check_fn() from producing inconsistent
335 thermal_zone_for_each_trip(tz->thermal_zone,
336 acpi_thermal_adjust_trip, &atd);
337 acpi_queue_thermal_check(tz);
338 acpi_bus_generate_netlink_event(adev->pnp.device_class,
339 dev_name(&adev->dev), event, 0);
342 static long acpi_thermal_get_critical_trip(struct acpi_thermal *tz)
344 unsigned long long tmp;
348 tmp = celsius_to_deci_kelvin(crt);
352 acpi_handle_debug(tz->device->handle, "Critical threshold disabled\n");
353 return THERMAL_TEMP_INVALID;
356 status = acpi_evaluate_integer(tz->device->handle, "_CRT", NULL, &tmp);
357 if (ACPI_FAILURE(status)) {
358 acpi_handle_debug(tz->device->handle, "No critical threshold\n");
359 return THERMAL_TEMP_INVALID;
363 * Below zero (Celsius) values clearly aren't right for sure,
364 * so discard them as invalid.
366 pr_info(FW_BUG "Invalid critical threshold (%llu)\n", tmp);
367 return THERMAL_TEMP_INVALID;
371 acpi_handle_debug(tz->device->handle, "Critical threshold [%llu]\n", tmp);
375 static long acpi_thermal_get_hot_trip(struct acpi_thermal *tz)
377 unsigned long long tmp;
380 status = acpi_evaluate_integer(tz->device->handle, "_HOT", NULL, &tmp);
381 if (ACPI_FAILURE(status)) {
382 acpi_handle_debug(tz->device->handle, "No hot threshold\n");
383 return THERMAL_TEMP_INVALID;
386 acpi_handle_debug(tz->device->handle, "Hot threshold [%llu]\n", tmp);
390 static bool passive_trip_params_init(struct acpi_thermal *tz)
392 unsigned long long tmp;
395 status = acpi_evaluate_integer(tz->device->handle, "_TC1", NULL, &tmp);
396 if (ACPI_FAILURE(status))
399 tz->trips.passive.tc1 = tmp;
401 status = acpi_evaluate_integer(tz->device->handle, "_TC2", NULL, &tmp);
402 if (ACPI_FAILURE(status))
405 tz->trips.passive.tc2 = tmp;
407 status = acpi_evaluate_integer(tz->device->handle, "_TSP", NULL, &tmp);
408 if (ACPI_FAILURE(status))
411 tz->trips.passive.tsp = tmp;
416 static bool acpi_thermal_init_trip(struct acpi_thermal *tz, int index)
418 struct acpi_thermal_trip *acpi_trip;
421 if (index == ACPI_THERMAL_TRIP_PASSIVE) {
422 acpi_trip = &tz->trips.passive.trip;
427 if (!passive_trip_params_init(tz))
430 temp = psv > 0 ? celsius_to_deci_kelvin(psv) :
431 get_passive_temp(tz);
433 acpi_trip = &tz->trips.active[index].trip;
438 temp = get_active_temp(tz, index);
441 if (temp == THERMAL_TEMP_INVALID)
444 if (!update_trip_devices(tz, acpi_trip, index, false))
447 acpi_trip->temp_dk = temp;
451 acpi_trip->temp_dk = THERMAL_TEMP_INVALID;
455 static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
457 unsigned int count = 0;
460 if (acpi_thermal_init_trip(tz, ACPI_THERMAL_TRIP_PASSIVE))
463 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
464 if (acpi_thermal_init_trip(tz, i))
471 while (++i < ACPI_THERMAL_MAX_ACTIVE)
472 tz->trips.active[i].trip.temp_dk = THERMAL_TEMP_INVALID;
477 /* sys I/F for generic thermal sysfs support */
479 static int thermal_get_temp(struct thermal_zone_device *thermal, int *temp)
481 struct acpi_thermal *tz = thermal_zone_device_priv(thermal);
487 result = acpi_thermal_get_temperature(tz);
491 *temp = deci_kelvin_to_millicelsius_with_offset(tz->temp_dk,
496 static int thermal_get_trend(struct thermal_zone_device *thermal,
497 const struct thermal_trip *trip,
498 enum thermal_trend *trend)
500 struct acpi_thermal *tz = thermal_zone_device_priv(thermal);
501 struct acpi_thermal_trip *acpi_trip;
507 acpi_trip = trip->priv;
508 if (!acpi_trip || !acpi_thermal_trip_valid(acpi_trip))
511 switch (trip->type) {
512 case THERMAL_TRIP_PASSIVE:
513 t = tz->trips.passive.tc1 * (tz->temp_dk -
515 tz->trips.passive.tc2 * (tz->temp_dk -
518 *trend = THERMAL_TREND_RAISING;
520 *trend = THERMAL_TREND_DROPPING;
522 *trend = THERMAL_TREND_STABLE;
526 case THERMAL_TRIP_ACTIVE:
527 t = acpi_thermal_temp(tz, tz->temp_dk);
528 if (t <= trip->temperature)
531 *trend = THERMAL_TREND_RAISING;
542 static void acpi_thermal_zone_device_hot(struct thermal_zone_device *thermal)
544 struct acpi_thermal *tz = thermal_zone_device_priv(thermal);
546 acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
547 dev_name(&tz->device->dev),
548 ACPI_THERMAL_NOTIFY_HOT, 1);
551 static void acpi_thermal_zone_device_critical(struct thermal_zone_device *thermal)
553 struct acpi_thermal *tz = thermal_zone_device_priv(thermal);
555 acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
556 dev_name(&tz->device->dev),
557 ACPI_THERMAL_NOTIFY_CRITICAL, 1);
559 thermal_zone_device_critical(thermal);
562 struct acpi_thermal_bind_data {
563 struct thermal_zone_device *thermal;
564 struct thermal_cooling_device *cdev;
568 static int bind_unbind_cdev_cb(struct thermal_trip *trip, void *arg)
570 struct acpi_thermal_trip *acpi_trip = trip->priv;
571 struct acpi_thermal_bind_data *bd = arg;
572 struct thermal_zone_device *thermal = bd->thermal;
573 struct thermal_cooling_device *cdev = bd->cdev;
574 struct acpi_device *cdev_adev = cdev->devdata;
577 /* Skip critical and hot trips. */
581 for (i = 0; i < acpi_trip->devices.count; i++) {
582 acpi_handle handle = acpi_trip->devices.handles[i];
583 struct acpi_device *adev = acpi_fetch_acpi_dev(handle);
585 if (adev != cdev_adev)
591 ret = thermal_bind_cdev_to_trip(thermal, trip, cdev,
594 THERMAL_WEIGHT_DEFAULT);
598 thermal_unbind_cdev_from_trip(thermal, trip, cdev);
605 static int acpi_thermal_bind_unbind_cdev(struct thermal_zone_device *thermal,
606 struct thermal_cooling_device *cdev,
609 struct acpi_thermal_bind_data bd = {
610 .thermal = thermal, .cdev = cdev, .bind = bind
613 return for_each_thermal_trip(thermal, bind_unbind_cdev_cb, &bd);
617 acpi_thermal_bind_cooling_device(struct thermal_zone_device *thermal,
618 struct thermal_cooling_device *cdev)
620 return acpi_thermal_bind_unbind_cdev(thermal, cdev, true);
624 acpi_thermal_unbind_cooling_device(struct thermal_zone_device *thermal,
625 struct thermal_cooling_device *cdev)
627 return acpi_thermal_bind_unbind_cdev(thermal, cdev, false);
630 static struct thermal_zone_device_ops acpi_thermal_zone_ops = {
631 .bind = acpi_thermal_bind_cooling_device,
632 .unbind = acpi_thermal_unbind_cooling_device,
633 .get_temp = thermal_get_temp,
634 .get_trend = thermal_get_trend,
635 .hot = acpi_thermal_zone_device_hot,
636 .critical = acpi_thermal_zone_device_critical,
639 static int acpi_thermal_zone_sysfs_add(struct acpi_thermal *tz)
641 struct device *tzdev = thermal_zone_device(tz->thermal_zone);
644 ret = sysfs_create_link(&tz->device->dev.kobj,
645 &tzdev->kobj, "thermal_zone");
649 ret = sysfs_create_link(&tzdev->kobj,
650 &tz->device->dev.kobj, "device");
652 sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
657 static void acpi_thermal_zone_sysfs_remove(struct acpi_thermal *tz)
659 struct device *tzdev = thermal_zone_device(tz->thermal_zone);
661 sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
662 sysfs_remove_link(&tzdev->kobj, "device");
665 static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz,
666 unsigned int trip_count,
671 tz->thermal_zone = thermal_zone_device_register_with_trips("acpitz",
675 &acpi_thermal_zone_ops,
678 tz->polling_frequency * 100);
679 if (IS_ERR(tz->thermal_zone))
680 return PTR_ERR(tz->thermal_zone);
682 result = acpi_thermal_zone_sysfs_add(tz);
686 result = thermal_zone_device_enable(tz->thermal_zone);
690 dev_info(&tz->device->dev, "registered as thermal_zone%d\n",
691 thermal_zone_device_id(tz->thermal_zone));
696 acpi_thermal_zone_sysfs_remove(tz);
698 thermal_zone_device_unregister(tz->thermal_zone);
703 static void acpi_thermal_unregister_thermal_zone(struct acpi_thermal *tz)
705 thermal_zone_device_disable(tz->thermal_zone);
706 acpi_thermal_zone_sysfs_remove(tz);
707 thermal_zone_device_unregister(tz->thermal_zone);
708 tz->thermal_zone = NULL;
712 /* --------------------------------------------------------------------------
714 -------------------------------------------------------------------------- */
716 static void acpi_thermal_notify(acpi_handle handle, u32 event, void *data)
718 struct acpi_device *device = data;
719 struct acpi_thermal *tz = acpi_driver_data(device);
725 case ACPI_THERMAL_NOTIFY_TEMPERATURE:
726 acpi_queue_thermal_check(tz);
728 case ACPI_THERMAL_NOTIFY_THRESHOLDS:
729 case ACPI_THERMAL_NOTIFY_DEVICES:
730 acpi_thermal_trips_update(tz, event);
733 acpi_handle_debug(device->handle, "Unsupported event [0x%x]\n",
740 * On some platforms, the AML code has dependency about
741 * the evaluating order of _TMP and _CRT/_HOT/_PSV/_ACx.
742 * 1. On HP Pavilion G4-1016tx, _TMP must be invoked after
743 * /_CRT/_HOT/_PSV/_ACx, or else system will be power off.
744 * 2. On HP Compaq 6715b/6715s, the return value of _PSV is 0
745 * if _TMP has never been evaluated.
747 * As this dependency is totally transparent to OS, evaluate
748 * all of them once, in the order of _CRT/_HOT/_PSV/_ACx,
749 * _TMP, before they are actually used.
751 static void acpi_thermal_aml_dependency_fix(struct acpi_thermal *tz)
753 acpi_handle handle = tz->device->handle;
754 unsigned long long value;
757 acpi_evaluate_integer(handle, "_CRT", NULL, &value);
758 acpi_evaluate_integer(handle, "_HOT", NULL, &value);
759 acpi_evaluate_integer(handle, "_PSV", NULL, &value);
760 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
761 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
764 status = acpi_evaluate_integer(handle, name, NULL, &value);
765 if (status == AE_NOT_FOUND)
768 acpi_evaluate_integer(handle, "_TMP", NULL, &value);
772 * The exact offset between Kelvin and degree Celsius is 273.15. However ACPI
773 * handles temperature values with a single decimal place. As a consequence,
774 * some implementations use an offset of 273.1 and others use an offset of
775 * 273.2. Try to find out which one is being used, to present the most
776 * accurate and visually appealing number.
778 * The heuristic below should work for all ACPI thermal zones which have a
779 * critical trip point with a value being a multiple of 0.5 degree Celsius.
781 static void acpi_thermal_guess_offset(struct acpi_thermal *tz, long crit_temp)
783 if (crit_temp != THERMAL_TEMP_INVALID && crit_temp % 5 == 1)
784 tz->kelvin_offset = 273100;
786 tz->kelvin_offset = 273200;
789 static void acpi_thermal_check_fn(struct work_struct *work)
791 struct acpi_thermal *tz = container_of(work, struct acpi_thermal,
795 * In general, it is not sufficient to check the pending bit, because
796 * subsequent instances of this function may be queued after one of them
797 * has started running (e.g. if _TMP sleeps). Avoid bailing out if just
798 * one of them is running, though, because it may have done the actual
799 * check some time ago, so allow at least one of them to block on the
800 * mutex while another one is running the update.
802 if (!refcount_dec_not_one(&tz->thermal_check_count))
805 mutex_lock(&tz->thermal_check_lock);
807 thermal_zone_device_update(tz->thermal_zone, THERMAL_EVENT_UNSPECIFIED);
809 refcount_inc(&tz->thermal_check_count);
811 mutex_unlock(&tz->thermal_check_lock);
814 static void acpi_thermal_free_thermal_zone(struct acpi_thermal *tz)
818 acpi_handle_list_free(&tz->trips.passive.trip.devices);
819 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
820 acpi_handle_list_free(&tz->trips.active[i].trip.devices);
825 static int acpi_thermal_add(struct acpi_device *device)
827 struct acpi_thermal_trip *acpi_trip;
828 struct thermal_trip *trip;
829 struct acpi_thermal *tz;
830 unsigned int trip_count;
831 int crit_temp, hot_temp;
832 int passive_delay = 0;
839 tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
844 strcpy(tz->name, device->pnp.bus_id);
845 strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
846 strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
847 device->driver_data = tz;
849 acpi_thermal_aml_dependency_fix(tz);
851 /* Get trip points [_CRT, _PSV, etc.] (required). */
852 trip_count = acpi_thermal_get_trip_points(tz);
854 crit_temp = acpi_thermal_get_critical_trip(tz);
855 if (crit_temp != THERMAL_TEMP_INVALID)
858 hot_temp = acpi_thermal_get_hot_trip(tz);
859 if (hot_temp != THERMAL_TEMP_INVALID)
863 pr_warn(FW_BUG "No valid trip points!\n");
868 /* Get temperature [_TMP] (required). */
869 result = acpi_thermal_get_temperature(tz);
873 /* Set the cooling mode [_SCP] to active cooling. */
874 acpi_execute_simple_method(tz->device->handle, "_SCP",
875 ACPI_THERMAL_MODE_ACTIVE);
877 /* Determine the default polling frequency [_TZP]. */
879 tz->polling_frequency = tzp;
881 acpi_thermal_get_polling_frequency(tz);
883 acpi_thermal_guess_offset(tz, crit_temp);
885 trip = kcalloc(trip_count, sizeof(*trip), GFP_KERNEL);
891 tz->trip_table = trip;
893 if (crit_temp != THERMAL_TEMP_INVALID) {
894 trip->type = THERMAL_TRIP_CRITICAL;
895 trip->temperature = acpi_thermal_temp(tz, crit_temp);
899 if (hot_temp != THERMAL_TEMP_INVALID) {
900 trip->type = THERMAL_TRIP_HOT;
901 trip->temperature = acpi_thermal_temp(tz, hot_temp);
905 acpi_trip = &tz->trips.passive.trip;
906 if (acpi_thermal_trip_valid(acpi_trip)) {
907 passive_delay = tz->trips.passive.tsp * 100;
909 trip->type = THERMAL_TRIP_PASSIVE;
910 trip->temperature = acpi_thermal_temp(tz, acpi_trip->temp_dk);
911 trip->priv = acpi_trip;
915 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
916 acpi_trip = &tz->trips.active[i].trip;
918 if (!acpi_thermal_trip_valid(acpi_trip))
921 trip->type = THERMAL_TRIP_ACTIVE;
922 trip->temperature = acpi_thermal_temp(tz, acpi_trip->temp_dk);
923 trip->priv = acpi_trip;
927 result = acpi_thermal_register_thermal_zone(tz, trip_count, passive_delay);
931 refcount_set(&tz->thermal_check_count, 3);
932 mutex_init(&tz->thermal_check_lock);
933 INIT_WORK(&tz->thermal_check_work, acpi_thermal_check_fn);
935 pr_info("%s [%s] (%ld C)\n", acpi_device_name(device),
936 acpi_device_bid(device), deci_kelvin_to_celsius(tz->temp_dk));
938 result = acpi_dev_install_notify_handler(device, ACPI_DEVICE_NOTIFY,
939 acpi_thermal_notify, device);
946 flush_workqueue(acpi_thermal_pm_queue);
947 acpi_thermal_unregister_thermal_zone(tz);
949 kfree(tz->trip_table);
951 acpi_thermal_free_thermal_zone(tz);
956 static void acpi_thermal_remove(struct acpi_device *device)
958 struct acpi_thermal *tz;
960 if (!device || !acpi_driver_data(device))
963 tz = acpi_driver_data(device);
965 acpi_dev_remove_notify_handler(device, ACPI_DEVICE_NOTIFY,
966 acpi_thermal_notify);
968 flush_workqueue(acpi_thermal_pm_queue);
969 acpi_thermal_unregister_thermal_zone(tz);
970 kfree(tz->trip_table);
971 acpi_thermal_free_thermal_zone(tz);
974 #ifdef CONFIG_PM_SLEEP
975 static int acpi_thermal_suspend(struct device *dev)
977 /* Make sure the previously queued thermal check work has been done */
978 flush_workqueue(acpi_thermal_pm_queue);
982 static int acpi_thermal_resume(struct device *dev)
984 struct acpi_thermal *tz;
985 int i, j, power_state;
990 tz = acpi_driver_data(to_acpi_device(dev));
994 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
995 struct acpi_thermal_trip *acpi_trip = &tz->trips.active[i].trip;
997 if (!acpi_thermal_trip_valid(acpi_trip))
1000 for (j = 0; j < acpi_trip->devices.count; j++) {
1001 acpi_bus_update_power(acpi_trip->devices.handles[j],
1006 acpi_queue_thermal_check(tz);
1011 #define acpi_thermal_suspend NULL
1012 #define acpi_thermal_resume NULL
1014 static SIMPLE_DEV_PM_OPS(acpi_thermal_pm, acpi_thermal_suspend, acpi_thermal_resume);
1016 static const struct acpi_device_id thermal_device_ids[] = {
1017 {ACPI_THERMAL_HID, 0},
1020 MODULE_DEVICE_TABLE(acpi, thermal_device_ids);
1022 static struct acpi_driver acpi_thermal_driver = {
1024 .class = ACPI_THERMAL_CLASS,
1025 .ids = thermal_device_ids,
1027 .add = acpi_thermal_add,
1028 .remove = acpi_thermal_remove,
1030 .drv.pm = &acpi_thermal_pm,
1033 static int thermal_act(const struct dmi_system_id *d)
1036 pr_notice("%s detected: disabling all active thermal trip points\n",
1043 static int thermal_nocrt(const struct dmi_system_id *d)
1045 pr_notice("%s detected: disabling all critical thermal trip point actions.\n",
1051 static int thermal_tzp(const struct dmi_system_id *d)
1054 pr_notice("%s detected: enabling thermal zone polling\n",
1056 tzp = 300; /* 300 dS = 30 Seconds */
1061 static int thermal_psv(const struct dmi_system_id *d)
1064 pr_notice("%s detected: disabling all passive thermal trip points\n",
1071 static const struct dmi_system_id thermal_dmi_table[] __initconst = {
1073 * Award BIOS on this AOpen makes thermal control almost worthless.
1074 * http://bugzilla.kernel.org/show_bug.cgi?id=8842
1077 .callback = thermal_act,
1078 .ident = "AOpen i915GMm-HFS",
1080 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1081 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1085 .callback = thermal_psv,
1086 .ident = "AOpen i915GMm-HFS",
1088 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1089 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1093 .callback = thermal_tzp,
1094 .ident = "AOpen i915GMm-HFS",
1096 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1097 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1101 .callback = thermal_nocrt,
1102 .ident = "Gigabyte GA-7ZX",
1104 DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
1105 DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
1111 static int __init acpi_thermal_init(void)
1115 dmi_check_system(thermal_dmi_table);
1118 pr_notice("thermal control disabled\n");
1122 acpi_thermal_pm_queue = alloc_workqueue("acpi_thermal_pm",
1123 WQ_HIGHPRI | WQ_MEM_RECLAIM, 0);
1124 if (!acpi_thermal_pm_queue)
1127 result = acpi_bus_register_driver(&acpi_thermal_driver);
1129 destroy_workqueue(acpi_thermal_pm_queue);
1136 static void __exit acpi_thermal_exit(void)
1138 acpi_bus_unregister_driver(&acpi_thermal_driver);
1139 destroy_workqueue(acpi_thermal_pm_queue);
1142 module_init(acpi_thermal_init);
1143 module_exit(acpi_thermal_exit);
1145 MODULE_AUTHOR("Paul Diefenbaugh");
1146 MODULE_DESCRIPTION("ACPI Thermal Zone Driver");
1147 MODULE_LICENSE("GPL");