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