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