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 #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 int error; 161 162 /* 163 * In contrast to what's described in the datasheet, there seems 164 * to be no way of probing the presence of that device using I2C 165 * commands. So we need to blindly believe it is there, and wait 166 * for interrupts to occur. 167 */ 168 169 eeti = devm_kzalloc(dev, sizeof(*eeti), GFP_KERNEL); 170 if (!eeti) { 171 dev_err(dev, "failed to allocate driver data\n"); 172 return -ENOMEM; 173 } 174 175 input = devm_input_allocate_device(dev); 176 if (!input) { 177 dev_err(dev, "Failed to allocate input device.\n"); 178 return -ENOMEM; 179 } 180 181 input_set_capability(input, EV_KEY, BTN_TOUCH); 182 183 input_set_abs_params(input, ABS_X, 0, EETI_MAXVAL, 0, 0); 184 input_set_abs_params(input, ABS_Y, 0, EETI_MAXVAL, 0, 0); 185 input_set_abs_params(input, ABS_PRESSURE, 0, 0xff, 0, 0); 186 187 input->name = client->name; 188 input->id.bustype = BUS_I2C; 189 input->open = eeti_ts_open; 190 input->close = eeti_ts_close; 191 192 eeti->client = client; 193 eeti->input = input; 194 eeti->irq_gpio = pdata->irq_gpio; 195 196 error = devm_gpio_request_one(dev, pdata->irq_gpio, GPIOF_IN, 197 client->name); 198 if (error) 199 return error; 200 201 eeti->irq_active_high = pdata->irq_active_high; 202 203 i2c_set_clientdata(client, eeti); 204 input_set_drvdata(input, eeti); 205 206 error = devm_request_threaded_irq(dev, client->irq, 207 NULL, eeti_ts_isr, 208 IRQF_ONESHOT, 209 client->name, eeti); 210 if (error) { 211 dev_err(dev, "Unable to request touchscreen IRQ: %d\n", 212 error); 213 return error; 214 } 215 216 /* 217 * Disable the device for now. It will be enabled once the 218 * input device is opened. 219 */ 220 eeti_ts_stop(eeti); 221 222 error = input_register_device(input); 223 if (error) 224 return error; 225 226 return 0; 227 } 228 229 static int __maybe_unused eeti_ts_suspend(struct device *dev) 230 { 231 struct i2c_client *client = to_i2c_client(dev); 232 struct eeti_ts *eeti = i2c_get_clientdata(client); 233 struct input_dev *input_dev = eeti->input; 234 235 mutex_lock(&input_dev->mutex); 236 237 if (input_dev->users) 238 eeti_ts_stop(eeti); 239 240 mutex_unlock(&input_dev->mutex); 241 242 if (device_may_wakeup(&client->dev)) 243 enable_irq_wake(client->irq); 244 245 return 0; 246 } 247 248 static int __maybe_unused eeti_ts_resume(struct device *dev) 249 { 250 struct i2c_client *client = to_i2c_client(dev); 251 struct eeti_ts *eeti = i2c_get_clientdata(client); 252 struct input_dev *input_dev = eeti->input; 253 254 if (device_may_wakeup(&client->dev)) 255 disable_irq_wake(client->irq); 256 257 mutex_lock(&input_dev->mutex); 258 259 if (input_dev->users) 260 eeti_ts_start(eeti); 261 262 mutex_unlock(&input_dev->mutex); 263 264 return 0; 265 } 266 267 static SIMPLE_DEV_PM_OPS(eeti_ts_pm, eeti_ts_suspend, eeti_ts_resume); 268 269 static const struct i2c_device_id eeti_ts_id[] = { 270 { "eeti_ts", 0 }, 271 { } 272 }; 273 MODULE_DEVICE_TABLE(i2c, eeti_ts_id); 274 275 static struct i2c_driver eeti_ts_driver = { 276 .driver = { 277 .name = "eeti_ts", 278 .pm = &eeti_ts_pm, 279 }, 280 .probe = eeti_ts_probe, 281 .id_table = eeti_ts_id, 282 }; 283 284 module_i2c_driver(eeti_ts_driver); 285 286 MODULE_DESCRIPTION("EETI Touchscreen driver"); 287 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>"); 288 MODULE_LICENSE("GPL"); 289