1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Supports for the button array on SoC tablets originally running
4  * Windows 8.
5  *
6  * (C) Copyright 2014 Intel Corporation
7  */
8 
9 #include <linux/module.h>
10 #include <linux/input.h>
11 #include <linux/init.h>
12 #include <linux/irq.h>
13 #include <linux/kernel.h>
14 #include <linux/acpi.h>
15 #include <linux/dmi.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/gpio_keys.h>
18 #include <linux/gpio.h>
19 #include <linux/platform_device.h>
20 
21 struct soc_button_info {
22 	const char *name;
23 	int acpi_index;
24 	unsigned int event_type;
25 	unsigned int event_code;
26 	bool autorepeat;
27 	bool wakeup;
28 	bool active_low;
29 };
30 
31 struct soc_device_data {
32 	const struct soc_button_info *button_info;
33 	int (*check)(struct device *dev);
34 };
35 
36 /*
37  * Some of the buttons like volume up/down are auto repeat, while others
38  * are not. To support both, we register two platform devices, and put
39  * buttons into them based on whether the key should be auto repeat.
40  */
41 #define BUTTON_TYPES	2
42 
43 struct soc_button_data {
44 	struct platform_device *children[BUTTON_TYPES];
45 };
46 
47 /*
48  * Some 2-in-1s which use the soc_button_array driver have this ugly issue in
49  * their DSDT where the _LID method modifies the irq-type settings of the GPIOs
50  * used for the power and home buttons. The intend of this AML code is to
51  * disable these buttons when the lid is closed.
52  * The AML does this by directly poking the GPIO controllers registers. This is
53  * problematic because when re-enabling the irq, which happens whenever _LID
54  * gets called with the lid open (e.g. on boot and on resume), it sets the
55  * irq-type to IRQ_TYPE_LEVEL_LOW. Where as the gpio-keys driver programs the
56  * type to, and expects it to be, IRQ_TYPE_EDGE_BOTH.
57  * To work around this we don't set gpio_keys_button.gpio on these 2-in-1s,
58  * instead we get the irq for the GPIO ourselves, configure it as
59  * IRQ_TYPE_LEVEL_LOW (to match how the _LID AML code configures it) and pass
60  * the irq in gpio_keys_button.irq. Below is a list of affected devices.
61  */
62 static const struct dmi_system_id dmi_use_low_level_irq[] = {
63 	{
64 		/*
65 		 * Acer Switch 10 SW5-012. _LID method messes with home- and
66 		 * power-button GPIO IRQ settings. When (re-)enabling the irq
67 		 * it ors in its own flags without clearing the previous set
68 		 * ones, leading to an irq-type of IRQ_TYPE_LEVEL_LOW |
69 		 * IRQ_TYPE_LEVEL_HIGH causing a continuous interrupt storm.
70 		 */
71 		.matches = {
72 			DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
73 			DMI_MATCH(DMI_PRODUCT_NAME, "Aspire SW5-012"),
74 		},
75 	},
76 	{
77 		/*
78 		 * Acer One S1003. _LID method messes with power-button GPIO
79 		 * IRQ settings, leading to a non working power-button.
80 		 */
81 		.matches = {
82 			DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
83 			DMI_MATCH(DMI_PRODUCT_NAME, "One S1003"),
84 		},
85 	},
86 	{} /* Terminating entry */
87 };
88 
89 /*
90  * Get the Nth GPIO number from the ACPI object.
91  */
92 static int soc_button_lookup_gpio(struct device *dev, int acpi_index,
93 				  int *gpio_ret, int *irq_ret)
94 {
95 	struct gpio_desc *desc;
96 
97 	desc = gpiod_get_index(dev, NULL, acpi_index, GPIOD_ASIS);
98 	if (IS_ERR(desc))
99 		return PTR_ERR(desc);
100 
101 	*gpio_ret = desc_to_gpio(desc);
102 	*irq_ret = gpiod_to_irq(desc);
103 
104 	gpiod_put(desc);
105 
106 	return 0;
107 }
108 
109 static struct platform_device *
110 soc_button_device_create(struct platform_device *pdev,
111 			 const struct soc_button_info *button_info,
112 			 bool autorepeat)
113 {
114 	const struct soc_button_info *info;
115 	struct platform_device *pd;
116 	struct gpio_keys_button *gpio_keys;
117 	struct gpio_keys_platform_data *gpio_keys_pdata;
118 	int error, gpio, irq;
119 	int n_buttons = 0;
120 
121 	for (info = button_info; info->name; info++)
122 		if (info->autorepeat == autorepeat)
123 			n_buttons++;
124 
125 	gpio_keys_pdata = devm_kzalloc(&pdev->dev,
126 				       sizeof(*gpio_keys_pdata) +
127 					sizeof(*gpio_keys) * n_buttons,
128 				       GFP_KERNEL);
129 	if (!gpio_keys_pdata)
130 		return ERR_PTR(-ENOMEM);
131 
132 	gpio_keys = (void *)(gpio_keys_pdata + 1);
133 	n_buttons = 0;
134 
135 	for (info = button_info; info->name; info++) {
136 		if (info->autorepeat != autorepeat)
137 			continue;
138 
139 		error = soc_button_lookup_gpio(&pdev->dev, info->acpi_index, &gpio, &irq);
140 		if (error || irq < 0) {
141 			/*
142 			 * Skip GPIO if not present. Note we deliberately
143 			 * ignore -EPROBE_DEFER errors here. On some devices
144 			 * Intel is using so called virtual GPIOs which are not
145 			 * GPIOs at all but some way for AML code to check some
146 			 * random status bits without need a custom opregion.
147 			 * In some cases the resources table we parse points to
148 			 * such a virtual GPIO, since these are not real GPIOs
149 			 * we do not have a driver for these so they will never
150 			 * show up, therefore we ignore -EPROBE_DEFER.
151 			 */
152 			continue;
153 		}
154 
155 		/* See dmi_use_low_level_irq[] comment */
156 		if (!autorepeat && dmi_check_system(dmi_use_low_level_irq)) {
157 			irq_set_irq_type(irq, IRQ_TYPE_LEVEL_LOW);
158 			gpio_keys[n_buttons].irq = irq;
159 			gpio_keys[n_buttons].gpio = -ENOENT;
160 		} else {
161 			gpio_keys[n_buttons].gpio = gpio;
162 		}
163 
164 		gpio_keys[n_buttons].type = info->event_type;
165 		gpio_keys[n_buttons].code = info->event_code;
166 		gpio_keys[n_buttons].active_low = info->active_low;
167 		gpio_keys[n_buttons].desc = info->name;
168 		gpio_keys[n_buttons].wakeup = info->wakeup;
169 		/* These devices often use cheap buttons, use 50 ms debounce */
170 		gpio_keys[n_buttons].debounce_interval = 50;
171 		n_buttons++;
172 	}
173 
174 	if (n_buttons == 0) {
175 		error = -ENODEV;
176 		goto err_free_mem;
177 	}
178 
179 	gpio_keys_pdata->buttons = gpio_keys;
180 	gpio_keys_pdata->nbuttons = n_buttons;
181 	gpio_keys_pdata->rep = autorepeat;
182 
183 	pd = platform_device_register_resndata(&pdev->dev, "gpio-keys",
184 					       PLATFORM_DEVID_AUTO, NULL, 0,
185 					       gpio_keys_pdata,
186 					       sizeof(*gpio_keys_pdata));
187 	error = PTR_ERR_OR_ZERO(pd);
188 	if (error) {
189 		dev_err(&pdev->dev,
190 			"failed registering gpio-keys: %d\n", error);
191 		goto err_free_mem;
192 	}
193 
194 	return pd;
195 
196 err_free_mem:
197 	devm_kfree(&pdev->dev, gpio_keys_pdata);
198 	return ERR_PTR(error);
199 }
200 
201 static int soc_button_get_acpi_object_int(const union acpi_object *obj)
202 {
203 	if (obj->type != ACPI_TYPE_INTEGER)
204 		return -1;
205 
206 	return obj->integer.value;
207 }
208 
209 /* Parse a single ACPI0011 _DSD button descriptor */
210 static int soc_button_parse_btn_desc(struct device *dev,
211 				     const union acpi_object *desc,
212 				     int collection_uid,
213 				     struct soc_button_info *info)
214 {
215 	int upage, usage;
216 
217 	if (desc->type != ACPI_TYPE_PACKAGE ||
218 	    desc->package.count != 5 ||
219 	    /* First byte should be 1 (control) */
220 	    soc_button_get_acpi_object_int(&desc->package.elements[0]) != 1 ||
221 	    /* Third byte should be collection uid */
222 	    soc_button_get_acpi_object_int(&desc->package.elements[2]) !=
223 							    collection_uid) {
224 		dev_err(dev, "Invalid ACPI Button Descriptor\n");
225 		return -ENODEV;
226 	}
227 
228 	info->event_type = EV_KEY;
229 	info->active_low = true;
230 	info->acpi_index =
231 		soc_button_get_acpi_object_int(&desc->package.elements[1]);
232 	upage = soc_button_get_acpi_object_int(&desc->package.elements[3]);
233 	usage = soc_button_get_acpi_object_int(&desc->package.elements[4]);
234 
235 	/*
236 	 * The UUID: fa6bd625-9ce8-470d-a2c7-b3ca36c4282e descriptors use HID
237 	 * usage page and usage codes, but otherwise the device is not HID
238 	 * compliant: it uses one irq per button instead of generating HID
239 	 * input reports and some buttons should generate wakeups where as
240 	 * others should not, so we cannot use the HID subsystem.
241 	 *
242 	 * Luckily all devices only use a few usage page + usage combinations,
243 	 * so we can simply check for the known combinations here.
244 	 */
245 	if (upage == 0x01 && usage == 0x81) {
246 		info->name = "power";
247 		info->event_code = KEY_POWER;
248 		info->wakeup = true;
249 	} else if (upage == 0x01 && usage == 0xca) {
250 		info->name = "rotation lock switch";
251 		info->event_type = EV_SW;
252 		info->event_code = SW_ROTATE_LOCK;
253 	} else if (upage == 0x07 && usage == 0xe3) {
254 		info->name = "home";
255 		info->event_code = KEY_LEFTMETA;
256 		info->wakeup = true;
257 	} else if (upage == 0x0c && usage == 0xe9) {
258 		info->name = "volume_up";
259 		info->event_code = KEY_VOLUMEUP;
260 		info->autorepeat = true;
261 	} else if (upage == 0x0c && usage == 0xea) {
262 		info->name = "volume_down";
263 		info->event_code = KEY_VOLUMEDOWN;
264 		info->autorepeat = true;
265 	} else {
266 		dev_warn(dev, "Unknown button index %d upage %02x usage %02x, ignoring\n",
267 			 info->acpi_index, upage, usage);
268 		info->name = "unknown";
269 		info->event_code = KEY_RESERVED;
270 	}
271 
272 	return 0;
273 }
274 
275 /* ACPI0011 _DSD btns descriptors UUID: fa6bd625-9ce8-470d-a2c7-b3ca36c4282e */
276 static const u8 btns_desc_uuid[16] = {
277 	0x25, 0xd6, 0x6b, 0xfa, 0xe8, 0x9c, 0x0d, 0x47,
278 	0xa2, 0xc7, 0xb3, 0xca, 0x36, 0xc4, 0x28, 0x2e
279 };
280 
281 /* Parse ACPI0011 _DSD button descriptors */
282 static struct soc_button_info *soc_button_get_button_info(struct device *dev)
283 {
284 	struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
285 	const union acpi_object *desc, *el0, *uuid, *btns_desc = NULL;
286 	struct soc_button_info *button_info;
287 	acpi_status status;
288 	int i, btn, collection_uid = -1;
289 
290 	status = acpi_evaluate_object_typed(ACPI_HANDLE(dev), "_DSD", NULL,
291 					    &buf, ACPI_TYPE_PACKAGE);
292 	if (ACPI_FAILURE(status)) {
293 		dev_err(dev, "ACPI _DSD object not found\n");
294 		return ERR_PTR(-ENODEV);
295 	}
296 
297 	/* Look for the Button Descriptors UUID */
298 	desc = buf.pointer;
299 	for (i = 0; (i + 1) < desc->package.count; i += 2) {
300 		uuid = &desc->package.elements[i];
301 
302 		if (uuid->type != ACPI_TYPE_BUFFER ||
303 		    uuid->buffer.length != 16 ||
304 		    desc->package.elements[i + 1].type != ACPI_TYPE_PACKAGE) {
305 			break;
306 		}
307 
308 		if (memcmp(uuid->buffer.pointer, btns_desc_uuid, 16) == 0) {
309 			btns_desc = &desc->package.elements[i + 1];
310 			break;
311 		}
312 	}
313 
314 	if (!btns_desc) {
315 		dev_err(dev, "ACPI Button Descriptors not found\n");
316 		button_info = ERR_PTR(-ENODEV);
317 		goto out;
318 	}
319 
320 	/* The first package describes the collection */
321 	el0 = &btns_desc->package.elements[0];
322 	if (el0->type == ACPI_TYPE_PACKAGE &&
323 	    el0->package.count == 5 &&
324 	    /* First byte should be 0 (collection) */
325 	    soc_button_get_acpi_object_int(&el0->package.elements[0]) == 0 &&
326 	    /* Third byte should be 0 (top level collection) */
327 	    soc_button_get_acpi_object_int(&el0->package.elements[2]) == 0) {
328 		collection_uid = soc_button_get_acpi_object_int(
329 						&el0->package.elements[1]);
330 	}
331 	if (collection_uid == -1) {
332 		dev_err(dev, "Invalid Button Collection Descriptor\n");
333 		button_info = ERR_PTR(-ENODEV);
334 		goto out;
335 	}
336 
337 	/* There are package.count - 1 buttons + 1 terminating empty entry */
338 	button_info = devm_kcalloc(dev, btns_desc->package.count,
339 				   sizeof(*button_info), GFP_KERNEL);
340 	if (!button_info) {
341 		button_info = ERR_PTR(-ENOMEM);
342 		goto out;
343 	}
344 
345 	/* Parse the button descriptors */
346 	for (i = 1, btn = 0; i < btns_desc->package.count; i++, btn++) {
347 		if (soc_button_parse_btn_desc(dev,
348 					      &btns_desc->package.elements[i],
349 					      collection_uid,
350 					      &button_info[btn])) {
351 			button_info = ERR_PTR(-ENODEV);
352 			goto out;
353 		}
354 	}
355 
356 out:
357 	kfree(buf.pointer);
358 	return button_info;
359 }
360 
361 static int soc_button_remove(struct platform_device *pdev)
362 {
363 	struct soc_button_data *priv = platform_get_drvdata(pdev);
364 
365 	int i;
366 
367 	for (i = 0; i < BUTTON_TYPES; i++)
368 		if (priv->children[i])
369 			platform_device_unregister(priv->children[i]);
370 
371 	return 0;
372 }
373 
374 static int soc_button_probe(struct platform_device *pdev)
375 {
376 	struct device *dev = &pdev->dev;
377 	const struct soc_device_data *device_data;
378 	const struct soc_button_info *button_info;
379 	struct soc_button_data *priv;
380 	struct platform_device *pd;
381 	int i;
382 	int error;
383 
384 	device_data = acpi_device_get_match_data(dev);
385 	if (device_data && device_data->check) {
386 		error = device_data->check(dev);
387 		if (error)
388 			return error;
389 	}
390 
391 	if (device_data && device_data->button_info) {
392 		button_info = device_data->button_info;
393 	} else {
394 		button_info = soc_button_get_button_info(dev);
395 		if (IS_ERR(button_info))
396 			return PTR_ERR(button_info);
397 	}
398 
399 	error = gpiod_count(dev, NULL);
400 	if (error < 0) {
401 		dev_dbg(dev, "no GPIO attached, ignoring...\n");
402 		return -ENODEV;
403 	}
404 
405 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
406 	if (!priv)
407 		return -ENOMEM;
408 
409 	platform_set_drvdata(pdev, priv);
410 
411 	for (i = 0; i < BUTTON_TYPES; i++) {
412 		pd = soc_button_device_create(pdev, button_info, i == 0);
413 		if (IS_ERR(pd)) {
414 			error = PTR_ERR(pd);
415 			if (error != -ENODEV) {
416 				soc_button_remove(pdev);
417 				return error;
418 			}
419 			continue;
420 		}
421 
422 		priv->children[i] = pd;
423 	}
424 
425 	if (!priv->children[0] && !priv->children[1])
426 		return -ENODEV;
427 
428 	if (!device_data || !device_data->button_info)
429 		devm_kfree(dev, button_info);
430 
431 	return 0;
432 }
433 
434 /*
435  * Definition of buttons on the tablet. The ACPI index of each button
436  * is defined in section 2.8.7.2 of "Windows ACPI Design Guide for SoC
437  * Platforms"
438  */
439 static const struct soc_button_info soc_button_PNP0C40[] = {
440 	{ "power", 0, EV_KEY, KEY_POWER, false, true, true },
441 	{ "home", 1, EV_KEY, KEY_LEFTMETA, false, true, true },
442 	{ "volume_up", 2, EV_KEY, KEY_VOLUMEUP, true, false, true },
443 	{ "volume_down", 3, EV_KEY, KEY_VOLUMEDOWN, true, false, true },
444 	{ "rotation_lock", 4, EV_KEY, KEY_ROTATE_LOCK_TOGGLE, false, false, true },
445 	{ }
446 };
447 
448 static const struct soc_device_data soc_device_PNP0C40 = {
449 	.button_info = soc_button_PNP0C40,
450 };
451 
452 static const struct soc_button_info soc_button_INT33D3[] = {
453 	{ "tablet_mode", 0, EV_SW, SW_TABLET_MODE, false, false, false },
454 	{ }
455 };
456 
457 static const struct soc_device_data soc_device_INT33D3 = {
458 	.button_info = soc_button_INT33D3,
459 };
460 
461 /*
462  * Special device check for Surface Book 2 and Surface Pro (2017).
463  * Both, the Surface Pro 4 (surfacepro3_button.c) and the above mentioned
464  * devices use MSHW0040 for power and volume buttons, however the way they
465  * have to be addressed differs. Make sure that we only load this drivers
466  * for the correct devices by checking the OEM Platform Revision provided by
467  * the _DSM method.
468  */
469 #define MSHW0040_DSM_REVISION		0x01
470 #define MSHW0040_DSM_GET_OMPR		0x02	// get OEM Platform Revision
471 static const guid_t MSHW0040_DSM_UUID =
472 	GUID_INIT(0x6fd05c69, 0xcde3, 0x49f4, 0x95, 0xed, 0xab, 0x16, 0x65,
473 		  0x49, 0x80, 0x35);
474 
475 static int soc_device_check_MSHW0040(struct device *dev)
476 {
477 	acpi_handle handle = ACPI_HANDLE(dev);
478 	union acpi_object *result;
479 	u64 oem_platform_rev = 0;	// valid revisions are nonzero
480 
481 	// get OEM platform revision
482 	result = acpi_evaluate_dsm_typed(handle, &MSHW0040_DSM_UUID,
483 					 MSHW0040_DSM_REVISION,
484 					 MSHW0040_DSM_GET_OMPR, NULL,
485 					 ACPI_TYPE_INTEGER);
486 
487 	if (result) {
488 		oem_platform_rev = result->integer.value;
489 		ACPI_FREE(result);
490 	}
491 
492 	/*
493 	 * If the revision is zero here, the _DSM evaluation has failed. This
494 	 * indicates that we have a Pro 4 or Book 1 and this driver should not
495 	 * be used.
496 	 */
497 	if (oem_platform_rev == 0)
498 		return -ENODEV;
499 
500 	dev_dbg(dev, "OEM Platform Revision %llu\n", oem_platform_rev);
501 
502 	return 0;
503 }
504 
505 /*
506  * Button infos for Microsoft Surface Book 2 and Surface Pro (2017).
507  * Obtained from DSDT/testing.
508  */
509 static const struct soc_button_info soc_button_MSHW0040[] = {
510 	{ "power", 0, EV_KEY, KEY_POWER, false, true, true },
511 	{ "volume_up", 2, EV_KEY, KEY_VOLUMEUP, true, false, true },
512 	{ "volume_down", 4, EV_KEY, KEY_VOLUMEDOWN, true, false, true },
513 	{ }
514 };
515 
516 static const struct soc_device_data soc_device_MSHW0040 = {
517 	.button_info = soc_button_MSHW0040,
518 	.check = soc_device_check_MSHW0040,
519 };
520 
521 static const struct acpi_device_id soc_button_acpi_match[] = {
522 	{ "PNP0C40", (unsigned long)&soc_device_PNP0C40 },
523 	{ "INT33D3", (unsigned long)&soc_device_INT33D3 },
524 	{ "ID9001", (unsigned long)&soc_device_INT33D3 },
525 	{ "ACPI0011", 0 },
526 
527 	/* Microsoft Surface Devices (5th and 6th generation) */
528 	{ "MSHW0040", (unsigned long)&soc_device_MSHW0040 },
529 
530 	{ }
531 };
532 
533 MODULE_DEVICE_TABLE(acpi, soc_button_acpi_match);
534 
535 static struct platform_driver soc_button_driver = {
536 	.probe          = soc_button_probe,
537 	.remove		= soc_button_remove,
538 	.driver		= {
539 		.name = KBUILD_MODNAME,
540 		.acpi_match_table = ACPI_PTR(soc_button_acpi_match),
541 	},
542 };
543 module_platform_driver(soc_button_driver);
544 
545 MODULE_LICENSE("GPL");
546