Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / tools / lib / thermal / thermal.c
blob6f02e35391594bd012751f20134ca029d57fbfce
1 // SPDX-License-Identifier: LGPL-2.1+
2 // Copyright (C) 2022, Linaro Ltd - Daniel Lezcano <daniel.lezcano@linaro.org>
3 #include <stdio.h>
4 #include <limits.h>
5 #include <thermal.h>
7 #include "thermal_nl.h"
9 int for_each_thermal_threshold(struct thermal_threshold *th, cb_th_t cb, void *arg)
11 int i, ret = 0;
13 if (!th)
14 return 0;
16 for (i = 0; th[i].temperature != INT_MAX; i++)
17 ret |= cb(&th[i], arg);
19 return ret;
22 int for_each_thermal_cdev(struct thermal_cdev *cdev, cb_tc_t cb, void *arg)
24 int i, ret = 0;
26 if (!cdev)
27 return 0;
29 for (i = 0; cdev[i].id != -1; i++)
30 ret |= cb(&cdev[i], arg);
32 return ret;
35 int for_each_thermal_trip(struct thermal_trip *tt, cb_tt_t cb, void *arg)
37 int i, ret = 0;
39 if (!tt)
40 return 0;
42 for (i = 0; tt[i].id != -1; i++)
43 ret |= cb(&tt[i], arg);
45 return ret;
48 int for_each_thermal_zone(struct thermal_zone *tz, cb_tz_t cb, void *arg)
50 int i, ret = 0;
52 if (!tz)
53 return 0;
55 for (i = 0; tz[i].id != -1; i++)
56 ret |= cb(&tz[i], arg);
58 return ret;
61 struct thermal_zone *thermal_zone_find_by_name(struct thermal_zone *tz,
62 const char *name)
64 int i;
66 if (!tz || !name)
67 return NULL;
69 for (i = 0; tz[i].id != -1; i++) {
70 if (!strcmp(tz[i].name, name))
71 return &tz[i];
74 return NULL;
77 struct thermal_zone *thermal_zone_find_by_id(struct thermal_zone *tz, int id)
79 int i;
81 if (!tz || id < 0)
82 return NULL;
84 for (i = 0; tz[i].id != -1; i++) {
85 if (tz[i].id == id)
86 return &tz[i];
89 return NULL;
92 static int __thermal_zone_discover(struct thermal_zone *tz, void *th)
94 if (thermal_cmd_get_trip(th, tz) < 0)
95 return -1;
97 if (thermal_cmd_threshold_get(th, tz))
98 return -1;
100 if (thermal_cmd_get_governor(th, tz))
101 return -1;
103 return 0;
106 struct thermal_zone *thermal_zone_discover(struct thermal_handler *th)
108 struct thermal_zone *tz;
110 if (thermal_cmd_get_tz(th, &tz) < 0)
111 return NULL;
113 if (for_each_thermal_zone(tz, __thermal_zone_discover, th))
114 return NULL;
116 return tz;
119 void thermal_exit(struct thermal_handler *th)
121 thermal_cmd_exit(th);
122 thermal_events_exit(th);
123 thermal_sampling_exit(th);
125 free(th);
128 struct thermal_handler *thermal_init(struct thermal_ops *ops)
130 struct thermal_handler *th;
132 th = malloc(sizeof(*th));
133 if (!th)
134 return NULL;
135 th->ops = ops;
137 if (thermal_events_init(th))
138 goto out_free;
140 if (thermal_sampling_init(th))
141 goto out_free;
143 if (thermal_cmd_init(th))
144 goto out_free;
146 return th;
148 out_free:
149 free(th);
151 return NULL;