1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * thermal_helpers.c - helper functions to handle thermal devices
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2016 Eduardo Valentin <edubezval@gmail.com>
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Highly based on original thermal_core.c
8*4882a593Smuzhiyun * Copyright (C) 2008 Intel Corp
9*4882a593Smuzhiyun * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
10*4882a593Smuzhiyun * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
11*4882a593Smuzhiyun */
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun #include <linux/device.h>
16*4882a593Smuzhiyun #include <linux/err.h>
17*4882a593Smuzhiyun #include <linux/export.h>
18*4882a593Smuzhiyun #include <linux/slab.h>
19*4882a593Smuzhiyun #include <linux/string.h>
20*4882a593Smuzhiyun #include <linux/sysfs.h>
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun #include <trace/events/thermal.h>
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun #include "thermal_core.h"
25*4882a593Smuzhiyun
get_tz_trend(struct thermal_zone_device * tz,int trip)26*4882a593Smuzhiyun int get_tz_trend(struct thermal_zone_device *tz, int trip)
27*4882a593Smuzhiyun {
28*4882a593Smuzhiyun enum thermal_trend trend;
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun if (tz->emul_temperature || !tz->ops->get_trend ||
31*4882a593Smuzhiyun tz->ops->get_trend(tz, trip, &trend)) {
32*4882a593Smuzhiyun if (tz->temperature > tz->last_temperature)
33*4882a593Smuzhiyun trend = THERMAL_TREND_RAISING;
34*4882a593Smuzhiyun else if (tz->temperature < tz->last_temperature)
35*4882a593Smuzhiyun trend = THERMAL_TREND_DROPPING;
36*4882a593Smuzhiyun else
37*4882a593Smuzhiyun trend = THERMAL_TREND_STABLE;
38*4882a593Smuzhiyun }
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun return trend;
41*4882a593Smuzhiyun }
42*4882a593Smuzhiyun EXPORT_SYMBOL(get_tz_trend);
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun struct thermal_instance *
get_thermal_instance(struct thermal_zone_device * tz,struct thermal_cooling_device * cdev,int trip)45*4882a593Smuzhiyun get_thermal_instance(struct thermal_zone_device *tz,
46*4882a593Smuzhiyun struct thermal_cooling_device *cdev, int trip)
47*4882a593Smuzhiyun {
48*4882a593Smuzhiyun struct thermal_instance *pos = NULL;
49*4882a593Smuzhiyun struct thermal_instance *target_instance = NULL;
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun mutex_lock(&tz->lock);
52*4882a593Smuzhiyun mutex_lock(&cdev->lock);
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
55*4882a593Smuzhiyun if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
56*4882a593Smuzhiyun target_instance = pos;
57*4882a593Smuzhiyun break;
58*4882a593Smuzhiyun }
59*4882a593Smuzhiyun }
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun mutex_unlock(&cdev->lock);
62*4882a593Smuzhiyun mutex_unlock(&tz->lock);
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun return target_instance;
65*4882a593Smuzhiyun }
66*4882a593Smuzhiyun EXPORT_SYMBOL(get_thermal_instance);
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun /**
69*4882a593Smuzhiyun * thermal_zone_get_temp() - returns the temperature of a thermal zone
70*4882a593Smuzhiyun * @tz: a valid pointer to a struct thermal_zone_device
71*4882a593Smuzhiyun * @temp: a valid pointer to where to store the resulting temperature.
72*4882a593Smuzhiyun *
73*4882a593Smuzhiyun * When a valid thermal zone reference is passed, it will fetch its
74*4882a593Smuzhiyun * temperature and fill @temp.
75*4882a593Smuzhiyun *
76*4882a593Smuzhiyun * Return: On success returns 0, an error code otherwise
77*4882a593Smuzhiyun */
thermal_zone_get_temp(struct thermal_zone_device * tz,int * temp)78*4882a593Smuzhiyun int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
79*4882a593Smuzhiyun {
80*4882a593Smuzhiyun int ret = -EINVAL;
81*4882a593Smuzhiyun int count;
82*4882a593Smuzhiyun int crit_temp = INT_MAX;
83*4882a593Smuzhiyun enum thermal_trip_type type;
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun if (!tz || IS_ERR(tz) || !tz->ops->get_temp)
86*4882a593Smuzhiyun goto exit;
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun mutex_lock(&tz->lock);
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun ret = tz->ops->get_temp(tz, temp);
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun if (IS_ENABLED(CONFIG_THERMAL_EMULATION) && tz->emul_temperature) {
93*4882a593Smuzhiyun for (count = 0; count < tz->trips; count++) {
94*4882a593Smuzhiyun ret = tz->ops->get_trip_type(tz, count, &type);
95*4882a593Smuzhiyun if (!ret && type == THERMAL_TRIP_CRITICAL) {
96*4882a593Smuzhiyun ret = tz->ops->get_trip_temp(tz, count,
97*4882a593Smuzhiyun &crit_temp);
98*4882a593Smuzhiyun break;
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun /*
103*4882a593Smuzhiyun * Only allow emulating a temperature when the real temperature
104*4882a593Smuzhiyun * is below the critical temperature so that the emulation code
105*4882a593Smuzhiyun * cannot hide critical conditions.
106*4882a593Smuzhiyun */
107*4882a593Smuzhiyun if (!ret && *temp < crit_temp)
108*4882a593Smuzhiyun *temp = tz->emul_temperature;
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun mutex_unlock(&tz->lock);
112*4882a593Smuzhiyun exit:
113*4882a593Smuzhiyun return ret;
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun /**
118*4882a593Smuzhiyun * thermal_zone_set_trips - Computes the next trip points for the driver
119*4882a593Smuzhiyun * @tz: a pointer to a thermal zone device structure
120*4882a593Smuzhiyun *
121*4882a593Smuzhiyun * The function computes the next temperature boundaries by browsing
122*4882a593Smuzhiyun * the trip points. The result is the closer low and high trip points
123*4882a593Smuzhiyun * to the current temperature. These values are passed to the backend
124*4882a593Smuzhiyun * driver to let it set its own notification mechanism (usually an
125*4882a593Smuzhiyun * interrupt).
126*4882a593Smuzhiyun *
127*4882a593Smuzhiyun * It does not return a value
128*4882a593Smuzhiyun */
thermal_zone_set_trips(struct thermal_zone_device * tz)129*4882a593Smuzhiyun void thermal_zone_set_trips(struct thermal_zone_device *tz)
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun int low = -INT_MAX;
132*4882a593Smuzhiyun int high = INT_MAX;
133*4882a593Smuzhiyun int trip_temp, hysteresis;
134*4882a593Smuzhiyun int i, ret;
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun mutex_lock(&tz->lock);
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun if (!tz->ops->set_trips || !tz->ops->get_trip_hyst)
139*4882a593Smuzhiyun goto exit;
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun for (i = 0; i < tz->trips; i++) {
142*4882a593Smuzhiyun int trip_low;
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun tz->ops->get_trip_temp(tz, i, &trip_temp);
145*4882a593Smuzhiyun tz->ops->get_trip_hyst(tz, i, &hysteresis);
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun trip_low = trip_temp - hysteresis;
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun if (trip_low < tz->temperature && trip_low > low)
150*4882a593Smuzhiyun low = trip_low;
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun if (trip_temp > tz->temperature && trip_temp < high)
153*4882a593Smuzhiyun high = trip_temp;
154*4882a593Smuzhiyun }
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun /* No need to change trip points */
157*4882a593Smuzhiyun if (tz->prev_low_trip == low && tz->prev_high_trip == high)
158*4882a593Smuzhiyun goto exit;
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun tz->prev_low_trip = low;
161*4882a593Smuzhiyun tz->prev_high_trip = high;
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun dev_dbg(&tz->device,
164*4882a593Smuzhiyun "new temperature boundaries: %d < x < %d\n", low, high);
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun /*
167*4882a593Smuzhiyun * Set a temperature window. When this window is left the driver
168*4882a593Smuzhiyun * must inform the thermal core via thermal_zone_device_update.
169*4882a593Smuzhiyun */
170*4882a593Smuzhiyun ret = tz->ops->set_trips(tz, low, high);
171*4882a593Smuzhiyun if (ret)
172*4882a593Smuzhiyun dev_err(&tz->device, "Failed to set trips: %d\n", ret);
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun exit:
175*4882a593Smuzhiyun mutex_unlock(&tz->lock);
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun
thermal_cdev_set_cur_state(struct thermal_cooling_device * cdev,int target)178*4882a593Smuzhiyun static void thermal_cdev_set_cur_state(struct thermal_cooling_device *cdev,
179*4882a593Smuzhiyun int target)
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun if (cdev->ops->set_cur_state(cdev, target))
182*4882a593Smuzhiyun return;
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun thermal_notify_cdev_state_update(cdev->id, target);
185*4882a593Smuzhiyun thermal_cooling_device_stats_update(cdev, target);
186*4882a593Smuzhiyun }
187*4882a593Smuzhiyun
thermal_cdev_update(struct thermal_cooling_device * cdev)188*4882a593Smuzhiyun void thermal_cdev_update(struct thermal_cooling_device *cdev)
189*4882a593Smuzhiyun {
190*4882a593Smuzhiyun struct thermal_instance *instance;
191*4882a593Smuzhiyun unsigned long target = 0;
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun mutex_lock(&cdev->lock);
194*4882a593Smuzhiyun /* cooling device is updated*/
195*4882a593Smuzhiyun if (cdev->updated) {
196*4882a593Smuzhiyun mutex_unlock(&cdev->lock);
197*4882a593Smuzhiyun return;
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun /* Make sure cdev enters the deepest cooling state */
201*4882a593Smuzhiyun list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
202*4882a593Smuzhiyun dev_dbg(&cdev->device, "zone%d->target=%lu\n",
203*4882a593Smuzhiyun instance->tz->id, instance->target);
204*4882a593Smuzhiyun if (instance->target == THERMAL_NO_TARGET)
205*4882a593Smuzhiyun continue;
206*4882a593Smuzhiyun if (instance->target > target)
207*4882a593Smuzhiyun target = instance->target;
208*4882a593Smuzhiyun }
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun thermal_cdev_set_cur_state(cdev, target);
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun cdev->updated = true;
213*4882a593Smuzhiyun mutex_unlock(&cdev->lock);
214*4882a593Smuzhiyun trace_cdev_update(cdev, target);
215*4882a593Smuzhiyun dev_dbg(&cdev->device, "set to state %lu\n", target);
216*4882a593Smuzhiyun }
217*4882a593Smuzhiyun EXPORT_SYMBOL(thermal_cdev_update);
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun /**
220*4882a593Smuzhiyun * thermal_zone_get_slope - return the slope attribute of the thermal zone
221*4882a593Smuzhiyun * @tz: thermal zone device with the slope attribute
222*4882a593Smuzhiyun *
223*4882a593Smuzhiyun * Return: If the thermal zone device has a slope attribute, return it, else
224*4882a593Smuzhiyun * return 1.
225*4882a593Smuzhiyun */
thermal_zone_get_slope(struct thermal_zone_device * tz)226*4882a593Smuzhiyun int thermal_zone_get_slope(struct thermal_zone_device *tz)
227*4882a593Smuzhiyun {
228*4882a593Smuzhiyun if (tz && tz->tzp)
229*4882a593Smuzhiyun return tz->tzp->slope;
230*4882a593Smuzhiyun return 1;
231*4882a593Smuzhiyun }
232*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(thermal_zone_get_slope);
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun /**
235*4882a593Smuzhiyun * thermal_zone_get_offset - return the offset attribute of the thermal zone
236*4882a593Smuzhiyun * @tz: thermal zone device with the offset attribute
237*4882a593Smuzhiyun *
238*4882a593Smuzhiyun * Return: If the thermal zone device has a offset attribute, return it, else
239*4882a593Smuzhiyun * return 0.
240*4882a593Smuzhiyun */
thermal_zone_get_offset(struct thermal_zone_device * tz)241*4882a593Smuzhiyun int thermal_zone_get_offset(struct thermal_zone_device *tz)
242*4882a593Smuzhiyun {
243*4882a593Smuzhiyun if (tz && tz->tzp)
244*4882a593Smuzhiyun return tz->tzp->offset;
245*4882a593Smuzhiyun return 0;
246*4882a593Smuzhiyun }
247*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(thermal_zone_get_offset);
248