Skip to content

Commit f6bbca6

Browse files
committed
zephyr-cp: add native Lilac physical power-button mechanism
1 parent fb26861 commit f6bbca6

8 files changed

Lines changed: 189 additions & 0 deletions

File tree

‎ports/zephyr-cp/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ if(CONFIG_BOARD_FRAMEWORK_LILAC_DIAG)
2323
target_sources(app PRIVATE boards/framework/lilac_diag/adc.c)
2424
target_sources(app PRIVATE boards/framework/lilac_diag/kbc.c)
2525
target_sources(app PRIVATE boards/framework/lilac_diag/keyboard.c)
26+
target_sources(app PRIVATE boards/framework/lilac_diag/buttonctl.c)
2627
endif()
2728

2829
# Add I2C emulator control for native_sim testing
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include "buttonctl.h"
2+
#include <string.h>
3+
void buttonctl_arm(struct buttonctl *b, bool enabled, int64_t now) {
4+
memset(b, 0, sizeof(*b));
5+
b->enabled = enabled;
6+
b->candidate = -1;
7+
b->candidate_since = now;
8+
b->release_due = -1;
9+
}
10+
bool buttonctl_poll(struct buttonctl *b) {
11+
bool pending = b->enabled && b->start_pending;
12+
b->start_pending = false;
13+
return pending;
14+
}
15+
int buttonctl_tick(struct buttonctl *b, int raw, bool can_start,
16+
bool host_ready, bool powered, int64_t now) {
17+
if (!b->enabled) { return 0; }
18+
int action = 0;
19+
if (raw < 0) {
20+
b->errors++;
21+
if (b->host_low) { action |= BUTTON_RELEASE; }
22+
b->host_low = b->ready = b->pressed = b->start_pending = false;
23+
b->candidate = -1;
24+
b->release_due = -1;
25+
return action;
26+
}
27+
raw = !!raw;
28+
if (raw != b->candidate) {
29+
b->candidate = raw;
30+
b->candidate_since = now;
31+
}
32+
if (!host_ready && b->host_low) {
33+
b->host_low = false;
34+
b->release_due = -1;
35+
action |= BUTTON_RELEASE;
36+
}
37+
if (b->release_due >= 0 && now >= b->release_due) {
38+
b->release_due = -1;
39+
b->host_low = false;
40+
action |= BUTTON_RELEASE;
41+
}
42+
if (now - b->candidate_since >= BUTTON_DEBOUNCE_MS) {
43+
if (raw) {
44+
b->ready = true; // boot/reload-held button must first be released
45+
b->pressed = false;
46+
b->forwarded = b->held_off = false;
47+
} else if (b->ready && !b->pressed) {
48+
b->pressed = true;
49+
b->pressed_since = now;
50+
b->presses++;
51+
b->forwarded = host_ready;
52+
if (can_start) { b->start_pending = true; }
53+
else if (host_ready) {
54+
b->host_low = true;
55+
b->release_due = now + BUTTON_PULSE_MS;
56+
action |= BUTTON_ASSERT;
57+
}
58+
}
59+
}
60+
if (b->pressed && !b->held_off) {
61+
int64_t held = now - b->pressed_since;
62+
// Stock pre-OS behavior: 32 ms initial pulse, reassert at 4 s,
63+
// force G3 at 8 s. An off->on press never leaks to the AP again.
64+
if (held >= BUTTON_HOLD_MS) {
65+
b->held_off = true;
66+
b->start_pending = false;
67+
b->holds++;
68+
if (powered) { action |= BUTTON_OFF; }
69+
} else if (b->forwarded && host_ready && held >= 4000 && !b->host_low) {
70+
b->host_low = true;
71+
action |= BUTTON_ASSERT;
72+
}
73+
}
74+
if (!b->pressed && b->host_low && b->release_due < 0) {
75+
b->host_low = false;
76+
action |= BUTTON_RELEASE;
77+
}
78+
if (!can_start) { b->start_pending = false; }
79+
return action;
80+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Physical power-button mechanism; no Zephyr or hardware access.
2+
#pragma once
3+
#include <stdbool.h>
4+
#include <stdint.h>
5+
#define BUTTON_DEBOUNCE_MS 30
6+
#define BUTTON_PULSE_MS 32
7+
#define BUTTON_HOLD_MS 8000
8+
#define BUTTON_ASSERT 1
9+
#define BUTTON_RELEASE 2
10+
#define BUTTON_OFF 4
11+
struct buttonctl {
12+
bool enabled, ready, pressed, start_pending, host_low, forwarded, held_off;
13+
int8_t candidate;
14+
int64_t candidate_since, pressed_since, release_due;
15+
uint32_t presses, holds, errors;
16+
};
17+
void buttonctl_arm(struct buttonctl *b, bool enabled, int64_t now);
18+
// raw_level: active-low GPD2, negative on read error. All power decisions
19+
// remain with powerctl/the supervised Python policy; this returns actions.
20+
int buttonctl_tick(struct buttonctl *b, int raw_level, bool can_start,
21+
bool host_ready, bool powered, int64_t now);
22+
bool buttonctl_poll(struct buttonctl *b);

‎ports/zephyr-cp/boards/framework/lilac_diag/lilac_diag.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,7 @@ int lilac_power_led_set(int percent);
183183

184184
// RGB duty percentages, sides bit 0 left / bit 1 right.
185185
int lilac_side_led_set(int red, int green, int blue, int sides);
186+
187+
int lilac_button_arm(bool enabled);
188+
bool lilac_button_poll(void);
189+
void lilac_button_state(int32_t out[7]);

‎ports/zephyr-cp/boards/framework/lilac_diag/lilac_module.c‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,10 +594,30 @@ static mp_obj_t keyboard_map(size_t n, const mp_obj_t *args) {
594594
}
595595
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(keyboard_map_obj, 2, 3, keyboard_map);
596596

597+
// Native GPD2 button: arm/disarm, consume one off-state press, diagnostics.
598+
static mp_obj_t lilac_button_arm_fn(mp_obj_t enabled) {
599+
int ret = lilac_button_arm(mp_obj_is_true(enabled));
600+
if (ret < 0) { mp_raise_OSError(-ret); }
601+
return mp_const_none;
602+
}
603+
static MP_DEFINE_CONST_FUN_OBJ_1(lilac_button_arm_obj, lilac_button_arm_fn);
604+
static mp_obj_t lilac_button_poll_fn(void) { return mp_obj_new_bool(lilac_button_poll()); }
605+
static MP_DEFINE_CONST_FUN_OBJ_0(lilac_button_poll_obj, lilac_button_poll_fn);
606+
static mp_obj_t lilac_button_state_fn(void) {
607+
int32_t v[7]; mp_obj_t items[7];
608+
lilac_button_state(v);
609+
for (int i = 0; i < 7; i++) { items[i] = mp_obj_new_int(v[i]); }
610+
return mp_obj_new_tuple(7, items);
611+
}
612+
static MP_DEFINE_CONST_FUN_OBJ_0(lilac_button_state_obj, lilac_button_state_fn);
613+
597614
static const mp_rom_map_elem_t lilac_module_globals_table[] = {
598615
{ MP_ROM_QSTR(MP_QSTR_keyboard_state), MP_ROM_PTR(&keyboard_state_obj) },
599616
{ MP_ROM_QSTR(MP_QSTR_keyboard_map), MP_ROM_PTR(&keyboard_map_obj) },
600617
{ MP_ROM_QSTR(MP_QSTR_side_led_set), MP_ROM_PTR(&side_led_set_obj) },
618+
{ MP_ROM_QSTR(MP_QSTR_button_arm), MP_ROM_PTR(&lilac_button_arm_obj) },
619+
{ MP_ROM_QSTR(MP_QSTR_button_poll), MP_ROM_PTR(&lilac_button_poll_obj) },
620+
{ MP_ROM_QSTR(MP_QSTR_button_state), MP_ROM_PTR(&lilac_button_state_obj) },
601621
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_lilac) },
602622
{ MP_ROM_QSTR(MP_QSTR_inputs), MP_ROM_PTR(&lilac_inputs_obj) },
603623
{ MP_ROM_QSTR(MP_QSTR_input), MP_ROM_PTR(&lilac_input_obj) },

