162306a36Sopenharmony_ci/* SPDX-License-Identifier: LGPL-2.1+ */
262306a36Sopenharmony_ci/* Copyright (C) 2022, Linaro Ltd - Daniel Lezcano <daniel.lezcano@linaro.org> */
362306a36Sopenharmony_ci#ifndef __LIBTHERMAL_H
462306a36Sopenharmony_ci#define __LIBTHERMAL_H
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#include <linux/thermal.h>
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#ifndef LIBTHERMAL_API
962306a36Sopenharmony_ci#define LIBTHERMAL_API __attribute__((visibility("default")))
1062306a36Sopenharmony_ci#endif
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci#ifdef __cplusplus
1362306a36Sopenharmony_ciextern "C" {
1462306a36Sopenharmony_ci#endif
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_cistruct thermal_sampling_ops {
1762306a36Sopenharmony_ci	int (*tz_temp)(int tz_id, int temp, void *arg);
1862306a36Sopenharmony_ci};
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_cistruct thermal_events_ops {
2162306a36Sopenharmony_ci	int (*tz_create)(const char *name, int tz_id, void *arg);
2262306a36Sopenharmony_ci	int (*tz_delete)(int tz_id, void *arg);
2362306a36Sopenharmony_ci	int (*tz_enable)(int tz_id, void *arg);
2462306a36Sopenharmony_ci	int (*tz_disable)(int tz_id, void *arg);
2562306a36Sopenharmony_ci	int (*trip_high)(int tz_id, int trip_id, int temp, void *arg);
2662306a36Sopenharmony_ci	int (*trip_low)(int tz_id, int trip_id, int temp, void *arg);
2762306a36Sopenharmony_ci	int (*trip_add)(int tz_id, int trip_id, int type, int temp, int hyst, void *arg);
2862306a36Sopenharmony_ci	int (*trip_change)(int tz_id, int trip_id, int type, int temp, int hyst, void *arg);
2962306a36Sopenharmony_ci	int (*trip_delete)(int tz_id, int trip_id, void *arg);
3062306a36Sopenharmony_ci	int (*cdev_add)(const char *name, int cdev_id, int max_state, void *arg);
3162306a36Sopenharmony_ci	int (*cdev_delete)(int cdev_id, void *arg);
3262306a36Sopenharmony_ci	int (*cdev_update)(int cdev_id, int cur_state, void *arg);
3362306a36Sopenharmony_ci	int (*gov_change)(int tz_id, const char *gov_name, void *arg);
3462306a36Sopenharmony_ci};
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_cistruct thermal_ops {
3762306a36Sopenharmony_ci	struct thermal_sampling_ops sampling;
3862306a36Sopenharmony_ci	struct thermal_events_ops events;
3962306a36Sopenharmony_ci};
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_cistruct thermal_trip {
4262306a36Sopenharmony_ci	int id;
4362306a36Sopenharmony_ci	int type;
4462306a36Sopenharmony_ci	int temp;
4562306a36Sopenharmony_ci	int hyst;
4662306a36Sopenharmony_ci};
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_cistruct thermal_zone {
4962306a36Sopenharmony_ci	int id;
5062306a36Sopenharmony_ci	int temp;
5162306a36Sopenharmony_ci	char name[THERMAL_NAME_LENGTH];
5262306a36Sopenharmony_ci	char governor[THERMAL_NAME_LENGTH];
5362306a36Sopenharmony_ci	struct thermal_trip *trip;
5462306a36Sopenharmony_ci};
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_cistruct thermal_cdev {
5762306a36Sopenharmony_ci	int id;
5862306a36Sopenharmony_ci	char name[THERMAL_NAME_LENGTH];
5962306a36Sopenharmony_ci	int max_state;
6062306a36Sopenharmony_ci	int min_state;
6162306a36Sopenharmony_ci	int cur_state;
6262306a36Sopenharmony_ci};
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_citypedef enum {
6562306a36Sopenharmony_ci	THERMAL_ERROR = -1,
6662306a36Sopenharmony_ci	THERMAL_SUCCESS = 0,
6762306a36Sopenharmony_ci} thermal_error_t;
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_cistruct thermal_handler;
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_citypedef int (*cb_tz_t)(struct thermal_zone *, void *);
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_citypedef int (*cb_tt_t)(struct thermal_trip *, void *);
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_citypedef int (*cb_tc_t)(struct thermal_cdev *, void *);
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ciLIBTHERMAL_API int for_each_thermal_zone(struct thermal_zone *tz, cb_tz_t cb, void *arg);
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ciLIBTHERMAL_API int for_each_thermal_trip(struct thermal_trip *tt, cb_tt_t cb, void *arg);
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ciLIBTHERMAL_API int for_each_thermal_cdev(struct thermal_cdev *cdev, cb_tc_t cb, void *arg);
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ciLIBTHERMAL_API struct thermal_zone *thermal_zone_find_by_name(struct thermal_zone *tz,
8462306a36Sopenharmony_ci							      const char *name);
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ciLIBTHERMAL_API struct thermal_zone *thermal_zone_find_by_id(struct thermal_zone *tz, int id);
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ciLIBTHERMAL_API struct thermal_zone *thermal_zone_discover(struct thermal_handler *th);
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ciLIBTHERMAL_API struct thermal_handler *thermal_init(struct thermal_ops *ops);
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ciLIBTHERMAL_API void thermal_exit(struct thermal_handler *th);
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci/*
9562306a36Sopenharmony_ci * Netlink thermal events
9662306a36Sopenharmony_ci */
9762306a36Sopenharmony_ciLIBTHERMAL_API thermal_error_t thermal_events_exit(struct thermal_handler *th);
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ciLIBTHERMAL_API thermal_error_t thermal_events_init(struct thermal_handler *th);
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ciLIBTHERMAL_API thermal_error_t thermal_events_handle(struct thermal_handler *th, void *arg);
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ciLIBTHERMAL_API int thermal_events_fd(struct thermal_handler *th);
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci/*
10662306a36Sopenharmony_ci * Netlink thermal commands
10762306a36Sopenharmony_ci */
10862306a36Sopenharmony_ciLIBTHERMAL_API thermal_error_t thermal_cmd_exit(struct thermal_handler *th);
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ciLIBTHERMAL_API thermal_error_t thermal_cmd_init(struct thermal_handler *th);
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ciLIBTHERMAL_API thermal_error_t thermal_cmd_get_tz(struct thermal_handler *th,
11362306a36Sopenharmony_ci						  struct thermal_zone **tz);
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ciLIBTHERMAL_API thermal_error_t thermal_cmd_get_cdev(struct thermal_handler *th,
11662306a36Sopenharmony_ci						    struct thermal_cdev **tc);
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ciLIBTHERMAL_API thermal_error_t thermal_cmd_get_trip(struct thermal_handler *th,
11962306a36Sopenharmony_ci						    struct thermal_zone *tz);
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ciLIBTHERMAL_API thermal_error_t thermal_cmd_get_governor(struct thermal_handler *th,
12262306a36Sopenharmony_ci							struct thermal_zone *tz);
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ciLIBTHERMAL_API thermal_error_t thermal_cmd_get_temp(struct thermal_handler *th,
12562306a36Sopenharmony_ci						    struct thermal_zone *tz);
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci/*
12862306a36Sopenharmony_ci * Netlink thermal samples
12962306a36Sopenharmony_ci */
13062306a36Sopenharmony_ciLIBTHERMAL_API thermal_error_t thermal_sampling_exit(struct thermal_handler *th);
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ciLIBTHERMAL_API thermal_error_t thermal_sampling_init(struct thermal_handler *th);
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ciLIBTHERMAL_API thermal_error_t thermal_sampling_handle(struct thermal_handler *th, void *arg);
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ciLIBTHERMAL_API int thermal_sampling_fd(struct thermal_handler *th);
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ci#endif /* __LIBTHERMAL_H */
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci#ifdef __cplusplus
14162306a36Sopenharmony_ci}
14262306a36Sopenharmony_ci#endif
143