1 /* 2 * Driver for Goodix Touchscreens 3 * 4 * Copyright (c) 2014 Red Hat Inc. 5 * 6 * This code is based on gt9xx.c authored by andrew@goodix.com: 7 * 8 * 2010 - 2012 Goodix Technology. 9 */ 10 11 /* 12 * This program is free software; you can redistribute it and/or modify it 13 * under the terms of the GNU General Public License as published by the Free 14 * Software Foundation; version 2 of the License. 15 */ 16 17 #include <linux/kernel.h> 18 #include <linux/i2c.h> 19 #include <linux/input.h> 20 #include <linux/input/mt.h> 21 #include <linux/module.h> 22 #include <linux/delay.h> 23 #include <linux/irq.h> 24 #include <linux/interrupt.h> 25 #include <linux/slab.h> 26 #include <linux/acpi.h> 27 #include <linux/of.h> 28 #include <asm/unaligned.h> 29 30 struct goodix_ts_data { 31 struct i2c_client *client; 32 struct input_dev *input_dev; 33 int abs_x_max; 34 int abs_y_max; 35 unsigned int max_touch_num; 36 unsigned int int_trigger_type; 37 }; 38 39 #define GOODIX_MAX_HEIGHT 4096 40 #define GOODIX_MAX_WIDTH 4096 41 #define GOODIX_INT_TRIGGER 1 42 #define GOODIX_CONTACT_SIZE 8 43 #define GOODIX_MAX_CONTACTS 10 44 45 #define GOODIX_CONFIG_MAX_LENGTH 240 46 47 /* Register defines */ 48 #define GOODIX_READ_COOR_ADDR 0x814E 49 #define GOODIX_REG_CONFIG_DATA 0x8047 50 #define GOODIX_REG_VERSION 0x8140 51 52 #define RESOLUTION_LOC 1 53 #define MAX_CONTACTS_LOC 5 54 #define TRIGGER_LOC 6 55 56 static const unsigned long goodix_irq_flags[] = { 57 IRQ_TYPE_EDGE_RISING, 58 IRQ_TYPE_EDGE_FALLING, 59 IRQ_TYPE_LEVEL_LOW, 60 IRQ_TYPE_LEVEL_HIGH, 61 }; 62 63 /** 64 * goodix_i2c_read - read data from a register of the i2c slave device. 65 * 66 * @client: i2c device. 67 * @reg: the register to read from. 68 * @buf: raw write data buffer. 69 * @len: length of the buffer to write 70 */ 71 static int goodix_i2c_read(struct i2c_client *client, 72 u16 reg, u8 *buf, int len) 73 { 74 struct i2c_msg msgs[2]; 75 u16 wbuf = cpu_to_be16(reg); 76 int ret; 77 78 msgs[0].flags = 0; 79 msgs[0].addr = client->addr; 80 msgs[0].len = 2; 81 msgs[0].buf = (u8 *) &wbuf; 82 83 msgs[1].flags = I2C_M_RD; 84 msgs[1].addr = client->addr; 85 msgs[1].len = len; 86 msgs[1].buf = buf; 87 88 ret = i2c_transfer(client->adapter, msgs, 2); 89 return ret < 0 ? ret : (ret != ARRAY_SIZE(msgs) ? -EIO : 0); 90 } 91 92 static int goodix_ts_read_input_report(struct goodix_ts_data *ts, u8 *data) 93 { 94 int touch_num; 95 int error; 96 97 error = goodix_i2c_read(ts->client, GOODIX_READ_COOR_ADDR, data, 98 GOODIX_CONTACT_SIZE + 1); 99 if (error) { 100 dev_err(&ts->client->dev, "I2C transfer error: %d\n", error); 101 return error; 102 } 103 104 touch_num = data[0] & 0x0f; 105 if (touch_num > ts->max_touch_num) 106 return -EPROTO; 107 108 if (touch_num > 1) { 109 data += 1 + GOODIX_CONTACT_SIZE; 110 error = goodix_i2c_read(ts->client, 111 GOODIX_READ_COOR_ADDR + 112 1 + GOODIX_CONTACT_SIZE, 113 data, 114 GOODIX_CONTACT_SIZE * (touch_num - 1)); 115 if (error) 116 return error; 117 } 118 119 return touch_num; 120 } 121 122 static void goodix_ts_report_touch(struct goodix_ts_data *ts, u8 *coor_data) 123 { 124 int id = coor_data[0] & 0x0F; 125 int input_x = get_unaligned_le16(&coor_data[1]); 126 int input_y = get_unaligned_le16(&coor_data[3]); 127 int input_w = get_unaligned_le16(&coor_data[5]); 128 129 input_mt_slot(ts->input_dev, id); 130 input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true); 131 input_report_abs(ts->input_dev, ABS_MT_POSITION_X, input_x); 132 input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, input_y); 133 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w); 134 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w); 135 } 136 137 /** 138 * goodix_process_events - Process incoming events 139 * 140 * @ts: our goodix_ts_data pointer 141 * 142 * Called when the IRQ is triggered. Read the current device state, and push 143 * the input events to the user space. 144 */ 145 static void goodix_process_events(struct goodix_ts_data *ts) 146 { 147 u8 point_data[1 + GOODIX_CONTACT_SIZE * ts->max_touch_num]; 148 int touch_num; 149 int i; 150 151 touch_num = goodix_ts_read_input_report(ts, point_data); 152 if (touch_num < 0) 153 return; 154 155 for (i = 0; i < touch_num; i++) 156 goodix_ts_report_touch(ts, 157 &point_data[1 + GOODIX_CONTACT_SIZE * i]); 158 159 input_mt_sync_frame(ts->input_dev); 160 input_sync(ts->input_dev); 161 } 162 163 /** 164 * goodix_ts_irq_handler - The IRQ handler 165 * 166 * @irq: interrupt number. 167 * @dev_id: private data pointer. 168 */ 169 static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id) 170 { 171 static const u8 end_cmd[] = { 172 GOODIX_READ_COOR_ADDR >> 8, 173 GOODIX_READ_COOR_ADDR & 0xff, 174 0 175 }; 176 struct goodix_ts_data *ts = dev_id; 177 178 goodix_process_events(ts); 179 180 if (i2c_master_send(ts->client, end_cmd, sizeof(end_cmd)) < 0) 181 dev_err(&ts->client->dev, "I2C write end_cmd error\n"); 182 183 return IRQ_HANDLED; 184 } 185 186 /** 187 * goodix_read_config - Read the embedded configuration of the panel 188 * 189 * @ts: our goodix_ts_data pointer 190 * 191 * Must be called during probe 192 */ 193 static void goodix_read_config(struct goodix_ts_data *ts) 194 { 195 u8 config[GOODIX_CONFIG_MAX_LENGTH]; 196 int error; 197 198 error = goodix_i2c_read(ts->client, GOODIX_REG_CONFIG_DATA, 199 config, 200 GOODIX_CONFIG_MAX_LENGTH); 201 if (error) { 202 dev_warn(&ts->client->dev, 203 "Error reading config (%d), using defaults\n", 204 error); 205 ts->abs_x_max = GOODIX_MAX_WIDTH; 206 ts->abs_y_max = GOODIX_MAX_HEIGHT; 207 ts->int_trigger_type = GOODIX_INT_TRIGGER; 208 ts->max_touch_num = GOODIX_MAX_CONTACTS; 209 return; 210 } 211 212 ts->abs_x_max = get_unaligned_le16(&config[RESOLUTION_LOC]); 213 ts->abs_y_max = get_unaligned_le16(&config[RESOLUTION_LOC + 2]); 214 ts->int_trigger_type = config[TRIGGER_LOC] & 0x03; 215 ts->max_touch_num = config[MAX_CONTACTS_LOC] & 0x0f; 216 if (!ts->abs_x_max || !ts->abs_y_max || !ts->max_touch_num) { 217 dev_err(&ts->client->dev, 218 "Invalid config, using defaults\n"); 219 ts->abs_x_max = GOODIX_MAX_WIDTH; 220 ts->abs_y_max = GOODIX_MAX_HEIGHT; 221 ts->max_touch_num = GOODIX_MAX_CONTACTS; 222 } 223 } 224 225 /** 226 * goodix_read_version - Read goodix touchscreen version 227 * 228 * @client: the i2c client 229 * @version: output buffer containing the version on success 230 */ 231 static int goodix_read_version(struct i2c_client *client, u16 *version) 232 { 233 int error; 234 u8 buf[6]; 235 236 error = goodix_i2c_read(client, GOODIX_REG_VERSION, buf, sizeof(buf)); 237 if (error) { 238 dev_err(&client->dev, "read version failed: %d\n", error); 239 return error; 240 } 241 242 if (version) 243 *version = get_unaligned_le16(&buf[4]); 244 245 dev_info(&client->dev, "IC VERSION: %6ph\n", buf); 246 247 return 0; 248 } 249 250 /** 251 * goodix_i2c_test - I2C test function to check if the device answers. 252 * 253 * @client: the i2c client 254 */ 255 static int goodix_i2c_test(struct i2c_client *client) 256 { 257 int retry = 0; 258 int error; 259 u8 test; 260 261 while (retry++ < 2) { 262 error = goodix_i2c_read(client, GOODIX_REG_CONFIG_DATA, 263 &test, 1); 264 if (!error) 265 return 0; 266 267 dev_err(&client->dev, "i2c test failed attempt %d: %d\n", 268 retry, error); 269 msleep(20); 270 } 271 272 return error; 273 } 274 275 /** 276 * goodix_request_input_dev - Allocate, populate and register the input device 277 * 278 * @ts: our goodix_ts_data pointer 279 * 280 * Must be called during probe 281 */ 282 static int goodix_request_input_dev(struct goodix_ts_data *ts) 283 { 284 int error; 285 286 ts->input_dev = devm_input_allocate_device(&ts->client->dev); 287 if (!ts->input_dev) { 288 dev_err(&ts->client->dev, "Failed to allocate input device."); 289 return -ENOMEM; 290 } 291 292 ts->input_dev->evbit[0] = BIT_MASK(EV_SYN) | 293 BIT_MASK(EV_KEY) | 294 BIT_MASK(EV_ABS); 295 296 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X, 0, 297 ts->abs_x_max, 0, 0); 298 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y, 0, 299 ts->abs_y_max, 0, 0); 300 input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0); 301 input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0); 302 303 input_mt_init_slots(ts->input_dev, ts->max_touch_num, 304 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED); 305 306 ts->input_dev->name = "Goodix Capacitive TouchScreen"; 307 ts->input_dev->phys = "input/ts"; 308 ts->input_dev->id.bustype = BUS_I2C; 309 ts->input_dev->id.vendor = 0x0416; 310 ts->input_dev->id.product = 0x1001; 311 ts->input_dev->id.version = 10427; 312 313 error = input_register_device(ts->input_dev); 314 if (error) { 315 dev_err(&ts->client->dev, 316 "Failed to register input device: %d", error); 317 return error; 318 } 319 320 return 0; 321 } 322 323 static int goodix_ts_probe(struct i2c_client *client, 324 const struct i2c_device_id *id) 325 { 326 struct goodix_ts_data *ts; 327 unsigned long irq_flags; 328 int error; 329 u16 version_info; 330 331 dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr); 332 333 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { 334 dev_err(&client->dev, "I2C check functionality failed.\n"); 335 return -ENXIO; 336 } 337 338 ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL); 339 if (!ts) 340 return -ENOMEM; 341 342 ts->client = client; 343 i2c_set_clientdata(client, ts); 344 345 error = goodix_i2c_test(client); 346 if (error) { 347 dev_err(&client->dev, "I2C communication failure: %d\n", error); 348 return error; 349 } 350 351 error = goodix_read_version(client, &version_info); 352 if (error) { 353 dev_err(&client->dev, "Read version failed.\n"); 354 return error; 355 } 356 357 goodix_read_config(ts); 358 359 error = goodix_request_input_dev(ts); 360 if (error) 361 return error; 362 363 irq_flags = goodix_irq_flags[ts->int_trigger_type] | IRQF_ONESHOT; 364 error = devm_request_threaded_irq(&ts->client->dev, client->irq, 365 NULL, goodix_ts_irq_handler, 366 irq_flags, client->name, ts); 367 if (error) { 368 dev_err(&client->dev, "request IRQ failed: %d\n", error); 369 return error; 370 } 371 372 return 0; 373 } 374 375 static const struct i2c_device_id goodix_ts_id[] = { 376 { "GDIX1001:00", 0 }, 377 { } 378 }; 379 380 #ifdef CONFIG_ACPI 381 static const struct acpi_device_id goodix_acpi_match[] = { 382 { "GDIX1001", 0 }, 383 { } 384 }; 385 MODULE_DEVICE_TABLE(acpi, goodix_acpi_match); 386 #endif 387 388 #ifdef CONFIG_OF 389 static const struct of_device_id goodix_of_match[] = { 390 { .compatible = "goodix,gt911" }, 391 { .compatible = "goodix,gt9110" }, 392 { .compatible = "goodix,gt912" }, 393 { .compatible = "goodix,gt927" }, 394 { .compatible = "goodix,gt9271" }, 395 { .compatible = "goodix,gt928" }, 396 { .compatible = "goodix,gt967" }, 397 { } 398 }; 399 MODULE_DEVICE_TABLE(of, goodix_of_match); 400 #endif 401 402 static struct i2c_driver goodix_ts_driver = { 403 .probe = goodix_ts_probe, 404 .id_table = goodix_ts_id, 405 .driver = { 406 .name = "Goodix-TS", 407 .owner = THIS_MODULE, 408 .acpi_match_table = ACPI_PTR(goodix_acpi_match), 409 .of_match_table = of_match_ptr(goodix_of_match), 410 }, 411 }; 412 module_i2c_driver(goodix_ts_driver); 413 414 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>"); 415 MODULE_AUTHOR("Bastien Nocera <hadess@hadess.net>"); 416 MODULE_DESCRIPTION("Goodix touchscreen driver"); 417 MODULE_LICENSE("GPL v2"); 418