Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: LGPL-2.1+
0002 // Copyright (C) 2022, Linaro Ltd - Daniel Lezcano <daniel.lezcano@linaro.org>
0003 #include <stdio.h>
0004 #include <thermal.h>
0005 
0006 #include "thermal_nl.h"
0007 
0008 int for_each_thermal_cdev(struct thermal_cdev *cdev, cb_tc_t cb, void *arg)
0009 {
0010     int i, ret = 0;
0011 
0012     if (!cdev)
0013         return 0;
0014 
0015     for (i = 0; cdev[i].id != -1; i++)
0016         ret |= cb(&cdev[i], arg);
0017 
0018     return ret;
0019 }
0020 
0021 int for_each_thermal_trip(struct thermal_trip *tt, cb_tt_t cb, void *arg)
0022 {
0023     int i, ret = 0;
0024 
0025     if (!tt)
0026         return 0;
0027 
0028     for (i = 0; tt[i].id != -1; i++)
0029         ret |= cb(&tt[i], arg);
0030 
0031     return ret;
0032 }
0033 
0034 int for_each_thermal_zone(struct thermal_zone *tz, cb_tz_t cb, void *arg)
0035 {
0036     int i, ret = 0;
0037 
0038     if (!tz)
0039         return 0;
0040 
0041     for (i = 0; tz[i].id != -1; i++)
0042         ret |= cb(&tz[i], arg);
0043 
0044     return ret;
0045 }
0046 
0047 struct thermal_zone *thermal_zone_find_by_name(struct thermal_zone *tz,
0048                            const char *name)
0049 {
0050     int i;
0051 
0052     if (!tz || !name)
0053         return NULL;
0054 
0055     for (i = 0; tz[i].id != -1; i++) {
0056         if (!strcmp(tz[i].name, name))
0057             return &tz[i];
0058     }
0059 
0060     return NULL;
0061 }
0062 
0063 struct thermal_zone *thermal_zone_find_by_id(struct thermal_zone *tz, int id)
0064 {
0065     int i;
0066 
0067     if (!tz || id < 0)
0068         return NULL;
0069 
0070     for (i = 0; tz[i].id != -1; i++) {
0071         if (tz[i].id == id)
0072             return &tz[i];
0073     }
0074 
0075     return NULL;
0076 }
0077 
0078 static int __thermal_zone_discover(struct thermal_zone *tz, void *th)
0079 {
0080     if (thermal_cmd_get_trip(th, tz) < 0)
0081         return -1;
0082 
0083     if (thermal_cmd_get_governor(th, tz))
0084         return -1;
0085 
0086     return 0;
0087 }
0088 
0089 struct thermal_zone *thermal_zone_discover(struct thermal_handler *th)
0090 {
0091     struct thermal_zone *tz;
0092 
0093     if (thermal_cmd_get_tz(th, &tz) < 0)
0094         return NULL;
0095 
0096     if (for_each_thermal_zone(tz, __thermal_zone_discover, th))
0097         return NULL;
0098 
0099     return tz;
0100 }
0101 
0102 void thermal_exit(struct thermal_handler *th)
0103 {
0104     thermal_cmd_exit(th);
0105     thermal_events_exit(th);
0106     thermal_sampling_exit(th);
0107 
0108     free(th);
0109 }
0110 
0111 struct thermal_handler *thermal_init(struct thermal_ops *ops)
0112 {
0113     struct thermal_handler *th;
0114 
0115     th = malloc(sizeof(*th));
0116     if (!th)
0117         return NULL;
0118     th->ops = ops;
0119 
0120     if (thermal_events_init(th))
0121         goto out_free;
0122 
0123     if (thermal_sampling_init(th))
0124         goto out_free;
0125 
0126     if (thermal_cmd_init(th))
0127         goto out_free;
0128 
0129     return th;
0130 
0131 out_free:
0132     free(th);
0133 
0134     return NULL;
0135 }