Linux Audio

Check our new training course

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