1 // SPDX-License-Identifier: GPL-2.0
2 // Driver to detect Tablet Mode for ChromeOS convertible.
3 //
4 // Copyright (C) 2017 Google, Inc.
5 // Author: Gwendal Grignou <gwendal@chromium.org>
6 //
7 // On Chromebook using ACPI, this device listens for notification
8 // from GOOG0006 and issue method TBMC to retrieve the status.
9 //
10 // GOOG0006 issues the notification when it receives EC_HOST_EVENT_MODE_CHANGE
11 // from the EC.
12 // Method TBMC reads EC_ACPI_MEM_DEVICE_ORIENTATION byte from the shared
13 // memory region.
14 
15 #include <linux/acpi.h>
16 #include <linux/input.h>
17 #include <linux/io.h>
18 #include <linux/module.h>
19 #include <linux/printk.h>
20 
21 #define DRV_NAME "chromeos_tbmc"
22 #define ACPI_DRV_NAME "GOOG0006"
23 
24 static int chromeos_tbmc_query_switch(struct acpi_device *adev,
25 				     struct input_dev *idev)
26 {
27 	unsigned long long state;
28 	acpi_status status;
29 
30 	status = acpi_evaluate_integer(adev->handle, "TBMC", NULL, &state);
31 	if (ACPI_FAILURE(status))
32 		return -ENODEV;
33 
34 	/* input layer checks if event is redundant */
35 	input_report_switch(idev, SW_TABLET_MODE, state);
36 	input_sync(idev);
37 
38 	return 0;
39 }
40 
41 static __maybe_unused int chromeos_tbmc_resume(struct device *dev)
42 {
43 	struct acpi_device *adev = to_acpi_device(dev);
44 
45 	return chromeos_tbmc_query_switch(adev, adev->driver_data);
46 }
47 
48 static void chromeos_tbmc_notify(struct acpi_device *adev, u32 event)
49 {
50 	switch (event) {
51 	case 0x80:
52 		chromeos_tbmc_query_switch(adev, adev->driver_data);
53 		break;
54 	default:
55 		dev_err(&adev->dev, "Unexpected event: 0x%08X\n", event);
56 	}
57 }
58 
59 static int chromeos_tbmc_open(struct input_dev *idev)
60 {
61 	struct acpi_device *adev = input_get_drvdata(idev);
62 
63 	return chromeos_tbmc_query_switch(adev, idev);
64 }
65 
66 static int chromeos_tbmc_add(struct acpi_device *adev)
67 {
68 	struct input_dev *idev;
69 	struct device *dev = &adev->dev;
70 	int ret;
71 
72 	idev = devm_input_allocate_device(dev);
73 	if (!idev)
74 		return -ENOMEM;
75 
76 	idev->name = "Tablet Mode Switch";
77 	idev->phys = acpi_device_hid(adev);
78 
79 	idev->id.bustype = BUS_HOST;
80 	idev->id.version = 1;
81 	idev->id.product = 0;
82 	idev->open = chromeos_tbmc_open;
83 
84 	input_set_drvdata(idev, adev);
85 	adev->driver_data = idev;
86 
87 	input_set_capability(idev, EV_SW, SW_TABLET_MODE);
88 	ret = input_register_device(idev);
89 	if (ret) {
90 		dev_err(dev, "cannot register input device\n");
91 		return ret;
92 	}
93 	return 0;
94 }
95 
96 static const struct acpi_device_id chromeos_tbmc_acpi_device_ids[] = {
97 	{ ACPI_DRV_NAME, 0 },
98 	{ }
99 };
100 MODULE_DEVICE_TABLE(acpi, chromeos_tbmc_acpi_device_ids);
101 
102 static const SIMPLE_DEV_PM_OPS(chromeos_tbmc_pm_ops, NULL,
103 		chromeos_tbmc_resume);
104 
105 static struct acpi_driver chromeos_tbmc_driver = {
106 	.name = DRV_NAME,
107 	.class = DRV_NAME,
108 	.ids = chromeos_tbmc_acpi_device_ids,
109 	.ops = {
110 		.add = chromeos_tbmc_add,
111 		.notify = chromeos_tbmc_notify,
112 	},
113 	.drv.pm = &chromeos_tbmc_pm_ops,
114 };
115 
116 module_acpi_driver(chromeos_tbmc_driver);
117 
118 MODULE_LICENSE("GPL v2");
119 MODULE_DESCRIPTION("ChromeOS ACPI tablet switch driver");
120