xref: /linux/drivers/platform/x86/xo15-ebook.c (revision 1193e205dbb6feca917dc8e1862ffcdf2194234b)
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)
5  *
6  *  Copyright (C) 2009 Paul Fox <pgf@laptop.org>
7  *  Copyright (C) 2010 One Laptop per Child
8  */
9 
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 
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>
18 
19 #define MODULE_NAME "xo15-ebook"
20 
21 #define XO15_EBOOK_CLASS		MODULE_NAME
22 #define XO15_EBOOK_TYPE_UNKNOWN	0x00
23 #define XO15_EBOOK_NOTIFY_STATUS	0x80
24 
25 #define XO15_EBOOK_SUBCLASS		"ebook"
26 #define XO15_EBOOK_HID			"XO15EBK"
27 #define XO15_EBOOK_DEVICE_NAME		"EBook Switch"
28 
29 MODULE_DESCRIPTION("OLPC XO-1.5 ebook switch driver");
30 MODULE_LICENSE("GPL");
31 
32 static const struct acpi_device_id ebook_device_ids[] = {
33 	{ XO15_EBOOK_HID, 0 },
34 	{ "", 0 },
35 };
36 MODULE_DEVICE_TABLE(acpi, ebook_device_ids);
37 
38 struct ebook_switch {
39 	struct input_dev *input;
40 	char phys[32];			/* for input device */
41 };
42 
ebook_send_state(struct acpi_device * device)43 static int ebook_send_state(struct acpi_device *device)
44 {
45 	struct ebook_switch *button = acpi_driver_data(device);
46 	unsigned long long state;
47 	acpi_status status;
48 
49 	status = acpi_evaluate_integer(device->handle, "EBK", NULL, &state);
50 	if (ACPI_FAILURE(status))
51 		return -EIO;
52 
53 	/* input layer checks if event is redundant */
54 	input_report_switch(button->input, SW_TABLET_MODE, !state);
55 	input_sync(button->input);
56 	return 0;
57 }
58 
ebook_switch_notify(struct acpi_device * device,u32 event)59 static void ebook_switch_notify(struct acpi_device *device, u32 event)
60 {
61 	switch (event) {
62 	case ACPI_FIXED_HARDWARE_EVENT:
63 	case XO15_EBOOK_NOTIFY_STATUS:
64 		ebook_send_state(device);
65 		break;
66 	default:
67 		acpi_handle_debug(device->handle,
68 				  "Unsupported event [0x%x]\n", event);
69 		break;
70 	}
71 }
72 
73 #ifdef CONFIG_PM_SLEEP
ebook_switch_resume(struct device * dev)74 static int ebook_switch_resume(struct device *dev)
75 {
76 	return ebook_send_state(to_acpi_device(dev));
77 }
78 #endif
79 
80 static SIMPLE_DEV_PM_OPS(ebook_switch_pm, NULL, ebook_switch_resume);
81 
ebook_switch_add(struct acpi_device * device)82 static int ebook_switch_add(struct acpi_device *device)
83 {
84 	const struct acpi_device_id *id;
85 	struct ebook_switch *button;
86 	struct input_dev *input;
87 	int error;
88 
89 	button = kzalloc(sizeof(struct ebook_switch), GFP_KERNEL);
90 	if (!button)
91 		return -ENOMEM;
92 
93 	device->driver_data = button;
94 
95 	button->input = input = input_allocate_device();
96 	if (!input) {
97 		error = -ENOMEM;
98 		goto err_free_button;
99 	}
100 
101 	id = acpi_match_acpi_device(ebook_device_ids, device);
102 	if (!id) {
103 		dev_err(&device->dev, "Unsupported hid\n");
104 		error = -ENODEV;
105 		goto err_free_input;
106 	}
107 
108 	strscpy(acpi_device_name(device), XO15_EBOOK_DEVICE_NAME);
109 	strscpy(acpi_device_class(device), XO15_EBOOK_CLASS "/" XO15_EBOOK_SUBCLASS);
110 
111 	snprintf(button->phys, sizeof(button->phys), "%s/button/input0", id->id);
112 
113 	input->name = acpi_device_name(device);
114 	input->phys = button->phys;
115 	input->id.bustype = BUS_HOST;
116 	input->dev.parent = &device->dev;
117 
118 	input->evbit[0] = BIT_MASK(EV_SW);
119 	set_bit(SW_TABLET_MODE, input->swbit);
120 
121 	error = input_register_device(input);
122 	if (error)
123 		goto err_free_input;
124 
125 	ebook_send_state(device);
126 
127 	if (device->wakeup.flags.valid) {
128 		/* Button's GPE is run-wake GPE */
129 		acpi_enable_gpe(device->wakeup.gpe_device,
130 				device->wakeup.gpe_number);
131 		device_set_wakeup_enable(&device->dev, true);
132 	}
133 
134 	return 0;
135 
136  err_free_input:
137 	input_free_device(input);
138  err_free_button:
139 	kfree(button);
140 	return error;
141 }
142 
ebook_switch_remove(struct acpi_device * device)143 static void ebook_switch_remove(struct acpi_device *device)
144 {
145 	struct ebook_switch *button = acpi_driver_data(device);
146 
147 	input_unregister_device(button->input);
148 	kfree(button);
149 }
150 
151 static struct acpi_driver xo15_ebook_driver = {
152 	.name = MODULE_NAME,
153 	.class = XO15_EBOOK_CLASS,
154 	.ids = ebook_device_ids,
155 	.ops = {
156 		.add = ebook_switch_add,
157 		.remove = ebook_switch_remove,
158 		.notify = ebook_switch_notify,
159 	},
160 	.drv.pm = &ebook_switch_pm,
161 };
162 module_acpi_driver(xo15_ebook_driver);
163