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