1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2008 Intel Corp 4 * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com> 5 * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com> 6 * Copyright 2022 Linaro Limited 7 * 8 * Thermal trips handling 9 */ 10 #include "thermal_core.h" 11 12 static const char *trip_type_names[] = { 13 [THERMAL_TRIP_ACTIVE] = "active", 14 [THERMAL_TRIP_PASSIVE] = "passive", 15 [THERMAL_TRIP_HOT] = "hot", 16 [THERMAL_TRIP_CRITICAL] = "critical", 17 }; 18 19 const char *thermal_trip_type_name(enum thermal_trip_type trip_type) 20 { 21 if (trip_type < THERMAL_TRIP_ACTIVE || trip_type > THERMAL_TRIP_CRITICAL) 22 return "unknown"; 23 24 return trip_type_names[trip_type]; 25 } 26 27 int for_each_thermal_trip(struct thermal_zone_device *tz, 28 int (*cb)(struct thermal_trip *, void *), 29 void *data) 30 { 31 struct thermal_trip_desc *td; 32 int ret; 33 34 for_each_trip_desc(tz, td) { 35 ret = cb(&td->trip, data); 36 if (ret) 37 return ret; 38 } 39 40 return 0; 41 } 42 EXPORT_SYMBOL_GPL(for_each_thermal_trip); 43 44 int thermal_zone_for_each_trip(struct thermal_zone_device *tz, 45 int (*cb)(struct thermal_trip *, void *), 46 void *data) 47 { 48 int ret; 49 50 mutex_lock(&tz->lock); 51 ret = for_each_thermal_trip(tz, cb, data); 52 mutex_unlock(&tz->lock); 53 54 return ret; 55 } 56 EXPORT_SYMBOL_GPL(thermal_zone_for_each_trip); 57 58 void thermal_zone_set_trips(struct thermal_zone_device *tz, int low, int high) 59 { 60 int ret; 61 62 lockdep_assert_held(&tz->lock); 63 64 if (!tz->ops.set_trips) 65 return; 66 67 /* No need to change trip points */ 68 if (tz->prev_low_trip == low && tz->prev_high_trip == high) 69 return; 70 71 tz->prev_low_trip = low; 72 tz->prev_high_trip = high; 73 74 dev_dbg(&tz->device, 75 "new temperature boundaries: %d < x < %d\n", low, high); 76 77 /* 78 * Set a temperature window. When this window is left the driver 79 * must inform the thermal core via thermal_zone_device_update. 80 */ 81 ret = tz->ops.set_trips(tz, low, high); 82 if (ret) 83 dev_err(&tz->device, "Failed to set trips: %d\n", ret); 84 } 85 86 int thermal_zone_trip_id(const struct thermal_zone_device *tz, 87 const struct thermal_trip *trip) 88 { 89 /* 90 * Assume the trip to be located within the bounds of the thermal 91 * zone's trips[] table. 92 */ 93 return trip_to_trip_desc(trip) - tz->trips; 94 } 95 96 void thermal_zone_set_trip_hyst(struct thermal_zone_device *tz, 97 struct thermal_trip *trip, int hyst) 98 { 99 WRITE_ONCE(trip->hysteresis, hyst); 100 thermal_notify_tz_trip_change(tz, trip); 101 } 102 103 void thermal_zone_set_trip_temp(struct thermal_zone_device *tz, 104 struct thermal_trip *trip, int temp) 105 { 106 if (trip->temperature == temp) 107 return; 108 109 WRITE_ONCE(trip->temperature, temp); 110 thermal_notify_tz_trip_change(tz, trip); 111 112 if (temp == THERMAL_TEMP_INVALID) { 113 struct thermal_trip_desc *td = trip_to_trip_desc(trip); 114 115 if (tz->temperature >= td->threshold) { 116 /* 117 * The trip has been crossed on the way up, so some 118 * adjustments are needed to compensate for the lack 119 * of it going forward. 120 */ 121 if (trip->type == THERMAL_TRIP_PASSIVE) { 122 tz->passive--; 123 WARN_ON_ONCE(tz->passive < 0); 124 } 125 thermal_zone_trip_down(tz, trip); 126 } 127 /* 128 * Invalidate the threshold to avoid triggering a spurious 129 * trip crossing notification when the trip becomes valid. 130 */ 131 td->threshold = INT_MAX; 132 } 133 } 134 EXPORT_SYMBOL_GPL(thermal_zone_set_trip_temp); 135