1 // SPDX-License-Identifier: GPL-2.0
2 /* WMI driver for Lenovo Yoga Book YB1-X90* / -X91* tablets */
4 #include <linux/acpi.h>
5 #include <linux/devm-helpers.h>
6 #include <linux/gpio/consumer.h>
7 #include <linux/gpio/machine.h>
8 #include <linux/interrupt.h>
9 #include <linux/module.h>
10 #include <linux/leds.h>
11 #include <linux/wmi.h>
12 #include <linux/workqueue.h>
14 #define YB_MBTN_EVENT_GUID "243FEC1D-1963-41C1-8100-06A9D82A94B4"
15 #define YB_MBTN_METHOD_GUID "742B0CA1-0B20-404B-9CAA-AEFCABF30CE0"
17 #define YB_PAD_ENABLE 1
18 #define YB_PAD_DISABLE 2
19 #define YB_LIGHTUP_BTN 3
21 #define YB_KBD_BL_DEFAULT 128
33 struct wmi_device *wdev;
34 struct acpi_device *kbd_adev;
35 struct acpi_device *dig_adev;
36 struct device *kbd_dev;
37 struct device *dig_dev;
38 struct gpio_desc *backside_hall_gpio;
39 int backside_hall_irq;
40 struct work_struct work;
41 struct led_classdev kbd_bl_led;
46 static int yogabook_wmi_do_action(struct wmi_device *wdev, int action)
48 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
49 struct acpi_buffer input;
53 dev_dbg(&wdev->dev, "Do action: %d\n", action);
55 input.pointer = &dummy_arg;
56 input.length = sizeof(dummy_arg);
58 status = wmi_evaluate_method(YB_MBTN_METHOD_GUID, 0, action, &input,
60 if (ACPI_FAILURE(status)) {
61 dev_err(&wdev->dev, "Calling WMI method failure: 0x%x\n",
66 kfree(output.pointer);
72 * To control keyboard backlight, call the method KBLC() of the TCS1 ACPI
73 * device (Goodix touchpad acts as virtual sensor keyboard).
75 static int yogabook_wmi_set_kbd_backlight(struct wmi_device *wdev,
78 struct yogabook_wmi *data = dev_get_drvdata(&wdev->dev);
79 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
80 struct acpi_object_list input;
81 union acpi_object param;
84 if (data->kbd_adev->power.state != ACPI_STATE_D0) {
85 dev_warn(&wdev->dev, "keyboard touchscreen not in D0, cannot set brightness\n");
89 dev_dbg(&wdev->dev, "Set KBLC level to %u\n", level);
92 input.pointer = ¶m;
94 param.type = ACPI_TYPE_INTEGER;
95 param.integer.value = 255 - level;
97 status = acpi_evaluate_object(acpi_device_handle(data->kbd_adev), "KBLC",
99 if (ACPI_FAILURE(status)) {
100 dev_err(&wdev->dev, "Failed to call KBLC method: 0x%x\n", status);
104 kfree(output.pointer);
108 static void yogabook_wmi_work(struct work_struct *work)
110 struct yogabook_wmi *data = container_of(work, struct yogabook_wmi, work);
111 struct device *dev = &data->wdev->dev;
112 bool kbd_on, digitizer_on;
115 if (test_bit(YB_SUSPENDED, &data->flags))
118 if (test_bit(YB_TABLET_MODE, &data->flags)) {
120 digitizer_on = false;
121 } else if (test_bit(YB_DIGITIZER_MODE, &data->flags)) {
126 digitizer_on = false;
129 if (!kbd_on && test_bit(YB_KBD_IS_ON, &data->flags)) {
131 * Must be done before releasing the keyboard touchscreen driver,
132 * so that the keyboard touchscreen dev is still in D0.
134 yogabook_wmi_set_kbd_backlight(data->wdev, 0);
135 device_release_driver(data->kbd_dev);
136 clear_bit(YB_KBD_IS_ON, &data->flags);
139 if (!digitizer_on && test_bit(YB_DIGITIZER_IS_ON, &data->flags)) {
140 yogabook_wmi_do_action(data->wdev, YB_PAD_DISABLE);
141 device_release_driver(data->dig_dev);
142 clear_bit(YB_DIGITIZER_IS_ON, &data->flags);
145 if (kbd_on && !test_bit(YB_KBD_IS_ON, &data->flags)) {
146 r = device_reprobe(data->kbd_dev);
148 dev_warn(dev, "Reprobe of keyboard touchscreen failed: %d\n", r);
150 yogabook_wmi_set_kbd_backlight(data->wdev, data->brightness);
151 set_bit(YB_KBD_IS_ON, &data->flags);
154 if (digitizer_on && !test_bit(YB_DIGITIZER_IS_ON, &data->flags)) {
155 r = device_reprobe(data->dig_dev);
157 dev_warn(dev, "Reprobe of digitizer failed: %d\n", r);
159 yogabook_wmi_do_action(data->wdev, YB_PAD_ENABLE);
160 set_bit(YB_DIGITIZER_IS_ON, &data->flags);
164 static void yogabook_wmi_notify(struct wmi_device *wdev, union acpi_object *dummy)
166 struct yogabook_wmi *data = dev_get_drvdata(&wdev->dev);
168 if (test_bit(YB_SUSPENDED, &data->flags))
171 if (test_bit(YB_DIGITIZER_MODE, &data->flags))
172 clear_bit(YB_DIGITIZER_MODE, &data->flags);
174 set_bit(YB_DIGITIZER_MODE, &data->flags);
177 * We are called from the ACPI core and the driver [un]binding which is
178 * done also needs ACPI functions, use a workqueue to avoid deadlocking.
180 schedule_work(&data->work);
183 static irqreturn_t yogabook_backside_hall_irq(int irq, void *_data)
185 struct yogabook_wmi *data = _data;
187 if (gpiod_get_value(data->backside_hall_gpio))
188 set_bit(YB_TABLET_MODE, &data->flags);
190 clear_bit(YB_TABLET_MODE, &data->flags);
192 schedule_work(&data->work);
197 static enum led_brightness kbd_brightness_get(struct led_classdev *cdev)
199 struct yogabook_wmi *data =
200 container_of(cdev, struct yogabook_wmi, kbd_bl_led);
202 return data->brightness;
205 static int kbd_brightness_set(struct led_classdev *cdev,
206 enum led_brightness value)
208 struct yogabook_wmi *data =
209 container_of(cdev, struct yogabook_wmi, kbd_bl_led);
210 struct wmi_device *wdev = data->wdev;
212 if ((value < 0) || (value > 255))
215 data->brightness = value;
217 if (data->kbd_adev->power.state != ACPI_STATE_D0)
220 return yogabook_wmi_set_kbd_backlight(wdev, data->brightness);
223 static struct gpiod_lookup_table yogabook_wmi_gpios = {
224 .dev_id = "243FEC1D-1963-41C1-8100-06A9D82A94B4",
226 GPIO_LOOKUP("INT33FF:02", 18, "backside_hall_sw", GPIO_ACTIVE_LOW),
231 static void yogabook_wmi_rm_gpio_lookup(void *unused)
233 gpiod_remove_lookup_table(&yogabook_wmi_gpios);
236 static int yogabook_wmi_probe(struct wmi_device *wdev, const void *context)
238 struct yogabook_wmi *data;
241 data = devm_kzalloc(&wdev->dev, sizeof(struct yogabook_wmi), GFP_KERNEL);
245 dev_set_drvdata(&wdev->dev, data);
248 data->brightness = YB_KBD_BL_DEFAULT;
249 set_bit(YB_KBD_IS_ON, &data->flags);
250 set_bit(YB_DIGITIZER_IS_ON, &data->flags);
252 r = devm_work_autocancel(&wdev->dev, &data->work, yogabook_wmi_work);
256 data->kbd_adev = acpi_dev_get_first_match_dev("GDIX1001", NULL, -1);
257 if (!data->kbd_adev) {
258 dev_err(&wdev->dev, "Cannot find the touchpad device in ACPI tables\n");
262 data->dig_adev = acpi_dev_get_first_match_dev("WCOM0019", NULL, -1);
263 if (!data->dig_adev) {
264 dev_err(&wdev->dev, "Cannot find the digitizer device in ACPI tables\n");
269 data->kbd_dev = get_device(acpi_get_first_physical_node(data->kbd_adev));
270 if (!data->kbd_dev || !data->kbd_dev->driver) {
275 data->dig_dev = get_device(acpi_get_first_physical_node(data->dig_adev));
276 if (!data->dig_dev || !data->dig_dev->driver) {
281 gpiod_add_lookup_table(&yogabook_wmi_gpios);
283 r = devm_add_action_or_reset(&wdev->dev, yogabook_wmi_rm_gpio_lookup, NULL);
287 data->backside_hall_gpio =
288 devm_gpiod_get(&wdev->dev, "backside_hall_sw", GPIOD_IN);
289 if (IS_ERR(data->backside_hall_gpio)) {
290 r = PTR_ERR(data->backside_hall_gpio);
291 dev_err_probe(&wdev->dev, r, "Getting backside_hall_sw GPIO\n");
295 r = gpiod_to_irq(data->backside_hall_gpio);
297 dev_err_probe(&wdev->dev, r, "Getting backside_hall_sw IRQ\n");
300 data->backside_hall_irq = r;
302 r = devm_request_irq(&wdev->dev, data->backside_hall_irq,
303 yogabook_backside_hall_irq,
304 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
305 "backside_hall_sw", data);
307 dev_err_probe(&wdev->dev, r, "Requesting backside_hall_sw IRQ\n");
311 schedule_work(&data->work);
313 data->kbd_bl_led.name = "ybwmi::kbd_backlight";
314 data->kbd_bl_led.brightness_set_blocking = kbd_brightness_set;
315 data->kbd_bl_led.brightness_get = kbd_brightness_get;
316 data->kbd_bl_led.max_brightness = 255;
318 r = devm_led_classdev_register(&wdev->dev, &data->kbd_bl_led);
320 dev_err_probe(&wdev->dev, r, "Registering backlight LED device\n");
327 put_device(data->dig_dev);
328 put_device(data->kbd_dev);
329 acpi_dev_put(data->dig_adev);
330 acpi_dev_put(data->kbd_adev);
334 static void yogabook_wmi_remove(struct wmi_device *wdev)
336 struct yogabook_wmi *data = dev_get_drvdata(&wdev->dev);
338 put_device(data->dig_dev);
339 put_device(data->kbd_dev);
340 acpi_dev_put(data->dig_adev);
341 acpi_dev_put(data->kbd_adev);
344 static int __maybe_unused yogabook_wmi_suspend(struct device *dev)
346 struct wmi_device *wdev = container_of(dev, struct wmi_device, dev);
347 struct yogabook_wmi *data = dev_get_drvdata(dev);
349 set_bit(YB_SUSPENDED, &data->flags);
351 flush_work(&data->work);
353 /* Turn off the pen button at sleep */
354 if (test_bit(YB_DIGITIZER_IS_ON, &data->flags))
355 yogabook_wmi_do_action(wdev, YB_PAD_DISABLE);
360 static int __maybe_unused yogabook_wmi_resume(struct device *dev)
362 struct wmi_device *wdev = container_of(dev, struct wmi_device, dev);
363 struct yogabook_wmi *data = dev_get_drvdata(dev);
365 if (test_bit(YB_KBD_IS_ON, &data->flags)) {
366 /* Ensure keyboard touchpad is on before we call KBLC() */
367 acpi_device_set_power(data->kbd_adev, ACPI_STATE_D0);
368 yogabook_wmi_set_kbd_backlight(wdev, data->brightness);
371 if (test_bit(YB_DIGITIZER_IS_ON, &data->flags))
372 yogabook_wmi_do_action(wdev, YB_PAD_ENABLE);
374 clear_bit(YB_SUSPENDED, &data->flags);
376 /* Check for YB_TABLET_MODE changes made during suspend */
377 schedule_work(&data->work);
382 static const struct wmi_device_id yogabook_wmi_id_table[] = {
384 .guid_string = YB_MBTN_EVENT_GUID,
386 { } /* Terminating entry */
389 static SIMPLE_DEV_PM_OPS(yogabook_wmi_pm_ops,
390 yogabook_wmi_suspend, yogabook_wmi_resume);
392 static struct wmi_driver yogabook_wmi_driver = {
394 .name = "yogabook-wmi",
395 .pm = &yogabook_wmi_pm_ops,
397 .no_notify_data = true,
398 .id_table = yogabook_wmi_id_table,
399 .probe = yogabook_wmi_probe,
400 .remove = yogabook_wmi_remove,
401 .notify = yogabook_wmi_notify,
403 module_wmi_driver(yogabook_wmi_driver);
405 MODULE_DEVICE_TABLE(wmi, yogabook_wmi_id_table);
406 MODULE_AUTHOR("Yauhen Kharuzhy");
407 MODULE_DESCRIPTION("Lenovo Yoga Book WMI driver");
408 MODULE_LICENSE("GPL v2");