blob: 3d737143ec1184ef8008c9fda5c5fde2ca9a06a6 [file] [log] [blame]
Lina Iyer7e3c0382018-05-07 11:52:29 -06001// SPDX-License-Identifier: GPL-2.0
Eduardo Valentincd221c72016-11-07 21:09:04 -08002/*
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 Valentincd221c72016-11-07 21:09:04 -080011 */
12
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
Eduardo Valentincd221c72016-11-07 21:09:04 -080015#include <linux/device.h>
16#include <linux/err.h>
17#include <linux/slab.h>
18#include <linux/string.h>
Amit Kucheria231b98a2020-05-11 17:54:51 +053019#include <linux/sysfs.h>
Eduardo Valentincd221c72016-11-07 21:09:04 -080020
21#include <trace/events/thermal.h>
22
23#include "thermal_core.h"
24
25int 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}
41EXPORT_SYMBOL(get_tz_trend);
42
43struct thermal_instance *
44get_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}
65EXPORT_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 */
77int 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 }
109
110 mutex_unlock(&tz->lock);
111exit:
112 return ret;
113}
114EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
115
Daniel Lezcanobceb5642020-03-31 18:54:48 +0200116/**
117 * thermal_zone_set_trips - Computes the next trip points for the driver
118 * @tz: a pointer to a thermal zone device structure
119 *
120 * The function computes the next temperature boundaries by browsing
121 * the trip points. The result is the closer low and high trip points
122 * to the current temperature. These values are passed to the backend
123 * driver to let it set its own notification mechanism (usually an
124 * interrupt).
125 *
126 * It does not return a value
127 */
Eduardo Valentincd221c72016-11-07 21:09:04 -0800128void thermal_zone_set_trips(struct thermal_zone_device *tz)
129{
130 int low = -INT_MAX;
131 int high = INT_MAX;
132 int trip_temp, hysteresis;
133 int i, ret;
134
135 mutex_lock(&tz->lock);
136
137 if (!tz->ops->set_trips || !tz->ops->get_trip_hyst)
138 goto exit;
139
140 for (i = 0; i < tz->trips; i++) {
141 int trip_low;
142
143 tz->ops->get_trip_temp(tz, i, &trip_temp);
144 tz->ops->get_trip_hyst(tz, i, &hysteresis);
145
146 trip_low = trip_temp - hysteresis;
147
148 if (trip_low < tz->temperature && trip_low > low)
149 low = trip_low;
150
151 if (trip_temp > tz->temperature && trip_temp < high)
152 high = trip_temp;
153 }
154
155 /* No need to change trip points */
156 if (tz->prev_low_trip == low && tz->prev_high_trip == high)
157 goto exit;
158
159 tz->prev_low_trip = low;
160 tz->prev_high_trip = high;
161
162 dev_dbg(&tz->device,
163 "new temperature boundaries: %d < x < %d\n", low, high);
164
165 /*
166 * Set a temperature window. When this window is left the driver
167 * must inform the thermal core via thermal_zone_device_update.
168 */
169 ret = tz->ops->set_trips(tz, low, high);
170 if (ret)
171 dev_err(&tz->device, "Failed to set trips: %d\n", ret);
172
173exit:
174 mutex_unlock(&tz->lock);
175}
Eduardo Valentincd221c72016-11-07 21:09:04 -0800176
177void thermal_cdev_update(struct thermal_cooling_device *cdev)
178{
179 struct thermal_instance *instance;
180 unsigned long target = 0;
181
182 mutex_lock(&cdev->lock);
183 /* cooling device is updated*/
184 if (cdev->updated) {
185 mutex_unlock(&cdev->lock);
186 return;
187 }
188
189 /* Make sure cdev enters the deepest cooling state */
190 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
191 dev_dbg(&cdev->device, "zone%d->target=%lu\n",
192 instance->tz->id, instance->target);
193 if (instance->target == THERMAL_NO_TARGET)
194 continue;
195 if (instance->target > target)
196 target = instance->target;
197 }
Viresh Kumar8ea22952018-04-02 16:26:25 +0530198
199 if (!cdev->ops->set_cur_state(cdev, target))
200 thermal_cooling_device_stats_update(cdev, target);
201
Eduardo Valentincd221c72016-11-07 21:09:04 -0800202 cdev->updated = true;
203 mutex_unlock(&cdev->lock);
204 trace_cdev_update(cdev, target);
205 dev_dbg(&cdev->device, "set to state %lu\n", target);
206}
207EXPORT_SYMBOL(thermal_cdev_update);
Eduardo Valentin373f91d2016-11-07 21:09:27 -0800208
209/**
210 * thermal_zone_get_slope - return the slope attribute of the thermal zone
211 * @tz: thermal zone device with the slope attribute
212 *
213 * Return: If the thermal zone device has a slope attribute, return it, else
214 * return 1.
215 */
216int thermal_zone_get_slope(struct thermal_zone_device *tz)
217{
218 if (tz && tz->tzp)
219 return tz->tzp->slope;
220 return 1;
221}
222EXPORT_SYMBOL_GPL(thermal_zone_get_slope);
223
224/**
225 * thermal_zone_get_offset - return the offset attribute of the thermal zone
226 * @tz: thermal zone device with the offset attribute
227 *
228 * Return: If the thermal zone device has a offset attribute, return it, else
229 * return 0.
230 */
231int thermal_zone_get_offset(struct thermal_zone_device *tz)
232{
233 if (tz && tz->tzp)
234 return tz->tzp->offset;
235 return 0;
236}
237EXPORT_SYMBOL_GPL(thermal_zone_get_offset);