1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #define LOG_TAG "libpixelpowerstats"
17 
18 #include "OsloStateResidencyDataProvider.h"
19 
20 #include <android-base/logging.h>
21 #include <android-base/unique_fd.h>
22 #include <fcntl.h>
23 #include <sys/ioctl.h>
24 
25 #include <utility>
26 
27 #include <linux/mfd/adnc/iaxxx-module.h>
28 #include "tests/oslo_iaxxx_sensor_control.h"
29 
30 namespace android {
31 namespace hardware {
32 namespace google {
33 namespace pixel {
34 namespace powerstats {
35 
OsloStateResidencyDataProvider(uint32_t id)36 OsloStateResidencyDataProvider::OsloStateResidencyDataProvider(uint32_t id)
37         : mPath("/dev/iaxxx-module-celldrv"), mPowerEntityId(id) {}
38 
getResults(std::unordered_map<uint32_t,PowerEntityStateResidencyResult> & results)39 bool OsloStateResidencyDataProvider::getResults(
40         std::unordered_map<uint32_t, PowerEntityStateResidencyResult> &results) {
41     android::base::unique_fd devNode(open(mPath.c_str(), O_RDWR));
42     if (devNode.get() < 0) {
43         PLOG(ERROR) << __func__ << ":Failed to open file " << mPath;
44         return false;
45     }
46 
47     int err = 0;
48     struct iaxxx_sensor_param sp = {
49         .inst_id = 0,
50         .param_id = SENSOR_PARAM_DUMP_STATS,
51         .param_val = 1,
52         .block_id = 0,
53     };
54 
55     err = ioctl(devNode.get(), MODULE_SENSOR_SET_PARAM, (unsigned long)&sp);
56     if (err) {
57         PLOG(ERROR) << __func__ << ": MODULE_SENSOR_SET_PARAM IOCTL failed";
58         return false;
59     }
60 
61     struct iaxxx_sensor_mode_stats stats[SENSOR_NUM_MODE];
62     err = ioctl(devNode.get(), IAXXX_SENSOR_MODE_STATS, (unsigned long)stats);
63     if (err) {
64         PLOG(ERROR) << __func__ << ": IAXXX_SENSOR_MODE_STATS IOCTL failed";
65         return false;
66     }
67 
68     PowerEntityStateResidencyResult result = {
69         .powerEntityId = mPowerEntityId,
70         .stateResidencyData = {
71             {.powerEntityStateId = SENSOR_MODE_OFF,
72              .totalTimeInStateMs = stats[SENSOR_MODE_OFF].totalTimeSpentMs,
73              .totalStateEntryCount = stats[SENSOR_MODE_OFF].totalNumEntries,
74              .lastEntryTimestampMs = stats[SENSOR_MODE_OFF].lastEntryTimeStampMs},
75             {.powerEntityStateId = SENSOR_MODE_ENTRANCE,
76              .totalTimeInStateMs = stats[SENSOR_MODE_ENTRANCE].totalTimeSpentMs,
77              .totalStateEntryCount = stats[SENSOR_MODE_ENTRANCE].totalNumEntries,
78              .lastEntryTimestampMs = stats[SENSOR_MODE_ENTRANCE].lastEntryTimeStampMs},
79             {.powerEntityStateId = SENSOR_MODE_INTERACTIVE,
80              .totalTimeInStateMs = stats[SENSOR_MODE_INTERACTIVE].totalTimeSpentMs,
81              .totalStateEntryCount = stats[SENSOR_MODE_INTERACTIVE].totalNumEntries,
82              .lastEntryTimestampMs = stats[SENSOR_MODE_INTERACTIVE].lastEntryTimeStampMs}}};
83 
84     results.insert(std::make_pair(mPowerEntityId, result));
85     return true;
86 }
87 
getStateSpaces()88 std::vector<PowerEntityStateSpace> OsloStateResidencyDataProvider::getStateSpaces() {
89     return {{.powerEntityId = mPowerEntityId,
90              .states = {
91                  {.powerEntityStateId = SENSOR_MODE_OFF, .powerEntityStateName = "Off"},
92                  {.powerEntityStateId = SENSOR_MODE_ENTRANCE, .powerEntityStateName = "Entrance"},
93                  {.powerEntityStateId = SENSOR_MODE_INTERACTIVE,
94                   .powerEntityStateName = "Interactive"}}}};
95 }
96 
97 }  // namespace powerstats
98 }  // namespace pixel
99 }  // namespace google
100 }  // namespace hardware
101 }  // namespace android
102