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