2019-05-07 00:53:29 +02:00
|
|
|
/*
|
2021-10-04 21:59:10 +02:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2019-05-07 00:53:29 +02: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/>.
|
|
|
|
*/
|
|
|
|
#pragma once
|
2020-11-08 13:16:50 +01:00
|
|
|
#include <stratosphere.hpp>
|
2019-05-07 00:53:29 +02:00
|
|
|
|
2019-10-24 11:30:10 +02:00
|
|
|
namespace ams::boot {
|
2019-05-07 00:53:29 +02:00
|
|
|
|
2020-11-08 13:16:50 +01:00
|
|
|
enum ChargerStatus {
|
|
|
|
ChargerStatus_Unknown = 0,
|
|
|
|
ChargerStatus_Charging = 1,
|
|
|
|
ChargerStatus_NotCharging = 2,
|
|
|
|
ChargerStatus_ChargeTerminationDone = 3,
|
|
|
|
};
|
|
|
|
|
2019-06-22 09:10:21 +02:00
|
|
|
class ChargerDriver {
|
|
|
|
private:
|
2021-10-10 09:14:06 +02:00
|
|
|
powctl::Session m_charger_session;
|
2019-06-22 09:10:21 +02:00
|
|
|
public:
|
2021-10-10 09:14:06 +02:00
|
|
|
ChargerDriver() : m_charger_session() {
|
|
|
|
R_ABORT_UNLESS(powctl::OpenSession(std::addressof(m_charger_session), powctl::DeviceCode_Bq24193, ddsf::AccessMode_ReadWrite));
|
2019-06-22 09:10:21 +02:00
|
|
|
}
|
2019-05-07 00:53:29 +02:00
|
|
|
|
2019-06-22 09:10:21 +02:00
|
|
|
~ChargerDriver() {
|
2021-10-10 09:14:06 +02:00
|
|
|
powctl::CloseSession(m_charger_session);
|
2019-06-22 09:10:21 +02:00
|
|
|
}
|
|
|
|
|
2020-11-08 13:16:50 +01:00
|
|
|
Result Initialize(bool set_input_current_limit) {
|
2021-05-12 17:46:41 +02:00
|
|
|
/* Configure PINMUX_AUX_CAM_FLASH_EN as tristate + passthrough. */
|
|
|
|
{
|
|
|
|
const uintptr_t apb_regs = dd::QueryIoMapping(0x70000000ul, os::MemoryPageSize);
|
|
|
|
reg::ClearBits(apb_regs + PINMUX_AUX_CAM_FLASH_EN, reg::EncodeMask(PINMUX_REG_BITS_MASK(AUX_TRISTATE)));
|
|
|
|
}
|
|
|
|
|
2020-11-08 13:16:50 +01:00
|
|
|
/* Set input current limit to 500 ma. */
|
|
|
|
if (set_input_current_limit) {
|
2021-10-10 09:14:06 +02:00
|
|
|
R_TRY(powctl::SetChargerInputCurrentLimit(m_charger_session, 500));
|
2020-11-08 13:16:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set input voltage limit to 500 mv. */
|
2021-10-10 09:14:06 +02:00
|
|
|
R_TRY(powctl::SetChargerInputVoltageLimit(m_charger_session, 500));
|
2020-11-08 13:16:50 +01:00
|
|
|
|
|
|
|
/* Disable hi-z mode. */
|
2021-10-10 09:14:06 +02:00
|
|
|
R_TRY(powctl::SetChargerHiZEnabled(m_charger_session, false));
|
2020-11-08 13:16:50 +01:00
|
|
|
|
|
|
|
/* Set configuration to charge battery. */
|
2021-10-10 09:14:06 +02:00
|
|
|
R_TRY(powctl::SetChargerChargerConfiguration(m_charger_session, powctl::ChargerConfiguration_ChargeBattery));
|
2020-11-08 13:16:50 +01:00
|
|
|
|
2022-03-26 08:14:36 +01:00
|
|
|
R_SUCCEED();
|
2020-11-08 13:16:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result GetChargeCurrentState(powctl::ChargeCurrentState *out) {
|
2022-03-26 22:48:33 +01:00
|
|
|
R_RETURN(powctl::GetChargerChargeCurrentState(out, m_charger_session));
|
2020-11-08 13:16:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result SetChargeCurrentState(powctl::ChargeCurrentState state) {
|
2022-03-26 22:48:33 +01:00
|
|
|
R_RETURN(powctl::SetChargerChargeCurrentState(m_charger_session, state));
|
2020-11-08 13:16:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result GetInputCurrentLimit(int *out) {
|
2022-03-26 22:48:33 +01:00
|
|
|
R_RETURN(powctl::GetChargerInputCurrentLimit(out, m_charger_session));
|
2020-11-08 13:16:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result SetChargerConfiguration(powctl::ChargerConfiguration cfg) {
|
2022-03-26 22:48:33 +01:00
|
|
|
R_RETURN(powctl::SetChargerChargerConfiguration(m_charger_session, cfg));
|
2020-11-08 13:16:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result GetFastChargeCurrentLimit(int *out) {
|
2022-03-26 22:48:33 +01:00
|
|
|
R_RETURN(powctl::GetChargerFastChargeCurrentLimit(out, m_charger_session));
|
2020-11-08 13:16:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result SetFastChargeCurrentLimit(int limit) {
|
2022-03-26 22:48:33 +01:00
|
|
|
R_RETURN(powctl::SetChargerFastChargeCurrentLimit(m_charger_session, limit));
|
2020-11-08 13:16:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result GetChargeVoltageLimit(int *out) {
|
2022-03-26 22:48:33 +01:00
|
|
|
R_RETURN(powctl::GetChargerChargeVoltageLimit(out, m_charger_session));
|
2020-11-08 13:16:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result SetChargeVoltageLimit(int limit) {
|
2022-03-26 22:48:33 +01:00
|
|
|
R_RETURN(powctl::SetChargerChargeVoltageLimit(m_charger_session, limit));
|
2020-11-08 13:16:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result GetChargerStatus(boot::ChargerStatus *out) {
|
|
|
|
/* Default to unknown. */
|
|
|
|
*out = ChargerStatus_Unknown;
|
|
|
|
|
|
|
|
/* Get the powctl status. */
|
|
|
|
powctl::ChargerStatus powctl_status;
|
2021-10-10 09:14:06 +02:00
|
|
|
R_TRY(powctl::GetChargerChargerStatus(std::addressof(powctl_status), m_charger_session));
|
2020-11-08 13:16:50 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-03-26 08:14:36 +01:00
|
|
|
R_SUCCEED();
|
2020-11-08 13:16:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result GetBatteryCompensation(int *out) {
|
2022-03-26 22:48:33 +01:00
|
|
|
R_RETURN(powctl::GetChargerBatteryCompensation(out, m_charger_session));
|
2020-11-08 13:16:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result SetBatteryCompensation(int v) {
|
2022-03-26 22:48:33 +01:00
|
|
|
R_RETURN(powctl::SetChargerBatteryCompensation(m_charger_session, v));
|
2020-11-08 13:16:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result GetVoltageClamp(int *out) {
|
2022-03-26 22:48:33 +01:00
|
|
|
R_RETURN(powctl::GetChargerVoltageClamp(out, m_charger_session));
|
2020-11-08 13:16:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result SetVoltageClamp(int v) {
|
2022-03-26 22:48:33 +01:00
|
|
|
R_RETURN(powctl::SetChargerVoltageClamp(m_charger_session, v));
|
2020-11-08 13:16:50 +01:00
|
|
|
}
|
2019-06-22 09:10:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|