2019-05-03 03:33:12 +01:00
|
|
|
/*
|
2021-10-04 20:59:10 +01:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2019-05-03 03:33:12 +01:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
2020-05-11 23:02:10 +01:00
|
|
|
#include <stratosphere.hpp>
|
2019-06-22 08:10:21 +01:00
|
|
|
#include "boot_i2c_utils.hpp"
|
2019-05-03 03:33:12 +01:00
|
|
|
#include "boot_pmic_driver.hpp"
|
|
|
|
|
2019-10-24 10:30:10 +01:00
|
|
|
namespace ams::boot {
|
2019-05-03 03:33:12 +01:00
|
|
|
|
2019-06-22 08:10:21 +01:00
|
|
|
void PmicDriver::ShutdownSystem() {
|
2020-11-08 12:16:50 +00:00
|
|
|
this->ShutdownSystem(false);
|
2019-05-03 03:33:12 +01:00
|
|
|
}
|
|
|
|
|
2019-06-22 08:10:21 +01:00
|
|
|
void PmicDriver::RebootSystem() {
|
2020-11-08 12:16:50 +00:00
|
|
|
this->ShutdownSystem(true);
|
2019-05-09 10:45:31 +01:00
|
|
|
}
|
|
|
|
|
2019-06-22 08:10:21 +01:00
|
|
|
Result PmicDriver::GetAcOk(bool *out) {
|
|
|
|
u8 power_status;
|
2020-11-08 12:16:50 +00:00
|
|
|
R_TRY(this->GetPowerStatus(std::addressof(power_status)));
|
2019-06-22 08:10:21 +01:00
|
|
|
*out = (power_status & 0x02) != 0;
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
2019-05-09 10:45:31 +01:00
|
|
|
}
|
|
|
|
|
2020-11-08 12:16:50 +00:00
|
|
|
Result PmicDriver::GetOnOffIrq(u8 *out) {
|
2019-06-22 08:10:21 +01:00
|
|
|
const u8 addr = 0x0B;
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(ReadI2cRegister(m_i2c_session, out, sizeof(*out), std::addressof(addr), sizeof(addr)));
|
2019-05-09 10:45:31 +01:00
|
|
|
}
|
|
|
|
|
2019-06-22 08:10:21 +01:00
|
|
|
Result PmicDriver::GetPowerStatus(u8 *out) {
|
|
|
|
const u8 addr = 0x15;
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(ReadI2cRegister(m_i2c_session, out, sizeof(*out), std::addressof(addr), sizeof(addr)));
|
2019-05-09 10:45:31 +01:00
|
|
|
}
|
|
|
|
|
2019-06-22 08:10:21 +01:00
|
|
|
Result PmicDriver::GetNvErc(u8 *out) {
|
|
|
|
const u8 addr = 0x0C;
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(ReadI2cRegister(m_i2c_session, out, sizeof(*out), std::addressof(addr), sizeof(addr)));
|
2019-06-22 08:10:21 +01:00
|
|
|
}
|
2019-05-09 10:45:31 +01:00
|
|
|
|
2019-06-22 08:10:21 +01:00
|
|
|
Result PmicDriver::GetPowerButtonPressed(bool *out) {
|
2020-11-08 12:16:50 +00:00
|
|
|
u8 on_off_irq;
|
|
|
|
R_TRY(this->GetOnOffIrq(std::addressof(on_off_irq)));
|
|
|
|
*out = (on_off_irq & 0x08) != 0;
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
2019-05-09 10:45:31 +01:00
|
|
|
}
|
|
|
|
|
2020-11-08 12:16:50 +00:00
|
|
|
void PmicDriver::ShutdownSystem(bool reboot) {
|
2019-06-22 08:10:21 +01:00
|
|
|
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;
|
2021-10-10 08:14:06 +01:00
|
|
|
R_ABORT_UNLESS(ReadI2cRegister(m_i2c_session, std::addressof(on_off_2_val), sizeof(on_off_2_val), std::addressof(on_off_2_addr), sizeof(on_off_2_addr)));
|
2019-06-22 08:10:21 +01:00
|
|
|
if (reboot) {
|
|
|
|
on_off_2_val |= 0x80;
|
|
|
|
} else {
|
|
|
|
on_off_2_val &= ~0x80;
|
|
|
|
}
|
2021-10-10 08:14:06 +01:00
|
|
|
R_ABORT_UNLESS(WriteI2cRegister(m_i2c_session, std::addressof(on_off_2_val), sizeof(on_off_2_val), std::addressof(on_off_2_addr), sizeof(on_off_2_addr)));
|
2019-06-22 08:10:21 +01:00
|
|
|
|
|
|
|
/* Get value, set software reset mask. */
|
|
|
|
u8 on_off_1_val = 0;
|
2021-10-10 08:14:06 +01:00
|
|
|
R_ABORT_UNLESS(ReadI2cRegister(m_i2c_session, std::addressof(on_off_1_val), sizeof(on_off_1_val), std::addressof(on_off_1_addr), sizeof(on_off_1_addr)));
|
2019-06-22 08:10:21 +01:00
|
|
|
on_off_1_val |= 0x80;
|
|
|
|
|
2020-02-24 16:21:31 +00:00
|
|
|
/* Finalize the battery on non-Calcio. */
|
|
|
|
if (spl::GetHardwareType() != spl::HardwareType::Calcio) {
|
2019-06-22 08:10:21 +01:00
|
|
|
BatteryDriver battery_driver;
|
2020-11-08 12:16:50 +00:00
|
|
|
this->FinalizeBattery(battery_driver);
|
2019-06-22 08:10:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Actually write the value to trigger shutdown/reset. */
|
2021-10-10 08:14:06 +01:00
|
|
|
R_ABORT_UNLESS(WriteI2cRegister(m_i2c_session, std::addressof(on_off_1_val), sizeof(on_off_1_val), std::addressof(on_off_1_addr), sizeof(on_off_1_addr)));
|
2019-06-22 08:10:21 +01:00
|
|
|
|
|
|
|
/* Allow up to 5 seconds for shutdown/reboot to take place. */
|
2020-11-08 12:16:50 +00:00
|
|
|
os::SleepThread(TimeSpan::FromSeconds(5));
|
|
|
|
AMS_ABORT("Shutdown failed");
|
2019-05-09 10:45:31 +01:00
|
|
|
}
|
|
|
|
|
2020-11-08 12:16:50 +00:00
|
|
|
void PmicDriver::FinalizeBattery(BatteryDriver &battery_driver) {
|
2019-06-22 08:10:21 +01:00
|
|
|
/* Get whether shutdown is enabled. */
|
|
|
|
bool shutdown_enabled;
|
2020-11-08 12:16:50 +00:00
|
|
|
if (R_FAILED(battery_driver.IsI2cShutdownEnabled(std::addressof(shutdown_enabled)))) {
|
2019-06-22 08:10:21 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-24 16:21:31 +00:00
|
|
|
/* On Hoag, we don't want to use the desired shutdown value when battery charged. */
|
|
|
|
bool use_desired_shutdown = true;
|
|
|
|
if (spl::GetHardwareType() == spl::HardwareType::Hoag) {
|
2020-11-08 12:16:50 +00:00
|
|
|
float battery_charge_raw;
|
2021-10-16 00:30:27 +01:00
|
|
|
if (R_FAILED(battery_driver.GetChargePercentage(std::addressof(battery_charge_raw))) || battery_charge_raw >= 80.0) {
|
2020-02-24 16:21:31 +00:00
|
|
|
use_desired_shutdown = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-22 08:10:21 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-02-24 16:21:31 +00:00
|
|
|
desired_shutdown_enabled &= use_desired_shutdown;
|
|
|
|
|
2019-06-22 08:10:21 +01:00
|
|
|
if (shutdown_enabled != desired_shutdown_enabled) {
|
2020-11-08 12:16:50 +00:00
|
|
|
battery_driver.SetI2cShutdownEnabled(desired_shutdown_enabled);
|
2019-06-22 08:10:21 +01:00
|
|
|
}
|
2019-05-09 10:45:31 +01:00
|
|
|
}
|
2019-06-22 08:10:21 +01:00
|
|
|
|
2019-05-09 10:45:31 +01:00
|
|
|
}
|