blob: 4b1377bd59446a50405e00414418adf9ac8f8381 [file] [log] [blame]
Paul Fox89ca1172011-02-03 16:27:55 +00001/*
2 * OLPC XO-1.5 ebook switch driver
3 * (based on generic ACPI button driver)
4 *
5 * Copyright (C) 2009 Paul Fox <pgf@laptop.org>
6 * Copyright (C) 2010 One Laptop per Child
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or (at
11 * your option) any later version.
12 */
13
Joe Perchesad3f2f02011-03-29 15:21:54 -070014#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
Paul Fox89ca1172011-02-03 16:27:55 +000016#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/init.h>
19#include <linux/types.h>
20#include <linux/input.h>
21#include <acpi/acpi_bus.h>
22#include <acpi/acpi_drivers.h>
23
24#define MODULE_NAME "xo15-ebook"
Paul Fox89ca1172011-02-03 16:27:55 +000025
26#define XO15_EBOOK_CLASS MODULE_NAME
27#define XO15_EBOOK_TYPE_UNKNOWN 0x00
28#define XO15_EBOOK_NOTIFY_STATUS 0x80
29
30#define XO15_EBOOK_SUBCLASS "ebook"
31#define XO15_EBOOK_HID "XO15EBK"
32#define XO15_EBOOK_DEVICE_NAME "EBook Switch"
33
34ACPI_MODULE_NAME(MODULE_NAME);
35
36MODULE_DESCRIPTION("OLPC XO-1.5 ebook switch driver");
37MODULE_LICENSE("GPL");
38
39static const struct acpi_device_id ebook_device_ids[] = {
40 { XO15_EBOOK_HID, 0 },
41 { "", 0 },
42};
43MODULE_DEVICE_TABLE(acpi, ebook_device_ids);
44
45struct ebook_switch {
46 struct input_dev *input;
47 char phys[32]; /* for input device */
48};
49
50static int ebook_send_state(struct acpi_device *device)
51{
52 struct ebook_switch *button = acpi_driver_data(device);
53 unsigned long long state;
54 acpi_status status;
55
56 status = acpi_evaluate_integer(device->handle, "EBK", NULL, &state);
57 if (ACPI_FAILURE(status))
58 return -EIO;
59
60 /* input layer checks if event is redundant */
61 input_report_switch(button->input, SW_TABLET_MODE, !state);
62 input_sync(button->input);
63 return 0;
64}
65
66static void ebook_switch_notify(struct acpi_device *device, u32 event)
67{
68 switch (event) {
69 case ACPI_FIXED_HARDWARE_EVENT:
70 case XO15_EBOOK_NOTIFY_STATUS:
71 ebook_send_state(device);
72 break;
73 default:
74 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
75 "Unsupported event [0x%x]\n", event));
76 break;
77 }
78}
79
Rafael J. Wysocki3567a4e2012-08-09 23:00:13 +020080#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki44cb98c2012-06-27 23:27:55 +020081static int ebook_switch_resume(struct device *dev)
Paul Fox89ca1172011-02-03 16:27:55 +000082{
Rafael J. Wysocki44cb98c2012-06-27 23:27:55 +020083 return ebook_send_state(to_acpi_device(dev));
Paul Fox89ca1172011-02-03 16:27:55 +000084}
Rafael J. Wysocki3567a4e2012-08-09 23:00:13 +020085#endif
Paul Fox89ca1172011-02-03 16:27:55 +000086
Rafael J. Wysocki44cb98c2012-06-27 23:27:55 +020087static SIMPLE_DEV_PM_OPS(ebook_switch_pm, NULL, ebook_switch_resume);
88
Paul Fox89ca1172011-02-03 16:27:55 +000089static int ebook_switch_add(struct acpi_device *device)
90{
91 struct ebook_switch *button;
92 struct input_dev *input;
93 const char *hid = acpi_device_hid(device);
94 char *name, *class;
95 int error;
96
97 button = kzalloc(sizeof(struct ebook_switch), GFP_KERNEL);
98 if (!button)
99 return -ENOMEM;
100
101 device->driver_data = button;
102
103 button->input = input = input_allocate_device();
104 if (!input) {
105 error = -ENOMEM;
106 goto err_free_button;
107 }
108
109 name = acpi_device_name(device);
110 class = acpi_device_class(device);
111
112 if (strcmp(hid, XO15_EBOOK_HID)) {
Joe Perchesad3f2f02011-03-29 15:21:54 -0700113 pr_err("Unsupported hid [%s]\n", hid);
Paul Fox89ca1172011-02-03 16:27:55 +0000114 error = -ENODEV;
115 goto err_free_input;
116 }
117
118 strcpy(name, XO15_EBOOK_DEVICE_NAME);
119 sprintf(class, "%s/%s", XO15_EBOOK_CLASS, XO15_EBOOK_SUBCLASS);
120
121 snprintf(button->phys, sizeof(button->phys), "%s/button/input0", hid);
122
123 input->name = name;
124 input->phys = button->phys;
125 input->id.bustype = BUS_HOST;
126 input->dev.parent = &device->dev;
127
128 input->evbit[0] = BIT_MASK(EV_SW);
129 set_bit(SW_TABLET_MODE, input->swbit);
130
131 error = input_register_device(input);
132 if (error)
133 goto err_free_input;
134
135 ebook_send_state(device);
136
137 if (device->wakeup.flags.valid) {
138 /* Button's GPE is run-wake GPE */
139 acpi_enable_gpe(device->wakeup.gpe_device,
140 device->wakeup.gpe_number);
Paul Fox89ca1172011-02-03 16:27:55 +0000141 device_set_wakeup_enable(&device->dev, true);
142 }
143
144 return 0;
145
146 err_free_input:
147 input_free_device(input);
148 err_free_button:
149 kfree(button);
150 return error;
151}
152
Rafael J. Wysocki51fac832013-01-24 00:24:48 +0100153static int ebook_switch_remove(struct acpi_device *device)
Paul Fox89ca1172011-02-03 16:27:55 +0000154{
155 struct ebook_switch *button = acpi_driver_data(device);
156
157 input_unregister_device(button->input);
158 kfree(button);
159 return 0;
160}
161
162static struct acpi_driver xo15_ebook_driver = {
163 .name = MODULE_NAME,
164 .class = XO15_EBOOK_CLASS,
165 .ids = ebook_device_ids,
166 .ops = {
167 .add = ebook_switch_add,
Paul Fox89ca1172011-02-03 16:27:55 +0000168 .remove = ebook_switch_remove,
169 .notify = ebook_switch_notify,
170 },
Rafael J. Wysocki44cb98c2012-06-27 23:27:55 +0200171 .drv.pm = &ebook_switch_pm,
Paul Fox89ca1172011-02-03 16:27:55 +0000172};
Mika Westerberga509d222012-09-07 10:31:49 +0300173module_acpi_driver(xo15_ebook_driver);