Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 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> |
Bastien Nocera | 8b5a359 | 2015-07-24 09:08:53 -0700 | [diff] [blame] | 18 | #include <linux/dmi.h> |
Irina Tirdea | ec6e1b4 | 2015-12-17 15:57:34 -0800 | [diff] [blame^] | 19 | #include <linux/gpio/consumer.h> |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 20 | #include <linux/i2c.h> |
| 21 | #include <linux/input.h> |
| 22 | #include <linux/input/mt.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/delay.h> |
| 25 | #include <linux/irq.h> |
| 26 | #include <linux/interrupt.h> |
| 27 | #include <linux/slab.h> |
Aleksei Mamlin | 771d8f1 | 2015-03-06 16:43:38 -0800 | [diff] [blame] | 28 | #include <linux/acpi.h> |
| 29 | #include <linux/of.h> |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 30 | #include <asm/unaligned.h> |
| 31 | |
| 32 | struct goodix_ts_data { |
| 33 | struct i2c_client *client; |
| 34 | struct input_dev *input_dev; |
| 35 | int abs_x_max; |
| 36 | int abs_y_max; |
| 37 | unsigned int max_touch_num; |
| 38 | unsigned int int_trigger_type; |
Bastien Nocera | 8b5a359 | 2015-07-24 09:08:53 -0700 | [diff] [blame] | 39 | bool rotated_screen; |
Irina Tirdea | a779fbc | 2015-12-17 15:55:21 -0800 | [diff] [blame] | 40 | int cfg_len; |
Irina Tirdea | ec6e1b4 | 2015-12-17 15:57:34 -0800 | [diff] [blame^] | 41 | struct gpio_desc *gpiod_int; |
| 42 | struct gpio_desc *gpiod_rst; |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 43 | }; |
| 44 | |
Irina Tirdea | ec6e1b4 | 2015-12-17 15:57:34 -0800 | [diff] [blame^] | 45 | #define GOODIX_GPIO_INT_NAME "irq" |
| 46 | #define GOODIX_GPIO_RST_NAME "reset" |
| 47 | |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 48 | #define GOODIX_MAX_HEIGHT 4096 |
| 49 | #define GOODIX_MAX_WIDTH 4096 |
| 50 | #define GOODIX_INT_TRIGGER 1 |
| 51 | #define GOODIX_CONTACT_SIZE 8 |
| 52 | #define GOODIX_MAX_CONTACTS 10 |
| 53 | |
| 54 | #define GOODIX_CONFIG_MAX_LENGTH 240 |
Irina Tirdea | a779fbc | 2015-12-17 15:55:21 -0800 | [diff] [blame] | 55 | #define GOODIX_CONFIG_911_LENGTH 186 |
| 56 | #define GOODIX_CONFIG_967_LENGTH 228 |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 57 | |
| 58 | /* Register defines */ |
| 59 | #define GOODIX_READ_COOR_ADDR 0x814E |
| 60 | #define GOODIX_REG_CONFIG_DATA 0x8047 |
Irina Tirdea | e70b030 | 2015-06-09 11:04:40 -0700 | [diff] [blame] | 61 | #define GOODIX_REG_ID 0x8140 |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 62 | |
| 63 | #define RESOLUTION_LOC 1 |
Aleksei Mamlin | a7ac7c9 | 2015-03-06 16:38:16 -0800 | [diff] [blame] | 64 | #define MAX_CONTACTS_LOC 5 |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 65 | #define TRIGGER_LOC 6 |
| 66 | |
| 67 | static const unsigned long goodix_irq_flags[] = { |
| 68 | IRQ_TYPE_EDGE_RISING, |
| 69 | IRQ_TYPE_EDGE_FALLING, |
| 70 | IRQ_TYPE_LEVEL_LOW, |
| 71 | IRQ_TYPE_LEVEL_HIGH, |
| 72 | }; |
| 73 | |
Bastien Nocera | 8b5a359 | 2015-07-24 09:08:53 -0700 | [diff] [blame] | 74 | /* |
| 75 | * Those tablets have their coordinates origin at the bottom right |
| 76 | * of the tablet, as if rotated 180 degrees |
| 77 | */ |
| 78 | static const struct dmi_system_id rotated_screen[] = { |
| 79 | #if defined(CONFIG_DMI) && defined(CONFIG_X86) |
| 80 | { |
| 81 | .ident = "WinBook TW100", |
| 82 | .matches = { |
| 83 | DMI_MATCH(DMI_SYS_VENDOR, "WinBook"), |
| 84 | DMI_MATCH(DMI_PRODUCT_NAME, "TW100") |
| 85 | } |
| 86 | }, |
| 87 | { |
| 88 | .ident = "WinBook TW700", |
| 89 | .matches = { |
| 90 | DMI_MATCH(DMI_SYS_VENDOR, "WinBook"), |
| 91 | DMI_MATCH(DMI_PRODUCT_NAME, "TW700") |
| 92 | }, |
| 93 | }, |
| 94 | #endif |
| 95 | {} |
| 96 | }; |
| 97 | |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 98 | /** |
| 99 | * goodix_i2c_read - read data from a register of the i2c slave device. |
| 100 | * |
| 101 | * @client: i2c device. |
| 102 | * @reg: the register to read from. |
| 103 | * @buf: raw write data buffer. |
| 104 | * @len: length of the buffer to write |
| 105 | */ |
| 106 | static int goodix_i2c_read(struct i2c_client *client, |
Irina Tirdea | 0dfb35b | 2015-06-09 11:01:38 -0700 | [diff] [blame] | 107 | u16 reg, u8 *buf, int len) |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 108 | { |
| 109 | struct i2c_msg msgs[2]; |
| 110 | u16 wbuf = cpu_to_be16(reg); |
| 111 | int ret; |
| 112 | |
| 113 | msgs[0].flags = 0; |
| 114 | msgs[0].addr = client->addr; |
| 115 | msgs[0].len = 2; |
Irina Tirdea | 0dfb35b | 2015-06-09 11:01:38 -0700 | [diff] [blame] | 116 | msgs[0].buf = (u8 *)&wbuf; |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 117 | |
| 118 | msgs[1].flags = I2C_M_RD; |
| 119 | msgs[1].addr = client->addr; |
| 120 | msgs[1].len = len; |
| 121 | msgs[1].buf = buf; |
| 122 | |
| 123 | ret = i2c_transfer(client->adapter, msgs, 2); |
| 124 | return ret < 0 ? ret : (ret != ARRAY_SIZE(msgs) ? -EIO : 0); |
| 125 | } |
| 126 | |
Irina Tirdea | a779fbc | 2015-12-17 15:55:21 -0800 | [diff] [blame] | 127 | static int goodix_get_cfg_len(u16 id) |
| 128 | { |
| 129 | switch (id) { |
| 130 | case 911: |
| 131 | case 9271: |
| 132 | case 9110: |
| 133 | case 927: |
| 134 | case 928: |
| 135 | return GOODIX_CONFIG_911_LENGTH; |
| 136 | |
| 137 | case 912: |
| 138 | case 967: |
| 139 | return GOODIX_CONFIG_967_LENGTH; |
| 140 | |
| 141 | default: |
| 142 | return GOODIX_CONFIG_MAX_LENGTH; |
| 143 | } |
| 144 | } |
| 145 | |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 146 | static int goodix_ts_read_input_report(struct goodix_ts_data *ts, u8 *data) |
| 147 | { |
| 148 | int touch_num; |
| 149 | int error; |
| 150 | |
| 151 | error = goodix_i2c_read(ts->client, GOODIX_READ_COOR_ADDR, data, |
| 152 | GOODIX_CONTACT_SIZE + 1); |
| 153 | if (error) { |
| 154 | dev_err(&ts->client->dev, "I2C transfer error: %d\n", error); |
| 155 | return error; |
| 156 | } |
| 157 | |
Paul Cercueil | 5f6f117 | 2015-05-06 16:52:13 -0700 | [diff] [blame] | 158 | if (!(data[0] & 0x80)) |
| 159 | return -EAGAIN; |
| 160 | |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 161 | touch_num = data[0] & 0x0f; |
Aleksei Mamlin | a7ac7c9 | 2015-03-06 16:38:16 -0800 | [diff] [blame] | 162 | if (touch_num > ts->max_touch_num) |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 163 | return -EPROTO; |
| 164 | |
| 165 | if (touch_num > 1) { |
| 166 | data += 1 + GOODIX_CONTACT_SIZE; |
| 167 | error = goodix_i2c_read(ts->client, |
| 168 | GOODIX_READ_COOR_ADDR + |
| 169 | 1 + GOODIX_CONTACT_SIZE, |
| 170 | data, |
| 171 | GOODIX_CONTACT_SIZE * (touch_num - 1)); |
| 172 | if (error) |
| 173 | return error; |
| 174 | } |
| 175 | |
| 176 | return touch_num; |
| 177 | } |
| 178 | |
| 179 | static void goodix_ts_report_touch(struct goodix_ts_data *ts, u8 *coor_data) |
| 180 | { |
| 181 | int id = coor_data[0] & 0x0F; |
| 182 | int input_x = get_unaligned_le16(&coor_data[1]); |
| 183 | int input_y = get_unaligned_le16(&coor_data[3]); |
| 184 | int input_w = get_unaligned_le16(&coor_data[5]); |
| 185 | |
Bastien Nocera | 8b5a359 | 2015-07-24 09:08:53 -0700 | [diff] [blame] | 186 | if (ts->rotated_screen) { |
| 187 | input_x = ts->abs_x_max - input_x; |
| 188 | input_y = ts->abs_y_max - input_y; |
| 189 | } |
| 190 | |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 191 | input_mt_slot(ts->input_dev, id); |
| 192 | input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true); |
| 193 | input_report_abs(ts->input_dev, ABS_MT_POSITION_X, input_x); |
| 194 | input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, input_y); |
| 195 | input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w); |
| 196 | input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * goodix_process_events - Process incoming events |
| 201 | * |
| 202 | * @ts: our goodix_ts_data pointer |
| 203 | * |
| 204 | * Called when the IRQ is triggered. Read the current device state, and push |
| 205 | * the input events to the user space. |
| 206 | */ |
| 207 | static void goodix_process_events(struct goodix_ts_data *ts) |
| 208 | { |
Irina Tirdea | 0e0432f | 2015-06-09 11:03:15 -0700 | [diff] [blame] | 209 | u8 point_data[1 + GOODIX_CONTACT_SIZE * GOODIX_MAX_CONTACTS]; |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 210 | int touch_num; |
| 211 | int i; |
| 212 | |
| 213 | touch_num = goodix_ts_read_input_report(ts, point_data); |
| 214 | if (touch_num < 0) |
| 215 | return; |
| 216 | |
| 217 | for (i = 0; i < touch_num; i++) |
| 218 | goodix_ts_report_touch(ts, |
| 219 | &point_data[1 + GOODIX_CONTACT_SIZE * i]); |
| 220 | |
| 221 | input_mt_sync_frame(ts->input_dev); |
| 222 | input_sync(ts->input_dev); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * goodix_ts_irq_handler - The IRQ handler |
| 227 | * |
| 228 | * @irq: interrupt number. |
| 229 | * @dev_id: private data pointer. |
| 230 | */ |
| 231 | static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id) |
| 232 | { |
| 233 | static const u8 end_cmd[] = { |
| 234 | GOODIX_READ_COOR_ADDR >> 8, |
| 235 | GOODIX_READ_COOR_ADDR & 0xff, |
| 236 | 0 |
| 237 | }; |
| 238 | struct goodix_ts_data *ts = dev_id; |
| 239 | |
| 240 | goodix_process_events(ts); |
| 241 | |
| 242 | if (i2c_master_send(ts->client, end_cmd, sizeof(end_cmd)) < 0) |
| 243 | dev_err(&ts->client->dev, "I2C write end_cmd error\n"); |
| 244 | |
| 245 | return IRQ_HANDLED; |
| 246 | } |
| 247 | |
Irina Tirdea | ec6e1b4 | 2015-12-17 15:57:34 -0800 | [diff] [blame^] | 248 | static int goodix_int_sync(struct goodix_ts_data *ts) |
| 249 | { |
| 250 | int error; |
| 251 | |
| 252 | error = gpiod_direction_output(ts->gpiod_int, 0); |
| 253 | if (error) |
| 254 | return error; |
| 255 | |
| 256 | msleep(50); /* T5: 50ms */ |
| 257 | |
| 258 | error = gpiod_direction_input(ts->gpiod_int); |
| 259 | if (error) |
| 260 | return error; |
| 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * goodix_reset - Reset device during power on |
| 267 | * |
| 268 | * @ts: goodix_ts_data pointer |
| 269 | */ |
| 270 | static int goodix_reset(struct goodix_ts_data *ts) |
| 271 | { |
| 272 | int error; |
| 273 | |
| 274 | /* begin select I2C slave addr */ |
| 275 | error = gpiod_direction_output(ts->gpiod_rst, 0); |
| 276 | if (error) |
| 277 | return error; |
| 278 | |
| 279 | msleep(20); /* T2: > 10ms */ |
| 280 | |
| 281 | /* HIGH: 0x28/0x29, LOW: 0xBA/0xBB */ |
| 282 | error = gpiod_direction_output(ts->gpiod_int, ts->client->addr == 0x14); |
| 283 | if (error) |
| 284 | return error; |
| 285 | |
| 286 | usleep_range(100, 2000); /* T3: > 100us */ |
| 287 | |
| 288 | error = gpiod_direction_output(ts->gpiod_rst, 1); |
| 289 | if (error) |
| 290 | return error; |
| 291 | |
| 292 | usleep_range(6000, 10000); /* T4: > 5ms */ |
| 293 | |
| 294 | /* end select I2C slave addr */ |
| 295 | error = gpiod_direction_input(ts->gpiod_rst); |
| 296 | if (error) |
| 297 | return error; |
| 298 | |
| 299 | error = goodix_int_sync(ts); |
| 300 | if (error) |
| 301 | return error; |
| 302 | |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * goodix_get_gpio_config - Get GPIO config from ACPI/DT |
| 308 | * |
| 309 | * @ts: goodix_ts_data pointer |
| 310 | */ |
| 311 | static int goodix_get_gpio_config(struct goodix_ts_data *ts) |
| 312 | { |
| 313 | int error; |
| 314 | struct device *dev; |
| 315 | struct gpio_desc *gpiod; |
| 316 | |
| 317 | if (!ts->client) |
| 318 | return -EINVAL; |
| 319 | dev = &ts->client->dev; |
| 320 | |
| 321 | /* Get the interrupt GPIO pin number */ |
| 322 | gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_INT_NAME, GPIOD_IN); |
| 323 | if (IS_ERR(gpiod)) { |
| 324 | error = PTR_ERR(gpiod); |
| 325 | if (error != -EPROBE_DEFER) |
| 326 | dev_dbg(dev, "Failed to get %s GPIO: %d\n", |
| 327 | GOODIX_GPIO_INT_NAME, error); |
| 328 | return error; |
| 329 | } |
| 330 | |
| 331 | ts->gpiod_int = gpiod; |
| 332 | |
| 333 | /* Get the reset line GPIO pin number */ |
| 334 | gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_RST_NAME, GPIOD_IN); |
| 335 | if (IS_ERR(gpiod)) { |
| 336 | error = PTR_ERR(gpiod); |
| 337 | if (error != -EPROBE_DEFER) |
| 338 | dev_dbg(dev, "Failed to get %s GPIO: %d\n", |
| 339 | GOODIX_GPIO_RST_NAME, error); |
| 340 | return error; |
| 341 | } |
| 342 | |
| 343 | ts->gpiod_rst = gpiod; |
| 344 | |
| 345 | return 0; |
| 346 | } |
| 347 | |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 348 | /** |
| 349 | * goodix_read_config - Read the embedded configuration of the panel |
| 350 | * |
| 351 | * @ts: our goodix_ts_data pointer |
| 352 | * |
| 353 | * Must be called during probe |
| 354 | */ |
| 355 | static void goodix_read_config(struct goodix_ts_data *ts) |
| 356 | { |
| 357 | u8 config[GOODIX_CONFIG_MAX_LENGTH]; |
| 358 | int error; |
| 359 | |
| 360 | error = goodix_i2c_read(ts->client, GOODIX_REG_CONFIG_DATA, |
Irina Tirdea | a779fbc | 2015-12-17 15:55:21 -0800 | [diff] [blame] | 361 | config, ts->cfg_len); |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 362 | if (error) { |
| 363 | dev_warn(&ts->client->dev, |
| 364 | "Error reading config (%d), using defaults\n", |
| 365 | error); |
| 366 | ts->abs_x_max = GOODIX_MAX_WIDTH; |
| 367 | ts->abs_y_max = GOODIX_MAX_HEIGHT; |
| 368 | ts->int_trigger_type = GOODIX_INT_TRIGGER; |
Aleksei Mamlin | a7ac7c9 | 2015-03-06 16:38:16 -0800 | [diff] [blame] | 369 | ts->max_touch_num = GOODIX_MAX_CONTACTS; |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 370 | return; |
| 371 | } |
| 372 | |
| 373 | ts->abs_x_max = get_unaligned_le16(&config[RESOLUTION_LOC]); |
| 374 | ts->abs_y_max = get_unaligned_le16(&config[RESOLUTION_LOC + 2]); |
Aleksei Mamlin | a7ac7c9 | 2015-03-06 16:38:16 -0800 | [diff] [blame] | 375 | ts->int_trigger_type = config[TRIGGER_LOC] & 0x03; |
| 376 | ts->max_touch_num = config[MAX_CONTACTS_LOC] & 0x0f; |
| 377 | if (!ts->abs_x_max || !ts->abs_y_max || !ts->max_touch_num) { |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 378 | dev_err(&ts->client->dev, |
| 379 | "Invalid config, using defaults\n"); |
| 380 | ts->abs_x_max = GOODIX_MAX_WIDTH; |
| 381 | ts->abs_y_max = GOODIX_MAX_HEIGHT; |
Aleksei Mamlin | a7ac7c9 | 2015-03-06 16:38:16 -0800 | [diff] [blame] | 382 | ts->max_touch_num = GOODIX_MAX_CONTACTS; |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 383 | } |
Bastien Nocera | 8b5a359 | 2015-07-24 09:08:53 -0700 | [diff] [blame] | 384 | |
| 385 | ts->rotated_screen = dmi_check_system(rotated_screen); |
| 386 | if (ts->rotated_screen) |
| 387 | dev_dbg(&ts->client->dev, |
| 388 | "Applying '180 degrees rotated screen' quirk\n"); |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 391 | /** |
| 392 | * goodix_read_version - Read goodix touchscreen version |
| 393 | * |
| 394 | * @client: the i2c client |
| 395 | * @version: output buffer containing the version on success |
Irina Tirdea | e70b030 | 2015-06-09 11:04:40 -0700 | [diff] [blame] | 396 | * @id: output buffer containing the id on success |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 397 | */ |
Irina Tirdea | e70b030 | 2015-06-09 11:04:40 -0700 | [diff] [blame] | 398 | static int goodix_read_version(struct i2c_client *client, u16 *version, u16 *id) |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 399 | { |
| 400 | int error; |
| 401 | u8 buf[6]; |
Irina Tirdea | e70b030 | 2015-06-09 11:04:40 -0700 | [diff] [blame] | 402 | char id_str[5]; |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 403 | |
Irina Tirdea | e70b030 | 2015-06-09 11:04:40 -0700 | [diff] [blame] | 404 | error = goodix_i2c_read(client, GOODIX_REG_ID, buf, sizeof(buf)); |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 405 | if (error) { |
| 406 | dev_err(&client->dev, "read version failed: %d\n", error); |
| 407 | return error; |
| 408 | } |
| 409 | |
Irina Tirdea | e70b030 | 2015-06-09 11:04:40 -0700 | [diff] [blame] | 410 | memcpy(id_str, buf, 4); |
| 411 | id_str[4] = 0; |
| 412 | if (kstrtou16(id_str, 10, id)) |
| 413 | *id = 0x1001; |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 414 | |
Irina Tirdea | e70b030 | 2015-06-09 11:04:40 -0700 | [diff] [blame] | 415 | *version = get_unaligned_le16(&buf[4]); |
| 416 | |
| 417 | dev_info(&client->dev, "ID %d, version: %04x\n", *id, *version); |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 418 | |
| 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * goodix_i2c_test - I2C test function to check if the device answers. |
| 424 | * |
| 425 | * @client: the i2c client |
| 426 | */ |
| 427 | static int goodix_i2c_test(struct i2c_client *client) |
| 428 | { |
| 429 | int retry = 0; |
| 430 | int error; |
| 431 | u8 test; |
| 432 | |
| 433 | while (retry++ < 2) { |
| 434 | error = goodix_i2c_read(client, GOODIX_REG_CONFIG_DATA, |
| 435 | &test, 1); |
| 436 | if (!error) |
| 437 | return 0; |
| 438 | |
| 439 | dev_err(&client->dev, "i2c test failed attempt %d: %d\n", |
| 440 | retry, error); |
| 441 | msleep(20); |
| 442 | } |
| 443 | |
| 444 | return error; |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * goodix_request_input_dev - Allocate, populate and register the input device |
| 449 | * |
| 450 | * @ts: our goodix_ts_data pointer |
Irina Tirdea | e70b030 | 2015-06-09 11:04:40 -0700 | [diff] [blame] | 451 | * @version: device firmware version |
| 452 | * @id: device ID |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 453 | * |
| 454 | * Must be called during probe |
| 455 | */ |
Irina Tirdea | e70b030 | 2015-06-09 11:04:40 -0700 | [diff] [blame] | 456 | static int goodix_request_input_dev(struct goodix_ts_data *ts, u16 version, |
| 457 | u16 id) |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 458 | { |
| 459 | int error; |
| 460 | |
| 461 | ts->input_dev = devm_input_allocate_device(&ts->client->dev); |
| 462 | if (!ts->input_dev) { |
| 463 | dev_err(&ts->client->dev, "Failed to allocate input device."); |
| 464 | return -ENOMEM; |
| 465 | } |
| 466 | |
Irina Tirdea | 0dfb35b | 2015-06-09 11:01:38 -0700 | [diff] [blame] | 467 | input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X, |
| 468 | 0, ts->abs_x_max, 0, 0); |
| 469 | input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y, |
| 470 | 0, ts->abs_y_max, 0, 0); |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 471 | input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0); |
| 472 | input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0); |
| 473 | |
Aleksei Mamlin | a7ac7c9 | 2015-03-06 16:38:16 -0800 | [diff] [blame] | 474 | input_mt_init_slots(ts->input_dev, ts->max_touch_num, |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 475 | INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED); |
| 476 | |
| 477 | ts->input_dev->name = "Goodix Capacitive TouchScreen"; |
| 478 | ts->input_dev->phys = "input/ts"; |
| 479 | ts->input_dev->id.bustype = BUS_I2C; |
| 480 | ts->input_dev->id.vendor = 0x0416; |
Irina Tirdea | e70b030 | 2015-06-09 11:04:40 -0700 | [diff] [blame] | 481 | ts->input_dev->id.product = id; |
| 482 | ts->input_dev->id.version = version; |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 483 | |
| 484 | error = input_register_device(ts->input_dev); |
| 485 | if (error) { |
| 486 | dev_err(&ts->client->dev, |
| 487 | "Failed to register input device: %d", error); |
| 488 | return error; |
| 489 | } |
| 490 | |
| 491 | return 0; |
| 492 | } |
| 493 | |
| 494 | static int goodix_ts_probe(struct i2c_client *client, |
| 495 | const struct i2c_device_id *id) |
| 496 | { |
| 497 | struct goodix_ts_data *ts; |
| 498 | unsigned long irq_flags; |
| 499 | int error; |
Irina Tirdea | e70b030 | 2015-06-09 11:04:40 -0700 | [diff] [blame] | 500 | u16 version_info, id_info; |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 501 | |
| 502 | dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr); |
| 503 | |
| 504 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { |
| 505 | dev_err(&client->dev, "I2C check functionality failed.\n"); |
| 506 | return -ENXIO; |
| 507 | } |
| 508 | |
| 509 | ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL); |
| 510 | if (!ts) |
| 511 | return -ENOMEM; |
| 512 | |
| 513 | ts->client = client; |
| 514 | i2c_set_clientdata(client, ts); |
| 515 | |
Irina Tirdea | ec6e1b4 | 2015-12-17 15:57:34 -0800 | [diff] [blame^] | 516 | error = goodix_get_gpio_config(ts); |
| 517 | if (error) |
| 518 | return error; |
| 519 | |
| 520 | if (ts->gpiod_int && ts->gpiod_rst) { |
| 521 | /* reset the controller */ |
| 522 | error = goodix_reset(ts); |
| 523 | if (error) { |
| 524 | dev_err(&client->dev, "Controller reset failed.\n"); |
| 525 | return error; |
| 526 | } |
| 527 | } |
| 528 | |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 529 | error = goodix_i2c_test(client); |
| 530 | if (error) { |
| 531 | dev_err(&client->dev, "I2C communication failure: %d\n", error); |
| 532 | return error; |
| 533 | } |
| 534 | |
Irina Tirdea | e70b030 | 2015-06-09 11:04:40 -0700 | [diff] [blame] | 535 | error = goodix_read_version(client, &version_info, &id_info); |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 536 | if (error) { |
| 537 | dev_err(&client->dev, "Read version failed.\n"); |
| 538 | return error; |
| 539 | } |
| 540 | |
Irina Tirdea | a779fbc | 2015-12-17 15:55:21 -0800 | [diff] [blame] | 541 | ts->cfg_len = goodix_get_cfg_len(id_info); |
| 542 | |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 543 | goodix_read_config(ts); |
| 544 | |
Irina Tirdea | e70b030 | 2015-06-09 11:04:40 -0700 | [diff] [blame] | 545 | error = goodix_request_input_dev(ts, version_info, id_info); |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 546 | if (error) |
| 547 | return error; |
| 548 | |
| 549 | irq_flags = goodix_irq_flags[ts->int_trigger_type] | IRQF_ONESHOT; |
| 550 | error = devm_request_threaded_irq(&ts->client->dev, client->irq, |
| 551 | NULL, goodix_ts_irq_handler, |
| 552 | irq_flags, client->name, ts); |
| 553 | if (error) { |
| 554 | dev_err(&client->dev, "request IRQ failed: %d\n", error); |
| 555 | return error; |
| 556 | } |
| 557 | |
| 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | static const struct i2c_device_id goodix_ts_id[] = { |
| 562 | { "GDIX1001:00", 0 }, |
| 563 | { } |
| 564 | }; |
Javier Martinez Canillas | 2e9e910 | 2015-07-30 10:38:52 -0700 | [diff] [blame] | 565 | MODULE_DEVICE_TABLE(i2c, goodix_ts_id); |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 566 | |
Aleksei Mamlin | 771d8f1 | 2015-03-06 16:43:38 -0800 | [diff] [blame] | 567 | #ifdef CONFIG_ACPI |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 568 | static const struct acpi_device_id goodix_acpi_match[] = { |
| 569 | { "GDIX1001", 0 }, |
| 570 | { } |
| 571 | }; |
| 572 | MODULE_DEVICE_TABLE(acpi, goodix_acpi_match); |
Aleksei Mamlin | 771d8f1 | 2015-03-06 16:43:38 -0800 | [diff] [blame] | 573 | #endif |
| 574 | |
| 575 | #ifdef CONFIG_OF |
| 576 | static const struct of_device_id goodix_of_match[] = { |
| 577 | { .compatible = "goodix,gt911" }, |
| 578 | { .compatible = "goodix,gt9110" }, |
| 579 | { .compatible = "goodix,gt912" }, |
| 580 | { .compatible = "goodix,gt927" }, |
| 581 | { .compatible = "goodix,gt9271" }, |
| 582 | { .compatible = "goodix,gt928" }, |
| 583 | { .compatible = "goodix,gt967" }, |
| 584 | { } |
| 585 | }; |
| 586 | MODULE_DEVICE_TABLE(of, goodix_of_match); |
| 587 | #endif |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 588 | |
| 589 | static struct i2c_driver goodix_ts_driver = { |
| 590 | .probe = goodix_ts_probe, |
| 591 | .id_table = goodix_ts_id, |
| 592 | .driver = { |
| 593 | .name = "Goodix-TS", |
Aleksei Mamlin | 771d8f1 | 2015-03-06 16:43:38 -0800 | [diff] [blame] | 594 | .acpi_match_table = ACPI_PTR(goodix_acpi_match), |
| 595 | .of_match_table = of_match_ptr(goodix_of_match), |
Bastien Nocera | ca96ea8 | 2014-10-31 09:26:16 -0700 | [diff] [blame] | 596 | }, |
| 597 | }; |
| 598 | module_i2c_driver(goodix_ts_driver); |
| 599 | |
| 600 | MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>"); |
| 601 | MODULE_AUTHOR("Bastien Nocera <hadess@hadess.net>"); |
| 602 | MODULE_DESCRIPTION("Goodix touchscreen driver"); |
| 603 | MODULE_LICENSE("GPL v2"); |