Fabio Estevam | a150c1f | 2018-07-17 04:06:00 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Zhang Jiejing | 59bae1d | 2011-11-12 00:03:18 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Driver for EETI eGalax Multiple Touch Controller |
| 4 | * |
| 5 | * Copyright (C) 2011 Freescale Semiconductor, Inc. |
| 6 | * |
| 7 | * based on max11801_ts.c |
Zhang Jiejing | 59bae1d | 2011-11-12 00:03:18 -0800 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | /* EETI eGalax serial touch screen controller is a I2C based multiple |
| 11 | * touch screen controller, it supports 5 point multiple touch. */ |
| 12 | |
| 13 | /* TODO: |
| 14 | - auto idle mode support |
| 15 | */ |
| 16 | |
| 17 | #include <linux/module.h> |
Zhang Jiejing | 59bae1d | 2011-11-12 00:03:18 -0800 | [diff] [blame] | 18 | #include <linux/i2c.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/input.h> |
| 21 | #include <linux/irq.h> |
| 22 | #include <linux/gpio.h> |
| 23 | #include <linux/delay.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/bitops.h> |
| 26 | #include <linux/input/mt.h> |
Hui Wang | ae495e8 | 2012-10-25 00:38:01 -0700 | [diff] [blame] | 27 | #include <linux/of_gpio.h> |
Zhang Jiejing | 59bae1d | 2011-11-12 00:03:18 -0800 | [diff] [blame] | 28 | |
| 29 | /* |
| 30 | * Mouse Mode: some panel may configure the controller to mouse mode, |
| 31 | * which can only report one point at a given time. |
| 32 | * This driver will ignore events in this mode. |
| 33 | */ |
| 34 | #define REPORT_MODE_MOUSE 0x1 |
| 35 | /* |
| 36 | * Vendor Mode: this mode is used to transfer some vendor specific |
| 37 | * messages. |
| 38 | * This driver will ignore events in this mode. |
| 39 | */ |
| 40 | #define REPORT_MODE_VENDOR 0x3 |
| 41 | /* Multiple Touch Mode */ |
| 42 | #define REPORT_MODE_MTTOUCH 0x4 |
| 43 | |
| 44 | #define MAX_SUPPORT_POINTS 5 |
| 45 | |
| 46 | #define EVENT_VALID_OFFSET 7 |
| 47 | #define EVENT_VALID_MASK (0x1 << EVENT_VALID_OFFSET) |
| 48 | #define EVENT_ID_OFFSET 2 |
| 49 | #define EVENT_ID_MASK (0xf << EVENT_ID_OFFSET) |
| 50 | #define EVENT_IN_RANGE (0x1 << 1) |
| 51 | #define EVENT_DOWN_UP (0X1 << 0) |
| 52 | |
| 53 | #define MAX_I2C_DATA_LEN 10 |
| 54 | |
| 55 | #define EGALAX_MAX_X 32760 |
| 56 | #define EGALAX_MAX_Y 32760 |
| 57 | #define EGALAX_MAX_TRIES 100 |
| 58 | |
| 59 | struct egalax_ts { |
| 60 | struct i2c_client *client; |
| 61 | struct input_dev *input_dev; |
| 62 | }; |
| 63 | |
| 64 | static irqreturn_t egalax_ts_interrupt(int irq, void *dev_id) |
| 65 | { |
| 66 | struct egalax_ts *ts = dev_id; |
| 67 | struct input_dev *input_dev = ts->input_dev; |
| 68 | struct i2c_client *client = ts->client; |
| 69 | u8 buf[MAX_I2C_DATA_LEN]; |
| 70 | int id, ret, x, y, z; |
| 71 | int tries = 0; |
| 72 | bool down, valid; |
| 73 | u8 state; |
| 74 | |
| 75 | do { |
| 76 | ret = i2c_master_recv(client, buf, MAX_I2C_DATA_LEN); |
| 77 | } while (ret == -EAGAIN && tries++ < EGALAX_MAX_TRIES); |
| 78 | |
| 79 | if (ret < 0) |
| 80 | return IRQ_HANDLED; |
| 81 | |
| 82 | if (buf[0] != REPORT_MODE_MTTOUCH) { |
| 83 | /* ignore mouse events and vendor events */ |
| 84 | return IRQ_HANDLED; |
| 85 | } |
| 86 | |
| 87 | state = buf[1]; |
| 88 | x = (buf[3] << 8) | buf[2]; |
| 89 | y = (buf[5] << 8) | buf[4]; |
| 90 | z = (buf[7] << 8) | buf[6]; |
| 91 | |
| 92 | valid = state & EVENT_VALID_MASK; |
| 93 | id = (state & EVENT_ID_MASK) >> EVENT_ID_OFFSET; |
| 94 | down = state & EVENT_DOWN_UP; |
| 95 | |
| 96 | if (!valid || id > MAX_SUPPORT_POINTS) { |
| 97 | dev_dbg(&client->dev, "point invalid\n"); |
| 98 | return IRQ_HANDLED; |
| 99 | } |
| 100 | |
| 101 | input_mt_slot(input_dev, id); |
| 102 | input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, down); |
| 103 | |
| 104 | dev_dbg(&client->dev, "%s id:%d x:%d y:%d z:%d", |
| 105 | down ? "down" : "up", id, x, y, z); |
| 106 | |
| 107 | if (down) { |
| 108 | input_report_abs(input_dev, ABS_MT_POSITION_X, x); |
| 109 | input_report_abs(input_dev, ABS_MT_POSITION_Y, y); |
| 110 | input_report_abs(input_dev, ABS_MT_PRESSURE, z); |
| 111 | } |
| 112 | |
| 113 | input_mt_report_pointer_emulation(input_dev, true); |
| 114 | input_sync(input_dev); |
| 115 | |
| 116 | return IRQ_HANDLED; |
| 117 | } |
| 118 | |
| 119 | /* wake up controller by an falling edge of interrupt gpio. */ |
| 120 | static int egalax_wake_up_device(struct i2c_client *client) |
| 121 | { |
Hui Wang | ae495e8 | 2012-10-25 00:38:01 -0700 | [diff] [blame] | 122 | struct device_node *np = client->dev.of_node; |
| 123 | int gpio; |
Zhang Jiejing | 59bae1d | 2011-11-12 00:03:18 -0800 | [diff] [blame] | 124 | int ret; |
| 125 | |
Hui Wang | ae495e8 | 2012-10-25 00:38:01 -0700 | [diff] [blame] | 126 | if (!np) |
| 127 | return -ENODEV; |
| 128 | |
| 129 | gpio = of_get_named_gpio(np, "wakeup-gpios", 0); |
| 130 | if (!gpio_is_valid(gpio)) |
| 131 | return -ENODEV; |
| 132 | |
Zhang Jiejing | 59bae1d | 2011-11-12 00:03:18 -0800 | [diff] [blame] | 133 | ret = gpio_request(gpio, "egalax_irq"); |
| 134 | if (ret < 0) { |
| 135 | dev_err(&client->dev, |
| 136 | "request gpio failed, cannot wake up controller: %d\n", |
| 137 | ret); |
| 138 | return ret; |
| 139 | } |
| 140 | |
| 141 | /* wake up controller via an falling edge on IRQ gpio. */ |
| 142 | gpio_direction_output(gpio, 0); |
| 143 | gpio_set_value(gpio, 1); |
| 144 | |
| 145 | /* controller should be waken up, return irq. */ |
| 146 | gpio_direction_input(gpio); |
| 147 | gpio_free(gpio); |
| 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 152 | static int egalax_firmware_version(struct i2c_client *client) |
Zhang Jiejing | 59bae1d | 2011-11-12 00:03:18 -0800 | [diff] [blame] | 153 | { |
| 154 | static const u8 cmd[MAX_I2C_DATA_LEN] = { 0x03, 0x03, 0xa, 0x01, 0x41 }; |
| 155 | int ret; |
| 156 | |
| 157 | ret = i2c_master_send(client, cmd, MAX_I2C_DATA_LEN); |
| 158 | if (ret < 0) |
| 159 | return ret; |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 164 | static int egalax_ts_probe(struct i2c_client *client, |
Andy Shevchenko | 1ffb064 | 2013-04-24 09:53:25 -0700 | [diff] [blame] | 165 | const struct i2c_device_id *id) |
Zhang Jiejing | 59bae1d | 2011-11-12 00:03:18 -0800 | [diff] [blame] | 166 | { |
| 167 | struct egalax_ts *ts; |
| 168 | struct input_dev *input_dev; |
Zhang Jiejing | 59bae1d | 2011-11-12 00:03:18 -0800 | [diff] [blame] | 169 | int error; |
| 170 | |
Andy Shevchenko | 1ffb064 | 2013-04-24 09:53:25 -0700 | [diff] [blame] | 171 | ts = devm_kzalloc(&client->dev, sizeof(struct egalax_ts), GFP_KERNEL); |
Zhang Jiejing | 59bae1d | 2011-11-12 00:03:18 -0800 | [diff] [blame] | 172 | if (!ts) { |
| 173 | dev_err(&client->dev, "Failed to allocate memory\n"); |
| 174 | return -ENOMEM; |
| 175 | } |
| 176 | |
Andy Shevchenko | 1ffb064 | 2013-04-24 09:53:25 -0700 | [diff] [blame] | 177 | input_dev = devm_input_allocate_device(&client->dev); |
Zhang Jiejing | 59bae1d | 2011-11-12 00:03:18 -0800 | [diff] [blame] | 178 | if (!input_dev) { |
| 179 | dev_err(&client->dev, "Failed to allocate memory\n"); |
Andy Shevchenko | 1ffb064 | 2013-04-24 09:53:25 -0700 | [diff] [blame] | 180 | return -ENOMEM; |
Zhang Jiejing | 59bae1d | 2011-11-12 00:03:18 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | ts->client = client; |
| 184 | ts->input_dev = input_dev; |
| 185 | |
| 186 | /* controller may be in sleep, wake it up. */ |
Hui Wang | ae495e8 | 2012-10-25 00:38:01 -0700 | [diff] [blame] | 187 | error = egalax_wake_up_device(client); |
| 188 | if (error) { |
| 189 | dev_err(&client->dev, "Failed to wake up the controller\n"); |
Andy Shevchenko | 1ffb064 | 2013-04-24 09:53:25 -0700 | [diff] [blame] | 190 | return error; |
Hui Wang | ae495e8 | 2012-10-25 00:38:01 -0700 | [diff] [blame] | 191 | } |
Zhang Jiejing | 59bae1d | 2011-11-12 00:03:18 -0800 | [diff] [blame] | 192 | |
Andy Shevchenko | 1ffb064 | 2013-04-24 09:53:25 -0700 | [diff] [blame] | 193 | error = egalax_firmware_version(client); |
| 194 | if (error < 0) { |
Zhang Jiejing | 59bae1d | 2011-11-12 00:03:18 -0800 | [diff] [blame] | 195 | dev_err(&client->dev, "Failed to read firmware version\n"); |
Andy Shevchenko | 1ffb064 | 2013-04-24 09:53:25 -0700 | [diff] [blame] | 196 | return error; |
Zhang Jiejing | 59bae1d | 2011-11-12 00:03:18 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | input_dev->name = "EETI eGalax Touch Screen"; |
| 200 | input_dev->id.bustype = BUS_I2C; |
Zhang Jiejing | 59bae1d | 2011-11-12 00:03:18 -0800 | [diff] [blame] | 201 | |
| 202 | __set_bit(EV_ABS, input_dev->evbit); |
| 203 | __set_bit(EV_KEY, input_dev->evbit); |
| 204 | __set_bit(BTN_TOUCH, input_dev->keybit); |
| 205 | |
| 206 | input_set_abs_params(input_dev, ABS_X, 0, EGALAX_MAX_X, 0, 0); |
| 207 | input_set_abs_params(input_dev, ABS_Y, 0, EGALAX_MAX_Y, 0, 0); |
| 208 | input_set_abs_params(input_dev, |
| 209 | ABS_MT_POSITION_X, 0, EGALAX_MAX_X, 0, 0); |
| 210 | input_set_abs_params(input_dev, |
Heiko Abraham | 3c9cfa7 | 2013-05-05 19:49:49 -0700 | [diff] [blame] | 211 | ABS_MT_POSITION_Y, 0, EGALAX_MAX_Y, 0, 0); |
Henrik Rydberg | b4adbbe | 2012-08-11 22:07:55 +0200 | [diff] [blame] | 212 | input_mt_init_slots(input_dev, MAX_SUPPORT_POINTS, 0); |
Zhang Jiejing | 59bae1d | 2011-11-12 00:03:18 -0800 | [diff] [blame] | 213 | |
Andy Shevchenko | 1ffb064 | 2013-04-24 09:53:25 -0700 | [diff] [blame] | 214 | error = devm_request_threaded_irq(&client->dev, client->irq, NULL, |
| 215 | egalax_ts_interrupt, |
| 216 | IRQF_TRIGGER_LOW | IRQF_ONESHOT, |
| 217 | "egalax_ts", ts); |
Zhang Jiejing | 59bae1d | 2011-11-12 00:03:18 -0800 | [diff] [blame] | 218 | if (error < 0) { |
| 219 | dev_err(&client->dev, "Failed to register interrupt\n"); |
Andy Shevchenko | 1ffb064 | 2013-04-24 09:53:25 -0700 | [diff] [blame] | 220 | return error; |
Zhang Jiejing | 59bae1d | 2011-11-12 00:03:18 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | error = input_register_device(ts->input_dev); |
| 224 | if (error) |
Andy Shevchenko | 1ffb064 | 2013-04-24 09:53:25 -0700 | [diff] [blame] | 225 | return error; |
Zhang Jiejing | 59bae1d | 2011-11-12 00:03:18 -0800 | [diff] [blame] | 226 | |
Zhang Jiejing | 59bae1d | 2011-11-12 00:03:18 -0800 | [diff] [blame] | 227 | return 0; |
Zhang Jiejing | 59bae1d | 2011-11-12 00:03:18 -0800 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | static const struct i2c_device_id egalax_ts_id[] = { |
| 231 | { "egalax_ts", 0 }, |
| 232 | { } |
| 233 | }; |
| 234 | MODULE_DEVICE_TABLE(i2c, egalax_ts_id); |
| 235 | |
Jingoo Han | 02b6a58 | 2014-11-02 00:04:14 -0700 | [diff] [blame] | 236 | static int __maybe_unused egalax_ts_suspend(struct device *dev) |
Zhang Jiejing | 59bae1d | 2011-11-12 00:03:18 -0800 | [diff] [blame] | 237 | { |
| 238 | static const u8 suspend_cmd[MAX_I2C_DATA_LEN] = { |
| 239 | 0x3, 0x6, 0xa, 0x3, 0x36, 0x3f, 0x2, 0, 0, 0 |
| 240 | }; |
| 241 | struct i2c_client *client = to_i2c_client(dev); |
| 242 | int ret; |
| 243 | |
| 244 | ret = i2c_master_send(client, suspend_cmd, MAX_I2C_DATA_LEN); |
| 245 | return ret > 0 ? 0 : ret; |
| 246 | } |
| 247 | |
Jingoo Han | 02b6a58 | 2014-11-02 00:04:14 -0700 | [diff] [blame] | 248 | static int __maybe_unused egalax_ts_resume(struct device *dev) |
Zhang Jiejing | 59bae1d | 2011-11-12 00:03:18 -0800 | [diff] [blame] | 249 | { |
| 250 | struct i2c_client *client = to_i2c_client(dev); |
| 251 | |
| 252 | return egalax_wake_up_device(client); |
| 253 | } |
Zhang Jiejing | 59bae1d | 2011-11-12 00:03:18 -0800 | [diff] [blame] | 254 | |
| 255 | static SIMPLE_DEV_PM_OPS(egalax_ts_pm_ops, egalax_ts_suspend, egalax_ts_resume); |
| 256 | |
Jingoo Han | a5fd844c | 2014-05-07 13:08:53 -0700 | [diff] [blame] | 257 | static const struct of_device_id egalax_ts_dt_ids[] = { |
Hui Wang | ae495e8 | 2012-10-25 00:38:01 -0700 | [diff] [blame] | 258 | { .compatible = "eeti,egalax_ts" }, |
| 259 | { /* sentinel */ } |
| 260 | }; |
Javier Martinez Canillas | 02d9bd0 | 2015-07-30 10:39:28 -0700 | [diff] [blame] | 261 | MODULE_DEVICE_TABLE(of, egalax_ts_dt_ids); |
Hui Wang | ae495e8 | 2012-10-25 00:38:01 -0700 | [diff] [blame] | 262 | |
Zhang Jiejing | 59bae1d | 2011-11-12 00:03:18 -0800 | [diff] [blame] | 263 | static struct i2c_driver egalax_ts_driver = { |
| 264 | .driver = { |
| 265 | .name = "egalax_ts", |
Zhang Jiejing | 59bae1d | 2011-11-12 00:03:18 -0800 | [diff] [blame] | 266 | .pm = &egalax_ts_pm_ops, |
Sachin Kamat | 085f17c | 2013-10-06 00:52:12 -0700 | [diff] [blame] | 267 | .of_match_table = egalax_ts_dt_ids, |
Zhang Jiejing | 59bae1d | 2011-11-12 00:03:18 -0800 | [diff] [blame] | 268 | }, |
| 269 | .id_table = egalax_ts_id, |
| 270 | .probe = egalax_ts_probe, |
Zhang Jiejing | 59bae1d | 2011-11-12 00:03:18 -0800 | [diff] [blame] | 271 | }; |
| 272 | |
Axel Lin | 1b92c1c | 2012-03-16 23:05:41 -0700 | [diff] [blame] | 273 | module_i2c_driver(egalax_ts_driver); |
Zhang Jiejing | 59bae1d | 2011-11-12 00:03:18 -0800 | [diff] [blame] | 274 | |
| 275 | MODULE_AUTHOR("Freescale Semiconductor, Inc."); |
| 276 | MODULE_DESCRIPTION("Touchscreen driver for EETI eGalax touch controller"); |
| 277 | MODULE_LICENSE("GPL"); |