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