1 /* 2 * Driver for ELAN eKTF2127 i2c touchscreen controller 3 * 4 * For this driver the layout of the Chipone icn8318 i2c 5 * touchscreencontroller is used. 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * Author: 13 * Michel Verlaan <michel.verl@gmail.com> 14 * Siebren Vroegindeweij <siebren.vroegindeweij@hotmail.com> 15 * 16 * Original chipone_icn8318 driver: 17 * Hans de Goede <hdegoede@redhat.com> 18 */ 19 20 #include <linux/gpio/consumer.h> 21 #include <linux/interrupt.h> 22 #include <linux/i2c.h> 23 #include <linux/input.h> 24 #include <linux/input/mt.h> 25 #include <linux/input/touchscreen.h> 26 #include <linux/module.h> 27 #include <linux/of.h> 28 #include <linux/delay.h> 29 30 /* Packet header defines (first byte of data send / received) */ 31 #define EKTF2127_NOISE 0x40 32 #define EKTF2127_RESPONSE 0x52 33 #define EKTF2127_REQUEST 0x53 34 #define EKTF2127_HELLO 0x55 35 #define EKTF2127_REPORT 0x5d 36 #define EKTF2127_CALIB_DONE 0x66 37 38 /* Register defines (second byte of data send / received) */ 39 #define EKTF2127_ENV_NOISY 0x41 40 #define EKTF2127_HEIGHT 0x60 41 #define EKTF2127_WIDTH 0x63 42 43 /* 2 bytes header + 5 * 3 bytes coordinates + 3 bytes pressure info + footer */ 44 #define EKTF2127_TOUCH_REPORT_SIZE 21 45 #define EKTF2127_MAX_TOUCHES 5 46 47 struct ektf2127_ts { 48 struct i2c_client *client; 49 struct input_dev *input; 50 struct gpio_desc *power_gpios; 51 struct touchscreen_properties prop; 52 }; 53 54 static void ektf2127_parse_coordinates(const u8 *buf, unsigned int touch_count, 55 struct input_mt_pos *touches) 56 { 57 int index = 0; 58 int i; 59 60 for (i = 0; i < touch_count; i++) { 61 index = 2 + i * 3; 62 63 touches[i].x = (buf[index] & 0x0f); 64 touches[i].x <<= 8; 65 touches[i].x |= buf[index + 2]; 66 67 touches[i].y = (buf[index] & 0xf0); 68 touches[i].y <<= 4; 69 touches[i].y |= buf[index + 1]; 70 } 71 } 72 73 static void ektf2127_report_event(struct ektf2127_ts *ts, const u8 *buf) 74 { 75 struct input_mt_pos touches[EKTF2127_MAX_TOUCHES]; 76 int slots[EKTF2127_MAX_TOUCHES]; 77 unsigned int touch_count, i; 78 79 touch_count = buf[1] & 0x07; 80 if (touch_count > EKTF2127_MAX_TOUCHES) { 81 dev_err(&ts->client->dev, 82 "Too many touches %d > %d\n", 83 touch_count, EKTF2127_MAX_TOUCHES); 84 touch_count = EKTF2127_MAX_TOUCHES; 85 } 86 87 ektf2127_parse_coordinates(buf, touch_count, touches); 88 input_mt_assign_slots(ts->input, slots, touches, 89 touch_count, 0); 90 91 for (i = 0; i < touch_count; i++) { 92 input_mt_slot(ts->input, slots[i]); 93 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, true); 94 touchscreen_report_pos(ts->input, &ts->prop, 95 touches[i].x, touches[i].y, true); 96 } 97 98 input_mt_sync_frame(ts->input); 99 input_sync(ts->input); 100 } 101 102 static irqreturn_t ektf2127_irq(int irq, void *dev_id) 103 { 104 struct ektf2127_ts *ts = dev_id; 105 struct device *dev = &ts->client->dev; 106 char buf[EKTF2127_TOUCH_REPORT_SIZE]; 107 int ret; 108 109 ret = i2c_master_recv(ts->client, buf, EKTF2127_TOUCH_REPORT_SIZE); 110 if (ret != EKTF2127_TOUCH_REPORT_SIZE) { 111 dev_err(dev, "Error reading touch data: %d\n", ret); 112 goto out; 113 } 114 115 switch (buf[0]) { 116 case EKTF2127_REPORT: 117 ektf2127_report_event(ts, buf); 118 break; 119 120 case EKTF2127_NOISE: 121 if (buf[1] == EKTF2127_ENV_NOISY) 122 dev_dbg(dev, "Environment is electrically noisy\n"); 123 break; 124 125 case EKTF2127_HELLO: 126 case EKTF2127_CALIB_DONE: 127 break; 128 129 default: 130 dev_err(dev, "Unexpected packet header byte %#02x\n", buf[0]); 131 break; 132 } 133 134 out: 135 return IRQ_HANDLED; 136 } 137 138 static int ektf2127_start(struct input_dev *dev) 139 { 140 struct ektf2127_ts *ts = input_get_drvdata(dev); 141 142 enable_irq(ts->client->irq); 143 gpiod_set_value_cansleep(ts->power_gpios, 1); 144 145 return 0; 146 } 147 148 static void ektf2127_stop(struct input_dev *dev) 149 { 150 struct ektf2127_ts *ts = input_get_drvdata(dev); 151 152 disable_irq(ts->client->irq); 153 gpiod_set_value_cansleep(ts->power_gpios, 0); 154 } 155 156 static int __maybe_unused ektf2127_suspend(struct device *dev) 157 { 158 struct ektf2127_ts *ts = i2c_get_clientdata(to_i2c_client(dev)); 159 160 mutex_lock(&ts->input->mutex); 161 if (ts->input->users) 162 ektf2127_stop(ts->input); 163 mutex_unlock(&ts->input->mutex); 164 165 return 0; 166 } 167 168 static int __maybe_unused ektf2127_resume(struct device *dev) 169 { 170 struct ektf2127_ts *ts = i2c_get_clientdata(to_i2c_client(dev)); 171 172 mutex_lock(&ts->input->mutex); 173 if (ts->input->users) 174 ektf2127_start(ts->input); 175 mutex_unlock(&ts->input->mutex); 176 177 return 0; 178 } 179 180 static SIMPLE_DEV_PM_OPS(ektf2127_pm_ops, ektf2127_suspend, 181 ektf2127_resume); 182 183 static int ektf2127_query_dimension(struct i2c_client *client, bool width) 184 { 185 struct device *dev = &client->dev; 186 const char *what = width ? "width" : "height"; 187 u8 what_code = width ? EKTF2127_WIDTH : EKTF2127_HEIGHT; 188 u8 buf[4]; 189 int ret; 190 int error; 191 192 /* Request dimension */ 193 buf[0] = EKTF2127_REQUEST; 194 buf[1] = width ? EKTF2127_WIDTH : EKTF2127_HEIGHT; 195 buf[2] = 0x00; 196 buf[3] = 0x00; 197 ret = i2c_master_send(client, buf, sizeof(buf)); 198 if (ret != sizeof(buf)) { 199 error = ret < 0 ? ret : -EIO; 200 dev_err(dev, "Failed to request %s: %d\n", what, error); 201 return error; 202 } 203 204 msleep(20); 205 206 /* Read response */ 207 ret = i2c_master_recv(client, buf, sizeof(buf)); 208 if (ret != sizeof(buf)) { 209 error = ret < 0 ? ret : -EIO; 210 dev_err(dev, "Failed to receive %s data: %d\n", what, error); 211 return error; 212 } 213 214 if (buf[0] != EKTF2127_RESPONSE || buf[1] != what_code) { 215 dev_err(dev, "Unexpected %s data: %#02x %#02x\n", 216 what, buf[0], buf[1]); 217 return -EIO; 218 } 219 220 return (((buf[3] & 0xf0) << 4) | buf[2]) - 1; 221 } 222 223 static int ektf2127_probe(struct i2c_client *client, 224 const struct i2c_device_id *id) 225 { 226 struct device *dev = &client->dev; 227 struct ektf2127_ts *ts; 228 struct input_dev *input; 229 u8 buf[4]; 230 int max_x, max_y; 231 int error; 232 233 if (!client->irq) { 234 dev_err(dev, "Error no irq specified\n"); 235 return -EINVAL; 236 } 237 238 ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL); 239 if (!ts) 240 return -ENOMEM; 241 242 /* This requests the gpio *and* turns on the touchscreen controller */ 243 ts->power_gpios = devm_gpiod_get(dev, "power", GPIOD_OUT_HIGH); 244 if (IS_ERR(ts->power_gpios)) { 245 error = PTR_ERR(ts->power_gpios); 246 if (error != -EPROBE_DEFER) 247 dev_err(dev, "Error getting power gpio: %d\n", error); 248 return error; 249 } 250 251 input = devm_input_allocate_device(dev); 252 if (!input) 253 return -ENOMEM; 254 255 input->name = client->name; 256 input->id.bustype = BUS_I2C; 257 input->open = ektf2127_start; 258 input->close = ektf2127_stop; 259 260 ts->client = client; 261 262 /* Read hello (ignore result, depends on initial power state) */ 263 msleep(20); 264 i2c_master_recv(ts->client, buf, sizeof(buf)); 265 266 /* Read resolution from chip */ 267 max_x = ektf2127_query_dimension(client, true); 268 if (max_x < 0) 269 return max_x; 270 271 max_y = ektf2127_query_dimension(client, false); 272 if (max_y < 0) 273 return max_y; 274 275 input_set_abs_params(input, ABS_MT_POSITION_X, 0, max_x, 0, 0); 276 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, max_y, 0, 0); 277 touchscreen_parse_properties(input, true, &ts->prop); 278 279 error = input_mt_init_slots(input, EKTF2127_MAX_TOUCHES, 280 INPUT_MT_DIRECT | 281 INPUT_MT_DROP_UNUSED | 282 INPUT_MT_TRACK); 283 if (error) 284 return error; 285 286 ts->input = input; 287 input_set_drvdata(input, ts); 288 289 error = devm_request_threaded_irq(dev, client->irq, 290 NULL, ektf2127_irq, 291 IRQF_ONESHOT, client->name, ts); 292 if (error) { 293 dev_err(dev, "Error requesting irq: %d\n", error); 294 return error; 295 } 296 297 /* Stop device till opened */ 298 ektf2127_stop(ts->input); 299 300 error = input_register_device(input); 301 if (error) 302 return error; 303 304 i2c_set_clientdata(client, ts); 305 306 return 0; 307 } 308 309 #ifdef CONFIG_OF 310 static const struct of_device_id ektf2127_of_match[] = { 311 { .compatible = "elan,ektf2127" }, 312 {} 313 }; 314 MODULE_DEVICE_TABLE(of, ektf2127_of_match); 315 #endif 316 317 static const struct i2c_device_id ektf2127_i2c_id[] = { 318 { "ektf2127", 0 }, 319 {} 320 }; 321 MODULE_DEVICE_TABLE(i2c, ektf2127_i2c_id); 322 323 static struct i2c_driver ektf2127_driver = { 324 .driver = { 325 .name = "elan_ektf2127", 326 .pm = &ektf2127_pm_ops, 327 .of_match_table = of_match_ptr(ektf2127_of_match), 328 }, 329 .probe = ektf2127_probe, 330 .id_table = ektf2127_i2c_id, 331 }; 332 module_i2c_driver(ektf2127_driver); 333 334 MODULE_DESCRIPTION("ELAN eKTF2127 I2C Touchscreen Driver"); 335 MODULE_AUTHOR("Michel Verlaan, Siebren Vroegindeweij"); 336 MODULE_LICENSE("GPL"); 337