‎ports/zephyr-cp/boards/framework/lilac_diag/power.c‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "lilac_diag.h"
1717
#include "powerctl.h"
1818
#include "keyboard.h"
19+
#include "buttonctl.h"
1920

2021
LOG_MODULE_REGISTER(lilac_power, LOG_LEVEL_INF);
2122

@@ -61,6 +62,8 @@ static bool fp_power_on;
6162

6263
static struct k_spinlock lock;
6364
static struct powerctl pc;
65+
static struct buttonctl button;
66+
static const struct pin button_pin = PIN(gpiod, 2); // on_off_btn_l, active low
6467
static struct k_timer timer;
6568
static uint32_t ticks, write_errors, input_errors;
6669

@@ -111,6 +114,19 @@ static void timer_handler(struct k_timer *t) {
111114
sample(now);
112115
size_t n = powerctl_tick(&pc, now, w);
113116
apply(w, n);
117+
bool powered = powerctl_any_on(&pc);
118+
bool healthy = pc.armed && pc.fault == PWR_FAULT_NONE;
119+
bool host_ready = healthy && pc.out[PWR_RSMRST] && pc.in[PWR_IN_SLP_S5];
120+
int action = buttonctl_tick(&button,
121+
gpio_pin_get_raw(button_pin.port, button_pin.pin),
122+
healthy && !powered, host_ready, powered, now);
123+
if (action & BUTTON_OFF) {
124+
n = powerctl_power_off(&pc, now, w);
125+
apply(w, n);
126+
} else if (action & (BUTTON_ASSERT | BUTTON_RELEASE)) {
127+
n = powerctl_button_signal(&pc, action & BUTTON_ASSERT, now, w);
128+
apply(w, n);
129+
}
114130
lilac_keyboard_power(pc.armed && !pc.fault && pc.out[PWR_SYSON]);
115131
bool periph = powerctl_s0_peripherals(&pc);
116132
if (periph != periph_on) {
@@ -130,6 +146,7 @@ static void timer_handler(struct k_timer *t) {
130146

131147
static int lilac_power_init(void) {
132148
powerctl_init(&pc, LILAC_POWER_ARMED, k_uptime_get());
149+
buttonctl_arm(&button, false, k_uptime_get());
133150
k_timer_init(&timer, timer_handler, NULL);
134151
k_timer_start(&timer, K_MSEC(1), K_MSEC(1));
135152
return 0;
@@ -182,6 +199,7 @@ void lilac_power_off(void) {
182199
void lilac_power_reload(void) {
183200
struct powerctl_write w[PWR_MAX_WRITES];
184201
k_spinlock_key_t key = k_spin_lock(&lock);
202+
buttonctl_arm(&button, false, k_uptime_get());
185203
if (powerctl_any_on(&pc)) {
186204
size_t n = powerctl_emergency(&pc, PWR_FAULT_RELOAD, k_uptime_get(), w);
187205
apply(w, n);
@@ -222,3 +240,32 @@ void lilac_power_status(struct lilac_power_status *st) {
222240
const char *lilac_power_out_name(int out) {
223241
return powerctl_out_name(out);
224242
}
243+
244+
// Arming is policy opt-in, not an alternate way around the rail interlocks.
245+
int lilac_button_arm(bool enabled) {
246+
k_spinlock_key_t key = k_spin_lock(&lock);
247+
if (enabled && (!pc.armed || pc.fault || powerctl_any_on(&pc))) {
248+
k_spin_unlock(&lock, key);
249+
return -EBUSY;
250+
}
251+
if (!enabled && button.host_low) {
252+
struct powerctl_write w[PWR_MAX_WRITES];
253+
apply(w, powerctl_button_signal(&pc, false, k_uptime_get(), w));
254+
}
255+
buttonctl_arm(&button, enabled, k_uptime_get());
256+
k_spin_unlock(&lock, key);
257+
return 0;
258+
}
259+
bool lilac_button_poll(void) {
260+
k_spinlock_key_t key = k_spin_lock(&lock);
261+
bool pending = buttonctl_poll(&button);
262+
k_spin_unlock(&lock, key);
263+
return pending;
264+
}
265+
void lilac_button_state(int32_t out[7]) {
266+
k_spinlock_key_t key = k_spin_lock(&lock);
267+
out[0] = button.enabled; out[1] = button.ready; out[2] = button.pressed;
268+
out[3] = button.start_pending; out[4] = button.presses;
269+
out[5] = button.holds; out[6] = button.errors;
270+
k_spin_unlock(&lock, key);
271+
}

‎ports/zephyr-cp/boards/framework/lilac_diag/powerctl.c‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,17 @@ int powerctl_request(struct powerctl *pc, int out, int level, int64_t now,
192192
return 0;
193193
}
194194

195+
size_t powerctl_button_signal(struct powerctl *pc, bool pressed, int64_t now,
196+
struct powerctl_write *w) {
197+
size_t n = 0;
198+
if (!pc->armed || pc->fault || !pc->out[PWR_RSMRST] ||
199+
(pressed && (!pc->in[PWR_IN_SLP_S5] || powering_down(pc))) || pc->pbtn_release_ms >= 0) {
200+
return 0;
201+
}
202+
put(pc, PWR_PBTN, !pressed, now, w, &n);
203+
return n;
204+
}
205+
195206
int powerctl_pbtn_pulse(struct powerctl *pc, int64_t now,
196207
struct powerctl_write *w, size_t *n) {
197208
*n = 0;

‎ports/zephyr-cp/boards/framework/lilac_diag/powerctl.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ int powerctl_request(struct powerctl *pc, int out, int level, int64_t now,
100100
// 20 ms power-button press (stock G3S5): RSMRST high for 90 ms, PBTN released.
101101
int powerctl_pbtn_pulse(struct powerctl *pc, int64_t now,
102102
struct powerctl_write *w, size_t *n);
103+
// Physical button only: PBTN changes with no lease refresh. Requires an
104+
// armed, healthy, running host; never release PBTN into G3 or a boot pulse.
105+
size_t powerctl_button_signal(struct powerctl *pc, bool pressed, int64_t now,
106+
struct powerctl_write *w);
103107
void powerctl_heartbeat(struct powerctl *pc, int64_t now);
104108
// Immediate shutdown in force_g3 order (stock chipset_force_shutdown()),
105109
// not latched. Returns the writes (all outputs low).

0 commit comments

Comments
 (0)