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>
25 struct tmon_platform_data ptdata
;
26 const char *trip_type_name
[] = {
33 int sysfs_set_ulong(char *path
, char *filename
, unsigned long val
)
37 char filepath
[PATH_MAX
+ 2]; /* NUL and '/' */
39 snprintf(filepath
, sizeof(filepath
), "%s/%s", path
, filename
);
41 fd
= fopen(filepath
, "w");
43 syslog(LOG_ERR
, "Err: open %s: %s\n", __func__
, filepath
);
46 ret
= fprintf(fd
, "%lu", val
);
52 /* history of thermal data, used for control algo */
53 #define NR_THERMAL_RECORDS 3
54 struct thermal_data_record trec
[NR_THERMAL_RECORDS
];
55 int cur_thermal_record
; /* index to the trec array */
57 static int sysfs_get_ulong(char *path
, char *filename
, unsigned long *p_ulong
)
61 char filepath
[PATH_MAX
+ 2]; /* NUL and '/' */
63 snprintf(filepath
, sizeof(filepath
), "%s/%s", path
, filename
);
65 fd
= fopen(filepath
, "r");
67 syslog(LOG_ERR
, "Err: open %s: %s\n", __func__
, filepath
);
70 ret
= fscanf(fd
, "%lu", p_ulong
);
76 static int sysfs_get_string(char *path
, char *filename
, char *str
)
80 char filepath
[PATH_MAX
+ 2]; /* NUL and '/' */
82 snprintf(filepath
, sizeof(filepath
), "%s/%s", path
, filename
);
84 fd
= fopen(filepath
, "r");
86 syslog(LOG_ERR
, "Err: open %s: %s\n", __func__
, filepath
);
89 ret
= fscanf(fd
, "%256s", str
);
95 /* get states of the cooling device instance */
96 static int probe_cdev(struct cdev_info
*cdi
, char *path
)
98 sysfs_get_string(path
, "type", cdi
->type
);
99 sysfs_get_ulong(path
, "max_state", &cdi
->max_state
);
100 sysfs_get_ulong(path
, "cur_state", &cdi
->cur_state
);
102 syslog(LOG_INFO
, "%s: %s: type %s, max %lu, curr %lu inst %d\n",
104 cdi
->type
, cdi
->max_state
, cdi
->cur_state
, cdi
->instance
);
109 static int str_to_trip_type(char *name
)
113 for (i
= 0; i
< NR_THERMAL_TRIP_TYPE
; i
++) {
114 if (!strcmp(name
, trip_type_name
[i
]))
121 /* scan and fill in trip point info for a thermal zone and trip point id */
122 static int get_trip_point_data(char *tz_path
, int tzid
, int tpid
)
128 if (tpid
>= MAX_NR_TRIP
)
130 /* check trip point type */
131 snprintf(filename
, sizeof(filename
), "trip_point_%d_type", tpid
);
132 sysfs_get_string(tz_path
, filename
, temp_str
);
133 trip_type
= str_to_trip_type(temp_str
);
135 syslog(LOG_ERR
, "%s:%s no matching type\n", __func__
, temp_str
);
138 ptdata
.tzi
[tzid
].tp
[tpid
].type
= trip_type
;
139 syslog(LOG_INFO
, "%s:tz:%d tp:%d:type:%s type id %d\n", __func__
, tzid
,
140 tpid
, temp_str
, trip_type
);
142 /* TODO: check attribute */
147 /* return instance id for file format such as trip_point_4_temp */
148 static int get_instance_id(char *name
, int pos
, int skip
)
153 ch
= strtok(name
, "_");
156 syslog(LOG_INFO
, "%s:%s:%s:%d", __func__
, name
, ch
, i
);
157 ch
= strtok(NULL
, "_");
159 return atol(ch
+ skip
);
165 /* Find trip point info of a thermal zone */
166 static int find_tzone_tp(char *tz_name
, char *d_name
, struct tz_info
*tzi
,
170 unsigned long temp_ulong
;
172 if (strstr(d_name
, "trip_point") &&
173 strstr(d_name
, "temp")) {
174 /* check if trip point temp is non-zero
175 * ignore 0/invalid trip points
177 sysfs_get_ulong(tz_name
, d_name
, &temp_ulong
);
178 if (temp_ulong
< MAX_TEMP_KC
) {
180 /* found a valid trip point */
181 tp_id
= get_instance_id(d_name
, 2, 0);
182 syslog(LOG_DEBUG
, "tzone %s trip %d temp %lu tpnode %s",
183 tz_name
, tp_id
, temp_ulong
, d_name
);
184 if (tp_id
< 0 || tp_id
>= MAX_NR_TRIP
) {
185 syslog(LOG_ERR
, "Failed to find TP inst %s\n",
189 get_trip_point_data(tz_name
, tz_id
, tp_id
);
190 tzi
->tp
[tp_id
].temp
= temp_ulong
;
197 /* check cooling devices for binding info. */
198 static int find_tzone_cdev(struct dirent
*nl
, char *tz_name
,
199 struct tz_info
*tzi
, int tz_id
, int cid
)
201 unsigned long trip_instance
= 0;
202 char cdev_name_linked
[256];
203 char cdev_name
[PATH_MAX
];
204 char cdev_trip_name
[PATH_MAX
];
207 if (nl
->d_type
== DT_LNK
) {
208 syslog(LOG_DEBUG
, "TZ%d: cdev: %s cid %d\n", tz_id
, nl
->d_name
,
211 if (tzi
->nr_cdev
> ptdata
.nr_cooling_dev
) {
212 syslog(LOG_ERR
, "Err: Too many cdev? %d\n",
216 /* find the link to real cooling device record binding */
217 snprintf(cdev_name
, sizeof(cdev_name
) - 2, "%s/%s",
218 tz_name
, nl
->d_name
);
219 memset(cdev_name_linked
, 0, sizeof(cdev_name_linked
));
220 if (readlink(cdev_name
, cdev_name_linked
,
221 sizeof(cdev_name_linked
) - 1) != -1) {
222 cdev_id
= get_instance_id(cdev_name_linked
, 1,
223 sizeof("device") - 1);
224 syslog(LOG_DEBUG
, "cdev %s linked to %s : %d\n",
225 cdev_name
, cdev_name_linked
, cdev_id
);
226 tzi
->cdev_binding
|= (1 << cdev_id
);
228 /* find the trip point in which the cdev is binded to
231 snprintf(cdev_trip_name
, sizeof(cdev_trip_name
) - 1,
232 "%s%s", nl
->d_name
, "_trip_point");
233 sysfs_get_ulong(tz_name
, cdev_trip_name
,
235 /* validate trip point range, e.g. trip could return -1
236 * when passive is enabled
238 if (trip_instance
> MAX_NR_TRIP
)
240 tzi
->trip_binding
[cdev_id
] |= 1 << trip_instance
;
241 syslog(LOG_DEBUG
, "cdev %s -> trip:%lu: 0x%lx %d\n",
242 cdev_name
, trip_instance
,
243 tzi
->trip_binding
[cdev_id
],
256 /*****************************************************************************
257 * Before calling scan_tzones, thermal sysfs must be probed to determine
258 * the number of thermal zones and cooling devices.
259 * We loop through each thermal zone and fill in tz_info struct, i.e.
261 root@jacob-chiefriver:~# tree -d /sys/class/thermal/thermal_zone0
262 /sys/class/thermal/thermal_zone0
263 |-- cdev0 -> ../cooling_device4
264 |-- cdev1 -> ../cooling_device3
265 |-- cdev10 -> ../cooling_device7
266 |-- cdev11 -> ../cooling_device6
267 |-- cdev12 -> ../cooling_device5
268 |-- cdev2 -> ../cooling_device2
269 |-- cdev3 -> ../cooling_device1
270 |-- cdev4 -> ../cooling_device0
271 |-- cdev5 -> ../cooling_device12
272 |-- cdev6 -> ../cooling_device11
273 |-- cdev7 -> ../cooling_device10
274 |-- cdev8 -> ../cooling_device9
275 |-- cdev9 -> ../cooling_device8
276 |-- device -> ../../../LNXSYSTM:00/device:62/LNXTHERM:00
278 `-- subsystem -> ../../../../class/thermal
279 *****************************************************************************/
280 static int scan_tzones(void)
283 struct dirent
**namelist
;
287 if (!ptdata
.nr_tz_sensor
)
290 for (i
= 0; i
<= ptdata
.max_tz_instance
; i
++) {
291 memset(tz_name
, 0, sizeof(tz_name
));
292 snprintf(tz_name
, 256, "%s/%s%d", THERMAL_SYSFS
, TZONE
, i
);
294 dir
= opendir(tz_name
);
296 syslog(LOG_INFO
, "Thermal zone %s skipped\n", tz_name
);
299 /* keep track of valid tzones */
300 n
= scandir(tz_name
, &namelist
, 0, alphasort
);
302 syslog(LOG_ERR
, "scandir failed in %s", tz_name
);
304 sysfs_get_string(tz_name
, "type", ptdata
.tzi
[k
].type
);
305 ptdata
.tzi
[k
].instance
= i
;
306 /* detect trip points and cdev attached to this tzone */
307 j
= 0; /* index for cdev */
308 ptdata
.tzi
[k
].nr_cdev
= 0;
309 ptdata
.tzi
[k
].nr_trip_pts
= 0;
313 if (find_tzone_tp(tz_name
, namelist
[n
]->d_name
,
316 temp_str
= strstr(namelist
[n
]->d_name
, "cdev");
321 if (!find_tzone_cdev(namelist
[n
], tz_name
,
322 &ptdata
.tzi
[k
], i
, j
))
323 j
++; /* increment cdev index */
328 /*TODO: reverse trip points */
330 syslog(LOG_INFO
, "TZ %d has %d cdev\n", i
,
331 ptdata
.tzi
[k
].nr_cdev
);
338 static int scan_cdevs(void)
341 struct dirent
**namelist
;
345 if (!ptdata
.nr_cooling_dev
) {
346 fprintf(stderr
, "No cooling devices found\n");
349 for (i
= 0; i
<= ptdata
.max_cdev_instance
; i
++) {
350 memset(cdev_name
, 0, sizeof(cdev_name
));
351 snprintf(cdev_name
, 256, "%s/%s%d", THERMAL_SYSFS
, CDEV
, i
);
353 dir
= opendir(cdev_name
);
355 syslog(LOG_INFO
, "Cooling dev %s skipped\n", cdev_name
);
356 /* there is a gap in cooling device id, check again
357 * for the same index.
362 n
= scandir(cdev_name
, &namelist
, 0, alphasort
);
364 syslog(LOG_ERR
, "scandir failed in %s", cdev_name
);
366 sysfs_get_string(cdev_name
, "type", ptdata
.cdi
[k
].type
);
367 ptdata
.cdi
[k
].instance
= i
;
368 if (strstr(ptdata
.cdi
[k
].type
, ctrl_cdev
)) {
369 ptdata
.cdi
[k
].flag
|= CDEV_FLAG_IN_CONTROL
;
370 syslog(LOG_DEBUG
, "control cdev id %d\n", i
);
383 int probe_thermal_sysfs(void)
386 struct dirent
**namelist
;
389 dir
= opendir(THERMAL_SYSFS
);
391 fprintf(stderr
, "\nNo thermal sysfs, exit\n");
394 n
= scandir(THERMAL_SYSFS
, &namelist
, 0, alphasort
);
396 syslog(LOG_ERR
, "scandir failed in thermal sysfs");
398 /* detect number of thermal zones and cooling devices */
402 if (strstr(namelist
[n
]->d_name
, CDEV
)) {
403 inst
= get_instance_id(namelist
[n
]->d_name
, 1,
404 sizeof("device") - 1);
405 /* keep track of the max cooling device since
408 if (inst
> ptdata
.max_cdev_instance
)
409 ptdata
.max_cdev_instance
= inst
;
411 syslog(LOG_DEBUG
, "found cdev: %s %d %d\n",
413 ptdata
.nr_cooling_dev
,
414 ptdata
.max_cdev_instance
);
415 ptdata
.nr_cooling_dev
++;
416 } else if (strstr(namelist
[n
]->d_name
, TZONE
)) {
417 inst
= get_instance_id(namelist
[n
]->d_name
, 1,
419 if (inst
> ptdata
.max_tz_instance
)
420 ptdata
.max_tz_instance
= inst
;
422 syslog(LOG_DEBUG
, "found tzone: %s %d %d\n",
425 ptdata
.max_tz_instance
);
426 ptdata
.nr_tz_sensor
++;
432 syslog(LOG_INFO
, "found %d tzone(s), %d cdev(s), target zone %d\n",
433 ptdata
.nr_tz_sensor
, ptdata
.nr_cooling_dev
,
434 target_thermal_zone
);
437 if (!ptdata
.nr_tz_sensor
) {
438 fprintf(stderr
, "\nNo thermal zones found, exit\n\n");
442 ptdata
.tzi
= calloc(ptdata
.max_tz_instance
+1, sizeof(struct tz_info
));
444 fprintf(stderr
, "Err: allocate tz_info\n");
448 /* we still show thermal zone information if there is no cdev */
449 if (ptdata
.nr_cooling_dev
) {
450 ptdata
.cdi
= calloc(ptdata
.max_cdev_instance
+ 1,
451 sizeof(struct cdev_info
));
454 fprintf(stderr
, "Err: allocate cdev_info\n");
459 /* now probe tzones */
467 /* convert sysfs zone instance to zone array index */
468 int zone_instance_to_index(int zone_inst
)
472 for (i
= 0; i
< ptdata
.nr_tz_sensor
; i
++)
473 if (ptdata
.tzi
[i
].instance
== zone_inst
)
478 /* read temperature of all thermal zones */
479 int update_thermal_data()
482 int next_thermal_record
= cur_thermal_record
+ 1;
484 static unsigned long samples
;
486 if (!ptdata
.nr_tz_sensor
) {
487 syslog(LOG_ERR
, "No thermal zones found!\n");
491 /* circular buffer for keeping historic data */
492 if (next_thermal_record
>= NR_THERMAL_RECORDS
)
493 next_thermal_record
= 0;
494 gettimeofday(&trec
[next_thermal_record
].tv
, NULL
);
496 fprintf(tmon_log
, "%lu ", ++samples
);
497 fprintf(tmon_log
, "%3.1f ", p_param
.t_target
);
499 for (i
= 0; i
< ptdata
.nr_tz_sensor
; i
++) {
500 memset(tz_name
, 0, sizeof(tz_name
));
501 snprintf(tz_name
, 256, "%s/%s%d", THERMAL_SYSFS
, TZONE
,
502 ptdata
.tzi
[i
].instance
);
503 sysfs_get_ulong(tz_name
, "temp",
504 &trec
[next_thermal_record
].temp
[i
]);
506 fprintf(tmon_log
, "%lu ",
507 trec
[next_thermal_record
].temp
[i
] / 1000);
509 cur_thermal_record
= next_thermal_record
;
510 for (i
= 0; i
< ptdata
.nr_cooling_dev
; i
++) {
514 snprintf(cdev_name
, 256, "%s/%s%d", THERMAL_SYSFS
, CDEV
,
515 ptdata
.cdi
[i
].instance
);
516 probe_cdev(&ptdata
.cdi
[i
], cdev_name
);
517 val
= ptdata
.cdi
[i
].cur_state
;
521 fprintf(tmon_log
, "%lu ", val
);
525 fprintf(tmon_log
, "\n");
532 void set_ctrl_state(unsigned long state
)
534 char ctrl_cdev_path
[256];
536 unsigned long cdev_state
;
540 /* set all ctrl cdev to the same state */
541 for (i
= 0; i
< ptdata
.nr_cooling_dev
; i
++) {
542 if (ptdata
.cdi
[i
].flag
& CDEV_FLAG_IN_CONTROL
) {
543 if (ptdata
.cdi
[i
].max_state
< 10) {
544 strcpy(ctrl_cdev
, "None.");
547 /* scale to percentage of max_state */
548 cdev_state
= state
* ptdata
.cdi
[i
].max_state
/100;
550 "ctrl cdev %d set state %lu scaled to %lu\n",
551 ptdata
.cdi
[i
].instance
, state
, cdev_state
);
552 snprintf(ctrl_cdev_path
, 256, "%s/%s%d", THERMAL_SYSFS
,
553 CDEV
, ptdata
.cdi
[i
].instance
);
554 syslog(LOG_DEBUG
, "ctrl cdev path %s", ctrl_cdev_path
);
555 sysfs_set_ulong(ctrl_cdev_path
, "cur_state",
561 void get_ctrl_state(unsigned long *state
)
563 char ctrl_cdev_path
[256];
564 int ctrl_cdev_id
= -1;
567 /* TODO: take average of all ctrl types. also consider change based on
568 * uevent. Take the first reading for now.
570 for (i
= 0; i
< ptdata
.nr_cooling_dev
; i
++) {
571 if (ptdata
.cdi
[i
].flag
& CDEV_FLAG_IN_CONTROL
) {
572 ctrl_cdev_id
= ptdata
.cdi
[i
].instance
;
573 syslog(LOG_INFO
, "ctrl cdev %d get state\n",
574 ptdata
.cdi
[i
].instance
);
578 if (ctrl_cdev_id
== -1) {
582 snprintf(ctrl_cdev_path
, 256, "%s/%s%d", THERMAL_SYSFS
,
584 sysfs_get_ulong(ctrl_cdev_path
, "cur_state", state
);
587 void free_thermal_data(void)