boot: refactor battery checking to use new powctl apis
This commit is contained in:
@@ -14,52 +14,111 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#pragma once
|
||||
#include "boot_bq24193_charger.hpp"
|
||||
#include "boot_i2c_utils.hpp"
|
||||
#include <stratosphere.hpp>
|
||||
|
||||
namespace ams::boot {
|
||||
|
||||
enum ChargerStatus {
|
||||
ChargerStatus_Unknown = 0,
|
||||
ChargerStatus_Charging = 1,
|
||||
ChargerStatus_NotCharging = 2,
|
||||
ChargerStatus_ChargeTerminationDone = 3,
|
||||
};
|
||||
|
||||
class ChargerDriver {
|
||||
private:
|
||||
static constexpr u32 GpioPadName_Bq24193Charger = 0xA;
|
||||
private:
|
||||
i2c::driver::I2cSession i2c_session;
|
||||
powctl::Session charger_session;
|
||||
public:
|
||||
ChargerDriver() {
|
||||
R_ABORT_UNLESS(i2c::driver::OpenSession(std::addressof(this->i2c_session), i2c::DeviceCode_Bq24193));
|
||||
|
||||
//boot::gpio::Configure(GpioPadName_Bq24193Charger);
|
||||
//boot::gpio::SetDirection(GpioPadName_Bq24193Charger, GpioDirection_Output);
|
||||
ChargerDriver() : charger_session() {
|
||||
R_ABORT_UNLESS(powctl::OpenSession(std::addressof(this->charger_session), powctl::DeviceCode_Bq24193, ddsf::AccessMode_ReadWrite));
|
||||
}
|
||||
|
||||
~ChargerDriver() {
|
||||
i2c::driver::CloseSession(this->i2c_session);
|
||||
powctl::CloseSession(this->charger_session);
|
||||
}
|
||||
private:
|
||||
Result Read(u8 addr, u8 *out_data);
|
||||
Result Write(u8 addr, u8 val);
|
||||
Result ReadWrite(u8 addr, u8 mask, u8 val);
|
||||
|
||||
Result SetInputCurrentLimit(bq24193::InputCurrentLimit current);
|
||||
Result SetForce20PercentChargeCurrent(bool force);
|
||||
Result SetPreChargeCurrentLimit(u32 current);
|
||||
Result SetTerminationCurrentLimit(u32 current);
|
||||
Result SetMinimumSystemVoltageLimit(u32 voltage);
|
||||
Result SetWatchdogTimerSetting(bq24193::WatchdogTimerSetting setting);
|
||||
Result SetChargingSafetyTimerEnabled(bool enabled);
|
||||
Result ResetWatchdogTimer();
|
||||
Result SetBoostModeCurrentLimit(bq24193::BoostModeCurrentLimit current);
|
||||
Result SetHiZEnabled(bool enabled);
|
||||
Result Initialize(bool set_input_current_limit) {
|
||||
/* Set input current limit to 500 ma. */
|
||||
if (set_input_current_limit) {
|
||||
R_TRY(powctl::SetChargerInputCurrentLimit(this->charger_session, 500));
|
||||
}
|
||||
|
||||
public:
|
||||
Result Initialize();
|
||||
Result Initialize(bool set_input_current_limit);
|
||||
Result SetChargeVoltageLimit(u32 voltage);
|
||||
Result SetFastChargeCurrentLimit(u32 current);
|
||||
Result SetChargeEnabled(bool enabled);
|
||||
Result SetChargerConfiguration(bq24193::ChargerConfiguration config);
|
||||
Result GetInputCurrentLimit(bq24193::InputCurrentLimit *out);
|
||||
Result GetChargeVoltageLimit(u32 *out);
|
||||
/* Set input voltage limit to 500 mv. */
|
||||
R_TRY(powctl::SetChargerInputVoltageLimit(this->charger_session, 500));
|
||||
|
||||
/* Disable hi-z mode. */
|
||||
R_TRY(powctl::SetChargerHiZEnabled(this->charger_session, false));
|
||||
|
||||
/* Set configuration to charge battery. */
|
||||
R_TRY(powctl::SetChargerChargerConfiguration(this->charger_session, powctl::ChargerConfiguration_ChargeBattery));
|
||||
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
Result GetChargeCurrentState(powctl::ChargeCurrentState *out) {
|
||||
return powctl::GetChargerChargeCurrentState(out, this->charger_session);
|
||||
}
|
||||
|
||||
Result SetChargeCurrentState(powctl::ChargeCurrentState state) {
|
||||
return powctl::SetChargerChargeCurrentState(this->charger_session, state);
|
||||
}
|
||||
|
||||
Result GetInputCurrentLimit(int *out) {
|
||||
return powctl::GetChargerInputCurrentLimit(out, this->charger_session);
|
||||
}
|
||||
|
||||
Result SetChargerConfiguration(powctl::ChargerConfiguration cfg) {
|
||||
return powctl::SetChargerChargerConfiguration(this->charger_session, cfg);
|
||||
}
|
||||
|
||||
Result GetFastChargeCurrentLimit(int *out) {
|
||||
return powctl::GetChargerFastChargeCurrentLimit(out, this->charger_session);
|
||||
}
|
||||
|
||||
Result SetFastChargeCurrentLimit(int limit) {
|
||||
return powctl::SetChargerFastChargeCurrentLimit(this->charger_session, limit);
|
||||
}
|
||||
|
||||
Result GetChargeVoltageLimit(int *out) {
|
||||
return powctl::GetChargerChargeVoltageLimit(out, this->charger_session);
|
||||
}
|
||||
|
||||
Result SetChargeVoltageLimit(int limit) {
|
||||
return powctl::SetChargerChargeVoltageLimit(this->charger_session, limit);
|
||||
}
|
||||
|
||||
Result GetChargerStatus(boot::ChargerStatus *out) {
|
||||
/* Default to unknown. */
|
||||
*out = ChargerStatus_Unknown;
|
||||
|
||||
/* Get the powctl status. */
|
||||
powctl::ChargerStatus powctl_status;
|
||||
R_TRY(powctl::GetChargerChargerStatus(std::addressof(powctl_status), this->charger_session));
|
||||
|
||||
switch (powctl_status) {
|
||||
case powctl::ChargerStatus_Charging: *out = boot::ChargerStatus_Charging; break;
|
||||
case powctl::ChargerStatus_NotCharging: *out = boot::ChargerStatus_NotCharging; break;
|
||||
case powctl::ChargerStatus_ChargeTerminationDone: *out = boot::ChargerStatus_ChargeTerminationDone; break;
|
||||
}
|
||||
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
Result GetBatteryCompensation(int *out) {
|
||||
return powctl::GetChargerBatteryCompensation(out, this->charger_session);
|
||||
}
|
||||
|
||||
Result SetBatteryCompensation(int v) {
|
||||
return powctl::SetChargerBatteryCompensation(this->charger_session, v);
|
||||
}
|
||||
|
||||
Result GetVoltageClamp(int *out) {
|
||||
return powctl::GetChargerVoltageClamp(out, this->charger_session);
|
||||
}
|
||||
|
||||
Result SetVoltageClamp(int v) {
|
||||
return powctl::SetChargerVoltageClamp(this->charger_session, v);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user