power: Convert to fully binderized 1.1 implementation

Based on commits c043816b3ceb8d8094c13f76341d4c86f09a44d1
and f3e845ce21adf40b4caa8982447b0bf99eeeee81
from AOSP device/google/wahoo.

Commit 1:
commit: c043816b3ceb8d8094c13f76341d4c86f09a44d1
author: Ahmed ElArabawy <arabawy@google.com>
date:   Wed Apr 12 09:50:07 2017 -0700

power hal: Add power HAL API 1.1 impl for Wahoo

Add a full binderized implementation for Power hal

Many subsystems (e.g.wifi) could be living on an
independent power island (sourced from VBatt directly)
and might even have their own dedicated XTAL to source
their clocks. Since these SOCs are capable of
autonomously operating (while the platform is in one
of the sleep states), they are still drawing power
from the VBatt. Hence it is critical to understand
the  (SOC) level low power statistics as well when
the battery level changes and be able to find any
correlation in event of unexpected battery drain.

This commit adds the support of the Power Hal 1.1
to wahoo based devices(that includes Muskie/walleye).
This includes the new api for wlan specific power stats

Bug: 29339696
Test: Manual
Change-Id: Iee4e38f2d9ced31f8b6a333b535fa1d9a302ec26
Signed-off-by: Ahmed ElArabawy <arabawy@google.com>

Commit 2:
commit: f3e845ce21adf40b4caa8982447b0bf99eeeee81
author: Ahmed ElArabawy <arabawy@google.com>
aate:   Wed May 24 15:28:04 2017 +0000

Re-introduce of POWER HAL API 1.1 impl for Wahoo

Power HAL 1.1 support for wahoo based devices was initially introduced in CL
ag/2098359
However, this caused a regression in application startup times due
to a bug in passing parameters for power hints on application launch
Hence, that CL was reverted in CL ag/2270791

This commit brings back the support of the Power Hal 1.1
to wahoo based devices. This includes the changes of the original CL
as will as a fix for the app startup time regression

The fix is similar to that in ag/1767023 (done for power HAL 1.0 default
implementation)
where a NULL is passed to the powerHint function when the passed data is
Zero
(instead of passing a pointer to the data). This enable the App Launch
power hints to work properly

The commit has been tested not to cause that regression

Bug: 62040325
Test: Performance Tests along with other tests

