Sys-clk-OC: Option to regard Power Delivery chargers supplying >=18W (typically, 9V/2A) as official chargers

This commit is contained in:
KazushiM
2021-09-15 12:23:54 +08:00
parent 584e8d8665
commit a45b767a80
6 changed files with 98 additions and 29 deletions

View File

@@ -11,9 +11,42 @@
#include <nxExt.h>
#include "clocks.h"
#include "errors.h"
#include "file_utils.h"
bool Clocks::isMariko = false;
typedef enum {
None = 0,
PD = 1,
TypeC_1500mA = 2,
TypeC_3000mA = 3,
DCP = 4,
CDP = 5,
SDP = 6,
Apple_500mA = 7,
Apple_1000mA = 8,
Apple_2000mA = 9
} BatteryChargeInfoFieldsChargerType;
typedef struct {
uint32_t InputCurrentLimit; //Input (Sink) current limit in mA
uint32_t VBUSCurrentLimit; //Output (Source/VBUS/OTG) current limit in mA
uint32_t ChargeCurrentLimit; //Battery charging current limit in mA (512mA when Docked, 768mA when BatteryTemperature < 17.0 C)
uint32_t ChargeVoltageLimit; //Battery charging voltage limit in mV (3952mV when BatteryTemperature >= 51.0 C)
uint32_t unk_x10; //Possibly an emum, getting the same value as PowerRole in all tested cases
uint32_t unk_x14; //Possibly flags
uint16_t PDControllerState; //Power Delivery Controller State
uint32_t BatteryTemperature; //Battery temperature in milli C
uint32_t RawBatteryCharge; //Raw battery charged capacity per cent-mille (i.e. 100% = 100000 pcm)
uint32_t VoltageAvg; //Voltage avg in mV (more in Notes)
uint32_t BatteryAge; //Battery age (capacity full / capacity design) per cent-mille (i.e. 100% = 100000 pcm)
uint16_t PowerRole; //Sink or Source
BatteryChargeInfoFieldsChargerType ChargerType;
uint32_t ChargerVoltageLimit; //Charger and external device voltage limit in mV
uint32_t ChargerCurrentLimit; //Charger and external device current limit in mA
uint16_t Flags; //Unknown flags
} BatteryChargeInfoFields;
void Clocks::GetList(SysClkModule module, std::uint32_t **outClocks)
{
switch(module)
@@ -193,6 +226,10 @@ void Clocks::ResetToStock(unsigned int module)
}
}
Result psmGetBatteryChargeInfoFields(Service* psmService, BatteryChargeInfoFields *out) {
return serviceDispatchOut(psmService, 17, *out);
}
SysClkProfile Clocks::GetCurrentProfile()
{
std::uint32_t mode = 0;
@@ -204,21 +241,34 @@ SysClkProfile Clocks::GetCurrentProfile()
return SysClkProfile_Docked;
}
if(FileUtils::IsPd18wAsOfficialChargerEnabled())
{
static Service* psmService = psmGetServiceSession();
static BatteryChargeInfoFields* _Fields = new BatteryChargeInfoFields;
psmGetBatteryChargeInfoFields(psmService, _Fields);
if(_Fields->ChargerType == PD)
{
uint8_t ChargerWatts = (_Fields->ChargerVoltageLimit * _Fields->ChargerCurrentLimit)/1000000.0;
if(ChargerWatts >= 17)
return SysClkProfile_HandheldChargingOfficial;
}
}
PsmChargerType chargerType;
rc = psmGetChargerType(&chargerType);
ASSERT_RESULT_OK(rc, "psmGetChargerType");
if(chargerType == PsmChargerType_EnoughPower)
switch(chargerType)
{
return SysClkProfile_HandheldChargingOfficial;
case PsmChargerType_EnoughPower:
return SysClkProfile_HandheldChargingOfficial;
case PsmChargerType_LowPower:
case PsmChargerType_NotSupported:
return SysClkProfile_HandheldChargingUSB;
default:
return SysClkProfile_Handheld;
}
else if(chargerType == PsmChargerType_LowPower || chargerType == PsmChargerType_NotSupported)
{
return SysClkProfile_HandheldChargingUSB;
}
return SysClkProfile_Handheld;
}
void Clocks::SetHz(SysClkModule module, std::uint32_t hz)

View File

@@ -21,6 +21,7 @@ static bool g_log_enabled = false;
static bool g_boost_enabled = false;
static bool g_boost_start_enabled = false;
static bool g_downclock_dock_enabled = false;
static bool g_pd18w_as_official_charger_enabled = false;
static bool g_reversenx_tool_exist = false;
static bool g_reversenx_sync_enabled = false;
static std::uint64_t g_last_flag_check = 0;
@@ -183,6 +184,13 @@ void FileUtils::InitCheckFlags()
fclose(file);
}
file = fopen(FILE_PD18W_AS_OFFICIAL_CHARGER_FLAG_PATH, "r");
if (file)
{
g_pd18w_as_official_charger_enabled = true;
fclose(file);
}
file = fopen(FILE_REVERSENX_SYNC_FLAG_PATH, "r");
if (file)
{
@@ -213,6 +221,11 @@ bool FileUtils::IsDownclockDockEnabled()
return g_downclock_dock_enabled;
}
bool FileUtils::IsPd18wAsOfficialChargerEnabled()
{
return g_pd18w_as_official_charger_enabled;
}
bool FileUtils::IsReverseNXToolExist()
{
return g_reversenx_tool_exist;

View File

@@ -25,6 +25,7 @@
#define FILE_BOOST_FLAG_PATH FILE_CONFIG_DIR "/boost.flag"
#define FILE_BOOST_START_FLAG_PATH FILE_CONFIG_DIR "/boost_start.flag"
#define FILE_DOWNCLOCK_DOCK_FLAG_PATH FILE_CONFIG_DIR "/downclock_dock.flag"
#define FILE_PD18W_AS_OFFICIAL_CHARGER_FLAG_PATH FILE_CONFIG_DIR "/pd18w_official.flag"
#define FILE_SALTYNX_PATH "/atmosphere/contents/0000000000534C56/flags/boot2.flag" // Just check for SaltyNX boot flag
#define FILE_REVERSENX_SYNC_FLAG_PATH FILE_CONFIG_DIR "/ReverseNX_sync.flag"
#define FILE_REVERSENX_RT_CONF_PATH FILE_CONFIG_DIR "/ReverseNX-RT.conf"
@@ -39,6 +40,7 @@ class FileUtils
static bool IsBoostEnabled();
static bool IsBoostStartEnabled();
static bool IsDownclockDockEnabled();
static bool IsPd18wAsOfficialChargerEnabled();
static bool IsReverseNXSyncEnabled();
static bool IsReverseNXToolExist();
static void InitializeAsync();