18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * tmon.h contains data structures and constants used by TMON
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2012 Intel Corporation. All rights reserved.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Author Name Jacob Pan <jacob.jun.pan@linux.intel.com>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#ifndef TMON_H
118c2ecf20Sopenharmony_ci#define TMON_H
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#define MAX_DISP_TEMP 125
148c2ecf20Sopenharmony_ci#define MAX_CTRL_TEMP 105
158c2ecf20Sopenharmony_ci#define MIN_CTRL_TEMP 40
168c2ecf20Sopenharmony_ci#define MAX_NR_TZONE 16
178c2ecf20Sopenharmony_ci#define MAX_NR_CDEV 32
188c2ecf20Sopenharmony_ci#define MAX_NR_TRIP 16
198c2ecf20Sopenharmony_ci#define MAX_NR_CDEV_TRIP 12 /* number of cooling devices that can bind
208c2ecf20Sopenharmony_ci			     * to a thermal zone trip.
218c2ecf20Sopenharmony_ci			     */
228c2ecf20Sopenharmony_ci#define MAX_TEMP_KC 140000
238c2ecf20Sopenharmony_ci/* starting char position to draw sensor data, such as tz names
248c2ecf20Sopenharmony_ci * trip point list, etc.
258c2ecf20Sopenharmony_ci */
268c2ecf20Sopenharmony_ci#define DATA_LEFT_ALIGN 10
278c2ecf20Sopenharmony_ci#define NR_LINES_TZDATA 1
288c2ecf20Sopenharmony_ci#define TMON_LOG_FILE "/var/tmp/tmon.log"
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#include <sys/time.h>
318c2ecf20Sopenharmony_ci#include <pthread.h>
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ciextern unsigned long ticktime;
348c2ecf20Sopenharmony_ciextern double time_elapsed;
358c2ecf20Sopenharmony_ciextern unsigned long target_temp_user;
368c2ecf20Sopenharmony_ciextern int dialogue_on;
378c2ecf20Sopenharmony_ciextern char ctrl_cdev[];
388c2ecf20Sopenharmony_ciextern pthread_mutex_t input_lock;
398c2ecf20Sopenharmony_ciextern int tmon_exit;
408c2ecf20Sopenharmony_ciextern int target_thermal_zone;
418c2ecf20Sopenharmony_ci/* use fixed size record to simplify data processing and transfer
428c2ecf20Sopenharmony_ci * TBD: more info to be added, e.g. programmable trip point data.
438c2ecf20Sopenharmony_ci*/
448c2ecf20Sopenharmony_cistruct thermal_data_record {
458c2ecf20Sopenharmony_ci	struct timeval tv;
468c2ecf20Sopenharmony_ci	unsigned long temp[MAX_NR_TZONE];
478c2ecf20Sopenharmony_ci	double pid_out_pct;
488c2ecf20Sopenharmony_ci};
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cistruct cdev_info {
518c2ecf20Sopenharmony_ci	char type[64];
528c2ecf20Sopenharmony_ci	int instance;
538c2ecf20Sopenharmony_ci	unsigned long max_state;
548c2ecf20Sopenharmony_ci	unsigned long cur_state;
558c2ecf20Sopenharmony_ci	unsigned long flag;
568c2ecf20Sopenharmony_ci};
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_cienum trip_type {
598c2ecf20Sopenharmony_ci	THERMAL_TRIP_CRITICAL,
608c2ecf20Sopenharmony_ci	THERMAL_TRIP_HOT,
618c2ecf20Sopenharmony_ci	THERMAL_TRIP_PASSIVE,
628c2ecf20Sopenharmony_ci	THERMAL_TRIP_ACTIVE,
638c2ecf20Sopenharmony_ci	NR_THERMAL_TRIP_TYPE,
648c2ecf20Sopenharmony_ci};
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_cistruct trip_point {
678c2ecf20Sopenharmony_ci	enum trip_type type;
688c2ecf20Sopenharmony_ci	unsigned long temp;
698c2ecf20Sopenharmony_ci	unsigned long hysteresis;
708c2ecf20Sopenharmony_ci	int attribute; /* programmability etc. */
718c2ecf20Sopenharmony_ci};
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci/* thermal zone configuration information, binding with cooling devices could
748c2ecf20Sopenharmony_ci * change at runtime.
758c2ecf20Sopenharmony_ci */
768c2ecf20Sopenharmony_cistruct tz_info {
778c2ecf20Sopenharmony_ci	char type[256]; /* e.g. acpitz */
788c2ecf20Sopenharmony_ci	int instance;
798c2ecf20Sopenharmony_ci	int passive; /* active zone has passive node to force passive mode */
808c2ecf20Sopenharmony_ci	int nr_cdev; /* number of cooling device binded */
818c2ecf20Sopenharmony_ci	int nr_trip_pts;
828c2ecf20Sopenharmony_ci	struct trip_point tp[MAX_NR_TRIP];
838c2ecf20Sopenharmony_ci	unsigned long cdev_binding; /* bitmap for attached cdevs */
848c2ecf20Sopenharmony_ci	/* cdev bind trip points, allow one cdev bind to multiple trips */
858c2ecf20Sopenharmony_ci	unsigned long trip_binding[MAX_NR_CDEV];
868c2ecf20Sopenharmony_ci};
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistruct tmon_platform_data {
898c2ecf20Sopenharmony_ci	int nr_tz_sensor;
908c2ecf20Sopenharmony_ci	int nr_cooling_dev;
918c2ecf20Sopenharmony_ci	/* keep track of instance ids since there might be gaps */
928c2ecf20Sopenharmony_ci	int max_tz_instance;
938c2ecf20Sopenharmony_ci	int max_cdev_instance;
948c2ecf20Sopenharmony_ci	struct tz_info *tzi;
958c2ecf20Sopenharmony_ci	struct cdev_info *cdi;
968c2ecf20Sopenharmony_ci};
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistruct control_ops {
998c2ecf20Sopenharmony_ci	void (*set_ratio)(unsigned long ratio);
1008c2ecf20Sopenharmony_ci	unsigned long (*get_ratio)(unsigned long ratio);
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci};
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_cienum cdev_types {
1058c2ecf20Sopenharmony_ci	CDEV_TYPE_PROC,
1068c2ecf20Sopenharmony_ci	CDEV_TYPE_FAN,
1078c2ecf20Sopenharmony_ci	CDEV_TYPE_MEM,
1088c2ecf20Sopenharmony_ci	CDEV_TYPE_NR,
1098c2ecf20Sopenharmony_ci};
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci/* REVISIT: the idea is to group sensors if possible, e.g. on intel mid
1128c2ecf20Sopenharmony_ci * we have "skin0", "skin1", "sys", "msicdie"
1138c2ecf20Sopenharmony_ci * on DPTF enabled systems, we might have PCH, TSKN, TAMB, etc.
1148c2ecf20Sopenharmony_ci */
1158c2ecf20Sopenharmony_cienum tzone_types {
1168c2ecf20Sopenharmony_ci	TZONE_TYPE_ACPI,
1178c2ecf20Sopenharmony_ci	TZONE_TYPE_PCH,
1188c2ecf20Sopenharmony_ci	TZONE_TYPE_NR,
1198c2ecf20Sopenharmony_ci};
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci/* limit the output of PID controller adjustment */
1228c2ecf20Sopenharmony_ci#define LIMIT_HIGH (95)
1238c2ecf20Sopenharmony_ci#define LIMIT_LOW  (2)
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_cistruct pid_params {
1268c2ecf20Sopenharmony_ci	double kp;  /* Controller gain from Dialog Box */
1278c2ecf20Sopenharmony_ci	double ki;  /* Time-constant for I action from Dialog Box */
1288c2ecf20Sopenharmony_ci	double kd;  /* Time-constant for D action from Dialog Box */
1298c2ecf20Sopenharmony_ci	double ts;
1308c2ecf20Sopenharmony_ci	double k_lpf;
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	double t_target;
1338c2ecf20Sopenharmony_ci	double y_k;
1348c2ecf20Sopenharmony_ci};
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ciextern int init_thermal_controller(void);
1378c2ecf20Sopenharmony_ciextern void controller_handler(const double xk, double *yk);
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ciextern struct tmon_platform_data ptdata;
1408c2ecf20Sopenharmony_ciextern struct pid_params p_param;
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ciextern FILE *tmon_log;
1438c2ecf20Sopenharmony_ciextern int cur_thermal_record; /* index to the trec array */
1448c2ecf20Sopenharmony_ciextern struct thermal_data_record trec[];
1458c2ecf20Sopenharmony_ciextern const char *trip_type_name[];
1468c2ecf20Sopenharmony_ciextern unsigned long no_control;
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ciextern void initialize_curses(void);
1498c2ecf20Sopenharmony_ciextern void show_controller_stats(char *line);
1508c2ecf20Sopenharmony_ciextern void show_title_bar(void);
1518c2ecf20Sopenharmony_ciextern void setup_windows(void);
1528c2ecf20Sopenharmony_ciextern void disable_tui(void);
1538c2ecf20Sopenharmony_ciextern void show_sensors_w(void);
1548c2ecf20Sopenharmony_ciextern void show_data_w(void);
1558c2ecf20Sopenharmony_ciextern void write_status_bar(int x, char *line);
1568c2ecf20Sopenharmony_ciextern void show_control_w();
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ciextern void show_cooling_device(void);
1598c2ecf20Sopenharmony_ciextern void show_dialogue(void);
1608c2ecf20Sopenharmony_ciextern int update_thermal_data(void);
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ciextern int probe_thermal_sysfs(void);
1638c2ecf20Sopenharmony_ciextern void free_thermal_data(void);
1648c2ecf20Sopenharmony_ciextern	void resize_handler(int sig);
1658c2ecf20Sopenharmony_ciextern void set_ctrl_state(unsigned long state);
1668c2ecf20Sopenharmony_ciextern void get_ctrl_state(unsigned long *state);
1678c2ecf20Sopenharmony_ciextern void *handle_tui_events(void *arg);
1688c2ecf20Sopenharmony_ciextern int sysfs_set_ulong(char *path, char *filename, unsigned long val);
1698c2ecf20Sopenharmony_ciextern int zone_instance_to_index(int zone_inst);
1708c2ecf20Sopenharmony_ciextern void close_windows(void);
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci#define PT_COLOR_DEFAULT    1
1738c2ecf20Sopenharmony_ci#define PT_COLOR_HEADER_BAR 2
1748c2ecf20Sopenharmony_ci#define PT_COLOR_ERROR      3
1758c2ecf20Sopenharmony_ci#define PT_COLOR_RED        4
1768c2ecf20Sopenharmony_ci#define PT_COLOR_YELLOW     5
1778c2ecf20Sopenharmony_ci#define PT_COLOR_GREEN      6
1788c2ecf20Sopenharmony_ci#define PT_COLOR_BRIGHT     7
1798c2ecf20Sopenharmony_ci#define PT_COLOR_BLUE	    8
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci/* each thermal zone uses 12 chars, 8 for name, 2 for instance, 2 space
1828c2ecf20Sopenharmony_ci * also used to list trip points in forms of AAAC, which represents
1838c2ecf20Sopenharmony_ci * A: Active
1848c2ecf20Sopenharmony_ci * C: Critical
1858c2ecf20Sopenharmony_ci */
1868c2ecf20Sopenharmony_ci#define TZONE_RECORD_SIZE 12
1878c2ecf20Sopenharmony_ci#define TZ_LEFT_ALIGN 32
1888c2ecf20Sopenharmony_ci#define CDEV_NAME_SIZE 20
1898c2ecf20Sopenharmony_ci#define CDEV_FLAG_IN_CONTROL (1 << 0)
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci/* dialogue box starts */
1928c2ecf20Sopenharmony_ci#define DIAG_X 48
1938c2ecf20Sopenharmony_ci#define DIAG_Y 8
1948c2ecf20Sopenharmony_ci#define THERMAL_SYSFS "/sys/class/thermal"
1958c2ecf20Sopenharmony_ci#define CDEV "cooling_device"
1968c2ecf20Sopenharmony_ci#define TZONE "thermal_zone"
1978c2ecf20Sopenharmony_ci#define TDATA_LEFT 16
1988c2ecf20Sopenharmony_ci#endif /* TMON_H */
199