2026-11-22 10:49:59 +02:00
|
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
|
#include "geminux-power-policy.h"
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
void geminux_policy_init(geminux_power_policy_t *p) {
|
|
|
|
|
memset(p, 0, sizeof(*p));
|
|
|
|
|
p->profile = POWER_PROFILE_BALANCED;
|
|
|
|
|
p->lid_suspend_enabled = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool geminux_policy_set_profile_name(geminux_power_policy_t *p, const char *name) {
|
|
|
|
|
if (!p || !name) return false;
|
|
|
|
|
if (strcmp(name, "eco") == 0) {
|
|
|
|
|
p->profile = POWER_PROFILE_ECO;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (strcmp(name, "balanced") == 0) {
|
|
|
|
|
p->profile = POWER_PROFILE_BALANCED;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (strcmp(name, "performance") == 0) {
|
|
|
|
|
p->profile = POWER_PROFILE_PERFORMANCE;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char *geminux_policy_profile_name(geminux_profile_t prof) {
|
|
|
|
|
switch (prof) {
|
|
|
|
|
case POWER_PROFILE_ECO: return "eco";
|
|
|
|
|
case POWER_PROFILE_PERFORMANCE: return "performance";
|
|
|
|
|
case POWER_PROFILE_BALANCED:
|
|
|
|
|
default: return "balanced";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
geminux_profile_params_t geminux_policy_params(geminux_profile_t prof, bool on_ac) {
|
|
|
|
|
geminux_profile_params_t out;
|
|
|
|
|
out.governor = "schedutil";
|
|
|
|
|
if (prof == POWER_PROFILE_ECO) {
|
2026-11-24 11:05:18 +02:00
|
|
|
out.governor = "schedutil";
|
2026-11-22 10:49:59 +02:00
|
|
|
out.a72_online = false;
|
2026-11-24 11:05:18 +02:00
|
|
|
out.a53_ll_max_khz = 715000UL;
|
|
|
|
|
out.a53_max_khz = 962000UL;
|
2026-11-22 10:49:59 +02:00
|
|
|
out.a72_max_khz = 0UL;
|
|
|
|
|
out.dim_timeout_s = on_ac ? 180 : 60;
|
|
|
|
|
out.suspend_timeout_s = on_ac ? 0 : 180;
|
|
|
|
|
} else if (prof == POWER_PROFILE_PERFORMANCE) {
|
2026-11-24 11:05:18 +02:00
|
|
|
out.governor = "performance";
|
2026-11-22 10:49:59 +02:00
|
|
|
out.a72_online = true;
|
2026-11-23 20:00:17 +02:00
|
|
|
out.a53_ll_max_khz = 1547000UL;
|
2026-11-22 10:49:59 +02:00
|
|
|
out.a53_max_khz = 1846000UL;
|
|
|
|
|
out.a72_max_khz = 2600000UL;
|
|
|
|
|
out.dim_timeout_s = on_ac ? 600 : 180;
|
|
|
|
|
out.suspend_timeout_s = 0;
|
|
|
|
|
} else {
|
2026-11-24 11:05:18 +02:00
|
|
|
out.governor = "schedutil";
|
2026-11-22 10:49:59 +02:00
|
|
|
out.a72_online = true;
|
2026-11-24 11:05:18 +02:00
|
|
|
out.a53_ll_max_khz = on_ac ? 1547000UL : 897000UL;
|
|
|
|
|
out.a53_max_khz = on_ac ? 1846000UL : 1209000UL;
|
2026-11-22 10:49:59 +02:00
|
|
|
out.a72_max_khz = 2002000UL;
|
|
|
|
|
out.dim_timeout_s = on_ac ? 300 : 120;
|
|
|
|
|
out.suspend_timeout_s = on_ac ? 0 : 300;
|
|
|
|
|
}
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-23 09:31:00 +02:00
|
|
|
geminux_idle_action_t geminux_policy_eval_idle(
|
|
|
|
|
const geminux_power_policy_t *p,
|
|
|
|
|
geminux_idle_stage_t stage,
|
|
|
|
|
uint64_t elapsed_ms,
|
|
|
|
|
uint64_t *next_wait_ms) {
|
|
|
|
|
if (next_wait_ms) *next_wait_ms = 0;
|
|
|
|
|
if (!p || p->lid_closed || p->hdmi_connected || p->inhibit_count > 0) {
|
|
|
|
|
return IDLE_ACT_NONE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
geminux_profile_params_t par = geminux_policy_params(p->profile, p->on_ac_power);
|
|
|
|
|
uint64_t dim_ms = (par.dim_timeout_s > 0) ? (uint64_t)par.dim_timeout_s * 1000ULL : 0;
|
|
|
|
|
uint64_t susp_ms =
|
|
|
|
|
(!p->on_ac_power && par.suspend_timeout_s > 0) ? (uint64_t)par.suspend_timeout_s * 1000ULL : 0;
|
|
|
|
|
|
|
|
|
|
if (susp_ms > 0) {
|
|
|
|
|
if (elapsed_ms >= susp_ms) {
|
|
|
|
|
if (next_wait_ms) *next_wait_ms = 0;
|
|
|
|
|
return IDLE_ACT_SUSPEND;
|
|
|
|
|
}
|
|
|
|
|
if (dim_ms > 0 && dim_ms < susp_ms) {
|
|
|
|
|
if (elapsed_ms >= dim_ms) {
|
|
|
|
|
if (next_wait_ms) *next_wait_ms = susp_ms - elapsed_ms;
|
|
|
|
|
return (stage == IDLE_STAGE_ACTIVE) ? IDLE_ACT_DIM : IDLE_ACT_NONE;
|
|
|
|
|
}
|
|
|
|
|
if (next_wait_ms) *next_wait_ms = dim_ms - elapsed_ms;
|
|
|
|
|
return IDLE_ACT_NONE;
|
|
|
|
|
}
|
|
|
|
|
if (next_wait_ms) *next_wait_ms = susp_ms - elapsed_ms;
|
|
|
|
|
return IDLE_ACT_NONE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (dim_ms > 0) {
|
|
|
|
|
if (elapsed_ms >= dim_ms) {
|
|
|
|
|
if (next_wait_ms) *next_wait_ms = 0;
|
|
|
|
|
return (stage != IDLE_STAGE_LOCKED_BLANK) ? IDLE_ACT_LOCK_BLANK : IDLE_ACT_NONE;
|
|
|
|
|
}
|
|
|
|
|
if (next_wait_ms) *next_wait_ms = dim_ms - elapsed_ms;
|
|
|
|
|
return IDLE_ACT_NONE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return IDLE_ACT_NONE;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-22 10:49:59 +02:00
|
|
|
bool geminux_policy_add_inhibit(geminux_power_policy_t *p, const char *tag) {
|
|
|
|
|
if (!p || !tag || !tag[0]) return false;
|
|
|
|
|
for (int i = 0; i < p->inhibit_count; i++) {
|
|
|
|
|
if (strcmp(p->inhibitors[i], tag) == 0) return true;
|
|
|
|
|
}
|
|
|
|
|
if (p->inhibit_count >= GEMINUX_MAX_INHIBITORS) return false;
|
|
|
|
|
snprintf(p->inhibitors[p->inhibit_count], sizeof(p->inhibitors[0]), "%s", tag);
|
|
|
|
|
p->inhibit_count++;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool geminux_policy_del_inhibit(geminux_power_policy_t *p, const char *tag) {
|
|
|
|
|
if (!p || !tag || !tag[0]) return false;
|
|
|
|
|
for (int i = 0; i < p->inhibit_count; i++) {
|
|
|
|
|
if (strcmp(p->inhibitors[i], tag) == 0) {
|
|
|
|
|
for (int j = i; j + 1 < p->inhibit_count; j++) {
|
|
|
|
|
memcpy(p->inhibitors[j], p->inhibitors[j + 1], sizeof(p->inhibitors[0]));
|
|
|
|
|
}
|
|
|
|
|
p->inhibit_count--;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool geminux_should_suspend_on_lid(const geminux_power_policy_t *p) {
|
|
|
|
|
if (!p || !p->lid_closed || !p->lid_suspend_enabled) return false;
|
|
|
|
|
if (p->hdmi_connected) return false;
|
|
|
|
|
if (p->inhibit_count > 0) return false;
|
|
|
|
|
if (p->on_ac_power && p->inhibit_ac_lid) return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2026-11-24 11:05:18 +02:00
|
|
|
|
|
|
|
|
static bool geminux_fg_r_valid(uint32_t r_uohm) {
|
|
|
|
|
return r_uohm >= 50000U && r_uohm <= 1500000U;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool geminux_fg_full_valid(uint32_t full_uah) {
|
|
|
|
|
return full_uah >= 1000000U && full_uah <= 10000000U;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool geminux_fg_state_parse(const char *text, uint32_t *r_uohm, uint32_t *full_uah) {
|
|
|
|
|
if (!text || !r_uohm || !full_uah) return false;
|
|
|
|
|
uint32_t r = 0, full = 0;
|
|
|
|
|
bool got_r = false, got_full = false;
|
|
|
|
|
const char *p = text;
|
|
|
|
|
while (*p) {
|
|
|
|
|
const char *nl = strchr(p, '\n');
|
|
|
|
|
size_t len = nl ? (size_t)(nl - p) : strlen(p);
|
|
|
|
|
size_t start = 0;
|
|
|
|
|
while (start < len && (p[start] == ' ' || p[start] == '\t')) {
|
|
|
|
|
start++;
|
|
|
|
|
}
|
|
|
|
|
while (len > start && (p[len - 1] == '\r' || p[len - 1] == ' ' || p[len - 1] == '\t')) {
|
|
|
|
|
len--;
|
|
|
|
|
}
|
|
|
|
|
size_t trim_len = len - start;
|
|
|
|
|
if (trim_len > 0 && trim_len < 96) {
|
|
|
|
|
char line[96];
|
|
|
|
|
memcpy(line, p + start, trim_len);
|
|
|
|
|
line[trim_len] = '\0';
|
|
|
|
|
unsigned long long val = 0;
|
|
|
|
|
char tail = '\0';
|
|
|
|
|
if (sscanf(line, "learned_resistance_uohm=%llu%c", &val, &tail) == 1 &&
|
|
|
|
|
val <= 0xffffffffULL) {
|
|
|
|
|
r = (uint32_t)val;
|
|
|
|
|
got_r = true;
|
|
|
|
|
} else if (sscanf(line, "learned_full_uah=%llu%c", &val, &tail) == 1 &&
|
|
|
|
|
val <= 0xffffffffULL) {
|
|
|
|
|
full = (uint32_t)val;
|
|
|
|
|
got_full = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
p = nl ? (nl + 1) : (p + strlen(p));
|
|
|
|
|
}
|
|
|
|
|
if (!got_r || !got_full || !geminux_fg_r_valid(r) || !geminux_fg_full_valid(full)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
*r_uohm = r;
|
|
|
|
|
*full_uah = full;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int geminux_fg_state_format(uint32_t r_uohm, uint32_t full_uah, char *buf, size_t sz) {
|
|
|
|
|
if (!buf || sz == 0 || !geminux_fg_r_valid(r_uohm) || !geminux_fg_full_valid(full_uah)) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
int n = snprintf(buf, sz, "learned_resistance_uohm=%u\nlearned_full_uah=%u\n",
|
|
|
|
|
r_uohm, full_uah);
|
|
|
|
|
return (n > 0 && (size_t)n < sz) ? 0 : -1;
|
|
|
|
|
}
|