Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// SPDX-License-Identifier: GPL-2.0
/* WMI driver for Lenovo Yoga Book YB1-X90* / -X91* tablets */

#include <linux/acpi.h>
#include <linux/devm-helpers.h>
#include <linux/gpio/consumer.h>
#include <linux/gpio/machine.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/leds.h>
#include <linux/wmi.h>
#include <linux/workqueue.h>

#define YB_MBTN_EVENT_GUID	"243FEC1D-1963-41C1-8100-06A9D82A94B4"
#define YB_MBTN_METHOD_GUID	"742B0CA1-0B20-404B-9CAA-AEFCABF30CE0"

#define YB_PAD_ENABLE	1
#define YB_PAD_DISABLE	2
#define YB_LIGHTUP_BTN	3

#define YB_KBD_BL_DEFAULT 128

/* flags */
enum {
	YB_KBD_IS_ON,
	YB_DIGITIZER_IS_ON,
	YB_DIGITIZER_MODE,
	YB_TABLET_MODE,
	YB_SUSPENDED,
};

struct yogabook_wmi {
	struct wmi_device *wdev;
	struct acpi_device *kbd_adev;
	struct acpi_device *dig_adev;
	struct device *kbd_dev;
	struct device *dig_dev;
	struct gpio_desc *backside_hall_gpio;
	int backside_hall_irq;
	struct work_struct work;
	struct led_classdev kbd_bl_led;
	unsigned long flags;
	uint8_t brightness;
};

static int yogabook_wmi_do_action(struct wmi_device *wdev, int action)
{
	struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
	struct acpi_buffer input;
	acpi_status status;
	u32 dummy_arg = 0;

	dev_dbg(&wdev->dev, "Do action: %d\n", action);

	input.pointer = &dummy_arg;
	input.length = sizeof(dummy_arg);

	status = wmi_evaluate_method(YB_MBTN_METHOD_GUID, 0, action, &input,
				     &output);
	if (ACPI_FAILURE(status)) {
		dev_err(&wdev->dev, "Calling WMI method failure: 0x%x\n",
			status);
		return status;
	}

	kfree(output.pointer);

	return 0;
}

/*
 * To control keyboard backlight, call the method KBLC() of the TCS1 ACPI
 * device (Goodix touchpad acts as virtual sensor keyboard).
 */
static int yogabook_wmi_set_kbd_backlight(struct wmi_device *wdev,
					  uint8_t level)
{
	struct yogabook_wmi *data = dev_get_drvdata(&wdev->dev);
	struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
	struct acpi_object_list input;
	union acpi_object param;
	acpi_status status;

	if (data->kbd_adev->power.state != ACPI_STATE_D0) {
		dev_warn(&wdev->dev, "keyboard touchscreen not in D0, cannot set brightness\n");
		return -ENXIO;
	}

	dev_dbg(&wdev->dev, "Set KBLC level to %u\n", level);

	input.count = 1;
	input.pointer = &param;

	param.type = ACPI_TYPE_INTEGER;
	param.integer.value = 255 - level;

	status = acpi_evaluate_object(acpi_device_handle(data->kbd_adev), "KBLC",
				      &input, &output);
	if (ACPI_FAILURE(status)) {
		dev_err(&wdev->dev, "Failed to call KBLC method: 0x%x\n", status);
		return status;
	}

	kfree(output.pointer);
	return 0;
}

static void yogabook_wmi_work(struct work_struct *work)
{
	struct yogabook_wmi *data = container_of(work, struct yogabook_wmi, work);
	struct device *dev = &data->wdev->dev;
	bool kbd_on, digitizer_on;
	int r;

	if (test_bit(YB_SUSPENDED, &data->flags))
		return;

	if (test_bit(YB_TABLET_MODE, &data->flags)) {
		kbd_on = false;
		digitizer_on = false;
	} else if (test_bit(YB_DIGITIZER_MODE, &data->flags)) {
		digitizer_on = true;
		kbd_on = false;
	} else {
		kbd_on = true;
		digitizer_on = false;
	}

	if (!kbd_on && test_bit(YB_KBD_IS_ON, &data->flags)) {
		/*
		 * Must be done before releasing the keyboard touchscreen driver,
		 * so that the keyboard touchscreen dev is still in D0.
		 */
		yogabook_wmi_set_kbd_backlight(data->wdev, 0);
		device_release_driver(data->kbd_dev);
		clear_bit(YB_KBD_IS_ON, &data->flags);
	}

	if (!digitizer_on && test_bit(YB_DIGITIZER_IS_ON, &data->flags)) {
		yogabook_wmi_do_action(data->wdev, YB_PAD_DISABLE);
		device_release_driver(data->dig_dev);
		clear_bit(YB_DIGITIZER_IS_ON, &data->flags);
	}

	if (kbd_on && !test_bit(YB_KBD_IS_ON, &data->flags)) {
		r = device_reprobe(data->kbd_dev);
		if (r)
			dev_warn(dev, "Reprobe of keyboard touchscreen failed: %d\n", r);

		yogabook_wmi_set_kbd_backlight(data->wdev, data->brightness);
		set_bit(YB_KBD_IS_ON, &data->flags);
	}

	if (digitizer_on && !test_bit(YB_DIGITIZER_IS_ON, &data->flags)) {
		r = device_reprobe(data->dig_dev);
		if (r)
			dev_warn(dev, "Reprobe of digitizer failed: %d\n", r);

		yogabook_wmi_do_action(data->wdev, YB_PAD_ENABLE);
		set_bit(YB_DIGITIZER_IS_ON, &data->flags);
	}
}

