2 * sysfs.c sysfs ABI access functions for TMON program
4 * Copyright (C) 2013 Intel Corporation. All rights reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 or later as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * Author: Jacob Pan <jacob.jun.pan@linux.intel.com>
34 struct tmon_platform_data ptdata;
35 const char *trip_type_name[] = {
42 int sysfs_set_ulong(char *path, char *filename, unsigned long val)
46 char filepath[PATH_MAX + 2]; /* NUL and '/' */
48 snprintf(filepath, sizeof(filepath), "%s/%s", path, filename);
50 fd = fopen(filepath, "w");
52 syslog(LOG_ERR, "Err: open %s: %s\n", __func__, filepath);
55 ret = fprintf(fd, "%lu", val);
61 /* history of thermal data, used for control algo */
62 #define NR_THERMAL_RECORDS 3
63 struct thermal_data_record trec[NR_THERMAL_RECORDS];
64 int cur_thermal_record; /* index to the trec array */
66 static int sysfs_get_ulong(char *path, char *filename, unsigned long *p_ulong)
70 char filepath[PATH_MAX + 2]; /* NUL and '/' */
72 snprintf(filepath, sizeof(filepath), "%s/%s", path, filename);
74 fd = fopen(filepath, "r");
76 syslog(LOG_ERR, "Err: open %s: %s\n", __func__, filepath);
79 ret = fscanf(fd, "%lu", p_ulong);
85 static int sysfs_get_string(char *path, char *filename, char *str)
89 char filepath[PATH_MAX + 2]; /* NUL and '/' */
91 snprintf(filepath, sizeof(filepath), "%s/%s", path, filename);
93 fd = fopen(filepath, "r");
95 syslog(LOG_ERR, "Err: open %s: %s\n", __func__, filepath);
98 ret = fscanf(fd, "%256s", str);
104 /* get states of the cooling device instance */
105 static int probe_cdev(struct cdev_info *cdi, char *path)
107 sysfs_get_string(path, "type", cdi->type);
108 sysfs_get_ulong(path, "max_state", &cdi->max_state);
109 sysfs_get_ulong(path, "cur_state", &cdi->cur_state);
111 syslog(LOG_INFO, "%s: %s: type %s, max %lu, curr %lu inst %d\n",
113 cdi->type, cdi->max_state, cdi->cur_state, cdi->instance);
118 static int str_to_trip_type(char *name)
122 for (i = 0; i < NR_THERMAL_TRIP_TYPE; i++) {
123 if (!strcmp(name, trip_type_name[i]))
130 /* scan and fill in trip point info for a thermal zone and trip point id */
131 static int get_trip_point_data(char *tz_path, int tzid, int tpid)
137 if (tpid >= MAX_NR_TRIP)
139 /* check trip point type */
140 snprintf(filename, sizeof(filename), "trip_point_%d_type", tpid);
141 sysfs_get_string(tz_path, filename, temp_str);
142 trip_type = str_to_trip_type(temp_str);
144 syslog(LOG_ERR, "%s:%s no matching type\n", __func__, temp_str);
147 ptdata.tzi[tzid].tp[tpid].type = trip_type;
148 syslog(LOG_INFO, "%s:tz:%d tp:%d:type:%s type id %d\n", __func__, tzid,
149 tpid, temp_str, trip_type);
151 /* TODO: check attribute */
156 /* return instance id for file format such as trip_point_4_temp */
157 static int get_instance_id(char *name, int pos, int skip)
162 ch = strtok(name, "_");
165 syslog(LOG_INFO, "%s:%s:%s:%d", __func__, name, ch, i);
166 ch = strtok(NULL, "_");
168 return atol(ch + skip);
174 /* Find trip point info of a thermal zone */
175 static int find_tzone_tp(char *tz_name, char *d_name, struct tz_info *tzi,
179 unsigned long temp_ulong;
181 if (strstr(d_name, "trip_point") &&
182 strstr(d_name, "temp")) {
183 /* check if trip point temp is non-zero
184 * ignore 0/invalid trip points
186 sysfs_get_ulong(tz_name, d_name, &temp_ulong);
187 if (temp_ulong < MAX_TEMP_KC) {
189 /* found a valid trip point */
190 tp_id = get_instance_id(d_name, 2, 0);
191 syslog(LOG_DEBUG, "tzone %s trip %d temp %lu tpnode %s",
192 tz_name, tp_id, temp_ulong, d_name);
193 if (tp_id < 0 || tp_id >= MAX_NR_TRIP) {
194 syslog(LOG_ERR, "Failed to find TP inst %s\n",
198 get_trip_point_data(tz_name, tz_id, tp_id);
199 tzi->tp[tp_id].temp = temp_ulong;
206 /* check cooling devices for binding info. */
207 static int find_tzone_cdev(struct dirent *nl, char *tz_name,
208 struct tz_info *tzi, int tz_id, int cid)
210 unsigned long trip_instance = 0;
211 char cdev_name_linked[256];
212 char cdev_name[PATH_MAX];
213 char cdev_trip_name[PATH_MAX];
216 if (nl->d_type == DT_LNK) {
217 syslog(LOG_DEBUG, "TZ%d: cdev: %s cid %d\n", tz_id, nl->d_name,
220 if (tzi->nr_cdev > ptdata.nr_cooling_dev) {
221 syslog(LOG_ERR, "Err: Too many cdev? %d\n",
225 /* find the link to real cooling device record binding */
226 snprintf(cdev_name, sizeof(cdev_name) - 2, "%s/%s",
227 tz_name, nl->d_name);
228 memset(cdev_name_linked, 0, sizeof(cdev_name_linked));
229 if (readlink(cdev_name, cdev_name_linked,
230 sizeof(cdev_name_linked) - 1) != -1) {
231 cdev_id = get_instance_id(cdev_name_linked, 1,
232 sizeof("device") - 1);
233 syslog(LOG_DEBUG, "cdev %s linked to %s : %d\n",
234 cdev_name, cdev_name_linked, cdev_id);
235 tzi->cdev_binding |= (1 << cdev_id);
237 /* find the trip point in which the cdev is binded to
240 snprintf(cdev_trip_name, sizeof(cdev_trip_name) - 1,
241 "%s%s", nl->d_name, "_trip_point");
242 sysfs_get_ulong(tz_name, cdev_trip_name,
244 /* validate trip point range, e.g. trip could return -1
245 * when passive is enabled
247 if (trip_instance > MAX_NR_TRIP)
249 tzi->trip_binding[cdev_id] |= 1 << trip_instance;
250 syslog(LOG_DEBUG, "cdev %s -> trip:%lu: 0x%lx %d\n",
251 cdev_name, trip_instance,
252 tzi->trip_binding[cdev_id],
265 /*****************************************************************************
266 * Before calling scan_tzones, thermal sysfs must be probed to determine
267 * the number of thermal zones and cooling devices.
268 * We loop through each thermal zone and fill in tz_info struct, i.e.
270 root@jacob-chiefriver:~# tree -d /sys/class/thermal/thermal_zone0
271 /sys/class/thermal/thermal_zone0
272 |-- cdev0 -> ../cooling_device4
273 |-- cdev1 -> ../cooling_device3
274 |-- cdev10 -> ../cooling_device7
275 |-- cdev11 -> ../cooling_device6
276 |-- cdev12 -> ../cooling_device5
277 |-- cdev2 -> ../cooling_device2
278 |-- cdev3 -> ../cooling_device1
279 |-- cdev4 -> ../cooling_device0
280 |-- cdev5 -> ../cooling_device12
281 |-- cdev6 -> ../cooling_device11
282 |-- cdev7 -> ../cooling_device10
283 |-- cdev8 -> ../cooling_device9
284 |-- cdev9 -> ../cooling_device8
285 |-- device -> ../../../LNXSYSTM:00/device:62/LNXTHERM:00
287 `-- subsystem -> ../../../../class/thermal
288 *****************************************************************************/
289 static int scan_tzones(void)
292 struct dirent **namelist;
296 if (!ptdata.nr_tz_sensor)
299 for (i = 0; i <= ptdata.max_tz_instance; i++) {
300 memset(tz_name, 0, sizeof(tz_name));
301 snprintf(tz_name, 256, "%s/%s%d", THERMAL_SYSFS, TZONE, i);
303 dir = opendir(tz_name);
305 syslog(LOG_INFO, "Thermal zone %s skipped\n", tz_name);
308 /* keep track of valid tzones */
309 n = scandir(tz_name, &namelist, 0, alphasort);
311 syslog(LOG_ERR, "scandir failed in %s", tz_name);
313 sysfs_get_string(tz_name, "type", ptdata.tzi[k].type);
314 ptdata.tzi[k].instance = i;
315 /* detect trip points and cdev attached to this tzone */
316 j = 0; /* index for cdev */
317 ptdata.tzi[k].nr_cdev = 0;
318 ptdata.tzi[k].nr_trip_pts = 0;
322 if (find_tzone_tp(tz_name, namelist[n]->d_name,
325 temp_str = strstr(namelist[n]->d_name, "cdev");
330 if (!find_tzone_cdev(namelist[n], tz_name,
331 &ptdata.tzi[k], i, j))
332 j++; /* increment cdev index */
337 /*TODO: reverse trip points */
339 syslog(LOG_INFO, "TZ %d has %d cdev\n", i,
340 ptdata.tzi[k].nr_cdev);
347 static int scan_cdevs(void)
350 struct dirent **namelist;
354 if (!ptdata.nr_cooling_dev) {
355 fprintf(stderr, "No cooling devices found\n");
358 for (i = 0; i <= ptdata.max_cdev_instance; i++) {
359 memset(cdev_name, 0, sizeof(cdev_name));
360 snprintf(cdev_name, 256, "%s/%s%d", THERMAL_SYSFS, CDEV, i);
362 dir = opendir(cdev_name);
364 syslog(LOG_INFO, "Cooling dev %s skipped\n", cdev_name);
365 /* there is a gap in cooling device id, check again
366 * for the same index.
371 n = scandir(cdev_name, &namelist, 0, alphasort);
373 syslog(LOG_ERR, "scandir failed in %s", cdev_name);
375 sysfs_get_string(cdev_name, "type", ptdata.cdi[k].type);
376 ptdata.cdi[k].instance = i;
377 if (strstr(ptdata.cdi[k].type, ctrl_cdev)) {
378 ptdata.cdi[k].flag |= CDEV_FLAG_IN_CONTROL;
379 syslog(LOG_DEBUG, "control cdev id %d\n", i);
392 int probe_thermal_sysfs(void)
395 struct dirent **namelist;
398 dir = opendir(THERMAL_SYSFS);
400 fprintf(stderr, "\nNo thermal sysfs, exit\n");
403 n = scandir(THERMAL_SYSFS, &namelist, 0, alphasort);
405 syslog(LOG_ERR, "scandir failed in thermal sysfs");
407 /* detect number of thermal zones and cooling devices */
411 if (strstr(namelist[n]->d_name, CDEV)) {
412 inst = get_instance_id(namelist[n]->d_name, 1,
413 sizeof("device") - 1);
414 /* keep track of the max cooling device since
417 if (inst > ptdata.max_cdev_instance)
418 ptdata.max_cdev_instance = inst;
420 syslog(LOG_DEBUG, "found cdev: %s %d %d\n",
422 ptdata.nr_cooling_dev,
423 ptdata.max_cdev_instance);
424 ptdata.nr_cooling_dev++;
425 } else if (strstr(namelist[n]->d_name, TZONE)) {
426 inst = get_instance_id(namelist[n]->d_name, 1,
428 if (inst > ptdata.max_tz_instance)
429 ptdata.max_tz_instance = inst;
431 syslog(LOG_DEBUG, "found tzone: %s %d %d\n",
434 ptdata.max_tz_instance);
435 ptdata.nr_tz_sensor++;
441 syslog(LOG_INFO, "found %d tzone(s), %d cdev(s), target zone %d\n",
442 ptdata.nr_tz_sensor, ptdata.nr_cooling_dev,
443 target_thermal_zone);
446 if (!ptdata.nr_tz_sensor) {
447 fprintf(stderr, "\nNo thermal zones found, exit\n\n");
451 ptdata.tzi = calloc(ptdata.max_tz_instance+1, sizeof(struct tz_info));
453 fprintf(stderr, "Err: allocate tz_info\n");
457 /* we still show thermal zone information if there is no cdev */
458 if (ptdata.nr_cooling_dev) {
459 ptdata.cdi = calloc(ptdata.max_cdev_instance + 1,
460 sizeof(struct cdev_info));
463 fprintf(stderr, "Err: allocate cdev_info\n");
468 /* now probe tzones */
476 /* convert sysfs zone instance to zone array index */
477 int zone_instance_to_index(int zone_inst)
481 for (i = 0; i < ptdata.nr_tz_sensor; i++)
482 if (ptdata.tzi[i].instance == zone_inst)
487 /* read temperature of all thermal zones */
488 int update_thermal_data()
491 int next_thermal_record = cur_thermal_record + 1;
493 static unsigned long samples;
495 if (!ptdata.nr_tz_sensor) {
496 syslog(LOG_ERR, "No thermal zones found!\n");
500 /* circular buffer for keeping historic data */
501 if (next_thermal_record >= NR_THERMAL_RECORDS)
502 next_thermal_record = 0;
503 gettimeofday(&trec[next_thermal_record].tv, NULL);
505 fprintf(tmon_log, "%lu ", ++samples);
506 fprintf(tmon_log, "%3.1f ", p_param.t_target);
508 for (i = 0; i < ptdata.nr_tz_sensor; i++) {
509 memset(tz_name, 0, sizeof(tz_name));
510 snprintf(tz_name, 256, "%s/%s%d", THERMAL_SYSFS, TZONE,
511 ptdata.tzi[i].instance);
512 sysfs_get_ulong(tz_name, "temp",
513 &trec[next_thermal_record].temp[i]);
515 fprintf(tmon_log, "%lu ",
516 trec[next_thermal_record].temp[i] / 1000);
518 cur_thermal_record = next_thermal_record;
519 for (i = 0; i < ptdata.nr_cooling_dev; i++) {
523 snprintf(cdev_name, 256, "%s/%s%d", THERMAL_SYSFS, CDEV,
524 ptdata.cdi[i].instance);
525 probe_cdev(&ptdata.cdi[i], cdev_name);
526 val = ptdata.cdi[i].cur_state;
530 fprintf(tmon_log, "%lu ", val);
534 fprintf(tmon_log, "\n");
541 void set_ctrl_state(unsigned long state)
543 char ctrl_cdev_path[256];
545 unsigned long cdev_state;
549 /* set all ctrl cdev to the same state */
550 for (i = 0; i < ptdata.nr_cooling_dev; i++) {
551 if (ptdata.cdi[i].flag & CDEV_FLAG_IN_CONTROL) {
552 if (ptdata.cdi[i].max_state < 10) {
553 strcpy(ctrl_cdev, "None.");
556 /* scale to percentage of max_state */
557 cdev_state = state * ptdata.cdi[i].max_state/100;
559 "ctrl cdev %d set state %lu scaled to %lu\n",
560 ptdata.cdi[i].instance, state, cdev_state);
561 snprintf(ctrl_cdev_path, 256, "%s/%s%d", THERMAL_SYSFS,
562 CDEV, ptdata.cdi[i].instance);
563 syslog(LOG_DEBUG, "ctrl cdev path %s", ctrl_cdev_path);
564 sysfs_set_ulong(ctrl_cdev_path, "cur_state",
570 void get_ctrl_state(unsigned long *state)
572 char ctrl_cdev_path[256];
573 int ctrl_cdev_id = -1;
576 /* TODO: take average of all ctrl types. also consider change based on
577 * uevent. Take the first reading for now.
579 for (i = 0; i < ptdata.nr_cooling_dev; i++) {
580 if (ptdata.cdi[i].flag & CDEV_FLAG_IN_CONTROL) {
581 ctrl_cdev_id = ptdata.cdi[i].instance;
582 syslog(LOG_INFO, "ctrl cdev %d get state\n",
583 ptdata.cdi[i].instance);
587 if (ctrl_cdev_id == -1) {
591 snprintf(ctrl_cdev_path, 256, "%s/%s%d", THERMAL_SYSFS,
593 sysfs_get_ulong(ctrl_cdev_path, "cur_state", state);
596 void free_thermal_data(void)