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> 35 #include <linux/input/eeti_ts.h> 36 #include <linux/slab.h> 37 38 static bool flip_x; 39 module_param(flip_x, bool, 0644); 40 MODULE_PARM_DESC(flip_x, "flip x coordinate"); 41 42 static bool flip_y; 43 module_param(flip_y, bool, 0644); 44 MODULE_PARM_DESC(flip_y, "flip y coordinate"); 45 46 struct eeti_ts { 47 struct i2c_client *client; 48 struct input_dev *input; 49 struct work_struct work; 50 struct mutex mutex; 51 int irq_gpio, irq, irq_active_high; 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 63 static inline int eeti_ts_irq_active(struct eeti_ts *eeti) 64 { 65 return gpio_get_value(eeti->irq_gpio) == eeti->irq_active_high; 66 } 67 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 *eeti = 73 container_of(work, struct eeti_ts, work); 74 75 mutex_lock(&eeti->mutex); 76 77 while (eeti_ts_irq_active(eeti) && --to) 78 i2c_master_recv(eeti->client, buf, sizeof(buf)); 79 80 if (!to) { 81 dev_err(&eeti->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(eeti->input, ABS_PRESSURE, buf[5]); 107 108 input_report_abs(eeti->input, ABS_X, x); 109 input_report_abs(eeti->input, ABS_Y, y); 110 input_report_key(eeti->input, BTN_TOUCH, !!pressed); 111 input_sync(eeti->input); 112 113 out: 114 mutex_unlock(&eeti->mutex); 115 } 116 117 static irqreturn_t eeti_ts_isr(int irq, void *dev_id) 118 { 119 struct eeti_ts *eeti = dev_id; 120 121 /* postpone I2C transactions as we are atomic */ 122 schedule_work(&eeti->work); 123 124 return IRQ_HANDLED; 125 } 126 127 static void eeti_ts_start(struct eeti_ts *eeti) 128 { 129 enable_irq(eeti->irq); 130 131 /* Read the events once to arm the IRQ */ 132 eeti_ts_read(&eeti->work); 133 } 134 135 static void eeti_ts_stop(struct eeti_ts *eeti) 136 { 137 disable_irq(eeti->irq); 138 cancel_work_sync(&eeti->work); 139 } 140 141 static int eeti_ts_open(struct input_dev *dev) 142 { 143 struct eeti_ts *eeti = input_get_drvdata(dev); 144 145 eeti_ts_start(eeti); 146 147 return 0; 148 } 149 150 static void eeti_ts_close(struct input_dev *dev) 151 { 152 struct eeti_ts *eeti = input_get_drvdata(dev); 153 154 eeti_ts_stop(eeti); 155 } 156 157 static int eeti_ts_probe(struct i2c_client *client, 158 const struct i2c_device_id *idp) 159 { 160 struct eeti_ts_platform_data *pdata = dev_get_platdata(&client->dev); 161 struct eeti_ts *eeti; 162 struct input_dev *input; 163 unsigned int irq_flags; 164 int err = -ENOMEM; 165 166 /* 167 * In contrast to what's described in the datasheet, there seems 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 170 * for interrupts to occur. 171 */ 172 173 eeti = kzalloc(sizeof(*eeti), GFP_KERNEL); 174 if (!eeti) { 175 dev_err(&client->dev, "failed to allocate driver data\n"); 176 return -ENOMEM; 177 } 178 179 mutex_init(&eeti->mutex); 180 181 input = input_allocate_device(); 182 if (!input) { 183 dev_err(&client->dev, "Failed to allocate input device.\n"); 184 goto err1; 185 } 186 187 input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS); 188 input->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH); 189 190 input_set_abs_params(input, ABS_X, 0, EETI_MAXVAL, 0, 0); 191 input_set_abs_params(input, ABS_Y, 0, EETI_MAXVAL, 0, 0); 192 input_set_abs_params(input, ABS_PRESSURE, 0, 0xff, 0, 0); 193 194 input->name = client->name; 195 input->id.bustype = BUS_I2C; 196 input->dev.parent = &client->dev; 197 input->open = eeti_ts_open; 198 input->close = eeti_ts_close; 199 200 eeti->client = client; 201 eeti->input = input; 202 eeti->irq_gpio = pdata->irq_gpio; 203 eeti->irq = gpio_to_irq(pdata->irq_gpio); 204 205 err = gpio_request_one(pdata->irq_gpio, GPIOF_IN, client->name); 206 if (err < 0) 207 goto err1; 208 209 eeti->irq_active_high = pdata->irq_active_high; 210 211 irq_flags = eeti->irq_active_high ? 212 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; 213 214 INIT_WORK(&eeti->work, eeti_ts_read); 215 i2c_set_clientdata(client, eeti); 216 input_set_drvdata(input, eeti); 217 218 err = input_register_device(input); 219 if (err) 220 goto err2; 221 222 err = request_irq(eeti->irq, eeti_ts_isr, irq_flags, 223 client->name, eeti); 224 if (err) { 225 dev_err(&client->dev, "Unable to request touchscreen IRQ.\n"); 226 goto err3; 227 } 228 229 /* 230 * Disable the device for now. It will be enabled once the 231 * input device is opened. 232 */ 233 eeti_ts_stop(eeti); 234 235 return 0; 236 237 err3: 238 input_unregister_device(input); 239 input = NULL; /* so we dont try to free it below */ 240 err2: 241 gpio_free(pdata->irq_gpio); 242 err1: 243 input_free_device(input); 244 kfree(eeti); 245 return err; 246 } 247 248 static int eeti_ts_remove(struct i2c_client *client) 249 { 250 struct eeti_ts *eeti = i2c_get_clientdata(client); 251 252 free_irq(eeti->irq, eeti); 253 /* 254 * eeti_ts_stop() leaves IRQ disabled. We need to re-enable it 255 * so that device still works if we reload the driver. 256 */ 257 enable_irq(eeti->irq); 258 259 input_unregister_device(eeti->input); 260 kfree(eeti); 261 262 return 0; 263 } 264 265 static int __maybe_unused eeti_ts_suspend(struct device *dev) 266 { 267 struct i2c_client *client = to_i2c_client(dev); 268 struct eeti_ts *eeti = i2c_get_clientdata(client); 269 struct input_dev *input_dev = eeti->input; 270 271 mutex_lock(&input_dev->mutex); 272 273 if (input_dev->users) 274 eeti_ts_stop(eeti); 275 276 mutex_unlock(&input_dev->mutex); 277 278 if (device_may_wakeup(&client->dev)) 279 enable_irq_wake(eeti->irq); 280 281 return 0; 282 } 283 284 static int __maybe_unused eeti_ts_resume(struct device *dev) 285 { 286 struct i2c_client *client = to_i2c_client(dev); 287 struct eeti_ts *eeti = i2c_get_clientdata(client); 288 struct input_dev *input_dev = eeti->input; 289 290 if (device_may_wakeup(&client->dev)) 291 disable_irq_wake(eeti->irq); 292 293 mutex_lock(&input_dev->mutex); 294 295 if (input_dev->users) 296 eeti_ts_start(eeti); 297 298 mutex_unlock(&input_dev->mutex); 299 300 return 0; 301 } 302 303 static SIMPLE_DEV_PM_OPS(eeti_ts_pm, eeti_ts_suspend, eeti_ts_resume); 304 305 static const struct i2c_device_id eeti_ts_id[] = { 306 { "eeti_ts", 0 }, 307 { } 308 }; 309 MODULE_DEVICE_TABLE(i2c, eeti_ts_id); 310 311 static struct i2c_driver eeti_ts_driver = { 312 .driver = { 313 .name = "eeti_ts", 314 .pm = &eeti_ts_pm, 315 }, 316 .probe = eeti_ts_probe, 317 .remove = eeti_ts_remove, 318 .id_table = eeti_ts_id, 319 }; 320 321 module_i2c_driver(eeti_ts_driver); 322 323 MODULE_DESCRIPTION("EETI Touchscreen driver"); 324 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>"); 325 MODULE_LICENSE("GPL"); 326