static void yogabook_wmi_notify(struct wmi_device *wdev, union acpi_object *dummy)
{
	struct yogabook_wmi *data = dev_get_drvdata(&wdev->dev);

	if (test_bit(YB_SUSPENDED, &data->flags))
		return;

	if (test_bit(YB_DIGITIZER_MODE, &data->flags))
		clear_bit(YB_DIGITIZER_MODE, &data->flags);
	else
		set_bit(YB_DIGITIZER_MODE, &data->flags);

	/*
	 * We are called from the ACPI core and the driver [un]binding which is
	 * done also needs ACPI functions, use a workqueue to avoid deadlocking.
	 */
	schedule_work(&data->work);
}

static irqreturn_t yogabook_backside_hall_irq(int irq, void *_data)
{
	struct yogabook_wmi *data = _data;

	if (gpiod_get_value(data->backside_hall_gpio))
		set_bit(YB_TABLET_MODE, &data->flags);
	else
		clear_bit(YB_TABLET_MODE, &data->flags);

	schedule_work(&data->work);

	return IRQ_HANDLED;
}

static enum led_brightness kbd_brightness_get(struct led_classdev *cdev)
{
	struct yogabook_wmi *data =
		container_of(cdev, struct yogabook_wmi, kbd_bl_led);

	return data->brightness;
}

static int kbd_brightness_set(struct led_classdev *cdev,
			      enum led_brightness value)
{
	struct yogabook_wmi *data =
		container_of(cdev, struct yogabook_wmi, kbd_bl_led);
	struct wmi_device *wdev = data->wdev;

	if ((value < 0) || (value > 255))
		return -EINVAL;

	data->brightness = value;

	if (data->kbd_adev->power.state != ACPI_STATE_D0)
		return 0;

	return yogabook_wmi_set_kbd_backlight(wdev, data->brightness);
}

static struct gpiod_lookup_table yogabook_wmi_gpios = {
	.dev_id		= "243FEC1D-1963-41C1-8100-06A9D82A94B4",
	.table		= {
		GPIO_LOOKUP("INT33FF:02", 18, "backside_hall_sw", GPIO_ACTIVE_LOW),
		{}
	},
};

static void yogabook_wmi_rm_gpio_lookup(void *unused)
{
	gpiod_remove_lookup_table(&yogabook_wmi_gpios);
}

