1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * sysfs.c sysfs ABI access functions for TMON program
5 * Copyright (C) 2013 Intel Corporation. All rights reserved.
7 * Author: Jacob Pan <jacob.jun.pan@linux.intel.com>
24 struct tmon_platform_data ptdata;
25 const char *trip_type_name[] = {
32 int sysfs_set_ulong(char *path, char *filename, unsigned long val)
38 snprintf(filepath, 256, "%s/%s", path, filename);
40 fd = fopen(filepath, "w");
42 syslog(LOG_ERR, "Err: open %s: %s\n", __func__, filepath);
45 ret = fprintf(fd, "%lu", val);
51 /* history of thermal data, used for control algo */
52 #define NR_THERMAL_RECORDS 3
53 struct thermal_data_record trec[NR_THERMAL_RECORDS];
54 int cur_thermal_record; /* index to the trec array */
56 static int sysfs_get_ulong(char *path, char *filename, unsigned long *p_ulong)
62 snprintf(filepath, 256, "%s/%s", path, filename);
64 fd = fopen(filepath, "r");
66 syslog(LOG_ERR, "Err: open %s: %s\n", __func__, filepath);
69 ret = fscanf(fd, "%lu", p_ulong);
75 static int sysfs_get_string(char *path, char *filename, char *str)
81 snprintf(filepath, 256, "%s/%s", path, filename);
83 fd = fopen(filepath, "r");
85 syslog(LOG_ERR, "Err: open %s: %s\n", __func__, filepath);
88 ret = fscanf(fd, "%256s", str);
94 /* get states of the cooling device instance */
95 static int probe_cdev(struct cdev_info *cdi, char *path)
97 sysfs_get_string(path, "type", cdi->type);
98 sysfs_get_ulong(path, "max_state", &cdi->max_state);
99 sysfs_get_ulong(path, "cur_state", &cdi->cur_state);
101 syslog(LOG_INFO, "%s: %s: type %s, max %lu, curr %lu inst %d\n",
103 cdi->type, cdi->max_state, cdi->cur_state, cdi->instance);
108 static int str_to_trip_type(char *name)
112 for (i = 0; i < NR_THERMAL_TRIP_TYPE; i++) {
113 if (!strcmp(name, trip_type_name[i]))
120 /* scan and fill in trip point info for a thermal zone and trip point id */
121 static int get_trip_point_data(char *tz_path, int tzid, int tpid)
127 if (tpid >= MAX_NR_TRIP)
129 /* check trip point type */
130 snprintf(filename, sizeof(filename), "trip_point_%d_type", tpid);
131 sysfs_get_string(tz_path, filename, temp_str);
132 trip_type = str_to_trip_type(temp_str);
134 syslog(LOG_ERR, "%s:%s no matching type\n", __func__, temp_str);
137 ptdata.tzi[tzid].tp[tpid].type = trip_type;
138 syslog(LOG_INFO, "%s:tz:%d tp:%d:type:%s type id %d\n", __func__, tzid,
139 tpid, temp_str, trip_type);
141 /* TODO: check attribute */
146 /* return instance id for file format such as trip_point_4_temp */
147 static int get_instance_id(char *name, int pos, int skip)
152 ch = strtok(name, "_");
155 syslog(LOG_INFO, "%s:%s:%s:%d", __func__, name, ch, i);
156 ch = strtok(NULL, "_");
158 return atol(ch + skip);
164 /* Find trip point info of a thermal zone */
165 static int find_tzone_tp(char *tz_name, char *d_name, struct tz_info *tzi,
169 unsigned long temp_ulong;
171 if (strstr(d_name, "trip_point") &&
172 strstr(d_name, "temp")) {
173 /* check if trip point temp is non-zero
174 * ignore 0/invalid trip points
176 sysfs_get_ulong(tz_name, d_name, &temp_ulong);
177 if (temp_ulong < MAX_TEMP_KC) {
179 /* found a valid trip point */
180 tp_id = get_instance_id(d_name, 2, 0);
181 syslog(LOG_DEBUG, "tzone %s trip %d temp %lu tpnode %s",
182 tz_name, tp_id, temp_ulong, d_name);
183 if (tp_id < 0 || tp_id >= MAX_NR_TRIP) {
184 syslog(LOG_ERR, "Failed to find TP inst %s\n",
188 get_trip_point_data(tz_name, tz_id, tp_id);
189 tzi->tp[tp_id].temp = temp_ulong;
196 /* check cooling devices for binding info. */
197 static int find_tzone_cdev(struct dirent *nl, char *tz_name,
198 struct tz_info *tzi, int tz_id, int cid)
200 unsigned long trip_instance = 0;
201 char cdev_name_linked[256];
203 char cdev_trip_name[256];
206 if (nl->d_type == DT_LNK) {
207 syslog(LOG_DEBUG, "TZ%d: cdev: %s cid %d\n", tz_id, nl->d_name,
210 if (tzi->nr_cdev > ptdata.nr_cooling_dev) {
211 syslog(LOG_ERR, "Err: Too many cdev? %d\n",
215 /* find the link to real cooling device record binding */
216 snprintf(cdev_name, 256, "%s/%s", tz_name, nl->d_name);
217 memset(cdev_name_linked, 0, sizeof(cdev_name_linked));
218 if (readlink(cdev_name, cdev_name_linked,
219 sizeof(cdev_name_linked) - 1) != -1) {
220 cdev_id = get_instance_id(cdev_name_linked, 1,
221 sizeof("device") - 1);
222 syslog(LOG_DEBUG, "cdev %s linked to %s : %d\n",
223 cdev_name, cdev_name_linked, cdev_id);
224 tzi->cdev_binding |= (1 << cdev_id);
226 /* find the trip point in which the cdev is binded to
229 snprintf(cdev_trip_name, 256, "%s%s", nl->d_name,
231 sysfs_get_ulong(tz_name, cdev_trip_name,
233 /* validate trip point range, e.g. trip could return -1
234 * when passive is enabled
236 if (trip_instance > MAX_NR_TRIP)
238 tzi->trip_binding[cdev_id] |= 1 << trip_instance;
239 syslog(LOG_DEBUG, "cdev %s -> trip:%lu: 0x%lx %d\n",
240 cdev_name, trip_instance,
241 tzi->trip_binding[cdev_id],
254 /*****************************************************************************
255 * Before calling scan_tzones, thermal sysfs must be probed to determine
256 * the number of thermal zones and cooling devices.
257 * We loop through each thermal zone and fill in tz_info struct, i.e.
259 root@jacob-chiefriver:~# tree -d /sys/class/thermal/thermal_zone0
260 /sys/class/thermal/thermal_zone0
261 |-- cdev0 -> ../cooling_device4
262 |-- cdev1 -> ../cooling_device3
263 |-- cdev10 -> ../cooling_device7
264 |-- cdev11 -> ../cooling_device6
265 |-- cdev12 -> ../cooling_device5
266 |-- cdev2 -> ../cooling_device2
267 |-- cdev3 -> ../cooling_device1
268 |-- cdev4 -> ../cooling_device0
269 |-- cdev5 -> ../cooling_device12
270 |-- cdev6 -> ../cooling_device11
271 |-- cdev7 -> ../cooling_device10
272 |-- cdev8 -> ../cooling_device9
273 |-- cdev9 -> ../cooling_device8
274 |-- device -> ../../../LNXSYSTM:00/device:62/LNXTHERM:00
276 `-- subsystem -> ../../../../class/thermal
277 *****************************************************************************/
278 static int scan_tzones(void)
281 struct dirent **namelist;
285 if (!ptdata.nr_tz_sensor)
288 for (i = 0; i <= ptdata.max_tz_instance; i++) {
289 memset(tz_name, 0, sizeof(tz_name));
290 snprintf(tz_name, 256, "%s/%s%d", THERMAL_SYSFS, TZONE, i);
292 dir = opendir(tz_name);
294 syslog(LOG_INFO, "Thermal zone %s skipped\n", tz_name);
297 /* keep track of valid tzones */
298 n = scandir(tz_name, &namelist, 0, alphasort);
300 syslog(LOG_ERR, "scandir failed in %s", tz_name);
302 sysfs_get_string(tz_name, "type", ptdata.tzi[k].type);
303 ptdata.tzi[k].instance = i;
304 /* detect trip points and cdev attached to this tzone */
305 j = 0; /* index for cdev */
306 ptdata.tzi[k].nr_cdev = 0;
307 ptdata.tzi[k].nr_trip_pts = 0;
311 if (find_tzone_tp(tz_name, namelist[n]->d_name,
314 temp_str = strstr(namelist[n]->d_name, "cdev");
319 if (!find_tzone_cdev(namelist[n], tz_name,
320 &ptdata.tzi[k], i, j))
321 j++; /* increment cdev index */
326 /*TODO: reverse trip points */
328 syslog(LOG_INFO, "TZ %d has %d cdev\n", i,
329 ptdata.tzi[k].nr_cdev);
336 static int scan_cdevs(void)
339 struct dirent **namelist;
343 if (!ptdata.nr_cooling_dev) {
344 fprintf(stderr, "No cooling devices found\n");
347 for (i = 0; i <= ptdata.max_cdev_instance; i++) {
348 memset(cdev_name, 0, sizeof(cdev_name));
349 snprintf(cdev_name, 256, "%s/%s%d", THERMAL_SYSFS, CDEV, i);
351 dir = opendir(cdev_name);
353 syslog(LOG_INFO, "Cooling dev %s skipped\n", cdev_name);
354 /* there is a gap in cooling device id, check again
355 * for the same index.
360 n = scandir(cdev_name, &namelist, 0, alphasort);
362 syslog(LOG_ERR, "scandir failed in %s", cdev_name);
364 sysfs_get_string(cdev_name, "type", ptdata.cdi[k].type);
365 ptdata.cdi[k].instance = i;
366 if (strstr(ptdata.cdi[k].type, ctrl_cdev)) {
367 ptdata.cdi[k].flag |= CDEV_FLAG_IN_CONTROL;
368 syslog(LOG_DEBUG, "control cdev id %d\n", i);
381 int probe_thermal_sysfs(void)
384 struct dirent **namelist;
387 dir = opendir(THERMAL_SYSFS);
389 fprintf(stderr, "\nNo thermal sysfs, exit\n");
392 n = scandir(THERMAL_SYSFS, &namelist, 0, alphasort);
394 syslog(LOG_ERR, "scandir failed in thermal sysfs");
396 /* detect number of thermal zones and cooling devices */
400 if (strstr(namelist[n]->d_name, CDEV)) {
401 inst = get_instance_id(namelist[n]->d_name, 1,
402 sizeof("device") - 1);
403 /* keep track of the max cooling device since
406 if (inst > ptdata.max_cdev_instance)
407 ptdata.max_cdev_instance = inst;
409 syslog(LOG_DEBUG, "found cdev: %s %d %d\n",
411 ptdata.nr_cooling_dev,
412 ptdata.max_cdev_instance);
413 ptdata.nr_cooling_dev++;
414 } else if (strstr(namelist[n]->d_name, TZONE)) {
415 inst = get_instance_id(namelist[n]->d_name, 1,
417 if (inst > ptdata.max_tz_instance)
418 ptdata.max_tz_instance = inst;
420 syslog(LOG_DEBUG, "found tzone: %s %d %d\n",
423 ptdata.max_tz_instance);
424 ptdata.nr_tz_sensor++;
430 syslog(LOG_INFO, "found %d tzone(s), %d cdev(s), target zone %d\n",
431 ptdata.nr_tz_sensor, ptdata.nr_cooling_dev,
432 target_thermal_zone);
435 if (!ptdata.nr_tz_sensor) {
436 fprintf(stderr, "\nNo thermal zones found, exit\n\n");
440 ptdata.tzi = calloc(ptdata.max_tz_instance+1, sizeof(struct tz_info));
442 fprintf(stderr, "Err: allocate tz_info\n");
446 /* we still show thermal zone information if there is no cdev */
447 if (ptdata.nr_cooling_dev) {
448 ptdata.cdi = calloc(ptdata.max_cdev_instance + 1,
449 sizeof(struct cdev_info));
452 fprintf(stderr, "Err: allocate cdev_info\n");
457 /* now probe tzones */
465 /* convert sysfs zone instance to zone array index */
466 int zone_instance_to_index(int zone_inst)
470 for (i = 0; i < ptdata.nr_tz_sensor; i++)
471 if (ptdata.tzi[i].instance == zone_inst)
476 /* read temperature of all thermal zones */
477 int update_thermal_data()
480 int next_thermal_record = cur_thermal_record + 1;
482 static unsigned long samples;
484 if (!ptdata.nr_tz_sensor) {
485 syslog(LOG_ERR, "No thermal zones found!\n");
489 /* circular buffer for keeping historic data */
490 if (next_thermal_record >= NR_THERMAL_RECORDS)
491 next_thermal_record = 0;
492 gettimeofday(&trec[next_thermal_record].tv, NULL);
494 fprintf(tmon_log, "%lu ", ++samples);
495 fprintf(tmon_log, "%3.1f ", p_param.t_target);
497 for (i = 0; i < ptdata.nr_tz_sensor; i++) {
498 memset(tz_name, 0, sizeof(tz_name));
499 snprintf(tz_name, 256, "%s/%s%d", THERMAL_SYSFS, TZONE,
500 ptdata.tzi[i].instance);
501 sysfs_get_ulong(tz_name, "temp",
502 &trec[next_thermal_record].temp[i]);
504 fprintf(tmon_log, "%lu ",
505 trec[next_thermal_record].temp[i] / 1000);
507 cur_thermal_record = next_thermal_record;
508 for (i = 0; i < ptdata.nr_cooling_dev; i++) {
512 snprintf(cdev_name, 256, "%s/%s%d", THERMAL_SYSFS, CDEV,
513 ptdata.cdi[i].instance);
514 probe_cdev(&ptdata.cdi[i], cdev_name);
515 val = ptdata.cdi[i].cur_state;
519 fprintf(tmon_log, "%lu ", val);
523 fprintf(tmon_log, "\n");
530 void set_ctrl_state(unsigned long state)
532 char ctrl_cdev_path[256];
534 unsigned long cdev_state;
538 /* set all ctrl cdev to the same state */
539 for (i = 0; i < ptdata.nr_cooling_dev; i++) {
540 if (ptdata.cdi[i].flag & CDEV_FLAG_IN_CONTROL) {
541 if (ptdata.cdi[i].max_state < 10) {
542 strcpy(ctrl_cdev, "None.");
545 /* scale to percentage of max_state */
546 cdev_state = state * ptdata.cdi[i].max_state/100;
548 "ctrl cdev %d set state %lu scaled to %lu\n",
549 ptdata.cdi[i].instance, state, cdev_state);
550 snprintf(ctrl_cdev_path, 256, "%s/%s%d", THERMAL_SYSFS,
551 CDEV, ptdata.cdi[i].instance);
552 syslog(LOG_DEBUG, "ctrl cdev path %s", ctrl_cdev_path);
553 sysfs_set_ulong(ctrl_cdev_path, "cur_state",
559 void get_ctrl_state(unsigned long *state)
561 char ctrl_cdev_path[256];
562 int ctrl_cdev_id = -1;
565 /* TODO: take average of all ctrl types. also consider change based on
566 * uevent. Take the first reading for now.
568 for (i = 0; i < ptdata.nr_cooling_dev; i++) {
569 if (ptdata.cdi[i].flag & CDEV_FLAG_IN_CONTROL) {
570 ctrl_cdev_id = ptdata.cdi[i].instance;
571 syslog(LOG_INFO, "ctrl cdev %d get state\n",
572 ptdata.cdi[i].instance);
576 if (ctrl_cdev_id == -1) {
580 snprintf(ctrl_cdev_path, 256, "%s/%s%d", THERMAL_SYSFS,
582 sysfs_get_ulong(ctrl_cdev_path, "cur_state", state);
585 void free_thermal_data(void)