summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorManaf Meethalavalappu Pallikunhi <manafm@codeaurora.org>2018-08-24 12:34:45 +0530
committerManaf Meethalavalappu Pallikunhi <manafm@codeaurora.org>2018-09-07 14:39:06 +0530
commit24ab2c825f3d2fc41c519d5d8ba25e86e39b65d0 (patch)
tree3b1701e9f6e1230d314a01ef2c8ab0e19fe83ec5
parent34f922d74a2e0bc81815785c49e7c8598a628647 (diff)
downloadthermal-24ab2c825f3d2fc41c519d5d8ba25e86e39b65d0.tar.gz
thermal-hal: Re-arch thermal HAL implementation
Re-design thermal HAL server side implementation to accomodate below features. -Use sensor names and get thermal zone ids on the fly using sensor names. -Generalize APIs implementation and remove repeated code implementation in target specific files. -Minimize target specific configuration and avoid target specific file going forward. Change-Id: Ib61c591d60d17aaeac0eb84ec9b3a4775173a416
-rw-r--r--Android.mk2
-rw-r--r--thermal-845.c228
-rw-r--r--thermal-8998.c223
-rw-r--r--thermal_common.c385
-rw-r--r--thermal_common.h52
5 files changed, 572 insertions, 318 deletions
diff --git a/Android.mk b/Android.mk
index c8725dd..797e5b2 100644
--- a/Android.mk
+++ b/Android.mk
@@ -23,12 +23,14 @@ LOCAL_PROPRIETARY_MODULE := true
ifeq ($(call is-board-platform-in-list,msm8998), true)
LOCAL_SRC_FILES := thermal.c
LOCAL_SRC_FILES += thermal-8998.c
+LOCAL_SRC_FILES += thermal_common.c
SUPPORT_THERMAL_HAL:=1
endif
ifeq ($(call is-board-platform-in-list,sdm845), true)
LOCAL_SRC_FILES := thermal.c
LOCAL_SRC_FILES += thermal-845.c
+LOCAL_SRC_FILES += thermal_common.c
SUPPORT_THERMAL_HAL:=1
endif
diff --git a/thermal-845.c b/thermal-845.c
index a6a9e3e..59925b5 100644
--- a/thermal-845.c
+++ b/thermal-845.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2017, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2017-2018 The Linux Foundation. All rights reserved.
* Not a contribution
* Copyright (C) 2016 The Android Open Source Project
*
@@ -16,177 +16,85 @@
* limitations under the License.
*/
-#include <ctype.h>
-#include <errno.h>
-#include <inttypes.h>
-#include <stdlib.h>
-#include <string.h>
#define LOG_TAG "ThermalHAL-845"
#include <utils/Log.h>
#include <hardware/hardware.h>
#include <hardware/thermal.h>
-
-#define MAX_LENGTH 50
-
-#define TEMPERATURE_FILE_FORMAT "/sys/class/thermal/thermal_zone%d/temp"
-
-#define BATTERY_SENSOR_NUM 60
-#define GPU_SENSOR_NUM 11
-#define SKIN_SENSOR_NUM 56
-
-const int CPU_SENSORS[] = {1, 2, 3, 4, 7, 8, 9,10};
-
-#define CPU_NUM (sizeof(CPU_SENSORS) / sizeof(int))
-// Sum of CPU_NUM + 3 for GPU, BATTERY, and SKIN.
-#define TEMPERATURE_NUM (CPU_NUM + 3)
-
-//therm-reset-temp
-#define CPU_SHUTDOWN_THRESHOLD 115
-//limit-temp
-#define CPU_THROTTLING_THRESHOLD 60
-#define BATTERY_SHUTDOWN_THRESHOLD 60
-//must match thermal-engine.conf
-#define SKIN_THROTTLING_THRESHOLD 44
-#define SKIN_SHUTDOWN_THRESHOLD 70
-#define VR_THROTTLED_BELOW_MIN 58
-
-#define GPU_LABEL "GPU"
-#define BATTERY_LABEL "battery"
-#define SKIN_LABEL "skin"
-
-const char *CPU_LABEL[] = {"CPU0", "CPU1", "CPU2", "CPU3", "CPU4", "CPU5", "CPU6", "CPU7"};
-
-const char *get_cpu_label(unsigned int cpu_num) {
- ALOGD("Entering %s",__func__);
- if(cpu_num >= CPU_NUM)
- return NULL;
-
- return CPU_LABEL[cpu_num];
-}
-
-size_t get_num_cpus() {
- ALOGD("Entering %s",__func__);
- return CPU_NUM;
-}
-
-/**
- * Reads device temperature.
- *
- * @param sensor_num Number of sensor file with temperature.
- * @param type Device temperature type.
- * @param name Device temperature name.
- * @param mult Multiplier used to translate temperature to Celsius.
- * @param throttling_threshold Throttling threshold for the temperature.
- * @param shutdown_threshold Shutdown threshold for the temperature.
- * @param out Pointer to temperature_t structure that will be filled with current
- * values.
- *
- * @return 0 on success or negative value -errno on error.
- */
-static ssize_t read_temperature(int sensor_num, int type, const char *name, float mult,
- float throttling_threshold, float shutdown_threshold, float vr_throttling_threshold,
- temperature_t *out) {
- ALOGD("Entering %s",__func__);
- FILE *file;
- char file_name[MAX_LENGTH];
- float temp;
-
- snprintf(file_name, sizeof(file_name), TEMPERATURE_FILE_FORMAT, sensor_num);
- file = fopen(file_name, "r");
- if (file == NULL) {
- ALOGE("%s: failed to open: %s", __func__, strerror(errno));
- return -errno;
+#include "thermal_common.h"
+
+static char *cpu_sensors_845[] =
+{
+ "cpu0-silver-usr",
+ "cpu1-silver-usr",
+ "cpu2-silver-usr",
+ "cpu3-silver-usr",
+ "cpu0-gold-usr",
+ "cpu1-gold-usr",
+ "cpu2-gold-usr",
+ "cpu3-gold-usr",
+};
+
+static char *misc_sensors_845[] =
+{
+ "gpu0-usr",
+ "battery",
+ "xo-therm-adc"
+};
+
+static struct target_therm_cfg sensor_cfg_845[] = {
+ {
+ .type = DEVICE_TEMPERATURE_CPU,
+ .sensor_list = cpu_sensors_845,
+ .sens_cnt = ARRAY_SIZE(cpu_sensors_845),
+ .mult = 0.001,
+ .throt_thresh = 60,
+ .shutdwn_thresh = 115,
+ },
+ {
+ .type = DEVICE_TEMPERATURE_GPU,
+ .sensor_list = &misc_sensors_845[0],
+ .sens_cnt = 1,
+ .mult = 0.001,
+ .label = "GPU",
+ },
+ {
+ .type = DEVICE_TEMPERATURE_BATTERY,
+ .sensor_list = &misc_sensors_845[1],
+ .sens_cnt = 1,
+ .mult = 0.001,
+ .shutdwn_thresh = 60,
+ .label = "battery",
+ },
+ {
+ .type = DEVICE_TEMPERATURE_SKIN,
+ .sensor_list = &misc_sensors_845[2],
+ .sens_cnt = 1,
+ .mult = 0.001,
+ .throt_thresh = 44,
+ .shutdwn_thresh = 70,
+ .vr_thresh = 58,
+ .label = "skin",
}
- if (1 != fscanf(file, "%f", &temp)) {
- fclose(file);
- ALOGE("%s: failed to read a float: %s", __func__, strerror(errno));
- return errno ? -errno : -EIO;
- }
-
- fclose(file);
-
- (*out) = (temperature_t) {
- .type = type,
- .name = name,
- .current_value = temp * mult,
- .throttling_threshold = throttling_threshold,
- .shutdown_threshold = shutdown_threshold,
- .vr_throttling_threshold = vr_throttling_threshold
- };
-
- return 0;
-}
-
-static ssize_t get_cpu_temperatures(temperature_t *list, size_t size) {
- ALOGD("Entering %s",__func__);
- size_t cpu;
- for (cpu = 0; cpu < CPU_NUM; cpu++) {
- if (cpu >= size) {
- break;
- }
- // tsens_tz_sensor[4,6,9,11]: temperature in decidegrees Celsius.
- ssize_t result = read_temperature(CPU_SENSORS[cpu], DEVICE_TEMPERATURE_CPU, CPU_LABEL[cpu],
- 0.001, CPU_THROTTLING_THRESHOLD, CPU_SHUTDOWN_THRESHOLD, UNKNOWN_TEMPERATURE,
- &list[cpu]);
- if (result != 0) {
- return result;
- }
- }
-
- return cpu;
-}
+};
ssize_t get_temperatures(thermal_module_t *module, temperature_t *list, size_t size) {
ALOGD("Entering %s",__func__);
- ssize_t result = 0;
- size_t current_index = 0;
-
- if (list == NULL) {
- return TEMPERATURE_NUM;
- }
-
- result = get_cpu_temperatures(list, size);
- if (result < 0) {
- return result;
- }
- current_index += result;
-
- // GPU temperature.
- if (current_index < size) {
- // tsens_tz_sensor14: temperature in millidegrees Celsius.
- result = read_temperature(GPU_SENSOR_NUM, DEVICE_TEMPERATURE_GPU, GPU_LABEL, 0.001,
- UNKNOWN_TEMPERATURE, UNKNOWN_TEMPERATURE, UNKNOWN_TEMPERATURE,
- &list[current_index]);
- if (result < 0) {
- return result;
+ static int thermal_sens_size;
+
+ if (!thermal_sens_size) {
+ thermal_sens_size = thermal_zone_init(sensor_cfg_845,
+ ARRAY_SIZE(sensor_cfg_845));
+ if (thermal_sens_size <= 0) {
+ ALOGE("thermal sensor initialization is failed\n");
+ thermal_sens_size = 0;
+ return 0;
}
- current_index++;
}
- // Battery temperature.
- if (current_index < size) {
- // tsens_tz_sensor29: battery: temperature in millidegrees Celsius.
- result = read_temperature(BATTERY_SENSOR_NUM, DEVICE_TEMPERATURE_BATTERY, BATTERY_LABEL,
- 0.001, UNKNOWN_TEMPERATURE, BATTERY_SHUTDOWN_THRESHOLD, UNKNOWN_TEMPERATURE,
- &list[current_index]);
- if (result < 0) {
- return result;
- }
- current_index++;
- }
+ if (list == NULL)
+ return thermal_sens_size;
- // Skin temperature.
- if (current_index < size) {
- // tsens_tz_sensor24: temperature in millidegrees Celsius.
- result = read_temperature(SKIN_SENSOR_NUM, DEVICE_TEMPERATURE_SKIN, SKIN_LABEL, .001,
- SKIN_THROTTLING_THRESHOLD, SKIN_SHUTDOWN_THRESHOLD, VR_THROTTLED_BELOW_MIN,
- &list[current_index]);
- if (result < 0) {
- return result;
- }
- current_index++;
- }
- return TEMPERATURE_NUM;
+ return get_temperature_for_all(list, size);
}
diff --git a/thermal-8998.c b/thermal-8998.c
index a43159b..94bf375 100644
--- a/thermal-8998.c
+++ b/thermal-8998.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2017, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2017-2018 The Linux Foundation. All rights reserved.
* Not a contribution
* Copyright (C) 2016 The Android Open Source Project
*
@@ -16,178 +16,85 @@
* limitations under the License.
*/
-#include <ctype.h>
-#include <errno.h>
-#include <inttypes.h>
-#include <stdlib.h>
-#include <string.h>
#define LOG_TAG "ThermalHAL-8998"
#include <utils/Log.h>
#include <hardware/hardware.h>
#include <hardware/thermal.h>
+#include "thermal_common.h"
-#define MAX_LENGTH 50
-
-#define TEMPERATURE_FILE_FORMAT "/sys/class/thermal/thermal_zone%d/temp"
-
-#define BATTERY_SENSOR_NUM 0
-#define GPU_SENSOR_NUM 17
-#define SKIN_SENSOR_NUM 5
-
-const int CPU_SENSORS[] = {8, 9, 10, 11, 12, 13, 14,15};
-
-#define CPU_NUM (sizeof(CPU_SENSORS) / sizeof(int))
-// Sum of CPU_NUM + 3 for GPU, BATTERY, and SKIN.
-#define TEMPERATURE_NUM (CPU_NUM + 3)
-
-//therm-reset-temp
-#define CPU_SHUTDOWN_THRESHOLD 115
-//limit-temp
-#define CPU_THROTTLING_THRESHOLD 60
-#define BATTERY_SHUTDOWN_THRESHOLD 60
-//must match thermal-engine.conf
-#define SKIN_THROTTLING_THRESHOLD 44
-#define SKIN_SHUTDOWN_THRESHOLD 70
-#define VR_THROTTLED_BELOW_MIN 58
-
-#define GPU_LABEL "GPU"
-#define BATTERY_LABEL "battery"
-#define SKIN_LABEL "skin"
-
-const char *CPU_LABEL[] = {"CPU0", "CPU1", "CPU2", "CPU3", "CPU4", "CPU5", "CPU6", "CPU7"};
-
-const char *get_cpu_label(unsigned int cpu_num) {
-
- if(cpu_num >= CPU_NUM)
- return NULL;
-
- return CPU_LABEL[cpu_num];
-}
-
-size_t get_num_cpus() {
- return CPU_NUM;
-}
-
-/**
- * Reads device temperature.
- *
- * @param sensor_num Number of sensor file with temperature.
- * @param type Device temperature type.
- * @param name Device temperature name.
- * @param mult Multiplier used to translate temperature to Celsius.
- * @param throttling_threshold Throttling threshold for the temperature.
- * @param shutdown_threshold Shutdown threshold for the temperature.
- * @param out Pointer to temperature_t structure that will be filled with current
- * values.
- *
- * @return 0 on success or negative value -errno on error.
- */
-static ssize_t read_temperature(int sensor_num, int type, const char *name, float mult,
- float throttling_threshold, float shutdown_threshold, float vr_throttling_threshold,
- temperature_t *out)
+static char *cpu_sensors_8998[] =
{
- ALOGD("Entering %s",__func__);
- FILE *file;
- char file_name[MAX_LENGTH];
- float temp;
-
- snprintf(file_name, sizeof(file_name), TEMPERATURE_FILE_FORMAT, sensor_num);
- file = fopen(file_name, "r");
- if (file == NULL) {
- ALOGE("%s: failed to open: %s", __func__, strerror(errno));
- return -errno;
- }
- if (1 != fscanf(file, "%f", &temp)) {
- fclose(file);
- ALOGE("%s: failed to read a float: %s", __func__, strerror(errno));
- return errno ? -errno : -EIO;
- }
-
- fclose(file);
-
- (*out) = (temperature_t) {
- .type = type,
- .name = name,
- .current_value = temp * mult,
- .throttling_threshold = throttling_threshold,
- .shutdown_threshold = shutdown_threshold,
- .vr_throttling_threshold = vr_throttling_threshold
- };
-
- return 0;
-}
-
-static ssize_t get_cpu_temperatures(temperature_t *list, size_t size)
+ "tsens_tz_sensor1",
+ "tsens_tz_sensor2",
+ "tsens_tz_sensor3",
+ "tsens_tz_sensor4",
+ "tsens_tz_sensor7",
+ "tsens_tz_sensor8",
+ "tsens_tz_sensor9",
+ "tsens_tz_sensor10",
+};
+
+static char *misc_sensors_8998[] =
{
- ALOGD("Entering %s",__func__);
- size_t cpu;
- for (cpu = 0; cpu < CPU_NUM; cpu++) {
- if (cpu >= size) {
- break;
- }
- // tsens_tz_sensor[4,6,9,11]: temperature in decidegrees Celsius.
- ssize_t result = read_temperature(CPU_SENSORS[cpu], DEVICE_TEMPERATURE_CPU, CPU_LABEL[cpu],
- 0.1, CPU_THROTTLING_THRESHOLD, CPU_SHUTDOWN_THRESHOLD, UNKNOWN_TEMPERATURE,
- &list[cpu]);
- if (result != 0) {
- return result;
- }
+ "tsens_tz_sensor12",
+ "battery",
+ "quiet_therm"
+};
+
+static struct target_therm_cfg sensor_cfg_8998[] = {
+ {
+ .type = DEVICE_TEMPERATURE_CPU,
+ .sensor_list = cpu_sensors_8998,
+ .sens_cnt = ARRAY_SIZE(cpu_sensors_8998),
+ .mult = 0.1,
+ .throt_thresh = 60,
+ .shutdwn_thresh = 115,
+ },
+ {
+ .type = DEVICE_TEMPERATURE_GPU,
+ .sensor_list = &misc_sensors_8998[0],
+ .sens_cnt = 1,
+ .mult = 0.1,
+ .label = "GPU",
+ },
+ {
+ .type = DEVICE_TEMPERATURE_BATTERY,
+ .sensor_list = &misc_sensors_8998[1],
+ .sens_cnt = 1,
+ .mult = 0.001,
+ .shutdwn_thresh = 60,
+ .label = "battery",
+ },
+ {
+ .type = DEVICE_TEMPERATURE_SKIN,
+ .sensor_list = &misc_sensors_8998[2],
+ .sens_cnt = 1,
+ .mult = 1.0,
+ .throt_thresh = 44,
+ .shutdwn_thresh = 70,
+ .vr_thresh = 58,
+ .label = "skin",
}
-
- return cpu;
-}
+};
ssize_t get_temperatures(thermal_module_t *module, temperature_t *list, size_t size) {
ALOGD("Entering %s",__func__);
- ssize_t result = 0;
- size_t current_index = 0;
-
- if (list == NULL) {
- return TEMPERATURE_NUM;
- }
-
- result = get_cpu_temperatures(list, size);
- if (result < 0) {
- return result;
- }
- current_index += result;
-
- // GPU temperature.
- if (current_index < size) {
- // tsens_tz_sensor14: temperature in decidegrees Celsius.
- result = read_temperature(GPU_SENSOR_NUM, DEVICE_TEMPERATURE_GPU, GPU_LABEL, 0.1,
- UNKNOWN_TEMPERATURE, UNKNOWN_TEMPERATURE, UNKNOWN_TEMPERATURE,
- &list[current_index]);
- if (result < 0) {
- return result;
+ static int thermal_sens_size;
+
+ if (!thermal_sens_size) {
+ thermal_sens_size = thermal_zone_init(sensor_cfg_8998,
+ ARRAY_SIZE(sensor_cfg_8998));
+ if (thermal_sens_size <= 0) {
+ ALOGE("thermal sensor initialization is failed\n");
+ thermal_sens_size = 0;
+ return 0;
}
- current_index++;
}
- // Battery temperature.
- if (current_index < size) {
- // tsens_tz_sensor29: battery: temperature in millidegrees Celsius.
- result = read_temperature(BATTERY_SENSOR_NUM, DEVICE_TEMPERATURE_BATTERY, BATTERY_LABEL,
- 0.001, UNKNOWN_TEMPERATURE, BATTERY_SHUTDOWN_THRESHOLD, UNKNOWN_TEMPERATURE,
- &list[current_index]);
- if (result < 0) {
- return result;
- }
- current_index++;
- }
+ if (list == NULL)
+ return thermal_sens_size;
- // Skin temperature.
- if (current_index < size) {
- // tsens_tz_sensor24: temperature in Celsius.
- result = read_temperature(SKIN_SENSOR_NUM, DEVICE_TEMPERATURE_SKIN, SKIN_LABEL, 1.,
- SKIN_THROTTLING_THRESHOLD, SKIN_SHUTDOWN_THRESHOLD, VR_THROTTLED_BELOW_MIN,
- &list[current_index]);
- if (result < 0) {
- return result;
- }
- current_index++;
- }
- return TEMPERATURE_NUM;
+ return get_temperature_for_all(list, size);
}
diff --git a/thermal_common.c b/thermal_common.c
new file mode 100644
index 0000000..803b0a1
--- /dev/null
+++ b/thermal_common.c
@@ -0,0 +1,385 @@
+/*
+ * Copyright (c) 2018, The Linux Foundation. All rights reserved.
+ * Not a contribution
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <errno.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <dirent.h>
+
+#define LOG_TAG "ThermalHAL-UTIL"
+#include <utils/Log.h>
+
+#include <hardware/hardware.h>
+#include <hardware/thermal.h>
+#include "thermal_common.h"
+
+#define MAX_LENGTH 50
+#define MAX_PATH (256)
+#define CPU_LABEL "CPU%d"
+#define THERMAL_SYSFS "/sys/devices/virtual/thermal"
+#define TZ_DIR_NAME "thermal_zone"
+#define TZ_DIR_FMT "thermal_zone%d"
+#define THERMAL_TYPE "/sys/devices/virtual/thermal/%s/type"
+#define TEMPERATURE_FILE_FORMAT "/sys/class/thermal/thermal_zone%d/temp"
+
+static char **cpu_label;
+static struct vendor_temperature *sensors;
+static unsigned int sensor_cnt;
+
+/**
+ * Get number of cpus of target.
+ *
+ * @return number of cpus on success or 0 on error.
+ */
+size_t get_num_cpus() {
+ ALOGD("Entering %s",__func__);
+ static int ncpus;
+
+ if (!ncpus) {
+ ncpus = (int)sysconf(_SC_NPROCESSORS_CONF);
+ if (ncpus < 1)
+ ALOGE("%s: Error retrieving number of cores", __func__);
+ }
+ return ncpus;
+}
+
+/**
+ * Get cpu label for a given cpu.
+ *
+ * @param cpu_num: cpu number.
+ *
+ * @return cpu label string on success or NULL on error.
+ */
+const char *get_cpu_label(unsigned int cpu_num) {
+ ALOGD("Entering %s",__func__);
+ unsigned int cpu = 0;
+
+ if (cpu_label == NULL) {
+ cpu_label= (char**)calloc(get_num_cpus(), sizeof(char *));
+ if (!cpu_label)
+ return NULL;
+ for(cpu = 0; cpu < get_num_cpus(); cpu++) {
+ cpu_label[cpu] = (char *)calloc(sizeof("CPUN"), sizeof(char));
+ if(!cpu_label[cpu])
+ return NULL;
+ snprintf(cpu_label[cpu], sizeof("CPUN"), CPU_LABEL, cpu);
+ }
+ }
+ if(cpu_num >= get_num_cpus())
+ return NULL;
+
+ return cpu_label[cpu_num];
+}
+
+/**
+ * Read data from a target sysfs file.
+ *
+ * @param path: Absolute path for a file to be read.
+ * @param buf: Char buffer to store data from file.
+ * @param count: Size of data buffer.
+ *
+ * @return number of bytes read on success or negative value on error.
+ */
+static int read_line_from_file(const char *path, char *buf, size_t count)
+{
+ char * fgets_ret;
+ FILE * fd;
+ int rv;
+
+ fd = fopen(path, "r");
+ if (fd == NULL)
+ return -1;
+
+ fgets_ret = fgets(buf, (int)count, fd);
+ if (NULL != fgets_ret) {
+ rv = (int)strlen(buf);
+ } else {
+ rv = ferror(fd);
+ }
+
+ fclose(fd);
+
+ return rv;
+}
+
+/**
+ * Function to get thermal zone id from sensor name.
+ *
+ * @param sensor_name: Name of sensor.
+ *
+ * @return positive integer on success or negative value on error.
+ */
+static int get_tzn(const char *sensor_name)
+{
+ DIR *tdir = NULL;
+ struct dirent *tdirent = NULL;
+ int found = -1;
+ int tzn = 0;
+ char name[MAX_PATH] = {0};
+ char cwd[MAX_PATH] = {0};
+ int ret = 0;
+
+ if (!getcwd(cwd, sizeof(cwd)))
+ return found;
+
+ /* Change dir to read the entries. Doesnt work otherwise */
+ ret = chdir(THERMAL_SYSFS);
+ if (ret) {
+ ALOGE("Unable to change to %s\n", THERMAL_SYSFS);
+ return found;
+ }
+ tdir = opendir(THERMAL_SYSFS);
+ if (!tdir) {
+ ALOGE("Unable to open %s\n", THERMAL_SYSFS);
+ return found;
+ }
+
+ while ((tdirent = readdir(tdir))) {
+ char buf[50];
+ struct dirent *tzdirent;
+ DIR *tzdir = NULL;
+
+ if (strncmp(tdirent->d_name, TZ_DIR_NAME,
+ strlen(TZ_DIR_NAME)) != 0)
+ continue;
+
+ tzdir = opendir(tdirent->d_name);
+ if (!tzdir)
+ continue;
+ while ((tzdirent = readdir(tzdir))) {
+ if (strcmp(tzdirent->d_name, "type"))
+ continue;
+ snprintf(name, MAX_PATH, THERMAL_TYPE,
+ tdirent->d_name);
+ ret = read_line_from_file(name, buf, sizeof(buf));
+ if (ret <= 0) {
+ ALOGE("%s: sensor name read error for tz:%s\n",
+ __func__, tdirent->d_name);
+ break;
+ }
+ if (buf[ret - 1] == '\n')
+ buf[ret - 1] = '\0';
+ else
+ buf[ret] = '\0';
+
+ if (!strcmp(buf, sensor_name)) {
+ found = 1;
+ break;
+ }
+ }
+ closedir(tzdir);
+ if (found == 1)
+ break;
+ }
+
+ if (found == 1) {
+ sscanf(tdirent->d_name, TZ_DIR_FMT, &tzn);
+ ALOGD("Sensor %s found at tz: %d\n",
+ sensor_name, tzn);
+ found = tzn;
+ }
+
+ closedir(tdir);
+ /* Restore current working dir */
+ ret = chdir(cwd);
+
+ return found;
+}
+
+/**
+ * Helper function for sensor intialization.
+ *
+ * @param v_sen_t: pointer to a sensor static config.
+ * @param sensor: pointer to a sensor vendor_temperature structure.
+ * @param type: Type of sensor ie cpu, battery, gpu, skin etc.
+ * @param sens_idx: Index for sensor of same type.
+ *
+ * @return 0 on success or negative value -errno on error.
+ */
+static int initialize_sensor(struct target_therm_cfg *v_sen_t,
+ struct vendor_temperature *sensor,
+ enum temperature_type type,
+ int sens_idx)
+{
+ if (v_sen_t == NULL || sensor == NULL ||
+ sens_idx < 0) {
+ ALOGE("%s:Invalid input, sens_idx%d\n", __func__, sens_idx);
+ return -1;
+ }
+
+ sensor->tzn = get_tzn(v_sen_t->sensor_list[sens_idx]);
+ if (sensor->tzn < 0) {
+ ALOGE("No thermal zone for sensor: %s, ret:%d\n",
+ v_sen_t->sensor_list[sens_idx], sensor->tzn);
+ return -1;
+ }
+ if (type == DEVICE_TEMPERATURE_CPU)
+ sensor->t.name = get_cpu_label(sens_idx);
+ else
+ sensor->t.name = v_sen_t->label;
+
+ sensor->t.type = v_sen_t->type;
+ sensor->mult = v_sen_t->mult;
+
+ if (v_sen_t->throt_thresh != 0)
+ sensor->t.throttling_threshold = v_sen_t->throt_thresh;
+ else
+ sensor->t.throttling_threshold = UNKNOWN_TEMPERATURE;
+
+ if (v_sen_t->shutdwn_thresh != 0)
+ sensor->t.shutdown_threshold = v_sen_t->shutdwn_thresh;
+ else
+ sensor->t.shutdown_threshold = UNKNOWN_TEMPERATURE;
+
+ if (v_sen_t->vr_thresh != 0)
+ sensor->t.vr_throttling_threshold = v_sen_t->vr_thresh;
+ else
+ sensor->t.vr_throttling_threshold = UNKNOWN_TEMPERATURE;
+
+ return 0;
+}
+
+/**
+ * Initialize all sensors.
+ *
+ * @param v_sen_t: Base pointer to array of target specific sensor configs.
+ * @param cfg_cnt: Number of set of config for a given target.
+ *
+ * @return number of sensor on success or negative value or zero on error.
+ */
+int thermal_zone_init(struct target_therm_cfg *v_sen_t, int cfg_cnt)
+{
+ unsigned int idx = 0, cpu = 0;
+ int j = 0;
+
+ if (sensors != NULL && sensor_cnt != 0)
+ return sensor_cnt;
+
+ if (v_sen_t == NULL || cfg_cnt == 0) {
+ ALOGE("%s:Invalid input\n", __func__);
+ return -1;
+ }
+ sensors = calloc(get_num_cpus() + cfg_cnt - 1,
+ sizeof(struct vendor_temperature));
+
+ for (j = 0, idx = 0; j < cfg_cnt &&
+ idx < (get_num_cpus() + cfg_cnt - 1); j++) {
+ if (v_sen_t[j].type == DEVICE_TEMPERATURE_CPU) {
+ /* Initialize cpu thermal zone id */
+ for (cpu = 0; cpu < get_num_cpus() &&
+ idx < (get_num_cpus() + cfg_cnt - 1); cpu++, idx++) {
+ if (initialize_sensor(&v_sen_t[j], &sensors[idx],
+ v_sen_t[j].type, cpu)) {
+ free(sensors);
+ return -1;
+ }
+ }
+ } else {
+ /* Initialize misc thermal zone id */
+ if (initialize_sensor(&v_sen_t[j], &sensors[idx],
+ v_sen_t[j].type, 0)) {
+ free(sensors);
+ return -1;
+ }
+ idx++;
+ }
+ }
+ sensor_cnt = idx;
+
+ return sensor_cnt;
+}
+
+/**
+ * Reads sensor temperature.
+ *
+ * @param sensor_num: thermal zone id for the sensor to be read
+ * @param type: Device temperature type.
+ * @param name: Device temperature name.
+ * @param mult: Multiplier used to translate temperature to Celsius.
+ * @param throttling_threshold: Throttling threshold for the sensor.
+ * @param shutdown_threshold: Shutdown threshold for the sensor.
+ * @param out: Pointer to temperature_t structure that will be filled with
+ * temperature values.
+ *
+ * @return 0 on success or negative value -errno on error.
+ */
+static ssize_t read_temperature(int sensor_num, int type, const char *name,
+ float mult, float throttling_threshold, float shutdown_threshold,
+ float vr_throttling_threshold,
+ temperature_t *out) {
+ ALOGD("Entering %s",__func__);
+ char file_name[MAX_LENGTH];
+ float temp;
+ char buf[16] = {0};
+ int ret = 0;
+
+ snprintf(file_name, sizeof(file_name), TEMPERATURE_FILE_FORMAT, sensor_num);
+ ret = read_line_from_file(file_name, buf, sizeof(buf));
+ if (ret <= 0) {
+ ALOGE("Temperature read error: %d for sensor[%d]:%s\n",
+ ret, sensor_num, name);
+ return -1;
+ }
+ temp = atof(buf);
+
+ (*out) = (temperature_t) {
+ .type = type,
+ .name = name,
+ .current_value = temp * mult,
+ .throttling_threshold = throttling_threshold,
+ .shutdown_threshold = shutdown_threshold,
+ .vr_throttling_threshold = vr_throttling_threshold
+ };
+
+ return 0;
+}
+
+/**
+ * Reads all sensor temperature.
+ *
+ * @param list: Base pointer to array of temperature_t structure that will be
+ * filled with temperature values.
+ * @param size: Number of sensor temperature needs to be filled in list.
+ *
+ * @return number of sensor data filled on success or 0 or negative value
+ * -errno on error.
+ */
+ssize_t get_temperature_for_all(temperature_t *list, size_t size)
+{
+ ALOGD("Entering %s",__func__);
+ size_t idx;
+
+ if (sensors == NULL) {
+ ALOGE("No sensor configured\n");
+ return 0;
+ }
+
+ for (idx = 0; idx < sensor_cnt && idx < size; idx++) {
+ ssize_t result = read_temperature(sensors[idx].tzn, sensors[idx].t.type,
+ sensors[idx].t.name, sensors[idx].mult,
+ sensors[idx].t.throttling_threshold,
+ sensors[idx].t.shutdown_threshold,
+ sensors[idx].t.vr_throttling_threshold,
+ &list[idx]);
+ if (result != 0)
+ return result;
+ }
+ return idx;
+}
+
diff --git a/thermal_common.h b/thermal_common.h
new file mode 100644
index 0000000..4137322
--- /dev/null
+++ b/thermal_common.h
@@ -0,0 +1,52 @@
+/* Copyright (c) 2018, The Linux Foundation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ * * Neither the name of The Linux Foundation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <hardware/thermal.h>
+#define ARRAY_SIZE(x) (int)(sizeof(x)/sizeof(x[0]))
+
+struct target_therm_cfg {
+ enum temperature_type type;
+ char **sensor_list;
+ uint8_t sens_cnt;
+ char *label;
+ float mult;
+ int throt_thresh;
+ int shutdwn_thresh;
+ int vr_thresh;
+};
+
+struct vendor_temperature {
+ int tzn;
+ float mult;
+ temperature_t t;
+};
+
+size_t get_num_cpus();
+const char *get_cpu_label(unsigned int cpu_num);
+int thermal_zone_init(struct target_therm_cfg *v_sen_t, int cfg_cnt);
+ssize_t get_temperature_for_all(temperature_t *list, size_t size);