1616#include "lilac_diag.h"
1717#include "powerctl.h"
1818#include "keyboard.h"
19+ #include "buttonctl.h"
1920
2021LOG_MODULE_REGISTER (lilac_power , LOG_LEVEL_INF );
2122
@@ -61,6 +62,8 @@ static bool fp_power_on;
6162
6263static struct k_spinlock lock ;
6364static struct powerctl pc ;
65+ static struct buttonctl button ;
66+ static const struct pin button_pin = PIN (gpiod , 2 ); // on_off_btn_l, active low
6467static struct k_timer timer ;
6568static 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
131147static 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) {
182199void 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) {
222240const 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+ }
0 commit comments