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