sys-clk: deprecate
This commit is contained in:
@@ -1,36 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <utility>
|
||||
|
||||
template<typename F>
|
||||
class ScopeGuard {
|
||||
public:
|
||||
ScopeGuard(F&& f)
|
||||
: f(f), engaged(true) {};
|
||||
|
||||
~ScopeGuard() {
|
||||
if (engaged)
|
||||
f();
|
||||
};
|
||||
|
||||
ScopeGuard(ScopeGuard&& rhs)
|
||||
: f(std::move(rhs.f)) {};
|
||||
|
||||
void dismiss() { engaged = false; }
|
||||
|
||||
private:
|
||||
F f;
|
||||
bool engaged;
|
||||
};
|
||||
|
||||
struct MakeScopeExit {
|
||||
template<typename F>
|
||||
ScopeGuard<F> operator+=(F&& f) {
|
||||
return ScopeGuard<F>(std::move(f));
|
||||
};
|
||||
};
|
||||
|
||||
#define STRING_CAT2(x, y) x##y
|
||||
#define STRING_CAT(x, y) STRING_CAT2(x, y)
|
||||
#define SCOPE_GUARD MakeScopeExit() += [&]() __attribute__((always_inline))
|
||||
#define SCOPE_EXIT auto STRING_CAT(scope_exit_, __LINE__) = SCOPE_GUARD
|
||||
@@ -1,28 +0,0 @@
|
||||
/*
|
||||
* --------------------------------------------------------------------------
|
||||
* "THE BEER-WARE LICENSE" (Revision 42):
|
||||
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
|
||||
* wrote this file. As long as you retain this notice you can do whatever you
|
||||
* want with this stuff. If you meet any of us some day, and you think this
|
||||
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
|
||||
* --------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
#include "cpp_util.hpp"
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "sysclk/ipc.h"
|
||||
#include "sysclk/clocks.h"
|
||||
#include "sysclk/apm.h"
|
||||
#include "sysclk/config.h"
|
||||
#include "sysclk/errors.h"
|
||||
#include "sysclk/i2c.h"
|
||||
#include "sysclk/psm_ext.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -1,22 +0,0 @@
|
||||
/*
|
||||
* --------------------------------------------------------------------------
|
||||
* "THE BEER-WARE LICENSE" (Revision 42):
|
||||
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
|
||||
* wrote this file. As long as you retain this notice you can do whatever you
|
||||
* want with this stuff. If you meet any of us some day, and you think this
|
||||
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
|
||||
* --------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "clocks.h"
|
||||
|
||||
typedef struct {
|
||||
uint32_t id;
|
||||
uint32_t cpu_hz;
|
||||
uint32_t gpu_hz;
|
||||
uint32_t mem_hz;
|
||||
} SysClkApmConfiguration;
|
||||
|
||||
extern SysClkApmConfiguration sysclk_g_apm_configurations[];
|
||||
@@ -1,50 +0,0 @@
|
||||
/*
|
||||
* --------------------------------------------------------------------------
|
||||
* "THE BEER-WARE LICENSE" (Revision 42):
|
||||
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
|
||||
* wrote this file. As long as you retain this notice you can do whatever you
|
||||
* want with this stuff. If you meet any of us some day, and you think this
|
||||
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
|
||||
* --------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "types.h"
|
||||
#include "../config.h"
|
||||
#include "../clocks.h"
|
||||
#include "../ipc.h"
|
||||
|
||||
bool sysclkIpcRunning();
|
||||
Result sysclkIpcInitialize(void);
|
||||
void sysclkIpcExit(void);
|
||||
|
||||
Result sysclkIpcGetAPIVersion(u32* out_ver);
|
||||
Result sysclkIpcGetVersionString(char* out, size_t len);
|
||||
Result sysclkIpcGetCurrentContext(SysClkContext* out_context);
|
||||
Result sysclkIpcGetProfileCount(u64 tid, u8* out_count);
|
||||
Result sysclkIpcSetEnabled(bool enabled);
|
||||
Result sysclkIpcExitCmd();
|
||||
Result sysclkIpcSetOverride(SysClkModule module, u32 hz);
|
||||
Result sysclkIpcGetProfiles(u64 tid, SysClkTitleProfileList* out_profiles);
|
||||
Result sysclkIpcSetProfiles(u64 tid, SysClkTitleProfileList* profiles);
|
||||
Result sysclkIpcGetConfigValues(SysClkConfigValueList* out_configValues);
|
||||
Result sysclkIpcSetConfigValues(SysClkConfigValueList* configValues);
|
||||
Result sysclkIpcSetReverseNXRTMode(ReverseNXMode mode);
|
||||
Result sysclkIpcGetFrequencyTable(SysClkModule module, SysClkProfile profile, SysClkFrequencyTable* out_table);
|
||||
Result sysclkIpcGetIsMariko(bool* out_is_mariko);
|
||||
Result sysclkIpcGetBatteryChargingDisabledOverride(bool* out_is_true);
|
||||
Result sysclkIpcSetBatteryChargingDisabledOverride(bool toggle_true);
|
||||
|
||||
static inline Result sysclkIpcRemoveOverride(SysClkModule module)
|
||||
{
|
||||
return sysclkIpcSetOverride(module, 0);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -1,29 +0,0 @@
|
||||
/*
|
||||
* --------------------------------------------------------------------------
|
||||
* "THE BEER-WARE LICENSE" (Revision 42):
|
||||
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
|
||||
* wrote this file. As long as you retain this notice you can do whatever you
|
||||
* want with this stuff. If you meet any of us some day, and you think this
|
||||
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
|
||||
* --------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef __SWITCH__
|
||||
|
||||
#include <switch/types.h>
|
||||
#include <switch/result.h>
|
||||
|
||||
#else
|
||||
|
||||
#define R_FAILED(res) ((res) != 0)
|
||||
#define R_SUCCEEDED(res) ((res) == 0)
|
||||
|
||||
typedef std::uint32_t Result;
|
||||
typedef std::uint32_t u32;
|
||||
typedef std::int32_t s32;
|
||||
typedef std::uint64_t u64;
|
||||
typedef std::uint8_t u8;
|
||||
|
||||
#endif
|
||||
@@ -1,183 +0,0 @@
|
||||
/*
|
||||
* --------------------------------------------------------------------------
|
||||
* "THE BEER-WARE LICENSE" (Revision 42):
|
||||
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
|
||||
* wrote this file. As long as you retain this notice you can do whatever you
|
||||
* want with this stuff. If you meet any of us some day, and you think this
|
||||
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
|
||||
* --------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef enum
|
||||
{
|
||||
SysClkProfile_Handheld = 0,
|
||||
SysClkProfile_HandheldCharging,
|
||||
SysClkProfile_HandheldChargingUSB,
|
||||
SysClkProfile_HandheldChargingOfficial,
|
||||
SysClkProfile_Docked,
|
||||
SysClkProfile_EnumMax
|
||||
} SysClkProfile;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
SysClkModule_CPU = 0,
|
||||
SysClkModule_GPU,
|
||||
SysClkModule_MEM,
|
||||
SysClkModule_EnumMax
|
||||
} SysClkModule;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
SysClkThermalSensor_SOC = 0,
|
||||
SysClkThermalSensor_PCB,
|
||||
SysClkThermalSensor_Skin,
|
||||
SysClkThermalSensor_EnumMax
|
||||
} SysClkThermalSensor;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t enabled;
|
||||
uint64_t applicationId;
|
||||
SysClkProfile profile;
|
||||
uint32_t freqs[SysClkModule_EnumMax];
|
||||
uint32_t overrideFreqs[SysClkModule_EnumMax];
|
||||
uint32_t temps[SysClkThermalSensor_EnumMax];
|
||||
uint32_t perfConfId;
|
||||
} SysClkContext;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
ReverseNX_NotFound = 0,
|
||||
ReverseNX_SystemDefault = 0,
|
||||
ReverseNX_Handheld,
|
||||
ReverseNX_Docked,
|
||||
} ReverseNXMode;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
bool systemCoreBoostCPU;
|
||||
bool batteryChargingDisabledOverride;
|
||||
SysClkProfile realProfile;
|
||||
} SysClkOcExtra;
|
||||
|
||||
#define FREQ_TABLE_MAX_ENTRY_COUNT 31
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint32_t freq[FREQ_TABLE_MAX_ENTRY_COUNT];
|
||||
} SysClkFrequencyTable;
|
||||
|
||||
typedef enum {
|
||||
SysClkOcGovernorConfig_AllDisabled = 0,
|
||||
SysClkOcGovernorConfig_CPU_Shift = 0,
|
||||
SysClkOcGovernorConfig_CPUOnly = 1 << SysClkOcGovernorConfig_CPU_Shift,
|
||||
SysClkOcGovernorConfig_CPU = 1 << SysClkOcGovernorConfig_CPU_Shift,
|
||||
SysClkOcGovernorConfig_GPU_Shift = 1,
|
||||
SysClkOcGovernorConfig_GPUOnly = 1 << SysClkOcGovernorConfig_GPU_Shift,
|
||||
SysClkOcGovernorConfig_GPU = 1 << SysClkOcGovernorConfig_GPU_Shift,
|
||||
SysClkOcGovernorConfig_AllEnabled = 3,
|
||||
SysClkOcGovernorConfig_Default = 3,
|
||||
SysClkOcGovernorConfig_Mask = 3,
|
||||
} SysClkOcGovernorConfig;
|
||||
|
||||
inline bool GetGovernorEnabled(SysClkOcGovernorConfig config, SysClkModule module) {
|
||||
switch (module) {
|
||||
case SysClkModule_CPU:
|
||||
return (config >> SysClkOcGovernorConfig_CPU_Shift) & 1;
|
||||
case SysClkModule_GPU:
|
||||
return (config >> SysClkOcGovernorConfig_GPU_Shift) & 1;
|
||||
case SysClkModule_MEM:
|
||||
return false;
|
||||
default:
|
||||
return config != SysClkOcGovernorConfig_AllDisabled;
|
||||
}
|
||||
}
|
||||
|
||||
inline SysClkOcGovernorConfig ToggleGovernor(SysClkOcGovernorConfig prev, SysClkModule module, bool state) {
|
||||
uint8_t shift;
|
||||
switch (module) {
|
||||
case SysClkModule_CPU:
|
||||
shift = SysClkOcGovernorConfig_CPU_Shift;
|
||||
break;
|
||||
case SysClkModule_GPU:
|
||||
shift = SysClkOcGovernorConfig_GPU_Shift;
|
||||
break;
|
||||
case SysClkModule_MEM:
|
||||
return prev;
|
||||
default:
|
||||
return state ? SysClkOcGovernorConfig_AllEnabled : SysClkOcGovernorConfig_AllDisabled;
|
||||
}
|
||||
return (SysClkOcGovernorConfig)((prev & ~(1 << shift)) | state << shift);
|
||||
}
|
||||
|
||||
typedef struct
|
||||
{
|
||||
union {
|
||||
uint32_t mhz[(size_t)SysClkProfile_EnumMax * (size_t)SysClkModule_EnumMax];
|
||||
uint32_t mhzMap[SysClkProfile_EnumMax][SysClkModule_EnumMax];
|
||||
};
|
||||
SysClkOcGovernorConfig governorConfig;
|
||||
} SysClkTitleProfileList;
|
||||
|
||||
#define SYSCLK_GLOBAL_PROFILE_TID 0xA111111111111111
|
||||
|
||||
extern uint32_t g_freq_table_mem_hz[];
|
||||
extern uint32_t g_freq_table_cpu_hz[];
|
||||
extern uint32_t g_freq_table_gpu_hz[];
|
||||
|
||||
#define SYSCLK_ENUM_VALID(n, v) ((v) < n##_EnumMax)
|
||||
|
||||
static inline const char* sysclkFormatModule(SysClkModule module, bool pretty)
|
||||
{
|
||||
switch(module)
|
||||
{
|
||||
case SysClkModule_CPU:
|
||||
return pretty ? "CPU" : "cpu";
|
||||
case SysClkModule_GPU:
|
||||
return pretty ? "GPU" : "gpu";
|
||||
case SysClkModule_MEM:
|
||||
return pretty ? "Memory" : "mem";
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static inline const char* sysclkFormatThermalSensor(SysClkThermalSensor thermSensor, bool pretty)
|
||||
{
|
||||
switch(thermSensor)
|
||||
{
|
||||
case SysClkThermalSensor_SOC:
|
||||
return pretty ? "SOC" : "soc";
|
||||
case SysClkThermalSensor_PCB:
|
||||
return pretty ? "PCB" : "pcb";
|
||||
case SysClkThermalSensor_Skin:
|
||||
return pretty ? "Skin" : "skin";
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static inline const char* sysclkFormatProfile(SysClkProfile profile, bool pretty)
|
||||
{
|
||||
switch(profile)
|
||||
{
|
||||
case SysClkProfile_Docked:
|
||||
return pretty ? "Docked" : "docked";
|
||||
case SysClkProfile_Handheld:
|
||||
return pretty ? "Handheld" : "handheld";
|
||||
case SysClkProfile_HandheldCharging:
|
||||
return pretty ? "Charging" : "handheld_charging";
|
||||
case SysClkProfile_HandheldChargingUSB:
|
||||
return pretty ? "USB Charger" : "handheld_charging_usb";
|
||||
case SysClkProfile_HandheldChargingOfficial:
|
||||
return pretty ? "Official Charger" : "handheld_charging_official";
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
@@ -1,111 +0,0 @@
|
||||
/*
|
||||
* --------------------------------------------------------------------------
|
||||
* "THE BEER-WARE LICENSE" (Revision 42):
|
||||
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
|
||||
* wrote this file. As long as you retain this notice you can do whatever you
|
||||
* want with this stuff. If you meet any of us some day, and you think this
|
||||
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
|
||||
* --------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
const uint32_t CHARGING_CURRENT_MA_LIMIT = 2000;
|
||||
|
||||
typedef enum {
|
||||
SysClkConfigValue_PollingIntervalMs = 0,
|
||||
SysClkConfigValue_TempLogIntervalMs,
|
||||
SysClkConfigValue_CsvWriteIntervalMs,
|
||||
SysClkConfigValue_AutoCPUBoost,
|
||||
SysClkConfigValue_SyncReverseNXMode,
|
||||
SysClkConfigValue_AllowUnsafeFrequencies,
|
||||
SysClkConfigValue_ChargingCurrentLimit,
|
||||
SysClkConfigValue_ChargingLimitPercentage,
|
||||
SysClkConfigValue_GovernorExperimental,
|
||||
SysClkConfigValue_GovernorHandheldOnly,
|
||||
SysClkConfigValue_EnumMax,
|
||||
} SysClkConfigValue;
|
||||
|
||||
typedef struct {
|
||||
uint64_t values[SysClkConfigValue_EnumMax];
|
||||
} SysClkConfigValueList;
|
||||
|
||||
static inline const char* sysclkFormatConfigValue(SysClkConfigValue val, bool pretty)
|
||||
{
|
||||
switch(val)
|
||||
{
|
||||
case SysClkConfigValue_PollingIntervalMs:
|
||||
return pretty ? "Polling Interval (ms)" : "poll_interval_ms";
|
||||
case SysClkConfigValue_TempLogIntervalMs:
|
||||
return pretty ? "Temperature logging interval (ms)" : "temp_log_interval_ms";
|
||||
case SysClkConfigValue_CsvWriteIntervalMs:
|
||||
return pretty ? "CSV write interval (ms)" : "csv_write_interval_ms";
|
||||
case SysClkConfigValue_AutoCPUBoost:
|
||||
return pretty ? "Auto CPU Boost" : "auto_cpu_boost";
|
||||
case SysClkConfigValue_SyncReverseNXMode:
|
||||
return pretty ? "Sync ReverseNX Mode Sync" : "sync_reversenx_mode";
|
||||
case SysClkConfigValue_AllowUnsafeFrequencies:
|
||||
return pretty ? "Allow Unsafe Frequencies" : "allow_unsafe_freq";
|
||||
case SysClkConfigValue_ChargingCurrentLimit:
|
||||
return pretty ? "Charging Current Limit (mA)" : "charging_current";
|
||||
case SysClkConfigValue_ChargingLimitPercentage:
|
||||
return pretty ? "Charging Limit (%%)" : "charging_limit_perc";
|
||||
case SysClkConfigValue_GovernorExperimental:
|
||||
return pretty ? "Frequency Governor (Experimental)" : "governor_experimental";
|
||||
case SysClkConfigValue_GovernorHandheldOnly:
|
||||
return pretty ? "Frequency Governor Handheld Only" : "governor_handheld_only";
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static inline uint64_t sysclkDefaultConfigValue(SysClkConfigValue val)
|
||||
{
|
||||
switch(val)
|
||||
{
|
||||
case SysClkConfigValue_PollingIntervalMs:
|
||||
return 500ULL;
|
||||
case SysClkConfigValue_TempLogIntervalMs:
|
||||
case SysClkConfigValue_CsvWriteIntervalMs:
|
||||
case SysClkConfigValue_AllowUnsafeFrequencies:
|
||||
case SysClkConfigValue_GovernorExperimental:
|
||||
case SysClkConfigValue_GovernorHandheldOnly:
|
||||
case SysClkConfigValue_AutoCPUBoost:
|
||||
return 0ULL;
|
||||
case SysClkConfigValue_SyncReverseNXMode:
|
||||
return 1ULL;
|
||||
case SysClkConfigValue_ChargingCurrentLimit:
|
||||
return 2000ULL;
|
||||
case SysClkConfigValue_ChargingLimitPercentage:
|
||||
return 100ULL;
|
||||
default:
|
||||
return 0ULL;
|
||||
}
|
||||
}
|
||||
|
||||
static inline uint64_t sysclkValidConfigValue(SysClkConfigValue val, uint64_t input)
|
||||
{
|
||||
switch(val)
|
||||
{
|
||||
case SysClkConfigValue_PollingIntervalMs:
|
||||
return input > 0;
|
||||
case SysClkConfigValue_TempLogIntervalMs:
|
||||
case SysClkConfigValue_CsvWriteIntervalMs:
|
||||
return true;
|
||||
case SysClkConfigValue_AutoCPUBoost:
|
||||
case SysClkConfigValue_SyncReverseNXMode:
|
||||
case SysClkConfigValue_AllowUnsafeFrequencies:
|
||||
case SysClkConfigValue_GovernorExperimental:
|
||||
case SysClkConfigValue_GovernorHandheldOnly:
|
||||
return (input & 0x1) == input;
|
||||
case SysClkConfigValue_ChargingCurrentLimit:
|
||||
return (input >= 100 && input <= CHARGING_CURRENT_MA_LIMIT && input % 100 == 0);
|
||||
case SysClkConfigValue_ChargingLimitPercentage:
|
||||
return (input <= 100 && input >= 20);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
/*
|
||||
* --------------------------------------------------------------------------
|
||||
* "THE BEER-WARE LICENSE" (Revision 42):
|
||||
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
|
||||
* wrote this file. As long as you retain this notice you can do whatever you
|
||||
* want with this stuff. If you meet any of us some day, and you think this
|
||||
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
|
||||
* --------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#define SYSCLK_ERROR_MODULE 388
|
||||
#define SYSCLK_ERROR(desc) ((SYSCLK_ERROR_MODULE & 0x1FF) | (SysClkError_##desc & 0x1FFF)<<9)
|
||||
|
||||
typedef enum
|
||||
{
|
||||
SysClkError_Generic = 0,
|
||||
SysClkError_ConfigNotLoaded = 1,
|
||||
SysClkError_ConfigSaveFailed = 2,
|
||||
SysClkError_InternalFrequencyTableError = 3,
|
||||
} SysClkError;
|
||||
@@ -1,57 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <switch.h>
|
||||
|
||||
// To use i2c service, sm and i2c should be intialized via smInitialize() and i2cInitialize().
|
||||
|
||||
Result I2cSet_U8(I2cDevice dev, u8 reg, u8 val);
|
||||
|
||||
Result I2cRead_OutU8(I2cDevice dev, u8 reg, u8 *out);
|
||||
Result I2cRead_OutU16(I2cDevice dev, u8 reg, u16 *out);
|
||||
|
||||
// Max17050 fuel gauge
|
||||
float I2c_Max17050_GetBatteryCurrent();
|
||||
|
||||
const u8 MAX17050_CURRENT_REG = 0x0A;
|
||||
|
||||
// Buck Converter
|
||||
typedef enum I2c_BuckConverter_Reg {
|
||||
I2c_Max77620_SD1VOLT_REG = 0x17, // Used for Erista DDR VDDQ+VDD2 / Mariko VDD2
|
||||
I2c_Max77621_VOLT_REG = 0x00,
|
||||
I2c_Max77812_CPUVOLT_REG = 0x26,
|
||||
I2c_Max77812_GPUVOLT_REG = 0x23,
|
||||
I2c_Max77812_MEMVOLT_REG = 0x25, // Master 3 (GPU 1 + 2, DRAM 3, CPU 4), used for Mariko VDDQ
|
||||
} I2c_BuckConverter_Reg;
|
||||
|
||||
typedef struct I2c_BuckConverter_Domain {
|
||||
I2cDevice device;
|
||||
I2c_BuckConverter_Reg reg;
|
||||
u8 volt_mask;
|
||||
u32 uv_step;
|
||||
u32 uv_min;
|
||||
u32 uv_max;
|
||||
u8 por_val;
|
||||
} I2c_BuckConverter_Domain;
|
||||
|
||||
const I2c_BuckConverter_Domain I2c_Erista_CPU = { I2cDevice_Max77621Cpu, I2c_Max77621_VOLT_REG, 0x7F, 6250, 606250, 1400000, };
|
||||
const I2c_BuckConverter_Domain I2c_Erista_GPU = { I2cDevice_Max77621Gpu, I2c_Max77621_VOLT_REG, 0x7F, 6250, 606250, 1400000, };
|
||||
const I2c_BuckConverter_Domain I2c_Erista_DRAM = { I2cDevice_Max77620Pmic, I2c_Max77620_SD1VOLT_REG, 0x7F, 12500, 600000, 1250000, };
|
||||
const I2c_BuckConverter_Domain I2c_Mariko_CPU = { I2cDevice_Max77812_2, I2c_Max77812_CPUVOLT_REG, 0xFF, 5000, 250000, 1525000, 0x78 };
|
||||
const I2c_BuckConverter_Domain I2c_Mariko_GPU = { I2cDevice_Max77812_2, I2c_Max77812_GPUVOLT_REG, 0xFF, 5000, 250000, 1525000, 0x78 };
|
||||
const I2c_BuckConverter_Domain I2c_Mariko_DRAM_VDDQ = { I2cDevice_Max77812_2, I2c_Max77812_MEMVOLT_REG, 0xFF, 5000, 250000, 650000, 0x78 };
|
||||
const I2c_BuckConverter_Domain I2c_Mariko_DRAM_VDD2 = { I2cDevice_Max77620Pmic, I2c_Max77620_SD1VOLT_REG, 0x7F, 12500, 600000, 1250000, };
|
||||
|
||||
u32 I2c_BuckConverter_GetMvOut(const I2c_BuckConverter_Domain* domain);
|
||||
Result I2c_BuckConverter_SetMvOut(const I2c_BuckConverter_Domain* domain, u32 mvolt);
|
||||
|
||||
// Bq24193 Battery management
|
||||
u32 I2c_Bq24193_Convert_Raw_mA(u8 raw);
|
||||
u8 I2c_Bq24193_Convert_mA_Raw(u32 ma);
|
||||
|
||||
Result I2c_Bq24193_GetFastChargeCurrentLimit(u32 *ma);
|
||||
Result I2c_Bq24193_SetFastChargeCurrentLimit(u32 ma);
|
||||
|
||||
const u32 MA_RANGE_MIN = 512;
|
||||
const u32 MA_RANGE_MAX = 4544;
|
||||
|
||||
const u8 BQ24193_CHARGE_CURRENT_CONTROL_REG = 0x2;
|
||||
@@ -1,55 +0,0 @@
|
||||
/*
|
||||
* --------------------------------------------------------------------------
|
||||
* "THE BEER-WARE LICENSE" (Revision 42):
|
||||
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
|
||||
* wrote this file. As long as you retain this notice you can do whatever you
|
||||
* want with this stuff. If you meet any of us some day, and you think this
|
||||
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
|
||||
* --------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include "clocks.h"
|
||||
|
||||
#define SYSCLK_IPC_API_VERSION 2
|
||||
#define SYSCLK_IPC_SERVICE_NAME "sysclkOC"
|
||||
|
||||
enum SysClkIpcCmd
|
||||
{
|
||||
SysClkIpcCmd_GetApiVersion = 0,
|
||||
SysClkIpcCmd_GetVersionString = 1,
|
||||
SysClkIpcCmd_GetCurrentContext = 2,
|
||||
SysClkIpcCmd_Exit = 3,
|
||||
SysClkIpcCmd_GetProfileCount = 4,
|
||||
SysClkIpcCmd_GetProfiles = 5,
|
||||
SysClkIpcCmd_SetProfiles = 6,
|
||||
SysClkIpcCmd_SetEnabled = 7,
|
||||
SysClkIpcCmd_SetOverride = 8,
|
||||
SysClkIpcCmd_GetConfigValues = 9,
|
||||
SysClkIpcCmd_SetConfigValues = 10,
|
||||
SysClkIpcCmd_SetReverseNXRTMode = 11,
|
||||
SysClkIpcCmd_GetFrequencyTable = 12,
|
||||
SysClkIpcCmd_GetIsMariko = 13,
|
||||
SysClkIpcCmd_GetBatteryChargingDisabledOverride = 14,
|
||||
SysClkIpcCmd_SetBatteryChargingDisabledOverride = 15,
|
||||
};
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint64_t tid;
|
||||
SysClkTitleProfileList profiles;
|
||||
} SysClkIpc_SetProfiles_Args;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
SysClkModule module;
|
||||
uint32_t hz;
|
||||
} SysClkIpc_SetOverride_Args;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
SysClkModule module;
|
||||
SysClkProfile profile;
|
||||
} SysClkIpc_GetFrequencyTable_Args;
|
||||
@@ -1,77 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <switch.h>
|
||||
|
||||
typedef enum {
|
||||
PsmPDC_NewPDO = 1, //Received new Power Data Object
|
||||
PsmPDC_NoPD = 2, //No Power Delivery source is detected
|
||||
PsmPDC_AcceptedRDO = 3 //Received and accepted Request Data Object
|
||||
} PsmChargeInfoPDC; //BM92T series
|
||||
|
||||
typedef enum {
|
||||
PsmPowerRole_Sink = 1,
|
||||
PsmPowerRole_Source = 2
|
||||
} PsmPowerRole;
|
||||
|
||||
const char* PsmPowerRoleToStr(PsmPowerRole role);
|
||||
|
||||
typedef enum {
|
||||
PsmInfoChargerType_None = 0,
|
||||
PsmInfoChargerType_PD = 1,
|
||||
PsmInfoChargerType_TypeC_1500mA = 2,
|
||||
PsmInfoChargerType_TypeC_3000mA = 3,
|
||||
PsmInfoChargerType_DCP = 4,
|
||||
PsmInfoChargerType_CDP = 5,
|
||||
PsmInfoChargerType_SDP = 6,
|
||||
PsmInfoChargerType_Apple_500mA = 7,
|
||||
PsmInfoChargerType_Apple_1000mA = 8,
|
||||
PsmInfoChargerType_Apple_2000mA = 9
|
||||
} PsmInfoChargerType;
|
||||
|
||||
const char* PsmInfoChargerTypeToStr(PsmInfoChargerType type);
|
||||
|
||||
typedef enum {
|
||||
PsmFlags_NoHub = BIT(0), //If hub is disconnected
|
||||
PsmFlags_Rail = BIT(8), //At least one Joy-con is charging from rail
|
||||
PsmFlags_SPDSRC = BIT(12), //OTG
|
||||
PsmFlags_ACC = BIT(16) //Accessory
|
||||
} PsmChargeInfoFlags;
|
||||
|
||||
typedef struct {
|
||||
int32_t InputCurrentLimit; //Input (Sink) current limit in mA
|
||||
int32_t VBUSCurrentLimit; //Output (Source/VBUS/OTG) current limit in mA
|
||||
int32_t ChargeCurrentLimit; //Battery charging current limit in mA (512mA when Docked, 768mA when BatteryTemperature < 17.0 C)
|
||||
int32_t ChargeVoltageLimit; //Battery charging voltage limit in mV (3952mV when BatteryTemperature >= 51.0 C)
|
||||
int32_t unk_x10; //Possibly an emum, getting the same value as PowerRole in all tested cases
|
||||
int32_t unk_x14; //Possibly flags
|
||||
PsmChargeInfoPDC PDCState; //Power Delivery Controller State
|
||||
int32_t BatteryTemperature; //Battery temperature in milli C
|
||||
int32_t RawBatteryCharge; //Raw battery charged capacity per cent-mille (i.e. 100% = 100000 pcm)
|
||||
int32_t VoltageAvg; //Voltage avg in mV (more in Notes)
|
||||
int32_t BatteryAge; //Battery age (capacity full / capacity design) per cent-mille (i.e. 100% = 100000 pcm)
|
||||
PsmPowerRole PowerRole;
|
||||
PsmInfoChargerType ChargerType;
|
||||
int32_t ChargerVoltageLimit; //Charger and external device voltage limit in mV
|
||||
int32_t ChargerCurrentLimit; //Charger and external device current limit in mA
|
||||
PsmChargeInfoFlags Flags; //Unknown flags
|
||||
} PsmChargeInfo;
|
||||
|
||||
typedef enum {
|
||||
Psm_EnableBatteryCharging = 2,
|
||||
Psm_DisableBatteryCharging = 3,
|
||||
Psm_EnableFastBatteryCharging = 10,
|
||||
Psm_DisableFastBatteryCharging = 11,
|
||||
Psm_GetBatteryChargeInfoFields = 17,
|
||||
} IPsmServerCmd;
|
||||
|
||||
bool PsmIsChargerConnected(const PsmChargeInfo* info);
|
||||
bool PsmIsCharging(const PsmChargeInfo* info);
|
||||
|
||||
typedef enum {
|
||||
PsmBatteryState_Discharging,
|
||||
PsmBatteryState_ChargingPaused,
|
||||
PsmBatteryState_FastCharging
|
||||
} PsmBatteryState;
|
||||
|
||||
PsmBatteryState PsmGetBatteryState(const PsmChargeInfo* info);
|
||||
const char* PsmGetBatteryStateIcon(const PsmChargeInfo* info);
|
||||
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
* --------------------------------------------------------------------------
|
||||
* "THE BEER-WARE LICENSE" (Revision 42):
|
||||
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
|
||||
* wrote this file. As long as you retain this notice you can do whatever you
|
||||
* want with this stuff. If you meet any of us some day, and you think this
|
||||
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
|
||||
* --------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#include <sysclk/apm.h>
|
||||
|
||||
SysClkApmConfiguration sysclk_g_apm_configurations[] = {
|
||||
{0x00010000, 1020000000, 384000000, 1600000000},
|
||||
{0x00010001, 1020000000, 768000000, 1600000000},
|
||||
{0x00010002, 1224000000, 691200000, 1600000000},
|
||||
{0x00020000, 1020000000, 230400000, 1600000000},
|
||||
{0x00020001, 1020000000, 307200000, 1600000000},
|
||||
{0x00020002, 1224000000, 230400000, 1600000000},
|
||||
{0x00020003, 1020000000, 307000000, 1331200000},
|
||||
{0x00020004, 1020000000, 384000000, 1331200000},
|
||||
{0x00020005, 1020000000, 307200000, 1065600000},
|
||||
{0x00020006, 1020000000, 384000000, 1065600000},
|
||||
{0x92220007, 1020000000, 460800000, 1600000000},
|
||||
{0x92220008, 1020000000, 460800000, 1331200000},
|
||||
{0x92220009, 1785000000, 76800000, 1600000000},
|
||||
{0x9222000A, 1785000000, 76800000, 1331200000},
|
||||
{0x9222000B, 1020000000, 76800000, 1600000000},
|
||||
{0x9222000C, 1020000000, 76800000, 1331200000},
|
||||
{0, 0, 0, 0},
|
||||
};
|
||||
|
||||
@@ -1,145 +0,0 @@
|
||||
/*
|
||||
* --------------------------------------------------------------------------
|
||||
* "THE BEER-WARE LICENSE" (Revision 42):
|
||||
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
|
||||
* wrote this file. As long as you retain this notice you can do whatever you
|
||||
* want with this stuff. If you meet any of us some day, and you think this
|
||||
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
|
||||
* --------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#define NX_SERVICE_ASSUME_NON_DOMAIN
|
||||
#include <sysclk/client/ipc.h>
|
||||
#include <switch.h>
|
||||
#include <string.h>
|
||||
#include <stdatomic.h>
|
||||
|
||||
static Service g_sysclkSrv;
|
||||
static atomic_size_t g_refCnt;
|
||||
|
||||
bool sysclkIpcRunning()
|
||||
{
|
||||
Handle handle;
|
||||
bool running = R_FAILED(smRegisterService(&handle, smEncodeName(SYSCLK_IPC_SERVICE_NAME), false, 1));
|
||||
|
||||
if (!running)
|
||||
{
|
||||
smUnregisterService(smEncodeName(SYSCLK_IPC_SERVICE_NAME));
|
||||
}
|
||||
|
||||
return running;
|
||||
}
|
||||
|
||||
Result sysclkIpcInitialize(void)
|
||||
{
|
||||
Result rc = 0;
|
||||
|
||||
g_refCnt++;
|
||||
|
||||
if (serviceIsActive(&g_sysclkSrv))
|
||||
return 0;
|
||||
|
||||
rc = smGetService(&g_sysclkSrv, SYSCLK_IPC_SERVICE_NAME);
|
||||
|
||||
if (R_FAILED(rc)) sysclkIpcExit();
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
void sysclkIpcExit(void)
|
||||
{
|
||||
if (--g_refCnt == 0)
|
||||
{
|
||||
serviceClose(&g_sysclkSrv);
|
||||
}
|
||||
}
|
||||
|
||||
Result sysclkIpcGetAPIVersion(u32* out_ver)
|
||||
{
|
||||
return serviceDispatchOut(&g_sysclkSrv, SysClkIpcCmd_GetApiVersion, *out_ver);
|
||||
}
|
||||
|
||||
Result sysclkIpcGetVersionString(char* out, size_t len)
|
||||
{
|
||||
return serviceDispatch(&g_sysclkSrv, SysClkIpcCmd_GetVersionString,
|
||||
.buffer_attrs = { SfBufferAttr_HipcMapAlias | SfBufferAttr_Out },
|
||||
.buffers = {{out, len}},
|
||||
);
|
||||
}
|
||||
|
||||
Result sysclkIpcGetCurrentContext(SysClkContext* out_context)
|
||||
{
|
||||
return serviceDispatchOut(&g_sysclkSrv, SysClkIpcCmd_GetCurrentContext, *out_context);
|
||||
}
|
||||
|
||||
Result sysclkIpcGetProfileCount(u64 tid, u8* out_count)
|
||||
{
|
||||
return serviceDispatchInOut(&g_sysclkSrv, SysClkIpcCmd_GetProfileCount, tid, *out_count);
|
||||
}
|
||||
|
||||
Result sysclkIpcSetEnabled(bool enabled)
|
||||
{
|
||||
u8 enabledRaw = (u8)enabled;
|
||||
return serviceDispatchIn(&g_sysclkSrv, SysClkIpcCmd_SetEnabled, enabledRaw);
|
||||
}
|
||||
|
||||
Result sysclkIpcSetOverride(SysClkModule module, u32 hz)
|
||||
{
|
||||
SysClkIpc_SetOverride_Args args = {
|
||||
.module = module,
|
||||
.hz = hz
|
||||
};
|
||||
return serviceDispatchIn(&g_sysclkSrv, SysClkIpcCmd_SetOverride, args);
|
||||
}
|
||||
|
||||
Result sysclkIpcGetProfiles(u64 tid, SysClkTitleProfileList* out_profiles)
|
||||
{
|
||||
return serviceDispatchInOut(&g_sysclkSrv, SysClkIpcCmd_GetProfiles, tid, *out_profiles);
|
||||
}
|
||||
|
||||
Result sysclkIpcSetProfiles(u64 tid, SysClkTitleProfileList* profiles)
|
||||
{
|
||||
SysClkIpc_SetProfiles_Args args;
|
||||
args.tid = tid;
|
||||
memcpy(&args.profiles, profiles, sizeof(SysClkTitleProfileList));
|
||||
return serviceDispatchIn(&g_sysclkSrv, SysClkIpcCmd_SetProfiles, args);
|
||||
}
|
||||
|
||||
Result sysclkIpcGetConfigValues(SysClkConfigValueList* out_configValues)
|
||||
{
|
||||
return serviceDispatchOut(&g_sysclkSrv, SysClkIpcCmd_GetConfigValues, *out_configValues);
|
||||
}
|
||||
|
||||
Result sysclkIpcSetConfigValues(SysClkConfigValueList* configValues)
|
||||
{
|
||||
return serviceDispatchIn(&g_sysclkSrv, SysClkIpcCmd_SetConfigValues, *configValues);
|
||||
}
|
||||
|
||||
Result sysclkIpcSetReverseNXRTMode(ReverseNXMode mode)
|
||||
{
|
||||
return serviceDispatchIn(&g_sysclkSrv, SysClkIpcCmd_SetReverseNXRTMode, mode);
|
||||
}
|
||||
|
||||
Result sysclkIpcGetFrequencyTable(SysClkModule module, SysClkProfile profile, SysClkFrequencyTable* out_table)
|
||||
{
|
||||
SysClkIpc_GetFrequencyTable_Args args = {
|
||||
.module = module,
|
||||
.profile = profile,
|
||||
};
|
||||
return serviceDispatchInOut(&g_sysclkSrv, SysClkIpcCmd_GetFrequencyTable, args, *out_table);
|
||||
}
|
||||
|
||||
Result sysclkIpcGetIsMariko(bool* out_is_mariko)
|
||||
{
|
||||
return serviceDispatchOut(&g_sysclkSrv, SysClkIpcCmd_GetIsMariko, *out_is_mariko);
|
||||
}
|
||||
|
||||
Result sysclkIpcGetBatteryChargingDisabledOverride(bool* out_is_true)
|
||||
{
|
||||
return serviceDispatchOut(&g_sysclkSrv, SysClkIpcCmd_GetBatteryChargingDisabledOverride, *out_is_true);
|
||||
}
|
||||
|
||||
Result sysclkIpcSetBatteryChargingDisabledOverride(bool toggle_true)
|
||||
{
|
||||
return serviceDispatchIn(&g_sysclkSrv, SysClkIpcCmd_SetBatteryChargingDisabledOverride, toggle_true);
|
||||
}
|
||||
@@ -1,186 +0,0 @@
|
||||
#include <sysclk/i2c.h>
|
||||
|
||||
Result I2cSet_U8(I2cDevice dev, u8 reg, u8 val) {
|
||||
// ams::fatal::srv::StopSoundTask::StopSound()
|
||||
// I2C Bus Communication Reference: https://www.ti.com/lit/an/slva704/slva704.pdf
|
||||
struct {
|
||||
u8 reg;
|
||||
u8 val;
|
||||
} __attribute__((packed)) cmd;
|
||||
|
||||
I2cSession _session;
|
||||
Result res = i2cOpenSession(&_session, dev);
|
||||
if (res)
|
||||
return res;
|
||||
|
||||
cmd.reg = reg;
|
||||
cmd.val = val;
|
||||
res = i2csessionSendAuto(&_session, &cmd, sizeof(cmd), I2cTransactionOption_All);
|
||||
i2csessionClose(&_session);
|
||||
return res;
|
||||
}
|
||||
|
||||
Result I2cRead_OutU8(I2cDevice dev, u8 reg, u8 *out) {
|
||||
struct { u8 reg; } __attribute__((packed)) cmd;
|
||||
struct { u8 val; } __attribute__((packed)) rec;
|
||||
|
||||
I2cSession _session;
|
||||
Result res = i2cOpenSession(&_session, dev);
|
||||
if (res)
|
||||
return res;
|
||||
|
||||
cmd.reg = reg;
|
||||
res = i2csessionSendAuto(&_session, &cmd, sizeof(cmd), I2cTransactionOption_All);
|
||||
if (res) {
|
||||
i2csessionClose(&_session);
|
||||
return res;
|
||||
}
|
||||
|
||||
res = i2csessionReceiveAuto(&_session, &rec, sizeof(rec), I2cTransactionOption_All);
|
||||
i2csessionClose(&_session);
|
||||
if (res) {
|
||||
return res;
|
||||
}
|
||||
|
||||
*out = rec.val;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Result I2cRead_OutU16(I2cDevice dev, u8 reg, u16 *out) {
|
||||
struct { u8 reg; } __attribute__((packed)) cmd;
|
||||
struct { u16 val; } __attribute__((packed)) rec;
|
||||
|
||||
I2cSession _session;
|
||||
Result res = i2cOpenSession(&_session, dev);
|
||||
if (res)
|
||||
return res;
|
||||
|
||||
cmd.reg = reg;
|
||||
res = i2csessionSendAuto(&_session, &cmd, sizeof(cmd), I2cTransactionOption_All);
|
||||
if (res) {
|
||||
i2csessionClose(&_session);
|
||||
return res;
|
||||
}
|
||||
|
||||
res = i2csessionReceiveAuto(&_session, &rec, sizeof(rec), I2cTransactionOption_All);
|
||||
i2csessionClose(&_session);
|
||||
if (res) {
|
||||
return res;
|
||||
}
|
||||
|
||||
*out = rec.val;
|
||||
return 0;
|
||||
}
|
||||
|
||||
float I2c_Max17050_GetBatteryCurrent() {
|
||||
u16 val;
|
||||
Result res = I2cRead_OutU16(I2cDevice_Max17050, MAX17050_CURRENT_REG, &val);
|
||||
if (res)
|
||||
return 0.f;
|
||||
|
||||
const float SenseResistor = 5.; // in uOhm
|
||||
const float CGain = 1.99993;
|
||||
return (s16)val * (1.5625 / (SenseResistor * CGain));
|
||||
}
|
||||
|
||||
u32 I2c_BuckConverter_MultiplierToMvOut(const I2c_BuckConverter_Domain* domain, u8 multiplier) {
|
||||
return (domain->uv_min + domain->uv_step * multiplier) / 1000;
|
||||
}
|
||||
|
||||
u8 I2c_BuckConverter_MvOutToMultiplier(const I2c_BuckConverter_Domain* domain, u32 mvolt) {
|
||||
u32 uvolt = mvolt * 1000;
|
||||
if (uvolt < domain->uv_min)
|
||||
uvolt = domain->uv_min;
|
||||
if (uvolt > domain->uv_max)
|
||||
uvolt = domain->uv_max;
|
||||
|
||||
return (uvolt - domain->uv_min) / domain->uv_step;
|
||||
}
|
||||
|
||||
u32 I2c_BuckConverter_GetMvOut(const I2c_BuckConverter_Domain* domain) {
|
||||
u8 val;
|
||||
// Retry 5 times if received POR value
|
||||
for (int i = 0; i < 5; i++) {
|
||||
if (R_FAILED(I2cRead_OutU8(domain->device, domain->reg, &val)))
|
||||
return 0u;
|
||||
|
||||
// Wait 1us
|
||||
svcSleepThread(1E3);
|
||||
|
||||
if (!domain->por_val || val != domain->por_val)
|
||||
break;
|
||||
}
|
||||
return I2c_BuckConverter_MultiplierToMvOut(domain, val & domain->volt_mask);
|
||||
}
|
||||
|
||||
Result I2c_BuckConverter_SetMvOut(const I2c_BuckConverter_Domain* domain, u32 mvolt) {
|
||||
u8 val;
|
||||
Result res = I2cRead_OutU8(domain->device, domain->reg, &val);
|
||||
if (R_FAILED(res))
|
||||
return res;
|
||||
|
||||
u8 multiplier = I2c_BuckConverter_MvOutToMultiplier(domain, mvolt);
|
||||
val &= ~domain->volt_mask;
|
||||
val |= multiplier & domain->volt_mask;
|
||||
|
||||
res = I2cSet_U8(domain->device, domain->reg, val);
|
||||
if (R_FAILED(res))
|
||||
return res;
|
||||
|
||||
// 5ms Ramp delay
|
||||
svcSleepThread(5E6);
|
||||
u8 new_val;
|
||||
res = I2cRead_OutU8(domain->device, domain->reg, &new_val);
|
||||
if (R_FAILED(res))
|
||||
return res;
|
||||
if (new_val != val)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
u8 I2c_Bq24193_Convert_mA_Raw(u32 ma) {
|
||||
// Adjustment is required
|
||||
u8 raw = 0;
|
||||
|
||||
if (ma > MA_RANGE_MAX) // capping
|
||||
ma = MA_RANGE_MAX;
|
||||
|
||||
bool pct20 = ma <= (MA_RANGE_MIN - 64);
|
||||
if (pct20) {
|
||||
ma = ma * 5;
|
||||
raw |= 0x1;
|
||||
}
|
||||
|
||||
ma -= ma % 100; // round to 100
|
||||
ma -= (MA_RANGE_MIN - 64); // ceiling
|
||||
raw |= (ma >> 6) << 2;
|
||||
|
||||
return raw;
|
||||
};
|
||||
|
||||
u32 I2c_Bq24193_Convert_Raw_mA(u8 raw) {
|
||||
// No adjustment is allowed
|
||||
u32 ma = (((raw >> 2)) << 6) + MA_RANGE_MIN;
|
||||
|
||||
bool pct20 = raw & 1;
|
||||
if (pct20)
|
||||
ma = ma * 20 / 100;
|
||||
|
||||
return ma;
|
||||
};
|
||||
|
||||
Result I2c_Bq24193_GetFastChargeCurrentLimit(u32 *ma) {
|
||||
u8 raw;
|
||||
Result res = I2cRead_OutU8(I2cDevice_Bq24193, BQ24193_CHARGE_CURRENT_CONTROL_REG, &raw);
|
||||
if (res)
|
||||
return res;
|
||||
|
||||
*ma = I2c_Bq24193_Convert_Raw_mA(raw);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Result I2c_Bq24193_SetFastChargeCurrentLimit(u32 ma) {
|
||||
u8 raw = I2c_Bq24193_Convert_mA_Raw(ma);
|
||||
return I2cSet_U8(I2cDevice_Bq24193, BQ24193_CHARGE_CURRENT_CONTROL_REG, raw);
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
#include <sysclk/psm_ext.h>
|
||||
|
||||
const char* PsmPowerRoleToStr(PsmPowerRole role) {
|
||||
switch (role) {
|
||||
case PsmPowerRole_Sink: return "Sink";
|
||||
case PsmPowerRole_Source: return "Source";
|
||||
default: return "Unknown";
|
||||
}
|
||||
}
|
||||
|
||||
const char* PsmInfoChargerTypeToStr(PsmInfoChargerType type) {
|
||||
switch (type) {
|
||||
case PsmInfoChargerType_None: return "None";
|
||||
case PsmInfoChargerType_PD: return "USB-C PD";
|
||||
case PsmInfoChargerType_TypeC_1500mA:
|
||||
case PsmInfoChargerType_TypeC_3000mA: return "USB-C";
|
||||
case PsmInfoChargerType_DCP: return "USB DCP";
|
||||
case PsmInfoChargerType_CDP: return "USB CDP";
|
||||
case PsmInfoChargerType_SDP: return "USB SDP";
|
||||
case PsmInfoChargerType_Apple_500mA:
|
||||
case PsmInfoChargerType_Apple_1000mA:
|
||||
case PsmInfoChargerType_Apple_2000mA: return "Apple";
|
||||
default: return "Unknown";
|
||||
}
|
||||
}
|
||||
|
||||
bool PsmIsChargerConnected(const PsmChargeInfo* info) {
|
||||
return info->ChargerType != PsmInfoChargerType_None;
|
||||
}
|
||||
|
||||
bool PsmIsCharging(const PsmChargeInfo* info) {
|
||||
return PsmIsChargerConnected(info) && ((info->unk_x14 >> 8) & 1);
|
||||
}
|
||||
|
||||
PsmBatteryState PsmGetBatteryState(const PsmChargeInfo* info) {
|
||||
if (!PsmIsChargerConnected(info))
|
||||
return PsmBatteryState_Discharging;
|
||||
if (!PsmIsCharging(info))
|
||||
return PsmBatteryState_ChargingPaused;
|
||||
return PsmBatteryState_FastCharging;
|
||||
}
|
||||
|
||||
const char* PsmGetBatteryStateIcon(const PsmChargeInfo* info) {
|
||||
switch (PsmGetBatteryState(info)) {
|
||||
case PsmBatteryState_Discharging: return "\u25c0"; // ◀
|
||||
case PsmBatteryState_ChargingPaused:return "| |";
|
||||
case PsmBatteryState_FastCharging: return "\u25b6"; // ▶
|
||||
default: return "?";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user