drm/panthor: Don't add write fences to the shared BOs
[drm/drm-misc.git] / tools / lib / thermal / thermal.c
blob72a76dc205bcebdcde084874aab474b3f2c8c692
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 <thermal.h>
6 #include "thermal_nl.h"
8 int for_each_thermal_cdev(struct thermal_cdev *cdev, cb_tc_t cb, void *arg)
10 int i, ret = 0;
12 if (!cdev)
13 return 0;
15 for (i = 0; cdev[i].id != -1; i++)
16 ret |= cb(&cdev[i], arg);
18 return ret;
21 int for_each_thermal_trip(struct thermal_trip *tt, cb_tt_t cb, void *arg)
23 int i, ret = 0;
25 if (!tt)
26 return 0;
28 for (i = 0; tt[i].id != -1; i++)
29 ret |= cb(&tt[i], arg);
31 return ret;
34 int for_each_thermal_zone(struct thermal_zone *tz, cb_tz_t cb, void *arg)
36 int i, ret = 0;
38 if (!tz)
39 return 0;
41 for (i = 0; tz[i].id != -1; i++)
42 ret |= cb(&tz[i], arg);
44 return ret;
47 struct thermal_zone *thermal_zone_find_by_name(struct thermal_zone *tz,
48 const char *name)
50 int i;
52 if (!tz || !name)
53 return NULL;
55 for (i = 0; tz[i].id != -1; i++) {
56 if (!strcmp(tz[i].name, name))
57 return &tz[i];
60 return NULL;
63 struct thermal_zone *thermal_zone_find_by_id(struct thermal_zone *tz, int id)
65 int i;
67 if (!tz || id < 0)
68 return NULL;
70 for (i = 0; tz[i].id != -1; i++) {
71 if (tz[i].id == id)
72 return &tz[i];
75 return NULL;
78 static int __thermal_zone_discover(struct thermal_zone *tz, void *th)
80 if (thermal_cmd_get_trip(th, tz) < 0)
81 return -1;
83 if (thermal_cmd_get_governor(th, tz))
84 return -1;
86 return 0;
89 struct thermal_zone *thermal_zone_discover(struct thermal_handler *th)
91 struct thermal_zone *tz;
93 if (thermal_cmd_get_tz(th, &tz) < 0)
94 return NULL;
96 if (for_each_thermal_zone(tz, __thermal_zone_discover, th))
97 return NULL;
99 return tz;
102 void thermal_exit(struct thermal_handler *th)
104 thermal_cmd_exit(th);
105 thermal_events_exit(th);
106 thermal_sampling_exit(th);
108 free(th);
111 struct thermal_handler *thermal_init(struct thermal_ops *ops)
113 struct thermal_handler *th;
115 th = malloc(sizeof(*th));
116 if (!th)
117 return NULL;
118 th->ops = ops;
120 if (thermal_events_init(th))
121 goto out_free;
123 if (thermal_sampling_init(th))
124 goto out_free;
126 if (thermal_cmd_init(th))
127 goto out_free;
129 return th;
131 out_free:
132 free(th);
134 return NULL;