boot: refactor battery checking to use new powctl apis

This commit is contained in:
Michael Scire
2020-11-08 04:16:50 -08:00
parent cd7d7894f1
commit e93c3cbf58
44 changed files with 1426 additions and 1554 deletions

View File

@@ -41,6 +41,7 @@
/* At this point, just include the rest alphabetically. */
/* TODO: Figure out optimal order. */
#include <stratosphere/boot2.hpp>
#include <stratosphere/cal.hpp>
#include <stratosphere/capsrv.hpp>
#include <stratosphere/cfg.hpp>
#include <stratosphere/clkrst.hpp>

View File

@@ -0,0 +1,19 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stratosphere/cal/cal_battery_api.hpp>

View File

@@ -0,0 +1,24 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vapours.hpp>
namespace ams::cal {
Result GetBatteryVersion(u8 *out);
Result GetBatteryVendor(size_t *out_vendor_size, void *dst, size_t dst_size);
}

View File

@@ -20,4 +20,7 @@
#include <stratosphere/powctl/powctl_session_api.hpp>
#include <stratosphere/powctl/powctl_battery_api.hpp>
#include <stratosphere/powctl/powctl_charger_api.hpp>
#include <stratosphere/powctl/impl/powctl_battery_charge_percentage.hpp>
#include <stratosphere/powctl/driver/powctl_driver_api.hpp>
#include <stratosphere/powctl/driver/impl/powctl_select_charger_parameters.hpp>
#include <stratosphere/powctl/driver/impl/powctl_charge_arbiter.hpp>

View File

@@ -0,0 +1,161 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vapours.hpp>
#include <stratosphere/powctl/powctl_types.hpp>
#include <stratosphere/powctl/driver/impl/powctl_select_charger_parameters.hpp>
namespace ams::powctl::driver::impl {
class ChargeArbiter {
private:
const ChargeParametersRule *rules;
size_t num_rules;
int charge_voltage_limit;
BatteryTemperatureLevel temperature_level;
int avg_v_cell;
int open_circuit_voltage;
bool has_battery_done_current;
int battery_done_current;
PowerState power_state;
const ChargeParametersRule *selected_rule;
bool check_battery_done_current;
private:
static constexpr bool IsInRange(int value, int min, int max) {
if (!(min <= value)) {
return false;
}
if (max == std::numeric_limits<int>::max()) {
return value <= max;
} else {
return value < max;
}
}
bool IsAcceptablePowerState(const PowerState *acceptable, size_t num_acceptable) const {
for (size_t i = 0; i < num_acceptable; ++i) {
if (this->power_state == acceptable[i]) {
return true;
}
}
return false;
}
public:
ChargeArbiter(const ChargeParametersRule *r, size_t nr, int cvl)
: rules(r), num_rules(nr), charge_voltage_limit(cvl), temperature_level(BatteryTemperatureLevel::Medium),
avg_v_cell(4080), open_circuit_voltage(4001), has_battery_done_current(false), battery_done_current(0),
power_state(PowerState::FullAwake), selected_rule(nullptr), check_battery_done_current(false)
{
this->UpdateSelectedRule();
}
void SetBatteryTemperatureLevel(BatteryTemperatureLevel btl) {
this->temperature_level = btl;
this->UpdateSelectedRule();
}
void SetBatteryAverageVCell(int avg) {
this->avg_v_cell = avg;
this->UpdateSelectedRule();
}
void SetBatteryOpenCircuitVoltage(int ocv) {
this->open_circuit_voltage = ocv;
this->UpdateSelectedRule();
}
void SetBatteryDoneCurrent(int current) {
this->battery_done_current = current;
this->has_battery_done_current = true;
this->UpdateSelectedRule();
}
void SetPowerState(PowerState ps) {
this->power_state = ps;
this->UpdateSelectedRule();
}
int GetChargeVoltageLimit() const {
return this->charge_voltage_limit;
}
bool IsBatteryDoneCurrentAcceptable(int current) const {
const auto *rule = this->GetSelectedRule();
AMS_ASSERT(rule != nullptr);
return IsInRange(0, rule->min_battery_done_current, rule->max_battery_done_current);
}
const ChargeParametersRule *GetSelectedRule() const {
return this->selected_rule;
}
void UpdateSelectedRule() {
/* Try to find an entry that fits our current requirements. */
const ChargeParametersRule *best_rule = nullptr;
for (size_t i = 0; i < this->num_rules; ++i) {
/* Get the current rule. */
const ChargeParametersRule &cur_rule = this->rules[i];
/* Check the temperature level. */
if (this->temperature_level != cur_rule.temperature_level) {
continue;
}
/* Check that average voltage is in range. */
if (!IsInRange(this->avg_v_cell, cur_rule.min_avg_v_cell, cur_rule.max_avg_v_cell)) {
continue;
}
/* Check that open circuit voltage is in range. */
if (!IsInRange(this->open_circuit_voltage, cur_rule.min_open_circuit_voltage, cur_rule.max_open_circuit_voltage)) {
continue;
}
/* Check if our power state is acceptable. */
if (!this->IsAcceptablePowerState(cur_rule.acceptable_power_states, cur_rule.num_acceptable_power_states)) {
continue;
}
/* The limit is probably acceptable. */
if (this->selected_rule != std::addressof(cur_rule)) {
/* We're selecting a new rule. Check if our need to deal with battery current is acceptable. */
if (cur_rule.check_battery_current && this->check_battery_done_current) {
continue;
}
/* Set whether we need to check the battery done current. */
this->has_battery_done_current = false;
this->check_battery_done_current |= cur_rule.check_battery_current;
} else {
/* We're selecting the currently selected rule. Make sure the battery done current is acceptable if we have one. */
if (this->has_battery_done_current && !IsInRange(this->battery_done_current, cur_rule.min_battery_done_current, cur_rule.max_battery_done_current)) {
continue;
}
}
/* Select the current rule. */
best_rule = std::addressof(cur_rule);
break;
}
/* Update our selected rule. */
this->selected_rule = best_rule;
}
};
}

