Lina Iyer | 7e3c038 | 2018-05-07 11:52:29 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Eduardo Valentin | cd221c7 | 2016-11-07 21:09:04 -0800 | [diff] [blame] | 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> |
Eduardo Valentin | cd221c7 | 2016-11-07 21:09:04 -0800 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 14 | |
| 15 | #include <linux/sysfs.h> |
| 16 | #include <linux/device.h> |
| 17 | #include <linux/err.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/string.h> |
| 20 | |
| 21 | #include <trace/events/thermal.h> |
| 22 | |
| 23 | #include "thermal_core.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 | EXPORT_SYMBOL(get_tz_trend); |
| 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 | * Return: On success returns 0, an error code otherwise |
| 76 | */ |
| 77 | int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp) |
| 78 | { |
| 79 | int ret = -EINVAL; |
| 80 | int count; |
| 81 | int crit_temp = INT_MAX; |
| 82 | enum thermal_trip_type type; |
| 83 | |
| 84 | if (!tz || IS_ERR(tz) || !tz->ops->get_temp) |
| 85 | goto exit; |
| 86 | |
| 87 | mutex_lock(&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->trips; count++) { |
| 93 | ret = tz->ops->get_trip_type(tz, count, &type); |
| 94 | if (!ret && type == THERMAL_TRIP_CRITICAL) { |
| 95 | ret = tz->ops->get_trip_temp(tz, count, |
| 96 | &crit_temp); |
| 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 | } |
Ram Chandrasekar | 8a12149 | 2018-10-08 14:49:17 -0600 | [diff] [blame] | 109 | trace_thermal_query_temp(tz, *temp); |
Eduardo Valentin | cd221c7 | 2016-11-07 21:09:04 -0800 | [diff] [blame] | 110 | mutex_unlock(&tz->lock); |
| 111 | exit: |
| 112 | return ret; |
| 113 | } |
| 114 | EXPORT_SYMBOL_GPL(thermal_zone_get_temp); |
| 115 | |
| 116 | void thermal_zone_set_trips(struct thermal_zone_device *tz) |
| 117 | { |
| 118 | int low = -INT_MAX; |
| 119 | int high = INT_MAX; |
| 120 | int trip_temp, hysteresis; |
| 121 | int i, ret; |
| 122 | |
| 123 | mutex_lock(&tz->lock); |
| 124 | |
| 125 | if (!tz->ops->set_trips || !tz->ops->get_trip_hyst) |
| 126 | goto exit; |
| 127 | |
| 128 | for (i = 0; i < tz->trips; i++) { |
| 129 | int trip_low; |
| 130 | |
| 131 | tz->ops->get_trip_temp(tz, i, &trip_temp); |
| 132 | tz->ops->get_trip_hyst(tz, i, &hysteresis); |
| 133 | |
| 134 | trip_low = trip_temp - hysteresis; |
| 135 | |
| 136 | if (trip_low < tz->temperature && trip_low > low) |
| 137 | low = trip_low; |
| 138 | |
| 139 | if (trip_temp > tz->temperature && trip_temp < high) |
| 140 | high = trip_temp; |
| 141 | } |
| 142 | |
Eduardo Valentin | cd221c7 | 2016-11-07 21:09:04 -0800 | [diff] [blame] | 143 | tz->prev_low_trip = low; |
| 144 | tz->prev_high_trip = high; |
| 145 | |
| 146 | dev_dbg(&tz->device, |
| 147 | "new temperature boundaries: %d < x < %d\n", low, high); |
| 148 | |
| 149 | /* |
| 150 | * Set a temperature window. When this window is left the driver |
| 151 | * must inform the thermal core via thermal_zone_device_update. |
| 152 | */ |
| 153 | ret = tz->ops->set_trips(tz, low, high); |
| 154 | if (ret) |
| 155 | dev_err(&tz->device, "Failed to set trips: %d\n", ret); |
Ram Chandrasekar | 8a12149 | 2018-10-08 14:49:17 -0600 | [diff] [blame] | 156 | trace_thermal_set_trip(tz); |
Eduardo Valentin | cd221c7 | 2016-11-07 21:09:04 -0800 | [diff] [blame] | 157 | |
| 158 | exit: |
| 159 | mutex_unlock(&tz->lock); |
| 160 | } |
| 161 | EXPORT_SYMBOL_GPL(thermal_zone_set_trips); |
| 162 | |
| 163 | void thermal_cdev_update(struct thermal_cooling_device *cdev) |
| 164 | { |
| 165 | struct thermal_instance *instance; |
Ram Chandrasekar | 8a12149 | 2018-10-08 14:49:17 -0600 | [diff] [blame] | 166 | unsigned long current_target = 0, min_target = ULONG_MAX; |
Eduardo Valentin | cd221c7 | 2016-11-07 21:09:04 -0800 | [diff] [blame] | 167 | |
| 168 | mutex_lock(&cdev->lock); |
| 169 | /* cooling device is updated*/ |
| 170 | if (cdev->updated) { |
| 171 | mutex_unlock(&cdev->lock); |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | /* Make sure cdev enters the deepest cooling state */ |
Ram Chandrasekar | 8a12149 | 2018-10-08 14:49:17 -0600 | [diff] [blame] | 176 | current_target = cdev->sysfs_cur_state_req; |
| 177 | min_target = cdev->sysfs_min_state_req; |
Eduardo Valentin | cd221c7 | 2016-11-07 21:09:04 -0800 | [diff] [blame] | 178 | list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) { |
| 179 | dev_dbg(&cdev->device, "zone%d->target=%lu\n", |
| 180 | instance->tz->id, instance->target); |
| 181 | if (instance->target == THERMAL_NO_TARGET) |
| 182 | continue; |
Ram Chandrasekar | 8a12149 | 2018-10-08 14:49:17 -0600 | [diff] [blame] | 183 | if (instance->tz->governor->min_state_throttle) { |
| 184 | if (instance->target < min_target) |
| 185 | min_target = instance->target; |
| 186 | } else { |
| 187 | if (instance->target > current_target) |
| 188 | current_target = instance->target; |
| 189 | } |
Eduardo Valentin | cd221c7 | 2016-11-07 21:09:04 -0800 | [diff] [blame] | 190 | } |
Viresh Kumar | 8ea2295 | 2018-04-02 16:26:25 +0530 | [diff] [blame] | 191 | |
Ram Chandrasekar | 8a12149 | 2018-10-08 14:49:17 -0600 | [diff] [blame] | 192 | trace_cdev_update_start(cdev); |
| 193 | if (!cdev->ops->set_cur_state(cdev, current_target)) |
| 194 | thermal_cooling_device_stats_update(cdev, current_target); |
| 195 | if (cdev->ops->set_min_state) |
| 196 | cdev->ops->set_min_state(cdev, min_target); |
Viresh Kumar | 8ea2295 | 2018-04-02 16:26:25 +0530 | [diff] [blame] | 197 | |
Eduardo Valentin | cd221c7 | 2016-11-07 21:09:04 -0800 | [diff] [blame] | 198 | cdev->updated = true; |
| 199 | mutex_unlock(&cdev->lock); |
Ram Chandrasekar | 8a12149 | 2018-10-08 14:49:17 -0600 | [diff] [blame] | 200 | trace_cdev_update(cdev, current_target, min_target); |
| 201 | dev_dbg(&cdev->device, "set to state %lu min state %lu\n", |
| 202 | current_target, min_target); |
Eduardo Valentin | cd221c7 | 2016-11-07 21:09:04 -0800 | [diff] [blame] | 203 | } |
| 204 | EXPORT_SYMBOL(thermal_cdev_update); |
Eduardo Valentin | 373f91d | 2016-11-07 21:09:27 -0800 | [diff] [blame] | 205 | |
| 206 | /** |
| 207 | * thermal_zone_get_slope - return the slope attribute of the thermal zone |
| 208 | * @tz: thermal zone device with the slope attribute |
| 209 | * |
| 210 | * Return: If the thermal zone device has a slope attribute, return it, else |
| 211 | * return 1. |
| 212 | */ |
| 213 | int thermal_zone_get_slope(struct thermal_zone_device *tz) |
| 214 | { |
| 215 | if (tz && tz->tzp) |
| 216 | return tz->tzp->slope; |
| 217 | return 1; |
| 218 | } |
| 219 | EXPORT_SYMBOL_GPL(thermal_zone_get_slope); |
| 220 | |
| 221 | /** |
| 222 | * thermal_zone_get_offset - return the offset attribute of the thermal zone |
| 223 | * @tz: thermal zone device with the offset attribute |
| 224 | * |
| 225 | * Return: If the thermal zone device has a offset attribute, return it, else |
| 226 | * return 0. |
| 227 | */ |
| 228 | int thermal_zone_get_offset(struct thermal_zone_device *tz) |
| 229 | { |
| 230 | if (tz && tz->tzp) |
| 231 | return tz->tzp->offset; |
| 232 | return 0; |
| 233 | } |
| 234 | EXPORT_SYMBOL_GPL(thermal_zone_get_offset); |