Linux Audio

Check our new training course

Loading...
v6.13.7
 1// SPDX-License-Identifier: GPL-2.0
 2/*
 3 *  Copyright (C) 2008 Intel Corp
 4 *  Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
 5 *  Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
 6 *  Copyright 2022 Linaro Limited
 7 *
 8 * Thermal trips handling
 9 */
10#include "thermal_core.h"
11
12static const char *trip_type_names[] = {
13	[THERMAL_TRIP_ACTIVE] = "active",
14	[THERMAL_TRIP_PASSIVE] = "passive",
15	[THERMAL_TRIP_HOT] = "hot",
16	[THERMAL_TRIP_CRITICAL] = "critical",
17};
18
19const char *thermal_trip_type_name(enum thermal_trip_type trip_type)
20{
21	if (trip_type < THERMAL_TRIP_ACTIVE || trip_type > THERMAL_TRIP_CRITICAL)
22		return "unknown";
23
24	return trip_type_names[trip_type];
25}
26
27int for_each_thermal_trip(struct thermal_zone_device *tz,
28			  int (*cb)(struct thermal_trip *, void *),
29			  void *data)
30{
31	struct thermal_trip_desc *td;
32	int ret;
33
34	for_each_trip_desc(tz, td) {
35		ret = cb(&td->trip, data);
36		if (ret)
37			return ret;
38	}
39
40	return 0;
41}
42EXPORT_SYMBOL_GPL(for_each_thermal_trip);
43
44int thermal_zone_for_each_trip(struct thermal_zone_device *tz,
45			       int (*cb)(struct thermal_trip *, void *),
46			       void *data)
47{
48	guard(thermal_zone)(tz);
 
 
 
 
49
50	return for_each_thermal_trip(tz, cb, data);
51}
52EXPORT_SYMBOL_GPL(thermal_zone_for_each_trip);
53
54void thermal_zone_set_trips(struct thermal_zone_device *tz, int low, int high)
55{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56	int ret;
57
58	lockdep_assert_held(&tz->lock);
59
60	if (!tz->ops.set_trips)
61		return;
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63	/* No need to change trip points */
64	if (tz->prev_low_trip == low && tz->prev_high_trip == high)
65		return;
66
 
 
 
 
 
 
 
 
67	tz->prev_low_trip = low;
68	tz->prev_high_trip = high;
69
70	dev_dbg(&tz->device,
71		"new temperature boundaries: %d < x < %d\n", low, high);
72
73	/*
74	 * Set a temperature window. When this window is left the driver
75	 * must inform the thermal core via thermal_zone_device_update.
76	 */
77	ret = tz->ops.set_trips(tz, low, high);
78	if (ret)
79		dev_err(&tz->device, "Failed to set trips: %d\n", ret);
80}
81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82int thermal_zone_trip_id(const struct thermal_zone_device *tz,
83			 const struct thermal_trip *trip)
84{
85	/*
86	 * Assume the trip to be located within the bounds of the thermal
87	 * zone's trips[] table.
88	 */
89	return trip_to_trip_desc(trip) - tz->trips;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90}
v6.8
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 *  Copyright (C) 2008 Intel Corp
  4 *  Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
  5 *  Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
  6 *  Copyright 2022 Linaro Limited
  7 *
  8 * Thermal trips handling
  9 */
 10#include "thermal_core.h"
 11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 12int for_each_thermal_trip(struct thermal_zone_device *tz,
 13			  int (*cb)(struct thermal_trip *, void *),
 14			  void *data)
 15{
 16	struct thermal_trip *trip;
 17	int ret;
 18
 19	for_each_trip(tz, trip) {
 20		ret = cb(trip, data);
 21		if (ret)
 22			return ret;
 23	}
 24
 25	return 0;
 26}
 27EXPORT_SYMBOL_GPL(for_each_thermal_trip);
 28
 29int thermal_zone_for_each_trip(struct thermal_zone_device *tz,
 30			       int (*cb)(struct thermal_trip *, void *),
 31			       void *data)
 32{
 33	int ret;
 34
 35	mutex_lock(&tz->lock);
 36	ret = for_each_thermal_trip(tz, cb, data);
 37	mutex_unlock(&tz->lock);
 38
 39	return ret;
 40}
 41EXPORT_SYMBOL_GPL(thermal_zone_for_each_trip);
 42
 43int thermal_zone_get_num_trips(struct thermal_zone_device *tz)
 44{
 45	return tz->num_trips;
 46}
 47EXPORT_SYMBOL_GPL(thermal_zone_get_num_trips);
 48
 49/**
 50 * __thermal_zone_set_trips - Computes the next trip points for the driver
 51 * @tz: a pointer to a thermal zone device structure
 52 *
 53 * The function computes the next temperature boundaries by browsing
 54 * the trip points. The result is the closer low and high trip points
 55 * to the current temperature. These values are passed to the backend
 56 * driver to let it set its own notification mechanism (usually an
 57 * interrupt).
 58 *
 59 * This function must be called with tz->lock held. Both tz and tz->ops
 60 * must be valid pointers.
 61 *
 62 * It does not return a value
 63 */
 64void __thermal_zone_set_trips(struct thermal_zone_device *tz)
 65{
 66	const struct thermal_trip *trip;
 67	int low = -INT_MAX, high = INT_MAX;
 68	bool same_trip = false;
 69	int ret;
 70
 71	lockdep_assert_held(&tz->lock);
 72
 73	if (!tz->ops->set_trips)
 74		return;
 75
 76	for_each_trip(tz, trip) {
 77		bool low_set = false;
 78		int trip_low;
 79
 80		trip_low = trip->temperature - trip->hysteresis;
 81
 82		if (trip_low < tz->temperature && trip_low > low) {
 83			low = trip_low;
 84			low_set = true;
 85			same_trip = false;
 86		}
 87
 88		if (trip->temperature > tz->temperature &&
 89		    trip->temperature < high) {
 90			high = trip->temperature;
 91			same_trip = low_set;
 92		}
 93	}
 94
 95	/* No need to change trip points */
 96	if (tz->prev_low_trip == low && tz->prev_high_trip == high)
 97		return;
 98
 99	/*
100	 * If "high" and "low" are the same, skip the change unless this is the
101	 * first time.
102	 */
103	if (same_trip && (tz->prev_low_trip != -INT_MAX ||
104	    tz->prev_high_trip != INT_MAX))
105		return;
106
107	tz->prev_low_trip = low;
108	tz->prev_high_trip = high;
109
110	dev_dbg(&tz->device,
111		"new temperature boundaries: %d < x < %d\n", low, high);
112
113	/*
114	 * Set a temperature window. When this window is left the driver
115	 * must inform the thermal core via thermal_zone_device_update.
116	 */
117	ret = tz->ops->set_trips(tz, low, high);
118	if (ret)
119		dev_err(&tz->device, "Failed to set trips: %d\n", ret);
120}
121
122int __thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
123			    struct thermal_trip *trip)
124{
125	if (!tz || !tz->trips || trip_id < 0 || trip_id >= tz->num_trips || !trip)
126		return -EINVAL;
127
128	*trip = tz->trips[trip_id];
129	return 0;
130}
131EXPORT_SYMBOL_GPL(__thermal_zone_get_trip);
132
133int thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
134			  struct thermal_trip *trip)
135{
136	int ret;
137
138	mutex_lock(&tz->lock);
139	ret = __thermal_zone_get_trip(tz, trip_id, trip);
140	mutex_unlock(&tz->lock);
141
142	return ret;
143}
144EXPORT_SYMBOL_GPL(thermal_zone_get_trip);
145
146int thermal_zone_trip_id(const struct thermal_zone_device *tz,
147			 const struct thermal_trip *trip)
148{
149	/*
150	 * Assume the trip to be located within the bounds of the thermal
151	 * zone's trips[] table.
152	 */
153	return trip - tz->trips;
154}
155void thermal_zone_trip_updated(struct thermal_zone_device *tz,
156			       const struct thermal_trip *trip)
157{
158	thermal_notify_tz_trip_change(tz, trip);
159	__thermal_zone_device_update(tz, THERMAL_TRIP_CHANGED);
160}
161
162void thermal_zone_set_trip_temp(struct thermal_zone_device *tz,
163				struct thermal_trip *trip, int temp)
164{
165	if (trip->temperature == temp)
166		return;
167
168	trip->temperature = temp;
169	thermal_notify_tz_trip_change(tz, trip);
170}
171EXPORT_SYMBOL_GPL(thermal_zone_set_trip_temp);