1
0
Fork 0
mirror of https://github.com/Atmosphere-NX/Atmosphere.git synced 2024-11-18 01:46:47 +00:00

boot: implement PmicDriver::ShutdownSystem

This commit is contained in:
Michael Scire 2019-05-09 02:45:31 -07:00
parent 7581306109
commit 1456246f60
6 changed files with 85 additions and 4 deletions

View file

@ -354,3 +354,21 @@ Result BatteryDriver::GetBatteryPercentage(size_t *out) {
} }
return ResultSuccess; return ResultSuccess;
} }
Result BatteryDriver::SetShutdownTimer() {
return this->Write(Max17050ShdnTimer, 0xE000);
}
Result BatteryDriver::GetShutdownEnabled(bool *out) {
u16 val = 0;
Result rc = this->Read(Max17050Config, &val);
if (R_FAILED(rc)) {
return rc;
}
*out = (val & 0x0040) != 0;
return ResultSuccess;
}
Result BatteryDriver::SetShutdownEnabled(bool enabled) {
return this->ReadWrite(Max17050Config, 0x0040, enabled ? 0x0040 : 0x0000);
}

View file

@ -58,4 +58,7 @@ class BatteryDriver {
Result GetAverageVCell(u32 *out); Result GetAverageVCell(u32 *out);
Result GetSocRep(double *out); Result GetSocRep(double *out);
Result GetBatteryPercentage(size_t *out); Result GetBatteryPercentage(size_t *out);
Result SetShutdownTimer();
Result GetShutdownEnabled(bool *out);
Result SetShutdownEnabled(bool enabled);
}; };

View file

@ -267,7 +267,7 @@ void Boot::CheckBatteryCharge() {
pmic_driver.ShutdownSystem(); pmic_driver.ShutdownSystem();
break; break;
case CheckBatteryResult_Reboot: case CheckBatteryResult_Reboot:
pmic_driver.RebootSystem(); Boot::RebootSystem();
break; break;
default: default:
std::abort(); std::abort();

View file

@ -25,7 +25,6 @@
#include "boot_functions.hpp" #include "boot_functions.hpp"
#include "boot_reboot_manager.hpp" #include "boot_reboot_manager.hpp"
#include "i2c_driver/i2c_api.hpp"
extern "C" { extern "C" {
extern u32 __start__; extern u32 __start__;

View file

@ -68,6 +68,65 @@ Result PmicDriver::GetPowerButtonPressed(bool *out) {
} }
Result PmicDriver::ShutdownSystem(bool reboot) { Result PmicDriver::ShutdownSystem(bool reboot) {
/* TODO: Implement this. */ const u8 on_off_1_addr = 0x41;
const u8 on_off_2_addr = 0x42;
/* Get value, set or clear software reset mask. */
u8 on_off_2_val = 0;
if (R_FAILED(Boot::ReadI2cRegister(this->i2c_session, &on_off_2_val, sizeof(on_off_2_val), &on_off_2_addr, sizeof(on_off_2_addr)))) {
std::abort();
}
if (reboot) {
on_off_2_val |= 0x80;
} else {
on_off_2_val &= ~0x80;
}
if (R_FAILED(Boot::WriteI2cRegister(this->i2c_session, &on_off_2_val, sizeof(on_off_2_val), &on_off_2_addr, sizeof(on_off_2_addr)))) {
std::abort();
}
/* Get value, set software reset mask. */
u8 on_off_1_val = 0;
if (R_FAILED(Boot::ReadI2cRegister(this->i2c_session, &on_off_1_val, sizeof(on_off_1_val), &on_off_1_addr, sizeof(on_off_1_addr)))) {
std::abort();
}
on_off_1_val |= 0x80;
/* Finalize the battery. */
{
BatteryDriver battery_driver;
this->FinalizeBattery(&battery_driver);
}
/* Actually write the value to trigger shutdown/reset. */
if (R_FAILED(Boot::WriteI2cRegister(this->i2c_session, &on_off_1_val, sizeof(on_off_1_val), &on_off_1_addr, sizeof(on_off_1_addr)))) {
std::abort();
}
/* Allow up to 5 seconds for shutdown/reboot to take place. */
svcSleepThread(5'000'000'000ul);
std::abort(); std::abort();
} }
void PmicDriver::FinalizeBattery(BatteryDriver *battery_driver) {
/* Set shutdown timer. */
battery_driver->SetShutdownTimer();
/* Get whether shutdown is enabled. */
bool shutdown_enabled;
if (R_FAILED(battery_driver->GetShutdownEnabled(&shutdown_enabled))) {
return;
}
bool ac_ok;
bool desired_shutdown_enabled;
if (R_FAILED(this->GetAcOk(&ac_ok)) || ac_ok) {
desired_shutdown_enabled = false;
} else {
desired_shutdown_enabled = true;
}
if (shutdown_enabled != desired_shutdown_enabled) {
battery_driver->SetShutdownEnabled(desired_shutdown_enabled);
}
}

View file

@ -19,6 +19,7 @@
#include <stratosphere.hpp> #include <stratosphere.hpp>
#include "i2c_driver/i2c_api.hpp" #include "i2c_driver/i2c_api.hpp"
#include "boot_battery_driver.hpp"
class PmicDriver { class PmicDriver {
private: private:
@ -36,6 +37,7 @@ class PmicDriver {
private: private:
Result GetPowerStatus(u8 *out); Result GetPowerStatus(u8 *out);
Result ShutdownSystem(bool reboot); Result ShutdownSystem(bool reboot);
void FinalizeBattery(BatteryDriver *battery_driver);
public: public:
void ShutdownSystem(); void ShutdownSystem();
void RebootSystem(); void RebootSystem();