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, int trip_index) 26 { 27 struct thermal_trip *trip = tz->trips ? &tz->trips[trip_index] : NULL; 28 enum thermal_trend trend; 29 30 if (tz->emul_temperature || !tz->ops->get_trend || 31 tz->ops->get_trend(tz, trip, &trend)) { 32 if (tz->temperature > tz->last_temperature) 33 trend = THERMAL_TREND_RAISING; 34 else if (tz->temperature < tz->last_temperature) 35 trend = THERMAL_TREND_DROPPING; 36 else 37 trend = THERMAL_TREND_STABLE; 38 } 39 40 return trend; 41 } 42 43 struct thermal_instance * 44 get_thermal_instance(struct thermal_zone_device *tz, 45 struct thermal_cooling_device *cdev, int trip) 46 { 47 struct thermal_instance *pos = NULL; 48 struct thermal_instance *target_instance = NULL; 49 50 mutex_lock(&tz->lock); 51 mutex_lock(&cdev->lock); 52 53 list_for_each_entry(pos, &tz->thermal_instances, tz_node) { 54 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { 55 target_instance = pos; 56 break; 57 } 58 } 59 60 mutex_unlock(&cdev->lock); 61 mutex_unlock(&tz->lock); 62 63 return target_instance; 64 } 65 EXPORT_SYMBOL(get_thermal_instance); 66 67 /** 68 * __thermal_zone_get_temp() - returns the temperature of a thermal zone 69 * @tz: a valid pointer to a struct thermal_zone_device 70 * @temp: a valid pointer to where to store the resulting temperature. 71 * 72 * When a valid thermal zone reference is passed, it will fetch its 73 * temperature and fill @temp. 74 * 75 * Both tz and tz->ops must be valid pointers when calling this function, 76 * and the tz->ops->get_temp callback must be provided. 77 * The function must be called under tz->lock. 78 * 79 * Return: On success returns 0, an error code otherwise 80 */ 81 int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp) 82 { 83 int ret = -EINVAL; 84 int count; 85 int crit_temp = INT_MAX; 86 struct thermal_trip trip; 87 88 lockdep_assert_held(&tz->lock); 89 90 ret = tz->ops->get_temp(tz, temp); 91 92 if (IS_ENABLED(CONFIG_THERMAL_EMULATION) && tz->emul_temperature) { 93 for (count = 0; count < tz->num_trips; count++) { 94 ret = __thermal_zone_get_trip(tz, count, &trip); 95 if (!ret && trip.type == THERMAL_TRIP_CRITICAL) { 96 crit_temp = trip.temperature; 97 break; 98 } 99 } 100 101 /* 102 * Only allow emulating a temperature when the real temperature 103 * is below the critical temperature so that the emulation code 104 * cannot hide critical conditions. 105 */ 106 if (!ret && *temp < crit_temp) 107 *temp = tz->emul_temperature; 108 } 109 110 if (ret) 111 dev_dbg(&tz->device, "Failed to get temperature: %d\n", ret); 112 113 return ret; 114 } 115 116 /** 117 * thermal_zone_get_temp() - returns the temperature of a thermal zone 118 * @tz: a valid pointer to a struct thermal_zone_device 119 * @temp: a valid pointer to where to store the resulting temperature. 120 * 121 * When a valid thermal zone reference is passed, it will fetch its 122 * temperature and fill @temp. 123 * 124 * Return: On success returns 0, an error code otherwise 125 */ 126 int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp) 127 { 128 int ret; 129 130 if (IS_ERR_OR_NULL(tz)) 131 return -EINVAL; 132 133 mutex_lock(&tz->lock); 134 135 if (!tz->ops->get_temp) { 136 ret = -EINVAL; 137 goto unlock; 138 } 139 140 if (device_is_registered(&tz->device)) 141 ret = __thermal_zone_get_temp(tz, temp); 142 else 143 ret = -ENODEV; 144 145 unlock: 146 mutex_unlock(&tz->lock); 147 148 return ret; 149 } 150 EXPORT_SYMBOL_GPL(thermal_zone_get_temp); 151 152 static void thermal_cdev_set_cur_state(struct thermal_cooling_device *cdev, 153 int target) 154 { 155 if (cdev->ops->set_cur_state(cdev, target)) 156 return; 157 158 thermal_notify_cdev_state_update(cdev->id, target); 159 thermal_cooling_device_stats_update(cdev, target); 160 } 161 162 void __thermal_cdev_update(struct thermal_cooling_device *cdev) 163 { 164 struct thermal_instance *instance; 165 unsigned long target = 0; 166 167 /* Make sure cdev enters the deepest cooling state */ 168 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) { 169 dev_dbg(&cdev->device, "zone%d->target=%lu\n", 170 instance->tz->id, instance->target); 171 if (instance->target == THERMAL_NO_TARGET) 172 continue; 173 if (instance->target > target) 174 target = instance->target; 175 } 176 177 thermal_cdev_set_cur_state(cdev, target); 178 179 trace_cdev_update(cdev, target); 180 dev_dbg(&cdev->device, "set to state %lu\n", target); 181 } 182 183 /** 184 * thermal_cdev_update - update cooling device state if needed 185 * @cdev: pointer to struct thermal_cooling_device 186 * 187 * Update the cooling device state if there is a need. 188 */ 189 void thermal_cdev_update(struct thermal_cooling_device *cdev) 190 { 191 mutex_lock(&cdev->lock); 192 if (!cdev->updated) { 193 __thermal_cdev_update(cdev); 194 cdev->updated = true; 195 } 196 mutex_unlock(&cdev->lock); 197 } 198 199 /** 200 * thermal_zone_get_slope - return the slope attribute of the thermal zone 201 * @tz: thermal zone device with the slope attribute 202 * 203 * Return: If the thermal zone device has a slope attribute, return it, else 204 * return 1. 205 */ 206 int thermal_zone_get_slope(struct thermal_zone_device *tz) 207 { 208 if (tz && tz->tzp) 209 return tz->tzp->slope; 210 return 1; 211 } 212 EXPORT_SYMBOL_GPL(thermal_zone_get_slope); 213 214 /** 215 * thermal_zone_get_offset - return the offset attribute of the thermal zone 216 * @tz: thermal zone device with the offset attribute 217 * 218 * Return: If the thermal zone device has a offset attribute, return it, else 219 * return 0. 220 */ 221 int thermal_zone_get_offset(struct thermal_zone_device *tz) 222 { 223 if (tz && tz->tzp) 224 return tz->tzp->offset; 225 return 0; 226 } 227 EXPORT_SYMBOL_GPL(thermal_zone_get_offset); 228