xref: /linux/drivers/platform/surface/surface3-wmi.c (revision a4eb44a6435d6d8f9e642407a4a06f65eb90ca04)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  Driver for the LID cover switch of the Surface 3
4  *
5  *  Copyright (c) 2016 Red Hat Inc.
6  */
7 
8 
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 
13 #include <linux/acpi.h>
14 #include <linux/dmi.h>
15 #include <linux/input.h>
16 #include <linux/mutex.h>
17 #include <linux/platform_device.h>
18 #include <linux/spi/spi.h>
19 
20 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@redhat.com>");
21 MODULE_DESCRIPTION("Surface 3 platform driver");
22 MODULE_LICENSE("GPL");
23 
24 #define ACPI_BUTTON_HID_LID		"PNP0C0D"
25 #define SPI_CTL_OBJ_NAME		"SPI"
26 #define SPI_TS_OBJ_NAME			"NTRG"
27 
28 #define SURFACE3_LID_GUID "F7CC25EC-D20B-404C-8903-0ED4359C18AE"
29 
30 MODULE_ALIAS("wmi:" SURFACE3_LID_GUID);
31 
32 static const struct dmi_system_id surface3_dmi_table[] = {
33 #if defined(CONFIG_X86)
34 	{
35 		.matches = {
36 			DMI_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
37 			DMI_MATCH(DMI_PRODUCT_NAME, "Surface 3"),
38 		},
39 	},
40 #endif
41 	{ }
42 };
43 
44 struct surface3_wmi {
45 	struct acpi_device *touchscreen_adev;
46 	struct acpi_device *pnp0c0d_adev;
47 	struct acpi_hotplug_context hp;
48 	struct input_dev *input;
49 };
50 
51 static struct platform_device *s3_wmi_pdev;
52 
53 static struct surface3_wmi s3_wmi;
54 
55 static DEFINE_MUTEX(s3_wmi_lock);
56 
57 static int s3_wmi_query_block(const char *guid, int instance, int *ret)
58 {
59 	struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
60 	union acpi_object *obj = NULL;
61 	acpi_status status;
62 	int error = 0;
63 
64 	mutex_lock(&s3_wmi_lock);
65 	status = wmi_query_block(guid, instance, &output);
66 	if (ACPI_FAILURE(status)) {
67 		error = -EIO;
68 		goto out_free_unlock;
69 	}
70 
71 	obj = output.pointer;
72 
73 	if (!obj || obj->type != ACPI_TYPE_INTEGER) {
74 		if (obj) {
75 			pr_err("query block returned object type: %d - buffer length:%d\n",
76 			       obj->type,
77 			       obj->type == ACPI_TYPE_BUFFER ?
78 						obj->buffer.length : 0);
79 		}
80 		error = -EINVAL;
81 		goto out_free_unlock;
82 	}
83 	*ret = obj->integer.value;
84  out_free_unlock:
85 	kfree(obj);
86 	mutex_unlock(&s3_wmi_lock);
87 	return error;
88 }
89 
90 static inline int s3_wmi_query_lid(int *ret)
91 {
92 	return s3_wmi_query_block(SURFACE3_LID_GUID, 0, ret);
93 }
94 
95 static int s3_wmi_send_lid_state(void)
96 {
97 	int ret, lid_sw;
98 
99 	ret = s3_wmi_query_lid(&lid_sw);
100 	if (ret)
101 		return ret;
102 
103 	input_report_switch(s3_wmi.input, SW_LID, lid_sw);
104 	input_sync(s3_wmi.input);
105 
106 	return 0;
107 }
108 
109 static int s3_wmi_hp_notify(struct acpi_device *adev, u32 value)
110 {
111 	return s3_wmi_send_lid_state();
112 }
113 
114 static acpi_status s3_wmi_attach_spi_device(acpi_handle handle,
115 					    u32 level,
116 					    void *data,
117 					    void **return_value)
118 {
119 	struct acpi_device *adev, **ts_adev;
120 
121 	if (acpi_bus_get_device(handle, &adev))
122 		return AE_OK;
123 
124 	ts_adev = data;
125 
126 	if (strncmp(acpi_device_bid(adev), SPI_TS_OBJ_NAME,
127 	    strlen(SPI_TS_OBJ_NAME)))
128 		return AE_OK;
129 
130 	if (*ts_adev) {
131 		pr_err("duplicate entry %s\n", SPI_TS_OBJ_NAME);
132 		return AE_OK;
133 	}
134 
135 	*ts_adev = adev;
136 
137 	return AE_OK;
138 }
139 
140 static int s3_wmi_check_platform_device(struct device *dev, void *data)
141 {
142 	struct acpi_device *adev = ACPI_COMPANION(dev);
143 	struct acpi_device *ts_adev = NULL;
144 	acpi_status status;
145 
146 	/* ignore non ACPI devices */
147 	if (!adev)
148 		return 0;
149 
150 	/* check for LID ACPI switch */
151 	if (!strcmp(ACPI_BUTTON_HID_LID, acpi_device_hid(adev))) {
152 		s3_wmi.pnp0c0d_adev = adev;
153 		return 0;
154 	}
155 
156 	/* ignore non SPI controllers */
157 	if (strncmp(acpi_device_bid(adev), SPI_CTL_OBJ_NAME,
158 	    strlen(SPI_CTL_OBJ_NAME)))
159 		return 0;
160 
161 	status = acpi_walk_namespace(ACPI_TYPE_DEVICE, adev->handle, 1,
162 				     s3_wmi_attach_spi_device, NULL,
163 				     &ts_adev, NULL);
164 	if (ACPI_FAILURE(status))
165 		dev_warn(dev, "failed to enumerate SPI slaves\n");
166 
167 	if (!ts_adev)
168 		return 0;
169 
170 	s3_wmi.touchscreen_adev = ts_adev;
171 
172 	return 0;
173 }
174 
175 static int s3_wmi_create_and_register_input(struct platform_device *pdev)
176 {
177 	struct input_dev *input;
178 	int error;
179 
180 	input = devm_input_allocate_device(&pdev->dev);
181 	if (!input)
182 		return -ENOMEM;
183 
184 	input->name = "Lid Switch";
185 	input->phys = "button/input0";
186 	input->id.bustype = BUS_HOST;
187 	input->id.product = 0x0005;
188 
189 	input_set_capability(input, EV_SW, SW_LID);
190 
191 	error = input_register_device(input);
192 	if (error)
193 		goto out_err;
194 
195 	s3_wmi.input = input;
196 
197 	return 0;
198  out_err:
199 	input_free_device(s3_wmi.input);
200 	return error;
201 }
202 
203 static int __init s3_wmi_probe(struct platform_device *pdev)
204 {
205 	int error;
206 
207 	if (!dmi_check_system(surface3_dmi_table))
208 		return -ENODEV;
209 
210 	memset(&s3_wmi, 0, sizeof(s3_wmi));
211 
212 	bus_for_each_dev(&platform_bus_type, NULL, NULL,
213 			 s3_wmi_check_platform_device);
214 
215 	if (!s3_wmi.touchscreen_adev)
216 		return -ENODEV;
217 
218 	acpi_bus_trim(s3_wmi.pnp0c0d_adev);
219 
220 	error = s3_wmi_create_and_register_input(pdev);
221 	if (error)
222 		goto restore_acpi_lid;
223 
224 	acpi_initialize_hp_context(s3_wmi.touchscreen_adev, &s3_wmi.hp,
225 				   s3_wmi_hp_notify, NULL);
226 
227 	s3_wmi_send_lid_state();
228 
229 	return 0;
230 
231  restore_acpi_lid:
232 	acpi_bus_scan(s3_wmi.pnp0c0d_adev->handle);
233 	return error;
234 }
235 
236 static int s3_wmi_remove(struct platform_device *device)
237 {
238 	/* remove the hotplug context from the acpi device */
239 	s3_wmi.touchscreen_adev->hp = NULL;
240 
241 	/* reinstall the actual PNPC0C0D LID default handle */
242 	acpi_bus_scan(s3_wmi.pnp0c0d_adev->handle);
243 	return 0;
244 }
245 
246 static int __maybe_unused s3_wmi_resume(struct device *dev)
247 {
248 	s3_wmi_send_lid_state();
249 	return 0;
250 }
251 static SIMPLE_DEV_PM_OPS(s3_wmi_pm, NULL, s3_wmi_resume);
252 
253 static struct platform_driver s3_wmi_driver = {
254 	.driver = {
255 		.name = "surface3-wmi",
256 		.pm = &s3_wmi_pm,
257 	},
258 	.remove = s3_wmi_remove,
259 };
260 
261 static int __init s3_wmi_init(void)
262 {
263 	int error;
264 
265 	s3_wmi_pdev = platform_device_alloc("surface3-wmi", -1);
266 	if (!s3_wmi_pdev)
267 		return -ENOMEM;
268 
269 	error = platform_device_add(s3_wmi_pdev);
270 	if (error)
271 		goto err_device_put;
272 
273 	error = platform_driver_probe(&s3_wmi_driver, s3_wmi_probe);
274 	if (error)
275 		goto err_device_del;
276 
277 	pr_info("Surface 3 WMI Extras loaded\n");
278 	return 0;
279 
280  err_device_del:
281 	platform_device_del(s3_wmi_pdev);
282  err_device_put:
283 	platform_device_put(s3_wmi_pdev);
284 	return error;
285 }
286 
287 static void __exit s3_wmi_exit(void)
288 {
289 	platform_device_unregister(s3_wmi_pdev);
290 	platform_driver_unregister(&s3_wmi_driver);
291 }
292 
293 module_init(s3_wmi_init);
294 module_exit(s3_wmi_exit);
295