hocclk: format code

use clang format file provided for optimal formatting
This commit is contained in:
souldbminersmwc committed 2026-06-04 19:52:11 -04:00
1 parent c6a8884c14
commit 88bfa9a4ec
118 files changed
+7111 -9615

No files matched your search

+5 -12
View File
@@ -12,9 +12,9 @@
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
*/
/* --------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
@@ -24,22 +24,15 @@
* --------------------------------------------------------------------------
*/
#include "i2c.h"
#define I2C_CMD_SND 0
#define I2C_CMD_RCV 1
Result i2csessionExtRegReceive(I2cSession* s, u8 in, void* out, u8 out_size)
{
u8 cmdlist[5] = {
I2C_CMD_SND | (I2cTransactionOption_Start << 6),
sizeof(in),
in,
Result i2csessionExtRegReceive(I2cSession *s, u8 in, void *out, u8 out_size) {
u8 cmdlist[5] = { I2C_CMD_SND | (I2cTransactionOption_Start << 6), sizeof(in), in,
I2C_CMD_RCV | (I2cTransactionOption_All << 6),
out_size
};
I2C_CMD_RCV | (I2cTransactionOption_All << 6), out_size };
return i2csessionExecuteCommandList(s, out, out_size, cmdlist, sizeof(cmdlist));
}
+4 -6
View File
@@ -12,9 +12,9 @@
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
*/
/* --------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
@@ -24,17 +24,15 @@
* --------------------------------------------------------------------------
*/
#pragma once
#ifdef __cplusplus
extern "C"
{
extern "C" {
#endif
#include <switch.h>
Result i2csessionExtRegReceive(I2cSession* s, u8 in, void* out, u8 out_size);
Result i2csessionExtRegReceive(I2cSession *s, u8 in, void *out, u8 out_size);
#ifdef __cplusplus
}
+69 -38
View File
@@ -12,7 +12,7 @@
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
*/
#include "i2cDrv.h"
@@ -37,8 +37,12 @@ Result I2cSet_U8(I2cDevice dev, u8 reg, u8 val) {
}
Result I2cRead_OutU8(I2cDevice dev, u8 reg, u8 *out) {
struct { u8 reg; } __attribute__((packed)) cmd;
struct { u8 val; } __attribute__((packed)) rec;
struct {
u8 reg;
} __attribute__((packed)) cmd;
struct {
u8 val;
} __attribute__((packed)) rec;
I2cSession _session;
Result res = i2cOpenSession(&_session, dev);
@@ -63,8 +67,12 @@ Result I2cRead_OutU8(I2cDevice dev, u8 reg, u8 *out) {
}
Result I2cRead_OutU16(I2cDevice dev, u8 reg, u16 *out) {
struct { u8 reg; } __attribute__((packed)) cmd;
struct { u16 val; } __attribute__((packed)) rec;
struct {
u8 reg;
} __attribute__((packed)) cmd;
struct {
u16 val;
} __attribute__((packed)) rec;
I2cSession _session;
Result res = i2cOpenSession(&_session, dev);
@@ -94,20 +102,20 @@ float I2c_Max17050_GetBatteryCurrent() {
if (res)
return 0.f;
const float SenseResistor = 5.; // in uOhm
const float CGain = 1.99993;
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) {
u32 I2c_BuckConverter_MultiplierToMvOut(const I2c_BuckConverter_Domain *domain, u8 multiplier) {
return (domain->uv_min + domain->uv_step * multiplier) / 1000;
}
u32 I2c_BuckConverter_MultiplierToUvOut(const I2c_BuckConverter_Domain* domain, u8 multiplier) {
u32 I2c_BuckConverter_MultiplierToUvOut(const I2c_BuckConverter_Domain *domain, u8 multiplier) {
return domain->uv_min + domain->uv_step * multiplier;
}
u8 I2c_BuckConverter_MvOutToMultiplier(const I2c_BuckConverter_Domain* domain, u32 mvolt) {
u8 I2c_BuckConverter_MvOutToMultiplier(const I2c_BuckConverter_Domain *domain, u32 mvolt) {
u32 uvolt = mvolt * 1000;
if (uvolt < domain->uv_min)
uvolt = domain->uv_min;
@@ -117,7 +125,7 @@ u8 I2c_BuckConverter_MvOutToMultiplier(const I2c_BuckConverter_Domain* domain, u
return (uvolt - domain->uv_min) / domain->uv_step;
}
u32 I2c_BuckConverter_GetMvOut(const I2c_BuckConverter_Domain* domain) {
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++) {
@@ -133,7 +141,7 @@ u32 I2c_BuckConverter_GetMvOut(const I2c_BuckConverter_Domain* domain) {
return I2c_BuckConverter_MultiplierToMvOut(domain, val & domain->volt_mask);
}
u32 I2c_BuckConverter_GetUvOut(const I2c_BuckConverter_Domain* domain) {
u32 I2c_BuckConverter_GetUvOut(const I2c_BuckConverter_Domain *domain) {
u8 val;
// Retry 5 times if received POR value
for (int i = 0; i < 5; i++) {
@@ -149,7 +157,7 @@ u32 I2c_BuckConverter_GetUvOut(const I2c_BuckConverter_Domain* domain) {
return I2c_BuckConverter_MultiplierToUvOut(domain, val & domain->volt_mask);
}
Result I2c_BuckConverter_SetMvOut(const I2c_BuckConverter_Domain* domain, u32 mvolt) {
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))
@@ -179,7 +187,7 @@ u8 I2c_Bq24193_Convert_mA_Raw(u32 ma) {
// Adjustment is required
u8 raw = 0;
if (ma > MA_RANGE_MAX) // capping
if (ma > MA_RANGE_MAX) // capping
ma = MA_RANGE_MAX;
bool pct20 = ma <= (MA_RANGE_MIN - 64);
@@ -188,8 +196,8 @@ u8 I2c_Bq24193_Convert_mA_Raw(u32 ma) {
raw |= 0x1;
}
ma -= ma % 100; // round to 100
ma -= (MA_RANGE_MIN - 64); // ceiling
ma -= ma % 100; // round to 100
ma -= (MA_RANGE_MIN - 64); // ceiling
raw |= (ma >> 6) << 2;
return raw;
@@ -223,14 +231,21 @@ Result I2c_Bq24193_SetFastChargeCurrentLimit(u32 ma) {
// Converts mA to the raw value for bits [2:0] of REG00
static u8 I2c_Bq24193_Convert_InputmA_Raw(u32 ma) {
if (ma <= 100) return 0b000;
if (ma <= 150) return 0b001;
if (ma <= 500) return 0b010;
if (ma <= 900) return 0b011;
if (ma <= 1200) return 0b100;
if (ma <= 1500) return 0b101;
if (ma <= 2000) return 0b110;
return 0b111; // 3000mA max
if (ma <= 100)
return 0b000;
if (ma <= 150)
return 0b001;
if (ma <= 500)
return 0b010;
if (ma <= 900)
return 0b011;
if (ma <= 1200)
return 0b100;
if (ma <= 1500)
return 0b101;
if (ma <= 2000)
return 0b110;
return 0b111; // 3000mA max
}
Result I2c_Bq24193_SetInputCurrentLimit(u32 ma) {
@@ -239,35 +254,51 @@ Result I2c_Bq24193_SetInputCurrentLimit(u32 ma) {
return 0;
u8 raw;
Result res = I2cRead_OutU8(I2cDevice_Bq24193,
BQ24193_INPUT_SOURCE_CONTROL_REG,
&raw);
Result res = I2cRead_OutU8(I2cDevice_Bq24193, BQ24193_INPUT_SOURCE_CONTROL_REG, &raw);
if (R_FAILED(res))
return res;
raw &= ~0x07;
raw |= I2c_Bq24193_Convert_InputmA_Raw(ma);
return I2cSet_U8(I2cDevice_Bq24193,BQ24193_INPUT_SOURCE_CONTROL_REG,raw);
return I2cSet_U8(I2cDevice_Bq24193, BQ24193_INPUT_SOURCE_CONTROL_REG, raw);
}
// not used
Result I2c_Bq24193_GetInputCurrentLimit(u32 *ma) {
u8 raw;
Result res = I2cRead_OutU8(I2cDevice_Bq24193,BQ24193_INPUT_SOURCE_CONTROL_REG,&raw);
Result res = I2cRead_OutU8(I2cDevice_Bq24193, BQ24193_INPUT_SOURCE_CONTROL_REG, &raw);
if (R_FAILED(res))
return res;
switch (raw & 0x07) {
case 0b000: *ma = 100; break;
case 0b001: *ma = 150; break;
case 0b010: *ma = 500; break;
case 0b011: *ma = 900; break;
case 0b100: *ma = 1200; break;
case 0b101: *ma = 1500; break;
case 0b110: *ma = 2000; break;
case 0b111: *ma = 3000; break;
default: *ma = 0; break;
case 0b000:
*ma = 100;
break;
case 0b001:
*ma = 150;
break;
case 0b010:
*ma = 500;
break;
case 0b011:
*ma = 900;
break;
case 0b100:
*ma = 1200;
break;
case 0b101:
*ma = 1500;
break;
case 0b110:
*ma = 2000;
break;
case 0b111:
*ma = 3000;
break;
default:
*ma = 0;
break;
}
return 0;
}
+26 -16
View File
@@ -22,12 +22,12 @@ const u8 MAX17050_CURRENT_REG = 0x0A;
// Buck Converter
typedef enum I2c_BuckConverter_Reg {
I2c_Max77620_SD0VOLT_REG = 0x16,
I2c_Max77620_SD1VOLT_REG = 0x17, // Used for Erista DDR VDDQ+VDD2 / Mariko VDD2
I2c_Max77620_LDO0VOLT_REG = 0x23, // Used for Erista DDR VDDQ+VDD2 / Mariko VDD2
I2c_Max77621_VOLT_REG = 0x00,
I2c_Max77620_SD1VOLT_REG = 0x17, // Used for Erista DDR VDDQ+VDD2 / Mariko VDD2
I2c_Max77620_LDO0VOLT_REG = 0x23, // 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_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 {
@@ -40,22 +40,32 @@ typedef struct I2c_BuckConverter_Domain {
u8 por_val;
} I2c_BuckConverter_Domain;
const I2c_BuckConverter_Domain I2c_SOC = { I2cDevice_Max77620Pmic, I2c_Max77620_SD0VOLT_REG, 0x7F, 12500, 600000, 1400000, };
const I2c_BuckConverter_Domain I2c_VDD2 = { I2cDevice_Max77620Pmic, I2c_Max77620_SD1VOLT_REG, 0x7F, 12500, 600000, 1350000, };
const I2c_BuckConverter_Domain I2c_Display = { I2cDevice_Max77620Pmic, I2c_Max77620_LDO0VOLT_REG, 0x3F, 25000, 800000, 1325000, };
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_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, 700000, 0x78 };
const I2c_BuckConverter_Domain I2c_SOC = {
I2cDevice_Max77620Pmic, I2c_Max77620_SD0VOLT_REG, 0x7F, 12500, 600000, 1400000,
};
const I2c_BuckConverter_Domain I2c_VDD2 = {
I2cDevice_Max77620Pmic, I2c_Max77620_SD1VOLT_REG, 0x7F, 12500, 600000, 1350000,
};
const I2c_BuckConverter_Domain I2c_Display = {
I2cDevice_Max77620Pmic, I2c_Max77620_LDO0VOLT_REG, 0x3F, 25000, 800000, 1325000,
};
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_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, 700000, 0x78 };
u32 I2c_BuckConverter_GetMvOut(const I2c_BuckConverter_Domain* domain);
u32 I2c_BuckConverter_GetUvOut(const I2c_BuckConverter_Domain* domain);
Result I2c_BuckConverter_SetMvOut(const I2c_BuckConverter_Domain* domain, u32 mvolt);
u32 I2c_BuckConverter_GetMvOut(const I2c_BuckConverter_Domain *domain);
u32 I2c_BuckConverter_GetUvOut(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);
u8 I2c_Bq24193_Convert_mA_Raw(u32 ma);
Result I2c_Bq24193_GetFastChargeCurrentLimit(u32 *ma);
Result I2c_Bq24193_SetFastChargeCurrentLimit(u32 ma);
+17 -29
View File
@@ -22,15 +22,15 @@
*
*/
#include "max17050.h"
#include "i2c.h"
#include "max17050.h"
#define MAX17050_WAIT_NS 1000000000UL
#define MAX17050_VCELL 0x09
#define MAX17050_Current 0x0A
#define MAX17050_VCELL 0x09
#define MAX17050_Current 0x0A
#define MAX17050_AvgCurrent 0x0B
#define MAX17050_AvgVCELL 0x19
#define MAX17050_AvgVCELL 0x19
#define MAX17050_BOARD_CGAIN 2
#define MAX17050_BOARD_SNS_RESISTOR_UOHM 5000
@@ -40,15 +40,13 @@ static u64 g_update_ticks = 0;
static s32 g_power_now = 0;
static s32 g_power_avg = 0;
static Result _max17050_get_power(s32 *out_mw_now, s32 *out_mw_avg)
{
static Result _max17050_get_power(s32 *out_mw_now, s32 *out_mw_avg) {
s64 ma, mv;
u16 values[3] = {0};
u16 values[3] = { 0 };
Result rc = i2csessionExtRegReceive(&g_i2c_session, MAX17050_VCELL, values, sizeof(values));
if (R_SUCCEEDED(rc))
{
if (R_SUCCEEDED(rc)) {
ma = (s16)values[1];
ma = ma * 1562500 / (MAX17050_BOARD_SNS_RESISTOR_UOHM * MAX17050_BOARD_CGAIN);
@@ -57,13 +55,11 @@ static Result _max17050_get_power(s32 *out_mw_now, s32 *out_mw_avg)
*out_mw_now = ma * mv / 1000000;
}
if (R_SUCCEEDED(rc))
{
if (R_SUCCEEDED(rc)) {
rc = i2csessionExtRegReceive(&g_i2c_session, MAX17050_AvgVCELL, values, sizeof(u16));
}
if (R_SUCCEEDED(rc))
{
if (R_SUCCEEDED(rc)) {
ma = (s16)values[2];
ma = ma * 1562500 / (MAX17050_BOARD_SNS_RESISTOR_UOHM * MAX17050_BOARD_CGAIN);
@@ -75,50 +71,42 @@ static Result _max17050_get_power(s32 *out_mw_now, s32 *out_mw_avg)
return rc;
}
static void _max17050_update()
{
static void _max17050_update() {
u64 ticks = armGetSystemTick();
if(armTicksToNs(ticks - g_update_ticks) <= MAX17050_WAIT_NS)
{
if (armTicksToNs(ticks - g_update_ticks) <= MAX17050_WAIT_NS) {
return;
}
g_update_ticks = ticks;
if(!serviceIsActive(&g_i2c_session.s))
{
if (!serviceIsActive(&g_i2c_session.s)) {
return;
}
_max17050_get_power(&g_power_now, &g_power_avg);
}
Result max17050Initialize(void)
{
Result max17050Initialize(void) {
Result rc = i2cInitialize();
if(R_SUCCEEDED(rc))
{
if (R_SUCCEEDED(rc)) {
rc = i2cOpenSession(&g_i2c_session, I2cDevice_Max17050);
}
return rc;
}
void max17050Exit(void)
{
void max17050Exit(void) {
i2csessionClose(&g_i2c_session);
i2cExit();
}
s32 max17050PowerNow(void)
{
s32 max17050PowerNow(void) {
_max17050_update();
return g_power_now;
}
s32 max17050PowerAvg(void)
{
s32 max17050PowerAvg(void) {
_max17050_update();
return g_power_avg;
}
+3 -5
View File
@@ -12,9 +12,9 @@
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
*/
/* --------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
@@ -24,12 +24,10 @@
* --------------------------------------------------------------------------
*/
#pragma once
#ifdef __cplusplus
extern "C"
{
extern "C" {
#endif
#include <switch.h>
+14 -25
View File
@@ -17,13 +17,13 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "tmp451.h"
#include "i2c.h"
#include "tmp451.h"
#define TMP451_WAIT_NS 1000000000UL
#define TMP451_PCB_TEMP_REG 0x00
#define TMP451_SOC_TEMP_REG 0x01
#define TMP451_PCB_TEMP_REG 0x00
#define TMP451_SOC_TEMP_REG 0x01
#define TMP451_SOC_TMP_DEC_REG 0x10
#define TMP451_PCB_TMP_DEC_REG 0x15
@@ -33,37 +33,31 @@ static u64 g_update_ticks = 0;
static s32 g_temp_pcb = 0;
static s32 g_temp_soc = 0;
static Result _tmp451_get_temp(u8 reg, u8 dec_reg, s32* out)
{
static Result _tmp451_get_temp(u8 reg, u8 dec_reg, s32 *out) {
u8 val = 0;
Result rc = i2csessionExtRegReceive(&g_i2c_session, reg, &val, sizeof(val));
if(R_SUCCEEDED(rc))
{
if (R_SUCCEEDED(rc)) {
*out = (s32)val * 1000;
rc = i2csessionExtRegReceive(&g_i2c_session, dec_reg, &val, sizeof(val));
}
if(R_SUCCEEDED(rc))
{
if (R_SUCCEEDED(rc)) {
*out += ((s32)(val >> 4) * 625) / 10;
}
return rc;
}
static void _tmp451_update()
{
static void _tmp451_update() {
u64 ticks = armGetSystemTick();
if(armTicksToNs(ticks - g_update_ticks) <= TMP451_WAIT_NS)
{
if (armTicksToNs(ticks - g_update_ticks) <= TMP451_WAIT_NS) {
return;
}
g_update_ticks = ticks;
if(!serviceIsActive(&g_i2c_session.s))
{
if (!serviceIsActive(&g_i2c_session.s)) {
return;
}
@@ -71,32 +65,27 @@ static void _tmp451_update()
_tmp451_get_temp(TMP451_SOC_TEMP_REG, TMP451_SOC_TMP_DEC_REG, &g_temp_soc);
}
Result tmp451Initialize(void)
{
Result tmp451Initialize(void) {
Result rc = i2cInitialize();
if(R_SUCCEEDED(rc))
{
if (R_SUCCEEDED(rc)) {
rc = i2cOpenSession(&g_i2c_session, I2cDevice_Tmp451);
}
return rc;
}
void tmp451Exit(void)
{
void tmp451Exit(void) {
i2csessionClose(&g_i2c_session);
i2cExit();
}
s32 tmp451TempPcb(void)
{
s32 tmp451TempPcb(void) {
_tmp451_update();
return g_temp_pcb;
}
s32 tmp451TempSoc(void)
{
s32 tmp451TempSoc(void) {
_tmp451_update();
return g_temp_soc;
}
+3 -5
View File
@@ -12,9 +12,9 @@
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
*/
/* --------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
@@ -24,12 +24,10 @@
* --------------------------------------------------------------------------
*/
#pragma once
#ifdef __cplusplus
extern "C"
{
extern "C" {
#endif
#include <switch.h>