1cd5e85f5SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
294f69966SJacob Pan /*
394f69966SJacob Pan * sysfs.c sysfs ABI access functions for TMON program
494f69966SJacob Pan *
594f69966SJacob Pan * Copyright (C) 2013 Intel Corporation. All rights reserved.
694f69966SJacob Pan *
794f69966SJacob Pan * Author: Jacob Pan <jacob.jun.pan@linux.intel.com>
894f69966SJacob Pan */
994f69966SJacob Pan #include <unistd.h>
1094f69966SJacob Pan #include <stdio.h>
1194f69966SJacob Pan #include <stdlib.h>
1294f69966SJacob Pan #include <string.h>
1394f69966SJacob Pan #include <stdint.h>
1494f69966SJacob Pan #include <dirent.h>
1594f69966SJacob Pan #include <libintl.h>
16*6c58cf40SFlorian Fainelli #include <limits.h>
1794f69966SJacob Pan #include <ctype.h>
1894f69966SJacob Pan #include <time.h>
1994f69966SJacob Pan #include <syslog.h>
2094f69966SJacob Pan #include <sys/time.h>
2194f69966SJacob Pan #include <errno.h>
2294f69966SJacob Pan
2394f69966SJacob Pan #include "tmon.h"
2494f69966SJacob Pan
2594f69966SJacob Pan struct tmon_platform_data ptdata;
2694f69966SJacob Pan const char *trip_type_name[] = {
2794f69966SJacob Pan "critical",
2894f69966SJacob Pan "hot",
2994f69966SJacob Pan "passive",
3094f69966SJacob Pan "active",
3194f69966SJacob Pan };
3294f69966SJacob Pan
sysfs_set_ulong(char * path,char * filename,unsigned long val)3394f69966SJacob Pan int sysfs_set_ulong(char *path, char *filename, unsigned long val)
3494f69966SJacob Pan {
3594f69966SJacob Pan FILE *fd;
3694f69966SJacob Pan int ret = -1;
37*6c58cf40SFlorian Fainelli char filepath[PATH_MAX + 2]; /* NUL and '/' */
3894f69966SJacob Pan
39*6c58cf40SFlorian Fainelli snprintf(filepath, sizeof(filepath), "%s/%s", path, filename);
4094f69966SJacob Pan
4194f69966SJacob Pan fd = fopen(filepath, "w");
4294f69966SJacob Pan if (!fd) {
4394f69966SJacob Pan syslog(LOG_ERR, "Err: open %s: %s\n", __func__, filepath);
4494f69966SJacob Pan return ret;
4594f69966SJacob Pan }
4694f69966SJacob Pan ret = fprintf(fd, "%lu", val);
4794f69966SJacob Pan fclose(fd);
4894f69966SJacob Pan
4994f69966SJacob Pan return 0;
5094f69966SJacob Pan }
5194f69966SJacob Pan
5294f69966SJacob Pan /* history of thermal data, used for control algo */
5394f69966SJacob Pan #define NR_THERMAL_RECORDS 3
5494f69966SJacob Pan struct thermal_data_record trec[NR_THERMAL_RECORDS];
5594f69966SJacob Pan int cur_thermal_record; /* index to the trec array */
5694f69966SJacob Pan
sysfs_get_ulong(char * path,char * filename,unsigned long * p_ulong)5794f69966SJacob Pan static int sysfs_get_ulong(char *path, char *filename, unsigned long *p_ulong)
5894f69966SJacob Pan {
5994f69966SJacob Pan FILE *fd;
6094f69966SJacob Pan int ret = -1;
61*6c58cf40SFlorian Fainelli char filepath[PATH_MAX + 2]; /* NUL and '/' */
6294f69966SJacob Pan
63*6c58cf40SFlorian Fainelli snprintf(filepath, sizeof(filepath), "%s/%s", path, filename);
6494f69966SJacob Pan
6594f69966SJacob Pan fd = fopen(filepath, "r");
6694f69966SJacob Pan if (!fd) {
6794f69966SJacob Pan syslog(LOG_ERR, "Err: open %s: %s\n", __func__, filepath);
6894f69966SJacob Pan return ret;
6994f69966SJacob Pan }
7094f69966SJacob Pan ret = fscanf(fd, "%lu", p_ulong);
7194f69966SJacob Pan fclose(fd);
7294f69966SJacob Pan
7394f69966SJacob Pan return 0;
7494f69966SJacob Pan }
7594f69966SJacob Pan
sysfs_get_string(char * path,char * filename,char * str)7694f69966SJacob Pan static int sysfs_get_string(char *path, char *filename, char *str)
7794f69966SJacob Pan {
7894f69966SJacob Pan FILE *fd;
7994f69966SJacob Pan int ret = -1;
80*6c58cf40SFlorian Fainelli char filepath[PATH_MAX + 2]; /* NUL and '/' */
8194f69966SJacob Pan
82*6c58cf40SFlorian Fainelli snprintf(filepath, sizeof(filepath), "%s/%s", path, filename);
8394f69966SJacob Pan
8494f69966SJacob Pan fd = fopen(filepath, "r");
8594f69966SJacob Pan if (!fd) {
8694f69966SJacob Pan syslog(LOG_ERR, "Err: open %s: %s\n", __func__, filepath);
8794f69966SJacob Pan return ret;
8894f69966SJacob Pan }
8994f69966SJacob Pan ret = fscanf(fd, "%256s", str);
9094f69966SJacob Pan fclose(fd);
9194f69966SJacob Pan
9294f69966SJacob Pan return ret;
9394f69966SJacob Pan }
9494f69966SJacob Pan
9594f69966SJacob Pan /* get states of the cooling device instance */
probe_cdev(struct cdev_info * cdi,char * path)9694f69966SJacob Pan static int probe_cdev(struct cdev_info *cdi, char *path)
9794f69966SJacob Pan {
9894f69966SJacob Pan sysfs_get_string(path, "type", cdi->type);
9994f69966SJacob Pan sysfs_get_ulong(path, "max_state", &cdi->max_state);
10094f69966SJacob Pan sysfs_get_ulong(path, "cur_state", &cdi->cur_state);
10194f69966SJacob Pan
10294f69966SJacob Pan syslog(LOG_INFO, "%s: %s: type %s, max %lu, curr %lu inst %d\n",
10394f69966SJacob Pan __func__, path,
10494f69966SJacob Pan cdi->type, cdi->max_state, cdi->cur_state, cdi->instance);
10594f69966SJacob Pan
10694f69966SJacob Pan return 0;
10794f69966SJacob Pan }
10894f69966SJacob Pan
str_to_trip_type(char * name)10994f69966SJacob Pan static int str_to_trip_type(char *name)
11094f69966SJacob Pan {
11194f69966SJacob Pan int i;
11294f69966SJacob Pan
11394f69966SJacob Pan for (i = 0; i < NR_THERMAL_TRIP_TYPE; i++) {
11494f69966SJacob Pan if (!strcmp(name, trip_type_name[i]))
11594f69966SJacob Pan return i;
11694f69966SJacob Pan }
11794f69966SJacob Pan
11894f69966SJacob Pan return -ENOENT;
11994f69966SJacob Pan }
12094f69966SJacob Pan
12194f69966SJacob Pan /* scan and fill in trip point info for a thermal zone and trip point id */
get_trip_point_data(char * tz_path,int tzid,int tpid)12294f69966SJacob Pan static int get_trip_point_data(char *tz_path, int tzid, int tpid)
12394f69966SJacob Pan {
12494f69966SJacob Pan char filename[256];
12594f69966SJacob Pan char temp_str[256];
12694f69966SJacob Pan int trip_type;
12794f69966SJacob Pan
12894f69966SJacob Pan if (tpid >= MAX_NR_TRIP)
12994f69966SJacob Pan return -EINVAL;
13094f69966SJacob Pan /* check trip point type */
13194f69966SJacob Pan snprintf(filename, sizeof(filename), "trip_point_%d_type", tpid);
13294f69966SJacob Pan sysfs_get_string(tz_path, filename, temp_str);
13394f69966SJacob Pan trip_type = str_to_trip_type(temp_str);
13494f69966SJacob Pan if (trip_type < 0) {
13594f69966SJacob Pan syslog(LOG_ERR, "%s:%s no matching type\n", __func__, temp_str);
13694f69966SJacob Pan return -ENOENT;
13794f69966SJacob Pan }
13894f69966SJacob Pan ptdata.tzi[tzid].tp[tpid].type = trip_type;
13994f69966SJacob Pan syslog(LOG_INFO, "%s:tz:%d tp:%d:type:%s type id %d\n", __func__, tzid,
14094f69966SJacob Pan tpid, temp_str, trip_type);
14194f69966SJacob Pan
14294f69966SJacob Pan /* TODO: check attribute */
14394f69966SJacob Pan
14494f69966SJacob Pan return 0;
14594f69966SJacob Pan }
14694f69966SJacob Pan
14794f69966SJacob Pan /* return instance id for file format such as trip_point_4_temp */
get_instance_id(char * name,int pos,int skip)14894f69966SJacob Pan static int get_instance_id(char *name, int pos, int skip)
14994f69966SJacob Pan {
15094f69966SJacob Pan char *ch;
15194f69966SJacob Pan int i = 0;
15294f69966SJacob Pan
15394f69966SJacob Pan ch = strtok(name, "_");
15494f69966SJacob Pan while (ch != NULL) {
15594f69966SJacob Pan ++i;
15694f69966SJacob Pan syslog(LOG_INFO, "%s:%s:%s:%d", __func__, name, ch, i);
15794f69966SJacob Pan ch = strtok(NULL, "_");
15894f69966SJacob Pan if (pos == i)
15994f69966SJacob Pan return atol(ch + skip);
16094f69966SJacob Pan }
16194f69966SJacob Pan
16294f69966SJacob Pan return -1;
16394f69966SJacob Pan }
16494f69966SJacob Pan
16594f69966SJacob Pan /* Find trip point info of a thermal zone */
find_tzone_tp(char * tz_name,char * d_name,struct tz_info * tzi,int tz_id)16694f69966SJacob Pan static int find_tzone_tp(char *tz_name, char *d_name, struct tz_info *tzi,
16794f69966SJacob Pan int tz_id)
16894f69966SJacob Pan {
16994f69966SJacob Pan int tp_id;
17094f69966SJacob Pan unsigned long temp_ulong;
17194f69966SJacob Pan
17294f69966SJacob Pan if (strstr(d_name, "trip_point") &&
17394f69966SJacob Pan strstr(d_name, "temp")) {
17494f69966SJacob Pan /* check if trip point temp is non-zero
17594f69966SJacob Pan * ignore 0/invalid trip points
17694f69966SJacob Pan */
17794f69966SJacob Pan sysfs_get_ulong(tz_name, d_name, &temp_ulong);
17894f69966SJacob Pan if (temp_ulong < MAX_TEMP_KC) {
17994f69966SJacob Pan tzi->nr_trip_pts++;
18094f69966SJacob Pan /* found a valid trip point */
18194f69966SJacob Pan tp_id = get_instance_id(d_name, 2, 0);
18294f69966SJacob Pan syslog(LOG_DEBUG, "tzone %s trip %d temp %lu tpnode %s",
18394f69966SJacob Pan tz_name, tp_id, temp_ulong, d_name);
18494f69966SJacob Pan if (tp_id < 0 || tp_id >= MAX_NR_TRIP) {
18594f69966SJacob Pan syslog(LOG_ERR, "Failed to find TP inst %s\n",
18694f69966SJacob Pan d_name);
18794f69966SJacob Pan return -1;
18894f69966SJacob Pan }
18994f69966SJacob Pan get_trip_point_data(tz_name, tz_id, tp_id);
19094f69966SJacob Pan tzi->tp[tp_id].temp = temp_ulong;
19194f69966SJacob Pan }
19294f69966SJacob Pan }
19394f69966SJacob Pan
19494f69966SJacob Pan return 0;
19594f69966SJacob Pan }
19694f69966SJacob Pan
19794f69966SJacob Pan /* check cooling devices for binding info. */
find_tzone_cdev(struct dirent * nl,char * tz_name,struct tz_info * tzi,int tz_id,int cid)19894f69966SJacob Pan static int find_tzone_cdev(struct dirent *nl, char *tz_name,
19994f69966SJacob Pan struct tz_info *tzi, int tz_id, int cid)
20094f69966SJacob Pan {
20194f69966SJacob Pan unsigned long trip_instance = 0;
20294f69966SJacob Pan char cdev_name_linked[256];
203*6c58cf40SFlorian Fainelli char cdev_name[PATH_MAX];
204*6c58cf40SFlorian Fainelli char cdev_trip_name[PATH_MAX];
20594f69966SJacob Pan int cdev_id;
20694f69966SJacob Pan
20794f69966SJacob Pan if (nl->d_type == DT_LNK) {
20894f69966SJacob Pan syslog(LOG_DEBUG, "TZ%d: cdev: %s cid %d\n", tz_id, nl->d_name,
20994f69966SJacob Pan cid);
21094f69966SJacob Pan tzi->nr_cdev++;
21194f69966SJacob Pan if (tzi->nr_cdev > ptdata.nr_cooling_dev) {
21294f69966SJacob Pan syslog(LOG_ERR, "Err: Too many cdev? %d\n",
21394f69966SJacob Pan tzi->nr_cdev);
21494f69966SJacob Pan return -EINVAL;
21594f69966SJacob Pan }
21694f69966SJacob Pan /* find the link to real cooling device record binding */
217*6c58cf40SFlorian Fainelli snprintf(cdev_name, sizeof(cdev_name) - 2, "%s/%s",
218*6c58cf40SFlorian Fainelli tz_name, nl->d_name);
21994f69966SJacob Pan memset(cdev_name_linked, 0, sizeof(cdev_name_linked));
22094f69966SJacob Pan if (readlink(cdev_name, cdev_name_linked,
22194f69966SJacob Pan sizeof(cdev_name_linked) - 1) != -1) {
22294f69966SJacob Pan cdev_id = get_instance_id(cdev_name_linked, 1,
22394f69966SJacob Pan sizeof("device") - 1);
22494f69966SJacob Pan syslog(LOG_DEBUG, "cdev %s linked to %s : %d\n",
22594f69966SJacob Pan cdev_name, cdev_name_linked, cdev_id);
22694f69966SJacob Pan tzi->cdev_binding |= (1 << cdev_id);
22794f69966SJacob Pan
22894f69966SJacob Pan /* find the trip point in which the cdev is binded to
22994f69966SJacob Pan * in this tzone
23094f69966SJacob Pan */
231*6c58cf40SFlorian Fainelli snprintf(cdev_trip_name, sizeof(cdev_trip_name) - 1,
232*6c58cf40SFlorian Fainelli "%s%s", nl->d_name, "_trip_point");
23394f69966SJacob Pan sysfs_get_ulong(tz_name, cdev_trip_name,
23494f69966SJacob Pan &trip_instance);
23594f69966SJacob Pan /* validate trip point range, e.g. trip could return -1
23694f69966SJacob Pan * when passive is enabled
23794f69966SJacob Pan */
23894f69966SJacob Pan if (trip_instance > MAX_NR_TRIP)
23994f69966SJacob Pan trip_instance = 0;
24094f69966SJacob Pan tzi->trip_binding[cdev_id] |= 1 << trip_instance;
24194f69966SJacob Pan syslog(LOG_DEBUG, "cdev %s -> trip:%lu: 0x%lx %d\n",
24294f69966SJacob Pan cdev_name, trip_instance,
24394f69966SJacob Pan tzi->trip_binding[cdev_id],
24494f69966SJacob Pan cdev_id);
24594f69966SJacob Pan
24694f69966SJacob Pan
24794f69966SJacob Pan }
24894f69966SJacob Pan return 0;
24994f69966SJacob Pan }
25094f69966SJacob Pan
25194f69966SJacob Pan return -ENODEV;
25294f69966SJacob Pan }
25394f69966SJacob Pan
25494f69966SJacob Pan
25594f69966SJacob Pan
25694f69966SJacob Pan /*****************************************************************************
25794f69966SJacob Pan * Before calling scan_tzones, thermal sysfs must be probed to determine
25894f69966SJacob Pan * the number of thermal zones and cooling devices.
25994f69966SJacob Pan * We loop through each thermal zone and fill in tz_info struct, i.e.
26094f69966SJacob Pan * ptdata.tzi[]
26194f69966SJacob Pan root@jacob-chiefriver:~# tree -d /sys/class/thermal/thermal_zone0
26294f69966SJacob Pan /sys/class/thermal/thermal_zone0
26394f69966SJacob Pan |-- cdev0 -> ../cooling_device4
26494f69966SJacob Pan |-- cdev1 -> ../cooling_device3
26594f69966SJacob Pan |-- cdev10 -> ../cooling_device7
26694f69966SJacob Pan |-- cdev11 -> ../cooling_device6
26794f69966SJacob Pan |-- cdev12 -> ../cooling_device5
26894f69966SJacob Pan |-- cdev2 -> ../cooling_device2
26994f69966SJacob Pan |-- cdev3 -> ../cooling_device1
27094f69966SJacob Pan |-- cdev4 -> ../cooling_device0
27194f69966SJacob Pan |-- cdev5 -> ../cooling_device12
27294f69966SJacob Pan |-- cdev6 -> ../cooling_device11
27394f69966SJacob Pan |-- cdev7 -> ../cooling_device10
27494f69966SJacob Pan |-- cdev8 -> ../cooling_device9
27594f69966SJacob Pan |-- cdev9 -> ../cooling_device8
27694f69966SJacob Pan |-- device -> ../../../LNXSYSTM:00/device:62/LNXTHERM:00
27794f69966SJacob Pan |-- power
27894f69966SJacob Pan `-- subsystem -> ../../../../class/thermal
27994f69966SJacob Pan *****************************************************************************/
scan_tzones(void)28094f69966SJacob Pan static int scan_tzones(void)
28194f69966SJacob Pan {
28294f69966SJacob Pan DIR *dir;
28394f69966SJacob Pan struct dirent **namelist;
28494f69966SJacob Pan char tz_name[256];
28594f69966SJacob Pan int i, j, n, k = 0;
28694f69966SJacob Pan
28794f69966SJacob Pan if (!ptdata.nr_tz_sensor)
28894f69966SJacob Pan return -1;
28994f69966SJacob Pan
29094f69966SJacob Pan for (i = 0; i <= ptdata.max_tz_instance; i++) {
29194f69966SJacob Pan memset(tz_name, 0, sizeof(tz_name));
29294f69966SJacob Pan snprintf(tz_name, 256, "%s/%s%d", THERMAL_SYSFS, TZONE, i);
29394f69966SJacob Pan
29494f69966SJacob Pan dir = opendir(tz_name);
29594f69966SJacob Pan if (!dir) {
29694f69966SJacob Pan syslog(LOG_INFO, "Thermal zone %s skipped\n", tz_name);
29794f69966SJacob Pan continue;
29894f69966SJacob Pan }
29994f69966SJacob Pan /* keep track of valid tzones */
30094f69966SJacob Pan n = scandir(tz_name, &namelist, 0, alphasort);
30194f69966SJacob Pan if (n < 0)
30294f69966SJacob Pan syslog(LOG_ERR, "scandir failed in %s", tz_name);
30394f69966SJacob Pan else {
30494f69966SJacob Pan sysfs_get_string(tz_name, "type", ptdata.tzi[k].type);
30594f69966SJacob Pan ptdata.tzi[k].instance = i;
30694f69966SJacob Pan /* detect trip points and cdev attached to this tzone */
30794f69966SJacob Pan j = 0; /* index for cdev */
30894f69966SJacob Pan ptdata.tzi[k].nr_cdev = 0;
30994f69966SJacob Pan ptdata.tzi[k].nr_trip_pts = 0;
31094f69966SJacob Pan while (n--) {
31194f69966SJacob Pan char *temp_str;
31294f69966SJacob Pan
31394f69966SJacob Pan if (find_tzone_tp(tz_name, namelist[n]->d_name,
31494f69966SJacob Pan &ptdata.tzi[k], k))
31594f69966SJacob Pan break;
31694f69966SJacob Pan temp_str = strstr(namelist[n]->d_name, "cdev");
31794f69966SJacob Pan if (!temp_str) {
31894f69966SJacob Pan free(namelist[n]);
31994f69966SJacob Pan continue;
32094f69966SJacob Pan }
32194f69966SJacob Pan if (!find_tzone_cdev(namelist[n], tz_name,
32294f69966SJacob Pan &ptdata.tzi[k], i, j))
32394f69966SJacob Pan j++; /* increment cdev index */
32494f69966SJacob Pan free(namelist[n]);
32594f69966SJacob Pan }
32694f69966SJacob Pan free(namelist);
32794f69966SJacob Pan }
32894f69966SJacob Pan /*TODO: reverse trip points */
32994f69966SJacob Pan closedir(dir);
33094f69966SJacob Pan syslog(LOG_INFO, "TZ %d has %d cdev\n", i,
33194f69966SJacob Pan ptdata.tzi[k].nr_cdev);
33294f69966SJacob Pan k++;
33394f69966SJacob Pan }
33494f69966SJacob Pan
33594f69966SJacob Pan return 0;
33694f69966SJacob Pan }
33794f69966SJacob Pan
scan_cdevs(void)33894f69966SJacob Pan static int scan_cdevs(void)
33994f69966SJacob Pan {
34094f69966SJacob Pan DIR *dir;
34194f69966SJacob Pan struct dirent **namelist;
34294f69966SJacob Pan char cdev_name[256];
34394f69966SJacob Pan int i, n, k = 0;
34494f69966SJacob Pan
34594f69966SJacob Pan if (!ptdata.nr_cooling_dev) {
34694f69966SJacob Pan fprintf(stderr, "No cooling devices found\n");
34794f69966SJacob Pan return 0;
34894f69966SJacob Pan }
34994f69966SJacob Pan for (i = 0; i <= ptdata.max_cdev_instance; i++) {
35094f69966SJacob Pan memset(cdev_name, 0, sizeof(cdev_name));
35194f69966SJacob Pan snprintf(cdev_name, 256, "%s/%s%d", THERMAL_SYSFS, CDEV, i);
35294f69966SJacob Pan
35394f69966SJacob Pan dir = opendir(cdev_name);
35494f69966SJacob Pan if (!dir) {
35594f69966SJacob Pan syslog(LOG_INFO, "Cooling dev %s skipped\n", cdev_name);
35694f69966SJacob Pan /* there is a gap in cooling device id, check again
35794f69966SJacob Pan * for the same index.
35894f69966SJacob Pan */
35994f69966SJacob Pan continue;
36094f69966SJacob Pan }
36194f69966SJacob Pan
36294f69966SJacob Pan n = scandir(cdev_name, &namelist, 0, alphasort);
36394f69966SJacob Pan if (n < 0)
36494f69966SJacob Pan syslog(LOG_ERR, "scandir failed in %s", cdev_name);
36594f69966SJacob Pan else {
36694f69966SJacob Pan sysfs_get_string(cdev_name, "type", ptdata.cdi[k].type);
36794f69966SJacob Pan ptdata.cdi[k].instance = i;
36894f69966SJacob Pan if (strstr(ptdata.cdi[k].type, ctrl_cdev)) {
36994f69966SJacob Pan ptdata.cdi[k].flag |= CDEV_FLAG_IN_CONTROL;
37094f69966SJacob Pan syslog(LOG_DEBUG, "control cdev id %d\n", i);
37194f69966SJacob Pan }
37294f69966SJacob Pan while (n--)
37394f69966SJacob Pan free(namelist[n]);
37494f69966SJacob Pan free(namelist);
37594f69966SJacob Pan }
37694f69966SJacob Pan closedir(dir);
37794f69966SJacob Pan k++;
37894f69966SJacob Pan }
37994f69966SJacob Pan return 0;
38094f69966SJacob Pan }
38194f69966SJacob Pan
38294f69966SJacob Pan
probe_thermal_sysfs(void)38394f69966SJacob Pan int probe_thermal_sysfs(void)
38494f69966SJacob Pan {
38594f69966SJacob Pan DIR *dir;
38694f69966SJacob Pan struct dirent **namelist;
38794f69966SJacob Pan int n;
38894f69966SJacob Pan
38994f69966SJacob Pan dir = opendir(THERMAL_SYSFS);
39094f69966SJacob Pan if (!dir) {
39194f69966SJacob Pan fprintf(stderr, "\nNo thermal sysfs, exit\n");
39294f69966SJacob Pan return -1;
39394f69966SJacob Pan }
39494f69966SJacob Pan n = scandir(THERMAL_SYSFS, &namelist, 0, alphasort);
39594f69966SJacob Pan if (n < 0)
39694f69966SJacob Pan syslog(LOG_ERR, "scandir failed in thermal sysfs");
39794f69966SJacob Pan else {
39894f69966SJacob Pan /* detect number of thermal zones and cooling devices */
39994f69966SJacob Pan while (n--) {
40094f69966SJacob Pan int inst;
40194f69966SJacob Pan
40294f69966SJacob Pan if (strstr(namelist[n]->d_name, CDEV)) {
40394f69966SJacob Pan inst = get_instance_id(namelist[n]->d_name, 1,
40494f69966SJacob Pan sizeof("device") - 1);
40594f69966SJacob Pan /* keep track of the max cooling device since
40694f69966SJacob Pan * there may be gaps.
40794f69966SJacob Pan */
40894f69966SJacob Pan if (inst > ptdata.max_cdev_instance)
40994f69966SJacob Pan ptdata.max_cdev_instance = inst;
41094f69966SJacob Pan
41194f69966SJacob Pan syslog(LOG_DEBUG, "found cdev: %s %d %d\n",
41294f69966SJacob Pan namelist[n]->d_name,
41394f69966SJacob Pan ptdata.nr_cooling_dev,
41494f69966SJacob Pan ptdata.max_cdev_instance);
41594f69966SJacob Pan ptdata.nr_cooling_dev++;
41694f69966SJacob Pan } else if (strstr(namelist[n]->d_name, TZONE)) {
41794f69966SJacob Pan inst = get_instance_id(namelist[n]->d_name, 1,
41894f69966SJacob Pan sizeof("zone") - 1);
41994f69966SJacob Pan if (inst > ptdata.max_tz_instance)
42094f69966SJacob Pan ptdata.max_tz_instance = inst;
42194f69966SJacob Pan
42294f69966SJacob Pan syslog(LOG_DEBUG, "found tzone: %s %d %d\n",
42394f69966SJacob Pan namelist[n]->d_name,
42494f69966SJacob Pan ptdata.nr_tz_sensor,
42594f69966SJacob Pan ptdata.max_tz_instance);
42694f69966SJacob Pan ptdata.nr_tz_sensor++;
42794f69966SJacob Pan }
42894f69966SJacob Pan free(namelist[n]);
42994f69966SJacob Pan }
43094f69966SJacob Pan free(namelist);
43194f69966SJacob Pan }
43294f69966SJacob Pan syslog(LOG_INFO, "found %d tzone(s), %d cdev(s), target zone %d\n",
43394f69966SJacob Pan ptdata.nr_tz_sensor, ptdata.nr_cooling_dev,
43494f69966SJacob Pan target_thermal_zone);
43594f69966SJacob Pan closedir(dir);
43694f69966SJacob Pan
43794f69966SJacob Pan if (!ptdata.nr_tz_sensor) {
43894f69966SJacob Pan fprintf(stderr, "\nNo thermal zones found, exit\n\n");
43994f69966SJacob Pan return -1;
44094f69966SJacob Pan }
44194f69966SJacob Pan
442e4e458b4SArjun Sreedharan ptdata.tzi = calloc(ptdata.max_tz_instance+1, sizeof(struct tz_info));
44394f69966SJacob Pan if (!ptdata.tzi) {
44494f69966SJacob Pan fprintf(stderr, "Err: allocate tz_info\n");
44594f69966SJacob Pan return -1;
44694f69966SJacob Pan }
44794f69966SJacob Pan
44894f69966SJacob Pan /* we still show thermal zone information if there is no cdev */
44994f69966SJacob Pan if (ptdata.nr_cooling_dev) {
450e4e458b4SArjun Sreedharan ptdata.cdi = calloc(ptdata.max_cdev_instance + 1,
451e4e458b4SArjun Sreedharan sizeof(struct cdev_info));
45294f69966SJacob Pan if (!ptdata.cdi) {
45394f69966SJacob Pan free(ptdata.tzi);
45494f69966SJacob Pan fprintf(stderr, "Err: allocate cdev_info\n");
45594f69966SJacob Pan return -1;
45694f69966SJacob Pan }
45794f69966SJacob Pan }
45894f69966SJacob Pan
45994f69966SJacob Pan /* now probe tzones */
46094f69966SJacob Pan if (scan_tzones())
46194f69966SJacob Pan return -1;
46294f69966SJacob Pan if (scan_cdevs())
46394f69966SJacob Pan return -1;
46494f69966SJacob Pan return 0;
46594f69966SJacob Pan }
46694f69966SJacob Pan
46794f69966SJacob Pan /* convert sysfs zone instance to zone array index */
zone_instance_to_index(int zone_inst)46894f69966SJacob Pan int zone_instance_to_index(int zone_inst)
46994f69966SJacob Pan {
47094f69966SJacob Pan int i;
47194f69966SJacob Pan
47294f69966SJacob Pan for (i = 0; i < ptdata.nr_tz_sensor; i++)
47394f69966SJacob Pan if (ptdata.tzi[i].instance == zone_inst)
47494f69966SJacob Pan return i;
47594f69966SJacob Pan return -ENOENT;
47694f69966SJacob Pan }
47794f69966SJacob Pan
47894f69966SJacob Pan /* read temperature of all thermal zones */
update_thermal_data()47994f69966SJacob Pan int update_thermal_data()
48094f69966SJacob Pan {
48194f69966SJacob Pan int i;
4826c59f64bSFrank Asseg int next_thermal_record = cur_thermal_record + 1;
48394f69966SJacob Pan char tz_name[256];
48494f69966SJacob Pan static unsigned long samples;
48594f69966SJacob Pan
48694f69966SJacob Pan if (!ptdata.nr_tz_sensor) {
48794f69966SJacob Pan syslog(LOG_ERR, "No thermal zones found!\n");
48894f69966SJacob Pan return -1;
48994f69966SJacob Pan }
49094f69966SJacob Pan
49194f69966SJacob Pan /* circular buffer for keeping historic data */
4926c59f64bSFrank Asseg if (next_thermal_record >= NR_THERMAL_RECORDS)
4936c59f64bSFrank Asseg next_thermal_record = 0;
4946c59f64bSFrank Asseg gettimeofday(&trec[next_thermal_record].tv, NULL);
49594f69966SJacob Pan if (tmon_log) {
49694f69966SJacob Pan fprintf(tmon_log, "%lu ", ++samples);
49794f69966SJacob Pan fprintf(tmon_log, "%3.1f ", p_param.t_target);
49894f69966SJacob Pan }
49994f69966SJacob Pan for (i = 0; i < ptdata.nr_tz_sensor; i++) {
50094f69966SJacob Pan memset(tz_name, 0, sizeof(tz_name));
50194f69966SJacob Pan snprintf(tz_name, 256, "%s/%s%d", THERMAL_SYSFS, TZONE,
50294f69966SJacob Pan ptdata.tzi[i].instance);
50394f69966SJacob Pan sysfs_get_ulong(tz_name, "temp",
5046c59f64bSFrank Asseg &trec[next_thermal_record].temp[i]);
50594f69966SJacob Pan if (tmon_log)
50694f69966SJacob Pan fprintf(tmon_log, "%lu ",
5076c59f64bSFrank Asseg trec[next_thermal_record].temp[i] / 1000);
50894f69966SJacob Pan }
5096c59f64bSFrank Asseg cur_thermal_record = next_thermal_record;
51094f69966SJacob Pan for (i = 0; i < ptdata.nr_cooling_dev; i++) {
51194f69966SJacob Pan char cdev_name[256];
51294f69966SJacob Pan unsigned long val;
51394f69966SJacob Pan
51494f69966SJacob Pan snprintf(cdev_name, 256, "%s/%s%d", THERMAL_SYSFS, CDEV,
51594f69966SJacob Pan ptdata.cdi[i].instance);
51694f69966SJacob Pan probe_cdev(&ptdata.cdi[i], cdev_name);
51794f69966SJacob Pan val = ptdata.cdi[i].cur_state;
51894f69966SJacob Pan if (val > 1000000)
51994f69966SJacob Pan val = 0;
52094f69966SJacob Pan if (tmon_log)
52194f69966SJacob Pan fprintf(tmon_log, "%lu ", val);
52294f69966SJacob Pan }
52394f69966SJacob Pan
52494f69966SJacob Pan if (tmon_log) {
52594f69966SJacob Pan fprintf(tmon_log, "\n");
52694f69966SJacob Pan fflush(tmon_log);
52794f69966SJacob Pan }
52894f69966SJacob Pan
52994f69966SJacob Pan return 0;
53094f69966SJacob Pan }
53194f69966SJacob Pan
set_ctrl_state(unsigned long state)53294f69966SJacob Pan void set_ctrl_state(unsigned long state)
53394f69966SJacob Pan {
53494f69966SJacob Pan char ctrl_cdev_path[256];
53594f69966SJacob Pan int i;
53694f69966SJacob Pan unsigned long cdev_state;
53794f69966SJacob Pan
53894f69966SJacob Pan if (no_control)
53994f69966SJacob Pan return;
54094f69966SJacob Pan /* set all ctrl cdev to the same state */
54194f69966SJacob Pan for (i = 0; i < ptdata.nr_cooling_dev; i++) {
54294f69966SJacob Pan if (ptdata.cdi[i].flag & CDEV_FLAG_IN_CONTROL) {
54394f69966SJacob Pan if (ptdata.cdi[i].max_state < 10) {
54494f69966SJacob Pan strcpy(ctrl_cdev, "None.");
54594f69966SJacob Pan return;
54694f69966SJacob Pan }
54794f69966SJacob Pan /* scale to percentage of max_state */
54894f69966SJacob Pan cdev_state = state * ptdata.cdi[i].max_state/100;
54994f69966SJacob Pan syslog(LOG_DEBUG,
55094f69966SJacob Pan "ctrl cdev %d set state %lu scaled to %lu\n",
55194f69966SJacob Pan ptdata.cdi[i].instance, state, cdev_state);
55294f69966SJacob Pan snprintf(ctrl_cdev_path, 256, "%s/%s%d", THERMAL_SYSFS,
55394f69966SJacob Pan CDEV, ptdata.cdi[i].instance);
55494f69966SJacob Pan syslog(LOG_DEBUG, "ctrl cdev path %s", ctrl_cdev_path);
55594f69966SJacob Pan sysfs_set_ulong(ctrl_cdev_path, "cur_state",
55694f69966SJacob Pan cdev_state);
55794f69966SJacob Pan }
55894f69966SJacob Pan }
55994f69966SJacob Pan }
56094f69966SJacob Pan
get_ctrl_state(unsigned long * state)56194f69966SJacob Pan void get_ctrl_state(unsigned long *state)
56294f69966SJacob Pan {
56394f69966SJacob Pan char ctrl_cdev_path[256];
56494f69966SJacob Pan int ctrl_cdev_id = -1;
56594f69966SJacob Pan int i;
56694f69966SJacob Pan
56794f69966SJacob Pan /* TODO: take average of all ctrl types. also consider change based on
56894f69966SJacob Pan * uevent. Take the first reading for now.
56994f69966SJacob Pan */
57094f69966SJacob Pan for (i = 0; i < ptdata.nr_cooling_dev; i++) {
57194f69966SJacob Pan if (ptdata.cdi[i].flag & CDEV_FLAG_IN_CONTROL) {
57294f69966SJacob Pan ctrl_cdev_id = ptdata.cdi[i].instance;
57394f69966SJacob Pan syslog(LOG_INFO, "ctrl cdev %d get state\n",
57494f69966SJacob Pan ptdata.cdi[i].instance);
57594f69966SJacob Pan break;
57694f69966SJacob Pan }
57794f69966SJacob Pan }
57894f69966SJacob Pan if (ctrl_cdev_id == -1) {
57994f69966SJacob Pan *state = 0;
58094f69966SJacob Pan return;
58194f69966SJacob Pan }
58294f69966SJacob Pan snprintf(ctrl_cdev_path, 256, "%s/%s%d", THERMAL_SYSFS,
58394f69966SJacob Pan CDEV, ctrl_cdev_id);
58494f69966SJacob Pan sysfs_get_ulong(ctrl_cdev_path, "cur_state", state);
58594f69966SJacob Pan }
58694f69966SJacob Pan
free_thermal_data(void)58794f69966SJacob Pan void free_thermal_data(void)
58894f69966SJacob Pan {
58994f69966SJacob Pan free(ptdata.tzi);
59094f69966SJacob Pan free(ptdata.cdi);
59194f69966SJacob Pan }
592