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