mediatek: add support for the new MT7623 Arm SoC
[openwrt/openwrt.git] / target / linux / mediatek / patches / 0020-thermal-thermal-Add-support-for-hardware-tracked-tri.patch
1 From 346632bc00fe71c269709702fecb474bb22e933e Mon Sep 17 00:00:00 2001
2 From: Sascha Hauer <s.hauer@pengutronix.de>
3 Date: Wed, 13 May 2015 10:52:39 +0200
4 Subject: [PATCH 20/76] thermal: thermal: Add support for hardware-tracked
5 trip points
6
7 This adds support for hardware-tracked trip points to the device tree
8 thermal sensor framework.
9
10 The framework supports an arbitrary number of trip points. Whenever
11 the current temperature is updated, the trip points immediately
12 below and above the current temperature are found. A .set_trips
13 callback is then called with the temperatures. If there is no trip
14 point above or below the current temperature, the passed trip
15 temperature will be -INT_MAX or INT_MAX respectively. In this callback,
16 the driver should program the hardware such that it is notified
17 when either of these trip points are triggered. When a trip point
18 is triggered, the driver should call `thermal_zone_device_update'
19 for the respective thermal zone. This will cause the trip points
20 to be updated again.
21
22 If .set_trips is not implemented, the framework behaves as before.
23
24 This patch is based on an earlier version from Mikko Perttunen
25 <mikko.perttunen@kapsi.fi>
26
27 Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
28 ---
29 drivers/thermal/thermal_core.c | 43 ++++++++++++++++++++++++++++++++++++++++
30 include/linux/thermal.h | 3 +++
31 2 files changed, 46 insertions(+)
32
33 diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c
34 index 6bbf61f..3ae1795 100644
35 --- a/drivers/thermal/thermal_core.c
36 +++ b/drivers/thermal/thermal_core.c
37 @@ -453,6 +453,45 @@ int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
38 }
39 EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
40
41 +static void thermal_zone_set_trips(struct thermal_zone_device *tz)
42 +{
43 + int low = -INT_MAX;
44 + int high = INT_MAX;
45 + int trip_temp, hysteresis;
46 + int temp = tz->temperature;
47 + int i;
48 +
49 + if (!tz->ops->set_trips)
50 + return;
51 +
52 + /* No need to change trip points */
53 + if (temp > tz->prev_low_trip && temp < tz->prev_high_trip)
54 + return;
55 +
56 + for (i = 0; i < tz->trips; i++) {
57 + int trip_low;
58 +
59 + tz->ops->get_trip_temp(tz, i, &trip_temp);
60 + tz->ops->get_trip_hyst(tz, i, &hysteresis);
61 +
62 + trip_low = trip_temp - hysteresis;
63 +
64 + if (trip_low < temp && trip_low > low)
65 + low = trip_low;
66 +
67 + if (trip_temp > temp && trip_temp < high)
68 + high = trip_temp;
69 + }
70 +
71 + tz->prev_low_trip = low;
72 + tz->prev_high_trip = high;
73 +
74 + dev_dbg(&tz->device, "new temperature boundaries: %d < x < %d\n",
75 + low, high);
76 +
77 + tz->ops->set_trips(tz, low, high);
78 +}
79 +
80 void thermal_zone_device_update(struct thermal_zone_device *tz)
81 {
82 int temp, ret, count;
83 @@ -479,6 +518,8 @@ void thermal_zone_device_update(struct thermal_zone_device *tz)
84 dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n",
85 tz->last_temperature, tz->temperature);
86
87 + thermal_zone_set_trips(tz);
88 +
89 for (count = 0; count < tz->trips; count++)
90 handle_thermal_trip(tz, count);
91 }
92 @@ -1494,6 +1535,8 @@ struct thermal_zone_device *thermal_zone_device_register(const char *type,
93 tz->trips = trips;
94 tz->passive_delay = passive_delay;
95 tz->polling_delay = polling_delay;
96 + tz->prev_low_trip = INT_MAX;
97 + tz->prev_high_trip = -INT_MAX;
98
99 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
100 result = device_register(&tz->device);
101 diff --git a/include/linux/thermal.h b/include/linux/thermal.h
102 index 07bd5e8..aef6e13 100644
103 --- a/include/linux/thermal.h
104 +++ b/include/linux/thermal.h
105 @@ -87,6 +87,7 @@ struct thermal_zone_device_ops {
106 int (*unbind) (struct thermal_zone_device *,
107 struct thermal_cooling_device *);
108 int (*get_temp) (struct thermal_zone_device *, int *);
109 + int (*set_trips) (struct thermal_zone_device *, int, int);
110 int (*get_mode) (struct thermal_zone_device *,
111 enum thermal_device_mode *);
112 int (*set_mode) (struct thermal_zone_device *,
113 @@ -180,6 +181,8 @@ struct thermal_zone_device {
114 int last_temperature;
115 int emul_temperature;
116 int passive;
117 + int prev_low_trip;
118 + int prev_high_trip;
119 unsigned int forced_passive;
120 const struct thermal_zone_device_ops *ops;
121 const struct thermal_zone_params *tzp;
122 --
123 1.7.10.4
124