1e82882cdSGergo Koteles // SPDX-License-Identifier: GPL-2.0-or-later 2e82882cdSGergo Koteles /* 3e82882cdSGergo Koteles * lenovo-ymc.c - Lenovo Yoga Mode Control driver 4e82882cdSGergo Koteles * 5e82882cdSGergo Koteles * Copyright © 2022 Gergo Koteles <soyer@irl.hu> 6e82882cdSGergo Koteles */ 7e82882cdSGergo Koteles 8e82882cdSGergo Koteles #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 9e82882cdSGergo Koteles 10e82882cdSGergo Koteles #include <linux/acpi.h> 11e82882cdSGergo Koteles #include <linux/dmi.h> 12e82882cdSGergo Koteles #include <linux/input.h> 13e82882cdSGergo Koteles #include <linux/input/sparse-keymap.h> 14e82882cdSGergo Koteles #include <linux/wmi.h> 15e82882cdSGergo Koteles #include "ideapad-laptop.h" 16e82882cdSGergo Koteles 17e82882cdSGergo Koteles #define LENOVO_YMC_EVENT_GUID "06129D99-6083-4164-81AD-F092F9D773A6" 18e82882cdSGergo Koteles #define LENOVO_YMC_QUERY_GUID "09B0EE6E-C3FD-4243-8DA1-7911FF80BB8C" 19e82882cdSGergo Koteles 20e82882cdSGergo Koteles #define LENOVO_YMC_QUERY_INSTANCE 0 21e82882cdSGergo Koteles #define LENOVO_YMC_QUERY_METHOD 0x01 22e82882cdSGergo Koteles 23e82882cdSGergo Koteles static bool ec_trigger __read_mostly; 24e82882cdSGergo Koteles module_param(ec_trigger, bool, 0444); 25e82882cdSGergo Koteles MODULE_PARM_DESC(ec_trigger, "Enable EC triggering work-around to force emitting tablet mode events"); 26e82882cdSGergo Koteles 272b6aa661SHans de Goede static bool force; 282b6aa661SHans de Goede module_param(force, bool, 0444); 292b6aa661SHans de Goede MODULE_PARM_DESC(force, "Force loading on boards without a convertible DMI chassis-type"); 302b6aa661SHans de Goede 31e82882cdSGergo Koteles static const struct dmi_system_id ec_trigger_quirk_dmi_table[] = { 32e82882cdSGergo Koteles { 33e82882cdSGergo Koteles /* Lenovo Yoga 7 14ARB7 */ 34e82882cdSGergo Koteles .matches = { 35e82882cdSGergo Koteles DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"), 36e82882cdSGergo Koteles DMI_MATCH(DMI_PRODUCT_NAME, "82QF"), 37e82882cdSGergo Koteles }, 38e82882cdSGergo Koteles }, 39*db35610aSSwapnil Devesh { 40*db35610aSSwapnil Devesh /* Lenovo Yoga 7 14ACN6 */ 41*db35610aSSwapnil Devesh .matches = { 42*db35610aSSwapnil Devesh DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"), 43*db35610aSSwapnil Devesh DMI_MATCH(DMI_PRODUCT_NAME, "82N7"), 44*db35610aSSwapnil Devesh }, 45*db35610aSSwapnil Devesh }, 46e82882cdSGergo Koteles { } 47e82882cdSGergo Koteles }; 48e82882cdSGergo Koteles 492b6aa661SHans de Goede static const struct dmi_system_id allowed_chasis_types_dmi_table[] = { 502b6aa661SHans de Goede { 512b6aa661SHans de Goede .matches = { 522b6aa661SHans de Goede DMI_EXACT_MATCH(DMI_CHASSIS_TYPE, "31" /* Convertible */), 532b6aa661SHans de Goede }, 542b6aa661SHans de Goede }, 552b6aa661SHans de Goede { 562b6aa661SHans de Goede .matches = { 572b6aa661SHans de Goede DMI_EXACT_MATCH(DMI_CHASSIS_TYPE, "32" /* Detachable */), 582b6aa661SHans de Goede }, 592b6aa661SHans de Goede }, 602b6aa661SHans de Goede { } 612b6aa661SHans de Goede }; 622b6aa661SHans de Goede 63e82882cdSGergo Koteles struct lenovo_ymc_private { 64e82882cdSGergo Koteles struct input_dev *input_dev; 65e82882cdSGergo Koteles struct acpi_device *ec_acpi_dev; 66e82882cdSGergo Koteles }; 67e82882cdSGergo Koteles 68e82882cdSGergo Koteles static void lenovo_ymc_trigger_ec(struct wmi_device *wdev, struct lenovo_ymc_private *priv) 69e82882cdSGergo Koteles { 70e82882cdSGergo Koteles int err; 71e82882cdSGergo Koteles 72e82882cdSGergo Koteles if (!priv->ec_acpi_dev) 73e82882cdSGergo Koteles return; 74e82882cdSGergo Koteles 75e82882cdSGergo Koteles err = write_ec_cmd(priv->ec_acpi_dev->handle, VPCCMD_W_YMC, 1); 76e82882cdSGergo Koteles if (err) 77e82882cdSGergo Koteles dev_warn(&wdev->dev, "Could not write YMC: %d\n", err); 78e82882cdSGergo Koteles } 79e82882cdSGergo Koteles 80e82882cdSGergo Koteles static const struct key_entry lenovo_ymc_keymap[] = { 81e82882cdSGergo Koteles /* Laptop */ 82e82882cdSGergo Koteles { KE_SW, 0x01, { .sw = { SW_TABLET_MODE, 0 } } }, 83e82882cdSGergo Koteles /* Tablet */ 84e82882cdSGergo Koteles { KE_SW, 0x02, { .sw = { SW_TABLET_MODE, 1 } } }, 85e82882cdSGergo Koteles /* Drawing Board */ 86e82882cdSGergo Koteles { KE_SW, 0x03, { .sw = { SW_TABLET_MODE, 1 } } }, 87e82882cdSGergo Koteles /* Tent */ 88e82882cdSGergo Koteles { KE_SW, 0x04, { .sw = { SW_TABLET_MODE, 1 } } }, 89e82882cdSGergo Koteles { KE_END }, 90e82882cdSGergo Koteles }; 91e82882cdSGergo Koteles 92e82882cdSGergo Koteles static void lenovo_ymc_notify(struct wmi_device *wdev, union acpi_object *data) 93e82882cdSGergo Koteles { 94e82882cdSGergo Koteles struct lenovo_ymc_private *priv = dev_get_drvdata(&wdev->dev); 95e82882cdSGergo Koteles u32 input_val = 0; 96e82882cdSGergo Koteles struct acpi_buffer input = { sizeof(input_val), &input_val }; 97e82882cdSGergo Koteles struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL }; 98e82882cdSGergo Koteles union acpi_object *obj; 99e82882cdSGergo Koteles acpi_status status; 100e82882cdSGergo Koteles int code; 101e82882cdSGergo Koteles 102e82882cdSGergo Koteles status = wmi_evaluate_method(LENOVO_YMC_QUERY_GUID, 103e82882cdSGergo Koteles LENOVO_YMC_QUERY_INSTANCE, 104e82882cdSGergo Koteles LENOVO_YMC_QUERY_METHOD, 105e82882cdSGergo Koteles &input, &output); 106e82882cdSGergo Koteles 107e82882cdSGergo Koteles if (ACPI_FAILURE(status)) { 108e82882cdSGergo Koteles dev_warn(&wdev->dev, 109e82882cdSGergo Koteles "Failed to evaluate query method: %s\n", 110e82882cdSGergo Koteles acpi_format_exception(status)); 111e82882cdSGergo Koteles return; 112e82882cdSGergo Koteles } 113e82882cdSGergo Koteles 114e82882cdSGergo Koteles obj = output.pointer; 115e82882cdSGergo Koteles 116e82882cdSGergo Koteles if (obj->type != ACPI_TYPE_INTEGER) { 117e82882cdSGergo Koteles dev_warn(&wdev->dev, 118e82882cdSGergo Koteles "WMI event data is not an integer\n"); 119e82882cdSGergo Koteles goto free_obj; 120e82882cdSGergo Koteles } 121e82882cdSGergo Koteles code = obj->integer.value; 122e82882cdSGergo Koteles 123e82882cdSGergo Koteles if (!sparse_keymap_report_event(priv->input_dev, code, 1, true)) 124e82882cdSGergo Koteles dev_warn(&wdev->dev, "Unknown key %d pressed\n", code); 125e82882cdSGergo Koteles 126e82882cdSGergo Koteles free_obj: 127e82882cdSGergo Koteles kfree(obj); 128e82882cdSGergo Koteles lenovo_ymc_trigger_ec(wdev, priv); 129e82882cdSGergo Koteles } 130e82882cdSGergo Koteles 131e82882cdSGergo Koteles static void acpi_dev_put_helper(void *p) { acpi_dev_put(p); } 132e82882cdSGergo Koteles 133e82882cdSGergo Koteles static int lenovo_ymc_probe(struct wmi_device *wdev, const void *ctx) 134e82882cdSGergo Koteles { 135e82882cdSGergo Koteles struct lenovo_ymc_private *priv; 136e82882cdSGergo Koteles struct input_dev *input_dev; 137e82882cdSGergo Koteles int err; 138e82882cdSGergo Koteles 1392b6aa661SHans de Goede if (!dmi_check_system(allowed_chasis_types_dmi_table)) { 1402b6aa661SHans de Goede if (force) 1412b6aa661SHans de Goede dev_info(&wdev->dev, "Force loading Lenovo YMC support\n"); 1422b6aa661SHans de Goede else 1432b6aa661SHans de Goede return -ENODEV; 1442b6aa661SHans de Goede } 1452b6aa661SHans de Goede 146e82882cdSGergo Koteles ec_trigger |= dmi_check_system(ec_trigger_quirk_dmi_table); 147e82882cdSGergo Koteles 148e82882cdSGergo Koteles priv = devm_kzalloc(&wdev->dev, sizeof(*priv), GFP_KERNEL); 149e82882cdSGergo Koteles if (!priv) 150e82882cdSGergo Koteles return -ENOMEM; 151e82882cdSGergo Koteles 152e82882cdSGergo Koteles if (ec_trigger) { 153e82882cdSGergo Koteles pr_debug("Lenovo YMC enable EC triggering.\n"); 154e82882cdSGergo Koteles priv->ec_acpi_dev = acpi_dev_get_first_match_dev("VPC2004", NULL, -1); 155e82882cdSGergo Koteles 156e82882cdSGergo Koteles if (!priv->ec_acpi_dev) { 157e82882cdSGergo Koteles dev_err(&wdev->dev, "Could not find EC ACPI device.\n"); 158e82882cdSGergo Koteles return -ENODEV; 159e82882cdSGergo Koteles } 160e82882cdSGergo Koteles err = devm_add_action_or_reset(&wdev->dev, 161e82882cdSGergo Koteles acpi_dev_put_helper, priv->ec_acpi_dev); 162e82882cdSGergo Koteles if (err) { 163e82882cdSGergo Koteles dev_err(&wdev->dev, 164e82882cdSGergo Koteles "Could not clean up EC ACPI device: %d\n", err); 165e82882cdSGergo Koteles return err; 166e82882cdSGergo Koteles } 167e82882cdSGergo Koteles } 168e82882cdSGergo Koteles 169e82882cdSGergo Koteles input_dev = devm_input_allocate_device(&wdev->dev); 170e82882cdSGergo Koteles if (!input_dev) 171e82882cdSGergo Koteles return -ENOMEM; 172e82882cdSGergo Koteles 173e82882cdSGergo Koteles input_dev->name = "Lenovo Yoga Tablet Mode Control switch"; 174e82882cdSGergo Koteles input_dev->phys = LENOVO_YMC_EVENT_GUID "/input0"; 175e82882cdSGergo Koteles input_dev->id.bustype = BUS_HOST; 176e82882cdSGergo Koteles input_dev->dev.parent = &wdev->dev; 177e82882cdSGergo Koteles err = sparse_keymap_setup(input_dev, lenovo_ymc_keymap, NULL); 178e82882cdSGergo Koteles if (err) { 179e82882cdSGergo Koteles dev_err(&wdev->dev, 180e82882cdSGergo Koteles "Could not set up input device keymap: %d\n", err); 181e82882cdSGergo Koteles return err; 182e82882cdSGergo Koteles } 183e82882cdSGergo Koteles 184e82882cdSGergo Koteles err = input_register_device(input_dev); 185e82882cdSGergo Koteles if (err) { 186e82882cdSGergo Koteles dev_err(&wdev->dev, 187e82882cdSGergo Koteles "Could not register input device: %d\n", err); 188e82882cdSGergo Koteles return err; 189e82882cdSGergo Koteles } 190e82882cdSGergo Koteles 191e82882cdSGergo Koteles priv->input_dev = input_dev; 192e82882cdSGergo Koteles dev_set_drvdata(&wdev->dev, priv); 193e82882cdSGergo Koteles 194e82882cdSGergo Koteles /* Report the state for the first time on probe */ 195e82882cdSGergo Koteles lenovo_ymc_trigger_ec(wdev, priv); 196e82882cdSGergo Koteles lenovo_ymc_notify(wdev, NULL); 197e82882cdSGergo Koteles return 0; 198e82882cdSGergo Koteles } 199e82882cdSGergo Koteles 200e82882cdSGergo Koteles static const struct wmi_device_id lenovo_ymc_wmi_id_table[] = { 201e82882cdSGergo Koteles { .guid_string = LENOVO_YMC_EVENT_GUID }, 202e82882cdSGergo Koteles { } 203e82882cdSGergo Koteles }; 204e82882cdSGergo Koteles MODULE_DEVICE_TABLE(wmi, lenovo_ymc_wmi_id_table); 205e82882cdSGergo Koteles 206e82882cdSGergo Koteles static struct wmi_driver lenovo_ymc_driver = { 207e82882cdSGergo Koteles .driver = { 208e82882cdSGergo Koteles .name = "lenovo-ymc", 209e82882cdSGergo Koteles }, 210e82882cdSGergo Koteles .id_table = lenovo_ymc_wmi_id_table, 211e82882cdSGergo Koteles .probe = lenovo_ymc_probe, 212e82882cdSGergo Koteles .notify = lenovo_ymc_notify, 213e82882cdSGergo Koteles }; 214e82882cdSGergo Koteles 215e82882cdSGergo Koteles module_wmi_driver(lenovo_ymc_driver); 216e82882cdSGergo Koteles 217e82882cdSGergo Koteles MODULE_AUTHOR("Gergo Koteles <soyer@irl.hu>"); 218e82882cdSGergo Koteles MODULE_DESCRIPTION("Lenovo Yoga Mode Control driver"); 219e82882cdSGergo Koteles MODULE_LICENSE("GPL"); 220