Skip to content

Instantly share code, notes, and snippets.

@matthiasjg
Last active October 3, 2026 19:34
Show Gist options
  • Select an option

  • Save matthiasjg/78aaf7802146f0b89be3da9e4feb111f to your computer and use it in GitHub Desktop.

Select an option

Save matthiasjg/78aaf7802146f0b89be3da9e4feb111f to your computer and use it in GitHub Desktop.
MacBook8,1 (12" MacBook 2015) keyboard/touchpad/suspend/hibernate fix for Omarchy 4 / Arch Linux — applespi PIO mode via initcall_blacklist, s2idle + sleep hooks. Tested: Omarchy 4.0.0, kernel 7.1.8-arch1; revised 2026-10-03 (early-wake fix for suspend-then-hibernate; open issue: intermittent hibernate hang on 7.2.5-omarchy, see header)
#!/bin/bash
# MacBook8,1 (12" MacBook, early 2015) — internal keyboard + touchpad fix
# for Omarchy/Arch, including suspend, hibernate and suspend-then-hibernate
# (lid close on battery: s2idle now, auto-hibernate after 2h). Idempotent.
#
# Tested on: MacBook8,1, Omarchy 4.0.0, kernel 7.1.8/7.1.9-arch1, 2026-09-06
# Revised 2026-10-03 (kernel 7.2.5-omarchy, systemd 261): see root cause 5.
#
# Root causes found:
# 1. Boot: the DesignWare DMA engine (00:15.0, dw_dmac_pci, builtin) never
# completes SPI transfers for applespi on the GSPI controller (00:15.4);
# -110 timeouts. PIO mode works -> disable that driver via initcall
# blacklist.
# 2. Sleep: the applespi/SPI suspend callbacks wedge the device (deep/S3
# unrecoverably). -> s2idle as default suspend + detach the SPI stack
# around every sleep via a system-sleep hook (must live in
# /usr/lib/systemd/system-sleep - systemd does not scan /etc).
# 3. Hibernate: ACPI S4 "platform" mode hangs -> HibernateMode=shutdown;
# active zram swap during image write -> swapoff around hibernate;
# restore hangs when the boot kernel freezes an active applespi ->
# detach it in the initramfs right before the resume step (after the
# LUKS passphrase), with a boot service rebinding it on normal boots.
# 4. suspend-then-hibernate: systemd runs the system-sleep hooks around
# EACH phase. Restoring drivers at the middle (RTC-alarm) wake races
# the immediate re-unload against brcmfmac's async firmware probe
# (kernel oops, wifi dead until reboot) -> the hook is phase-aware
# and stays detached between the phases.
# 5. (2026-10-03) The phase detection used "RTC wakealarm empty" as proof
# that the hibernate timer had fired - but systemd's timer never shows
# up in wakealarm, so EVERY wake from the suspend phase looked like the
# timer. An early wake (upower's critical-battery s-t-h, woken 12s later
# by plugging in AC) left keyboard, touchpad and wifi detached for good.
# -> decide by $SYSTEMD_SLEEP_ACTION + time actually slept, and add a
# safety-net unit that re-checks after every completed sleep operation,
# whatever the hook decided. Critical battery now hibernates directly.
#
# KNOWN OPEN ISSUE (2026-10-03, kernel 7.2.5-3-omarchy): the hibernate at
# the end of a real 2h suspend-then-hibernate hung once ("PM: hibernation:
# hibernation entry", no image written, hard reset needed). Not reproduced
# in five further hibernates the same evening (plain, and right after a 30s
# s2idle; lid open/closed) - intermittent, or tied to the long suspend. To
# catch it, the hook keeps the kernel console alive and verbose during the
# hibernate step only: if it hangs, the last lines on screen say where.
# Also seen: systemd's hibernate timer can lose a race against the RTC wake
# (woken ~1s early -> treated as a user wake -> no hibernate that time).
#
# Note for zram-only setups: hibernation needs real disk swap, and a
# swapfile with negative priority (pri=-2) is silently skipped by systemd
# as hibernation target - give it a non-negative priority below zram's.
set -euo pipefail
[ "$(id -u)" -eq 0 ] || { echo "run as root"; exit 1; }
echo "== mkinitcpio: early-load SPI modules"
cp -n /etc/mkinitcpio.conf /etc/mkinitcpio.conf.bak-macbookfix || true
sed -i -E '/^MODULES=/{s/dw_dmac_pci ?//g; s/dw_dmac ?//g}' /etc/mkinitcpio.conf
for m in applespi spi_pxa2xx_platform spi_pxa2xx_pci; do
grep -qE "^MODULES=.*[( ]$m[ )]" /etc/mkinitcpio.conf || \
sed -i -E "s/^MODULES=\(/MODULES=($m /" /etc/mkinitcpio.conf
done
sed -i -E '/^MODULES=/{s/\( +/(/; s/ +\)/)/}' /etc/mkinitcpio.conf
echo "== limine: kernel parameters"
cat > /etc/limine-entry-tool.d/macbook81-spi-fix.conf <<'CONF'
# MacBook8,1: broken DesignWare DMA engine (00:15.0) -> force spi-pxa2xx
# into PIO mode by blacklisting its driver's initcall. s2idle: only suspend
# mode that doesn't wedge the applespi device.
KERNEL_CMDLINE[default]+=" initcall_blacklist=dw_pci_driver_init mem_sleep_default=s2idle"
CONF
echo "== initramfs hook: detach applespi before hibernate-image restore"
mkdir -p /etc/initcpio/hooks /etc/initcpio/install
cat > /etc/initcpio/hooks/macbook81-spi-detach <<'RHOOK'
#!/usr/bin/ash
# Runs after the LUKS passphrase, right before the resume hook. If a
# hibernation image is about to be restored, the boot kernel first freezes
# its devices - and applespi's freeze callback hangs this machine. Detach
# the SPI stack so the restore proceeds cleanly. On normal boots the
# macbook81-applespi-rebind.service reattaches it.
run_hook() {
rmmod applespi 2>/dev/null
if [ -e /sys/bus/pci/devices/0000:00:15.4/driver ]; then
echo 0000:00:15.4 > /sys/bus/pci/drivers/pxa2xx_spi_pci/unbind 2>/dev/null
fi
return 0
}
RHOOK
cat > /etc/initcpio/install/macbook81-spi-detach <<'IHOOK'
#!/bin/bash
build() { add_runscript; }
help() { echo "Detaches the MacBook8,1 applespi SPI stack before hibernate resume."; }
IHOOK
# Insert between omarchy_hooks.conf and omarchy_resume.conf (alphabetical
# sourcing) so the hook lands right before 'resume' in HOOKS.
cat > /etc/mkinitcpio.conf.d/omarchy_pre_resume_macbook81.conf <<'CONF'
HOOKS+=(macbook81-spi-detach)
CONF
echo "== boot service: rebind SPI stack on normal (non-resume) boots"
cat > /etc/systemd/system/macbook81-applespi-rebind.service <<'UNIT'
[Unit]
Description=Rebind MacBook8,1 applespi after initramfs detach
[Service]
Type=oneshot
ExecStart=/bin/bash -c '[ -e /sys/bus/pci/devices/0000:00:15.4/driver ] || echo 0000:00:15.4 > /sys/bus/pci/drivers/pxa2xx_spi_pci/bind; modprobe applespi'
[Install]
WantedBy=multi-user.target
UNIT
systemctl enable macbook81-applespi-rebind.service >/dev/null 2>&1
echo "== system-sleep hook (/usr/lib - the only dir systemd scans)"
rm -f /etc/systemd/system-sleep/macbook81-applespi
cat > /usr/lib/systemd/system-sleep/macbook81-applespi <<'HOOK'
#!/bin/bash
# MacBook8,1: detach the applespi SPI stack around any sleep (its
# suspend/resume callbacks wedge the device). Around hibernate additionally
# drop zram swap (can hang image writing) and unload the Broadcom wifi
# driver (brcmfmac intermittently hangs the image write). Never fail sleep.
#
# suspend-then-hibernate runs this hook around BOTH phases:
# pre -> [s2idle] -> post -> pre -> [hibernate] -> post
# Restoring drivers at the middle post is not just pointless - the pre that
# follows seconds later unloads brcmfmac again mid-probe (async firmware
# load), which oopses the kernel and leaves wifi dead until reboot. Detect
# that transition and skip the restore: it is the post of the first phase,
# systemd says that phase was the suspend ($SYSTEMD_SLEEP_ACTION), and we
# slept the full HibernateDelaySec. (Do NOT use the RTC wakealarm for this:
# systemd's timer never shows there, so it reads empty on every wake and a
# user/AC wake gets mistaken for the timer - keyboard gone until reboot.)
# A wake shortly before the timer can still be misjudged; "post ensure",
# run by macbook81-applespi-ensure.service once the whole sleep operation
# has finished, repairs whatever was left detached.
SPI_DEV=0000:00:15.4
DRV=/sys/bus/pci/drivers/pxa2xx_spi_pci
STATE=/run/macbook81-sth-phase # /run is RAM-backed: survives the hibernate image
START=/run/macbook81-sth-start
HIB_DELAY_S=7200 # keep in sync with HibernateDelaySec in sleep.conf.d
LOG=/var/log/macbook81-sleep.log
# Crash-proof log: fdatasync after every line so a hard reset can't eat it.
log() { echo "$(date '+%F %T') [$1/$2] $3" >> $LOG; sync -d $LOG 2>/dev/null; }
# Diagnostics for the open hibernate hang: narrate the hibernate step on the
# console, and put everything back afterwards.
KLOG=/run/macbook81-loglevel
console_verbose() {
[ -e $KLOG ] || cut -f1 /proc/sys/kernel/printk > $KLOG
echo N > /sys/module/printk/parameters/console_suspend
echo 1 > /sys/power/pm_debug_messages
dmesg -n 7
}
console_quiet() {
[ -e $KLOG ] || return 0
echo Y > /sys/module/printk/parameters/console_suspend
echo 0 > /sys/power/pm_debug_messages
dmesg -n "$(cat $KLOG)" 2>/dev/null || dmesg -n 1
rm -f $KLOG
}
spi_alive() { grep -q "Apple SPI Keyboard" /proc/bus/input/devices 2>/dev/null; }
restore_spi() {
# Bind + load, then verify the input devices actually came back; the
# revival occasionally fails silently, so retry with a full detach cycle.
try=0
while [ $try -lt 3 ]; do
try=$((try+1))
[ -e /sys/bus/pci/devices/$SPI_DEV/driver ] || echo $SPI_DEV > $DRV/bind 2>/dev/null
modprobe applespi 2>/dev/null
i=0
while [ $i -lt 10 ]; do
spi_alive && { log "$1" "$2" "applespi restored (attempt $try)"; return 0; }
sleep 0.3; i=$((i+1))
done
log "$1" "$2" "applespi NOT alive after attempt $try, recycling"
modprobe -r applespi 2>/dev/null
[ -e /sys/bus/pci/devices/$SPI_DEV/driver ] && echo $SPI_DEV > $DRV/unbind 2>/dev/null
sleep 0.5
done
log "$1" "$2" "applespi restore FAILED after 3 attempts"
return 1
}
hib=0
case "$2" in hibernate|hybrid-sleep|suspend-then-hibernate|ensure) hib=1;; esac
case "$1" in
pre)
log "$1" "$2" "detaching (hib=$hib)"
modprobe -r applespi 2>/dev/null
[ -e /sys/bus/pci/devices/$SPI_DEV/driver ] && echo $SPI_DEV > $DRV/unbind
if [ "$hib" = 1 ]; then
swapoff /dev/zram0 2>/dev/null
modprobe -r brcmfmac_wcc brcmfmac 2>/dev/null
fi
if [ "$2" = suspend-then-hibernate ]; then
n=$(cat $STATE 2>/dev/null) || n=0
echo $((n+1)) > $STATE
[ "$n" = 0 ] && date +%s > $START
log "$1" "$2" "phase counter -> $((n+1))"
fi
[ "${SYSTEMD_SLEEP_ACTION:-$2}" = hibernate ] && console_verbose
log "$1" "$2" "detach done"
;;
post)
if [ "$2" = suspend-then-hibernate ]; then
n=$(cat $STATE 2>/dev/null) || n=0
start=$(cat $START 2>/dev/null) || start=$(date +%s)
slept=$(( $(date +%s) - start ))
action=${SYSTEMD_SLEEP_ACTION:-suspend}
log "$1" "$2" "wake: phase=$n action=$action slept=${slept}s"
if [ "$n" = 1 ] && [ "$action" = suspend ] && [ "$slept" -ge $((HIB_DELAY_S - 60)) ]; then
log "$1" "$2" "hibernate timer wake: hibernate follows, staying detached"
exit 0
fi
fi
if [ "$2" = ensure ]; then
# Safety net after a completed sleep operation: silent when all is well.
spi_alive && [ -d /sys/module/brcmfmac ] && [ ! -e $STATE ] && exit 0
log "$1" "$2" "sleep finished with drivers still detached, repairing"
fi
rm -f $STATE $START
console_quiet
restore_spi "$1" "$2"
[ "$hib" = 1 ] && modprobe brcmfmac 2>/dev/null
swapon -p 100 /dev/zram0 2>/dev/null
log "$1" "$2" "restore done"
;;
esac
exit 0
HOOK
chmod +x /usr/lib/systemd/system-sleep/macbook81-applespi
echo "== safety net: re-check keyboard/wifi after every completed sleep"
# Ordered after the sleep targets, so it runs once the whole operation
# (both phases of suspend-then-hibernate) is over - never at the transition.
cat > /etc/systemd/system/macbook81-applespi-ensure.service <<'UNIT'
[Unit]
Description=MacBook8,1: ensure applespi and wifi are back after sleep
After=suspend.target hibernate.target hybrid-sleep.target suspend-then-hibernate.target
[Service]
Type=oneshot
ExecStart=/usr/lib/systemd/system-sleep/macbook81-applespi post ensure
[Install]
WantedBy=suspend.target hibernate.target hybrid-sleep.target suspend-then-hibernate.target
UNIT
systemctl enable macbook81-applespi-ensure.service >/dev/null 2>&1
echo "== hibernate mode: shutdown (ACPI S4 'platform' hangs on this Mac)"
mkdir -p /etc/systemd/sleep.conf.d
cat > /etc/systemd/sleep.conf.d/10-macbook-hibernate.conf <<'CONF'
[Sleep]
HibernateMode=shutdown
# s2idle drains ~10%/day on this machine: after 2h of suspend, wake via RTC
# alarm and hibernate instead (used by suspend-then-hibernate).
HibernateDelaySec=2h
CONF
echo "== critical battery: hibernate directly"
# UPower's default (Auto) picked suspend-then-hibernate here: 2h of s2idle
# on an almost empty battery, and it overrides sleep inhibitors (it fired
# in the middle of an omarchy-update). Write the image to disk right away.
mkdir -p /etc/UPower/UPower.conf.d
cat > /etc/UPower/UPower.conf.d/10-macbook81-critical.conf <<'CONF'
[UPower]
CriticalPowerAction=Hibernate
CONF
systemctl try-restart upower >/dev/null 2>&1 || true
echo "== lid switch: suspend-then-hibernate (s2idle first, hibernate after 2h)"
mkdir -p /etc/systemd/logind.conf.d
cat > /etc/systemd/logind.conf.d/30-macbook-lid-sth.conf <<'CONF'
[Login]
# On battery: s2idle now, auto-hibernate after HibernateDelaySec so a closed
# lid doesn't drain the battery over days. On AC plain suspend is fine.
HandleLidSwitch=suspend-then-hibernate
HandleLidSwitchExternalPower=suspend
CONF
systemctl try-restart systemd-logind >/dev/null 2>&1 || true
echo "== cleanup: remove debug drop-ins from testing"
rm -rf /etc/systemd/system/systemd-hibernate.service.d
rm -f /etc/systemd/sleep.conf.d/99-sth-test.conf
systemctl daemon-reload
echo "== rebuilding initramfs and boot entries"
mkinitcpio -P
limine-update
echo
echo "== Done. Test: reboot, then suspend, then 'systemctl hibernate'."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment