34 #ifdef ENABLE_THERMAL_PROTECTION
39 #include "mfr_temperature.h"
40 #include "plat_power.h"
41 #include "pwrlogger.h"
44 int uint32_compare(
const void* a,
const void* b)
46 const uint32_t l = * ((
const uint32_t*) a);
47 const uint32_t r = * ((
const uint32_t*) b);
49 if ( l == r )
return 0;
50 else if ( l < r )
return -1;
55 int PLAT_API_DetemineClockSpeeds(uint32_t *cpu_rate_Normal, uint32_t *cpu_rate_Scaled, uint32_t *cpu_rate_Minimal)
61 uint32_t freqList[32];
62 uint32_t numFreqs = 0;
65 fp = fopen (
"/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies",
"r");
67 LOG(
"[%s:%d] Unable to open '/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies' for reading\n", __FUNCTION__, __LINE__);
72 while (numFreqs <
sizeof(freqList)/
sizeof(freqList[0]) && (fscanf(fp,
"%u", &freqList[numFreqs]) == 1))
76 LOG(
"[%s] **ERROR** Unable to read sacaling frequencies!\n", __FUNCTION__);
82 qsort( (
void*)freqList, numFreqs,
sizeof(freqList[0]), uint32_compare );
83 LOG(
"[%s] Scaling Frequency List:\n", __FUNCTION__);
84 for(i=0; i < numFreqs; i++) LOG (
" [%s] %uhz\n", __FUNCTION__, freqList[i]);
88 scaled=freqList[numFreqs/2];
89 normal=freqList[numFreqs-1];
90 LOG(
"[%s] Using -- Normal:%u Scaled:%u Minimal:%u\n", __FUNCTION__, normal, scaled, minimal);
94 if (cpu_rate_Normal) *cpu_rate_Normal = normal;
95 if (cpu_rate_Scaled) *cpu_rate_Scaled = scaled;
96 if (cpu_rate_Minimal) *cpu_rate_Minimal = minimal;
100 int PLAT_API_SetClockSpeed(uint32_t speed)
103 uint32_t cur_speed = 0;
104 LOG(
"[%s]Setting clock speed to [%d]\n", __FUNCTION__, speed );
107 fp = fopen (
"/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor",
"w");
109 LOG(
"[%s:%d] Unable to open '/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor' for writing\n", __FUNCTION__, __LINE__);
114 fprintf(fp,
"userspace");
117 fp = fopen (
"/sys/devices/system/cpu/cpu0/cpufreq/scaling_setspeed",
"w");
119 LOG(
"[%s:%d] Unable to open '/sys/devices/system/cpu/cpu0/cpufreq/scaling_setspeed' for writing\n", __FUNCTION__, __LINE__);
124 fprintf(fp,
"%u", speed);
127 if (PLAT_API_GetClockSpeed(&cur_speed) != 1 ) {
128 LOG(
"[%s:%d] Failed to read current CPU speed\n", __FUNCTION__, __LINE__);
132 LOG(
"[%s] Clock speed set to [%d]\n", __FUNCTION__, cur_speed );
134 return (speed == cur_speed) ? 1 : 0;
137 int PLAT_API_GetClockSpeed(uint32_t *speed)
141 #ifdef ENABLE_LLAMA_PLATCO
142 fp = fopen (
"/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq",
"r");
144 LOG(
"[%s:%d] Unable to open '/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq' for writing\n", __FUNCTION__, __LINE__);
148 fp = fopen (
"/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq",
"r");
150 LOG(
"[%s:%d] Unable to open '/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq' for writing\n", __FUNCTION__, __LINE__);
155 if(0 >= fscanf(fp,
"%u", speed)) {
156 LOG(
"[%s:%d] Unable to get the speed \n", __FUNCTION__, __LINE__);
165 mfrTemperatureState_t state;
166 int temperatureValue;
170 mfrError_t result =
mfrGetTemperature(&state, &temperatureValue, &wifiTempValue);
177 state = (mfrTemperatureState_t)IARM_BUS_PWRMGR_TEMPERATURE_NORMAL;
178 temperatureValue=50.0;
180 result = mfrERR_NONE;
182 fp = fopen (
"/opt/force_temp.soc",
"r");
184 fscanf(fp,
"%d", &temperatureValue);
188 fp = fopen (
"/opt/force_temp.wifi",
"r");
190 fscanf(fp,
"%d", &wifiTempValue);
194 fp = fopen (
"/opt/force_temp.state",
"r");
196 fscanf(fp,
"%d", &state);
202 if (result == mfrERR_NONE)
204 LOG(
"[%s] Got MFR Temperatures SoC:%d Wifi:%d\n", __FUNCTION__, temperatureValue, wifiTempValue);
206 *curTemperature = temperatureValue;
207 *wifiTemperature = wifiTempValue;
214 int PLAT_API_SetTempThresholds(
float tempHigh,
float tempCritical)
217 int result = (response == mfrERR_NONE) ?1:0;
222 int PLAT_API_GetTempThresholds(
float *tempHigh,
float *tempCritical)
225 int highTemp = 0, criticalTemp= 0;
228 if(mfrERR_NONE == response)
231 *tempHigh = highTemp;
232 *tempCritical = criticalTemp;
237 #endif //ENABLE_THERMAL_PROTECTION