1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Helper code to detect 360 degree hinges (yoga) style 2-in-1 devices using 2 accelerometers
4  * to allow the OS to determine the angle between the display and the base of the device.
5  *
6  * On Windows these are read by a special HingeAngleService process which calls undocumented
7  * ACPI methods, to let the firmware know if the 2-in-1 is in tablet- or laptop-mode.
8  * The firmware may use this to disable the kbd and touchpad to avoid spurious input in
9  * tablet-mode as well as to report SW_TABLET_MODE info to the OS.
10  *
11  * Since Linux does not call these undocumented methods, the SW_TABLET_MODE info reported
12  * by various drivers/platform/x86 drivers is incorrect. These drivers use the detection
13  * code in this file to disable SW_TABLET_MODE reporting to avoid reporting broken info
14  * (instead userspace can derive the status itself by directly reading the 2 accels).
15  */
16 
17 #include <linux/acpi.h>
18 #include <linux/i2c.h>
19 
20 static int dual_accel_i2c_resource_count(struct acpi_resource *ares, void *data)
21 {
22 	struct acpi_resource_i2c_serialbus *sb;
23 	int *count = data;
24 
25 	if (i2c_acpi_get_i2c_resource(ares, &sb))
26 		*count = *count + 1;
27 
28 	return 1;
29 }
30 
31 static int dual_accel_i2c_client_count(struct acpi_device *adev)
32 {
33 	int ret, count = 0;
34 	LIST_HEAD(r);
35 
36 	ret = acpi_dev_get_resources(adev, &r, dual_accel_i2c_resource_count, &count);
37 	if (ret < 0)
38 		return ret;
39 
40 	acpi_dev_free_resource_list(&r);
41 	return count;
42 }
43 
44 static bool dual_accel_detect_bosc0200(void)
45 {
46 	struct acpi_device *adev;
47 	int count;
48 
49 	adev = acpi_dev_get_first_match_dev("BOSC0200", NULL, -1);
50 	if (!adev)
51 		return false;
52 
53 	count = dual_accel_i2c_client_count(adev);
54 
55 	acpi_dev_put(adev);
56 
57 	return count == 2;
58 }
59 
60 static bool dual_accel_detect(void)
61 {
62 	/* Systems which use a pair of accels with KIOX010A / KIOX020A ACPI ids */
63 	if (acpi_dev_present("KIOX010A", NULL, -1) &&
64 	    acpi_dev_present("KIOX020A", NULL, -1))
65 		return true;
66 
67 	/* Systems which use a single DUAL250E ACPI device to model 2 accels */
68 	if (acpi_dev_present("DUAL250E", NULL, -1))
69 		return true;
70 
71 	/* Systems which use a single BOSC0200 ACPI device to model 2 accels */
72 	if (dual_accel_detect_bosc0200())
73 		return true;
74 
75 	return false;
76 }
77