Skip to content

Commit 6f14ae4

Browse files
committed
Input: macsmc-hid: New driver to handle the Apple Mac SMC buttons/lid
This driver implements power button and lid switch support for Apple Mac devices using SMC controllers driven by the macsmc driver. In addition to basic input support, this also responds to the final shutdown warning (when the power button is held down long enough) by doing an emergency kernel poweroff. This allows the NVMe controller to be cleanly shut down, which prevents data loss for in-cache data. Signed-off-by: Hector Martin <[email protected]>
1 parent 4330080 commit 6f14ae4

3 files changed

Lines changed: 170 additions & 0 deletions

File tree

drivers/input/misc/Kconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,4 +928,16 @@ config INPUT_STPMIC1_ONKEY
928928
To compile this driver as a module, choose M here: the
929929
module will be called stpmic1_onkey.
930930

931+
config INPUT_MACSMC_HID
932+
tristate "Apple Mac SMC lid/buttons"
933+
depends on APPLE_SMC
934+
default ARCH_APPLE
935+
help
936+
Say Y here if you want to use the input events delivered via the
937+
SMC controller on Apple Mac machines using the macsmc driver.
938+
This includes lid open/close and the power button.
939+
940+
To compile this driver as a module, choose M here: the
941+
module will be called macsmc-hid.
942+
931943
endif

drivers/input/misc/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ obj-$(CONFIG_INPUT_IQS7222) += iqs7222.o
4848
obj-$(CONFIG_INPUT_KEYSPAN_REMOTE) += keyspan_remote.o
4949
obj-$(CONFIG_INPUT_KXTJ9) += kxtj9.o
5050
obj-$(CONFIG_INPUT_M68K_BEEP) += m68kspkr.o
51+
obj-$(CONFIG_INPUT_MACSMC_HID) += macsmc-hid.o
5152
obj-$(CONFIG_INPUT_MAX77650_ONKEY) += max77650-onkey.o
5253
obj-$(CONFIG_INPUT_MAX77693_HAPTIC) += max77693-haptic.o
5354
obj-$(CONFIG_INPUT_MAX8925_ONKEY) += max8925_onkey.o

