Brian Swetland | 6f2ac00 | 2013-01-23 19:45:00 -0800 | [diff] [blame] | 1 | /* |
| 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 Swetland | 6f2ac00 | 2013-01-23 19:45:00 -0800 | [diff] [blame] | 17 | #include <linux/interrupt.h> |
| 18 | #include <linux/types.h> |
| 19 | #include <linux/input.h> |
Lingfeng Yang | 8325793 | 2015-12-18 12:04:43 -0800 | [diff] [blame] | 20 | #include <linux/input/mt.h> |
Brian Swetland | 6f2ac00 | 2013-01-23 19:45:00 -0800 | [diff] [blame] | 21 | #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 Hu | 0581ce0 | 2016-02-26 12:06:47 -0800 | [diff] [blame] | 26 | #include <linux/acpi.h> |
Brian Swetland | 6f2ac00 | 2013-01-23 19:45:00 -0800 | [diff] [blame] | 27 | |
Lingfeng Yang | 8325793 | 2015-12-18 12:04:43 -0800 | [diff] [blame] | 28 | #define GOLDFISH_MAX_FINGERS 5 |
| 29 | |
Brian Swetland | 6f2ac00 | 2013-01-23 19:45:00 -0800 | [diff] [blame] | 30 | enum { |
| 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 | |
| 41 | struct event_dev { |
| 42 | struct input_dev *input; |
| 43 | int irq; |
| 44 | void __iomem *addr; |
| 45 | char name[0]; |
| 46 | }; |
| 47 | |
| 48 | static 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 Yang | 8325793 | 2015-12-18 12:04:43 -0800 | [diff] [blame] | 58 | // 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 Swetland | 6f2ac00 | 2013-01-23 19:45:00 -0800 | [diff] [blame] | 73 | return IRQ_HANDLED; |
| 74 | } |
| 75 | |
| 76 | static 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 | |
| 99 | static 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 | |
| 127 | static 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 Yang | 8325793 | 2015-12-18 12:04:43 -0800 | [diff] [blame] | 174 | // 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 Swetland | 6f2ac00 | 2013-01-23 19:45:00 -0800 | [diff] [blame] | 183 | |
| 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 Hackmann | 8c5dc5a | 2016-02-26 12:05:02 -0800 | [diff] [blame] | 208 | static const struct of_device_id goldfish_events_of_match[] = { |
| 209 | { .compatible = "google,goldfish-events-keypad", }, |
| 210 | {}, |
| 211 | }; |
| 212 | MODULE_DEVICE_TABLE(of, goldfish_events_of_match); |
| 213 | |
Jason Hu | 0581ce0 | 2016-02-26 12:06:47 -0800 | [diff] [blame] | 214 | #ifdef CONFIG_ACPI |
| 215 | static const struct acpi_device_id goldfish_events_acpi_match[] = { |
| 216 | { "GFSH0002", 0 }, |
| 217 | { }, |
| 218 | }; |
| 219 | MODULE_DEVICE_TABLE(acpi, goldfish_events_acpi_match); |
| 220 | #endif |
| 221 | |
Brian Swetland | 6f2ac00 | 2013-01-23 19:45:00 -0800 | [diff] [blame] | 222 | static struct platform_driver events_driver = { |
| 223 | .probe = events_probe, |
| 224 | .driver = { |
Brian Swetland | 6f2ac00 | 2013-01-23 19:45:00 -0800 | [diff] [blame] | 225 | .name = "goldfish_events", |
Greg Hackmann | 8c5dc5a | 2016-02-26 12:05:02 -0800 | [diff] [blame] | 226 | .of_match_table = goldfish_events_of_match, |
Jason Hu | 0581ce0 | 2016-02-26 12:06:47 -0800 | [diff] [blame] | 227 | .acpi_match_table = ACPI_PTR(goldfish_events_acpi_match), |
Brian Swetland | 6f2ac00 | 2013-01-23 19:45:00 -0800 | [diff] [blame] | 228 | }, |
| 229 | }; |
| 230 | |
| 231 | module_platform_driver(events_driver); |
| 232 | |
| 233 | MODULE_AUTHOR("Brian Swetland"); |
| 234 | MODULE_DESCRIPTION("Goldfish Event Device"); |
| 235 | MODULE_LICENSE("GPL"); |