4 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the License);
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
26 #include <hw/thermal.h>
27 #include <hw/shared.h>
29 #define THERMAL_PATH "/sys/class/sec/temperature/ap_therm"
31 static struct event_data {
32 ThermalUpdated updated_cb;
38 static int thermal_get_info(struct thermal_info *info)
48 fp = fopen(THERMAL_PATH, "r");
50 _E("Failed to open %s(%d)", THERMAL_PATH, errno);
54 len = fread(buf, 1, sizeof(buf) - 1, fp);
57 _E("Failed to read %s(%d)", THERMAL_PATH, errno);
62 ret = strstr(buf, "temp:");
64 _E("Invalid temp value (%s)", buf);
67 info->temp = atoi(ret + 5); /* 5 == strlen("temp:") */
69 ret = strstr(buf, "adc:");
71 _E("Invalid adc value (%s)", buf);
74 info->adc = atoi(ret + 4); /* 4 == strlen("adc:") */
76 _I("AP temp(%d) adc(%d)", info->temp, info->adc);
81 static gboolean thermal_timeout(gpointer data)
83 struct thermal_info info;
86 ret = thermal_get_info(&info);
88 _E("Failed to read thermal state (%d)", ret);
89 return G_SOURCE_CONTINUE;
93 edata.updated_cb(&info, edata.data);
95 return G_SOURCE_CONTINUE;
98 static int thermal_register_changed_event(ThermalUpdated updated_cb, void *data)
101 g_source_remove(timer);
103 timer = g_timeout_add(10000, thermal_timeout, NULL);
105 _E("Failed to add timer for thermal");
109 edata.updated_cb = updated_cb;
115 static int thermal_unregister_changed_event(ThermalUpdated updated_cb)
118 g_source_remove(timer);
122 edata.updated_cb = NULL;
128 static int thermal_open(struct hw_info *info,
129 const char *id, struct hw_common **common)
131 struct thermal_device *thermal_dev;
133 if (!info || !common)
136 thermal_dev = calloc(1, sizeof(struct thermal_device));
140 thermal_dev->common.info = info;
141 thermal_dev->register_changed_event
142 = thermal_register_changed_event;
143 thermal_dev->unregister_changed_event
144 = thermal_unregister_changed_event;
145 thermal_dev->get_info
148 *common = (struct hw_common *)thermal_dev;
152 static int thermal_close(struct hw_common *common)
161 HARDWARE_MODULE_STRUCTURE = {
162 .magic = HARDWARE_INFO_TAG,
163 .hal_version = HARDWARE_INFO_VERSION,
164 .device_version = THERMAL_HARDWARE_DEVICE_VERSION,
165 .id = THERMAL_HARDWARE_DEVICE_ID,
167 .open = thermal_open,
168 .close = thermal_close,