1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * thermal_helpers.c - helper functions to handle thermal devices 4 * 5 * Copyright (C) 2016 Eduardo Valentin <edubezval@gmail.com> 6 * 7 * Highly based on original thermal_core.c 8 * Copyright (C) 2008 Intel Corp 9 * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com> 10 * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com> 11 */ 12 13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 14 15 #include <linux/device.h> 16 #include <linux/err.h> 17 #include <linux/export.h> 18 #include <linux/slab.h> 19 #include <linux/string.h> 20 #include <linux/sysfs.h> 21 22 #include "thermal_core.h" 23 #include "thermal_trace.h" 24 25 int get_tz_trend(struct thermal_zone_device *tz, const struct thermal_trip *trip) 26 { 27 enum thermal_trend trend; 28 29 if (tz->emul_temperature || !tz->ops.get_trend || 30 tz->ops.get_trend(tz, trip, &trend)) { 31 if (tz->temperature > tz->last_temperature) 32 trend = THERMAL_TREND_RAISING; 33 else if (tz->temperature < tz->last_temperature) 34 trend = THERMAL_TREND_DROPPING; 35 else 36 trend = THERMAL_TREND_STABLE; 37 } 38 39 return trend; 40 } 41 42 static bool thermal_instance_present(struct thermal_zone_device *tz, 43 struct thermal_cooling_device *cdev, 44 const struct thermal_trip *trip) 45 { 46 struct thermal_instance *ti; 47 48 list_for_each_entry(ti, &tz->thermal_instances, tz_node) { 49 if (ti->trip == trip && ti->cdev == cdev) 50 return true; 51 } 52 53 return false; 54 } 55 56 bool thermal_trip_is_bound_to_cdev(struct thermal_zone_device *tz, 57 const struct thermal_trip *trip, 58 struct thermal_cooling_device *cdev) 59 { 60 bool ret; 61 62 mutex_lock(&tz->lock); 63 mutex_lock(&cdev->lock); 64 65 ret = thermal_instance_present(tz, cdev, trip); 66 67 mutex_unlock(&cdev->lock); 68 mutex_unlock(&tz->lock); 69 70 return ret; 71 } 72 EXPORT_SYMBOL_GPL(thermal_trip_is_bound_to_cdev); 73 74 /** 75 * __thermal_zone_get_temp() - returns the temperature of a thermal zone 76 * @tz: a valid pointer to a struct thermal_zone_device 77 * @temp: a valid pointer to where to store the resulting temperature. 78 * 79 * When a valid thermal zone reference is passed, it will fetch its 80 * temperature and fill @temp. 81 * 82 * Both tz and tz->ops must be valid pointers when calling this function, 83 * and the tz->ops.get_temp callback must be provided. 84 * The function must be called under tz->lock. 85 * 86 * Return: On success returns 0, an error code otherwise 87 */ 88 int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp) 89 { 90 const struct thermal_trip_desc *td; 91 int crit_temp = INT_MAX; 92 int ret = -EINVAL; 93 94 lockdep_assert_held(&tz->lock); 95 96 ret = tz->ops.get_temp(tz, temp); 97 98 if (IS_ENABLED(CONFIG_THERMAL_EMULATION) && tz->emul_temperature) { 99 for_each_trip_desc(tz, td) { 100 const struct thermal_trip *trip = &td->trip; 101 102 if (trip->type == THERMAL_TRIP_CRITICAL) { 103 crit_temp = trip->temperature; 104 break; 105 } 106 } 107 108 /* 109 * Only allow emulating a temperature when the real temperature 110 * is below the critical temperature so that the emulation code 111 * cannot hide critical conditions. 112 */ 113 if (!ret && *temp < crit_temp) 114 *temp = tz->emul_temperature; 115 } 116 117 if (ret) 118 dev_dbg(&tz->device, "Failed to get temperature: %d\n", ret); 119 120 return ret; 121 } 122 123 /** 124 * thermal_zone_get_temp() - returns the temperature of a thermal zone 125 * @tz: a valid pointer to a struct thermal_zone_device 126 * @temp: a valid pointer to where to store the resulting temperature. 127 * 128 * When a valid thermal zone reference is passed, it will fetch its 129 * temperature and fill @temp. 130 * 131 * Return: On success returns 0, an error code otherwise 132 */ 133 int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp) 134 { 135 int ret; 136 137 if (IS_ERR_OR_NULL(tz)) 138 return -EINVAL; 139 140 mutex_lock(&tz->lock); 141 142 if (!tz->ops.get_temp) { 143 ret = -EINVAL; 144 goto unlock; 145 } 146 147 ret = __thermal_zone_get_temp(tz, temp); 148 if (!ret && *temp <= THERMAL_TEMP_INVALID) 149 ret = -ENODATA; 150 151 unlock: 152 mutex_unlock(&tz->lock); 153 154 return ret; 155 } 156 EXPORT_SYMBOL_GPL(thermal_zone_get_temp); 157 158 static int thermal_cdev_set_cur_state(struct thermal_cooling_device *cdev, int state) 159 { 160 int ret; 161 162 /* 163 * No check is needed for the ops->set_cur_state as the 164 * registering function checked the ops are correctly set 165 */ 166 ret = cdev->ops->set_cur_state(cdev, state); 167 if (ret) 168 return ret; 169 170 thermal_notify_cdev_state_update(cdev, state); 171 thermal_cooling_device_stats_update(cdev, state); 172 thermal_debug_cdev_state_update(cdev, state); 173 174 return 0; 175 } 176 177 void __thermal_cdev_update(struct thermal_cooling_device *cdev) 178 { 179 struct thermal_instance *instance; 180 unsigned long target = 0; 181 182 /* Make sure cdev enters the deepest cooling state */ 183 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) { 184 if (instance->target == THERMAL_NO_TARGET) 185 continue; 186 if (instance->target > target) 187 target = instance->target; 188 } 189 190 thermal_cdev_set_cur_state(cdev, target); 191 192 trace_cdev_update(cdev, target); 193 dev_dbg(&cdev->device, "set to state %lu\n", target); 194 } 195 196 /** 197 * thermal_cdev_update - update cooling device state if needed 198 * @cdev: pointer to struct thermal_cooling_device 199 * 200 * Update the cooling device state if there is a need. 201 */ 202 void thermal_cdev_update(struct thermal_cooling_device *cdev) 203 { 204 mutex_lock(&cdev->lock); 205 if (!cdev->updated) { 206 __thermal_cdev_update(cdev); 207 cdev->updated = true; 208 } 209 mutex_unlock(&cdev->lock); 210 } 211 212 /** 213 * thermal_zone_get_slope - return the slope attribute of the thermal zone 214 * @tz: thermal zone device with the slope attribute 215 * 216 * Return: If the thermal zone device has a slope attribute, return it, else 217 * return 1. 218 */ 219 int thermal_zone_get_slope(struct thermal_zone_device *tz) 220 { 221 if (tz && tz->tzp) 222 return tz->tzp->slope; 223 return 1; 224 } 225 EXPORT_SYMBOL_GPL(thermal_zone_get_slope); 226 227 /** 228 * thermal_zone_get_offset - return the offset attribute of the thermal zone 229 * @tz: thermal zone device with the offset attribute 230 * 231 * Return: If the thermal zone device has a offset attribute, return it, else 232 * return 0. 233 */ 234 int thermal_zone_get_offset(struct thermal_zone_device *tz) 235 { 236 if (tz && tz->tzp) 237 return tz->tzp->offset; 238 return 0; 239 } 240 EXPORT_SYMBOL_GPL(thermal_zone_get_offset); 241