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, .block_id = 0, .param_id = SENSOR_PARAM_DUMP_STATS, .param_val = 1};
50 
51     err = ioctl(devNode.get(), MODULE_SENSOR_SET_PARAM, (unsigned long)&sp);
52     if (err) {
53         PLOG(ERROR) << __func__ << ": MODULE_SENSOR_SET_PARAM IOCTL failed";
54         return false;
55     }
56 
57     struct iaxxx_sensor_mode_stats stats[SENSOR_NUM_MODE];
58     err = ioctl(devNode.get(), IAXXX_SENSOR_MODE_STATS, (unsigned long)stats);
59     if (err) {
60         PLOG(ERROR) << __func__ << ": IAXXX_SENSOR_MODE_STATS IOCTL failed";
61         return false;
62     }
63 
64     PowerEntityStateResidencyResult result = {
65         .powerEntityId = mPowerEntityId,
66         .stateResidencyData = {
67             {.powerEntityStateId = SENSOR_MODE_OFF,
68              .totalTimeInStateMs = stats[SENSOR_MODE_OFF].totalTimeSpentMs,
69              .totalStateEntryCount = stats[SENSOR_MODE_OFF].totalNumEntries,
70              .lastEntryTimestampMs = stats[SENSOR_MODE_OFF].lastEntryTimeStampMs},
71             {.powerEntityStateId = SENSOR_MODE_ENTRANCE,
72              .totalTimeInStateMs = stats[SENSOR_MODE_ENTRANCE].totalTimeSpentMs,
73              .totalStateEntryCount = stats[SENSOR_MODE_ENTRANCE].totalNumEntries,
74              .lastEntryTimestampMs = stats[SENSOR_MODE_ENTRANCE].lastEntryTimeStampMs},
75             {.powerEntityStateId = SENSOR_MODE_INTERACTIVE,
76              .totalTimeInStateMs = stats[SENSOR_MODE_INTERACTIVE].totalTimeSpentMs,
77              .totalStateEntryCount = stats[SENSOR_MODE_INTERACTIVE].totalNumEntries,
78              .lastEntryTimestampMs = stats[SENSOR_MODE_INTERACTIVE].lastEntryTimeStampMs}}};
79 
80     results.insert(std::make_pair(mPowerEntityId, result));
81     return true;
82 }
83 
getStateSpaces()84 std::vector<PowerEntityStateSpace> OsloStateResidencyDataProvider::getStateSpaces() {
85     return {{.powerEntityId = mPowerEntityId,
86              .states = {
87                  {.powerEntityStateId = SENSOR_MODE_OFF, .powerEntityStateName = "Off"},
88                  {.powerEntityStateId = SENSOR_MODE_ENTRANCE, .powerEntityStateName = "Entrance"},
89                  {.powerEntityStateId = SENSOR_MODE_INTERACTIVE,
90                   .powerEntityStateName = "Interactive"}}}};
91 }
92 
93 }  // namespace powerstats
94 }  // namespace pixel
95 }  // namespace google
96 }  // namespace hardware
97 }  // namespace android
98