1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
|
#include <linux/module.h> #include <linux/i2c.h> #include <linux/regmap.h> #include <linux/gpio/consumer.h> #include <linux/of_irq.h> #include <linux/interrupt.h> #include <linux/input.h> #include <linux/input/mt.h> #include <linux/debugfs.h> #include <linux/delay.h> #include <linux/slab.h> #include <linux/gpio.h> #include <linux/of_gpio.h> #include <linux/input/mt.h> #include <linux/input/touchscreen.h> #include <linux/i2c.h>
#define GT_CTRL_REG 0X8040 #define GT_MODSW_REG 0X804D #define GT_CFGS_REG 0X8047 #define GT_CHECK_REG 0X80FF #define GT_PID_REG 0X8140
#define GT_GSTID_REG 0X814E #define GT_TP1_REG 0X814F #define GT_TP2_REG 0X8157 #define GT_TP3_REG 0X815F #define GT_TP4_REG 0X8167 #define GT_TP5_REG 0X816F #define MAX_SUPPORT_POINTS 3 #define MORE_GT911 0
#define GT911_TP_DATA_SIZE 8 #define BUFFER_SIZE (GT911_TP_DATA_SIZE * MAX_SUPPORT_POINTS)
struct gt911_dev { int irq_pin,reset_pin; int irqnum; void *private_data; struct input_dev *input; struct i2c_client *client;
}; struct gt911_dev gt911;
const unsigned char gt911_CT[]= { 0x48,0xe0,0x01,0x10,0x01,0x05,0x0d,0x00,0x01,0x08, 0x28,0x05,0x50,0x32,0x03,0x05,0x00,0x00,0xff,0xff, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x89,0x28,0x0a, 0x17,0x15,0x31,0x0d,0x00,0x00,0x02,0x9b,0x03,0x25, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x32,0x00,0x00, 0x00,0x0f,0x94,0x94,0xc5,0x02,0x07,0x00,0x00,0x04, 0x8d,0x13,0x00,0x5c,0x1e,0x00,0x3c,0x30,0x00,0x29, 0x4c,0x00,0x1e,0x78,0x00,0x1e,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x08,0x0a,0x0c,0x0e,0x10,0x12,0x14,0x16, 0x18,0x1a,0x00,0x00,0x00,0x00,0x1f,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0x00,0x02,0x04,0x05,0x06,0x08,0x0a,0x0c, 0x0e,0x1d,0x1e,0x1f,0x20,0x22,0x24,0x28,0x29,0xff, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff, 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, };
static int gt911_ts_reset(struct i2c_client *client, struct gt911_dev *dev) { int ret = 0;
if (gpio_is_valid(dev->reset_pin)) { ret = devm_gpio_request_one(&client->dev, dev->reset_pin, GPIOF_OUT_INIT_LOW, "gt911 reset"); if (ret) { return ret; } }
if (gpio_is_valid(dev->irq_pin)) { ret = devm_gpio_request_one(&client->dev, dev->irq_pin, GPIOF_OUT_INIT_LOW, "gt911 irq"); if (ret) { return ret; } }
gpio_set_value(dev->reset_pin, 0); msleep(10); gpio_set_value(dev->reset_pin, 1); msleep(10); gpio_set_value(dev->irq_pin, 0); msleep(50); gpio_direction_input(dev->irq_pin);
return 0; }
static int gt911_read_regs(struct gt911_dev *dev, u16 reg, u8 *buf, int len) { int ret; u8 regdata[2]; struct i2c_msg msg[2]; struct i2c_client *client = (struct i2c_client *)dev->client; regdata[0] = reg >> 8; regdata[1] = reg & 0xFF;
msg[0].addr = client->addr; msg[0].flags = !I2C_M_RD; msg[0].buf = ®data[0]; msg[0].len = 2;
msg[1].addr = client->addr; msg[1].flags = I2C_M_RD; msg[1].buf = buf; msg[1].len = len;
ret = i2c_transfer(client->adapter, msg, 2); if(ret == 2) { ret = 0; } else { ret = -EREMOTEIO; } return ret; }
static s32 gt911_write_regs(struct gt911_dev *dev, u16 reg, u8 *buf, u8 len) { u8 b[256]; struct i2c_msg msg; struct i2c_client *client = (struct i2c_client *)dev->client; b[0] = reg >> 8; b[1] = reg & 0XFF; memcpy(&b[2],buf,len);
msg.addr = client->addr; msg.flags = 0;
msg.buf = b; msg.len = len + 2;
return i2c_transfer(client->adapter, &msg, 1); }
#if MORE_GT911 == 0 static irqreturn_t gt911_irq_handler(int irq, void *dev_id) { int touch_num = 0; int input_x, input_y; int id = 0; int ret = 0; u8 data; u8 touch_data[5]; struct gt911_dev *dev = dev_id;
ret = gt911_read_regs(dev, GT_GSTID_REG, &data, 1); if (data == 0x00) { goto fail; } else { touch_num = data & 0x0f; }
if(touch_num) { gt911_read_regs(dev, GT_TP1_REG, touch_data, 5); id = touch_data[0] & 0x0F; if(id == 0) { input_x = touch_data[1] | (touch_data[2] << 8); input_y = touch_data[3] | (touch_data[4] << 8);
input_mt_slot(dev->input, id); input_mt_report_slot_state(dev->input, MT_TOOL_FINGER, true); input_report_abs(dev->input, ABS_MT_POSITION_X, input_x); input_report_abs(dev->input, ABS_MT_POSITION_Y, input_y); } } else if(touch_num == 0){ input_mt_slot(dev->input, id); input_mt_report_slot_state(dev->input, MT_TOOL_FINGER, false); }
input_mt_report_pointer_emulation(dev->input, true); input_sync(dev->input);
data = 0x00; gt911_write_regs(dev, GT_GSTID_REG, &data, 1);
fail: return IRQ_HANDLED; }
#else
static irqreturn_t gt911_irq_handler(int irq, void *dev_id) { struct gt911_dev *dev = dev_id; int input_x, input_y; int i,id = 0; int ret = 0; u8 data; int touch_num = 0; u8 buf[BUFFER_SIZE]; u16 reported_ids = 0; static u16 last_reported_ids = 0;
ret = gt911_read_regs(dev, GT_GSTID_REG, &data, 1); if (data == 0x00) { goto clear_irq; } else { touch_num = data & 0x0f; }
if(touch_num>0) { ret = gt911_read_regs(dev, GT_TP1_REG, buf, sizeof(buf)); if(ret<0) { goto clear_irq; } } if(touch_num>MAX_SUPPORT_POINTS)touch_num=MAX_SUPPORT_POINTS; for(i=0;i<touch_num;i++) { int pos = i*GT911_TP_DATA_SIZE; id = buf[pos] & 0x0f; input_x = (buf[pos + 1] | (buf[pos + 2] << 8)) & 0x0fff; input_y = (buf[pos + 3] | (buf[pos + 4] << 8)) & 0x0fff;
reported_ids |= (1 << id);
input_mt_slot(dev->input, id); input_mt_report_slot_state(dev->input, MT_TOOL_FINGER, true); input_report_abs(dev->input, ABS_MT_POSITION_X, input_x); input_report_abs(dev->input, ABS_MT_POSITION_Y, input_y); }
for (i = 0; i < MAX_SUPPORT_POINTS; i++) { if ((last_reported_ids & (1 << i)) && !(reported_ids & (1 << i))) { input_mt_slot(dev->input, i); input_mt_report_slot_state(dev->input, MT_TOOL_FINGER, false); } }
last_reported_ids = reported_ids; input_mt_report_pointer_emulation(dev->input, true); input_sync(dev->input);
clear_irq: data = 0x00; ret = gt911_write_regs(dev, GT_GSTID_REG, &data, 1); if (ret < 0) { printk(KERN_ERR "GT911: Failed to clear interrupt status\n"); } return IRQ_HANDLED; }
#endif
static int gt911_ts_irq(struct i2c_client *client, struct gt911_dev *dev) { int ret = 0;
ret = devm_request_threaded_irq(&client->dev, client->irq, NULL, gt911_irq_handler, IRQ_TYPE_EDGE_RISING | IRQF_ONESHOT, client->name, >911); if (ret) { dev_err(&client->dev, "Unable to request touchscreen IRQ.\n"); return ret; }
return 0; }
void gt911_send_cfg(struct gt911_dev *dev, unsigned char mode) { unsigned char buf[2]; unsigned int i = 0;
buf[0] = 0; buf[1] = mode; for(i = 0; i < (sizeof(gt911_CT)); i++) buf[0] += gt911_CT[i]; buf[0] = (~buf[0]) + 1;
gt911_write_regs(dev, GT_CFGS_REG, (u8 *)gt911_CT, sizeof(gt911_CT)); gt911_write_regs(dev, GT_CHECK_REG, buf, 2); }
int gt911_probe(struct i2c_client *client, const struct i2c_device_id *id) { u8 data, ret;
gt911.client = client;
gt911.irq_pin = of_get_named_gpio(client->dev.of_node, "interrupt-gpios", 0); gt911.reset_pin = of_get_named_gpio(client->dev.of_node, "reset-gpios", 0);
printk("irq:%d reset:%d\r\n",gt911.irq_pin,gt911.reset_pin);
ret = gt911_ts_reset(client, >911); if(ret < 0) { goto fail; }
data = 0x02; gt911_write_regs(>911, GT_CTRL_REG, &data, 1); mdelay(100); data = 0x0; gt911_write_regs(>911, GT_CTRL_REG, &data, 1); mdelay(100);
gt911.input = devm_input_allocate_device(&client->dev); if (!gt911.input) { ret = -ENOMEM; goto fail; } gt911.input->name = client->name; gt911.input->id.bustype = BUS_I2C; gt911.input->dev.parent = &client->dev;
__set_bit(EV_KEY, gt911.input->evbit); __set_bit(EV_ABS, gt911.input->evbit); __set_bit(BTN_TOUCH, gt911.input->keybit);
input_set_abs_params(gt911.input, ABS_X, 0, 1024, 0, 0); input_set_abs_params(gt911.input, ABS_Y, 0, 600, 0, 0); input_set_abs_params(gt911.input, ABS_MT_POSITION_X,0, 1024, 0, 0); input_set_abs_params(gt911.input, ABS_MT_POSITION_Y,0, 600, 0, 0);
ret = input_mt_init_slots(gt911.input, MAX_SUPPORT_POINTS, 0); if (ret) { goto fail; }
ret = input_register_device(gt911.input); if (ret) goto fail;
ret = gt911_ts_irq(client, >911); if(ret < 0) { goto fail; } return 0;
fail: return ret; }
int gt911_remove(struct i2c_client *client) { return 0; }
const struct i2c_device_id gt911_id_table[] = { { "goodix,gt911", 0, }, { } };
const struct of_device_id gt911_of_match_table[] = { {.compatible = "goodix,gt911" }, { } };
struct i2c_driver gt911_i2c_driver = { .driver = { .name = "gt911", .owner = THIS_MODULE, .of_match_table = gt911_of_match_table, }, .id_table = gt911_id_table, .probe = gt911_probe, .remove = gt911_remove, };
module_i2c_driver(gt911_i2c_driver); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Mouse_H");
|