View File

@@ -0,0 +1,64 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vapours.hpp>
#include <stratosphere/powctl/powctl_types.hpp>
namespace ams::powctl::driver::impl {
struct ChargeParametersRule {
BatteryTemperatureLevel temperature_level;
int min_avg_v_cell;
int max_avg_v_cell;
int min_open_circuit_voltage;
int max_open_circuit_voltage;
int min_battery_done_current;
int max_battery_done_current;
const PowerState *acceptable_power_states;
size_t num_acceptable_power_states;
bool check_battery_current;
bool reinitialize_charger;
int charge_voltage_limit;
int fast_charge_current_limit;
int battery_compensation;
int voltage_clamp;
};
struct UnknownParameterX {
int _00;
int _04;
double _08;
double _10;
};
struct ChargeParameters {
int temp_min;
int temp_low;
int temp_high;
int temp_max;
int low_voltage_fast_charge_current_limit;
int default_charge_voltage_limit;
const UnknownParameterX *unknown_x_table;
size_t x_table_size;
double _28;
double _30;
const ChargeParametersRule *rules;
size_t num_rules;
};
const ChargeParameters &GetChargeParameters();
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vapours.hpp>
#include <stratosphere/powctl/powctl_types.hpp>
#if defined(ATMOSPHERE_BOARD_NINTENDO_NX)
#include <stratosphere/powctl/driver/impl/powctl_charger_parameters.board.nintendo_nx.hpp>
#else
#error "unknown board for powctl::driver::impl::ChargerParameters"
#endif

View File

@@ -0,0 +1,65 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vapours.hpp>
#include <stratosphere/powctl/powctl_types.hpp>
namespace ams::powctl::impl {
constexpr inline const double MinRawDefaultPercentage = 3.0;
constexpr inline const double MaxRawDefaultPercentage = 99.0;
constexpr inline const double MinRawThresholdPercentage = 11.0;
constexpr inline const int MinDisplayPercentage = 1;
constexpr inline const int MaxDisplayPercentage = 100;
constexpr inline void CalculateMarginatedRawPercentage(double *out_marginated_min, double *out_marginated_max, double min, double max) {
/* Ensure minimum is in correct range. */
min = std::max(std::min(min, MinRawThresholdPercentage), MinRawDefaultPercentage);
/* Calculate the marginated values. */
constexpr const double MinMarginPercentage = 0.93359375;
constexpr const double MaxMarginPercentage = -0.83593750;
const auto margin_factor = (max - min) / (MaxRawDefaultPercentage - MinRawDefaultPercentage);
*out_marginated_min = min + MinMarginPercentage * margin_factor;
*out_marginated_max = max + MaxMarginPercentage * margin_factor;
}
constexpr inline int GetDisplayPercentage(double raw_percentage, double min, double max) {
/* Calculate the display percentage. */
constexpr const double BaseDisplayPercentage = 2.0;
const auto display_percentage = BaseDisplayPercentage + ((static_cast<double>(MaxDisplayPercentage - MinDisplayPercentage) * (raw_percentage - min)) / (max - min));
/* Clamp the display percentage within bounds. */
return std::max(std::min(static_cast<int>(display_percentage), MaxDisplayPercentage), MinDisplayPercentage);
}
constexpr inline int ConvertBatteryChargePercentage(double raw_percentage, double min, double max) {
/* Marginate the min/max. */
double marginated_min = 0.0, marginated_max = 0.0;
CalculateMarginatedRawPercentage(std::addressof(marginated_min), std::addressof(marginated_max), min, max);
/* Convert to display percentage. */
return GetDisplayPercentage(raw_percentage, marginated_min, marginated_max);
}
constexpr inline int ConvertBatteryChargePercentage(double raw_percentage) {
return ConvertBatteryChargePercentage(raw_percentage, MinRawDefaultPercentage, MaxRawDefaultPercentage);
}
}

View File

@@ -25,15 +25,15 @@ namespace ams::powctl {
Result GetBatterySocVf(float *out_percent, Session &session);
Result GetBatteryFullCapacity(u32 *out_mah, Session &session);
Result GetBatteryRemainingCapacity(u32 *out_mah, Session &session);
Result GetBatteryFullCapacity(int *out_mah, Session &session);
Result GetBatteryRemainingCapacity(int *out_mah, Session &session);
Result SetBatteryPercentageMinimumAlertThreshold(Session &session, float percentage);
Result SetBatteryPercentageMaximumAlertThreshold(Session &session, float percentage);
Result SetBatteryPercentageFullThreshold(Session &session, float percentage);
Result GetBatteryAverageCurrent(u32 *out_ma, Session &session);
Result GetBatteryCurrent(u32 *out_ma, Session &session);
Result GetBatteryAverageCurrent(int *out_ma, Session &session);
Result GetBatteryCurrent(int *out_ma, Session &session);
Result GetBatteryInternalState(void *dst, size_t *out_size, Session &session, size_t dst_size);
Result SetBatteryInternalState(Session &session, const void *src, size_t src_size);
@@ -44,10 +44,10 @@ namespace ams::powctl {
Result IsBatteryI2cShutdownEnabled(bool *out, Session &session);
Result SetBatteryI2cShutdownEnabled(Session &session, bool en);
Result IsBatteryRemoved(bool *out, Session &session);
Result IsBatteryPresent(bool *out, Session &session);
Result GetBatteryCycles(u32 *out, Session &session);
Result SetBatteryCycles(Session &session, u32 cycles);
Result GetBatteryCycles(int *out, Session &session);
Result SetBatteryCycles(Session &session, int cycles);
Result GetBatteryAge(float *out_percent, Session &session);
@@ -57,14 +57,14 @@ namespace ams::powctl {
Result SetBatteryTemperatureMinimumAlertThreshold(Session &session, float c);
Result SetBatteryTemperatureMaximumAlertThreshold(Session &session, float c);
Result GetBatteryVCell(u32 *out_mv, Session &session);
Result GetBatteryAverageVCell(u32 *out_mv, Session &session);
Result GetBatteryVCell(int *out_mv, Session &session);
Result GetBatteryAverageVCell(int *out_mv, Session &session);
Result GetBatteryAverageVCellTime(TimeSpan *out, Session &session);
Result GetBatteryOpenCircuitVoltage(u32 *out_mv, Session &session);
Result GetBatteryOpenCircuitVoltage(int *out_mv, Session &session);
Result SetBatteryVoltageMinimumAlertThreshold(Session &session, u32 mv);
Result SetBatteryVoltageMaximumAlertThreshold(Session &session, u32 mv);
Result SetBatteryVoltageMinimumAlertThreshold(Session &session, int mv);
Result SetBatteryVoltageMaximumAlertThreshold(Session &session, int mv);
}

View File

@@ -24,22 +24,22 @@ namespace ams::powctl {
Result GetChargerChargeCurrentState(ChargeCurrentState *out, Session &session);
Result SetChargerChargeCurrentState(Session &session, ChargeCurrentState state);
Result GetChargerFastChargeCurrentLimit(u32 *out_ma, Session &session);
Result SetChargerFastChargeCurrentLimit(Session &session, u32 ma);
Result GetChargerFastChargeCurrentLimit(int *out_ma, Session &session);
Result SetChargerFastChargeCurrentLimit(Session &session, int ma);
Result GetChargerChargeVoltageLimit(u32 *out_mv, Session &session);
Result SetChargerChargeVoltageLimit(Session &session, u32 mv);
Result GetChargerChargeVoltageLimit(int *out_mv, Session &session);
Result SetChargerChargeVoltageLimit(Session &session, int mv);
Result SetChargerChargerConfiguration(Session &session, ChargerConfiguration cfg);
Result IsChargerHiZEnabled(bool *out, Session &session);
Result SetChargerHiZEnabled(Session &session, bool en);
Result GetChargerInputCurrentLimit(u32 *out_ma, Session &session);
Result SetChargerInputCurrentLimit(Session &session, u32 ma);
Result GetChargerInputCurrentLimit(int *out_ma, Session &session);
Result SetChargerInputCurrentLimit(Session &session, int ma);
Result GetChargerInputVoltageLimit(u32 *out_mv, Session &session);
Result SetChargerInputVoltageLimit(Session &session, u32 mv);
Result GetChargerInputVoltageLimit(int *out_mv, Session &session);
Result SetChargerInputVoltageLimit(Session &session, int mv);
Result GetChargerChargerStatus(ChargerStatus *out, Session &session);
@@ -49,10 +49,10 @@ namespace ams::powctl {
Result SetChargerWatchdogTimerTimeout(Session &session, TimeSpan timeout);
Result ResetChargerWatchdogTimer(Session &session);
Result GetChargerBatteryCompensation(u32 *out_mo, Session &session);
Result SetChargerBatteryCompensation(Session &session, u32 mo);
Result GetChargerBatteryCompensation(int *out_mo, Session &session);
Result SetChargerBatteryCompensation(Session &session, int mo);
Result GetChargerVoltageClamp(u32 *out_mv, Session &session);
Result SetChargerVoltageClamp(Session &session, u32 mv);
Result GetChargerVoltageClamp(int *out_mv, Session &session);
Result SetChargerVoltageClamp(Session &session, int mv);
}

View File

@@ -27,9 +27,9 @@ namespace ams::powctl {
};
enum ChargerConfiguration {
ChargerConfiguration_ChargeDisable = 0,
ChargerConfiguration_ChargeBattery = 1,
ChargerConfiguration_Otg = 2,
ChargerConfiguration_ChargeDisable = 1,
ChargerConfiguration_ChargeBattery = 2,
ChargerConfiguration_Otg = 3,
};
enum ChargeCurrentState {
@@ -38,4 +38,20 @@ namespace ams::powctl {
ChargeCurrentState_Charging = 0x3,
};
enum class BatteryTemperatureLevel {
TooLow = 0,
Low = 1,
Medium = 2,
High = 3,
TooHigh = 4,
};
enum class PowerState {
FullAwake = 0,
MinimumAwake = 1,
SleepCharge = 2,
SleepDischarge = 3,
ShutdownChargeMain = 4,
};
}

View File

@@ -150,6 +150,15 @@ namespace ams::spl {
};
};
static_assert(sizeof(BootReasonValue) == sizeof(u32), "BootReasonValue definition!");
enum BootReason {
BootReason_Unknown = 0,
BootReason_AcOk = 1,
BootReason_OnKey = 2,
BootReason_RtcAlarm1 = 3,
BootReason_RtcAlarm2 = 4,
};
#pragma pack(push, 1)
struct AesKey {