summaryrefslogtreecommitdiff
path: root/src/libthermal/thermal.c
blob: bbf9693afd7faddde372b316893e1a627c4ff300 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

#include "thermal.h"

int for_each_thermal_cdev(struct thermal_cdev *cdev, cb_tc_t cb, void *arg)
{
	int i, ret = 0;

	for (i = 0; cdev[i].id != -1; i++)
		ret |= cb(&cdev[i], arg);

	return ret;
}	

int for_each_thermal_trip(struct thermal_trip *tt, cb_tt_t cb, void *arg)
{
	int i, ret = 0;

	for (i = 0; tt[i].id != -1; i++)
		ret |= cb(&tt[i], arg);

	return ret;
}

int for_each_thermal_zone(struct thermal_zone *tz, cb_tz_t cb, void *arg)
{
	int i, ret = 0;

	for (i = 0; tz[i].id != -1; i++)
		ret |= cb(&tz[i], arg);

	return ret;
}

static int __thermal_zone_discover(struct thermal_zone *tz, void *th)
{
	if (thermal_cmd_get_trip(th, tz) < 0)
		return -1;

	if (thermal_cmd_get_governor(th, tz))
		return -1;

	return 0;
}

struct thermal_zone *thermal_zone_discover(struct thermal_handler *th)
{
	struct thermal_zone *tz;

	if (thermal_cmd_get_tz(th, &tz) < 0)
		return NULL;

	if (for_each_thermal_zone(tz, __thermal_zone_discover, th))
		return NULL;

	return tz;
}

struct thermal_handler *thermal_init(struct thermal_ops *ops)
{
	struct thermal_handler *th;

	th = malloc(sizeof(*th));
	if (!th)
		return NULL;
	th->ops = ops;

	if (thermal_events_init(th))
		goto out_free;

	if (thermal_sampling_init(th))
		goto out_free;

	if (thermal_cmd_init(th))
		goto out_free;
	
	return th;

out_free:
	free(th);
	
	return NULL;
}