1f23027caSMaximilian Luz // SPDX-License-Identifier: GPL-2.0-only 2f23027caSMaximilian Luz /* 3f23027caSMaximilian Luz * Driver for the LID cover switch of the Surface 3 4f23027caSMaximilian Luz * 5f23027caSMaximilian Luz * Copyright (c) 2016 Red Hat Inc. 6f23027caSMaximilian Luz */ 7f23027caSMaximilian Luz 8f23027caSMaximilian Luz 9f23027caSMaximilian Luz #include <linux/kernel.h> 10f23027caSMaximilian Luz #include <linux/module.h> 11f23027caSMaximilian Luz #include <linux/slab.h> 12f23027caSMaximilian Luz 13f23027caSMaximilian Luz #include <linux/acpi.h> 14f23027caSMaximilian Luz #include <linux/dmi.h> 15f23027caSMaximilian Luz #include <linux/input.h> 16f23027caSMaximilian Luz #include <linux/mutex.h> 17f23027caSMaximilian Luz #include <linux/platform_device.h> 18f23027caSMaximilian Luz #include <linux/spi/spi.h> 19f23027caSMaximilian Luz 20f23027caSMaximilian Luz MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@redhat.com>"); 21f23027caSMaximilian Luz MODULE_DESCRIPTION("Surface 3 platform driver"); 22f23027caSMaximilian Luz MODULE_LICENSE("GPL"); 23f23027caSMaximilian Luz 24f23027caSMaximilian Luz #define ACPI_BUTTON_HID_LID "PNP0C0D" 25f23027caSMaximilian Luz #define SPI_CTL_OBJ_NAME "SPI" 26f23027caSMaximilian Luz #define SPI_TS_OBJ_NAME "NTRG" 27f23027caSMaximilian Luz 28f23027caSMaximilian Luz #define SURFACE3_LID_GUID "F7CC25EC-D20B-404C-8903-0ED4359C18AE" 29f23027caSMaximilian Luz 30f23027caSMaximilian Luz MODULE_ALIAS("wmi:" SURFACE3_LID_GUID); 31f23027caSMaximilian Luz 32f23027caSMaximilian Luz static const struct dmi_system_id surface3_dmi_table[] = { 33f23027caSMaximilian Luz #if defined(CONFIG_X86) 34f23027caSMaximilian Luz { 35f23027caSMaximilian Luz .matches = { 36f23027caSMaximilian Luz DMI_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"), 37f23027caSMaximilian Luz DMI_MATCH(DMI_PRODUCT_NAME, "Surface 3"), 38f23027caSMaximilian Luz }, 39f23027caSMaximilian Luz }, 40f23027caSMaximilian Luz #endif 41f23027caSMaximilian Luz { } 42f23027caSMaximilian Luz }; 43f23027caSMaximilian Luz 44f23027caSMaximilian Luz struct surface3_wmi { 45f23027caSMaximilian Luz struct acpi_device *touchscreen_adev; 46f23027caSMaximilian Luz struct acpi_device *pnp0c0d_adev; 47f23027caSMaximilian Luz struct acpi_hotplug_context hp; 48f23027caSMaximilian Luz struct input_dev *input; 49f23027caSMaximilian Luz }; 50f23027caSMaximilian Luz 51f23027caSMaximilian Luz static struct platform_device *s3_wmi_pdev; 52f23027caSMaximilian Luz 53f23027caSMaximilian Luz static struct surface3_wmi s3_wmi; 54f23027caSMaximilian Luz 55f23027caSMaximilian Luz static DEFINE_MUTEX(s3_wmi_lock); 56f23027caSMaximilian Luz 57f23027caSMaximilian Luz static int s3_wmi_query_block(const char *guid, int instance, int *ret) 58f23027caSMaximilian Luz { 59f23027caSMaximilian Luz struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL }; 60f807f4b7SHans de Goede union acpi_object *obj = NULL; 61f23027caSMaximilian Luz acpi_status status; 62f23027caSMaximilian Luz int error = 0; 63f23027caSMaximilian Luz 64f23027caSMaximilian Luz mutex_lock(&s3_wmi_lock); 65f23027caSMaximilian Luz status = wmi_query_block(guid, instance, &output); 66f807f4b7SHans de Goede if (ACPI_FAILURE(status)) { 67f807f4b7SHans de Goede error = -EIO; 68f807f4b7SHans de Goede goto out_free_unlock; 69f807f4b7SHans de Goede } 70f23027caSMaximilian Luz 71f23027caSMaximilian Luz obj = output.pointer; 72f23027caSMaximilian Luz 73f23027caSMaximilian Luz if (!obj || obj->type != ACPI_TYPE_INTEGER) { 74f23027caSMaximilian Luz if (obj) { 75f23027caSMaximilian Luz pr_err("query block returned object type: %d - buffer length:%d\n", 76f23027caSMaximilian Luz obj->type, 77f23027caSMaximilian Luz obj->type == ACPI_TYPE_BUFFER ? 78f23027caSMaximilian Luz obj->buffer.length : 0); 79f23027caSMaximilian Luz } 80f23027caSMaximilian Luz error = -EINVAL; 81f23027caSMaximilian Luz goto out_free_unlock; 82f23027caSMaximilian Luz } 83f23027caSMaximilian Luz *ret = obj->integer.value; 84f23027caSMaximilian Luz out_free_unlock: 85f23027caSMaximilian Luz kfree(obj); 86f23027caSMaximilian Luz mutex_unlock(&s3_wmi_lock); 87f23027caSMaximilian Luz return error; 88f23027caSMaximilian Luz } 89f23027caSMaximilian Luz 90f23027caSMaximilian Luz static inline int s3_wmi_query_lid(int *ret) 91f23027caSMaximilian Luz { 92f23027caSMaximilian Luz return s3_wmi_query_block(SURFACE3_LID_GUID, 0, ret); 93f23027caSMaximilian Luz } 94f23027caSMaximilian Luz 95f23027caSMaximilian Luz static int s3_wmi_send_lid_state(void) 96f23027caSMaximilian Luz { 97f23027caSMaximilian Luz int ret, lid_sw; 98f23027caSMaximilian Luz 99f23027caSMaximilian Luz ret = s3_wmi_query_lid(&lid_sw); 100f23027caSMaximilian Luz if (ret) 101f23027caSMaximilian Luz return ret; 102f23027caSMaximilian Luz 103f23027caSMaximilian Luz input_report_switch(s3_wmi.input, SW_LID, lid_sw); 104f23027caSMaximilian Luz input_sync(s3_wmi.input); 105f23027caSMaximilian Luz 106f23027caSMaximilian Luz return 0; 107f23027caSMaximilian Luz } 108f23027caSMaximilian Luz 109f23027caSMaximilian Luz static int s3_wmi_hp_notify(struct acpi_device *adev, u32 value) 110f23027caSMaximilian Luz { 111f23027caSMaximilian Luz return s3_wmi_send_lid_state(); 112f23027caSMaximilian Luz } 113f23027caSMaximilian Luz 114f23027caSMaximilian Luz static acpi_status s3_wmi_attach_spi_device(acpi_handle handle, 115f23027caSMaximilian Luz u32 level, 116f23027caSMaximilian Luz void *data, 117f23027caSMaximilian Luz void **return_value) 118f23027caSMaximilian Luz { 1196768bddbSRafael J. Wysocki struct acpi_device *adev = acpi_fetch_acpi_dev(handle); 1206768bddbSRafael J. Wysocki struct acpi_device **ts_adev = data; 121f23027caSMaximilian Luz 1226768bddbSRafael J. Wysocki if (!adev || strncmp(acpi_device_bid(adev), SPI_TS_OBJ_NAME, 123f23027caSMaximilian Luz strlen(SPI_TS_OBJ_NAME))) 124f23027caSMaximilian Luz return AE_OK; 125f23027caSMaximilian Luz 126f23027caSMaximilian Luz if (*ts_adev) { 127f23027caSMaximilian Luz pr_err("duplicate entry %s\n", SPI_TS_OBJ_NAME); 128f23027caSMaximilian Luz return AE_OK; 129f23027caSMaximilian Luz } 130f23027caSMaximilian Luz 131f23027caSMaximilian Luz *ts_adev = adev; 132f23027caSMaximilian Luz 133f23027caSMaximilian Luz return AE_OK; 134f23027caSMaximilian Luz } 135f23027caSMaximilian Luz 136f23027caSMaximilian Luz static int s3_wmi_check_platform_device(struct device *dev, void *data) 137f23027caSMaximilian Luz { 13855588713SRafael J. Wysocki struct acpi_device *adev = ACPI_COMPANION(dev); 13955588713SRafael J. Wysocki struct acpi_device *ts_adev = NULL; 140f23027caSMaximilian Luz acpi_status status; 141f23027caSMaximilian Luz 142f23027caSMaximilian Luz /* ignore non ACPI devices */ 14355588713SRafael J. Wysocki if (!adev) 144f23027caSMaximilian Luz return 0; 145f23027caSMaximilian Luz 146f23027caSMaximilian Luz /* check for LID ACPI switch */ 147f23027caSMaximilian Luz if (!strcmp(ACPI_BUTTON_HID_LID, acpi_device_hid(adev))) { 148f23027caSMaximilian Luz s3_wmi.pnp0c0d_adev = adev; 149f23027caSMaximilian Luz return 0; 150f23027caSMaximilian Luz } 151f23027caSMaximilian Luz 152f23027caSMaximilian Luz /* ignore non SPI controllers */ 153f23027caSMaximilian Luz if (strncmp(acpi_device_bid(adev), SPI_CTL_OBJ_NAME, 154f23027caSMaximilian Luz strlen(SPI_CTL_OBJ_NAME))) 155f23027caSMaximilian Luz return 0; 156f23027caSMaximilian Luz 15755588713SRafael J. Wysocki status = acpi_walk_namespace(ACPI_TYPE_DEVICE, adev->handle, 1, 158f23027caSMaximilian Luz s3_wmi_attach_spi_device, NULL, 159f23027caSMaximilian Luz &ts_adev, NULL); 160f23027caSMaximilian Luz if (ACPI_FAILURE(status)) 161f23027caSMaximilian Luz dev_warn(dev, "failed to enumerate SPI slaves\n"); 162f23027caSMaximilian Luz 163f23027caSMaximilian Luz if (!ts_adev) 164f23027caSMaximilian Luz return 0; 165f23027caSMaximilian Luz 166f23027caSMaximilian Luz s3_wmi.touchscreen_adev = ts_adev; 167f23027caSMaximilian Luz 168f23027caSMaximilian Luz return 0; 169f23027caSMaximilian Luz } 170f23027caSMaximilian Luz 171f23027caSMaximilian Luz static int s3_wmi_create_and_register_input(struct platform_device *pdev) 172f23027caSMaximilian Luz { 173f23027caSMaximilian Luz struct input_dev *input; 174f23027caSMaximilian Luz int error; 175f23027caSMaximilian Luz 176f23027caSMaximilian Luz input = devm_input_allocate_device(&pdev->dev); 177f23027caSMaximilian Luz if (!input) 178f23027caSMaximilian Luz return -ENOMEM; 179f23027caSMaximilian Luz 180f23027caSMaximilian Luz input->name = "Lid Switch"; 181f23027caSMaximilian Luz input->phys = "button/input0"; 182f23027caSMaximilian Luz input->id.bustype = BUS_HOST; 183f23027caSMaximilian Luz input->id.product = 0x0005; 184f23027caSMaximilian Luz 185f23027caSMaximilian Luz input_set_capability(input, EV_SW, SW_LID); 186f23027caSMaximilian Luz 187f23027caSMaximilian Luz error = input_register_device(input); 188f23027caSMaximilian Luz if (error) 189*34fc6834SChristophe JAILLET return error; 190f23027caSMaximilian Luz 191f23027caSMaximilian Luz s3_wmi.input = input; 192f23027caSMaximilian Luz 193f23027caSMaximilian Luz return 0; 194f23027caSMaximilian Luz } 195f23027caSMaximilian Luz 196f23027caSMaximilian Luz static int __init s3_wmi_probe(struct platform_device *pdev) 197f23027caSMaximilian Luz { 198f23027caSMaximilian Luz int error; 199f23027caSMaximilian Luz 200f23027caSMaximilian Luz if (!dmi_check_system(surface3_dmi_table)) 201f23027caSMaximilian Luz return -ENODEV; 202f23027caSMaximilian Luz 203f23027caSMaximilian Luz memset(&s3_wmi, 0, sizeof(s3_wmi)); 204f23027caSMaximilian Luz 205f23027caSMaximilian Luz bus_for_each_dev(&platform_bus_type, NULL, NULL, 206f23027caSMaximilian Luz s3_wmi_check_platform_device); 207f23027caSMaximilian Luz 208f23027caSMaximilian Luz if (!s3_wmi.touchscreen_adev) 209f23027caSMaximilian Luz return -ENODEV; 210f23027caSMaximilian Luz 211f23027caSMaximilian Luz acpi_bus_trim(s3_wmi.pnp0c0d_adev); 212f23027caSMaximilian Luz 213f23027caSMaximilian Luz error = s3_wmi_create_and_register_input(pdev); 214f23027caSMaximilian Luz if (error) 215f23027caSMaximilian Luz goto restore_acpi_lid; 216f23027caSMaximilian Luz 217f23027caSMaximilian Luz acpi_initialize_hp_context(s3_wmi.touchscreen_adev, &s3_wmi.hp, 218f23027caSMaximilian Luz s3_wmi_hp_notify, NULL); 219f23027caSMaximilian Luz 220f23027caSMaximilian Luz s3_wmi_send_lid_state(); 221f23027caSMaximilian Luz 222f23027caSMaximilian Luz return 0; 223f23027caSMaximilian Luz 224f23027caSMaximilian Luz restore_acpi_lid: 225f23027caSMaximilian Luz acpi_bus_scan(s3_wmi.pnp0c0d_adev->handle); 226f23027caSMaximilian Luz return error; 227f23027caSMaximilian Luz } 228f23027caSMaximilian Luz 229f23027caSMaximilian Luz static int s3_wmi_remove(struct platform_device *device) 230f23027caSMaximilian Luz { 231f23027caSMaximilian Luz /* remove the hotplug context from the acpi device */ 232f23027caSMaximilian Luz s3_wmi.touchscreen_adev->hp = NULL; 233f23027caSMaximilian Luz 234f23027caSMaximilian Luz /* reinstall the actual PNPC0C0D LID default handle */ 235f23027caSMaximilian Luz acpi_bus_scan(s3_wmi.pnp0c0d_adev->handle); 236f23027caSMaximilian Luz return 0; 237f23027caSMaximilian Luz } 238f23027caSMaximilian Luz 239f23027caSMaximilian Luz static int __maybe_unused s3_wmi_resume(struct device *dev) 240f23027caSMaximilian Luz { 241f23027caSMaximilian Luz s3_wmi_send_lid_state(); 242f23027caSMaximilian Luz return 0; 243f23027caSMaximilian Luz } 244f23027caSMaximilian Luz static SIMPLE_DEV_PM_OPS(s3_wmi_pm, NULL, s3_wmi_resume); 245f23027caSMaximilian Luz 246f23027caSMaximilian Luz static struct platform_driver s3_wmi_driver = { 247f23027caSMaximilian Luz .driver = { 248f23027caSMaximilian Luz .name = "surface3-wmi", 249f23027caSMaximilian Luz .pm = &s3_wmi_pm, 250f23027caSMaximilian Luz }, 251f23027caSMaximilian Luz .remove = s3_wmi_remove, 252f23027caSMaximilian Luz }; 253f23027caSMaximilian Luz 254f23027caSMaximilian Luz static int __init s3_wmi_init(void) 255f23027caSMaximilian Luz { 256f23027caSMaximilian Luz int error; 257f23027caSMaximilian Luz 258f23027caSMaximilian Luz s3_wmi_pdev = platform_device_alloc("surface3-wmi", -1); 259f23027caSMaximilian Luz if (!s3_wmi_pdev) 260f23027caSMaximilian Luz return -ENOMEM; 261f23027caSMaximilian Luz 262f23027caSMaximilian Luz error = platform_device_add(s3_wmi_pdev); 263f23027caSMaximilian Luz if (error) 264f23027caSMaximilian Luz goto err_device_put; 265f23027caSMaximilian Luz 266f23027caSMaximilian Luz error = platform_driver_probe(&s3_wmi_driver, s3_wmi_probe); 267f23027caSMaximilian Luz if (error) 268f23027caSMaximilian Luz goto err_device_del; 269f23027caSMaximilian Luz 270f23027caSMaximilian Luz pr_info("Surface 3 WMI Extras loaded\n"); 271f23027caSMaximilian Luz return 0; 272f23027caSMaximilian Luz 273f23027caSMaximilian Luz err_device_del: 274f23027caSMaximilian Luz platform_device_del(s3_wmi_pdev); 275f23027caSMaximilian Luz err_device_put: 276f23027caSMaximilian Luz platform_device_put(s3_wmi_pdev); 277f23027caSMaximilian Luz return error; 278f23027caSMaximilian Luz } 279f23027caSMaximilian Luz 280f23027caSMaximilian Luz static void __exit s3_wmi_exit(void) 281f23027caSMaximilian Luz { 282f23027caSMaximilian Luz platform_device_unregister(s3_wmi_pdev); 283f23027caSMaximilian Luz platform_driver_unregister(&s3_wmi_driver); 284f23027caSMaximilian Luz } 285f23027caSMaximilian Luz 286f23027caSMaximilian Luz module_init(s3_wmi_init); 287f23027caSMaximilian Luz module_exit(s3_wmi_exit); 288