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