xref: /openbmc/linux/drivers/platform/x86/lenovo-ymc.c (revision fac59652993f075d57860769c99045b3ca18780d)
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 	},
39db35610aSSwapnil Devesh 	{
40db35610aSSwapnil Devesh 		/* Lenovo Yoga 7 14ACN6 */
41db35610aSSwapnil Devesh 		.matches = {
42db35610aSSwapnil Devesh 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
43db35610aSSwapnil Devesh 			DMI_MATCH(DMI_PRODUCT_NAME, "82N7"),
44db35610aSSwapnil Devesh 		},
45db35610aSSwapnil 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 
lenovo_ymc_trigger_ec(struct wmi_device * wdev,struct lenovo_ymc_private * priv)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[] = {
81*f5e30a30SGergo Koteles 	/* Ignore the uninitialized state */
82*f5e30a30SGergo Koteles 	{ KE_IGNORE, 0x00 },
83e82882cdSGergo Koteles 	/* Laptop */
84e82882cdSGergo Koteles 	{ KE_SW, 0x01, { .sw = { SW_TABLET_MODE, 0 } } },
85e82882cdSGergo Koteles 	/* Tablet */
86e82882cdSGergo Koteles 	{ KE_SW, 0x02, { .sw = { SW_TABLET_MODE, 1 } } },
87e82882cdSGergo Koteles 	/* Drawing Board */
88e82882cdSGergo Koteles 	{ KE_SW, 0x03, { .sw = { SW_TABLET_MODE, 1 } } },
89e82882cdSGergo Koteles 	/* Tent */
90e82882cdSGergo Koteles 	{ KE_SW, 0x04, { .sw = { SW_TABLET_MODE, 1 } } },
91e82882cdSGergo Koteles 	{ KE_END },
92e82882cdSGergo Koteles };
93e82882cdSGergo Koteles 
lenovo_ymc_notify(struct wmi_device * wdev,union acpi_object * data)94e82882cdSGergo Koteles static void lenovo_ymc_notify(struct wmi_device *wdev, union acpi_object *data)
95e82882cdSGergo Koteles {
96e82882cdSGergo Koteles 	struct lenovo_ymc_private *priv = dev_get_drvdata(&wdev->dev);
97e82882cdSGergo Koteles 	u32 input_val = 0;
98e82882cdSGergo Koteles 	struct acpi_buffer input = { sizeof(input_val), &input_val };
99e82882cdSGergo Koteles 	struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
100e82882cdSGergo Koteles 	union acpi_object *obj;
101e82882cdSGergo Koteles 	acpi_status status;
102e82882cdSGergo Koteles 	int code;
103e82882cdSGergo Koteles 
104e82882cdSGergo Koteles 	status = wmi_evaluate_method(LENOVO_YMC_QUERY_GUID,
105e82882cdSGergo Koteles 				LENOVO_YMC_QUERY_INSTANCE,
106e82882cdSGergo Koteles 				LENOVO_YMC_QUERY_METHOD,
107e82882cdSGergo Koteles 				&input, &output);
108e82882cdSGergo Koteles 
109e82882cdSGergo Koteles 	if (ACPI_FAILURE(status)) {
110e82882cdSGergo Koteles 		dev_warn(&wdev->dev,
111e82882cdSGergo Koteles 			"Failed to evaluate query method: %s\n",
112e82882cdSGergo Koteles 			acpi_format_exception(status));
113e82882cdSGergo Koteles 		return;
114e82882cdSGergo Koteles 	}
115e82882cdSGergo Koteles 
116e82882cdSGergo Koteles 	obj = output.pointer;
117e82882cdSGergo Koteles 
118e82882cdSGergo Koteles 	if (obj->type != ACPI_TYPE_INTEGER) {
119e82882cdSGergo Koteles 		dev_warn(&wdev->dev,
120e82882cdSGergo Koteles 			"WMI event data is not an integer\n");
121e82882cdSGergo Koteles 		goto free_obj;
122e82882cdSGergo Koteles 	}
123e82882cdSGergo Koteles 	code = obj->integer.value;
124e82882cdSGergo Koteles 
125e82882cdSGergo Koteles 	if (!sparse_keymap_report_event(priv->input_dev, code, 1, true))
126e82882cdSGergo Koteles 		dev_warn(&wdev->dev, "Unknown key %d pressed\n", code);
127e82882cdSGergo Koteles 
128e82882cdSGergo Koteles free_obj:
129e82882cdSGergo Koteles 	kfree(obj);
130e82882cdSGergo Koteles 	lenovo_ymc_trigger_ec(wdev, priv);
131e82882cdSGergo Koteles }
132e82882cdSGergo Koteles 
acpi_dev_put_helper(void * p)133e82882cdSGergo Koteles static void acpi_dev_put_helper(void *p) { acpi_dev_put(p); }
134e82882cdSGergo Koteles 
lenovo_ymc_probe(struct wmi_device * wdev,const void * ctx)135e82882cdSGergo Koteles static int lenovo_ymc_probe(struct wmi_device *wdev, const void *ctx)
136e82882cdSGergo Koteles {
137e82882cdSGergo Koteles 	struct lenovo_ymc_private *priv;
138e82882cdSGergo Koteles 	struct input_dev *input_dev;
139e82882cdSGergo Koteles 	int err;
140e82882cdSGergo Koteles 
1412b6aa661SHans de Goede 	if (!dmi_check_system(allowed_chasis_types_dmi_table)) {
1422b6aa661SHans de Goede 		if (force)
1432b6aa661SHans de Goede 			dev_info(&wdev->dev, "Force loading Lenovo YMC support\n");
1442b6aa661SHans de Goede 		else
1452b6aa661SHans de Goede 			return -ENODEV;
1462b6aa661SHans de Goede 	}
1472b6aa661SHans de Goede 
148e82882cdSGergo Koteles 	ec_trigger |= dmi_check_system(ec_trigger_quirk_dmi_table);
149e82882cdSGergo Koteles 
150e82882cdSGergo Koteles 	priv = devm_kzalloc(&wdev->dev, sizeof(*priv), GFP_KERNEL);
151e82882cdSGergo Koteles 	if (!priv)
152e82882cdSGergo Koteles 		return -ENOMEM;
153e82882cdSGergo Koteles 
154e82882cdSGergo Koteles 	if (ec_trigger) {
155e82882cdSGergo Koteles 		pr_debug("Lenovo YMC enable EC triggering.\n");
156e82882cdSGergo Koteles 		priv->ec_acpi_dev = acpi_dev_get_first_match_dev("VPC2004", NULL, -1);
157e82882cdSGergo Koteles 
158e82882cdSGergo Koteles 		if (!priv->ec_acpi_dev) {
159e82882cdSGergo Koteles 			dev_err(&wdev->dev, "Could not find EC ACPI device.\n");
160e82882cdSGergo Koteles 			return -ENODEV;
161e82882cdSGergo Koteles 		}
162e82882cdSGergo Koteles 		err = devm_add_action_or_reset(&wdev->dev,
163e82882cdSGergo Koteles 				acpi_dev_put_helper, priv->ec_acpi_dev);
164e82882cdSGergo Koteles 		if (err) {
165e82882cdSGergo Koteles 			dev_err(&wdev->dev,
166e82882cdSGergo Koteles 				"Could not clean up EC ACPI device: %d\n", err);
167e82882cdSGergo Koteles 			return err;
168e82882cdSGergo Koteles 		}
169e82882cdSGergo Koteles 	}
170e82882cdSGergo Koteles 
171e82882cdSGergo Koteles 	input_dev = devm_input_allocate_device(&wdev->dev);
172e82882cdSGergo Koteles 	if (!input_dev)
173e82882cdSGergo Koteles 		return -ENOMEM;
174e82882cdSGergo Koteles 
175e82882cdSGergo Koteles 	input_dev->name = "Lenovo Yoga Tablet Mode Control switch";
176e82882cdSGergo Koteles 	input_dev->phys = LENOVO_YMC_EVENT_GUID "/input0";
177e82882cdSGergo Koteles 	input_dev->id.bustype = BUS_HOST;
178e82882cdSGergo Koteles 	input_dev->dev.parent = &wdev->dev;
179e82882cdSGergo Koteles 	err = sparse_keymap_setup(input_dev, lenovo_ymc_keymap, NULL);
180e82882cdSGergo Koteles 	if (err) {
181e82882cdSGergo Koteles 		dev_err(&wdev->dev,
182e82882cdSGergo Koteles 			"Could not set up input device keymap: %d\n", err);
183e82882cdSGergo Koteles 		return err;
184e82882cdSGergo Koteles 	}
185e82882cdSGergo Koteles 
186e82882cdSGergo Koteles 	err = input_register_device(input_dev);
187e82882cdSGergo Koteles 	if (err) {
188e82882cdSGergo Koteles 		dev_err(&wdev->dev,
189e82882cdSGergo Koteles 			"Could not register input device: %d\n", err);
190e82882cdSGergo Koteles 		return err;
191e82882cdSGergo Koteles 	}
192e82882cdSGergo Koteles 
193e82882cdSGergo Koteles 	priv->input_dev = input_dev;
194e82882cdSGergo Koteles 	dev_set_drvdata(&wdev->dev, priv);
195e82882cdSGergo Koteles 
196e82882cdSGergo Koteles 	/* Report the state for the first time on probe */
197e82882cdSGergo Koteles 	lenovo_ymc_trigger_ec(wdev, priv);
198e82882cdSGergo Koteles 	lenovo_ymc_notify(wdev, NULL);
199e82882cdSGergo Koteles 	return 0;
200e82882cdSGergo Koteles }
201e82882cdSGergo Koteles 
202e82882cdSGergo Koteles static const struct wmi_device_id lenovo_ymc_wmi_id_table[] = {
203e82882cdSGergo Koteles 	{ .guid_string = LENOVO_YMC_EVENT_GUID },
204e82882cdSGergo Koteles 	{ }
205e82882cdSGergo Koteles };
206e82882cdSGergo Koteles MODULE_DEVICE_TABLE(wmi, lenovo_ymc_wmi_id_table);
207e82882cdSGergo Koteles 
208e82882cdSGergo Koteles static struct wmi_driver lenovo_ymc_driver = {
209e82882cdSGergo Koteles 	.driver = {
210e82882cdSGergo Koteles 		.name = "lenovo-ymc",
211e82882cdSGergo Koteles 	},
212e82882cdSGergo Koteles 	.id_table = lenovo_ymc_wmi_id_table,
213e82882cdSGergo Koteles 	.probe = lenovo_ymc_probe,
214e82882cdSGergo Koteles 	.notify = lenovo_ymc_notify,
215e82882cdSGergo Koteles };
216e82882cdSGergo Koteles 
217e82882cdSGergo Koteles module_wmi_driver(lenovo_ymc_driver);
218e82882cdSGergo Koteles 
219e82882cdSGergo Koteles MODULE_AUTHOR("Gergo Koteles <soyer@irl.hu>");
220e82882cdSGergo Koteles MODULE_DESCRIPTION("Lenovo Yoga Mode Control driver");
221e82882cdSGergo Koteles MODULE_LICENSE("GPL");
222