drivers/input/misc/macsmc-hid.c

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
// SPDX-License-Identifier: GPL-2.0-only OR MIT
2+
/*
3+
* Apple SMC input event driver
4+
* Copyright The Asahi Linux Contributors
5+
*
6+
* This driver exposes HID events from the SMC as an input device.
7+
* This includes the lid open/close and power button notifications.
8+
*/
9+
10+
#include <linux/device.h>
11+
#include <linux/input.h>
12+
#include <linux/mfd/core.h>
13+
#include <linux/mfd/macsmc.h>
14+
#include <linux/reboot.h>
15+
16+
struct macsmc_hid {
17+
struct device *dev;
18+
struct apple_smc *smc;
19+
struct input_dev *input;
20+
struct notifier_block nb;
21+
};
22+
23+
#define SMC_EV_BTN 0x7201
24+
#define SMC_EV_LID 0x7203
25+
26+
#define BTN_POWER 0x06
27+
#define BTN_POWER_HELD1 0xfe
28+
#define BTN_POWER_HELD2 0x00
29+
30+
static int macsmc_hid_event(struct notifier_block *nb, unsigned long event, void *data)
31+
{
32+
struct macsmc_hid *smchid = container_of(nb, struct macsmc_hid, nb);
33+
u16 type = event >> 16;
34+
u8 d1 = (event >> 8) & 0xff;
35+
u8 d2 = event & 0xff;
36+
37+
switch (type) {
38+
case SMC_EV_BTN:
39+
switch (d1) {
40+
case BTN_POWER:
41+
input_report_key(smchid->input, KEY_POWER, d2);
42+
input_sync(smchid->input);
43+
break;
44+
case BTN_POWER_HELD1:
45+
/*
46+
* TODO: is this pre-warning useful?
47+
*/
48+
if (d2)
49+
dev_warn(smchid->dev, "Power button held down\n");
50+
break;
51+
case BTN_POWER_HELD2:
52+
/*
53+
* If we get here, we have about 4 seconds before forced shutdown.
54+
* Try to do an emergency shutdown to make sure the NVMe cache is
55+
* flushed. macOS actually does this by panicing (!)...
56+
*/
57+
if (d2) {
58+
dev_crit(smchid->dev, "Triggering forced shutdown!\n");
59+
if (kernel_can_power_off())
60+
kernel_power_off();
61+
else /* Missing macsmc-reboot driver? */
62+
kernel_restart("SMC power button triggered restart");
63+
}
64+
break;
65+
default:
66+
dev_info(smchid->dev, "Unknown SMC button event: %02x %02x\n", d1, d2);
67+
break;
68+
}
69+
return NOTIFY_OK;
70+
case SMC_EV_LID:
71+
input_report_switch(smchid->input, SW_LID, d1);
72+
input_sync(smchid->input);
73+
return NOTIFY_OK;
74+
}
75+
76+
return NOTIFY_DONE;
77+
}
78+
79+
static int macsmc_hid_probe(struct platform_device *pdev)
80+
{
81+
struct apple_smc *smc = dev_get_drvdata(pdev->dev.parent);
82+
struct macsmc_hid *smchid;
83+
bool have_lid, have_power;
84+
int ret;
85+
86+
have_lid = apple_smc_key_exists(smc, SMC_KEY(MSLD));
87+
have_power = apple_smc_key_exists(smc, SMC_KEY(bHLD));
88+
89+
if (!have_lid && !have_power)
90+
return -ENODEV;
91+
92+
smchid = devm_kzalloc(&pdev->dev, sizeof(*smchid), GFP_KERNEL);
93+
if (!smchid)
94+
return -ENOMEM;
95+
96+
smchid->dev = &pdev->dev;
97+
smchid->smc = smc;
98+
99+
smchid->input = devm_input_allocate_device(&pdev->dev);
100+
if (!smchid->input)
101+
return -ENOMEM;
102+
103+
smchid->input->phys = "macsmc-hid (0)";
104+
smchid->input->name = "Apple SMC power/lid events";
105+
106+
if (have_lid)
107+
input_set_capability(smchid->input, EV_SW, SW_LID);
108+
if (have_power)
109+
input_set_capability(smchid->input, EV_KEY, KEY_POWER);
110+
111+
ret = input_register_device(smchid->input);
112+
if (ret) {
113+
dev_err(&pdev->dev, "Failed to register input device: %d\n", ret);
114+
return ret;
115+
}
116+
117+
if (have_lid) {
118+
u8 val;
119+
120+
ret = apple_smc_read_u8(smc, SMC_KEY(MSLD), &val);
121+
if (ret < 0) {
122+
dev_err(&pdev->dev, "Failed to read initial lid state\n");
123+
} else {
124+
input_report_switch(smchid->input, SW_LID, val);
125+
}
126+
}
127+
if (have_power) {
128+
u32 val;
129+
130+
ret = apple_smc_read_u32(smc, SMC_KEY(bHLD), &val);
131+
if (ret < 0) {
132+
dev_err(&pdev->dev, "Failed to read initial power button state\n");
133+
} else {
134+
input_report_key(smchid->input, KEY_POWER, val & 1);
135+
}
136+
}
137+
138+
input_sync(smchid->input);
139+
140+
smchid->nb.notifier_call = macsmc_hid_event;
141+
apple_smc_register_notifier(smc, &smchid->nb);
142+
143+
return 0;
144+
}
145+
146+
static struct platform_driver macsmc_hid_driver = {
147+
.driver = {
148+
.name = "macsmc-hid",
149+
},
150+
.probe = macsmc_hid_probe,
151+
};
152+
module_platform_driver(macsmc_hid_driver);
153+
154+
MODULE_AUTHOR("Hector Martin <[email protected]>");
155+
MODULE_LICENSE("Dual MIT/GPL");
156+
MODULE_DESCRIPTION("Apple SMC GPIO driver");
157+
MODULE_ALIAS("platform:macsmc-hid");

0 commit comments

Comments
 (0)