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 int irq_gpio, irq_active_high; 51 bool running; 52 }; 53 54 #define EETI_TS_BITDEPTH (11) 55 #define EETI_MAXVAL ((1 << (EETI_TS_BITDEPTH + 1)) - 1) 56 57 #define REPORT_BIT_PRESSED BIT(0) 58 #define REPORT_BIT_AD0 BIT(1) 59 #define REPORT_BIT_AD1 BIT(2) 60 #define REPORT_BIT_HAS_PRESSURE BIT(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_cansleep(eeti->irq_gpio) == eeti->irq_active_high; 66 } 67 68 static void eeti_ts_report_event(struct eeti_ts *eeti, u8 *buf) 69 { 70 unsigned int res; 71 u16 x, y; 72 73 res = REPORT_RES_BITS(buf[0] & (REPORT_BIT_AD0 | REPORT_BIT_AD1)); 74 75 x = get_unaligned_be16(&buf[1]); 76 y = get_unaligned_be16(&buf[3]); 77 78 /* fix the range to 11 bits */ 79 x >>= res - EETI_TS_BITDEPTH; 80 y >>= res - EETI_TS_BITDEPTH; 81 82 if (flip_x) 83 x = EETI_MAXVAL - x; 84 85 if (flip_y) 86 y = EETI_MAXVAL - y; 87 88 if (buf[0] & REPORT_BIT_HAS_PRESSURE) 89 input_report_abs(eeti->input, ABS_PRESSURE, buf[5]); 90 91 input_report_abs(eeti->input, ABS_X, x); 92 input_report_abs(eeti->input, ABS_Y, y); 93 input_report_key(eeti->input, BTN_TOUCH, buf[0] & REPORT_BIT_PRESSED); 94 input_sync(eeti->input); 95 } 96 97 static irqreturn_t eeti_ts_isr(int irq, void *dev_id) 98 { 99 struct eeti_ts *eeti = dev_id; 100 int len; 101 int error; 102 char buf[6]; 103 104 do { 105 len = i2c_master_recv(eeti->client, buf, sizeof(buf)); 106 if (len != sizeof(buf)) { 107 error = len < 0 ? len : -EIO; 108 dev_err(&eeti->client->dev, 109 "failed to read touchscreen data: %d\n", 110 error); 111 break; 112 } 113 114 if (buf[0] & 0x80) { 115 /* Motion packet */ 116 eeti_ts_report_event(eeti, buf); 117 } 118 } while (eeti->running && eeti_ts_irq_active(eeti)); 119 120 return IRQ_HANDLED; 121 } 122 123 static void eeti_ts_start(struct eeti_ts *eeti) 124 { 125 eeti->running = true; 126 wmb(); 127 enable_irq(eeti->client->irq); 128 } 129 130 static void eeti_ts_stop(struct eeti_ts *eeti) 131 { 132 eeti->running = false; 133 wmb(); 134 disable_irq(eeti->client->irq); 135 } 136 137 static int eeti_ts_open(struct input_dev *dev) 138 { 139 struct eeti_ts *eeti = input_get_drvdata(dev); 140 141 eeti_ts_start(eeti); 142 143 return 0; 144 } 145 146 static void eeti_ts_close(struct input_dev *dev) 147 { 148 struct eeti_ts *eeti = input_get_drvdata(dev); 149 150 eeti_ts_stop(eeti); 151 } 152 153 static int eeti_ts_probe(struct i2c_client *client, 154 const struct i2c_device_id *idp) 155 { 156 struct device *dev = &client->dev; 157 struct eeti_ts_platform_data *pdata = dev_get_platdata(dev); 158 struct eeti_ts *eeti; 159 struct input_dev *input; 160 unsigned int irq_flags; 161 int error; 162 163 /* 164 * In contrast to what's described in the datasheet, there seems 165 * to be no way of probing the presence of that device using I2C 166 * commands. So we need to blindly believe it is there, and wait 167 * for interrupts to occur. 168 */ 169 170 eeti = devm_kzalloc(dev, sizeof(*eeti), GFP_KERNEL); 171 if (!eeti) { 172 dev_err(dev, "failed to allocate driver data\n"); 173 return -ENOMEM; 174 } 175 176 input = devm_input_allocate_device(dev); 177 if (!input) { 178 dev_err(dev, "Failed to allocate input device.\n"); 179 return -ENOMEM; 180 } 181 182 input_set_capability(input, EV_KEY, BTN_TOUCH); 183 184 input_set_abs_params(input, ABS_X, 0, EETI_MAXVAL, 0, 0); 185 input_set_abs_params(input, ABS_Y, 0, EETI_MAXVAL, 0, 0); 186 input_set_abs_params(input, ABS_PRESSURE, 0, 0xff, 0, 0); 187 188 input->name = client->name; 189 input->id.bustype = BUS_I2C; 190 input->open = eeti_ts_open; 191 input->close = eeti_ts_close; 192 193 eeti->client = client; 194 eeti->input = input; 195 eeti->irq_gpio = pdata->irq_gpio; 196 197 error = devm_gpio_request_one(dev, pdata->irq_gpio, GPIOF_IN, 198 client->name); 199 if (error) 200 return error; 201 202 eeti->irq_active_high = pdata->irq_active_high; 203 204 irq_flags = eeti->irq_active_high ? 205 IRQF_TRIGGER_HIGH : IRQF_TRIGGER_LOW; 206 207 i2c_set_clientdata(client, eeti); 208 input_set_drvdata(input, eeti); 209 210 error = devm_request_threaded_irq(dev, client->irq, 211 NULL, eeti_ts_isr, 212 irq_flags | IRQF_ONESHOT, 213 client->name, eeti); 214 if (error) { 215 dev_err(dev, "Unable to request touchscreen IRQ: %d\n", 216 error); 217 return error; 218 } 219 220 /* 221 * Disable the device for now. It will be enabled once the 222 * input device is opened. 223 */ 224 eeti_ts_stop(eeti); 225 226 error = input_register_device(input); 227 if (error) 228 return error; 229 230 return 0; 231 } 232 233 static int __maybe_unused eeti_ts_suspend(struct device *dev) 234 { 235 struct i2c_client *client = to_i2c_client(dev); 236 struct eeti_ts *eeti = i2c_get_clientdata(client); 237 struct input_dev *input_dev = eeti->input; 238 239 mutex_lock(&input_dev->mutex); 240 241 if (input_dev->users) 242 eeti_ts_stop(eeti); 243 244 mutex_unlock(&input_dev->mutex); 245 246 if (device_may_wakeup(&client->dev)) 247 enable_irq_wake(client->irq); 248 249 return 0; 250 } 251 252 static int __maybe_unused eeti_ts_resume(struct device *dev) 253 { 254 struct i2c_client *client = to_i2c_client(dev); 255 struct eeti_ts *eeti = i2c_get_clientdata(client); 256 struct input_dev *input_dev = eeti->input; 257 258 if (device_may_wakeup(&client->dev)) 259 disable_irq_wake(client->irq); 260 261 mutex_lock(&input_dev->mutex); 262 263 if (input_dev->users) 264 eeti_ts_start(eeti); 265 266 mutex_unlock(&input_dev->mutex); 267 268 return 0; 269 } 270 271 static SIMPLE_DEV_PM_OPS(eeti_ts_pm, eeti_ts_suspend, eeti_ts_resume); 272 273 static const struct i2c_device_id eeti_ts_id[] = { 274 { "eeti_ts", 0 }, 275 { } 276 }; 277 MODULE_DEVICE_TABLE(i2c, eeti_ts_id); 278 279 static struct i2c_driver eeti_ts_driver = { 280 .driver = { 281 .name = "eeti_ts", 282 .pm = &eeti_ts_pm, 283 }, 284 .probe = eeti_ts_probe, 285 .id_table = eeti_ts_id, 286 }; 287 288 module_i2c_driver(eeti_ts_driver); 289 290 MODULE_DESCRIPTION("EETI Touchscreen driver"); 291 MODULE_AUTHOR("Daniel Mack <[email protected]>"); 292 MODULE_LICENSE("GPL"); 293