Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Touch Screen driver for EETI's I2C connected touch screen panels |
| 3 | * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de> |
| 4 | * |
| 5 | * See EETI's software guide for the protocol specification: |
| 6 | * http://home.eeti.com.tw/web20/eg/guide.htm |
| 7 | * |
| 8 | * Based on migor_ts.c |
| 9 | * Copyright (c) 2008 Magnus Damm |
| 10 | * Copyright (c) 2007 Ujjwal Pande <ujjwal@kenati.com> |
| 11 | * |
| 12 | * This file is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public |
| 14 | * License as published by the Free Software Foundation; either |
| 15 | * version 2 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * This file is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 | * General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public |
| 23 | * License along with this library; if not, write to the Free Software |
| 24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 25 | */ |
| 26 | |
| 27 | #include <linux/module.h> |
| 28 | #include <linux/moduleparam.h> |
| 29 | #include <linux/kernel.h> |
| 30 | #include <linux/input.h> |
| 31 | #include <linux/interrupt.h> |
| 32 | #include <linux/i2c.h> |
| 33 | #include <linux/timer.h> |
| 34 | #include <linux/gpio.h> |
Daniel Mack | 25a70e3 | 2009-08-12 00:50:09 -0700 | [diff] [blame] | 35 | #include <linux/input/eeti_ts.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 36 | #include <linux/slab.h> |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 37 | |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 38 | static bool flip_x; |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 39 | module_param(flip_x, bool, 0644); |
| 40 | MODULE_PARM_DESC(flip_x, "flip x coordinate"); |
| 41 | |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 42 | static bool flip_y; |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 43 | module_param(flip_y, bool, 0644); |
| 44 | MODULE_PARM_DESC(flip_y, "flip y coordinate"); |
| 45 | |
| 46 | struct eeti_ts_priv { |
| 47 | struct i2c_client *client; |
| 48 | struct input_dev *input; |
| 49 | struct work_struct work; |
| 50 | struct mutex mutex; |
Arnd Bergmann | 4eef6cb | 2012-04-30 16:21:37 +0000 | [diff] [blame] | 51 | int irq_gpio, irq, irq_active_high; |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | #define EETI_TS_BITDEPTH (11) |
| 55 | #define EETI_MAXVAL ((1 << (EETI_TS_BITDEPTH + 1)) - 1) |
| 56 | |
| 57 | #define REPORT_BIT_PRESSED (1 << 0) |
| 58 | #define REPORT_BIT_AD0 (1 << 1) |
| 59 | #define REPORT_BIT_AD1 (1 << 2) |
| 60 | #define REPORT_BIT_HAS_PRESSURE (1 << 6) |
| 61 | #define REPORT_RES_BITS(v) (((v) >> 1) + EETI_TS_BITDEPTH) |
| 62 | |
Daniel Mack | 25a70e3 | 2009-08-12 00:50:09 -0700 | [diff] [blame] | 63 | static inline int eeti_ts_irq_active(struct eeti_ts_priv *priv) |
| 64 | { |
Arnd Bergmann | 4eef6cb | 2012-04-30 16:21:37 +0000 | [diff] [blame] | 65 | return gpio_get_value(priv->irq_gpio) == priv->irq_active_high; |
Daniel Mack | 25a70e3 | 2009-08-12 00:50:09 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 68 | static void eeti_ts_read(struct work_struct *work) |
| 69 | { |
| 70 | char buf[6]; |
| 71 | unsigned int x, y, res, pressed, to = 100; |
| 72 | struct eeti_ts_priv *priv = |
| 73 | container_of(work, struct eeti_ts_priv, work); |
| 74 | |
| 75 | mutex_lock(&priv->mutex); |
| 76 | |
Daniel Mack | 25a70e3 | 2009-08-12 00:50:09 -0700 | [diff] [blame] | 77 | while (eeti_ts_irq_active(priv) && --to) |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 78 | i2c_master_recv(priv->client, buf, sizeof(buf)); |
| 79 | |
| 80 | if (!to) { |
| 81 | dev_err(&priv->client->dev, |
| 82 | "unable to clear IRQ - line stuck?\n"); |
| 83 | goto out; |
| 84 | } |
| 85 | |
| 86 | /* drop non-report packets */ |
| 87 | if (!(buf[0] & 0x80)) |
| 88 | goto out; |
| 89 | |
| 90 | pressed = buf[0] & REPORT_BIT_PRESSED; |
| 91 | res = REPORT_RES_BITS(buf[0] & (REPORT_BIT_AD0 | REPORT_BIT_AD1)); |
| 92 | x = buf[2] | (buf[1] << 8); |
| 93 | y = buf[4] | (buf[3] << 8); |
| 94 | |
| 95 | /* fix the range to 11 bits */ |
| 96 | x >>= res - EETI_TS_BITDEPTH; |
| 97 | y >>= res - EETI_TS_BITDEPTH; |
| 98 | |
| 99 | if (flip_x) |
| 100 | x = EETI_MAXVAL - x; |
| 101 | |
| 102 | if (flip_y) |
| 103 | y = EETI_MAXVAL - y; |
| 104 | |
| 105 | if (buf[0] & REPORT_BIT_HAS_PRESSURE) |
| 106 | input_report_abs(priv->input, ABS_PRESSURE, buf[5]); |
| 107 | |
| 108 | input_report_abs(priv->input, ABS_X, x); |
| 109 | input_report_abs(priv->input, ABS_Y, y); |
| 110 | input_report_key(priv->input, BTN_TOUCH, !!pressed); |
| 111 | input_sync(priv->input); |
| 112 | |
| 113 | out: |
| 114 | mutex_unlock(&priv->mutex); |
| 115 | } |
| 116 | |
| 117 | static irqreturn_t eeti_ts_isr(int irq, void *dev_id) |
| 118 | { |
| 119 | struct eeti_ts_priv *priv = dev_id; |
| 120 | |
| 121 | /* postpone I2C transactions as we are atomic */ |
| 122 | schedule_work(&priv->work); |
| 123 | |
| 124 | return IRQ_HANDLED; |
| 125 | } |
| 126 | |
Daniel Mack | 7fbef0d | 2010-04-19 00:42:16 -0700 | [diff] [blame] | 127 | static void eeti_ts_start(struct eeti_ts_priv *priv) |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 128 | { |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 129 | enable_irq(priv->irq); |
| 130 | |
| 131 | /* Read the events once to arm the IRQ */ |
| 132 | eeti_ts_read(&priv->work); |
Daniel Mack | 7fbef0d | 2010-04-19 00:42:16 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | static void eeti_ts_stop(struct eeti_ts_priv *priv) |
| 136 | { |
| 137 | disable_irq(priv->irq); |
| 138 | cancel_work_sync(&priv->work); |
| 139 | } |
| 140 | |
| 141 | static int eeti_ts_open(struct input_dev *dev) |
| 142 | { |
| 143 | struct eeti_ts_priv *priv = input_get_drvdata(dev); |
| 144 | |
| 145 | eeti_ts_start(priv); |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | static void eeti_ts_close(struct input_dev *dev) |
| 151 | { |
| 152 | struct eeti_ts_priv *priv = input_get_drvdata(dev); |
| 153 | |
Daniel Mack | 7fbef0d | 2010-04-19 00:42:16 -0700 | [diff] [blame] | 154 | eeti_ts_stop(priv); |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 157 | static int eeti_ts_probe(struct i2c_client *client, |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 158 | const struct i2c_device_id *idp) |
| 159 | { |
Jingoo Han | c838cb3 | 2013-12-05 19:21:10 -0800 | [diff] [blame] | 160 | struct eeti_ts_platform_data *pdata = dev_get_platdata(&client->dev); |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 161 | struct eeti_ts_priv *priv; |
| 162 | struct input_dev *input; |
Daniel Mack | 25a70e3 | 2009-08-12 00:50:09 -0700 | [diff] [blame] | 163 | unsigned int irq_flags; |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 164 | int err = -ENOMEM; |
| 165 | |
Daniel Mack | 7fbef0d | 2010-04-19 00:42:16 -0700 | [diff] [blame] | 166 | /* |
| 167 | * In contrast to what's described in the datasheet, there seems |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 168 | * to be no way of probing the presence of that device using I2C |
| 169 | * commands. So we need to blindly believe it is there, and wait |
Daniel Mack | 7fbef0d | 2010-04-19 00:42:16 -0700 | [diff] [blame] | 170 | * for interrupts to occur. |
| 171 | */ |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 172 | |
| 173 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 174 | if (!priv) { |
| 175 | dev_err(&client->dev, "failed to allocate driver data\n"); |
Guenter Roeck | ad03ae4 | 2017-01-18 11:24:06 -0800 | [diff] [blame] | 176 | return -ENOMEM; |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | mutex_init(&priv->mutex); |
| 180 | input = input_allocate_device(); |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 181 | if (!input) { |
| 182 | dev_err(&client->dev, "Failed to allocate input device.\n"); |
| 183 | goto err1; |
| 184 | } |
| 185 | |
| 186 | input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS); |
| 187 | input->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH); |
| 188 | |
| 189 | input_set_abs_params(input, ABS_X, 0, EETI_MAXVAL, 0, 0); |
| 190 | input_set_abs_params(input, ABS_Y, 0, EETI_MAXVAL, 0, 0); |
| 191 | input_set_abs_params(input, ABS_PRESSURE, 0, 0xff, 0, 0); |
| 192 | |
| 193 | input->name = client->name; |
| 194 | input->id.bustype = BUS_I2C; |
| 195 | input->dev.parent = &client->dev; |
| 196 | input->open = eeti_ts_open; |
| 197 | input->close = eeti_ts_close; |
| 198 | |
| 199 | priv->client = client; |
| 200 | priv->input = input; |
Arnd Bergmann | 4eef6cb | 2012-04-30 16:21:37 +0000 | [diff] [blame] | 201 | priv->irq_gpio = pdata->irq_gpio; |
| 202 | priv->irq = gpio_to_irq(pdata->irq_gpio); |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 203 | |
Arnd Bergmann | 4eef6cb | 2012-04-30 16:21:37 +0000 | [diff] [blame] | 204 | err = gpio_request_one(pdata->irq_gpio, GPIOF_IN, client->name); |
| 205 | if (err < 0) |
| 206 | goto err1; |
Daniel Mack | 25a70e3 | 2009-08-12 00:50:09 -0700 | [diff] [blame] | 207 | |
Sachin Kamat | 80e3e53 | 2013-03-28 01:14:46 -0700 | [diff] [blame] | 208 | priv->irq_active_high = pdata->irq_active_high; |
Daniel Mack | 25a70e3 | 2009-08-12 00:50:09 -0700 | [diff] [blame] | 209 | |
| 210 | irq_flags = priv->irq_active_high ? |
| 211 | IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; |
| 212 | |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 213 | INIT_WORK(&priv->work, eeti_ts_read); |
| 214 | i2c_set_clientdata(client, priv); |
| 215 | input_set_drvdata(input, priv); |
| 216 | |
| 217 | err = input_register_device(input); |
| 218 | if (err) |
Arnd Bergmann | 4eef6cb | 2012-04-30 16:21:37 +0000 | [diff] [blame] | 219 | goto err2; |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 220 | |
Daniel Mack | 25a70e3 | 2009-08-12 00:50:09 -0700 | [diff] [blame] | 221 | err = request_irq(priv->irq, eeti_ts_isr, irq_flags, |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 222 | client->name, priv); |
| 223 | if (err) { |
| 224 | dev_err(&client->dev, "Unable to request touchscreen IRQ.\n"); |
Arnd Bergmann | 4eef6cb | 2012-04-30 16:21:37 +0000 | [diff] [blame] | 225 | goto err3; |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Daniel Mack | 7fbef0d | 2010-04-19 00:42:16 -0700 | [diff] [blame] | 228 | /* |
| 229 | * Disable the device for now. It will be enabled once the |
| 230 | * input device is opened. |
| 231 | */ |
| 232 | eeti_ts_stop(priv); |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 233 | |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 234 | return 0; |
| 235 | |
Arnd Bergmann | 4eef6cb | 2012-04-30 16:21:37 +0000 | [diff] [blame] | 236 | err3: |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 237 | input_unregister_device(input); |
| 238 | input = NULL; /* so we dont try to free it below */ |
Arnd Bergmann | 4eef6cb | 2012-04-30 16:21:37 +0000 | [diff] [blame] | 239 | err2: |
| 240 | gpio_free(pdata->irq_gpio); |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 241 | err1: |
| 242 | input_free_device(input); |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 243 | kfree(priv); |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 244 | return err; |
| 245 | } |
| 246 | |
Bill Pemberton | e2619cf | 2012-11-23 21:50:47 -0800 | [diff] [blame] | 247 | static int eeti_ts_remove(struct i2c_client *client) |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 248 | { |
| 249 | struct eeti_ts_priv *priv = i2c_get_clientdata(client); |
| 250 | |
| 251 | free_irq(priv->irq, priv); |
Daniel Mack | 7fbef0d | 2010-04-19 00:42:16 -0700 | [diff] [blame] | 252 | /* |
| 253 | * eeti_ts_stop() leaves IRQ disabled. We need to re-enable it |
| 254 | * so that device still works if we reload the driver. |
| 255 | */ |
| 256 | enable_irq(priv->irq); |
| 257 | |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 258 | input_unregister_device(priv->input); |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 259 | kfree(priv); |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
Jingoo Han | 02b6a58 | 2014-11-02 00:04:14 -0700 | [diff] [blame] | 264 | static int __maybe_unused eeti_ts_suspend(struct device *dev) |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 265 | { |
Mark Brown | 85012ff | 2011-01-06 23:01:02 -0800 | [diff] [blame] | 266 | struct i2c_client *client = to_i2c_client(dev); |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 267 | struct eeti_ts_priv *priv = i2c_get_clientdata(client); |
Daniel Mack | 7fbef0d | 2010-04-19 00:42:16 -0700 | [diff] [blame] | 268 | struct input_dev *input_dev = priv->input; |
| 269 | |
| 270 | mutex_lock(&input_dev->mutex); |
| 271 | |
| 272 | if (input_dev->users) |
| 273 | eeti_ts_stop(priv); |
| 274 | |
| 275 | mutex_unlock(&input_dev->mutex); |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 276 | |
| 277 | if (device_may_wakeup(&client->dev)) |
| 278 | enable_irq_wake(priv->irq); |
| 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
Jingoo Han | 02b6a58 | 2014-11-02 00:04:14 -0700 | [diff] [blame] | 283 | static int __maybe_unused eeti_ts_resume(struct device *dev) |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 284 | { |
Mark Brown | 85012ff | 2011-01-06 23:01:02 -0800 | [diff] [blame] | 285 | struct i2c_client *client = to_i2c_client(dev); |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 286 | struct eeti_ts_priv *priv = i2c_get_clientdata(client); |
Daniel Mack | 7fbef0d | 2010-04-19 00:42:16 -0700 | [diff] [blame] | 287 | struct input_dev *input_dev = priv->input; |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 288 | |
| 289 | if (device_may_wakeup(&client->dev)) |
| 290 | disable_irq_wake(priv->irq); |
| 291 | |
Daniel Mack | 7fbef0d | 2010-04-19 00:42:16 -0700 | [diff] [blame] | 292 | mutex_lock(&input_dev->mutex); |
| 293 | |
| 294 | if (input_dev->users) |
| 295 | eeti_ts_start(priv); |
| 296 | |
| 297 | mutex_unlock(&input_dev->mutex); |
| 298 | |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 299 | return 0; |
| 300 | } |
Mark Brown | 85012ff | 2011-01-06 23:01:02 -0800 | [diff] [blame] | 301 | |
| 302 | static SIMPLE_DEV_PM_OPS(eeti_ts_pm, eeti_ts_suspend, eeti_ts_resume); |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 303 | |
| 304 | static const struct i2c_device_id eeti_ts_id[] = { |
| 305 | { "eeti_ts", 0 }, |
| 306 | { } |
| 307 | }; |
| 308 | MODULE_DEVICE_TABLE(i2c, eeti_ts_id); |
| 309 | |
| 310 | static struct i2c_driver eeti_ts_driver = { |
| 311 | .driver = { |
| 312 | .name = "eeti_ts", |
Mark Brown | 85012ff | 2011-01-06 23:01:02 -0800 | [diff] [blame] | 313 | .pm = &eeti_ts_pm, |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 314 | }, |
| 315 | .probe = eeti_ts_probe, |
Bill Pemberton | 1cb0aa8 | 2012-11-23 21:27:39 -0800 | [diff] [blame] | 316 | .remove = eeti_ts_remove, |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 317 | .id_table = eeti_ts_id, |
| 318 | }; |
| 319 | |
Axel Lin | 1b92c1c | 2012-03-16 23:05:41 -0700 | [diff] [blame] | 320 | module_i2c_driver(eeti_ts_driver); |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 321 | |
| 322 | MODULE_DESCRIPTION("EETI Touchscreen driver"); |
| 323 | MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>"); |
| 324 | MODULE_LICENSE("GPL"); |