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