1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include <linux/thermal.h>
26#include <trace/events/thermal.h>
27
28#include "thermal_core.h"
29
30
31
32
33
34static int get_trip_level(struct thermal_zone_device *tz)
35{
36 int count = 0;
37 int trip_temp;
38 enum thermal_trip_type trip_type;
39
40 if (tz->trips == 0 || !tz->ops->get_trip_temp)
41 return 0;
42
43 for (count = 0; count < tz->trips; count++) {
44 tz->ops->get_trip_temp(tz, count, &trip_temp);
45 if (tz->temperature < trip_temp)
46 break;
47 }
48
49
50
51
52
53 if (count > 0) {
54 tz->ops->get_trip_type(tz, count - 1, &trip_type);
55 trace_thermal_zone_trip(tz, count - 1, trip_type);
56 }
57
58 return count;
59}
60
61static long get_target_state(struct thermal_zone_device *tz,
62 struct thermal_cooling_device *cdev, int percentage, int level)
63{
64 unsigned long max_state;
65
66 cdev->ops->get_max_state(cdev, &max_state);
67
68 return (long)(percentage * level * max_state) / (100 * tz->trips);
69}
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89static int fair_share_throttle(struct thermal_zone_device *tz, int trip)
90{
91 struct thermal_instance *instance;
92 int total_weight = 0;
93 int total_instance = 0;
94 int cur_trip_level = get_trip_level(tz);
95
96 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
97 if (instance->trip != trip)
98 continue;
99
100 total_weight += instance->weight;
101 total_instance++;
102 }
103
104 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
105 int percentage;
106 struct thermal_cooling_device *cdev = instance->cdev;
107
108 if (instance->trip != trip)
109 continue;
110
111 if (!total_weight)
112 percentage = 100 / total_instance;
113 else
114 percentage = (instance->weight * 100) / total_weight;
115
116 instance->target = get_target_state(tz, cdev, percentage,
117 cur_trip_level);
118
119 instance->cdev->updated = false;
120 thermal_cdev_update(cdev);
121 }
122 return 0;
123}
124
125static struct thermal_governor thermal_gov_fair_share = {
126 .name = "fair_share",
127 .throttle = fair_share_throttle,
128};
129
130int thermal_gov_fair_share_register(void)
131{
132 return thermal_register_governor(&thermal_gov_fair_share);
133}
134
135void thermal_gov_fair_share_unregister(void)
136{
137 thermal_unregister_governor(&thermal_gov_fair_share);
138}
139
140