blob: 579efa1d179962a2f965433e45e4c4cbdad8405e [file] [log] [blame]
Benjamin Schwartzc798cb32021-03-18 17:55:40 -07001/*
2 * Copyright (C) 2021 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
17#define LOG_TAG "android.hardware.power.stats-service.pixel"
18
Benjamin Schwartze04673c2021-04-05 22:26:29 -070019#include <dataproviders/DisplayStateResidencyDataProvider.h>
Benjamin Schwartzeeaf5ea2021-04-14 14:49:31 -070020#include <dataproviders/GenericStateResidencyDataProvider.h>
Benjamin Schwartze04673c2021-04-05 22:26:29 -070021#include <dataproviders/PowerStatsEnergyConsumer.h>
Benjamin Schwartzc798cb32021-03-18 17:55:40 -070022#include <Gs101CommonDataProviders.h>
Benjamin Schwartze04673c2021-04-05 22:26:29 -070023#include <PowerStatsAidl.h>
Benjamin Schwartzc798cb32021-03-18 17:55:40 -070024
25#include <android-base/logging.h>
26#include <android-base/properties.h>
27#include <android/binder_manager.h>
28#include <android/binder_process.h>
29#include <log/log.h>
30
Benjamin Schwartze04673c2021-04-05 22:26:29 -070031using aidl::android::hardware::power::stats::DisplayStateResidencyDataProvider;
32using aidl::android::hardware::power::stats::EnergyConsumerType;
Benjamin Schwartzeeaf5ea2021-04-14 14:49:31 -070033using aidl::android::hardware::power::stats::GenericStateResidencyDataProvider;
Benjamin Schwartze04673c2021-04-05 22:26:29 -070034using aidl::android::hardware::power::stats::PowerStatsEnergyConsumer;
35
samouf1820db2021-07-29 07:38:05 +000036const char kBootRevision[] = "ro.boot.revision";
37std::map<std::string, std::string> displayChannelNames = {
38 {"PROTO1.0", "PPVAR_VSYS_PWR_DISP"},
39 {"EVT1.0", "PPVAR_VSYS_PWR_DISP"},
40 {"EVT1.1", "VSYS_PWR_DISPLAY"},
41};
42
Benjamin Schwartze04673c2021-04-05 22:26:29 -070043void addDisplay(std::shared_ptr<PowerStats> p) {
44 // Add display residency stats
45 std::vector<std::string> states = {
46 "Off",
47 "LP: 1440x3120@10",
48 "LP: 1440x3120@30",
49 "On: 1440x3120@10",
50 "On: 1440x3120@30",
51 "On: 1440x3120@60",
52 "On: 1440x3120@90",
53 "On: 1440x3120@120",
54 "HBM: 1440x3120@60",
55 };
56
57 p->addStateResidencyDataProvider(std::make_unique<DisplayStateResidencyDataProvider>("Display",
58 "/sys/class/backlight/panel0-backlight/state",
59 states));
60
samouf1820db2021-07-29 07:38:05 +000061 std::string rev = android::base::GetProperty(kBootRevision, "");
62
63 std::string channelName;
64 if (displayChannelNames.find(rev) == displayChannelNames.end()) {
65 channelName = displayChannelNames["EVT1.1"];
66 } else {
67 channelName = displayChannelNames[rev];
68 }
69
Benjamin Schwartze04673c2021-04-05 22:26:29 -070070 // Add display energy consumer
71 /*
72 * TODO(b/167216667): Add correct display power model here. Must read from display rail
73 * and include proper coefficients for display states.
74 */
75 p->addEnergyConsumer(PowerStatsEnergyConsumer::createMeterAndEntityConsumer(p,
samouf1820db2021-07-29 07:38:05 +000076 EnergyConsumerType::DISPLAY, "display", {channelName}, "Display",
Benjamin Schwartze04673c2021-04-05 22:26:29 -070077 {{"LP: 1440x3120@10", 1},
78 {"LP: 1440x3120@30", 2},
79 {"On: 1440x3120@10", 3},
80 {"On: 1440x3120@30", 4},
81 {"On: 1440x3120@60", 5},
82 {"On: 1440x3120@90", 6},
83 {"On: 1440x3120@120", 7},
84 {"HBM: 1440x3120@60", 8}}));
85}
86
Benjamin Schwartzeeaf5ea2021-04-14 14:49:31 -070087void addUwb(std::shared_ptr<PowerStats> p) {
88 // A constant to represent the number of nanoseconds in one millisecond.
89 const int NS_TO_MS = 1000000;
90
91 // ACPM stats are reported in nanoseconds. The transform function
92 // converts nanoseconds to milliseconds.
93 std::function<uint64_t(uint64_t)> uwbNsToMs = [](uint64_t a) { return a / NS_TO_MS; };
94 const GenericStateResidencyDataProvider::StateResidencyConfig stateConfig = {
95 .entryCountSupported = true,
96 .entryCountPrefix = "count:",
97 .totalTimeSupported = true,
98 .totalTimePrefix = "dur ns:",
99 .totalTimeTransform = uwbNsToMs,
100 .lastEntrySupported = false,
101 };
102
103 const std::vector<std::pair<std::string, std::string>> stateHeaders = {
104 std::make_pair("Off", "Off state:"),
105 std::make_pair("Run", "Run state:"),
106 std::make_pair("Idle", "Idle state:"),
107 std::make_pair("Tx", "Tx state:"),
108 std::make_pair("Rx", "Rx state:"),
109 };
110
111 std::vector<GenericStateResidencyDataProvider::PowerEntityConfig> cfgs;
112 cfgs.emplace_back(generateGenericStateResidencyConfigs(stateConfig, stateHeaders),
113 "UWB", "");
114
115 p->addStateResidencyDataProvider(std::make_unique<GenericStateResidencyDataProvider>(
116 "/sys/devices/platform/10d30000.spi/spi_master/spi10/spi10.0/uwb/power_stats", cfgs));
117}
118
Benjamin Schwartzc798cb32021-03-18 17:55:40 -0700119int main() {
120 LOG(INFO) << "Pixel PowerStats HAL AIDL Service is starting.";
121
122 // single thread
123 ABinderProcess_setThreadPoolMaxThreadCount(0);
124
125 std::shared_ptr<PowerStats> p = ndk::SharedRefBase::make<PowerStats>();
126
127 addGs101CommonDataProviders(p);
Benjamin Schwartze04673c2021-04-05 22:26:29 -0700128 addDisplay(p);
Manish Varma9aa69ff2021-05-13 18:37:05 -0700129 addNFC(p, "/sys/devices/platform/10960000.hsi2c/i2c-4/i2c-st21nfc/power_stats");
Benjamin Schwartzeeaf5ea2021-04-14 14:49:31 -0700130 addUwb(p);
Benjamin Schwartzc798cb32021-03-18 17:55:40 -0700131
132 const std::string instance = std::string() + PowerStats::descriptor + "/default";
133 binder_status_t status = AServiceManager_addService(p->asBinder().get(), instance.c_str());
134 LOG_ALWAYS_FATAL_IF(status != STATUS_OK);
135
136 ABinderProcess_joinThreadPool();
137 return EXIT_FAILURE; // should not reach
138}