1*47c4b0deSDaniel Lezcano /* SPDX-License-Identifier: LGPL-2.1+ */
2*47c4b0deSDaniel Lezcano /* Copyright (C) 2022, Linaro Ltd - Daniel Lezcano <daniel.lezcano@linaro.org> */
3*47c4b0deSDaniel Lezcano #ifndef __LIBTHERMAL_H
4*47c4b0deSDaniel Lezcano #define __LIBTHERMAL_H
5*47c4b0deSDaniel Lezcano 
6*47c4b0deSDaniel Lezcano #include <linux/thermal.h>
7*47c4b0deSDaniel Lezcano 
8*47c4b0deSDaniel Lezcano #ifndef LIBTHERMAL_API
9*47c4b0deSDaniel Lezcano #define LIBTHERMAL_API __attribute__((visibility("default")))
10*47c4b0deSDaniel Lezcano #endif
11*47c4b0deSDaniel Lezcano 
12*47c4b0deSDaniel Lezcano #ifdef __cplusplus
13*47c4b0deSDaniel Lezcano extern "C" {
14*47c4b0deSDaniel Lezcano #endif
15*47c4b0deSDaniel Lezcano 
16*47c4b0deSDaniel Lezcano struct thermal_sampling_ops {
17*47c4b0deSDaniel Lezcano 	int (*tz_temp)(int tz_id, int temp, void *arg);
18*47c4b0deSDaniel Lezcano };
19*47c4b0deSDaniel Lezcano 
20*47c4b0deSDaniel Lezcano struct thermal_events_ops {
21*47c4b0deSDaniel Lezcano 	int (*tz_create)(const char *name, int tz_id, void *arg);
22*47c4b0deSDaniel Lezcano 	int (*tz_delete)(int tz_id, void *arg);
23*47c4b0deSDaniel Lezcano 	int (*tz_enable)(int tz_id, void *arg);
24*47c4b0deSDaniel Lezcano 	int (*tz_disable)(int tz_id, void *arg);
25*47c4b0deSDaniel Lezcano 	int (*trip_high)(int tz_id, int trip_id, int temp, void *arg);
26*47c4b0deSDaniel Lezcano 	int (*trip_low)(int tz_id, int trip_id, int temp, void *arg);
27*47c4b0deSDaniel Lezcano 	int (*trip_add)(int tz_id, int trip_id, int type, int temp, int hyst, void *arg);
28*47c4b0deSDaniel Lezcano 	int (*trip_change)(int tz_id, int trip_id, int type, int temp, int hyst, void *arg);
29*47c4b0deSDaniel Lezcano 	int (*trip_delete)(int tz_id, int trip_id, void *arg);
30*47c4b0deSDaniel Lezcano 	int (*cdev_add)(const char *name, int cdev_id, int max_state, void *arg);
31*47c4b0deSDaniel Lezcano 	int (*cdev_delete)(int cdev_id, void *arg);
32*47c4b0deSDaniel Lezcano 	int (*cdev_update)(int cdev_id, int cur_state, void *arg);
33*47c4b0deSDaniel Lezcano 	int (*gov_change)(int tz_id, const char *gov_name, void *arg);
34*47c4b0deSDaniel Lezcano };
35*47c4b0deSDaniel Lezcano 
36*47c4b0deSDaniel Lezcano struct thermal_ops {
37*47c4b0deSDaniel Lezcano 	struct thermal_sampling_ops sampling;
38*47c4b0deSDaniel Lezcano 	struct thermal_events_ops events;
39*47c4b0deSDaniel Lezcano };
40*47c4b0deSDaniel Lezcano 
41*47c4b0deSDaniel Lezcano struct thermal_trip {
42*47c4b0deSDaniel Lezcano 	int id;
43*47c4b0deSDaniel Lezcano 	int type;
44*47c4b0deSDaniel Lezcano 	int temp;
45*47c4b0deSDaniel Lezcano 	int hyst;
46*47c4b0deSDaniel Lezcano };
47*47c4b0deSDaniel Lezcano 
48*47c4b0deSDaniel Lezcano struct thermal_zone {
49*47c4b0deSDaniel Lezcano 	int id;
50*47c4b0deSDaniel Lezcano 	int temp;
51*47c4b0deSDaniel Lezcano 	char name[THERMAL_NAME_LENGTH];
52*47c4b0deSDaniel Lezcano 	char governor[THERMAL_NAME_LENGTH];
53*47c4b0deSDaniel Lezcano 	struct thermal_trip *trip;
54*47c4b0deSDaniel Lezcano };
55*47c4b0deSDaniel Lezcano 
56*47c4b0deSDaniel Lezcano struct thermal_cdev {
57*47c4b0deSDaniel Lezcano 	int id;
58*47c4b0deSDaniel Lezcano 	char name[THERMAL_NAME_LENGTH];
59*47c4b0deSDaniel Lezcano 	int max_state;
60*47c4b0deSDaniel Lezcano 	int min_state;
61*47c4b0deSDaniel Lezcano 	int cur_state;
62*47c4b0deSDaniel Lezcano };
63*47c4b0deSDaniel Lezcano 
64*47c4b0deSDaniel Lezcano typedef enum {
65*47c4b0deSDaniel Lezcano 	THERMAL_ERROR = -1,
66*47c4b0deSDaniel Lezcano 	THERMAL_SUCCESS = 0,
67*47c4b0deSDaniel Lezcano } thermal_error_t;
68*47c4b0deSDaniel Lezcano 
69*47c4b0deSDaniel Lezcano struct thermal_handler;
70*47c4b0deSDaniel Lezcano 
71*47c4b0deSDaniel Lezcano typedef int (*cb_tz_t)(struct thermal_zone *, void *);
72*47c4b0deSDaniel Lezcano 
73*47c4b0deSDaniel Lezcano typedef int (*cb_tt_t)(struct thermal_trip *, void *);
74*47c4b0deSDaniel Lezcano 
75*47c4b0deSDaniel Lezcano typedef int (*cb_tc_t)(struct thermal_cdev *, void *);
76*47c4b0deSDaniel Lezcano 
77*47c4b0deSDaniel Lezcano LIBTHERMAL_API int for_each_thermal_zone(struct thermal_zone *tz, cb_tz_t cb, void *arg);
78*47c4b0deSDaniel Lezcano 
79*47c4b0deSDaniel Lezcano LIBTHERMAL_API int for_each_thermal_trip(struct thermal_trip *tt, cb_tt_t cb, void *arg);
80*47c4b0deSDaniel Lezcano 
81*47c4b0deSDaniel Lezcano LIBTHERMAL_API int for_each_thermal_cdev(struct thermal_cdev *cdev, cb_tc_t cb, void *arg);
82*47c4b0deSDaniel Lezcano 
83*47c4b0deSDaniel Lezcano LIBTHERMAL_API struct thermal_zone *thermal_zone_find_by_name(struct thermal_zone *tz,
84*47c4b0deSDaniel Lezcano 							      const char *name);
85*47c4b0deSDaniel Lezcano 
86*47c4b0deSDaniel Lezcano LIBTHERMAL_API struct thermal_zone *thermal_zone_find_by_id(struct thermal_zone *tz, int id);
87*47c4b0deSDaniel Lezcano 
88*47c4b0deSDaniel Lezcano LIBTHERMAL_API struct thermal_zone *thermal_zone_discover(struct thermal_handler *th);
89*47c4b0deSDaniel Lezcano 
90*47c4b0deSDaniel Lezcano LIBTHERMAL_API struct thermal_handler *thermal_init(struct thermal_ops *ops);
91*47c4b0deSDaniel Lezcano 
92*47c4b0deSDaniel Lezcano LIBTHERMAL_API void thermal_exit(struct thermal_handler *th);
93*47c4b0deSDaniel Lezcano 
94*47c4b0deSDaniel Lezcano /*
95*47c4b0deSDaniel Lezcano  * Netlink thermal events
96*47c4b0deSDaniel Lezcano  */
97*47c4b0deSDaniel Lezcano LIBTHERMAL_API thermal_error_t thermal_events_exit(struct thermal_handler *th);
98*47c4b0deSDaniel Lezcano 
99*47c4b0deSDaniel Lezcano LIBTHERMAL_API thermal_error_t thermal_events_init(struct thermal_handler *th);
100*47c4b0deSDaniel Lezcano 
101*47c4b0deSDaniel Lezcano LIBTHERMAL_API thermal_error_t thermal_events_handle(struct thermal_handler *th, void *arg);
102*47c4b0deSDaniel Lezcano 
103*47c4b0deSDaniel Lezcano LIBTHERMAL_API int thermal_events_fd(struct thermal_handler *th);
104*47c4b0deSDaniel Lezcano 
105*47c4b0deSDaniel Lezcano /*
106*47c4b0deSDaniel Lezcano  * Netlink thermal commands
107*47c4b0deSDaniel Lezcano  */
108*47c4b0deSDaniel Lezcano LIBTHERMAL_API thermal_error_t thermal_cmd_exit(struct thermal_handler *th);
109*47c4b0deSDaniel Lezcano 
110*47c4b0deSDaniel Lezcano LIBTHERMAL_API thermal_error_t thermal_cmd_init(struct thermal_handler *th);
111*47c4b0deSDaniel Lezcano 
112*47c4b0deSDaniel Lezcano LIBTHERMAL_API thermal_error_t thermal_cmd_get_tz(struct thermal_handler *th,
113*47c4b0deSDaniel Lezcano 						  struct thermal_zone **tz);
114*47c4b0deSDaniel Lezcano 
115*47c4b0deSDaniel Lezcano LIBTHERMAL_API thermal_error_t thermal_cmd_get_cdev(struct thermal_handler *th,
116*47c4b0deSDaniel Lezcano 						    struct thermal_cdev **tc);
117*47c4b0deSDaniel Lezcano 
118*47c4b0deSDaniel Lezcano LIBTHERMAL_API thermal_error_t thermal_cmd_get_trip(struct thermal_handler *th,
119*47c4b0deSDaniel Lezcano 						    struct thermal_zone *tz);
120*47c4b0deSDaniel Lezcano 
121*47c4b0deSDaniel Lezcano LIBTHERMAL_API thermal_error_t thermal_cmd_get_governor(struct thermal_handler *th,
122*47c4b0deSDaniel Lezcano 							struct thermal_zone *tz);
123*47c4b0deSDaniel Lezcano 
124*47c4b0deSDaniel Lezcano LIBTHERMAL_API thermal_error_t thermal_cmd_get_temp(struct thermal_handler *th,
125*47c4b0deSDaniel Lezcano 						    struct thermal_zone *tz);
126*47c4b0deSDaniel Lezcano 
127*47c4b0deSDaniel Lezcano /*
128*47c4b0deSDaniel Lezcano  * Netlink thermal samples
129*47c4b0deSDaniel Lezcano  */
130*47c4b0deSDaniel Lezcano LIBTHERMAL_API thermal_error_t thermal_sampling_exit(struct thermal_handler *th);
131*47c4b0deSDaniel Lezcano 
132*47c4b0deSDaniel Lezcano LIBTHERMAL_API thermal_error_t thermal_sampling_init(struct thermal_handler *th);
133*47c4b0deSDaniel Lezcano 
134*47c4b0deSDaniel Lezcano LIBTHERMAL_API thermal_error_t thermal_sampling_handle(struct thermal_handler *th, void *arg);
135*47c4b0deSDaniel Lezcano 
136*47c4b0deSDaniel Lezcano LIBTHERMAL_API int thermal_sampling_fd(struct thermal_handler *th);
137*47c4b0deSDaniel Lezcano 
138*47c4b0deSDaniel Lezcano #endif /* __LIBTHERMAL_H */
139*47c4b0deSDaniel Lezcano 
140*47c4b0deSDaniel Lezcano #ifdef __cplusplus
141*47c4b0deSDaniel Lezcano }
142*47c4b0deSDaniel Lezcano #endif
143