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 { 119f23027caSMaximilian Luz struct acpi_device *adev, **ts_adev; 120f23027caSMaximilian Luz 121f23027caSMaximilian Luz if (acpi_bus_get_device(handle, &adev)) 122f23027caSMaximilian Luz return AE_OK; 123f23027caSMaximilian Luz 124f23027caSMaximilian Luz ts_adev = data; 125f23027caSMaximilian Luz 126f23027caSMaximilian Luz if (strncmp(acpi_device_bid(adev), SPI_TS_OBJ_NAME, 127f23027caSMaximilian Luz strlen(SPI_TS_OBJ_NAME))) 128f23027caSMaximilian Luz return AE_OK; 129f23027caSMaximilian Luz 130f23027caSMaximilian Luz if (*ts_adev) { 131f23027caSMaximilian Luz pr_err("duplicate entry %s\n", SPI_TS_OBJ_NAME); 132f23027caSMaximilian Luz return AE_OK; 133f23027caSMaximilian Luz } 134f23027caSMaximilian Luz 135f23027caSMaximilian Luz *ts_adev = adev; 136f23027caSMaximilian Luz 137f23027caSMaximilian Luz return AE_OK; 138f23027caSMaximilian Luz } 139f23027caSMaximilian Luz 140f23027caSMaximilian Luz static int s3_wmi_check_platform_device(struct device *dev, void *data) 141f23027caSMaximilian Luz { 142*55588713SRafael J. Wysocki struct acpi_device *adev = ACPI_COMPANION(dev); 143*55588713SRafael J. Wysocki struct acpi_device *ts_adev = NULL; 144f23027caSMaximilian Luz acpi_status status; 145f23027caSMaximilian Luz 146f23027caSMaximilian Luz /* ignore non ACPI devices */ 147*55588713SRafael J. Wysocki if (!adev) 148f23027caSMaximilian Luz return 0; 149f23027caSMaximilian Luz 150f23027caSMaximilian Luz /* check for LID ACPI switch */ 151f23027caSMaximilian Luz if (!strcmp(ACPI_BUTTON_HID_LID, acpi_device_hid(adev))) { 152f23027caSMaximilian Luz s3_wmi.pnp0c0d_adev = adev; 153f23027caSMaximilian Luz return 0; 154f23027caSMaximilian Luz } 155f23027caSMaximilian Luz 156f23027caSMaximilian Luz /* ignore non SPI controllers */ 157f23027caSMaximilian Luz if (strncmp(acpi_device_bid(adev), SPI_CTL_OBJ_NAME, 158f23027caSMaximilian Luz strlen(SPI_CTL_OBJ_NAME))) 159f23027caSMaximilian Luz return 0; 160f23027caSMaximilian Luz 161*55588713SRafael J. Wysocki status = acpi_walk_namespace(ACPI_TYPE_DEVICE, adev->handle, 1, 162f23027caSMaximilian Luz s3_wmi_attach_spi_device, NULL, 163f23027caSMaximilian Luz &ts_adev, NULL); 164f23027caSMaximilian Luz if (ACPI_FAILURE(status)) 165f23027caSMaximilian Luz dev_warn(dev, "failed to enumerate SPI slaves\n"); 166f23027caSMaximilian Luz 167f23027caSMaximilian Luz if (!ts_adev) 168f23027caSMaximilian Luz return 0; 169f23027caSMaximilian Luz 170f23027caSMaximilian Luz s3_wmi.touchscreen_adev = ts_adev; 171f23027caSMaximilian Luz 172f23027caSMaximilian Luz return 0; 173f23027caSMaximilian Luz } 174f23027caSMaximilian Luz 175f23027caSMaximilian Luz static int s3_wmi_create_and_register_input(struct platform_device *pdev) 176f23027caSMaximilian Luz { 177f23027caSMaximilian Luz struct input_dev *input; 178f23027caSMaximilian Luz int error; 179f23027caSMaximilian Luz 180f23027caSMaximilian Luz input = devm_input_allocate_device(&pdev->dev); 181f23027caSMaximilian Luz if (!input) 182f23027caSMaximilian Luz return -ENOMEM; 183f23027caSMaximilian Luz 184f23027caSMaximilian Luz input->name = "Lid Switch"; 185f23027caSMaximilian Luz input->phys = "button/input0"; 186f23027caSMaximilian Luz input->id.bustype = BUS_HOST; 187f23027caSMaximilian Luz input->id.product = 0x0005; 188f23027caSMaximilian Luz 189f23027caSMaximilian Luz input_set_capability(input, EV_SW, SW_LID); 190f23027caSMaximilian Luz 191f23027caSMaximilian Luz error = input_register_device(input); 192f23027caSMaximilian Luz if (error) 193f23027caSMaximilian Luz goto out_err; 194f23027caSMaximilian Luz 195f23027caSMaximilian Luz s3_wmi.input = input; 196f23027caSMaximilian Luz 197f23027caSMaximilian Luz return 0; 198f23027caSMaximilian Luz out_err: 199f23027caSMaximilian Luz input_free_device(s3_wmi.input); 200f23027caSMaximilian Luz return error; 201f23027caSMaximilian Luz } 202f23027caSMaximilian Luz 203f23027caSMaximilian Luz static int __init s3_wmi_probe(struct platform_device *pdev) 204f23027caSMaximilian Luz { 205f23027caSMaximilian Luz int error; 206f23027caSMaximilian Luz 207f23027caSMaximilian Luz if (!dmi_check_system(surface3_dmi_table)) 208f23027caSMaximilian Luz return -ENODEV; 209f23027caSMaximilian Luz 210f23027caSMaximilian Luz memset(&s3_wmi, 0, sizeof(s3_wmi)); 211f23027caSMaximilian Luz 212f23027caSMaximilian Luz bus_for_each_dev(&platform_bus_type, NULL, NULL, 213f23027caSMaximilian Luz s3_wmi_check_platform_device); 214f23027caSMaximilian Luz 215f23027caSMaximilian Luz if (!s3_wmi.touchscreen_adev) 216f23027caSMaximilian Luz return -ENODEV; 217f23027caSMaximilian Luz 218f23027caSMaximilian Luz acpi_bus_trim(s3_wmi.pnp0c0d_adev); 219f23027caSMaximilian Luz 220f23027caSMaximilian Luz error = s3_wmi_create_and_register_input(pdev); 221f23027caSMaximilian Luz if (error) 222f23027caSMaximilian Luz goto restore_acpi_lid; 223f23027caSMaximilian Luz 224f23027caSMaximilian Luz acpi_initialize_hp_context(s3_wmi.touchscreen_adev, &s3_wmi.hp, 225f23027caSMaximilian Luz s3_wmi_hp_notify, NULL); 226f23027caSMaximilian Luz 227f23027caSMaximilian Luz s3_wmi_send_lid_state(); 228f23027caSMaximilian Luz 229f23027caSMaximilian Luz return 0; 230f23027caSMaximilian Luz 231f23027caSMaximilian Luz restore_acpi_lid: 232f23027caSMaximilian Luz acpi_bus_scan(s3_wmi.pnp0c0d_adev->handle); 233f23027caSMaximilian Luz return error; 234f23027caSMaximilian Luz } 235f23027caSMaximilian Luz 236f23027caSMaximilian Luz static int s3_wmi_remove(struct platform_device *device) 237f23027caSMaximilian Luz { 238f23027caSMaximilian Luz /* remove the hotplug context from the acpi device */ 239f23027caSMaximilian Luz s3_wmi.touchscreen_adev->hp = NULL; 240f23027caSMaximilian Luz 241f23027caSMaximilian Luz /* reinstall the actual PNPC0C0D LID default handle */ 242f23027caSMaximilian Luz acpi_bus_scan(s3_wmi.pnp0c0d_adev->handle); 243f23027caSMaximilian Luz return 0; 244f23027caSMaximilian Luz } 245f23027caSMaximilian Luz 246f23027caSMaximilian Luz static int __maybe_unused s3_wmi_resume(struct device *dev) 247f23027caSMaximilian Luz { 248f23027caSMaximilian Luz s3_wmi_send_lid_state(); 249f23027caSMaximilian Luz return 0; 250f23027caSMaximilian Luz } 251f23027caSMaximilian Luz static SIMPLE_DEV_PM_OPS(s3_wmi_pm, NULL, s3_wmi_resume); 252f23027caSMaximilian Luz 253f23027caSMaximilian Luz static struct platform_driver s3_wmi_driver = { 254f23027caSMaximilian Luz .driver = { 255f23027caSMaximilian Luz .name = "surface3-wmi", 256f23027caSMaximilian Luz .pm = &s3_wmi_pm, 257f23027caSMaximilian Luz }, 258f23027caSMaximilian Luz .remove = s3_wmi_remove, 259f23027caSMaximilian Luz }; 260f23027caSMaximilian Luz 261f23027caSMaximilian Luz static int __init s3_wmi_init(void) 262f23027caSMaximilian Luz { 263f23027caSMaximilian Luz int error; 264f23027caSMaximilian Luz 265f23027caSMaximilian Luz s3_wmi_pdev = platform_device_alloc("surface3-wmi", -1); 266f23027caSMaximilian Luz if (!s3_wmi_pdev) 267f23027caSMaximilian Luz return -ENOMEM; 268f23027caSMaximilian Luz 269f23027caSMaximilian Luz error = platform_device_add(s3_wmi_pdev); 270f23027caSMaximilian Luz if (error) 271f23027caSMaximilian Luz goto err_device_put; 272f23027caSMaximilian Luz 273f23027caSMaximilian Luz error = platform_driver_probe(&s3_wmi_driver, s3_wmi_probe); 274f23027caSMaximilian Luz if (error) 275f23027caSMaximilian Luz goto err_device_del; 276f23027caSMaximilian Luz 277f23027caSMaximilian Luz pr_info("Surface 3 WMI Extras loaded\n"); 278f23027caSMaximilian Luz return 0; 279f23027caSMaximilian Luz 280f23027caSMaximilian Luz err_device_del: 281f23027caSMaximilian Luz platform_device_del(s3_wmi_pdev); 282f23027caSMaximilian Luz err_device_put: 283f23027caSMaximilian Luz platform_device_put(s3_wmi_pdev); 284f23027caSMaximilian Luz return error; 285f23027caSMaximilian Luz } 286f23027caSMaximilian Luz 287f23027caSMaximilian Luz static void __exit s3_wmi_exit(void) 288f23027caSMaximilian Luz { 289f23027caSMaximilian Luz platform_device_unregister(s3_wmi_pdev); 290f23027caSMaximilian Luz platform_driver_unregister(&s3_wmi_driver); 291f23027caSMaximilian Luz } 292f23027caSMaximilian Luz 293f23027caSMaximilian Luz module_init(s3_wmi_init); 294f23027caSMaximilian Luz module_exit(s3_wmi_exit); 295