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