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 | |
| 38 | static int flip_x; |
| 39 | module_param(flip_x, bool, 0644); |
| 40 | MODULE_PARM_DESC(flip_x, "flip x coordinate"); |
| 41 | |
| 42 | static int flip_y; |
| 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; |
Daniel Mack | 25a70e3 | 2009-08-12 00:50:09 -0700 | [diff] [blame] | 51 | int 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 | { |
| 65 | return gpio_get_value(irq_to_gpio(priv->irq)) == priv->irq_active_high; |
| 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 | |
| 127 | static int eeti_ts_open(struct input_dev *dev) |
| 128 | { |
| 129 | struct eeti_ts_priv *priv = input_get_drvdata(dev); |
| 130 | |
| 131 | enable_irq(priv->irq); |
| 132 | |
| 133 | /* Read the events once to arm the IRQ */ |
| 134 | eeti_ts_read(&priv->work); |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | static void eeti_ts_close(struct input_dev *dev) |
| 140 | { |
| 141 | struct eeti_ts_priv *priv = input_get_drvdata(dev); |
| 142 | |
| 143 | disable_irq(priv->irq); |
| 144 | cancel_work_sync(&priv->work); |
| 145 | } |
| 146 | |
| 147 | static int __devinit eeti_ts_probe(struct i2c_client *client, |
| 148 | const struct i2c_device_id *idp) |
| 149 | { |
Daniel Mack | 25a70e3 | 2009-08-12 00:50:09 -0700 | [diff] [blame] | 150 | struct eeti_ts_platform_data *pdata; |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 151 | struct eeti_ts_priv *priv; |
| 152 | struct input_dev *input; |
Daniel Mack | 25a70e3 | 2009-08-12 00:50:09 -0700 | [diff] [blame] | 153 | unsigned int irq_flags; |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 154 | int err = -ENOMEM; |
| 155 | |
| 156 | /* In contrast to what's described in the datasheet, there seems |
| 157 | * to be no way of probing the presence of that device using I2C |
| 158 | * commands. So we need to blindly believe it is there, and wait |
| 159 | * for interrupts to occur. */ |
| 160 | |
| 161 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 162 | if (!priv) { |
| 163 | dev_err(&client->dev, "failed to allocate driver data\n"); |
| 164 | goto err0; |
| 165 | } |
| 166 | |
| 167 | mutex_init(&priv->mutex); |
| 168 | input = input_allocate_device(); |
| 169 | |
| 170 | if (!input) { |
| 171 | dev_err(&client->dev, "Failed to allocate input device.\n"); |
| 172 | goto err1; |
| 173 | } |
| 174 | |
| 175 | input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS); |
| 176 | input->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH); |
| 177 | |
| 178 | input_set_abs_params(input, ABS_X, 0, EETI_MAXVAL, 0, 0); |
| 179 | input_set_abs_params(input, ABS_Y, 0, EETI_MAXVAL, 0, 0); |
| 180 | input_set_abs_params(input, ABS_PRESSURE, 0, 0xff, 0, 0); |
| 181 | |
| 182 | input->name = client->name; |
| 183 | input->id.bustype = BUS_I2C; |
| 184 | input->dev.parent = &client->dev; |
| 185 | input->open = eeti_ts_open; |
| 186 | input->close = eeti_ts_close; |
| 187 | |
| 188 | priv->client = client; |
| 189 | priv->input = input; |
| 190 | priv->irq = client->irq; |
| 191 | |
Daniel Mack | 25a70e3 | 2009-08-12 00:50:09 -0700 | [diff] [blame] | 192 | pdata = client->dev.platform_data; |
| 193 | |
| 194 | if (pdata) |
| 195 | priv->irq_active_high = pdata->irq_active_high; |
| 196 | |
| 197 | irq_flags = priv->irq_active_high ? |
| 198 | IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; |
| 199 | |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 200 | INIT_WORK(&priv->work, eeti_ts_read); |
| 201 | i2c_set_clientdata(client, priv); |
| 202 | input_set_drvdata(input, priv); |
| 203 | |
| 204 | err = input_register_device(input); |
| 205 | if (err) |
| 206 | goto err1; |
| 207 | |
Daniel Mack | 25a70e3 | 2009-08-12 00:50:09 -0700 | [diff] [blame] | 208 | err = request_irq(priv->irq, eeti_ts_isr, irq_flags, |
Daniel Mack | 10494dc | 2009-05-18 16:10:39 -0700 | [diff] [blame] | 209 | client->name, priv); |
| 210 | if (err) { |
| 211 | dev_err(&client->dev, "Unable to request touchscreen IRQ.\n"); |
| 212 | goto err2; |
| 213 | } |
| 214 | |
| 215 | /* Disable the irq for now. It will be enabled once the input device |
| 216 | * is opened. */ |
| 217 | disable_irq(priv->irq); |
| 218 | |
| 219 | device_init_wakeup(&client->dev, 0); |
| 220 | return 0; |
| 221 | |
| 222 | err2: |
| 223 | input_unregister_device(input); |
| 224 | input = NULL; /* so we dont try to free it below */ |
| 225 | err1: |
| 226 | input_free_device(input); |
| 227 | i2c_set_clientdata(client, NULL); |
| 228 | kfree(priv); |
| 229 | err0: |
| 230 | return err; |
| 231 | } |
| 232 | |
| 233 | static int __devexit eeti_ts_remove(struct i2c_client *client) |
| 234 | { |
| 235 | struct eeti_ts_priv *priv = i2c_get_clientdata(client); |
| 236 | |
| 237 | free_irq(priv->irq, priv); |
| 238 | input_unregister_device(priv->input); |
| 239 | i2c_set_clientdata(client, NULL); |
| 240 | kfree(priv); |
| 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | #ifdef CONFIG_PM |
| 246 | static int eeti_ts_suspend(struct i2c_client *client, pm_message_t mesg) |
| 247 | { |
| 248 | struct eeti_ts_priv *priv = i2c_get_clientdata(client); |
| 249 | |
| 250 | if (device_may_wakeup(&client->dev)) |
| 251 | enable_irq_wake(priv->irq); |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | static int eeti_ts_resume(struct i2c_client *client) |
| 257 | { |
| 258 | struct eeti_ts_priv *priv = i2c_get_clientdata(client); |
| 259 | |
| 260 | if (device_may_wakeup(&client->dev)) |
| 261 | disable_irq_wake(priv->irq); |
| 262 | |
| 263 | return 0; |
| 264 | } |
| 265 | #else |
| 266 | #define eeti_ts_suspend NULL |
| 267 | #define eeti_ts_resume NULL |
| 268 | #endif |
| 269 | |
| 270 | static const struct i2c_device_id eeti_ts_id[] = { |
| 271 | { "eeti_ts", 0 }, |
| 272 | { } |
| 273 | }; |
| 274 | MODULE_DEVICE_TABLE(i2c, eeti_ts_id); |
| 275 | |
| 276 | static struct i2c_driver eeti_ts_driver = { |
| 277 | .driver = { |
| 278 | .name = "eeti_ts", |
| 279 | }, |
| 280 | .probe = eeti_ts_probe, |
| 281 | .remove = __devexit_p(eeti_ts_remove), |
| 282 | .suspend = eeti_ts_suspend, |
| 283 | .resume = eeti_ts_resume, |
| 284 | .id_table = eeti_ts_id, |
| 285 | }; |
| 286 | |
| 287 | static int __init eeti_ts_init(void) |
| 288 | { |
| 289 | return i2c_add_driver(&eeti_ts_driver); |
| 290 | } |
| 291 | |
| 292 | static void __exit eeti_ts_exit(void) |
| 293 | { |
| 294 | i2c_del_driver(&eeti_ts_driver); |
| 295 | } |
| 296 | |
| 297 | MODULE_DESCRIPTION("EETI Touchscreen driver"); |
| 298 | MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>"); |
| 299 | MODULE_LICENSE("GPL"); |
| 300 | |
| 301 | module_init(eeti_ts_init); |
| 302 | module_exit(eeti_ts_exit); |
| 303 | |