Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / platform / x86 / xo15-ebook.c
blob8337c99d2ce25bab372cb82095fdb6a574eed74d
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * OLPC XO-1.5 ebook switch driver
4 * (based on generic ACPI button driver)
6 * Copyright (C) 2009 Paul Fox <pgf@laptop.org>
7 * Copyright (C) 2010 One Laptop per Child
8 */
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 ACPI_MODULE_NAME(MODULE_NAME);
31 MODULE_DESCRIPTION("OLPC XO-1.5 ebook switch driver");
32 MODULE_LICENSE("GPL");
34 static const struct acpi_device_id ebook_device_ids[] = {
35 { XO15_EBOOK_HID, 0 },
36 { "", 0 },
38 MODULE_DEVICE_TABLE(acpi, ebook_device_ids);
40 struct ebook_switch {
41 struct input_dev *input;
42 char phys[32]; /* for input device */
45 static int ebook_send_state(struct acpi_device *device)
47 struct ebook_switch *button = acpi_driver_data(device);
48 unsigned long long state;
49 acpi_status status;
51 status = acpi_evaluate_integer(device->handle, "EBK", NULL, &state);
52 if (ACPI_FAILURE(status))
53 return -EIO;
55 /* input layer checks if event is redundant */
56 input_report_switch(button->input, SW_TABLET_MODE, !state);
57 input_sync(button->input);
58 return 0;
61 static void ebook_switch_notify(struct acpi_device *device, u32 event)
63 switch (event) {
64 case ACPI_FIXED_HARDWARE_EVENT:
65 case XO15_EBOOK_NOTIFY_STATUS:
66 ebook_send_state(device);
67 break;
68 default:
69 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
70 "Unsupported event [0x%x]\n", event));
71 break;
75 #ifdef CONFIG_PM_SLEEP
76 static int ebook_switch_resume(struct device *dev)
78 return ebook_send_state(to_acpi_device(dev));
80 #endif
82 static SIMPLE_DEV_PM_OPS(ebook_switch_pm, NULL, ebook_switch_resume);
84 static int ebook_switch_add(struct acpi_device *device)
86 struct ebook_switch *button;
87 struct input_dev *input;
88 const char *hid = acpi_device_hid(device);
89 char *name, *class;
90 int error;
92 button = kzalloc(sizeof(struct ebook_switch), GFP_KERNEL);
93 if (!button)
94 return -ENOMEM;
96 device->driver_data = button;
98 button->input = input = input_allocate_device();
99 if (!input) {
100 error = -ENOMEM;
101 goto err_free_button;
104 name = acpi_device_name(device);
105 class = acpi_device_class(device);
107 if (strcmp(hid, XO15_EBOOK_HID)) {
108 pr_err("Unsupported hid [%s]\n", hid);
109 error = -ENODEV;
110 goto err_free_input;
113 strcpy(name, XO15_EBOOK_DEVICE_NAME);
114 sprintf(class, "%s/%s", XO15_EBOOK_CLASS, XO15_EBOOK_SUBCLASS);
116 snprintf(button->phys, sizeof(button->phys), "%s/button/input0", hid);
118 input->name = name;
119 input->phys = button->phys;
120 input->id.bustype = BUS_HOST;
121 input->dev.parent = &device->dev;
123 input->evbit[0] = BIT_MASK(EV_SW);
124 set_bit(SW_TABLET_MODE, input->swbit);
126 error = input_register_device(input);
127 if (error)
128 goto err_free_input;
130 ebook_send_state(device);
132 if (device->wakeup.flags.valid) {
133 /* Button's GPE is run-wake GPE */
134 acpi_enable_gpe(device->wakeup.gpe_device,
135 device->wakeup.gpe_number);
136 device_set_wakeup_enable(&device->dev, true);
139 return 0;
141 err_free_input:
142 input_free_device(input);
143 err_free_button:
144 kfree(button);
145 return error;
148 static int ebook_switch_remove(struct acpi_device *device)
150 struct ebook_switch *button = acpi_driver_data(device);
152 input_unregister_device(button->input);
153 kfree(button);
154 return 0;
157 static struct acpi_driver xo15_ebook_driver = {
158 .name = MODULE_NAME,
159 .class = XO15_EBOOK_CLASS,
160 .ids = ebook_device_ids,
161 .ops = {
162 .add = ebook_switch_add,
163 .remove = ebook_switch_remove,
164 .notify = ebook_switch_notify,
166 .drv.pm = &ebook_switch_pm,
168 module_acpi_driver(xo15_ebook_driver);