1 // SPDX-License-Identifier: GPL-2.0-only
3 * Supports for the button array on SoC tablets originally running
6 * (C) Copyright 2014 Intel Corporation
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>
19 struct soc_button_info {
22 unsigned int event_type;
23 unsigned int event_code;
28 struct soc_device_data {
29 const struct soc_button_info *button_info;
30 int (*check)(struct device *dev);
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.
38 #define BUTTON_TYPES 2
40 struct soc_button_data {
41 struct platform_device *children[BUTTON_TYPES];
45 * Get the Nth GPIO number from the ACPI object.
47 static int soc_button_lookup_gpio(struct device *dev, int acpi_index)
49 struct gpio_desc *desc;
52 desc = gpiod_get_index(dev, NULL, acpi_index, GPIOD_ASIS);
56 gpio = desc_to_gpio(desc);
63 static struct platform_device *
64 soc_button_device_create(struct platform_device *pdev,
65 const struct soc_button_info *button_info,
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;
76 for (info = button_info; info->name; info++)
77 if (info->autorepeat == autorepeat)
80 gpio_keys_pdata = devm_kzalloc(&pdev->dev,
81 sizeof(*gpio_keys_pdata) +
82 sizeof(*gpio_keys) * n_buttons,
85 return ERR_PTR(-ENOMEM);
87 gpio_keys = (void *)(gpio_keys_pdata + 1);
90 for (info = button_info; info->name; info++) {
91 if (info->autorepeat != autorepeat)
94 gpio = soc_button_lookup_gpio(&pdev->dev, info->acpi_index);
95 if (gpio < 0 && gpio != -ENOENT) {
98 } else if (!gpio_is_valid(gpio)) {
99 /* Skip GPIO if not present */
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;
114 if (n_buttons == 0) {
119 gpio_keys_pdata->buttons = gpio_keys;
120 gpio_keys_pdata->nbuttons = n_buttons;
121 gpio_keys_pdata->rep = autorepeat;
123 pd = platform_device_register_resndata(&pdev->dev, "gpio-keys",
124 PLATFORM_DEVID_AUTO, NULL, 0,
126 sizeof(*gpio_keys_pdata));
127 error = PTR_ERR_OR_ZERO(pd);
130 "failed registering gpio-keys: %d\n", error);
137 devm_kfree(&pdev->dev, gpio_keys_pdata);
138 return ERR_PTR(error);
141 static int soc_button_get_acpi_object_int(const union acpi_object *obj)
143 if (obj->type != ACPI_TYPE_INTEGER)
146 return obj->integer.value;
149 /* Parse a single ACPI0011 _DSD button descriptor */
150 static int soc_button_parse_btn_desc(struct device *dev,
151 const union acpi_object *desc,
153 struct soc_button_info *info)
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]) !=
164 dev_err(dev, "Invalid ACPI Button Descriptor\n");
168 info->event_type = EV_KEY;
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]);
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.
181 * Luckily all devices only use a few usage page + usage combinations,
182 * so we can simply check for the known combinations here.
184 if (upage == 0x01 && usage == 0x81) {
185 info->name = "power";
186 info->event_code = KEY_POWER;
188 } else if (upage == 0x01 && usage == 0xca) {
189 info->name = "rotation lock switch";
190 info->event_type = EV_SW;
191 info->event_code = SW_ROTATE_LOCK;
192 } else if (upage == 0x07 && usage == 0xe3) {
194 info->event_code = KEY_LEFTMETA;
196 } else if (upage == 0x0c && usage == 0xe9) {
197 info->name = "volume_up";
198 info->event_code = KEY_VOLUMEUP;
199 info->autorepeat = true;
200 } else if (upage == 0x0c && usage == 0xea) {
201 info->name = "volume_down";
202 info->event_code = KEY_VOLUMEDOWN;
203 info->autorepeat = true;
205 dev_warn(dev, "Unknown button index %d upage %02x usage %02x, ignoring\n",
206 info->acpi_index, upage, usage);
207 info->name = "unknown";
208 info->event_code = KEY_RESERVED;
214 /* ACPI0011 _DSD btns descriptors UUID: fa6bd625-9ce8-470d-a2c7-b3ca36c4282e */
215 static const u8 btns_desc_uuid[16] = {
216 0x25, 0xd6, 0x6b, 0xfa, 0xe8, 0x9c, 0x0d, 0x47,
217 0xa2, 0xc7, 0xb3, 0xca, 0x36, 0xc4, 0x28, 0x2e
220 /* Parse ACPI0011 _DSD button descriptors */
221 static struct soc_button_info *soc_button_get_button_info(struct device *dev)
223 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
224 const union acpi_object *desc, *el0, *uuid, *btns_desc = NULL;
225 struct soc_button_info *button_info;
227 int i, btn, collection_uid = -1;
229 status = acpi_evaluate_object_typed(ACPI_HANDLE(dev), "_DSD", NULL,
230 &buf, ACPI_TYPE_PACKAGE);
231 if (ACPI_FAILURE(status)) {
232 dev_err(dev, "ACPI _DSD object not found\n");
233 return ERR_PTR(-ENODEV);
236 /* Look for the Button Descriptors UUID */
238 for (i = 0; (i + 1) < desc->package.count; i += 2) {
239 uuid = &desc->package.elements[i];
241 if (uuid->type != ACPI_TYPE_BUFFER ||
242 uuid->buffer.length != 16 ||
243 desc->package.elements[i + 1].type != ACPI_TYPE_PACKAGE) {
247 if (memcmp(uuid->buffer.pointer, btns_desc_uuid, 16) == 0) {
248 btns_desc = &desc->package.elements[i + 1];
254 dev_err(dev, "ACPI Button Descriptors not found\n");
255 button_info = ERR_PTR(-ENODEV);
259 /* The first package describes the collection */
260 el0 = &btns_desc->package.elements[0];
261 if (el0->type == ACPI_TYPE_PACKAGE &&
262 el0->package.count == 5 &&
263 /* First byte should be 0 (collection) */
264 soc_button_get_acpi_object_int(&el0->package.elements[0]) == 0 &&
265 /* Third byte should be 0 (top level collection) */
266 soc_button_get_acpi_object_int(&el0->package.elements[2]) == 0) {
267 collection_uid = soc_button_get_acpi_object_int(
268 &el0->package.elements[1]);
270 if (collection_uid == -1) {
271 dev_err(dev, "Invalid Button Collection Descriptor\n");
272 button_info = ERR_PTR(-ENODEV);
276 /* There are package.count - 1 buttons + 1 terminating empty entry */
277 button_info = devm_kcalloc(dev, btns_desc->package.count,
278 sizeof(*button_info), GFP_KERNEL);
280 button_info = ERR_PTR(-ENOMEM);
284 /* Parse the button descriptors */
285 for (i = 1, btn = 0; i < btns_desc->package.count; i++, btn++) {
286 if (soc_button_parse_btn_desc(dev,
287 &btns_desc->package.elements[i],
289 &button_info[btn])) {
290 button_info = ERR_PTR(-ENODEV);
300 static int soc_button_remove(struct platform_device *pdev)
302 struct soc_button_data *priv = platform_get_drvdata(pdev);
306 for (i = 0; i < BUTTON_TYPES; i++)
307 if (priv->children[i])
308 platform_device_unregister(priv->children[i]);
313 static int soc_button_probe(struct platform_device *pdev)
315 struct device *dev = &pdev->dev;
316 const struct soc_device_data *device_data;
317 const struct soc_button_info *button_info;
318 struct soc_button_data *priv;
319 struct platform_device *pd;
323 device_data = acpi_device_get_match_data(dev);
324 if (device_data && device_data->check) {
325 error = device_data->check(dev);
330 if (device_data && device_data->button_info) {
331 button_info = device_data->button_info;
333 button_info = soc_button_get_button_info(dev);
334 if (IS_ERR(button_info))
335 return PTR_ERR(button_info);
338 error = gpiod_count(dev, NULL);
340 dev_dbg(dev, "no GPIO attached, ignoring...\n");
344 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
348 platform_set_drvdata(pdev, priv);
350 for (i = 0; i < BUTTON_TYPES; i++) {
351 pd = soc_button_device_create(pdev, button_info, i == 0);
354 if (error != -ENODEV) {
355 soc_button_remove(pdev);
361 priv->children[i] = pd;
364 if (!priv->children[0] && !priv->children[1])
367 if (!device_data || !device_data->button_info)
368 devm_kfree(dev, button_info);
374 * Definition of buttons on the tablet. The ACPI index of each button
375 * is defined in section 2.8.7.2 of "Windows ACPI Design Guide for SoC
378 static const struct soc_button_info soc_button_PNP0C40[] = {
379 { "power", 0, EV_KEY, KEY_POWER, false, true },
380 { "home", 1, EV_KEY, KEY_LEFTMETA, false, true },
381 { "volume_up", 2, EV_KEY, KEY_VOLUMEUP, true, false },
382 { "volume_down", 3, EV_KEY, KEY_VOLUMEDOWN, true, false },
383 { "rotation_lock", 4, EV_KEY, KEY_ROTATE_LOCK_TOGGLE, false, false },
387 static const struct soc_device_data soc_device_PNP0C40 = {
388 .button_info = soc_button_PNP0C40,
392 * Special device check for Surface Book 2 and Surface Pro (2017).
393 * Both, the Surface Pro 4 (surfacepro3_button.c) and the above mentioned
394 * devices use MSHW0040 for power and volume buttons, however the way they
395 * have to be addressed differs. Make sure that we only load this drivers
396 * for the correct devices by checking the OEM Platform Revision provided by
399 #define MSHW0040_DSM_REVISION 0x01
400 #define MSHW0040_DSM_GET_OMPR 0x02 // get OEM Platform Revision
401 static const guid_t MSHW0040_DSM_UUID =
402 GUID_INIT(0x6fd05c69, 0xcde3, 0x49f4, 0x95, 0xed, 0xab, 0x16, 0x65,
405 static int soc_device_check_MSHW0040(struct device *dev)
407 acpi_handle handle = ACPI_HANDLE(dev);
408 union acpi_object *result;
409 u64 oem_platform_rev = 0; // valid revisions are nonzero
411 // get OEM platform revision
412 result = acpi_evaluate_dsm_typed(handle, &MSHW0040_DSM_UUID,
413 MSHW0040_DSM_REVISION,
414 MSHW0040_DSM_GET_OMPR, NULL,
418 oem_platform_rev = result->integer.value;
423 * If the revision is zero here, the _DSM evaluation has failed. This
424 * indicates that we have a Pro 4 or Book 1 and this driver should not
427 if (oem_platform_rev == 0)
430 dev_dbg(dev, "OEM Platform Revision %llu\n", oem_platform_rev);
436 * Button infos for Microsoft Surface Book 2 and Surface Pro (2017).
437 * Obtained from DSDT/testing.
439 static const struct soc_button_info soc_button_MSHW0040[] = {
440 { "power", 0, EV_KEY, KEY_POWER, false, true },
441 { "volume_up", 2, EV_KEY, KEY_VOLUMEUP, true, false },
442 { "volume_down", 4, EV_KEY, KEY_VOLUMEDOWN, true, false },
446 static const struct soc_device_data soc_device_MSHW0040 = {
447 .button_info = soc_button_MSHW0040,
448 .check = soc_device_check_MSHW0040,
451 static const struct acpi_device_id soc_button_acpi_match[] = {
452 { "PNP0C40", (unsigned long)&soc_device_PNP0C40 },
455 /* Microsoft Surface Devices (5th and 6th generation) */
456 { "MSHW0040", (unsigned long)&soc_device_MSHW0040 },
461 MODULE_DEVICE_TABLE(acpi, soc_button_acpi_match);
463 static struct platform_driver soc_button_driver = {
464 .probe = soc_button_probe,
465 .remove = soc_button_remove,
467 .name = KBUILD_MODNAME,
468 .acpi_match_table = ACPI_PTR(soc_button_acpi_match),
471 module_platform_driver(soc_button_driver);
473 MODULE_LICENSE("GPL");