1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Touch Screen driver for EETI's I2C connected touch screen panels 4 * Copyright (c) 2009,2018 Daniel Mack <daniel@zonque.org> 5 * 6 * See EETI's software guide for the protocol specification: 7 * http://home.eeti.com.tw/documentation.html 8 * 9 * Based on migor_ts.c 10 * Copyright (c) 2008 Magnus Damm 11 * Copyright (c) 2007 Ujjwal Pande <ujjwal@kenati.com> 12 */ 13 14 #include <linux/module.h> 15 #include <linux/kernel.h> 16 #include <linux/input.h> 17 #include <linux/input/touchscreen.h> 18 #include <linux/interrupt.h> 19 #include <linux/i2c.h> 20 #include <linux/timer.h> 21 #include <linux/gpio/consumer.h> 22 #include <linux/of.h> 23 #include <linux/slab.h> 24 #include <asm/unaligned.h> 25 26 struct eeti_ts { 27 struct i2c_client *client; 28 struct input_dev *input; 29 struct gpio_desc *attn_gpio; 30 struct touchscreen_properties props; 31 bool running; 32 }; 33 34 #define EETI_TS_BITDEPTH (11) 35 #define EETI_MAXVAL ((1 << (EETI_TS_BITDEPTH + 1)) - 1) 36 37 #define REPORT_BIT_PRESSED BIT(0) 38 #define REPORT_BIT_AD0 BIT(1) 39 #define REPORT_BIT_AD1 BIT(2) 40 #define REPORT_BIT_HAS_PRESSURE BIT(6) 41 #define REPORT_RES_BITS(v) (((v) >> 1) + EETI_TS_BITDEPTH) 42 43 static void eeti_ts_report_event(struct eeti_ts *eeti, u8 *buf) 44 { 45 unsigned int res; 46 u16 x, y; 47 48 res = REPORT_RES_BITS(buf[0] & (REPORT_BIT_AD0 | REPORT_BIT_AD1)); 49 50 x = get_unaligned_be16(&buf[1]); 51 y = get_unaligned_be16(&buf[3]); 52 53 /* fix the range to 11 bits */ 54 x >>= res - EETI_TS_BITDEPTH; 55 y >>= res - EETI_TS_BITDEPTH; 56 57 if (buf[0] & REPORT_BIT_HAS_PRESSURE) 58 input_report_abs(eeti->input, ABS_PRESSURE, buf[5]); 59 60 touchscreen_report_pos(eeti->input, &eeti->props, x, y, false); 61 input_report_key(eeti->input, BTN_TOUCH, buf[0] & REPORT_BIT_PRESSED); 62 input_sync(eeti->input); 63 } 64 65 static irqreturn_t eeti_ts_isr(int irq, void *dev_id) 66 { 67 struct eeti_ts *eeti = dev_id; 68 int len; 69 int error; 70 char buf[6]; 71 72 do { 73 len = i2c_master_recv(eeti->client, buf, sizeof(buf)); 74 if (len != sizeof(buf)) { 75 error = len < 0 ? len : -EIO; 76 dev_err(&eeti->client->dev, 77 "failed to read touchscreen data: %d\n", 78 error); 79 break; 80 } 81 82 if (buf[0] & 0x80) { 83 /* Motion packet */ 84 eeti_ts_report_event(eeti, buf); 85 } 86 } while (eeti->running && 87 eeti->attn_gpio && gpiod_get_value_cansleep(eeti->attn_gpio)); 88 89 return IRQ_HANDLED; 90 } 91 92 static void eeti_ts_start(struct eeti_ts *eeti) 93 { 94 eeti->running = true; 95 wmb(); 96 enable_irq(eeti->client->irq); 97 } 98 99 static void eeti_ts_stop(struct eeti_ts *eeti) 100 { 101 eeti->running = false; 102 wmb(); 103 disable_irq(eeti->client->irq); 104 } 105 106 static int eeti_ts_open(struct input_dev *dev) 107 { 108 struct eeti_ts *eeti = input_get_drvdata(dev); 109 110 eeti_ts_start(eeti); 111 112 return 0; 113 } 114 115 static void eeti_ts_close(struct input_dev *dev) 116 { 117 struct eeti_ts *eeti = input_get_drvdata(dev); 118 119 eeti_ts_stop(eeti); 120 } 121 122 static int eeti_ts_probe(struct i2c_client *client, 123 const struct i2c_device_id *idp) 124 { 125 struct device *dev = &client->dev; 126 struct eeti_ts *eeti; 127 struct input_dev *input; 128 int error; 129 130 /* 131 * In contrast to what's described in the datasheet, there seems 132 * to be no way of probing the presence of that device using I2C 133 * commands. So we need to blindly believe it is there, and wait 134 * for interrupts to occur. 135 */ 136 137 eeti = devm_kzalloc(dev, sizeof(*eeti), GFP_KERNEL); 138 if (!eeti) { 139 dev_err(dev, "failed to allocate driver data\n"); 140 return -ENOMEM; 141 } 142 143 input = devm_input_allocate_device(dev); 144 if (!input) { 145 dev_err(dev, "Failed to allocate input device.\n"); 146 return -ENOMEM; 147 } 148 149 input_set_capability(input, EV_KEY, BTN_TOUCH); 150 151 input_set_abs_params(input, ABS_X, 0, EETI_MAXVAL, 0, 0); 152 input_set_abs_params(input, ABS_Y, 0, EETI_MAXVAL, 0, 0); 153 input_set_abs_params(input, ABS_PRESSURE, 0, 0xff, 0, 0); 154 155 touchscreen_parse_properties(input, false, &eeti->props); 156 157 input->name = client->name; 158 input->id.bustype = BUS_I2C; 159 input->open = eeti_ts_open; 160 input->close = eeti_ts_close; 161 162 eeti->client = client; 163 eeti->input = input; 164 165 eeti->attn_gpio = devm_gpiod_get_optional(dev, "attn", GPIOD_IN); 166 if (IS_ERR(eeti->attn_gpio)) 167 return PTR_ERR(eeti->attn_gpio); 168 169 i2c_set_clientdata(client, eeti); 170 input_set_drvdata(input, eeti); 171 172 error = devm_request_threaded_irq(dev, client->irq, 173 NULL, eeti_ts_isr, 174 IRQF_ONESHOT, 175 client->name, eeti); 176 if (error) { 177 dev_err(dev, "Unable to request touchscreen IRQ: %d\n", 178 error); 179 return error; 180 } 181 182 /* 183 * Disable the device for now. It will be enabled once the 184 * input device is opened. 185 */ 186 eeti_ts_stop(eeti); 187 188 error = input_register_device(input); 189 if (error) 190 return error; 191 192 return 0; 193 } 194 195 static int __maybe_unused eeti_ts_suspend(struct device *dev) 196 { 197 struct i2c_client *client = to_i2c_client(dev); 198 struct eeti_ts *eeti = i2c_get_clientdata(client); 199 struct input_dev *input_dev = eeti->input; 200 201 mutex_lock(&input_dev->mutex); 202 203 if (input_dev->users) 204 eeti_ts_stop(eeti); 205 206 mutex_unlock(&input_dev->mutex); 207 208 if (device_may_wakeup(&client->dev)) 209 enable_irq_wake(client->irq); 210 211 return 0; 212 } 213 214 static int __maybe_unused eeti_ts_resume(struct device *dev) 215 { 216 struct i2c_client *client = to_i2c_client(dev); 217 struct eeti_ts *eeti = i2c_get_clientdata(client); 218 struct input_dev *input_dev = eeti->input; 219 220 if (device_may_wakeup(&client->dev)) 221 disable_irq_wake(client->irq); 222 223 mutex_lock(&input_dev->mutex); 224 225 if (input_dev->users) 226 eeti_ts_start(eeti); 227 228 mutex_unlock(&input_dev->mutex); 229 230 return 0; 231 } 232 233 static SIMPLE_DEV_PM_OPS(eeti_ts_pm, eeti_ts_suspend, eeti_ts_resume); 234 235 static const struct i2c_device_id eeti_ts_id[] = { 236 { "eeti_ts", 0 }, 237 { } 238 }; 239 MODULE_DEVICE_TABLE(i2c, eeti_ts_id); 240 241 #ifdef CONFIG_OF 242 static const struct of_device_id of_eeti_ts_match[] = { 243 { .compatible = "eeti,exc3000-i2c", }, 244 { } 245 }; 246 #endif 247 248 static struct i2c_driver eeti_ts_driver = { 249 .driver = { 250 .name = "eeti_ts", 251 .pm = &eeti_ts_pm, 252 .of_match_table = of_match_ptr(of_eeti_ts_match), 253 }, 254 .probe = eeti_ts_probe, 255 .id_table = eeti_ts_id, 256 }; 257 258 module_i2c_driver(eeti_ts_driver); 259 260 MODULE_DESCRIPTION("EETI Touchscreen driver"); 261 MODULE_AUTHOR("Daniel Mack <daniel@zonque.org>"); 262 MODULE_LICENSE("GPL"); 263