cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

lenovo-yogabook-wmi.c (10717B)


      1// SPDX-License-Identifier: GPL-2.0
      2/* WMI driver for Lenovo Yoga Book YB1-X90* / -X91* tablets */
      3
      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>
     13
     14#define YB_MBTN_EVENT_GUID	"243FEC1D-1963-41C1-8100-06A9D82A94B4"
     15#define YB_MBTN_METHOD_GUID	"742B0CA1-0B20-404B-9CAA-AEFCABF30CE0"
     16
     17#define YB_PAD_ENABLE	1
     18#define YB_PAD_DISABLE	2
     19#define YB_LIGHTUP_BTN	3
     20
     21#define YB_KBD_BL_DEFAULT 128
     22
     23/* flags */
     24enum {
     25	YB_KBD_IS_ON,
     26	YB_DIGITIZER_IS_ON,
     27	YB_DIGITIZER_MODE,
     28	YB_TABLET_MODE,
     29	YB_SUSPENDED,
     30};
     31
     32struct yogabook_wmi {
     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;
     42	unsigned long flags;
     43	uint8_t brightness;
     44};
     45
     46static int yogabook_wmi_do_action(struct wmi_device *wdev, int action)
     47{
     48	struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
     49	struct acpi_buffer input;
     50	acpi_status status;
     51	u32 dummy_arg = 0;
     52
     53	dev_dbg(&wdev->dev, "Do action: %d\n", action);
     54
     55	input.pointer = &dummy_arg;
     56	input.length = sizeof(dummy_arg);
     57
     58	status = wmi_evaluate_method(YB_MBTN_METHOD_GUID, 0, action, &input,
     59				     &output);
     60	if (ACPI_FAILURE(status)) {
     61		dev_err(&wdev->dev, "Calling WMI method failure: 0x%x\n",
     62			status);
     63		return status;
     64	}
     65
     66	kfree(output.pointer);
     67
     68	return 0;
     69}
     70
     71/*
     72 * To control keyboard backlight, call the method KBLC() of the TCS1 ACPI
     73 * device (Goodix touchpad acts as virtual sensor keyboard).
     74 */
     75static int yogabook_wmi_set_kbd_backlight(struct wmi_device *wdev,
     76					  uint8_t level)
     77{
     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;
     82	acpi_status status;
     83
     84	if (data->kbd_adev->power.state != ACPI_STATE_D0) {
     85		dev_warn(&wdev->dev, "keyboard touchscreen not in D0, cannot set brightness\n");
     86		return -ENXIO;
     87	}
     88
     89	dev_dbg(&wdev->dev, "Set KBLC level to %u\n", level);
     90
     91	input.count = 1;
     92	input.pointer = &param;
     93
     94	param.type = ACPI_TYPE_INTEGER;
     95	param.integer.value = 255 - level;
     96
     97	status = acpi_evaluate_object(acpi_device_handle(data->kbd_adev), "KBLC",
     98				      &input, &output);
     99	if (ACPI_FAILURE(status)) {
    100		dev_err(&wdev->dev, "Failed to call KBLC method: 0x%x\n", status);
    101		return status;
    102	}
    103
    104	kfree(output.pointer);
    105	return 0;
    106}
    107
    108static void yogabook_wmi_work(struct work_struct *work)
    109{
    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;
    113	int r;
    114
    115	if (test_bit(YB_SUSPENDED, &data->flags))
    116		return;
    117
    118	if (test_bit(YB_TABLET_MODE, &data->flags)) {
    119		kbd_on = false;
    120		digitizer_on = false;
    121	} else if (test_bit(YB_DIGITIZER_MODE, &data->flags)) {
    122		digitizer_on = true;
    123		kbd_on = false;
    124	} else {
    125		kbd_on = true;
    126		digitizer_on = false;
    127	}
    128
    129	if (!kbd_on && test_bit(YB_KBD_IS_ON, &data->flags)) {
    130		/*
    131		 * Must be done before releasing the keyboard touchscreen driver,
    132		 * so that the keyboard touchscreen dev is still in D0.
    133		 */
    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);
    137	}
    138
    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);
    143	}
    144
    145	if (kbd_on && !test_bit(YB_KBD_IS_ON, &data->flags)) {
    146		r = device_reprobe(data->kbd_dev);
    147		if (r)
    148			dev_warn(dev, "Reprobe of keyboard touchscreen failed: %d\n", r);
    149
    150		yogabook_wmi_set_kbd_backlight(data->wdev, data->brightness);
    151		set_bit(YB_KBD_IS_ON, &data->flags);
    152	}
    153
    154	if (digitizer_on && !test_bit(YB_DIGITIZER_IS_ON, &data->flags)) {
    155		r = device_reprobe(data->dig_dev);
    156		if (r)
    157			dev_warn(dev, "Reprobe of digitizer failed: %d\n", r);
    158
    159		yogabook_wmi_do_action(data->wdev, YB_PAD_ENABLE);
    160		set_bit(YB_DIGITIZER_IS_ON, &data->flags);
    161	}
    162}
    163
    164static void yogabook_wmi_notify(struct wmi_device *wdev, union acpi_object *dummy)
    165{
    166	struct yogabook_wmi *data = dev_get_drvdata(&wdev->dev);
    167
    168	if (test_bit(YB_SUSPENDED, &data->flags))
    169		return;
    170
    171	if (test_bit(YB_DIGITIZER_MODE, &data->flags))
    172		clear_bit(YB_DIGITIZER_MODE, &data->flags);
    173	else
    174		set_bit(YB_DIGITIZER_MODE, &data->flags);
    175
    176	/*
    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.
    179	 */
    180	schedule_work(&data->work);
    181}
    182
    183static irqreturn_t yogabook_backside_hall_irq(int irq, void *_data)
    184{
    185	struct yogabook_wmi *data = _data;
    186
    187	if (gpiod_get_value(data->backside_hall_gpio))
    188		set_bit(YB_TABLET_MODE, &data->flags);
    189	else
    190		clear_bit(YB_TABLET_MODE, &data->flags);
    191
    192	schedule_work(&data->work);
    193
    194	return IRQ_HANDLED;
    195}
    196
    197static enum led_brightness kbd_brightness_get(struct led_classdev *cdev)
    198{
    199	struct yogabook_wmi *data =
    200		container_of(cdev, struct yogabook_wmi, kbd_bl_led);
    201
    202	return data->brightness;
    203}
    204
    205static int kbd_brightness_set(struct led_classdev *cdev,
    206			      enum led_brightness value)
    207{
    208	struct yogabook_wmi *data =
    209		container_of(cdev, struct yogabook_wmi, kbd_bl_led);
    210	struct wmi_device *wdev = data->wdev;
    211
    212	if ((value < 0) || (value > 255))
    213		return -EINVAL;
    214
    215	data->brightness = value;
    216
    217	if (data->kbd_adev->power.state != ACPI_STATE_D0)
    218		return 0;
    219
    220	return yogabook_wmi_set_kbd_backlight(wdev, data->brightness);
    221}
    222
    223static struct gpiod_lookup_table yogabook_wmi_gpios = {
    224	.dev_id		= "243FEC1D-1963-41C1-8100-06A9D82A94B4",
    225	.table		= {
    226		GPIO_LOOKUP("INT33FF:02", 18, "backside_hall_sw", GPIO_ACTIVE_LOW),
    227		{}
    228	},
    229};
    230
    231static void yogabook_wmi_rm_gpio_lookup(void *unused)
    232{
    233	gpiod_remove_lookup_table(&yogabook_wmi_gpios);
    234}
    235
    236static int yogabook_wmi_probe(struct wmi_device *wdev, const void *context)
    237{
    238	struct yogabook_wmi *data;
    239	int r;
    240
    241	data = devm_kzalloc(&wdev->dev, sizeof(struct yogabook_wmi), GFP_KERNEL);
    242	if (data == NULL)
    243		return -ENOMEM;
    244
    245	dev_set_drvdata(&wdev->dev, data);
    246
    247	data->wdev = wdev;
    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);
    251
    252	r = devm_work_autocancel(&wdev->dev, &data->work, yogabook_wmi_work);
    253	if (r)
    254		return r;
    255
    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");
    259		return -ENODEV;
    260	}
    261
    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");
    265		r = -ENODEV;
    266		goto error_put_devs;
    267	}
    268
    269	data->kbd_dev = get_device(acpi_get_first_physical_node(data->kbd_adev));
    270	if (!data->kbd_dev || !data->kbd_dev->driver) {
    271		r = -EPROBE_DEFER;
    272		goto error_put_devs;
    273	}
    274
    275	data->dig_dev = get_device(acpi_get_first_physical_node(data->dig_adev));
    276	if (!data->dig_dev || !data->dig_dev->driver) {
    277		r = -EPROBE_DEFER;
    278		goto error_put_devs;
    279	}
    280
    281	gpiod_add_lookup_table(&yogabook_wmi_gpios);
    282
    283	r = devm_add_action_or_reset(&wdev->dev, yogabook_wmi_rm_gpio_lookup, NULL);
    284	if (r)
    285		goto error_put_devs;
    286
    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");
    292		goto error_put_devs;
    293	}
    294
    295	r = gpiod_to_irq(data->backside_hall_gpio);
    296	if (r < 0) {
    297		dev_err_probe(&wdev->dev, r, "Getting backside_hall_sw IRQ\n");
    298		goto error_put_devs;
    299	}
    300	data->backside_hall_irq = r;
    301
    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);
    306	if (r) {
    307		dev_err_probe(&wdev->dev, r, "Requesting backside_hall_sw IRQ\n");
    308		goto error_put_devs;
    309	}
    310
    311	schedule_work(&data->work);
    312
    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;
    317
    318	r = devm_led_classdev_register(&wdev->dev, &data->kbd_bl_led);
    319	if (r < 0) {
    320		dev_err_probe(&wdev->dev, r, "Registering backlight LED device\n");
    321		goto error_put_devs;
    322	}
    323
    324	return 0;
    325
    326error_put_devs:
    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);
    331	return r;
    332}
    333
    334static void yogabook_wmi_remove(struct wmi_device *wdev)
    335{
    336	struct yogabook_wmi *data = dev_get_drvdata(&wdev->dev);
    337
    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);
    342}
    343
    344static int __maybe_unused yogabook_wmi_suspend(struct device *dev)
    345{
    346	struct wmi_device *wdev = container_of(dev, struct wmi_device, dev);
    347	struct yogabook_wmi *data = dev_get_drvdata(dev);
    348
    349	set_bit(YB_SUSPENDED, &data->flags);
    350
    351	flush_work(&data->work);
    352
    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);
    356
    357	return 0;
    358}
    359
    360static int __maybe_unused yogabook_wmi_resume(struct device *dev)
    361{
    362	struct wmi_device *wdev = container_of(dev, struct wmi_device, dev);
    363	struct yogabook_wmi *data = dev_get_drvdata(dev);
    364
    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);
    369	}
    370
    371	if (test_bit(YB_DIGITIZER_IS_ON, &data->flags))
    372		yogabook_wmi_do_action(wdev, YB_PAD_ENABLE);
    373
    374	clear_bit(YB_SUSPENDED, &data->flags);
    375
    376	/* Check for YB_TABLET_MODE changes made during suspend */
    377	schedule_work(&data->work);
    378
    379	return 0;
    380}
    381
    382static const struct wmi_device_id yogabook_wmi_id_table[] = {
    383	{
    384		.guid_string = YB_MBTN_EVENT_GUID,
    385	},
    386	{ } /* Terminating entry */
    387};
    388
    389static SIMPLE_DEV_PM_OPS(yogabook_wmi_pm_ops,
    390			 yogabook_wmi_suspend, yogabook_wmi_resume);
    391
    392static struct wmi_driver yogabook_wmi_driver = {
    393	.driver = {
    394		.name = "yogabook-wmi",
    395		.pm = &yogabook_wmi_pm_ops,
    396	},
    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,
    402};
    403module_wmi_driver(yogabook_wmi_driver);
    404
    405MODULE_DEVICE_TABLE(wmi, yogabook_wmi_id_table);
    406MODULE_AUTHOR("Yauhen Kharuzhy");
    407MODULE_DESCRIPTION("Lenovo Yoga Book WMI driver");
    408MODULE_LICENSE("GPL v2");