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 | |
Eduardo Valentin | cd221c7 | 2016-11-07 21:09:04 -0800 | [diff] [blame] | 15 | #include <linux/device.h> |
| 16 | #include <linux/err.h> |
Amit Kucheria | 3a74c88 | 2020-05-11 17:54:52 +0530 | [diff] [blame] | 17 | #include <linux/export.h> |
Eduardo Valentin | cd221c7 | 2016-11-07 21:09:04 -0800 | [diff] [blame] | 18 | #include <linux/slab.h> |
| 19 | #include <linux/string.h> |
Amit Kucheria | 231b98a | 2020-05-11 17:54:51 +0530 | [diff] [blame] | 20 | #include <linux/sysfs.h> |
Eduardo Valentin | cd221c7 | 2016-11-07 21:09:04 -0800 | [diff] [blame] | 21 | |
| 22 | #include <trace/events/thermal.h> |
| 23 | |
| 24 | #include "thermal_core.h" |
| 25 | |
| 26 | int get_tz_trend(struct thermal_zone_device *tz, int trip) |
| 27 | { |
| 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 | EXPORT_SYMBOL(get_tz_trend); |
| 43 | |
| 44 | struct thermal_instance * |
| 45 | get_thermal_instance(struct thermal_zone_device *tz, |
| 46 | struct thermal_cooling_device *cdev, int trip) |
| 47 | { |
| 48 | struct thermal_instance *pos = NULL; |
| 49 | struct thermal_instance *target_instance = NULL; |
| 50 | |
| 51 | mutex_lock(&tz->lock); |
| 52 | mutex_lock(&cdev->lock); |
| 53 | |
| 54 | list_for_each_entry(pos, &tz->thermal_instances, tz_node) { |
| 55 | if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { |
| 56 | target_instance = pos; |
| 57 | break; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | mutex_unlock(&cdev->lock); |
| 62 | mutex_unlock(&tz->lock); |
| 63 | |
| 64 | return target_instance; |
| 65 | } |
| 66 | EXPORT_SYMBOL(get_thermal_instance); |
| 67 | |
| 68 | /** |
| 69 | * thermal_zone_get_temp() - returns the temperature of a thermal zone |
| 70 | * @tz: a valid pointer to a struct thermal_zone_device |
| 71 | * @temp: a valid pointer to where to store the resulting temperature. |
| 72 | * |
| 73 | * When a valid thermal zone reference is passed, it will fetch its |
| 74 | * temperature and fill @temp. |
| 75 | * |
| 76 | * Return: On success returns 0, an error code otherwise |
| 77 | */ |
| 78 | int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp) |
| 79 | { |
| 80 | int ret = -EINVAL; |
| 81 | int count; |
| 82 | int crit_temp = INT_MAX; |
| 83 | enum thermal_trip_type type; |
| 84 | |
| 85 | if (!tz || IS_ERR(tz) || !tz->ops->get_temp) |
| 86 | goto exit; |
| 87 | |
| 88 | mutex_lock(&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->trips; count++) { |
| 94 | ret = tz->ops->get_trip_type(tz, count, &type); |
| 95 | if (!ret && type == THERMAL_TRIP_CRITICAL) { |
| 96 | ret = tz->ops->get_trip_temp(tz, count, |
| 97 | &crit_temp); |
| 98 | break; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * Only allow emulating a temperature when the real temperature |
| 104 | * is below the critical temperature so that the emulation code |
| 105 | * cannot hide critical conditions. |
| 106 | */ |
| 107 | if (!ret && *temp < crit_temp) |
| 108 | *temp = tz->emul_temperature; |
| 109 | } |
| 110 | |
| 111 | mutex_unlock(&tz->lock); |
| 112 | exit: |
| 113 | return ret; |
| 114 | } |
| 115 | EXPORT_SYMBOL_GPL(thermal_zone_get_temp); |
| 116 | |
Daniel Lezcano | bceb564 | 2020-03-31 18:54:48 +0200 | [diff] [blame] | 117 | /** |
| 118 | * thermal_zone_set_trips - Computes the next trip points for the driver |
| 119 | * @tz: a pointer to a thermal zone device structure |
| 120 | * |
| 121 | * The function computes the next temperature boundaries by browsing |
| 122 | * the trip points. The result is the closer low and high trip points |
| 123 | * to the current temperature. These values are passed to the backend |
| 124 | * driver to let it set its own notification mechanism (usually an |
| 125 | * interrupt). |
| 126 | * |
| 127 | * It does not return a value |
| 128 | */ |
Eduardo Valentin | cd221c7 | 2016-11-07 21:09:04 -0800 | [diff] [blame] | 129 | void thermal_zone_set_trips(struct thermal_zone_device *tz) |
| 130 | { |
| 131 | int low = -INT_MAX; |
| 132 | int high = INT_MAX; |
| 133 | int trip_temp, hysteresis; |
| 134 | int i, ret; |
| 135 | |
| 136 | mutex_lock(&tz->lock); |
| 137 | |
| 138 | if (!tz->ops->set_trips || !tz->ops->get_trip_hyst) |
| 139 | goto exit; |
| 140 | |
| 141 | for (i = 0; i < tz->trips; i++) { |
| 142 | int trip_low; |
| 143 | |
| 144 | tz->ops->get_trip_temp(tz, i, &trip_temp); |
| 145 | tz->ops->get_trip_hyst(tz, i, &hysteresis); |
| 146 | |
| 147 | trip_low = trip_temp - hysteresis; |
| 148 | |
| 149 | if (trip_low < tz->temperature && trip_low > low) |
| 150 | low = trip_low; |
| 151 | |
| 152 | if (trip_temp > tz->temperature && trip_temp < high) |
| 153 | high = trip_temp; |
| 154 | } |
| 155 | |
| 156 | /* No need to change trip points */ |
| 157 | if (tz->prev_low_trip == low && tz->prev_high_trip == high) |
| 158 | goto exit; |
| 159 | |
| 160 | tz->prev_low_trip = low; |
| 161 | tz->prev_high_trip = high; |
| 162 | |
| 163 | dev_dbg(&tz->device, |
| 164 | "new temperature boundaries: %d < x < %d\n", low, high); |
| 165 | |
| 166 | /* |
| 167 | * Set a temperature window. When this window is left the driver |
| 168 | * must inform the thermal core via thermal_zone_device_update. |
| 169 | */ |
| 170 | ret = tz->ops->set_trips(tz, low, high); |
| 171 | if (ret) |
| 172 | dev_err(&tz->device, "Failed to set trips: %d\n", ret); |
| 173 | |
| 174 | exit: |
| 175 | mutex_unlock(&tz->lock); |
| 176 | } |
Eduardo Valentin | cd221c7 | 2016-11-07 21:09:04 -0800 | [diff] [blame] | 177 | |
| 178 | void thermal_cdev_update(struct thermal_cooling_device *cdev) |
| 179 | { |
| 180 | struct thermal_instance *instance; |
| 181 | unsigned long target = 0; |
| 182 | |
| 183 | mutex_lock(&cdev->lock); |
| 184 | /* cooling device is updated*/ |
| 185 | if (cdev->updated) { |
| 186 | mutex_unlock(&cdev->lock); |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | /* Make sure cdev enters the deepest cooling state */ |
| 191 | list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) { |
| 192 | dev_dbg(&cdev->device, "zone%d->target=%lu\n", |
| 193 | instance->tz->id, instance->target); |
| 194 | if (instance->target == THERMAL_NO_TARGET) |
| 195 | continue; |
| 196 | if (instance->target > target) |
| 197 | target = instance->target; |
| 198 | } |
Viresh Kumar | 8ea2295 | 2018-04-02 16:26:25 +0530 | [diff] [blame] | 199 | |
| 200 | if (!cdev->ops->set_cur_state(cdev, target)) |
| 201 | thermal_cooling_device_stats_update(cdev, target); |
| 202 | |
Eduardo Valentin | cd221c7 | 2016-11-07 21:09:04 -0800 | [diff] [blame] | 203 | cdev->updated = true; |
| 204 | mutex_unlock(&cdev->lock); |
| 205 | trace_cdev_update(cdev, target); |
| 206 | dev_dbg(&cdev->device, "set to state %lu\n", target); |
| 207 | } |
| 208 | EXPORT_SYMBOL(thermal_cdev_update); |
Eduardo Valentin | 373f91d | 2016-11-07 21:09:27 -0800 | [diff] [blame] | 209 | |
| 210 | /** |
| 211 | * thermal_zone_get_slope - return the slope attribute of the thermal zone |
| 212 | * @tz: thermal zone device with the slope attribute |
| 213 | * |
| 214 | * Return: If the thermal zone device has a slope attribute, return it, else |
| 215 | * return 1. |
| 216 | */ |
| 217 | int thermal_zone_get_slope(struct thermal_zone_device *tz) |
| 218 | { |
| 219 | if (tz && tz->tzp) |
| 220 | return tz->tzp->slope; |
| 221 | return 1; |
| 222 | } |
| 223 | EXPORT_SYMBOL_GPL(thermal_zone_get_slope); |
| 224 | |
| 225 | /** |
| 226 | * thermal_zone_get_offset - return the offset attribute of the thermal zone |
| 227 | * @tz: thermal zone device with the offset attribute |
| 228 | * |
| 229 | * Return: If the thermal zone device has a offset attribute, return it, else |
| 230 | * return 0. |
| 231 | */ |
| 232 | int thermal_zone_get_offset(struct thermal_zone_device *tz) |
| 233 | { |
| 234 | if (tz && tz->tzp) |
| 235 | return tz->tzp->offset; |
| 236 | return 0; |
| 237 | } |
| 238 | EXPORT_SYMBOL_GPL(thermal_zone_get_offset); |