1 // SPDX-License-Identifier: GPL-2.0+
2 // Driver to detect Tablet Mode for ChromeOS convertible.
4 // Copyright (C) 2017 Google, Inc.
5 // Author: Gwendal Grignou <gwendal@chromium.org>
7 #include <linux/acpi.h>
8 #include <linux/input.h>
10 #include <linux/module.h>
11 #include <linux/printk.h>
13 #define DRV_NAME "chromeos_tbmc"
14 #define ACPI_DRV_NAME "GOOG0006"
16 static int chromeos_tbmc_query_switch(struct acpi_device
*adev
,
17 struct input_dev
*idev
)
19 unsigned long long state
;
22 status
= acpi_evaluate_integer(adev
->handle
, "TBMC", NULL
, &state
);
23 if (ACPI_FAILURE(status
))
26 /* input layer checks if event is redundant */
27 input_report_switch(idev
, SW_TABLET_MODE
, state
);
33 static __maybe_unused
int chromeos_tbmc_resume(struct device
*dev
)
35 struct acpi_device
*adev
= to_acpi_device(dev
);
37 return chromeos_tbmc_query_switch(adev
, adev
->driver_data
);
40 static void chromeos_tbmc_notify(struct acpi_device
*adev
, u32 event
)
44 chromeos_tbmc_query_switch(adev
, adev
->driver_data
);
47 dev_err(&adev
->dev
, "Unexpected event: 0x%08X\n", event
);
51 static int chromeos_tbmc_open(struct input_dev
*idev
)
53 struct acpi_device
*adev
= input_get_drvdata(idev
);
55 return chromeos_tbmc_query_switch(adev
, idev
);
58 static int chromeos_tbmc_add(struct acpi_device
*adev
)
60 struct input_dev
*idev
;
61 struct device
*dev
= &adev
->dev
;
64 idev
= devm_input_allocate_device(dev
);
68 idev
->name
= "Tablet Mode Switch";
69 idev
->phys
= acpi_device_hid(adev
);
71 idev
->id
.bustype
= BUS_HOST
;
74 idev
->open
= chromeos_tbmc_open
;
76 input_set_drvdata(idev
, adev
);
77 adev
->driver_data
= idev
;
79 input_set_capability(idev
, EV_SW
, SW_TABLET_MODE
);
80 ret
= input_register_device(idev
);
82 dev_err(dev
, "cannot register input device\n");
88 static const struct acpi_device_id chromeos_tbmc_acpi_device_ids
[] = {
92 MODULE_DEVICE_TABLE(acpi
, chromeos_tbmc_acpi_device_ids
);
94 static const SIMPLE_DEV_PM_OPS(chromeos_tbmc_pm_ops
, NULL
,
95 chromeos_tbmc_resume
);
97 static struct acpi_driver chromeos_tbmc_driver
= {
100 .ids
= chromeos_tbmc_acpi_device_ids
,
102 .add
= chromeos_tbmc_add
,
103 .notify
= chromeos_tbmc_notify
,
105 .drv
.pm
= &chromeos_tbmc_pm_ops
,
108 module_acpi_driver(chromeos_tbmc_driver
);
110 MODULE_LICENSE("GPL v2");
111 MODULE_DESCRIPTION("ChromeOS ACPI tablet switch driver");