Change-Id: I29ce38b2de92c2b9ad878b0076288b689695b8a0
Signed-off-by: Ahmed ElArabawy <arabawy@google.com>
diff --git a/Power.cpp b/Power.cpp
new file mode 100644
index 0000000..186f325
--- /dev/null
+++ b/Power.cpp
@@ -0,0 +1,172 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ * Copyright (C) 2017 The LineageOS Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define LOG_TAG "android.hardware.power@1.1-service.wahoo"
+
+#include <android/log.h>
+#include <utils/Log.h>
+#include "Power.h"
+#include "power-common.h"
+#include "power-helper.h"
+
+/* RPM runs at 19.2Mhz. Divide by 19200 for msec */
+#define RPM_CLK 19200
+
+extern struct stat_pair rpm_stat_map[];
+
+namespace android {
+namespace hardware {
+namespace power {
+namespace V1_1 {
+namespace implementation {
+
+using ::android::hardware::power::V1_0::Feature;
+using ::android::hardware::power::V1_0::PowerHint;
+using ::android::hardware::power::V1_0::PowerStatePlatformSleepState;
+using ::android::hardware::power::V1_0::Status;
+using ::android::hardware::power::V1_1::PowerStateSubsystem;
+using ::android::hardware::hidl_vec;
+using ::android::hardware::Return;
+using ::android::hardware::Void;
+
+Power::Power() {
+    power_init();
+}
+
+// Methods from ::android::hardware::power::V1_0::IPower follow.
+Return<void> Power::setInteractive(bool interactive)  {
+    power_set_interactive(interactive ? 1 : 0);
+    return Void();
+}
+
+Return<void> Power::powerHint(PowerHint hint, int32_t data) {
+    power_hint(static_cast<power_hint_t>(hint), data ? (&data) : NULL);
+    return Void();
+}
+
+Return<void> Power::setFeature(Feature feature, bool activate)  {
+    set_feature(static_cast<feature_t>(feature), activate ? 1 : 0);
+    return Void();
+}
+
+Return<void> Power::getPlatformLowPowerStats(getPlatformLowPowerStats_cb _hidl_cb) {
+#if 0 // we don't do anything with this
+    hidl_vec<PowerStatePlatformSleepState> states;
+    uint64_t stats[MAX_PLATFORM_STATS * MAX_RPM_PARAMS] = {0};
+    uint64_t *values;
+    struct PowerStatePlatformSleepState *state;
+    int ret;
+
+    states.resize(PLATFORM_SLEEP_MODES_COUNT);
+
+    ret = extract_platform_stats(stats);
+    if (ret != 0) {
+        states.resize(0);
+        goto done;
+    }
+
+    /* Update statistics for XO_shutdown */
+    state = &states[RPM_MODE_XO];
+    state->name = "XO_shutdown";
+    values = stats + (RPM_MODE_XO * MAX_RPM_PARAMS);
+
+    state->residencyInMsecSinceBoot = values[1];
+    state->totalTransitions = values[0];
+    state->supportedOnlyInSuspend = false;
+    state->voters.resize(XO_VOTERS);
+    for(size_t i = 0; i < XO_VOTERS; i++) {
+        int voter = i + XO_VOTERS_START;
+        state->voters[i].name = rpm_stat_map[voter].label;
+        values = stats + (voter * MAX_RPM_PARAMS);
+        state->voters[i].totalTimeInMsecVotedForSinceBoot = values[0] / RPM_CLK;
+        state->voters[i].totalNumberOfTimesVotedSinceBoot = values[1];
+    }
+
+    /* Update statistics for VMIN state */
+    state = &states[RPM_MODE_VMIN];
+    state->name = "VMIN";
+    values = stats + (RPM_MODE_VMIN * MAX_RPM_PARAMS);
+
+    state->residencyInMsecSinceBoot = values[1];
+    state->totalTransitions = values[0];
+    state->supportedOnlyInSuspend = false;
+    state->voters.resize(VMIN_VOTERS);
+    //Note: No filling of state voters since VMIN_VOTERS = 0
+
+done:
+    _hidl_cb(states, Status::SUCCESS);
+#endif
+    return Void();
+}
+
+static int get_wlan_low_power_stats(struct PowerStateSubsystem &subsystem) {
+
+    uint64_t stats[WLAN_POWER_PARAMS_COUNT] = {0};
+    struct PowerStateSubsystemSleepState *state;
+    int ret;
+
+    ret = extract_wlan_stats(stats);
+    if (ret)
+        return ret;
+
+    subsystem.name = "wlan";
+    subsystem.states.resize(WLAN_STATES_COUNT);
+
+    /* Update statistics for Active State */
+    state = &subsystem.states[WLAN_STATE_ACTIVE];
+    state->name = "Active";
+    state->residencyInMsecSinceBoot = stats[CUMULATIVE_TOTAL_ON_TIME_MS];
+    state->totalTransitions = stats[DEEP_SLEEP_ENTER_COUNTER];
+    state->lastEntryTimestampMs = 0; //FIXME need a new value from Qcom
+    state->supportedOnlyInSuspend = false;
+
+    /* Update statistics for Deep-Sleep state */
+    state = &subsystem.states[WLAN_STATE_DEEP_SLEEP];
+    state->name = "Deep-Sleep";
+    state->residencyInMsecSinceBoot = stats[CUMULATIVE_SLEEP_TIME_MS];
+    state->totalTransitions = stats[DEEP_SLEEP_ENTER_COUNTER];
+    state->lastEntryTimestampMs = stats[LAST_DEEP_SLEEP_ENTER_TSTAMP_MS];
+    state->supportedOnlyInSuspend = false;
+
+    return 0;
+}
+
+// Methods from ::android::hardware::power::V1_1::IPower follow.
+Return<void> Power::getSubsystemLowPowerStats(getSubsystemLowPowerStats_cb _hidl_cb) {
+
+    hidl_vec<PowerStateSubsystem> subsystems;
+    int ret;
+
+    subsystems.resize(SUBSYSTEM_COUNT);
+
+    //We currently have only one Subsystem for WLAN
+    ret = get_wlan_low_power_stats(subsystems[SUBSYSTEM_WLAN]);
+    if (ret != 0)
+        goto done;
+
+    //Add query for other subsystems here
+
+done:
+    _hidl_cb(subsystems, Status::SUCCESS);
+    return Void();
+}
+
+}  // namespace implementation
+}  // namespace V1_1
+}  // namespace power
+}  // namespace hardware
+}  // namespace android