|
| 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/module.h> |
| 15 | +#include <linux/reboot.h> |
| 16 | + |
| 17 | +/** |
| 18 | + * struct macsmc_input |
| 19 | + * @dev: Underlying struct device for the input sub-device |
| 20 | + * @smc: Pointer to apple_smc struct of the mfd parent |
| 21 | + * @input: Allocated input_dev; devres managed |
| 22 | + * @nb: Notifier block used for incoming events from SMC (e.g. button pressed down) |
| 23 | + * @wakeup_mode: Set to true when system is suspended and power button events should wake it |
| 24 | + */ |
| 25 | +struct macsmc_input { |
| 26 | + struct device *dev; |
| 27 | + struct apple_smc *smc; |
| 28 | + struct input_dev *input; |
| 29 | + struct notifier_block nb; |
| 30 | + bool wakeup_mode; |
| 31 | +}; |
| 32 | + |
| 33 | +#define SMC_EV_BTN 0x7201 |
| 34 | +#define SMC_EV_LID 0x7203 |
| 35 | + |
| 36 | +#define BTN_POWER 0x01 /* power button on e.g. Mac Mini chasis pressed */ |
| 37 | +#define BTN_TOUCHID 0x06 /* combined TouchID / power button on MacBooks pressed */ |
| 38 | +#define BTN_POWER_HELD_SHORT 0xfe /* power button briefly held down */ |
| 39 | +#define BTN_POWER_HELD_LONG 0x00 /* power button held down; sent just before forced poweroff */ |
| 40 | + |
| 41 | +static void macsmc_input_event_button(struct macsmc_input *smcin, unsigned long event) |
| 42 | +{ |
| 43 | + u8 button = (event >> 8) & 0xff; |
| 44 | + u8 state = !!(event & 0xff); |
| 45 | + |
| 46 | + switch (button) { |
| 47 | + case BTN_POWER: |
| 48 | + case BTN_TOUCHID: |
| 49 | + if (smcin->wakeup_mode) { |
| 50 | + if (state) |
| 51 | + pm_wakeup_event(smcin->dev, 0); |
| 52 | + } else { |
| 53 | + input_report_key(smcin->input, KEY_POWER, state); |
| 54 | + input_sync(smcin->input); |
| 55 | + } |
| 56 | + break; |
| 57 | + case BTN_POWER_HELD_SHORT: /* power button held down; ignore */ |
| 58 | + break; |
| 59 | + case BTN_POWER_HELD_LONG: |
| 60 | + /* |
| 61 | + * If we get here the power button has been held down for a while and |
| 62 | + * we have about 4 seconds before forced power-off is triggered by SMC. |
| 63 | + * Try to do an emergency shutdown to make sure the NVMe cache is |
| 64 | + * flushed. macOS actually does this by panicing (!)... |
| 65 | + */ |
| 66 | + if (state) { |
| 67 | + dev_crit(smcin->dev, "Triggering forced shutdown!\n"); |
| 68 | + if (kernel_can_power_off()) |
| 69 | + kernel_power_off(); |
| 70 | + else /* Missing macsmc-reboot driver? */ |
| 71 | + kernel_restart("SMC power button triggered restart"); |
| 72 | + } |
| 73 | + break; |
| 74 | + default: |
| 75 | + dev_warn(smcin->dev, "Unknown SMC button event: %04lx\n", event & 0xffff); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +static void macsmc_input_event_lid(struct macsmc_input *smcin, unsigned long event) |
| 80 | +{ |
| 81 | + u8 lid_state = !!((event >> 8) & 0xff); |
| 82 | + |
| 83 | + if (smcin->wakeup_mode && !lid_state) |
| 84 | + pm_wakeup_event(smcin->dev, 0); |
| 85 | + |
| 86 | + input_report_switch(smcin->input, SW_LID, lid_state); |
| 87 | + input_sync(smcin->input); |
| 88 | +} |
| 89 | + |
| 90 | +static int macsmc_input_event(struct notifier_block *nb, unsigned long event, void *data) |
| 91 | +{ |
| 92 | + struct macsmc_input *smcin = container_of(nb, struct macsmc_input, nb); |
| 93 | + u16 type = event >> 16; |
| 94 | + |
| 95 | + switch (type) { |
| 96 | + case SMC_EV_BTN: |
| 97 | + macsmc_input_event_button(smcin, event); |
| 98 | + return NOTIFY_OK; |
| 99 | + case SMC_EV_LID: |
| 100 | + macsmc_input_event_lid(smcin, event); |
| 101 | + return NOTIFY_OK; |
| 102 | + default: |
| 103 | + /* SMC event meant for another driver */ |
| 104 | + return NOTIFY_DONE; |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +static int macsmc_input_probe(struct platform_device *pdev) |
| 109 | +{ |
| 110 | + struct apple_smc *smc = dev_get_drvdata(pdev->dev.parent); |
| 111 | + struct macsmc_input *smcin; |
| 112 | + bool have_lid, have_power; |
| 113 | + int error; |
| 114 | + |
| 115 | + /* Bail early if this SMC neither supports power button nor lid events */ |
| 116 | + have_lid = apple_smc_key_exists(smc, SMC_KEY(MSLD)); |
| 117 | + have_power = apple_smc_key_exists(smc, SMC_KEY(bHLD)); |
| 118 | + if (!have_lid && !have_power) |
| 119 | + return -ENODEV; |
| 120 | + |
| 121 | + smcin = devm_kzalloc(&pdev->dev, sizeof(*smcin), GFP_KERNEL); |
| 122 | + if (!smcin) |
| 123 | + return -ENOMEM; |
| 124 | + |
| 125 | + smcin->dev = &pdev->dev; |
| 126 | + smcin->smc = smc; |
| 127 | + platform_set_drvdata(pdev, smcin); |
| 128 | + |
| 129 | + smcin->input = devm_input_allocate_device(&pdev->dev); |
| 130 | + if (!smcin->input) |
| 131 | + return -ENOMEM; |
| 132 | + |
| 133 | + smcin->input->phys = "macsmc-input (0)"; |
| 134 | + smcin->input->name = "Apple SMC power/lid events"; |
| 135 | + |
| 136 | + if (have_lid) |
| 137 | + input_set_capability(smcin->input, EV_SW, SW_LID); |
| 138 | + if (have_power) |
| 139 | + input_set_capability(smcin->input, EV_KEY, KEY_POWER); |
| 140 | + |
| 141 | + if (have_lid) { |
| 142 | + u8 val; |
| 143 | + |
| 144 | + error = apple_smc_read_u8(smc, SMC_KEY(MSLD), &val); |
| 145 | + if (error < 0) |
| 146 | + dev_warn(&pdev->dev, "Failed to read initial lid state\n"); |
| 147 | + else |
| 148 | + input_report_switch(smcin->input, SW_LID, val); |
| 149 | + } |
| 150 | + |
| 151 | + if (have_power) { |
| 152 | + u32 val; |
| 153 | + |
| 154 | + error = apple_smc_read_u32(smc, SMC_KEY(bHLD), &val); |
| 155 | + if (error < 0) |
| 156 | + dev_warn(&pdev->dev, "Failed to read initial power button state\n"); |
| 157 | + else |
| 158 | + input_report_key(smcin->input, KEY_POWER, val & 1); |
| 159 | + } |
| 160 | + |
| 161 | + error = input_register_device(smcin->input); |
| 162 | + if (error) { |
| 163 | + dev_err(&pdev->dev, "Failed to register input device: %d\n", error); |
| 164 | + return error; |
| 165 | + } |
| 166 | + |
| 167 | + input_sync(smcin->input); |
| 168 | + |
| 169 | + smcin->nb.notifier_call = macsmc_input_event; |
| 170 | + blocking_notifier_chain_register(&smc->event_handlers, &smcin->nb); |
| 171 | + |
| 172 | + device_init_wakeup(&pdev->dev, 1); |
| 173 | + |
| 174 | + return 0; |
| 175 | +} |
| 176 | + |
| 177 | +static int macsmc_input_pm_prepare(struct device *dev) |
| 178 | +{ |
| 179 | + struct macsmc_input *smcin = dev_get_drvdata(dev); |
| 180 | + |
| 181 | + smcin->wakeup_mode = true; |
| 182 | + return 0; |
| 183 | +} |
| 184 | + |
| 185 | +static void macsmc_input_pm_complete(struct device *dev) |
| 186 | +{ |
| 187 | + struct macsmc_input *smcin = dev_get_drvdata(dev); |
| 188 | + |
| 189 | + smcin->wakeup_mode = false; |
| 190 | +} |
| 191 | + |
| 192 | +static const struct dev_pm_ops macsmc_input_pm_ops = { |
| 193 | + .prepare = macsmc_input_pm_prepare, |
| 194 | + .complete = macsmc_input_pm_complete, |
| 195 | +}; |
| 196 | + |
| 197 | +static struct platform_driver macsmc_input_driver = { |
| 198 | + .driver = { |
| 199 | + .name = "macsmc-input", |
| 200 | + .pm = &macsmc_input_pm_ops, |
| 201 | + }, |
| 202 | + .probe = macsmc_input_probe, |
| 203 | +}; |
| 204 | +module_platform_driver(macsmc_input_driver); |
| 205 | + |
| 206 | +MODULE_AUTHOR( "Hector Martin <[email protected]>"); |
| 207 | +MODULE_LICENSE("Dual MIT/GPL"); |
| 208 | +MODULE_DESCRIPTION("Apple SMC input driver"); |
0 commit comments