static int yogabook_wmi_probe(struct wmi_device *wdev, const void *context)
{
	struct yogabook_wmi *data;
	int r;

	data = devm_kzalloc(&wdev->dev, sizeof(struct yogabook_wmi), GFP_KERNEL);
	if (data == NULL)
		return -ENOMEM;

	dev_set_drvdata(&wdev->dev, data);

	data->wdev = wdev;
	data->brightness = YB_KBD_BL_DEFAULT;
	set_bit(YB_KBD_IS_ON, &data->flags);
	set_bit(YB_DIGITIZER_IS_ON, &data->flags);

	r = devm_work_autocancel(&wdev->dev, &data->work, yogabook_wmi_work);
	if (r)
		return r;

	data->kbd_adev = acpi_dev_get_first_match_dev("GDIX1001", NULL, -1);
	if (!data->kbd_adev) {
		dev_err(&wdev->dev, "Cannot find the touchpad device in ACPI tables\n");
		return -ENODEV;
	}

	data->dig_adev = acpi_dev_get_first_match_dev("WCOM0019", NULL, -1);
	if (!data->dig_adev) {
		dev_err(&wdev->dev, "Cannot find the digitizer device in ACPI tables\n");
		r = -ENODEV;
		goto error_put_devs;
	}

	data->kbd_dev = get_device(acpi_get_first_physical_node(data->kbd_adev));
	if (!data->kbd_dev || !data->kbd_dev->driver) {
		r = -EPROBE_DEFER;
		goto error_put_devs;
	}

	data->dig_dev = get_device(acpi_get_first_physical_node(data->dig_adev));
	if (!data->dig_dev || !data->dig_dev->driver) {
		r = -EPROBE_DEFER;
		goto error_put_devs;
	}

	gpiod_add_lookup_table(&yogabook_wmi_gpios);

	r = devm_add_action_or_reset(&wdev->dev, yogabook_wmi_rm_gpio_lookup, NULL);
	if (r)
		goto error_put_devs;

	data->backside_hall_gpio =
		devm_gpiod_get(&wdev->dev, "backside_hall_sw", GPIOD_IN);
	if (IS_ERR(data->backside_hall_gpio)) {
		r = PTR_ERR(data->backside_hall_gpio);
		dev_err_probe(&wdev->dev, r, "Getting backside_hall_sw GPIO\n");
		goto error_put_devs;
	}

	r = gpiod_to_irq(data->backside_hall_gpio);
	if (r < 0) {
		dev_err_probe(&wdev->dev, r, "Getting backside_hall_sw IRQ\n");
		goto error_put_devs;
	}
	data->backside_hall_irq = r;

	r = devm_request_irq(&wdev->dev, data->backside_hall_irq,
			     yogabook_backside_hall_irq,
			     IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
			     "backside_hall_sw", data);
	if (r) {
		dev_err_probe(&wdev->dev, r, "Requesting backside_hall_sw IRQ\n");
		goto error_put_devs;
	}

	schedule_work(&data->work);

	data->kbd_bl_led.name = "ybwmi::kbd_backlight";
	data->kbd_bl_led.brightness_set_blocking = kbd_brightness_set;
	data->kbd_bl_led.brightness_get = kbd_brightness_get;
	data->kbd_bl_led.max_brightness = 255;

	r = devm_led_classdev_register(&wdev->dev, &data->kbd_bl_led);
	if (r < 0) {
		dev_err_probe(&wdev->dev, r, "Registering backlight LED device\n");
		goto error_put_devs;
	}

	return 0;

error_put_devs:
	put_device(data->dig_dev);
	put_device(data->kbd_dev);
	acpi_dev_put(data->dig_adev);
	acpi_dev_put(data->kbd_adev);
	return r;
}

static void yogabook_wmi_remove(struct wmi_device *wdev)
{
	struct yogabook_wmi *data = dev_get_drvdata(&wdev->dev);

	put_device(data->dig_dev);
	put_device(data->kbd_dev);
	acpi_dev_put(data->dig_adev);
	acpi_dev_put(data->kbd_adev);
}

static int __maybe_unused yogabook_wmi_suspend(struct device *dev)
{
	struct wmi_device *wdev = container_of(dev, struct wmi_device, dev);
	struct yogabook_wmi *data = dev_get_drvdata(dev);

	set_bit(YB_SUSPENDED, &data->flags);

	flush_work(&data->work);

	/* Turn off the pen button at sleep */
	if (test_bit(YB_DIGITIZER_IS_ON, &data->flags))
		yogabook_wmi_do_action(wdev, YB_PAD_DISABLE);

	return 0;
}

static int __maybe_unused yogabook_wmi_resume(struct device *dev)
{
	struct wmi_device *wdev = container_of(dev, struct wmi_device, dev);
	struct yogabook_wmi *data = dev_get_drvdata(dev);

	if (test_bit(YB_KBD_IS_ON, &data->flags)) {
		/* Ensure keyboard touchpad is on before we call KBLC() */
		acpi_device_set_power(data->kbd_adev, ACPI_STATE_D0);
		yogabook_wmi_set_kbd_backlight(wdev, data->brightness);
	}

	if (test_bit(YB_DIGITIZER_IS_ON, &data->flags))
		yogabook_wmi_do_action(wdev, YB_PAD_ENABLE);

	clear_bit(YB_SUSPENDED, &data->flags);

	/* Check for YB_TABLET_MODE changes made during suspend */
	schedule_work(&data->work);

	return 0;
}

static const struct wmi_device_id yogabook_wmi_id_table[] = {
	{
		.guid_string = YB_MBTN_EVENT_GUID,
	},
	{ } /* Terminating entry */
};

static SIMPLE_DEV_PM_OPS(yogabook_wmi_pm_ops,
			 yogabook_wmi_suspend, yogabook_wmi_resume);

static struct wmi_driver yogabook_wmi_driver = {
	.driver = {
		.name = "yogabook-wmi",
		.pm = &yogabook_wmi_pm_ops,
	},
	.no_notify_data = true,
	.id_table = yogabook_wmi_id_table,
	.probe = yogabook_wmi_probe,
	.remove = yogabook_wmi_remove,
	.notify = yogabook_wmi_notify,
};
module_wmi_driver(yogabook_wmi_driver);

MODULE_DEVICE_TABLE(wmi, yogabook_wmi_id_table);
MODULE_AUTHOR("Yauhen Kharuzhy");
MODULE_DESCRIPTION("Lenovo Yoga Book WMI driver");
MODULE_LICENSE("GPL v2");