blob: 47c6d000465a72855922ef70ee96350b8f1a7031 [file] [log] [blame]
Thomas Gleixnerb886d83c2019-06-01 10:08:55 +02001// SPDX-License-Identifier: GPL-2.0-only
Chen Yu2508a452015-08-18 23:30:25 +08002/*
3 * power/home/volume button support for
Weng Xuetian6d5ac6e2016-01-17 15:10:38 -08004 * Microsoft Surface Pro 3/4 tablet.
Chen Yu2508a452015-08-18 23:30:25 +08005 *
6 * Copyright (c) 2015 Intel Corporation.
7 * All rights reserved.
Chen Yu2508a452015-08-18 23:30:25 +08008 */
9
10#include <linux/kernel.h>
11#include <linux/module.h>
12#include <linux/init.h>
13#include <linux/types.h>
14#include <linux/input.h>
15#include <linux/acpi.h>
16#include <acpi/button.h>
17
Weng Xuetian6d5ac6e2016-01-17 15:10:38 -080018#define SURFACE_PRO3_BUTTON_HID "MSHW0028"
19#define SURFACE_PRO4_BUTTON_HID "MSHW0040"
Chen Yu2508a452015-08-18 23:30:25 +080020#define SURFACE_BUTTON_OBJ_NAME "VGBI"
Weng Xuetian6d5ac6e2016-01-17 15:10:38 -080021#define SURFACE_BUTTON_DEVICE_NAME "Surface Pro 3/4 Buttons"
Chen Yu2508a452015-08-18 23:30:25 +080022
Andy Shevchenkoe27ffe72016-05-10 14:49:55 +030023#define SURFACE_BUTTON_NOTIFY_TABLET_MODE 0xc8
24
Chen Yu2508a452015-08-18 23:30:25 +080025#define SURFACE_BUTTON_NOTIFY_PRESS_POWER 0xc6
26#define SURFACE_BUTTON_NOTIFY_RELEASE_POWER 0xc7
27
28#define SURFACE_BUTTON_NOTIFY_PRESS_HOME 0xc4
29#define SURFACE_BUTTON_NOTIFY_RELEASE_HOME 0xc5
30
31#define SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_UP 0xc0
32#define SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_UP 0xc1
33
Andy Shevchenkoe27ffe72016-05-10 14:49:55 +030034#define SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_DOWN 0xc2
Chen Yu2508a452015-08-18 23:30:25 +080035#define SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_DOWN 0xc3
36
37ACPI_MODULE_NAME("surface pro 3 button");
38
39MODULE_AUTHOR("Chen Yu");
40MODULE_DESCRIPTION("Surface Pro3 Button Driver");
41MODULE_LICENSE("GPL v2");
42
43/*
44 * Power button, Home button, Volume buttons support is supposed to
45 * be covered by drivers/input/misc/soc_button_array.c, which is implemented
46 * according to "Windows ACPI Design Guide for SoC Platforms".
47 * However surface pro3 seems not to obey the specs, instead it uses
48 * device VGBI(MSHW0028) for dispatching the events.
49 * We choose acpi_driver rather than platform_driver/i2c_driver because
50 * although VGBI has an i2c resource connected to i2c controller, it
51 * is not embedded in any i2c controller's scope, thus neither platform_device
52 * will be created, nor i2c_client will be enumerated, we have to use
53 * acpi_driver.
54 */
55static const struct acpi_device_id surface_button_device_ids[] = {
Weng Xuetian6d5ac6e2016-01-17 15:10:38 -080056 {SURFACE_PRO3_BUTTON_HID, 0},
57 {SURFACE_PRO4_BUTTON_HID, 0},
Chen Yu2508a452015-08-18 23:30:25 +080058 {"", 0},
59};
60MODULE_DEVICE_TABLE(acpi, surface_button_device_ids);
61
62struct surface_button {
63 unsigned int type;
64 struct input_dev *input;
65 char phys[32]; /* for input device */
66 unsigned long pushed;
67 bool suspended;
68};
69
70static void surface_button_notify(struct acpi_device *device, u32 event)
71{
72 struct surface_button *button = acpi_driver_data(device);
73 struct input_dev *input;
74 int key_code = KEY_RESERVED;
75 bool pressed = false;
76
77 switch (event) {
78 /* Power button press,release handle */
79 case SURFACE_BUTTON_NOTIFY_PRESS_POWER:
80 pressed = true;
81 /*fall through*/
82 case SURFACE_BUTTON_NOTIFY_RELEASE_POWER:
83 key_code = KEY_POWER;
84 break;
85 /* Home button press,release handle */
86 case SURFACE_BUTTON_NOTIFY_PRESS_HOME:
87 pressed = true;
88 /*fall through*/
89 case SURFACE_BUTTON_NOTIFY_RELEASE_HOME:
90 key_code = KEY_LEFTMETA;
91 break;
92 /* Volume up button press,release handle */
93 case SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_UP:
94 pressed = true;
95 /*fall through*/
96 case SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_UP:
97 key_code = KEY_VOLUMEUP;
98 break;
99 /* Volume down button press,release handle */
100 case SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_DOWN:
101 pressed = true;
102 /*fall through*/
103 case SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_DOWN:
104 key_code = KEY_VOLUMEDOWN;
105 break;
Andy Shevchenkoe27ffe72016-05-10 14:49:55 +0300106 case SURFACE_BUTTON_NOTIFY_TABLET_MODE:
107 dev_warn_once(&device->dev, "Tablet mode is not supported\n");
108 break;
Chen Yu2508a452015-08-18 23:30:25 +0800109 default:
110 dev_info_ratelimited(&device->dev,
Andy Shevchenkoe27ffe72016-05-10 14:49:55 +0300111 "Unsupported event [0x%x]\n", event);
Chen Yu2508a452015-08-18 23:30:25 +0800112 break;
113 }
114 input = button->input;
Julia Lawall4bef0a22015-12-27 22:10:58 +0100115 if (key_code == KEY_RESERVED)
Chen Yu2508a452015-08-18 23:30:25 +0800116 return;
117 if (pressed)
Rafael J. Wysocki19351f32018-01-10 13:26:35 +0100118 pm_wakeup_dev_event(&device->dev, 0, button->suspended);
Chen Yu2508a452015-08-18 23:30:25 +0800119 if (button->suspended)
120 return;
121 input_report_key(input, key_code, pressed?1:0);
122 input_sync(input);
123}
124
125#ifdef CONFIG_PM_SLEEP
126static int surface_button_suspend(struct device *dev)
127{
128 struct acpi_device *device = to_acpi_device(dev);
129 struct surface_button *button = acpi_driver_data(device);
130
131 button->suspended = true;
132 return 0;
133}
134
135static int surface_button_resume(struct device *dev)
136{
137 struct acpi_device *device = to_acpi_device(dev);
138 struct surface_button *button = acpi_driver_data(device);
139
140 button->suspended = false;
141 return 0;
142}
143#endif
144
145static int surface_button_add(struct acpi_device *device)
146{
147 struct surface_button *button;
148 struct input_dev *input;
149 const char *hid = acpi_device_hid(device);
150 char *name;
151 int error;
152
153 if (strncmp(acpi_device_bid(device), SURFACE_BUTTON_OBJ_NAME,
154 strlen(SURFACE_BUTTON_OBJ_NAME)))
155 return -ENODEV;
156
157 button = kzalloc(sizeof(struct surface_button), GFP_KERNEL);
158 if (!button)
159 return -ENOMEM;
160
161 device->driver_data = button;
162 button->input = input = input_allocate_device();
163 if (!input) {
164 error = -ENOMEM;
165 goto err_free_button;
166 }
167
168 name = acpi_device_name(device);
169 strcpy(name, SURFACE_BUTTON_DEVICE_NAME);
170 snprintf(button->phys, sizeof(button->phys), "%s/buttons", hid);
171
172 input->name = name;
173 input->phys = button->phys;
174 input->id.bustype = BUS_HOST;
175 input->dev.parent = &device->dev;
176 input_set_capability(input, EV_KEY, KEY_POWER);
177 input_set_capability(input, EV_KEY, KEY_LEFTMETA);
178 input_set_capability(input, EV_KEY, KEY_VOLUMEUP);
179 input_set_capability(input, EV_KEY, KEY_VOLUMEDOWN);
180
181 error = input_register_device(input);
182 if (error)
183 goto err_free_input;
Rafael J. Wysocki19351f32018-01-10 13:26:35 +0100184
185 device_init_wakeup(&device->dev, true);
Chen Yu2508a452015-08-18 23:30:25 +0800186 dev_info(&device->dev,
187 "%s [%s]\n", name, acpi_device_bid(device));
188 return 0;
189
190 err_free_input:
191 input_free_device(input);
192 err_free_button:
193 kfree(button);
194 return error;
195}
196
197static int surface_button_remove(struct acpi_device *device)
198{
199 struct surface_button *button = acpi_driver_data(device);
200
201 input_unregister_device(button->input);
202 kfree(button);
203 return 0;
204}
205
206static SIMPLE_DEV_PM_OPS(surface_button_pm,
207 surface_button_suspend, surface_button_resume);
208
209static struct acpi_driver surface_button_driver = {
210 .name = "surface_pro3_button",
211 .class = "SurfacePro3",
212 .ids = surface_button_device_ids,
213 .ops = {
214 .add = surface_button_add,
215 .remove = surface_button_remove,
216 .notify = surface_button_notify,
217 },
218 .drv.pm = &surface_button_pm,
219};
220
221module_acpi_driver(surface_button_driver);