0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include <linux/thermal.h>
0014 #include <trace/events/thermal.h>
0015
0016 #include "thermal_core.h"
0017
0018
0019
0020
0021
0022 static int get_trip_level(struct thermal_zone_device *tz)
0023 {
0024 int count = 0;
0025 int trip_temp;
0026 enum thermal_trip_type trip_type;
0027
0028 if (tz->num_trips == 0 || !tz->ops->get_trip_temp)
0029 return 0;
0030
0031 for (count = 0; count < tz->num_trips; count++) {
0032 tz->ops->get_trip_temp(tz, count, &trip_temp);
0033 if (tz->temperature < trip_temp)
0034 break;
0035 }
0036
0037
0038
0039
0040
0041 if (count > 0) {
0042 tz->ops->get_trip_type(tz, count - 1, &trip_type);
0043 trace_thermal_zone_trip(tz, count - 1, trip_type);
0044 }
0045
0046 return count;
0047 }
0048
0049 static long get_target_state(struct thermal_zone_device *tz,
0050 struct thermal_cooling_device *cdev, int percentage, int level)
0051 {
0052 unsigned long max_state;
0053
0054 cdev->ops->get_max_state(cdev, &max_state);
0055
0056 return (long)(percentage * level * max_state) / (100 * tz->num_trips);
0057 }
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078 static int fair_share_throttle(struct thermal_zone_device *tz, int trip)
0079 {
0080 struct thermal_instance *instance;
0081 int total_weight = 0;
0082 int total_instance = 0;
0083 int cur_trip_level = get_trip_level(tz);
0084
0085 mutex_lock(&tz->lock);
0086
0087 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
0088 if (instance->trip != trip)
0089 continue;
0090
0091 total_weight += instance->weight;
0092 total_instance++;
0093 }
0094
0095 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
0096 int percentage;
0097 struct thermal_cooling_device *cdev = instance->cdev;
0098
0099 if (instance->trip != trip)
0100 continue;
0101
0102 if (!total_weight)
0103 percentage = 100 / total_instance;
0104 else
0105 percentage = (instance->weight * 100) / total_weight;
0106
0107 instance->target = get_target_state(tz, cdev, percentage,
0108 cur_trip_level);
0109
0110 mutex_lock(&cdev->lock);
0111 __thermal_cdev_update(cdev);
0112 mutex_unlock(&cdev->lock);
0113 }
0114
0115 mutex_unlock(&tz->lock);
0116 return 0;
0117 }
0118
0119 static struct thermal_governor thermal_gov_fair_share = {
0120 .name = "fair_share",
0121 .throttle = fair_share_throttle,
0122 };
0123 THERMAL_GOVERNOR_DECLARE(thermal_gov_fair_share);