1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * OLPC XO-1.5 ebook switch driver
4 * (based on generic ACPI button driver)
7 * Copyright (C) 2010 One Laptop per Child
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/types.h>
16 #include <linux/input.h>
17 #include <linux/acpi.h>
19 #define MODULE_NAME "xo15-ebook"
21 #define XO15_EBOOK_CLASS MODULE_NAME
22 #define XO15_EBOOK_TYPE_UNKNOWN 0x00
23 #define XO15_EBOOK_NOTIFY_STATUS 0x80
25 #define XO15_EBOOK_SUBCLASS "ebook"
26 #define XO15_EBOOK_HID "XO15EBK"
27 #define XO15_EBOOK_DEVICE_NAME "EBook Switch"
29 MODULE_DESCRIPTION("OLPC XO-1.5 ebook switch driver");
30 MODULE_LICENSE("GPL");
32 static const struct acpi_device_id ebook_device_ids[] = {
33 { XO15_EBOOK_HID, 0 },
36 MODULE_DEVICE_TABLE(acpi, ebook_device_ids);
39 struct input_dev *input;
40 char phys[32]; /* for input device */
43 static int ebook_send_state(struct acpi_device *device)
45 struct ebook_switch *button = acpi_driver_data(device);
46 unsigned long long state;
49 status = acpi_evaluate_integer(device->handle, "EBK", NULL, &state);
50 if (ACPI_FAILURE(status))
53 /* input layer checks if event is redundant */
54 input_report_switch(button->input, SW_TABLET_MODE, !state);
55 input_sync(button->input);
59 static void ebook_switch_notify(struct acpi_device *device, u32 event)
62 case ACPI_FIXED_HARDWARE_EVENT:
63 case XO15_EBOOK_NOTIFY_STATUS:
64 ebook_send_state(device);
67 acpi_handle_debug(device->handle,
68 "Unsupported event [0x%x]\n", event);
73 #ifdef CONFIG_PM_SLEEP
74 static int ebook_switch_resume(struct device *dev)
76 return ebook_send_state(to_acpi_device(dev));
80 static SIMPLE_DEV_PM_OPS(ebook_switch_pm, NULL, ebook_switch_resume);
82 static int ebook_switch_add(struct acpi_device *device)
84 const struct acpi_device_id *id;
85 struct ebook_switch *button;
86 struct input_dev *input;
90 button = kzalloc(sizeof(struct ebook_switch), GFP_KERNEL);
94 device->driver_data = button;
96 button->input = input = input_allocate_device();
102 name = acpi_device_name(device);
103 class = acpi_device_class(device);
105 id = acpi_match_acpi_device(ebook_device_ids, device);
107 dev_err(&device->dev, "Unsupported hid\n");
112 strcpy(name, XO15_EBOOK_DEVICE_NAME);
113 sprintf(class, "%s/%s", XO15_EBOOK_CLASS, XO15_EBOOK_SUBCLASS);
115 snprintf(button->phys, sizeof(button->phys), "%s/button/input0", id->id);
118 input->phys = button->phys;
119 input->id.bustype = BUS_HOST;
120 input->dev.parent = &device->dev;
122 input->evbit[0] = BIT_MASK(EV_SW);
123 set_bit(SW_TABLET_MODE, input->swbit);
125 error = input_register_device(input);
129 ebook_send_state(device);
131 if (device->wakeup.flags.valid) {
132 /* Button's GPE is run-wake GPE */
133 acpi_enable_gpe(device->wakeup.gpe_device,
134 device->wakeup.gpe_number);
135 device_set_wakeup_enable(&device->dev, true);
141 input_free_device(input);
147 static void ebook_switch_remove(struct acpi_device *device)
149 struct ebook_switch *button = acpi_driver_data(device);
151 input_unregister_device(button->input);
155 static struct acpi_driver xo15_ebook_driver = {
157 .class = XO15_EBOOK_CLASS,
158 .ids = ebook_device_ids,
160 .add = ebook_switch_add,
161 .remove = ebook_switch_remove,
162 .notify = ebook_switch_notify,
164 .drv.pm = &ebook_switch_pm,
166 module_acpi_driver(xo15_ebook_driver);