blob: c877e56a9bd5760f0ef9151460a6fd5f7d99a6b2 [file] [log] [blame]
Brian Swetland6f2ac002013-01-23 19:45:00 -08001/*
2 * Copyright (C) 2007 Google, Inc.
3 * Copyright (C) 2012 Intel, Inc.
4 *
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 */
15
16#include <linux/module.h>
Brian Swetland6f2ac002013-01-23 19:45:00 -080017#include <linux/interrupt.h>
18#include <linux/types.h>
19#include <linux/input.h>
Lingfeng Yang83257932015-12-18 12:04:43 -080020#include <linux/input/mt.h>
Brian Swetland6f2ac002013-01-23 19:45:00 -080021#include <linux/kernel.h>
22#include <linux/platform_device.h>
23#include <linux/slab.h>
24#include <linux/irq.h>
25#include <linux/io.h>
Jason Hu0581ce02016-02-26 12:06:47 -080026#include <linux/acpi.h>
Brian Swetland6f2ac002013-01-23 19:45:00 -080027
Lingfeng Yang83257932015-12-18 12:04:43 -080028#define GOLDFISH_MAX_FINGERS 5
29
Brian Swetland6f2ac002013-01-23 19:45:00 -080030enum {
31 REG_READ = 0x00,
32 REG_SET_PAGE = 0x00,
33 REG_LEN = 0x04,
34 REG_DATA = 0x08,
35
36 PAGE_NAME = 0x00000,
37 PAGE_EVBITS = 0x10000,
38 PAGE_ABSDATA = 0x20000 | EV_ABS,
39};
40
41struct event_dev {
42 struct input_dev *input;
43 int irq;
44 void __iomem *addr;
45 char name[0];
46};
47
48static irqreturn_t events_interrupt(int irq, void *dev_id)
49{
50 struct event_dev *edev = dev_id;
51 unsigned type, code, value;
52
53 type = __raw_readl(edev->addr + REG_READ);
54 code = __raw_readl(edev->addr + REG_READ);
55 value = __raw_readl(edev->addr + REG_READ);
56
57 input_event(edev->input, type, code, value);
Lingfeng Yang83257932015-12-18 12:04:43 -080058 // Send an extra (EV_SYN, SYN_REPORT, 0x0) event
59 // if a key was pressed. Some keyboard device
60 // drivers may only send the EV_KEY event and
61 // not EV_SYN.
62 // Note that sending an extra SYN_REPORT is not
63 // necessary nor correct protocol with other
64 // devices such as touchscreens, which will send
65 // their own SYN_REPORT's when sufficient event
66 // information has been collected (e.g., for
67 // touchscreens, when pressure and X/Y coordinates
68 // have been received). Hence, we will only send
69 // this extra SYN_REPORT if type == EV_KEY.
70 if (type == EV_KEY) {
71 input_sync(edev->input);
72 }
Brian Swetland6f2ac002013-01-23 19:45:00 -080073 return IRQ_HANDLED;
74}
75
76static void events_import_bits(struct event_dev *edev,
77 unsigned long bits[], unsigned type, size_t count)
78{
79 void __iomem *addr = edev->addr;
80 int i, j;
81 size_t size;
82 uint8_t val;
83
84 __raw_writel(PAGE_EVBITS | type, addr + REG_SET_PAGE);
85
86 size = __raw_readl(addr + REG_LEN) * 8;
87 if (size < count)
88 count = size;
89
90 addr += REG_DATA;
91 for (i = 0; i < count; i += 8) {
92 val = __raw_readb(addr++);
93 for (j = 0; j < 8; j++)
94 if (val & 1 << j)
95 set_bit(i + j, bits);
96 }
97}
98
99static void events_import_abs_params(struct event_dev *edev)
100{
101 struct input_dev *input_dev = edev->input;
102 void __iomem *addr = edev->addr;
103 u32 val[4];
104 int count;
105 int i, j;
106
107 __raw_writel(PAGE_ABSDATA, addr + REG_SET_PAGE);
108
109 count = __raw_readl(addr + REG_LEN) / sizeof(val);
110 if (count > ABS_MAX)
111 count = ABS_MAX;
112
113 for (i = 0; i < count; i++) {
114 if (!test_bit(i, input_dev->absbit))
115 continue;
116
117 for (j = 0; j < ARRAY_SIZE(val); j++) {
118 int offset = (i * ARRAY_SIZE(val) + j) * sizeof(u32);
119 val[j] = __raw_readl(edev->addr + REG_DATA + offset);
120 }
121
122 input_set_abs_params(input_dev, i,
123 val[0], val[1], val[2], val[3]);
124 }
125}
126
127static int events_probe(struct platform_device *pdev)
128{
129 struct input_dev *input_dev;
130 struct event_dev *edev;
131 struct resource *res;
132 unsigned keymapnamelen;
133 void __iomem *addr;
134 int irq;
135 int i;
136 int error;
137
138 irq = platform_get_irq(pdev, 0);
139 if (irq < 0)
140 return -EINVAL;
141
142 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
143 if (!res)
144 return -EINVAL;
145
146 addr = devm_ioremap(&pdev->dev, res->start, 4096);
147 if (!addr)
148 return -ENOMEM;
149
150 __raw_writel(PAGE_NAME, addr + REG_SET_PAGE);
151 keymapnamelen = __raw_readl(addr + REG_LEN);
152
153 edev = devm_kzalloc(&pdev->dev,
154 sizeof(struct event_dev) + keymapnamelen + 1,
155 GFP_KERNEL);
156 if (!edev)
157 return -ENOMEM;
158
159 input_dev = devm_input_allocate_device(&pdev->dev);
160 if (!input_dev)
161 return -ENOMEM;
162
163 edev->input = input_dev;
164 edev->addr = addr;
165 edev->irq = irq;
166
167 for (i = 0; i < keymapnamelen; i++)
168 edev->name[i] = __raw_readb(edev->addr + REG_DATA + i);
169
170 pr_debug("events_probe() keymap=%s\n", edev->name);
171
172 input_dev->name = edev->name;
173 input_dev->id.bustype = BUS_HOST;
Lingfeng Yang83257932015-12-18 12:04:43 -0800174 // Set the Goldfish Device to be multi-touch.
175 // In the Ranchu kernel, there is multi-touch-specific
176 // code for handling ABS_MT_SLOT events.
177 // See drivers/input/input.c:input_handle_abs_event.
178 // If we do not issue input_mt_init_slots,
179 // the kernel will filter out needed ABS_MT_SLOT
180 // events when we touch the screen in more than one place,
181 // preventing multi-touch with more than one finger from working.
182 input_mt_init_slots(input_dev, GOLDFISH_MAX_FINGERS, 0);
Brian Swetland6f2ac002013-01-23 19:45:00 -0800183
184 events_import_bits(edev, input_dev->evbit, EV_SYN, EV_MAX);
185 events_import_bits(edev, input_dev->keybit, EV_KEY, KEY_MAX);
186 events_import_bits(edev, input_dev->relbit, EV_REL, REL_MAX);
187 events_import_bits(edev, input_dev->absbit, EV_ABS, ABS_MAX);
188 events_import_bits(edev, input_dev->mscbit, EV_MSC, MSC_MAX);
189 events_import_bits(edev, input_dev->ledbit, EV_LED, LED_MAX);
190 events_import_bits(edev, input_dev->sndbit, EV_SND, SND_MAX);
191 events_import_bits(edev, input_dev->ffbit, EV_FF, FF_MAX);
192 events_import_bits(edev, input_dev->swbit, EV_SW, SW_MAX);
193
194 events_import_abs_params(edev);
195
196 error = devm_request_irq(&pdev->dev, edev->irq, events_interrupt, 0,
197 "goldfish-events-keypad", edev);
198 if (error)
199 return error;
200
201 error = input_register_device(input_dev);
202 if (error)
203 return error;
204
205 return 0;
206}
207
Greg Hackmann8c5dc5a2016-02-26 12:05:02 -0800208static const struct of_device_id goldfish_events_of_match[] = {
209 { .compatible = "google,goldfish-events-keypad", },
210 {},
211};
212MODULE_DEVICE_TABLE(of, goldfish_events_of_match);
213
Jason Hu0581ce02016-02-26 12:06:47 -0800214#ifdef CONFIG_ACPI
215static const struct acpi_device_id goldfish_events_acpi_match[] = {
216 { "GFSH0002", 0 },
217 { },
218};
219MODULE_DEVICE_TABLE(acpi, goldfish_events_acpi_match);
220#endif
221
Brian Swetland6f2ac002013-01-23 19:45:00 -0800222static struct platform_driver events_driver = {
223 .probe = events_probe,
224 .driver = {
Brian Swetland6f2ac002013-01-23 19:45:00 -0800225 .name = "goldfish_events",
Greg Hackmann8c5dc5a2016-02-26 12:05:02 -0800226 .of_match_table = goldfish_events_of_match,
Jason Hu0581ce02016-02-26 12:06:47 -0800227 .acpi_match_table = ACPI_PTR(goldfish_events_acpi_match),
Brian Swetland6f2ac002013-01-23 19:45:00 -0800228 },
229};
230
231module_platform_driver(events_driver);
232
233MODULE_AUTHOR("Brian Swetland");
234MODULE_DESCRIPTION("Goldfish Event Device");
235MODULE_LICENSE("GPL");