accel/amdxdna: Return error when setting clock failed for npu1
[drm/drm-misc.git] / tools / lib / thermal / sampling.c
blobf67c1f9ea1d78553ef3c699c50fc3feef195e991
1 // SPDX-License-Identifier: LGPL-2.1+
2 // Copyright (C) 2022, Linaro Ltd - Daniel Lezcano <daniel.lezcano@linaro.org>
3 #include <errno.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <unistd.h>
8 #include <thermal.h>
9 #include "thermal_nl.h"
11 static int handle_thermal_sample(struct nl_msg *n, void *arg)
13 struct nlmsghdr *nlh = nlmsg_hdr(n);
14 struct genlmsghdr *genlhdr = genlmsg_hdr(nlh);
15 struct nlattr *attrs[THERMAL_GENL_ATTR_MAX + 1];
16 struct thermal_handler_param *thp = arg;
17 struct thermal_handler *th = thp->th;
19 arg = thp->arg;
21 genlmsg_parse(nlh, 0, attrs, THERMAL_GENL_ATTR_MAX, NULL);
23 switch (genlhdr->cmd) {
25 case THERMAL_GENL_SAMPLING_TEMP:
26 return th->ops->sampling.tz_temp(
27 nla_get_u32(attrs[THERMAL_GENL_ATTR_TZ_ID]),
28 nla_get_u32(attrs[THERMAL_GENL_ATTR_TZ_TEMP]), arg);
29 default:
30 return THERMAL_ERROR;
34 thermal_error_t thermal_sampling_handle(struct thermal_handler *th, void *arg)
36 struct thermal_handler_param thp = { .th = th, .arg = arg };
38 if (!th)
39 return THERMAL_ERROR;
41 if (nl_cb_set(th->cb_sampling, NL_CB_VALID, NL_CB_CUSTOM,
42 handle_thermal_sample, &thp))
43 return THERMAL_ERROR;
45 return nl_recvmsgs(th->sk_sampling, th->cb_sampling);
48 int thermal_sampling_fd(struct thermal_handler *th)
50 if (!th)
51 return -1;
53 return nl_socket_get_fd(th->sk_sampling);
56 thermal_error_t thermal_sampling_exit(struct thermal_handler *th)
58 if (nl_unsubscribe_thermal(th->sk_sampling, th->cb_sampling,
59 THERMAL_GENL_SAMPLING_GROUP_NAME))
60 return THERMAL_ERROR;
62 nl_thermal_disconnect(th->sk_sampling, th->cb_sampling);
64 return THERMAL_SUCCESS;
67 thermal_error_t thermal_sampling_init(struct thermal_handler *th)
69 if (nl_thermal_connect(&th->sk_sampling, &th->cb_sampling))
70 return THERMAL_ERROR;
72 if (nl_subscribe_thermal(th->sk_sampling, th->cb_sampling,
73 THERMAL_GENL_SAMPLING_GROUP_NAME))
74 return THERMAL_ERROR;
76 return THERMAL_SUCCESS;